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"
46 #include "lutil_ldap.h"
50 #include <openssl/ssl.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 char *global_host = NULL;
63 char *global_realm = NULL;
64 char *ldap_srvtab = "";
65 char **default_passwd_hash = NULL;
66 struct berval default_search_base = BER_BVNULL;
67 struct berval default_search_nbase = BER_BVNULL;
69 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
70 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
72 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
73 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
75 char *slapd_pid_file = NULL;
76 char *slapd_args_file = NULL;
78 int use_reverse_lookup = 0;
81 int slapi_plugins_used = 0;
84 static int fp_getline(FILE *fp, ConfigArgs *c);
85 static void fp_getline_init(ConfigArgs *c);
86 static int fp_parse_line(ConfigArgs *c);
88 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
89 static char *strtok_quote_ldif(char **line);
92 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
95 c = ch_calloc( 1, sizeof( ConfigArgs ) );
96 if ( c == NULL ) return(NULL);
102 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
107 init_config_argv( ConfigArgs *c )
109 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
110 c->argv_size = ARGS_STEP + 1;
113 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
116 for(i = 0; Conf[i].name; i++)
117 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
118 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
119 if ( !Conf[i].name ) return NULL;
123 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
124 int rc, arg_user, arg_type, arg_syn, iarg;
128 if(Conf->arg_type == ARG_IGNORED) {
129 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
130 c->log, Conf->name, 0);
133 arg_type = Conf->arg_type & ARGS_TYPES;
134 arg_user = Conf->arg_type & ARGS_USERLAND;
135 arg_syn = Conf->arg_type & ARGS_SYNTAX;
137 if((arg_type == ARG_DN) && c->argc == 1) {
141 if(Conf->min_args && (c->argc < Conf->min_args)) {
142 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
143 c->argv[0], Conf->what );
144 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
145 return(ARG_BAD_CONF);
147 if(Conf->max_args && (c->argc > Conf->max_args)) {
148 char *ignored = " ignored";
150 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
151 c->argv[0], Conf->what );
154 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
155 c->log, c->msg, ignored );
156 return(ARG_BAD_CONF);
158 if((arg_syn & ARG_DB) && !c->be) {
159 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
161 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
163 return(ARG_BAD_CONF);
165 if((arg_syn & ARG_PRE_BI) && c->bi) {
166 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
167 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
168 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
170 return(ARG_BAD_CONF);
172 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
173 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
175 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
177 return(ARG_BAD_CONF);
179 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
180 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
181 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
183 return(ARG_BAD_CONF);
185 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
186 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
188 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
190 return(ARG_BAD_CONF);
193 memset(&c->values, 0, sizeof(c->values));
194 if(arg_type == ARG_STRING) {
196 c->value_string = ch_strdup(c->argv[1]);
197 } else if(arg_type == ARG_BERVAL) {
199 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
200 } else if(arg_type == ARG_DN) {
202 ber_str2bv( c->argv[1], 0, 0, &bv );
203 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
204 if ( rc != LDAP_SUCCESS ) {
205 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
206 c->argv[0], rc, ldap_err2string( rc ));
207 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
208 return(ARG_BAD_CONF);
211 ch_free( c->value_ndn.bv_val );
212 ch_free( c->value_dn.bv_val );
214 } else { /* all numeric */
216 iarg = 0; larg = 0; barg = 0;
219 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
220 snprintf( c->msg, sizeof( c->msg ),
221 "<%s> unable to parse \"%s\" as int",
222 c->argv[0], c->argv[1] );
223 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
225 return(ARG_BAD_CONF);
229 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
230 snprintf( c->msg, sizeof( c->msg ),
231 "<%s> unable to parse \"%s\" as long",
232 c->argv[0], c->argv[1] );
233 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
235 return(ARG_BAD_CONF);
238 case ARG_BER_LEN_T: {
240 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
241 snprintf( c->msg, sizeof( c->msg ),
242 "<%s> unable to parse \"%s\" as ber_len_t",
243 c->argv[0], c->argv[1] );
244 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
246 return(ARG_BAD_CONF);
253 } else if ( !strcasecmp(c->argv[1], "on") ||
254 !strcasecmp(c->argv[1], "true") ||
255 !strcasecmp(c->argv[1], "yes") )
258 } else if ( !strcasecmp(c->argv[1], "off") ||
259 !strcasecmp(c->argv[1], "false") ||
260 !strcasecmp(c->argv[1], "no") )
264 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
266 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
268 return(ARG_BAD_CONF);
272 j = (arg_type & ARG_NONZERO) ? 1 : 0;
273 if(iarg < j && larg < j && barg < j ) {
274 larg = larg ? larg : (barg ? barg : iarg);
275 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
277 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
279 return(ARG_BAD_CONF);
283 case ARG_INT: c->value_int = iarg; break;
284 case ARG_LONG: c->value_long = larg; break;
285 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
291 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
295 arg_type = Conf->arg_type;
296 if(arg_type & ARG_MAGIC) {
297 if(!c->be) c->be = frontendDB;
299 rc = (*((ConfigDriver*)Conf->arg_item))(c);
301 if(c->be == frontendDB) c->be = NULL;
305 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
307 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
310 return(ARG_BAD_CONF);
314 if(arg_type & ARG_OFFSET) {
315 if (c->be && (!overlay_is_over(c->be) ||
316 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
317 ptr = c->be->be_private;
319 ptr = c->bi->bi_private;
321 snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
323 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
325 return(ARG_BAD_CONF);
327 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
328 } else if (arg_type & ARGS_TYPES) {
329 ptr = Conf->arg_item;
331 if(arg_type & ARGS_TYPES)
332 switch(arg_type & ARGS_TYPES) {
334 case ARG_INT: *(int*)ptr = c->value_int; break;
335 case ARG_LONG: *(long*)ptr = c->value_long; break;
336 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
338 char *cc = *(char**)ptr;
340 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
341 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
342 c->log, Conf->name, 0 );
343 return(ARG_BAD_CONF);
347 *(char **)ptr = c->value_string;
351 *(struct berval *)ptr = c->value_bv;
357 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
360 arg_type = Conf->arg_type;
361 if(arg_type == ARG_IGNORED) {
362 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
363 c->log, Conf->name, 0);
366 rc = config_check_vals( Conf, c, 0 );
368 return config_set_vals( Conf, c );
372 config_del_vals(ConfigTable *cf, ConfigArgs *c)
376 /* If there is no handler, just ignore it */
377 if ( cf->arg_type & ARG_MAGIC ) {
378 c->op = LDAP_MOD_DELETE;
379 c->type = cf->arg_type & ARGS_USERLAND;
380 rc = (*((ConfigDriver*)cf->arg_item))(c);
386 config_get_vals(ConfigTable *cf, ConfigArgs *c)
392 if ( cf->arg_type & ARG_IGNORED ) {
396 memset(&c->values, 0, sizeof(c->values));
397 c->rvalue_vals = NULL;
398 c->rvalue_nvals = NULL;
399 c->op = SLAP_CONFIG_EMIT;
400 c->type = cf->arg_type & ARGS_USERLAND;
402 if ( cf->arg_type & ARG_MAGIC ) {
403 rc = (*((ConfigDriver*)cf->arg_item))(c);
406 if ( cf->arg_type & ARG_OFFSET ) {
407 if (c->be && (!overlay_is_over(c->be) ||
408 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
409 ptr = c->be->be_private;
411 ptr = c->bi->bi_private;
414 ptr = (void *)((char *)ptr + (long)cf->arg_item);
419 switch(cf->arg_type & ARGS_TYPES) {
421 case ARG_INT: c->value_int = *(int *)ptr; break;
422 case ARG_LONG: c->value_long = *(long *)ptr; break;
423 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
426 c->value_string = ch_strdup(*(char **)ptr);
429 ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
432 if ( cf->arg_type & ARGS_TYPES) {
435 switch(cf->arg_type & ARGS_TYPES) {
436 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
437 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
438 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
439 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
440 c->value_int ? "TRUE" : "FALSE"); break;
442 if ( c->value_string && c->value_string[0]) {
443 ber_str2bv( c->value_string, 0, 0, &bv);
449 if ( !BER_BVISEMPTY( &c->value_bv )) {
459 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
462 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
463 ber_bvarray_add(&c->rvalue_vals, &bv);
464 } else if ( !BER_BVISNULL( &bv ) ) {
465 value_add_one(&c->rvalue_vals, &bv);
467 /* else: maybe c->rvalue_vals already set? */
473 init_config_attrs(ConfigTable *ct) {
474 LDAPAttributeType *at;
478 for (i=0; ct[i].name; i++ ) {
481 if ( !ct[i].attribute ) continue;
482 at = ldap_str2attributetype( ct[i].attribute,
483 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
485 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
486 ct[i].attribute, ldap_scherr2str(code), err );
490 code = at_add( at, 0, NULL, &err );
492 if ( code == SLAP_SCHERR_ATTR_DUP ) {
496 ldap_attributetype_free( at );
497 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
498 ct[i].attribute, scherr2str(code), err );
502 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
503 if ( freeit || code ) {
504 ldap_attributetype_free( at );
509 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
510 ct[i].attribute, err );
519 init_config_ocs( ConfigOCs *ocs ) {
522 for (i=0;ocs[i].co_def;i++) {
527 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
528 LDAP_SCHEMA_ALLOW_ALL );
530 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
531 ocs[i].co_def, ldap_scherr2str(code), err );
534 code = oc_add(oc,0,NULL,&err);
535 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
536 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
537 ocs[i].co_def, scherr2str(code), err );
538 ldap_objectclass_free(oc);
541 ocs[i].co_oc = oc_find(oc->oc_names[0]);
543 ldap_objectclass_free(oc);
550 /* Split an LDIF line into space-separated tokens. Words may be grouped
551 * by quotes. A quoted string may begin in the middle of a word, but must
552 * end at the end of the word (be followed by whitespace or EOS). Any other
553 * quotes are passed through unchanged. All other characters are passed
557 strtok_quote_ldif( char **line )
559 char *beg, *ptr, *quote=NULL;
567 while( isspace( (unsigned char) *ptr )) ptr++;
578 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
588 if ( isspace( (unsigned char) *ptr )) {
594 while ( quote < ptr ) {
602 while ( isspace( (unsigned char) *ptr )) ptr++;
609 config_parse_ldif( ConfigArgs *c )
612 c->tline = ch_strdup(c->line);
615 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
617 if ( c->argc >= c->argv_size ) {
618 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
621 c->argv_size += ARGS_STEP;
624 c->argv[c->argc] = NULL;
628 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
632 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
633 ct->ad->ad_cname.bv_val, valx );
635 c->argv[0] = ct->ad->ad_cname.bv_val;
637 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
638 c->argv[c->argc] = c->line;
640 c->argv[c->argc] = NULL;
643 config_parse_ldif( c );
645 rc = config_check_vals( ct, c, 1 );
650 rc = LDAP_CONSTRAINT_VIOLATION;
656 config_parse_add(ConfigTable *ct, ConfigArgs *c)
660 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
661 ct->ad->ad_cname.bv_val, c->valx );
663 c->argv[0] = ct->ad->ad_cname.bv_val;
665 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
666 c->argv[c->argc] = c->line;
668 c->argv[c->argc] = NULL;
671 config_parse_ldif( c );
673 c->op = LDAP_MOD_ADD;
674 rc = config_add_vals( ct, c );
681 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
689 c = ch_calloc( 1, sizeof( ConfigArgs ) );
695 memcpy( c, cf, sizeof( ConfigArgs ) );
697 c->depth = depth; /* XXX */
704 init_config_argv( c );
706 if ( stat( fname, &s ) != 0 ) {
708 Debug(LDAP_DEBUG_ANY,
709 "could not stat config file \"%s\": %s (%d)\n",
710 fname, strerror(errno), errno);
715 if ( !S_ISREG( s.st_mode ) ) {
717 Debug(LDAP_DEBUG_ANY,
718 "regular file expected, got \"%s\"\n",
724 fp = fopen( fname, "r" );
727 Debug(LDAP_DEBUG_ANY,
728 "could not open config file \"%s\": %s (%d)\n",
729 fname, strerror(errno), errno);
734 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
740 while ( fp_getline( fp, c ) ) {
741 /* skip comments and blank lines */
742 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
746 snprintf( c->log, sizeof( c->log ), "%s: line %d",
747 c->fname, c->lineno );
751 if ( fp_parse_line( c ) ) {
757 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
763 c->op = SLAP_CONFIG_ADD;
765 ct = config_find_keyword( cft, c );
767 rc = config_add_vals( ct, c );
770 if ( rc & ARGS_USERLAND ) {
771 /* XXX a usertype would be opaque here */
772 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
773 c->log, c->argv[0], 0);
777 } else if ( rc == ARG_BAD_CONF ) {
782 } else if ( c->bi && !c->be ) {
783 rc = SLAP_CONF_UNKNOWN;
784 if ( c->bi->bi_cf_ocs ) {
785 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
787 rc = config_add_vals( ct, c );
790 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
791 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
796 case SLAP_CONF_UNKNOWN:
797 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
798 "<%s> inside backend info definition.\n",
799 c->log, *c->argv, 0);
806 } else if ( c->be && c->be != frontendDB ) {
807 rc = SLAP_CONF_UNKNOWN;
808 if ( c->be->be_cf_ocs ) {
809 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
811 rc = config_add_vals( ct, c );
814 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
815 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
818 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
820 /* global overlays may need
821 * definitions inside other databases...
823 rc = (*frontendDB->be_config)( frontendDB,
824 c->fname, (int)c->lineno, c->argc, c->argv );
831 case SLAP_CONF_UNKNOWN:
832 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
833 "<%s> inside backend database definition.\n",
834 c->log, *c->argv, 0);
841 } else if ( frontendDB->be_config ) {
842 rc = (*frontendDB->be_config)( frontendDB,
843 c->fname, (int)c->lineno, c->argc, c->argv);
846 case SLAP_CONF_UNKNOWN:
847 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
848 "<%s> inside global database definition.\n",
849 c->log, *c->argv, 0);
858 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
859 "<%s> outside backend info and database definitions.\n",
860 c->log, *c->argv, 0);
876 /* restrictops, allows, disallows, requires, loglevel */
879 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
881 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
882 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
888 verb_to_mask(const char *word, slap_verbmasks *v) {
890 ber_str2bv( word, 0, 0, &bword );
891 return bverb_to_mask( &bword, v );
895 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
897 for(i = 1; i < argc; i++) {
898 j = verb_to_mask(argv[i], v);
899 if(BER_BVISNULL(&v[j].word)) return i;
900 while (!v[j].mask) j--;
906 /* Mask keywords that represent multiple bits should occur before single
907 * bit keywords in the verbmasks array.
910 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
914 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
915 if (!v[i].mask) continue;
916 if (( m & v[i].mask ) == v[i].mask ) {
917 value_add_one( bva, &v[i].word );
928 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
932 assert( *vp == NULL );
934 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
936 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
938 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
939 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
940 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
943 BER_BVZERO( &(*vp)[ i ].word );
949 slap_verbmasks_destroy( slap_verbmasks *v )
955 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
956 ch_free( v[ i ].word.bv_val );
965 slap_verbmasks_append(
969 slap_mask_t *ignore )
974 return LDAP_OPERATIONS_ERROR;
977 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
978 if ( !(*vp)[ i ].mask ) continue;
980 if ( ignore != NULL ) {
983 for ( j = 0; ignore[ j ] != 0; j++ ) {
984 if ( (*vp)[ i ].mask == ignore[ j ] ) {
990 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
991 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
992 /* already set; ignore */
996 return LDAP_TYPE_OR_VALUE_EXISTS;
999 if ( m & (*vp)[ i ].mask ) {
1001 return LDAP_CONSTRAINT_VIOLATION;
1006 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1007 ber_dupbv( &(*vp)[ i ].word, v );
1008 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1009 BER_BVZERO( &(*vp)[ i + 1 ].word );
1011 return LDAP_SUCCESS;
1015 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1018 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1019 if ( m == v[i].mask ) {
1030 static slap_verbmasks tlskey[] = {
1031 { BER_BVC("no"), SB_TLS_OFF },
1032 { BER_BVC("yes"), SB_TLS_ON },
1033 { BER_BVC("critical"), SB_TLS_CRITICAL },
1038 static slap_verbmasks methkey[] = {
1039 { BER_BVC("none"), LDAP_AUTH_NONE },
1040 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1041 #ifdef HAVE_CYRUS_SASL
1042 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1047 static slap_cf_aux_table bindkey[] = {
1048 { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1049 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1050 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1051 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1052 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1053 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1054 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1055 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1056 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1058 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1060 #define aux_TLS (bindkey+10) /* beginning of TLS keywords */
1062 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1063 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1064 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1065 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1066 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1067 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1068 #ifdef HAVE_OPENSSL_CRL
1069 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1072 { BER_BVNULL, 0, 0, 0, NULL }
1076 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1078 int rc = SLAP_CONF_UNKNOWN;
1079 slap_cf_aux_table *tab;
1081 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1082 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1087 unsigned long *ulptr;
1088 struct berval *bptr;
1089 const char *val = word + tab->key.bv_len;
1091 switch ( tab->type ) {
1093 cptr = (char **)((char *)dst + tab->off);
1094 *cptr = ch_strdup( val );
1099 bptr = (struct berval *)((char *)dst + tab->off);
1100 ber_str2bv( val, 0, 1, bptr );
1105 assert( tab->aux != NULL );
1106 iptr = (int *)((char *)dst + tab->off);
1109 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1110 if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1111 *iptr = tab->aux[j].mask;
1118 iptr = (int *)((char *)dst + tab->off);
1120 rc = lutil_atoix( iptr, val, 0 );
1124 uptr = (unsigned *)((char *)dst + tab->off);
1126 rc = lutil_atoux( uptr, val, 0 );
1130 lptr = (long *)((char *)dst + tab->off);
1132 rc = lutil_atolx( lptr, val, 0 );
1136 ulptr = (unsigned long *)((char *)dst + tab->off);
1138 rc = lutil_atoulx( ulptr, val, 0 );
1143 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1155 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1157 char buf[AC_LINE_MAX], *ptr;
1158 slap_cf_aux_table *tab;
1162 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1167 unsigned long *ulptr;
1168 struct berval *bptr;
1170 cptr = (char **)((char *)src + tab->off);
1172 switch ( tab->type ) {
1174 bptr = (struct berval *)((char *)src + tab->off);
1175 cptr = &bptr->bv_val;
1179 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1180 if ( tab->quote ) *ptr++ = '"';
1181 ptr = lutil_strcopy( ptr, *cptr );
1182 if ( tab->quote ) *ptr++ = '"';
1187 assert( tab->aux != NULL );
1188 iptr = (int *)((char *)src + tab->off);
1190 for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1191 if ( *iptr == tab->aux[i].mask ) {
1193 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1194 ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1201 iptr = (int *)((char *)src + tab->off);
1203 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1204 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1208 uptr = (unsigned *)((char *)src + tab->off);
1210 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1211 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1215 lptr = (long *)((char *)src + tab->off);
1217 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1218 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1222 ulptr = (unsigned long *)((char *)src + tab->off);
1224 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1225 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1233 tmp.bv_len = ptr - buf;
1234 ber_dupbv( bv, &tmp );
1239 bindconf_parse( const char *word, slap_bindconf *bc )
1242 /* Detect TLS config changes explicitly */
1243 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1244 bc->sb_tls_do_init = 1;
1248 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1252 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1254 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1257 void bindconf_free( slap_bindconf *bc ) {
1258 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1259 ch_free( bc->sb_uri.bv_val );
1260 BER_BVZERO( &bc->sb_uri );
1262 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1263 ch_free( bc->sb_binddn.bv_val );
1264 BER_BVZERO( &bc->sb_binddn );
1266 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1267 ch_free( bc->sb_cred.bv_val );
1268 BER_BVZERO( &bc->sb_cred );
1270 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1271 ch_free( bc->sb_saslmech.bv_val );
1272 BER_BVZERO( &bc->sb_saslmech );
1274 if ( bc->sb_secprops ) {
1275 ch_free( bc->sb_secprops );
1276 bc->sb_secprops = NULL;
1278 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1279 ch_free( bc->sb_realm.bv_val );
1280 BER_BVZERO( &bc->sb_realm );
1282 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1283 ch_free( bc->sb_authcId.bv_val );
1284 BER_BVZERO( &bc->sb_authcId );
1286 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1287 ch_free( bc->sb_authzId.bv_val );
1288 BER_BVZERO( &bc->sb_authzId );
1291 if ( bc->sb_tls_ctx ) {
1292 SSL_CTX_free( bc->sb_tls_ctx );
1293 bc->sb_tls_ctx = NULL;
1295 if ( bc->sb_tls_cert ) {
1296 ch_free( bc->sb_tls_cert );
1297 bc->sb_tls_cert = NULL;
1299 if ( bc->sb_tls_key ) {
1300 ch_free( bc->sb_tls_key );
1301 bc->sb_tls_key = NULL;
1303 if ( bc->sb_tls_cacert ) {
1304 ch_free( bc->sb_tls_cacert );
1305 bc->sb_tls_cacert = NULL;
1307 if ( bc->sb_tls_cacertdir ) {
1308 ch_free( bc->sb_tls_cacertdir );
1309 bc->sb_tls_cacertdir = NULL;
1311 if ( bc->sb_tls_reqcert ) {
1312 ch_free( bc->sb_tls_reqcert );
1313 bc->sb_tls_reqcert = NULL;
1315 if ( bc->sb_tls_cipher_suite ) {
1316 ch_free( bc->sb_tls_cipher_suite );
1317 bc->sb_tls_cipher_suite = NULL;
1319 #ifdef HAVE_OPENSSL_CRL
1320 if ( bc->sb_tls_crlcheck ) {
1321 ch_free( bc->sb_tls_crlcheck );
1322 bc->sb_tls_crlcheck = NULL;
1334 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1335 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1336 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1337 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1338 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1342 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1344 int i, rc, newctx = 0, res = 0;
1345 char *ptr = (char *)bc, **word;
1347 bc->sb_tls_do_init = 0;
1349 for (i=0; bindtlsopts[i].opt; i++) {
1350 word = (char **)(ptr + bindtlsopts[i].offset);
1352 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1354 Debug( LDAP_DEBUG_ANY,
1355 "bindconf_tls_set: failed to set %s to %s\n",
1356 bindtlsopts[i].key, *word, 0 );
1362 if ( bc->sb_tls_reqcert ) {
1363 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1364 bc->sb_tls_reqcert );
1366 Debug( LDAP_DEBUG_ANY,
1367 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1368 bc->sb_tls_reqcert, 0, 0 );
1373 #ifdef HAVE_OPENSSL_CRL
1374 if ( bc->sb_tls_crlcheck ) {
1375 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1376 bc->sb_tls_crlcheck );
1378 Debug( LDAP_DEBUG_ANY,
1379 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1380 bc->sb_tls_crlcheck, 0, 0 );
1389 if ( bc->sb_tls_ctx ) {
1390 SSL_CTX_free( bc->sb_tls_ctx );
1391 bc->sb_tls_ctx = NULL;
1393 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1397 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1405 * connect to a client using the bindconf data
1406 * note: should move "version" into bindconf...
1409 slap_client_connect( LDAP **ldp, slap_bindconf *sb, int version )
1414 /* Init connection to master */
1415 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1416 if ( rc != LDAP_SUCCESS ) {
1417 Debug( LDAP_DEBUG_ANY,
1418 "slap_client_connect: "
1419 "ldap_initialize(%s) failed (%d)\n",
1420 sb->sb_uri.bv_val, rc, 0 );
1424 if ( version != 0 ) {
1425 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1426 (const void *)&version );
1430 if ( sb->sb_tls_do_init ) {
1431 rc = bindconf_tls_set( sb, ld );
1433 } else if ( sb->sb_tls_ctx ) {
1434 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1439 Debug( LDAP_DEBUG_ANY,
1440 "slap_client_connect: "
1441 "URI=%s TLS context initialization failed (%d)\n",
1442 sb->sb_uri.bv_val, rc, 0 );
1449 rc = ldap_start_tls_s( ld, NULL, NULL );
1450 if ( rc != LDAP_SUCCESS ) {
1451 Debug( LDAP_DEBUG_ANY,
1452 "slap_client_connect: URI=%s "
1453 "%s, ldap_start_tls failed (%d)\n",
1455 sb->sb_tls == SB_TLS_CRITICAL ?
1456 "Error" : "Warning",
1458 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1464 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1465 #ifdef HAVE_CYRUS_SASL
1468 if ( sb->sb_secprops != NULL ) {
1469 rc = ldap_set_option( ld,
1470 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1472 if( rc != LDAP_OPT_SUCCESS ) {
1473 Debug( LDAP_DEBUG_ANY,
1474 "slap_client_connect: "
1475 "error, ldap_set_option "
1476 "(%s,SECPROPS,\"%s\") failed!\n",
1477 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1482 defaults = lutil_sasl_defaults( ld,
1483 sb->sb_saslmech.bv_val,
1484 sb->sb_realm.bv_val,
1485 sb->sb_authcId.bv_val,
1487 sb->sb_authzId.bv_val );
1489 rc = ldap_sasl_interactive_bind_s( ld,
1490 sb->sb_binddn.bv_val,
1491 sb->sb_saslmech.bv_val,
1494 lutil_sasl_interact,
1497 lutil_sasl_freedefs( defaults );
1499 /* FIXME: different error behaviors according to
1501 * 2) on err policy : exit, retry, backoff ...
1503 if ( rc != LDAP_SUCCESS ) {
1504 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1506 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1507 "ldap_sasl_interactive_bind_s failed (%d)\n",
1508 sb->sb_uri.bv_val, rc, 0 );
1510 /* FIXME (see above comment) */
1511 /* if Kerberos credentials cache is not active, retry */
1512 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1513 rc == LDAP_LOCAL_ERROR )
1515 rc = LDAP_SERVER_DOWN;
1520 #else /* HAVE_CYRUS_SASL */
1521 /* Should never get here, we trapped this at config time */
1523 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1528 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1529 rc = ldap_sasl_bind_s( ld,
1530 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1531 &sb->sb_cred, NULL, NULL, NULL );
1532 if ( rc != LDAP_SUCCESS ) {
1533 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1535 "ldap_sasl_bind_s failed (%d)\n",
1536 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1544 ldap_unbind_ext( ld, NULL, NULL );
1555 /* -------------------------------------- */
1559 strtok_quote( char *line, char *sep, char **quote_ptr )
1566 if ( line != NULL ) {
1569 while ( *next && strchr( sep, *next ) ) {
1573 if ( *next == '\0' ) {
1579 for ( inquote = 0; *next; ) {
1587 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1593 next + 1, strlen( next + 1 ) + 1 );
1594 next++; /* dont parse the escaped character */
1599 if ( strchr( sep, *next ) != NULL ) {
1613 static char buf[AC_LINE_MAX];
1615 static size_t lmax, lcur;
1617 #define CATLINE( buf ) \
1619 size_t len = strlen( buf ); \
1620 while ( lcur + len + 1 > lmax ) { \
1621 lmax += AC_LINE_MAX; \
1622 line = (char *) ch_realloc( line, lmax ); \
1624 strcpy( line + lcur, buf ); \
1629 fp_getline_init(ConfigArgs *c) {
1635 fp_getline( FILE *fp, ConfigArgs *c )
1643 /* avoid stack of bufs */
1644 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1650 while ( fgets( buf, sizeof( buf ), fp ) ) {
1651 p = strchr( buf, '\n' );
1653 if ( p > buf && p[-1] == '\r' ) {
1661 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1668 if ( !isspace( (unsigned char)buf[0] ) ) {
1679 return(line[0] ? 1 : 0);
1683 fp_parse_line(ConfigArgs *c)
1686 static char *const hide[] = {
1687 "rootpw", "replica", "syncrepl", /* in slapd */
1688 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
1689 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
1690 "pseudorootpw", /* in back-meta */
1691 "dbpasswd", /* in back-sql */
1695 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1697 c->tline = ch_strdup(c->line);
1698 token = strtok_quote(c->tline, " \t", "e_ptr);
1700 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1701 if(quote_ptr) *quote_ptr = ' ';
1702 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1703 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1704 if(quote_ptr) *quote_ptr = '\0';
1706 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
1707 if(c->argc >= c->argv_size) {
1709 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1711 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1715 c->argv_size += ARGS_STEP;
1719 c->argv[c->argc++] = token;
1721 c->argv[c->argc] = NULL;
1728 ucdata_unload( UCDATA_ALL );
1730 /* NOTE: in case of early exit, frontendDB can be NULL */
1731 if ( frontendDB->be_schemandn.bv_val )
1732 free( frontendDB->be_schemandn.bv_val );
1733 if ( frontendDB->be_schemadn.bv_val )
1734 free( frontendDB->be_schemadn.bv_val );
1735 if ( frontendDB->be_acl )
1736 acl_destroy( frontendDB->be_acl, NULL );
1739 if ( slapd_args_file )
1740 free ( slapd_args_file );
1741 if ( slapd_pid_file )
1742 free ( slapd_pid_file );
1743 if ( default_passwd_hash )
1744 ldap_charray_free( default_passwd_hash );
1748 slap_str2clist( char ***out, char *in, const char *brkstr )
1756 /* find last element in list */
1757 for (i = 0; *out && (*out)[i]; i++);
1759 /* protect the input string from strtok */
1760 str = ch_strdup( in );
1762 if ( *str == '\0' ) {
1767 /* Count words in string */
1769 for ( s = str; *s; s++ ) {
1770 if ( strchr( brkstr, *s ) != NULL ) {
1775 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1777 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1779 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1781 *new = ch_strdup( s );
1790 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1791 int argc, char **argv )
1793 ConfigArgs c = { 0 };
1804 c.op = SLAP_CONFIG_ADD;
1805 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1807 rc = SLAP_CONF_UNKNOWN;
1808 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1810 rc = config_add_vals( ct, &c );