From: Art Cancro Date: Sun, 10 Sep 2017 21:39:39 +0000 (-0400) Subject: started working on code for extracting email addresses from ldap X-Git-Tag: v939~525 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=219bbe065f3a3e1117f3fadd3e28164098d4f625 started working on code for extracting email addresses from ldap --- diff --git a/citadel/citadel_ldap.h b/citadel/citadel_ldap.h index 9a312795a..fdf199b01 100644 --- a/citadel/citadel_ldap.h +++ b/citadel/citadel_ldap.h @@ -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); diff --git a/citadel/ldap.c b/citadel/ldap.c index d5f5d41ce..8e51087b9 100644 --- a/citadel/ldap.c +++ b/citadel/ldap.c @@ -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 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 */ diff --git a/citadel/user_ops.c b/citadel/user_ops.c index b78cc592f..87dfa5a25 100644 --- a/citadel/user_ops.c +++ b/citadel/user_ops.c @@ -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. */