1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2017 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"
54 #define LUTIL_ATOULX lutil_atoullx
57 #define LUTIL_ATOULX lutil_atoulx
64 * defaults for various global variables
66 slap_mask_t global_allows = 0;
67 slap_mask_t global_disallows = 0;
68 int global_gentlehup = 0;
69 int global_idletimeout = 0;
70 int global_writetimeout = 0;
71 char *global_host = NULL;
72 struct berval global_host_bv = BER_BVNULL;
73 char *global_realm = NULL;
74 char *sasl_host = NULL;
75 char **default_passwd_hash = NULL;
76 struct berval default_search_base = BER_BVNULL;
77 struct berval default_search_nbase = BER_BVNULL;
79 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
80 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
82 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
83 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
85 char *slapd_pid_file = NULL;
86 char *slapd_args_file = NULL;
88 int use_reverse_lookup = 0;
91 int slapi_plugins_used = 0;
94 static int fp_getline(FILE *fp, ConfigArgs *c);
95 static void fp_getline_init(ConfigArgs *c);
97 static char *strtok_quote(char *line, char *sep, char **quote_ptr, int *inquote);
98 static char *strtok_quote_ldif(char **line);
101 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
104 c = ch_calloc( 1, sizeof( ConfigArgs ) );
105 if ( c == NULL ) return(NULL);
111 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
116 init_config_argv( ConfigArgs *c )
118 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
119 c->argv_size = ARGS_STEP + 1;
122 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
125 for(i = 0; Conf[i].name; i++)
126 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
127 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
128 if ( !Conf[i].name ) return NULL;
129 if (( Conf[i].arg_type & ARGS_TYPES ) == ARG_BINARY ) {
130 size_t decode_len = LUTIL_BASE64_DECODE_LEN(c->linelen);
132 c->tline = ch_malloc( decode_len+1 );
133 c->linelen = lutil_b64_pton( c->line, c->tline, decode_len );
134 if ( c->linelen < 0 )
145 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
146 int rc, arg_user, arg_type, arg_syn, iarg;
152 if(Conf->arg_type == ARG_IGNORED) {
153 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
154 c->log, Conf->name, 0);
157 arg_type = Conf->arg_type & ARGS_TYPES;
158 arg_user = Conf->arg_type & ARGS_USERLAND;
159 arg_syn = Conf->arg_type & ARGS_SYNTAX;
161 if((arg_type == ARG_DN) && c->argc == 1) {
165 if(Conf->min_args && (c->argc < Conf->min_args)) {
166 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
167 c->argv[0], Conf->what ? Conf->what : "" );
168 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
169 return(ARG_BAD_CONF);
171 if(Conf->max_args && (c->argc > Conf->max_args)) {
172 char *ignored = " ignored";
174 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
175 c->argv[0], Conf->what );
178 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
179 c->log, c->cr_msg, ignored );
180 return(ARG_BAD_CONF);
182 if((arg_syn & ARG_DB) && !c->be) {
183 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
185 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
186 c->log, c->cr_msg, 0);
187 return(ARG_BAD_CONF);
189 if((arg_syn & ARG_PRE_BI) && c->bi) {
190 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
191 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
192 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
193 c->log, c->cr_msg, 0 );
194 return(ARG_BAD_CONF);
196 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
197 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
199 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
200 c->log, c->cr_msg, 0);
201 return(ARG_BAD_CONF);
203 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
204 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
205 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
206 c->log, c->cr_msg, 0);
207 return(ARG_BAD_CONF);
209 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
210 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
212 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
213 c->log, c->cr_msg, 0);
214 return(ARG_BAD_CONF);
217 memset(&c->values, 0, sizeof(c->values));
218 if(arg_type == ARG_STRING) {
219 assert( c->argc == 2 );
221 c->value_string = ch_strdup(c->argv[1]);
222 } else if(arg_type == ARG_BERVAL) {
223 assert( c->argc == 2 );
225 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
226 } else if(arg_type == ARG_BINARY) {
227 assert( c->argc == 2 );
229 c->value_bv.bv_len = c->linelen;
230 c->value_bv.bv_val = ch_malloc( c->linelen );
231 AC_MEMCPY( c->value_bv.bv_val, c->line, c->linelen );
233 } else if(arg_type == ARG_DN) {
235 assert( c->argc == 2 );
236 ber_str2bv( c->argv[1], 0, 0, &bv );
237 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
238 if ( rc != LDAP_SUCCESS ) {
239 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
240 c->argv[0], rc, ldap_err2string( rc ));
241 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
242 return(ARG_BAD_CONF);
245 ch_free( c->value_ndn.bv_val );
246 ch_free( c->value_dn.bv_val );
248 } else if(arg_type == ARG_ATDESC) {
249 const char *text = NULL;
250 assert( c->argc == 2 );
252 rc = slap_str2ad( c->argv[1], &c->value_ad, &text );
253 if ( rc != LDAP_SUCCESS ) {
254 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid AttributeDescription %d (%s)",
255 c->argv[0], rc, text );
256 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
257 return(ARG_BAD_CONF);
259 } else { /* all numeric */
261 iarg = 0; larg = 0; barg = 0;
264 assert( c->argc == 2 );
265 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
266 snprintf( c->cr_msg, sizeof( c->cr_msg ),
267 "<%s> unable to parse \"%s\" as int",
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);
275 assert( c->argc == 2 );
276 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
277 snprintf( c->cr_msg, sizeof( c->cr_msg ),
278 "<%s> unable to parse \"%s\" as unsigned int",
279 c->argv[0], c->argv[1] );
280 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
281 c->log, c->cr_msg, 0);
282 return(ARG_BAD_CONF);
286 assert( c->argc == 2 );
287 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
288 snprintf( c->cr_msg, sizeof( c->cr_msg ),
289 "<%s> unable to parse \"%s\" as long",
290 c->argv[0], c->argv[1] );
291 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
292 c->log, c->cr_msg, 0);
293 return(ARG_BAD_CONF);
297 assert( c->argc == 2 );
298 if ( LUTIL_ATOULX( &ularg, c->argv[1], 0 ) != 0 ) {
299 snprintf( c->cr_msg, sizeof( c->cr_msg ),
300 "<%s> unable to parse \"%s\" as unsigned long",
301 c->argv[0], c->argv[1] );
302 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
303 c->log, c->cr_msg, 0);
304 return(ARG_BAD_CONF);
307 case ARG_BER_LEN_T: {
309 assert( c->argc == 2 );
310 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
311 snprintf( c->cr_msg, sizeof( c->cr_msg ),
312 "<%s> unable to parse \"%s\" as ber_len_t",
313 c->argv[0], c->argv[1] );
314 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
315 c->log, c->cr_msg, 0);
316 return(ARG_BAD_CONF);
321 /* note: this is an explicit exception
322 * to the "need exactly 2 args" rule */
325 } else if ( !strcasecmp(c->argv[1], "on") ||
326 !strcasecmp(c->argv[1], "true") ||
327 !strcasecmp(c->argv[1], "yes") )
330 } else if ( !strcasecmp(c->argv[1], "off") ||
331 !strcasecmp(c->argv[1], "false") ||
332 !strcasecmp(c->argv[1], "no") )
336 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
338 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
339 c->log, c->cr_msg, 0 );
340 return(ARG_BAD_CONF);
344 j = (arg_type & ARG_NONZERO) ? 1 : 0;
345 if(iarg < j && larg < j && barg < (unsigned)j ) {
346 larg = larg ? larg : (barg ? (long)barg : iarg);
347 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
349 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
350 c->log, c->cr_msg, 0 );
351 return(ARG_BAD_CONF);
355 case ARG_INT: c->value_int = iarg; break;
356 case ARG_UINT: c->value_uint = uiarg; break;
357 case ARG_LONG: c->value_long = larg; break;
358 case ARG_ULONG: c->value_ulong = ularg; break;
359 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
365 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
369 arg_type = Conf->arg_type;
370 if(arg_type & ARG_MAGIC) {
371 if(!c->be) c->be = frontendDB;
373 rc = (*((ConfigDriver*)Conf->arg_item))(c);
375 if(c->be == frontendDB) c->be = NULL;
378 if ( !c->cr_msg[0] ) {
379 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
381 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
382 c->log, c->cr_msg, 0 );
384 return(ARG_BAD_CONF);
388 if(arg_type & ARG_OFFSET) {
389 if (c->be && c->table == Cft_Database)
390 ptr = c->be->be_private;
392 ptr = c->bi->bi_private;
394 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
396 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
397 c->log, c->cr_msg, 0);
398 return(ARG_BAD_CONF);
400 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
401 } else if (arg_type & ARGS_TYPES) {
402 ptr = Conf->arg_item;
404 if(arg_type & ARGS_TYPES)
405 switch(arg_type & ARGS_TYPES) {
407 case ARG_INT: *(int*)ptr = c->value_int; break;
408 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break;
409 case ARG_LONG: *(long*)ptr = c->value_long; break;
410 case ARG_ULONG: *(size_t*)ptr = c->value_ulong; break;
411 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
413 char *cc = *(char**)ptr;
415 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
416 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
417 c->log, Conf->name, 0 );
418 return(ARG_BAD_CONF);
422 *(char **)ptr = c->value_string;
427 *(struct berval *)ptr = c->value_bv;
430 *(AttributeDescription **)ptr = c->value_ad;
436 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
439 arg_type = Conf->arg_type;
440 if(arg_type == ARG_IGNORED) {
441 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
442 c->log, Conf->name, 0);
445 rc = config_check_vals( Conf, c, 0 );
447 return config_set_vals( Conf, c );
451 config_del_vals(ConfigTable *cf, ConfigArgs *c)
455 /* If there is no handler, just ignore it */
456 if ( cf->arg_type & ARG_MAGIC ) {
457 c->argv[0] = cf->ad->ad_cname.bv_val;
458 c->op = LDAP_MOD_DELETE;
459 c->type = cf->arg_type & ARGS_USERLAND;
460 rc = (*((ConfigDriver*)cf->arg_item))(c);
466 config_get_vals(ConfigTable *cf, ConfigArgs *c)
472 if ( cf->arg_type & ARG_IGNORED ) {
476 memset(&c->values, 0, sizeof(c->values));
477 c->rvalue_vals = NULL;
478 c->rvalue_nvals = NULL;
479 c->op = SLAP_CONFIG_EMIT;
480 c->type = cf->arg_type & ARGS_USERLAND;
482 if ( cf->arg_type & ARG_MAGIC ) {
483 rc = (*((ConfigDriver*)cf->arg_item))(c);
486 if ( cf->arg_type & ARG_OFFSET ) {
487 if (c->be && c->table == Cft_Database)
488 ptr = c->be->be_private;
490 ptr = c->bi->bi_private;
493 ptr = (void *)((char *)ptr + (long)cf->arg_item);
498 switch(cf->arg_type & ARGS_TYPES) {
500 case ARG_INT: c->value_int = *(int *)ptr; break;
501 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break;
502 case ARG_LONG: c->value_long = *(long *)ptr; break;
503 case ARG_ULONG: c->value_ulong = *(size_t *)ptr; break;
504 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
507 c->value_string = ch_strdup(*(char **)ptr);
510 c->value_bv = *((struct berval *)ptr); break;
512 c->value_ad = *(AttributeDescription **)ptr; break;
515 if ( cf->arg_type & ARGS_TYPES) {
518 switch(cf->arg_type & ARGS_TYPES) {
519 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
520 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
521 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
522 case ARG_ULONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%" Z "u", c->value_ulong); break;
523 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
524 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
525 c->value_int ? "TRUE" : "FALSE"); break;
527 if ( c->value_string && c->value_string[0]) {
528 ber_str2bv( c->value_string, 0, 0, &bv);
534 if ( !BER_BVISEMPTY( &c->value_bv )) {
542 bv = c->value_ad->ad_cname;
551 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
554 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
555 ber_bvarray_add(&c->rvalue_vals, &bv);
556 } else if ( !BER_BVISNULL( &bv ) ) {
557 value_add_one(&c->rvalue_vals, &bv);
559 /* else: maybe c->rvalue_vals already set? */
565 init_config_attrs(ConfigTable *ct) {
568 for (i=0; ct[i].name; i++ ) {
569 if ( !ct[i].attribute ) continue;
570 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
572 fprintf( stderr, "init_config_attrs: register_at failed\n" );
575 if (( ct[i].arg_type & ARGS_TYPES ) == ARG_BINARY ) {
576 ldif_must_b64_encode_register( ct[i].ad->ad_cname.bv_val,
577 ct[i].ad->ad_type->sat_oid );
585 init_config_ocs( ConfigOCs *ocs ) {
588 for (i=0;ocs[i].co_def;i++) {
589 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
591 fprintf( stderr, "init_config_ocs: register_oc failed\n" );
598 /* Split an LDIF line into space-separated tokens. Words may be grouped
599 * by quotes. A quoted string may begin in the middle of a word, but must
600 * end at the end of the word (be followed by whitespace or EOS). Any other
601 * quotes are passed through unchanged. All other characters are passed
605 strtok_quote_ldif( char **line )
607 char *beg, *ptr, *quote=NULL;
615 while( isspace( (unsigned char) *ptr )) ptr++;
626 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
636 if ( isspace( (unsigned char) *ptr )) {
642 while ( quote < ptr ) {
650 while ( isspace( (unsigned char) *ptr )) ptr++;
657 config_parse_ldif( ConfigArgs *c )
660 c->tline = ch_strdup(c->line);
663 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
665 if ( c->argc >= c->argv_size ) {
666 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
669 c->argv_size += ARGS_STEP;
672 c->argv[c->argc] = NULL;
676 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
679 int arg_type = ct->arg_type & ARGS_TYPES;
681 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
682 ct->ad->ad_cname.bv_val, valx );
684 c->argv[0] = ct->ad->ad_cname.bv_val;
686 if ( (( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) ||
687 (arg_type == ARG_BERVAL || arg_type == ARG_BINARY)) {
688 c->argv[c->argc] = c->line;
690 c->argv[c->argc] = NULL;
693 config_parse_ldif( c );
695 rc = config_check_vals( ct, c, 1 );
700 rc = LDAP_CONSTRAINT_VIOLATION;
706 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
709 int arg_type = ct->arg_type & ARGS_TYPES;
711 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
712 ct->ad->ad_cname.bv_val, valx );
714 c->argv[0] = ct->ad->ad_cname.bv_val;
716 if ( (( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) ||
717 (arg_type == ARG_BERVAL || arg_type == ARG_BINARY)) {
718 c->argv[c->argc] = c->line;
720 c->argv[c->argc] = NULL;
723 config_parse_ldif( c );
725 c->op = LDAP_MOD_ADD;
726 rc = config_add_vals( ct, c );
733 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
741 c = ch_calloc( 1, sizeof( ConfigArgs ) );
747 memcpy( c, cf, sizeof( ConfigArgs ) );
749 c->depth = depth; /* XXX */
756 init_config_argv( c );
758 if ( stat( fname, &s ) != 0 ) {
760 Debug(LDAP_DEBUG_ANY,
761 "could not stat config file \"%s\": %s (%d)\n",
762 fname, strerror(errno), errno);
768 if ( !S_ISREG( s.st_mode ) ) {
770 Debug(LDAP_DEBUG_ANY,
771 "regular file expected, got \"%s\"\n",
778 fp = fopen( fname, "r" );
781 Debug(LDAP_DEBUG_ANY,
782 "could not open config file \"%s\": %s (%d)\n",
783 fname, strerror(errno), errno);
789 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
795 while ( fp_getline( fp, c ) ) {
796 /* skip comments and blank lines */
797 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
801 snprintf( c->log, sizeof( c->log ), "%s: line %d",
802 c->fname, c->lineno );
806 if ( config_fp_parse_line( c ) ) {
812 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
818 c->op = SLAP_CONFIG_ADD;
820 ct = config_find_keyword( cft, c );
822 c->table = Cft_Global;
823 rc = config_add_vals( ct, c );
826 if ( rc & ARGS_USERLAND ) {
827 /* XXX a usertype would be opaque here */
828 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
829 c->log, c->argv[0], 0);
833 } else if ( rc == ARG_BAD_CONF ) {
838 } else if ( c->bi && !c->be ) {
839 rc = SLAP_CONF_UNKNOWN;
840 if ( c->bi->bi_cf_ocs ) {
841 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
843 c->table = c->bi->bi_cf_ocs->co_type;
844 rc = config_add_vals( ct, c );
847 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
848 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
853 case SLAP_CONF_UNKNOWN:
854 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
855 "<%s> inside backend info definition.\n",
856 c->log, *c->argv, 0);
863 } else if ( c->be && c->be != frontendDB ) {
864 rc = SLAP_CONF_UNKNOWN;
865 if ( c->be->be_cf_ocs ) {
866 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
868 c->table = c->be->be_cf_ocs->co_type;
869 rc = config_add_vals( ct, c );
872 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
873 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
876 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
878 /* global overlays may need
879 * definitions inside other databases...
881 rc = (*frontendDB->be_config)( frontendDB,
882 c->fname, (int)c->lineno, c->argc, c->argv );
889 case SLAP_CONF_UNKNOWN:
890 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
891 "<%s> inside backend database definition.\n",
892 c->log, *c->argv, 0);
899 } else if ( frontendDB->be_config ) {
900 rc = (*frontendDB->be_config)( frontendDB,
901 c->fname, (int)c->lineno, c->argc, c->argv);
904 case SLAP_CONF_UNKNOWN:
905 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
906 "<%s> inside global database definition.\n",
907 c->log, *c->argv, 0);
916 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
917 "<%s> outside backend info and database definitions.\n",
918 c->log, *c->argv, 0);
938 /* restrictops, allows, disallows, requires, loglevel */
941 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
943 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
944 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
950 verb_to_mask(const char *word, slap_verbmasks *v) {
952 ber_str2bv( word, 0, 0, &bword );
953 return bverb_to_mask( &bword, v );
957 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
959 for(i = 1; i < argc; i++) {
960 j = verb_to_mask(argv[i], v);
961 if(BER_BVISNULL(&v[j].word)) return i;
962 while (!v[j].mask) j--;
968 /* Mask keywords that represent multiple bits should occur before single
969 * bit keywords in the verbmasks array.
972 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
976 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
977 if (!v[i].mask) continue;
978 if (( m & v[i].mask ) == v[i].mask ) {
979 value_add_one( bva, &v[i].word );
989 /* Return the verbs as a single string, separated by delim */
991 mask_to_verbstring(slap_verbmasks *v, slap_mask_t m0, char delim, struct berval *bv)
999 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1000 if (!v[i].mask) continue;
1001 if (( m & v[i].mask ) == v[i].mask ) {
1002 bv->bv_len += v[i].word.bv_len + 1;
1008 bv->bv_val = ch_malloc(bv->bv_len);
1012 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1013 if (!v[i].mask) continue;
1014 if (( m & v[i].mask ) == v[i].mask ) {
1015 ptr = lutil_strcopy(ptr, v[i].word.bv_val);
1026 /* Parse a verbstring */
1028 verbstring_to_mask(slap_verbmasks *v, char *str, char delim, slap_mask_t *m) {
1035 d = strchr( str, delim );
1037 bv.bv_len = d - str;
1039 bv.bv_len = strlen( str );
1040 j = bverb_to_mask( &bv, v );
1041 if(BER_BVISNULL(&v[j].word)) return 1;
1042 while (!v[j].mask) j--;
1044 str += bv.bv_len + 1;
1050 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
1054 assert( *vp == NULL );
1056 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
1058 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
1060 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1061 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
1062 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
1065 BER_BVZERO( &(*vp)[ i ].word );
1071 slap_verbmasks_destroy( slap_verbmasks *v )
1075 assert( v != NULL );
1077 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1078 ch_free( v[ i ].word.bv_val );
1087 slap_verbmasks_append(
1088 slap_verbmasks **vp,
1091 slap_mask_t *ignore )
1096 return LDAP_OPERATIONS_ERROR;
1099 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
1100 if ( !(*vp)[ i ].mask ) continue;
1102 if ( ignore != NULL ) {
1105 for ( j = 0; ignore[ j ] != 0; j++ ) {
1106 if ( (*vp)[ i ].mask == ignore[ j ] ) {
1112 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
1113 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
1114 /* already set; ignore */
1115 return LDAP_SUCCESS;
1118 return LDAP_TYPE_OR_VALUE_EXISTS;
1121 if ( m & (*vp)[ i ].mask ) {
1123 return LDAP_CONSTRAINT_VIOLATION;
1128 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1129 ber_dupbv( &(*vp)[ i ].word, v );
1130 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1131 BER_BVZERO( &(*vp)[ i + 1 ].word );
1133 return LDAP_SUCCESS;
1137 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1140 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1141 if ( m == v[i].mask ) {
1151 /* register a new verbmask */
1153 slap_verbmask_register( slap_verbmasks *vm_, slap_verbmasks **vmp, struct berval *bv, int mask )
1155 slap_verbmasks *vm = *vmp;
1158 /* check for duplicate word */
1159 /* NOTE: we accept duplicate codes; the first occurrence will be used
1160 * when mapping from mask to verb */
1161 i = verb_to_mask( bv->bv_val, vm );
1162 if ( !BER_BVISNULL( &vm[ i ].word ) ) {
1166 for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
1170 /* first time: duplicate array */
1171 vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
1172 for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
1174 ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
1175 *((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
1179 vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
1182 ber_dupbv( &vm[ i ].word, bv );
1183 *((slap_mask_t*)&vm[ i ].mask) = mask;
1185 BER_BVZERO( &vm[ i+1 ].word );
1192 static slap_verbmasks slap_ldap_response_code_[] = {
1193 { BER_BVC("success"), LDAP_SUCCESS },
1195 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR },
1196 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR },
1197 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED },
1198 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED },
1199 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE },
1200 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE },
1202 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED },
1203 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED },
1204 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED },
1205 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED },
1206 #if 0 /* not LDAPv3 */
1207 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS },
1210 { BER_BVC("referral"), LDAP_REFERRAL },
1211 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED },
1212 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
1213 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED },
1214 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS },
1216 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE },
1217 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE },
1218 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING },
1219 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION },
1220 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS },
1221 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX },
1223 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT },
1224 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM },
1225 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX },
1226 #if 0 /* not LDAPv3 */
1227 { BER_BVC("isLeaf"), LDAP_IS_LEAF },
1229 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM },
1231 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE },
1232 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH },
1233 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS },
1234 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS },
1236 { BER_BVC("busy"), LDAP_BUSY },
1237 { BER_BVC("unavailable"), LDAP_UNAVAILABLE },
1238 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM },
1239 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT },
1241 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION },
1242 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION },
1243 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF },
1244 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN },
1245 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS },
1246 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS },
1247 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE },
1248 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS },
1250 { BER_BVC("other"), LDAP_OTHER },
1252 /* extension-specific */
1254 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED },
1255 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION },
1256 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA },
1257 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME },
1258 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED },
1260 { BER_BVC("cancelled"), LDAP_CANCELLED },
1261 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION },
1262 { BER_BVC("tooLate"), LDAP_TOO_LATE },
1263 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL },
1265 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED },
1267 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED },
1269 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED },
1271 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION },
1276 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_;
1279 slap_ldap_response_code_register( struct berval *bv, int err )
1281 return slap_verbmask_register( slap_ldap_response_code_,
1282 &slap_ldap_response_code, bv, err );
1286 static slap_verbmasks tlskey[] = {
1287 { BER_BVC("no"), SB_TLS_OFF },
1288 { BER_BVC("yes"), SB_TLS_ON },
1289 { BER_BVC("critical"), SB_TLS_CRITICAL },
1293 static slap_verbmasks crlkeys[] = {
1294 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE },
1295 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER },
1296 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL },
1300 static slap_verbmasks vfykeys[] = {
1301 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER },
1302 { BER_BVC("allow"), LDAP_OPT_X_TLS_ALLOW },
1303 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY },
1304 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND },
1305 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD },
1306 { BER_BVC("true"), LDAP_OPT_X_TLS_HARD },
1311 static slap_verbmasks methkey[] = {
1312 { BER_BVC("none"), LDAP_AUTH_NONE },
1313 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1314 #ifdef HAVE_CYRUS_SASL
1315 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1320 static slap_verbmasks versionkey[] = {
1321 { BER_BVC("2"), LDAP_VERSION2 },
1322 { BER_BVC("3"), LDAP_VERSION3 },
1327 slap_keepalive_parse(
1330 slap_cf_aux_table *tab0,
1335 slap_keepalive *sk = (slap_keepalive *)bc;
1336 int rc = snprintf( val->bv_val, val->bv_len, "%d:%d:%d",
1337 sk->sk_idle, sk->sk_probes, sk->sk_interval );
1342 if ( (unsigned)rc >= val->bv_len ) {
1349 char *s = val->bv_val;
1351 slap_keepalive *sk = (slap_keepalive *)bc;
1354 if ( s[0] == ':' ) {
1359 sk2.sk_idle = strtol( s, &next, 10 );
1360 if ( next == s || next[0] != ':' ) {
1364 if ( sk2.sk_idle < 0 ) {
1371 if ( s[0] == ':' ) {
1376 sk2.sk_probes = strtol( s, &next, 10 );
1377 if ( next == s || next[0] != ':' ) {
1381 if ( sk2.sk_probes < 0 ) {
1389 sk2.sk_interval = 0;
1392 sk2.sk_interval = strtol( s, &next, 10 );
1393 if ( next == s || next[0] != '\0' ) {
1397 if ( sk2.sk_interval < 0 ) {
1404 ber_memfree( val->bv_val );
1415 slap_cf_aux_table *tab0,
1419 slap_bindconf *bc = bcp;
1421 if ( bc->sb_uri.bv_len >= val->bv_len )
1423 val->bv_len = bc->sb_uri.bv_len;
1424 AC_MEMCPY( val->bv_val, bc->sb_uri.bv_val, val->bv_len );
1428 if ( ldap_is_ldaps_url( val->bv_val ))
1429 bc->sb_tls_do_init = 1;
1435 static slap_cf_aux_table bindkey[] = {
1436 { BER_BVC("uri="), 0, 'x', 1, slap_sb_uri },
1437 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1438 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1439 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1440 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1441 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1442 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1443 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1444 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1445 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1446 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1447 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1448 { BER_BVC("keepalive="), offsetof(slap_bindconf, sb_keepalive), 'x', 0, (slap_verbmasks *)slap_keepalive_parse },
1450 /* NOTE: replace "13" with the actual index
1451 * of the first TLS-related line */
1452 #define aux_TLS (bindkey+13) /* beginning of TLS keywords */
1454 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1455 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1456 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1457 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1458 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1459 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 0, NULL },
1460 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 0, NULL },
1461 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 0, NULL },
1462 #ifdef HAVE_OPENSSL_CRL
1463 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 0, NULL },
1466 { BER_BVNULL, 0, 0, 0, NULL }
1471 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1472 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1475 * 'U': unsigned long
1479 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1481 int rc = SLAP_CONF_UNKNOWN;
1482 slap_cf_aux_table *tab;
1484 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1485 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1490 unsigned long *ulptr;
1491 struct berval *bptr;
1492 const char *val = word + tab->key.bv_len;
1494 switch ( tab->type ) {
1496 cptr = (char **)((char *)dst + tab->off);
1497 *cptr = ch_strdup( val );
1502 bptr = (struct berval *)((char *)dst + tab->off);
1503 if ( tab->aux != NULL ) {
1505 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1507 ber_str2bv( val, 0, 0, &dn );
1508 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1511 ber_str2bv( val, 0, 1, bptr );
1517 iptr = (int *)((char *)dst + tab->off);
1519 if ( tab->aux != NULL ) {
1520 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1522 assert( aux != NULL );
1525 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1526 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1527 *iptr = aux[j].mask;
1534 rc = lutil_atoix( iptr, val, 0 );
1539 uptr = (unsigned *)((char *)dst + tab->off);
1541 rc = lutil_atoux( uptr, val, 0 );
1545 lptr = (long *)((char *)dst + tab->off);
1547 rc = lutil_atolx( lptr, val, 0 );
1551 ulptr = (unsigned long *)((char *)dst + tab->off);
1553 rc = lutil_atoulx( ulptr, val, 0 );
1557 if ( tab->aux != NULL ) {
1558 struct berval value;
1559 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1561 ber_str2bv( val, 0, 1, &value );
1563 rc = func( &value, (void *)((char *)dst + tab->off), tab, tabmsg, 0 );
1572 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1584 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1586 char buf[AC_LINE_MAX], *ptr;
1587 slap_cf_aux_table *tab;
1591 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1596 unsigned long *ulptr;
1597 struct berval *bptr;
1599 cptr = (char **)((char *)src + tab->off);
1601 switch ( tab->type ) {
1603 bptr = (struct berval *)((char *)src + tab->off);
1604 cptr = &bptr->bv_val;
1609 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1610 if ( tab->quote ) *ptr++ = '"';
1611 ptr = lutil_strcopy( ptr, *cptr );
1612 if ( tab->quote ) *ptr++ = '"';
1617 iptr = (int *)((char *)src + tab->off);
1619 if ( tab->aux != NULL ) {
1620 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1622 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1623 if ( *iptr == aux[i].mask ) {
1625 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1626 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1633 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1634 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1639 uptr = (unsigned *)((char *)src + tab->off);
1641 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1642 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1646 lptr = (long *)((char *)src + tab->off);
1648 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1649 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1653 ulptr = (unsigned long *)((char *)src + tab->off);
1655 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1656 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1663 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1664 if ( tab->quote ) *ptr++ = '"';
1665 if ( tab->aux != NULL ) {
1666 struct berval value;
1667 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1671 value.bv_len = buf + sizeof( buf ) - ptr;
1673 rc = func( &value, (void *)((char *)src + tab->off), tab, "(unparse)", 1 );
1676 ptr += value.bv_len;
1683 if ( tab->quote ) *ptr++ = '"';
1692 tmp.bv_len = ptr - buf;
1693 ber_dupbv( bv, &tmp );
1698 slap_tls_get_config( LDAP *ld, int opt, char **val )
1701 slap_verbmasks *keys;
1706 case LDAP_OPT_X_TLS_CRLCHECK:
1709 case LDAP_OPT_X_TLS_REQUIRE_CERT:
1712 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
1714 ldap_pvt_tls_get_option( ld, opt, &ival );
1715 snprintf( buf, sizeof( buf ), "%d.%d",
1716 ( ival >> 8 ) & 0xff, ival & 0xff );
1717 *val = ch_strdup( buf );
1723 ldap_pvt_tls_get_option( ld, opt, &ival );
1724 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1725 if (keys[i].mask == ival) {
1726 *val = ch_strdup( keys[i].word.bv_val );
1735 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1738 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1739 bc->sb_tls_do_init = 1;
1747 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1750 return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1756 bindconf_parse( const char *word, slap_bindconf *bc )
1759 /* Detect TLS config changes explicitly */
1760 if ( bindconf_tls_parse( word, bc ) == 0 ) {
1764 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1768 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1770 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1773 void bindconf_free( slap_bindconf *bc ) {
1774 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1775 ch_free( bc->sb_uri.bv_val );
1776 BER_BVZERO( &bc->sb_uri );
1778 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1779 ch_free( bc->sb_binddn.bv_val );
1780 BER_BVZERO( &bc->sb_binddn );
1782 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1783 ch_free( bc->sb_cred.bv_val );
1784 BER_BVZERO( &bc->sb_cred );
1786 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1787 ch_free( bc->sb_saslmech.bv_val );
1788 BER_BVZERO( &bc->sb_saslmech );
1790 if ( bc->sb_secprops ) {
1791 ch_free( bc->sb_secprops );
1792 bc->sb_secprops = NULL;
1794 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1795 ch_free( bc->sb_realm.bv_val );
1796 BER_BVZERO( &bc->sb_realm );
1798 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1799 ch_free( bc->sb_authcId.bv_val );
1800 BER_BVZERO( &bc->sb_authcId );
1802 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1803 ch_free( bc->sb_authzId.bv_val );
1804 BER_BVZERO( &bc->sb_authzId );
1807 if ( bc->sb_tls_cert ) {
1808 ch_free( bc->sb_tls_cert );
1809 bc->sb_tls_cert = NULL;
1811 if ( bc->sb_tls_key ) {
1812 ch_free( bc->sb_tls_key );
1813 bc->sb_tls_key = NULL;
1815 if ( bc->sb_tls_cacert ) {
1816 ch_free( bc->sb_tls_cacert );
1817 bc->sb_tls_cacert = NULL;
1819 if ( bc->sb_tls_cacertdir ) {
1820 ch_free( bc->sb_tls_cacertdir );
1821 bc->sb_tls_cacertdir = NULL;
1823 if ( bc->sb_tls_reqcert ) {
1824 ch_free( bc->sb_tls_reqcert );
1825 bc->sb_tls_reqcert = NULL;
1827 if ( bc->sb_tls_cipher_suite ) {
1828 ch_free( bc->sb_tls_cipher_suite );
1829 bc->sb_tls_cipher_suite = NULL;
1831 if ( bc->sb_tls_protocol_min ) {
1832 ch_free( bc->sb_tls_protocol_min );
1833 bc->sb_tls_protocol_min = NULL;
1835 #ifdef HAVE_OPENSSL_CRL
1836 if ( bc->sb_tls_crlcheck ) {
1837 ch_free( bc->sb_tls_crlcheck );
1838 bc->sb_tls_crlcheck = NULL;
1841 if ( bc->sb_tls_ctx ) {
1842 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1843 bc->sb_tls_ctx = NULL;
1849 bindconf_tls_defaults( slap_bindconf *bc )
1852 if ( bc->sb_tls_do_init ) {
1853 if ( !bc->sb_tls_cacert )
1854 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1855 &bc->sb_tls_cacert );
1856 if ( !bc->sb_tls_cacertdir )
1857 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1858 &bc->sb_tls_cacertdir );
1859 if ( !bc->sb_tls_cert )
1860 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1862 if ( !bc->sb_tls_key )
1863 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1865 if ( !bc->sb_tls_cipher_suite )
1866 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1867 &bc->sb_tls_cipher_suite );
1868 if ( !bc->sb_tls_reqcert )
1869 bc->sb_tls_reqcert = ch_strdup("demand");
1870 #ifdef HAVE_OPENSSL_CRL
1871 if ( !bc->sb_tls_crlcheck )
1872 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1873 &bc->sb_tls_crlcheck );
1885 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1886 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1887 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1888 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1889 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1890 { "tls_protocol_min", offsetof(slap_bindconf, sb_tls_protocol_min), LDAP_OPT_X_TLS_PROTOCOL_MIN },
1894 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1896 int i, rc, newctx = 0, res = 0;
1897 char *ptr = (char *)bc, **word;
1899 bc->sb_tls_do_init = 0;
1901 for (i=0; bindtlsopts[i].opt; i++) {
1902 word = (char **)(ptr + bindtlsopts[i].offset);
1904 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1906 Debug( LDAP_DEBUG_ANY,
1907 "bindconf_tls_set: failed to set %s to %s\n",
1908 bindtlsopts[i].key, *word, 0 );
1914 if ( bc->sb_tls_reqcert ) {
1915 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1916 bc->sb_tls_reqcert );
1918 Debug( LDAP_DEBUG_ANY,
1919 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1920 bc->sb_tls_reqcert, 0, 0 );
1925 if ( bc->sb_tls_protocol_min ) {
1926 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN,
1927 bc->sb_tls_protocol_min );
1929 Debug( LDAP_DEBUG_ANY,
1930 "bindconf_tls_set: failed to set tls_protocol_min to %s\n",
1931 bc->sb_tls_protocol_min, 0, 0 );
1936 #ifdef HAVE_OPENSSL_CRL
1937 if ( bc->sb_tls_crlcheck ) {
1938 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1939 bc->sb_tls_crlcheck );
1941 Debug( LDAP_DEBUG_ANY,
1942 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1943 bc->sb_tls_crlcheck, 0, 0 );
1952 if ( bc->sb_tls_ctx ) {
1953 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1954 bc->sb_tls_ctx = NULL;
1956 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1960 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1968 * set connection keepalive options
1971 slap_client_keepalive(LDAP *ld, slap_keepalive *sk)
1975 if ( sk->sk_idle ) {
1976 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_IDLE, &sk->sk_idle );
1979 if ( sk->sk_probes ) {
1980 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_PROBES, &sk->sk_probes );
1983 if ( sk->sk_interval ) {
1984 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_INTERVAL, &sk->sk_interval );
1991 * connect to a client using the bindconf data
1992 * note: should move "version" into bindconf...
1995 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
2001 /* Init connection to master */
2002 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
2003 if ( rc != LDAP_SUCCESS ) {
2004 Debug( LDAP_DEBUG_ANY,
2005 "slap_client_connect: "
2006 "ldap_initialize(%s) failed (%d)\n",
2007 sb->sb_uri.bv_val, rc, 0 );
2011 if ( sb->sb_version != 0 ) {
2012 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
2013 (const void *)&sb->sb_version );
2016 if ( sb->sb_timeout_api ) {
2017 tv.tv_sec = sb->sb_timeout_api;
2019 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
2022 if ( sb->sb_timeout_net ) {
2023 tv.tv_sec = sb->sb_timeout_net;
2025 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
2028 /* setting network keepalive options */
2029 slap_client_keepalive(ld, &sb->sb_keepalive);
2032 if ( sb->sb_tls_do_init ) {
2033 rc = bindconf_tls_set( sb, ld );
2035 } else if ( sb->sb_tls_ctx ) {
2036 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
2041 Debug( LDAP_DEBUG_ANY,
2042 "slap_client_connect: "
2043 "URI=%s TLS context initialization failed (%d)\n",
2044 sb->sb_uri.bv_val, rc, 0 );
2051 rc = ldap_start_tls_s( ld, NULL, NULL );
2052 if ( rc != LDAP_SUCCESS ) {
2053 Debug( LDAP_DEBUG_ANY,
2054 "slap_client_connect: URI=%s "
2055 "%s, ldap_start_tls failed (%d)\n",
2057 sb->sb_tls == SB_TLS_CRITICAL ?
2058 "Error" : "Warning",
2060 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
2066 if ( sb->sb_method == LDAP_AUTH_SASL ) {
2067 #ifdef HAVE_CYRUS_SASL
2070 if ( sb->sb_secprops != NULL ) {
2071 rc = ldap_set_option( ld,
2072 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
2074 if( rc != LDAP_OPT_SUCCESS ) {
2075 Debug( LDAP_DEBUG_ANY,
2076 "slap_client_connect: "
2077 "error, ldap_set_option "
2078 "(%s,SECPROPS,\"%s\") failed!\n",
2079 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
2084 defaults = lutil_sasl_defaults( ld,
2085 sb->sb_saslmech.bv_val,
2086 sb->sb_realm.bv_val,
2087 sb->sb_authcId.bv_val,
2089 sb->sb_authzId.bv_val );
2090 if ( defaults == NULL ) {
2095 rc = ldap_sasl_interactive_bind_s( ld,
2096 sb->sb_binddn.bv_val,
2097 sb->sb_saslmech.bv_val,
2100 lutil_sasl_interact,
2103 lutil_sasl_freedefs( defaults );
2105 /* FIXME: different error behaviors according to
2107 * 2) on err policy : exit, retry, backoff ...
2109 if ( rc != LDAP_SUCCESS ) {
2110 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
2112 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
2113 "ldap_sasl_interactive_bind_s failed (%d)\n",
2114 sb->sb_uri.bv_val, rc, 0 );
2116 /* FIXME (see above comment) */
2117 /* if Kerberos credentials cache is not active, retry */
2118 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
2119 rc == LDAP_LOCAL_ERROR )
2121 rc = LDAP_SERVER_DOWN;
2126 #else /* HAVE_CYRUS_SASL */
2127 /* Should never get here, we trapped this at config time */
2129 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
2134 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
2135 rc = ldap_sasl_bind_s( ld,
2136 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
2137 &sb->sb_cred, NULL, NULL, NULL );
2138 if ( rc != LDAP_SUCCESS ) {
2139 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
2141 "ldap_sasl_bind_s failed (%d)\n",
2142 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
2150 ldap_unbind_ext( ld, NULL, NULL );
2161 /* -------------------------------------- */
2165 strtok_quote( char *line, char *sep, char **quote_ptr, int *iqp )
2172 if ( line != NULL ) {
2175 while ( *next && strchr( sep, *next ) ) {
2179 if ( *next == '\0' ) {
2185 for ( inquote = 0; *next; ) {
2193 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2199 next + 1, strlen( next + 1 ) + 1 );
2200 next++; /* dont parse the escaped character */
2205 if ( strchr( sep, *next ) != NULL ) {
2220 static char buf[AC_LINE_MAX];
2222 static size_t lmax, lcur;
2224 #define CATLINE( buf ) \
2226 size_t len = strlen( buf ); \
2227 while ( lcur + len + 1 > lmax ) { \
2228 lmax += AC_LINE_MAX; \
2229 line = (char *) ch_realloc( line, lmax ); \
2231 strcpy( line + lcur, buf ); \
2236 fp_getline_init(ConfigArgs *c) {
2242 fp_getline( FILE *fp, ConfigArgs *c )
2250 /* avoid stack of bufs */
2251 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
2257 while ( fgets( buf, sizeof( buf ), fp ) ) {
2258 p = strchr( buf, '\n' );
2260 if ( p > buf && p[-1] == '\r' ) {
2268 && ( p = line + strlen( line ) - 1 )[0] == '\\'
2275 if ( !isspace( (unsigned char)buf[0] ) ) {
2286 return(line[0] ? 1 : 0);
2290 config_fp_parse_line(ConfigArgs *c)
2293 static char *const hide[] = {
2294 "rootpw", "replica", "syncrepl", /* in slapd */
2295 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
2296 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
2297 "pseudorootpw", /* in back-meta */
2298 "dbpasswd", /* in back-sql */
2301 static char *const raw[] = {
2302 "attributetype", "objectclass", "ditcontentrule", "ldapsyntax", NULL };
2304 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
2307 c->tline = ch_strdup(c->line);
2308 c->linelen = strlen(c->line);
2309 token = strtok_quote(c->tline, " \t", "e_ptr, &inquote);
2311 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2312 if(quote_ptr) *quote_ptr = ' ';
2313 Debug(LDAP_DEBUG_CONFIG, "%s (%s%s)\n", c->log,
2314 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2315 if(quote_ptr) *quote_ptr = '\0';
2317 for(;; token = strtok_quote(NULL, " \t", "e_ptr, &inquote)) {
2318 if(c->argc >= c->argv_size) {
2320 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2322 Debug(LDAP_DEBUG_ANY, "%s: out of memory\n", c->log, 0, 0);
2326 c->argv_size += ARGS_STEP;
2330 c->argv[c->argc++] = token;
2332 c->argv[c->argc] = NULL;
2334 /* these directives parse c->line independently of argv tokenizing */
2335 for(i = 0; raw[i]; i++) if (!strcasecmp(c->argv[0], raw[i])) return 0;
2337 Debug(LDAP_DEBUG_ANY, "%s: unterminated quoted string \"%s\"\n", c->log, c->argv[c->argc-1], 0);
2346 ucdata_unload( UCDATA_ALL );
2348 /* NOTE: in case of early exit, frontendDB can be NULL */
2349 if ( frontendDB->be_schemandn.bv_val )
2350 free( frontendDB->be_schemandn.bv_val );
2351 if ( frontendDB->be_schemadn.bv_val )
2352 free( frontendDB->be_schemadn.bv_val );
2353 if ( frontendDB->be_acl )
2354 acl_destroy( frontendDB->be_acl );
2357 if ( slapd_args_file )
2358 free ( slapd_args_file );
2359 if ( slapd_pid_file )
2360 free ( slapd_pid_file );
2361 if ( default_passwd_hash )
2362 ldap_charray_free( default_passwd_hash );
2366 slap_str2clist( char ***out, char *in, const char *brkstr )
2374 /* find last element in list */
2375 for (i = 0; *out && (*out)[i]; i++);
2377 /* protect the input string from strtok */
2378 str = ch_strdup( in );
2380 if ( *str == '\0' ) {
2385 /* Count words in string */
2387 for ( s = str; *s; s++ ) {
2388 if ( strchr( brkstr, *s ) != NULL ) {
2393 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2395 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2397 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2399 *new = ch_strdup( s );
2408 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
2409 int argc, char **argv )
2411 ConfigArgs c = { 0 };
2422 c.op = SLAP_CONFIG_ADD;
2423 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
2425 rc = SLAP_CONF_UNKNOWN;
2426 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
2428 c.table = be->be_cf_ocs->co_type;
2429 rc = config_add_vals( ct, &c );
2434 /* See if the given URL (in plain and parsed form) matches
2435 * any of the server's listener addresses. Return matching
2436 * Listener or NULL for no match.
2438 Listener *config_check_my_url( const char *url, LDAPURLDesc *lud )
2440 Listener **l = slapd_get_listeners();
2443 /* Try a straight compare with Listener strings */
2444 for ( i=0; l && l[i]; i++ ) {
2445 if ( !strcasecmp( url, l[i]->sl_url.bv_val )) {
2451 /* If hostname is empty, or is localhost, or matches
2452 * our hostname, this url refers to this host.
2453 * Compare it against listeners and ports.
2455 if ( !lud->lud_host || !lud->lud_host[0] ||
2456 !strncasecmp("localhost", lud->lud_host,
2457 STRLENOF("localhost")) ||
2458 !strcasecmp( global_host, lud->lud_host )) {
2460 for ( i=0; l && l[i]; i++ ) {
2462 ldap_url_parse( l[i]->sl_url.bv_val, &lu2 );
2464 if ( strcasecmp( lud->lud_scheme,
2467 if ( lud->lud_port != lu2->lud_port )
2469 /* Listener on ANY address */
2470 if ( !lu2->lud_host || !lu2->lud_host[0] ) {
2474 /* URL on ANY address */
2475 if ( !lud->lud_host || !lud->lud_host[0] ) {
2479 /* Listener has specific host, must
2482 if ( !strcasecmp( lud->lud_host,
2488 ldap_free_urldesc( lu2 );