Search in sources :

Example 56 with Attributes

use of javax.naming.directory.Attributes in project bookkeeper by apache.

the class DNS method reverseDns.

/**
 * Returns the hostname associated with the specified IP address by the
 * provided nameserver.
 * <p/>
 * Loopback addresses
 *
 * @param hostIp The address to reverse lookup
 * @param ns     The host name of a reachable DNS server
 * @return The host name associated with the provided IP
 * @throws NamingException If a NamingException is encountered
 */
public static String reverseDns(InetAddress hostIp, String ns) throws NamingException {
    // 
    // Builds the reverse IP lookup form
    // This is formed by reversing the IP numbers and appending in-addr.arpa
    // 
    String[] parts = hostIp.getHostAddress().split("\\.");
    if (parts.length != 4) {
        // Not proper address. May be IPv6
        throw new NamingException("IPV6");
    }
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0] + ".in-addr.arpa";
    DirContext ictx = new InitialDirContext();
    Attributes attribute;
    try {
        attribute = ictx.getAttributes(// Use "dns:///" if the default
        "dns://" + ((ns == null) ? "" : ns) + // nameserver is to be used
        "/" + reverseIP, new String[] { "PTR" });
    } finally {
        ictx.close();
    }
    if (null == attribute) {
        throw new NamingException("No attribute is found");
    }
    Attribute ptrAttr = attribute.get("PTR");
    if (null == ptrAttr) {
        throw new NamingException("No PTR attribute is found");
    }
    if (null == ptrAttr.get()) {
        throw new NamingException("PTR attribute value is null");
    }
    return ptrAttr.get().toString();
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 57 with Attributes

use of javax.naming.directory.Attributes in project mssql-jdbc by Microsoft.

the class DNSUtilities method findSrvRecords.

/**
 * Find all SRV Record using DNS.
 *
 * @param dnsSrvRecordToFind
 *            the DNS record, for instance: _ldap._tcp.dc._msdcs.DOMAIN.COM to find all LDAP servers in DOMAIN.COM
 * @return the collection of records with facilities to find the best candidate
 * @throws NamingException
 *             if DNS is not available
 */
public static Set<DNSRecordSRV> findSrvRecords(final String dnsSrvRecordToFind) throws NamingException {
    Hashtable<Object, Object> env = new Hashtable<>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    env.put("java.naming.provider.url", "dns:");
    DirContext ctx = new InitialDirContext(env);
    Attributes attrs = ctx.getAttributes(dnsSrvRecordToFind, new String[] { "SRV" });
    NamingEnumeration<? extends Attribute> allServers = attrs.getAll();
    TreeSet<DNSRecordSRV> records = new TreeSet<>();
    while (allServers.hasMoreElements()) {
        Attribute a = allServers.nextElement();
        NamingEnumeration<?> srvRecord = a.getAll();
        while (srvRecord.hasMore()) {
            final String record = String.valueOf(srvRecord.nextElement());
            try {
                DNSRecordSRV rec = DNSRecordSRV.parseFromDNSRecord(record);
                if (rec != null) {
                    records.add(rec);
                }
            } catch (IllegalArgumentException errorParsingRecord) {
                if (LOG.isLoggable(DNS_ERR_LOG_LEVEL)) {
                    LOG.log(DNS_ERR_LOG_LEVEL, String.format("Failed to parse SRV DNS Record: '%s'", record), errorParsingRecord);
                }
            }
        }
        srvRecord.close();
    }
    allServers.close();
    return records;
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) TreeSet(java.util.TreeSet)

Example 58 with Attributes

use of javax.naming.directory.Attributes in project bw-calendar-engine by Bedework.

the class CardDAVDirImpl method getGroups.

/* Return all groups for principal == null or all groups for which principal
   * is a member
   *
   */
