1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 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>
36 #include <ac/unistd.h>
38 #include <sys/types.h>
42 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
47 #include "slapi/slapi.h"
50 #include "lutil_ldap.h"
56 * defaults for various global variables
58 slap_mask_t global_allows = 0;
59 slap_mask_t global_disallows = 0;
60 int global_gentlehup = 0;
61 int global_idletimeout = 0;
62 int global_writetimeout = 0;
63 char *global_host = NULL;
64 struct berval global_host_bv = BER_BVNULL;
65 char *global_realm = NULL;
66 char *sasl_host = NULL;
67 char **default_passwd_hash = NULL;
68 struct berval default_search_base = BER_BVNULL;
69 struct berval default_search_nbase = BER_BVNULL;
71 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
72 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
74 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
75 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
77 char *slapd_pid_file = NULL;
78 char *slapd_args_file = NULL;
80 int use_reverse_lookup = 0;
83 int slapi_plugins_used = 0;
86 static int fp_getline(FILE *fp, ConfigArgs *c);
87 static void fp_getline_init(ConfigArgs *c);
89 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
90 static char *strtok_quote_ldif(char **line);
93 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
96 c = ch_calloc( 1, sizeof( ConfigArgs ) );
97 if ( c == NULL ) return(NULL);
103 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
108 init_config_argv( ConfigArgs *c )
110 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
111 c->argv_size = ARGS_STEP + 1;
114 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
117 for(i = 0; Conf[i].name; i++)
118 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
119 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
120 if ( !Conf[i].name ) return NULL;
124 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
125 int rc, arg_user, arg_type, arg_syn, iarg;
131 if(Conf->arg_type == ARG_IGNORED) {
132 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
133 c->log, Conf->name, 0);
136 arg_type = Conf->arg_type & ARGS_TYPES;
137 arg_user = Conf->arg_type & ARGS_USERLAND;
138 arg_syn = Conf->arg_type & ARGS_SYNTAX;
140 if((arg_type == ARG_DN) && c->argc == 1) {
144 if(Conf->min_args && (c->argc < Conf->min_args)) {
145 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
146 c->argv[0], Conf->what ? Conf->what : "" );
147 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
148 return(ARG_BAD_CONF);
150 if(Conf->max_args && (c->argc > Conf->max_args)) {
151 char *ignored = " ignored";
153 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
154 c->argv[0], Conf->what );
157 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
158 c->log, c->cr_msg, ignored );
159 return(ARG_BAD_CONF);
161 if((arg_syn & ARG_DB) && !c->be) {
162 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
164 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
165 c->log, c->cr_msg, 0);
166 return(ARG_BAD_CONF);
168 if((arg_syn & ARG_PRE_BI) && c->bi) {
169 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
170 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
171 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
172 c->log, c->cr_msg, 0 );
173 return(ARG_BAD_CONF);
175 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
176 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
178 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
179 c->log, c->cr_msg, 0);
180 return(ARG_BAD_CONF);
182 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
183 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
184 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
185 c->log, c->cr_msg, 0);
186 return(ARG_BAD_CONF);
188 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
189 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
191 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
192 c->log, c->cr_msg, 0);
193 return(ARG_BAD_CONF);
196 memset(&c->values, 0, sizeof(c->values));
197 if(arg_type == ARG_STRING) {
199 c->value_string = ch_strdup(c->argv[1]);
200 } else if(arg_type == ARG_BERVAL) {
202 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
203 } else if(arg_type == ARG_DN) {
205 ber_str2bv( c->argv[1], 0, 0, &bv );
206 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
207 if ( rc != LDAP_SUCCESS ) {
208 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
209 c->argv[0], rc, ldap_err2string( rc ));
210 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
211 return(ARG_BAD_CONF);
214 ch_free( c->value_ndn.bv_val );
215 ch_free( c->value_dn.bv_val );
217 } else if(arg_type == ARG_ATDESC) {
218 const char *text = NULL;
220 rc = slap_str2ad( c->argv[1], &c->value_ad, &text );
221 if ( rc != LDAP_SUCCESS ) {
222 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid AttributeDescription %d (%s)",
223 c->argv[0], rc, text );
224 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
225 return(ARG_BAD_CONF);
227 } else { /* all numeric */
229 iarg = 0; larg = 0; barg = 0;
232 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
233 snprintf( c->cr_msg, sizeof( c->cr_msg ),
234 "<%s> unable to parse \"%s\" as int",
235 c->argv[0], c->argv[1] );
236 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
237 c->log, c->cr_msg, 0);
238 return(ARG_BAD_CONF);
242 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
243 snprintf( c->cr_msg, sizeof( c->cr_msg ),
244 "<%s> unable to parse \"%s\" as unsigned int",
245 c->argv[0], c->argv[1] );
246 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
247 c->log, c->cr_msg, 0);
248 return(ARG_BAD_CONF);
252 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
253 snprintf( c->cr_msg, sizeof( c->cr_msg ),
254 "<%s> unable to parse \"%s\" as long",
255 c->argv[0], c->argv[1] );
256 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
257 c->log, c->cr_msg, 0);
258 return(ARG_BAD_CONF);
262 if ( lutil_atoulx( &ularg, c->argv[1], 0 ) != 0 ) {
263 snprintf( c->cr_msg, sizeof( c->cr_msg ),
264 "<%s> unable to parse \"%s\" as unsigned long",
265 c->argv[0], c->argv[1] );
266 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
267 c->log, c->cr_msg, 0);
268 return(ARG_BAD_CONF);
271 case ARG_BER_LEN_T: {
273 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
274 snprintf( c->cr_msg, sizeof( c->cr_msg ),
275 "<%s> unable to parse \"%s\" as ber_len_t",
276 c->argv[0], c->argv[1] );
277 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
278 c->log, c->cr_msg, 0);
279 return(ARG_BAD_CONF);
286 } else if ( !strcasecmp(c->argv[1], "on") ||
287 !strcasecmp(c->argv[1], "true") ||
288 !strcasecmp(c->argv[1], "yes") )
291 } else if ( !strcasecmp(c->argv[1], "off") ||
292 !strcasecmp(c->argv[1], "false") ||
293 !strcasecmp(c->argv[1], "no") )
297 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
299 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
300 c->log, c->cr_msg, 0 );
301 return(ARG_BAD_CONF);
305 j = (arg_type & ARG_NONZERO) ? 1 : 0;
306 if(iarg < j && larg < j && barg < (unsigned)j ) {
307 larg = larg ? larg : (barg ? (long)barg : iarg);
308 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
310 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
311 c->log, c->cr_msg, 0 );
312 return(ARG_BAD_CONF);
316 case ARG_INT: c->value_int = iarg; break;
317 case ARG_UINT: c->value_uint = uiarg; break;
318 case ARG_LONG: c->value_long = larg; break;
319 case ARG_ULONG: c->value_ulong = ularg; break;
320 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
326 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
330 arg_type = Conf->arg_type;
331 if(arg_type & ARG_MAGIC) {
332 if(!c->be) c->be = frontendDB;
334 rc = (*((ConfigDriver*)Conf->arg_item))(c);
336 if(c->be == frontendDB) c->be = NULL;
339 if ( !c->cr_msg[0] ) {
340 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
342 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
343 c->log, c->cr_msg, 0 );
345 return(ARG_BAD_CONF);
349 if(arg_type & ARG_OFFSET) {
350 if (c->be && c->table == Cft_Database)
351 ptr = c->be->be_private;
353 ptr = c->bi->bi_private;
355 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
357 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
358 c->log, c->cr_msg, 0);
359 return(ARG_BAD_CONF);
361 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
362 } else if (arg_type & ARGS_TYPES) {
363 ptr = Conf->arg_item;
365 if(arg_type & ARGS_TYPES)
366 switch(arg_type & ARGS_TYPES) {
368 case ARG_INT: *(int*)ptr = c->value_int; break;
369 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break;
370 case ARG_LONG: *(long*)ptr = c->value_long; break;
371 case ARG_ULONG: *(unsigned long*)ptr = c->value_ulong; break;
372 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
374 char *cc = *(char**)ptr;
376 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
377 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
378 c->log, Conf->name, 0 );
379 return(ARG_BAD_CONF);
383 *(char **)ptr = c->value_string;
387 *(struct berval *)ptr = c->value_bv;
390 *(AttributeDescription **)ptr = c->value_ad;
396 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
399 arg_type = Conf->arg_type;
400 if(arg_type == ARG_IGNORED) {
401 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
402 c->log, Conf->name, 0);
405 rc = config_check_vals( Conf, c, 0 );
407 return config_set_vals( Conf, c );
411 config_del_vals(ConfigTable *cf, ConfigArgs *c)
415 /* If there is no handler, just ignore it */
416 if ( cf->arg_type & ARG_MAGIC ) {
417 c->argv[0] = cf->ad->ad_cname.bv_val;
418 c->op = LDAP_MOD_DELETE;
419 c->type = cf->arg_type & ARGS_USERLAND;
420 rc = (*((ConfigDriver*)cf->arg_item))(c);
426 config_get_vals(ConfigTable *cf, ConfigArgs *c)
432 if ( cf->arg_type & ARG_IGNORED ) {
436 memset(&c->values, 0, sizeof(c->values));
437 c->rvalue_vals = NULL;
438 c->rvalue_nvals = NULL;
439 c->op = SLAP_CONFIG_EMIT;
440 c->type = cf->arg_type & ARGS_USERLAND;
442 if ( cf->arg_type & ARG_MAGIC ) {
443 rc = (*((ConfigDriver*)cf->arg_item))(c);
446 if ( cf->arg_type & ARG_OFFSET ) {
447 if (c->be && c->table == Cft_Database)
448 ptr = c->be->be_private;
450 ptr = c->bi->bi_private;
453 ptr = (void *)((char *)ptr + (long)cf->arg_item);
458 switch(cf->arg_type & ARGS_TYPES) {
460 case ARG_INT: c->value_int = *(int *)ptr; break;
461 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break;
462 case ARG_LONG: c->value_long = *(long *)ptr; break;
463 case ARG_ULONG: c->value_ulong = *(unsigned long *)ptr; break;
464 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
467 c->value_string = ch_strdup(*(char **)ptr);
470 c->value_bv = *((struct berval *)ptr); break;
472 c->value_ad = *(AttributeDescription **)ptr; break;
475 if ( cf->arg_type & ARGS_TYPES) {
478 switch(cf->arg_type & ARGS_TYPES) {
479 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
480 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
481 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
482 case ARG_ULONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%lu", c->value_ulong); break;
483 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
484 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
485 c->value_int ? "TRUE" : "FALSE"); break;
487 if ( c->value_string && c->value_string[0]) {
488 ber_str2bv( c->value_string, 0, 0, &bv);
494 if ( !BER_BVISEMPTY( &c->value_bv )) {
502 bv = c->value_ad->ad_cname;
511 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
514 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
515 ber_bvarray_add(&c->rvalue_vals, &bv);
516 } else if ( !BER_BVISNULL( &bv ) ) {
517 value_add_one(&c->rvalue_vals, &bv);
519 /* else: maybe c->rvalue_vals already set? */
525 init_config_attrs(ConfigTable *ct) {
528 for (i=0; ct[i].name; i++ ) {
529 if ( !ct[i].attribute ) continue;
530 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
532 fprintf( stderr, "init_config_attrs: register_at failed\n" );
541 init_config_ocs( ConfigOCs *ocs ) {
544 for (i=0;ocs[i].co_def;i++) {
545 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
547 fprintf( stderr, "init_config_ocs: register_oc failed\n" );
554 /* Split an LDIF line into space-separated tokens. Words may be grouped
555 * by quotes. A quoted string may begin in the middle of a word, but must
556 * end at the end of the word (be followed by whitespace or EOS). Any other
557 * quotes are passed through unchanged. All other characters are passed
561 strtok_quote_ldif( char **line )
563 char *beg, *ptr, *quote=NULL;
571 while( isspace( (unsigned char) *ptr )) ptr++;
582 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
592 if ( isspace( (unsigned char) *ptr )) {
598 while ( quote < ptr ) {
606 while ( isspace( (unsigned char) *ptr )) ptr++;
613 config_parse_ldif( ConfigArgs *c )
616 c->tline = ch_strdup(c->line);
619 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
621 if ( c->argc >= c->argv_size ) {
622 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
625 c->argv_size += ARGS_STEP;
628 c->argv[c->argc] = NULL;
632 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
636 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
637 ct->ad->ad_cname.bv_val, valx );
639 c->argv[0] = ct->ad->ad_cname.bv_val;
641 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
642 c->argv[c->argc] = c->line;
644 c->argv[c->argc] = NULL;
647 config_parse_ldif( c );
649 rc = config_check_vals( ct, c, 1 );
654 rc = LDAP_CONSTRAINT_VIOLATION;
660 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
664 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
665 ct->ad->ad_cname.bv_val, valx );
667 c->argv[0] = ct->ad->ad_cname.bv_val;
669 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
670 c->argv[c->argc] = c->line;
672 c->argv[c->argc] = NULL;
675 config_parse_ldif( c );
677 c->op = LDAP_MOD_ADD;
678 rc = config_add_vals( ct, c );
685 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
693 c = ch_calloc( 1, sizeof( ConfigArgs ) );
699 memcpy( c, cf, sizeof( ConfigArgs ) );
701 c->depth = depth; /* XXX */
708 init_config_argv( c );
710 if ( stat( fname, &s ) != 0 ) {
712 Debug(LDAP_DEBUG_ANY,
713 "could not stat config file \"%s\": %s (%d)\n",
714 fname, strerror(errno), errno);
719 if ( !S_ISREG( s.st_mode ) ) {
721 Debug(LDAP_DEBUG_ANY,
722 "regular file expected, got \"%s\"\n",
728 fp = fopen( fname, "r" );
731 Debug(LDAP_DEBUG_ANY,
732 "could not open config file \"%s\": %s (%d)\n",
733 fname, strerror(errno), errno);
738 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
744 while ( fp_getline( fp, c ) ) {
745 /* skip comments and blank lines */
746 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
750 snprintf( c->log, sizeof( c->log ), "%s: line %d",
751 c->fname, c->lineno );
755 if ( config_fp_parse_line( c ) ) {
761 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
767 c->op = SLAP_CONFIG_ADD;
769 ct = config_find_keyword( cft, c );
771 c->table = Cft_Global;
772 rc = config_add_vals( ct, c );
775 if ( rc & ARGS_USERLAND ) {
776 /* XXX a usertype would be opaque here */
777 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
778 c->log, c->argv[0], 0);
782 } else if ( rc == ARG_BAD_CONF ) {
787 } else if ( c->bi && !c->be ) {
788 rc = SLAP_CONF_UNKNOWN;
789 if ( c->bi->bi_cf_ocs ) {
790 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
792 c->table = c->bi->bi_cf_ocs->co_type;
793 rc = config_add_vals( ct, c );
796 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
797 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
802 case SLAP_CONF_UNKNOWN:
803 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
804 "<%s> inside backend info definition.\n",
805 c->log, *c->argv, 0);
812 } else if ( c->be && c->be != frontendDB ) {
813 rc = SLAP_CONF_UNKNOWN;
814 if ( c->be->be_cf_ocs ) {
815 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
817 c->table = c->be->be_cf_ocs->co_type;
818 rc = config_add_vals( ct, c );
821 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
822 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
825 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
827 /* global overlays may need
828 * definitions inside other databases...
830 rc = (*frontendDB->be_config)( frontendDB,
831 c->fname, (int)c->lineno, c->argc, c->argv );
838 case SLAP_CONF_UNKNOWN:
839 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
840 "<%s> inside backend database definition.\n",
841 c->log, *c->argv, 0);
848 } else if ( frontendDB->be_config ) {
849 rc = (*frontendDB->be_config)( frontendDB,
850 c->fname, (int)c->lineno, c->argc, c->argv);
853 case SLAP_CONF_UNKNOWN:
854 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
855 "<%s> inside global database definition.\n",
856 c->log, *c->argv, 0);
865 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
866 "<%s> outside backend info and database definitions.\n",
867 c->log, *c->argv, 0);
887 /* restrictops, allows, disallows, requires, loglevel */
890 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
892 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
893 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
899 verb_to_mask(const char *word, slap_verbmasks *v) {
901 ber_str2bv( word, 0, 0, &bword );
902 return bverb_to_mask( &bword, v );
906 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
908 for(i = 1; i < argc; i++) {
909 j = verb_to_mask(argv[i], v);
910 if(BER_BVISNULL(&v[j].word)) return i;
911 while (!v[j].mask) j--;
917 /* Mask keywords that represent multiple bits should occur before single
918 * bit keywords in the verbmasks array.
921 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
925 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
926 if (!v[i].mask) continue;
927 if (( m & v[i].mask ) == v[i].mask ) {
928 value_add_one( bva, &v[i].word );
938 /* Return the verbs as a single string, separated by delim */
940 mask_to_verbstring(slap_verbmasks *v, slap_mask_t m0, char delim, struct berval *bv)
948 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
949 if (!v[i].mask) continue;
950 if (( m & v[i].mask ) == v[i].mask ) {
951 bv->bv_len += v[i].word.bv_len + 1;
957 bv->bv_val = ch_malloc(bv->bv_len);
961 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
962 if (!v[i].mask) continue;
963 if (( m & v[i].mask ) == v[i].mask ) {
964 ptr = lutil_strcopy(ptr, v[i].word.bv_val);
975 /* Parse a verbstring */
977 verbstring_to_mask(slap_verbmasks *v, char *str, char delim, slap_mask_t *m) {
984 d = strchr( str, delim );
988 bv.bv_len = strlen( str );
989 j = bverb_to_mask( &bv, v );
990 if(BER_BVISNULL(&v[j].word)) return 1;
991 while (!v[j].mask) j--;
993 str += bv.bv_len + 1;
999 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
1003 assert( *vp == NULL );
1005 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
1007 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
1009 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1010 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
1011 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
1014 BER_BVZERO( &(*vp)[ i ].word );
1020 slap_verbmasks_destroy( slap_verbmasks *v )
1024 assert( v != NULL );
1026 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1027 ch_free( v[ i ].word.bv_val );
1036 slap_verbmasks_append(
1037 slap_verbmasks **vp,
1040 slap_mask_t *ignore )
1045 return LDAP_OPERATIONS_ERROR;
1048 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
1049 if ( !(*vp)[ i ].mask ) continue;
1051 if ( ignore != NULL ) {
1054 for ( j = 0; ignore[ j ] != 0; j++ ) {
1055 if ( (*vp)[ i ].mask == ignore[ j ] ) {
1061 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
1062 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
1063 /* already set; ignore */
1064 return LDAP_SUCCESS;
1067 return LDAP_TYPE_OR_VALUE_EXISTS;
1070 if ( m & (*vp)[ i ].mask ) {
1072 return LDAP_CONSTRAINT_VIOLATION;
1077 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1078 ber_dupbv( &(*vp)[ i ].word, v );
1079 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1080 BER_BVZERO( &(*vp)[ i + 1 ].word );
1082 return LDAP_SUCCESS;
1086 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1089 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1090 if ( m == v[i].mask ) {
1100 /* register a new verbmask */
1102 slap_verbmask_register( slap_verbmasks *vm_, slap_verbmasks **vmp, struct berval *bv, int mask )
1104 slap_verbmasks *vm = *vmp;
1107 /* check for duplicate word */
1108 /* NOTE: we accept duplicate codes; the first occurrence will be used
1109 * when mapping from mask to verb */
1110 i = verb_to_mask( bv->bv_val, vm );
1111 if ( !BER_BVISNULL( &vm[ i ].word ) ) {
1115 for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
1119 /* first time: duplicate array */
1120 vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
1121 for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
1123 ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
1124 *((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
1128 vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
1131 ber_dupbv( &vm[ i ].word, bv );
1132 *((slap_mask_t*)&vm[ i ].mask) = mask;
1134 BER_BVZERO( &vm[ i+1 ].word );
1141 static slap_verbmasks slap_ldap_response_code_[] = {
1142 { BER_BVC("success"), LDAP_SUCCESS },
1144 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR },
1145 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR },
1146 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED },
1147 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED },
1148 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE },
1149 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE },
1151 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED },
1152 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED },
1153 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED },
1154 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED },
1155 #if 0 /* not LDAPv3 */
1156 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS },
1159 { BER_BVC("referral"), LDAP_REFERRAL },
1160 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED },
1161 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
1162 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED },
1163 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS },
1165 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE },
1166 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE },
1167 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING },
1168 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION },
1169 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS },
1170 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX },
1172 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT },
1173 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM },
1174 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX },
1175 #if 0 /* not LDAPv3 */
1176 { BER_BVC("isLeaf"), LDAP_IS_LEAF },
1178 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM },
1180 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE },
1181 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH },
1182 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS },
1183 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS },
1185 { BER_BVC("busy"), LDAP_BUSY },
1186 { BER_BVC("unavailable"), LDAP_UNAVAILABLE },
1187 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM },
1188 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT },
1190 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION },
1191 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION },
1192 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF },
1193 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN },
1194 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS },
1195 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS },
1196 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE },
1197 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS },
1199 { BER_BVC("other"), LDAP_OTHER },
1201 /* extension-specific */
1203 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED },
1204 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION },
1205 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA },
1206 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME },
1207 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED },
1209 { BER_BVC("cancelled"), LDAP_CANCELLED },
1210 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION },
1211 { BER_BVC("tooLate"), LDAP_TOO_LATE },
1212 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL },
1214 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED },
1216 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED },
1218 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED },
1220 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION },
1225 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_;
1228 slap_ldap_response_code_register( struct berval *bv, int err )
1230 return slap_verbmask_register( slap_ldap_response_code_,
1231 &slap_ldap_response_code, bv, err );
1235 static slap_verbmasks tlskey[] = {
1236 { BER_BVC("no"), SB_TLS_OFF },
1237 { BER_BVC("yes"), SB_TLS_ON },
1238 { BER_BVC("critical"), SB_TLS_CRITICAL },
1242 static slap_verbmasks crlkeys[] = {
1243 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE },
1244 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER },
1245 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL },
1249 static slap_verbmasks vfykeys[] = {
1250 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER },
1251 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND },
1252 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY },
1253 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD },
1258 static slap_verbmasks methkey[] = {
1259 { BER_BVC("none"), LDAP_AUTH_NONE },
1260 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1261 #ifdef HAVE_CYRUS_SASL
1262 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1267 static slap_verbmasks versionkey[] = {
1268 { BER_BVC("2"), LDAP_VERSION2 },
1269 { BER_BVC("3"), LDAP_VERSION3 },
1274 slap_keepalive_parse(
1277 slap_cf_aux_table *tab0,
1282 slap_keepalive *sk = (slap_keepalive *)bc;
1283 int rc = snprintf( val->bv_val, val->bv_len, "%d:%d:%d",
1284 sk->sk_idle, sk->sk_probes, sk->sk_interval );
1289 if ( (unsigned)rc >= val->bv_len ) {
1296 char *s = val->bv_val;
1298 slap_keepalive *sk = (slap_keepalive *)bc;
1301 if ( s[0] == ':' ) {
1306 sk2.sk_idle = strtol( s, &next, 10 );
1307 if ( next == s || next[0] != ':' ) {
1311 if ( sk2.sk_idle < 0 ) {
1318 if ( s[0] == ':' ) {
1323 sk2.sk_probes = strtol( s, &next, 10 );
1324 if ( next == s || next[0] != ':' ) {
1328 if ( sk2.sk_probes < 0 ) {
1336 sk2.sk_interval = 0;
1340 sk2.sk_interval = strtol( s, &next, 10 );
1341 if ( next == s || next[0] != '\0' ) {
1345 if ( sk2.sk_interval < 0 ) {
1352 ber_memfree( val->bv_val );
1363 slap_cf_aux_table *tab0,
1367 slap_bindconf *bc = bcp;
1369 if ( bc->sb_uri.bv_len >= val->bv_len )
1371 val->bv_len = bc->sb_uri.bv_len;
1372 AC_MEMCPY( val->bv_val, bc->sb_uri.bv_val, val->bv_len );
1376 if ( ldap_is_ldaps_url( val->bv_val ))
1377 bc->sb_tls_do_init = 1;
1383 static slap_cf_aux_table bindkey[] = {
1384 { BER_BVC("uri="), 0, 'x', 1, slap_sb_uri },
1385 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1386 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1387 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1388 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1389 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1390 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1391 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1392 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1393 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1394 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1395 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1396 { BER_BVC("keepalive="), offsetof(slap_bindconf, sb_keepalive), 'x', 0, (slap_verbmasks *)slap_keepalive_parse },
1398 /* NOTE: replace "13" with the actual index
1399 * of the first TLS-related line */
1400 #define aux_TLS (bindkey+13) /* beginning of TLS keywords */
1402 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1403 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1404 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1405 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1406 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1407 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 0, NULL },
1408 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 0, NULL },
1409 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 0, NULL },
1410 #ifdef HAVE_OPENSSL_CRL
1411 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 0, NULL },
1414 { BER_BVNULL, 0, 0, 0, NULL }
1419 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1420 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1423 * 'U': unsigned long
1427 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1429 int rc = SLAP_CONF_UNKNOWN;
1430 slap_cf_aux_table *tab;
1432 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1433 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1438 unsigned long *ulptr;
1439 struct berval *bptr;
1440 const char *val = word + tab->key.bv_len;
1442 switch ( tab->type ) {
1444 cptr = (char **)((char *)dst + tab->off);
1445 *cptr = ch_strdup( val );
1450 bptr = (struct berval *)((char *)dst + tab->off);
1451 if ( tab->aux != NULL ) {
1453 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1455 ber_str2bv( val, 0, 0, &dn );
1456 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1459 ber_str2bv( val, 0, 1, bptr );
1465 iptr = (int *)((char *)dst + tab->off);
1467 if ( tab->aux != NULL ) {
1468 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1470 assert( aux != NULL );
1473 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1474 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1475 *iptr = aux[j].mask;
1482 rc = lutil_atoix( iptr, val, 0 );
1487 uptr = (unsigned *)((char *)dst + tab->off);
1489 rc = lutil_atoux( uptr, val, 0 );
1493 lptr = (long *)((char *)dst + tab->off);
1495 rc = lutil_atolx( lptr, val, 0 );
1499 ulptr = (unsigned long *)((char *)dst + tab->off);
1501 rc = lutil_atoulx( ulptr, val, 0 );
1505 if ( tab->aux != NULL ) {
1506 struct berval value;
1507 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1509 ber_str2bv( val, 0, 1, &value );
1511 rc = func( &value, (void *)((char *)dst + tab->off), tab, tabmsg, 0 );
1520 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1532 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1534 char buf[AC_LINE_MAX], *ptr;
1535 slap_cf_aux_table *tab;
1539 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1544 unsigned long *ulptr;
1545 struct berval *bptr;
1547 cptr = (char **)((char *)src + tab->off);
1549 switch ( tab->type ) {
1551 bptr = (struct berval *)((char *)src + tab->off);
1552 cptr = &bptr->bv_val;
1557 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1558 if ( tab->quote ) *ptr++ = '"';
1559 ptr = lutil_strcopy( ptr, *cptr );
1560 if ( tab->quote ) *ptr++ = '"';
1565 iptr = (int *)((char *)src + tab->off);
1567 if ( tab->aux != NULL ) {
1568 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1570 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1571 if ( *iptr == aux[i].mask ) {
1573 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1574 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1581 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1582 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1587 uptr = (unsigned *)((char *)src + tab->off);
1589 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1590 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1594 lptr = (long *)((char *)src + tab->off);
1596 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1597 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1601 ulptr = (unsigned long *)((char *)src + tab->off);
1603 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1604 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1611 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1612 if ( tab->quote ) *ptr++ = '"';
1613 if ( tab->aux != NULL ) {
1614 struct berval value;
1615 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1619 value.bv_len = buf + sizeof( buf ) - ptr;
1621 rc = func( &value, (void *)((char *)src + tab->off), tab, "(unparse)", 1 );
1624 ptr += value.bv_len;
1631 if ( tab->quote ) *ptr++ = '"';
1640 tmp.bv_len = ptr - buf;
1641 ber_dupbv( bv, &tmp );
1646 slap_tls_get_config( LDAP *ld, int opt, char **val )
1649 slap_verbmasks *keys;
1654 case LDAP_OPT_X_TLS_CRLCHECK:
1657 case LDAP_OPT_X_TLS_REQUIRE_CERT:
1660 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
1662 ldap_pvt_tls_get_option( ld, opt, &ival );
1663 snprintf( buf, sizeof( buf ), "%d.%d",
1664 ( ival >> 8 ) & 0xff, ival & 0xff );
1665 *val = ch_strdup( buf );
1671 ldap_pvt_tls_get_option( ld, opt, &ival );
1672 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1673 if (keys[i].mask == ival) {
1674 *val = ch_strdup( keys[i].word.bv_val );
1683 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1686 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1687 bc->sb_tls_do_init = 1;
1695 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1698 return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1704 bindconf_parse( const char *word, slap_bindconf *bc )
1707 /* Detect TLS config changes explicitly */
1708 if ( bindconf_tls_parse( word, bc ) == 0 ) {
1712 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1716 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1718 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1721 void bindconf_free( slap_bindconf *bc ) {
1722 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1723 ch_free( bc->sb_uri.bv_val );
1724 BER_BVZERO( &bc->sb_uri );
1726 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1727 ch_free( bc->sb_binddn.bv_val );
1728 BER_BVZERO( &bc->sb_binddn );
1730 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1731 ch_free( bc->sb_cred.bv_val );
1732 BER_BVZERO( &bc->sb_cred );
1734 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1735 ch_free( bc->sb_saslmech.bv_val );
1736 BER_BVZERO( &bc->sb_saslmech );
1738 if ( bc->sb_secprops ) {
1739 ch_free( bc->sb_secprops );
1740 bc->sb_secprops = NULL;
1742 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1743 ch_free( bc->sb_realm.bv_val );
1744 BER_BVZERO( &bc->sb_realm );
1746 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1747 ch_free( bc->sb_authcId.bv_val );
1748 BER_BVZERO( &bc->sb_authcId );
1750 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1751 ch_free( bc->sb_authzId.bv_val );
1752 BER_BVZERO( &bc->sb_authzId );
1755 if ( bc->sb_tls_cert ) {
1756 ch_free( bc->sb_tls_cert );
1757 bc->sb_tls_cert = NULL;
1759 if ( bc->sb_tls_key ) {
1760 ch_free( bc->sb_tls_key );
1761 bc->sb_tls_key = NULL;
1763 if ( bc->sb_tls_cacert ) {
1764 ch_free( bc->sb_tls_cacert );
1765 bc->sb_tls_cacert = NULL;
1767 if ( bc->sb_tls_cacertdir ) {
1768 ch_free( bc->sb_tls_cacertdir );
1769 bc->sb_tls_cacertdir = NULL;
1771 if ( bc->sb_tls_reqcert ) {
1772 ch_free( bc->sb_tls_reqcert );
1773 bc->sb_tls_reqcert = NULL;
1775 if ( bc->sb_tls_cipher_suite ) {
1776 ch_free( bc->sb_tls_cipher_suite );
1777 bc->sb_tls_cipher_suite = NULL;
1779 if ( bc->sb_tls_protocol_min ) {
1780 ch_free( bc->sb_tls_protocol_min );
1781 bc->sb_tls_protocol_min = NULL;
1783 #ifdef HAVE_OPENSSL_CRL
1784 if ( bc->sb_tls_crlcheck ) {
1785 ch_free( bc->sb_tls_crlcheck );
1786 bc->sb_tls_crlcheck = NULL;
1789 if ( bc->sb_tls_ctx ) {
1790 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1791 bc->sb_tls_ctx = NULL;
1797 bindconf_tls_defaults( slap_bindconf *bc )
1800 if ( bc->sb_tls_do_init ) {
1801 if ( !bc->sb_tls_cacert )
1802 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1803 &bc->sb_tls_cacert );
1804 if ( !bc->sb_tls_cacertdir )
1805 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1806 &bc->sb_tls_cacertdir );
1807 if ( !bc->sb_tls_cert )
1808 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1810 if ( !bc->sb_tls_key )
1811 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1813 if ( !bc->sb_tls_cipher_suite )
1814 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1815 &bc->sb_tls_cipher_suite );
1816 if ( !bc->sb_tls_reqcert )
1817 bc->sb_tls_reqcert = ch_strdup("demand");
1818 #ifdef HAVE_OPENSSL_CRL
1819 if ( !bc->sb_tls_crlcheck )
1820 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1821 &bc->sb_tls_crlcheck );
1833 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1834 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1835 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1836 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1837 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1838 { "tls_protocol_min", offsetof(slap_bindconf, sb_tls_protocol_min), LDAP_OPT_X_TLS_PROTOCOL_MIN },
1842 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1844 int i, rc, newctx = 0, res = 0;
1845 char *ptr = (char *)bc, **word;
1847 bc->sb_tls_do_init = 0;
1849 for (i=0; bindtlsopts[i].opt; i++) {
1850 word = (char **)(ptr + bindtlsopts[i].offset);
1852 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1854 Debug( LDAP_DEBUG_ANY,
1855 "bindconf_tls_set: failed to set %s to %s\n",
1856 bindtlsopts[i].key, *word, 0 );
1862 if ( bc->sb_tls_reqcert ) {
1863 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1864 bc->sb_tls_reqcert );
1866 Debug( LDAP_DEBUG_ANY,
1867 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1868 bc->sb_tls_reqcert, 0, 0 );
1873 if ( bc->sb_tls_protocol_min ) {
1874 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN,
1875 bc->sb_tls_protocol_min );
1877 Debug( LDAP_DEBUG_ANY,
1878 "bindconf_tls_set: failed to set tls_protocol_min to %s\n",
1879 bc->sb_tls_protocol_min, 0, 0 );
1884 #ifdef HAVE_OPENSSL_CRL
1885 if ( bc->sb_tls_crlcheck ) {
1886 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1887 bc->sb_tls_crlcheck );
1889 Debug( LDAP_DEBUG_ANY,
1890 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1891 bc->sb_tls_crlcheck, 0, 0 );
1900 if ( bc->sb_tls_ctx ) {
1901 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1902 bc->sb_tls_ctx = NULL;
1904 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1908 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1916 * connect to a client using the bindconf data
1917 * note: should move "version" into bindconf...
1920 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1926 /* Init connection to master */
1927 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1928 if ( rc != LDAP_SUCCESS ) {
1929 Debug( LDAP_DEBUG_ANY,
1930 "slap_client_connect: "
1931 "ldap_initialize(%s) failed (%d)\n",
1932 sb->sb_uri.bv_val, rc, 0 );
1936 if ( sb->sb_version != 0 ) {
1937 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1938 (const void *)&sb->sb_version );
1941 if ( sb->sb_timeout_api ) {
1942 tv.tv_sec = sb->sb_timeout_api;
1944 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1947 if ( sb->sb_timeout_net ) {
1948 tv.tv_sec = sb->sb_timeout_net;
1950 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1953 if ( sb->sb_keepalive.sk_idle ) {
1954 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_IDLE, &sb->sb_keepalive.sk_idle );
1957 if ( sb->sb_keepalive.sk_probes ) {
1958 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_PROBES, &sb->sb_keepalive.sk_probes );
1961 if ( sb->sb_keepalive.sk_interval ) {
1962 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_INTERVAL, &sb->sb_keepalive.sk_interval );
1966 if ( sb->sb_tls_do_init ) {
1967 rc = bindconf_tls_set( sb, ld );
1969 } else if ( sb->sb_tls_ctx ) {
1970 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1975 Debug( LDAP_DEBUG_ANY,
1976 "slap_client_connect: "
1977 "URI=%s TLS context initialization failed (%d)\n",
1978 sb->sb_uri.bv_val, rc, 0 );
1985 rc = ldap_start_tls_s( ld, NULL, NULL );
1986 if ( rc != LDAP_SUCCESS ) {
1987 Debug( LDAP_DEBUG_ANY,
1988 "slap_client_connect: URI=%s "
1989 "%s, ldap_start_tls failed (%d)\n",
1991 sb->sb_tls == SB_TLS_CRITICAL ?
1992 "Error" : "Warning",
1994 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
2000 if ( sb->sb_method == LDAP_AUTH_SASL ) {
2001 #ifdef HAVE_CYRUS_SASL
2004 if ( sb->sb_secprops != NULL ) {
2005 rc = ldap_set_option( ld,
2006 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
2008 if( rc != LDAP_OPT_SUCCESS ) {
2009 Debug( LDAP_DEBUG_ANY,
2010 "slap_client_connect: "
2011 "error, ldap_set_option "
2012 "(%s,SECPROPS,\"%s\") failed!\n",
2013 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
2018 defaults = lutil_sasl_defaults( ld,
2019 sb->sb_saslmech.bv_val,
2020 sb->sb_realm.bv_val,
2021 sb->sb_authcId.bv_val,
2023 sb->sb_authzId.bv_val );
2024 if ( defaults == NULL ) {
2029 rc = ldap_sasl_interactive_bind_s( ld,
2030 sb->sb_binddn.bv_val,
2031 sb->sb_saslmech.bv_val,
2034 lutil_sasl_interact,
2037 lutil_sasl_freedefs( defaults );
2039 /* FIXME: different error behaviors according to
2041 * 2) on err policy : exit, retry, backoff ...
2043 if ( rc != LDAP_SUCCESS ) {
2044 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
2046 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
2047 "ldap_sasl_interactive_bind_s failed (%d)\n",
2048 sb->sb_uri.bv_val, rc, 0 );
2050 /* FIXME (see above comment) */
2051 /* if Kerberos credentials cache is not active, retry */
2052 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
2053 rc == LDAP_LOCAL_ERROR )
2055 rc = LDAP_SERVER_DOWN;
2060 #else /* HAVE_CYRUS_SASL */
2061 /* Should never get here, we trapped this at config time */
2063 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
2068 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
2069 rc = ldap_sasl_bind_s( ld,
2070 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
2071 &sb->sb_cred, NULL, NULL, NULL );
2072 if ( rc != LDAP_SUCCESS ) {
2073 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
2075 "ldap_sasl_bind_s failed (%d)\n",
2076 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
2084 ldap_unbind_ext( ld, NULL, NULL );
2095 /* -------------------------------------- */
2099 strtok_quote( char *line, char *sep, char **quote_ptr )
2106 if ( line != NULL ) {
2109 while ( *next && strchr( sep, *next ) ) {
2113 if ( *next == '\0' ) {
2119 for ( inquote = 0; *next; ) {
2127 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2133 next + 1, strlen( next + 1 ) + 1 );
2134 next++; /* dont parse the escaped character */
2139 if ( strchr( sep, *next ) != NULL ) {
2153 static char buf[AC_LINE_MAX];
2155 static size_t lmax, lcur;
2157 #define CATLINE( buf ) \
2159 size_t len = strlen( buf ); \
2160 while ( lcur + len + 1 > lmax ) { \
2161 lmax += AC_LINE_MAX; \
2162 line = (char *) ch_realloc( line, lmax ); \
2164 strcpy( line + lcur, buf ); \
2169 fp_getline_init(ConfigArgs *c) {
2175 fp_getline( FILE *fp, ConfigArgs *c )
2183 /* avoid stack of bufs */
2184 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
2190 while ( fgets( buf, sizeof( buf ), fp ) ) {
2191 p = strchr( buf, '\n' );
2193 if ( p > buf && p[-1] == '\r' ) {
2201 && ( p = line + strlen( line ) - 1 )[0] == '\\'
2208 if ( !isspace( (unsigned char)buf[0] ) ) {
2219 return(line[0] ? 1 : 0);
2223 config_fp_parse_line(ConfigArgs *c)
2226 static char *const hide[] = {
2227 "rootpw", "replica", "syncrepl", /* in slapd */
2228 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
2229 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
2230 "pseudorootpw", /* in back-meta */
2231 "dbpasswd", /* in back-sql */
2235 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
2237 c->tline = ch_strdup(c->line);
2238 token = strtok_quote(c->tline, " \t", "e_ptr);
2240 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2241 if(quote_ptr) *quote_ptr = ' ';
2242 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
2243 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2244 if(quote_ptr) *quote_ptr = '\0';
2246 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
2247 if(c->argc >= c->argv_size) {
2249 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2251 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
2255 c->argv_size += ARGS_STEP;
2259 c->argv[c->argc++] = token;
2261 c->argv[c->argc] = NULL;
2268 ucdata_unload( UCDATA_ALL );
2270 /* NOTE: in case of early exit, frontendDB can be NULL */
2271 if ( frontendDB->be_schemandn.bv_val )
2272 free( frontendDB->be_schemandn.bv_val );
2273 if ( frontendDB->be_schemadn.bv_val )
2274 free( frontendDB->be_schemadn.bv_val );
2275 if ( frontendDB->be_acl )
2276 acl_destroy( frontendDB->be_acl );
2279 if ( slapd_args_file )
2280 free ( slapd_args_file );
2281 if ( slapd_pid_file )
2282 free ( slapd_pid_file );
2283 if ( default_passwd_hash )
2284 ldap_charray_free( default_passwd_hash );
2288 slap_str2clist( char ***out, char *in, const char *brkstr )
2296 /* find last element in list */
2297 for (i = 0; *out && (*out)[i]; i++);
2299 /* protect the input string from strtok */
2300 str = ch_strdup( in );
2302 if ( *str == '\0' ) {
2307 /* Count words in string */
2309 for ( s = str; *s; s++ ) {
2310 if ( strchr( brkstr, *s ) != NULL ) {
2315 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2317 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2319 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2321 *new = ch_strdup( s );
2330 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
2331 int argc, char **argv )
2333 ConfigArgs c = { 0 };
2344 c.op = SLAP_CONFIG_ADD;
2345 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
2347 rc = SLAP_CONF_UNKNOWN;
2348 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
2350 c.table = be->be_cf_ocs->co_type;
2351 rc = config_add_vals( ct, &c );
2356 /* See if the given URL (in plain and parsed form) matches
2357 * any of the server's listener addresses. Return matching
2358 * Listener or NULL for no match.
2360 Listener *config_check_my_url( const char *url, LDAPURLDesc *lud )
2362 Listener **l = slapd_get_listeners();
2365 /* Try a straight compare with Listener strings */
2366 for ( i=0; l && l[i]; i++ ) {
2367 if ( !strcasecmp( url, l[i]->sl_url.bv_val )) {
2373 /* If hostname is empty, or is localhost, or matches
2374 * our hostname, this url refers to this host.
2375 * Compare it against listeners and ports.
2377 if ( !lud->lud_host || !lud->lud_host[0] ||
2378 !strncasecmp("localhost", lud->lud_host,
2379 STRLENOF("localhost")) ||
2380 !strcasecmp( global_host, lud->lud_host )) {
2382 for ( i=0; l && l[i]; i++ ) {
2384 ldap_url_parse( l[i]->sl_url.bv_val, &lu2 );
2386 if ( strcasecmp( lud->lud_scheme,
2389 if ( lud->lud_port != lu2->lud_port )
2391 /* Listener on ANY address */
2392 if ( !lu2->lud_host || !lu2->lud_host[0] ) {
2396 /* URL on ANY address */
2397 if ( !lud->lud_host || !lud->lud_host[0] ) {
2401 /* Listener has specific host, must
2404 if ( !strcasecmp( lud->lud_host,
2410 ldap_free_urldesc( lu2 );