1
2import CyrusSasl.*;
3import java.net.*;
4import java.io.*;
5import java.util.*;
6
7class testserver {
8
9    static ServerSocket ssock;
10
11    private static PrintWriter os=null;
12    private static InputStreamReader ir=null;
13    private static Socket s=null;
14    private static BufferedReader br=null;
15
16    private static void give_capabilities() throws IOException
17    {
18	String []list = Sasl.getMechanismNames();
19
20	String cap="* CAPABILITY IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS X-NON-HIERARCHICAL-RENAME NO_ATOMIC_RENAME";
21
22	for (int lup=0;lup<list.length;lup++)
23	    cap+= (" AUTH="+list[lup]);
24
25	send(cap);
26
27	send(". OK foo");
28    }
29
30    private static void do_auth(String mech, Socket remoteclient, int minssf, int maxssf) throws IOException
31    {
32	SaslClient conn;
33	Hashtable props = new Hashtable();
34	props.put("javax.security.sasl.encryption.minimum",String.valueOf(minssf));
35	props.put("javax.security.sasl.encryption.maximum",String.valueOf(maxssf));
36	props.put("javax.security.sasl.ip.local",s.getLocalAddress().getHostName());
37	props.put("javax.security.sasl.ip.remote",
38		  remoteclient.getInetAddress().getHostAddress());
39
40	ServerHandler cbh = new ServerHandler();
41
42	try {
43
44	    SaslServer saslconn = Sasl.CreateSaslServer(mech,
45							"imap",
46							s.getLocalAddress().getHostName(),
47							props,
48							cbh);
49
50	    byte[]in = null;
51
52	    while (true)
53	    {
54		byte[] out = saslconn.evaluateResponse(in);
55
56		if (saslconn.isComplete()==true) {
57		    break;
58		} else {
59		    String outline = "+ ";
60		    if (out!=null) {
61			System.out.println("outlen = "+ (out.length));
62			outline = "+ "+SaslUtils.encode64(out);
63		    }
64		    send(outline);
65		}
66
67		String line = br.readLine();
68
69		System.out.println("in = "+line);
70
71		if (line!=null)
72		    in = SaslUtils.decode64(line);
73		else
74		    in = null;
75	    }
76
77	    send(". OK Authenticated");
78
79	    System.out.println("Authenticated!!!\n");
80
81	} catch (SaslException e) {
82	    send(". NO Authentication failed");
83	}
84    }
85
86    private static void pretend_to_be_imapd() throws IOException
87    {
88	String line;
89
90	send("* OK pretend imapd. Use the '.' tag");
91
92	while (true) {
93
94	    line = br.readLine();
95
96	    if (line == null) {
97		System.out.println("I think the client quit on us");
98		System.exit(0);
99	    }
100
101	    if (line.startsWith(". ")==false) {
102		send("* BAD Must use '.' tag");
103		continue;
104	    }
105
106	    line=line.substring(2);
107
108	    if (line.equalsIgnoreCase("CAPABILITY")==true) {
109
110		give_capabilities();
111
112		continue;
113	    }
114
115	    if (line.toUpperCase().startsWith("AUTHENTICATE ")==true) {
116		line = line.substring(13);
117
118		System.out.println("mechanism = "+line);
119
120		do_auth(line,s,0,0);
121
122		continue;
123	    }
124
125	    if (line.equalsIgnoreCase("NOOP")==true) {
126		send(". OK lalala");
127		continue;
128	    }
129
130	    if (line.equalsIgnoreCase("LOGOUT")==true) {
131
132		send(". OK yeah i'll exit. seya");
133		s.close();
134		System.exit(0);
135	    }
136
137	    send("* BAD don't support whatever you tried");
138	}
139
140
141
142    }
143
144    static void send(String str)
145    {
146	os.print(str+"\r\n");
147	os.flush();
148    }
149
150    public static void main (String args[])
151    {
152	int port = 2143;
153
154	try {
155
156	    ssock = new ServerSocket(port);
157
158	    System.out.println("Listening on port "+port);
159
160	    s = ssock.accept();
161	    os=new PrintWriter(s.getOutputStream());
162	    ir=new InputStreamReader(s.getInputStream());
163	    br=new BufferedReader(ir);
164
165
166	    pretend_to_be_imapd();
167
168	} catch (IOException e) {
169	    System.out.println("IO exception");
170	}
171    }
172
173
174
175}
176