001: package org.esupportail.cas.server.handlers.nis.multiple;
002:
003: import java.util.Hashtable;
004:
005: import javax.naming.Context;
006: import javax.naming.InitialContext;
007: import javax.naming.directory.InitialDirContext;
008:
009: import org.dom4j.Element;
010: import org.esupportail.cas.server.util.RedundantHandler;
011: import org.esupportail.cas.server.util.Server;
012: import org.esupportail.cas.server.util.crypt.Crypt;
013: import org.esupportail.cas.server.util.log.Log;
014:
015: /**
016: * This class implements a NIS (Network Information Service) server.
017: *
018: * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
019: */
020: public final class NisServer extends Server {
021:
022: /**
023: * The server hostname or IP address.
024: */
025: private String host;
026:
027: /**
028: * Constructor.
029: *
030: * @param handlerDebug debugging mode of the handler
031: * @param handler the handler the server will be used by
032: * @param serverElement the XML element that declares the server
033: * @throws Exception Exception
034: */
035: public NisServer(final Boolean handlerDebug,
036: final RedundantHandler handler, final Element serverElement)
037: throws Exception {
038: super (handlerDebug, handler, serverElement);
039: traceBegin();
040:
041: host = getServerSubElementContent(serverElement, "host", true/*needed*/);
042: trace("host = " + host);
043:
044: traceEnd();
045: }
046:
047: /**
048: * Try to authenticate a user (by searching into a NIS domain).
049: *
050: * @param username the user's name
051: * @param password the user's password
052: *
053: * @return Server.AUTHENTICATE_SUCCESS, Server.AUTHENTICATE_NOAUTH
054: * or Server.AUTHENTICATE_FAILURE.
055: */
056: public int authenticate(final String username, final String password) {
057: traceBegin();
058:
059: NisHandler handler = (NisHandler) getHandler();
060: String url = "nis://" + host + "/" + handler.getDomain();
061: String map = handler.getMap();
062:
063: try {
064: trace("Connecting to the NIS domain...");
065: Hashtable hashtable = new Hashtable(5, 0.75f);
066: hashtable.put(Context.INITIAL_CONTEXT_FACTORY,
067: "com.sun.jndi.nis.NISCtxFactory");
068: hashtable.put(Context.PROVIDER_URL, url);
069: hashtable.put(Context.SECURITY_AUTHENTICATION, "simple");
070: InitialContext context = new InitialDirContext(hashtable);
071:
072: trace("Retrieving the information corresponding to the user...");
073: String nisEntry = context.lookup(
074: "system/" + map + "/" + username).toString();
075:
076: // we've got all needed information, close the context
077: context.close();
078:
079: trace("Username found, checking password ("
080: + handler.getEncryption() + ")...");
081: // extracting the encrypted password
082: String[] nisFields = nisEntry.split(":");
083: String nisEncryptedPassword = nisFields[1];
084:
085: // compare the passwords
086: boolean match = Crypt.match(handler.getEncryption(),
087: password, nisEncryptedPassword);
088:
089: if (Crypt.match(handler.getEncryption(), password,
090: nisEncryptedPassword)) {
091: trace("Password matches.");
092: traceEnd("AUTHENTICATE_SUCCESS");
093: return AUTHENTICATE_SUCCESS;
094: } else {
095: trace("Password does not match.");
096: traceEnd("AUTHENTICATE_NOAUTH");
097: return AUTHENTICATE_NOAUTH;
098: }
099: } catch (javax.naming.NoInitialContextException e) {
100: Log.warn(e.toString());
101: Log
102: .warn("JNDI nis provider (nis.jar) is probably not installed");
103: traceEnd("AUTHENTICATE_FAILURE");
104: return AUTHENTICATE_FAILURE;
105: } catch (javax.naming.ConfigurationException e) {
106: Log.warn("Bad NIS configuration: " + e.getMessage());
107: traceEnd("AUTHENTICATE_FAILURE");
108: return AUTHENTICATE_FAILURE;
109: } catch (javax.naming.CommunicationException e) {
110: Log.warn("NIS server not responding.");
111: traceEnd("AUTHENTICATE_FAILURE");
112: return AUTHENTICATE_FAILURE;
113: } catch (javax.naming.CannotProceedException e) {
114: Log.warn("Can not proceed: " + e.getMessage());
115: traceEnd("AUTHENTICATE_NOAUTH");
116: return AUTHENTICATE_NOAUTH;
117: } catch (javax.naming.NameNotFoundException e) {
118: trace("Username not found: " + e.getMessage());
119: traceEnd("AUTHENTICATE_NOAUTH");
120: return AUTHENTICATE_NOAUTH;
121: } catch (Exception e) {
122: Log.warn("Failure: " + e.toString());
123: traceEnd("AUTHENTICATE_FAILURE");
124: return AUTHENTICATE_FAILURE;
125: }
126: }
127:
128: }
|