1. /*
  2. * @(#)JndiLoginModule.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.security.auth.module;
  8. import javax.security.auth.*;
  9. import javax.security.auth.callback.*;
  10. import javax.security.auth.login.*;
  11. import javax.security.auth.spi.*;
  12. import javax.naming.*;
  13. import javax.naming.directory.*;
  14. import java.io.IOException;
  15. import java.util.Map;
  16. import java.util.LinkedList;
  17. import java.util.ResourceBundle;
  18. import com.sun.security.auth.UnixPrincipal;
  19. import com.sun.security.auth.UnixNumericUserPrincipal;
  20. import com.sun.security.auth.UnixNumericGroupPrincipal;
  21. import sun.security.util.AuthResources;
  22. /**
  23. * <p> The module prompts for a username and password
  24. * and then verifies the password against the password stored in
  25. * a directory service configured under JNDI.
  26. *
  27. * <p> This <code>LoginModule</code> interoperates with
  28. * any conformant JNDI service provider. To direct this
  29. * <code>LoginModule</code> to use a specific JNDI service provider,
  30. * two options must be specified in the login <code>Configuration</code>
  31. * for this <code>LoginModule</code>.
  32. * <pre>
  33. * user.provider.url=<b>name_service_url</b>
  34. * group.provider.url=<b>name_service_url</b>
  35. * </pre>
  36. *
  37. * <b>name_service_url</b> specifies
  38. * the directory service and path where this <code>LoginModule</code>
  39. * can access the relevant user and group information. Because this
  40. * <code>LoginModule</code> only performs one-level searches to
  41. * find the relevant user information, the <code>URL</code>
  42. * must point to a directory one level above where the user and group
  43. * information is stored in the directory service.
  44. * For example, to instruct this <code>LoginModule</code>
  45. * to contact a NIS server, the following URLs must be specified:
  46. * <pre>
  47. * user.provider.url="nis://<b>NISServerHostName</b>/<b>NISDomain</b>/user"
  48. * group.provider.url="nis://<b>NISServerHostName</b>/<b>NISDomain</b>/system/group"
  49. * </pre>
  50. *
  51. * <b>NISServerHostName</b> specifies the server host name of the
  52. * NIS server (for example, <i>nis.sun.com</i>, and <b>NISDomain</b>
  53. * specifies the domain for that NIS server (for example, <i>jaas.sun.com</i>.
  54. * To contact an LDAP server, the following URLs must be specified:
  55. * <pre>
  56. * user.provider.url="ldap://<b>LDAPServerHostName</b>/<b>LDAPName</b>"
  57. * group.provider.url="ldap://<b>LDAPServerHostName</b>/<b>LDAPName</b>"
  58. * </pre>
  59. *
  60. * <b>LDAPServerHostName</b> specifies the server host name of the
  61. * LDAP server, which may include a port number
  62. * (for example, <i>ldap.sun.com:389</i>),
  63. * and <b>LDAPName</b> specifies the entry name in the LDAP directory
  64. * (for example, <i>ou=People,o=Sun,c=US</i> and <i>ou=Groups,o=Sun,c=US</i>
  65. * for user and group information, respectively).
  66. *
  67. * <p> The format in which the user's information must be stored in
  68. * the directory service is specified in RFC 2307. Specifically,
  69. * this <code>LoginModule</code> will search for the user's entry in the
  70. * directory service using the user's <i>uid</i> attribute,
  71. * where <i>uid=<b>username</b></i>. If the search succeeds,
  72. * this <code>LoginModule</code> will then
  73. * obtain the user's encrypted password from the retrieved entry
  74. * using the <i>userPassword</i> attribute.
  75. * This <code>LoginModule</code> assumes that the password is stored
  76. * as a byte array, which when converted to a <code>String</code>,
  77. * has the following format:
  78. * <pre>
  79. * "{crypt}<b>encrypted_password</b>"
  80. * </pre>
  81. *
  82. * The LDAP directory server must be configured
  83. * to permit read access to the userPassword attribute.
  84. * If the user entered a valid username and password,
  85. * this <code>LoginModule</code> associates a
  86. * <code>UnixPrincipal</code>, <code>UnixNumericUserPrincipal</code>,
  87. * and the relevant UnixNumericGroupPrincipals with the
  88. * <code>Subject</code>.
  89. *
  90. * <p> This LoginModule also recognizes the following <code>Configuration</code>
  91. * options:
  92. * <pre>
  93. * debug if, true, debug messages are output to System.out.
  94. *
  95. * useFirstPass if, true, this LoginModule retrieves the
  96. * username and password from the module's shared state,
  97. * using "javax.security.auth.login.name" and
  98. * "javax.security.auth.login.password" as the respective
  99. * keys. The retrieved values are used for authentication.
  100. * If authentication fails, no attempt for a retry is made,
  101. * and the failure is reported back to the calling
  102. * application.
  103. *
  104. * tryFirstPass if, true, this LoginModule retrieves the
  105. * the username and password from the module's shared state,
  106. * using "javax.security.auth.login.name" and
  107. * "javax.security.auth.login.password" as the respective
  108. * keys. The retrieved values are used for authentication.
  109. * If authentication fails, the module uses the
  110. * CallbackHandler to retrieve a new username and password,
  111. * and another attempt to authenticate is made.
  112. * If the authentication fails, the failure is reported
  113. * back to the calling application.
  114. *
  115. * storePass if, true, this LoginModule stores the username and password
  116. * obtained from the CallbackHandler in the module's
  117. * shared state, using "javax.security.auth.login.name" and
  118. * "javax.security.auth.login.password" as the respective
  119. * keys. This is not performed if existing values already
  120. * exist for the username and password in the shared state,
  121. * or if authentication fails.
  122. *
  123. * clearPass if, true, this <code>LoginModule</code> clears the
  124. * username and password stored in the module's shared state
  125. * after both phases of authentication (login and commit)
  126. * have completed.
  127. * </pre>
  128. *
  129. * @version 1.8, 01/23/03
  130. */
  131. public class JndiLoginModule implements LoginModule {
  132. static final java.util.ResourceBundle rb =
  133. java.util.ResourceBundle.getBundle("sun.security.util.AuthResources");
  134. /** JNDI Provider */
  135. public final String USER_PROVIDER = "user.provider.url";
  136. public final String GROUP_PROVIDER = "group.provider.url";
  137. // configurable options
  138. private boolean debug = false;
  139. private boolean strongDebug = false;
  140. private String userProvider;
  141. private String groupProvider;
  142. private boolean useFirstPass = false;
  143. private boolean tryFirstPass = false;
  144. private boolean storePass = false;
  145. private boolean clearPass = false;
  146. // the authentication status
  147. private boolean succeeded = false;
  148. private boolean commitSucceeded = false;
  149. // username, password, and JNDI context
  150. private String username;
  151. private char[] password;
  152. DirContext ctx;
  153. // the user (assume it is a UnixPrincipal)
  154. private UnixPrincipal userPrincipal;
  155. private UnixNumericUserPrincipal UIDPrincipal;
  156. private UnixNumericGroupPrincipal GIDPrincipal;
  157. private LinkedList supplementaryGroups = new LinkedList();
  158. // initial state
  159. private Subject subject;
  160. private CallbackHandler callbackHandler;
  161. private Map sharedState;
  162. private Map options;
  163. private static final String CRYPT = "{crypt}";
  164. private static final String USER_PWD = "userPassword";
  165. private static final String USER_UID = "uidNumber";
  166. private static final String USER_GID = "gidNumber";
  167. private static final String GROUP_ID = "gidNumber";
  168. private static final String NAME = "javax.security.auth.login.name";
  169. private static final String PWD = "javax.security.auth.login.password";
  170. /**
  171. * Initialize this <code>LoginModule</code>.
  172. *
  173. * <p>
  174. *
  175. * @param subject the <code>Subject</code> to be authenticated. <p>
  176. *
  177. * @param callbackHandler a <code>CallbackHandler</code> for communicating
  178. * with the end user (prompting for usernames and
  179. * passwords, for example). <p>
  180. *
  181. * @param sharedState shared <code>LoginModule</code> state. <p>
  182. *
  183. * @param options options specified in the login
  184. * <code>Configuration</code> for this particular
  185. * <code>LoginModule</code>.
  186. */
  187. public void initialize(Subject subject, CallbackHandler callbackHandler,
  188. Map sharedState, Map options) {
  189. this.subject = subject;
  190. this.callbackHandler = callbackHandler;
  191. this.sharedState = sharedState;
  192. this.options = options;
  193. // initialize any configured options
  194. debug = "true".equalsIgnoreCase((String)options.get("debug"));
  195. strongDebug =
  196. "true".equalsIgnoreCase((String)options.get("strongDebug"));
  197. userProvider = (String)options.get(USER_PROVIDER);
  198. groupProvider = (String)options.get(GROUP_PROVIDER);
  199. tryFirstPass =
  200. "true".equalsIgnoreCase((String)options.get("tryFirstPass"));
  201. useFirstPass =
  202. "true".equalsIgnoreCase((String)options.get("useFirstPass"));
  203. storePass =
  204. "true".equalsIgnoreCase((String)options.get("storePass"));
  205. clearPass =
  206. "true".equalsIgnoreCase((String)options.get("clearPass"));
  207. }
  208. /**
  209. * <p> Prompt for username and password.
  210. * Verify the password against the relevant name service.
  211. *
  212. * <p>
  213. *
  214. * @return true always, since this <code>LoginModule</code>
  215. * should not be ignored.
  216. *
  217. * @exception FailedLoginException if the authentication fails. <p>
  218. *
  219. * @exception LoginException if this <code>LoginModule</code>
  220. * is unable to perform the authentication.
  221. */
  222. public boolean login() throws LoginException {
  223. if (userProvider == null) {
  224. throw new LoginException
  225. ("Error: Unable to locate JNDI user provider");
  226. }
  227. if (groupProvider == null) {
  228. throw new LoginException
  229. ("Error: Unable to locate JNDI group provider");
  230. }
  231. if (debug) {
  232. System.out.println("\t\t[JndiLoginModule] user provider: " +
  233. userProvider);
  234. System.out.println("\t\t[JndiLoginModule] group provider: " +
  235. groupProvider);
  236. }
  237. // attempt the authentication
  238. if (tryFirstPass) {
  239. try {
  240. // attempt the authentication by getting the
  241. // username and password from shared state
  242. attemptAuthentication(true);
  243. // authentication succeeded
  244. succeeded = true;
  245. if (debug) {
  246. System.out.println("\t\t[JndiLoginModule] " +
  247. "tryFirstPass succeeded");
  248. }
  249. return true;
  250. } catch (LoginException le) {
  251. // authentication failed -- try again below by prompting
  252. cleanState();
  253. if (debug) {
  254. System.out.println("\t\t[JndiLoginModule] " +
  255. "tryFirstPass failed with:" +
  256. le.toString());
  257. }
  258. }
  259. } else if (useFirstPass) {
  260. try {
  261. // attempt the authentication by getting the
  262. // username and password from shared state
  263. attemptAuthentication(true);
  264. // authentication succeeded
  265. succeeded = true;
  266. if (debug) {
  267. System.out.println("\t\t[JndiLoginModule] " +
  268. "useFirstPass succeeded");
  269. }
  270. return true;
  271. } catch (LoginException le) {
  272. // authentication failed
  273. cleanState();
  274. if (debug) {
  275. System.out.println("\t\t[JndiLoginModule] " +
  276. "useFirstPass failed");
  277. }
  278. throw le;
  279. }
  280. }
  281. // attempt the authentication by prompting for the username and pwd
  282. try {
  283. attemptAuthentication(false);
  284. // authentication succeeded
  285. succeeded = true;
  286. if (debug) {
  287. System.out.println("\t\t[JndiLoginModule] " +
  288. "regular authentication succeeded");
  289. }
  290. return true;
  291. } catch (LoginException le) {
  292. cleanState();
  293. if (debug) {
  294. System.out.println("\t\t[JndiLoginModule] " +
  295. "regular authentication failed");
  296. }
  297. throw le;
  298. }
  299. }
  300. /**
  301. * Abstract method to commit the authentication process (phase 2).
  302. *
  303. * <p> This method is called if the LoginContext's
  304. * overall authentication succeeded
  305. * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
  306. * succeeded).
  307. *
  308. * <p> If this LoginModule's own authentication attempt
  309. * succeeded (checked by retrieving the private state saved by the
  310. * <code>login</code> method), then this method associates a
  311. * <code>UnixPrincipal</code>
  312. * with the <code>Subject</code> located in the
  313. * <code>LoginModule</code>. If this LoginModule's own
  314. * authentication attempted failed, then this method removes
  315. * any state that was originally saved.
  316. *
  317. * <p>
  318. *
  319. * @exception LoginException if the commit fails
  320. *
  321. * @return true if this LoginModule's own login and commit
  322. * attempts succeeded, or false otherwise.
  323. */
  324. public boolean commit() throws LoginException {
  325. if (succeeded == false) {
  326. return false;
  327. } else {
  328. if (subject.isReadOnly()) {
  329. cleanState();
  330. throw new LoginException ("Subject is Readonly");
  331. }
  332. // add Principals to the Subject
  333. if (!subject.getPrincipals().contains(userPrincipal))
  334. subject.getPrincipals().add(userPrincipal);
  335. if (!subject.getPrincipals().contains(UIDPrincipal))
  336. subject.getPrincipals().add(UIDPrincipal);
  337. if (!subject.getPrincipals().contains(GIDPrincipal))
  338. subject.getPrincipals().add(GIDPrincipal);
  339. for (int i = 0; i < supplementaryGroups.size(); i++) {
  340. if (!subject.getPrincipals().contains
  341. ((UnixNumericGroupPrincipal)supplementaryGroups.get(i)))
  342. subject.getPrincipals().add((UnixNumericGroupPrincipal)
  343. supplementaryGroups.get(i));
  344. }
  345. if (debug) {
  346. System.out.println("\t\t[JndiLoginModule]: " +
  347. "added UnixPrincipal,");
  348. System.out.println("\t\t\t\tUnixNumericUserPrincipal,");
  349. System.out.println("\t\t\t\tUnixNumericGroupPrincipal(s),");
  350. System.out.println("\t\t\t to Subject");
  351. }
  352. }
  353. // in any case, clean out state
  354. cleanState();
  355. commitSucceeded = true;
  356. return true;
  357. }
  358. /**
  359. * <p> This method is called if the LoginContext's
  360. * overall authentication failed.
  361. * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
  362. * did not succeed).
  363. *
  364. * <p> If this LoginModule's own authentication attempt
  365. * succeeded (checked by retrieving the private state saved by the
  366. * <code>login</code> and <code>commit</code> methods),
  367. * then this method cleans up any state that was originally saved.
  368. *
  369. * <p>
  370. *
  371. * @exception LoginException if the abort fails.
  372. *
  373. * @return false if this LoginModule's own login and/or commit attempts
  374. * failed, and true otherwise.
  375. */
  376. public boolean abort() throws LoginException {
  377. if (debug)
  378. System.out.println("\t\t[JndiLoginModule]: " +
  379. "aborted authentication failed");
  380. if (succeeded == false) {
  381. return false;
  382. } else if (succeeded == true && commitSucceeded == false) {
  383. // Clean out state
  384. succeeded = false;
  385. cleanState();
  386. userPrincipal = null;
  387. UIDPrincipal = null;
  388. GIDPrincipal = null;
  389. supplementaryGroups = new LinkedList();
  390. } else {
  391. // overall authentication succeeded and commit succeeded,
  392. // but someone else's commit failed
  393. logout();
  394. }
  395. return true;
  396. }
  397. /**
  398. * Logout a user.
  399. *
  400. * <p> This method removes the Principals
  401. * that were added by the <code>commit</code> method.
  402. *
  403. * <p>
  404. *
  405. * @exception LoginException if the logout fails.
  406. *
  407. * @return true in all cases since this <code>LoginModule</code>
  408. * should not be ignored.
  409. */
  410. public boolean logout() throws LoginException {
  411. if (subject.isReadOnly()) {
  412. cleanState();
  413. throw new LoginException ("Subject is Readonly");
  414. }
  415. subject.getPrincipals().remove(userPrincipal);
  416. subject.getPrincipals().remove(UIDPrincipal);
  417. subject.getPrincipals().remove(GIDPrincipal);
  418. for (int i = 0; i < supplementaryGroups.size(); i++) {
  419. subject.getPrincipals().remove
  420. ((UnixNumericGroupPrincipal)supplementaryGroups.get(i));
  421. }
  422. // clean out state
  423. cleanState();
  424. succeeded = false;
  425. commitSucceeded = false;
  426. userPrincipal = null;
  427. UIDPrincipal = null;
  428. GIDPrincipal = null;
  429. supplementaryGroups = new LinkedList();
  430. if (debug) {
  431. System.out.println("\t\t[JndiLoginModule]: " +
  432. "logged out Subject");
  433. }
  434. return true;
  435. }
  436. /**
  437. * Attempt authentication
  438. *
  439. * <p>
  440. *
  441. * @param getPasswdFromSharedState boolean that tells this method whether
  442. * to retrieve the password from the sharedState.
  443. */
  444. private void attemptAuthentication(boolean getPasswdFromSharedState)
  445. throws LoginException {
  446. String encryptedPassword = null;
  447. // first get the username and password
  448. getUsernamePassword(getPasswdFromSharedState);
  449. try {
  450. // get the user's passwd entry from the user provider URL
  451. InitialContext iCtx = new InitialContext();
  452. ctx = (DirContext)iCtx.lookup(userProvider);
  453. /*
  454. SearchControls controls = new SearchControls
  455. (SearchControls.ONELEVEL_SCOPE,
  456. 0,
  457. 5000,
  458. new String[] { USER_PWD },
  459. false,
  460. false);
  461. */
  462. SearchControls controls = new SearchControls();
  463. NamingEnumeration ne = ctx.search("",
  464. "(uid=" + username + ")",
  465. controls);
  466. if (ne.hasMore()) {
  467. SearchResult result = (SearchResult)ne.next();
  468. Attributes attributes = result.getAttributes();
  469. // get the password
  470. // this module works only if the LDAP directory server
  471. // is configured to permit read access to the userPassword
  472. // attribute. The directory administrator need to grant
  473. // this access.
  474. //
  475. // A workaround would be to make the server do authentication
  476. // by setting the Context.SECURITY_PRINCIPAL
  477. // and Context.SECURITY_CREDENTIALS property.
  478. // However, this would make it not work with systems that
  479. // don't do authentication at the server (like NIS).
  480. //
  481. // Setting the SECURITY_* properties and using "simple"
  482. // authentication for LDAP is recommended only for secure
  483. // channels. For nonsecure channels, SSL is recommended.
  484. Attribute pwd = attributes.get(USER_PWD);
  485. String encryptedPwd = new String((byte[])pwd.get(), "UTF8");
  486. encryptedPassword = encryptedPwd.substring(CRYPT.length());
  487. // check the password
  488. if (verifyPassword
  489. (encryptedPassword, new String(password)) == true) {
  490. // authentication succeeded
  491. if (debug)
  492. System.out.println("\t\t[JndiLoginModule] " +
  493. "attemptAuthentication() succeeded");
  494. } else {
  495. // authentication failed
  496. if (debug)
  497. System.out.println("\t\t[JndiLoginModule] " +
  498. "attemptAuthentication() failed");
  499. throw new FailedLoginException("Login incorrect");
  500. }
  501. // save input as shared state only if
  502. // authentication succeeded
  503. if (storePass &&
  504. !sharedState.containsKey(NAME) &&
  505. !sharedState.containsKey(PWD)) {
  506. sharedState.put(NAME, username);
  507. sharedState.put(PWD, password);
  508. }
  509. // create the user principal
  510. userPrincipal = new UnixPrincipal(username);
  511. // get the UID
  512. Attribute uid = attributes.get(USER_UID);
  513. String uidNumber = (String)uid.get();
  514. UIDPrincipal = new UnixNumericUserPrincipal(uidNumber);
  515. if (debug && uidNumber != null) {
  516. System.out.println("\t\t[JndiLoginModule] " +
  517. "user: '" + username + "' has UID: " +
  518. uidNumber);
  519. }
  520. // get the GID
  521. Attribute gid = attributes.get(USER_GID);
  522. String gidNumber = (String)gid.get();
  523. GIDPrincipal = new UnixNumericGroupPrincipal
  524. (gidNumber, true);
  525. if (debug && gidNumber != null) {
  526. System.out.println("\t\t[JndiLoginModule] " +
  527. "user: '" + username + "' has GID: " +
  528. gidNumber);
  529. }
  530. // get the supplementary groups from the group provider URL
  531. ctx = (DirContext)iCtx.lookup(groupProvider);
  532. ne = ctx.search("", new BasicAttributes("memberUid", username));
  533. while (ne.hasMore()) {
  534. result = (SearchResult)ne.next();
  535. attributes = result.getAttributes();
  536. gid = attributes.get(GROUP_ID);
  537. String suppGid = (String)gid.get();
  538. if (!gidNumber.equals(suppGid)) {
  539. UnixNumericGroupPrincipal suppPrincipal =
  540. new UnixNumericGroupPrincipal(suppGid, false);
  541. supplementaryGroups.add(suppPrincipal);
  542. if (debug && suppGid != null) {
  543. System.out.println("\t\t[JndiLoginModule] " +
  544. "user: '" + username +
  545. "' has Supplementary Group: " +
  546. suppGid);
  547. }
  548. }
  549. }
  550. } else {
  551. // bad username
  552. if (debug) {
  553. System.out.println("\t\t[JndiLoginModule]: User not found");
  554. }
  555. throw new FailedLoginException("User not found");
  556. }
  557. } catch (NamingException ne) {
  558. // bad username
  559. if (debug) {
  560. System.out.println("\t\t[JndiLoginModule]: User not found");
  561. ne.printStackTrace();
  562. }
  563. throw new FailedLoginException("User not found");
  564. } catch (java.io.UnsupportedEncodingException uee) {
  565. // password stored in incorrect format
  566. if (debug) {
  567. System.out.println("\t\t[JndiLoginModule]: " +
  568. "password incorrectly encoded");
  569. uee.printStackTrace();
  570. }
  571. throw new LoginException("Login failure due to incorrect " +
  572. "password encoding in the password database");
  573. }
  574. // authentication succeeded
  575. }
  576. /**
  577. * Get the username and password.
  578. * This method does not return any value.
  579. * Instead, it sets global name and password variables.
  580. *
  581. * <p> Also note that this method will set the username and password
  582. * values in the shared state in case subsequent LoginModules
  583. * want to use them via use/tryFirstPass.
  584. *
  585. * <p>
  586. *
  587. * @param getPasswdFromSharedState boolean that tells this method whether
  588. * to retrieve the password from the sharedState.
  589. */
  590. private void getUsernamePassword(boolean getPasswdFromSharedState)
  591. throws LoginException {
  592. if (getPasswdFromSharedState) {
  593. // use the password saved by the first module in the stack
  594. username = (String)sharedState.get(NAME);
  595. password = (char[])sharedState.get(PWD);
  596. return;
  597. }
  598. // prompt for a username and password
  599. if (callbackHandler == null)
  600. throw new LoginException("Error: no CallbackHandler available " +
  601. "to garner authentication information from the user");
  602. String protocol = userProvider.substring(0, userProvider.indexOf(":"));
  603. Callback[] callbacks = new Callback[2];
  604. callbacks[0] = new NameCallback(protocol + " "
  605. + rb.getString("username: "));
  606. callbacks[1] = new PasswordCallback(protocol + " " +
  607. rb.getString("password: "),
  608. false);
  609. try {
  610. callbackHandler.handle(callbacks);
  611. username = ((NameCallback)callbacks[0]).getName();
  612. char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
  613. password = new char[tmpPassword.length];
  614. System.arraycopy(tmpPassword, 0,
  615. password, 0, tmpPassword.length);
  616. ((PasswordCallback)callbacks[1]).clearPassword();
  617. } catch (java.io.IOException ioe) {
  618. throw new LoginException(ioe.toString());
  619. } catch (UnsupportedCallbackException uce) {
  620. throw new LoginException("Error: " + uce.getCallback().toString() +
  621. " not available to garner authentication information " +
  622. "from the user");
  623. }
  624. // print debugging information
  625. if (strongDebug) {
  626. System.out.println("\t\t[JndiLoginModule] " +
  627. "user entered username: " +
  628. username);
  629. System.out.print("\t\t[JndiLoginModule] " +
  630. "user entered password: ");
  631. for (int i = 0; i < password.length; i++)
  632. System.out.print(password[i]);
  633. System.out.println();
  634. }
  635. }
  636. /**
  637. * Verify a password against the encrypted passwd from /etc/shadow
  638. */
  639. private boolean verifyPassword(String encryptedPassword, String password) {
  640. if (encryptedPassword == null)
  641. return false;
  642. Crypt c = new Crypt();
  643. byte oldCrypt[] = encryptedPassword.getBytes();
  644. byte newCrypt[] = c.crypt(password.getBytes(),
  645. oldCrypt);
  646. if (newCrypt.length != oldCrypt.length)
  647. return false;
  648. for (int i = 0; i < newCrypt.length; i++) {
  649. if (oldCrypt[i] != newCrypt[i])
  650. return false;
  651. }
  652. return true;
  653. }
  654. /**
  655. * Clean out state because of a failed authentication attempt
  656. */
  657. private void cleanState() {
  658. username = null;
  659. if (password != null) {
  660. for (int i = 0; i < password.length; i++)
  661. password[i] = ' ';
  662. password = null;
  663. }
  664. ctx = null;
  665. if (clearPass) {
  666. sharedState.remove(NAME);
  667. sharedState.remove(PWD);
  668. }
  669. }
  670. }