From 9aa9af7e51d337140c10b946bc96e7997f8930b4 Mon Sep 17 00:00:00 2001 From: Timothy Prepscius Date: Wed, 17 Jul 2013 18:47:42 -0400 Subject: [PATCH] adds the app tools --- java/app/tools/.classpath | 18 ++ java/app/tools/.project | 17 ++ java/app/tools/lib | 1 + java/app/tools/src/app/tools/Arguments.java | 47 +++ .../CheckUsersAndScheduleForDeletion.java | 14 + .../app/tools/ConvertAccountMailToJson.java | 136 +++++++++ .../src/app/tools/ConvertFileMailToJson.java | 66 ++++ .../app/tools/DecryptMailToFileSystem.java | 111 +++++++ java/app/tools/src/app/tools/ExportPem.java | 40 +++ .../src/app/tools/ExpungeDeletedUsers.java | 76 +++++ .../tools/src/app/tools/ExpungeS3User.java | 34 +++ java/app/tools/src/app/tools/Main.java | 283 ++++++++++++++++++ .../src/app/tools/RemoveErrantJamesUsers.java | 11 + .../app/tools/ScheduleUserForDeletion.java | 25 ++ .../tools/SendPushNotificationForDevice.java | 25 ++ .../tools/SendPushNotificationForUser.java | 35 +++ .../tools/src/app/tools/ShowUserKeyBlock.java | 50 ++++ .../src/app/tools/ShowUserMailBlock.java | 38 +++ 18 files changed, 1027 insertions(+) create mode 100644 java/app/tools/.classpath create mode 100644 java/app/tools/.project create mode 120000 java/app/tools/lib create mode 100644 java/app/tools/src/app/tools/Arguments.java create mode 100644 java/app/tools/src/app/tools/CheckUsersAndScheduleForDeletion.java create mode 100644 java/app/tools/src/app/tools/ConvertAccountMailToJson.java create mode 100644 java/app/tools/src/app/tools/ConvertFileMailToJson.java create mode 100644 java/app/tools/src/app/tools/DecryptMailToFileSystem.java create mode 100644 java/app/tools/src/app/tools/ExportPem.java create mode 100644 java/app/tools/src/app/tools/ExpungeDeletedUsers.java create mode 100644 java/app/tools/src/app/tools/ExpungeS3User.java create mode 100644 java/app/tools/src/app/tools/Main.java create mode 100644 java/app/tools/src/app/tools/RemoveErrantJamesUsers.java create mode 100644 java/app/tools/src/app/tools/ScheduleUserForDeletion.java create mode 100644 java/app/tools/src/app/tools/SendPushNotificationForDevice.java create mode 100644 java/app/tools/src/app/tools/SendPushNotificationForUser.java create mode 100644 java/app/tools/src/app/tools/ShowUserKeyBlock.java create mode 100644 java/app/tools/src/app/tools/ShowUserMailBlock.java diff --git a/java/app/tools/.classpath b/java/app/tools/.classpath new file mode 100644 index 0000000..6569e03 --- /dev/null +++ b/java/app/tools/.classpath @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/java/app/tools/.project b/java/app/tools/.project new file mode 100644 index 0000000..f974f19 --- /dev/null +++ b/java/app/tools/.project @@ -0,0 +1,17 @@ + + + Mailiverse.App.Tools + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/java/app/tools/lib b/java/app/tools/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/java/app/tools/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/java/app/tools/src/app/tools/Arguments.java b/java/app/tools/src/app/tools/Arguments.java new file mode 100644 index 0000000..8ad42fa --- /dev/null +++ b/java/app/tools/src/app/tools/Arguments.java @@ -0,0 +1,47 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.util.HashMap; +import java.util.Map; + +public class Arguments +{ + public static Map map (String[] s, int offset) + { + Map map = new HashMap(); + + for (int i=offset; i kv, String[] keys) + { + for (String k : keys) + { + if (!kv.containsKey(k)) + return false; + } + + return true; + } + + +} diff --git a/java/app/tools/src/app/tools/CheckUsersAndScheduleForDeletion.java b/java/app/tools/src/app/tools/CheckUsersAndScheduleForDeletion.java new file mode 100644 index 0000000..2847728 --- /dev/null +++ b/java/app/tools/src/app/tools/CheckUsersAndScheduleForDeletion.java @@ -0,0 +1,14 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +public class CheckUsersAndScheduleForDeletion +{ + public static void main(String[] args) + { + + } +} diff --git a/java/app/tools/src/app/tools/ConvertAccountMailToJson.java b/java/app/tools/src/app/tools/ConvertAccountMailToJson.java new file mode 100644 index 0000000..931b177 --- /dev/null +++ b/java/app/tools/src/app/tools/ConvertAccountMailToJson.java @@ -0,0 +1,136 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import key.auth.KeyServerAuthenticatorSync; +import mail.server.util.JavaMailToJSON; +import core.connector.FileInfo; +import core.connector.dropbox.ClientInfoDropbox; +import core.connector.dropbox.sync.DropboxConnector; +import core.connector.s3.ClientInfoS3; +import core.connector.s3.sync.S3Connector; +import core.connector.sync.EncryptedStoreConnector; +import core.connector.sync.StoreConnector; +import core.constants.ConstantsEnvironmentKeys; +import core.constants.ConstantsServer; +import core.constants.ConstantsStorage; +import core.crypt.CryptorRSAAES; +import core.crypt.CryptorRSAFactoryEnvironment; +import core.util.Environment; + +public class ConvertAccountMailToJson +{ + public static void convert (String name, String password, boolean overwrite) throws Exception + { + KeyServerAuthenticatorSync auth = new KeyServerAuthenticatorSync("YOUR_SERVER", ConstantsServer.KEY_AUTH_PORT); + Environment environment = auth.get(name, password, null); + Environment clientEnv = environment.childEnvironment(ConstantsEnvironmentKeys.CLIENT_ENVIRONMENT); + + String handlerName = clientEnv.get(ConstantsEnvironmentKeys.HANDLER); + StoreConnector storeConnector = null; + + if (handlerName.equals(ConstantsStorage.HANDLER_S3)) + { + storeConnector = + new S3Connector( + new ClientInfoS3(clientEnv.childEnvironment(ConstantsStorage.HANDLER_S3) + ) + ); + } + else + { + storeConnector = + new DropboxConnector( + new ClientInfoDropbox(clientEnv.childEnvironment(ConstantsStorage.HANDLER_DROPBOX) + ) + ); + } + + StoreConnector connector = new EncryptedStoreConnector( + new CryptorRSAAES(CryptorRSAFactoryEnvironment.create(clientEnv)), + storeConnector + ); + + connector.open(); + + connector.ensureDirectories(ConstantsStorage.NEW_IN_JSON, ConstantsStorage.NEW_OUT_JSON); + + List inList = connector.listDirectory(ConstantsStorage.NEW_IN); + List outList = connector.listDirectory(ConstantsStorage.NEW_OUT); + List inListJson = connector.listDirectory(ConstantsStorage.NEW_IN_JSON); + List outListJson = connector.listDirectory(ConstantsStorage.NEW_OUT_JSON); + + /* + for (FileInfo i : inList) + i.path = ConstantsStorage.NEW_IN + i.path; + + for (FileInfo i : outList) + i.path = ConstantsStorage.NEW_OUT + i.path; + */ + + ArrayList all = new ArrayList(); + all.addAll(inList); + all.addAll(outList); + + Collections.sort(all, new FileInfo.SortByDateAscending()); + + Set jsons = new HashSet(); + + for (FileInfo i : inListJson) + jsons.add(i.relativePath.substring(i.relativePath.indexOf("_")+1)); + + for (FileInfo i : outListJson) + jsons.add(i.relativePath.substring(i.relativePath.indexOf("_")+1)); + + JavaMailToJSON converter = new JavaMailToJSON(); + for (FileInfo i : all) + { + System.out.print("handling " + i.path); + + if (i.path.indexOf(ConstantsStorage.JSON)!=-1) + { + System.out.println(" is json, continuing."); + continue; + } + + String jsonPath = i.relativePath.substring(i.relativePath.indexOf('_') + 1); + if (jsons.contains(jsonPath) && !overwrite) + { + System.out.println(" has json counterpart, specify overwrite to overwrite."); + continue; + } + + System.out.print(" reading"); + byte[] raw = connector.get(i.path); + System.out.print(", converting"); + byte[] json = converter.convert(i.path, i.date, raw, new JavaMailToJSON.MailDescription()); + System.out.print(", writing"); + + String datePath = + (i.relativePath.contains("_")) ? + i.relativePath : + i.date.getTime() + "_" + i.relativePath; + + String jsonFileName = i.path.contains(ConstantsStorage.IN) ? + (ConstantsStorage.NEW_IN_JSON + "/" + datePath) : + (ConstantsStorage.NEW_OUT_JSON + "/" + datePath); + + System.out.print(" to " + jsonFileName); + connector.put(jsonFileName, json); + System.out.println(" : finished"); + } + + System.out.println("closing"); + connector.close(); + } + +} diff --git a/java/app/tools/src/app/tools/ConvertFileMailToJson.java b/java/app/tools/src/app/tools/ConvertFileMailToJson.java new file mode 100644 index 0000000..1dc2d92 --- /dev/null +++ b/java/app/tools/src/app/tools/ConvertFileMailToJson.java @@ -0,0 +1,66 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.Date; + +import key.auth.KeyServerAuthenticatorSync; +import mail.server.util.JavaMailToJSON; +import core.crypt.CryptorRSAAES; +import core.crypt.CryptorRSAFactoryEnvironment; +import core.util.Environment; +import core.util.Streams; +import core.util.Zip; + +public class ConvertFileMailToJson +{ + public static void main(String[] args) throws Exception + { + String name = args[0]; + String password = args[1]; + + KeyServerAuthenticatorSync auth = new KeyServerAuthenticatorSync(); + Environment e = auth.get(name, password, null); + CryptorRSAAES rsa = new CryptorRSAAES(CryptorRSAFactoryEnvironment.create(e.childEnvironment("client"))); + + JavaMailToJSON converter = new JavaMailToJSON(); + + + for (int i=2; i args = Arguments.map(_args, 0); + String name = args.get("name"); + String password = args.get("password"); + String out = args.get("out"); + + if (name == null || password == null || out == null) + throw new IllegalArgumentException(); + + KeyServerAuthenticatorSync key = new KeyServerAuthenticatorSync(); + Environment environment = key.get(name, password, null); + Environment clientEnv = environment.childEnvironment(ConstantsEnvironmentKeys.CLIENT_ENVIRONMENT); + + String handlerName = clientEnv.get(ConstantsEnvironmentKeys.HANDLER); + StoreConnector storeConnector = null; + + if (handlerName.equals(ConstantsStorage.HANDLER_S3)) + { + storeConnector = + new S3Connector( + new ClientInfoS3(clientEnv.childEnvironment(ConstantsStorage.HANDLER_S3) + ) + ); + } + else + { + storeConnector = + new DropboxConnector( + new ClientInfoDropbox(clientEnv.childEnvironment(ConstantsStorage.HANDLER_DROPBOX) + ) + ); + } + + StoreConnector connector = new EncryptedStoreConnector( + new CryptorRSAAES(CryptorRSAFactoryEnvironment.create(clientEnv)), + storeConnector + ); + + connector.open(); + + String[] dirs = new String[] { + ConstantsStorage.NEW_IN, ConstantsStorage.NEW_OUT, + ConstantsStorage.NEW_IN_JSON, ConstantsStorage.NEW_OUT_JSON + }; + + List all = new ArrayList(); + + for (String dir : dirs) + { + List list = connector.listDirectory(dir); + all.addAll(list); + } + + Collections.sort(all, new FileInfo.SortByDateAscending()); + + for (FileInfo f : all) + { + if (f.relativePath.contains(".lock")) + continue; + + System.out.print("reading " + f.path); + byte[] block = connector.get(f.path); + + String directory = out + "/" + f.path; + directory = directory.substring(0, directory.lastIndexOf('/')); + + System.out.print(" ["+directory+"]"); + new File(directory).mkdirs(); + + String outPath= out + "/" + f.path; + System.out.print(" writing " + outPath); + + FileOutputStream of = new FileOutputStream (outPath); + of.write(block); + of.close(); + + System.out.println(" done."); + } + } +} diff --git a/java/app/tools/src/app/tools/ExportPem.java b/java/app/tools/src/app/tools/ExportPem.java new file mode 100644 index 0000000..c082d7a --- /dev/null +++ b/java/app/tools/src/app/tools/ExportPem.java @@ -0,0 +1,40 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.security.KeyStore; +import java.security.PublicKey; +import java.util.Map; + +import core.util.Base64; + +public class ExportPem { + + public static void main (String[] args) throws Exception + { + Map a = Arguments.map(args, 1); + if (!Arguments.containsAll(a, new String[] { "file"})) + throw new IllegalArgumentException(); + + String fileNameIn = a.get("file"); + FileInputStream in = new FileInputStream(fileNameIn); + + KeyStore tks = KeyStore.getInstance("JKS"); + tks.load(in, "password".toCharArray()); + PublicKey publicKey = tks.getCertificate(tks.aliases().nextElement()).getPublicKey(); + + byte[] bytes = publicKey.getEncoded(); + + String fileNameOut = fileNameIn + ".pem.b64"; + FileWriter out = new FileWriter (fileNameOut); + out.write(Base64.encode(bytes)); + out.close(); + } + +} diff --git a/java/app/tools/src/app/tools/ExpungeDeletedUsers.java b/java/app/tools/src/app/tools/ExpungeDeletedUsers.java new file mode 100644 index 0000000..5498f18 --- /dev/null +++ b/java/app/tools/src/app/tools/ExpungeDeletedUsers.java @@ -0,0 +1,76 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.util.Map; + +import core.connector.s3.ClientInfoS3; +import core.constants.ConstantsEnvironmentKeys; +import core.constants.ConstantsStorage; +import core.util.Environment; +import core.util.LogOut; +import mail.server.db.MailUserDb; +import mail.server.storage.AWSStorageDelete; + +public class ExpungeDeletedUsers +{ + static LogOut log = new LogOut(ExpungeDeletedUsers.class); + + public static void main (String[] _args) throws Exception + { + Map args = Arguments.map(_args,0); + + ExpungeDeletedUsers expunger = new ExpungeDeletedUsers(); + String num = args.get("num"); + if (num == null) + num = "-1"; + + expunger.expungeQueue(Integer.parseInt(num), args.containsKey("force")); + } + + public void expungeQueue(int maxToExpunge, boolean force) throws Exception + { + Class.forName("com.mysql.jdbc.Driver"); + + MailUserDb mailUserDb; + mailUserDb = new MailUserDb(); + + String user; + AWSStorageDelete deletion = new AWSStorageDelete(); + while (maxToExpunge-- != 0 && (user = mailUserDb.getDeletedUser()) != null) + { + try + { + Environment e = mailUserDb.getDeletedUserEnvironment(user); + + if (e.containsKey(ConstantsEnvironmentKeys.HANDLER) && e.get(ConstantsEnvironmentKeys.HANDLER).equals(ConstantsStorage.HANDLER_S3)) + { + ClientInfoS3 clientInfo = new ClientInfoS3(e.childEnvironment(ConstantsStorage.HANDLER_S3)); + deletion.delete(clientInfo.getBucketName()); + } + } + catch (Exception e) + { + if (!force) + throw e; + + log.exception(e); + } + + try + { + mailUserDb.expungeUser(user); + } + catch (Exception e) + { + if (!force) + throw e; + + log.exception(e); + } + } + } +} diff --git a/java/app/tools/src/app/tools/ExpungeS3User.java b/java/app/tools/src/app/tools/ExpungeS3User.java new file mode 100644 index 0000000..6a46c1c --- /dev/null +++ b/java/app/tools/src/app/tools/ExpungeS3User.java @@ -0,0 +1,34 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.util.Map; + +import core.util.Passwords; + +import mail.server.storage.AWSStorageDelete; + +public class ExpungeS3User +{ + public static void main(String[] _args) throws Exception + { + Map args = Arguments.map(_args, 0); + + String accessKey = args.get("accesskey"); + String secretKey = args.get("secretkey"); + String bucketName = args.get("bucketname"); + + if (bucketName == null) + throw new IllegalArgumentException(); + + AWSStorageDelete deleter = new AWSStorageDelete(); + + if (accessKey == null && secretKey == null) + deleter.delete(bucketName); + else + deleter.delete(bucketName, accessKey, secretKey); + } +} diff --git a/java/app/tools/src/app/tools/Main.java b/java/app/tools/src/app/tools/Main.java new file mode 100644 index 0000000..eabcc84 --- /dev/null +++ b/java/app/tools/src/app/tools/Main.java @@ -0,0 +1,283 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import mail.auth.MailServerAuthenticatorSync; + +import com.dropbox.client2.session.AccessTokenPair; +import com.dropbox.client2.session.AppKeyPair; + +import core.connector.dropbox.sync.DropboxSignup; +import core.constants.ConstantsClient; +import core.constants.ConstantsDropbox; +import core.constants.ConstantsEnvironmentKeys; +import core.constants.ConstantsServer; +import core.constants.ConstantsStorage; +import core.util.Environment; +import core.util.JSONSerializer; +import core.util.LogNull; +import core.util.LogOut; +import core.util.Streams; + +import key.auth.KeyServerAuthenticatorSync; + +public class Main +{ + static LogNull log = new LogNull(Main.class); + + public static void usage (Exception e) throws Exception + { + System.out.println( + "Usage: java -jar Tools.jar [action] [args]" + "\n" + + " --help this screen" + "\n" + + " --get-environment name=x password=Y file=Z" + "\n" + + " --put-environment name=x password=Y file=Z" + "\n" + + " --dropbox-authorize name=X password=Y" + "\n" + + " --change-password name=X password=Y new-password=Z" + "\n" + + " --delete name=X password=Y" + "\n" + + " --convert-account-mail-to-json name=X password=Y [overwrite]" + "\n" + + " --export-pem file=X" + "\n" + + " --expunge-deleted-users [force]" + "\n" + + " --expunge-s3-user accessKey=[] secretKey=[] bucketName=[]" + "\n" + + " --schedule-user-for-deletion user=name" + "\n" + + " --show-user-mail-block user=name" + "\n" + + " --show-user-key-block user=name password=password" + "\n" + + " --decryptMailToFileSystem name=name password=password out=outpath" + "\n" + + " --sendPushNotificationForDevice device=id" + "\n" + + " --sendPushNotificationForUser user=email" + "\n" + + "" + ); + + if (e != null) + { + System.out.println(e); + throw e; + } + } + + public static void main (String[] args) throws Exception + { + System.out.println(new Date()); + try + { + if (args.length == 0) + { + usage(null); + return; + } + else + { + String c = args[0].toLowerCase(); + if (c.equalsIgnoreCase("--help")) + usage(null); + else + if (c.equalsIgnoreCase("--dropbox-authorize")) + dropboxAuthorize(args); + else + if (c.equalsIgnoreCase("--get-environment")) + getEnvironment(args); + else + if (c.equalsIgnoreCase("--put-environment")) + putEnvironment(args); + else + if (c.equalsIgnoreCase("--change-password")) + changePassword(args); + else + if (c.equalsIgnoreCase("--delete")) + delete(args); + else + if (c.equalsIgnoreCase("--convert-account-mail-to-json")) + convertAccountMailToJson(args); + else + if (c.equalsIgnoreCase("--export-pem")) + ExportPem.main(args); + else + if (c.equalsIgnoreCase("--expunge-deleted-users")) + ExpungeDeletedUsers.main(args); + else + if (c.equalsIgnoreCase("--expunge-s3-user")) + ExpungeS3User.main(args); + else + if (c.equalsIgnoreCase("--schedule-user-for-deletion")) + ScheduleUserForDeletion.main(args); + else + if (c.equalsIgnoreCase("--show-user-mail-block")) + ShowUserMailBlock.main(args); + else + if (c.equalsIgnoreCase("--show-user-key-block")) + ShowUserKeyBlock.main(args); + else + if (c.equalsIgnoreCase("--decryptMailToFileSystem")) + DecryptMailToFileSystem.main(args); + else + if (c.equalsIgnoreCase("--sendPushNotificationForDevice")) + SendPushNotificationForDevice.main(args); + else + if (c.equalsIgnoreCase("--sendPushNotificationForUser")) + SendPushNotificationForUser.main(args); + else + throw new IllegalArgumentException("Unknown command " + c); + } + } + catch (IllegalArgumentException e) + { + usage(e); + } + catch (Exception e) + { + System.err.println("Caught " + e); + throw e; + } + System.out.println(new Date()); + } + + public static void convertAccountMailToJson(String[] args) throws Exception + { + Map a = Arguments.map(args, 1); + if (!Arguments.containsAll(a, new String[] {"name", "password"})) + throw new IllegalArgumentException(); + + String name = a.get("name") + ConstantsClient.ATHOST; + String password = a.get("password"); + + boolean overwrite = a.containsKey("overwrite"); + + ConvertAccountMailToJson.convert(name, password, overwrite); + } + + public static void dropboxAuthorize (String[] args) throws Exception + { + Map a = Arguments.map(args, 1); + if (!Arguments.containsAll(a, new String[] {"name", "password"})) + throw new IllegalArgumentException(); + + PrintStream out = System.out; + + String name = a.get("name") + ConstantsClient.ATHOST; + String password = a.get("password"); + + out.println("Getting existing environment"); + KeyServerAuthenticatorSync keyServer = new KeyServerAuthenticatorSync(); + MailServerAuthenticatorSync mailServer = new MailServerAuthenticatorSync(); + + Environment e = keyServer.get(name, password, null); + + out.println("Requesting access token"); + AppKeyPair appToken = new AppKeyPair(ConstantsClient.DROPBOX_APPKEY, ConstantsClient.DROPBOX_APPSECRET); + AccessTokenPair requestToken = DropboxSignup.getDropboxRequestToken(appToken); + + out.println("Please open in a web browser: " + + ConstantsClient.DROPBOX_AUTH_URL.replace("REQUEST_TOKEN_KEY", requestToken.key) + ); + + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + out.print ("Enter the authorization code recieved: "); + String authCode = in.readLine(); + + if (!authCode.equals(requestToken.key)) + throw new Exception("Invalid authentication code"); + + out.println("Requesting access token"); + AccessTokenPair accessToken = DropboxSignup.getDropboxAccessToken(appToken, requestToken); + + out.println("Setting new environment variables"); + + String handler = ConstantsStorage.HANDLER_DROPBOX; + String prefix = handler + "/"; + + String client = ConstantsEnvironmentKeys.CLIENT_ENVIRONMENT + "/"; + e.put(client + prefix + ConstantsDropbox.DropboxUserPrefix, name); + e.put(client + prefix + ConstantsDropbox.DropboxAppKey, appToken.key); + e.put(client + prefix + ConstantsDropbox.DropboxAppSecret, appToken.secret); + e.put(client + prefix + ConstantsDropbox.DropboxTokenKey, accessToken.key); + e.put(client + prefix + ConstantsDropbox.DropboxTokenSecret, accessToken.secret); + + String server = ConstantsEnvironmentKeys.SERVER_ENVIRONMENT + "/"; + e.put(server + prefix + ConstantsDropbox.DropboxUserPrefix, name); + e.put(server + prefix + ConstantsDropbox.DropboxAppKey, appToken.key); + e.put(server + prefix + ConstantsDropbox.DropboxAppSecret, appToken.secret); + e.put(server + prefix + ConstantsDropbox.DropboxTokenKey, accessToken.key); + e.put(server + prefix + ConstantsDropbox.DropboxTokenSecret, accessToken.secret); + + out.println("putting environment into keyserver"); + keyServer.put(name, password, e, null); + + out.println("putting server environment into mailserver"); + mailServer.put(name, password, e.childEnvironment(ConstantsEnvironmentKeys.SERVER_ENVIRONMENT)); + + out.println("finished"); + } + + public static void getEnvironment (String args[]) throws Exception + { + PrintStream out = System.out; + out.println("Getting existing environment"); + KeyServerAuthenticatorSync keyServer = new KeyServerAuthenticatorSync(); + + Map a = Arguments.map(args, 1); + if (!Arguments.containsAll(a, new String[] {"name", "password", "file"})) + throw new IllegalArgumentException(); + + String name = a.get("name"); + String password = a.get("password"); + String fileName = a.get("file"); + + Environment e = keyServer.get(name, password, null); + FileWriter writer = new FileWriter(fileName); + writer.write(new String(JSONSerializer.serialize(e))); + writer.flush(); + writer.close(); + + out.println("finished"); + } + + public static void putEnvironment (String args[]) throws Exception + { + PrintStream out = System.out; + out.println("Putting existing environment"); + KeyServerAuthenticatorSync keyServer = new KeyServerAuthenticatorSync(); + MailServerAuthenticatorSync mailServer = new MailServerAuthenticatorSync(); + + Map a = Arguments.map(args, 1); + if (!Arguments.containsAll(a, new String[] {"name", "password", "file"})) + throw new IllegalArgumentException(); + + String name = a.get("name"); + String password = a.get("password"); + String fileName = a.get("file"); + + FileInputStream in = new FileInputStream (fileName); + Environment e = JSONSerializer.deserialize(Streams.readFullyBytes(in)); + in.close(); + + out.println("putting keyserver"); + keyServer.put(name, password, e, null); + + out.println("putting mailserver"); + mailServer.put(name, password, e.childEnvironment(ConstantsEnvironmentKeys.SERVER_ENVIRONMENT)); + + out.println("finished"); + } + + public static void changePassword (String[] args) + { + + } + + public static void delete(String[] args) + { + + } +} diff --git a/java/app/tools/src/app/tools/RemoveErrantJamesUsers.java b/java/app/tools/src/app/tools/RemoveErrantJamesUsers.java new file mode 100644 index 0000000..1eba01f --- /dev/null +++ b/java/app/tools/src/app/tools/RemoveErrantJamesUsers.java @@ -0,0 +1,11 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +public class RemoveErrantJamesUsers +{ + +} diff --git a/java/app/tools/src/app/tools/ScheduleUserForDeletion.java b/java/app/tools/src/app/tools/ScheduleUserForDeletion.java new file mode 100644 index 0000000..bfaabdd --- /dev/null +++ b/java/app/tools/src/app/tools/ScheduleUserForDeletion.java @@ -0,0 +1,25 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.IOException; +import java.sql.SQLException; + +import core.exceptions.CryptoException; +import mail.server.db.MailUserDb; +import mail.streamserver.MailServerSessionDb; + +public class ScheduleUserForDeletion +{ + public static void main (String[] args) throws Exception + { + Class.forName("com.mysql.jdbc.Driver"); + + String user = Arguments.map(args, 0).get("user"); + MailServerSessionDb db = new MailServerSessionDb(new MailUserDb()); + db.deleteUser(user); + } +} diff --git a/java/app/tools/src/app/tools/SendPushNotificationForDevice.java b/java/app/tools/src/app/tools/SendPushNotificationForDevice.java new file mode 100644 index 0000000..6626025 --- /dev/null +++ b/java/app/tools/src/app/tools/SendPushNotificationForDevice.java @@ -0,0 +1,25 @@ +package app.tools; + +import java.util.Map; + +import mail.server.push.ApplePushService; + +public class SendPushNotificationForDevice +{ + public static void main (String[] _args) + { + Map args = Arguments.map(_args, 0); + + String deviceType = "ios"; + String deviceId = args.get("device"); + + if (deviceId == null) + throw new IllegalArgumentException(); + + System.out.println ("Sending push notification to: " + deviceId); + + ApplePushService pusher = new ApplePushService(); + pusher.notifyUserOfEmail(deviceId, "author", "subject", "body"); + pusher.shutdown(); + } +} diff --git a/java/app/tools/src/app/tools/SendPushNotificationForUser.java b/java/app/tools/src/app/tools/SendPushNotificationForUser.java new file mode 100644 index 0000000..b818a1a --- /dev/null +++ b/java/app/tools/src/app/tools/SendPushNotificationForUser.java @@ -0,0 +1,35 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Map; + +import mail.server.push.ApplePushService; +import mail.server.push.PushDispatcher; + +public class SendPushNotificationForUser +{ + public static void main (String[] _args) throws Exception + { + Class.forName("com.mysql.jdbc.Driver"); + + Map args = Arguments.map(_args, 0); + + String user = args.get("user"); + + if (user == null) + throw new IllegalArgumentException(); + + System.out.println ("Sending push notification to: " + user); + + PushDispatcher pusher = new PushDispatcher(); + pusher.notifyUserOfEmail(user, "author@somehow.com", "subject", "body"); + pusher.shutdown(); + } + +} diff --git a/java/app/tools/src/app/tools/ShowUserKeyBlock.java b/java/app/tools/src/app/tools/ShowUserKeyBlock.java new file mode 100644 index 0000000..78b25bc --- /dev/null +++ b/java/app/tools/src/app/tools/ShowUserKeyBlock.java @@ -0,0 +1,50 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Map; +import java.util.Map.Entry; + +import key.server.sql.KeyUserDb; + +import core.crypt.KeyPairFromPassword; +import core.crypt.KeyPairFromPasswordCryptor; +import core.exceptions.CryptoException; +import core.util.Environment; +import core.util.JSONSerializer; +import core.util.Zip; + +public class ShowUserKeyBlock +{ + public static void main (String[] _args) throws CryptoException, IOException, SQLException, ClassNotFoundException + { + Class.forName("com.mysql.jdbc.Driver"); + + Map args = Arguments.map(_args,0); + String user = args.get("user"); + String password = args.get("password"); + if (user == null || password == null) + throw new IllegalArgumentException(); + + KeyPairFromPassword keyPair = new KeyPairFromPassword(password); + keyPair.generate(); + + KeyPairFromPasswordCryptor cryptor = new KeyPairFromPasswordCryptor(keyPair); + + KeyUserDb db = new KeyUserDb(); + byte[] encryptedBlock = db.getBlock(user); + byte[] block = Zip.inflate(cryptor.decrypt(encryptedBlock)); + + Environment e = JSONSerializer.deserialize(block); + for (Entry i : e.entrySet()) + { + System.out.println(i.getKey() + ":" + i.getValue()); + } + } + +} diff --git a/java/app/tools/src/app/tools/ShowUserMailBlock.java b/java/app/tools/src/app/tools/ShowUserMailBlock.java new file mode 100644 index 0000000..b0207aa --- /dev/null +++ b/java/app/tools/src/app/tools/ShowUserMailBlock.java @@ -0,0 +1,38 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package app.tools; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Map; +import java.util.Map.Entry; + +import core.exceptions.CryptoException; +import core.util.Environment; +import core.util.JSONSerializer; + +import mail.server.db.MailUserDb; + +public class ShowUserMailBlock +{ + public static void main (String[] _args) throws CryptoException, IOException, SQLException, ClassNotFoundException + { + Class.forName("com.mysql.jdbc.Driver"); + + Map args = Arguments.map(_args,0); + String user = args.get("user"); + if (user == null) + throw new IllegalArgumentException(); + + MailUserDb db = new MailUserDb(); + byte[] block = db.getBlock(user); + Environment e = JSONSerializer.deserialize(block); + for (Entry i : e.entrySet()) + { + System.out.println(i.getKey() + ":" + i.getValue()); + } + } +}