Browse Source

* replace a lot of snprintf() calls with oal_error()

master
Alex 'AdUser' Z 7 years ago
parent
commit
b2dcf7894d
  1. 54
      src/config.c
  2. 32
      src/ldapauth.c

54
src/config.c

@ -29,10 +29,8 @@ int parse_config(oal_config_t * const config, const char *file) {
assert(config != NULL);
assert(file != NULL);
if ((f = fopen(file, "r")) == NULL) {
snprintf(config->error, sizeof(config->error), "can't open file: %s", strerror(errno));
return 1;
}
if ((f = fopen(file, "r")) == NULL)
return oal_error(config, "can't open file: %s", strerror(errno));
while (fgets(buf, bufsize, f)) {
linenum++;
@ -44,25 +42,19 @@ int parse_config(oal_config_t * const config, const char *file) {
continue; /* ignore comments */
if (strlen(key) == 0)
continue; /* ignore empty lines */
if (!isalpha(*key)) {
snprintf(config->error, sizeof(config->error), "can't parse line %d", linenum);
return 1;
}
if (!isalpha(*key))
return oal_error(config, "can't parse line %d", linenum);
/* find start of value */
value = key;
while(*value && !isspace(*value))
value++;
if (!isspace(*value)) {
snprintf(config->error, sizeof(config->error), "can't find value at line %d", linenum);
return 1;
}
if (!isspace(*value))
return oal_error(config, "can't find value at line %d", linenum);
*value = '\0', value += 1;
while (isspace(*value))
value++;
if (!*value) {
snprintf(config->error, sizeof(config->error), "can't find value at line %d", linenum);
return 1;
}
if (!*value)
return oal_error(config, "can't find value at line %d", linenum);
/* strip trailing spaces and newline */
valsize = strnlen(value, bufsize - (value - buf));
while (valsize && isspace(value[valsize - 1])) {
@ -90,10 +82,8 @@ int parse_config(oal_config_t * const config, const char *file) {
} else
if (strncmp(key, "userfilter", 10) == 0) {
config->userfilter = strndup(value, valsize);
} else
{
snprintf(config->error, sizeof(config->error), "unknown key '%s' at line %d", key, linenum);
return 1;
} else {
return oal_error(config, "unknown key '%s' at line %d", key, linenum);
}
}
@ -103,22 +93,14 @@ int parse_config(oal_config_t * const config, const char *file) {
int check_config(oal_config_t * const config) {
assert(config != NULL);
if (!config->bindurls) {
snprintf(config->error, sizeof(config->error), "'bindurls' not set in config");
return 1;
}
if (!config->basedn) {
snprintf(config->error, sizeof(config->error), "'basedn' not set in config");
return 1;
}
if (!config->userfilter) {
snprintf(config->error, sizeof(config->error), "'userfilter' not set in config");
return 1;
}
if (config->binddn && !config->bindpass) {
snprintf(config->error, sizeof(config->error), "'bindn' set, but 'bindpass' missing in config");
return 1;
}
if (!config->bindurls)
return oal_error(config, "'bindurls' not set in config");
if (!config->basedn)
return oal_error(config, "'basedn' not set in config");
if (!config->userfilter)
return oal_error(config, "'userfilter' not set in config");
if (config->binddn && !config->bindpass)
return oal_error(config, "'bindn' set, but 'bindpass' missing in config");
return 0;
}

32
src/ldapauth.c

@ -65,7 +65,7 @@ oal_connect(LDAP ** ld,
int rc = 0;
if ((rc = ldap_initialize(ld, config->bindurls)) != LDAP_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't connect to ldap server(s): %s", strerror(errno));
oal_error(config, "can't connect to ldap server(s): %s", strerror(errno));
return 1;
}
@ -76,38 +76,38 @@ oal_connect(LDAP ** ld,
/* hardcoded options */
if (ldap_set_option(*ld, LDAP_OPT_PROTOCOL_VERSION, &ldapver) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set ldap protocol version");
oal_error(config, "can't set ldap protocol version");
return 1;
}
if (ldap_set_option(*ld, LDAP_OPT_SIZELIMIT, &sizelimit) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set max results limit");
oal_error(config, "can't set max results limit");
return 1;
}
if (ldap_set_option(*ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set follow referrals to 'off'");
oal_error(config, "can't set follow referrals to 'off'");
return 1;
}
/* timeouts */
if (ldap_set_option(*ld, LDAP_OPT_NETWORK_TIMEOUT, &tv) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set network timeout: %d", config->bindtimeout);
oal_error(config, "can't set network timeout: %d", config->bindtimeout);
return 1;
}
if (ldap_set_option(*ld, LDAP_OPT_TIMEOUT, &tv) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set search timeout: %d", config->bindtimeout);
oal_error(config, "can't set search timeout: %d", config->bindtimeout);
return 1;
}
if (ldap_set_option(*ld, LDAP_OPT_DEBUG_LEVEL, &ldapdebug) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set debug level for ldap conn");
oal_error(config, "can't set debug level for ldap conn");
return 1;
}
/* required */
if (ldap_set_option(*ld, LDAP_OPT_DEFBASE, config->basedn) != LDAP_OPT_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't set searchbase: %s", config->basedn);
oal_error(config, "can't set searchbase: %s", config->basedn);
return 1;
}
if ((rc = ldap_simple_bind_s(*ld, binddn, bindpass)) != LDAP_SUCCESS) {
snprintf(config->error, sizeof(config->error), "can't bind to ldap server: %s", ldap_err2string(rc));
oal_error(config, "can't bind to ldap server: %s", ldap_err2string(rc));
return 1;
}
@ -139,12 +139,12 @@ oal_check_cred(oal_config_t * const config,
assert(password != NULL);
if (oal_ldap_escape(uid, sizeof(uid), username) < 0) {
snprintf(config->error, sizeof(config->error), "can't escape username: it's too long");
oal_error(config, "can't escape username: it's too long");
return -1;
}
if (snprintf(filter, sizeof(filter), config->userfilter, uid, uid) >= (int) sizeof(filter)) {
snprintf(config->error, sizeof(config->error), "can't interpolate userfilter: lack of space");
oal_error(config, "can't interpolate userfilter: lack of space");
return -1;
}
@ -155,21 +155,21 @@ oal_check_cred(oal_config_t * const config,
lrc = ldap_search_s(sld, config->basedn, LDAP_SCOPE_SUBTREE, filter, searchattr, 1, &res);
if (lrc != LDAP_SUCCESS) {
snprintf(config->error, sizeof(config->error), "ldap search failed: %s", ldap_err2string(lrc));
oal_error(config, "ldap search failed: %s", ldap_err2string(lrc));
goto cleanup; /* TODO */
}
lrc = ldap_count_entries(sld, res);
if (lrc <= 0) {
if (lrc == 0) {
snprintf(config->error, sizeof(config->error), "user not found");
oal_error(config, "user not found");
rc = 0;
}
goto cleanup;
}
if ((msg = ldap_first_entry(sld, res)) == NULL) {
snprintf(config->error, sizeof(config->error), "ldap search found something, but can't get result");
oal_error(config, "ldap search found something, but can't get result");
goto cleanup;
}
@ -178,7 +178,7 @@ oal_check_cred(oal_config_t * const config,
continue;
if ((udn = ldap_get_dn(sld, msg)) == NULL || strlen(udn) == 0) {
snprintf(config->error, sizeof(config->error), "can't get DN of found user");
oal_error(config, "can't get DN of found user");
break;
}
fprintf(stderr, "dn: %s\n", udn);
@ -188,7 +188,7 @@ fprintf(stderr, "dn: %s\n", udn);
ldap_unbind(ald);
break; /* success */
} else {
snprintf(config->error, sizeof(config->error), "password mismatch");
oal_error(config, "password mismatch");
rc = 0;
break;
}

Loading…
Cancel
Save