1 15 16 package org.apache.lenya.ac.cifs; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.IOException ; 21 import java.util.Properties ; 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 ; 38 39 43 public class CIFSUser extends FileUser { 44 45 private static Properties defaultProperties = null; 46 private static Category log = Logger.getLogger(CIFSUser.class); 47 48 private static final String DOMAIN_CONTROLLER = "domain-controller"; 50 51 private static final String DOMAIN = "domain"; 53 54 55 58 public CIFSUser() { 59 60 } 61 62 65 public CIFSUser(File configurationDirectory, String id, 66 String fullName,String email,String password) { 67 super(configurationDirectory, id, fullName, email, password); 68 69 } 70 71 75 protected void initialize() throws ConfigurationException { 76 try { 77 readProperties(super.getConfigurationDirectory()); 78 } catch (final IOException ioe) { 79 throw new ConfigurationException("Reading cifs.properties file in ["+ 80 super.getConfigurationDirectory()+"] failed", ioe); 81 } 82 } 83 84 89 public void configure(Configuration config) throws ConfigurationException { 90 super.configure(config); 91 initialize(); 92 } 93 94 100 public boolean authenticate(String 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 return true; 115 } catch( final SmbAuthException sae ) { 116 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 return false; 125 } catch(final UnknownHostException unho) { 126 return false; 127 } 128 129 } 130 131 135 private void readProperties(File configurationDirectory) throws IOException { 136 File propertiesFile = new File (configurationDirectory, "cifs.properties"); 138 139 if (defaultProperties == null) { 140 defaultProperties = new Properties (); 141 142 FileInputStream in = null; 143 try { 144 in = new FileInputStream (propertiesFile); 145 defaultProperties.load(in); 146 } finally { 147 if (in != null) { 148 in.close(); 149 } 150 } 151 152 } 153 } 154 155 159 private String getDomainController() { 160 return (String )defaultProperties.get(DOMAIN_CONTROLLER); 161 } 162 163 167 private String getDomainName() { 168 return (String )defaultProperties.get(DOMAIN); 169 } 170 171 } | Popular Tags |