]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_ldap.c
mk_module_init.sh now tests to see if echo supports -e and -E
[citadel.git] / citadel / serv_ldap.c
index 6b15584e088d97167017615a60c55894a4a41e6a..f3e5a774a64b37cda558ac3f7fe8748f87527167 100644 (file)
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
 #include "vcard.h"
 #include "tools.h"
 
+
+#include "ctdl_module.h"
+
+
+
 #ifdef HAVE_LDAP
 
 #include <ldap.h>
@@ -106,7 +109,10 @@ void CtdlCreateLdapRoot(void) {
        i = ldap_add_s(dirserver, config.c_ldap_base_dn, mods);
        end_critical_section(S_LDAP);
 
-       if (i != LDAP_SUCCESS) {
+       if (i == LDAP_ALREADY_EXISTS) {
+               lprintf(CTDL_INFO, "Base DN is already present in the directory; no need to add it again.\n");
+       }
+       else if (i != LDAP_SUCCESS) {
                lprintf(CTDL_CRIT, "ldap_add_s() failed: %s (%d)\n",
                        ldap_err2string(i), i);
        }
@@ -149,12 +155,13 @@ void CtdlCreateHostOU(char *host) {
        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(CTDL_ERR, "ldap_add_s() failed: %s (%d)\n",
+       if (i == LDAP_ALREADY_EXISTS) {
+               lprintf(CTDL_INFO, "Host OU is already present in the directory; no need to add it again.\n");
+       }
+       else if (i != LDAP_SUCCESS) {
+               lprintf(CTDL_CRIT, "ldap_add_s() failed: %s (%d)\n",
                        ldap_err2string(i), i);
        }
-       */
 }
 
 
@@ -220,13 +227,14 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
        int have_addr = 0;
        int have_cn = 0;
 
-       char givenname[SIZ];
-       char sn[SIZ];
-       char uid[SIZ];
-       char street[SIZ];
-       char city[SIZ];
-       char state[SIZ];
-       char zipcode[SIZ];
+       char givenname[128];
+       char sn[128];
+       char uid[256];
+       char street[256];
+       char city[128];
+       char state[3];
+       char zipcode[10];
+       char calFBURL[256];
 
        if (dirserver == NULL) return;
        if (msg == NULL) return;
@@ -237,6 +245,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
        /* Initialize variables */
        strcpy(givenname, "");
        strcpy(sn, "");
+       strcpy(calFBURL, "");
 
        sprintf(this_dn, "cn=%s,ou=%s,%s",
                msg->cm_fields['A'],
@@ -280,7 +289,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
        attrs[0]->mod_op        = LDAP_MOD_ADD;
        attrs[0]->mod_type      = "objectclass";
        attrs[0]->mod_values    = malloc(3 * sizeof(char *));
-       attrs[0]->mod_values[0] = strdup("inetOrgPerson");
+       attrs[0]->mod_values[0] = strdup("citadelInetOrgPerson");
        attrs[0]->mod_values[1] = NULL;
 
        /* Convert the vCard fields to LDAP properties */
@@ -288,8 +297,8 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
        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, ';');
-                       extract_token(givenname,        v->prop[i].value, 1, ';');
+                       extract_token(sn,               v->prop[i].value, 0, ';', sizeof sn);
+                       extract_token(givenname,        v->prop[i].value, 1, ';', sizeof givenname);
                }
 
                if (!strcasecmp(v->prop[i].name, "fn")) {
@@ -333,17 +342,17 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
                                have_addr = 1;
                                strcpy(street, "");
                                extract_token(&street[strlen(street)],
-                                       v->prop[i].value, 0, ';'); /* po box */
+                                       v->prop[i].value, 0, ';', (sizeof street - strlen(street))); /* po box */
                                strcat(street, " ");
                                extract_token(&street[strlen(street)],
-                                       v->prop[i].value, 1, ';'); /* extend addr */
+                                       v->prop[i].value, 1, ';', (sizeof street - strlen(street))); /* extend addr */
                                strcat(street, " ");
                                extract_token(&street[strlen(street)],
-                                       v->prop[i].value, 2, ';'); /* street */
+                                       v->prop[i].value, 2, ';', (sizeof street - strlen(street))); /* street */
                                striplt(street);
