From 4d66047a3136662532e79d5e41038db246d334dd Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Wed, 30 May 2012 13:21:40 -0500 Subject: Basic user editing, and full group editing, support now in place --- src/libtdeldap.cpp | 226 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 185 insertions(+), 41 deletions(-) (limited to 'src/libtdeldap.cpp') diff --git a/src/libtdeldap.cpp b/src/libtdeldap.cpp index b9ffdf4..2f834e9 100644 --- a/src/libtdeldap.cpp +++ b/src/libtdeldap.cpp @@ -384,7 +384,7 @@ printf("[RAJA DEBUG 100.3] %s: %s\n\r", attr, vals[i]->bv_val); // FIXME // This attribute is not present in my current LDAP schema // userinfo.uniqueIdentifier = vals[i]->bv_val; - else if (ldap_field == "preferredLanguage") { + else if (ldap_field == "businessCategory") { userinfo.businessCategory = vals[i]->bv_val; } else if (ldap_field == "carLicense") { @@ -420,9 +420,7 @@ printf("[RAJA DEBUG 100.1] In LDAPManager::users() bind was OK\n\r"); fflush(std LDAPMessage* msg; TQString ldap_base_dn = m_basedc; TQString ldap_filter = "(objectClass=posixAccount)"; - struct timeval timeout; - timeout.tv_sec = 10; // 10 second timeout - retcode = ldap_search_ext_s(m_ldap, ldap_base_dn.ascii(), LDAP_SCOPE_SUBTREE, ldap_filter.ascii(), ldap_user_and_operational_attributes, 0, NULL, NULL, &timeout, 0, &msg); + retcode = ldap_search_ext_s(m_ldap, ldap_base_dn.ascii(), LDAP_SCOPE_SUBTREE, ldap_filter.ascii(), ldap_user_and_operational_attributes, 0, NULL, NULL, NULL, 0, &msg); if (retcode != LDAP_SUCCESS) { KMessageBox::error(0, i18n("LDAP search failure

