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>
34 #include "citserver.h"
40 #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),
107 "LDAP: Could not connect to server.");
111 ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION,
114 CtdlLogPrintf(CTDL_INFO, "LDAP: Binding to %s\n", config.c_ldap_bind_dn);
116 i = ldap_simple_bind_s(dirserver,
117 config.c_ldap_bind_dn,
118 config.c_ldap_bind_pw);
119 if (i != LDAP_SUCCESS) {
120 CtdlLogPrintf(CTDL_CRIT, "LDAP: Cannot bind: %s (%d)\n",
121 ldap_err2string(i), i);
122 dirserver = NULL; /* FIXME disconnect from ldap */
123 CtdlAideMessage(ldap_err2string(i),
124 "LDAP: Cannot bind to server");
127 ldap_time_disconnect = 1;
134 * Create the root node. If it's already there, so what?
136 void create_ldap_root(void)
139 char *objectClass_values[3];
140 LDAPMod dc, objectClass;
145 /* We just want the top-level dc, not the whole hierarchy */
146 strcpy(topdc, config.c_ldap_base_dn);
147 for (i = 0; topdc[i]; ++i) {
148 if (topdc[i] == ',') {
153 for (i = 0; topdc[i]; ++i) {
155 strcpy(topdc, &topdc[i + 1]);
158 /* Set up the transaction */
159 dc.mod_op = LDAP_MOD_ADD;
161 dc_values[0] = topdc;
163 dc.mod_values = dc_values;
164 objectClass.mod_op = LDAP_MOD_ADD;
165 objectClass.mod_type = "objectClass";
166 objectClass_values[0] = "top";
167 objectClass_values[1] = "domain";
168 objectClass_values[2] = NULL;
169 objectClass.mod_values = objectClass_values;
171 mods[1] = &objectClass;
174 /* Perform the transaction */
175 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Setting up Base DN node...\n");
177 begin_critical_section(S_LDAP);
178 if (connect_to_ldap()) {
179 end_critical_section(S_LDAP);
182 i = ldap_add_s(dirserver, config.c_ldap_base_dn, mods);
183 end_critical_section(S_LDAP);
185 if (i == LDAP_ALREADY_EXISTS) {
186 CtdlLogPrintf(CTDL_INFO,
187 "LDAP: Base DN is already present in the directory; no need to add it again.\n");
188 } else if (i != LDAP_SUCCESS) {
189 CtdlLogPrintf(CTDL_CRIT, "LDAP: ldap_add_s() failed: %s (%d)\n",
190 ldap_err2string(i), i);
196 * Create an OU node representing a Citadel host.
197 * parameter cn is not used, its just there to keep the hook interface consistant
198 * parameter object not used here, present for interface compatability
200 int create_ldap_host_OU(char *cn, char *host, void **object)
203 char *objectClass_values[3];
204 LDAPMod dc, objectClass;
209 /* The DN is this OU plus the base. */
210 snprintf(dn, sizeof dn, "ou=%s,%s", host, config.c_ldap_base_dn);
212 /* Set up the transaction */
213 dc.mod_op = LDAP_MOD_ADD;
217 dc.mod_values = dc_values;
218 objectClass.mod_op = LDAP_MOD_ADD;
219 objectClass.mod_type = "objectClass";
220 objectClass_values[0] = "top";
221 objectClass_values[1] = "organizationalUnit";
222 objectClass_values[2] = NULL;
223 objectClass.mod_values = objectClass_values;
225 mods[1] = &objectClass;
228 /* Perform the transaction */
229 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Setting up Host OU node...\n");
231 begin_critical_section(S_LDAP);
232 if (connect_to_ldap()) {
233 end_critical_section(S_LDAP);
236 i = ldap_add_s(dirserver, dn, mods);
237 end_critical_section(S_LDAP);
239 if (i == LDAP_ALREADY_EXISTS) {
240 CtdlLogPrintf(CTDL_INFO,
241 "LDAP: Host OU is already present in the directory; no need to add it again.\n");
242 } else if (i != LDAP_SUCCESS) {
243 CtdlLogPrintf(CTDL_CRIT, "LDAP: ldap_add_s() failed: %s (%d)\n",
244 ldap_err2string(i), i);
256 * Create a base LDAP object for the interface
259 int create_ldap_object(char *cn, char *ou, void **object)
261 // We do nothing here, this just gets the base structure created by the interface.
262 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Created ldap object\n");
268 * Add an attribute to the ldap object
271 int add_ldap_object(char *cn, char *ou, void **object)
279 CtdlLogPrintf(CTDL_DEBUG,
280 "LDAP: Adding ldap attribute name:\"%s\" value:\"%s\"\n",
285 while (attrs[num_attrs])
289 for (cur_attr = 0; cur_attr < num_attrs; cur_attr++) {
290 if (!strcmp(attrs[cur_attr]->mod_type, cn)) { // Adding a value to the attribute
291 if (attrs[cur_attr]->mod_values) {
292 while (attrs[cur_attr]->
293 mod_values[num_values]) {
297 mod_values[num_values])) {
298 CtdlLogPrintf(CTDL_DEBUG,
299 "LDAP: Ignoring duplicate attribute/value pair\n");
305 attrs[cur_attr]->mod_values =
306 realloc(attrs[cur_attr]->mod_values,
307 (num_values + 2) * (sizeof(char *)));
308 attrs[cur_attr]->mod_values[num_values] =
310 attrs[cur_attr]->mod_values[num_values + 1] = NULL;
316 realloc(attrs, (sizeof(LDAPMod *)) * (num_attrs + 2));
318 attrs = malloc((sizeof(LDAPMod *)) * (num_attrs + 2));
319 attrs[num_attrs] = malloc(sizeof(LDAPMod));
320 memset(attrs[num_attrs], 0, sizeof(LDAPMod));
321 attrs[num_attrs + 1] = NULL;
322 attrs[num_attrs]->mod_op = LDAP_MOD_ADD;
323 attrs[num_attrs]->mod_type = strdup(cn);
324 attrs[num_attrs]->mod_values = malloc(2 * sizeof(char *));
325 attrs[num_attrs]->mod_values[0] = strdup(ou);
326 attrs[num_attrs]->mod_values[1] = NULL;
333 * SAve the object to the LDAP server
335 int save_ldap_object(char *cn, char *ou, void **object)
344 if (dirserver == NULL)
349 sprintf(this_dn, "%s,%s", cn, config.c_ldap_base_dn);
351 CtdlLogPrintf(CTDL_INFO, "LDAP: Calling ldap_add_s() for dn of '%s'\n",
354 /* The last attribute must be a NULL one. */
355 attrs = (LDAPMod **) * object;
357 while (attrs[num_attrs]) {
359 while (attrs[num_attrs]->mod_values[count]) {
360 CtdlLogPrintf(CTDL_DEBUG,
361 "LDAP: attribute %d, value %d = \'%s=%s\'\n",
363 attrs[num_attrs]->mod_type,
371 CtdlLogPrintf(CTDL_ERR,
372 "LDAP: no attributes in save_ldap_object\n");
376 begin_critical_section(S_LDAP);
377 if (connect_to_ldap()) {
378 end_critical_section(S_LDAP);
382 i = ldap_add_s(dirserver, this_dn, attrs);
384 if (i == LDAP_SERVER_DOWN) {
386 ("The LDAP server appears to be down.\nThe save to LDAP did not occurr.\n",
387 "LDAP: save failed");
388 end_critical_section(S_LDAP);
392 /* If the entry already exists, repopulate it instead */
393 /* repopulating doesn't work as Citadel may want some attributes to be deleted.
394 * we have no way of knowing which attributes to delete and LDAP won't work it out for us
395 * so now we delete the old entry and create a new one.
397 if (i == LDAP_ALREADY_EXISTS) {
398 end_critical_section(S_LDAP);
399 CtdlLogPrintf(CTDL_INFO,
400 "LDAP: Create, already exists, deleteing first.\n");
401 if (delete_from_ldap(cn, ou, NULL))
403 begin_critical_section(S_LDAP);
404 CtdlLogPrintf(CTDL_INFO,
405 "LDAP: Calling ldap_add_s() to recreate for dn of '%s'\n",
407 i = ldap_add_s(dirserver, this_dn, attrs);
410 if (i != LDAP_SUCCESS) {
411 CtdlLogPrintf(CTDL_ERR, "LDAP: ldap_add_s() failed: %s (%d)\n",
412 ldap_err2string(i), i);
414 ("The LDAP server refused the save command.\nDid you update the schema?\n",
415 "LDAP: save failed (schema?)");
416 end_critical_section(S_LDAP);
419 end_critical_section(S_LDAP);
427 int free_ldap_object(char *cn, char *ou, void **object)
434 attrs = (LDAPMod **) * object;
436 while (attrs[num_attrs])
440 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Freeing attributes\n");
441 /* Free the attributes */
442 for (i = 0; i < num_attrs; ++i) {
443 if (attrs[i] != NULL) {
445 /* First, free the value strings */
446 if (attrs[i]->mod_values != NULL) {
448 attrs[i]->mod_values[j] != NULL;
450 free(attrs[i]->mod_values[j]);
454 /* Free the value strings pointer list */
455 if (attrs[i]->mod_values != NULL) {
456 free(attrs[i]->mod_values);
459 /* Now free the LDAPMod struct itself. */
471 * Delete a record from the LDAP
473 * parameter object not used here, present for hook interface compatability
475 int delete_from_ldap(char *cn, char *ou, void **object)
481 if (dirserver == NULL)
486 sprintf(this_dn, "%s,%s", cn, config.c_ldap_base_dn);
488 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Calling ldap_delete_s()\n");
490 begin_critical_section(S_LDAP);
491 if (connect_to_ldap()) {
492 end_critical_section(S_LDAP);
496 i = ldap_delete_s(dirserver, this_dn);
498 if (i == LDAP_SERVER_DOWN) {
499 end_critical_section(S_LDAP);
501 ("The LDAP server appears to be down.\nThe delete from LDAP did not occurr.\n",
502 "LDAP: delete failed");
506 if (i != LDAP_SUCCESS) {
507 CtdlLogPrintf(CTDL_ERR,
508 "LDAP: ldap_delete_s() failed: %s (%d)\n",
509 ldap_err2string(i), i);
510 end_critical_section(S_LDAP);
511 CtdlAideMessage(ldap_err2string(i), "LDAP: delete failed");
514 end_critical_section(S_LDAP);
521 void ldap_disconnect_timer(void)
523 begin_critical_section(S_LDAP);
524 if (ldap_time_disconnect) {
525 ldap_time_disconnect--;
526 end_critical_section(S_LDAP);
530 end_critical_section(S_LDAP);
534 #endif /* HAVE_LDAP */
538 * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
540 CTDL_MODULE_INIT(ldap)
543 if (!IsEmptyStr(config.c_ldap_base_dn)) {
544 CtdlRegisterCleanupHook(serv_ldap_cleanup);
545 CtdlRegisterSessionHook(ldap_disconnect_timer, EVT_TIMER);
546 CtdlRegisterDirectoryServiceFunc(delete_from_ldap,
549 CtdlRegisterDirectoryServiceFunc(create_ldap_host_OU,
550 DIRECTORY_CREATE_HOST,
552 CtdlRegisterDirectoryServiceFunc(create_ldap_object,
553 DIRECTORY_CREATE_OBJECT,
555 CtdlRegisterDirectoryServiceFunc(add_ldap_object,
556 DIRECTORY_ATTRIB_ADD,
558 CtdlRegisterDirectoryServiceFunc(save_ldap_object,
559 DIRECTORY_SAVE_OBJECT,
561 CtdlRegisterDirectoryServiceFunc(free_ldap_object,
562 DIRECTORY_FREE_OBJECT,
566 #endif /* HAVE_LDAP */
568 /* return our Subversion id for the Log */