4 * A module which implements the LDAP connector for Citadel.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
32 #include <libcitadel.h>
35 #include "citserver.h"
41 #include "serv_ldap.h"
44 #include "ctdl_module.h"
50 #define LDAP_DEPRECATED 1 /* to stop warnings with newer libraries */
54 LDAP *dirserver = NULL;
55 int ldap_time_disconnect = 0;
59 /* There is a forward referance so.... */
60 int delete_from_ldap(char *cn, char *ou, void **object);
64 * LDAP connector cleanup function
66 void serv_ldap_cleanup(void)
69 CtdlLogPrintf(CTDL_INFO,
70 "LDAP: Unbinding from directory server\n");
71 ldap_unbind(dirserver);
74 ldap_time_disconnect = 0;
82 * Make sure this function is only called from within a begin_critical_section(S_LDAP)
83 * If you don't things will break!!!!!.
87 int connect_to_ldap(void)
92 if (dirserver) { // Already connected
93 ldap_time_disconnect = 1 ; // reset the timer.
97 CtdlLogPrintf(CTDL_INFO, "LDAP: Connecting to LDAP server %s:%d...\n",
98 config.c_ldap_host, config.c_ldap_port);
100 dirserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
101 if (dirserver == NULL) {
102 CtdlLogPrintf(CTDL_CRIT,
103 "LDAP: Could not connect to %s:%d : %s\n",
104 config.c_ldap_host, config.c_ldap_port,
106 CtdlAideMessage(strerror(errno), "LDAP: Could not connect to server.");
110 ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
112 CtdlLogPrintf(CTDL_INFO, "LDAP: Binding to %s\n", config.c_ldap_bind_dn);
114 i = ldap_simple_bind_s(dirserver, config.c_ldap_bind_dn, config.c_ldap_bind_pw);
115 if (i == LDAP_SUCCESS) {
117 "WARNING: populating an external LDAP address book is deprecated.\n"
118 "This function will be discontinued in a future release.\n"
119 "Please migrate to vCard-based address books as soon as possible.\n"
120 "Visit the Citadel support forum if you need further assistance.\n"
122 "Warning: LDAP address book is deprecated"
126 CtdlLogPrintf(CTDL_CRIT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
127 dirserver = NULL; /* FIXME disconnect from ldap */
128 CtdlAideMessage(ldap_err2string(i), "LDAP: Cannot bind to server");
131 ldap_time_disconnect = 1;
138 * Create the root node. If it's already there, so what?
140 void create_ldap_root(void)
143 char *objectClass_values[3];
144 LDAPMod dc, objectClass;
149 /* We just want the top-level dc, not the whole hierarchy */
150 strcpy(topdc, config.c_ldap_base_dn);
151 for (i = 0; topdc[i]; ++i) {
152 if (topdc[i] == ',') {
157 for (i = 0; topdc[i]; ++i) {
159 strcpy(topdc, &topdc[i + 1]);
162 /* Set up the transaction */
163 dc.mod_op = LDAP_MOD_ADD;
165 dc_values[0] = topdc;
167 dc.mod_values = dc_values;
168 objectClass.mod_op = LDAP_MOD_ADD;
169 objectClass.mod_type = "objectClass";
170 objectClass_values[0] = "top";
171 objectClass_values[1] = "domain";
172 objectClass_values[2] = NULL;
173 objectClass.mod_values = objectClass_values;
175 mods[1] = &objectClass;
178 /* Perform the transaction */
179 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Setting up Base DN node...\n");
181 begin_critical_section(S_LDAP);
182 if (connect_to_ldap()) {
183 end_critical_section(S_LDAP);
186 i = ldap_add_s(dirserver, config.c_ldap_base_dn, mods);
187 end_critical_section(S_LDAP);
189 if (i == LDAP_ALREADY_EXISTS) {
190 CtdlLogPrintf(CTDL_INFO,
191 "LDAP: Base DN is already present in the directory; no need to add it again.\n");
192 } else if (i != LDAP_SUCCESS) {
193 CtdlLogPrintf(CTDL_CRIT, "LDAP: ldap_add_s() failed: %s (%d)\n",
194 ldap_err2string(i), i);
200 * Create an OU node representing a Citadel host.
201 * parameter cn is not used, its just there to keep the hook interface consistant
202 * parameter object not used here, present for interface compatability
204 int create_ldap_host_OU(char *cn, char *host, void **object)
207 char *objectClass_values[3];
208 LDAPMod dc, objectClass;
213 /* The DN is this OU plus the base. */
214 snprintf(dn, sizeof dn, "ou=%s,%s", host, config.c_ldap_base_dn);
216 /* Set up the transaction */
217 dc.mod_op = LDAP_MOD_ADD;
221 dc.mod_values = dc_values;
222 objectClass.mod_op = LDAP_MOD_ADD;
223 objectClass.mod_type = "objectClass";
224 objectClass_values[0] = "top";
225 objectClass_values[1] = "organizationalUnit";
226 objectClass_values[2] = NULL;
227 objectClass.mod_values = objectClass_values;
229 mods[1] = &objectClass;
232 /* Perform the transaction */
233 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Setting up Host OU node...\n");
235 begin_critical_section(S_LDAP);
236 if (connect_to_ldap()) {
237 end_critical_section(S_LDAP);
240 i = ldap_add_s(dirserver, dn, mods);
241 end_critical_section(S_LDAP);
243 if (i == LDAP_ALREADY_EXISTS) {
244 CtdlLogPrintf(CTDL_INFO,
245 "LDAP: Host OU is already present in the directory; no need to add it again.\n");
246 } else if (i != LDAP_SUCCESS) {
247 CtdlLogPrintf(CTDL_CRIT, "LDAP: ldap_add_s() failed: %s (%d)\n",
248 ldap_err2string(i), i);
260 * Create a base LDAP object for the interface
263 int create_ldap_object(char *cn, char *ou, void **object)
265 // We do nothing here, this just gets the base structure created by the interface.
266 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Created ldap object\n");
272 * Add an attribute to the ldap object
275 int add_ldap_object(char *cn, char *ou, void **object)
283 CtdlLogPrintf(CTDL_DEBUG,
284 "LDAP: Adding ldap attribute name:\"%s\" value:\"%s\"\n",
289 while (attrs[num_attrs])
293 for (cur_attr = 0; cur_attr < num_attrs; cur_attr++) {
294 if (!strcmp(attrs[cur_attr]->mod_type, cn)) { // Adding a value to the attribute
295 if (attrs[cur_attr]->mod_values) {
296 while (attrs[cur_attr]->
297 mod_values[num_values]) {
301 mod_values[num_values])) {
302 CtdlLogPrintf(CTDL_DEBUG,
303 "LDAP: Ignoring duplicate attribute/value pair\n");
309 attrs[cur_attr]->mod_values =
310 realloc(attrs[cur_attr]->mod_values,
311 (num_values + 2) * (sizeof(char *)));
312 attrs[cur_attr]->mod_values[num_values] =
314 attrs[cur_attr]->mod_values[num_values + 1] = NULL;
320 realloc(attrs, (sizeof(LDAPMod *)) * (num_attrs + 2));
322 attrs = malloc((sizeof(LDAPMod *)) * (num_attrs + 2));
323 attrs[num_attrs] = malloc(sizeof(LDAPMod));
324 memset(attrs[num_attrs], 0, sizeof(LDAPMod));
325 attrs[num_attrs + 1] = NULL;
326 attrs[num_attrs]->mod_op = LDAP_MOD_ADD;
327 attrs[num_attrs]->mod_type = strdup(cn);
328 attrs[num_attrs]->mod_values = malloc(2 * sizeof(char *));
329 attrs[num_attrs]->mod_values[0] = strdup(ou);
330 attrs[num_attrs]->mod_values[1] = NULL;
337 * SAve the object to the LDAP server
339 int save_ldap_object(char *cn, char *ou, void **object)
348 if (dirserver == NULL)
353 sprintf(this_dn, "%s,%s", cn, config.c_ldap_base_dn);
355 CtdlLogPrintf(CTDL_INFO, "LDAP: Calling ldap_add_s() for dn of '%s'\n",
358 /* The last attribute must be a NULL one. */
359 attrs = (LDAPMod **) * object;
361 while (attrs[num_attrs]) {
363 while (attrs[num_attrs]->mod_values[count]) {
364 CtdlLogPrintf(CTDL_DEBUG,
365 "LDAP: attribute %d, value %d = \'%s=%s\'\n",
367 attrs[num_attrs]->mod_type,
375 CtdlLogPrintf(CTDL_ERR,
376 "LDAP: no attributes in save_ldap_object\n");
380 begin_critical_section(S_LDAP);
381 if (connect_to_ldap()) {
382 end_critical_section(S_LDAP);
386 i = ldap_add_s(dirserver, this_dn, attrs);
388 if (i == LDAP_SERVER_DOWN) {
390 ("The LDAP server appears to be down.\nThe save to LDAP did not occurr.\n",
391 "LDAP: save failed");
392 end_critical_section(S_LDAP);
396 /* If the entry already exists, repopulate it instead */
397 /* repopulating doesn't work as Citadel may want some attributes to be deleted.
398 * we have no way of knowing which attributes to delete and LDAP won't work it out for us
399 * so now we delete the old entry and create a new one.
401 if (i == LDAP_ALREADY_EXISTS) {
402 end_critical_section(S_LDAP);
403 CtdlLogPrintf(CTDL_INFO,
404 "LDAP: Create, already exists, deleteing first.\n");
405 if (delete_from_ldap(cn, ou, NULL))
407 begin_critical_section(S_LDAP);
408 CtdlLogPrintf(CTDL_INFO,
409 "LDAP: Calling ldap_add_s() to recreate for dn of '%s'\n",
411 i = ldap_add_s(dirserver, this_dn, attrs);
414 if (i != LDAP_SUCCESS) {
415 CtdlLogPrintf(CTDL_ERR, "LDAP: ldap_add_s() failed: %s (%d)\n",
416 ldap_err2string(i), i);
418 ("The LDAP server refused the save command.\nDid you update the schema?\n",
419 "LDAP: save failed (schema?)");
420 end_critical_section(S_LDAP);
423 end_critical_section(S_LDAP);
431 int free_ldap_object(char *cn, char *ou, void **object)
438 attrs = (LDAPMod **) * object;
440 while (attrs[num_attrs])
444 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Freeing attributes\n");
445 /* Free the attributes */
446 for (i = 0; i < num_attrs; ++i) {
447 if (attrs[i] != NULL) {
449 /* First, free the value strings */
450 if (attrs[i]->mod_values != NULL) {
452 attrs[i]->mod_values[j] != NULL;
454 free(attrs[i]->mod_values[j]);
458 /* Free the value strings pointer list */
459 if (attrs[i]->mod_values != NULL) {
460 free(attrs[i]->mod_values);
463 /* Now free the LDAPMod struct itself. */
475 * Delete a record from the LDAP
477 * parameter object not used here, present for hook interface compatability
479 int delete_from_ldap(char *cn, char *ou, void **object)
485 if (dirserver == NULL)
490 sprintf(this_dn, "%s,%s", cn, config.c_ldap_base_dn);
492 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Calling ldap_delete_s()\n");
494 begin_critical_section(S_LDAP);
495 if (connect_to_ldap()) {
496 end_critical_section(S_LDAP);
500 i = ldap_delete_s(dirserver, this_dn);
502 if (i == LDAP_SERVER_DOWN) {
503 end_critical_section(S_LDAP);
505 ("The LDAP server appears to be down.\nThe delete from LDAP did not occurr.\n",
506 "LDAP: delete failed");
510 if (i != LDAP_SUCCESS) {
511 CtdlLogPrintf(CTDL_ERR,
512 "LDAP: ldap_delete_s() failed: %s (%d)\n",
513 ldap_err2string(i), i);
514 end_critical_section(S_LDAP);
515 CtdlAideMessage(ldap_err2string(i), "LDAP: delete failed");
518 end_critical_section(S_LDAP);
525 void ldap_disconnect_timer(void)
527 begin_critical_section(S_LDAP);
528 if (ldap_time_disconnect) {
529 ldap_time_disconnect--;
530 end_critical_section(S_LDAP);
534 end_critical_section(S_LDAP);
538 #endif /* HAVE_LDAP */
542 * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
544 CTDL_MODULE_INIT(ldap)
549 if (!IsEmptyStr(config.c_ldap_base_dn)) {
550 CtdlRegisterCleanupHook(serv_ldap_cleanup);
551 CtdlRegisterSessionHook(ldap_disconnect_timer, EVT_TIMER);
552 CtdlRegisterDirectoryServiceFunc(delete_from_ldap,
555 CtdlRegisterDirectoryServiceFunc(create_ldap_host_OU,
556 DIRECTORY_CREATE_HOST,
558 CtdlRegisterDirectoryServiceFunc(create_ldap_object,
559 DIRECTORY_CREATE_OBJECT,
561 CtdlRegisterDirectoryServiceFunc(add_ldap_object,
562 DIRECTORY_ATTRIB_ADD,
564 CtdlRegisterDirectoryServiceFunc(save_ldap_object,
565 DIRECTORY_SAVE_OBJECT,
567 CtdlRegisterDirectoryServiceFunc(free_ldap_object,
568 DIRECTORY_FREE_OBJECT,
572 #endif /* HAVE_LDAP */
575 /* return our Subversion id for the Log */