2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 Michael Rasmussen and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Functions necessary to access LDAP servers.
26 * 1) When adding a contact to an empty addressbook from the pop-up menu
27 * when right-clicking on an email address causes claws-mail to crash in
28 * addritem.c line 965. Severity: Show stopper. Solved in 2.9.2cvs17
29 * 2) Updating a contact gets lost if the user makes a new search on the
30 * same LdapServer. Severity: Medium. Solved in 2.9.2cvs17 (patch added to solve 1) also solved this bug)
31 * 3) After adding a new contact the displayName for the contact is empty
32 * until the user makes a reread from the LdapServer. Severity: minor.
33 * Solved in 2.9.2cvs24
43 #include <glib/gi18n.h>
49 #include "ldapupdate.h"
52 #include "addrcache.h"
54 #include "ldapquery.h"
55 #include "ldapserver.h"
58 #include "adbookbase.h"
59 #include "editaddress_other_attributes_ldap.h"
62 * Structure to hold user defined attributes
65 typedef struct _AttrKeyValue AttrKeyValue;
66 struct _AttrKeyValue {
72 * Structure to hold contact information.
73 * Each addressbook will have 0..N contacts.
75 typedef struct _EmailKeyValue EmailKeyValue;
76 struct _EmailKeyValue {
83 * Structure to hold information about RDN.
85 typedef struct _Rdn Rdn;
93 * Retrieve address group item for update.
94 * \param group Group to print.
95 * \param array GHashTAble of item_group, or <i>NULL</i> if none created.
97 void ldapsvr_retrieve_item_group(ItemGroup *group, GHashTable *array) {
98 /* Not implemented in this release */
99 g_return_if_fail(group != NULL);
103 * Create an initial EmailKeyValue structure
104 * \return empty structure
106 EmailKeyValue *emailkeyvalue_create() {
109 buf = g_new0(EmailKeyValue, 1);
117 * Create an initial AttrKeyValue structure
118 * \return empty structure
120 AttrKeyValue *attrkeyvalue_create() {
123 buf = g_new0(AttrKeyValue, 1);
130 * Free created AttrKeyValue structure
131 * \param akv AttrKeyValue structure to free
133 void attrkeyvalue_free(AttrKeyValue *akv) {
147 * Retrieve E-Mail address object for update.
148 * \param item ItemEmail to update.
149 * \return object, or <i>NULL</i> if none created.
151 EmailKeyValue *ldapsvr_retrieve_item_email(ItemEMail *item) {
152 EmailKeyValue *newItem;
153 g_return_val_if_fail(item != NULL, NULL);
154 newItem = emailkeyvalue_create();
155 newItem->alias = g_strdup(ADDRITEM_NAME(item));
156 newItem->mail = g_strdup(item->address);
157 newItem->remarks = g_strdup(item->remarks);
162 * Retrieve user attribute object for update.
163 * \param item UserAttribute to update.
164 * \return object, or <i>NULL</i> if none created.
166 AttrKeyValue *ldapsvr_retrieve_attribute(UserAttribute *item) {
167 AttrKeyValue *newItem;
168 g_return_val_if_fail(item != NULL, NULL);
169 newItem = attrkeyvalue_create();
170 newItem->key = g_strdup(item->name);
171 newItem->value = g_strdup(item->value);
176 * Retrieve person object for update.
177 * \param person ItemPerson to update.
178 * \param array GHashTable with user input.
179 * \return false if update is not needed, or true if update is needed.
181 gboolean ldapsvr_retrieve_item_person(ItemPerson *person, GHashTable *array) {
184 g_return_val_if_fail(person != NULL, FALSE);
185 switch (person->status) {
186 case NONE: return FALSE;
187 case ADD_ENTRY: g_hash_table_insert(array, "status", "new"); break;
188 case UPDATE_ENTRY: g_hash_table_insert(array, "status", "update"); break;
189 case DELETE_ENTRY: g_hash_table_insert(array, "status", "delete"); break;
190 default: g_critical(_("ldapsvr_retrieve_item_person->Unknown status: %d"), person->status);
192 g_hash_table_insert(array, "uid", ADDRITEM_ID(person));
193 g_hash_table_insert(array, "cn", ADDRITEM_NAME(person));
194 g_hash_table_insert(array, "givenName", person->firstName);
195 g_hash_table_insert(array, "sn", person->lastName);
196 g_hash_table_insert(array, "nickName", person->nickName);
197 g_hash_table_insert(array, "dn", person->externalID);
198 node = person->listEMail;
201 EmailKeyValue *newEmail = ldapsvr_retrieve_item_email(node->data);
203 attr = g_list_append(attr, newEmail);
204 node = g_list_next(node);
206 g_hash_table_insert(array, "mail", attr);
207 node = person->listAttrib;
210 AttrKeyValue *newAttr = ldapsvr_retrieve_attribute(node->data);
212 attr = g_list_append(attr, newAttr);
213 node = g_list_next(node);
215 g_hash_table_insert(array, "attribute", attr);
220 * Print contents of contacts hashtable for debug.
221 * This function must be called with g_hash_table_foreach.
222 * \param key Key to process.
223 * \param data Data to process.
224 * \param fd Output stream.
226 void ldapsvr_print_contacts_hashtable(gpointer key, gpointer data, gpointer fd) {
227 gchar *keyName = (gchar *) key;
230 if (g_ascii_strcasecmp("mail", keyName) == 0) {
231 node = (GList *) data;
233 EmailKeyValue *item = node->data;
234 if (debug_get_mode()) {
235 debug_print("\t\talias = %s\n", item->alias?item->alias:"null");
236 debug_print("\t\tmail = %s\n", item->mail?item->mail:"null");
237 debug_print("\t\tremarks = %s\n", item->remarks?item->remarks:"null");
240 FILE *stream = (FILE *) fd;
241 fprintf(stream, "\t\talias = %s\n", item->alias?item->alias:"null");
242 fprintf(stream, "\t\tmail = %s\n", item->mail?item->mail:"null");
243 fprintf(stream, "\t\tremarks = %s\n", item->remarks?item->remarks:"null");
245 node = g_list_next(node);
248 else if (g_ascii_strcasecmp("attribute", keyName) == 0) {
249 node = (GList *) data;
251 AttrKeyValue *item = node->data;
252 if (debug_get_mode()) {
253 debug_print("\t\t%s = %s\n", item->key?item->key:"null",
254 item->value?item->value:"null");
257 FILE *stream = (FILE *) fd;
258 fprintf(stream, "\t\t%s = %s\n", item->key?item->key:"null",
259 item->value?item->value:"null");
261 node = g_list_next(node);
265 if (debug_get_mode())
266 debug_print("\t\t%s = %s\n", keyName?keyName:"null", data?(gchar *)data:"null");
268 FILE *stream = (FILE *) fd;
269 fprintf(stream, "\t\t%s = %s\n", keyName?keyName:"null", data?(gchar *)data:"null");
275 * Free list of changed contacts
277 * \param list List of GHashTable
279 void ldapsvr_free_hashtable(GList *list) {
282 g_hash_table_destroy(tmp->data);
284 tmp = g_list_next(tmp);
291 * Get person object from cache
293 * \param server Resource to LDAP
294 * \param uid PersonID in cache
295 * \return person object, or <i>NULL</i> if fail
297 ItemPerson *ldapsvr_get_contact(LdapServer *server, gchar *uid) {
299 g_return_val_if_fail(server != NULL || uid != NULL, NULL);
300 aio = addrcache_get_object(server->addressCache, uid);
302 if(aio->type == ITEMTYPE_PERSON) {
303 return (ItemPerson *) aio;
310 * Connect to LDAP server.
311 * \param ctl Control object to process.
312 * \return LDAP Resource to LDAP.
314 LDAP *ldapsvr_connect(LdapControl *ctl) {
320 g_return_val_if_fail(ctl != NULL, NULL);
322 uri = g_strdup_printf("ldap%s://%s:%d",
323 ctl->enableSSL?"s":"",
324 ctl->hostName, ctl->port);
325 ldap_initialize(&ld, uri);
331 debug_print("connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port);
335 version = LDAP_VERSION3;
336 rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
337 if (rc == LDAP_OPT_SUCCESS) {
338 ctl->version = LDAP_VERSION3;
341 if (ctl->version == LDAP_VERSION3) {
342 if (ctl->enableTLS && !ctl->enableSSL) {
343 rc = ldap_start_tls_s(ld, NULL, NULL);
345 if (rc != LDAP_SUCCESS) {
346 g_printerr("LDAP Error(tls): ldap_simple_bind_s: %s\n",
347 ldap_err2string(rc));
354 /* Bind to the server, if required */
356 if (* ctl->bindDN != '\0') {
357 rc = claws_ldap_simple_bind_s(ld, ctl->bindDN, ctl->bindPass);
358 if (rc != LDAP_SUCCESS) {
359 g_printerr("bindDN: %s, bindPass: %s\n", ctl->bindDN, ctl->bindPass);
360 g_printerr("LDAP Error(bind): ldap_simple_bind_s: %s\n",
361 ldap_err2string(rc));
370 * Disconnect to LDAP server.
371 * \param ld Resource to LDAP.
373 void ldapsvr_disconnect(LDAP *ld) {
375 g_return_if_fail(ld != NULL);
376 ldap_unbind_ext(ld, NULL, NULL);
380 * Create an initial Rdn structure
382 * \return empty structure
387 buf = g_new0(Rdn, 1);
388 buf->attribute = NULL;
395 * Free a created Rdn structure
396 * \param rdn Structure to free
398 void rdn_free(Rdn *rdn) {
399 if (rdn->attribute) {
400 g_free(rdn->attribute);
401 rdn->attribute = NULL;
416 * update Rdn structure
418 * \param rdn Rdn structure to update
419 * \param head Uniq part of dn
420 * \param tail Common part of dn
422 void update_rdn(Rdn *rdn, gchar *head, gchar *tail) {
423 rdn->value = g_strdup(head);
424 rdn->new_dn = g_strdup_printf("mail=%s%s", head, tail);
428 * Deside if dn needs to be changed
430 * \param hash GHashTable with user input.
431 * \param dn dn for current object
432 * \return Rdn structure
434 Rdn *ldapsvr_modify_dn(GHashTable *hash, gchar *dn) {
436 gchar *pos, *compare;
439 g_return_val_if_fail(hash != NULL || dn != NULL, NULL);
441 pos = g_strstr_len(dn, strlen(dn), "=");
442 compare = g_strndup(dn, pos - dn);
447 rest = g_strstr_len(pos, strlen(pos), ",");
448 val = g_strndup(pos, rest - pos);
455 rdn->value = g_strdup(val);
456 rdn->attribute = g_strdup(compare);
458 if (strcmp("mail", rdn->attribute) == 0) {
459 GList *list = g_hash_table_lookup(hash, rdn->attribute);
461 EmailKeyValue *item = list->data;
462 compare = g_strdup((gchar *) item->mail);
463 if (strcmp(compare, rdn->value) == 0) {
464 update_rdn(rdn, compare, rest);
468 list = g_list_next(list);
470 /* if compare and rdn->attribute are equal then last email removed/empty */
471 if (strcmp(compare, rdn->attribute) != 0) {
472 /* RDN changed. Find new */
473 update_rdn(rdn, compare, rest);
478 /* We cannot remove dn */
485 compare = g_hash_table_lookup(hash, rdn->attribute);
486 /* if compare and rdn->attribute are equal then dn removed/empty */
487 if (strcmp(compare, rdn->attribute) != 0) {
488 update_rdn(rdn, compare, rest);
492 /* We cannot remove dn */
502 * This macro is borrowed from the Balsa project
503 * Creates a LDAPMod structure
505 * \param mods Empty LDAPMod structure
506 * \param modarr Array with values to insert
507 * \param op Operation to perform on LDAP
508 * \param attr Attribute to insert
509 * \param strv Empty array which is NULL terminated
510 * \param val Value for attribute
512 #define SETMOD(mods,modarr,op,attr,strv,val) \
513 do { (mods) = &(modarr); (modarr).mod_type=attr; (modarr).mod_op=op;\
514 (strv)[0]=(val); (modarr).mod_values=strv; \
518 * Creates a LDAPMod structure
520 * \param mods Empty LDAPMod structure
521 * \param modarr Array with values to insert
522 * \param op Operation to perform on LDAP
523 * \param attr Attribute to insert
524 * \param strv Array with values to insert. Must be NULL terminated
526 #define SETMODS(mods,modarr,op,attr,strv) \
527 do { (mods) = &(modarr); (modarr).mod_type=attr; \
528 (modarr).mod_op=op; (modarr).mod_values=strv; \
533 * Clean up, close LDAP connection, and refresh cache
535 * \param ld Resource to LDAP
536 * \param server AddressBook resource
537 * \param contact GHashTable with current changed object
539 void clean_up(LDAP *ld, LdapServer *server, GHashTable *contact) {
541 ldapsvr_get_contact(server, g_hash_table_lookup(contact , "uid"));
544 person->status = NONE;
545 displayName = g_hash_table_lookup(contact, "displayName");
547 person->nickName = g_strdup(displayName);
549 if (server->retVal != LDAPRC_SUCCESS) {
552 addrcache_remove_person(server->addressCache, person);
554 g_critical(N_("ldapsvr_update_book: Could not clean cache\n"));
556 addritem_free_item_person(res);
560 ldapsvr_disconnect(ld);
564 * Get cn attribute from dn
566 * \param dn Distinguesh Name for current object
567 * \return AttrKeyValue, or <i>NULL</i> if none created
569 AttrKeyValue *get_cn(gchar *dn) {
575 g_return_val_if_fail(dn != NULL, NULL);
577 cn = attrkeyvalue_create();
578 start = g_strstr_len(dn, strlen(dn), "cn");
580 attrkeyvalue_free(cn);
583 end = g_strstr_len(start, strlen(start), ",");
584 item = g_strndup(start, end - start);
586 attrkeyvalue_free(cn);
589 key_value = g_strsplit(item, "=", 2);
590 cn->key = g_strdup(key_value[0]);
591 cn->value = g_strdup(key_value[1]);
592 g_strfreev(key_value);
598 * Get mail attribute from dn
600 * \param dn Distinguesh Name for current object
601 * \return AttrKeyValue, or <i>NULL</i> if none created
603 AttrKeyValue *get_mail(gchar *dn) {
609 g_return_val_if_fail(dn != NULL, NULL);
611 mail = attrkeyvalue_create();
612 start = g_strstr_len(dn, strlen(dn), "mail");
614 attrkeyvalue_free(mail);
617 end = g_strstr_len(start, strlen(start), ",");
618 item = g_strndup(start, end - start);
620 attrkeyvalue_free(mail);
623 key_value = g_strsplit(item, "=", 2);
624 mail->key = g_strdup(key_value[0]);
625 mail->value = g_strdup(key_value[1]);
626 g_strfreev(key_value);
632 * Get ou or o attribute from dn
634 * \param dn Distinguesh Name for current object
635 * \return AttrKeyValue, or <i>NULL</i> if none created
637 AttrKeyValue *get_ou(gchar *dn) {
644 g_return_val_if_fail(dn != NULL, NULL);
645 ou = attrkeyvalue_create();
646 start = g_strstr_len(dn, strlen(dn), ",o=");
648 start = g_strstr_len(dn, strlen(dn), ",ou=");
650 attrkeyvalue_free(ou);
654 end = g_strstr_len(start, strlen(start), ",");
655 item = g_strndup(start, end - start);
657 attrkeyvalue_free(ou);
660 key_value = g_strsplit(item, "=", 2);
661 ou->key = g_strdup(key_value[0]);
662 ou->value = g_strdup(key_value[1]);
663 g_strfreev(key_value);
669 * Print the contents of a LDAPMod structure for debuging purposes
671 * \param mods LDAPMod structure
673 void ldapsvr_print_ldapmod(LDAPMod *mods[]) {
677 g_return_if_fail(mods != NULL);
678 g_printerr( "Type\n");
679 for (i = 0; NULL != mods[i]; i++) {
680 LDAPMod *mod = (LDAPMod *) mods[i];
682 switch (mod->mod_op) {
683 case LDAP_MOD_ADD: mod_op = g_strdup("ADD"); break;
684 case LDAP_MOD_REPLACE: mod_op = g_strdup("MODIFY"); break;
685 case LDAP_MOD_DELETE: mod_op = g_strdup("DELETE"); break;
686 default: mod_op = g_strdup("UNKNOWN");
688 g_printerr( "Operation: %s\tType:%s\nValues:\n", mod_op, mod->mod_type);
689 vals = mod->mod_vals.modv_strvals;
691 g_printerr( "\t%s\n", *vals++);
697 * Make a compare for every new value we want to store in the
698 * directory with the current values. Great tool for debugging
699 * against invalid syntax in attributes
701 * \param ld AddressBook resource
702 * \param dn dn for the entry
703 * \param cnt Number of attributes to compare
704 * \param mods LDAPMod structure
706 void ldapsvr_compare_attr(LDAP *ld, gchar *dn, gint cnt, LDAPMod *mods[]) {
709 g_return_if_fail(ld != NULL || dn != NULL || cnt >= 0 || mods != NULL);
710 for (i = 0; i < cnt; i++) {
711 gchar *value = g_strdup(mods[i]->mod_vals.modv_strvals[0]);
712 if (!value || strcmp(value, "") == 0)
713 value = g_strdup("thisisonlyadummy");
714 rc = ldap_compare_s(ld, dn, mods[i]->mod_type, value);
715 g_printerr("ldap_compare for (%s:%s)\" failed[0x%x]: %s\n",
716 mods[i]->mod_type, value, rc, ldap_err2string(rc));
722 * compare attribute to LDAP in case of LDAP_INAPPROPRIATE_MATCHING
724 * \param ld AddressBook resource
725 * \param server Reference to server
726 * \param dn dn for the entry
727 * \param attr Attribute
728 * \param value New value
729 * \return int, return will be LDAP_MOD_ADD, LDAP_MOD_REPLACE, or LDAP_MOD_DELETE
731 int ldapsvr_compare_manual_attr(LDAP *ld, LdapServer *server, gchar *dn, char *attr, char *value) {
732 LDAPMessage *res, *e = NULL;
734 struct berval **vals;
742 g_return_val_if_fail(ld != NULL || server != NULL || attr != NULL, -1);
743 ctl = server->control;
747 filter = g_strdup_printf("(&(mail=%s)(%s=*))", mail->value, attr);
748 attrkeyvalue_free(mail);
750 rc = ldap_search_s(ld, ctl->baseDN, /*LDAP_SCOPE_SUBTREE*/LDAP_SCOPE_ONELEVEL, filter, NULL, 0, &res);
752 g_printerr("ldap_search for attr=%s\" failed[0x%x]: %s\n",attr, rc, ldap_err2string(rc));
756 e = ldap_first_entry(ld, res);
757 /* entry has this attribute */
759 attribute = ldap_first_attribute( ld, e, &ber );
762 if( ( vals = ldap_get_values_len( ld, e, attr ) ) != NULL ) {
763 for( i = 0; vals[i] != NULL; i++ ) {
764 debug_print("Compare: %s=%s\n", attr, vals[i]->bv_val);
765 /* attribute has same value */
766 if (strcmp(vals[i]->bv_val, value) == 0)
768 /* attribute has new value */
770 retVal = LDAP_MOD_REPLACE;
773 ldap_value_free_len(vals);
776 retVal = LDAP_MOD_DELETE;
781 ldap_memfree(attribute);
783 /* entry does not have this attribute */
785 /* Only add if this is a real attribute */
787 retVal = LDAP_MOD_ADD;
788 /* This is dummy value used to avoid ldap_compare error */
801 * Deside which kind of operation is required to handle
802 * updating the specified attribute
804 * \param ld AddressBook resource
805 * \param server Reference to server
806 * \param dn dn for the entry
807 * \param attr Attribute
808 * \param value New value
809 * \return int, return will be LDAP_MOD_ADD, LDAP_MOD_REPLACE, or LDAP_MOD_DELETE
811 int ldapsvr_deside_operation(LDAP *ld, LdapServer *server, char *dn, char *attr, char *value) {
813 gboolean dummy = FALSE;
815 g_return_val_if_fail(ld != NULL || server != NULL || dn != NULL || attr != NULL, -1);
818 /* value containing empty string cause invalid syntax. A bug in
819 * the LDAP library? Therefore we add a dummy value
821 if (strcmp(value,"") == 0) {
822 value = g_strdup("thisisonlyadummy");
825 rc = ldap_compare_s(ld, dn, attr, value);
826 debug_print("ldap_compare for (%s:%s)\" error_code[0x%x]: %s\n",
827 attr, value, rc, ldap_err2string(rc));
829 case LDAP_COMPARE_FALSE:
831 return LDAP_MOD_DELETE;
833 return LDAP_MOD_REPLACE;
834 case LDAP_COMPARE_TRUE: return -1;
835 case LDAP_NO_SUCH_ATTRIBUTE: return LDAP_MOD_ADD;
836 /* LDAP_INAPPROPRIATE_MATCHING needs extensive testing because I
837 * am not aware off the condition causing this return value!
839 case LDAP_INAPPROPRIATE_MATCHING:
842 return ldapsvr_compare_manual_attr(ld, server, dn, attr, value);
843 case LDAP_UNDEFINED_TYPE: return -2;
844 case LDAP_INVALID_SYNTAX: return -2;
850 * Check if attribute is part of the current search criteria
852 * \param list Array containing attributes in the current search criteria
853 * \param attr Attribute to check
854 * \result <i>TRUE</i> if attribute is found in the current search criteria
856 gboolean ldapsvr_check_search_attributes(char **list, char *attr) {
858 if (strcmp(*list++, attr) == 0)
865 * Deside which other attributes needs updating
867 * \param ld LDAP resource
868 * \param server AddressBook resource
869 * \param dn dn for the entry
870 * \param contact GHashTable with information for the current contact
872 void ldapsvr_handle_other_attributes(LDAP *ld, LdapServer *server, char *dn, GHashTable *contact) {
874 gboolean CHECKED_ATTRIBUTE[ATTRIBUTE_SIZE + 1];
875 LDAPMod *mods[ATTRIBUTE_SIZE + 1];
876 LDAPMod modarr[ATTRIBUTE_SIZE];
878 char *attr[ATTRIBUTE_SIZE + 1][2];
881 g_return_if_fail(server != NULL || dn != NULL || contact != NULL);
882 for (i = 0; i <= ATTRIBUTE_SIZE; i++) {
883 CHECKED_ATTRIBUTE[i] = FALSE;
884 attr[i][0] = attr[i][1] = NULL;
886 node = g_hash_table_lookup(contact , "attribute");
888 AttrKeyValue *item = node->data;
890 int index = get_attribute_index(item->key);
892 debug_print("Found other attribute: %s = %s\n",
893 item->key?item->key:"null", item->value?item->value:"null");
894 mod_op = ldapsvr_deside_operation(ld, server, dn, item->key, item->value);
895 /* Only consider attributes which we no how to handle.
896 * Set to TRUE in CHECKED_ATTRIBUTE array to indicate no further action
899 CHECKED_ATTRIBUTE[index] = TRUE;
900 node = g_list_next(node);
903 if (mod_op == LDAP_MOD_DELETE) {
904 /* Setting param to NULL instructs OpenLDAP to remove any
905 * value stored for this attribute and remove the attribute
906 * completely. Should multiple instances of an attribute be
907 * allowed in the future param is required to have the value
908 * store for the attribute which is going to be deleted
912 if (mod_op == LDAP_MOD_REPLACE && strcmp(item->value, "") == 0) {
913 /* Having an empty string is considered a syntax error in
914 * ldap. E.g attributes with empty strings are not allowed
915 * in which case we treate this as a request for deleting
918 mod_op = LDAP_MOD_DELETE;
921 if (mod_op == LDAP_MOD_ADD && strcmp(item->value, "") == 0) {
922 /* Adding an empty string is considered a syntax error in
923 * ldap. E.g attributes with empty strings are not allowed
924 * in which case we silently refuse to add this entry
928 SETMOD(mods[cnt], modarr[cnt], mod_op, g_strdup(item->key), attr[cnt], g_strdup(item->value));
930 CHECKED_ATTRIBUTE[index] = TRUE;
934 node = g_list_next(node);
936 char **attribs = ldapctl_full_attribute_array(server->control);
937 for (i = 0; i < ATTRIBUTE_SIZE; i++) {
938 /* Attributes which holds no information are to be removed */
939 if (CHECKED_ATTRIBUTE[i] == FALSE) {
940 /* Only consider those attributes which is currently part of the search criteria.
941 * If attributes are not part of the search criteria they would seem to hold
942 * no information since their values will not be populated in the GUI
944 if (ldapsvr_check_search_attributes(attribs, (char *) ATTRIBUTE[i])) {
945 mod_op = ldapsvr_deside_operation(ld, server, dn, (char *) ATTRIBUTE[i], "");
946 if (mod_op == LDAP_MOD_DELETE) {
947 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_DELETE, g_strdup((char *) ATTRIBUTE[i]), attr[cnt], NULL);
953 ldapctl_free_attribute_array(attribs);
955 if (debug_get_mode())
956 ldapsvr_print_ldapmod(mods);
957 server->retVal = LDAPRC_SUCCESS;
958 rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL);
961 case LDAP_ALREADY_EXISTS:
962 server->retVal = LDAPRC_ALREADY_EXIST;
965 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", dn, rc, ldap_err2string(rc));
967 server->retVal = LDAPRC_STRONG_AUTH;
969 server->retVal = LDAPRC_NAMING_VIOLATION;
973 char **attribs = ldapctl_full_attribute_array(server->control);
974 for (i = 0; i < ATTRIBUTE_SIZE; i++) {
975 if (ldapsvr_check_search_attributes(attribs, (char *) ATTRIBUTE[i])) {
976 if (CHECKED_ATTRIBUTE[i] == FALSE) {
977 AddrItemObject *aio = addrcache_get_object(server->addressCache, g_hash_table_lookup(contact , "uid"));
978 ItemPerson *person = (ItemPerson *) aio;
979 addritem_person_remove_attribute(person, (const gchar *) ATTRIBUTE[i]);
983 ldapctl_free_attribute_array(attribs);
988 * Add new contact to LDAP
990 * \param server AddressBook resource
991 * \param contact GHashTable with object to add
993 void ldapsvr_add_contact(LdapServer *server, GHashTable *contact) {
994 gchar *email = NULL, *param = NULL;
996 LDAPMod *mods[MODSIZE];
999 char *cn[] = {NULL, NULL};
1000 char *displayName[] = {NULL, NULL};
1001 char *givenName[] = {NULL, NULL};
1003 char *sn[] = {NULL, NULL};
1004 char *org[] = {NULL, NULL};
1005 char *obj[] = {/*"top",*/ "person", "organizationalPerson", "inetOrgPerson", NULL};
1008 AttrKeyValue *ou, *commonName;
1013 g_return_if_fail(server != NULL || contact != NULL);
1014 node = g_hash_table_lookup(contact , "mail");
1016 EmailKeyValue *newEmail = node->data;
1017 email = g_strdup(newEmail->mail);
1019 if (email == NULL) {
1020 server->retVal = LDAPRC_NODN;
1021 clean_up(ld, server, contact);
1024 base_dn = g_strdup_printf("mail=%s,%s",
1025 email, server->control->baseDN?server->control->baseDN:"null");
1028 ldapsvr_get_contact(server, g_hash_table_lookup(contact , "uid"));
1029 person->externalID = g_strdup(base_dn);
1030 debug_print("dn: %s\n", base_dn);
1031 ld = ldapsvr_connect(server->control);
1033 clean_up(ld, server, contact);
1034 debug_print("no ldap found\n");
1037 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "objectClass", obj);
1039 ou = get_ou(base_dn);
1041 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, g_strdup(ou->key), org, g_strdup(ou->value));
1043 attrkeyvalue_free(ou);
1046 commonName = get_cn(base_dn);
1047 if (commonName == NULL) {
1048 param = g_hash_table_lookup(contact , "cn");
1050 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "cn", cn, param);
1053 clean_up(ld, server, contact);
1054 debug_print("no CN found\n");
1059 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, g_strdup(commonName->key), cn, g_strdup(commonName->value));
1061 param = g_hash_table_lookup(contact , "cn");
1062 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "displayName", displayName, param);
1063 g_hash_table_insert(contact, "displayName", param);
1064 attrkeyvalue_free(commonName);
1067 param = g_hash_table_lookup(contact , "givenName");
1069 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "givenName", givenName, param);
1072 mailList = g_hash_table_lookup(contact , "mail");
1075 tmp = g_malloc(sizeof(*tmp) * (g_list_length(mailList)+1));
1078 EmailKeyValue *item = mailList->data;
1079 *tmp++ = g_strdup((gchar *) item->mail);
1080 mailList = g_list_next(mailList);
1083 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "mail", mail);
1086 param = g_hash_table_lookup(contact, "sn");
1088 param = g_strdup(N_("Some SN"));
1089 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "sn", sn, param);
1092 if (debug_get_mode()) {
1093 ldapsvr_print_ldapmod(mods);
1095 server->retVal = LDAPRC_SUCCESS;
1096 rc = ldap_add_ext_s(ld, base_dn, mods, NULL, NULL);
1099 case LDAP_ALREADY_EXISTS:
1100 server->retVal = LDAPRC_ALREADY_EXIST;
1103 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1104 base_dn, rc, ldap_err2string(rc));
1106 server->retVal = LDAPRC_STRONG_AUTH;
1108 server->retVal = LDAPRC_NAMING_VIOLATION;
1111 ldapsvr_handle_other_attributes(ld, server, base_dn, contact);
1113 clean_up(ld, server, contact);
1117 * Update contact to LDAP
1119 * \param server AddressBook resource
1120 * \param contact GHashTable with object to update
1122 void ldapsvr_update_contact(LdapServer *server, GHashTable *contact) {
1124 LDAPMod *mods[MODSIZE];
1128 Rdn *NoRemove = NULL;
1129 char *cn[] = {NULL, NULL};
1130 char *givenName[] = {NULL, NULL};
1132 char *sn[] = {NULL, NULL};
1136 g_return_if_fail(server != NULL || contact != NULL);
1137 ld = ldapsvr_connect(server->control);
1139 clean_up(ld, server, contact);
1142 dn = g_hash_table_lookup(contact, "dn");
1144 clean_up(ld, server, contact);
1147 NoRemove = ldapsvr_modify_dn(contact, dn);
1149 /* We are trying to change RDN */
1150 gchar *newRdn = g_strdup_printf("%s=%s", NoRemove->attribute, NoRemove->value);
1151 int rc = ldap_modrdn2_s(ld, dn, newRdn, 1);
1152 if(rc != LDAP_SUCCESS) {
1153 if (rc == LDAP_ALREADY_EXISTS) {
1154 /* We are messing with a contact with more than one listed email
1155 * address and the email we are changing is not the one used for dn
1157 /* It needs to be able to handle renaming errors to an already defined
1158 * dn. For now we just refuse the update. It will be caught later on as
1159 * a LDAPRC_NAMING_VIOLATION error.
1163 g_printerr("Current dn: %s\n", dn);
1164 g_printerr("new dn: %s\n", newRdn);
1165 g_printerr("LDAP Error(ldap_modrdn2_s) failed[0x%x]: %s\n", rc, ldap_err2string(rc));
1167 clean_up(ld, server, contact);
1173 dn = g_strdup(NoRemove->new_dn);
1177 server->retVal = LDAPRC_NODN;
1178 clean_up(ld, server, contact);
1181 param = g_hash_table_lookup(contact , "cn");
1182 mod_op = ldapsvr_deside_operation(ld, server, dn, "displayName", param);
1183 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("cn", NoRemove->attribute) != 0)) {
1184 if (mod_op == LDAP_MOD_DELETE) {
1185 /* Setting param to NULL instructs OpenLDAP to remove any
1186 * value stored for this attribute and remove the attribute
1187 * completely. Should multiple instances of an attribute be
1188 * allowed in the future param is required to have the value
1189 * store for the attribute which is going to be deleted
1193 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1194 /* Having an empty string is considered a syntax error in
1195 * ldap. E.g attributes with empty strings are not allowed
1196 * in which case we treate this as a request for deleting
1199 mod_op = LDAP_MOD_DELETE;
1202 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1203 /* Adding an empty string is considered a syntax error in
1204 * ldap. E.g attributes with empty strings are not allowed
1205 * in which case we silently refuse to add this entry
1209 SETMOD(mods[cnt], modarr[cnt], mod_op, "displayName", cn, param);
1211 g_hash_table_insert(contact, "displayName", param);
1214 param = g_hash_table_lookup(contact , "givenName");
1215 mod_op = ldapsvr_deside_operation(ld, server, dn, "givenName", param);
1216 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("givenName", NoRemove->attribute) != 0)) {
1217 if (mod_op == LDAP_MOD_DELETE) {
1218 /* Setting param to NULL instructs OpenLDAP to remove any
1219 * value stored for this attribute and remove the attribute
1220 * completely. Should multiple instances of an attribute be
1221 * allowed in the future param is required to have the value
1222 * store for the attribute which is going to be deleted
1226 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1227 /* Having an empty string is considered a syntax error in
1228 * ldap. E.g attributes with empty strings are not allowed
1229 * in which case we treate this as a request for deleting
1232 mod_op = LDAP_MOD_DELETE;
1235 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1236 /* Adding an empty string is considered a syntax error in
1237 * ldap. E.g attributes with empty strings are not allowed
1238 * in which case we silently refuse to add this entry
1242 SETMOD(mods[cnt], modarr[cnt], mod_op, "givenName", givenName, param);
1246 mailList = g_hash_table_lookup(contact , "mail");
1248 debug_print("# of mail: %d\n", g_list_length(mailList));
1249 if (!(strcmp("mail", NoRemove->attribute) == 0 && g_list_length(mailList) == 1)) {
1251 tmp = g_malloc(sizeof(*tmp) * (g_list_length(mailList)+1));
1254 EmailKeyValue *item = mailList->data;
1255 *tmp++ = g_strdup((gchar *) item->mail);
1256 mailList = g_list_next(mailList);
1260 * At least one email address is required
1261 * in which case it will always be a replace
1263 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_REPLACE, "mail", mail);
1269 * an error condition since at least one email adress
1270 * is required. Should never occur though.
1273 param = g_hash_table_lookup(contact , "sn");
1274 mod_op = ldapsvr_deside_operation(ld, server, dn, "sn", param);
1275 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("sn", NoRemove->attribute) != 0)) {
1276 if (mod_op == LDAP_MOD_DELETE) {
1277 /* Setting param to NULL instructs OpenLDAP to remove any
1278 * value stored for this attribute and remove the attribute
1279 * completely. Should multiple instances of an attribute be
1280 * allowed in the future param is required to have the value
1281 * store for the attribute which is going to be deleted
1285 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1286 /* Having an empty string is considered a syntax error in
1287 * ldap. E.g attributes with empty strings are not allowed
1288 * in which case we treate this as a request for deleting
1291 mod_op = LDAP_MOD_DELETE;
1294 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1295 /* Adding an empty string is considered a syntax error in
1296 * ldap. E.g attributes with empty strings are not allowed
1297 * in which case we silently refuse to add this entry
1301 SETMOD(mods[cnt], modarr[cnt], mod_op, "sn", sn, param);
1305 debug_print("newDN: %s\n", dn);
1308 server->retVal = LDAPRC_SUCCESS;
1312 rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL);
1314 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1315 dn, rc, ldap_err2string(rc));
1316 server->retVal = LDAPRC_NAMING_VIOLATION;
1321 ldapsvr_handle_other_attributes(ld, server, dn, contact);
1322 /* If we do not make changes persistent at this point then changes
1323 * will be lost if the user makes new search on the same server since
1324 * changes are only present in Claws' internal cache. This issue has to
1325 * be solved in addressbook.c since this involves access to structures
1326 * which are only accessible in addressbook.c */
1327 clean_up(ld, server, contact);
1331 * Delete contact from LDAP
1333 * \param server AddressBook resource
1334 * \param contact GHashTable with object to delete
1336 void ldapsvr_delete_contact(LdapServer *server, GHashTable *contact) {
1341 g_return_if_fail(server != NULL || contact != NULL);
1342 ld = ldapsvr_connect(server->control);
1344 clean_up(ld, server, contact);
1347 dn = g_hash_table_lookup(contact, "dn");
1349 clean_up(ld, server, contact);
1352 server->retVal = LDAPRC_SUCCESS;
1353 rc = ldap_delete_ext_s(ld, dn, NULL, NULL);
1355 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1356 dn, rc, ldap_err2string(rc));
1357 server->retVal = LDAPRC_NODN;
1359 clean_up(ld, server, contact);
1363 * Update any changes to the server.
1365 * \param server AddressBook resource.
1366 * \param person ItemPerson holding user input.
1368 void ldapsvr_update_book(LdapServer *server, ItemPerson *item) {
1370 GHashTable *contact = NULL;
1371 GList *contacts = NULL, *head = NULL;
1373 g_return_if_fail(server != NULL);
1374 debug_print("updating ldap addressbook\n");
1376 contact = g_hash_table_new(g_str_hash, g_str_equal);
1378 gboolean result = ldapsvr_retrieve_item_person(item, contact);
1379 debug_print("Found contact to update: %s\n", result? "Yes" : "No");
1381 if (debug_get_mode()) {
1382 addritem_print_item_person(item, stdout);
1384 contacts = g_list_append(contacts, contact);
1388 ItemFolder *folder = server->addressCache->rootFolder;
1389 node = folder->listFolder;
1392 AddrItemObject *aio = node->data;
1394 if (aio->type == ITEMTYPE_FOLDER) {
1395 ItemFolder *folder = (ItemFolder *) aio;
1396 GList *persons = folder->listPerson;
1398 AddrItemObject *aio = persons->data;
1400 if (aio->type == ITEMTYPE_PERSON) {
1401 ItemPerson *item = (ItemPerson *) aio;
1402 gboolean result = ldapsvr_retrieve_item_person(item, contact);
1403 debug_print("Found contact to update: %s\n", result? "Yes" : "No");
1405 if (debug_get_mode()) {
1406 gchar *uid = g_hash_table_lookup(contact, "uid");
1407 item = ldapsvr_get_contact(server, uid);
1408 addritem_print_item_person(item, stdout);
1410 contacts = g_list_append(contacts, contact);
1414 persons = g_list_next(persons);
1419 g_printerr("\t\tpid : ???\n");
1421 node = g_list_next(node);
1426 if (debug_get_mode()) {
1428 debug_print("Contacts which must be updated in LDAP:\n");
1430 debug_print("\tContact:\n");
1431 g_hash_table_foreach(contacts->data,
1432 ldapsvr_print_contacts_hashtable, stderr);
1433 contacts = g_list_next(contacts);
1436 if (contacts == NULL)
1440 contact = (GHashTable *) contacts->data;
1441 status = (gchar *) g_hash_table_lookup(contact, "status");
1443 status = g_strdup("NULL");
1444 if (g_ascii_strcasecmp(status, "new") == 0) {
1445 ldapsvr_add_contact(server, contact);
1447 else if (g_ascii_strcasecmp(status, "update") == 0) {
1448 ldapsvr_update_contact(server, contact);
1450 else if (g_ascii_strcasecmp(status, "delete") == 0) {
1451 ldapsvr_delete_contact(server, contact);
1454 g_critical(_("ldapsvr_update_book->Unknown status: %s\n"), status);
1455 contacts = g_list_next(contacts);
1457 ldapsvr_free_hashtable(head);
1460 #endif /* USE_LDAP */