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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
import CyrusSasl.*;
import java.net.*;
import java.io.*;
import java.util.*;
class testserver {
static ServerSocket ssock;
private static PrintWriter os=null;
private static InputStreamReader ir=null;
private static Socket s=null;
private static BufferedReader br=null;
private static void give_capabilities() throws IOException
{
String []list = Sasl.getMechanismNames();
String cap="* CAPABILITY IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS X-NON-HIERARCHICAL-RENAME NO_ATOMIC_RENAME";
for (int lup=0;lup<list.length;lup++)
cap+= (" AUTH="+list[lup]);
send(cap);
send(". OK foo");
}
private static void do_auth(String mech, Socket remoteclient, int minssf, int maxssf) throws IOException
{
SaslClient conn;
Hashtable props = new Hashtable();
props.put("javax.security.sasl.encryption.minimum",String.valueOf(minssf));
props.put("javax.security.sasl.encryption.maximum",String.valueOf(maxssf));
props.put("javax.security.sasl.ip.local",s.getLocalAddress().getHostName());
props.put("javax.security.sasl.ip.remote",
remoteclient.getInetAddress().getHostAddress());
ServerHandler cbh = new ServerHandler();
try {
SaslServer saslconn = Sasl.CreateSaslServer(mech,
"imap",
s.getLocalAddress().getHostName(),
props,
cbh);
byte[]in = null;
while (true)
{
byte[] out = saslconn.evaluateResponse(in);
if (saslconn.isComplete()==true) {
break;
} else {
String outline = "+ ";
if (out!=null) {
System.out.println("outlen = "+ (out.length));
outline = "+ "+SaslUtils.encode64(out);
}
send(outline);
}
String line = br.readLine();
System.out.println("in = "+line);
if (line!=null)
in = SaslUtils.decode64(line);
else
in = null;
}
send(". OK Authenticated");
System.out.println("Authenticated!!!\n");
} catch (SaslException e) {
send(". NO Authentication failed");
}
}
private static void pretend_to_be_imapd() throws IOException
{
String line;
send("* OK pretend imapd. Use the '.' tag");
while (true) {
line = br.readLine();
if (line == null) {
System.out.println("I think the client quit on us");
System.exit(0);
}
if (line.startsWith(". ")==false) {
send("* BAD Must use '.' tag");
continue;
}
line=line.substring(2);
if (line.equalsIgnoreCase("CAPABILITY")==true) {
give_capabilities();
continue;
}
if (line.toUpperCase().startsWith("AUTHENTICATE ")==true) {
line = line.substring(13);
System.out.println("mechanism = "+line);
do_auth(line,s,0,0);
continue;
}
if (line.equalsIgnoreCase("NOOP")==true) {
send(". OK lalala");
continue;
}
if (line.equalsIgnoreCase("LOGOUT")==true) {
send(". OK yeah i'll exit. seya");
s.close();
System.exit(0);
}
send("* BAD don't support whatever you tried");
}
}
static void send(String str)
{
os.print(str+"\r\n");
os.flush();
}
public static void main (String args[])
{
int port = 2143;
try {
ssock = new ServerSocket(port);
System.out.println("Listening on port "+port);
s = ssock.accept();
os=new PrintWriter(s.getOutputStream());
ir=new InputStreamReader(s.getInputStream());
br=new BufferedReader(ir);
pretend_to_be_imapd();
} catch (IOException e) {
System.out.println("IO exception");
}
}
}
|