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(7, "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(9, "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(3, "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(9, "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(3, "ldap_add_s() failed: %s (%d)\n",
155 ldap_err2string(i), i);
167 void CtdlConnectToLdap(void) {
169 int ldap_version = 3;
171 lprintf(7, "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(3, "Could not connect to %s:%d : %s\n",
183 ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
185 lprintf(7, "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(3, "Cannot bind: %s (%d)\n", ldap_err2string(i), i);
193 dirserver = NULL; /* FIXME disconnect from ldap */
197 CtdlCreateLdapRoot();
203 * Write (add, or change if already exists) a directory entry to the
204 * LDAP server, based on the information supplied in a vCard.
206 void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
207 struct vCard *v = NULL;
210 LDAPMod **attrs = NULL;
213 int alias_attr = (-1);
215 int phone_attr = (-1);
227 if (dirserver == NULL) return;
228 if (msg == NULL) return;
229 if (msg->cm_fields['M'] == NULL) return;
230 if (msg->cm_fields['A'] == NULL) return;
231 if (msg->cm_fields['N'] == NULL) return;
233 /* First make sure the OU for the user's home Citadel host is created */
234 CtdlCreateHostOU(msg->cm_fields['N']);
236 /* Initialize variables */
237 strcpy(givenname, "_");
240 sprintf(this_dn, "cn=%s,ou=%s,%s",
243 config.c_ldap_base_dn
246 sprintf(uid, "%s@%s",
251 /* The first LDAP attribute will be an 'objectclass' list. Citadel
252 * doesn't do anything with this. It's just there for compatibility
256 attrs = mallok( (sizeof(LDAPMod *) * num_attrs) );
257 attrs[0] = mallok(sizeof(LDAPMod));
258 memset(attrs[0], 0, sizeof(LDAPMod));
259 attrs[0]->mod_op = LDAP_MOD_ADD;
260 attrs[0]->mod_type = "objectclass";
261 attrs[0]->mod_values = mallok(2 * sizeof(char *));
262 attrs[0]->mod_values[0] = strdoop("inetOrgPerson");
264 attrs[0]->mod_values[1] = strdoop("organizationalPerson");
265 attrs[0]->mod_values[2] = strdoop("person");
266 attrs[0]->mod_values[3] = strdoop("Top");
268 attrs[0]->mod_values[1] = NULL;
270 /* Convert the vCard fields to LDAP properties */
271 v = vcard_load(msg->cm_fields['M']);
272 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
274 if (!strcasecmp(v->prop[i].name, "n")) {
275 extract_token(sn, v->prop[i].value, 0, ';');
276 extract_token(givenname, v->prop[i].value, 1, ';');
279 if (!strcasecmp(v->prop[i].name, "fn")) {
280 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
281 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
282 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
283 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
284 attrs[num_attrs-1]->mod_type = "cn";
285 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
286 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
287 attrs[num_attrs-1]->mod_values[1] = NULL;
291 if (!strcasecmp(v->prop[i].name, "title")) {
292 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
293 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
294 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
295 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
296 attrs[num_attrs-1]->mod_type = "title";
297 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
298 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
299 attrs[num_attrs-1]->mod_values[1] = NULL;
302 if (!strcasecmp(v->prop[i].name, "org")) {
303 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
304 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
305 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
306 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
307 attrs[num_attrs-1]->mod_type = "o";
308 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
309 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
310 attrs[num_attrs-1]->mod_values[1] = NULL;
313 if ( (!strcasecmp(v->prop[i].name, "adr"))
314 ||(!strncasecmp(v->prop[i].name, "adr;", 4)) ) {
315 /* Unfortunately, we can only do a single address */
319 extract_token(&street[strlen(street)],
320 v->prop[i].value, 0, ';'); /* po box */
322 extract_token(&street[strlen(street)],
323 v->prop[i].value, 1, ';'); /* extend addr */
325 extract_token(&street[strlen(street)],
326 v->prop[i].value, 2, ';'); /* street */
328 extract_token(city, v->prop[i].value, 3, ';');
329 extract_token(state, v->prop[i].value, 4, ';');
330 extract_token(zipcode, v->prop[i].value, 5, ';');
332 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
333 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
334 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
335 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
336 attrs[num_attrs-1]->mod_type = "street";
337 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
338 attrs[num_attrs-1]->mod_values[0] = strdoop(street);
339 attrs[num_attrs-1]->mod_values[1] = NULL;
341 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
342 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
343 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
344 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
345 attrs[num_attrs-1]->mod_type = "l";
346 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
347 attrs[num_attrs-1]->mod_values[0] = strdoop(city);
348 attrs[num_attrs-1]->mod_values[1] = NULL;
350 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
351 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
352 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
353 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
354 attrs[num_attrs-1]->mod_type = "st";
355 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
356 attrs[num_attrs-1]->mod_values[0] = strdoop(state);
357 attrs[num_attrs-1]->mod_values[1] = NULL;
359 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
360 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
361 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
362 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
363 attrs[num_attrs-1]->mod_type = "postalcode";
364 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
365 attrs[num_attrs-1]->mod_values[0] = strdoop(zipcode);
366 attrs[num_attrs-1]->mod_values[1] = NULL;
370 if ( (!strcasecmp(v->prop[i].name, "tel"))
371 ||(!strncasecmp(v->prop[i].name, "tel;", 4)) ) {
373 /* The first 'tel' property creates the 'telephoneNumber' attribute */
374 if (num_phones == 1) {
375 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
376 phone_attr = num_attrs-1;
377 attrs[phone_attr] = mallok(sizeof(LDAPMod));
378 memset(attrs[phone_attr], 0, sizeof(LDAPMod));
379 attrs[phone_attr]->mod_op = LDAP_MOD_ADD;
380 attrs[phone_attr]->mod_type = "telephoneNumber";
381 attrs[phone_attr]->mod_values = mallok(2 * sizeof(char *));
382 attrs[phone_attr]->mod_values[0] = strdoop(v->prop[i].value);
383 attrs[phone_attr]->mod_values[1] = NULL;
385 /* Subsequent 'tel' properties *add to* the 'telephoneNumber' attribute */
387 attrs[phone_attr]->mod_values = reallok(attrs[phone_attr]->mod_values,
388 num_phones * sizeof(char *));
389 attrs[phone_attr]->mod_values[num_phones-1]
390 = strdoop(v->prop[i].value);
391 attrs[phone_attr]->mod_values[num_phones]
397 if ( (!strcasecmp(v->prop[i].name, "email"))
398 ||(!strcasecmp(v->prop[i].name, "email;internet")) ) {
401 lprintf(9, "email addr %d\n", num_emails);
403 /* The first email address creates the 'mail' attribute */
404 if (num_emails == 1) {
405 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
406 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
407 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
408 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
409 attrs[num_attrs-1]->mod_type = "mail";
410 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
411 attrs[num_attrs-1]->mod_values[0] = strdoop(v->prop[i].value);
412 attrs[num_attrs-1]->mod_values[1] = NULL;
414 /* The second email address creates the 'alias' attribute */
415 else if (num_emails == 2) {
416 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
417 alias_attr = num_attrs-1;
418 attrs[alias_attr] = mallok(sizeof(LDAPMod));
419 memset(attrs[alias_attr], 0, sizeof(LDAPMod));
420 attrs[alias_attr]->mod_op = LDAP_MOD_ADD;
421 attrs[alias_attr]->mod_type = "alias";
422 attrs[alias_attr]->mod_values = mallok(2 * sizeof(char *));
423 attrs[alias_attr]->mod_values[0] = strdoop(v->prop[i].value);
424 attrs[alias_attr]->mod_values[1] = NULL;
426 /* Subsequent email addresses *add to* the 'alias' attribute */
427 else if (num_emails > 2) {
428 attrs[alias_attr]->mod_values = reallok(attrs[alias_attr]->mod_values,
429 num_emails * sizeof(char *));
430 attrs[alias_attr]->mod_values[num_emails-2]
431 = strdoop(v->prop[i].value);
432 attrs[alias_attr]->mod_values[num_emails-1]
440 vcard_free(v); /* Don't need this anymore. */
442 /* "sn" (surname) based on info in vCard */
443 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
444 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
445 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
446 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
447 attrs[num_attrs-1]->mod_type = "sn";
448 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
449 attrs[num_attrs-1]->mod_values[0] = strdoop(sn);
450 attrs[num_attrs-1]->mod_values[1] = NULL;
452 /* "givenname" (first name) based on info in vCard */
453 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
454 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
455 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
456 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
457 attrs[num_attrs-1]->mod_type = "givenname";
458 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
459 attrs[num_attrs-1]->mod_values[0] = strdoop(givenname);
460 attrs[num_attrs-1]->mod_values[1] = NULL;
462 /* "uid" is a Kolab compatibility thing. We just do cituser@citnode */
463 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
464 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
465 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
466 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
467 attrs[num_attrs-1]->mod_type = "uid";
468 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
469 attrs[num_attrs-1]->mod_values[0] = strdoop(uid);
470 attrs[num_attrs-1]->mod_values[1] = NULL;
472 /* Add a "cn" (Common Name) attribute based on the user's screen name,
473 * but only there was no 'fn' (full name) property in the vCard
476 attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
477 attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
478 memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
479 attrs[num_attrs-1]->mod_op = LDAP_MOD_ADD;
480 attrs[num_attrs-1]->mod_type = "cn";
481 attrs[num_attrs-1]->mod_values = mallok(2 * sizeof(char *));
482 attrs[num_attrs-1]->mod_values[0] = strdoop(msg->cm_fields['A']);
483 attrs[num_attrs-1]->mod_values[1] = NULL;
486 /* The last attribute must be a NULL one. */
487 attrs = realloc(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
488 attrs[num_attrs - 1] = NULL;
490 lprintf(9, "Calling ldap_add_s()\n");
491 begin_critical_section(S_LDAP);
492 i = ldap_add_s(dirserver, this_dn, attrs);
493 end_critical_section(S_LDAP);
495 /* If the entry already exists, repopulate it instead */
496 if (i == LDAP_ALREADY_EXISTS) {
497 for (j=0; j<(num_attrs-1); ++j) {
498 attrs[j]->mod_op = LDAP_MOD_REPLACE;
500 lprintf(9, "Calling ldap_modify_s()\n");
501 begin_critical_section(S_LDAP);
502 i = ldap_modify_s(dirserver, this_dn, attrs);
503 end_critical_section(S_LDAP);
506 if (i != LDAP_SUCCESS) {
507 lprintf(3, "ldap_add_s() failed: %s (%d)\n",
508 ldap_err2string(i), i);
511 lprintf(9, "Freeing attributes\n");
512 /* Free the attributes */
513 for (i=0; i<num_attrs; ++i) {
514 if (attrs[i] != NULL) {
516 /* First, free the value strings */
517 if (attrs[i]->mod_values != NULL) {
518 for (j=0; attrs[i]->mod_values[j] != NULL; ++j) {
519 phree(attrs[i]->mod_values[j]);
523 /* Free the value strings pointer list */
524 if (attrs[i]->mod_values != NULL) {
525 phree(attrs[i]->mod_values);
528 /* Now free the LDAPMod struct itself. */
533 lprintf(9, "LDAP operation complete.\n");
537 #endif /* HAVE_LDAP */
541 * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
543 char *serv_ldap_init(void)
546 CtdlRegisterCleanupHook(serv_ldap_cleanup);
548 if (strlen(config.c_ldap_host) > 0) {
552 #endif /* HAVE_LDAP */