Browse Source

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

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

32
src/ldapauth.c

@ -65,7 +65,7 @@ oal_connect(LDAP ** ld,
int rc = 0; int rc = 0;
if ((rc = ldap_initialize(ld, config->bindurls)) != LDAP_SUCCESS) { 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; return 1;
} }
@ -76,38 +76,38 @@ oal_connect(LDAP ** ld,
/* hardcoded options */ /* hardcoded options */
if (ldap_set_option(*ld, LDAP_OPT_PROTOCOL_VERSION, &ldapver) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
if (ldap_set_option(*ld, LDAP_OPT_SIZELIMIT, &sizelimit) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
if (ldap_set_option(*ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
/* timeouts */ /* timeouts */
if (ldap_set_option(*ld, LDAP_OPT_NETWORK_TIMEOUT, &tv) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
if (ldap_set_option(*ld, LDAP_OPT_TIMEOUT, &tv) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
if (ldap_set_option(*ld, LDAP_OPT_DEBUG_LEVEL, &ldapdebug) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
/* required */ /* required */
if (ldap_set_option(*ld, LDAP_OPT_DEFBASE, config->basedn) != LDAP_OPT_SUCCESS) { 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; return 1;
} }
if ((rc = ldap_simple_bind_s(*ld, binddn, bindpass)) != LDAP_SUCCESS) { 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; return 1;
} }
@ -139,12 +139,12 @@ oal_check_cred(oal_config_t * const config,
assert(password != NULL); assert(password != NULL);
if (oal_ldap_escape(uid, sizeof(uid), username) < 0) { 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; return -1;
} }
if (snprintf(filter, sizeof(filter), config->userfilter, uid, uid) >= (int) sizeof(filter)) { 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; 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); lrc = ldap_search_s(sld, config->basedn, LDAP_SCOPE_SUBTREE, filter, searchattr, 1, &res);
if (lrc != LDAP_SUCCESS) { 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 */ goto cleanup; /* TODO */
} }
lrc = ldap_count_entries(sld, res); lrc = ldap_count_entries(sld, res);
if (lrc <= 0) { if (lrc <= 0) {
if (lrc == 0) { if (lrc == 0) {
snprintf(config->error, sizeof(config->error), "user not found"); oal_error(config, "user not found");
rc = 0; rc = 0;
} }
goto cleanup; goto cleanup;
} }
if ((msg = ldap_first_entry(sld, res)) == NULL) { 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; goto cleanup;
} }
@ -178,7 +178,7 @@ oal_check_cred(oal_config_t * const config,
continue; continue;
if ((udn = ldap_get_dn(sld, msg)) == NULL || strlen(udn) == 0) { 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; break;
} }
fprintf(stderr, "dn: %s\n", udn); fprintf(stderr, "dn: %s\n", udn);
@ -188,7 +188,7 @@ fprintf(stderr, "dn: %s\n", udn);
ldap_unbind(ald); ldap_unbind(ald);
break; /* success */ break; /* success */
} else { } else {
snprintf(config->error, sizeof(config->error), "password mismatch"); oal_error(config, "password mismatch");
rc = 0; rc = 0;
break; break;
} }

Loading…
Cancel
Save