* 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
((dest.length() == MIN_DEST_LENGTH && dest.endsWith("AA")) ||
(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
;
}
@ -253,6 +255,7 @@ public class AddressBook {
try {
ConfigParser.write(this.addresses, file);
} catch (IOException exp) {
System.err.println("Error writing addressbook " + file.getAbsolutePath() + " : " + exp.toString());
}
}
}

View File

@ -269,6 +269,8 @@ public class ConfigParser {
/**
* 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.
* Write to a temp file in the same directory and then rename, to not corrupt
* simultaneous accesses by the router.
*
* @param map
* A Map to write to file.
@ -278,9 +280,18 @@ public class ConfigParser {
* if file cannot be written to.
*/
public static void write(Map map, File file) throws IOException {
File tmp = File.createTempFile("hoststxt-", ".tmp", file.getAbsoluteFile().getParentFile());
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 contents of List list to BufferedReader output. Output is written