File: jimtest.java

package info (click to toggle)
cyrus-sasl2 2.1.23.dfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,692 kB
  • ctags: 7,265
  • sloc: ansic: 50,844; sh: 12,869; java: 1,614; xml: 1,498; makefile: 649; perl: 11
file content (304 lines) | stat: -rw-r--r-- 6,092 bytes parent folder | download | duplicates (11)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

import java.net.*;
import java.io.*;
import java.util.Hashtable;
import CyrusSasl.*;

class jimtest
{
    private static PrintWriter os=null;
    private static InputStreamReader ir=null;
    private static Socket s=null;
    private static BufferedReader br=null;

  static void send(String str)
  {
      os.print(str+"\r\n");
      os.flush();
    
  }

    static boolean connect(String servername, int port)
    {
	try
	    {
		s=new Socket(servername,port);
	    } catch (UnknownHostException e){
		System.out.println("invalid host");
		return false;
	    } catch (IOException e) {
		System.out.println("invalid host");
		return false;
	    }

	try {
	    os=new PrintWriter(s.getOutputStream());
	    ir=new InputStreamReader(s.getInputStream());
	    br=new BufferedReader(ir);			

	} catch (IOException e) {
	    System.out.println("IO no work");	
	    return false;      
	}

		
	System.out.println("connected...");

	return true;						
    }

    static String[] parsecapabilities(String line)
    {
	String[] ret = new String[100];
	int size = 0;
	String tmp;
	int pos = 0;

	while (pos < line.length() )
	{
	    char c;
	    tmp = "";

	    do {
		c = line.charAt(pos);
		tmp+=c;
		pos++;
	    } while ((c!=' ') && (pos < line.length()));
	    
	    if (tmp.startsWith("AUTH=")==true)
	    {
		ret[size++] = tmp.substring(5);
	    }
	}
	
	return ret;
    }

    static String[] askcapabilities()
    {
	String line;
	String mechs[];
	
	try {
	    
	    send(". CAPABILITY");
	    
	    do {
		line = br.readLine();
	    } while (line.startsWith("* CAPABILITY")==false);
	    
	    mechs = parsecapabilities(line);

	    do {
		line = br.readLine();
	    } while (line.startsWith(".")==false);

	} catch (IOException e) {
	    System.out.println("IO no work");	
	    return null;      
	}
	
	return mechs;
    }

    static SaslClient start_sasl(String[] mechs, String remoteserver, String localaddr, int minssf, int maxssf)
    {
	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",localaddr);
	props.put("javax.security.sasl.ip.remote",remoteserver);

	Handler cbh = new Handler();

	try {
	    conn = Sasl.createSaslClient(mechs,
					 "tmartin",
					 "imap",
					 remoteserver,
					 props,
					 cbh);

	    if (conn == null) {
		System.out.println("conn is null");
	    }
	    
	    if (conn.hasInitialResponse()) {
		/* xxx */
	    }

	    send(". AUTHENTICATE "+conn.getMechanismName());

	    do {

		String line = br.readLine();

		if (line.startsWith("+ ")==true) {

		    line = line.substring(2);

		    byte[] in = SaslUtils.decode64(line);

		    byte[] out = conn.evaluateChallenge(in);

		    String outline = SaslUtils.encode64(out);

		    send(outline);

		} else if (line.startsWith(". OK")==true) {
		    System.out.println("S: " + line);

		    if (conn.isComplete()==false) {
			System.out.println("Something funny going on here...");
			System.exit(1);
		    }
		    return conn;
		} else {
		    System.out.println("S: "+ line);
		    /* authentication failed */
		    return null;
		} 

	    } while (true);
	    
	} catch (SaslException e) {
	    System.out.println("SASL exception\n");
	} catch (IOException e) {
	    System.out.println("IO exception\n");
	}

	return null;
    }

    static void be_interactive(SaslClient conn)
    {
	try {
	    InputStream saslin = conn.getInputStream(s.getInputStream());
	    OutputStream saslout = conn.getOutputStream(s.getOutputStream());
	    int len;
	    byte[] arr;

	    while (true)
	     {
		 if ((len = System.in.available())>0) {

		     /* read from keyboard */
		     arr = new byte[len+1];
		     System.in.read(arr,0,len);

		     if (arr[len-1]=='\n') {
			 arr[len-1]= (byte) '\r';
			 arr[len]= (byte) '\n';
		     }
		     
		     /* write out to stream */		     
		     saslout.write(arr);
		     saslout.flush();

		 } else if ((len = saslin.available())>0) {

		     /* read from socket */
		     arr = new byte[len];
		     saslin.read(arr);

		     System.out.print(new String(arr));

		 } else {
		     /* sleep */
		 }
	     }

	} catch (SaslException e) {

	} catch (IOException e) {

	}

	
    }

    static void usage()
    {
	System.out.println("Usage:");
	System.out.println("jimtest [-k minssf] [-l maxssf] [-m mech] [-p port] server");
	System.exit(1);
    }

    public static void main (String args[])
    {
	String[] mechs;
	SaslClient conn;

	String arg;
	int i = 0;
	int minssf = 0;
	int maxssf = 9999;
	String onemech = null;
	int port = 143;

        while ((i < (args.length-1) ) && (args[i].startsWith("-"))) {
	    arg = args[i++];
	    	    
	    // use this type of check for arguments that require arguments
	    if (arg.equals("-k")) {
		if (i < args.length)
		    minssf = Integer.parseInt(args[i++]);
		else {
		    System.err.println("-k requires a number");
		    usage();
		}
	    } else if (arg.equals("-l")) {
		if (i < args.length)
		    maxssf = Integer.parseInt(args[i++]);
		else {
		    System.err.println("-l requires a number");
		    usage();
		}
	    } else if (arg.equals("-m")) {
		if (i < args.length)
		    onemech = args[i++];
		else {
		    System.err.println("-m requires parameter");
		    usage();
		}
	    } else if (arg.equals("-p")) {
		if (i < args.length)
		    port = Integer.parseInt(args[i++]);
		else {
		    System.err.println("-p requires a number");
		    usage();
		}
	    } else {
		usage();
	    }
	}

	if (i != args.length-1) usage();

	String servername = args[i];

	if (connect(servername,port)==false) {
	    System.out.println("Unable to connect to host: "+servername);
	    System.exit(1);
	}

	mechs = askcapabilities();

	if (onemech!=null) {
	    mechs = new String[1];
	    mechs[0]=onemech;
	}

	conn = start_sasl(mechs,servername, s.getLocalAddress().getHostName(), minssf,maxssf);

	if (conn == null) {
	    System.out.println("Authentication failed");
	    System.exit(1);
	}

	be_interactive(conn);
    }


}