public Object doLdapLogin(String user,String passwd,RoomClient currentClient,String SID,String domain){
  log.debug("LdapLoginmanagement.doLdapLogin");
  HashMap<String,String> configData;
  try {
    configData=getLdapConfigData(domain);
  }
 catch (  Exception e) {
    log.error("Error on LdapAuth : " + e.getMessage());
    return null;
  }
  if (configData == null || configData.size() < 1) {
    log.error("Error on LdapLogin : Configurationdata couldnt be retrieved!");
    return null;
  }
  String ldap_url=configData.get(CONFIGKEY_LDAP_URL);
  String ldap_server_type=configData.get(CONFIGKEY_LDAP_SERVER_TYPE);
  String ldap_admin_dn=configData.get(CONFIGKEY_LDAP_ADMIN_DN);
  String ldap_passwd=configData.get(CONFIGKEY_LDAP_ADMIN_PASSWD);
  String ldap_search_scope=configData.get(CONFIGKEY_LDAP_SEARCH_SCOPE);
  String ldap_fieldname_user_principal=configData.get(CONFIGKEY_LDAP_FIELDNAME_USER_PRINCIPAL);
  String ldap_sync_passwd_to_om=configData.get(CONFIGKEY_LDAP_SYNC_PASSWD_OM);
  String ldap_user_attr_lastname=configData.get(CONFIGKEY_LDAP_KEY_LASTNAME);
  String ldap_user_attr_firstname=configData.get(CONFIGKEY_LDAP_KEY_FIRSTNAME);
  String ldap_user_attr_mail=configData.get(CONFIGKEY_LDAP_KEY_MAIL);
  String ldap_user_attr_street=configData.get(CONFIGKEY_LDAP_KEY_STREET);
  String ldap_user_attr_additional_name=configData.get(CONFIGKEY_LDAP_KEY_ADDITIONAL_NAME);
  String ldap_user_attr_fax=configData.get(CONFIGKEY_LDAP_KEY_FAX);
  String ldap_user_attr_zip=configData.get(CONFIGKEY_LDAP_KEY_ZIP);
  String ldap_user_attr_country=configData.get(CONFIGKEY_LDAP_KEY_COUNTRY);
  String ldap_user_attr_town=configData.get(CONFIGKEY_LDAP_KEY_TOWN);
  String ldap_user_attr_phone=configData.get(CONFIGKEY_LDAP_KEY_PHONE);
  String ldap_user_attr_timezone=configData.get(CONFIGKEY_LDAP_TIMEZONE_NAME);
  String ldap_use_lower_case=configData.get(CONFIGKEY_LDAP_USE_LOWER_CASE);
  if (ldap_use_lower_case != null && ldap_use_lower_case.equals("true")) {
    user=user.toLowerCase();
  }
  if (ldap_user_attr_lastname == null) {
    ldap_user_attr_lastname=LDAP_KEY_LASTNAME;
  }
  if (ldap_user_attr_firstname == null) {
    ldap_user_attr_firstname=LDAP_KEY_FIRSTNAME;
  }
  if (ldap_user_attr_mail == null) {
    ldap_user_attr_mail=LDAP_KEY_MAIL;
  }
  if (ldap_user_attr_street == null) {
    ldap_user_attr_street=LDAP_KEY_STREET;
  }
  if (ldap_user_attr_additional_name == null) {
    ldap_user_attr_additional_name=LDAP_KEY_ADDITIONAL_NAME;
  }
  if (ldap_user_attr_fax == null) {
    ldap_user_attr_fax=LDAP_KEY_FAX;
  }
  if (ldap_user_attr_zip == null) {
    ldap_user_attr_zip=LDAP_KEY_ZIP;
  }
  if (ldap_user_attr_country == null) {
    ldap_user_attr_country=LDAP_KEY_COUNTRY;
  }
  if (ldap_user_attr_town == null) {
    ldap_user_attr_town=LDAP_KEY_TOWN;
  }
  if (ldap_user_attr_phone == null) {
    ldap_user_attr_phone=LDAP_KEY_PHONE;
  }
  if (ldap_user_attr_timezone == null) {
    ldap_user_attr_timezone=LDAP_KEY_TIMEZONE;
  }
  String ldap_auth_type=configData.get(CONFIGKEY_LDAP_AUTH_TYPE);
  if (ldap_auth_type == null)   ldap_auth_type="";
  if (!isValidAuthType(ldap_auth_type)) {
    log.error("ConfigKey in Ldap Config contains invalid auth type : '" + ldap_auth_type + "' -> Defaulting to "+ LdapAuthBase.LDAP_AUTH_TYPE_SIMPLE);
    ldap_auth_type=LdapAuthBase.LDAP_AUTH_TYPE_SIMPLE;
  }
  String ldap_search_filter="(" + ldap_fieldname_user_principal + "="+ user+ ")";
  log.debug("Searching userdata with LDAP Search Filter :" + ldap_search_filter);
  ldap_search_scope=ldap_search_scope.replaceAll(":","=");
  ldap_admin_dn=ldap_admin_dn.replaceAll(":","=");
  LdapAuthBase lAuth=new LdapAuthBase(ldap_url,ldap_admin_dn,ldap_passwd,ldap_auth_type);
  log.debug("authenticating admin...");
  lAuth.authenticateUser(ldap_admin_dn,ldap_passwd);
  log.debug("Checking server type...");
  if (ldap_server_type.equalsIgnoreCase("OpenLDAP")) {
    String ldapUserDN=user;
    log.debug("LDAP server is OpenLDAP");
    log.debug("LDAP search base: " + ldap_search_scope);
    HashMap<String,String> uidCnDictionary=lAuth.getUidCnHashMap(ldap_search_scope,ldap_search_filter,ldap_fieldname_user_principal);
    if (uidCnDictionary.get(user) != null) {
      ldapUserDN=uidCnDictionary.get(user) + "," + ldap_search_scope;
      log.debug("Authentication with DN: " + ldapUserDN);
    }
    try {
      if (!lAuth.authenticateUser(ldapUserDN,passwd)) {
        log.error(ldapUserDN + " not authenticated.");
        return new Long(-11);
      }
    }
 catch (    Exception e) {
      log.error("Error on LdapAuth : " + e.getMessage());
      return null;
    }
  }
 else {
    try {
      if (!lAuth.authenticateUser(user,passwd))       return new Long(-11);
    }
 catch (    Exception e) {
      log.error("Error on LdapAuth : " + e.getMessage());
      return null;
    }
  }
  Users u=null;
  try {
    u=userManagement.getUserByLogin(user);
  }
 catch (  Exception e) {
    log.error("Error retrieving Userdata : " + e.getMessage());
  }
  if (u == null) {
    log.debug("user doesnt exist local -> create new");
    List<String> attributes=new ArrayList<String>();
    attributes.add(ldap_user_attr_lastname);
    attributes.add(ldap_user_attr_firstname);
    attributes.add(ldap_user_attr_mail);
    attributes.add(ldap_user_attr_street);
    attributes.add(ldap_user_attr_additional_name);
    attributes.add(ldap_user_attr_fax);
    attributes.add(ldap_user_attr_zip);
    attributes.add(ldap_user_attr_country);
    attributes.add(ldap_user_attr_town);
    attributes.add(ldap_user_attr_phone);
    attributes.add(ldap_user_attr_timezone);
    HashMap<String,String> ldapAttrs=new HashMap<String,String>();
    ldapAttrs.put("lastnameAttr",ldap_user_attr_lastname);
    ldapAttrs.put("firstnameAttr",ldap_user_attr_firstname);
    ldapAttrs.put("mailAttr",ldap_user_attr_mail);
    ldapAttrs.put("streetAttr",ldap_user_attr_street);
    ldapAttrs.put("additionalNameAttr",ldap_user_attr_additional_name);
    ldapAttrs.put("faxAttr",ldap_user_attr_fax);
    ldapAttrs.put("zipAttr",ldap_user_attr_zip);
    ldapAttrs.put("countryAttr",ldap_user_attr_country);
    ldapAttrs.put("townAttr",ldap_user_attr_town);
    ldapAttrs.put("phoneAttr",ldap_user_attr_phone);
    ldapAttrs.put("phoneAttr",ldap_user_attr_phone);
    ldapAttrs.put("timezoneAttr",ldap_user_attr_timezone);
    Vector<HashMap<String,String>> result=lAuth.getData(ldap_search_scope,ldap_search_filter,attributes);
    if (result == null || result.size() < 1) {
      log.error("Error on Ldap request - no result for user " + user);
      return new Long(-10);
    }
    if (result.size() > 1) {
      log.error("Error on Ldap request - more than one result for user " + user);
      return null;
    }
    HashMap<String,String> userData=result.get(0);
    try {
      Long userid;
      if (ldap_sync_passwd_to_om != null && ldap_sync_passwd_to_om.equals("no")) {
        Random r=new Random();
        String token=Long.toString(Math.abs(r.nextLong()),36);
        log.debug("Synching Ldap user to OM DB with RANDOM password: " + token);
        userid=createUserFromLdapData(userData,token,user,ldapAttrs);
      }
 else {
        log.debug("Synching Ldap user to OM DB with password");
        userid=createUserFromLdapData(userData,passwd,user,ldapAttrs);
      }
      log.debug("New User ID : " + userid);
      if (currentClient != null) {
        currentClient.setUser_id(userid);
      }
      Boolean bool=sessionManagement.updateUser(SID,userid);
      if (bool == null) {
        log.error("Error on Updating Session");
        return new Long(-1);
      }
 else       if (!bool) {
        log.error("Invalid Session Object");
        return new Long(-35);
      }
      Users u2=userManagement.getUserById(userid);
      if (u2 == null)       return new Long(-1);
      u2.setExternalUserType(EXTERNAL_USER_TYPE_LDAP);
      userManagement.refreshUserObject(u2);
      log.debug("getUserbyId : " + userid + " : "+ u2.getLogin());
      return u2;
    }
 catch (    Exception e) {
      log.error("Error on Working Userdata : ",e);
      return new Long(-1);
    }
  }
 else {
    log.debug("User already exists -> Update of current passwd");
    if (currentClient != null) {
      currentClient.setUser_id(u.getUser_id());
    }
    Boolean bool=sessionManagement.updateUser(SID,u.getUser_id());
    if (bool == null) {
      log.error("Error on Updating Session");
      return new Long(-1);
    }
 else     if (!bool) {
      log.error("Invalid Session Object");
      return new Long(-35);
    }
    if (ldap_sync_passwd_to_om == null || !ldap_sync_passwd_to_om.equals("no")) {
      u.setPassword(passwd);
    }
    try {
      userManagement.updateUserObject(u,true);
    }
 catch (    Exception e) {
      log.error("Error updating user : " + e.getMessage());
      return new Long(-1);
    }
    return u;
  }
}

