1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2016 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;
132 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
133 int rc, arg_user, arg_type, arg_syn, iarg;
139 if(Conf->arg_type == ARG_IGNORED) {
140 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
141 c->log, Conf->name, 0);
144 arg_type = Conf->arg_type & ARGS_TYPES;
145 arg_user = Conf->arg_type & ARGS_USERLAND;
146 arg_syn = Conf->arg_type & ARGS_SYNTAX;
148 if((arg_type == ARG_DN) && c->argc == 1) {
152 if(Conf->min_args && (c->argc < Conf->min_args)) {
153 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
154 c->argv[0], Conf->what ? Conf->what : "" );
155 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
156 return(ARG_BAD_CONF);
158 if(Conf->max_args && (c->argc > Conf->max_args)) {
159 char *ignored = " ignored";
161 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
162 c->argv[0], Conf->what );
165 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
166 c->log, c->cr_msg, ignored );
167 return(ARG_BAD_CONF);
169 if((arg_syn & ARG_DB) && !c->be) {
170 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
172 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
173 c->log, c->cr_msg, 0);
174 return(ARG_BAD_CONF);
176 if((arg_syn & ARG_PRE_BI) && c->bi) {
177 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
178 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
179 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
180 c->log, c->cr_msg, 0 );
181 return(ARG_BAD_CONF);
183 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
184 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
186 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
187 c->log, c->cr_msg, 0);
188 return(ARG_BAD_CONF);
190 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
191 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
192 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
193 c->log, c->cr_msg, 0);
194 return(ARG_BAD_CONF);
196 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
197 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
199 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
200 c->log, c->cr_msg, 0);
201 return(ARG_BAD_CONF);
204 memset(&c->values, 0, sizeof(c->values));
205 if(arg_type == ARG_STRING) {
206 assert( c->argc == 2 );
208 c->value_string = ch_strdup(c->argv[1]);
209 } else if(arg_type == ARG_BERVAL) {
210 assert( c->argc == 2 );
212 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
213 } else if(arg_type == ARG_DN) {
215 assert( c->argc == 2 );
216 ber_str2bv( c->argv[1], 0, 0, &bv );
217 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
218 if ( rc != LDAP_SUCCESS ) {
219 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
220 c->argv[0], rc, ldap_err2string( rc ));
221 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
222 return(ARG_BAD_CONF);
225 ch_free( c->value_ndn.bv_val );
226 ch_free( c->value_dn.bv_val );
228 } else if(arg_type == ARG_ATDESC) {
229 const char *text = NULL;
230 assert( c->argc == 2 );
232 rc = slap_str2ad( c->argv[1], &c->value_ad, &text );
233 if ( rc != LDAP_SUCCESS ) {
234 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid AttributeDescription %d (%s)",
235 c->argv[0], rc, text );
236 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
237 return(ARG_BAD_CONF);
239 } else { /* all numeric */
241 iarg = 0; larg = 0; barg = 0;
244 assert( c->argc == 2 );
245 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
246 snprintf( c->cr_msg, sizeof( c->cr_msg ),
247 "<%s> unable to parse \"%s\" as int",
248 c->argv[0], c->argv[1] );
249 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
250 c->log, c->cr_msg, 0);
251 return(ARG_BAD_CONF);
255 assert( c->argc == 2 );
256 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
257 snprintf( c->cr_msg, sizeof( c->cr_msg ),
258 "<%s> unable to parse \"%s\" as unsigned int",
259 c->argv[0], c->argv[1] );
260 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
261 c->log, c->cr_msg, 0);
262 return(ARG_BAD_CONF);
266 assert( c->argc == 2 );
267 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
268 snprintf( c->cr_msg, sizeof( c->cr_msg ),
269 "<%s> unable to parse \"%s\" as long",
270 c->argv[0], c->argv[1] );
271 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
272 c->log, c->cr_msg, 0);
273 return(ARG_BAD_CONF);
277 assert( c->argc == 2 );
278 if ( LUTIL_ATOULX( &ularg, c->argv[1], 0 ) != 0 ) {
279 snprintf( c->cr_msg, sizeof( c->cr_msg ),
280 "<%s> unable to parse \"%s\" as unsigned long",
281 c->argv[0], c->argv[1] );
282 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
283 c->log, c->cr_msg, 0);
284 return(ARG_BAD_CONF);
287 case ARG_BER_LEN_T: {
289 assert( c->argc == 2 );
290 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
291 snprintf( c->cr_msg, sizeof( c->cr_msg ),
292 "<%s> unable to parse \"%s\" as ber_len_t",
293 c->argv[0], c->argv[1] );
294 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
295 c->log, c->cr_msg, 0);
296 return(ARG_BAD_CONF);
301 /* note: this is an explicit exception
302 * to the "need exactly 2 args" rule */
305 } else if ( !strcasecmp(c->argv[1], "on") ||
306 !strcasecmp(c->argv[1], "true") ||
307 !strcasecmp(c->argv[1], "yes") )
310 } else if ( !strcasecmp(c->argv[1], "off") ||
311 !strcasecmp(c->argv[1], "false") ||
312 !strcasecmp(c->argv[1], "no") )
316 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
318 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
319 c->log, c->cr_msg, 0 );
320 return(ARG_BAD_CONF);
324 j = (arg_type & ARG_NONZERO) ? 1 : 0;
325 if(iarg < j && larg < j && barg < (unsigned)j ) {
326 larg = larg ? larg : (barg ? (long)barg : iarg);
327 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
329 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
330 c->log, c->cr_msg, 0 );
331 return(ARG_BAD_CONF);
335 case ARG_INT: c->value_int = iarg; break;
336 case ARG_UINT: c->value_uint = uiarg; break;
337 case ARG_LONG: c->value_long = larg; break;
338 case ARG_ULONG: c->value_ulong = ularg; break;
339 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
345 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
349 arg_type = Conf->arg_type;
350 if(arg_type & ARG_MAGIC) {
351 if(!c->be) c->be = frontendDB;
353 rc = (*((ConfigDriver*)Conf->arg_item))(c);
355 if(c->be == frontendDB) c->be = NULL;
358 if ( !c->cr_msg[0] ) {
359 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
361 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
362 c->log, c->cr_msg, 0 );
364 return(ARG_BAD_CONF);
368 if(arg_type & ARG_OFFSET) {
369 if (c->be && c->table == Cft_Database)
370 ptr = c->be->be_private;
372 ptr = c->bi->bi_private;
374 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
376 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
377 c->log, c->cr_msg, 0);
378 return(ARG_BAD_CONF);
380 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
381 } else if (arg_type & ARGS_TYPES) {
382 ptr = Conf->arg_item;
384 if(arg_type & ARGS_TYPES)
385 switch(arg_type & ARGS_TYPES) {
387 case ARG_INT: *(int*)ptr = c->value_int; break;
388 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break;
389 case ARG_LONG: *(long*)ptr = c->value_long; break;
390 case ARG_ULONG: *(size_t*)ptr = c->value_ulong; break;
391 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
393 char *cc = *(char**)ptr;
395 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
396 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
397 c->log, Conf->name, 0 );
398 return(ARG_BAD_CONF);
402 *(char **)ptr = c->value_string;
406 *(struct berval *)ptr = c->value_bv;
409 *(AttributeDescription **)ptr = c->value_ad;
415 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
418 arg_type = Conf->arg_type;
419 if(arg_type == ARG_IGNORED) {
420 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
421 c->log, Conf->name, 0);
424 rc = config_check_vals( Conf, c, 0 );
426 return config_set_vals( Conf, c );
430 config_del_vals(ConfigTable *cf, ConfigArgs *c)
434 /* If there is no handler, just ignore it */
435 if ( cf->arg_type & ARG_MAGIC ) {
436 c->argv[0] = cf->ad->ad_cname.bv_val;
437 c->op = LDAP_MOD_DELETE;
438 c->type = cf->arg_type & ARGS_USERLAND;
439 rc = (*((ConfigDriver*)cf->arg_item))(c);
445 config_get_vals(ConfigTable *cf, ConfigArgs *c)
451 if ( cf->arg_type & ARG_IGNORED ) {
455 memset(&c->values, 0, sizeof(c->values));
456 c->rvalue_vals = NULL;
457 c->rvalue_nvals = NULL;
458 c->op = SLAP_CONFIG_EMIT;
459 c->type = cf->arg_type & ARGS_USERLAND;
461 if ( cf->arg_type & ARG_MAGIC ) {
462 rc = (*((ConfigDriver*)cf->arg_item))(c);
465 if ( cf->arg_type & ARG_OFFSET ) {
466 if (c->be && c->table == Cft_Database)
467 ptr = c->be->be_private;
469 ptr = c->bi->bi_private;
472 ptr = (void *)((char *)ptr + (long)cf->arg_item);
477 switch(cf->arg_type & ARGS_TYPES) {
479 case ARG_INT: c->value_int = *(int *)ptr; break;
480 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break;
481 case ARG_LONG: c->value_long = *(long *)ptr; break;
482 case ARG_ULONG: c->value_ulong = *(size_t *)ptr; break;
483 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
486 c->value_string = ch_strdup(*(char **)ptr);
489 c->value_bv = *((struct berval *)ptr); break;
491 c->value_ad = *(AttributeDescription **)ptr; break;
494 if ( cf->arg_type & ARGS_TYPES) {
497 switch(cf->arg_type & ARGS_TYPES) {
498 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
499 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
500 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
501 case ARG_ULONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%" Z "u", c->value_ulong); break;
502 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
503 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
504 c->value_int ? "TRUE" : "FALSE"); break;
506 if ( c->value_string && c->value_string[0]) {
507 ber_str2bv( c->value_string, 0, 0, &bv);
513 if ( !BER_BVISEMPTY( &c->value_bv )) {
521 bv = c->value_ad->ad_cname;
530 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
533 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
534 ber_bvarray_add(&c->rvalue_vals, &bv);
535 } else if ( !BER_BVISNULL( &bv ) ) {
536 value_add_one(&c->rvalue_vals, &bv);
538 /* else: maybe c->rvalue_vals already set? */
544 init_config_attrs(ConfigTable *ct) {
547 for (i=0; ct[i].name; i++ ) {
548 if ( !ct[i].attribute ) continue;
549 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
551 fprintf( stderr, "init_config_attrs: register_at failed\n" );
560 init_config_ocs( ConfigOCs *ocs ) {
563 for (i=0;ocs[i].co_def;i++) {
564 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
566 fprintf( stderr, "init_config_ocs: register_oc failed\n" );
573 /* Split an LDIF line into space-separated tokens. Words may be grouped
574 * by quotes. A quoted string may begin in the middle of a word, but must
575 * end at the end of the word (be followed by whitespace or EOS). Any other
576 * quotes are passed through unchanged. All other characters are passed
580 strtok_quote_ldif( char **line )
582 char *beg, *ptr, *quote=NULL;
590 while( isspace( (unsigned char) *ptr )) ptr++;
601 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
611 if ( isspace( (unsigned char) *ptr )) {
617 while ( quote < ptr ) {
625 while ( isspace( (unsigned char) *ptr )) ptr++;
632 config_parse_ldif( ConfigArgs *c )
635 c->tline = ch_strdup(c->line);
638 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
640 if ( c->argc >= c->argv_size ) {
641 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
644 c->argv_size += ARGS_STEP;
647 c->argv[c->argc] = NULL;
651 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
655 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
656 ct->ad->ad_cname.bv_val, valx );
658 c->argv[0] = ct->ad->ad_cname.bv_val;
660 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
661 c->argv[c->argc] = c->line;
663 c->argv[c->argc] = NULL;
666 config_parse_ldif( c );
668 rc = config_check_vals( ct, c, 1 );
673 rc = LDAP_CONSTRAINT_VIOLATION;
679 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
683 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
684 ct->ad->ad_cname.bv_val, valx );
686 c->argv[0] = ct->ad->ad_cname.bv_val;
688 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
689 c->argv[c->argc] = c->line;
691 c->argv[c->argc] = NULL;
694 config_parse_ldif( c );
696 c->op = LDAP_MOD_ADD;
697 rc = config_add_vals( ct, c );
704 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
712 c = ch_calloc( 1, sizeof( ConfigArgs ) );
718 memcpy( c, cf, sizeof( ConfigArgs ) );
720 c->depth = depth; /* XXX */
727 init_config_argv( c );
729 if ( stat( fname, &s ) != 0 ) {
731 Debug(LDAP_DEBUG_ANY,
732 "could not stat config file \"%s\": %s (%d)\n",
733 fname, strerror(errno), errno);
739 if ( !S_ISREG( s.st_mode ) ) {
741 Debug(LDAP_DEBUG_ANY,
742 "regular file expected, got \"%s\"\n",
749 fp = fopen( fname, "r" );
752 Debug(LDAP_DEBUG_ANY,
753 "could not open config file \"%s\": %s (%d)\n",
754 fname, strerror(errno), errno);
760 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
766 while ( fp_getline( fp, c ) ) {
767 /* skip comments and blank lines */
768 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
772 snprintf( c->log, sizeof( c->log ), "%s: line %d",
773 c->fname, c->lineno );
777 if ( config_fp_parse_line( c ) ) {
783 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
789 c->op = SLAP_CONFIG_ADD;
791 ct = config_find_keyword( cft, c );
793 c->table = Cft_Global;
794 rc = config_add_vals( ct, c );
797 if ( rc & ARGS_USERLAND ) {
798 /* XXX a usertype would be opaque here */
799 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
800 c->log, c->argv[0], 0);
804 } else if ( rc == ARG_BAD_CONF ) {
809 } else if ( c->bi && !c->be ) {
810 rc = SLAP_CONF_UNKNOWN;
811 if ( c->bi->bi_cf_ocs ) {
812 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
814 c->table = c->bi->bi_cf_ocs->co_type;
815 rc = config_add_vals( ct, c );
818 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
819 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
824 case SLAP_CONF_UNKNOWN:
825 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
826 "<%s> inside backend info definition.\n",
827 c->log, *c->argv, 0);
834 } else if ( c->be && c->be != frontendDB ) {
835 rc = SLAP_CONF_UNKNOWN;
836 if ( c->be->be_cf_ocs ) {
837 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
839 c->table = c->be->be_cf_ocs->co_type;
840 rc = config_add_vals( ct, c );
843 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
844 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
847 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
849 /* global overlays may need
850 * definitions inside other databases...
852 rc = (*frontendDB->be_config)( frontendDB,
853 c->fname, (int)c->lineno, c->argc, c->argv );
860 case SLAP_CONF_UNKNOWN:
861 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
862 "<%s> inside backend database definition.\n",
863 c->log, *c->argv, 0);
870 } else if ( frontendDB->be_config ) {
871 rc = (*frontendDB->be_config)( frontendDB,
872 c->fname, (int)c->lineno, c->argc, c->argv);
875 case SLAP_CONF_UNKNOWN:
876 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
877 "<%s> inside global database definition.\n",
878 c->log, *c->argv, 0);
887 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
888 "<%s> outside backend info and database definitions.\n",
889 c->log, *c->argv, 0);
909 /* restrictops, allows, disallows, requires, loglevel */
912 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
914 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
915 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
921 verb_to_mask(const char *word, slap_verbmasks *v) {
923 ber_str2bv( word, 0, 0, &bword );
924 return bverb_to_mask( &bword, v );
928 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
930 for(i = 1; i < argc; i++) {
931 j = verb_to_mask(argv[i], v);
932 if(BER_BVISNULL(&v[j].word)) return i;
933 while (!v[j].mask) j--;
939 /* Mask keywords that represent multiple bits should occur before single
940 * bit keywords in the verbmasks array.
943 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
947 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
948 if (!v[i].mask) continue;
949 if (( m & v[i].mask ) == v[i].mask ) {
950 value_add_one( bva, &v[i].word );
960 /* Return the verbs as a single string, separated by delim */
962 mask_to_verbstring(slap_verbmasks *v, slap_mask_t m0, char delim, struct berval *bv)
970 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
971 if (!v[i].mask) continue;
972 if (( m & v[i].mask ) == v[i].mask ) {
973 bv->bv_len += v[i].word.bv_len + 1;
979 bv->bv_val = ch_malloc(bv->bv_len);
983 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
984 if (!v[i].mask) continue;
985 if (( m & v[i].mask ) == v[i].mask ) {
986 ptr = lutil_strcopy(ptr, v[i].word.bv_val);
997 /* Parse a verbstring */
999 verbstring_to_mask(slap_verbmasks *v, char *str, char delim, slap_mask_t *m) {
1006 d = strchr( str, delim );
1008 bv.bv_len = d - str;
1010 bv.bv_len = strlen( str );
1011 j = bverb_to_mask( &bv, v );
1012 if(BER_BVISNULL(&v[j].word)) return 1;
1013 while (!v[j].mask) j--;
1015 str += bv.bv_len + 1;
1021 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
1025 assert( *vp == NULL );
1027 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
1029 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
1031 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1032 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
1033 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
1036 BER_BVZERO( &(*vp)[ i ].word );
1042 slap_verbmasks_destroy( slap_verbmasks *v )
1046 assert( v != NULL );
1048 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1049 ch_free( v[ i ].word.bv_val );
1058 slap_verbmasks_append(
1059 slap_verbmasks **vp,
1062 slap_mask_t *ignore )
1067 return LDAP_OPERATIONS_ERROR;
1070 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
1071 if ( !(*vp)[ i ].mask ) continue;
1073 if ( ignore != NULL ) {
1076 for ( j = 0; ignore[ j ] != 0; j++ ) {
1077 if ( (*vp)[ i ].mask == ignore[ j ] ) {
1083 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
1084 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
1085 /* already set; ignore */
1086 return LDAP_SUCCESS;
1089 return LDAP_TYPE_OR_VALUE_EXISTS;
1092 if ( m & (*vp)[ i ].mask ) {
1094 return LDAP_CONSTRAINT_VIOLATION;
1099 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1100 ber_dupbv( &(*vp)[ i ].word, v );
1101 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1102 BER_BVZERO( &(*vp)[ i + 1 ].word );
1104 return LDAP_SUCCESS;
1108 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1111 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1112 if ( m == v[i].mask ) {
1122 /* register a new verbmask */
1124 slap_verbmask_register( slap_verbmasks *vm_, slap_verbmasks **vmp, struct berval *bv, int mask )
1126 slap_verbmasks *vm = *vmp;
1129 /* check for duplicate word */
1130 /* NOTE: we accept duplicate codes; the first occurrence will be used
1131 * when mapping from mask to verb */
1132 i = verb_to_mask( bv->bv_val, vm );
1133 if ( !BER_BVISNULL( &vm[ i ].word ) ) {
1137 for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
1141 /* first time: duplicate array */
1142 vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
1143 for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
1145 ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
1146 *((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
1150 vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
1153 ber_dupbv( &vm[ i ].word, bv );
1154 *((slap_mask_t*)&vm[ i ].mask) = mask;
1156 BER_BVZERO( &vm[ i+1 ].word );
1163 static slap_verbmasks slap_ldap_response_code_[] = {
1164 { BER_BVC("success"), LDAP_SUCCESS },
1166 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR },
1167 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR },
1168 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED },
1169 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED },
1170 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE },
1171 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE },
1173 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED },
1174 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED },
1175 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED },
1176 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED },
1177 #if 0 /* not LDAPv3 */
1178 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS },
1181 { BER_BVC("referral"), LDAP_REFERRAL },
1182 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED },
1183 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
1184 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED },
1185 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS },
1187 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE },
1188 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE },
1189 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING },
1190 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION },
1191 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS },
1192 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX },
1194 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT },
1195 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM },
1196 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX },
1197 #if 0 /* not LDAPv3 */
1198 { BER_BVC("isLeaf"), LDAP_IS_LEAF },
1200 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM },
1202 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE },
1203 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH },
1204 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS },
1205 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS },
1207 { BER_BVC("busy"), LDAP_BUSY },
1208 { BER_BVC("unavailable"), LDAP_UNAVAILABLE },
1209 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM },
1210 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT },
1212 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION },
1213 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION },
1214 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF },
1215 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN },
1216 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS },
1217 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS },
1218 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE },
1219 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS },
1221 { BER_BVC("other"), LDAP_OTHER },
1223 /* extension-specific */
1225 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED },
1226 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION },
1227 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA },
1228 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME },
1229 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED },
1231 { BER_BVC("cancelled"), LDAP_CANCELLED },
1232 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION },
1233 { BER_BVC("tooLate"), LDAP_TOO_LATE },
1234 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL },
1236 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED },
1238 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED },
1240 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED },
1242 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION },
1247 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_;
1250 slap_ldap_response_code_register( struct berval *bv, int err )
1252 return slap_verbmask_register( slap_ldap_response_code_,
1253 &slap_ldap_response_code, bv, err );
1257 static slap_verbmasks tlskey[] = {
1258 { BER_BVC("no"), SB_TLS_OFF },
1259 { BER_BVC("yes"), SB_TLS_ON },
1260 { BER_BVC("critical"), SB_TLS_CRITICAL },
1264 static slap_verbmasks crlkeys[] = {
1265 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE },
1266 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER },
1267 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL },
1271 static slap_verbmasks vfykeys[] = {
1272 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER },
1273 { BER_BVC("allow"), LDAP_OPT_X_TLS_ALLOW },
1274 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY },
1275 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND },
1276 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD },
1277 { BER_BVC("true"), LDAP_OPT_X_TLS_HARD },
1282 static slap_verbmasks methkey[] = {
1283 { BER_BVC("none"), LDAP_AUTH_NONE },
1284 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1285 #ifdef HAVE_CYRUS_SASL
1286 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1291 static slap_verbmasks versionkey[] = {
1292 { BER_BVC("2"), LDAP_VERSION2 },
1293 { BER_BVC("3"), LDAP_VERSION3 },
1298 slap_keepalive_parse(
1301 slap_cf_aux_table *tab0,
1306 slap_keepalive *sk = (slap_keepalive *)bc;
1307 int rc = snprintf( val->bv_val, val->bv_len, "%d:%d:%d",
1308 sk->sk_idle, sk->sk_probes, sk->sk_interval );
1313 if ( (unsigned)rc >= val->bv_len ) {
1320 char *s = val->bv_val;
1322 slap_keepalive *sk = (slap_keepalive *)bc;
1325 if ( s[0] == ':' ) {
1330 sk2.sk_idle = strtol( s, &next, 10 );
1331 if ( next == s || next[0] != ':' ) {
1335 if ( sk2.sk_idle < 0 ) {
1342 if ( s[0] == ':' ) {
1347 sk2.sk_probes = strtol( s, &next, 10 );
1348 if ( next == s || next[0] != ':' ) {
1352 if ( sk2.sk_probes < 0 ) {
1360 sk2.sk_interval = 0;
1363 sk2.sk_interval = strtol( s, &next, 10 );
1364 if ( next == s || next[0] != '\0' ) {
1368 if ( sk2.sk_interval < 0 ) {
1375 ber_memfree( val->bv_val );
1386 slap_cf_aux_table *tab0,
1390 slap_bindconf *bc = bcp;
1392 if ( bc->sb_uri.bv_len >= val->bv_len )
1394 val->bv_len = bc->sb_uri.bv_len;
1395 AC_MEMCPY( val->bv_val, bc->sb_uri.bv_val, val->bv_len );
1399 if ( ldap_is_ldaps_url( val->bv_val ))
1400 bc->sb_tls_do_init = 1;
1406 static slap_cf_aux_table bindkey[] = {
1407 { BER_BVC("uri="), 0, 'x', 1, slap_sb_uri },
1408 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1409 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1410 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1411 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1412 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1413 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1414 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1415 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1416 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1417 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1418 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1419 { BER_BVC("keepalive="), offsetof(slap_bindconf, sb_keepalive), 'x', 0, (slap_verbmasks *)slap_keepalive_parse },
1421 /* NOTE: replace "13" with the actual index
1422 * of the first TLS-related line */
1423 #define aux_TLS (bindkey+13) /* beginning of TLS keywords */
1425 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1426 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1427 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1428 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1429 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1430 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 0, NULL },
1431 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 0, NULL },
1432 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 0, NULL },
1433 #ifdef HAVE_OPENSSL_CRL
1434 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 0, NULL },
1437 { BER_BVNULL, 0, 0, 0, NULL }
1442 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1443 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1446 * 'U': unsigned long
1450 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1452 int rc = SLAP_CONF_UNKNOWN;
1453 slap_cf_aux_table *tab;
1455 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1456 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1461 unsigned long *ulptr;
1462 struct berval *bptr;
1463 const char *val = word + tab->key.bv_len;
1465 switch ( tab->type ) {
1467 cptr = (char **)((char *)dst + tab->off);
1468 *cptr = ch_strdup( val );
1473 bptr = (struct berval *)((char *)dst + tab->off);
1474 if ( tab->aux != NULL ) {
1476 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1478 ber_str2bv( val, 0, 0, &dn );
1479 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1482 ber_str2bv( val, 0, 1, bptr );
1488 iptr = (int *)((char *)dst + tab->off);
1490 if ( tab->aux != NULL ) {
1491 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1493 assert( aux != NULL );
1496 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1497 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1498 *iptr = aux[j].mask;
1505 rc = lutil_atoix( iptr, val, 0 );
1510 uptr = (unsigned *)((char *)dst + tab->off);
1512 rc = lutil_atoux( uptr, val, 0 );
1516 lptr = (long *)((char *)dst + tab->off);
1518 rc = lutil_atolx( lptr, val, 0 );
1522 ulptr = (unsigned long *)((char *)dst + tab->off);
1524 rc = lutil_atoulx( ulptr, val, 0 );
1528 if ( tab->aux != NULL ) {
1529 struct berval value;
1530 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1532 ber_str2bv( val, 0, 1, &value );
1534 rc = func( &value, (void *)((char *)dst + tab->off), tab, tabmsg, 0 );
1543 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1555 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1557 char buf[AC_LINE_MAX], *ptr;
1558 slap_cf_aux_table *tab;
1562 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1567 unsigned long *ulptr;
1568 struct berval *bptr;
1570 cptr = (char **)((char *)src + tab->off);
1572 switch ( tab->type ) {
1574 bptr = (struct berval *)((char *)src + tab->off);
1575 cptr = &bptr->bv_val;
1580 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1581 if ( tab->quote ) *ptr++ = '"';
1582 ptr = lutil_strcopy( ptr, *cptr );
1583 if ( tab->quote ) *ptr++ = '"';
1588 iptr = (int *)((char *)src + tab->off);
1590 if ( tab->aux != NULL ) {
1591 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1593 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1594 if ( *iptr == aux[i].mask ) {
1596 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1597 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1604 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1605 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1610 uptr = (unsigned *)((char *)src + tab->off);
1612 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1613 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1617 lptr = (long *)((char *)src + tab->off);
1619 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1620 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1624 ulptr = (unsigned long *)((char *)src + tab->off);
1626 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1627 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1634 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1635 if ( tab->quote ) *ptr++ = '"';
1636 if ( tab->aux != NULL ) {
1637 struct berval value;
1638 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1642 value.bv_len = buf + sizeof( buf ) - ptr;
1644 rc = func( &value, (void *)((char *)src + tab->off), tab, "(unparse)", 1 );
1647 ptr += value.bv_len;
1654 if ( tab->quote ) *ptr++ = '"';
1663 tmp.bv_len = ptr - buf;
1664 ber_dupbv( bv, &tmp );
1669 slap_tls_get_config( LDAP *ld, int opt, char **val )
1672 slap_verbmasks *keys;
1677 case LDAP_OPT_X_TLS_CRLCHECK:
1680 case LDAP_OPT_X_TLS_REQUIRE_CERT:
1683 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
1685 ldap_pvt_tls_get_option( ld, opt, &ival );
1686 snprintf( buf, sizeof( buf ), "%d.%d",
1687 ( ival >> 8 ) & 0xff, ival & 0xff );
1688 *val = ch_strdup( buf );
1694 ldap_pvt_tls_get_option( ld, opt, &ival );
1695 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1696 if (keys[i].mask == ival) {
1697 *val = ch_strdup( keys[i].word.bv_val );
1706 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1709 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1710 bc->sb_tls_do_init = 1;
1718 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1721 return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1727 bindconf_parse( const char *word, slap_bindconf *bc )
1730 /* Detect TLS config changes explicitly */
1731 if ( bindconf_tls_parse( word, bc ) == 0 ) {
1735 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1739 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1741 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1744 void bindconf_free( slap_bindconf *bc ) {
1745 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1746 ch_free( bc->sb_uri.bv_val );
1747 BER_BVZERO( &bc->sb_uri );
1749 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1750 ch_free( bc->sb_binddn.bv_val );
1751 BER_BVZERO( &bc->sb_binddn );
1753 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1754 ch_free( bc->sb_cred.bv_val );
1755 BER_BVZERO( &bc->sb_cred );
1757 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1758 ch_free( bc->sb_saslmech.bv_val );
1759 BER_BVZERO( &bc->sb_saslmech );
1761 if ( bc->sb_secprops ) {
1762 ch_free( bc->sb_secprops );
1763 bc->sb_secprops = NULL;
1765 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1766 ch_free( bc->sb_realm.bv_val );
1767 BER_BVZERO( &bc->sb_realm );
1769 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1770 ch_free( bc->sb_authcId.bv_val );
1771 BER_BVZERO( &bc->sb_authcId );
1773 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1774 ch_free( bc->sb_authzId.bv_val );
1775 BER_BVZERO( &bc->sb_authzId );
1778 if ( bc->sb_tls_cert ) {
1779 ch_free( bc->sb_tls_cert );
1780 bc->sb_tls_cert = NULL;
1782 if ( bc->sb_tls_key ) {
1783 ch_free( bc->sb_tls_key );
1784 bc->sb_tls_key = NULL;
1786 if ( bc->sb_tls_cacert ) {
1787 ch_free( bc->sb_tls_cacert );
1788 bc->sb_tls_cacert = NULL;
1790 if ( bc->sb_tls_cacertdir ) {
1791 ch_free( bc->sb_tls_cacertdir );
1792 bc->sb_tls_cacertdir = NULL;
1794 if ( bc->sb_tls_reqcert ) {
1795 ch_free( bc->sb_tls_reqcert );
1796 bc->sb_tls_reqcert = NULL;
1798 if ( bc->sb_tls_cipher_suite ) {
1799 ch_free( bc->sb_tls_cipher_suite );
1800 bc->sb_tls_cipher_suite = NULL;
1802 if ( bc->sb_tls_protocol_min ) {
1803 ch_free( bc->sb_tls_protocol_min );
1804 bc->sb_tls_protocol_min = NULL;
1806 #ifdef HAVE_OPENSSL_CRL
1807 if ( bc->sb_tls_crlcheck ) {
1808 ch_free( bc->sb_tls_crlcheck );
1809 bc->sb_tls_crlcheck = NULL;
1812 if ( bc->sb_tls_ctx ) {
1813 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1814 bc->sb_tls_ctx = NULL;
1820 bindconf_tls_defaults( slap_bindconf *bc )
1823 if ( bc->sb_tls_do_init ) {
1824 if ( !bc->sb_tls_cacert )
1825 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1826 &bc->sb_tls_cacert );
1827 if ( !bc->sb_tls_cacertdir )
1828 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1829 &bc->sb_tls_cacertdir );
1830 if ( !bc->sb_tls_cert )
1831 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1833 if ( !bc->sb_tls_key )
1834 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1836 if ( !bc->sb_tls_cipher_suite )
1837 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1838 &bc->sb_tls_cipher_suite );
1839 if ( !bc->sb_tls_reqcert )
1840 bc->sb_tls_reqcert = ch_strdup("demand");
1841 #ifdef HAVE_OPENSSL_CRL
1842 if ( !bc->sb_tls_crlcheck )
1843 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1844 &bc->sb_tls_crlcheck );
1856 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1857 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1858 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1859 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1860 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1861 { "tls_protocol_min", offsetof(slap_bindconf, sb_tls_protocol_min), LDAP_OPT_X_TLS_PROTOCOL_MIN },
1865 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1867 int i, rc, newctx = 0, res = 0;
1868 char *ptr = (char *)bc, **word;
1870 bc->sb_tls_do_init = 0;
1872 for (i=0; bindtlsopts[i].opt; i++) {
1873 word = (char **)(ptr + bindtlsopts[i].offset);
1875 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1877 Debug( LDAP_DEBUG_ANY,
1878 "bindconf_tls_set: failed to set %s to %s\n",
1879 bindtlsopts[i].key, *word, 0 );
1885 if ( bc->sb_tls_reqcert ) {
1886 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1887 bc->sb_tls_reqcert );
1889 Debug( LDAP_DEBUG_ANY,
1890 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1891 bc->sb_tls_reqcert, 0, 0 );
1896 if ( bc->sb_tls_protocol_min ) {
1897 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN,
1898 bc->sb_tls_protocol_min );
1900 Debug( LDAP_DEBUG_ANY,
1901 "bindconf_tls_set: failed to set tls_protocol_min to %s\n",
1902 bc->sb_tls_protocol_min, 0, 0 );
1907 #ifdef HAVE_OPENSSL_CRL
1908 if ( bc->sb_tls_crlcheck ) {
1909 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1910 bc->sb_tls_crlcheck );
1912 Debug( LDAP_DEBUG_ANY,
1913 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1914 bc->sb_tls_crlcheck, 0, 0 );
1923 if ( bc->sb_tls_ctx ) {
1924 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1925 bc->sb_tls_ctx = NULL;
1927 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1931 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1939 * set connection keepalive options
1942 slap_client_keepalive(LDAP *ld, slap_keepalive *sk)
1946 if ( sk->sk_idle ) {
1947 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_IDLE, &sk->sk_idle );
1950 if ( sk->sk_probes ) {
1951 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_PROBES, &sk->sk_probes );
1954 if ( sk->sk_interval ) {
1955 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_INTERVAL, &sk->sk_interval );
1962 * connect to a client using the bindconf data
1963 * note: should move "version" into bindconf...
1966 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1972 /* Init connection to master */
1973 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1974 if ( rc != LDAP_SUCCESS ) {
1975 Debug( LDAP_DEBUG_ANY,
1976 "slap_client_connect: "
1977 "ldap_initialize(%s) failed (%d)\n",
1978 sb->sb_uri.bv_val, rc, 0 );
1982 if ( sb->sb_version != 0 ) {
1983 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1984 (const void *)&sb->sb_version );
1987 if ( sb->sb_timeout_api ) {
1988 tv.tv_sec = sb->sb_timeout_api;
1990 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1993 if ( sb->sb_timeout_net ) {
1994 tv.tv_sec = sb->sb_timeout_net;
1996 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1999 /* setting network keepalive options */
2000 slap_client_keepalive(ld, &sb->sb_keepalive);
2003 if ( sb->sb_tls_do_init ) {
2004 rc = bindconf_tls_set( sb, ld );
2006 } else if ( sb->sb_tls_ctx ) {
2007 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
2012 Debug( LDAP_DEBUG_ANY,
2013 "slap_client_connect: "
2014 "URI=%s TLS context initialization failed (%d)\n",
2015 sb->sb_uri.bv_val, rc, 0 );
2022 rc = ldap_start_tls_s( ld, NULL, NULL );
2023 if ( rc != LDAP_SUCCESS ) {
2024 Debug( LDAP_DEBUG_ANY,
2025 "slap_client_connect: URI=%s "
2026 "%s, ldap_start_tls failed (%d)\n",
2028 sb->sb_tls == SB_TLS_CRITICAL ?
2029 "Error" : "Warning",
2031 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
2037 if ( sb->sb_method == LDAP_AUTH_SASL ) {
2038 #ifdef HAVE_CYRUS_SASL
2041 if ( sb->sb_secprops != NULL ) {
2042 rc = ldap_set_option( ld,
2043 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
2045 if( rc != LDAP_OPT_SUCCESS ) {
2046 Debug( LDAP_DEBUG_ANY,
2047 "slap_client_connect: "
2048 "error, ldap_set_option "
2049 "(%s,SECPROPS,\"%s\") failed!\n",
2050 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
2055 defaults = lutil_sasl_defaults( ld,
2056 sb->sb_saslmech.bv_val,
2057 sb->sb_realm.bv_val,
2058 sb->sb_authcId.bv_val,
2060 sb->sb_authzId.bv_val );
2061 if ( defaults == NULL ) {
2066 rc = ldap_sasl_interactive_bind_s( ld,
2067 sb->sb_binddn.bv_val,
2068 sb->sb_saslmech.bv_val,
2071 lutil_sasl_interact,
2074 lutil_sasl_freedefs( defaults );
2076 /* FIXME: different error behaviors according to
2078 * 2) on err policy : exit, retry, backoff ...
2080 if ( rc != LDAP_SUCCESS ) {
2081 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
2083 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
2084 "ldap_sasl_interactive_bind_s failed (%d)\n",
2085 sb->sb_uri.bv_val, rc, 0 );
2087 /* FIXME (see above comment) */
2088 /* if Kerberos credentials cache is not active, retry */
2089 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
2090 rc == LDAP_LOCAL_ERROR )
2092 rc = LDAP_SERVER_DOWN;
2097 #else /* HAVE_CYRUS_SASL */
2098 /* Should never get here, we trapped this at config time */
2100 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
2105 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
2106 rc = ldap_sasl_bind_s( ld,
2107 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
2108 &sb->sb_cred, NULL, NULL, NULL );
2109 if ( rc != LDAP_SUCCESS ) {
2110 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
2112 "ldap_sasl_bind_s failed (%d)\n",
2113 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
2121 ldap_unbind_ext( ld, NULL, NULL );
2132 /* -------------------------------------- */
2136 strtok_quote( char *line, char *sep, char **quote_ptr, int *iqp )
2143 if ( line != NULL ) {
2146 while ( *next && strchr( sep, *next ) ) {
2150 if ( *next == '\0' ) {
2156 for ( inquote = 0; *next; ) {
2164 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2170 next + 1, strlen( next + 1 ) + 1 );
2171 next++; /* dont parse the escaped character */
2176 if ( strchr( sep, *next ) != NULL ) {
2191 static char buf[AC_LINE_MAX];
2193 static size_t lmax, lcur;
2195 #define CATLINE( buf ) \
2197 size_t len = strlen( buf ); \
2198 while ( lcur + len + 1 > lmax ) { \
2199 lmax += AC_LINE_MAX; \
2200 line = (char *) ch_realloc( line, lmax ); \
2202 strcpy( line + lcur, buf ); \
2207 fp_getline_init(ConfigArgs *c) {
2213 fp_getline( FILE *fp, ConfigArgs *c )
2221 /* avoid stack of bufs */
2222 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
2228 while ( fgets( buf, sizeof( buf ), fp ) ) {
2229 p = strchr( buf, '\n' );
2231 if ( p > buf && p[-1] == '\r' ) {
2239 && ( p = line + strlen( line ) - 1 )[0] == '\\'
2246 if ( !isspace( (unsigned char)buf[0] ) ) {
2257 return(line[0] ? 1 : 0);
2261 config_fp_parse_line(ConfigArgs *c)
2264 static char *const hide[] = {
2265 "rootpw", "replica", "syncrepl", /* in slapd */
2266 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
2267 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
2268 "pseudorootpw", /* in back-meta */
2269 "dbpasswd", /* in back-sql */
2272 static char *const raw[] = {
2273 "attributetype", "objectclass", "ditcontentrule", "ldapsyntax", NULL };
2275 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
2278 c->tline = ch_strdup(c->line);
2279 token = strtok_quote(c->tline, " \t", "e_ptr, &inquote);
2281 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2282 if(quote_ptr) *quote_ptr = ' ';
2283 Debug(LDAP_DEBUG_CONFIG, "%s (%s%s)\n", c->log,
2284 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2285 if(quote_ptr) *quote_ptr = '\0';
2287 for(;; token = strtok_quote(NULL, " \t", "e_ptr, &inquote)) {
2288 if(c->argc >= c->argv_size) {
2290 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2292 Debug(LDAP_DEBUG_ANY, "%s: out of memory\n", c->log, 0, 0);
2296 c->argv_size += ARGS_STEP;
2300 c->argv[c->argc++] = token;
2302 c->argv[c->argc] = NULL;
2304 /* these directives parse c->line independently of argv tokenizing */
2305 for(i = 0; raw[i]; i++) if (!strcasecmp(c->argv[0], raw[i])) return 0;
2307 Debug(LDAP_DEBUG_ANY, "%s: unterminated quoted string \"%s\"\n", c->log, c->argv[c->argc-1], 0);
2316 ucdata_unload( UCDATA_ALL );
2318 /* NOTE: in case of early exit, frontendDB can be NULL */
2319 if ( frontendDB->be_schemandn.bv_val )
2320 free( frontendDB->be_schemandn.bv_val );
2321 if ( frontendDB->be_schemadn.bv_val )
2322 free( frontendDB->be_schemadn.bv_val );
2323 if ( frontendDB->be_acl )
2324 acl_destroy( frontendDB->be_acl );
2327 if ( slapd_args_file )
2328 free ( slapd_args_file );
2329 if ( slapd_pid_file )
2330 free ( slapd_pid_file );
2331 if ( default_passwd_hash )
2332 ldap_charray_free( default_passwd_hash );
2336 slap_str2clist( char ***out, char *in, const char *brkstr )
2344 /* find last element in list */
2345 for (i = 0; *out && (*out)[i]; i++);
2347 /* protect the input string from strtok */
2348 str = ch_strdup( in );
2350 if ( *str == '\0' ) {
2355 /* Count words in string */
2357 for ( s = str; *s; s++ ) {
2358 if ( strchr( brkstr, *s ) != NULL ) {
2363 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2365 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2367 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2369 *new = ch_strdup( s );
2378 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
2379 int argc, char **argv )
2381 ConfigArgs c = { 0 };
2392 c.op = SLAP_CONFIG_ADD;
2393 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
2395 rc = SLAP_CONF_UNKNOWN;
2396 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
2398 c.table = be->be_cf_ocs->co_type;
2399 rc = config_add_vals( ct, &c );
2404 /* See if the given URL (in plain and parsed form) matches
2405 * any of the server's listener addresses. Return matching
2406 * Listener or NULL for no match.
2408 Listener *config_check_my_url( const char *url, LDAPURLDesc *lud )
2410 Listener **l = slapd_get_listeners();
2413 /* Try a straight compare with Listener strings */
2414 for ( i=0; l && l[i]; i++ ) {
2415 if ( !strcasecmp( url, l[i]->sl_url.bv_val )) {
2421 /* If hostname is empty, or is localhost, or matches
2422 * our hostname, this url refers to this host.
2423 * Compare it against listeners and ports.
2425 if ( !lud->lud_host || !lud->lud_host[0] ||
2426 !strncasecmp("localhost", lud->lud_host,
2427 STRLENOF("localhost")) ||
2428 !strcasecmp( global_host, lud->lud_host )) {
2430 for ( i=0; l && l[i]; i++ ) {
2432 ldap_url_parse( l[i]->sl_url.bv_val, &lu2 );
2434 if ( strcasecmp( lud->lud_scheme,
2437 if ( lud->lud_port != lu2->lud_port )
2439 /* Listener on ANY address */
2440 if ( !lu2->lud_host || !lu2->lud_host[0] ) {
2444 /* URL on ANY address */
2445 if ( !lud->lud_host || !lud->lud_host[0] ) {
2449 /* Listener has specific host, must
2452 if ( !strcasecmp( lud->lud_host,
2458 ldap_free_urldesc( lu2 );