use of javax.naming.directory.Attribute in project Lucee by lucee.
the class AbsDefaultHostnameVerifier method extractCN.
static String extractCN(final String subjectPrincipal) throws SSLException {
if (subjectPrincipal == null) {
return null;
}
try {
final LdapName subjectDN = new LdapName(subjectPrincipal);
final List<Rdn> rdns = subjectDN.getRdns();
for (int i = rdns.size() - 1; i >= 0; i--) {
final Rdn rds = rdns.get(i);
final Attributes attributes = rds.toAttributes();
final Attribute cn = attributes.get("cn");
if (cn != null) {
try {
final Object value = cn.get();
if (value != null) {
return value.toString();
}
} catch (NoSuchElementException ignore) {
} catch (NamingException ignore) {
}
}
}
return null;
} catch (InvalidNameException e) {
throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
}
}
use of javax.naming.directory.Attribute in project Lucee by lucee.
the class LDAPClient method query.
/**
* @param dn
* @param strAttributes
* @param scope
* @param startrow
* @param maxrows
* @param timeout
* @param sort
* @param sortType
* @param sortDirection
* @param start
* @param separator
* @param filter
* @return
* @throws NamingException
* @throws PageException
* @throws IOException
*/
public Query query(String strAttributes, int scope, int startrow, int maxrows, int timeout, String[] sort, int sortType, int sortDirection, String start, String separator, String filter) throws NamingException, PageException, IOException {
// strAttributes=strAttributes.trim();
boolean attEQAsterix = strAttributes.trim().equals("*");
String[] attributes = attEQAsterix ? new String[] { "name", "value" } : toStringAttributes(strAttributes, ",");
// Control
SearchControls controls = new SearchControls();
controls.setReturningObjFlag(true);
controls.setSearchScope(scope);
if (!attEQAsterix)
controls.setReturningAttributes(toStringAttributes(strAttributes, ","));
if (maxrows > 0)
controls.setCountLimit(startrow + maxrows + 1);
if (timeout > 0)
controls.setTimeLimit(timeout);
InitialLdapContext context = new InitialLdapContext(env, null);
// Search
Query qry = new QueryImpl(attributes, 0, "query");
try {
NamingEnumeration results = context.search(start, filter, controls);
// Fill result
int row = 1;
if (!attEQAsterix) {
while (results.hasMoreElements()) {
SearchResult resultRow = (SearchResult) results.next();
if (row++ < startrow)
continue;
int len = qry.addRow();
NamingEnumeration rowEnum = resultRow.getAttributes().getAll();
String dn = resultRow.getNameInNamespace();
qry.setAtEL("dn", len, dn);
while (rowEnum.hasMore()) {
Attribute attr = (Attribute) rowEnum.next();
Collection.Key key = KeyImpl.init(attr.getID());
Enumeration values = attr.getAll();
Object value;
String existing, strValue;
while (values.hasMoreElements()) {
value = values.nextElement();
strValue = Caster.toString(value, null);
existing = Caster.toString(qry.getAt(key, len, null), null);
if (!StringUtil.isEmpty(existing) && !StringUtil.isEmpty(strValue)) {
value = existing + separator + strValue;
} else if (!StringUtil.isEmpty(existing))
value = existing;
qry.setAtEL(key, len, value);
}
}
if (maxrows > 0 && len >= maxrows)
break;
}
} else {
outer: while (results.hasMoreElements()) {
SearchResult resultRow = (SearchResult) results.next();
if (row++ < startrow)
continue;
Attributes attributesRow = resultRow.getAttributes();
NamingEnumeration rowEnum = attributesRow.getIDs();
while (rowEnum.hasMoreElements()) {
int len = qry.addRow();
String name = Caster.toString(rowEnum.next());
Object value = null;
try {
value = attributesRow.get(name).get();
} catch (Exception e) {
}
qry.setAtEL("name", len, name);
qry.setAtEL("value", len, value);
if (maxrows > 0 && len >= maxrows)
break outer;
}
qry.setAtEL("name", qry.size(), "dn");
}
}
} finally {
context.close();
}
// Sort
if (sort != null && sort.length > 0) {
int order = sortDirection == SORT_DIRECTION_ASC ? Query.ORDER_ASC : Query.ORDER_DESC;
for (int i = sort.length - 1; i >= 0; i--) {
String item = sort[i];
if (item.indexOf(' ') != -1)
item = ListUtil.first(item, " ", true);
qry.sort(KeyImpl.getInstance(item), order);
// keys[i] = new SortKey(item);
}
}
return qry;
}
use of javax.naming.directory.Attribute in project karaf by apache.
the class LDAPCache method doGetUserRoles.
private String[] doGetUserRoles(String user, String userDn, String userDnNamespace) throws NamingException {
DirContext context = open();
SearchControls controls = new SearchControls();
if (options.getRoleSearchSubtree()) {
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
String filter = options.getRoleFilter();
if (filter != null) {
filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement(userDn));
filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement(userDnNamespace));
filter = filter.replace("\\", "\\\\");
LOGGER.debug("Looking for the user roles in LDAP with ");
LOGGER.debug(" base DN: " + options.getRoleBaseDn());
LOGGER.debug(" filter: " + filter);
NamingEnumeration<SearchResult> namingEnumeration = context.search(options.getRoleBaseDn(), filter, controls);
try {
List<String> rolesList = new ArrayList<>();
while (namingEnumeration.hasMore()) {
SearchResult result = namingEnumeration.next();
Attributes attributes = result.getAttributes();
Attribute roles1 = attributes.get(options.getRoleNameAttribute());
if (roles1 != null) {
for (int i = 0; i < roles1.size(); i++) {
String role = (String) roles1.get(i);
if (role != null) {
LOGGER.debug("User {} is a member of role {}", user, role);
// handle role mapping
Set<String> roleMappings = tryMappingRole(role);
if (roleMappings.isEmpty()) {
rolesList.add(role);
} else {
for (String roleMapped : roleMappings) {
rolesList.add(roleMapped);
}
}
}
}
}
}
return rolesList.toArray(new String[rolesList.size()]);
} finally {
if (namingEnumeration != null) {
try {
namingEnumeration.close();
} catch (NamingException e) {
// Ignore
}
}
}
} else {
LOGGER.debug("The user role filter is null so no roles are retrieved");
return new String[] {};
}
}
use of javax.naming.directory.Attribute in project knox by apache.
the class KnoxLdapRealm method addRoleIfMember.
private void addRoleIfMember(final String userDn, final SearchResult group, final Set<String> roleNames, final Set<String> groupNames, final LdapContextFactory ldapContextFactory) throws NamingException {
NamingEnumeration<? extends Attribute> attributeEnum = null;
NamingEnumeration<?> e = null;
try {
LdapName userLdapDn = new LdapName(userDn);
Attribute attribute = group.getAttributes().get(getGroupIdAttribute());
String groupName = attribute.get().toString();
attributeEnum = group.getAttributes().getAll();
while (attributeEnum.hasMore()) {
final Attribute attr = attributeEnum.next();
if (!memberAttribute.equalsIgnoreCase(attr.getID())) {
continue;
}
e = attr.getAll();
while (e.hasMore()) {
String attrValue = e.next().toString();
if (memberAttribute.equalsIgnoreCase(MEMBER_URL)) {
boolean dynamicGroupMember = isUserMemberOfDynamicGroup(userLdapDn, // memberUrl value
attrValue, ldapContextFactory);
if (dynamicGroupMember) {
groupNames.add(groupName);
String roleName = roleNameFor(groupName);
if (roleName != null) {
roleNames.add(roleName);
} else {
roleNames.add(groupName);
}
}
} else {
if (groupObjectClass.equalsIgnoreCase(POSIX_GROUP)) {
attrValue = memberAttributeValuePrefix + attrValue + memberAttributeValueSuffix;
}
if (userLdapDn.equals(new LdapName(attrValue))) {
groupNames.add(groupName);
String roleName = roleNameFor(groupName);
if (roleName != null) {
roleNames.add(roleName);
} else {
roleNames.add(groupName);
}
break;
}
}
}
}
} finally {
try {
if (attributeEnum != null) {
attributeEnum.close();
}
} finally {
if (e != null) {
e.close();
}
}
}
}
use of javax.naming.directory.Attribute in project pentaho-kettle by pentaho.
the class MailValidation method getMX.
private static ArrayList<String> getMX(String hostName) throws NamingException {
// Perform a DNS lookup for MX records in the domain
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
Attribute attr = attrs.get("MX");
// if we don't have an MX record, try the machine itself
if ((attr == null) || (attr.size() == 0)) {
attrs = ictx.getAttributes(hostName, new String[] { "A" });
attr = attrs.get("A");
if (attr == null) {
throw new NamingException(BaseMessages.getString(PKG, "MailValidator.NoMatchName", hostName));
}
}
// Huzzah! we have machines to try. Return them as an array list
// NOTE: We SHOULD take the preference into account to be absolutely
// correct. This is left as an exercise for anyone who cares.
ArrayList<String> res = new ArrayList<String>();
NamingEnumeration<?> en = attr.getAll();
while (en.hasMore()) {
String x = (String) en.next();
String[] f = x.split(" ");
if (f[1].endsWith(".")) {
f[1] = f[1].substring(0, (f[1].length() - 1));
}
res.add(f[1]);
}
return res;
}
Aggregations