1. /*
  2. * @(#)JndiLoginModule.java 1.11 04/05/05
  3. *
  4. * Copyright 2004 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.11, 05/05/04
  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<String,?> sharedState,
  189. Map<String,?> options) {
  190. this.subject = subject;
  191. this.callbackHandler = callbackHandler;
  192. this.sharedState = sharedState;
  193. this.options = options;
  194. // initialize any configured options
  195. debug = "true".equalsIgnoreCase((String)options.get("debug"));
  196. strongDebug =
  197. "true".equalsIgnoreCase((String)options.get("strongDebug"));
  198. userProvider = (String)options.get(USER_PROVIDER);
  199. groupProvider = (String)options.get(GROUP_PROVIDER);
  200. tryFirstPass =
  201. "true".equalsIgnoreCase((String)options.get("tryFirstPass"));
  202. useFirstPass =
  203. "true".equalsIgnoreCase((String)options.get("useFirstPass"));
  204. storePass =
  205. "true".equalsIgnoreCase((String)options.get("storePass"));
  206. clearPass =
  207. "true".equalsIgnoreCase((String)options.get("clearPass"));
  208. }
  209. /**
  210. * <p> Prompt for username and password.
  211. * Verify the password against the relevant name service.
  212. *
  213. * <p>
  214. *
  215. * @return true always, since this <code>LoginModule</code>
  216. * should not be ignored.
  217. *
  218. * @exception FailedLoginException if the authentication fails. <p>
  219. *
  220. * @exception LoginException if this <code>LoginModule</code>
  221. * is unable to perform the authentication.
  222. */
  223. public boolean login() throws LoginException {
  224. if (userProvider == null) {
  225. throw new LoginException
  226. ("Error: Unable to locate JNDI user provider");
  227. }
  228. if (groupProvider == null) {
  229. throw new LoginException
  230. ("Error: Unable to locate JNDI group provider");
  231. }
  232. if (debug) {
  233. System.out.println("\t\t[JndiLoginModule] user provider: " +
  234. userProvider);
  235. System.out.println("\t\t[JndiLoginModule] group provider: " +
  236. groupProvider);
  237. }
  238. // attempt the authentication
  239. if (tryFirstPass) {
  240. try {
  241. // attempt the authentication by getting the
  242. // username and password from shared state
  243. attemptAuthentication(true);
  244. // authentication succeeded
  245. succeeded = true;
  246. if (debug) {
  247. System.out.println("\t\t[JndiLoginModule] " +
  248. "tryFirstPass succeeded");
  249. }
  250. return true;
  251. } catch (LoginException le) {
  252. // authentication failed -- try again below by prompting
  253. cleanState();
  254. if (debug) {
  255. System.out.println("\t\t[JndiLoginModule] " +
  256. "tryFirstPass failed with:" +
  257. le.toString());
  258. }
  259. }
  260. } else if (useFirstPass) {
  261. try {
  262. // attempt the authentication by getting the
  263. // username and password from shared state
  264. attemptAuthentication(true);
  265. // authentication succeeded
  266. succeeded = true;
  267. if (debug) {
  268. System.out.println("\t\t[JndiLoginModule] " +
  269. "useFirstPass succeeded");
  270. }
  271. return true;
  272. } catch (LoginException le) {
  273. // authentication failed
  274. cleanState();
  275. if (debug) {
  276. System.out.println("\t\t[JndiLoginModule] " +
  277. "useFirstPass failed");
  278. }
  279. throw le;
  280. }
  281. }
  282. // attempt the authentication by prompting for the username and pwd
  283. try {
  284. attemptAuthentication(false);
  285. // authentication succeeded
  286. succeeded = true;
  287. if (debug) {
  288. System.out.println("\t\t[JndiLoginModule] " +
  289. "regular authentication succeeded");
  290. }
  291. return true;
  292. } catch (LoginException le) {
  293. cleanState();
  294. if (debug) {
  295. System.out.println("\t\t[JndiLoginModule] " +
  296. "regular authentication failed");
  297. }
  298. throw le;
  299. }
  300. }
  301. /**
  302. * Abstract method to commit the authentication process (phase 2).
  303. *
  304. * <p> This method is called if the LoginContext's
  305. * overall authentication succeeded
  306. * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
  307. * succeeded).
  308. *
  309. * <p> If this LoginModule's own authentication attempt
  310. * succeeded (checked by retrieving the private state saved by the
  311. * <code>login</code> method), then this method associates a
  312. * <code>UnixPrincipal</code>
  313. * with the <code>Subject</code> located in the
  314. * <code>LoginModule</code>. If this LoginModule's own
  315. * authentication attempted failed, then this method removes
  316. * any state that was originally saved.
  317. *
  318. * <p>
  319. *
  320. * @exception LoginException if the commit fails
  321. *
  322. * @return true if this LoginModule's own login and commit
  323. * attempts succeeded, or false otherwise.
  324. */
  325. public boolean commit() throws LoginException {
  326. if (succeeded == false) {
  327. return false;
  328. } else {
  329. if (subject.isReadOnly()) {
  330. cleanState();
  331. throw new LoginException ("Subject is Readonly");
  332. }
  333. // add Principals to the Subject
  334. if (!subject.getPrincipals().contains(userPrincipal))
  335. subject.getPrincipals().add(userPrincipal);
  336. if (!subject.getPrincipals().contains(UIDPrincipal))
  337. subject.getPrincipals().add(UIDPrincipal);
  338. if (!subject.getPrincipals().contains(GIDPrincipal))
  339. subject.getPrincipals().add(GIDPrincipal);
  340. for (int i = 0; i < supplementaryGroups.size(); i++) {
  341. if (!subject.getPrincipals().contains
  342. ((UnixNumericGroupPrincipal)supplementaryGroups.get(i)))
  343. subject.getPrincipals().add((UnixNumericGroupPrincipal)
  344. supplementaryGroups.get(i));
  345. }
  346. if (debug) {
  347. System.out.println("\t\t[JndiLoginModule]: " +
  348. "added UnixPrincipal,");
  349. System.out.println("\t\t\t\tUnixNumericUserPrincipal,");
  350. System.out.println("\t\t\t\tUnixNumericGroupPrincipal(s),");
  351. System.out.println("\t\t\t to Subject");
  352. }
  353. }
  354. // in any case, clean out state
  355. cleanState();
  356. commitSucceeded = true;
  357. return true;
  358. }
  359. /**
  360. * <p> This method is called if the LoginContext's
  361. * overall authentication failed.
  362. * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
  363. * did not succeed).
  364. *
  365. * <p> If this LoginModule's own authentication attempt
  366. * succeeded (checked by retrieving the private state saved by the
  367. * <code>login</code> and <code>commit</code> methods),
  368. * then this method cleans up any state that was originally saved.
  369. *
  370. * <p>
  371. *
  372. * @exception LoginException if the abort fails.
  373. *
  374. * @return false if this LoginModule's own login and/or commit attempts
  375. * failed, and true otherwise.
  376. */
  377. public boolean abort() throws LoginException {
  378. if (debug)
  379. System.out.println("\t\t[JndiLoginModule]: " +
  380. "aborted authentication failed");
  381. if (succeeded == false) {
  382. return false;
  383. } else if (succeeded == true && commitSucceeded == false) {
  384. // Clean out state
  385. succeeded = false;
  386. cleanState();
  387. userPrincipal = null;
  388. UIDPrincipal = null;
  389. GIDPrincipal = null;
  390. supplementaryGroups = new LinkedList();
  391. } else {
  392. // overall authentication succeeded and commit succeeded,
  393. // but someone else's commit failed
  394. logout();
  395. }
  396. return true;
  397. }
  398. /**
  399. * Logout a user.
  400. *
  401. * <p> This method removes the Principals
  402. * that were added by the <code>commit</code> method.
  403. *
  404. * <p>
  405. *
  406. * @exception LoginException if the logout fails.
  407. *
  408. * @return true in all cases since this <code>LoginModule</code>
  409. * should not be ignored.
  410. */
  411. public boolean logout() throws LoginException {
  412. if (subject.isReadOnly()) {
  413. cleanState();
  414. throw new LoginException ("Subject is Readonly");
  415. }
  416. subject.getPrincipals().remove(userPrincipal);
  417. subject.getPrincipals().remove(UIDPrincipal);
  418. subject.getPrincipals().remove(GIDPrincipal);
  419. for (int i = 0; i < supplementaryGroups.size(); i++) {
  420. subject.getPrincipals().remove
  421. ((UnixNumericGroupPrincipal)supplementaryGroups.get(i));
  422. }
  423. // clean out state
  424. cleanState();
  425. succeeded = false;
  426. commitSucceeded = false;
  427. userPrincipal = null;
  428. UIDPrincipal = null;
  429. GIDPrincipal = null;
  430. supplementaryGroups = new LinkedList();
  431. if (debug) {
  432. System.out.println("\t\t[JndiLoginModule]: " +
  433. "logged out Subject");
  434. }
  435. return true;
  436. }
  437. /**
  438. * Attempt authentication
  439. *
  440. * <p>
  441. *
  442. * @param getPasswdFromSharedState boolean that tells this method whether
  443. * to retrieve the password from the sharedState.
  444. */
  445. private void attemptAuthentication(boolean getPasswdFromSharedState)
  446. throws LoginException {
  447. String encryptedPassword = null;
  448. // first get the username and password
  449. getUsernamePassword(getPasswdFromSharedState);
  450. try {
  451. // get the user's passwd entry from the user provider URL
  452. InitialContext iCtx = new InitialContext();
  453. ctx = (DirContext)iCtx.lookup(userProvider);
  454. /*
  455. SearchControls controls = new SearchControls
  456. (SearchControls.ONELEVEL_SCOPE,
  457. 0,
  458. 5000,
  459. new String[] { USER_PWD },
  460. false,
  461. false);
  462. */
  463. SearchControls controls = new SearchControls();
  464. NamingEnumeration ne = ctx.search("",
  465. "(uid=" + username + ")",
  466. controls);
  467. if (ne.hasMore()) {
  468. SearchResult result = (SearchResult)ne.next();
  469. Attributes attributes = result.getAttributes();
  470. // get the password
  471. // this module works only if the LDAP directory server
  472. // is configured to permit read access to the userPassword
  473. // attribute. The directory administrator need to grant
  474. // this access.
  475. //
  476. // A workaround would be to make the server do authentication
  477. // by setting the Context.SECURITY_PRINCIPAL
  478. // and Context.SECURITY_CREDENTIALS property.
  479. // However, this would make it not work with systems that
  480. // don't do authentication at the server (like NIS).
  481. //
  482. // Setting the SECURITY_* properties and using "simple"
  483. // authentication for LDAP is recommended only for secure
  484. // channels. For nonsecure channels, SSL is recommended.
  485. Attribute pwd = attributes.get(USER_PWD);
  486. String encryptedPwd = new String((byte[])pwd.get(), "UTF8");
  487. encryptedPassword = encryptedPwd.substring(CRYPT.length());
  488. // check the password
  489. if (verifyPassword
  490. (encryptedPassword, new String(password)) == true) {
  491. // authentication succeeded
  492. if (debug)
  493. System.out.println("\t\t[JndiLoginModule] " +
  494. "attemptAuthentication() succeeded");
  495. } else {
  496. // authentication failed
  497. if (debug)
  498. System.out.println("\t\t[JndiLoginModule] " +
  499. "attemptAuthentication() failed");
  500. throw new FailedLoginException("Login incorrect");
  501. }
  502. // save input as shared state only if
  503. // authentication succeeded
  504. if (storePass &&
  505. !sharedState.containsKey(NAME) &&
  506. !sharedState.containsKey(PWD)) {
  507. sharedState.put(NAME, username);
  508. sharedState.put(PWD, password);
  509. }
  510. // create the user principal
  511. userPrincipal = new UnixPrincipal(username);
  512. // get the UID
  513. Attribute uid = attributes.get(USER_UID);
  514. String uidNumber = (String)uid.get();
  515. UIDPrincipal = new UnixNumericUserPrincipal(uidNumber);
  516. if (debug && uidNumber != null) {
  517. System.out.println("\t\t[JndiLoginModule] " +
  518. "user: '" + username + "' has UID: " +
  519. uidNumber);
  520. }
  521. // get the GID
  522. Attribute gid = attributes.get(USER_GID);
  523. String gidNumber = (String)gid.get();
  524. GIDPrincipal = new UnixNumericGroupPrincipal
  525. (gidNumber, true);
  526. if (debug && gidNumber != null) {
  527. System.out.println("\t\t[JndiLoginModule] " +
  528. "user: '" + username + "' has GID: " +
  529. gidNumber);
  530. }
  531. // get the supplementary groups from the group provider URL
  532. ctx = (DirContext)iCtx.lookup(groupProvider);
  533. ne = ctx.search("", new BasicAttributes("memberUid", username));
  534. while (ne.hasMore()) {
  535. result = (SearchResult)ne.next();
  536. attributes = result.getAttributes();
  537. gid = attributes.get(GROUP_ID);
  538. String suppGid = (String)gid.get();
  539. if (!gidNumber.equals(suppGid)) {
  540. UnixNumericGroupPrincipal suppPrincipal =
  541. new UnixNumericGroupPrincipal(suppGid, false);
  542. supplementaryGroups.add(suppPrincipal);
  543. if (debug && suppGid != null) {
  544. System.out.println("\t\t[JndiLoginModule] " +
  545. "user: '" + username +
  546. "' has Supplementary Group: " +
  547. suppGid);
  548. }
  549. }
  550. }
  551. } else {
  552. // bad username
  553. if (debug) {
  554. System.out.println("\t\t[JndiLoginModule]: User not found");
  555. }
  556. throw new FailedLoginException("User not found");
  557. }
  558. } catch (NamingException ne) {
  559. // bad username
  560. if (debug) {
  561. System.out.println("\t\t[JndiLoginModule]: User not found");
  562. ne.printStackTrace();
  563. }
  564. throw new FailedLoginException("User not found");
  565. } catch (java.io.UnsupportedEncodingException uee) {
  566. // password stored in incorrect format
  567. if (debug) {
  568. System.out.println("\t\t[JndiLoginModule]: " +
  569. "password incorrectly encoded");
  570. uee.printStackTrace();
  571. }
  572. throw new LoginException("Login failure due to incorrect " +
  573. "password encoding in the password database");
  574. }
  575. // authentication succeeded
  576. }
  577. /**
  578. * Get the username and password.
  579. * This method does not return any value.
  580. * Instead, it sets global name and password variables.
  581. *
  582. * <p> Also note that this method will set the username and password
  583. * values in the shared state in case subsequent LoginModules
  584. * want to use them via use/tryFirstPass.
  585. *
  586. * <p>
  587. *
  588. * @param getPasswdFromSharedState boolean that tells this method whether
  589. * to retrieve the password from the sharedState.
  590. */
  591. private void getUsernamePassword(boolean getPasswdFromSharedState)
  592. throws LoginException {
  593. if (getPasswdFromSharedState) {
  594. // use the password saved by the first module in the stack
  595. username = (String)sharedState.get(NAME);
  596. password = (char[])sharedState.get(PWD);
  597. return;
  598. }
  599. // prompt for a username and password
  600. if (callbackHandler == null)
  601. throw new LoginException("Error: no CallbackHandler available " +
  602. "to garner authentication information from the user");
  603. String protocol = userProvider.substring(0, userProvider.indexOf(":"));
  604. Callback[] callbacks = new Callback[2];
  605. callbacks[0] = new NameCallback(protocol + " "
  606. + rb.getString("username: "));
  607. callbacks[1] = new PasswordCallback(protocol + " " +
  608. rb.getString("password: "),
  609. false);
  610. try {
  611. callbackHandler.handle(callbacks);
  612. username = ((NameCallback)callbacks[0]).getName();
  613. char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
  614. password = new char[tmpPassword.length];
  615. System.arraycopy(tmpPassword, 0,
  616. password, 0, tmpPassword.length);
  617. ((PasswordCallback)callbacks[1]).clearPassword();
  618. } catch (java.io.IOException ioe) {
  619. throw new LoginException(ioe.toString());
  620. } catch (UnsupportedCallbackException uce) {
  621. throw new LoginException("Error: " + uce.getCallback().toString() +
  622. " not available to garner authentication information " +
  623. "from the user");
  624. }
  625. // print debugging information
  626. if (strongDebug) {
  627. System.out.println("\t\t[JndiLoginModule] " +
  628. "user entered username: " +
  629. username);
  630. System.out.print("\t\t[JndiLoginModule] " +
  631. "user entered password: ");
  632. for (int i = 0; i < password.length; i++)
  633. System.out.print(password[i]);
  634. System.out.println();
  635. }
  636. }
  637. /**
  638. * Verify a password against the encrypted passwd from /etc/shadow
  639. */
  640. private boolean verifyPassword(String encryptedPassword, String password) {
  641. if (encryptedPassword == null)
  642. return false;
  643. Crypt c = new Crypt();
  644. try {
  645. byte oldCrypt[] = encryptedPassword.getBytes("UTF8");
  646. byte newCrypt[] = c.crypt(password.getBytes("UTF8"),
  647. oldCrypt);
  648. if (newCrypt.length != oldCrypt.length)
  649. return false;
  650. for (int i = 0; i < newCrypt.length; i++) {
  651. if (oldCrypt[i] != newCrypt[i])
  652. return false;
  653. }
  654. } catch (java.io.UnsupportedEncodingException uee) {
  655. // cannot happen, but return false just to be safe
  656. return false;
  657. }
  658. return true;
  659. }
  660. /**
  661. * Clean out state because of a failed authentication attempt
  662. */
  663. private void cleanState() {
  664. username = null;
  665. if (password != null) {
  666. for (int i = 0; i < password.length; i++)
  667. password[i] = ' ';
  668. password = null;
  669. }
  670. ctx = null;
  671. if (clearPass) {
  672. sharedState.remove(NAME);
  673. sharedState.remove(PWD);
  674. }
  675. }
  676. }