Search in sources :

Example 1 with ShellCommand

use of net.i2p.util.ShellCommand in project i2p.i2p by i2p.

the class ThreadDump method dump.

/**
 *  Signal the wrapper to asynchronously dump threads to wrapper.log.
 *  It waits for the signal to complete (which should be fast)
 *  but does not wait for the dump itself.
 *
 *  @param secondsToWait if <= 0, don't wait
 *  @return success, false if windows or no wrapper, true if secondsToWait <= 0,
 *                         false if timed out, dump result otherwise
 */
public static boolean dump(I2PAppContext context, int secondsToWait) {
    if (SystemVersion.isWindows() || !context.hasWrapper())
        return false;
    ShellCommand sc = new ShellCommand();
    File i2pr = new File(context.getBaseDir(), "i2prouter");
    String[] args = new String[2];
    args[0] = i2pr.getAbsolutePath();
    args[1] = "dump";
    boolean success = sc.executeSilentAndWaitTimed(args, secondsToWait);
    if (secondsToWait <= 0)
        success = true;
    if (success) {
        Log log = context.logManager().getLog(ThreadDump.class);
        File f = new File(context.getConfigDir(), "wrapper.log");
        String loc = f.exists() ? f.getAbsolutePath() : "wrapper.log";
        log.log(Log.CRIT, "Threads dumped to " + loc);
    }
    return success;
}
Also used : Log(net.i2p.util.Log) ShellCommand(net.i2p.util.ShellCommand) File(java.io.File)

Example 2 with ShellCommand

use of net.i2p.util.ShellCommand in project i2p.i2p by i2p.

the class KeyStoreUtil method createKeysCLI.

/**
 *  OLD way - keytool
 *  Create a keypair and store it in the keystore at ks, creating it if necessary.
 *
 *  Warning, may take a long time.
 *
 *  @param ks path to the keystore
 *  @param ksPW the keystore password
 *  @param alias the name of the key
 *  @param cname e.g. randomstuff.console.i2p.net
 *  @param ou e.g. console
 *  @param validDays e.g. 3652 (10 years)
 *  @param keyAlg e.g. DSA , RSA, EC
 *  @param keySize e.g. 1024
 *  @param keyPW the key password, must be at least 6 characters
 *
 *  @return success
 *  @since 0.8.3, consolidated from RouterConsoleRunner and SSLClientListenerRunner in 0.9.9
 */
private static boolean createKeysCLI(File ks, String ksPW, String alias, String cname, String ou, int validDays, String keyAlg, int keySize, String keyPW) {
    if (ks.exists()) {
        try {
            if (getCert(ks, ksPW, alias) != null) {
                error("Not overwriting key " + alias + ", already exists in " + ks, null);
                return false;
            }
        } catch (IOException e) {
            error("Not overwriting key \"" + alias + "\", already exists in " + ks, e);
            return false;
        } catch (GeneralSecurityException e) {
            error("Not overwriting key \"" + alias + "\", already exists in " + ks, e);
            return false;
        }
    } else {
        File dir = ks.getParentFile();
        if (dir != null && !dir.exists()) {
            File sdir = new SecureDirectory(dir.getAbsolutePath());
            if (!sdir.mkdir()) {
                error("Can't create directory " + dir, null);
                return false;
            }
        }
    }
    String keytool = (new File(System.getProperty("java.home"), "bin/keytool")).getAbsolutePath();
    List<String> a = new ArrayList<String>(32);
    a.add(keytool);
    // -genkeypair preferred in newer keytools, but this works with more
    a.add("-genkey");
    // a.add("-v");         // verbose, gives you a stack trace on exception
    a.add("-storetype");
    a.add(KeyStore.getDefaultType());
    a.add("-keystore");
    a.add(ks.getAbsolutePath());
    a.add("-storepass");
    a.add(ksPW);
    a.add("-alias");
    a.add(alias);
    a.add("-dname");
    a.add("CN=" + cname + ",OU=" + ou + ",O=I2P Anonymous Network,L=XX,ST=XX,C=XX");
    // 10 years
    a.add("-validity");
    // 10 years
    a.add(Integer.toString(validDays));
    a.add("-keyalg");
    a.add(keyAlg);
    a.add("-sigalg");
    a.add(getSigAlg(keySize, keyAlg));
    a.add("-keysize");
    a.add(Integer.toString(keySize));
    a.add("-keypass");
    a.add(keyPW);
    if (keyAlg.equals("Ed") || keyAlg.equals("EdDSA") || keyAlg.equals("ElGamal")) {
        File f = I2PAppContext.getGlobalContext().getBaseDir();
        f = new File(f, "lib");
        f = new File(f, "i2p.jar");
        // providerpath is not in the man page; see keytool -genkey -help
        a.add("-providerpath");
        a.add(f.getAbsolutePath());
        a.add("-providerclass");
        a.add("net.i2p.crypto.provider.I2PProvider");
    }
    String[] args = a.toArray(new String[a.size()]);
    // TODO pipe key password to process; requires ShellCommand enhancements
    boolean success = (new ShellCommand()).executeSilentAndWaitTimed(args, 240);
    if (success) {
        success = ks.exists();
        if (success) {
            try {
                success = getPrivateKey(ks, ksPW, alias, keyPW) != null;
                if (!success)
                    error("Key gen failed to get private key", null);
            } catch (IOException e) {
                error("Key gen failed to get private key", e);
                success = false;
            } catch (GeneralSecurityException e) {
                error("Key gen failed to get private key", e);
                success = false;
            }
        }
        if (!success)
            error("Key gen failed for unknown reasons", null);
    }
    if (success) {
        SecureFileOutputStream.setPerms(ks);
        info("Created self-signed certificate for " + cname + " in keystore: " + ks.getAbsolutePath());
    } else {
        StringBuilder buf = new StringBuilder(256);
        for (int i = 0; i < args.length; i++) {
            buf.append('"').append(args[i]).append("\" ");
        }
        error("Failed to generate keys using command line: " + buf, null);
    }
    return success;
}
Also used : SecureDirectory(net.i2p.util.SecureDirectory) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ShellCommand(net.i2p.util.ShellCommand) File(java.io.File)

Aggregations

File (java.io.File)2 ShellCommand (net.i2p.util.ShellCommand)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 Log (net.i2p.util.Log)1 SecureDirectory (net.i2p.util.SecureDirectory)1