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

@@ -31,6 +31,7 @@ import net.i2p.util.KeyRing;
import net.i2p.util.LogManager;
//import net.i2p.util.PooledRandomSource;
import net.i2p.util.RandomSource;
import net.i2p.util.SecureDirectory;
/**
* <p>Provide a base scope for accessing singletons that I2P exposes. Rather than
@@ -217,7 +218,7 @@ public class I2PAppContext {
// config defaults to base
s = getProperty("i2p.dir.config");
if (s != null) {
_configDir = new File(s);
_configDir = new SecureDirectory(s);
if (!_configDir.exists())
_configDir.mkdir();
} else {
@@ -226,7 +227,7 @@ public class I2PAppContext {
// router defaults to config
s = getProperty("i2p.dir.router");
if (s != null) {
_routerDir = new File(s);
_routerDir = new SecureDirectory(s);
if (!_routerDir.exists())
_routerDir.mkdir();
} else {
@@ -240,7 +241,7 @@ public class I2PAppContext {
// these all default to router
s = getProperty("i2p.dir.log");
if (s != null) {
_logDir = new File(s);
_logDir = new SecureDirectory(s);
if (!_logDir.exists())
_logDir.mkdir();
} else {
@@ -248,7 +249,7 @@ public class I2PAppContext {
}
s = getProperty("i2p.dir.app");
if (s != null) {
_appDir = new File(s);
_appDir = new SecureDirectory(s);
if (!_appDir.exists())
_appDir.mkdir();
} else {
@@ -278,14 +279,14 @@ public class I2PAppContext {
String d = getProperty("i2p.dir.temp", System.getProperty("java.io.tmpdir"));
// our random() probably isn't warmed up yet
String f = "i2p-" + Math.abs((new java.util.Random()).nextInt()) + ".tmp";
_tmpDir = new File(d, f);
_tmpDir = new SecureDirectory(d, f);
if (_tmpDir.exists()) {
// good or bad ?
} else if (_tmpDir.mkdir()) {
_tmpDir.deleteOnExit();
} else {
System.err.println("Could not create temp dir " + _tmpDir.getAbsolutePath());
_tmpDir = new File(_routerDir, "tmp");
_tmpDir = new SecureDirectory(_routerDir, "tmp");
_tmpDir.mkdir();
}
}

View File

@@ -17,7 +17,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -42,6 +41,7 @@ import net.i2p.util.ByteCache;
import net.i2p.util.OrderedProperties;
import net.i2p.util.ReusableGZIPInputStream;
import net.i2p.util.ReusableGZIPOutputStream;
import net.i2p.util.SecureFileOutputStream;
/**
* Defines some simple IO routines for dealing with marshalling data structures
@@ -304,11 +304,12 @@ public class DataHelper {
/**
* Writes the props to the file, unsorted (unless props is an OrderedProperties)
* Note that this does not escape the \r or \n that are unescaped in loadProps() above.
* As of 0.8.1, file will be mode 600.
*/
public static void storeProps(Properties props, File file) throws IOException {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8")));
out.println("# NOTE: This I2P config file must use UTF-8 encoding");
for (Iterator iter = props.keySet().iterator(); iter.hasNext(); ) {
String name = (String)iter.next();

View File

@@ -11,7 +11,6 @@ package net.i2p.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -158,7 +157,8 @@ class LogWriter implements Runnable {
File parent = f.getParentFile();
if (parent != null) {
if (!parent.exists()) {
boolean ok = parent.mkdirs();
File sd = new SecureDirectory(parent.getAbsolutePath());
boolean ok = sd.mkdirs();
if (!ok) {
System.err.println("Unable to create the parent directory: " + parent.getAbsolutePath());
//System.exit(0);
@@ -171,7 +171,7 @@ class LogWriter implements Runnable {
}
closeFile();
try {
_currentOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
_currentOut = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(f), "UTF-8"));
} catch (IOException ioe) {
System.err.println("Error rotating into [" + f.getAbsolutePath() + "]" + ioe);
}

View File

@@ -145,7 +145,7 @@ public class RandomSource extends SecureRandom implements EntropyHarvester {
File f = new File(I2PAppContext.getGlobalContext().getConfigDir(), SEEDFILE);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos = new SecureFileOutputStream(f);
fos.write(buf);
} catch (IOException ioe) {
// ignore

View File

@@ -38,6 +38,18 @@ public class SecureDirectory extends File {
return rv;
}
/**
* Sets directory to mode 700 if the directory is created
* Does NOT change the mode of other created directories
*/
@Override
public boolean mkdirs() {
boolean rv = super.mkdirs();
if (rv)
setPerms();
return rv;
}
/**
* Tries to set the permissions to 700,
* ignores errors