Search in sources :

Example 11 with Dn

use of org.apache.directory.api.ldap.model.name.Dn in project syncope by apache.

the class LdifInputStreamLoader method execute.

/**
 * Opens the LDIF file and loads the entries into the context.
 *
 * @return The count of entries created.
 */
public int execute() {
    try {
        try {
            for (LdifEntry ldifEntry : new LdifReader(ldif)) {
                Dn dn = ldifEntry.getDn();
                if (ldifEntry.isEntry()) {
                    Entry entry = ldifEntry.getEntry();
                    try {
                        coreSession.lookup(dn);
                        LOG.debug("Found {}, will not create.", dn);
                    } catch (Exception e) {
                        try {
                            coreSession.add(new DefaultEntry(coreSession.getDirectoryService().getSchemaManager(), entry));
                            count++;
                            LOG.debug("Created {}.", dn);
                        } catch (LdapException e1) {
                            LOG.error("Could not create entry " + entry, e1);
                        }
                    }
                } else {
                    // modify
                    List<Modification> items = ldifEntry.getModifications();
                    try {
                        coreSession.modify(dn, items);
                        LOG.debug("Modified: " + dn + " with modificationItems: " + items);
                    } catch (LdapException e) {
                        LOG.debug("Could not modify: " + dn + " with modificationItems: " + items, e);
                    }
                }
            }
        } finally {
            ldif.close();
        }
    } catch (Exception ioe) {
        LOG.error(I18n.err(I18n.ERR_174), ioe);
    }
    return count;
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 12 with Dn

use of org.apache.directory.api.ldap.model.name.Dn in project openmeetings by apache.

the class LdapLoginManager method login.

/**
 * Ldap Login
 *
 * Connection Data is retrieved from ConfigurationFile
 *
 * @param _login - user login
 * @param passwd - user password
 * @param domainId - user domain id
 * @return - {@link User} with this credentials or <code>null</code>
 * @throws OmException - in case of any error
 */
public User login(String _login, String passwd, Long domainId) throws OmException {
    log.debug("LdapLoginmanager.doLdapLogin");
    if (!userDao.validLogin(_login)) {
        log.error("Invalid login provided");
        return null;
    }
    User u = null;
    try (LdapWorker w = new LdapWorker(domainId)) {
        String login = w.options.useLowerCase ? _login.toLowerCase() : _login;
        boolean authenticated = true;
        Dn userDn = null;
        Entry entry = null;
        switch(w.options.type) {
            case SEARCHANDBIND:
                {
                    bindAdmin(w.conn, w.options);
                    Dn baseDn = new Dn(w.options.searchBase);
                    String searchQ = String.format(w.options.searchQuery, login);
                    try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(new SearchRequestImpl().setBase(baseDn).setFilter(searchQ).setScope(w.options.scope).addAttributes("*").setDerefAliases(w.options.derefMode)))) {
                        while (cursor.next()) {
                            try {
                                Entry e = cursor.get();
                                if (userDn != null) {
                                    log.error("more than 1 user found in LDAP");
                                    throw UNKNOWN;
                                }
                                userDn = e.getDn();
                                if (w.options.useAdminForAttrs) {
                                    entry = e;
                                }
                            } catch (CursorLdapReferralException cle) {
                                log.warn("Referral LDAP entry found, ignore it");
                            }
                        }
                    }
                    if (userDn == null) {
                        log.error("NONE users found in LDAP");
                        throw BAD_CREDENTIALS;
                    }
                    w.conn.bind(userDn, passwd);
                }
                break;
            case SIMPLEBIND:
                userDn = new Dn(String.format(w.options.userDn, login));
                w.conn.bind(userDn, passwd);
                break;
            case NONE:
            default:
                authenticated = false;
                break;
        }
        u = authenticated ? userDao.getByLogin(login, Type.ldap, domainId) : userDao.login(login, passwd);
        log.debug("getByLogin:: authenticated ? {}, login = '{}', domain = {}, user = {}", authenticated, login, domainId, u);
        if (u == null && Provisionning.AUTOCREATE != w.options.prov) {
            log.error("User not found in OM DB and Provisionning.AUTOCREATE was not set");
            throw BAD_CREDENTIALS;
        }
        if (authenticated && entry == null) {
            if (w.options.useAdminForAttrs) {
                bindAdmin(w.conn, w.options);
            }
            entry = w.conn.lookup(userDn);
        }
        switch(w.options.prov) {
            case AUTOUPDATE:
            case AUTOCREATE:
                u = w.getUser(entry, u);
                if (w.options.syncPasswd) {
                    u.updatePassword(cfgDao, passwd);
                }
                u = userDao.update(u, null);
                break;
            case NONE:
            default:
                break;
        }
    } catch (LdapAuthenticationException ae) {
        log.error("Not authenticated.", ae);
        throw BAD_CREDENTIALS;
    } catch (OmException e) {
        throw e;
    } catch (Exception e) {
        log.error("Unexpected exception.", e);
        throw new OmException(e);
    }
    return u;
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) EntryCursorImpl(org.apache.directory.ldap.client.api.EntryCursorImpl) User(org.apache.openmeetings.db.entity.user.User) GroupUser(org.apache.openmeetings.db.entity.user.GroupUser) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) OmException(org.apache.openmeetings.util.OmException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) OmException(org.apache.openmeetings.util.OmException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)

Example 13 with Dn

use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.

the class UserClass_NameTest method initNames.

