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 "sysdep_decls.h"
35 #include "citserver.h"
38 #include "serv_extensions.h"
43 #include "serv_ldap.h"
51 LDAP *dirserver = NULL;
54 * LDAP connector cleanup function
56 void serv_ldap_cleanup(void)
58 if (!dirserver) return;
60 lprintf(CTDL_INFO, "Unbinding from directory server\n");
61 ldap_unbind(dirserver);
68 * Create the root node. If it's already there, so what?
70 void CtdlCreateLdapRoot(void) {
72 char *objectClass_values[3];
73 LDAPMod dc, objectClass;
78 /* We just want the top-level dc, not the whole hierarchy */
79 strcpy(topdc, config.c_ldap_base_dn);
80 for (i=0; i<strlen(topdc); ++i) {
81 if (topdc[i] == ',') topdc[i] = 0;
83 for (i=0; i<strlen(topdc); ++i) {
84 if (topdc[i] == '=') strcpy(topdc, &topdc[i+1]);
87 /* Set up the transaction */
88 dc.mod_op = LDAP_MOD_ADD;
92 dc.mod_values = dc_values;
93 objectClass.mod_op = LDAP_MOD_ADD;
94 objectClass.mod_type = "objectClass";
95 objectClass_values[0] = "top";
96 objectClass_values[1] = "domain";
97 objectClass_values[2] = NULL;
98 objectClass.mod_values = objectClass_values;
100 mods[1] = &objectClass;
103 /* Perform the transaction */
104 lprintf(CTDL_DEBUG, "Setting up Base DN node...\n");
105 begin_critical_section(S_LDAP);
106 i = ldap_add_s(dirserver, config.c_ldap_base_dn, mods);
107 end_critical_section(S_LDAP);
109 if (i != LDAP_SUCCESS) {
110 lprintf(CTDL_CRIT, "ldap_add_s() failed: %s (%d)\n",
111 ldap_err2string(i), i);
117 * Create an OU node representing a Citadel host.
119 void CtdlCreateHostOU(char *host) {
121 char *objectClass_values[3];
122 LDAPMod dc, objectClass;
127 /* The DN is this OU plus the base. */
128 snprintf(dn, sizeof dn, "ou=%s,%s", host, config.c_ldap_base_dn);
130 /* Set up the transaction */
131 dc.mod_op = LDAP_MOD_ADD;
135 dc.mod_values = dc_values;
136 objectClass.mod_op = LDAP_MOD_ADD;
137 objectClass.mod_type = "objectClass";
138 objectClass_values[0] = "top";
139 objectClass_values[1] = "organizationalUnit";
140 objectClass_values[2] = NULL;
141 objectClass.mod_values = objectClass_values;
143 mods[1] = &objectClass;
146 /* Perform the transaction */
147 lprintf(CTDL_DEBUG, "Setting up Host OU node...\n");
148 begin_critical_section(S_LDAP);
149 i = ldap_add_s(dirserver, dn, mods);
150 end_critical_section(S_LDAP);
152 /* ignore the error -- it's ok if it already exists
153 if (i != LDAP_SUCCESS) {
154 lprintf(CTDL_ERR, "ldap_add_s() failed: %s (%d)\n",
155 ldap_err2string(i), i);
167 void CtdlConnectToLdap(void) {
169 int ldap_version = 3;
171 lprintf(CTDL_INFO, "Connecting to LDAP server %s:%d...\n",
172 config.c_ldap_host, config.c_ldap_port);
174 dirserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
175 if (dirserver == NULL) {
176 lprintf(CTDL_CRIT, "Could not connect to %s:%d : %s\n",
183 ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
185 lprintf(CTDL_INFO, "Binding to %s\n", config.c_ldap_bind_dn);
187 i = ldap_simple_bind_s(dirserver,
188 config.c_ldap_bind_dn,
189 config.c_ldap_bind_pw
191 if (i != LDAP_SUCCESS) {
192 lprintf(CTDL_CRIT, "Cannot bind: %s (%d)\n", ldap_err2string(i), i);
193 dirserver = NULL; /* FIXME disconnect from ldap */
197 CtdlCreateLdapRoot();
202 * vCard-to-LDAP conversions.
204 * If 'op' is set to V2L_WRITE, then write
205 * (add, or change if already exists) a directory entry to the
206 * LDAP server, based on the information supplied in a vCard.
208 * If 'op' is set to V2L_DELETE, then delete the entry from LDAP.
210 void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
211 struct vCard *v = NULL;
214 LDAPMod **attrs = NULL;
217 int alias_attr = (-1);
219 int phone_attr = (-1);
231 if (dirserver == NULL) return;
232 if (msg == NULL) return;
233 if (msg->cm_fields['M'] == NULL) return;
234 if (msg->cm_fields['A'] == NULL) return;
235 if (msg->cm_fields['N'] == NULL) return;
237 /* Initialize variables */
238 strcpy(givenname, "");
241 sprintf(this_dn, "cn=%s,ou=%s,%s",
244 config.c_ldap_base_dn
247 sprintf(uid, "%s@%s",
252 /* Are we just deleting? If so, it's simple... */
253 if (op == V2L_DELETE) {
254 lprintf(CTDL_DEBUG, "Calling ldap_delete_s()\n");
255 begin_critical_section(S_LDAP);
256 i = ldap_delete_s(dirserver, this_dn);
257 end_critical_section(S_LDAP);
258 if (i != LDAP_SUCCESS) {
259 lprintf(CTDL_ERR, "ldap_delete_s() failed: %s (%d)\n",
260 ldap_err2string(i), i);
266 * If we get to this point then it must be a V2L_WRITE operation.
269 /* First make sure the OU for the user's home Citadel host is created */
270 CtdlCreateHostOU(msg->cm_fields['N']);
272 /* The first LDAP attribute will be an 'objectclass' list. Citadel
273 * doesn't do anything with this. It's just there for compatibility
277 attrs = mallok( (sizeof(LDAPMod *) * num_attrs) );
278 attrs[0] = mallok(sizeof(LDAPMod));
279 memset(attrs[0], 0, sizeof(LDAPMod));
280 attrs[0]->mod_op = LDAP_MOD_ADD;
281 attrs[0]->mod_type = "objectclass";
282 attrs[0]->mod_values = mallok(3 * sizeof(char *));
283 attrs[0]->mod_values[0] = strdoop("inetOrgPerson");
284 attrs[0]->mod_values[1] = NULL;
286 /* Convert the vCard fields to LDAP properties */
287 v = vcard_load(msg->cm_fields['M']);
288 if (v->numprops) for (i=0; i<(v->numprops); ++i) if (striplt(v->prop[i].value), strlen(v->prop[i].value) > 0) {
290 if (!strcasecmp(v->prop[i].name, "n")) {
291 extract_token(sn, v->prop[i].value, 0, ';');
292 extract_token(givenname, v->prop[i].value, 1, ';');
295 if (!strcasecmp(v->prop[i].name, "fn")) {
296 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
297 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
298 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
299 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
300 attrs[num_attrs-1]->mod_type = "cn";
301 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
302 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
303 attrs[num_attrs-1]->mod_values[1] = NULL;
307 if (!strcasecmp(v->prop[i].name, "title")) {
308 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
309 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
310 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
311 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
312 attrs[num_attrs-1]->mod_type = "title";
313 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
314 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
315 attrs[num_attrs-1]->mod_values[1] = NULL;
318 if (!strcasecmp(v->prop[i].name, "org")) {
319 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
320 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
321 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
322 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
323 attrs[num_attrs-1]->mod_type = "o";
324 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
325 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
326 attrs[num_attrs-1]->mod_values[1] = NULL;
329 if ( (!strcasecmp(v->prop[i].name, "adr"))
330 ||(!strncasecmp(v->prop[i].name, "adr;", 4)) ) {
331 /* Unfortunately, we can only do a single address */
335 extract_token(&street[strlen(street)],
336 v->prop[i].value, 0, ';'); /* po box */
338 extract_token(&street[strlen(street)],
339 v->prop[i].value, 1, ';'); /* extend addr */
341 extract_token(&street[strlen(street)],
342 v->prop[i].value, 2, ';'); /* street */
344 extract_token(city, v->prop[i].value, 3, ';');
345 extract_token(state, v->prop[i].value, 4, ';');
346 extract_token(zipcode, v->prop[i].value, 5, ';');
348 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
349 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
350 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
351 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
352 attrs[num_attrs-1]->mod_type = "street";
353 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
354 attrs[num_attrs-1]->mod_values[0] = strdoop(street);
355 attrs[num_attrs-1]->mod_values[1] = NULL;
357 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
358 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
359 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
360 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
361 attrs[num_attrs-1]->mod_type = "l";
362 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
363 attrs[num_attrs-1]->mod_values[0] = strdoop(city);
364 attrs[num_attrs-1]->mod_values[1] = NULL;
366 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
367 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
368 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
369 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
370 attrs[num_attrs-1]->mod_type = "st";
371 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
372 attrs[num_attrs-1]->mod_values[0] = strdoop(state);
373 attrs[num_attrs-1]->mod_values[1] = NULL;
375 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
376 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
377 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
378 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
379 attrs[num_attrs-1]->mod_type = "postalcode";
380 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
381 attrs[num_attrs-1]->mod_values[0] = strdoop(zipcode);
382 attrs[num_attrs-1]->mod_values[1] = NULL;
386 if ( (!strcasecmp(v->prop[i].name, "tel"))
387 ||(!strncasecmp(v->prop[i].name, "tel;", 4)) ) {
389 /* The first 'tel' property creates the 'telephoneNumber' attribute */
390 if (num_phones == 1) {
391 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
392 phone_attr = num_attrs-1;
393 attrs[phone_attr] = mallok(sizeof(LDAPMod));
394 memset(attrs[phone_attr], 0, sizeof(LDAPMod));
395 attrs[phone_attr]->mod_op = LDAP_MOD_ADD;
396 attrs[phone_attr]->mod_type = "telephoneNumber";
397 attrs[phone_attr]->mod_values = mallok(2 * sizeof(char *));
398 attrs[phone_attr]->mod_values[0] = strdoop(v->prop[i].value);
399 attrs[phone_attr]->mod_values[1] = NULL;
401 /* Subsequent 'tel' properties *add to* the 'telephoneNumber' attribute */
403 attrs[phone_attr]->mod_values = reallok(attrs[phone_attr]->mod_values,
404 num_phones * sizeof(char *));
405 attrs[phone_attr]->mod_values[num_phones-1]
406 = strdoop(v->prop[i].value);
407 attrs[phone_attr]->mod_values[num_phones]
413 if ( (!strcasecmp(v->prop[i].name, "email"))
414 ||(!strcasecmp(v->prop[i].name, "email;internet")) ) {
417 lprintf(CTDL_DEBUG, "email addr %d\n", num_emails);
419 /* The first email address creates the 'mail' attribute */
420 if (num_emails == 1) {
421 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
422 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
423 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
424 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
425 attrs[num_attrs-1]->mod_type = "mail";
426 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
427 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
428 attrs[num_attrs-1]->mod_values[1] = NULL;
430 /* The second email address creates the 'alias' attribute */
431 else if (num_emails == 2) {
432 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
433 alias_attr = num_attrs-1;
434 attrs[alias_attr] = mallok(sizeof(LDAPMod));
435 memset(attrs[alias_attr], 0, sizeof(LDAPMod));
436 attrs[alias_attr]->mod_op = LDAP_MOD_ADD;
437 attrs[alias_attr]->mod_type = "alias";
438 attrs[alias_attr]->mod_values = mallok(2 * sizeof(char *));
439 attrs[alias_attr]->mod_values[0] = strdoop(v->prop[i].value);
440 attrs[alias_attr]->mod_values[1] = NULL;
442 /* Subsequent email addresses *add to* the 'alias' attribute */
443 else if (num_emails > 2) {
444 attrs[alias_attr]->mod_values = reallok(attrs[alias_attr]->mod_values,
445 num_emails * sizeof(char *));
446 attrs[alias_attr]->mod_values[num_emails-2]
447 = strdoop(v->prop[i].value);
448 attrs[alias_attr]->mod_values[num_emails-1]
456 vcard_free(v); /* Don't need this anymore. */
458 /* "sn" (surname) based on info in vCard */
459 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
460 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
461 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
462 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
463 attrs[num_attrs-1]->mod_type = "sn";
464 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
465 attrs[num_attrs-1]->mod_values[0] = strdoop(sn);
466 attrs[num_attrs-1]->mod_values[1] = NULL;
468 /* "givenname" (first name) based on info in vCard */
469 if (strlen(givenname) == 0) strcpy(givenname, "_");
470 if (strlen(sn) == 0) strcpy(sn, "_");
471 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
472 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
473 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
474 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
475 attrs[num_attrs-1]->mod_type = "givenname";
476 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
477 attrs[num_attrs-1]->mod_values[0] = strdoop(givenname);
478 attrs[num_attrs-1]->mod_values[1] = NULL;
480 /* "uid" is a Kolab compatibility thing. We just do cituser@citnode */
481 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
482 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
483 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
484 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
485 attrs[num_attrs-1]->mod_type = "uid";
486 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
487 attrs[num_attrs-1]->mod_values[0] = strdoop(uid);
488 attrs[num_attrs-1]->mod_values[1] = NULL;
490 /* Add a "cn" (Common Name) attribute based on the user's screen name,
491 * but only there was no 'fn' (full name) property in the vCard
494 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
495 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
496 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
497 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
498 attrs[num_attrs-1]->mod_type = "cn";
499 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
500 attrs[num_attrs-1]->mod_values[0] = strdoop(msg->cm_fields['A']);
501 attrs[num_attrs-1]->mod_values[1] = NULL;
504 /* The last attribute must be a NULL one. */
505 attrs = realloc(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
506 attrs[num_attrs - 1] = NULL;
508 lprintf(CTDL_DEBUG, "Calling ldap_add_s()\n");
509 begin_critical_section(S_LDAP);
510 i = ldap_add_s(dirserver, this_dn, attrs);
511 end_critical_section(S_LDAP);
513 /* If the entry already exists, repopulate it instead */
514 if (i == LDAP_ALREADY_EXISTS) {
515 for (j=0; j<(num_attrs-1); ++j) {
516 attrs[j]->mod_op = LDAP_MOD_REPLACE;
518 lprintf(CTDL_DEBUG, "Calling ldap_modify_s()\n");
519 begin_critical_section(S_LDAP);
520 i = ldap_modify_s(dirserver, this_dn, attrs);
521 end_critical_section(S_LDAP);
524 if (i != LDAP_SUCCESS) {
525 lprintf(CTDL_ERR, "ldap_add_s() failed: %s (%d)\n",
526 ldap_err2string(i), i);
529 lprintf(CTDL_DEBUG, "Freeing attributes\n");
530 /* Free the attributes */
531 for (i=0; i<num_attrs; ++i) {
532 if (attrs[i] != NULL) {
534 /* First, free the value strings */
535 if (attrs[i]->mod_values != NULL) {
536 for (j=0; attrs[i]->mod_values[j] != NULL; ++j) {
537 phree(attrs[i]->mod_values[j]);
541 /* Free the value strings pointer list */
542 if (attrs[i]->mod_values != NULL) {
543 phree(attrs[i]->mod_values);
546 /* Now free the LDAPMod struct itself. */
551 lprintf(CTDL_DEBUG, "LDAP write operation complete.\n");
555 #endif /* HAVE_LDAP */
559 * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
561 char *serv_ldap_init(void)
564 CtdlRegisterCleanupHook(serv_ldap_cleanup);
566 if (strlen(config.c_ldap_host) > 0) {
570 #endif /* HAVE_LDAP */