-                               extract_token(city, v->prop[i].value, 3, ';');
-                               extract_token(state, v->prop[i].value, 4, ';');
-                               extract_token(zipcode, v->prop[i].value, 5, ';');
+                               extract_token(city, v->prop[i].value, 3, ';', sizeof city);
+                               extract_token(state, v->prop[i].value, 4, ';', sizeof state);
+                               extract_token(zipcode, v->prop[i].value, 5, ';', sizeof zipcode);
 
                                attrs = realloc(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
                                attrs[num_attrs-1] = malloc(sizeof(LDAPMod));
@@ -452,6 +461,17 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
 
                }
 
+               /* Calendar free/busy URL (take the first one we find, but if a subsequent
+                * one contains the "pref" designation then we go with that instead.)
+                */
+               if ( (!strcasecmp(v->prop[i].name, "fburl"))
+                  ||(!strncasecmp(v->prop[i].name, "fburl;", 6)) ) {
+                       if ( (strlen(calFBURL) == 0)
+                          || (!strncasecmp(v->prop[i].name, "fburl;pref", 10)) ) {
+                               safestrncpy(calFBURL, v->prop[i].value, sizeof calFBURL);
+                       }
+               }
+
        }
        vcard_free(v);  /* Don't need this anymore. */
 
@@ -500,12 +520,24 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
                attrs[num_attrs-1]->mod_values[0]       = strdup(msg->cm_fields['A']);
                attrs[num_attrs-1]->mod_values[1]       = NULL;
        }
+
+       /* Add a "calFBURL" attribute if a calendar free/busy URL exists */
+       if (strlen(calFBURL) > 0) {
+               attrs = realloc(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
+               attrs[num_attrs-1] = malloc(sizeof(LDAPMod));
+               memset(attrs[num_attrs-1], 0, sizeof(LDAPMod));
+               attrs[num_attrs-1]->mod_op              = LDAP_MOD_ADD;
+               attrs[num_attrs-1]->mod_type            = "calFBURL";
+               attrs[num_attrs-1]->mod_values          = malloc(2 * sizeof(char *));
+               attrs[num_attrs-1]->mod_values[0]       = strdup(calFBURL);
+               attrs[num_attrs-1]->mod_values[1]       = NULL;
+       }
        
        /* The last attribute must be a NULL one. */
        attrs = realloc(attrs, (sizeof(LDAPMod *) * ++num_attrs) );
        attrs[num_attrs - 1] = NULL;
        
-       lprintf(CTDL_DEBUG, "Calling ldap_add_s()\n");
+       lprintf(CTDL_DEBUG, "Calling ldap_add_s() for '%s'\n", this_dn);
        begin_critical_section(S_LDAP);
        i = ldap_add_s(dirserver, this_dn, attrs);
        end_critical_section(S_LDAP);
@@ -515,7 +547,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
                for (j=0; j<(num_attrs-1); ++j) {
                        attrs[j]->mod_op = LDAP_MOD_REPLACE;
                }
-               lprintf(CTDL_DEBUG, "Calling ldap_modify_s()\n");
+               lprintf(CTDL_DEBUG, "Calling ldap_modify_s() for '%s'\n", this_dn);
                begin_critical_section(S_LDAP);
                i = ldap_modify_s(dirserver, this_dn, attrs);
                end_critical_section(S_LDAP);
@@ -558,7 +590,7 @@ void ctdl_vcard_to_ldap(struct CtdlMessage *msg, int op) {
 /*
  * Initialize the LDAP connector module ... or don't, if we don't have LDAP.
  */
-char *serv_ldap_init(void)
+CTDL_MODULE_INIT(ldap)
 {
 #ifdef HAVE_LDAP
        CtdlRegisterCleanupHook(serv_ldap_cleanup);
@@ -568,5 +600,7 @@ char *serv_ldap_init(void)
        }
 
 #endif                         /* HAVE_LDAP */
+
+       /* return our Subversion id for the Log */
        return "$Id$";
 }