-
Notifications
You must be signed in to change notification settings - Fork 2
/
CopyKeys.java
141 lines (130 loc) · 5.36 KB
/
CopyKeys.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// CopyKeys
// migration script that copies keys generated by one source into another one
// typically used when switching authentication back-ends such that
// encrypted content is still available
//
// Author: Simon Urbanek
package com.att.research.RCloud;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.concurrent.Executors;
import java.security.MessageDigest;
import java.security.SecureRandom;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import javax.net.ssl.SSLContext;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
public class CopyKeys {
public static final String version = "1.4";
public static String default_module = "pam", pam_realm = null;
public static void main(String[] args) throws IOException, DatabaseException {
int i = 0;
String bdb = null, src = null, dst = null;
boolean overwrite = false, list = false;
while (i < args.length) {
if (args[i].equals("-d") && ++i < args.length) bdb = args[i];
else if (args[i].equals("-o"))
overwrite = true;
else if (args[i].equals("-l"))
list = true;
else if (args[i].equals("-h")) {
System.out.println("\n Usage: CopyKeys -d <db-path> [-o] <from> <to>\n -d <db-path> -l\n\n");
System.exit(0);
} else if (src == null)
src = args[i];
else if (dst == null)
dst = args[i];
else {
System.err.println("\nERROR: superfluous argument: " + args[i]);
System.exit(1);
}
i++;
}
if (bdb == null || (!list && (src == null || dst == null))) {
System.err.println("\nERROR: db-path, from and to are mandatory - see -h if in doubt.");
System.exit(1);
}
if (src != null && src.equals(dst)) {
System.err.println("\nERROR: <from> and <to> must be different.");
System.exit(1);
}
Environment env;
Database db;
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
env = new Environment(new File(bdb), envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
db = env.openDatabase(null, "sessionKeyStore", dbConfig);
int copied = 0;
com.sleepycat.je.DiskOrderedCursor cur = db.openCursor(null);
DatabaseEntry key = new DatabaseEntry(), val = new DatabaseEntry();
while (cur.getNext(key, val, null) == OperationStatus.SUCCESS) {
String s = (key.getData() == null) ? null : new String(key.getData(), "UTF-8");
if (s != null && s.startsWith("k:")) { // key entry
if (list)
System.out.println("KEY: "+s+"\n");
else if ((i = s.indexOf("\n")) > 0) {
int j = s.indexOf("\n", i + 1);
if (j > 0) {
String old_source = s.substring(i + 1, j);
if (old_source.equals(src)) {
boolean do_copy = false;
String new_key = s.substring(0, i + 1) + dst + s.substring(j);
if (overwrite)
do_copy = true;
else {
DatabaseEntry theKey = new DatabaseEntry(new_key.getBytes("UTF-8"));
DatabaseEntry theData = new DatabaseEntry();
if (db.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
System.out.println("NOTE: " + s.substring(0, i) + " key exists, NOT overwriting");
if (!theData.equals(val))
System.err.println("WARN: " + s.substring(0, i) + " keys for from and to differ");
} else do_copy = true;
}
if (do_copy) {
System.out.println("INFO: copying key for " + s.substring(0, i));
DatabaseEntry theKey = new DatabaseEntry(new_key.getBytes("UTF-8"));
db.put(null, theKey, val);
copied++;
}
}
}
}
}
}
env.flushLog(true);
cur.close();
}
}