* ldap.c needs a little love. (I only added some extra debug messages and comments...
[citadel.git] / citadel / ldap.c
index 7a93098e59695a9e4fa5b0ab4057c23609682b96..0adf524a43e36581b6c5091c9d972f94e7427d4d 100644 (file)
@@ -1,10 +1,27 @@
 /*
- * 
+ * $Id$
+ *
+ * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
+ * actually speak to the LDAP server.
+ *
+ * Copyright (c) 2010 by Art Cancro and the citadel.org development team.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-
-int ldap_version = 3;
-
+int ctdl_require_ldap_version = 3;
 
 #include "sysdep.h"
 #include <errno.h>
@@ -52,11 +69,10 @@ int ldap_version = 3;
 #include "genstamp.h"
 #include "threads.h"
 #include "citadel_ldap.h"
+#include "ctdl_module.h"
 
 #ifdef HAVE_LDAP
-
-#define LDAP_DEPRECATED 1      /* Needed to suppress misleading warnings */
-
+#define LDAP_DEPRECATED 1      /* Suppress libldap's warning that we are using deprecated API calls */
 #include <ldap.h>
 
 int CtdlTryUserLDAP(char *username,
@@ -79,14 +95,16 @@ int CtdlTryUserLDAP(char *username,
        if (ldserver == NULL) {
                CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
                        config.c_ldap_host, config.c_ldap_port,
-                       strerror(errno));
+                       strerror(errno)
+               );
                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);
+       CtdlLogPrintf(CTDL_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
        i = ldap_simple_bind_s(ldserver,
                (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
                (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
@@ -106,24 +124,25 @@ int CtdlTryUserLDAP(char *username,
                sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
        }
 
-       i = ldap_search_st(ldserver,
-               config.c_ldap_base_dn,
-               LDAP_SCOPE_SUBTREE,
-               searchstring,
-               NULL,   // return all attributes
-               0,      // attributes + values
-               &tv,    // timeout
-               &search_result
+       /* FIXME migrate to ldap_search_ext_s() which is not deprecated.  http://tinyurl.com/y9c8a8l */
+       CtdlLogPrintf(CTDL_DEBUG, "LDAP search: %s\n", searchstring);
+       i = ldap_search_st(ldserver,                            // ld
+               config.c_ldap_base_dn,                          // base
+               LDAP_SCOPE_SUBTREE,                             // scope
+               searchstring,                                   // filter
+               NULL,   // return all attributes                // attrs
+               0,      // attributes + values                  // attrsonly
+               &tv,    // timeout                              // timeout
+               &search_result                                  // res
        );
        if (i != LDAP_SUCCESS) {
-               CtdlLogPrintf(CTDL_DEBUG,
-                       "Couldn't find what I was looking for: %s (%d)\n", ldap_err2string(i), i);
+               CtdlLogPrintf(CTDL_DEBUG, "LDAP search failed: %s (%d)\n", ldap_err2string(i), i);
                ldap_unbind(ldserver);
                return(i);
        }
 
        if (search_result == NULL) {
-               CtdlLogPrintf(CTDL_DEBUG, "No results were returned\n");
+               CtdlLogPrintf(CTDL_DEBUG, "LDAP search: zero results were returned\n");
                ldap_unbind(ldserver);
                return(2);
        }
@@ -211,7 +230,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");
@@ -230,6 +249,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 */