Search in sources :

Example 1 with LdapConfigProperties

use of org.bedework.calfacade.configs.LdapConfigProperties in project bw-calendar-engine by Bedework.

the class CardDAVDirImpl method findGroup.

/* Search for a group to ensure it exists
   *
   */
private BwGroup findGroup(final DirConfigProperties dirProps, final String groupName) throws CalFacadeException {
    LdapConfigProperties props = (LdapConfigProperties) dirProps;
    InitialLdapContext ctx = null;
    try {
        ctx = createLdapInitContext(props);
        BasicAttributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(props.getGroupIdAttr(), groupName);
        String[] idAttr = { props.getGroupIdAttr() };
        BwGroup group = null;
        NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, idAttr);
        while (response.hasMore()) {
            if (group != null) {
                throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
            }
            group = new BwGroup();
            group.setAccount(groupName);
            group.setPrincipalRef(makePrincipalUri(groupName, WhoDefs.whoTypeGroup));
        }
        return group;
    } 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) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingEnumeration(javax.naming.NamingEnumeration) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties)

Example 2 with LdapConfigProperties

use of org.bedework.calfacade.configs.LdapConfigProperties in project bw-calendar-engine by Bedework.

the class CardDAVDirImpl method createLdapInitContext.

private InitialLdapContext createLdapInitContext(final LdapConfigProperties props) throws CalFacadeException {
    Properties env = new Properties();
    // Map all options into the JNDI InitialLdapContext env
    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, props.getInitialContextFactory());
    env.setProperty(Context.SECURITY_AUTHENTICATION, props.getSecurityAuthentication());
    env.setProperty(Context.SECURITY_PROTOCOL, props.getSecurityProtocol());
    env.setProperty(Context.PROVIDER_URL, props.getProviderUrl());
    String protocol = env.getProperty(Context.SECURITY_PROTOCOL);
    String providerURL = env.getProperty(Context.PROVIDER_URL);
    if (providerURL == null) {
        providerURL = "ldap://localhost:" + ((protocol != null) && protocol.equals("ssl") ? "389" : "636");
        env.setProperty(Context.PROVIDER_URL, providerURL);
    }
    if (props.getAuthDn() != null) {
        env.setProperty(Context.SECURITY_PRINCIPAL, props.getAuthDn());
        env.put(Context.SECURITY_CREDENTIALS, props.getAuthPw());
    }
    InitialLdapContext ctx = null;
    try {
        ctx = new InitialLdapContext(env, null);
        if (debug) {
            debug("Logged into LDAP server, " + ctx);
        }
        return ctx;
    } catch (Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    }
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) Properties(java.util.Properties) DirConfigProperties(org.bedework.calfacade.configs.DirConfigProperties) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 3 with LdapConfigProperties

use of org.bedework.calfacade.configs.LdapConfigProperties 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 4 with LdapConfigProperties

use of org.bedework.calfacade.configs.LdapConfigProperties 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 5 with LdapConfigProperties

use of org.bedework.calfacade.configs.LdapConfigProperties in project bw-calendar-engine by Bedework.

the class UserGroupsLdapImpl method findGroup.

/* Search for a group to ensure it exists
   *
   */
private BwGroup findGroup(final DirConfigProperties dirProps, final String groupName) throws CalFacadeException {
    LdapConfigProperties props = (LdapConfigProperties) dirProps;
    InitialLdapContext ctx = null;
    try {
        ctx = createLdapInitContext(props);
        BasicAttributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(props.getGroupIdAttr(), groupName);
        String[] idAttr = { props.getGroupIdAttr() };
        BwGroup group = null;
        NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, idAttr);
        while (response.hasMore()) {
            if (group != null) {
                throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
            }
            group = new BwGroup();
            group.setAccount(groupName);
            group.setPrincipalRef(makePrincipalUri(groupName, WhoDefs.whoTypeGroup));
        }
        return group;
    } 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) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingEnumeration(javax.naming.NamingEnumeration) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties)

Aggregations

InitialLdapContext (javax.naming.ldap.InitialLdapContext)8 LdapConfigProperties (org.bedework.calfacade.configs.LdapConfigProperties)8 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)8 NamingEnumeration (javax.naming.NamingEnumeration)6 BasicAttributes (javax.naming.directory.BasicAttributes)6 BwGroup (org.bedework.calfacade.BwGroup)6 Attribute (javax.naming.directory.Attribute)4 Attributes (javax.naming.directory.Attributes)4 SearchResult (javax.naming.directory.SearchResult)4 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 BwPrincipal (org.bedework.calfacade.BwPrincipal)2 DirConfigProperties (org.bedework.calfacade.configs.DirConfigProperties)2