1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/string.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
37 #include <sys/types.h>
41 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
50 #include "slapi/slapi.h"
53 #include "lutil_ldap.h"
59 * defaults for various global variables
61 slap_mask_t global_allows = 0;
62 slap_mask_t global_disallows = 0;
63 int global_gentlehup = 0;
64 int global_idletimeout = 0;
65 int global_writetimeout = 0;
66 char *global_host = NULL;
67 struct berval global_host_bv = BER_BVNULL;
68 char *global_realm = NULL;
69 char *sasl_host = NULL;
70 char **default_passwd_hash = NULL;
71 struct berval default_search_base = BER_BVNULL;
72 struct berval default_search_nbase = BER_BVNULL;
74 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
75 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
77 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
78 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
80 char *slapd_pid_file = NULL;
81 char *slapd_args_file = NULL;
83 int use_reverse_lookup = 0;
86 int slapi_plugins_used = 0;
89 static int fp_getline(FILE *fp, ConfigArgs *c);
90 static void fp_getline_init(ConfigArgs *c);
92 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
93 static char *strtok_quote_ldif(char **line);
96 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
99 c = ch_calloc( 1, sizeof( ConfigArgs ) );
100 if ( c == NULL ) return(NULL);
106 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
111 init_config_argv( ConfigArgs *c )
113 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
114 c->argv_size = ARGS_STEP + 1;
117 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
120 for(i = 0; Conf[i].name; i++)
121 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
122 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
123 if ( !Conf[i].name ) return NULL;
127 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
128 int rc, arg_user, arg_type, arg_syn, iarg;
133 if(Conf->arg_type == ARG_IGNORED) {
134 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
135 c->log, Conf->name, 0);
138 arg_type = Conf->arg_type & ARGS_TYPES;
139 arg_user = Conf->arg_type & ARGS_USERLAND;
140 arg_syn = Conf->arg_type & ARGS_SYNTAX;
142 if((arg_type == ARG_DN) && c->argc == 1) {
146 if(Conf->min_args && (c->argc < Conf->min_args)) {
147 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
148 c->argv[0], Conf->what );
149 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
150 return(ARG_BAD_CONF);
152 if(Conf->max_args && (c->argc > Conf->max_args)) {
153 char *ignored = " ignored";
155 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
156 c->argv[0], Conf->what );
159 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
160 c->log, c->cr_msg, ignored );
161 return(ARG_BAD_CONF);
163 if((arg_syn & ARG_DB) && !c->be) {
164 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
166 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
167 c->log, c->cr_msg, 0);
168 return(ARG_BAD_CONF);
170 if((arg_syn & ARG_PRE_BI) && c->bi) {
171 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
172 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
173 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
174 c->log, c->cr_msg, 0 );
175 return(ARG_BAD_CONF);
177 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
178 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
180 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
181 c->log, c->cr_msg, 0);
182 return(ARG_BAD_CONF);
184 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
185 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
186 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
187 c->log, c->cr_msg, 0);
188 return(ARG_BAD_CONF);
190 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
191 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
193 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
194 c->log, c->cr_msg, 0);
195 return(ARG_BAD_CONF);
198 memset(&c->values, 0, sizeof(c->values));
199 if(arg_type == ARG_STRING) {
201 c->value_string = ch_strdup(c->argv[1]);
202 } else if(arg_type == ARG_BERVAL) {
204 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
205 } else if(arg_type == ARG_DN) {
207 ber_str2bv( c->argv[1], 0, 0, &bv );
208 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
209 if ( rc != LDAP_SUCCESS ) {
210 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
211 c->argv[0], rc, ldap_err2string( rc ));
212 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
213 return(ARG_BAD_CONF);
216 ch_free( c->value_ndn.bv_val );
217 ch_free( c->value_dn.bv_val );
219 } else if(arg_type == ARG_ATDESC) {
220 const char *text = NULL;
222 rc = slap_str2ad( c->argv[1], &c->value_ad, &text );
223 if ( rc != LDAP_SUCCESS ) {
224 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid AttributeDescription %d (%s)",
225 c->argv[0], rc, text );
226 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
227 return(ARG_BAD_CONF);
229 } else { /* all numeric */
231 iarg = 0; larg = 0; barg = 0;
234 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
235 snprintf( c->cr_msg, sizeof( c->cr_msg ),
236 "<%s> unable to parse \"%s\" as int",
237 c->argv[0], c->argv[1] );
238 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
239 c->log, c->cr_msg, 0);
240 return(ARG_BAD_CONF);
244 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
245 snprintf( c->cr_msg, sizeof( c->cr_msg ),
246 "<%s> unable to parse \"%s\" as unsigned int",
247 c->argv[0], c->argv[1] );
248 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
249 c->log, c->cr_msg, 0);
250 return(ARG_BAD_CONF);
254 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
255 snprintf( c->cr_msg, sizeof( c->cr_msg ),
256 "<%s> unable to parse \"%s\" as long",
257 c->argv[0], c->argv[1] );
258 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
259 c->log, c->cr_msg, 0);
260 return(ARG_BAD_CONF);
263 case ARG_BER_LEN_T: {
265 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
266 snprintf( c->cr_msg, sizeof( c->cr_msg ),
267 "<%s> unable to parse \"%s\" as ber_len_t",
268 c->argv[0], c->argv[1] );
269 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
270 c->log, c->cr_msg, 0);
271 return(ARG_BAD_CONF);
278 } else if ( !strcasecmp(c->argv[1], "on") ||
279 !strcasecmp(c->argv[1], "true") ||
280 !strcasecmp(c->argv[1], "yes") )
283 } else if ( !strcasecmp(c->argv[1], "off") ||
284 !strcasecmp(c->argv[1], "false") ||
285 !strcasecmp(c->argv[1], "no") )
289 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
291 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
292 c->log, c->cr_msg, 0 );
293 return(ARG_BAD_CONF);
297 j = (arg_type & ARG_NONZERO) ? 1 : 0;
298 if(iarg < j && larg < j && barg < j ) {
299 larg = larg ? larg : (barg ? barg : iarg);
300 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
302 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
303 c->log, c->cr_msg, 0 );
304 return(ARG_BAD_CONF);
308 case ARG_INT: c->value_int = iarg; break;
309 case ARG_UINT: c->value_uint = uiarg; break;
310 case ARG_LONG: c->value_long = larg; break;
311 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
317 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
321 arg_type = Conf->arg_type;
322 if(arg_type & ARG_MAGIC) {
323 if(!c->be) c->be = frontendDB;
325 rc = (*((ConfigDriver*)Conf->arg_item))(c);
327 if(c->be == frontendDB) c->be = NULL;
330 if ( !c->cr_msg[0] ) {
331 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
333 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
334 c->log, c->cr_msg, 0 );
336 return(ARG_BAD_CONF);
340 if(arg_type & ARG_OFFSET) {
341 if (c->be && c->table == Cft_Database)
342 ptr = c->be->be_private;
344 ptr = c->bi->bi_private;
346 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
348 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
349 c->log, c->cr_msg, 0);
350 return(ARG_BAD_CONF);
352 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
353 } else if (arg_type & ARGS_TYPES) {
354 ptr = Conf->arg_item;
356 if(arg_type & ARGS_TYPES)
357 switch(arg_type & ARGS_TYPES) {
359 case ARG_INT: *(int*)ptr = c->value_int; break;
360 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break;
361 case ARG_LONG: *(long*)ptr = c->value_long; break;
362 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
364 char *cc = *(char**)ptr;
366 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
367 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
368 c->log, Conf->name, 0 );
369 return(ARG_BAD_CONF);
373 *(char **)ptr = c->value_string;
377 *(struct berval *)ptr = c->value_bv;
380 *(AttributeDescription **)ptr = c->value_ad;
386 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
389 arg_type = Conf->arg_type;
390 if(arg_type == ARG_IGNORED) {
391 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
392 c->log, Conf->name, 0);
395 rc = config_check_vals( Conf, c, 0 );
397 return config_set_vals( Conf, c );
401 config_del_vals(ConfigTable *cf, ConfigArgs *c)
405 /* If there is no handler, just ignore it */
406 if ( cf->arg_type & ARG_MAGIC ) {
407 c->op = LDAP_MOD_DELETE;
408 c->type = cf->arg_type & ARGS_USERLAND;
409 rc = (*((ConfigDriver*)cf->arg_item))(c);
415 config_get_vals(ConfigTable *cf, ConfigArgs *c)
421 if ( cf->arg_type & ARG_IGNORED ) {
425 memset(&c->values, 0, sizeof(c->values));
426 c->rvalue_vals = NULL;
427 c->rvalue_nvals = NULL;
428 c->op = SLAP_CONFIG_EMIT;
429 c->type = cf->arg_type & ARGS_USERLAND;
431 if ( cf->arg_type & ARG_MAGIC ) {
432 rc = (*((ConfigDriver*)cf->arg_item))(c);
435 if ( cf->arg_type & ARG_OFFSET ) {
436 if (c->be && c->table == Cft_Database)
437 ptr = c->be->be_private;
439 ptr = c->bi->bi_private;
442 ptr = (void *)((char *)ptr + (long)cf->arg_item);
447 switch(cf->arg_type & ARGS_TYPES) {
449 case ARG_INT: c->value_int = *(int *)ptr; break;
450 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break;
451 case ARG_LONG: c->value_long = *(long *)ptr; break;
452 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
455 c->value_string = ch_strdup(*(char **)ptr);
458 ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
460 c->value_ad = *(AttributeDescription **)ptr; break;
463 if ( cf->arg_type & ARGS_TYPES) {
466 switch(cf->arg_type & ARGS_TYPES) {
467 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
468 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
469 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
470 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
471 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
472 c->value_int ? "TRUE" : "FALSE"); break;
474 if ( c->value_string && c->value_string[0]) {
475 ber_str2bv( c->value_string, 0, 0, &bv);
481 if ( !BER_BVISEMPTY( &c->value_bv )) {
489 bv = c->value_ad->ad_cname;
498 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
501 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
502 ber_bvarray_add(&c->rvalue_vals, &bv);
503 } else if ( !BER_BVISNULL( &bv ) ) {
504 value_add_one(&c->rvalue_vals, &bv);
506 /* else: maybe c->rvalue_vals already set? */
512 init_config_attrs(ConfigTable *ct) {
515 for (i=0; ct[i].name; i++ ) {
516 if ( !ct[i].attribute ) continue;
517 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
519 fprintf( stderr, "init_config_attrs: register_at failed\n" );
528 init_config_ocs( ConfigOCs *ocs ) {
531 for (i=0;ocs[i].co_def;i++) {
532 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
534 fprintf( stderr, "init_config_ocs: register_oc failed\n" );
541 /* Split an LDIF line into space-separated tokens. Words may be grouped
542 * by quotes. A quoted string may begin in the middle of a word, but must
543 * end at the end of the word (be followed by whitespace or EOS). Any other
544 * quotes are passed through unchanged. All other characters are passed
548 strtok_quote_ldif( char **line )
550 char *beg, *ptr, *quote=NULL;
558 while( isspace( (unsigned char) *ptr )) ptr++;
569 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
579 if ( isspace( (unsigned char) *ptr )) {
585 while ( quote < ptr ) {
593 while ( isspace( (unsigned char) *ptr )) ptr++;
600 config_parse_ldif( ConfigArgs *c )
603 c->tline = ch_strdup(c->line);
606 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
608 if ( c->argc >= c->argv_size ) {
609 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
612 c->argv_size += ARGS_STEP;
615 c->argv[c->argc] = NULL;
619 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
623 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
624 ct->ad->ad_cname.bv_val, valx );
626 c->argv[0] = ct->ad->ad_cname.bv_val;
628 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
629 c->argv[c->argc] = c->line;
631 c->argv[c->argc] = NULL;
634 config_parse_ldif( c );
636 rc = config_check_vals( ct, c, 1 );
641 rc = LDAP_CONSTRAINT_VIOLATION;
647 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
651 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
652 ct->ad->ad_cname.bv_val, valx );
654 c->argv[0] = ct->ad->ad_cname.bv_val;
656 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
657 c->argv[c->argc] = c->line;
659 c->argv[c->argc] = NULL;
662 config_parse_ldif( c );
664 c->op = LDAP_MOD_ADD;
665 rc = config_add_vals( ct, c );
672 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
680 c = ch_calloc( 1, sizeof( ConfigArgs ) );
686 memcpy( c, cf, sizeof( ConfigArgs ) );
688 c->depth = depth; /* XXX */
695 init_config_argv( c );
697 if ( stat( fname, &s ) != 0 ) {
699 Debug(LDAP_DEBUG_ANY,
700 "could not stat config file \"%s\": %s (%d)\n",
701 fname, strerror(errno), errno);
706 if ( !S_ISREG( s.st_mode ) ) {
708 Debug(LDAP_DEBUG_ANY,
709 "regular file expected, got \"%s\"\n",
715 fp = fopen( fname, "r" );
718 Debug(LDAP_DEBUG_ANY,
719 "could not open config file \"%s\": %s (%d)\n",
720 fname, strerror(errno), errno);
725 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
731 while ( fp_getline( fp, c ) ) {
732 /* skip comments and blank lines */
733 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
737 snprintf( c->log, sizeof( c->log ), "%s: line %d",
738 c->fname, c->lineno );
742 if ( config_fp_parse_line( c ) ) {
748 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
754 c->op = SLAP_CONFIG_ADD;
756 ct = config_find_keyword( cft, c );
758 c->table = Cft_Global;
759 rc = config_add_vals( ct, c );
762 if ( rc & ARGS_USERLAND ) {
763 /* XXX a usertype would be opaque here */
764 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
765 c->log, c->argv[0], 0);
769 } else if ( rc == ARG_BAD_CONF ) {
774 } else if ( c->bi && !c->be ) {
775 rc = SLAP_CONF_UNKNOWN;
776 if ( c->bi->bi_cf_ocs ) {
777 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
779 c->table = c->bi->bi_cf_ocs->co_type;
780 rc = config_add_vals( ct, c );
783 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
784 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
789 case SLAP_CONF_UNKNOWN:
790 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
791 "<%s> inside backend info definition.\n",
792 c->log, *c->argv, 0);
799 } else if ( c->be && c->be != frontendDB ) {
800 rc = SLAP_CONF_UNKNOWN;
801 if ( c->be->be_cf_ocs ) {
802 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
804 c->table = c->be->be_cf_ocs->co_type;
805 rc = config_add_vals( ct, c );
808 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
809 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
812 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
814 /* global overlays may need
815 * definitions inside other databases...
817 rc = (*frontendDB->be_config)( frontendDB,
818 c->fname, (int)c->lineno, c->argc, c->argv );
825 case SLAP_CONF_UNKNOWN:
826 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
827 "<%s> inside backend database definition.\n",
828 c->log, *c->argv, 0);
835 } else if ( frontendDB->be_config ) {
836 rc = (*frontendDB->be_config)( frontendDB,
837 c->fname, (int)c->lineno, c->argc, c->argv);
840 case SLAP_CONF_UNKNOWN:
841 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
842 "<%s> inside global database definition.\n",
843 c->log, *c->argv, 0);
852 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
853 "<%s> outside backend info and database definitions.\n",
854 c->log, *c->argv, 0);
874 /* restrictops, allows, disallows, requires, loglevel */
877 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
879 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
880 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
886 verb_to_mask(const char *word, slap_verbmasks *v) {
888 ber_str2bv( word, 0, 0, &bword );
889 return bverb_to_mask( &bword, v );
893 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
895 for(i = 1; i < argc; i++) {
896 j = verb_to_mask(argv[i], v);
897 if(BER_BVISNULL(&v[j].word)) return i;
898 while (!v[j].mask) j--;
904 /* Mask keywords that represent multiple bits should occur before single
905 * bit keywords in the verbmasks array.
908 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
912 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
913 if (!v[i].mask) continue;
914 if (( m & v[i].mask ) == v[i].mask ) {
915 value_add_one( bva, &v[i].word );
926 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
930 assert( *vp == NULL );
932 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
934 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
936 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
937 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
938 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
941 BER_BVZERO( &(*vp)[ i ].word );
947 slap_verbmasks_destroy( slap_verbmasks *v )
953 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
954 ch_free( v[ i ].word.bv_val );
963 slap_verbmasks_append(
967 slap_mask_t *ignore )
972 return LDAP_OPERATIONS_ERROR;
975 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
976 if ( !(*vp)[ i ].mask ) continue;
978 if ( ignore != NULL ) {
981 for ( j = 0; ignore[ j ] != 0; j++ ) {
982 if ( (*vp)[ i ].mask == ignore[ j ] ) {
988 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
989 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
990 /* already set; ignore */
994 return LDAP_TYPE_OR_VALUE_EXISTS;
997 if ( m & (*vp)[ i ].mask ) {
999 return LDAP_CONSTRAINT_VIOLATION;
1004 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1005 ber_dupbv( &(*vp)[ i ].word, v );
1006 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1007 BER_BVZERO( &(*vp)[ i + 1 ].word );
1009 return LDAP_SUCCESS;
1013 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1016 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1017 if ( m == v[i].mask ) {
1027 /* register a new verbmask */
1029 slap_verbmask_register( slap_verbmasks *vm_, slap_verbmasks **vmp, struct berval *bv, int mask )
1031 slap_verbmasks *vm = *vmp;
1034 /* check for duplicate word */
1035 /* NOTE: we accept duplicate codes; the first occurrence will be used
1036 * when mapping from mask to verb */
1037 i = verb_to_mask( bv->bv_val, vm );
1038 if ( !BER_BVISNULL( &vm[ i ].word ) ) {
1042 for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
1046 /* first time: duplicate array */
1047 vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
1048 for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
1050 ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
1051 *((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
1055 vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
1058 ber_dupbv( &vm[ i ].word, bv );
1059 *((slap_mask_t*)&vm[ i ].mask) = mask;
1061 BER_BVZERO( &vm[ i+1 ].word );
1068 static slap_verbmasks slap_ldap_response_code_[] = {
1069 { BER_BVC("success"), LDAP_SUCCESS },
1071 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR },
1072 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR },
1073 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED },
1074 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED },
1075 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE },
1076 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE },
1078 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED },
1079 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED },
1080 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED },
1081 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED },
1082 #if 0 /* not LDAPv3 */
1083 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS },
1086 { BER_BVC("referral"), LDAP_REFERRAL },
1087 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED },
1088 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
1089 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED },
1090 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS },
1092 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE },
1093 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE },
1094 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING },
1095 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION },
1096 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS },
1097 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX },
1099 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT },
1100 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM },
1101 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX },
1102 #if 0 /* not LDAPv3 */
1103 { BER_BVC("isLeaf"), LDAP_IS_LEAF },
1105 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM },
1107 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE },
1108 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH },
1109 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS },
1110 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS },
1112 { BER_BVC("busy"), LDAP_BUSY },
1113 { BER_BVC("unavailable"), LDAP_UNAVAILABLE },
1114 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM },
1115 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT },
1117 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION },
1118 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION },
1119 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF },
1120 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN },
1121 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS },
1122 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS },
1123 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE },
1124 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS },
1126 { BER_BVC("other"), LDAP_OTHER },
1128 /* extension-specific */
1130 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED },
1131 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION },
1132 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA },
1133 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME },
1134 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED },
1136 { BER_BVC("cancelled"), LDAP_CANCELLED },
1137 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION },
1138 { BER_BVC("tooLate"), LDAP_TOO_LATE },
1139 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL },
1141 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED },
1143 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED },
1145 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED },
1147 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION },
1152 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_;
1155 slap_ldap_response_code_register( struct berval *bv, int err )
1157 return slap_verbmask_register( slap_ldap_response_code_,
1158 &slap_ldap_response_code, bv, err );
1162 static slap_verbmasks tlskey[] = {
1163 { BER_BVC("no"), SB_TLS_OFF },
1164 { BER_BVC("yes"), SB_TLS_ON },
1165 { BER_BVC("critical"), SB_TLS_CRITICAL },
1169 static slap_verbmasks crlkeys[] = {
1170 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE },
1171 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER },
1172 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL },
1176 static slap_verbmasks vfykeys[] = {
1177 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER },
1178 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND },
1179 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY },
1180 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD },
1185 static slap_verbmasks methkey[] = {
1186 { BER_BVC("none"), LDAP_AUTH_NONE },
1187 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1188 #ifdef HAVE_CYRUS_SASL
1189 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1194 static slap_verbmasks versionkey[] = {
1195 { BER_BVC("2"), LDAP_VERSION2 },
1196 { BER_BVC("3"), LDAP_VERSION3 },
1200 static slap_cf_aux_table bindkey[] = {
1201 { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1202 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1203 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1204 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1205 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1206 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1207 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1208 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1209 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1210 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1211 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1212 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1214 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1216 /* NOTE: replace "13" with the actual index
1217 * of the first TLS-related line */
1218 #define aux_TLS (bindkey+13) /* beginning of TLS keywords */
1220 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1221 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1222 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1223 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1224 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1225 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1226 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 1, NULL },
1227 #ifdef HAVE_OPENSSL_CRL
1228 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1231 { BER_BVNULL, 0, 0, 0, NULL }
1236 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1237 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1240 * 'U': unsigned long
1244 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1246 int rc = SLAP_CONF_UNKNOWN;
1247 slap_cf_aux_table *tab;
1249 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1250 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1255 unsigned long *ulptr;
1256 struct berval *bptr;
1257 const char *val = word + tab->key.bv_len;
1259 switch ( tab->type ) {
1261 cptr = (char **)((char *)dst + tab->off);
1262 *cptr = ch_strdup( val );
1267 bptr = (struct berval *)((char *)dst + tab->off);
1268 if ( tab->aux != NULL ) {
1270 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1272 ber_str2bv( val, 0, 0, &dn );
1273 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1276 ber_str2bv( val, 0, 1, bptr );
1282 iptr = (int *)((char *)dst + tab->off);
1284 if ( tab->aux != NULL ) {
1285 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1287 assert( aux != NULL );
1290 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1291 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1292 *iptr = aux[j].mask;
1299 rc = lutil_atoix( iptr, val, 0 );
1304 uptr = (unsigned *)((char *)dst + tab->off);
1306 rc = lutil_atoux( uptr, val, 0 );
1310 lptr = (long *)((char *)dst + tab->off);
1312 rc = lutil_atolx( lptr, val, 0 );
1316 ulptr = (unsigned long *)((char *)dst + tab->off);
1318 rc = lutil_atoulx( ulptr, val, 0 );
1323 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1335 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1337 char buf[AC_LINE_MAX], *ptr;
1338 slap_cf_aux_table *tab;
1342 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1347 unsigned long *ulptr;
1348 struct berval *bptr;
1350 cptr = (char **)((char *)src + tab->off);
1352 switch ( tab->type ) {
1354 bptr = (struct berval *)((char *)src + tab->off);
1355 cptr = &bptr->bv_val;
1360 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1361 if ( tab->quote ) *ptr++ = '"';
1362 ptr = lutil_strcopy( ptr, *cptr );
1363 if ( tab->quote ) *ptr++ = '"';
1368 iptr = (int *)((char *)src + tab->off);
1370 if ( tab->aux != NULL ) {
1371 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1373 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1374 if ( *iptr == aux[i].mask ) {
1376 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1377 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1384 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1385 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1390 uptr = (unsigned *)((char *)src + tab->off);
1392 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1393 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1397 lptr = (long *)((char *)src + tab->off);
1399 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1400 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1404 ulptr = (unsigned long *)((char *)src + tab->off);
1406 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1407 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1415 tmp.bv_len = ptr - buf;
1416 ber_dupbv( bv, &tmp );
1421 slap_tls_get_config( LDAP *ld, int opt, char **val )
1424 slap_verbmasks *keys;
1429 case LDAP_OPT_X_TLS_CRLCHECK:
1432 case LDAP_OPT_X_TLS_REQUIRE_CERT:
1435 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
1437 ldap_pvt_tls_get_option( ld, opt, &ival );
1438 snprintf( buf, sizeof( buf ), "%d.%d",
1439 ( ival >> 8 ) & 0xff, ival & 0xff );
1440 *val = ch_strdup( buf );
1446 ldap_pvt_tls_get_option( ld, opt, &ival );
1447 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1448 if (keys[i].mask == ival) {
1449 *val = ch_strdup( keys[i].word.bv_val );
1458 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1461 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1462 bc->sb_tls_do_init = 1;
1470 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1473 return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1479 bindconf_parse( const char *word, slap_bindconf *bc )
1482 /* Detect TLS config changes explicitly */
1483 if ( bindconf_tls_parse( word, bc ) == 0 ) {
1487 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1491 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1493 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1496 void bindconf_free( slap_bindconf *bc ) {
1497 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1498 ch_free( bc->sb_uri.bv_val );
1499 BER_BVZERO( &bc->sb_uri );
1501 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1502 ch_free( bc->sb_binddn.bv_val );
1503 BER_BVZERO( &bc->sb_binddn );
1505 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1506 ch_free( bc->sb_cred.bv_val );
1507 BER_BVZERO( &bc->sb_cred );
1509 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1510 ch_free( bc->sb_saslmech.bv_val );
1511 BER_BVZERO( &bc->sb_saslmech );
1513 if ( bc->sb_secprops ) {
1514 ch_free( bc->sb_secprops );
1515 bc->sb_secprops = NULL;
1517 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1518 ch_free( bc->sb_realm.bv_val );
1519 BER_BVZERO( &bc->sb_realm );
1521 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1522 ch_free( bc->sb_authcId.bv_val );
1523 BER_BVZERO( &bc->sb_authcId );
1525 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1526 ch_free( bc->sb_authzId.bv_val );
1527 BER_BVZERO( &bc->sb_authzId );
1530 if ( bc->sb_tls_cert ) {
1531 ch_free( bc->sb_tls_cert );
1532 bc->sb_tls_cert = NULL;
1534 if ( bc->sb_tls_key ) {
1535 ch_free( bc->sb_tls_key );
1536 bc->sb_tls_key = NULL;
1538 if ( bc->sb_tls_cacert ) {
1539 ch_free( bc->sb_tls_cacert );
1540 bc->sb_tls_cacert = NULL;
1542 if ( bc->sb_tls_cacertdir ) {
1543 ch_free( bc->sb_tls_cacertdir );
1544 bc->sb_tls_cacertdir = NULL;
1546 if ( bc->sb_tls_reqcert ) {
1547 ch_free( bc->sb_tls_reqcert );
1548 bc->sb_tls_reqcert = NULL;
1550 if ( bc->sb_tls_cipher_suite ) {
1551 ch_free( bc->sb_tls_cipher_suite );
1552 bc->sb_tls_cipher_suite = NULL;
1554 if ( bc->sb_tls_protocol_min ) {
1555 ch_free( bc->sb_tls_protocol_min );
1556 bc->sb_tls_protocol_min = NULL;
1558 #ifdef HAVE_OPENSSL_CRL
1559 if ( bc->sb_tls_crlcheck ) {
1560 ch_free( bc->sb_tls_crlcheck );
1561 bc->sb_tls_crlcheck = NULL;
1568 bindconf_tls_defaults( slap_bindconf *bc )
1571 if ( bc->sb_tls_do_init ) {
1572 if ( !bc->sb_tls_cacert )
1573 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1574 &bc->sb_tls_cacert );
1575 if ( !bc->sb_tls_cacertdir )
1576 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1577 &bc->sb_tls_cacertdir );
1578 if ( !bc->sb_tls_cert )
1579 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1581 if ( !bc->sb_tls_key )
1582 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1584 if ( !bc->sb_tls_cipher_suite )
1585 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1586 &bc->sb_tls_cipher_suite );
1587 if ( !bc->sb_tls_reqcert )
1588 bc->sb_tls_reqcert = ch_strdup("demand");
1589 #ifdef HAVE_OPENSSL_CRL
1590 if ( !bc->sb_tls_crlcheck )
1591 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1592 &bc->sb_tls_crlcheck );
1604 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1605 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1606 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1607 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1608 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1609 { "tls_protocol_min", offsetof(slap_bindconf, sb_tls_protocol_min), LDAP_OPT_X_TLS_PROTOCOL_MIN },
1613 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1615 int i, rc, newctx = 0, res = 0;
1616 char *ptr = (char *)bc, **word;
1618 bc->sb_tls_do_init = 0;
1620 for (i=0; bindtlsopts[i].opt; i++) {
1621 word = (char **)(ptr + bindtlsopts[i].offset);
1623 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1625 Debug( LDAP_DEBUG_ANY,
1626 "bindconf_tls_set: failed to set %s to %s\n",
1627 bindtlsopts[i].key, *word, 0 );
1633 if ( bc->sb_tls_reqcert ) {
1634 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1635 bc->sb_tls_reqcert );
1637 Debug( LDAP_DEBUG_ANY,
1638 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1639 bc->sb_tls_reqcert, 0, 0 );
1644 if ( bc->sb_tls_protocol_min ) {
1645 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN,
1646 bc->sb_tls_protocol_min );
1648 Debug( LDAP_DEBUG_ANY,
1649 "bindconf_tls_set: failed to set tls_protocol_min to %s\n",
1650 bc->sb_tls_protocol_min, 0, 0 );
1655 #ifdef HAVE_OPENSSL_CRL
1656 if ( bc->sb_tls_crlcheck ) {
1657 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1658 bc->sb_tls_crlcheck );
1660 Debug( LDAP_DEBUG_ANY,
1661 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1662 bc->sb_tls_crlcheck, 0, 0 );
1671 if ( bc->sb_tls_ctx ) {
1672 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1673 bc->sb_tls_ctx = NULL;
1675 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1679 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1687 * connect to a client using the bindconf data
1688 * note: should move "version" into bindconf...
1691 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1697 /* Init connection to master */
1698 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1699 if ( rc != LDAP_SUCCESS ) {
1700 Debug( LDAP_DEBUG_ANY,
1701 "slap_client_connect: "
1702 "ldap_initialize(%s) failed (%d)\n",
1703 sb->sb_uri.bv_val, rc, 0 );
1707 if ( sb->sb_version != 0 ) {
1708 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1709 (const void *)&sb->sb_version );
1712 if ( sb->sb_timeout_api ) {
1713 tv.tv_sec = sb->sb_timeout_api;
1715 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1718 if ( sb->sb_timeout_net ) {
1719 tv.tv_sec = sb->sb_timeout_net;
1721 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1725 if ( sb->sb_tls_do_init ) {
1726 rc = bindconf_tls_set( sb, ld );
1728 } else if ( sb->sb_tls_ctx ) {
1729 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1734 Debug( LDAP_DEBUG_ANY,
1735 "slap_client_connect: "
1736 "URI=%s TLS context initialization failed (%d)\n",
1737 sb->sb_uri.bv_val, rc, 0 );
1744 rc = ldap_start_tls_s( ld, NULL, NULL );
1745 if ( rc != LDAP_SUCCESS ) {
1746 Debug( LDAP_DEBUG_ANY,
1747 "slap_client_connect: URI=%s "
1748 "%s, ldap_start_tls failed (%d)\n",
1750 sb->sb_tls == SB_TLS_CRITICAL ?
1751 "Error" : "Warning",
1753 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1759 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1760 #ifdef HAVE_CYRUS_SASL
1763 if ( sb->sb_secprops != NULL ) {
1764 rc = ldap_set_option( ld,
1765 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1767 if( rc != LDAP_OPT_SUCCESS ) {
1768 Debug( LDAP_DEBUG_ANY,
1769 "slap_client_connect: "
1770 "error, ldap_set_option "
1771 "(%s,SECPROPS,\"%s\") failed!\n",
1772 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1777 defaults = lutil_sasl_defaults( ld,
1778 sb->sb_saslmech.bv_val,
1779 sb->sb_realm.bv_val,
1780 sb->sb_authcId.bv_val,
1782 sb->sb_authzId.bv_val );
1783 if ( defaults == NULL ) {
1788 rc = ldap_sasl_interactive_bind_s( ld,
1789 sb->sb_binddn.bv_val,
1790 sb->sb_saslmech.bv_val,
1793 lutil_sasl_interact,
1796 lutil_sasl_freedefs( defaults );
1798 /* FIXME: different error behaviors according to
1800 * 2) on err policy : exit, retry, backoff ...
1802 if ( rc != LDAP_SUCCESS ) {
1803 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1805 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1806 "ldap_sasl_interactive_bind_s failed (%d)\n",
1807 sb->sb_uri.bv_val, rc, 0 );
1809 /* FIXME (see above comment) */
1810 /* if Kerberos credentials cache is not active, retry */
1811 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1812 rc == LDAP_LOCAL_ERROR )
1814 rc = LDAP_SERVER_DOWN;
1819 #else /* HAVE_CYRUS_SASL */
1820 /* Should never get here, we trapped this at config time */
1822 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1827 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1828 rc = ldap_sasl_bind_s( ld,
1829 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1830 &sb->sb_cred, NULL, NULL, NULL );
1831 if ( rc != LDAP_SUCCESS ) {
1832 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1834 "ldap_sasl_bind_s failed (%d)\n",
1835 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1843 ldap_unbind_ext( ld, NULL, NULL );
1854 /* -------------------------------------- */
1858 strtok_quote( char *line, char *sep, char **quote_ptr )
1865 if ( line != NULL ) {
1868 while ( *next && strchr( sep, *next ) ) {
1872 if ( *next == '\0' ) {
1878 for ( inquote = 0; *next; ) {
1886 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1892 next + 1, strlen( next + 1 ) + 1 );
1893 next++; /* dont parse the escaped character */
1898 if ( strchr( sep, *next ) != NULL ) {
1912 static char buf[AC_LINE_MAX];
1914 static size_t lmax, lcur;
1916 #define CATLINE( buf ) \
1918 size_t len = strlen( buf ); \
1919 while ( lcur + len + 1 > lmax ) { \
1920 lmax += AC_LINE_MAX; \
1921 line = (char *) ch_realloc( line, lmax ); \
1923 strcpy( line + lcur, buf ); \
1928 fp_getline_init(ConfigArgs *c) {
1934 fp_getline( FILE *fp, ConfigArgs *c )
1942 /* avoid stack of bufs */
1943 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1949 while ( fgets( buf, sizeof( buf ), fp ) ) {
1950 p = strchr( buf, '\n' );
1952 if ( p > buf && p[-1] == '\r' ) {
1960 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1967 if ( !isspace( (unsigned char)buf[0] ) ) {
1978 return(line[0] ? 1 : 0);
1982 config_fp_parse_line(ConfigArgs *c)
1985 static char *const hide[] = {
1986 "rootpw", "replica", "syncrepl", /* in slapd */
1987 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
1988 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
1989 "pseudorootpw", /* in back-meta */
1990 "dbpasswd", /* in back-sql */
1994 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1996 c->tline = ch_strdup(c->line);
1997 token = strtok_quote(c->tline, " \t", "e_ptr);
1999 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2000 if(quote_ptr) *quote_ptr = ' ';
2001 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
2002 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2003 if(quote_ptr) *quote_ptr = '\0';
2005 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
2006 if(c->argc >= c->argv_size) {
2008 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2010 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
2014 c->argv_size += ARGS_STEP;
2018 c->argv[c->argc++] = token;
2020 c->argv[c->argc] = NULL;
2027 ucdata_unload( UCDATA_ALL );
2029 /* NOTE: in case of early exit, frontendDB can be NULL */
2030 if ( frontendDB->be_schemandn.bv_val )
2031 free( frontendDB->be_schemandn.bv_val );
2032 if ( frontendDB->be_schemadn.bv_val )
2033 free( frontendDB->be_schemadn.bv_val );
2034 if ( frontendDB->be_acl )
2035 acl_destroy( frontendDB->be_acl );
2038 if ( slapd_args_file )
2039 free ( slapd_args_file );
2040 if ( slapd_pid_file )
2041 free ( slapd_pid_file );
2042 if ( default_passwd_hash )
2043 ldap_charray_free( default_passwd_hash );
2047 slap_str2clist( char ***out, char *in, const char *brkstr )
2055 /* find last element in list */
2056 for (i = 0; *out && (*out)[i]; i++);
2058 /* protect the input string from strtok */
2059 str = ch_strdup( in );
2061 if ( *str == '\0' ) {
2066 /* Count words in string */
2068 for ( s = str; *s; s++ ) {
2069 if ( strchr( brkstr, *s ) != NULL ) {
2074 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2076 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2078 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2080 *new = ch_strdup( s );
2089 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
2090 int argc, char **argv )
2092 ConfigArgs c = { 0 };
2103 c.op = SLAP_CONFIG_ADD;
2104 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
2106 rc = SLAP_CONF_UNKNOWN;
2107 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
2109 c.table = be->be_cf_ocs->co_type;
2110 rc = config_add_vals( ct, &c );