4 import java.util.Hashtable;
9 private static PrintWriter os=null;
10 private static InputStreamReader ir=null;
11 private static Socket s=null;
12 private static BufferedReader br=null;
14 static void send(String str)
21 static boolean connect(String servername, int port)
25 s=new Socket(servername,port);
26 } catch (UnknownHostException e){
27 System.out.println("invalid host");
29 } catch (IOException e) {
30 System.out.println("invalid host");
35 os=new PrintWriter(s.getOutputStream());
36 ir=new InputStreamReader(s.getInputStream());
37 br=new BufferedReader(ir);
39 } catch (IOException e) {
40 System.out.println("IO no work");
45 System.out.println("connected...");
50 static String[] parsecapabilities(String line)
52 String[] ret = new String[100];
57 while (pos < line.length() )
66 } while ((c!=' ') && (pos < line.length()));
68 if (tmp.startsWith("AUTH=")==true)
70 ret[size++] = tmp.substring(5);
77 static String[] askcapabilities()
88 } while (line.startsWith("* CAPABILITY")==false);
90 mechs = parsecapabilities(line);
94 } while (line.startsWith(".")==false);
96 } catch (IOException e) {
97 System.out.println("IO no work");
104 static SaslClient start_sasl(String[] mechs, String remoteserver, String localaddr, int minssf, int maxssf)
107 Hashtable props = new Hashtable();
108 props.put("javax.security.sasl.encryption.minimum",String.valueOf(minssf));
109 props.put("javax.security.sasl.encryption.maximum",String.valueOf(maxssf));
110 props.put("javax.security.sasl.ip.local",localaddr);
111 props.put("javax.security.sasl.ip.remote",remoteserver);
113 Handler cbh = new Handler();
116 conn = Sasl.createSaslClient(mechs,
124 System.out.println("conn is null");
127 if (conn.hasInitialResponse()) {
131 send(". AUTHENTICATE "+conn.getMechanismName());
135 String line = br.readLine();
137 if (line.startsWith("+ ")==true) {
139 line = line.substring(2);
141 byte[] in = SaslUtils.decode64(line);
143 byte[] out = conn.evaluateChallenge(in);
145 String outline = SaslUtils.encode64(out);
149 } else if (line.startsWith(". OK")==true) {
150 System.out.println("S: " + line);
152 if (conn.isComplete()==false) {
153 System.out.println("Something funny going on here...");
158 System.out.println("S: "+ line);
159 /* authentication failed */
165 } catch (SaslException e) {
166 System.out.println("SASL exception\n");
167 } catch (IOException e) {
168 System.out.println("IO exception\n");
174 static void be_interactive(SaslClient conn)
177 InputStream saslin = conn.getInputStream(s.getInputStream());
178 OutputStream saslout = conn.getOutputStream(s.getOutputStream());
184 if ((len = System.in.available())>0) {
186 /* read from keyboard */
187 arr = new byte[len+1];
188 System.in.read(arr,0,len);
190 if (arr[len-1]=='\n') {
191 arr[len-1]= (byte) '\r';
192 arr[len]= (byte) '\n';
195 /* write out to stream */
199 } else if ((len = saslin.available())>0) {
201 /* read from socket */
205 System.out.print(new String(arr));
212 } catch (SaslException e) {
214 } catch (IOException e) {
223 System.out.println("Usage:");
224 System.out.println("jimtest [-k minssf] [-l maxssf] [-m mech] [-p port] server");
228 public static void main (String args[])
237 String onemech = null;
240 while ((i < (args.length-1) ) && (args[i].startsWith("-"))) {
243 // use this type of check for arguments that require arguments
244 if (arg.equals("-k")) {
246 minssf = Integer.parseInt(args[i++]);
248 System.err.println("-k requires a number");
251 } else if (arg.equals("-l")) {
253 maxssf = Integer.parseInt(args[i++]);
255 System.err.println("-l requires a number");
258 } else if (arg.equals("-m")) {
262 System.err.println("-m requires parameter");
265 } else if (arg.equals("-p")) {
267 port = Integer.parseInt(args[i++]);
269 System.err.println("-p requires a number");
277 if (i != args.length-1) usage();
279 String servername = args[i];
281 if (connect(servername,port)==false) {
282 System.out.println("Unable to connect to host: "+servername);
286 mechs = askcapabilities();
289 mechs = new String[1];
293 conn = start_sasl(mechs,servername, s.getLocalAddress().getHostName(), minssf,maxssf);
296 System.out.println("Authentication failed");
300 be_interactive(conn);