2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2009 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 cm_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 cm_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 cm_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 cm_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 g_hash_table_insert(array, "person", person);
199 node = person->listEMail;
202 EmailKeyValue *newEmail = ldapsvr_retrieve_item_email(node->data);
204 attr = g_list_append(attr, newEmail);
205 node = g_list_next(node);
207 g_hash_table_insert(array, "mail", attr);
208 node = person->listAttrib;
211 AttrKeyValue *newAttr = ldapsvr_retrieve_attribute(node->data);
213 attr = g_list_append(attr, newAttr);
214 node = g_list_next(node);
216 g_hash_table_insert(array, "attribute", attr);
221 * Print contents of contacts hashtable for debug.
222 * This function must be called with g_hash_table_foreach.
223 * \param key Key to process.
224 * \param data Data to process.
225 * \param fd Output stream.
227 void ldapsvr_print_contacts_hashtable(gpointer key, gpointer data, gpointer fd) {
228 gchar *keyName = (gchar *) key;
231 if (g_ascii_strcasecmp("mail", keyName) == 0) {
232 node = (GList *) data;
234 EmailKeyValue *item = node->data;
235 if (debug_get_mode()) {
236 debug_print("\t\talias = %s\n", item->alias?item->alias:"null");
237 debug_print("\t\tmail = %s\n", item->mail?item->mail:"null");
238 debug_print("\t\tremarks = %s\n", item->remarks?item->remarks:"null");
241 FILE *stream = (FILE *) fd;
242 fprintf(stream, "\t\talias = %s\n", item->alias?item->alias:"null");
243 fprintf(stream, "\t\tmail = %s\n", item->mail?item->mail:"null");
244 fprintf(stream, "\t\tremarks = %s\n", item->remarks?item->remarks:"null");
246 node = g_list_next(node);
249 else if (g_ascii_strcasecmp("attribute", keyName) == 0) {
250 node = (GList *) data;
252 AttrKeyValue *item = node->data;
253 if (debug_get_mode()) {
254 debug_print("\t\t%s = %s\n", item->key?item->key:"null",
255 item->value?item->value:"null");
258 FILE *stream = (FILE *) fd;
259 fprintf(stream, "\t\t%s = %s\n", item->key?item->key:"null",
260 item->value?item->value:"null");
262 node = g_list_next(node);
266 if (debug_get_mode())
267 debug_print("\t\t%s = %s\n", keyName?keyName:"null", data?(gchar *)data:"null");
269 FILE *stream = (FILE *) fd;
270 fprintf(stream, "\t\t%s = %s\n", keyName?keyName:"null", data?(gchar *)data:"null");
276 * Free list of changed contacts
278 * \param list List of GHashTable
280 void ldapsvr_free_hashtable(GList *list) {
283 g_hash_table_destroy(tmp->data);
285 tmp = g_list_next(tmp);
292 * Get person object from cache
294 * \param server Resource to LDAP
295 * \param uid PersonID in cache
296 * \return person object, or <i>NULL</i> if fail
298 ItemPerson *ldapsvr_get_contact(LdapServer *server, gchar *uid) {
300 cm_return_val_if_fail(server != NULL || uid != NULL, NULL);
301 aio = addrcache_get_object(server->addressCache, uid);
303 if(aio->type == ITEMTYPE_PERSON) {
304 return (ItemPerson *) aio;
311 * Connect to LDAP server.
312 * \param ctl Control object to process.
313 * \return LDAP Resource to LDAP.
315 LDAP *ldapsvr_connect(LdapControl *ctl) {
322 cm_return_val_if_fail(ctl != NULL, NULL);
324 ldapsrv_set_options (ctl->timeOut, NULL);
325 uri = g_strdup_printf("ldap%s://%s:%d",
326 ctl->enableSSL?"s":"",
327 ctl->hostName, ctl->port);
328 ldap_initialize(&ld, uri);
335 debug_print("connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port);
339 version = LDAP_VERSION3;
340 rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
341 if (rc == LDAP_OPT_SUCCESS) {
342 ctl->version = LDAP_VERSION3;
345 if (ctl->version == LDAP_VERSION3) {
346 if (ctl->enableTLS && !ctl->enableSSL) {
347 rc = ldap_start_tls_s(ld, NULL, NULL);
349 if (rc != LDAP_SUCCESS) {
350 g_printerr("LDAP Error(tls): ldap_simple_bind_s: %s\n",
351 ldap_err2string(rc));
358 /* Bind to the server, if required */
360 if (* ctl->bindDN != '\0') {
361 pwd = ldapctl_get_bind_password(ctl);
362 rc = claws_ldap_simple_bind_s(ld, ctl->bindDN, pwd);
363 if (rc != LDAP_SUCCESS) {
364 g_printerr("bindDN: %s, bindPass: %s\n", ctl->bindDN, pwd);
365 g_printerr("LDAP Error(bind): ldap_simple_bind_s: %s\n",
366 ldap_err2string(rc));
377 * Disconnect to LDAP server.
378 * \param ld Resource to LDAP.
380 void ldapsvr_disconnect(LDAP *ld) {
382 cm_return_if_fail(ld != NULL);
383 ldap_unbind_ext(ld, NULL, NULL);
387 * Create an initial Rdn structure
389 * \return empty structure
394 buf = g_new0(Rdn, 1);
395 buf->attribute = NULL;
402 * Free a created Rdn structure
403 * \param rdn Structure to free
405 void rdn_free(Rdn *rdn) {
406 if (rdn->attribute) {
407 g_free(rdn->attribute);
408 rdn->attribute = NULL;
423 * update Rdn structure
425 * \param rdn Rdn structure to update
426 * \param head Uniq part of dn
427 * \param tail Common part of dn
429 void update_rdn(Rdn *rdn, gchar *head, gchar *tail) {
430 rdn->value = g_strdup(head);
431 rdn->new_dn = g_strdup_printf("%s=%s%s", rdn->attribute, head, tail);
435 * Deside if dn needs to be changed
437 * \param hash GHashTable with user input.
438 * \param dn dn for current object
439 * \return Rdn structure
441 Rdn *ldapsvr_modify_dn(GHashTable *hash, gchar *dn) {
443 gchar *pos, *compare;
446 cm_return_val_if_fail(hash != NULL || dn != NULL, NULL);
448 pos = g_strstr_len(dn, strlen(dn), "=");
452 compare = g_strndup(dn, pos - dn);
455 rest = g_strstr_len(pos, strlen(pos), ",");
456 val = g_strndup(pos, rest - pos);
463 rdn->value = g_strdup(val);
464 rdn->attribute = g_strdup(compare);
466 if (strcmp("mail", rdn->attribute) == 0) {
467 GList *list = g_hash_table_lookup(hash, rdn->attribute);
469 EmailKeyValue *item = list->data;
470 compare = g_strdup((gchar *) item->mail);
471 if (strcmp(compare, rdn->value) == 0) {
472 update_rdn(rdn, compare, rest);
476 list = g_list_next(list);
478 /* if compare and rdn->attribute are equal then last email removed/empty */
479 if (strcmp(compare, rdn->attribute) != 0) {
480 /* RDN changed. Find new */
481 update_rdn(rdn, compare, rest);
486 /* We cannot remove dn */
493 compare = g_hash_table_lookup(hash, rdn->attribute);
494 /* if compare and rdn->attribute are equal then dn removed/empty */
495 if (strcmp(compare, rdn->attribute) != 0) {
496 update_rdn(rdn, compare, rest);
500 /* We cannot remove dn */
510 * This macro is borrowed from the Balsa project
511 * Creates a LDAPMod structure
513 * \param mods Empty LDAPMod structure
514 * \param modarr Array with values to insert
515 * \param op Operation to perform on LDAP
516 * \param attr Attribute to insert
517 * \param strv Empty array which is NULL terminated
518 * \param val Value for attribute
520 #define SETMOD(mods,modarr,op,attr,strv,val) \
521 do { (mods) = &(modarr); (modarr).mod_type=attr; (modarr).mod_op=op;\
522 (strv)[0]=(val); (modarr).mod_values=strv; \
526 * Creates a LDAPMod structure
528 * \param mods Empty LDAPMod structure
529 * \param modarr Array with values to insert
530 * \param op Operation to perform on LDAP
531 * \param attr Attribute to insert
532 * \param strv Array with values to insert. Must be NULL terminated
534 #define SETMODS(mods,modarr,op,attr,strv) \
535 do { (mods) = &(modarr); (modarr).mod_type=attr; \
536 (modarr).mod_op=op; (modarr).mod_values=strv; \
541 * Clean up, close LDAP connection, and refresh cache
543 * \param ld Resource to LDAP
544 * \param server AddressBook resource
545 * \param contact GHashTable with current changed object
547 void clean_up(LDAP *ld, LdapServer *server, GHashTable *contact) {
549 ldapsvr_get_contact(server, g_hash_table_lookup(contact , "uid"));
552 person->status = NONE;
553 displayName = g_hash_table_lookup(contact, "displayName");
555 person->nickName = g_strdup(displayName);
557 if (server->retVal != LDAPRC_SUCCESS) {
560 addrcache_remove_person(server->addressCache, person);
562 g_critical(N_("ldapsvr_update_book: Could not clean cache\n"));
564 addritem_free_item_person(res);
568 ldapsvr_disconnect(ld);
572 * Get cn attribute from dn
574 * \param dn Distinguesh Name for current object
575 * \return AttrKeyValue, or <i>NULL</i> if none created
577 AttrKeyValue *get_cn(gchar *dn) {
583 cm_return_val_if_fail(dn != NULL, NULL);
585 cn = attrkeyvalue_create();
586 start = g_strstr_len(dn, strlen(dn), "cn");
588 attrkeyvalue_free(cn);
591 end = g_strstr_len(start, strlen(start), ",");
592 item = g_strndup(start, end - start);
594 attrkeyvalue_free(cn);
597 key_value = g_strsplit(item, "=", 2);
598 cn->key = g_strdup(key_value[0]);
599 cn->value = g_strdup(key_value[1]);
600 g_strfreev(key_value);
606 * Get mail attribute from dn
608 * \param dn Distinguesh Name for current object
609 * \return AttrKeyValue, or <i>NULL</i> if none created
611 AttrKeyValue *get_mail(gchar *dn) {
617 cm_return_val_if_fail(dn != NULL, NULL);
619 mail = attrkeyvalue_create();
620 start = g_strstr_len(dn, strlen(dn), "mail");
622 attrkeyvalue_free(mail);
625 end = g_strstr_len(start, strlen(start), ",");
626 item = g_strndup(start, end - start);
628 attrkeyvalue_free(mail);
631 key_value = g_strsplit(item, "=", 2);
632 mail->key = g_strdup(key_value[0]);
633 mail->value = g_strdup(key_value[1]);
634 g_strfreev(key_value);
640 * Get ou or o attribute from dn
642 * \param dn Distinguesh Name for current object
643 * \return AttrKeyValue, or <i>NULL</i> if none created
645 AttrKeyValue *get_ou(gchar *dn) {
652 cm_return_val_if_fail(dn != NULL, NULL);
653 ou = attrkeyvalue_create();
654 start = g_strstr_len(dn, strlen(dn), ",o=");
656 start = g_strstr_len(dn, strlen(dn), ",ou=");
658 attrkeyvalue_free(ou);
662 end = g_strstr_len(start, strlen(start), ",");
663 item = g_strndup(start, end - start);
665 attrkeyvalue_free(ou);
668 key_value = g_strsplit(item, "=", 2);
669 ou->key = g_strdup(key_value[0]);
670 ou->value = g_strdup(key_value[1]);
671 g_strfreev(key_value);
677 * Print the contents of a LDAPMod structure for debuging purposes
679 * \param mods LDAPMod structure
681 void ldapsvr_print_ldapmod(LDAPMod *mods[]) {
685 cm_return_if_fail(mods != NULL);
686 g_printerr( "Type\n");
687 for (i = 0; NULL != mods[i]; i++) {
688 LDAPMod *mod = (LDAPMod *) mods[i];
690 switch (mod->mod_op) {
691 case LDAP_MOD_ADD: mod_op = g_strdup("ADD"); break;
692 case LDAP_MOD_REPLACE: mod_op = g_strdup("MODIFY"); break;
693 case LDAP_MOD_DELETE: mod_op = g_strdup("DELETE"); break;
694 default: mod_op = g_strdup("UNKNOWN");
696 g_printerr( "Operation: %s\tType:%s\nValues:\n", mod_op, mod->mod_type);
697 vals = mod->mod_vals.modv_strvals;
699 g_printerr( "\t%s\n", *vals++);
705 * Make a compare for every new value we want to store in the
706 * directory with the current values. Great tool for debugging
707 * against invalid syntax in attributes
709 * \param ld AddressBook resource
710 * \param dn dn for the entry
711 * \param cnt Number of attributes to compare
712 * \param mods LDAPMod structure
714 void ldapsvr_compare_attr(LDAP *ld, gchar *dn, gint cnt, LDAPMod *mods[]) {
717 #ifdef OPEN_LDAP_API_AT_LEAST_3000
723 cm_return_if_fail(ld != NULL || dn != NULL || cnt >= 0 || mods != NULL);
724 for (i = 0; i < cnt; i++) {
725 gchar *value = g_strdup(mods[i]->mod_vals.modv_strvals[0]);
726 if (!value || strcmp(value, "") == 0)
727 value = g_strdup("thisisonlyadummy");
729 #ifdef OPEN_LDAP_API_AT_LEAST_3000
732 val.bv_len = strlen(value);
734 rc = ldap_compare_ext_s(ld, dn, mods[i]->mod_type, &val, NULL, NULL);
738 /* This is deprecated as of OpenLDAP-2.3.0 */
739 rc = ldap_compare_s(ld, dn, mods[i]->mod_type, value);
743 g_printerr("ldap_compare for (%s:%s)\" failed[0x%x]: %s\n",
744 mods[i]->mod_type, value, rc, ldap_err2string(rc));
750 * compare attribute to LDAP in case of LDAP_INAPPROPRIATE_MATCHING
752 * \param ld AddressBook resource
753 * \param server Reference to server
754 * \param dn dn for the entry
755 * \param attr Attribute
756 * \param value New value
757 * \return int, return will be LDAP_MOD_ADD, LDAP_MOD_REPLACE, or LDAP_MOD_DELETE
759 int ldapsvr_compare_manual_attr(LDAP *ld, LdapServer *server, gchar *dn, char *attr, char *value) {
760 LDAPMessage *res, *e = NULL;
762 struct berval **vals;
770 cm_return_val_if_fail(ld != NULL || server != NULL || attr != NULL, -1);
771 ctl = server->control;
775 filter = g_strdup_printf("(&(mail=%s)(%s=*))", mail->value, attr);
776 attrkeyvalue_free(mail);
779 #ifdef OPEN_LDAP_API_AT_LEAST_3000
781 rc = ldap_search_ext_s(ld, ctl->baseDN, LDAP_SCOPE_ONELEVEL, filter, NULL, 0, NULL, NULL, NULL, 0, &res);
785 /* This is deprecated as of OpenLDAP-2.3.0 */
786 rc = ldap_search_s(ld, ctl->baseDN, LDAP_SCOPE_ONELEVEL, filter, NULL, 0, &res);
791 g_printerr("ldap_search for attr=%s\" failed[0x%x]: %s\n",attr, rc, ldap_err2string(rc));
795 e = ldap_first_entry(ld, res);
796 /* entry has this attribute */
798 attribute = ldap_first_attribute( ld, e, &ber );
801 if( ( vals = ldap_get_values_len( ld, e, attr ) ) != NULL ) {
802 for( i = 0; vals[i] != NULL; i++ ) {
803 debug_print("Compare: %s=%s\n", attr, vals[i]->bv_val);
804 /* attribute has same value */
805 if (strcmp(vals[i]->bv_val, value) == 0)
807 /* attribute has new value */
809 retVal = LDAP_MOD_REPLACE;
812 ldap_value_free_len(vals);
815 retVal = LDAP_MOD_DELETE;
820 ldap_memfree(attribute);
822 /* entry does not have this attribute */
824 /* Only add if this is a real attribute */
826 retVal = LDAP_MOD_ADD;
827 /* This is dummy value used to avoid ldap_compare error */
840 * Deside which kind of operation is required to handle
841 * updating the specified attribute
843 * \param ld AddressBook resource
844 * \param server Reference to server
845 * \param dn dn for the entry
846 * \param attr Attribute
847 * \param value New value
848 * \return int, return will be LDAP_MOD_ADD, LDAP_MOD_REPLACE, or LDAP_MOD_DELETE
850 int ldapsvr_deside_operation(LDAP *ld, LdapServer *server, char *dn, char *attr, char *value) {
852 gboolean dummy = FALSE;
854 #ifdef OPEN_LDAP_API_AT_LEAST_3000
860 cm_return_val_if_fail(ld != NULL || server != NULL || dn != NULL || attr != NULL, -1);
863 /* value containing empty string cause invalid syntax. A bug in
864 * the LDAP library? Therefore we add a dummy value
866 if (strcmp(value,"") == 0) {
867 value = g_strdup("thisisonlyadummy");
871 #ifdef OPEN_LDAP_API_AT_LEAST_3000
874 val.bv_len = strlen(value);
876 rc = ldap_compare_ext_s(ld, dn, attr, &val, NULL, NULL);
880 /* This is deprecated as of OpenLDAP-2.3.0 */
881 rc = ldap_compare_s(ld, dn, attr, value);
885 debug_print("ldap_compare for (%s:%s)\" error_code[0x%x]: %s\n",
886 attr, value, rc, ldap_err2string(rc));
888 case LDAP_COMPARE_FALSE:
890 return LDAP_MOD_DELETE;
892 return LDAP_MOD_REPLACE;
893 case LDAP_COMPARE_TRUE: return -1;
894 case LDAP_NO_SUCH_ATTRIBUTE: return LDAP_MOD_ADD;
895 /* LDAP_INAPPROPRIATE_MATCHING needs extensive testing because I
896 * am not aware off the condition causing this return value!
898 case LDAP_INAPPROPRIATE_MATCHING:
901 return ldapsvr_compare_manual_attr(ld, server, dn, attr, value);
902 case LDAP_UNDEFINED_TYPE: return -2;
903 case LDAP_INVALID_SYNTAX: return -2;
909 * Check if attribute is part of the current search criteria
911 * \param list Array containing attributes in the current search criteria
912 * \param attr Attribute to check
913 * \result <i>TRUE</i> if attribute is found in the current search criteria
915 gboolean ldapsvr_check_search_attributes(char **list, char *attr) {
917 if (strcmp(*list++, attr) == 0)
924 * Deside which other attributes needs updating
926 * \param ld LDAP resource
927 * \param server AddressBook resource
928 * \param dn dn for the entry
929 * \param contact GHashTable with information for the current contact
931 void ldapsvr_handle_other_attributes(LDAP *ld, LdapServer *server, char *dn, GHashTable *contact) {
933 gboolean CHECKED_ATTRIBUTE[ATTRIBUTE_SIZE + 1];
934 LDAPMod *mods[ATTRIBUTE_SIZE + 1];
935 LDAPMod modarr[ATTRIBUTE_SIZE];
937 char *attr[ATTRIBUTE_SIZE + 1][2];
940 cm_return_if_fail(server != NULL || dn != NULL || contact != NULL);
941 for (i = 0; i <= ATTRIBUTE_SIZE; i++) {
942 CHECKED_ATTRIBUTE[i] = FALSE;
943 attr[i][0] = attr[i][1] = NULL;
945 node = g_hash_table_lookup(contact , "attribute");
947 AttrKeyValue *item = node->data;
949 int index = get_attribute_index(item->key);
951 debug_print("Found other attribute: %s = %s\n",
952 item->key?item->key:"null", item->value?item->value:"null");
953 mod_op = ldapsvr_deside_operation(ld, server, dn, item->key, item->value);
954 /* Only consider attributes which we no how to handle.
955 * Set to TRUE in CHECKED_ATTRIBUTE array to indicate no further action
958 CHECKED_ATTRIBUTE[index] = TRUE;
959 node = g_list_next(node);
962 if (mod_op == LDAP_MOD_DELETE) {
963 /* Setting param to NULL instructs OpenLDAP to remove any
964 * value stored for this attribute and remove the attribute
965 * completely. Should multiple instances of an attribute be
966 * allowed in the future param is required to have the value
967 * store for the attribute which is going to be deleted
971 if (mod_op == LDAP_MOD_REPLACE && strcmp(item->value, "") == 0) {
972 /* Having an empty string is considered a syntax error in
973 * ldap. E.g attributes with empty strings are not allowed
974 * in which case we treate this as a request for deleting
977 mod_op = LDAP_MOD_DELETE;
980 if (mod_op == LDAP_MOD_ADD && strcmp(item->value, "") == 0) {
981 /* Adding an empty string is considered a syntax error in
982 * ldap. E.g attributes with empty strings are not allowed
983 * in which case we silently refuse to add this entry
987 SETMOD(mods[cnt], modarr[cnt], mod_op, g_strdup(item->key), attr[cnt], g_strdup(item->value));
989 CHECKED_ATTRIBUTE[index] = TRUE;
993 node = g_list_next(node);
995 char **attribs = ldapctl_full_attribute_array(server->control);
996 for (i = 0; i < ATTRIBUTE_SIZE; i++) {
997 /* Attributes which holds no information are to be removed */
998 if (CHECKED_ATTRIBUTE[i] == FALSE) {
999 /* Only consider those attributes which is currently part of the search criteria.
1000 * If attributes are not part of the search criteria they would seem to hold
1001 * no information since their values will not be populated in the GUI
1003 if (!strcmp(ATTRIBUTE[i], "jpegPhoto")) {
1004 debug_print("not updating jpegPhoto\n");
1007 if (ldapsvr_check_search_attributes(attribs, (char *) ATTRIBUTE[i])) {
1008 mod_op = ldapsvr_deside_operation(ld, server, dn, (char *) ATTRIBUTE[i], "");
1009 if (mod_op == LDAP_MOD_DELETE) {
1010 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_DELETE, g_strdup((char *) ATTRIBUTE[i]), attr[cnt], NULL);
1016 ldapctl_free_attribute_array(attribs);
1018 if (debug_get_mode())
1019 ldapsvr_print_ldapmod(mods);
1020 server->retVal = LDAPRC_SUCCESS;
1021 rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL);
1024 case LDAP_ALREADY_EXISTS:
1025 server->retVal = LDAPRC_ALREADY_EXIST;
1028 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", dn, rc, ldap_err2string(rc));
1030 server->retVal = LDAPRC_STRONG_AUTH;
1032 server->retVal = LDAPRC_NAMING_VIOLATION;
1036 char **attribs = ldapctl_full_attribute_array(server->control);
1037 for (i = 0; i < ATTRIBUTE_SIZE; i++) {
1038 if (!strcmp(ATTRIBUTE[i], "jpegPhoto")) {
1039 debug_print("not updating jpegPhoto\n");
1042 if (ldapsvr_check_search_attributes(attribs, (char *) ATTRIBUTE[i])) {
1043 if (CHECKED_ATTRIBUTE[i] == FALSE) {
1044 AddrItemObject *aio = addrcache_get_object(server->addressCache, g_hash_table_lookup(contact , "uid"));
1045 ItemPerson *person = (ItemPerson *) aio;
1046 addritem_person_remove_attribute(person, (const gchar *) ATTRIBUTE[i]);
1050 ldapctl_free_attribute_array(attribs);
1055 * Add new contact to LDAP
1057 * \param server AddressBook resource
1058 * \param contact GHashTable with object to add
1060 void ldapsvr_add_contact(LdapServer *server, GHashTable *contact) {
1061 gchar *email = NULL, *param = NULL;
1063 LDAPMod *mods[MODSIZE];
1066 char *cn[] = {NULL, NULL};
1067 char *displayName[] = {NULL, NULL};
1068 char *givenName[] = {NULL, NULL};
1070 char *sn[] = {NULL, NULL};
1071 char *org[] = {NULL, NULL};
1072 char *obj[] = {/*"top",*/ "person", "organizationalPerson", "inetOrgPerson", NULL};
1075 AttrKeyValue *ou, *commonName;
1080 cm_return_if_fail(server != NULL || contact != NULL);
1081 node = g_hash_table_lookup(contact , "mail");
1083 EmailKeyValue *newEmail = node->data;
1084 email = g_strdup(newEmail->mail);
1086 if (email == NULL) {
1087 server->retVal = LDAPRC_NODN;
1088 clean_up(ld, server, contact);
1091 base_dn = g_strdup_printf("mail=%s,%s",
1092 email, server->control->baseDN?server->control->baseDN:"null");
1095 ldapsvr_get_contact(server, g_hash_table_lookup(contact , "uid"));
1096 person->externalID = g_strdup(base_dn);
1097 debug_print("dn: %s\n", base_dn);
1098 ld = ldapsvr_connect(server->control);
1100 clean_up(ld, server, contact);
1101 debug_print("no ldap found\n");
1104 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "objectClass", obj);
1106 ou = get_ou(base_dn);
1108 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, g_strdup(ou->key), org, g_strdup(ou->value));
1110 attrkeyvalue_free(ou);
1113 commonName = get_cn(base_dn);
1114 if (commonName == NULL) {
1115 param = g_hash_table_lookup(contact , "cn");
1117 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "cn", cn, param);
1120 clean_up(ld, server, contact);
1121 debug_print("no CN found\n");
1126 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, g_strdup(commonName->key), cn, g_strdup(commonName->value));
1128 param = g_hash_table_lookup(contact , "cn");
1129 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "displayName", displayName, param);
1130 g_hash_table_insert(contact, "displayName", param);
1131 attrkeyvalue_free(commonName);
1134 param = g_hash_table_lookup(contact , "givenName");
1136 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "givenName", givenName, param);
1139 mailList = g_hash_table_lookup(contact , "mail");
1142 tmp = g_malloc(sizeof(*tmp) * (g_list_length(mailList)+1));
1145 EmailKeyValue *item = mailList->data;
1146 *tmp++ = g_strdup((gchar *) item->mail);
1147 mailList = g_list_next(mailList);
1150 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "mail", mail);
1153 param = g_hash_table_lookup(contact, "sn");
1155 param = g_strdup(N_("Some SN"));
1156 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "sn", sn, param);
1159 if (debug_get_mode()) {
1160 ldapsvr_print_ldapmod(mods);
1162 server->retVal = LDAPRC_SUCCESS;
1163 rc = ldap_add_ext_s(ld, base_dn, mods, NULL, NULL);
1166 case LDAP_ALREADY_EXISTS:
1167 server->retVal = LDAPRC_ALREADY_EXIST;
1170 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1171 base_dn, rc, ldap_err2string(rc));
1173 server->retVal = LDAPRC_STRONG_AUTH;
1175 server->retVal = LDAPRC_NAMING_VIOLATION;
1178 ldapsvr_handle_other_attributes(ld, server, base_dn, contact);
1180 clean_up(ld, server, contact);
1184 * Update contact to LDAP
1186 * \param server AddressBook resource
1187 * \param contact GHashTable with object to update
1189 void ldapsvr_update_contact(LdapServer *server, GHashTable *contact) {
1191 LDAPMod *mods[MODSIZE];
1195 Rdn *NoRemove = NULL;
1196 char *cn[] = {NULL, NULL};
1197 char *givenName[] = {NULL, NULL};
1199 char *sn[] = {NULL, NULL};
1203 cm_return_if_fail(server != NULL || contact != NULL);
1204 ld = ldapsvr_connect(server->control);
1206 clean_up(ld, server, contact);
1209 dn = g_hash_table_lookup(contact, "dn");
1212 clean_up(ld, server, contact);
1215 NoRemove = ldapsvr_modify_dn(contact, dn);
1217 /* We are trying to change RDN */
1218 gchar *newRdn = g_strdup_printf("%s=%s", NoRemove->attribute, NoRemove->value);
1220 #ifdef OPEN_LDAP_API_AT_LEAST_3000
1222 int rc = ldap_rename_s(ld, dn, newRdn, NULL, 1, NULL, NULL);
1226 /* This is deprecated as of OpenLDAP-2.3.0 */
1227 int rc = ldap_modrdn2_s(ld, dn, newRdn, 1);
1231 if(rc != LDAP_SUCCESS) {
1232 if (rc == LDAP_ALREADY_EXISTS) {
1233 /* We are messing with a contact with more than one listed email
1234 * address and the email we are changing is not the one used for dn
1236 /* It needs to be able to handle renaming errors to an already defined
1237 * dn. For now we just refuse the update. It will be caught later on as
1238 * a LDAPRC_NAMING_VIOLATION error.
1242 g_printerr("Current dn: %s\n", dn);
1243 g_printerr("new dn: %s\n", newRdn);
1244 g_printerr("LDAP Error(ldap_modrdn2_s) failed[0x%x]: %s\n", rc, ldap_err2string(rc));
1246 clean_up(ld, server, contact);
1251 ItemPerson *person = g_hash_table_lookup(contact, "person");
1253 dn = g_strdup(NoRemove->new_dn);
1254 g_hash_table_replace(contact, "dn", dn);
1256 g_free(person->externalID);
1257 person->externalID = dn;
1262 server->retVal = LDAPRC_NODN;
1263 clean_up(ld, server, contact);
1266 param = g_hash_table_lookup(contact , "cn");
1267 mod_op = ldapsvr_deside_operation(ld, server, dn, "displayName", param);
1268 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("cn", NoRemove->attribute) != 0)) {
1269 if (mod_op == LDAP_MOD_DELETE) {
1270 /* Setting param to NULL instructs OpenLDAP to remove any
1271 * value stored for this attribute and remove the attribute
1272 * completely. Should multiple instances of an attribute be
1273 * allowed in the future param is required to have the value
1274 * store for the attribute which is going to be deleted
1278 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1279 /* Having an empty string is considered a syntax error in
1280 * ldap. E.g attributes with empty strings are not allowed
1281 * in which case we treate this as a request for deleting
1284 mod_op = LDAP_MOD_DELETE;
1287 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1288 /* Adding an empty string is considered a syntax error in
1289 * ldap. E.g attributes with empty strings are not allowed
1290 * in which case we silently refuse to add this entry
1294 SETMOD(mods[cnt], modarr[cnt], mod_op, "displayName", cn, param);
1296 g_hash_table_insert(contact, "displayName", param);
1299 param = g_hash_table_lookup(contact , "givenName");
1300 mod_op = ldapsvr_deside_operation(ld, server, dn, "givenName", param);
1301 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("givenName", NoRemove->attribute) != 0)) {
1302 if (mod_op == LDAP_MOD_DELETE) {
1303 /* Setting param to NULL instructs OpenLDAP to remove any
1304 * value stored for this attribute and remove the attribute
1305 * completely. Should multiple instances of an attribute be
1306 * allowed in the future param is required to have the value
1307 * store for the attribute which is going to be deleted
1311 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1312 /* Having an empty string is considered a syntax error in
1313 * ldap. E.g attributes with empty strings are not allowed
1314 * in which case we treate this as a request for deleting
1317 mod_op = LDAP_MOD_DELETE;
1320 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1321 /* Adding an empty string is considered a syntax error in
1322 * ldap. E.g attributes with empty strings are not allowed
1323 * in which case we silently refuse to add this entry
1327 SETMOD(mods[cnt], modarr[cnt], mod_op, "givenName", givenName, param);
1331 mailList = g_hash_table_lookup(contact , "mail");
1333 debug_print("# of mail: %d\n", g_list_length(mailList));
1334 if (!(strcmp("mail", NoRemove->attribute) == 0 && g_list_length(mailList) == 1)) {
1336 tmp = g_malloc(sizeof(*tmp) * (g_list_length(mailList)+1));
1339 EmailKeyValue *item = mailList->data;
1340 *tmp++ = g_strdup((gchar *) item->mail);
1341 mailList = g_list_next(mailList);
1345 * At least one email address is required
1346 * in which case it will always be a replace
1348 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_REPLACE, "mail", mail);
1354 * an error condition since at least one email adress
1355 * is required. Should never occur though.
1358 param = g_hash_table_lookup(contact , "sn");
1359 mod_op = ldapsvr_deside_operation(ld, server, dn, "sn", param);
1360 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("sn", NoRemove->attribute) != 0)) {
1361 if (mod_op == LDAP_MOD_DELETE) {
1362 /* Setting param to NULL instructs OpenLDAP to remove any
1363 * value stored for this attribute and remove the attribute
1364 * completely. Should multiple instances of an attribute be
1365 * allowed in the future param is required to have the value
1366 * store for the attribute which is going to be deleted
1370 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1371 /* Having an empty string is considered a syntax error in
1372 * ldap. E.g attributes with empty strings are not allowed
1373 * in which case we treate this as a request for deleting
1376 mod_op = LDAP_MOD_DELETE;
1379 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1380 /* Adding an empty string is considered a syntax error in
1381 * ldap. E.g attributes with empty strings are not allowed
1382 * in which case we silently refuse to add this entry
1386 SETMOD(mods[cnt], modarr[cnt], mod_op, "sn", sn, param);
1390 debug_print("newDN: %s\n", dn);
1393 server->retVal = LDAPRC_SUCCESS;
1397 rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL);
1399 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1400 dn, rc, ldap_err2string(rc));
1401 server->retVal = LDAPRC_NAMING_VIOLATION;
1406 ldapsvr_handle_other_attributes(ld, server, dn, contact);
1407 /* If we do not make changes persistent at this point then changes
1408 * will be lost if the user makes new search on the same server since
1409 * changes are only present in Claws' internal cache. This issue has to
1410 * be solved in addressbook.c since this involves access to structures
1411 * which are only accessible in addressbook.c */
1412 clean_up(ld, server, contact);
1416 * Delete contact from LDAP
1418 * \param server AddressBook resource
1419 * \param contact GHashTable with object to delete
1421 void ldapsvr_delete_contact(LdapServer *server, GHashTable *contact) {
1426 cm_return_if_fail(server != NULL || contact != NULL);
1427 ld = ldapsvr_connect(server->control);
1429 clean_up(ld, server, contact);
1432 dn = g_hash_table_lookup(contact, "dn");
1434 clean_up(ld, server, contact);
1437 server->retVal = LDAPRC_SUCCESS;
1438 rc = ldap_delete_ext_s(ld, dn, NULL, NULL);
1440 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1441 dn, rc, ldap_err2string(rc));
1442 server->retVal = LDAPRC_NODN;
1444 clean_up(ld, server, contact);
1448 * Update any changes to the server.
1450 * \param server AddressBook resource.
1451 * \param person ItemPerson holding user input.
1453 void ldapsvr_update_book(LdapServer *server, ItemPerson *item) {
1455 GHashTable *contact = NULL;
1456 GList *contacts = NULL, *head = NULL;
1458 cm_return_if_fail(server != NULL);
1459 debug_print("updating ldap addressbook\n");
1461 contact = g_hash_table_new(g_str_hash, g_str_equal);
1463 gboolean result = ldapsvr_retrieve_item_person(item, contact);
1464 debug_print("Found contact to update: %s\n", result? "Yes" : "No");
1466 if (debug_get_mode()) {
1467 addritem_print_item_person(item, stdout);
1469 contacts = g_list_append(contacts, contact);
1473 ItemFolder *folder = server->addressCache->rootFolder;
1474 node = folder->listFolder;
1477 AddrItemObject *aio = node->data;
1479 if (aio->type == ITEMTYPE_FOLDER) {
1480 ItemFolder *folder = (ItemFolder *) aio;
1481 GList *persons = folder->listPerson;
1483 AddrItemObject *aio = persons->data;
1485 if (aio->type == ITEMTYPE_PERSON) {
1486 ItemPerson *item = (ItemPerson *) aio;
1487 gboolean result = ldapsvr_retrieve_item_person(item, contact);
1488 debug_print("Found contact to update: %s\n", result? "Yes" : "No");
1490 if (debug_get_mode()) {
1491 gchar *uid = g_hash_table_lookup(contact, "uid");
1492 item = ldapsvr_get_contact(server, uid);
1493 addritem_print_item_person(item, stdout);
1495 contacts = g_list_append(contacts, contact);
1499 persons = g_list_next(persons);
1504 g_printerr("\t\tpid : ???\n");
1506 node = g_list_next(node);
1511 if (debug_get_mode()) {
1513 debug_print("Contacts which must be updated in LDAP:\n");
1515 debug_print("\tContact:\n");
1516 g_hash_table_foreach(contacts->data,
1517 ldapsvr_print_contacts_hashtable, stderr);
1518 contacts = g_list_next(contacts);
1521 if (contacts == NULL)
1525 contact = (GHashTable *) contacts->data;
1526 status = (gchar *) g_hash_table_lookup(contact, "status");
1528 status = g_strdup("NULL");
1529 if (g_ascii_strcasecmp(status, "new") == 0) {
1530 ldapsvr_add_contact(server, contact);
1532 else if (g_ascii_strcasecmp(status, "update") == 0) {
1533 ldapsvr_update_contact(server, contact);
1535 else if (g_ascii_strcasecmp(status, "delete") == 0) {
1536 ldapsvr_delete_contact(server, contact);
1539 g_critical(_("ldapsvr_update_book->Unknown status: %s\n"), status);
1540 contacts = g_list_next(contacts);
1542 ldapsvr_free_hashtable(head);
1545 #endif /* USE_LDAP */