Browse Source

* use static buffer instead strdup for error message

master
Alex 'AdUser' Z 9 years ago
parent
commit
7f24779e9f
  1. 42
      src/config.c
  2. 2
      src/config.h
  3. 29
      src/ldapauth.c
  4. 2
      tests/check_config.c

42
src/config.c

@ -7,12 +7,12 @@
#include "config.h" #include "config.h"
enum { bufsize = 1024 };
int parse_config(oal_config_t * const config, const char *file) { int parse_config(oal_config_t * const config, const char *file) {
FILE *f; FILE *f;
enum { bufsize = 1024 };
unsigned short linenum = 0; unsigned short linenum = 0;
char buf[bufsize]; char buf[bufsize];
char err[bufsize];
char *key, *value; char *key, *value;
size_t valsize; size_t valsize;
@ -20,8 +20,7 @@ int parse_config(oal_config_t * const config, const char *file) {
assert(file != NULL); assert(file != NULL);
if ((f = fopen(file, "r")) == NULL) { if ((f = fopen(file, "r")) == NULL) {
snprintf(err, bufsize, "can't open file: %s", strerror(errno)); snprintf(config->error, sizeof(config->error), "can't open file: %s", strerror(errno));
config->error = strndup(err, bufsize);
return 1; return 1;
} }
@ -36,8 +35,7 @@ int parse_config(oal_config_t * const config, const char *file) {
if (strlen(key) == 0) if (strlen(key) == 0)
continue; /* ignore empty lines */ continue; /* ignore empty lines */
if (!isalpha(*key)) { if (!isalpha(*key)) {
snprintf(err, bufsize, "can't parse line %d", linenum); snprintf(config->error, sizeof(config->error), "can't parse line %d", linenum);
config->error = strdup(err);
return 1; return 1;
} }
/* find start of value */ /* find start of value */
@ -45,16 +43,14 @@ int parse_config(oal_config_t * const config, const char *file) {
while(*value && !isspace(*value)) while(*value && !isspace(*value))
value++; value++;
if (!isspace(*value)) { if (!isspace(*value)) {
snprintf(err, bufsize, "can't find value at line %d", linenum); snprintf(config->error, sizeof(config->error), "can't find value at line %d", linenum);
config->error = strndup(err, bufsize);
return 1; return 1;
} }
*value = '\0', value += 1; *value = '\0', value += 1;
while (isspace(*value)) while (isspace(*value))
value++; value++;
if (!*value) { if (!*value) {
snprintf(err, bufsize, "can't find value at line %d", linenum); snprintf(config->error, sizeof(config->error), "can't find value at line %d", linenum);
config->error = strndup(err, bufsize);
return 1; return 1;
} }
/* strip trailing spaces and newline */ /* strip trailing spaces and newline */
@ -86,8 +82,7 @@ int parse_config(oal_config_t * const config, const char *file) {
config->userfilter = strndup(value, valsize); config->userfilter = strndup(value, valsize);
} else } else
{ {
snprintf(err, bufsize, "unknown key '%s' at line %d", key, linenum); snprintf(config->error, sizeof(config->error), "unknown key '%s' at line %d", key, linenum);
config->error = strndup(err, bufsize);
return 1; return 1;
} }
} }
@ -96,31 +91,24 @@ 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) {
enum { bufsize = 1024 };
char err[bufsize] = { '\0' };
assert(config != NULL); assert(config != NULL);
if (!config->bindurls) { if (!config->bindurls) {
snprintf(err, bufsize, "'bindurls' not set in config"); snprintf(config->error, sizeof(config->error), "'bindurls' not set in config");
goto error; return 1;
} }
if (!config->basedn) { if (!config->basedn) {
snprintf(err, bufsize, "'basedn' not set in config"); snprintf(config->error, sizeof(config->error), "'basedn' not set in config");
goto error; return 1;
} }
if (!config->userfilter) { if (!config->userfilter) {
snprintf(err, bufsize, "'userfilter' not set in config"); snprintf(config->error, sizeof(config->error), "'userfilter' not set in config");
goto error; return 1;
} }
if (config->binddn && !config->bindpass) { if (config->binddn && !config->bindpass) {
snprintf(err, bufsize, "'bindn' set, but 'bindpass' missing in config"); snprintf(config->error, sizeof(config->error), "'bindn' set, but 'bindpass' missing in config");
goto error; return 1;
} }
return 0; return 0;
error:
config->error = strndup(err, bufsize);
return 1;
} }

2
src/config.h

@ -9,7 +9,7 @@ typedef struct {
short referrals; /** if > 0 - follow referals */ short referrals; /** if > 0 - follow referals */
char *basedn; /** where to search for users */ char *basedn; /** where to search for users */
char *userfilter; /** ldap filter for user entry */ char *userfilter; /** ldap filter for user entry */
char *error; /** parser error */ char error[1024]; /** parser error */
} oal_config_t; } oal_config_t;
int parse_config(oal_config_t * const config, const char *file); int parse_config(oal_config_t * const config, const char *file);

29
src/ldapauth.c

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

2
tests/check_config.c

@ -6,7 +6,7 @@
#include "../src/config.h" #include "../src/config.h"
#define STEAL(attr) \ #define STEAL(attr) \
stealed = config.attr, config.attr = NULL, config.error = NULL; \ stealed = config.attr, config.attr = NULL, config.error[0] = '\0'; \
assert(check_config(&config) > 0); \ assert(check_config(&config) > 0); \
assert(config.error != NULL); \ assert(config.error != NULL); \
config.attr = stealed config.attr = stealed

Loading…
Cancel
Save