* Addressbook:

- Try to save files safely
      - Catch bad B64 lengths
This commit is contained in:
zzz
2010-11-14 15:05:24 +00:00
parent c29a275969
commit f3307d6508
2 changed files with 15 additions and 1 deletions

View File

@ -196,6 +196,8 @@ public class AddressBook {
// null cert ends with AAAA but other zero-length certs would be AA // null cert ends with AAAA but other zero-length certs would be AA
((dest.length() == MIN_DEST_LENGTH && dest.endsWith("AA")) || ((dest.length() == MIN_DEST_LENGTH && dest.endsWith("AA")) ||
(dest.length() > MIN_DEST_LENGTH && dest.length() <= MAX_DEST_LENGTH)) && (dest.length() > MIN_DEST_LENGTH && dest.length() <= MAX_DEST_LENGTH)) &&
// B64 comes in groups of 2, 3, or 4 chars, but never 1
((dest.length() % 4) != 1) &&
dest.replaceAll("[a-zA-Z0-9~-]", "").length() == 0 dest.replaceAll("[a-zA-Z0-9~-]", "").length() == 0
; ;
} }
@ -253,6 +255,7 @@ public class AddressBook {
try { try {
ConfigParser.write(this.addresses, file); ConfigParser.write(this.addresses, file);
} catch (IOException exp) { } catch (IOException exp) {
System.err.println("Error writing addressbook " + file.getAbsolutePath() + " : " + exp.toString());
} }
} }
} }

View File

@ -269,7 +269,9 @@ public class ConfigParser {
/** /**
* Write contents of Map map to the File file. Output is written * Write contents of Map map to the File file. Output is written
* with one key, value pair on each line, in the format: key=value. * with one key, value pair on each line, in the format: key=value.
* * Write to a temp file in the same directory and then rename, to not corrupt
* simultaneous accesses by the router.
*
* @param map * @param map
* A Map to write to file. * A Map to write to file.
* @param file * @param file
@ -278,8 +280,17 @@ public class ConfigParser {
* if file cannot be written to. * if file cannot be written to.
*/ */
public static void write(Map map, File file) throws IOException { public static void write(Map map, File file) throws IOException {
File tmp = File.createTempFile("hoststxt-", ".tmp", file.getAbsoluteFile().getParentFile());
ConfigParser ConfigParser
.write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(tmp), "UTF-8")));
boolean success = tmp.renameTo(file);
if (!success) {
// hmm, that didn't work, try it the old way
System.out.println("Warning: addressbook rename fail from " + tmp + " to " + file);
tmp.delete();
ConfigParser
.write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8"))); .write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8")));
}
} }
/** /**