public Object doLdapLogin ( String user , String passwd , RoomClient currentClient , String SID , String domain ) { log . debug ( "LdapLoginmanagement.doLdapLogin" ) ; HashMap < String , String > configData ; try { configData = getLdapConfigData ( domain ) ; } catch ( Exception e ) { log . error ( "Error on LdapAuth : " + e . getMessage ( ) ) ; return null ; } if ( configData == null || configData . size ( ) < 1 ) { log . error ( "Error on LdapLogin : Configurationdata couldnt be retrieved!" ) ; return null ; } String ldap_url = configData . get ( CONFIGKEY_LDAP_URL ) ; String ldap_server_type = configData . get ( CONFIGKEY_LDAP_SERVER_TYPE ) ; String ldap_admin_dn = configData . get ( CONFIGKEY_LDAP_ADMIN_DN ) ; String ldap_passwd = configData . get ( CONFIGKEY_LDAP_ADMIN_PASSWD ) ; String ldap_search_scope = configData . get ( CONFIGKEY_LDAP_SEARCH_SCOPE ) ; String ldap_fieldname_user_principal = configData . get ( CONFIGKEY_LDAP_FIELDNAME_USER_PRINCIPAL ) ; String ldap_sync_passwd_to_om = configData . get ( CONFIGKEY_LDAP_SYNC_PASSWD_OM ) ; String ldap_user_attr_lastname = configData . get ( CONFIGKEY_LDAP_KEY_LASTNAME ) ; String ldap_user_attr_firstname = configData . get ( CONFIGKEY_LDAP_KEY_FIRSTNAME ) ; String ldap_user_attr_mail = configData . get ( CONFIGKEY_LDAP_KEY_MAIL ) ; String ldap_user_attr_street = configData . get ( CONFIGKEY_LDAP_KEY_STREET ) ; String ldap_user_attr_additional_name = configData . get ( CONFIGKEY_LDAP_KEY_ADDITIONAL_NAME ) ; String ldap_user_attr_fax = configData . get ( CONFIGKEY_LDAP_KEY_FAX ) ; String ldap_user_attr_zip = configData . get ( CONFIGKEY_LDAP_KEY_ZIP ) ; String ldap_user_attr_country = configData . get ( CONFIGKEY_LDAP_KEY_COUNTRY ) ; String ldap_user_attr_town = configData . get ( CONFIGKEY_LDAP_KEY_TOWN ) ; String ldap_user_attr_phone = configData . get ( CONFIGKEY_LDAP_KEY_PHONE ) ; String ldap_user_attr_timezone = configData . get ( CONFIGKEY_LDAP_TIMEZONE_NAME ) ; String ldap_use_lower_case = configData . get ( CONFIGKEY_LDAP_USE_LOWER_CASE ) ; if ( ldap_use_lower_case != null && ldap_use_lower_case . equals ( "true" ) ) { user = user . toLowerCase ( ) ; } if ( ldap_user_attr_lastname == null ) { ldap_user_attr_lastname = LDAP_KEY_LASTNAME ; } if ( ldap_user_attr_firstname == null ) { ldap_user_attr_firstname = LDAP_KEY_FIRSTNAME ; } if ( ldap_user_attr_mail == null ) { ldap_user_attr_mail = LDAP_KEY_MAIL ; } if ( ldap_user_attr_street == null ) { ldap_user_attr_street = LDAP_KEY_STREET ; } if ( ldap_user_attr_additional_name == null ) { ldap_user_attr_additional_name = LDAP_KEY_ADDITIONAL_NAME ; } if ( ldap_user_attr_fax == null ) { ldap_user_attr_fax = LDAP_KEY_FAX ; } if ( ldap_user_attr_zip == null ) { ldap_user_attr_zip = LDAP_KEY_ZIP ; } if ( ldap_user_attr_country == null ) { ldap_user_attr_country = LDAP_KEY_COUNTRY ; } if ( ldap_user_attr_town == null ) { ldap_user_attr_town = LDAP_KEY_TOWN ; } if ( ldap_user_attr_phone == null ) { ldap_user_attr_phone = LDAP_KEY_PHONE ; } if ( ldap_user_attr_timezone == null ) { ldap_user_attr_timezone = LDAP_KEY_TIMEZONE ; } String ldap_auth_type = configData . get ( CONFIGKEY_LDAP_AUTH_TYPE ) ; if ( ldap_auth_type == null ) ldap_auth_type = "" ; if ( ! isValidAuthType ( ldap_auth_type ) ) { log . error ( "ConfigKey in Ldap Config contains invalid auth type : '" + ldap_auth_type + "' -> Defaulting to " + LdapAuthBase . LDAP_AUTH_TYPE_SIMPLE ) ; ldap_auth_type = LdapAuthBase . LDAP_AUTH_TYPE_SIMPLE ; } String ldap_search_filter = "(" + ldap_fieldname_user_principal + "=" + user + ")" ; log . debug ( "Searching userdata with LDAP Search Filter :" + ldap_search_filter ) ; ldap_search_scope = ldap_search_scope . replaceAll ( ":" , "=" ) ; ldap_admin_dn = ldap_admin_dn . replaceAll ( ":" , "=" ) ; LdapAuthBase lAuth = new LdapAuthBase ( ldap_url , ldap_admin_dn , ldap_passwd , ldap_auth_type ) ; log . debug ( "authenticating admin..." ) ; lAuth . authenticateUser ( ldap_admin_dn , ldap_passwd ) ; log . debug ( "Checking server type..." ) ; if ( ldap_server_type . equalsIgnoreCase ( "OpenLDAP" ) ) { String ldapUserDN = user ; log . debug ( "LDAP server is OpenLDAP" ) ; log . debug ( "LDAP search base: " + ldap_search_scope ) ; HashMap < String , String > uidCnDictionary = lAuth . getUidCnHashMap ( ldap_search_scope , ldap_search_filter , ldap_fieldname_user_principal ) ; if ( uidCnDictionary . get ( user ) != null ) { ldapUserDN = uidCnDictionary . get ( user ) + "," + ldap_search_scope ; log . debug ( "Authentication with DN: " + ldapUserDN ) ; } try { if ( ! lAuth . authenticateUser ( ldapUserDN , passwd ) ) { log . error ( ldapUserDN + " not authenticated." ) ; return new Long ( - 11 ) ; } } catch ( Exception e ) { log . error ( "Error on LdapAuth : " + e . getMessage ( ) ) ; return null ; } } else { try { if ( ! lAuth . authenticateUser ( user , passwd ) ) return new Long ( - 11 ) ; } catch ( Exception e ) { log . error ( "Error on LdapAuth : " + e . getMessage ( ) ) ; return null ; } } Users u = null ; try { u = userManagement . getUserByLogin ( user ) ; } catch ( Exception e ) { log . error ( "Error retrieving Userdata : " + e . getMessage ( ) ) ; } if ( u == null ) { log . debug ( "user doesnt exist local -> create new" ) ; List < String > attributes = new ArrayList < String > ( ) ; attributes . add ( ldap_user_attr_lastname ) ; attributes . add ( ldap_user_attr_firstname ) ; attributes . add ( ldap_user_attr_mail ) ; attributes . add ( ldap_user_attr_street ) ; attributes . add ( ldap_user_attr_additional_name ) ; attributes . add ( ldap_user_attr_fax ) ; attributes . add ( ldap_user_attr_zip ) ; attributes . add ( ldap_user_attr_country ) ; attributes . add ( ldap_user_attr_town ) ; attributes . add ( ldap_user_attr_phone ) ; attributes . add ( ldap_user_attr_timezone ) ; HashMap < String , String > ldapAttrs = new HashMap < String , String > ( ) ; ldapAttrs . put ( "lastnameAttr" , ldap_user_attr_lastname ) ; ldapAttrs . put ( "firstnameAttr" , ldap_user_attr_firstname ) ; ldapAttrs . put ( "mailAttr" , ldap_user_attr_mail ) ; ldapAttrs . put ( "streetAttr" , ldap_user_attr_street ) ; ldapAttrs . put ( "additionalNameAttr" , ldap_user_attr_additional_name ) ; ldapAttrs . put ( "faxAttr" , ldap_user_attr_fax ) ; ldapAttrs . put ( "zipAttr" , ldap_user_attr_zip ) ; ldapAttrs . put ( "countryAttr" , ldap_user_attr_country ) ; ldapAttrs . put ( "townAttr" , ldap_user_attr_town ) ; ldapAttrs . put ( "phoneAttr" , ldap_user_attr_phone ) ; ldapAttrs . put ( "phoneAttr" , ldap_user_attr_phone ) ; ldapAttrs . put ( "timezoneAttr" , ldap_user_attr_timezone ) ; Vector < HashMap < String , String > > result = lAuth . getData ( ldap_search_scope , ldap_search_filter , attributes ) ; if ( result == null || result . size ( ) < 1 ) { log . error ( "Error on Ldap request - no result for user " + user ) ; return new Long ( - 10 ) ; } if ( result . size ( ) > 1 ) { log . error ( "Error on Ldap request - more than one result for user " + user ) ; return null ; } HashMap < String , String > userData = result . get ( 0 ) ; try { Long userid ; if ( ldap_sync_passwd_to_om != null && ldap_sync_passwd_to_om . equals ( "no" ) ) { Random r = new Random ( ) ; String token = Long . toString ( Math . abs ( r . nextLong ( ) ) , 36 ) ; log . debug ( "Synching Ldap user to OM DB with RANDOM password: " + token ) ; userid = createUserFromLdapData ( userData , token , user , ldapAttrs ) ; } else { log . debug ( "Synching Ldap user to OM DB with password" ) ; userid = createUserFromLdapData ( userData , passwd , user , ldapAttrs ) ; } log . debug ( "New User ID : " + userid ) ; if ( currentClient != null ) { currentClient . setUser_id ( userid ) ; } Boolean bool = sessionManagement . updateUser ( SID , userid ) ; if ( bool == null ) { log . error ( "Error on Updating Session" ) ; return new Long ( - 1 ) ; } else if ( ! bool ) { log . error ( "Invalid Session Object" ) ; return new Long ( - 35 ) ; } Users u2 = userManagement . getUserById ( userid ) ; if ( u2 == null ) return new Long ( - 1 ) ; u2 . setExternalUserType ( EXTERNAL_USER_TYPE_LDAP ) ; userManagement . refreshUserObject ( u2 ) ; log . debug ( "getUserbyId : " + userid + " : " + u2 . getLogin ( ) ) ; return u2 ; } catch ( Exception e ) { log . error ( "Error on Working Userdata : " , e ) ; return new Long ( - 1 ) ; } } else { log . debug ( "User already exists -> Update of current passwd" ) ; if ( currentClient != null ) { currentClient . setUser_id ( u . getUser_id ( ) ) ; } Boolean bool = sessionManagement . updateUser ( SID , u . getUser_id ( ) ) ; if ( bool == null ) { log . error ( "Error on Updating Session" ) ; return new Long ( - 1 ) ; } else if ( ! bool ) { log . error ( "Invalid Session Object" ) ; return new Long ( - 35 ) ; } if ( ldap_sync_passwd_to_om == null || ! ldap_sync_passwd_to_om . equals ( "no" ) ) { u . setPassword ( passwd ) ; } try { userManagement . updateUserObject ( u , true ) ; } catch ( Exception e ) { log . error ( "Error updating user : " + e . getMessage ( ) ) ; return new Long ( - 1 ) ; } return u ; } }
ldap login connection data is retrieved from configurationfile
