1 31 package org.blojsom.authorization.ldap; 32 33 import netscape.ldap.*; 34 import netscape.ldap.factory.JSSESocketFactory; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.blojsom.BlojsomException; 38 import org.blojsom.ConfigurationException; 39 import org.blojsom.authorization.AuthorizationException; 40 import org.blojsom.authorization.database.DatabaseAuthorizationProvider; 41 import org.blojsom.blog.Blog; 42 import org.blojsom.util.BlojsomUtils; 43 44 import javax.servlet.ServletConfig ; 45 import java.util.Map ; 46 import java.util.Enumeration ; 47 48 76 public class LDAPAuthorizationProvider extends DatabaseAuthorizationProvider { 77 78 private static final String BLOG_LDAP_AUTHORIZATION_SERVER_IP = "blog-ldap-authorization-server"; 79 private static final String BLOG_LDAP_AUTHORIZATION_PORT_IP = "blog-ldap-authorization-port"; 80 private static final String BLOG_LDAP_AUTHORIZATION_DN_IP = "blog-ldap-authorization-dn"; 81 private static final String BLOG_LDAP_AUTHORIZATION_UID_IP = "blog-ldap-authorization-uid"; 82 private static final String BLOG_LDAP_AUTHORIZATION_BINDING_USER_IP = "blog-ldap-authorization-bindinguser"; 83 private static final String BLOG_LDAP_AUTHORIZATION_BINDING_PASSWORD_IP = "blog-ldap-authorization-bindingpassword"; 84 private static final String BLOG_LDAP_AUTHORIZATION_USE_SSL = "blog-ldap-authorization-use-ssl"; 85 86 private static final String UID_DEFAULT = "uid"; 87 88 private Log _logger = LogFactory.getLog(LDAPAuthorizationProvider.class); 89 private String _ldapServer; 90 private int _ldapPort = 389; 91 private String _ldapDN; 92 private String _uidAttributeName = UID_DEFAULT; 93 94 private String _bindingUser = null; 95 private String _bindingPassword = null; 96 private ServletConfig _servletConfig; 97 private boolean _useSSL = false; 98 99 102 public LDAPAuthorizationProvider() { 103 } 104 105 110 public void setServletConfig(ServletConfig servletConfig) { 111 _servletConfig = servletConfig; 112 } 113 114 119 public void init() throws ConfigurationException { 120 super.init(); 121 122 _ldapServer = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_SERVER_IP); 123 _ldapDN = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_DN_IP); 124 String port = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_PORT_IP); 125 if (!BlojsomUtils.checkNullOrBlank(_servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_UID_IP))) { 126 _uidAttributeName = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_UID_IP); 127 } 128 129 if (!BlojsomUtils.checkNullOrBlank(_servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_USE_SSL))) { 130 String bool = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_USE_SSL); 131 _useSSL = Boolean.valueOf(bool).booleanValue(); 132 } 133 134 if (BlojsomUtils.checkNullOrBlank(_ldapServer)) { 138 String msg = "No LDAP authorization server specified."; 139 if (_logger.isErrorEnabled()) { 140 _logger.error(msg); 141 } 142 143 throw new ConfigurationException(msg); 144 } 145 146 if (BlojsomUtils.checkNullOrBlank(_ldapDN)) { 147 String msg = "No LDAP authorization DN specified."; 148 if (_logger.isErrorEnabled()) { 149 _logger.error(msg); 150 } 151 152 throw new ConfigurationException(msg); 153 } 154 155 if (!BlojsomUtils.checkNullOrBlank(port)) { 156 try { 157 _ldapPort = Integer.valueOf(port).intValue(); 158 if ((0 > _ldapPort) || (_ldapPort > 65535)) { 159 if (_logger.isErrorEnabled()) { 160 _logger.error("LDAP port is not in valid range [0,65535]."); 161 } 162 163 throw new NumberFormatException (); 164 } 165 } catch (NumberFormatException nfe) { 166 String msg = "Invalid LDAP port '" + port + "' specified."; 167 if (_logger.isErrorEnabled()) { 168 _logger.error(msg); 169 } 170 171 throw new ConfigurationException(msg); 172 } 173 } 174 175 _bindingUser = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_BINDING_USER_IP); 176 _bindingPassword = _servletConfig.getInitParameter(BLOG_LDAP_AUTHORIZATION_BINDING_PASSWORD_IP); 177 178 if (_logger.isDebugEnabled()) { 179 _logger.debug("LDAP Authorization Provider server: " + _ldapServer); 180 _logger.debug("LDAP Authorization Provider port: " + _ldapPort); 181 _logger.debug("LDAP Authorization Provider DN: " + _ldapDN); 182 _logger.debug("LDAP Authorization Provider UID: " + _uidAttributeName); 183 _logger.debug("LDAP Authorization Provider binding user: " + _bindingUser); 184 _logger.debug("LDAP Authorization Provider binding password: **********"); 185 _logger.debug("LDAP Authorization Provider UseSSL: " + _useSSL); 186 187 _logger.debug("Initialized LDAP authorization provider"); 188 } 189 } 190 191 201 public void authorize(Blog blog, Map authorizationContext, String username, String password) throws AuthorizationException { 202 String dn = getDN(username); 203 204 if (BlojsomUtils.checkNullOrBlank(_ldapServer) || BlojsomUtils.checkNullOrBlank(dn)) { 205 String msg = "Authorization failed for blog: " + blog.getBlogId() + " for username: " + username + "; " + "LDAP not properly configured"; 206 if (_logger.isErrorEnabled()) { 207 _logger.error(msg); 208 } 209 210 throw new AuthorizationException(msg); 211 } 212 213 try { 214 LDAPConnection ldapConnection; 215 216 if (_useSSL) { 217 JSSESocketFactory ldapSocketFactory = new JSSESocketFactory(); 218 ldapConnection = new LDAPConnection(ldapSocketFactory); 219 } else { 220 ldapConnection = new LDAPConnection(); 221 } 222 223 ldapConnection.connect(_ldapServer, _ldapPort); 225 226 if (blog.getUseEncryptedPasswords().booleanValue()) { 227 password = BlojsomUtils.digestString(password, blog.getDigestAlgorithm()); 228 } 229 230 ldapConnection.authenticate(3, dn, password); 233 234 ldapConnection.disconnect(); 235 if (_logger.isDebugEnabled()) { 236 _logger.debug("Successfully authenticated user '" + username + "' via LDAP."); 237 } 238 } catch (LDAPException e) { 239 String reason; 240 switch (e.getLDAPResultCode()) { 241 case LDAPException.NO_SUCH_OBJECT: 243 reason = "The specified user does not exist: " + dn; 244 break; 245 case LDAPException.INVALID_CREDENTIALS: 247 reason = "Invalid password"; 248 break; 249 default: 251 reason = "Failed to authenticate as " + dn + ", " + e; 252 break; 253 } 254 255 String msg = "Authorization failed for blog: " + blog.getBlogId() + " for username: " + username + "; " + reason; 256 257 if (_logger.isErrorEnabled()) { 258 _logger.error(msg); 259 } 260 261 throw new AuthorizationException(msg); 262 } 263 } 264 265 271 protected String getDN(String username) { 272 try { 273 LDAPConnection ldapConnection; 274 275 if (_useSSL) { 276 JSSESocketFactory ldapSocketFactory = new JSSESocketFactory(); 277 ldapConnection = new LDAPConnection(ldapSocketFactory); 278 } else { 279 ldapConnection = new LDAPConnection(); 280 } 281 282 ldapConnection.connect(_ldapServer, _ldapPort); 284 285 if (!BlojsomUtils.checkNullOrBlank(_bindingUser) && !BlojsomUtils.checkNullOrBlank(_bindingPassword)) { 287 if (_logger.isDebugEnabled()) { 288 _logger.debug("Using LDAP authentication for LDAP connection"); 289 } 290 291 ldapConnection.authenticate(3, _bindingUser, _bindingPassword); 292 } 293 294 String [] attrs = {}; 296 LDAPSearchResults res = ldapConnection.search(_ldapDN, LDAPv2.SCOPE_SUB, "(" + _uidAttributeName + "=" + username + ")", attrs, true); 297 298 if (!res.hasMoreElements()) { 299 if (_logger.isDebugEnabled()) { 301 _logger.debug("User '" + username + "' does not exist in LDAP directory."); 302 } 303 304 ldapConnection.disconnect(); 305 306 return null; 307 } 308 309 String dn = res.next().getDN(); 310 ldapConnection.disconnect(); 311 if (_logger.isDebugEnabled()) { 312 _logger.debug("Successfully got user DN '" + dn + "' via LDAP."); 313 } 314 315 return dn; 316 } catch (LDAPException e) { 317 return null; 319 } 320 } 321 322 329 protected String getAttribute(String username, String attribute) { 330 LDAPConnection ldapConnection = null; 331 String value = null; 332 333 try { 334 if (_useSSL) { 336 JSSESocketFactory ldapSocketFactory = new JSSESocketFactory(); 337 ldapConnection = new LDAPConnection(ldapSocketFactory); 338 } else { 339 ldapConnection = new LDAPConnection(); 340 } 341 342 ldapConnection.connect(_ldapServer, _ldapPort); 343 344 if (!BlojsomUtils.checkNullOrBlank(_bindingUser) && !BlojsomUtils.checkNullOrBlank(_bindingPassword)) { 346 if (_logger.isDebugEnabled()) { 347 _logger.debug("Using LDAP authentication for LDAP connection"); 348 } 349 350 ldapConnection.authenticate(3, _bindingUser, _bindingPassword); 351 } 352 353 String attrs[] = {attribute}; 355 LDAPSearchResults res = ldapConnection.search(_ldapDN, LDAPConnection.SCOPE_SUB, "(" + _uidAttributeName + "=" + username + ")", attrs, false); 356 357 while (res.hasMoreElements()) { 359 LDAPEntry findEntry = null; 361 362 try { 363 findEntry = res.next(); 364 } catch (LDAPException e) { 365 if (_logger.isErrorEnabled()) { 366 _logger.error("Error: " + e.toString()); 367 } 368 369 continue; 370 } 371 372 if (_logger.isDebugEnabled()) { 374 _logger.debug(findEntry.getDN()); 375 } 376 377 LDAPAttributeSet findAttrs = findEntry.getAttributeSet(); 379 Enumeration enumAttrs = findAttrs.getAttributes(); 380 381 if (_logger.isDebugEnabled()) { 382 _logger.debug("\tAttributes: "); 383 } 384 385 while (enumAttrs.hasMoreElements()) { 387 LDAPAttribute anAttr = (LDAPAttribute) enumAttrs.nextElement(); 388 String attrName = anAttr.getName(); 389 if (_logger.isDebugEnabled()) { 390 _logger.debug("\t\t" + attrName); 391 } 392 393 Enumeration enumVals = anAttr.getStringValues(); 395 if (enumVals != null) { 396 while (enumVals.hasMoreElements()) { 397 String aVal = (String ) enumVals.nextElement(); 398 value = aVal; 399 400 if (_logger.isDebugEnabled()) { 401 _logger.debug("\t\t\t" + aVal); 402 } 403 } 404 } 405 } 406 } 407 } catch (LDAPException e) { 408 if (_logger.isErrorEnabled()) { 409 _logger.error("Error: " + e.toString()); 410 } 411 } 412 413 if ((ldapConnection != null) && ldapConnection.isConnected()) { 415 try { 416 ldapConnection.disconnect(); 417 } catch (LDAPException e) { 418 if (_logger.isErrorEnabled()) { 419 _logger.error("Error: " + e.toString()); 420 } 421 } 422 } 423 424 return value; 425 } 426 427 432 protected String getServer() { 433 return _ldapServer; 434 } 435 436 441 protected int getPort() { 442 return _ldapPort; 443 } 444 445 450 protected String getBaseDN() { 451 return _ldapDN; 452 } 453 } 454 | Popular Tags |