1 13 14 package org.ejbca.core.model.ca.publisher; 15 16 import java.io.IOException ; 17 import java.io.UnsupportedEncodingException ; 18 import java.security.cert.Certificate ; 19 import java.security.cert.CertificateParsingException ; 20 import java.security.cert.X509Certificate ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 import org.apache.log4j.Logger; 26 import org.ejbca.core.model.ra.ExtendedInformation; 27 import org.ejbca.util.CertTools; 28 import org.ejbca.util.dn.DNFieldExtractor; 29 30 31 import com.novell.ldap.LDAPAttribute; 32 import com.novell.ldap.LDAPAttributeSet; 33 import com.novell.ldap.LDAPEntry; 34 35 40 public class ActiveDirectoryPublisher extends LdapPublisher{ 41 42 private static final Logger log = Logger.getLogger(ActiveDirectoryPublisher.class); 43 44 public static final float LATEST_VERSION = 1; 45 46 public static final int TYPE_ADPUBLISHER = 3; 47 48 public static final int UAC_DISABLE = 2; 50 public static final int UAC_NORMAL = 512; 51 public static final int UAC_NEVEREXPIRE = 66048; 52 public static final int UAC_SMARTCARDREQUIRED = 0x40000; 53 54 public static final int DEFAULT_UAC = UAC_NEVEREXPIRE; 56 57 protected static final String USEPASSWORD = "usepassword"; 58 protected static final String USERACCOUNTCONTROL = "useraccountcontrol"; 59 protected static final String SAMACCOUNTNAME = "samaccountname"; 60 protected static final String USERDESCRIPTION = "userdescription"; 61 62 public static final String DEFAULT_USEROBJECTCLASS = "top;person;organizationalPerson;user"; 63 public static final String DEFAULT_CAOBJECTCLASS = "top;certificationAuthority"; 64 65 66 67 public ActiveDirectoryPublisher(){ 68 super(); 69 data.put(TYPE, new Integer (TYPE_ADPUBLISHER)); 70 71 setUserObjectClass(DEFAULT_USEROBJECTCLASS); 72 setCAObjectClass(DEFAULT_CAOBJECTCLASS); 73 setUseUserPassword(true); 74 setUserAccountControl(DEFAULT_UAC); 75 setSAMAccountName(DNFieldExtractor.UPN); 76 setUserDescription(""); 77 } 78 79 80 81 84 public boolean getUseUserPassword (){ 85 return ((Boolean ) data.get(USEPASSWORD)).booleanValue(); 86 } 87 88 91 public void setUseUserPassword (boolean useuserpassword){ 92 data.put(USEPASSWORD, new Boolean (useuserpassword)); 93 } 94 95 98 public int getUserAccountControl (){ 99 return ((Integer ) data.get(USERACCOUNTCONTROL)).intValue(); 100 } 101 102 105 public void setUserAccountControl(int useraccountcontrol){ 106 data.put(USERACCOUNTCONTROL, new Integer (useraccountcontrol)); 107 } 108 109 113 public int getSAMAccountName (){ 114 return ((Integer ) data.get(SAMACCOUNTNAME)).intValue(); 115 } 116 117 123 public void setSAMAccountName(int samaccountname){ 124 data.put(SAMACCOUNTNAME, new Integer (samaccountname)); 125 } 126 127 130 public String getUserDescription (){ 131 return (String ) data.get(USERDESCRIPTION); 132 } 133 134 137 public void setUserDescription(String userdescription){ 138 data.put(USERDESCRIPTION, userdescription); 139 } 140 141 154 protected LDAPAttributeSet getAttributeSet(Certificate cert, String objectclass, String dn, boolean extra, boolean person, 155 String password, ExtendedInformation extendedinformation) { 156 log.debug("ADPublisher : getAttributeSet"); 157 158 LDAPAttributeSet attributeSet = super.getAttributeSet(cert, objectclass, dn, extra, person, password, extendedinformation); 159 160 String cn = CertTools.getPartFromDN(dn, "CN"); 161 164 if(cert!= null && cert instanceof X509Certificate ){ 165 String upn = null; 166 try { 167 upn = CertTools.getUPNAltName((X509Certificate ) cert); 168 } catch (CertificateParsingException e) {} 169 catch (IOException e) {} 170 String samaccountname = upn; 171 if(upn != null && upn.indexOf('@') != -1){ 172 samaccountname = samaccountname.substring(0, upn.indexOf('@')); 174 } 175 176 177 switch(getSAMAccountName()){ 178 case DNFieldExtractor.CN: 179 samaccountname = cn; 180 break; 181 case DNFieldExtractor.UID: 182 samaccountname = CertTools.getPartFromDN(dn, "UID"); 183 break; 184 } 185 if(samaccountname !=null){ 186 attributeSet.add(new LDAPAttribute("samaccountname", samaccountname)); 187 } 188 189 if(upn != null) 190 attributeSet.add(new LDAPAttribute("userPrincipalName", upn)); 191 else 192 attributeSet.add(new LDAPAttribute("userPrincipalName", cn)); 193 194 } 195 attributeSet.add(new LDAPAttribute("displayName", cn)); 196 if(getUserDescription() != null && !getUserDescription().trim().equals("")){ 197 attributeSet.add(new LDAPAttribute("description", getUserDescription())); 198 } 199 200 if(getUseSSL() && password != null){ 201 203 205 206 String newVal = new String ("\"" + password + "\""); 208 209 212 byte _bytes[] = null; 213 try { 214 _bytes = newVal.getBytes("Unicode"); 215 } catch (UnsupportedEncodingException e) {} 216 byte bytes[] = new byte[_bytes.length - 2]; 217 System.arraycopy(_bytes, 2, bytes, 0, _bytes.length - 2); 218 219 attributeSet.add(new LDAPAttribute("unicodePwd", bytes)); 221 222 } 223 224 225 return attributeSet; 226 } 228 229 241 protected ArrayList getModificationSet(LDAPEntry oldEntry, String dn, boolean extra, boolean person) { 242 ArrayList modSet = super.getModificationSet(oldEntry, dn, false, person); 243 244 246 return modSet; 247 } 249 250 251 252 254 257 public Object clone() throws CloneNotSupportedException { 258 ActiveDirectoryPublisher clone = new ActiveDirectoryPublisher(); 259 HashMap clonedata = (HashMap ) clone.saveData(); 260 261 Iterator i = (data.keySet()).iterator(); 262 while(i.hasNext()){ 263 Object key = i.next(); 264 clonedata.put(key, data.get(key)); 265 } 266 267 clone.loadData(clonedata); 268 return clone; 269 } 270 271 274 public float getLatestVersion() { 275 return LATEST_VERSION; 276 } 277 278 279 } 280 | Popular Tags |