001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software distributed under the License
012: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
013: * or implied. See the License for the specific language governing permissions and limitations under
014: * the License.
015: *
016: */
017:
018: package org.apache.lenya.ac.cifs;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.util.Properties;
024:
025: import org.apache.lenya.ac.ItemManager;
026: import org.apache.lenya.ac.file.FileUser;
027:
028: import org.apache.avalon.framework.configuration.Configuration;
029: import org.apache.avalon.framework.configuration.ConfigurationException;
030: import org.apache.avalon.framework.logger.Logger;
031:
032: import jcifs.smb.NtlmPasswordAuthentication;
033: import jcifs.smb.SmbAuthException;
034: import jcifs.smb.SmbException;
035: import jcifs.smb.SmbSession;
036:
037: import jcifs.UniAddress;
038: import java.net.UnknownHostException;
039:
040: /**
041: * CIFS user.
042: * @version $Id: CIFSUser.java 485769 2006-12-11 17:41:23Z andreas $
043: */
044: public class CIFSUser extends FileUser {
045:
046: /**
047: *
048: */
049: private static final long serialVersionUID = 1L;
050:
051: private static Properties defaultProperties = null;
052:
053: // The name for the cifs.properties domain controller lookup
054: private static final String DOMAIN_CONTROLLER = "domain-controller";
055:
056: // The name for the cifs.properties domain name lookup
057: private static final String DOMAIN = "domain";
058:
059: /**
060: * Creates a new CIFSUser object.
061: * @param itemManager The item manager.
062: * @param logger The logger.
063: */
064: public CIFSUser(ItemManager itemManager, Logger logger) {
065: super (itemManager, logger);
066:
067: }
068:
069: /**
070: * Create a CIFSUser
071: * @param itemManager The item manager.
072: * @param logger The logger.
073: * @param id The user ID.
074: * @param fullName The user's name.
075: * @param email The e-mail address.
076: * @param password The password.
077: */
078: public CIFSUser(ItemManager itemManager, Logger logger, String id,
079: String fullName, String email, String password) {
080: super (itemManager, logger, id, fullName, email, password);
081:
082: }
083:
084: /**
085: * Initializes this user.
086: * @throws ConfigurationException when something went wrong.
087: */
088: protected void initialize() throws ConfigurationException {
089: try {
090: readProperties(super .getConfigurationDirectory());
091: } catch (final IOException ioe) {
092: throw new ConfigurationException(
093: "Reading cifs.properties file in ["
094: + super .getConfigurationDirectory()
095: + "] failed", ioe);
096: }
097: }
098:
099: /**
100: * Create a new CIFSUser from a configuration
101: * @param config the <code>Configuration</code> specifying the user
102: * details
103: * @throws ConfigurationException if the user could not be instantiated
104: */
105: public void configure(Configuration config)
106: throws ConfigurationException {
107: super .configure(config);
108: initialize();
109: }
110:
111: /**
112: * Authenticate a user. This is done by NTDomain Authentication using jcifs
113: * @param password to authenticate with
114: * @return true if the given password matches the password for this user
115: */
116: public boolean authenticate(String password) {
117:
118: System.setProperty(
119: "jcifs.smb.client.disablePlainTextPasswords", "true");
120: try {
121: UniAddress mydomaincontroller = UniAddress
122: .getByName(getDomainController());
123: NtlmPasswordAuthentication mycreds = new NtlmPasswordAuthentication(
124: getDomainName(), super .getId(), password);
125: SmbSession.logon(mydomaincontroller, mycreds);
126: // SUCCESS
127: return true;
128: } catch (final SmbAuthException sae) {
129: // AUTHENTICATION FAILURE
130: if (getLogger().isInfoEnabled()) {
131: getLogger().info(
132: "Authentication against ["
133: + getDomainController() + "]"
134: + " failed for " + getDomainName()
135: + "/" + super .getId());
136: }
137: return false;
138: } catch (final SmbException se) {
139: // NETWORK PROBLEMS?
140: return false;
141: } catch (final UnknownHostException unho) {
142: return false;
143: }
144:
145: }
146:
147: /**
148: * Read the properties
149: * @param configurationDirectory The configuration directory.
150: * @throws IOException if the properties cannot be found.
151: */
152: private void readProperties(File configurationDirectory)
153: throws IOException {
154: // create and load default properties
155: File propertiesFile = new File(configurationDirectory,
156: "cifs.properties");
157:
158: if (defaultProperties == null) {
159: defaultProperties = new Properties();
160:
161: FileInputStream in = null;
162: try {
163: in = new FileInputStream(propertiesFile);
164: defaultProperties.load(in);
165: } finally {
166: if (in != null) {
167: in.close();
168: }
169: }
170:
171: }
172: }
173:
174: /**
175: * Get the domain controller we want to authenticate against
176: * @return the name of the domain controller
177: */
178: private String getDomainController() {
179: return (String) defaultProperties.get(DOMAIN_CONTROLLER);
180: }
181:
182: /**
183: * Get the domain name
184: * @return the domain name
185: */
186: private String getDomainName() {
187: return (String) defaultProperties.get(DOMAIN);
188: }
189:
190: }
|