add CheckPassword code
[org.ibex.crypto.git] / src / org / ibex / crypto / CheckPassword.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.crypto;
6 import org.ibex.util.*;
7 import java.util.*;
8 import java.io.*;
9
10 /**
11  *  Implementation of the "checkpassword" protocol; requires bash and
12  *  /bin/true.
13  *
14  *  Documented here:
15  *
16  *    http://cr.yp.to/checkpwd/interface.html
17  *
18  *  A debian implementation can be found in the "fgetty" package, and
19  *  gets installed in /bin/checkpassword.
20  */
21 public class CheckPassword {
22     public static boolean verify(String user, String pass) {
23         try {
24             Process p =
25                 Runtime.getRuntime().exec(new String[] {
26                     "bash",
27                     "-c",
28                     "checkpassword /bin/true 3<&0"
29                 });
30             PrintWriter pw = new PrintWriter(p.getOutputStream());
31             pw.print(user);
32             pw.print('\0');
33             pw.print(pass);
34             pw.print('\0');
35             // timestamp (ignored)
36             pw.print('\0');
37             pw.flush();
38             pw.close();
39             int exitcode = p.waitFor();
40             // FEATURE: grab errors off of stderr?
41             if (exitcode!=0)
42                 Log.error(CheckPassword.class, "exitcode " + exitcode);
43             return exitcode==0;
44         } catch (Exception e) { Log.warn(CheckPassword.class, e); }
45         return false;
46     }
47 }