#define LDAP_DEPRECATED 1 /* Suppress libldap's warning that we are using deprecated API calls */
#include <ldap.h>
+
+
+/*
+ * This function is a replacement for all calls to ldap_init() in this file.
+ * It fills in the required fields with those from the server's global config.
+ */
+LDAP *ctdl_ldap_init(void) {
+ return ldap_init(config.c_ldap_host, config.c_ldap_port);
+}
+
+
+
+/*
+ * Look up a username in the LDAP directory to see if it's valid for logging in.
+ */
int CtdlTryUserLDAP(char *username,
char *found_dn, int found_dn_size,
char *fullname, int fullname_size,
char **values;
char *user_dn = NULL;
-#ifndef LDAP_INITIALIZE
if (fullname) safestrncpy(fullname, username, fullname_size);
- ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
+ ldserver = ctdl_ldap_init();
if (ldserver == NULL) {
syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
config.c_ldap_host, config.c_ldap_port,
syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
return(i);
}
-#else
- if (ldap_initialize(&ldserver, config.c_ldap_host))
- {
- syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
- config.c_ldap_host, config.c_ldap_port,
- strerror(errno)
- );
- return(errno);
- }
-
- striplt(config.c_ldap_bind_dn);
- striplt(config.c_ldap_bind_pw);
-
- syslog(LOG_DEBUG, "LDAP bind DN: %s", 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)
- );
-
- if (i != LDAP_SUCCESS) {
- syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
- return(i);
- }
-#endif
-
tv.tv_sec = 10;
tv.tv_usec = 0;
}
syslog(LOG_DEBUG, "LDAP: trying to bind as %s", user_dn);
- ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
+ ldserver = ctdl_ldap_init();
if (ldserver) {
ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
i = ldap_simple_bind_s(ldserver, user_dn, password);
if (!ldap_dn) return(0);
if (!v) return(0);
- ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
+ ldserver = ctdl_ldap_init();
if (ldserver == NULL) {
syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
config.c_ldap_host, config.c_ldap_port,