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 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 g_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) {
321 g_return_val_if_fail(ctl != NULL, NULL);
323 ldapsrv_set_options (ctl->timeOut, NULL);
324 uri = g_strdup_printf("ldap%s://%s:%d",
325 ctl->enableSSL?"s":"",
326 ctl->hostName, ctl->port);
327 ldap_initialize(&ld, uri);
334 debug_print("connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port);
338 version = LDAP_VERSION3;
339 rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
340 if (rc == LDAP_OPT_SUCCESS) {
341 ctl->version = LDAP_VERSION3;
344 if (ctl->version == LDAP_VERSION3) {
345 if (ctl->enableTLS && !ctl->enableSSL) {
346 rc = ldap_start_tls_s(ld, NULL, NULL);
348 if (rc != LDAP_SUCCESS) {
349 g_printerr("LDAP Error(tls): ldap_simple_bind_s: %s\n",
350 ldap_err2string(rc));
357 /* Bind to the server, if required */
359 if (* ctl->bindDN != '\0') {
360 rc = claws_ldap_simple_bind_s(ld, ctl->bindDN, ctl->bindPass);
361 if (rc != LDAP_SUCCESS) {
362 g_printerr("bindDN: %s, bindPass: %s\n", ctl->bindDN, ctl->bindPass);
363 g_printerr("LDAP Error(bind): ldap_simple_bind_s: %s\n",
364 ldap_err2string(rc));
373 * Disconnect to LDAP server.
374 * \param ld Resource to LDAP.
376 void ldapsvr_disconnect(LDAP *ld) {
378 g_return_if_fail(ld != NULL);
379 ldap_unbind_ext(ld, NULL, NULL);
383 * Create an initial Rdn structure
385 * \return empty structure
390 buf = g_new0(Rdn, 1);
391 buf->attribute = NULL;
398 * Free a created Rdn structure
399 * \param rdn Structure to free
401 void rdn_free(Rdn *rdn) {
402 if (rdn->attribute) {
403 g_free(rdn->attribute);
404 rdn->attribute = NULL;
419 * update Rdn structure
421 * \param rdn Rdn structure to update
422 * \param head Uniq part of dn
423 * \param tail Common part of dn
425 void update_rdn(Rdn *rdn, gchar *head, gchar *tail) {
426 rdn->value = g_strdup(head);
427 rdn->new_dn = g_strdup_printf("%s=%s%s", rdn->attribute, head, tail);
431 * Deside if dn needs to be changed
433 * \param hash GHashTable with user input.
434 * \param dn dn for current object
435 * \return Rdn structure
437 Rdn *ldapsvr_modify_dn(GHashTable *hash, gchar *dn) {
439 gchar *pos, *compare;
442 g_return_val_if_fail(hash != NULL || dn != NULL, NULL);
444 pos = g_strstr_len(dn, strlen(dn), "=");
445 compare = g_strndup(dn, pos - dn);
450 rest = g_strstr_len(pos, strlen(pos), ",");
451 val = g_strndup(pos, rest - pos);
458 rdn->value = g_strdup(val);
459 rdn->attribute = g_strdup(compare);
461 if (strcmp("mail", rdn->attribute) == 0) {
462 GList *list = g_hash_table_lookup(hash, rdn->attribute);
464 EmailKeyValue *item = list->data;
465 compare = g_strdup((gchar *) item->mail);
466 if (strcmp(compare, rdn->value) == 0) {
467 update_rdn(rdn, compare, rest);
471 list = g_list_next(list);
473 /* if compare and rdn->attribute are equal then last email removed/empty */
474 if (strcmp(compare, rdn->attribute) != 0) {
475 /* RDN changed. Find new */
476 update_rdn(rdn, compare, rest);
481 /* We cannot remove dn */
488 compare = g_hash_table_lookup(hash, rdn->attribute);
489 /* if compare and rdn->attribute are equal then dn removed/empty */
490 if (strcmp(compare, rdn->attribute) != 0) {
491 update_rdn(rdn, compare, rest);
495 /* We cannot remove dn */
505 * This macro is borrowed from the Balsa project
506 * Creates a LDAPMod structure
508 * \param mods Empty LDAPMod structure
509 * \param modarr Array with values to insert
510 * \param op Operation to perform on LDAP
511 * \param attr Attribute to insert
512 * \param strv Empty array which is NULL terminated
513 * \param val Value for attribute
515 #define SETMOD(mods,modarr,op,attr,strv,val) \
516 do { (mods) = &(modarr); (modarr).mod_type=attr; (modarr).mod_op=op;\
517 (strv)[0]=(val); (modarr).mod_values=strv; \
521 * Creates a LDAPMod structure
523 * \param mods Empty LDAPMod structure
524 * \param modarr Array with values to insert
525 * \param op Operation to perform on LDAP
526 * \param attr Attribute to insert
527 * \param strv Array with values to insert. Must be NULL terminated
529 #define SETMODS(mods,modarr,op,attr,strv) \
530 do { (mods) = &(modarr); (modarr).mod_type=attr; \
531 (modarr).mod_op=op; (modarr).mod_values=strv; \
536 * Clean up, close LDAP connection, and refresh cache
538 * \param ld Resource to LDAP
539 * \param server AddressBook resource
540 * \param contact GHashTable with current changed object
542 void clean_up(LDAP *ld, LdapServer *server, GHashTable *contact) {
544 ldapsvr_get_contact(server, g_hash_table_lookup(contact , "uid"));
547 person->status = NONE;
548 displayName = g_hash_table_lookup(contact, "displayName");
550 person->nickName = g_strdup(displayName);
552 if (server->retVal != LDAPRC_SUCCESS) {
555 addrcache_remove_person(server->addressCache, person);
557 g_critical(N_("ldapsvr_update_book: Could not clean cache\n"));
559 addritem_free_item_person(res);
563 ldapsvr_disconnect(ld);
567 * Get cn attribute from dn
569 * \param dn Distinguesh Name for current object
570 * \return AttrKeyValue, or <i>NULL</i> if none created
572 AttrKeyValue *get_cn(gchar *dn) {
578 g_return_val_if_fail(dn != NULL, NULL);
580 cn = attrkeyvalue_create();
581 start = g_strstr_len(dn, strlen(dn), "cn");
583 attrkeyvalue_free(cn);
586 end = g_strstr_len(start, strlen(start), ",");
587 item = g_strndup(start, end - start);
589 attrkeyvalue_free(cn);
592 key_value = g_strsplit(item, "=", 2);
593 cn->key = g_strdup(key_value[0]);
594 cn->value = g_strdup(key_value[1]);
595 g_strfreev(key_value);
601 * Get mail attribute from dn
603 * \param dn Distinguesh Name for current object
604 * \return AttrKeyValue, or <i>NULL</i> if none created
606 AttrKeyValue *get_mail(gchar *dn) {
612 g_return_val_if_fail(dn != NULL, NULL);
614 mail = attrkeyvalue_create();
615 start = g_strstr_len(dn, strlen(dn), "mail");
617 attrkeyvalue_free(mail);
620 end = g_strstr_len(start, strlen(start), ",");
621 item = g_strndup(start, end - start);
623 attrkeyvalue_free(mail);
626 key_value = g_strsplit(item, "=", 2);
627 mail->key = g_strdup(key_value[0]);
628 mail->value = g_strdup(key_value[1]);
629 g_strfreev(key_value);
635 * Get ou or o attribute from dn
637 * \param dn Distinguesh Name for current object
638 * \return AttrKeyValue, or <i>NULL</i> if none created
640 AttrKeyValue *get_ou(gchar *dn) {
647 g_return_val_if_fail(dn != NULL, NULL);
648 ou = attrkeyvalue_create();
649 start = g_strstr_len(dn, strlen(dn), ",o=");
651 start = g_strstr_len(dn, strlen(dn), ",ou=");
653 attrkeyvalue_free(ou);
657 end = g_strstr_len(start, strlen(start), ",");
658 item = g_strndup(start, end - start);
660 attrkeyvalue_free(ou);
663 key_value = g_strsplit(item, "=", 2);
664 ou->key = g_strdup(key_value[0]);
665 ou->value = g_strdup(key_value[1]);
666 g_strfreev(key_value);
672 * Print the contents of a LDAPMod structure for debuging purposes
674 * \param mods LDAPMod structure
676 void ldapsvr_print_ldapmod(LDAPMod *mods[]) {
680 g_return_if_fail(mods != NULL);
681 g_printerr( "Type\n");
682 for (i = 0; NULL != mods[i]; i++) {
683 LDAPMod *mod = (LDAPMod *) mods[i];
685 switch (mod->mod_op) {
686 case LDAP_MOD_ADD: mod_op = g_strdup("ADD"); break;
687 case LDAP_MOD_REPLACE: mod_op = g_strdup("MODIFY"); break;
688 case LDAP_MOD_DELETE: mod_op = g_strdup("DELETE"); break;
689 default: mod_op = g_strdup("UNKNOWN");
691 g_printerr( "Operation: %s\tType:%s\nValues:\n", mod_op, mod->mod_type);
692 vals = mod->mod_vals.modv_strvals;
694 g_printerr( "\t%s\n", *vals++);
700 * Make a compare for every new value we want to store in the
701 * directory with the current values. Great tool for debugging
702 * against invalid syntax in attributes
704 * \param ld AddressBook resource
705 * \param dn dn for the entry
706 * \param cnt Number of attributes to compare
707 * \param mods LDAPMod structure
709 void ldapsvr_compare_attr(LDAP *ld, gchar *dn, gint cnt, LDAPMod *mods[]) {
712 g_return_if_fail(ld != NULL || dn != NULL || cnt >= 0 || mods != NULL);
713 for (i = 0; i < cnt; i++) {
714 gchar *value = g_strdup(mods[i]->mod_vals.modv_strvals[0]);
715 if (!value || strcmp(value, "") == 0)
716 value = g_strdup("thisisonlyadummy");
717 rc = ldap_compare_s(ld, dn, mods[i]->mod_type, value);
718 g_printerr("ldap_compare for (%s:%s)\" failed[0x%x]: %s\n",
719 mods[i]->mod_type, value, rc, ldap_err2string(rc));
725 * compare attribute to LDAP in case of LDAP_INAPPROPRIATE_MATCHING
727 * \param ld AddressBook resource
728 * \param server Reference to server
729 * \param dn dn for the entry
730 * \param attr Attribute
731 * \param value New value
732 * \return int, return will be LDAP_MOD_ADD, LDAP_MOD_REPLACE, or LDAP_MOD_DELETE
734 int ldapsvr_compare_manual_attr(LDAP *ld, LdapServer *server, gchar *dn, char *attr, char *value) {
735 LDAPMessage *res, *e = NULL;
737 struct berval **vals;
745 g_return_val_if_fail(ld != NULL || server != NULL || attr != NULL, -1);
746 ctl = server->control;
750 filter = g_strdup_printf("(&(mail=%s)(%s=*))", mail->value, attr);
751 attrkeyvalue_free(mail);
753 rc = ldap_search_s(ld, ctl->baseDN, /*LDAP_SCOPE_SUBTREE*/LDAP_SCOPE_ONELEVEL, filter, NULL, 0, &res);
755 g_printerr("ldap_search for attr=%s\" failed[0x%x]: %s\n",attr, rc, ldap_err2string(rc));
759 e = ldap_first_entry(ld, res);
760 /* entry has this attribute */
762 attribute = ldap_first_attribute( ld, e, &ber );
765 if( ( vals = ldap_get_values_len( ld, e, attr ) ) != NULL ) {
766 for( i = 0; vals[i] != NULL; i++ ) {
767 debug_print("Compare: %s=%s\n", attr, vals[i]->bv_val);
768 /* attribute has same value */
769 if (strcmp(vals[i]->bv_val, value) == 0)
771 /* attribute has new value */
773 retVal = LDAP_MOD_REPLACE;
776 ldap_value_free_len(vals);
779 retVal = LDAP_MOD_DELETE;
784 ldap_memfree(attribute);
786 /* entry does not have this attribute */
788 /* Only add if this is a real attribute */
790 retVal = LDAP_MOD_ADD;
791 /* This is dummy value used to avoid ldap_compare error */
804 * Deside which kind of operation is required to handle
805 * updating the specified attribute
807 * \param ld AddressBook resource
808 * \param server Reference to server
809 * \param dn dn for the entry
810 * \param attr Attribute
811 * \param value New value
812 * \return int, return will be LDAP_MOD_ADD, LDAP_MOD_REPLACE, or LDAP_MOD_DELETE
814 int ldapsvr_deside_operation(LDAP *ld, LdapServer *server, char *dn, char *attr, char *value) {
816 gboolean dummy = FALSE;
818 g_return_val_if_fail(ld != NULL || server != NULL || dn != NULL || attr != NULL, -1);
821 /* value containing empty string cause invalid syntax. A bug in
822 * the LDAP library? Therefore we add a dummy value
824 if (strcmp(value,"") == 0) {
825 value = g_strdup("thisisonlyadummy");
828 rc = ldap_compare_s(ld, dn, attr, value);
829 debug_print("ldap_compare for (%s:%s)\" error_code[0x%x]: %s\n",
830 attr, value, rc, ldap_err2string(rc));
832 case LDAP_COMPARE_FALSE:
834 return LDAP_MOD_DELETE;
836 return LDAP_MOD_REPLACE;
837 case LDAP_COMPARE_TRUE: return -1;
838 case LDAP_NO_SUCH_ATTRIBUTE: return LDAP_MOD_ADD;
839 /* LDAP_INAPPROPRIATE_MATCHING needs extensive testing because I
840 * am not aware off the condition causing this return value!
842 case LDAP_INAPPROPRIATE_MATCHING:
845 return ldapsvr_compare_manual_attr(ld, server, dn, attr, value);
846 case LDAP_UNDEFINED_TYPE: return -2;
847 case LDAP_INVALID_SYNTAX: return -2;
853 * Check if attribute is part of the current search criteria
855 * \param list Array containing attributes in the current search criteria
856 * \param attr Attribute to check
857 * \result <i>TRUE</i> if attribute is found in the current search criteria
859 gboolean ldapsvr_check_search_attributes(char **list, char *attr) {
861 if (strcmp(*list++, attr) == 0)
868 * Deside which other attributes needs updating
870 * \param ld LDAP resource
871 * \param server AddressBook resource
872 * \param dn dn for the entry
873 * \param contact GHashTable with information for the current contact
875 void ldapsvr_handle_other_attributes(LDAP *ld, LdapServer *server, char *dn, GHashTable *contact) {
877 gboolean CHECKED_ATTRIBUTE[ATTRIBUTE_SIZE + 1];
878 LDAPMod *mods[ATTRIBUTE_SIZE + 1];
879 LDAPMod modarr[ATTRIBUTE_SIZE];
881 char *attr[ATTRIBUTE_SIZE + 1][2];
884 g_return_if_fail(server != NULL || dn != NULL || contact != NULL);
885 for (i = 0; i <= ATTRIBUTE_SIZE; i++) {
886 CHECKED_ATTRIBUTE[i] = FALSE;
887 attr[i][0] = attr[i][1] = NULL;
889 node = g_hash_table_lookup(contact , "attribute");
891 AttrKeyValue *item = node->data;
893 int index = get_attribute_index(item->key);
895 debug_print("Found other attribute: %s = %s\n",
896 item->key?item->key:"null", item->value?item->value:"null");
897 mod_op = ldapsvr_deside_operation(ld, server, dn, item->key, item->value);
898 /* Only consider attributes which we no how to handle.
899 * Set to TRUE in CHECKED_ATTRIBUTE array to indicate no further action
902 CHECKED_ATTRIBUTE[index] = TRUE;
903 node = g_list_next(node);
906 if (mod_op == LDAP_MOD_DELETE) {
907 /* Setting param to NULL instructs OpenLDAP to remove any
908 * value stored for this attribute and remove the attribute
909 * completely. Should multiple instances of an attribute be
910 * allowed in the future param is required to have the value
911 * store for the attribute which is going to be deleted
915 if (mod_op == LDAP_MOD_REPLACE && strcmp(item->value, "") == 0) {
916 /* Having an empty string is considered a syntax error in
917 * ldap. E.g attributes with empty strings are not allowed
918 * in which case we treate this as a request for deleting
921 mod_op = LDAP_MOD_DELETE;
924 if (mod_op == LDAP_MOD_ADD && strcmp(item->value, "") == 0) {
925 /* Adding an empty string is considered a syntax error in
926 * ldap. E.g attributes with empty strings are not allowed
927 * in which case we silently refuse to add this entry
931 SETMOD(mods[cnt], modarr[cnt], mod_op, g_strdup(item->key), attr[cnt], g_strdup(item->value));
933 CHECKED_ATTRIBUTE[index] = TRUE;
937 node = g_list_next(node);
939 char **attribs = ldapctl_full_attribute_array(server->control);
940 for (i = 0; i < ATTRIBUTE_SIZE; i++) {
941 /* Attributes which holds no information are to be removed */
942 if (CHECKED_ATTRIBUTE[i] == FALSE) {
943 /* Only consider those attributes which is currently part of the search criteria.
944 * If attributes are not part of the search criteria they would seem to hold
945 * no information since their values will not be populated in the GUI
947 if (!strcmp(ATTRIBUTE[i], "jpegPhoto")) {
948 debug_print("not updating jpegPhoto\n");
951 if (ldapsvr_check_search_attributes(attribs, (char *) ATTRIBUTE[i])) {
952 mod_op = ldapsvr_deside_operation(ld, server, dn, (char *) ATTRIBUTE[i], "");
953 if (mod_op == LDAP_MOD_DELETE) {
954 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_DELETE, g_strdup((char *) ATTRIBUTE[i]), attr[cnt], NULL);
960 ldapctl_free_attribute_array(attribs);
962 if (debug_get_mode())
963 ldapsvr_print_ldapmod(mods);
964 server->retVal = LDAPRC_SUCCESS;
965 rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL);
968 case LDAP_ALREADY_EXISTS:
969 server->retVal = LDAPRC_ALREADY_EXIST;
972 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", dn, rc, ldap_err2string(rc));
974 server->retVal = LDAPRC_STRONG_AUTH;
976 server->retVal = LDAPRC_NAMING_VIOLATION;
980 char **attribs = ldapctl_full_attribute_array(server->control);
981 for (i = 0; i < ATTRIBUTE_SIZE; i++) {
982 if (!strcmp(ATTRIBUTE[i], "jpegPhoto")) {
983 debug_print("not updating jpegPhoto\n");
986 if (ldapsvr_check_search_attributes(attribs, (char *) ATTRIBUTE[i])) {
987 if (CHECKED_ATTRIBUTE[i] == FALSE) {
988 AddrItemObject *aio = addrcache_get_object(server->addressCache, g_hash_table_lookup(contact , "uid"));
989 ItemPerson *person = (ItemPerson *) aio;
990 addritem_person_remove_attribute(person, (const gchar *) ATTRIBUTE[i]);
994 ldapctl_free_attribute_array(attribs);
999 * Add new contact to LDAP
1001 * \param server AddressBook resource
1002 * \param contact GHashTable with object to add
1004 void ldapsvr_add_contact(LdapServer *server, GHashTable *contact) {
1005 gchar *email = NULL, *param = NULL;
1007 LDAPMod *mods[MODSIZE];
1010 char *cn[] = {NULL, NULL};
1011 char *displayName[] = {NULL, NULL};
1012 char *givenName[] = {NULL, NULL};
1014 char *sn[] = {NULL, NULL};
1015 char *org[] = {NULL, NULL};
1016 char *obj[] = {/*"top",*/ "person", "organizationalPerson", "inetOrgPerson", NULL};
1019 AttrKeyValue *ou, *commonName;
1024 g_return_if_fail(server != NULL || contact != NULL);
1025 node = g_hash_table_lookup(contact , "mail");
1027 EmailKeyValue *newEmail = node->data;
1028 email = g_strdup(newEmail->mail);
1030 if (email == NULL) {
1031 server->retVal = LDAPRC_NODN;
1032 clean_up(ld, server, contact);
1035 base_dn = g_strdup_printf("mail=%s,%s",
1036 email, server->control->baseDN?server->control->baseDN:"null");
1039 ldapsvr_get_contact(server, g_hash_table_lookup(contact , "uid"));
1040 person->externalID = g_strdup(base_dn);
1041 debug_print("dn: %s\n", base_dn);
1042 ld = ldapsvr_connect(server->control);
1044 clean_up(ld, server, contact);
1045 debug_print("no ldap found\n");
1048 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "objectClass", obj);
1050 ou = get_ou(base_dn);
1052 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, g_strdup(ou->key), org, g_strdup(ou->value));
1054 attrkeyvalue_free(ou);
1057 commonName = get_cn(base_dn);
1058 if (commonName == NULL) {
1059 param = g_hash_table_lookup(contact , "cn");
1061 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "cn", cn, param);
1064 clean_up(ld, server, contact);
1065 debug_print("no CN found\n");
1070 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, g_strdup(commonName->key), cn, g_strdup(commonName->value));
1072 param = g_hash_table_lookup(contact , "cn");
1073 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "displayName", displayName, param);
1074 g_hash_table_insert(contact, "displayName", param);
1075 attrkeyvalue_free(commonName);
1078 param = g_hash_table_lookup(contact , "givenName");
1080 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "givenName", givenName, param);
1083 mailList = g_hash_table_lookup(contact , "mail");
1086 tmp = g_malloc(sizeof(*tmp) * (g_list_length(mailList)+1));
1089 EmailKeyValue *item = mailList->data;
1090 *tmp++ = g_strdup((gchar *) item->mail);
1091 mailList = g_list_next(mailList);
1094 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "mail", mail);
1097 param = g_hash_table_lookup(contact, "sn");
1099 param = g_strdup(N_("Some SN"));
1100 SETMOD(mods[cnt], modarr[cnt], LDAP_MOD_ADD, "sn", sn, param);
1103 if (debug_get_mode()) {
1104 ldapsvr_print_ldapmod(mods);
1106 server->retVal = LDAPRC_SUCCESS;
1107 rc = ldap_add_ext_s(ld, base_dn, mods, NULL, NULL);
1110 case LDAP_ALREADY_EXISTS:
1111 server->retVal = LDAPRC_ALREADY_EXIST;
1114 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1115 base_dn, rc, ldap_err2string(rc));
1117 server->retVal = LDAPRC_STRONG_AUTH;
1119 server->retVal = LDAPRC_NAMING_VIOLATION;
1122 ldapsvr_handle_other_attributes(ld, server, base_dn, contact);
1124 clean_up(ld, server, contact);
1128 * Update contact to LDAP
1130 * \param server AddressBook resource
1131 * \param contact GHashTable with object to update
1133 void ldapsvr_update_contact(LdapServer *server, GHashTable *contact) {
1135 LDAPMod *mods[MODSIZE];
1139 Rdn *NoRemove = NULL;
1140 char *cn[] = {NULL, NULL};
1141 char *givenName[] = {NULL, NULL};
1143 char *sn[] = {NULL, NULL};
1147 g_return_if_fail(server != NULL || contact != NULL);
1148 ld = ldapsvr_connect(server->control);
1150 clean_up(ld, server, contact);
1153 dn = g_hash_table_lookup(contact, "dn");
1156 clean_up(ld, server, contact);
1159 NoRemove = ldapsvr_modify_dn(contact, dn);
1161 /* We are trying to change RDN */
1162 gchar *newRdn = g_strdup_printf("%s=%s", NoRemove->attribute, NoRemove->value);
1163 int rc = ldap_modrdn2_s(ld, dn, newRdn, 1);
1164 if(rc != LDAP_SUCCESS) {
1165 if (rc == LDAP_ALREADY_EXISTS) {
1166 /* We are messing with a contact with more than one listed email
1167 * address and the email we are changing is not the one used for dn
1169 /* It needs to be able to handle renaming errors to an already defined
1170 * dn. For now we just refuse the update. It will be caught later on as
1171 * a LDAPRC_NAMING_VIOLATION error.
1175 g_printerr("Current dn: %s\n", dn);
1176 g_printerr("new dn: %s\n", newRdn);
1177 g_printerr("LDAP Error(ldap_modrdn2_s) failed[0x%x]: %s\n", rc, ldap_err2string(rc));
1179 clean_up(ld, server, contact);
1184 ItemPerson *person = g_hash_table_lookup(contact, "person");
1186 dn = g_strdup(NoRemove->new_dn);
1187 g_hash_table_replace(contact, "dn", dn);
1189 g_free(person->externalID);
1190 person->externalID = dn;
1195 server->retVal = LDAPRC_NODN;
1196 clean_up(ld, server, contact);
1199 param = g_hash_table_lookup(contact , "cn");
1200 mod_op = ldapsvr_deside_operation(ld, server, dn, "displayName", param);
1201 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("cn", NoRemove->attribute) != 0)) {
1202 if (mod_op == LDAP_MOD_DELETE) {
1203 /* Setting param to NULL instructs OpenLDAP to remove any
1204 * value stored for this attribute and remove the attribute
1205 * completely. Should multiple instances of an attribute be
1206 * allowed in the future param is required to have the value
1207 * store for the attribute which is going to be deleted
1211 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1212 /* Having an empty string is considered a syntax error in
1213 * ldap. E.g attributes with empty strings are not allowed
1214 * in which case we treate this as a request for deleting
1217 mod_op = LDAP_MOD_DELETE;
1220 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1221 /* Adding an empty string is considered a syntax error in
1222 * ldap. E.g attributes with empty strings are not allowed
1223 * in which case we silently refuse to add this entry
1227 SETMOD(mods[cnt], modarr[cnt], mod_op, "displayName", cn, param);
1229 g_hash_table_insert(contact, "displayName", param);
1232 param = g_hash_table_lookup(contact , "givenName");
1233 mod_op = ldapsvr_deside_operation(ld, server, dn, "givenName", param);
1234 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("givenName", NoRemove->attribute) != 0)) {
1235 if (mod_op == LDAP_MOD_DELETE) {
1236 /* Setting param to NULL instructs OpenLDAP to remove any
1237 * value stored for this attribute and remove the attribute
1238 * completely. Should multiple instances of an attribute be
1239 * allowed in the future param is required to have the value
1240 * store for the attribute which is going to be deleted
1244 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1245 /* Having an empty string is considered a syntax error in
1246 * ldap. E.g attributes with empty strings are not allowed
1247 * in which case we treate this as a request for deleting
1250 mod_op = LDAP_MOD_DELETE;
1253 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1254 /* Adding an empty string is considered a syntax error in
1255 * ldap. E.g attributes with empty strings are not allowed
1256 * in which case we silently refuse to add this entry
1260 SETMOD(mods[cnt], modarr[cnt], mod_op, "givenName", givenName, param);
1264 mailList = g_hash_table_lookup(contact , "mail");
1266 debug_print("# of mail: %d\n", g_list_length(mailList));
1267 if (!(strcmp("mail", NoRemove->attribute) == 0 && g_list_length(mailList) == 1)) {
1269 tmp = g_malloc(sizeof(*tmp) * (g_list_length(mailList)+1));
1272 EmailKeyValue *item = mailList->data;
1273 *tmp++ = g_strdup((gchar *) item->mail);
1274 mailList = g_list_next(mailList);
1278 * At least one email address is required
1279 * in which case it will always be a replace
1281 SETMODS(mods[cnt], modarr[cnt], LDAP_MOD_REPLACE, "mail", mail);
1287 * an error condition since at least one email adress
1288 * is required. Should never occur though.
1291 param = g_hash_table_lookup(contact , "sn");
1292 mod_op = ldapsvr_deside_operation(ld, server, dn, "sn", param);
1293 if (mod_op >= 0 && (strcmp(param, NoRemove->value) != 0 && strcmp("sn", NoRemove->attribute) != 0)) {
1294 if (mod_op == LDAP_MOD_DELETE) {
1295 /* Setting param to NULL instructs OpenLDAP to remove any
1296 * value stored for this attribute and remove the attribute
1297 * completely. Should multiple instances of an attribute be
1298 * allowed in the future param is required to have the value
1299 * store for the attribute which is going to be deleted
1303 if (mod_op == LDAP_MOD_REPLACE && strcmp(param, "") == 0) {
1304 /* Having an empty string is considered a syntax error in
1305 * ldap. E.g attributes with empty strings are not allowed
1306 * in which case we treate this as a request for deleting
1309 mod_op = LDAP_MOD_DELETE;
1312 if (mod_op == LDAP_MOD_ADD && strcmp(param, "") == 0) {
1313 /* Adding an empty string is considered a syntax error in
1314 * ldap. E.g attributes with empty strings are not allowed
1315 * in which case we silently refuse to add this entry
1319 SETMOD(mods[cnt], modarr[cnt], mod_op, "sn", sn, param);
1323 debug_print("newDN: %s\n", dn);
1326 server->retVal = LDAPRC_SUCCESS;
1330 rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL);
1332 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1333 dn, rc, ldap_err2string(rc));
1334 server->retVal = LDAPRC_NAMING_VIOLATION;
1339 ldapsvr_handle_other_attributes(ld, server, dn, contact);
1340 /* If we do not make changes persistent at this point then changes
1341 * will be lost if the user makes new search on the same server since
1342 * changes are only present in Claws' internal cache. This issue has to
1343 * be solved in addressbook.c since this involves access to structures
1344 * which are only accessible in addressbook.c */
1345 clean_up(ld, server, contact);
1349 * Delete contact from LDAP
1351 * \param server AddressBook resource
1352 * \param contact GHashTable with object to delete
1354 void ldapsvr_delete_contact(LdapServer *server, GHashTable *contact) {
1359 g_return_if_fail(server != NULL || contact != NULL);
1360 ld = ldapsvr_connect(server->control);
1362 clean_up(ld, server, contact);
1365 dn = g_hash_table_lookup(contact, "dn");
1367 clean_up(ld, server, contact);
1370 server->retVal = LDAPRC_SUCCESS;
1371 rc = ldap_delete_ext_s(ld, dn, NULL, NULL);
1373 g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n",
1374 dn, rc, ldap_err2string(rc));
1375 server->retVal = LDAPRC_NODN;
1377 clean_up(ld, server, contact);
1381 * Update any changes to the server.
1383 * \param server AddressBook resource.
1384 * \param person ItemPerson holding user input.
1386 void ldapsvr_update_book(LdapServer *server, ItemPerson *item) {
1388 GHashTable *contact = NULL;
1389 GList *contacts = NULL, *head = NULL;
1391 g_return_if_fail(server != NULL);
1392 debug_print("updating ldap addressbook\n");
1394 contact = g_hash_table_new(g_str_hash, g_str_equal);
1396 gboolean result = ldapsvr_retrieve_item_person(item, contact);
1397 debug_print("Found contact to update: %s\n", result? "Yes" : "No");
1399 if (debug_get_mode()) {
1400 addritem_print_item_person(item, stdout);
1402 contacts = g_list_append(contacts, contact);
1406 ItemFolder *folder = server->addressCache->rootFolder;
1407 node = folder->listFolder;
1410 AddrItemObject *aio = node->data;
1412 if (aio->type == ITEMTYPE_FOLDER) {
1413 ItemFolder *folder = (ItemFolder *) aio;
1414 GList *persons = folder->listPerson;
1416 AddrItemObject *aio = persons->data;
1418 if (aio->type == ITEMTYPE_PERSON) {
1419 ItemPerson *item = (ItemPerson *) aio;
1420 gboolean result = ldapsvr_retrieve_item_person(item, contact);
1421 debug_print("Found contact to update: %s\n", result? "Yes" : "No");
1423 if (debug_get_mode()) {
1424 gchar *uid = g_hash_table_lookup(contact, "uid");
1425 item = ldapsvr_get_contact(server, uid);
1426 addritem_print_item_person(item, stdout);
1428 contacts = g_list_append(contacts, contact);
1432 persons = g_list_next(persons);
1437 g_printerr("\t\tpid : ???\n");
1439 node = g_list_next(node);
1444 if (debug_get_mode()) {
1446 debug_print("Contacts which must be updated in LDAP:\n");
1448 debug_print("\tContact:\n");
1449 g_hash_table_foreach(contacts->data,
1450 ldapsvr_print_contacts_hashtable, stderr);
1451 contacts = g_list_next(contacts);
1454 if (contacts == NULL)
1458 contact = (GHashTable *) contacts->data;
1459 status = (gchar *) g_hash_table_lookup(contact, "status");
1461 status = g_strdup("NULL");
1462 if (g_ascii_strcasecmp(status, "new") == 0) {
1463 ldapsvr_add_contact(server, contact);
1465 else if (g_ascii_strcasecmp(status, "update") == 0) {
1466 ldapsvr_update_contact(server, contact);
1468 else if (g_ascii_strcasecmp(status, "delete") == 0) {
1469 ldapsvr_delete_contact(server, contact);
1472 g_critical(_("ldapsvr_update_book->Unknown status: %s\n"), status);
1473 contacts = g_list_next(contacts);
1475 ldapsvr_free_hashtable(head);
1478 #endif /* USE_LDAP */