KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > cifs > CIFSUser


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  *
14  */

15
16 package org.apache.lenya.ac.cifs;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.apache.lenya.ac.file.FileUser;
24
25 import org.apache.log4j.Category;
26 import org.apache.log4j.Logger;
27
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30
31 import jcifs.smb.NtlmPasswordAuthentication;
32 import jcifs.smb.SmbAuthException;
33 import jcifs.smb.SmbException;
34 import jcifs.smb.SmbSession;
35
36 import jcifs.UniAddress;
37 import java.net.UnknownHostException JavaDoc;
38
39 /**
40  * CIFS user.
41  * @version $Id: CIFSUser.java 169175 2005-05-08 22:02:45Z gregor $
42  */

43 public class CIFSUser extends FileUser {
44
45     private static Properties JavaDoc defaultProperties = null;
46     private static Category log = Logger.getLogger(CIFSUser.class);
47
48     // The name for the cifs.properties domain controller lookup
49
private static final String JavaDoc DOMAIN_CONTROLLER = "domain-controller";
50
51     // The name for the cifs.properties domain name lookup
52
private static final String JavaDoc DOMAIN = "domain";
53
54
55     /**
56     * Creates a new CIFSUser object.
57     */

58     public CIFSUser() {
59
60     }
61
62     /**
63     * Create a CIFSUser
64     */

65     public CIFSUser(File JavaDoc configurationDirectory, String JavaDoc id,
66                     String JavaDoc fullName,String JavaDoc email,String JavaDoc password) {
67         super(configurationDirectory, id, fullName, email, password);
68
69     }
70
71     /**
72      * Initializes this user.
73      * @throws ConfigurationException when something went wrong.
74      */

75     protected void initialize() throws ConfigurationException {
76        try {
77             readProperties(super.getConfigurationDirectory());
78         } catch (final IOException JavaDoc ioe) {
79             throw new ConfigurationException("Reading cifs.properties file in ["+
80                         super.getConfigurationDirectory()+"] failed", ioe);
81         }
82     }
83
84     /**
85      * Create a new CIFSUser from a configuration
86      * @param config the <code>Configuration</code> specifying the user details
87      * @throws ConfigurationException if the user could not be instantiated
88      */

89     public void configure(Configuration config) throws ConfigurationException {
90         super.configure(config);
91         initialize();
92     }
93
94     /**
95      * Authenticate a user. This is done by NTDomain Authentication
96      * using jcifs
97      * @param password to authenticate with
98      * @return true if the given password matches the password for this user
99      */

100     public boolean authenticate(String JavaDoc password) {
101
102         System.setProperty("jcifs.smb.client.disablePlainTextPasswords",
103                             "false" );
104         try {
105             UniAddress mydomaincontroller = UniAddress.getByName(
106                                                 getDomainController());
107             NtlmPasswordAuthentication mycreds = new
108                                 NtlmPasswordAuthentication(
109                                         getDomainName(),
110                                         super.getId(),
111                                         password);
112             SmbSession.logon( mydomaincontroller, mycreds );
113             // SUCCESS
114
return true;
115         } catch( final SmbAuthException sae ) {
116             // AUTHENTICATION FAILURE
117
if (log.isInfoEnabled()) {
118                 log.info("Authentication against [" + getDomainController() +"]" +
119                          " failed for " + getDomainName() + "/" + super.getId());
120             }
121             return false;
122         } catch(final SmbException se ) {
123             // NETWORK PROBLEMS?
124
return false;
125         } catch(final UnknownHostException JavaDoc unho) {
126             return false;
127         }
128
129     }
130
131     /**
132      * Read the properties
133      * @throws IOException if the properties cannot be found.
134      */

135     private void readProperties(File JavaDoc configurationDirectory) throws IOException JavaDoc {
136         // create and load default properties
137
File JavaDoc propertiesFile = new File JavaDoc(configurationDirectory, "cifs.properties");
138
139         if (defaultProperties == null) {
140             defaultProperties = new Properties JavaDoc();
141
142             FileInputStream JavaDoc in = null;
143             try {
144                 in = new FileInputStream JavaDoc(propertiesFile);
145                 defaultProperties.load(in);
146             } finally {
147                 if (in != null) {
148                     in.close();
149                 }
150             }
151
152         }
153     }
154
155     /**
156      * Get the domain controller we want to authenticate against
157      * @return the name of the domain controller
158      */

159      private String JavaDoc getDomainController() {
160          return (String JavaDoc)defaultProperties.get(DOMAIN_CONTROLLER);
161      }
162
163     /**
164      * Get the domain name
165      * @return the domain name
166      */

167      private String JavaDoc getDomainName() {
168          return (String JavaDoc)defaultProperties.get(DOMAIN);
169      }
170
171 }
Popular Tags