use of javax.naming.directory.DirContext in project uPortal by Jasig.
the class LDAPGroupStore method getConnection.
protected DirContext getConnection() {
// JNDI boilerplate to connect to an initial context
DirContext context = (DirContext) contexts.get("context");
if (context == null) {
Hashtable jndienv = new Hashtable();
jndienv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
jndienv.put(Context.SECURITY_AUTHENTICATION, "simple");
if (url.startsWith("ldaps")) {
// Handle SSL connections
String newurl = url.substring(0, 4) + url.substring(5);
jndienv.put(Context.SECURITY_PROTOCOL, "ssl");
jndienv.put(Context.PROVIDER_URL, newurl);
} else {
jndienv.put(Context.PROVIDER_URL, url);
}
if (logonid != null)
jndienv.put(Context.SECURITY_PRINCIPAL, logonid);
if (logonpassword != null)
jndienv.put(Context.SECURITY_CREDENTIALS, logonpassword);
try {
context = new InitialDirContext(jndienv);
} catch (NamingException nex) {
log.error("LDAPGroupStore: unable to get context", nex);
}
contexts.put("context", context);
}
return context;
}
use of javax.naming.directory.DirContext in project uPortal by Jasig.
the class SimpleLdapSecurityContext method authenticate.
/**
* Authenticates the user.
*/
public synchronized void authenticate() throws PortalSecurityException {
this.isauth = false;
ILdapServer ldapConn;
ldapConn = LdapServices.getDefaultLdapServer();
String creds = new String(this.myOpaqueCredentials.credentialstring);
if (this.myPrincipal.UID != null && !this.myPrincipal.UID.trim().equals("") && this.myOpaqueCredentials.credentialstring != null && !creds.trim().equals("")) {
DirContext conn = null;
NamingEnumeration results = null;
StringBuffer user = new StringBuffer("(");
String first_name = null;
String last_name = null;
user.append(ldapConn.getUidAttribute()).append("=");
user.append(this.myPrincipal.UID).append(")");
log.debug("SimpleLdapSecurityContext: Looking for {}", user.toString());
try {
conn = ldapConn.getConnection();
// set up search controls
SearchControls searchCtls = new SearchControls();
searchCtls.setReturningAttributes(attributes);
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// do lookup
if (conn != null) {
try {
results = conn.search(ldapConn.getBaseDN(), user.toString(), searchCtls);
if (results != null) {
if (!results.hasMore()) {
log.error("SimpleLdapSecurityContext: user not found: {}", this.myPrincipal.UID);
}
while (results != null && results.hasMore()) {
SearchResult entry = (SearchResult) results.next();
StringBuffer dnBuffer = new StringBuffer();
dnBuffer.append(entry.getName()).append(", ");
dnBuffer.append(ldapConn.getBaseDN());
Attributes attrs = entry.getAttributes();
first_name = getAttributeValue(attrs, ATTR_FIRSTNAME);
last_name = getAttributeValue(attrs, ATTR_LASTNAME);
// re-bind as user
conn.removeFromEnvironment(javax.naming.Context.SECURITY_PRINCIPAL);
conn.removeFromEnvironment(javax.naming.Context.SECURITY_CREDENTIALS);
conn.addToEnvironment(javax.naming.Context.SECURITY_PRINCIPAL, dnBuffer.toString());
conn.addToEnvironment(javax.naming.Context.SECURITY_CREDENTIALS, this.myOpaqueCredentials.credentialstring);
searchCtls = new SearchControls();
searchCtls.setReturningAttributes(new String[0]);
searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE);
String attrSearch = "(" + ldapConn.getUidAttribute() + "=*)";
log.debug("SimpleLdapSecurityContext: Looking in {} for {}", dnBuffer.toString(), attrSearch);
conn.search(dnBuffer.toString(), attrSearch, searchCtls);
this.isauth = true;
this.myPrincipal.FullName = first_name + " " + last_name;
log.debug("SimpleLdapSecurityContext: User {} ({}) is authenticated", this.myPrincipal.UID, this.myPrincipal.FullName);
// Since LDAP is case-insensitive with respect to uid, force
// user name to lower case for use by the portal
this.myPrincipal.UID = this.myPrincipal.UID.toLowerCase();
}
// while (results != null && results.hasMore())
} else {
log.error("SimpleLdapSecurityContext: No such user: {}", this.myPrincipal.UID);
}
} catch (AuthenticationException ae) {
log.info("SimpleLdapSecurityContext: Password invalid for user: " + this.myPrincipal.UID);
} catch (Exception e) {
log.error("SimpleLdapSecurityContext: LDAP Error with user: " + this.myPrincipal.UID + "; ", e);
throw new PortalSecurityException("SimpleLdapSecurityContext: LDAP Error" + e + " with user: " + this.myPrincipal.UID);
} finally {
ldapConn.releaseConnection(conn);
}
} else {
log.error("LDAP Server Connection unavailable");
}
} catch (final NamingException ne) {
log.error("Error getting connection to LDAP server.", ne);
}
} else {
// If the principal and/or credential are missing, the context authentication
// simply fails. It should not be construed that this is an error. It happens for guest
// access.
log.info("Principal or OpaqueCredentials not initialized prior to authenticate");
}
// Ok...we are now ready to authenticate all of our subcontexts.
super.authenticate();
return;
}
use of javax.naming.directory.DirContext in project Payara by payara.
the class WebappClassLoader method setResources.
/**
* Set associated resources.
*/
public void setResources(DirContext resources) {
this.resources = resources;
DirContext res = resources;
if (resources instanceof ProxyDirContext) {
ProxyDirContext proxyRes = (ProxyDirContext) res;
contextName = proxyRes.getContextName();
res = proxyRes.getDirContext();
}
if (res instanceof WebDirContext) {
((WebDirContext) res).setJarFileResourcesProvider(this);
}
}
use of javax.naming.directory.DirContext in project Payara by payara.
the class WebappLoader method copyDir.
/**
* Copy directory.
*/
private boolean copyDir(DirContext srcDir, File destDir) {
try {
NamingEnumeration<NameClassPair> enumeration = srcDir.list("");
while (enumeration.hasMoreElements()) {
NameClassPair ncPair = enumeration.nextElement();
String name = ncPair.getName();
Object object = srcDir.lookup(name);
File currentFile = new File(destDir, name);
if (object instanceof Resource) {
InputStream is = ((Resource) object).streamContent();
OutputStream os = new FileOutputStream(currentFile);
if (!copy(is, os))
return false;
} else if (object instanceof InputStream) {
OutputStream os = new FileOutputStream(currentFile);
if (!copy((InputStream) object, os))
return false;
} else if (object instanceof DirContext) {
if (!currentFile.isDirectory() && !currentFile.mkdir())
return false;
if (!copyDir((DirContext) object, currentFile))
return false;
}
}
} catch (NamingException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
use of javax.naming.directory.DirContext in project Payara by payara.
the class WebdavStatus method deleteCollection.
/**
* Deletes a collection.
*
* @param resources Resources implementation associated with the context
* @param path Path to the collection to be deleted
* @param errorList Contains the list of the errors which occurred
*/
private void deleteCollection(HttpServletRequest req, DirContext resources, String path, Hashtable<String, Integer> errorList) {
if (debug > 1)
log("Delete:" + path);
if (path.toUpperCase(Locale.ENGLISH).startsWith("/WEB-INF") || path.toUpperCase(Locale.ENGLISH).startsWith("/META-INF")) {
errorList.put(path, WebdavStatus.SC_FORBIDDEN);
return;
}
String ifHeader = req.getHeader("If");
if (ifHeader == null)
ifHeader = "";
String lockTokenHeader = req.getHeader("Lock-Token");
if (lockTokenHeader == null)
lockTokenHeader = "";
Enumeration<NameClassPair> enumeration;
try {
enumeration = resources.list(path);
} catch (NamingException e) {
errorList.put(path, WebdavStatus.SC_INTERNAL_SERVER_ERROR);
return;
}
while (enumeration.hasMoreElements()) {
NameClassPair ncPair = enumeration.nextElement();
String childName = path;
if (!"/".equals(childName))
childName += "/";
childName += ncPair.getName();
if (isLocked(childName, ifHeader + lockTokenHeader)) {
errorList.put(childName, Integer.valueOf(WebdavStatus.SC_LOCKED));
} else {
try {
Object object = resources.lookup(childName);
if (object instanceof DirContext) {
deleteCollection(req, resources, childName, errorList);
}
try {
resources.unbind(childName);
} catch (NamingException e) {
if (!(object instanceof DirContext)) {
// If it's not a collection, then it's an unknown
// error
errorList.put(childName, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
}
} catch (NamingException e) {
errorList.put(childName, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
}
}
}
Aggregations