|
|
|
@ -9,8 +9,6 @@
|
|
|
|
|
|
|
|
|
|
#include "config.h" |
|
|
|
|
|
|
|
|
|
enum { bufsize = 1024 }; |
|
|
|
|
|
|
|
|
|
/** shared connection, used for searching users and
|
|
|
|
|
* comparing their passwords if mode set to 'compare' |
|
|
|
|
* @returns 0 on success, 1 on error |
|
|
|
@ -23,12 +21,10 @@ oal_connect(oal_config_t * const config)
|
|
|
|
|
const short int ldapver = LDAP_VERSION3; |
|
|
|
|
const short int sizelimit = 5; |
|
|
|
|
struct timeval tv = { 30, 0 }; |
|
|
|
|
char err[bufsize]; |
|
|
|
|
int rc = 0; |
|
|
|
|
|
|
|
|
|
if ((rc = ldap_initialize(&ld, config->bindurls)) != LDAP_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't connnect to ldap server(s): %s", strerror(errno)); |
|
|
|
|
config->error = strndup(err, bufsize); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't connnect to ldap server(s): %s", strerror(errno)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (config->bindtimeout) |
|
|
|
@ -36,42 +32,41 @@ oal_connect(oal_config_t * const config)
|
|
|
|
|
|
|
|
|
|
/* hardcoded options */ |
|
|
|
|
if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldapver) != LDAP_OPT_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't set ldap protocol version"); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't set ldap protocol version"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
if (ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit) != LDAP_OPT_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't set max results limit"); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't set max results limit"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
/* timeouts */ |
|
|
|
|
if (ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv) != LDAP_OPT_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't set network timeout: %d", config->bindtimeout); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't set network timeout: %d", config->bindtimeout); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
if (ldap_set_option(ld, LDAP_OPT_TIMEOUT, &tv) != LDAP_OPT_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't set search timeout: %d", config->bindtimeout); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't set search timeout: %d", config->bindtimeout); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
/* TODO: hardcoded */ |
|
|
|
|
if (ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF) != LDAP_OPT_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't set follow referrals to 'off'"); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't set follow referrals to 'off'"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
/* required */ |
|
|
|
|
if (ldap_set_option(ld, LDAP_OPT_DEFBASE, config->basedn) != LDAP_OPT_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't set searchbase: %s", config->basedn); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't set searchbase: %s", config->basedn); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if((rc = ldap_simple_bind_s(ld, config->binddn, config->bindpass)) != LDAP_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "can't bind to ldap server: %s", ldap_err2string(rc)); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't bind to ldap server: %s", ldap_err2string(rc)); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; /* success */ |
|
|
|
|
|
|
|
|
|
error: |
|
|
|
|
config->error = strndup(err, bufsize); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -87,7 +82,6 @@ oal_check_cred(oal_config_t * const config,
|
|
|
|
|
LDAPMessage *msg = NULL; /* first message from search result */ |
|
|
|
|
char *searchattr[] = { (char *) LDAP_NO_ATTRS, NULL }; |
|
|
|
|
char *udn = NULL; /* DN of found user */ |
|
|
|
|
char err[bufsize]; |
|
|
|
|
int rc = 0; |
|
|
|
|
|
|
|
|
|
if (!ld && !oal_connect(config)) |
|
|
|
@ -95,7 +89,7 @@ oal_check_cred(oal_config_t * const config,
|
|
|
|
|
|
|
|
|
|
rc = ldap_search_s(ld, config->basedn, LDAP_SCOPE_SUBTREE, config->userfilter, searchattr, 1, &res); |
|
|
|
|
if (rc != LDAP_SUCCESS) { |
|
|
|
|
snprintf(err, bufsize, "ldap search failed: %s", ldap_err2string(rc)); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "ldap search failed: %s", ldap_err2string(rc)); |
|
|
|
|
goto error; /* TODO */ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -105,12 +99,12 @@ oal_check_cred(oal_config_t * const config,
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((msg = ldap_first_message(ld, res)) == NULL) { |
|
|
|
|
snprintf(err, bufsize, "ldap search found something, but can't get result"); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "ldap search found something, but can't get result"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((udn = ldap_get_dn(ld, msg)) == NULL) { |
|
|
|
|
snprintf(err, bufsize, "can't get DN of found user"); |
|
|
|
|
snprintf(config->error, sizeof(config->error), "can't get DN of found user"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -120,6 +114,5 @@ oal_check_cred(oal_config_t * const config,
|
|
|
|
|
if (res) ldap_msgfree(res); |
|
|
|
|
if (msg) ldap_msgfree(msg); |
|
|
|
|
if (udn) ldap_memfree(udn); |
|
|
|
|
config->error = strndup(err, bufsize); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|