Reason: [%3] %4").arg(retcode).arg(ldap_err2string(retcode)), i18n("LDAP Error")); return LDAPUserInfoList(); @@ -510,21 +508,25 @@ LDAPGroupInfo LDAPManager::getGroupByDistinguishedName(TQString dn) { } void create_single_attribute_operation(LDAPMod **mods, int *i, TQString attr, TQString value) { - char **values = (char**)malloc(2*sizeof(char*)); - values[0] = strdup(value.ascii()); - values[1] = NULL; - mods[*i]->mod_op = LDAP_MOD_ADD; - mods[*i]->mod_type = strdup(attr.ascii()); - mods[*i]->mod_values = values; - (*i)++; + if (value != "") { + char **values = (char**)malloc(2*sizeof(char*)); + values[0] = strdup(value.ascii()); + values[1] = NULL; + mods[*i]->mod_op = LDAP_MOD_ADD; + mods[*i]->mod_type = strdup(attr.ascii()); + mods[*i]->mod_values = values; + (*i)++; + } } void create_multiple_attributes_operation(LDAPMod **mods, int *i, TQString attr, TQStringList strings) { int j=0; char **values = (char**)malloc((strings.count()+1)*sizeof(char*)); for ( TQStringList::Iterator it = strings.begin(); it != strings.end(); ++it ) { - values[j] = strdup((*it).ascii()); - j++; + if ((*it) != "") { + values[j] = strdup((*it).ascii()); + j++; + } } values[j] = NULL; mods[*i]->mod_op = LDAP_MOD_ADD; @@ -534,34 +536,28 @@ void create_multiple_attributes_operation(LDAPMod **mods, int *i, TQString attr, } void add_single_attribute_operation(LDAPMod **mods, int *i, TQString attr, TQString value) { - mods[*i]->mod_op = LDAP_MOD_DELETE; - mods[*i]->mod_type = strdup(attr.ascii()); - mods[*i]->mod_values = NULL; - (*i)++; - - char **values = (char**)malloc(2*sizeof(char*)); - values[0] = strdup(value.ascii()); - values[1] = NULL; - mods[*i]->mod_op = LDAP_MOD_ADD; - mods[*i]->mod_type = strdup(attr.ascii()); - mods[*i]->mod_values = values; - (*i)++; + if (value != "") { + char **values = (char**)malloc(2*sizeof(char*)); + values[0] = strdup(value.ascii()); + values[1] = NULL; + mods[*i]->mod_op = LDAP_MOD_REPLACE; + mods[*i]->mod_type = strdup(attr.ascii()); + mods[*i]->mod_values = values; + (*i)++; + } } void add_multiple_attributes_operation(LDAPMod **mods, int *i, TQString attr, TQStringList strings) { - mods[*i]->mod_op = LDAP_MOD_DELETE; - mods[*i]->mod_type = strdup(attr.ascii()); - mods[*i]->mod_values = NULL; - (*i)++; - int j=0; char **values = (char**)malloc((strings.count()+1)*sizeof(char*)); for ( TQStringList::Iterator it = strings.begin(); it != strings.end(); ++it ) { - values[j] = strdup((*it).ascii()); - j++; + if ((*it) != "") { + values[j] = strdup((*it).ascii()); + j++; + } } values[j] = NULL; - mods[*i]->mod_op = LDAP_MOD_ADD; + mods[*i]->mod_op = LDAP_MOD_REPLACE; mods[*i]->mod_type = strdup(attr.ascii()); mods[*i]->mod_values = values; (*i)++; @@ -577,9 +573,8 @@ int LDAPManager::updateUserInfo(LDAPUserInfo user) { } else { // Assemble the LDAPMod structure - // We will replace attributes by first deleting them, then adding them back with their new values - int number_of_parameters = 43; // 43 primary attributes - number_of_parameters = (number_of_parameters * 2); // MODIFY/DELETE + // We will replace any existing attributes with the new values + int number_of_parameters = 40; // 40 primary attributes LDAPMod *mods[number_of_parameters+1]; for (i=0;imod_type != NULL) { free(mods[i]->mod_type); @@ -633,9 +683,8 @@ int LDAPManager::updateGroupInfo(LDAPGroupInfo group) { } else { // Assemble the LDAPMod structure - // We will replace attributes by first deleting them, then adding them back with their new values + // We will replace any existing attributes with the new values int number_of_parameters = 2; // 2 primary attributes - number_of_parameters = (number_of_parameters * 2); // MODIFY/DELETE LDAPMod *mods[number_of_parameters+1]; for (i=0;imod_type != NULL) { free(mods[i]->mod_type); @@ -683,6 +735,75 @@ int LDAPManager::updateGroupInfo(LDAPGroupInfo group) { } } +int LDAPManager::addUserInfo(LDAPUserInfo user) { + int retcode; + int i; + LDAPUserInfo userinfo; + + if (bind() < 0) { + return -1; + } + else { + // Create the base DN entry + int number_of_parameters = 13; // 13 primary attributes + LDAPMod *mods[number_of_parameters+1]; + for (i=0;imod_type = NULL; + mods[i]->mod_values = NULL; + } + mods[number_of_parameters] = NULL; + + // Load initial required LDAP object attributes + i=0; + create_single_attribute_operation(mods, &i, "uidNumber", TQString("%1").arg(user.uid)); + create_single_attribute_operation(mods, &i, "gidNumber", TQString("%1").arg(user.primary_gid)); + create_multiple_attributes_operation(mods, &i, "objectClass", TQStringList::split(" ", "inetOrgPerson krb5Realm krb5Principal krb5KDCEntry emsUser posixAccount")); + create_single_attribute_operation(mods, &i, "uid", user.name); + create_single_attribute_operation(mods, &i, "cn", user.commonName); + create_single_attribute_operation(mods, &i, "sn", user.surName); + create_single_attribute_operation(mods, &i, "homeDirectory", user.homedir); + // Kerberos + create_single_attribute_operation(mods, &i, "krb5KeyVersionNumber", "1"); + create_single_attribute_operation(mods, &i, "krb5PrincipalName", TQString(user.name.lower()) + "@" + m_realm.upper()); + create_single_attribute_operation(mods, &i, "krb5RealmName", m_realm.upper()); + // Zivios specific + create_single_attribute_operation(mods, &i, "emsdescription", "None"); + create_single_attribute_operation(mods, &i, "emsprimarygroupdn", "None"); + create_single_attribute_operation(mods, &i, "emstype", "UserEntry"); + LDAPMod *prevterm = mods[i]; + mods[i] = NULL; + + // Add new object + retcode = ldap_add_ext_s(m_ldap, user.distinguishedName.ascii(), mods, NULL, NULL); + + // Clean up + mods[i] = prevterm; + for (i=0;imod_type != NULL) { + free(mods[i]->mod_type); + } + if (mods[i]->mod_values != NULL) { + int j = 0; + while (mods[i]->mod_values[j] != NULL) { + free(mods[i]->mod_values[j]); + j++; + } + free(mods[i]->mod_values); + } + delete mods[i]; + } + + if (retcode != LDAP_SUCCESS) { + KMessageBox::error(0, i18n("LDAP addition failure

Reason: [%3] %4").arg(retcode).arg(ldap_err2string(retcode)), i18n("LDAP Error")); + return -2; + } + else { + return updateUserInfo(user); + } + } +} + int LDAPManager::addGroupInfo(LDAPGroupInfo group) { int retcode; int i; @@ -693,7 +814,7 @@ int LDAPManager::addGroupInfo(LDAPGroupInfo group) { } else { // Create the base DN entry - int number_of_parameters = 6; // 3 primary attributes + int number_of_parameters = 6; // 6 primary attributes LDAPMod *mods[number_of_parameters+1]; for (i=0;imod_type != NULL) { free(mods[i]->mod_type); @@ -743,6 +867,26 @@ int LDAPManager::addGroupInfo(LDAPGroupInfo group) { } } +int LDAPManager::deleteUserInfo(LDAPUserInfo user) { + int retcode; + LDAPUserInfo userinfo; + + if (bind() < 0) { + return -1; + } + else { + // Delete the base DN entry + retcode = ldap_delete_ext_s(m_ldap, user.distinguishedName.ascii(), NULL, NULL); + if (retcode != LDAP_SUCCESS) { + KMessageBox::error(0, i18n("LDAP deletion failure

Reason: [%3] %4").arg(retcode).arg(ldap_err2string(retcode)), i18n("LDAP Error")); + return -2; + } + else { + return 0; + } + } +} + int LDAPManager::deleteGroupInfo(LDAPGroupInfo group) { int retcode; LDAPGroupInfo groupinfo; -- cgit v1.2.1