started working on code for extracting email addresses from ldap
authorArt Cancro <ajc@citadel.org>
Sun, 10 Sep 2017 21:39:39 +0000 (17:39 -0400)
committerArt Cancro <ajc@citadel.org>
Sun, 10 Sep 2017 21:39:39 +0000 (17:39 -0400)
citadel/citadel_ldap.h
citadel/ldap.c
citadel/user_ops.c

index 9a312795a37c96888701dba8576831361a5f1341..fdf199b0185b2070de6134082a0a2c37bb568dab 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Configuration for LDAP authentication.  Most of this stuff gets pulled out of our site config file.
  *
- * Copyright (c) 1987-2012 by the citadel.org team
+ * Copyright (c) 1987-2017 by the citadel.org team
  *
  *  This program is open source software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 3.
@@ -15,3 +15,4 @@
 int CtdlTryUserLDAP(char *username, char *found_dn, int found_dn_size, char *fullname, int fullname_size, uid_t *found_uid, int lookup_based_on_uid);
 int CtdlTryPasswordLDAP(char *user_dn, const char *password);
 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v);
+int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs);
index d5f5d41ce11e5a9aa66d096edee7b17f007c616b..8e51087b9550d6c93168139361d35b276ac7ab30 100644 (file)
@@ -22,6 +22,7 @@ int ctdl_require_ldap_version = 3;
 #include "citadel_ldap.h"
 #include "ctdl_module.h"
 #include "user_ops.h"
+#include "internet_addressing.h"
 #include "config.h"
 
 #ifdef HAVE_LDAP
@@ -438,8 +439,107 @@ int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
 
        /* unbind so we can go back in as the authenticating user */
        ldap_unbind(ldserver);
-       
        return(changed_something);      /* tell the caller whether we made any changes */
 }
 
+
+/*
+ * Extract a user's Internet email addresses from LDAP.
+ * Returns zero if we got a valid set of addresses; nonzero for error.
+ */
+int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
+{
+       LDAP *ldserver = NULL;
+       int i;
+       struct timeval tv;
+       LDAPMessage *search_result = NULL;
+       LDAPMessage *entry = NULL;
+       char **mail;
+       char *attrs[] = { "*","+",NULL};
+
+       if (!ldap_dn) return(1);
+       if (!emailaddrs) return(1);
+
+       if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
+               return(2);
+       }
+
+       ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
+       ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
+
+       striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
+       striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
+       syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
+       i = ldap_simple_bind_s(ldserver,
+               (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
+               (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
+       );
+       if (i != LDAP_SUCCESS) {
+               syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
+               return(3);
+       }
+
+       tv.tv_sec = 10;
+       tv.tv_usec = 0;
+
+       syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
+       (void) ldap_search_ext_s(
+               ldserver,                               // ld
+               ldap_dn,                                // base
+               LDAP_SCOPE_SUBTREE,                     // scope
+               NULL,                                   // filter
+               attrs,                                  // attrs (all attributes)
+               0,                                      // attrsonly (attrs + values)
+               NULL,                                   // serverctrls (none)
+               NULL,                                   // clientctrls (none)
+               &tv,                                    // timeout
+               1,                                      // sizelimit (1 result max)
+               &search_result                          // res
+       );
+       
+       /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
+        * the search succeeds.  Instead, we check to see whether search_result is still NULL.
+        */
+       if (search_result == NULL) {
+               syslog(LOG_DEBUG, "ldap: zero search results were returned");
+               ldap_unbind(ldserver);
+               return(4);
+       }
+
+       /* At this point we've got at least one result from our query.  If there are multiple
+        * results, we still only look at the first one.
+        */
+       emailaddrs[0] = 0;      /* clear out any previous results */
+       entry = ldap_first_entry(ldserver, search_result);
+       if (entry) {
+               syslog(LOG_DEBUG, "ldap: search got user details");
+               mail=ldap_get_values(ldserver, search_result, "mail");
+
+               if (mail) {
+                       int q;
+                       for (q=0; q<ldap_count_values(mail); ++q) {
+                               if (IsDirectory(mail[q], 0)) {
+                                       syslog(LOG_DEBUG, "\035FIXME YES DIRECTORY %s\033[0m", mail[q]);
+                                       if ((strlen(emailaddrs) + strlen(mail[q]) + 2) > 512) {
+                                               syslog(LOG_ERR, "ldap: can't fit all email addresses into user record");
+                                       }
+                                       else {
+                                               if (!IsEmptyStr(emailaddrs)) {
+                                                       strcat(emailaddrs, "|");
+                                               }
+                                               strcat(emailaddrs, mail[q]);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /* free the results */
+       ldap_msgfree(search_result);
+
+       /* unbind so we can go back in as the authenticating user */
+       ldap_unbind(ldserver);
+       return(0);
+}
+
 #endif /* HAVE_LDAP */
index b78cc592fbdc84410569b7e242517707acbcc716..87dfa5a2515eea16776664d26640f1f8094526cb 100644 (file)
@@ -623,8 +623,7 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
                if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
                        return login_too_many_users;
                } else {
-                       safestrncpy(CC->curr_user, CC->user.fullname,
-                                       sizeof CC->curr_user);
+                       safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
                        return login_ok;
                }
        }
@@ -665,6 +664,13 @@ void do_login(void)
 
        CtdlPutUserLock(&CCC->user);
 
+
+       // PUT THE EMAIL EXTRACT HERE AJC FIXME
+       //#ifdef HAVE_LDAP
+               //if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
+               //int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
+       //#endif
+
        /*
         * No email address for user?  Make one up.
         */