/**
 * Initialize name instances
 */
@Before
public void initNames() throws LdapInvalidDnException {
    Set<String> dnSetA = new HashSet<>();
    dnSetA.add(new Dn("a=aa").getNormName());
    dnSetA.add(new Dn("b=bb").getNormName());
    Set<String> dnSetB = new HashSet<>();
    dnSetB.add(new Dn("b=bb").getNormName());
    dnSetB.add(new Dn("a=aa").getNormName());
    Set<String> dnSetC = new HashSet<>();
    dnSetC.add(new Dn("a=aa").getNormName());
    dnSetC.add(new Dn("b=bb").getNormName());
    Set<String> dnSetD = new HashSet<>();
    dnSetD.add(new Dn("b=bb").getNormName());
    dnSetD.add(new Dn("c=cc").getNormName());
    nameA = new Name(dnSetA);
    nameACopy = new Name(dnSetB);
    nameB = new Name(dnSetC);
    nameC = new Name(dnSetD);
}
Also used : Dn(org.apache.directory.api.ldap.model.name.Dn) HashSet(java.util.HashSet) Name(org.apache.directory.api.ldap.aci.UserClass.Name) Before(org.junit.Before)

Example 14 with Dn

use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.

the class UserClass_SubtreeTest method initNames.

/**
 * Initialize name instances
 */
@Before
public void initNames() throws Exception {
    SubtreeSpecification subtreeSpecA = new BaseSubtreeSpecification();
    SubtreeSpecification subtreeSpecB = new BaseSubtreeSpecification();
    SubtreeSpecification subtreeSpecC = new BaseSubtreeSpecification();
    SubtreeSpecification subtreeSpecD = new BaseSubtreeSpecification(new Dn("cn=dummy"));
    Set<SubtreeSpecification> colA = new HashSet<SubtreeSpecification>();
    colA.add(subtreeSpecA);
    colA.add(subtreeSpecB);
    colA.add(subtreeSpecC);
    Set<SubtreeSpecification> colB = new HashSet<SubtreeSpecification>();
    colB.add(subtreeSpecA);
    colB.add(subtreeSpecB);
    colB.add(subtreeSpecC);
    Set<SubtreeSpecification> colC = new HashSet<SubtreeSpecification>();
    colC.add(subtreeSpecB);
    colC.add(subtreeSpecC);
    colC.add(subtreeSpecD);
    subtreeA = new Subtree(colA);
    subtreeACopy = new Subtree(colA);
    subtreeB = new Subtree(colB);
    subtreeC = new Subtree(colC);
}
Also used : BaseSubtreeSpecification(org.apache.directory.api.ldap.model.subtree.BaseSubtreeSpecification) Subtree(org.apache.directory.api.ldap.aci.UserClass.Subtree) Dn(org.apache.directory.api.ldap.model.name.Dn) SubtreeSpecification(org.apache.directory.api.ldap.model.subtree.SubtreeSpecification) BaseSubtreeSpecification(org.apache.directory.api.ldap.model.subtree.BaseSubtreeSpecification) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 15 with Dn

use of org.apache.directory.api.ldap.model.name.Dn in project directory-ldap-api by apache.

the class EntryChangeControlTest method testEncodeEntryChangeControl.

/**
 * Test encoding of a EntryChangeControl.
 */
@Test
public void testEncodeEntryChangeControl() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x0D);
    bb.put(new byte[] { // EntryChangeNotification ::= SEQUENCE {
    0x30, // EntryChangeNotification ::= SEQUENCE {
    0x0B, 0x0A, 0x01, // changeType ENUMERATED {
    0x08, // }
    0x04, 0x03, 'a', '=', // previousDN LDAPDN OPTIONAL, -- modifyDN ops. only
    'b', 0x02, 0x01, // changeNumber INTEGER OPTIONAL -- if supported
    0x10 });
    String expected = Strings.dumpBytes(bb.array());
    bb.flip();
    EntryChangeDecorator decorator = new EntryChangeDecorator(codec);
    EntryChange entryChange = (EntryChange) decorator.getDecorated();
    entryChange.setChangeType(ChangeType.MODDN);
    entryChange.setChangeNumber(16);
    entryChange.setPreviousDn(new Dn("a=b"));
    bb = decorator.encode(ByteBuffer.allocate(decorator.computeLength()));
    String decoded = Strings.dumpBytes(bb.array());
    assertEquals(expected, decoded);
}
Also used : EntryChangeDecorator(org.apache.directory.api.ldap.codec.controls.search.entryChange.EntryChangeDecorator) Dn(org.apache.directory.api.ldap.model.name.Dn) EntryChange(org.apache.directory.api.ldap.model.message.controls.EntryChange) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

Dn (org.apache.directory.api.ldap.model.name.Dn)307 Test (org.junit.Test)183 Rdn (org.apache.directory.api.ldap.model.name.Rdn)63 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)50 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)39 Entry (org.apache.directory.api.ldap.model.entry.Entry)34 DnNode (org.apache.directory.api.ldap.util.tree.DnNode)30 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)20 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)19 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)17 Modification (org.apache.directory.api.ldap.model.entry.Modification)17 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)16 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)10 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)10 ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)10 Referral (org.apache.directory.api.ldap.model.message.Referral)10 File (java.io.File)9 ArrayList (java.util.ArrayList)9 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)8 Value (org.apache.directory.api.ldap.model.entry.Value)8