private Collection<BwGroup> getGroups(final DirConfigProperties dirProps, final BwPrincipal principal) throws CalFacadeException {
    LdapConfigProperties props = (LdapConfigProperties) dirProps;
    InitialLdapContext ctx = null;
    String member = null;
    if (principal != null) {
        if (principal.getKind() == WhoDefs.whoTypeUser) {
            member = getUserEntryValue(props, principal);
        } else if (principal.getKind() == WhoDefs.whoTypeGroup) {
            member = getGroupEntryValue(props, principal);
        }
    }
    try {
        ctx = createLdapInitContext(props);
        BasicAttributes matchAttrs = new BasicAttributes(true);
        if (member != null) {
            matchAttrs.put(props.getGroupMemberAttr(), member);
        }
        String[] idAttr = { props.getGroupIdAttr() };
        ArrayList<BwGroup> groups = new ArrayList<BwGroup>();
        NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, idAttr);
        while (response.hasMore()) {
            SearchResult sr = (SearchResult) response.next();
            Attributes attrs = sr.getAttributes();
            Attribute nmAttr = attrs.get(props.getGroupIdAttr());
            if (nmAttr.size() != 1) {
                throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
            }
            BwGroup group = new BwGroup();
            group.setAccount(nmAttr.get(0).toString());
            group.setPrincipalRef(makePrincipalUri(group.getAccount(), WhoDefs.whoTypeGroup));
            groups.add(group);
        }
        return groups;
    } catch (Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        // Close the context to release the connection
        if (ctx != null) {
            closeContext(ctx);
        }
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BwGroup(org.bedework.calfacade.BwGroup) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties)

Example 59 with Attributes

use of javax.naming.directory.Attributes in project bw-calendar-engine by Bedework.

the class CardDAVDirImpl method getGroupMembers.

/* Find members for given group
   *
   */
private void getGroupMembers(final DirConfigProperties dirProps, final BwGroup group) throws CalFacadeException {
    LdapConfigProperties props = (LdapConfigProperties) dirProps;
    InitialLdapContext ctx = null;
    try {
        ctx = createLdapInitContext(props);
        BasicAttributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(props.getGroupIdAttr(), group.getAccount());
        String[] memberAttr = { props.getGroupMemberAttr() };
        ArrayList<String> mbrs = null;
        boolean beenHere = false;
        NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, memberAttr);
        while (response.hasMore()) {
            SearchResult sr = (SearchResult) response.next();
            Attributes attrs = sr.getAttributes();
            if (beenHere) {
                throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
            }
            beenHere = true;
            Attribute membersAttr = attrs.get(props.getGroupMemberAttr());
            mbrs = new ArrayList<String>();
            for (int m = 0; m < membersAttr.size(); m++) {
                mbrs.add(membersAttr.get(m).toString());
            }
        }
        // LDAP We need a way to search recursively for groups.
        /* Search for each user in the group */
        String memberContext = props.getGroupMemberContextDn();
        String memberSearchAttr = props.getGroupMemberSearchAttr();
        String[] idAttr = { props.getGroupMemberUserIdAttr(), props.getGroupMemberGroupIdAttr(), "objectclass" };
        for (String mbr : mbrs) {
            if (memberContext != null) {
                matchAttrs = new BasicAttributes(true);
                matchAttrs.put(memberSearchAttr, mbr);
                response = ctx.search(memberContext, matchAttrs, idAttr);
            } else {
                response = ctx.search(memberContext, null, idAttr);
            }
            if (response.hasMore()) {
                SearchResult sr = (SearchResult) response.next();
                Attributes attrs = sr.getAttributes();
                Attribute ocsAttr = attrs.get("objectclass");
                String userOc = props.getUserObjectClass();
                String groupOc = props.getGroupObjectClass();
                boolean isGroup = false;
                for (int oci = 0; oci < ocsAttr.size(); oci++) {
                    String oc = ocsAttr.get(oci).toString();
                    if (userOc.equals(oc)) {
                        break;
                    }
                    if (groupOc.equals(oc)) {
                        isGroup = true;
                        break;
                    }
                }
                BwPrincipal p = null;
                Attribute attr;
                if (isGroup) {
                    p = BwPrincipal.makeGroupPrincipal();
                    attr = attrs.get(props.getGroupMemberGroupIdAttr());
                } else {
                    p = BwPrincipal.makeUserPrincipal();
                    attr = attrs.get(props.getGroupMemberUserIdAttr());
                }
                if (attr.size() != 1) {
                    throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
                }
                p.setAccount(attr.get(0).toString());
                p.setPrincipalRef(makePrincipalUri(p.getAccount(), p.getKind()));
                group.addGroupMember(p);
            }
        }
    } catch (Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        // Close the context to release the connection
        if (ctx != null) {
            closeContext(ctx);
        }
    }
    for (BwGroup g : group.getGroups()) {
        getGroupMembers(props, g);
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BwGroup(org.bedework.calfacade.BwGroup) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwPrincipal(org.bedework.calfacade.BwPrincipal) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties)

