aboutsummaryrefslogtreecommitdiff
path: root/modules/ldap/ldap.c
diff options
context:
space:
mode:
Diffstat (limited to 'modules/ldap/ldap.c')
-rw-r--r--modules/ldap/ldap.c136
1 files changed, 83 insertions, 53 deletions
diff --git a/modules/ldap/ldap.c b/modules/ldap/ldap.c
index bb382eb..7935236 100644
--- a/modules/ldap/ldap.c
+++ b/modules/ldap/ldap.c
@@ -72,7 +72,8 @@ struct ldap_db {
LDAP *ldap;
};
-static struct ldap_conf def_conf;
+static struct ldap_conf dfl_conf;
+static char dfl_config_file[] = "/etc/ldap.conf";
static char dfl_positive_reply[] = "OK";
static char dfl_negative_reply[] = "NOTFOUND";
static char dfl_onerror_reply[] = "NOTFOUND";
@@ -253,49 +254,86 @@ readconf(struct smap_option const *opt, const char *val, char **errmsg)
*errmsg = "parse error";
return rc;
}
-
+
+#define MKOPT_DEFAULT 0
+#define MKOPT_REUSE 0x01
+#define MKOPT_RESET 0x02
static int
-mod_ldap_init(int argc, char **argv)
+make_options(struct ldap_conf *conf, int flags,
+ struct smap_option **ret_options)
{
- struct smap_option init_option[] = {
+ static struct smap_option init_option[] = {
{ SMAP_OPTSTR(config-file), smap_opt_null,
- &init_option, 0, readconf },
- { SMAP_OPTSTR(ssl-ca), smap_opt_string,
- &def_conf.cacert },
- { SMAP_OPTSTR(tls-ca), smap_opt_string,
- &def_conf.cacert },
+ (void*)offsetof(struct ldap_conf, config_file) },
+ { SMAP_OPTSTR(tls_cacert), smap_opt_string,
+ (void*)offsetof(struct ldap_conf, cacert) },
+ { SMAP_OPTSTR(tls-cacert), smap_opt_string,
+ (void*)offsetof(struct ldap_conf, cacert) },
{ SMAP_OPTSTR(uri), smap_opt_string,
- &def_conf.uri },
+ (void*)offsetof(struct ldap_conf, uri) },
{ SMAP_OPTSTR(base), smap_opt_string,
- &def_conf.base },
+ (void*)offsetof(struct ldap_conf, base) },
{ SMAP_OPTSTR(filter), smap_opt_string,
- &def_conf.filter },
+ (void*)offsetof(struct ldap_conf, filter) },
{ SMAP_OPTSTR(binddn), smap_opt_string,
- &def_conf.binddn },
+ (void*)offsetof(struct ldap_conf, binddn) },
{ SMAP_OPTSTR(bindpw), smap_opt_string,
- &def_conf.bindpw },
+ (void*)offsetof(struct ldap_conf, bindpw) },
{ SMAP_OPTSTR(bindpwfile), smap_opt_string,
- &def_conf.bindpwfile },
+ (void*)offsetof(struct ldap_conf, bindpwfile) },
{ SMAP_OPTSTR(positive-reply), smap_opt_string,
- &def_conf.positive_reply },
+ (void*)offsetof(struct ldap_conf, positive_reply) },
{ SMAP_OPTSTR(negative-reply), smap_opt_string,
- &def_conf.negative_reply },
+ (void*)offsetof(struct ldap_conf, negative_reply) },
{ SMAP_OPTSTR(onerror-reply), smap_opt_string,
- &def_conf.onerror_reply },
+ (void*)offsetof(struct ldap_conf, onerror_reply) },
{ NULL }
};
+ int i;
+ struct smap_option *opt;
+
+ if (flags & MKOPT_REUSE)
+ opt = *ret_options;
+ else {
+ opt = malloc(sizeof(init_option));
+ if (!opt)
+ return 1;
+ *ret_options = opt;
+ }
+ memcpy(opt, init_option, sizeof(init_option));
+
+ if (flags & MKOPT_RESET)
+ opt->type = smap_opt_string;
+ for (i = 0; opt[i].name; i++) {
+ opt[i].data = (char*)conf + (size_t) opt[i].data;
+ if ((flags & MKOPT_RESET) && i)
+ opt[i].type = smap_opt_null;
+ }
+
+ return 0;
+}
+
+static int
+mod_ldap_init(int argc, char **argv)
+{
+ struct smap_option *opt;
+ int rc;
dbgid = smap_debug_alloc("ldap");
- if (smap_parseopt(init_option, argc, argv, 0, NULL))
+ if (make_options(&dfl_conf, MKOPT_DEFAULT, &opt)) {
+ smap_error("not enough memory");
return 1;
+ }
- return 0;
+ rc = smap_parseopt(opt, argc, argv, 0, NULL);
+ free(opt);
+ return rc;
}
static char *
@@ -681,46 +719,38 @@ mod_ldap_init_db(const char *dbid, int argc, char **argv)
struct ldap_db *db;
struct ldap_conf conf;
size_t i, j;
+ struct smap_option *opt;
- struct smap_option init_option[] = {
- { SMAP_OPTSTR(config-file), smap_opt_null,
- &init_option, 0, readconf },
- { SMAP_OPTSTR(ssl-ca), smap_opt_string,
- &conf.cacert },
- { SMAP_OPTSTR(tls-ca), smap_opt_string,
- &conf.cacert },
- { SMAP_OPTSTR(uri), smap_opt_string,
- &conf.uri },
- { SMAP_OPTSTR(base), smap_opt_string,
- &conf.base },
-
- { SMAP_OPTSTR(filter), smap_opt_string,
- &conf.filter },
-
- { SMAP_OPTSTR(binddn), smap_opt_string,
- &conf.binddn },
-
- { SMAP_OPTSTR(bindpw), smap_opt_string,
- &conf.bindpw },
- { SMAP_OPTSTR(bindpwfile), smap_opt_string,
- &conf.bindpwfile },
-
- { SMAP_OPTSTR(positive-reply), smap_opt_string,
- &conf.positive_reply },
- { SMAP_OPTSTR(negative-reply), smap_opt_string,
- &conf.negative_reply },
- { SMAP_OPTSTR(onerror-reply), smap_opt_string,
- &conf.onerror_reply },
- { NULL }
- };
+ if (!ldap_conf_cpy(&conf, &dfl_conf))
+ return NULL;
- if (!ldap_conf_cpy(&conf, &def_conf))
+ if (make_options(&conf, MKOPT_RESET, &opt)) {
+ smap_error("not enough memory");
+ ldap_conf_free(&conf);
+ return NULL;
+ }
+
+ if (smap_parseopt(opt, argc, argv, 0, NULL)) {
+ ldap_conf_free(&conf);
return NULL;
+ }
+
+ make_options(&conf, MKOPT_REUSE, &opt);
+
+ if (!conf.config_file && access(dfl_config_file, R_OK) == 0)
+ conf.config_file = strdup(dfl_config_file);
+
+ if (conf.config_file && parse_ldap_conf(conf.config_file, opt)) {
+ free(opt);
+ ldap_conf_free(&conf);
+ }
- if (smap_parseopt(init_option, argc, argv, 0, NULL)) {
+ if (smap_parseopt(opt, argc, argv, 0, NULL)) {
+ free(opt);
ldap_conf_free(&conf);
return NULL;
}
+ free(opt);
if (!conf.filter) {
smap_error("%s: filter must be defined", dbid);

Return to:

Send suggestions and report system problems to the System administrator.