1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/string.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
37 #include <sys/types.h>
43 #include "slapi/slapi.h"
49 #include <openssl/ssl.h>
55 * defaults for various global variables
57 slap_mask_t global_allows = 0;
58 slap_mask_t global_disallows = 0;
59 int global_gentlehup = 0;
60 int global_idletimeout = 0;
61 char *global_host = NULL;
62 char *global_realm = NULL;
63 char *ldap_srvtab = "";
64 char **default_passwd_hash = NULL;
65 struct berval default_search_base = BER_BVNULL;
66 struct berval default_search_nbase = BER_BVNULL;
68 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
69 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
71 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
72 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
74 char *slapd_pid_file = NULL;
75 char *slapd_args_file = NULL;
77 int use_reverse_lookup = 0;
80 int slapi_plugins_used = 0;
83 static int fp_getline(FILE *fp, ConfigArgs *c);
84 static void fp_getline_init(ConfigArgs *c);
85 static int fp_parse_line(ConfigArgs *c);
87 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
88 static char *strtok_quote_ldif(char **line);
91 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
94 c = ch_calloc( 1, sizeof( ConfigArgs ) );
95 if ( c == NULL ) return(NULL);
101 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
106 init_config_argv( ConfigArgs *c )
108 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
109 c->argv_size = ARGS_STEP + 1;
112 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
115 for(i = 0; Conf[i].name; i++)
116 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
117 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
118 if ( !Conf[i].name ) return NULL;
122 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
123 int rc, arg_user, arg_type, arg_syn, iarg;
127 if(Conf->arg_type == ARG_IGNORED) {
128 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
129 c->log, Conf->name, 0);
132 arg_type = Conf->arg_type & ARGS_TYPES;
133 arg_user = Conf->arg_type & ARGS_USERLAND;
134 arg_syn = Conf->arg_type & ARGS_SYNTAX;
136 if((arg_type == ARG_DN) && c->argc == 1) {
140 if(Conf->min_args && (c->argc < Conf->min_args)) {
141 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
142 c->argv[0], Conf->what );
143 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
144 return(ARG_BAD_CONF);
146 if(Conf->max_args && (c->argc > Conf->max_args)) {
147 char *ignored = " ignored";
149 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
150 c->argv[0], Conf->what );
154 #endif /* LDAP_DEVEL */
155 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
156 c->log, c->msg, ignored );
158 return(ARG_BAD_CONF);
159 #endif /* LDAP_DEVEL */
161 if((arg_syn & ARG_DB) && !c->be) {
162 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
164 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
166 return(ARG_BAD_CONF);
168 if((arg_syn & ARG_PRE_BI) && c->bi) {
169 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
170 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
171 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
173 return(ARG_BAD_CONF);
175 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
176 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
178 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
180 return(ARG_BAD_CONF);
182 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
183 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
184 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
186 return(ARG_BAD_CONF);
188 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
189 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
191 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
193 return(ARG_BAD_CONF);
196 memset(&c->values, 0, sizeof(c->values));
197 if(arg_type == ARG_STRING) {
199 c->value_string = ch_strdup(c->argv[1]);
200 } else if(arg_type == ARG_BERVAL) {
202 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
203 } else if(arg_type == ARG_DN) {
205 ber_str2bv( c->argv[1], 0, 0, &bv );
206 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
207 if ( rc != LDAP_SUCCESS ) {
208 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
209 c->argv[0], rc, ldap_err2string( rc ));
210 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
211 return(ARG_BAD_CONF);
214 ch_free( c->value_ndn.bv_val );
215 ch_free( c->value_dn.bv_val );
217 } else { /* all numeric */
219 iarg = 0; larg = 0; barg = 0;
222 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
223 snprintf( c->msg, sizeof( c->msg ),
224 "<%s> unable to parse \"%s\" as int",
225 c->argv[0], c->argv[1] );
226 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
228 return(ARG_BAD_CONF);
232 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
233 snprintf( c->msg, sizeof( c->msg ),
234 "<%s> unable to parse \"%s\" as long",
235 c->argv[0], c->argv[1] );
236 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
238 return(ARG_BAD_CONF);
241 case ARG_BER_LEN_T: {
243 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
244 snprintf( c->msg, sizeof( c->msg ),
245 "<%s> unable to parse \"%s\" as ber_len_t",
246 c->argv[0], c->argv[1] );
247 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
249 return(ARG_BAD_CONF);
256 } else if ( !strcasecmp(c->argv[1], "on") ||
257 !strcasecmp(c->argv[1], "true") ||
258 !strcasecmp(c->argv[1], "yes") )
261 } else if ( !strcasecmp(c->argv[1], "off") ||
262 !strcasecmp(c->argv[1], "false") ||
263 !strcasecmp(c->argv[1], "no") )
267 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
269 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
271 return(ARG_BAD_CONF);
275 j = (arg_type & ARG_NONZERO) ? 1 : 0;
276 if(iarg < j && larg < j && barg < j ) {
277 larg = larg ? larg : (barg ? barg : iarg);
278 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
280 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
282 return(ARG_BAD_CONF);
286 case ARG_INT: c->value_int = iarg; break;
287 case ARG_LONG: c->value_long = larg; break;
288 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
294 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
298 arg_type = Conf->arg_type;
299 if(arg_type & ARG_MAGIC) {
300 if(!c->be) c->be = frontendDB;
302 rc = (*((ConfigDriver*)Conf->arg_item))(c);
304 if(c->be == frontendDB) c->be = NULL;
308 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
310 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
313 return(ARG_BAD_CONF);
317 if(arg_type & ARG_OFFSET) {
318 if (c->be && (!overlay_is_over(c->be) ||
319 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
320 ptr = c->be->be_private;
322 ptr = c->bi->bi_private;
324 snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
326 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
328 return(ARG_BAD_CONF);
330 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
331 } else if (arg_type & ARGS_TYPES) {
332 ptr = Conf->arg_item;
334 if(arg_type & ARGS_TYPES)
335 switch(arg_type & ARGS_TYPES) {
337 case ARG_INT: *(int*)ptr = c->value_int; break;
338 case ARG_LONG: *(long*)ptr = c->value_long; break;
339 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
341 char *cc = *(char**)ptr;
343 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
344 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
345 c->log, Conf->name, 0 );
346 return(ARG_BAD_CONF);
350 *(char **)ptr = c->value_string;
354 *(struct berval *)ptr = c->value_bv;
360 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
363 arg_type = Conf->arg_type;
364 if(arg_type == ARG_IGNORED) {
365 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
366 c->log, Conf->name, 0);
369 rc = config_check_vals( Conf, c, 0 );
371 return config_set_vals( Conf, c );
375 config_del_vals(ConfigTable *cf, ConfigArgs *c)
379 /* If there is no handler, just ignore it */
380 if ( cf->arg_type & ARG_MAGIC ) {
381 c->op = LDAP_MOD_DELETE;
382 c->type = cf->arg_type & ARGS_USERLAND;
383 rc = (*((ConfigDriver*)cf->arg_item))(c);
389 config_get_vals(ConfigTable *cf, ConfigArgs *c)
395 if ( cf->arg_type & ARG_IGNORED ) {
399 memset(&c->values, 0, sizeof(c->values));
400 c->rvalue_vals = NULL;
401 c->rvalue_nvals = NULL;
402 c->op = SLAP_CONFIG_EMIT;
403 c->type = cf->arg_type & ARGS_USERLAND;
405 if ( cf->arg_type & ARG_MAGIC ) {
406 rc = (*((ConfigDriver*)cf->arg_item))(c);
409 if ( cf->arg_type & ARG_OFFSET ) {
410 if (c->be && (!overlay_is_over(c->be) ||
411 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
412 ptr = c->be->be_private;
414 ptr = c->bi->bi_private;
417 ptr = (void *)((char *)ptr + (long)cf->arg_item);
422 switch(cf->arg_type & ARGS_TYPES) {
424 case ARG_INT: c->value_int = *(int *)ptr; break;
425 case ARG_LONG: c->value_long = *(long *)ptr; break;
426 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
429 c->value_string = ch_strdup(*(char **)ptr);
432 ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
435 if ( cf->arg_type & ARGS_TYPES) {
438 switch(cf->arg_type & ARGS_TYPES) {
439 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
440 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
441 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
442 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
443 c->value_int ? "TRUE" : "FALSE"); break;
445 if ( c->value_string && c->value_string[0]) {
446 ber_str2bv( c->value_string, 0, 0, &bv);
452 if ( !BER_BVISEMPTY( &c->value_bv )) {
462 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
465 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
466 ber_bvarray_add(&c->rvalue_vals, &bv);
467 } else if ( !BER_BVISNULL( &bv ) ) {
468 value_add_one(&c->rvalue_vals, &bv);
470 /* else: maybe c->rvalue_vals already set? */
476 init_config_attrs(ConfigTable *ct) {
477 LDAPAttributeType *at;
481 for (i=0; ct[i].name; i++ ) {
484 if ( !ct[i].attribute ) continue;
485 at = ldap_str2attributetype( ct[i].attribute,
486 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
488 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
489 ct[i].attribute, ldap_scherr2str(code), err );
493 code = at_add( at, 0, NULL, &err );
495 if ( code == SLAP_SCHERR_ATTR_DUP ) {
499 ldap_attributetype_free( at );
500 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
501 ct[i].attribute, scherr2str(code), err );
505 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
506 if ( freeit || code ) {
507 ldap_attributetype_free( at );
512 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
513 ct[i].attribute, err );
522 init_config_ocs( ConfigOCs *ocs ) {
525 for (i=0;ocs[i].co_def;i++) {
530 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
531 LDAP_SCHEMA_ALLOW_ALL );
533 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
534 ocs[i].co_def, ldap_scherr2str(code), err );
537 code = oc_add(oc,0,NULL,&err);
538 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
539 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
540 ocs[i].co_def, scherr2str(code), err );
541 ldap_objectclass_free(oc);
544 ocs[i].co_oc = oc_find(oc->oc_names[0]);
546 ldap_objectclass_free(oc);
553 /* Split an LDIF line into space-separated tokens. Words may be grouped
554 * by quotes. A quoted string may begin in the middle of a word, but must
555 * end at the end of the word (be followed by whitespace or EOS). Any other
556 * quotes are passed through unchanged. All other characters are passed
560 strtok_quote_ldif( char **line )
562 char *beg, *ptr, *quote=NULL;
570 while( isspace( (unsigned char) *ptr )) ptr++;
581 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
591 if ( isspace( (unsigned char) *ptr )) {
597 while ( quote < ptr ) {
605 while ( isspace( (unsigned char) *ptr )) ptr++;
612 config_parse_ldif( ConfigArgs *c )
615 c->tline = ch_strdup(c->line);
618 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
620 if ( c->argc >= c->argv_size ) {
621 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
624 c->argv_size += ARGS_STEP;
627 c->argv[c->argc] = NULL;
631 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
635 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
636 ct->ad->ad_cname.bv_val, valx );
638 c->argv[0] = ct->ad->ad_cname.bv_val;
640 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
641 c->argv[c->argc] = c->line;
643 c->argv[c->argc] = NULL;
646 config_parse_ldif( c );
648 rc = config_check_vals( ct, c, 1 );
653 rc = LDAP_CONSTRAINT_VIOLATION;
659 config_parse_add(ConfigTable *ct, ConfigArgs *c)
663 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
664 ct->ad->ad_cname.bv_val, c->valx );
666 c->argv[0] = ct->ad->ad_cname.bv_val;
668 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
669 c->argv[c->argc] = c->line;
671 c->argv[c->argc] = NULL;
674 config_parse_ldif( c );
676 c->op = LDAP_MOD_ADD;
677 rc = config_add_vals( ct, c );
684 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
692 c = ch_calloc( 1, sizeof( ConfigArgs ) );
698 memcpy( c, cf, sizeof( ConfigArgs ) );
700 c->depth = depth; /* XXX */
707 init_config_argv( c );
709 if ( stat( fname, &s ) != 0 ) {
711 Debug(LDAP_DEBUG_ANY,
712 "could not stat config file \"%s\": %s (%d)\n",
713 fname, strerror(errno), errno);
718 if ( !S_ISREG( s.st_mode ) ) {
720 Debug(LDAP_DEBUG_ANY,
721 "regular file expected, got \"%s\"\n",
727 fp = fopen( fname, "r" );
730 Debug(LDAP_DEBUG_ANY,
731 "could not open config file \"%s\": %s (%d)\n",
732 fname, strerror(errno), errno);
737 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
743 while ( fp_getline( fp, c ) ) {
744 /* skip comments and blank lines */
745 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
749 snprintf( c->log, sizeof( c->log ), "%s: line %d",
750 c->fname, c->lineno );
754 if ( fp_parse_line( c ) ) {
760 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
766 c->op = SLAP_CONFIG_ADD;
768 ct = config_find_keyword( cft, c );
770 rc = config_add_vals( ct, c );
773 if ( rc & ARGS_USERLAND ) {
774 /* XXX a usertype would be opaque here */
775 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
776 c->log, c->argv[0], 0);
780 } else if ( rc == ARG_BAD_CONF ) {
785 } else if ( c->bi && !c->be ) {
786 rc = SLAP_CONF_UNKNOWN;
787 if ( c->bi->bi_cf_ocs ) {
788 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
790 rc = config_add_vals( ct, c );
793 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
794 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
799 case SLAP_CONF_UNKNOWN:
800 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
801 "<%s> inside backend info definition.\n",
802 c->log, *c->argv, 0);
809 } else if ( c->be && c->be != frontendDB ) {
810 rc = SLAP_CONF_UNKNOWN;
811 if ( c->be->be_cf_ocs ) {
812 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
814 rc = config_add_vals( ct, c );
817 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
818 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
821 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
823 /* global overlays may need
824 * definitions inside other databases...
826 rc = (*frontendDB->be_config)( frontendDB,
827 c->fname, (int)c->lineno, c->argc, c->argv );
834 case SLAP_CONF_UNKNOWN:
835 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
836 "<%s> inside backend database definition.\n",
837 c->log, *c->argv, 0);
844 } else if ( frontendDB->be_config ) {
845 rc = (*frontendDB->be_config)( frontendDB,
846 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 global database definition.\n",
852 c->log, *c->argv, 0);
861 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
862 "<%s> outside backend info and database definitions.\n",
863 c->log, *c->argv, 0);
879 /* restrictops, allows, disallows, requires, loglevel */
882 verb_to_mask(const char *word, slap_verbmasks *v) {
884 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
885 if(!strcasecmp(word, v[i].word.bv_val)) break;
891 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
893 for(i = 1; i < argc; i++) {
894 j = verb_to_mask(argv[i], v);
895 if(BER_BVISNULL(&v[j].word)) return i;
896 while (!v[j].mask) j--;
902 /* Mask keywords that represent multiple bits should occur before single
903 * bit keywords in the verbmasks array.
906 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
910 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
911 if (!v[i].mask) continue;
912 if (( m & v[i].mask ) == v[i].mask ) {
913 value_add_one( bva, &v[i].word );
924 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
928 assert( *vp == NULL );
930 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
932 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
934 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
935 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
936 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
939 BER_BVZERO( &(*vp)[ i ].word );
945 slap_verbmasks_destroy( slap_verbmasks *v )
951 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
952 ch_free( v[ i ].word.bv_val );
961 slap_verbmasks_append(
965 slap_mask_t *ignore )
970 return LDAP_OPERATIONS_ERROR;
973 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
974 if ( !(*vp)[ i ].mask ) continue;
976 if ( ignore != NULL ) {
979 for ( j = 0; ignore[ j ] != 0; j++ ) {
980 if ( (*vp)[ i ].mask == ignore[ j ] ) {
986 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
987 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
988 /* already set; ignore */
992 return LDAP_TYPE_OR_VALUE_EXISTS;
995 if ( m & (*vp)[ i ].mask ) {
997 return LDAP_CONSTRAINT_VIOLATION;
1002 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1003 ber_dupbv( &(*vp)[ i ].word, v );
1004 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1005 BER_BVZERO( &(*vp)[ i + 1 ].word );
1007 return LDAP_SUCCESS;
1011 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1014 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1015 if ( m == v[i].mask ) {
1026 static slap_verbmasks tlskey[] = {
1027 { BER_BVC("no"), SB_TLS_OFF },
1028 { BER_BVC("yes"), SB_TLS_ON },
1029 { BER_BVC("critical"), SB_TLS_CRITICAL },
1034 static slap_verbmasks methkey[] = {
1035 { BER_BVC("none"), LDAP_AUTH_NONE },
1036 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1037 #ifdef HAVE_CYRUS_SASL
1038 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1043 static slap_cf_aux_table bindkey[] = {
1044 { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1045 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1046 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1047 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1048 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1049 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1050 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1051 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1052 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1054 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1056 #define aux_TLS (bindkey+10) /* beginning of TLS keywords */
1058 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1059 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1060 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1061 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1062 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1063 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1064 #ifdef HAVE_OPENSSL_CRL
1065 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1068 { BER_BVNULL, 0, 0, 0, NULL }
1072 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1074 int rc = SLAP_CONF_UNKNOWN;
1075 slap_cf_aux_table *tab;
1077 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1078 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1083 unsigned long *ulptr;
1084 struct berval *bptr;
1085 const char *val = word + tab->key.bv_len;
1087 switch ( tab->type ) {
1089 cptr = (char **)((char *)dst + tab->off);
1090 *cptr = ch_strdup( val );
1095 bptr = (struct berval *)((char *)dst + tab->off);
1096 ber_str2bv( val, 0, 1, bptr );
1101 assert( tab->aux != NULL );
1102 iptr = (int *)((char *)dst + tab->off);
1105 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1106 if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1107 *iptr = tab->aux[j].mask;
1114 iptr = (int *)((char *)dst + tab->off);
1116 rc = lutil_atoix( iptr, val, 0 );
1120 uptr = (unsigned *)((char *)dst + tab->off);
1122 rc = lutil_atoux( uptr, val, 0 );
1126 lptr = (long *)((char *)dst + tab->off);
1128 rc = lutil_atolx( lptr, val, 0 );
1132 ulptr = (unsigned long *)((char *)dst + tab->off);
1134 rc = lutil_atoulx( ulptr, val, 0 );
1139 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1151 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1153 char buf[BUFSIZ], *ptr;
1154 slap_cf_aux_table *tab;
1158 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1163 unsigned long *ulptr;
1164 struct berval *bptr;
1166 cptr = (char **)((char *)src + tab->off);
1168 switch ( tab->type ) {
1170 bptr = (struct berval *)((char *)src + tab->off);
1171 cptr = &bptr->bv_val;
1175 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1176 if ( tab->quote ) *ptr++ = '"';
1177 ptr = lutil_strcopy( ptr, *cptr );
1178 if ( tab->quote ) *ptr++ = '"';
1183 assert( tab->aux != NULL );
1184 iptr = (int *)((char *)src + tab->off);
1186 for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1187 if ( *iptr == tab->aux[i].mask ) {
1189 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1190 ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1197 iptr = (int *)((char *)src + tab->off);
1199 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1200 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1204 uptr = (unsigned *)((char *)src + tab->off);
1206 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1207 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1211 lptr = (long *)((char *)src + tab->off);
1213 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1214 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1218 ulptr = (unsigned long *)((char *)src + tab->off);
1220 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1221 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1229 tmp.bv_len = ptr - buf;
1230 ber_dupbv( bv, &tmp );
1235 bindconf_parse( const char *word, slap_bindconf *bc )
1238 /* Detect TLS config changes explicitly */
1239 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1240 bc->sb_tls_do_init = 1;
1244 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1248 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1250 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1253 void bindconf_free( slap_bindconf *bc ) {
1254 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1255 ch_free( bc->sb_uri.bv_val );
1256 BER_BVZERO( &bc->sb_uri );
1258 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1259 ch_free( bc->sb_binddn.bv_val );
1260 BER_BVZERO( &bc->sb_binddn );
1262 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1263 ch_free( bc->sb_cred.bv_val );
1264 BER_BVZERO( &bc->sb_cred );
1266 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1267 ch_free( bc->sb_saslmech.bv_val );
1268 BER_BVZERO( &bc->sb_saslmech );
1270 if ( bc->sb_secprops ) {
1271 ch_free( bc->sb_secprops );
1272 bc->sb_secprops = NULL;
1274 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1275 ch_free( bc->sb_realm.bv_val );
1276 BER_BVZERO( &bc->sb_realm );
1278 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1279 ch_free( bc->sb_authcId.bv_val );
1280 BER_BVZERO( &bc->sb_authcId );
1282 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1283 ch_free( bc->sb_authzId.bv_val );
1284 BER_BVZERO( &bc->sb_authzId );
1287 if ( bc->sb_tls_ctx ) {
1288 SSL_CTX_free( bc->sb_tls_ctx );
1289 bc->sb_tls_ctx = NULL;
1291 if ( bc->sb_tls_cert ) {
1292 ch_free( bc->sb_tls_cert );
1293 bc->sb_tls_cert = NULL;
1295 if ( bc->sb_tls_key ) {
1296 ch_free( bc->sb_tls_key );
1297 bc->sb_tls_key = NULL;
1299 if ( bc->sb_tls_cacert ) {
1300 ch_free( bc->sb_tls_cacert );
1301 bc->sb_tls_cacert = NULL;
1303 if ( bc->sb_tls_cacertdir ) {
1304 ch_free( bc->sb_tls_cacertdir );
1305 bc->sb_tls_cacertdir = NULL;
1307 if ( bc->sb_tls_reqcert ) {
1308 ch_free( bc->sb_tls_reqcert );
1309 bc->sb_tls_reqcert = NULL;
1311 if ( bc->sb_tls_cipher_suite ) {
1312 ch_free( bc->sb_tls_cipher_suite );
1313 bc->sb_tls_cipher_suite = NULL;
1315 #ifdef HAVE_OPENSSL_CRL
1316 if ( bc->sb_tls_crlcheck ) {
1317 ch_free( bc->sb_tls_crlcheck );
1318 bc->sb_tls_crlcheck = NULL;
1330 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1331 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1332 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1333 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1334 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1338 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1340 int i, rc, newctx = 0, res = 0;
1341 char *ptr = (char *)bc, **word;
1343 bc->sb_tls_do_init = 0;
1345 for (i=0; bindtlsopts[i].opt; i++) {
1346 word = (char **)(ptr + bindtlsopts[i].offset);
1348 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1350 Debug( LDAP_DEBUG_ANY,
1351 "bindconf_tls_set: failed to set %s to %s\n",
1352 bindtlsopts[i].key, *word, 0 );
1358 if ( bc->sb_tls_reqcert ) {
1359 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1360 bc->sb_tls_reqcert );
1362 Debug( LDAP_DEBUG_ANY,
1363 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1364 bc->sb_tls_reqcert, 0, 0 );
1369 #ifdef HAVE_OPENSSL_CRL
1370 if ( bc->sb_tls_crlcheck ) {
1371 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1372 bc->sb_tls_crlcheck );
1374 Debug( LDAP_DEBUG_ANY,
1375 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1376 bc->sb_tls_crlcheck, 0, 0 );
1385 if ( bc->sb_tls_ctx ) {
1386 SSL_CTX_free( bc->sb_tls_ctx );
1387 bc->sb_tls_ctx = NULL;
1389 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1393 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1400 /* -------------------------------------- */
1404 strtok_quote( char *line, char *sep, char **quote_ptr )
1411 if ( line != NULL ) {
1414 while ( *next && strchr( sep, *next ) ) {
1418 if ( *next == '\0' ) {
1424 for ( inquote = 0; *next; ) {
1432 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1438 next + 1, strlen( next + 1 ) + 1 );
1439 next++; /* dont parse the escaped character */
1444 if ( strchr( sep, *next ) != NULL ) {
1458 static char buf[BUFSIZ];
1460 static size_t lmax, lcur;
1462 #define CATLINE( buf ) \
1464 size_t len = strlen( buf ); \
1465 while ( lcur + len + 1 > lmax ) { \
1467 line = (char *) ch_realloc( line, lmax ); \
1469 strcpy( line + lcur, buf ); \
1474 fp_getline_init(ConfigArgs *c) {
1480 fp_getline( FILE *fp, ConfigArgs *c )
1488 /* avoid stack of bufs */
1489 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1495 while ( fgets( buf, sizeof( buf ), fp ) ) {
1496 p = strchr( buf, '\n' );
1498 if ( p > buf && p[-1] == '\r' ) {
1506 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1513 if ( !isspace( (unsigned char)buf[0] ) ) {
1524 return(line[0] ? 1 : 0);
1528 fp_parse_line(ConfigArgs *c)
1531 static char *const hide[] = {
1532 "rootpw", "replica", "syncrepl", /* in slapd */
1533 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
1534 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
1535 "pseudorootpw", /* in back-meta */
1536 "dbpasswd", /* in back-sql */
1540 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1542 c->tline = ch_strdup(c->line);
1543 token = strtok_quote(c->tline, " \t", "e_ptr);
1545 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1546 if(quote_ptr) *quote_ptr = ' ';
1547 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1548 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1549 if(quote_ptr) *quote_ptr = '\0';
1551 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
1552 if(c->argc >= c->argv_size) {
1554 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1556 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1560 c->argv_size += ARGS_STEP;
1564 c->argv[c->argc++] = token;
1566 c->argv[c->argc] = NULL;
1573 ucdata_unload( UCDATA_ALL );
1575 /* NOTE: in case of early exit, frontendDB can be NULL */
1576 if ( frontendDB->be_schemandn.bv_val )
1577 free( frontendDB->be_schemandn.bv_val );
1578 if ( frontendDB->be_schemadn.bv_val )
1579 free( frontendDB->be_schemadn.bv_val );
1580 if ( frontendDB->be_acl )
1581 acl_destroy( frontendDB->be_acl, NULL );
1584 if ( slapd_args_file )
1585 free ( slapd_args_file );
1586 if ( slapd_pid_file )
1587 free ( slapd_pid_file );
1588 if ( default_passwd_hash )
1589 ldap_charray_free( default_passwd_hash );
1593 slap_str2clist( char ***out, char *in, const char *brkstr )
1601 /* find last element in list */
1602 for (i = 0; *out && (*out)[i]; i++);
1604 /* protect the input string from strtok */
1605 str = ch_strdup( in );
1607 if ( *str == '\0' ) {
1612 /* Count words in string */
1614 for ( s = str; *s; s++ ) {
1615 if ( strchr( brkstr, *s ) != NULL ) {
1620 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1622 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1624 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1626 *new = ch_strdup( s );
1635 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1636 int argc, char **argv )
1638 ConfigArgs c = { 0 };
1649 c.op = SLAP_CONFIG_ADD;
1650 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1652 rc = SLAP_CONF_UNKNOWN;
1653 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1655 rc = config_add_vals( ct, &c );