1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/string.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
36 #include <ac/unistd.h>
38 #include <sys/types.h>
42 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
47 #include "slapi/slapi.h"
50 #include "lutil_ldap.h"
56 * defaults for various global variables
58 slap_mask_t global_allows = 0;
59 slap_mask_t global_disallows = 0;
60 int global_gentlehup = 0;
61 int global_idletimeout = 0;
62 int global_writetimeout = 0;
63 char *global_host = NULL;
64 struct berval global_host_bv = BER_BVNULL;
65 char *global_realm = NULL;
66 char *sasl_host = NULL;
67 char **default_passwd_hash = NULL;
68 struct berval default_search_base = BER_BVNULL;
69 struct berval default_search_nbase = BER_BVNULL;
71 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
72 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
74 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
75 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
77 char *slapd_pid_file = NULL;
78 char *slapd_args_file = NULL;
80 int use_reverse_lookup = 0;
83 int slapi_plugins_used = 0;
86 static int fp_getline(FILE *fp, ConfigArgs *c);
87 static void fp_getline_init(ConfigArgs *c);
89 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
90 static char *strtok_quote_ldif(char **line);
93 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
96 c = ch_calloc( 1, sizeof( ConfigArgs ) );
97 if ( c == NULL ) return(NULL);
103 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
108 init_config_argv( ConfigArgs *c )
110 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
111 c->argv_size = ARGS_STEP + 1;
114 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
117 for(i = 0; Conf[i].name; i++)
118 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
119 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
120 if ( !Conf[i].name ) return NULL;
124 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
125 int rc, arg_user, arg_type, arg_syn, iarg;
131 if(Conf->arg_type == ARG_IGNORED) {
132 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
133 c->log, Conf->name, 0);
136 arg_type = Conf->arg_type & ARGS_TYPES;
137 arg_user = Conf->arg_type & ARGS_USERLAND;
138 arg_syn = Conf->arg_type & ARGS_SYNTAX;
140 if((arg_type == ARG_DN) && c->argc == 1) {
144 if(Conf->min_args && (c->argc < Conf->min_args)) {
145 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
146 c->argv[0], Conf->what ? Conf->what : "" );
147 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
148 return(ARG_BAD_CONF);
150 if(Conf->max_args && (c->argc > Conf->max_args)) {
151 char *ignored = " ignored";
153 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
154 c->argv[0], Conf->what );
157 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
158 c->log, c->cr_msg, ignored );
159 return(ARG_BAD_CONF);
161 if((arg_syn & ARG_DB) && !c->be) {
162 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
164 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
165 c->log, c->cr_msg, 0);
166 return(ARG_BAD_CONF);
168 if((arg_syn & ARG_PRE_BI) && c->bi) {
169 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
170 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
171 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
172 c->log, c->cr_msg, 0 );
173 return(ARG_BAD_CONF);
175 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
176 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
178 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
179 c->log, c->cr_msg, 0);
180 return(ARG_BAD_CONF);
182 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
183 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
184 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
185 c->log, c->cr_msg, 0);
186 return(ARG_BAD_CONF);
188 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
189 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
191 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
192 c->log, c->cr_msg, 0);
193 return(ARG_BAD_CONF);
196 memset(&c->values, 0, sizeof(c->values));
197 if(arg_type == ARG_STRING) {
198 assert( c->argc == 2 );
200 c->value_string = ch_strdup(c->argv[1]);
201 } else if(arg_type == ARG_BERVAL) {
202 assert( c->argc == 2 );
204 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
205 } else if(arg_type == ARG_DN) {
207 assert( c->argc == 2 );
208 ber_str2bv( c->argv[1], 0, 0, &bv );
209 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
210 if ( rc != LDAP_SUCCESS ) {
211 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
212 c->argv[0], rc, ldap_err2string( rc ));
213 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
214 return(ARG_BAD_CONF);
217 ch_free( c->value_ndn.bv_val );
218 ch_free( c->value_dn.bv_val );
220 } else if(arg_type == ARG_ATDESC) {
221 const char *text = NULL;
222 assert( c->argc == 2 );
224 rc = slap_str2ad( c->argv[1], &c->value_ad, &text );
225 if ( rc != LDAP_SUCCESS ) {
226 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid AttributeDescription %d (%s)",
227 c->argv[0], rc, text );
228 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
229 return(ARG_BAD_CONF);
231 } else { /* all numeric */
233 iarg = 0; larg = 0; barg = 0;
236 assert( c->argc == 2 );
237 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
238 snprintf( c->cr_msg, sizeof( c->cr_msg ),
239 "<%s> unable to parse \"%s\" as int",
240 c->argv[0], c->argv[1] );
241 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
242 c->log, c->cr_msg, 0);
243 return(ARG_BAD_CONF);
247 assert( c->argc == 2 );
248 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
249 snprintf( c->cr_msg, sizeof( c->cr_msg ),
250 "<%s> unable to parse \"%s\" as unsigned int",
251 c->argv[0], c->argv[1] );
252 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
253 c->log, c->cr_msg, 0);
254 return(ARG_BAD_CONF);
258 assert( c->argc == 2 );
259 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
260 snprintf( c->cr_msg, sizeof( c->cr_msg ),
261 "<%s> unable to parse \"%s\" as long",
262 c->argv[0], c->argv[1] );
263 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
264 c->log, c->cr_msg, 0);
265 return(ARG_BAD_CONF);
269 assert( c->argc == 2 );
270 if ( lutil_atoulx( &ularg, c->argv[1], 0 ) != 0 ) {
271 snprintf( c->cr_msg, sizeof( c->cr_msg ),
272 "<%s> unable to parse \"%s\" as unsigned long",
273 c->argv[0], c->argv[1] );
274 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
275 c->log, c->cr_msg, 0);
276 return(ARG_BAD_CONF);
279 case ARG_BER_LEN_T: {
281 assert( c->argc == 2 );
282 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
283 snprintf( c->cr_msg, sizeof( c->cr_msg ),
284 "<%s> unable to parse \"%s\" as ber_len_t",
285 c->argv[0], c->argv[1] );
286 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
287 c->log, c->cr_msg, 0);
288 return(ARG_BAD_CONF);
293 /* note: this is an explicit exception
294 * to the "need exactly 2 args" rule */
297 } else if ( !strcasecmp(c->argv[1], "on") ||
298 !strcasecmp(c->argv[1], "true") ||
299 !strcasecmp(c->argv[1], "yes") )
302 } else if ( !strcasecmp(c->argv[1], "off") ||
303 !strcasecmp(c->argv[1], "false") ||
304 !strcasecmp(c->argv[1], "no") )
308 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
310 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
311 c->log, c->cr_msg, 0 );
312 return(ARG_BAD_CONF);
316 j = (arg_type & ARG_NONZERO) ? 1 : 0;
317 if(iarg < j && larg < j && barg < (unsigned)j ) {
318 larg = larg ? larg : (barg ? (long)barg : iarg);
319 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
321 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
322 c->log, c->cr_msg, 0 );
323 return(ARG_BAD_CONF);
327 case ARG_INT: c->value_int = iarg; break;
328 case ARG_UINT: c->value_uint = uiarg; break;
329 case ARG_LONG: c->value_long = larg; break;
330 case ARG_ULONG: c->value_ulong = ularg; break;
331 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
337 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
341 arg_type = Conf->arg_type;
342 if(arg_type & ARG_MAGIC) {
343 if(!c->be) c->be = frontendDB;
345 rc = (*((ConfigDriver*)Conf->arg_item))(c);
347 if(c->be == frontendDB) c->be = NULL;
350 if ( !c->cr_msg[0] ) {
351 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
353 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
354 c->log, c->cr_msg, 0 );
356 return(ARG_BAD_CONF);
360 if(arg_type & ARG_OFFSET) {
361 if (c->be && c->table == Cft_Database)
362 ptr = c->be->be_private;
364 ptr = c->bi->bi_private;
366 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
368 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
369 c->log, c->cr_msg, 0);
370 return(ARG_BAD_CONF);
372 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
373 } else if (arg_type & ARGS_TYPES) {
374 ptr = Conf->arg_item;
376 if(arg_type & ARGS_TYPES)
377 switch(arg_type & ARGS_TYPES) {
379 case ARG_INT: *(int*)ptr = c->value_int; break;
380 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break;
381 case ARG_LONG: *(long*)ptr = c->value_long; break;
382 case ARG_ULONG: *(unsigned long*)ptr = c->value_ulong; break;
383 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
385 char *cc = *(char**)ptr;
387 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
388 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
389 c->log, Conf->name, 0 );
390 return(ARG_BAD_CONF);
394 *(char **)ptr = c->value_string;
398 *(struct berval *)ptr = c->value_bv;
401 *(AttributeDescription **)ptr = c->value_ad;
407 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
410 arg_type = Conf->arg_type;
411 if(arg_type == ARG_IGNORED) {
412 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
413 c->log, Conf->name, 0);
416 rc = config_check_vals( Conf, c, 0 );
418 return config_set_vals( Conf, c );
422 config_del_vals(ConfigTable *cf, ConfigArgs *c)
426 /* If there is no handler, just ignore it */
427 if ( cf->arg_type & ARG_MAGIC ) {
428 c->argv[0] = cf->ad->ad_cname.bv_val;
429 c->op = LDAP_MOD_DELETE;
430 c->type = cf->arg_type & ARGS_USERLAND;
431 rc = (*((ConfigDriver*)cf->arg_item))(c);
437 config_get_vals(ConfigTable *cf, ConfigArgs *c)
443 if ( cf->arg_type & ARG_IGNORED ) {
447 memset(&c->values, 0, sizeof(c->values));
448 c->rvalue_vals = NULL;
449 c->rvalue_nvals = NULL;
450 c->op = SLAP_CONFIG_EMIT;
451 c->type = cf->arg_type & ARGS_USERLAND;
453 if ( cf->arg_type & ARG_MAGIC ) {
454 rc = (*((ConfigDriver*)cf->arg_item))(c);
457 if ( cf->arg_type & ARG_OFFSET ) {
458 if (c->be && c->table == Cft_Database)
459 ptr = c->be->be_private;
461 ptr = c->bi->bi_private;
464 ptr = (void *)((char *)ptr + (long)cf->arg_item);
469 switch(cf->arg_type & ARGS_TYPES) {
471 case ARG_INT: c->value_int = *(int *)ptr; break;
472 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break;
473 case ARG_LONG: c->value_long = *(long *)ptr; break;
474 case ARG_ULONG: c->value_ulong = *(unsigned long *)ptr; break;
475 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
478 c->value_string = ch_strdup(*(char **)ptr);
481 c->value_bv = *((struct berval *)ptr); break;
483 c->value_ad = *(AttributeDescription **)ptr; break;
486 if ( cf->arg_type & ARGS_TYPES) {
489 switch(cf->arg_type & ARGS_TYPES) {
490 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
491 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
492 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
493 case ARG_ULONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%lu", c->value_ulong); break;
494 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
495 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
496 c->value_int ? "TRUE" : "FALSE"); break;
498 if ( c->value_string && c->value_string[0]) {
499 ber_str2bv( c->value_string, 0, 0, &bv);
505 if ( !BER_BVISEMPTY( &c->value_bv )) {
513 bv = c->value_ad->ad_cname;
522 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
525 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
526 ber_bvarray_add(&c->rvalue_vals, &bv);
527 } else if ( !BER_BVISNULL( &bv ) ) {
528 value_add_one(&c->rvalue_vals, &bv);
530 /* else: maybe c->rvalue_vals already set? */
536 init_config_attrs(ConfigTable *ct) {
539 for (i=0; ct[i].name; i++ ) {
540 if ( !ct[i].attribute ) continue;
541 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
543 fprintf( stderr, "init_config_attrs: register_at failed\n" );
552 init_config_ocs( ConfigOCs *ocs ) {
555 for (i=0;ocs[i].co_def;i++) {
556 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
558 fprintf( stderr, "init_config_ocs: register_oc failed\n" );
565 /* Split an LDIF line into space-separated tokens. Words may be grouped
566 * by quotes. A quoted string may begin in the middle of a word, but must
567 * end at the end of the word (be followed by whitespace or EOS). Any other
568 * quotes are passed through unchanged. All other characters are passed
572 strtok_quote_ldif( char **line )
574 char *beg, *ptr, *quote=NULL;
582 while( isspace( (unsigned char) *ptr )) ptr++;
593 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
603 if ( isspace( (unsigned char) *ptr )) {
609 while ( quote < ptr ) {
617 while ( isspace( (unsigned char) *ptr )) ptr++;
624 config_parse_ldif( ConfigArgs *c )
627 c->tline = ch_strdup(c->line);
630 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
632 if ( c->argc >= c->argv_size ) {
633 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
636 c->argv_size += ARGS_STEP;
639 c->argv[c->argc] = NULL;
643 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
647 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
648 ct->ad->ad_cname.bv_val, valx );
650 c->argv[0] = ct->ad->ad_cname.bv_val;
652 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
653 c->argv[c->argc] = c->line;
655 c->argv[c->argc] = NULL;
658 config_parse_ldif( c );
660 rc = config_check_vals( ct, c, 1 );
665 rc = LDAP_CONSTRAINT_VIOLATION;
671 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
675 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
676 ct->ad->ad_cname.bv_val, valx );
678 c->argv[0] = ct->ad->ad_cname.bv_val;
680 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
681 c->argv[c->argc] = c->line;
683 c->argv[c->argc] = NULL;
686 config_parse_ldif( c );
688 c->op = LDAP_MOD_ADD;
689 rc = config_add_vals( ct, c );
696 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
704 c = ch_calloc( 1, sizeof( ConfigArgs ) );
710 memcpy( c, cf, sizeof( ConfigArgs ) );
712 c->depth = depth; /* XXX */
719 init_config_argv( c );
721 if ( stat( fname, &s ) != 0 ) {
723 Debug(LDAP_DEBUG_ANY,
724 "could not stat config file \"%s\": %s (%d)\n",
725 fname, strerror(errno), errno);
730 if ( !S_ISREG( s.st_mode ) ) {
732 Debug(LDAP_DEBUG_ANY,
733 "regular file expected, got \"%s\"\n",
739 fp = fopen( fname, "r" );
742 Debug(LDAP_DEBUG_ANY,
743 "could not open config file \"%s\": %s (%d)\n",
744 fname, strerror(errno), errno);
749 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
755 while ( fp_getline( fp, c ) ) {
756 /* skip comments and blank lines */
757 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
761 snprintf( c->log, sizeof( c->log ), "%s: line %d",
762 c->fname, c->lineno );
766 if ( config_fp_parse_line( c ) ) {
772 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
778 c->op = SLAP_CONFIG_ADD;
780 ct = config_find_keyword( cft, c );
782 c->table = Cft_Global;
783 rc = config_add_vals( ct, c );
786 if ( rc & ARGS_USERLAND ) {
787 /* XXX a usertype would be opaque here */
788 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
789 c->log, c->argv[0], 0);
793 } else if ( rc == ARG_BAD_CONF ) {
798 } else if ( c->bi && !c->be ) {
799 rc = SLAP_CONF_UNKNOWN;
800 if ( c->bi->bi_cf_ocs ) {
801 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
803 c->table = c->bi->bi_cf_ocs->co_type;
804 rc = config_add_vals( ct, c );
807 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
808 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
813 case SLAP_CONF_UNKNOWN:
814 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
815 "<%s> inside backend info definition.\n",
816 c->log, *c->argv, 0);
823 } else if ( c->be && c->be != frontendDB ) {
824 rc = SLAP_CONF_UNKNOWN;
825 if ( c->be->be_cf_ocs ) {
826 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
828 c->table = c->be->be_cf_ocs->co_type;
829 rc = config_add_vals( ct, c );
832 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
833 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
836 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
838 /* global overlays may need
839 * definitions inside other databases...
841 rc = (*frontendDB->be_config)( frontendDB,
842 c->fname, (int)c->lineno, c->argc, c->argv );
849 case SLAP_CONF_UNKNOWN:
850 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
851 "<%s> inside backend database definition.\n",
852 c->log, *c->argv, 0);
859 } else if ( frontendDB->be_config ) {
860 rc = (*frontendDB->be_config)( frontendDB,
861 c->fname, (int)c->lineno, c->argc, c->argv);
864 case SLAP_CONF_UNKNOWN:
865 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
866 "<%s> inside global database definition.\n",
867 c->log, *c->argv, 0);
876 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
877 "<%s> outside backend info and database definitions.\n",
878 c->log, *c->argv, 0);
898 /* restrictops, allows, disallows, requires, loglevel */
901 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
903 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
904 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
910 verb_to_mask(const char *word, slap_verbmasks *v) {
912 ber_str2bv( word, 0, 0, &bword );
913 return bverb_to_mask( &bword, v );
917 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
919 for(i = 1; i < argc; i++) {
920 j = verb_to_mask(argv[i], v);
921 if(BER_BVISNULL(&v[j].word)) return i;
922 while (!v[j].mask) j--;
928 /* Mask keywords that represent multiple bits should occur before single
929 * bit keywords in the verbmasks array.
932 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
936 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
937 if (!v[i].mask) continue;
938 if (( m & v[i].mask ) == v[i].mask ) {
939 value_add_one( bva, &v[i].word );
949 /* Return the verbs as a single string, separated by delim */
951 mask_to_verbstring(slap_verbmasks *v, slap_mask_t m0, char delim, struct berval *bv)
959 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
960 if (!v[i].mask) continue;
961 if (( m & v[i].mask ) == v[i].mask ) {
962 bv->bv_len += v[i].word.bv_len + 1;
968 bv->bv_val = ch_malloc(bv->bv_len);
972 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
973 if (!v[i].mask) continue;
974 if (( m & v[i].mask ) == v[i].mask ) {
975 ptr = lutil_strcopy(ptr, v[i].word.bv_val);
986 /* Parse a verbstring */
988 verbstring_to_mask(slap_verbmasks *v, char *str, char delim, slap_mask_t *m) {
995 d = strchr( str, delim );
999 bv.bv_len = strlen( str );
1000 j = bverb_to_mask( &bv, v );
1001 if(BER_BVISNULL(&v[j].word)) return 1;
1002 while (!v[j].mask) j--;
1004 str += bv.bv_len + 1;
1010 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
1014 assert( *vp == NULL );
1016 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
1018 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
1020 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1021 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
1022 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
1025 BER_BVZERO( &(*vp)[ i ].word );
1031 slap_verbmasks_destroy( slap_verbmasks *v )
1035 assert( v != NULL );
1037 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
1038 ch_free( v[ i ].word.bv_val );
1047 slap_verbmasks_append(
1048 slap_verbmasks **vp,
1051 slap_mask_t *ignore )
1056 return LDAP_OPERATIONS_ERROR;
1059 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
1060 if ( !(*vp)[ i ].mask ) continue;
1062 if ( ignore != NULL ) {
1065 for ( j = 0; ignore[ j ] != 0; j++ ) {
1066 if ( (*vp)[ i ].mask == ignore[ j ] ) {
1072 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
1073 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
1074 /* already set; ignore */
1075 return LDAP_SUCCESS;
1078 return LDAP_TYPE_OR_VALUE_EXISTS;
1081 if ( m & (*vp)[ i ].mask ) {
1083 return LDAP_CONSTRAINT_VIOLATION;
1088 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1089 ber_dupbv( &(*vp)[ i ].word, v );
1090 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1091 BER_BVZERO( &(*vp)[ i + 1 ].word );
1093 return LDAP_SUCCESS;
1097 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1100 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1101 if ( m == v[i].mask ) {
1111 /* register a new verbmask */
1113 slap_verbmask_register( slap_verbmasks *vm_, slap_verbmasks **vmp, struct berval *bv, int mask )
1115 slap_verbmasks *vm = *vmp;
1118 /* check for duplicate word */
1119 /* NOTE: we accept duplicate codes; the first occurrence will be used
1120 * when mapping from mask to verb */
1121 i = verb_to_mask( bv->bv_val, vm );
1122 if ( !BER_BVISNULL( &vm[ i ].word ) ) {
1126 for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
1130 /* first time: duplicate array */
1131 vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
1132 for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
1134 ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
1135 *((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
1139 vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
1142 ber_dupbv( &vm[ i ].word, bv );
1143 *((slap_mask_t*)&vm[ i ].mask) = mask;
1145 BER_BVZERO( &vm[ i+1 ].word );
1152 static slap_verbmasks slap_ldap_response_code_[] = {
1153 { BER_BVC("success"), LDAP_SUCCESS },
1155 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR },
1156 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR },
1157 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED },
1158 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED },
1159 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE },
1160 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE },
1162 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED },
1163 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED },
1164 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED },
1165 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED },
1166 #if 0 /* not LDAPv3 */
1167 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS },
1170 { BER_BVC("referral"), LDAP_REFERRAL },
1171 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED },
1172 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
1173 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED },
1174 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS },
1176 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE },
1177 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE },
1178 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING },
1179 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION },
1180 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS },
1181 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX },
1183 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT },
1184 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM },
1185 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX },
1186 #if 0 /* not LDAPv3 */
1187 { BER_BVC("isLeaf"), LDAP_IS_LEAF },
1189 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM },
1191 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE },
1192 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH },
1193 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS },
1194 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS },
1196 { BER_BVC("busy"), LDAP_BUSY },
1197 { BER_BVC("unavailable"), LDAP_UNAVAILABLE },
1198 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM },
1199 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT },
1201 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION },
1202 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION },
1203 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF },
1204 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN },
1205 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS },
1206 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS },
1207 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE },
1208 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS },
1210 { BER_BVC("other"), LDAP_OTHER },
1212 /* extension-specific */
1214 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED },
1215 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION },
1216 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA },
1217 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME },
1218 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED },
1220 { BER_BVC("cancelled"), LDAP_CANCELLED },
1221 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION },
1222 { BER_BVC("tooLate"), LDAP_TOO_LATE },
1223 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL },
1225 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED },
1227 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED },
1229 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED },
1231 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION },
1236 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_;
1239 slap_ldap_response_code_register( struct berval *bv, int err )
1241 return slap_verbmask_register( slap_ldap_response_code_,
1242 &slap_ldap_response_code, bv, err );
1246 static slap_verbmasks tlskey[] = {
1247 { BER_BVC("no"), SB_TLS_OFF },
1248 { BER_BVC("yes"), SB_TLS_ON },
1249 { BER_BVC("critical"), SB_TLS_CRITICAL },
1253 static slap_verbmasks crlkeys[] = {
1254 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE },
1255 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER },
1256 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL },
1260 static slap_verbmasks vfykeys[] = {
1261 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER },
1262 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND },
1263 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY },
1264 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD },
1269 static slap_verbmasks methkey[] = {
1270 { BER_BVC("none"), LDAP_AUTH_NONE },
1271 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1272 #ifdef HAVE_CYRUS_SASL
1273 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1278 static slap_verbmasks versionkey[] = {
1279 { BER_BVC("2"), LDAP_VERSION2 },
1280 { BER_BVC("3"), LDAP_VERSION3 },
1285 slap_keepalive_parse(
1288 slap_cf_aux_table *tab0,
1293 slap_keepalive *sk = (slap_keepalive *)bc;
1294 int rc = snprintf( val->bv_val, val->bv_len, "%d:%d:%d",
1295 sk->sk_idle, sk->sk_probes, sk->sk_interval );
1300 if ( (unsigned)rc >= val->bv_len ) {
1307 char *s = val->bv_val;
1309 slap_keepalive *sk = (slap_keepalive *)bc;
1312 if ( s[0] == ':' ) {
1317 sk2.sk_idle = strtol( s, &next, 10 );
1318 if ( next == s || next[0] != ':' ) {
1322 if ( sk2.sk_idle < 0 ) {
1329 if ( s[0] == ':' ) {
1334 sk2.sk_probes = strtol( s, &next, 10 );
1335 if ( next == s || next[0] != ':' ) {
1339 if ( sk2.sk_probes < 0 ) {
1347 sk2.sk_interval = 0;
1351 sk2.sk_interval = strtol( s, &next, 10 );
1352 if ( next == s || next[0] != '\0' ) {
1356 if ( sk2.sk_interval < 0 ) {
1363 ber_memfree( val->bv_val );
1374 slap_cf_aux_table *tab0,
1378 slap_bindconf *bc = bcp;
1380 if ( bc->sb_uri.bv_len >= val->bv_len )
1382 val->bv_len = bc->sb_uri.bv_len;
1383 AC_MEMCPY( val->bv_val, bc->sb_uri.bv_val, val->bv_len );
1387 if ( ldap_is_ldaps_url( val->bv_val ))
1388 bc->sb_tls_do_init = 1;
1394 static slap_cf_aux_table bindkey[] = {
1395 { BER_BVC("uri="), 0, 'x', 1, slap_sb_uri },
1396 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1397 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1398 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1399 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1400 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1401 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1402 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1403 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1404 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1405 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1406 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1407 { BER_BVC("keepalive="), offsetof(slap_bindconf, sb_keepalive), 'x', 0, (slap_verbmasks *)slap_keepalive_parse },
1409 /* NOTE: replace "13" with the actual index
1410 * of the first TLS-related line */
1411 #define aux_TLS (bindkey+13) /* beginning of TLS keywords */
1413 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1414 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1415 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1416 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1417 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1418 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 0, NULL },
1419 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 0, NULL },
1420 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 0, NULL },
1421 #ifdef HAVE_OPENSSL_CRL
1422 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 0, NULL },
1425 { BER_BVNULL, 0, 0, 0, NULL }
1430 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1431 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1434 * 'U': unsigned long
1438 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1440 int rc = SLAP_CONF_UNKNOWN;
1441 slap_cf_aux_table *tab;
1443 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1444 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1449 unsigned long *ulptr;
1450 struct berval *bptr;
1451 const char *val = word + tab->key.bv_len;
1453 switch ( tab->type ) {
1455 cptr = (char **)((char *)dst + tab->off);
1456 *cptr = ch_strdup( val );
1461 bptr = (struct berval *)((char *)dst + tab->off);
1462 if ( tab->aux != NULL ) {
1464 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1466 ber_str2bv( val, 0, 0, &dn );
1467 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1470 ber_str2bv( val, 0, 1, bptr );
1476 iptr = (int *)((char *)dst + tab->off);
1478 if ( tab->aux != NULL ) {
1479 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1481 assert( aux != NULL );
1484 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1485 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1486 *iptr = aux[j].mask;
1493 rc = lutil_atoix( iptr, val, 0 );
1498 uptr = (unsigned *)((char *)dst + tab->off);
1500 rc = lutil_atoux( uptr, val, 0 );
1504 lptr = (long *)((char *)dst + tab->off);
1506 rc = lutil_atolx( lptr, val, 0 );
1510 ulptr = (unsigned long *)((char *)dst + tab->off);
1512 rc = lutil_atoulx( ulptr, val, 0 );
1516 if ( tab->aux != NULL ) {
1517 struct berval value;
1518 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1520 ber_str2bv( val, 0, 1, &value );
1522 rc = func( &value, (void *)((char *)dst + tab->off), tab, tabmsg, 0 );
1531 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1543 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1545 char buf[AC_LINE_MAX], *ptr;
1546 slap_cf_aux_table *tab;
1550 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1555 unsigned long *ulptr;
1556 struct berval *bptr;
1558 cptr = (char **)((char *)src + tab->off);
1560 switch ( tab->type ) {
1562 bptr = (struct berval *)((char *)src + tab->off);
1563 cptr = &bptr->bv_val;
1568 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1569 if ( tab->quote ) *ptr++ = '"';
1570 ptr = lutil_strcopy( ptr, *cptr );
1571 if ( tab->quote ) *ptr++ = '"';
1576 iptr = (int *)((char *)src + tab->off);
1578 if ( tab->aux != NULL ) {
1579 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1581 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1582 if ( *iptr == aux[i].mask ) {
1584 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1585 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1592 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1593 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1598 uptr = (unsigned *)((char *)src + tab->off);
1600 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1601 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1605 lptr = (long *)((char *)src + tab->off);
1607 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1608 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1612 ulptr = (unsigned long *)((char *)src + tab->off);
1614 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1615 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1622 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1623 if ( tab->quote ) *ptr++ = '"';
1624 if ( tab->aux != NULL ) {
1625 struct berval value;
1626 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux;
1630 value.bv_len = buf + sizeof( buf ) - ptr;
1632 rc = func( &value, (void *)((char *)src + tab->off), tab, "(unparse)", 1 );
1635 ptr += value.bv_len;
1642 if ( tab->quote ) *ptr++ = '"';
1651 tmp.bv_len = ptr - buf;
1652 ber_dupbv( bv, &tmp );
1657 slap_tls_get_config( LDAP *ld, int opt, char **val )
1660 slap_verbmasks *keys;
1665 case LDAP_OPT_X_TLS_CRLCHECK:
1668 case LDAP_OPT_X_TLS_REQUIRE_CERT:
1671 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
1673 ldap_pvt_tls_get_option( ld, opt, &ival );
1674 snprintf( buf, sizeof( buf ), "%d.%d",
1675 ( ival >> 8 ) & 0xff, ival & 0xff );
1676 *val = ch_strdup( buf );
1682 ldap_pvt_tls_get_option( ld, opt, &ival );
1683 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1684 if (keys[i].mask == ival) {
1685 *val = ch_strdup( keys[i].word.bv_val );
1694 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1697 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1698 bc->sb_tls_do_init = 1;
1706 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1709 return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1715 bindconf_parse( const char *word, slap_bindconf *bc )
1718 /* Detect TLS config changes explicitly */
1719 if ( bindconf_tls_parse( word, bc ) == 0 ) {
1723 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1727 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1729 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1732 void bindconf_free( slap_bindconf *bc ) {
1733 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1734 ch_free( bc->sb_uri.bv_val );
1735 BER_BVZERO( &bc->sb_uri );
1737 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1738 ch_free( bc->sb_binddn.bv_val );
1739 BER_BVZERO( &bc->sb_binddn );
1741 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1742 ch_free( bc->sb_cred.bv_val );
1743 BER_BVZERO( &bc->sb_cred );
1745 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1746 ch_free( bc->sb_saslmech.bv_val );
1747 BER_BVZERO( &bc->sb_saslmech );
1749 if ( bc->sb_secprops ) {
1750 ch_free( bc->sb_secprops );
1751 bc->sb_secprops = NULL;
1753 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1754 ch_free( bc->sb_realm.bv_val );
1755 BER_BVZERO( &bc->sb_realm );
1757 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1758 ch_free( bc->sb_authcId.bv_val );
1759 BER_BVZERO( &bc->sb_authcId );
1761 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1762 ch_free( bc->sb_authzId.bv_val );
1763 BER_BVZERO( &bc->sb_authzId );
1766 if ( bc->sb_tls_cert ) {
1767 ch_free( bc->sb_tls_cert );
1768 bc->sb_tls_cert = NULL;
1770 if ( bc->sb_tls_key ) {
1771 ch_free( bc->sb_tls_key );
1772 bc->sb_tls_key = NULL;
1774 if ( bc->sb_tls_cacert ) {
1775 ch_free( bc->sb_tls_cacert );
1776 bc->sb_tls_cacert = NULL;
1778 if ( bc->sb_tls_cacertdir ) {
1779 ch_free( bc->sb_tls_cacertdir );
1780 bc->sb_tls_cacertdir = NULL;
1782 if ( bc->sb_tls_reqcert ) {
1783 ch_free( bc->sb_tls_reqcert );
1784 bc->sb_tls_reqcert = NULL;
1786 if ( bc->sb_tls_cipher_suite ) {
1787 ch_free( bc->sb_tls_cipher_suite );
1788 bc->sb_tls_cipher_suite = NULL;
1790 if ( bc->sb_tls_protocol_min ) {
1791 ch_free( bc->sb_tls_protocol_min );
1792 bc->sb_tls_protocol_min = NULL;
1794 #ifdef HAVE_OPENSSL_CRL
1795 if ( bc->sb_tls_crlcheck ) {
1796 ch_free( bc->sb_tls_crlcheck );
1797 bc->sb_tls_crlcheck = NULL;
1800 if ( bc->sb_tls_ctx ) {
1801 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1802 bc->sb_tls_ctx = NULL;
1808 bindconf_tls_defaults( slap_bindconf *bc )
1811 if ( bc->sb_tls_do_init ) {
1812 if ( !bc->sb_tls_cacert )
1813 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1814 &bc->sb_tls_cacert );
1815 if ( !bc->sb_tls_cacertdir )
1816 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1817 &bc->sb_tls_cacertdir );
1818 if ( !bc->sb_tls_cert )
1819 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1821 if ( !bc->sb_tls_key )
1822 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1824 if ( !bc->sb_tls_cipher_suite )
1825 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1826 &bc->sb_tls_cipher_suite );
1827 if ( !bc->sb_tls_reqcert )
1828 bc->sb_tls_reqcert = ch_strdup("demand");
1829 #ifdef HAVE_OPENSSL_CRL
1830 if ( !bc->sb_tls_crlcheck )
1831 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1832 &bc->sb_tls_crlcheck );
1844 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1845 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1846 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1847 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1848 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1849 { "tls_protocol_min", offsetof(slap_bindconf, sb_tls_protocol_min), LDAP_OPT_X_TLS_PROTOCOL_MIN },
1853 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1855 int i, rc, newctx = 0, res = 0;
1856 char *ptr = (char *)bc, **word;
1858 bc->sb_tls_do_init = 0;
1860 for (i=0; bindtlsopts[i].opt; i++) {
1861 word = (char **)(ptr + bindtlsopts[i].offset);
1863 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1865 Debug( LDAP_DEBUG_ANY,
1866 "bindconf_tls_set: failed to set %s to %s\n",
1867 bindtlsopts[i].key, *word, 0 );
1873 if ( bc->sb_tls_reqcert ) {
1874 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1875 bc->sb_tls_reqcert );
1877 Debug( LDAP_DEBUG_ANY,
1878 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1879 bc->sb_tls_reqcert, 0, 0 );
1884 if ( bc->sb_tls_protocol_min ) {
1885 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN,
1886 bc->sb_tls_protocol_min );
1888 Debug( LDAP_DEBUG_ANY,
1889 "bindconf_tls_set: failed to set tls_protocol_min to %s\n",
1890 bc->sb_tls_protocol_min, 0, 0 );
1895 #ifdef HAVE_OPENSSL_CRL
1896 if ( bc->sb_tls_crlcheck ) {
1897 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1898 bc->sb_tls_crlcheck );
1900 Debug( LDAP_DEBUG_ANY,
1901 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1902 bc->sb_tls_crlcheck, 0, 0 );
1911 if ( bc->sb_tls_ctx ) {
1912 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1913 bc->sb_tls_ctx = NULL;
1915 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1919 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1927 * connect to a client using the bindconf data
1928 * note: should move "version" into bindconf...
1931 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1937 /* Init connection to master */
1938 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1939 if ( rc != LDAP_SUCCESS ) {
1940 Debug( LDAP_DEBUG_ANY,
1941 "slap_client_connect: "
1942 "ldap_initialize(%s) failed (%d)\n",
1943 sb->sb_uri.bv_val, rc, 0 );
1947 if ( sb->sb_version != 0 ) {
1948 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1949 (const void *)&sb->sb_version );
1952 if ( sb->sb_timeout_api ) {
1953 tv.tv_sec = sb->sb_timeout_api;
1955 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1958 if ( sb->sb_timeout_net ) {
1959 tv.tv_sec = sb->sb_timeout_net;
1961 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1964 if ( sb->sb_keepalive.sk_idle ) {
1965 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_IDLE, &sb->sb_keepalive.sk_idle );
1968 if ( sb->sb_keepalive.sk_probes ) {
1969 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_PROBES, &sb->sb_keepalive.sk_probes );
1972 if ( sb->sb_keepalive.sk_interval ) {
1973 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_INTERVAL, &sb->sb_keepalive.sk_interval );
1977 if ( sb->sb_tls_do_init ) {
1978 rc = bindconf_tls_set( sb, ld );
1980 } else if ( sb->sb_tls_ctx ) {
1981 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1986 Debug( LDAP_DEBUG_ANY,
1987 "slap_client_connect: "
1988 "URI=%s TLS context initialization failed (%d)\n",
1989 sb->sb_uri.bv_val, rc, 0 );
1996 rc = ldap_start_tls_s( ld, NULL, NULL );
1997 if ( rc != LDAP_SUCCESS ) {
1998 Debug( LDAP_DEBUG_ANY,
1999 "slap_client_connect: URI=%s "
2000 "%s, ldap_start_tls failed (%d)\n",
2002 sb->sb_tls == SB_TLS_CRITICAL ?
2003 "Error" : "Warning",
2005 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
2011 if ( sb->sb_method == LDAP_AUTH_SASL ) {
2012 #ifdef HAVE_CYRUS_SASL
2015 if ( sb->sb_secprops != NULL ) {
2016 rc = ldap_set_option( ld,
2017 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
2019 if( rc != LDAP_OPT_SUCCESS ) {
2020 Debug( LDAP_DEBUG_ANY,
2021 "slap_client_connect: "
2022 "error, ldap_set_option "
2023 "(%s,SECPROPS,\"%s\") failed!\n",
2024 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
2029 defaults = lutil_sasl_defaults( ld,
2030 sb->sb_saslmech.bv_val,
2031 sb->sb_realm.bv_val,
2032 sb->sb_authcId.bv_val,
2034 sb->sb_authzId.bv_val );
2035 if ( defaults == NULL ) {
2040 rc = ldap_sasl_interactive_bind_s( ld,
2041 sb->sb_binddn.bv_val,
2042 sb->sb_saslmech.bv_val,
2045 lutil_sasl_interact,
2048 lutil_sasl_freedefs( defaults );
2050 /* FIXME: different error behaviors according to
2052 * 2) on err policy : exit, retry, backoff ...
2054 if ( rc != LDAP_SUCCESS ) {
2055 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
2057 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
2058 "ldap_sasl_interactive_bind_s failed (%d)\n",
2059 sb->sb_uri.bv_val, rc, 0 );
2061 /* FIXME (see above comment) */
2062 /* if Kerberos credentials cache is not active, retry */
2063 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
2064 rc == LDAP_LOCAL_ERROR )
2066 rc = LDAP_SERVER_DOWN;
2071 #else /* HAVE_CYRUS_SASL */
2072 /* Should never get here, we trapped this at config time */
2074 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
2079 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
2080 rc = ldap_sasl_bind_s( ld,
2081 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
2082 &sb->sb_cred, NULL, NULL, NULL );
2083 if ( rc != LDAP_SUCCESS ) {
2084 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
2086 "ldap_sasl_bind_s failed (%d)\n",
2087 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
2095 ldap_unbind_ext( ld, NULL, NULL );
2106 /* -------------------------------------- */
2110 strtok_quote( char *line, char *sep, char **quote_ptr )
2117 if ( line != NULL ) {
2120 while ( *next && strchr( sep, *next ) ) {
2124 if ( *next == '\0' ) {
2130 for ( inquote = 0; *next; ) {
2138 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2144 next + 1, strlen( next + 1 ) + 1 );
2145 next++; /* dont parse the escaped character */
2150 if ( strchr( sep, *next ) != NULL ) {
2164 static char buf[AC_LINE_MAX];
2166 static size_t lmax, lcur;
2168 #define CATLINE( buf ) \
2170 size_t len = strlen( buf ); \
2171 while ( lcur + len + 1 > lmax ) { \
2172 lmax += AC_LINE_MAX; \
2173 line = (char *) ch_realloc( line, lmax ); \
2175 strcpy( line + lcur, buf ); \
2180 fp_getline_init(ConfigArgs *c) {
2186 fp_getline( FILE *fp, ConfigArgs *c )
2194 /* avoid stack of bufs */
2195 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
2201 while ( fgets( buf, sizeof( buf ), fp ) ) {
2202 p = strchr( buf, '\n' );
2204 if ( p > buf && p[-1] == '\r' ) {
2212 && ( p = line + strlen( line ) - 1 )[0] == '\\'
2219 if ( !isspace( (unsigned char)buf[0] ) ) {
2230 return(line[0] ? 1 : 0);
2234 config_fp_parse_line(ConfigArgs *c)
2237 static char *const hide[] = {
2238 "rootpw", "replica", "syncrepl", /* in slapd */
2239 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
2240 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
2241 "pseudorootpw", /* in back-meta */
2242 "dbpasswd", /* in back-sql */
2246 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
2248 c->tline = ch_strdup(c->line);
2249 token = strtok_quote(c->tline, " \t", "e_ptr);
2251 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
2252 if(quote_ptr) *quote_ptr = ' ';
2253 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
2254 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
2255 if(quote_ptr) *quote_ptr = '\0';
2257 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
2258 if(c->argc >= c->argv_size) {
2260 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
2262 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
2266 c->argv_size += ARGS_STEP;
2270 c->argv[c->argc++] = token;
2272 c->argv[c->argc] = NULL;
2279 ucdata_unload( UCDATA_ALL );
2281 /* NOTE: in case of early exit, frontendDB can be NULL */
2282 if ( frontendDB->be_schemandn.bv_val )
2283 free( frontendDB->be_schemandn.bv_val );
2284 if ( frontendDB->be_schemadn.bv_val )
2285 free( frontendDB->be_schemadn.bv_val );
2286 if ( frontendDB->be_acl )
2287 acl_destroy( frontendDB->be_acl );
2290 if ( slapd_args_file )
2291 free ( slapd_args_file );
2292 if ( slapd_pid_file )
2293 free ( slapd_pid_file );
2294 if ( default_passwd_hash )
2295 ldap_charray_free( default_passwd_hash );
2299 slap_str2clist( char ***out, char *in, const char *brkstr )
2307 /* find last element in list */
2308 for (i = 0; *out && (*out)[i]; i++);
2310 /* protect the input string from strtok */
2311 str = ch_strdup( in );
2313 if ( *str == '\0' ) {
2318 /* Count words in string */
2320 for ( s = str; *s; s++ ) {
2321 if ( strchr( brkstr, *s ) != NULL ) {
2326 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2328 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2330 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2332 *new = ch_strdup( s );
2341 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
2342 int argc, char **argv )
2344 ConfigArgs c = { 0 };
2355 c.op = SLAP_CONFIG_ADD;
2356 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
2358 rc = SLAP_CONF_UNKNOWN;
2359 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
2361 c.table = be->be_cf_ocs->co_type;
2362 rc = config_add_vals( ct, &c );
2367 /* See if the given URL (in plain and parsed form) matches
2368 * any of the server's listener addresses. Return matching
2369 * Listener or NULL for no match.
2371 Listener *config_check_my_url( const char *url, LDAPURLDesc *lud )
2373 Listener **l = slapd_get_listeners();
2376 /* Try a straight compare with Listener strings */
2377 for ( i=0; l && l[i]; i++ ) {
2378 if ( !strcasecmp( url, l[i]->sl_url.bv_val )) {
2384 /* If hostname is empty, or is localhost, or matches
2385 * our hostname, this url refers to this host.
2386 * Compare it against listeners and ports.
2388 if ( !lud->lud_host || !lud->lud_host[0] ||
2389 !strncasecmp("localhost", lud->lud_host,
2390 STRLENOF("localhost")) ||
2391 !strcasecmp( global_host, lud->lud_host )) {
2393 for ( i=0; l && l[i]; i++ ) {
2395 ldap_url_parse( l[i]->sl_url.bv_val, &lu2 );
2397 if ( strcasecmp( lud->lud_scheme,
2400 if ( lud->lud_port != lu2->lud_port )
2402 /* Listener on ANY address */
2403 if ( !lu2->lud_host || !lu2->lud_host[0] ) {
2407 /* URL on ANY address */
2408 if ( !lud->lud_host || !lud->lud_host[0] ) {
2412 /* Listener has specific host, must
2415 if ( !strcasecmp( lud->lud_host,
2421 ldap_free_urldesc( lu2 );