Example 60 with Attributes

use of javax.naming.directory.Attributes in project bw-calendar-engine by Bedework.

the class UserGroupsLdapImpl method getGroups.

/* Return all groups for principal == null or all groups for which principal
   * is a member
   *
   */
private Collection<BwGroup> getGroups(final DirConfigProperties dirProps, final BwPrincipal principal) throws CalFacadeException {
    final ArrayList<BwGroup> groups = new ArrayList<>();
    final LdapConfigProperties props = (LdapConfigProperties) dirProps;
    if (props.getGroupMemberAttr() == null) {
        warn("No group member attribute set - assuming no groups");
        return groups;
    }
    InitialLdapContext ctx = null;
    String member = null;
    if (principal != null) {
        if (principal.getKind() == WhoDefs.whoTypeUser) {
            member = getUserEntryValue(props, principal);
        } else if (principal.getKind() == WhoDefs.whoTypeGroup) {
            member = getGroupEntryValue(props, principal);
        }
    }
    try {
        try {
            ctx = createLdapInitContext(props);
        } catch (final Throwable t) {
            warn("*******************************************");
            warn("No group information available");
            error(t);
            return groups;
        }
        final BasicAttributes matchAttrs = new BasicAttributes(true);
        if (member != null) {
            matchAttrs.put(props.getGroupMemberAttr(), member);
        }
        final String[] idAttr = { props.getGroupIdAttr() };
        final NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, idAttr);
        while (response.hasMore()) {
            final SearchResult sr = (SearchResult) response.next();
            final Attributes attrs = sr.getAttributes();
            final Attribute nmAttr = attrs.get(props.getGroupIdAttr());
            if (nmAttr.size() != 1) {
                throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
            }
            final BwGroup group = new BwGroup();
            group.setAccount(nmAttr.get(0).toString());
            group.setPrincipalRef(makePrincipalUri(group.getAccount(), WhoDefs.whoTypeGroup));
            groups.add(group);
        }
        return groups;
    } catch (final Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        // Close the context to release the connection
        if (ctx != null) {
            closeContext(ctx);
        }
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BwGroup(org.bedework.calfacade.BwGroup) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties)

Aggregations

Attributes (javax.naming.directory.Attributes)252 Attribute (javax.naming.directory.Attribute)135 SearchResult (javax.naming.directory.SearchResult)87 NamingException (javax.naming.NamingException)84 BasicAttributes (javax.naming.directory.BasicAttributes)72 ArrayList (java.util.ArrayList)61 BasicAttribute (javax.naming.directory.BasicAttribute)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)49 NamingEnumeration (javax.naming.NamingEnumeration)44 Test (org.junit.Test)34 InitialDirContext (javax.naming.directory.InitialDirContext)32 LdapContext (javax.naming.ldap.LdapContext)29 HashMap (java.util.HashMap)25 InitialLdapContext (javax.naming.ldap.InitialLdapContext)24 Hashtable (java.util.Hashtable)20 HashSet (java.util.HashSet)18 Map (java.util.Map)17 IOException (java.io.IOException)16 Identity (org.olat.core.id.Identity)16