Package com.novell.ldap

Examples of com.novell.ldap.LDAPConnection


        log.debug("Removing user entry from ldap");
      }
    }

    int ldapVersion = LDAPConnection.LDAP_V3;
    LDAPConnection lc = createLdapConnection();

    final String dn;
    final String certdn;
    try {
      // Extract the users DN from the cert.
      certdn = CertTools.getSubjectDN(cert);
      dn = constructLDAPDN(certdn, userDN);
    } catch (Exception e) {
      String msg = intres.getLocalizedMessage("publisher.errorldapdecode", "certificate");
      log.error(msg, e);           
      throw new PublisherException(msg);           
    }

    // Extract the users email from the cert.
    String email = CertTools.getEMailAddress(cert);

    // Check if the entry is already present, we will update it with the new certificate.
    LDAPEntry oldEntry = searchOldEntity(username, ldapVersion, lc, certdn, userDN, email);

    ArrayList modSet = null;

    if (!CertTools.isCA(cert)) {
      if (log.isDebugEnabled()) {
        log.debug("Removing end user certificate from first available server of " + getHostnames());
      }
      if (oldEntry != null) {         
        if (removecert) {
          // Don't try to remove the cert if there does not exist any
          LDAPAttribute oldAttr = oldEntry.getAttribute(getUserCertAttribute());
          if (oldAttr != null) {
            modSet = getModificationSet(oldEntry, certdn, null, false, true, null);
            LDAPAttribute attr = new LDAPAttribute(getUserCertAttribute());
            modSet.add(new LDAPModification(LDAPModification.DELETE, attr));                   
          } else {
            String msg = intres.getLocalizedMessage("publisher.inforevokenocert");
            log.info(msg);
          }               
        }
      } else {
        String msg = intres.getLocalizedMessage("publisher.errorrevokenoentry");
        log.error(msg);           
        throw new PublisherException(msg);           
      }
    } else  {
      // Removal of CA certificate isn't support because of object class restrictions
      if (log.isDebugEnabled()) {
        log.debug("Not removing CA certificate from first available server of " + getHostnames() + ", because of object class restrictions.");
      }
    }

    // Try all the listed servers
    Iterator servers = getHostnameList().iterator();
    boolean connectionFailed;
    do {
      connectionFailed = false;
      String currentServer =(String) servers.next();
      if (log.isDebugEnabled()) {
        log.debug("currentServer: "+currentServer);
      }
      try {
        TCPTool.probeConnectionLDAP(currentServer, Integer.parseInt(getPort()), getConnectionTimeOut())// Avoid waiting for halfdead-servers
        lc.connect(currentServer, Integer.parseInt(getPort()));
        // authenticate to the server
        lc.bind(ldapVersion, getLoginDN(), getLoginPassword().getBytes("UTF8"), ldapBindConstraints);           
        // Add or modify the entry
        if (oldEntry != null && modSet != null && getModifyExistingUsers()) {
          if (removecert) {
            LDAPModification[] mods = new LDAPModification[modSet.size()];
            mods = (LDAPModification[])modSet.toArray(mods);
            lc.modify(oldEntry.getDN(), mods, ldapStoreConstraints);               
          }
          if (removeuser) {
            lc.delete(oldEntry.getDN(), ldapStoreConstraints);               
          }
          String msg = intres.getLocalizedMessage("publisher.ldapremove", dn);
          log.info(msg)
        } else {
          if (log.isDebugEnabled()) {
            if (oldEntry == null) {
              log.debug("Not modifying LDAP entry because there is no existing entry.");           
            }
            if (modSet == null) {
              log.debug("Not modifying LDAP entry because we don't have anything to modify.");           
            }
            if (!getModifyExistingUsers()) {
              log.debug("Not modifying LDAP entry because we're not configured to do so.");           
            }
          }
        }
      } catch (LDAPException e) {
        connectionFailed = true;
        if (servers.hasNext()) {
          log.warn("Failed to publish to " + currentServer + ". Trying next in list.");
        } else {
          String msg = intres.getLocalizedMessage("publisher.errorldapremove", dn);
          log.error(msg, e)
          throw new PublisherException(msg);           
        }
      } catch (UnsupportedEncodingException e) {
        String msg = intres.getLocalizedMessage("publisher.errorpassword", getLoginPassword());
        log.error(msg, e);
        throw new PublisherException(msg);           
      } finally {
        // disconnect with the server
        try {
          lc.disconnect(ldapDisconnectConstraints);
        } catch (LDAPException e) {
          String msg = intres.getLocalizedMessage("publisher.errordisconnect");
          log.error(msg, e);
        }
      }
View Full Code Here


  /**
   * @see org.ejbca.core.model.ca.publisher.BasePublisher#testConnection(Admin)
   */   
  public void testConnection(Admin admin) throws PublisherConnectionException {
    int ldapVersion = LDAPConnection.LDAP_V3;
    LDAPConnection lc = createLdapConnection();
    // Try all the listed servers
    Iterator servers = getHostnameList().iterator();
    boolean connectionFailed;
    do {
      connectionFailed = false;
      String currentServer = (String) servers.next();
      LDAPEntry entry = null;
      try {
        TCPTool.probeConnectionLDAP(currentServer, Integer.parseInt(getPort()), getConnectionTimeOut())// Avoid waiting for halfdead-servers
        // connect to the server
        lc.connect(currentServer, Integer.parseInt(getPort()));
        // authenticate to the server
        lc.bind(ldapVersion, getLoginDN(), getLoginPassword().getBytes("UTF8"), ldapBindConstraints);
        // try to read the base object
        String baseDN = getBaseDN();
        if (log.isDebugEnabled()) {
          log.debug("Trying to read top node '"+baseDN+"'");
        }
        entry = lc.read(baseDN, ldapSearchConstraints);     
        if(entry == null) {
          String msg = intres.getLocalizedMessage("publisher.errornobinddn");
          throw new PublisherConnectionException(msg);
        }
        if (log.isDebugEnabled()) {
          log.debug("Entry" + entry.toString());
        }
      } catch (LDAPException e) {
        connectionFailed = true;
        if (servers.hasNext()) {
          log.warn("Failed to connect to " + currentServer + ". Trying next in list.", e);
        } else {
          String msg = intres.getLocalizedMessage("publisher.errorldapbind", e.getMessage());
          log.error(msg, e);
          throw new PublisherConnectionException(msg);                           
        }
      } catch (UnsupportedEncodingException e) {
        String msg = intres.getLocalizedMessage("publisher.errorpassword", getLoginPassword());
        log.error(msg, e);
        throw new PublisherConnectionException(msg);           
      } finally {
        // disconnect with the server
        try {
          lc.disconnect(ldapDisconnectConstraints);
        } catch (LDAPException e) {
          String msg = intres.getLocalizedMessage("publisher.errordisconnect");
          log.error(msg, e);
        }
      }
View Full Code Here

      log.debug("bindtimeout: "+ldapBindConstraints.getTimeLimit());
      log.debug("disconnecttimeout: "+ldapDisconnectConstraints.getTimeLimit());
      log.debug("readtimeout: "+ldapSearchConstraints.getTimeLimit());
      log.debug("storetimeout: "+ldapStoreConstraints.getTimeLimit());
    }
    LDAPConnection lc;
    if (getUseSSL()) {
      lc = new LDAPConnection(new LDAPJSSESecureSocketFactory());
    } else {
      lc = new LDAPConnection();
    }
    lc.setConstraints(ldapConnectionConstraints);
    return lc;
  }
View Full Code Here

    public static final String LDAPServerAddress = "192.168.4.120";

    public LDAPService() {}

    public static SysUser getUserFromLDAP(String uid) throws EasyJException {
        LDAPConnection connection = new LDAPConnection();
        try {
            connection.connect(LDAPServerAddress, 389);
            connection.bind(LDAPConnection.LDAP_V3, "cn=admin,dc=sei,dc=pku",
                    "seiseforge");
            LDAPSearchResults rs = connection.search("uid=" + uid
                    + ",ou=People,o=SEForge,dc=sei,dc=pku",
                    LDAPConnection.SCOPE_SUB, "objectClass=*", null, false);
            SysUser user = new SysUser();
            user.setUserName(uid);
            if (!rs.hasMore()) {
                throw new EasyJException(
                        null,
                        "easyJ.system.service.LDAPService.getUserFromLDAP(String)",
                        user.getUserName() + "的用户名密码错", "用户名密码错");
            }
            while (rs.hasMore()) {

                LDAPEntry entry = rs.next();
                LDAPAttributeSet attSet = entry.getAttributeSet();
                Iterator it = attSet.iterator();
                while (it.hasNext()) {
                    LDAPAttribute attr = (LDAPAttribute) it.next();
                    if (attr.getName().equalsIgnoreCase("userPassword")) {
                        user.setPassword(attr.getStringValue());
                    }
                }
            }
            return user;
        } catch (LDAPException e) {
            // e.printStackTrace();
            throw new EasyJException(null,
                    "easyJ.system.service.LDAPService.getUserFromLDAP(String)",
                    "验证服务器出错", "验证服务器出错");
        } finally {
            try {
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
View Full Code Here

            }
        }
    }

    public static void addUserToLDAP(SysUser user) throws EasyJException {
        LDAPConnection connection = new LDAPConnection();
        try {
            LDAPAttributeSet attributeSet = new LDAPAttributeSet();

            attributeSet.add(new LDAPAttribute("objectclass", new String[] {
                "pilotPerson", "uidObject"
            }));
            attributeSet.add(new LDAPAttribute("uid", user.getUserName()));
            attributeSet.add(new LDAPAttribute("userPassword", user
                    .getPassword()));
            attributeSet.add(new LDAPAttribute("mail", user.getEmail()));
            attributeSet.add(new LDAPAttribute("sn", "snMass"));
            attributeSet.add(new LDAPAttribute("cn", "cnMass"));
            LDAPEntry entry = new LDAPEntry("uid=" + user.getUserName()
                    + ",ou=People,o=SEForge,dc=sei,dc=pku", attributeSet);
            connection.connect(LDAPServerAddress, 389);
            connection.bind(LDAPConnection.LDAP_V3, "cn=admin,dc=sei,dc=pku",
                    "seiseforge");
            connection.add(entry);
            System.out.println("成功的添加了一条记录!");
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            throw new EasyJException(null,
                    "easyJ.system.service.LDAPService.addUserToLDAP(SysUser)",
                    "验证服务器出错", "验证服务器出错");
View Full Code Here

        }

    }

    public static void changePassword(SysUser user) throws EasyJException {
        LDAPConnection connection = new LDAPConnection();
        try {
            connection.connect(LDAPServerAddress, 389);
            connection.bind(LDAPConnection.LDAP_V3, "cn=admin,dc=sei,dc=pku",
                    "seiseforge");
            connection.modify("uid=" + user.getUserName()
                    + ",ou=People,o=SEForge,dc=sei,dc=pku",
                    new LDAPModification(LdapContext.REPLACE_ATTRIBUTE,
                            new LDAPAttribute("userPassword", user
                                    .getPassword())));
            System.out.println("成功修改一条记录!");
            connection.disconnect();
        } catch (LDAPException e) {
            e.printStackTrace();
            throw new EasyJException(null,
                    "easyJ.system.service.LDAPService.changePassword(SysUser)",
                    "验证服务器出错", "验证服务器出错");
View Full Code Here

   *
   * @throws Exception the exception
   */
  public LDAPConnection createConnection () throws Exception {

      LDAPConnection connection = null;

        try {
          connection = new LDAPConnection();
          // connessione
          connection.connect("localhost",389);
          if (connection.isConnected()) {
            // login
            connection.bind(LDAPConnection.LDAP_V3,"cn=root,dc=spagobi,dc=com","root".getBytes("UTF8"));
            //connection.bind(3,"cn=biadmin,ou=People,dc=spagobi,dc=com","biadmin".getBytes("UTF8"));
          }
          if (connection.isBound()) {
            // valore della radice e dello scope di ricerca
            return connection;
          }
        }catch (LDAPException e) {
          System.out.println("errore in createConnection:: createConnection " + e);       
        }
        catch(UnsupportedEncodingException e) {
          System.out.println("errore in createConnection:: createConnection:  " + e);       
        }     
     
      if (!connection.isConnected() || !connection.isBound()) {
        System.out.println("errore in UserContextHandling:: createConnection: connessione fallita");       
        throw new Exception("UserContextHandling:: createConnection: connessione fallita");
      }
      return null;
     
View Full Code Here

   *
   * @throws Exception the exception
   */
  public boolean autenticateUser (String userId,String psw) throws Exception {

      LDAPConnection connection = null;

        try {
          connection = new LDAPConnection();
          // connessione
          connection.connect("localhost",389);
          if (connection.isConnected()) {
            // login
            connection.bind(LDAPConnection.LDAP_V3,"cn="+userId+",ou=People,dc=spagobi,dc=com",psw.getBytes("UTF8"));
          }
          if (connection.isBound()) {
            return true;
          }
        }catch (LDAPException e) {
          System.out.println("errore in createConnection:: createConnection " + e);       
        }
        catch(UnsupportedEncodingException e) {
          System.out.println("errore in createConnection:: createConnection:  " + e);       
        }     
     
      if (!connection.isConnected() || !connection.isBound()) {
        System.out.println("errore in UserContextHandling:: createConnection: connessione fallita");       
        return false;
      }
      return false;
     
View Full Code Here

    }
 
  private HashMap getUserAttributes(String userId) throws Exception {
    HashMap userAttributes = new HashMap();
     
      LDAPConnection connection = createConnection();
      if (connection != null) {
      try {
        String[] attrIDs = {"description","sn"};
          LDAPSearchResults searchResults = connection.search("ou=People,dc=spagobi,dc=com",
              LDAPConnection.SCOPE_SUB,
              "(&(objectclass=person)(cn=biadmin))",
              attrIDs,false);
         
          // popolamento userAttributes con attributeName e attributeValue
          LDAPEntry entry = null;
          LDAPAttributeSet attributeSet = null;
          if (searchResults.hasMore()) {
                try {
                    entry = searchResults.next();
                }catch(LDAPException e) {
                  e.printStackTrace();
                    System.out.println("errore in UserContext:: getUserAttributes: " + e.getMessage());
                }           
          }
           
            if (entry != null) {
              attributeSet = entry.getAttributeSet();
            userAttributes.put("dn", entry.getDN());             
            userAttributes.put("description", entry.getAttribute("description"));
            userAttributes.put("sn", entry.getAttribute("sn"));
                       
            }

     }catch (LDAPException e) {
         System.out.println("errore in UserContext:: getUserAttributes: " + e);            
         throw e;
     }finally {
       if (connection != null)
         connection.disconnect();
     }
     
      }
     
      return userAttributes;
View Full Code Here

  }

  private List getUserGroup(String userId) throws Exception {
    List userAttributes = new ArrayList();
     
      LDAPConnection connection = createConnection();
      if (connection != null) {
      try {
        String[] attrIDs = {"description","sn","ou"};
          LDAPSearchResults searchResults = connection.search("ou=People,dc=spagobi,dc=com",
              LDAPConnection.SCOPE_SUB,
              "(&(objectclass=person)(cn=biadmin))",
              attrIDs,false);
         
          // popolamento userAttributes con attributeName e attributeValue
          LDAPEntry entry = null;
          LDAPAttributeSet attributeSet = null;
          if (searchResults.hasMore()) {
                try {
                    entry = searchResults.next();
                }catch(LDAPException e) {
                  e.printStackTrace();
                    System.out.println("errore in UserContext:: getUserAttributes: " + e.getMessage());
                }           
          }
           
            if (entry != null) {
              attributeSet = entry.getAttributeSet();
              String[] ou=entry.getAttribute("ou").getStringValueArray();
             
            userAttributes.add(ou[0]);             
            userAttributes.add(ou[1]);
                       
            }

     }catch (LDAPException e) {
         System.out.println("errore in UserContext:: getUserAttributes: " + e);            
         throw e;
     }finally {
       if (connection != null)
         connection.disconnect();
     }
     
      }
     
      return userAttributes;
View Full Code Here

TOP

Related Classes of com.novell.ldap.LDAPConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.