Cleaning the API a bit more.
[citadel.git] / citadel / ldap.c
index 06f894f7dab3fcb5f1c8b17ec0040afeb1e86972..45051120bbcc27bb8eb11d5b8d447b3a1f052fad 100644 (file)
@@ -1,9 +1,15 @@
 /*
- * 
+ * $Id$
+ *
+ * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
+ * actually speak to the LDAP server.
+ *
+ * Copyright (c) 2009 by Art Cancro and the citadel.org development team.
+ * This program is released under the terms of the GNU General Public License v3
  */
 
 
-int ldap_version = 3;
+int ctdl_require_ldap_version = 3;
 
 
 #include "sysdep.h"
@@ -53,6 +59,9 @@ int ldap_version = 3;
 #include "threads.h"
 #include "citadel_ldap.h"
 
+#include "ctdl_module.h"
+
+
 #ifdef HAVE_LDAP
 
 #define LDAP_DEPRECATED 1      /* Needed to suppress misleading warnings */
@@ -83,7 +92,7 @@ int CtdlTryUserLDAP(char *username,
                return(errno);
        }
 
-       ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
+       ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
 
        striplt(config.c_ldap_bind_dn);
        striplt(config.c_ldap_bind_pw);
@@ -99,7 +108,12 @@ int CtdlTryUserLDAP(char *username,
        tv.tv_sec = 10;
        tv.tv_usec = 0;
 
-       sprintf(searchstring, SEARCH_STRING, username);
+       if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
+               sprintf(searchstring, "(sAMAccountName=%s)", username);
+       }
+       else {
+               sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
+       }
 
        i = ldap_search_st(ldserver,
                config.c_ldap_base_dn,
@@ -134,32 +148,50 @@ int CtdlTryUserLDAP(char *username,
                        CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
                }
 
-               values = ldap_get_values(ldserver, search_result, "cn");
-               if (values) {
-                       if (values[0]) {
-                               if (fullname) safestrncpy(fullname, values[0], fullname_size);
-                               CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
+               if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
+                       values = ldap_get_values(ldserver, search_result, "displayName");
+                       if (values) {
+                               if (values[0]) {
+                                       if (fullname) safestrncpy(fullname, values[0], fullname_size);
+                                       CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
+                               }
+                               ldap_value_free(values);
                        }
-                       ldap_value_free(values);
                }
-
-               values = ldap_get_values(ldserver, search_result, "uidNumber");
-               if (values) {
-                       if (values[0]) {
-                               CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
-                               if (uid != NULL) {
-                                       *uid = atoi(values[0]);
+               else {
+                       values = ldap_get_values(ldserver, search_result, "cn");
+                       if (values) {
+                               if (values[0]) {
+                                       if (fullname) safestrncpy(fullname, values[0], fullname_size);
+                                       CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
                                }
+                               ldap_value_free(values);
                        }
-                       ldap_value_free(values);
                }
 
-               values = ldap_get_values(ldserver, search_result, "objectGUID");
-               if (values) {
-                       if (values[0]) {
-                               CtdlLogPrintf(CTDL_DEBUG, "objectGUID = (%d characers)\n", strlen(values[0]));
+               if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
+                       values = ldap_get_values(ldserver, search_result, "objectGUID");
+                       if (values) {
+                               if (values[0]) {
+                                       if (uid != NULL) {
+                                               *uid = abs(HashLittle(values[0], strlen(values[0])));
+                                               CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
+                                       }
+                               }
+                               ldap_value_free(values);
+                       }
+               }
+               else {
+                       values = ldap_get_values(ldserver, search_result, "uidNumber");
+                       if (values) {
+                               if (values[0]) {
+                                       CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
+                                       if (uid != NULL) {
+                                               *uid = atoi(values[0]);
+                                       }
+                               }
+                               ldap_value_free(values);
                        }
-                       ldap_value_free(values);
                }
 
        }
@@ -188,7 +220,7 @@ int CtdlTryPasswordLDAP(char *user_dn, char *password)
 
        ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
        if (ldserver) {
-               ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
+               ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
                i = ldap_simple_bind_s(ldserver, user_dn, password);
                if (i == LDAP_SUCCESS) {
                        CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
@@ -207,6 +239,27 @@ int CtdlTryPasswordLDAP(char *user_dn, char *password)
 }
 
 
+/*
+ * Learn LDAP attributes and stuff them into the vCard.
+ * Returns nonzero if we changed anything.
+ */
+int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
+{
+       if (!ldap_dn) return(0);
+       if (!v) return(0);
+
+       /*
+        * FIXME LDAPSTUB this is a stub function
+        *
+        * ldap_dn will contain the DN of the user, and v will contain a pointer to
+        * the vCard that needs to be (re-)populated.  Put the requisite LDAP code here.
+        *
+       vcard_set_prop(v, "email;internet", xxx, 0);
+       return(1);      * return nonzero to tell the caller that we made changes that need to be saved *
+        *
+        */
 
+       return(0);      /* return zero to tell the caller that we didn't make any changes */
+}
 
 #endif /* HAVE_LDAP */