]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_ldap.c
* Use syslog-compatible logging levels in lprintf(); the loglevel chosen
[citadel.git] / citadel / serv_ldap.c
index 69f7222d2777b051990ee292f82579801ace08bb..c4ff284e1563060e08980990b338b07005cfc7c7 100644 (file)
@@ -57,7 +57,7 @@ void serv_ldap_cleanup(void)
 {
        if (!dirserver) return;
 
-       lprintf(7, "Unbinding from directory server\n");
+       lprintf(CTDL_INFO, "Unbinding from directory server\n");
        ldap_unbind(dirserver);
        dirserver = NULL;
 }
@@ -101,13 +101,13 @@ void CtdlCreateLdapRoot(void) {
        mods[2] = NULL;
 
        /* Perform the transaction */
-       lprintf(9, "Setting up Base DN node...\n");
+       lprintf(CTDL_DEBUG, "Setting up Base DN node...\n");
        begin_critical_section(S_LDAP);
        i = ldap_add_s(dirserver, config.c_ldap_base_dn, mods);
        end_critical_section(S_LDAP);
 
        if (i != LDAP_SUCCESS) {
-               lprintf(3, "ldap_add_s() failed: %s (%d)\n",
+               lprintf(CTDL_CRIT, "ldap_add_s() failed: %s (%d)\n",
                        ldap_err2string(i), i);
        }
 }
@@ -144,14 +144,14 @@ void CtdlCreateHostOU(char *host) {
        mods[2] = NULL;
 
        /* Perform the transaction */
-       lprintf(9, "Setting up Host OU node...\n");
+       lprintf(CTDL_DEBUG, "Setting up Host OU node...\n");
        begin_critical_section(S_LDAP);
        i = ldap_add_s(dirserver, dn, mods);
        end_critical_section(S_LDAP);
 
        /* ignore the error -- it's ok if it already exists
        if (i != LDAP_SUCCESS) {
-               lprintf(3, "ldap_add_s() failed: %s (%d)\n",
+               lprintf(CTDL_ERR, "ldap_add_s() failed: %s (%d)\n",
                        ldap_err2string(i), i);
        }
        */
@@ -168,12 +168,12 @@ void CtdlConnectToLdap(void) {
        int i;
        int ldap_version = 3;
 
-       lprintf(7, "Connecting to LDAP server %s:%d...\n",
+       lprintf(CTDL_INFO, "Connecting to LDAP server %s:%d...\n",
                config.c_ldap_host, config.c_ldap_port);
 
        dirserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
        if (dirserver == NULL) {
-               lprintf(3, "Could not connect to %s:%d : %s\n",
+               lprintf(CTDL_CRIT, "Could not connect to %s:%d : %s\n",
                        config.c_ldap_host,
                        config.c_ldap_port,
                        strerror(errno));
@@ -182,14 +182,14 @@ void CtdlConnectToLdap(void) {
 
        ldap_set_option(dirserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
 
-       lprintf(7, "Binding to %s\n", config.c_ldap_bind_dn);
+       lprintf(CTDL_INFO, "Binding to %s\n", config.c_ldap_bind_dn);
 
        i = ldap_simple_bind_s(dirserver,
                                config.c_ldap_bind_dn,
                                config.c_ldap_bind_pw
        );
        if (i != LDAP_SUCCESS) {
-               lprintf(3, "Cannot bind: %s (%d)\n", ldap_err2string(i), i);
+               lprintf(CTDL_CRIT, "Cannot bind: %s (%d)\n", ldap_err2string(i), i);
                dirserver = NULL;       /* FIXME disconnect from ldap */
                return;
        }
@@ -198,12 +198,16 @@ void CtdlConnectToLdap(void) {
 }
 
 
-
-/*
- * Write (add, or change if already exists) a directory entry to the
+/* 
+ * vCard-to-LDAP conversions.
+ *
+ * If 'op' is set to V2L_WRITE, then write
+ * (add, or change if already exists) a directory entry to the
  * LDAP server, based on the information supplied in a vCard.
+ *
+ * If 'op' is set to V2L_DELETE, then delete the entry from LDAP.
  */
-void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
+void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
        struct vCard *v = NULL;
        int i, j;
        char this_dn[SIZ];
@@ -230,12 +234,9 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
        if (msg->cm_fields['A'] == NULL) return;
        if (msg->cm_fields['N'] == NULL) return;
 
-       /* First make sure the OU for the user's home Citadel host is created */
-       CtdlCreateHostOU(msg->cm_fields['N']);
-
        /* Initialize variables */
-       strcpy(givenname, "_");
-       strcpy(sn, "_");
+       strcpy(givenname, "");
+       strcpy(sn, "");
 
        sprintf(this_dn, "cn=%s,ou=%s,%s",
                msg->cm_fields['A'],
@@ -248,6 +249,26 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
                msg->cm_fields['N']
        );
 
+       /* Are we just deleting?  If so, it's simple... */
+       if (op == V2L_DELETE) {
+               lprintf(CTDL_DEBUG, "Calling ldap_delete_s()\n");
+               begin_critical_section(S_LDAP);
+               i = ldap_delete_s(dirserver, this_dn);
+               end_critical_section(S_LDAP);
+               if (i != LDAP_SUCCESS) {
+                       lprintf(CTDL_ERR, "ldap_delete_s() failed: %s (%d)\n",
+                               ldap_err2string(i), i);
+               }
+               return;
+       }
+
+       /*
+        * If we get to this point then it must be a V2L_WRITE operation.
+        */
+
+       /* First make sure the OU for the user's home Citadel host is created */
+       CtdlCreateHostOU(msg->cm_fields['N']);
+
        /* The first LDAP attribute will be an 'objectclass' list.  Citadel
         * doesn't do anything with this.  It's just there for compatibility
         * with Kolab.
@@ -258,18 +279,13 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
        memset(attrs[0], 0, sizeof(LDAPMod));
        attrs[0]->mod_op        = LDAP_MOD_ADD;
        attrs[0]->mod_type      = "objectclass";
-       attrs[0]->mod_values    = mallok(2 * sizeof(char *));
+       attrs[0]->mod_values    = mallok(3 * sizeof(char *));
        attrs[0]->mod_values[0] = strdoop("inetOrgPerson");
-       /*
-       attrs[0]->mod_values[1] = strdoop("organizationalPerson");
-       attrs[0]->mod_values[2] = strdoop("person");
-       attrs[0]->mod_values[3] = strdoop("Top");
-       */
        attrs[0]->mod_values[1] = NULL;
 
        /* Convert the vCard fields to LDAP properties */
        v = vcard_load(msg->cm_fields['M']);
-       if (v->numprops) for (i=0; i<(v->numprops); ++i) {
+       if (v->numprops) for (i=0; i<(v->numprops); ++i) if (striplt(v->prop[i].value), strlen(v->prop[i].value) > 0) {
 
                if (!strcasecmp(v->prop[i].name, "n")) {
                        extract_token(sn,               v->prop[i].value, 0, ';');
@@ -398,7 +414,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
                   ||(!strcasecmp(v->prop[i].name, "email;internet")) ) {
        
                        ++num_emails;
-                       lprintf(9, "email addr %d\n", num_emails);
+                       lprintf(CTDL_DEBUG, "email addr %d\n", num_emails);
 
                        /* The first email address creates the 'mail' attribute */
                        if (num_emails == 1) {
@@ -450,6 +466,8 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
        attrs[num_attrs-1]->mod_values[1]       = NULL;
 
        /* "givenname" (first name) based on info in vCard */
+       if (strlen(givenname) == 0) strcpy(givenname, "_");
+       if (strlen(sn) == 0) strcpy(sn, "_");
        attrs = reallok(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
        attrs[num_attrs-1] = mallok(sizeof(LDAPMod));
        memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
@@ -487,7 +505,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
        attrs = realloc(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
        attrs[num_attrs - 1] = NULL;
        
-       lprintf(9, "Calling ldap_add_s()\n");
+       lprintf(CTDL_DEBUG, "Calling ldap_add_s()\n");
        begin_critical_section(S_LDAP);
        i = ldap_add_s(dirserver, this_dn, attrs);
        end_critical_section(S_LDAP);
@@ -497,18 +515,18 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
                for (j=0; j<(num_attrs-1); ++j) {
                        attrs[j]->mod_op = LDAP_MOD_REPLACE;
                }
-               lprintf(9, "Calling ldap_modify_s()\n");
+               lprintf(CTDL_DEBUG, "Calling ldap_modify_s()\n");
                begin_critical_section(S_LDAP);
                i = ldap_modify_s(dirserver, this_dn, attrs);
                end_critical_section(S_LDAP);
        }
 
        if (i != LDAP_SUCCESS) {
-               lprintf(3, "ldap_add_s() failed: %s (%d)\n",
+               lprintf(CTDL_ERR, "ldap_add_s() failed: %s (%d)\n",
                        ldap_err2string(i), i);
        }
 
-       lprintf(9, "Freeing attributes\n");
+       lprintf(CTDL_DEBUG, "Freeing attributes\n");
        /* Free the attributes */
        for (i=0; i<num_attrs; ++i) {
                if (attrs[i] != NULL) {
@@ -530,7 +548,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg) {
                }
        }
        phree(attrs);
-       lprintf(9, "LDAP operation complete.\n");
+       lprintf(CTDL_DEBUG, "LDAP write operation complete.\n");
 }