Set permissions on directories and files when they are created

This commit is contained in:
zzz
2010-07-06 15:22:48 +00:00
parent 50bda941ad
commit a1524241cb
27 changed files with 105 additions and 63 deletions

View File

@@ -27,6 +27,8 @@ import net.i2p.data.SigningPrivateKey;
import net.i2p.data.SigningPublicKey;
import net.i2p.util.Clock;
import net.i2p.util.Log;
import net.i2p.util.SecureDirectory;
import net.i2p.util.SecureFileOutputStream;
/**
* Maintain all of the key pairs for the router.
@@ -142,7 +144,7 @@ public class KeyManager {
}
public void runJob() {
String keyDir = getContext().getProperty(PROP_KEYDIR, DEFAULT_KEYDIR);
File dir = new File(getContext().getRouterDir(), keyDir);
File dir = new SecureDirectory(getContext().getRouterDir(), keyDir);
if (!dir.exists())
dir.mkdirs();
if (dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite()) {
@@ -219,7 +221,7 @@ public class KeyManager {
FileInputStream in = null;
try {
if (exists) {
out = new FileOutputStream(keyFile);
out = new SecureFileOutputStream(keyFile);
structure.writeBytes(out);
return structure;
} else {

View File

@@ -46,6 +46,7 @@ import net.i2p.util.FileUtil;
import net.i2p.util.I2PAppThread;
import net.i2p.util.I2PThread;
import net.i2p.util.Log;
import net.i2p.util.SecureFileOutputStream;
import net.i2p.util.SimpleScheduler;
import net.i2p.util.SimpleTimer;
@@ -305,6 +306,7 @@ public class Router {
public void setHigherVersionSeen(boolean seen) { _higherVersionSeen = seen; }
public long getWhenStarted() { return _started; }
/** wall clock uptime */
public long getUptime() {
if ( (_context == null) || (_context.clock() == null) ) return 1; // racing on startup
@@ -1053,11 +1055,12 @@ public class Router {
* this does escape the \r or \n that are unescaped in DataHelper.loadProps().
* Note that the escaping of \r or \n was probably a mistake and should be taken out.
*
* FIXME Synchronize!!
*/
public boolean saveConfig() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(_configFilename);
fos = new SecureFileOutputStream(_configFilename);
StringBuilder buf = new StringBuilder(8*1024);
buf.append("# NOTE: This I2P config file must use UTF-8 encoding\n");
synchronized (_config) {
@@ -1541,7 +1544,7 @@ private static class PersistRouterInfoJob extends JobImpl {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(infoFile);
fos = new SecureFileOutputStream(infoFile);
info.writeBytes(fos);
} catch (DataFormatException dfe) {
_log.error("Error rebuilding the router information", dfe);

View File

@@ -29,6 +29,8 @@ import net.i2p.router.RouterContext;
import net.i2p.router.networkdb.reseed.ReseedChecker;
import net.i2p.util.I2PThread;
import net.i2p.util.Log;
import net.i2p.util.SecureDirectory;
import net.i2p.util.SecureFileOutputStream;
/**
* Write out keys to disk when we get them and periodically read ones we don't know
@@ -288,7 +290,7 @@ class PersistentDataStore extends TransientDataStore {
long dataPublishDate = getPublishDate(data);
if (dbFile.lastModified() < dataPublishDate) {
// our filesystem is out of date, lets replace it
fos = new FileOutputStream(dbFile);
fos = new SecureFileOutputStream(dbFile);
try {
data.writeBytes(fos);
fos.close();
@@ -440,7 +442,7 @@ class PersistentDataStore extends TransientDataStore {
private File getDbDir() throws IOException {
File f = new File(_context.getRouterDir(), _dbDir);
File f = new SecureDirectory(_context.getRouterDir(), _dbDir);
if (!f.exists()) {
boolean created = f.mkdirs();
if (!created)

View File

@@ -17,6 +17,8 @@ import net.i2p.router.RouterContext;
import net.i2p.util.EepGet;
import net.i2p.util.I2PAppThread;
import net.i2p.util.Log;
import net.i2p.util.SecureDirectory;
import net.i2p.util.SecureFileOutputStream;
import net.i2p.util.SSLEepGet;
import net.i2p.util.Translate;
@@ -261,11 +263,11 @@ public class Reseeder {
private void writeSeed(String name, byte data[]) throws Exception {
String dirName = "netDb"; // _context.getProperty("router.networkDatabase.dbDir", "netDb");
File netDbDir = new File(_context.getRouterDir(), dirName);
File netDbDir = new SecureDirectory(_context.getRouterDir(), dirName);
if (!netDbDir.exists()) {
boolean ok = netDbDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(new File(netDbDir, "routerInfo-" + name + ".dat"));
FileOutputStream fos = new SecureFileOutputStream(new File(netDbDir, "routerInfo-" + name + ".dat"));
fos.write(data);
fos.close();
}

View File

@@ -3,7 +3,6 @@ package net.i2p.router.peermanager;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
@@ -19,6 +18,8 @@ import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.router.RouterContext;
import net.i2p.util.Log;
import net.i2p.util.SecureDirectory;
import net.i2p.util.SecureFileOutputStream;
class ProfilePersistenceHelper {
private Log _log;
@@ -61,7 +62,7 @@ class ProfilePersistenceHelper {
long before = _context.clock().now();
OutputStream fos = null;
try {
fos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f)));
fos = new BufferedOutputStream(new GZIPOutputStream(new SecureFileOutputStream(f)));
writeProfile(profile, fos);
} catch (IOException ioe) {
_log.error("Error writing profile to " + f);
@@ -310,7 +311,7 @@ class ProfilePersistenceHelper {
private File getProfileDir() {
if (_profileDir == null) {
String dir = _context.getProperty(PROP_PEER_PROFILE_DIR, DEFAULT_PEER_PROFILE_DIR);
_profileDir = new File(_context.getRouterDir(), dir);
_profileDir = new SecureDirectory(_context.getRouterDir(), dir);
}
return _profileDir;
}

View File

@@ -11,6 +11,7 @@ import java.util.Properties;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.router.RouterContext;
import net.i2p.util.SecureFileOutputStream;
/**
@@ -191,7 +192,7 @@ public class ClientAppConfig {
File cfgFile = configFile(ctx);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(cfgFile);
fos = new SecureFileOutputStream(cfgFile);
StringBuilder buf = new StringBuilder(2048);
for(int i = 0; i < apps.size(); i++) {
ClientAppConfig app = (ClientAppConfig) apps.get(i);

View File

@@ -27,6 +27,7 @@ import net.i2p.router.JobImpl;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.util.Log;
import net.i2p.util.SecureFileOutputStream;
public class CreateRouterInfoJob extends JobImpl {
private static Log _log = new Log(CreateRouterInfoJob.class);
@@ -80,12 +81,12 @@ public class CreateRouterInfoJob extends JobImpl {
String infoFilename = getContext().getProperty(Router.PROP_INFO_FILENAME, Router.PROP_INFO_FILENAME_DEFAULT);
File ifile = new File(getContext().getRouterDir(), infoFilename);
fos1 = new FileOutputStream(ifile);
fos1 = new SecureFileOutputStream(ifile);
info.writeBytes(fos1);
String keyFilename = getContext().getProperty(Router.PROP_KEYS_FILENAME, Router.PROP_KEYS_FILENAME_DEFAULT);
File kfile = new File(getContext().getRouterDir(), keyFilename);
fos2 = new FileOutputStream(kfile);
fos2 = new SecureFileOutputStream(kfile);
privkey.writeBytes(fos2);
signingPrivKey.writeBytes(fos2);
pubkey.writeBytes(fos2);

View File

@@ -26,6 +26,7 @@ import net.i2p.router.JobImpl;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.util.Log;
import net.i2p.util.SecureFileOutputStream;
/**
* This used be called from StartAcceptingClientsJob but is now disabled.
@@ -135,7 +136,7 @@ public class RebuildRouterInfoJob extends JobImpl {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(infoFile);
fos = new SecureFileOutputStream(infoFile);
info.writeBytes(fos);
} catch (DataFormatException dfe) {
_log.log(Log.CRIT, "Error rebuilding the router information", dfe);

View File

@@ -11,6 +11,8 @@ import java.io.PrintWriter;
import java.util.Properties;
import net.i2p.data.DataHelper;
import net.i2p.util.SecureDirectory;
import net.i2p.util.SecureFileOutputStream;
/**
* Get a working directory for i2p.
@@ -64,19 +66,19 @@ public class WorkingDir {
boolean isWindows = System.getProperty("os.name").startsWith("Win");
File dirf = null;
if (dir != null) {
dirf = new File(dir);
dirf = new SecureDirectory(dir);
} else {
String home = System.getProperty("user.home");
if (isWindows) {
String appdata = System.getenv("APPDATA");
if (appdata != null)
home = appdata;
dirf = new File(home, WORKING_DIR_DEFAULT_WINDOWS);
dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT_WINDOWS);
} else {
if (DAEMON_USER.equals(System.getProperty("user.name")))
dirf = new File(home, WORKING_DIR_DEFAULT_DAEMON);
dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT_DAEMON);
else
dirf = new File(home, WORKING_DIR_DEFAULT);
dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT);
}
}
@@ -143,7 +145,7 @@ public class WorkingDir {
// this one must be after MIGRATE_BASE
success &= migrateJettyXml(oldDirf, dirf);
success &= migrateClientsConfig(oldDirf, dirf);
success &= copy(new File(oldDirf, "docs/news.xml"), new File(dirf, "docs"));
success &= copy(new File(oldDirf, "docs/news.xml"), new SecureDirectory(dirf, "docs"));
// Report success or failure
if (success) {
@@ -197,7 +199,7 @@ public class WorkingDir {
PrintWriter out = null;
try {
in = new FileInputStream(oldFile);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF-8")));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(newFile), "UTF-8")));
out.println("# Modified by I2P User dir migration script");
String s = null;
boolean isDaemon = DAEMON_USER.equals(System.getProperty("user.name"));
@@ -240,7 +242,7 @@ public class WorkingDir {
PrintWriter out = null;
try {
in = new FileInputStream(oldFile);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF-8")));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(newFile), "UTF-8")));
String s = null;
while ((s = DataHelper.readLine(in)) != null) {
if (s.indexOf("./eepsite/") >= 0) {
@@ -270,7 +272,7 @@ public class WorkingDir {
* @param targetDir the directory to copy to, will be created if it doesn't exist
* @return true for success OR if src does not exist
*/
public static final boolean copy(File src, File targetDir) {
private static boolean copy(File src, File targetDir) {
if (!src.exists())
return true;
if (!targetDir.exists()) {
@@ -280,7 +282,8 @@ public class WorkingDir {
}
System.err.println("Created " + targetDir.getPath());
}
File targetFile = new File(targetDir, src.getName());
// SecureDirectory is a File so this works for non-directories too
File targetFile = new SecureDirectory(targetDir, src.getName());
if (!src.isDirectory())
return copyFile(src, targetFile);
File children[] = src.listFiles();
@@ -305,10 +308,10 @@ public class WorkingDir {
/**
* @param src not a directory, must exist
* @param dst not a directory, will be overwritten if existing
* @param dst not a directory, will be overwritten if existing, will be mode 600
* @return true if it was copied successfully
*/
public static boolean copyFile(File src, File dst) {
private static boolean copyFile(File src, File dst) {
if (!src.exists()) return false;
boolean rv = true;
@@ -317,7 +320,7 @@ public class WorkingDir {
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dst);
out = new SecureFileOutputStream(dst);
int read = 0;
while ( (read = in.read(buf)) != -1)