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 );
155 #endif /* LDAP_DEVEL */
156 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
157 c->log, c->msg, ignored );
159 return(ARG_BAD_CONF);
160 #endif /* LDAP_DEVEL */
162 if((arg_syn & ARG_DB) && !c->be) {
163 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
165 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
167 return(ARG_BAD_CONF);
169 if((arg_syn & ARG_PRE_BI) && c->bi) {
170 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
171 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
172 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
174 return(ARG_BAD_CONF);
176 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
177 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
179 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
181 return(ARG_BAD_CONF);
183 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
184 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
185 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
187 return(ARG_BAD_CONF);
189 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
190 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
192 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
194 return(ARG_BAD_CONF);
197 memset(&c->values, 0, sizeof(c->values));
198 if(arg_type == ARG_STRING) {
200 c->value_string = ch_strdup(c->argv[1]);
201 } else if(arg_type == ARG_BERVAL) {
203 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
204 } else if(arg_type == ARG_DN) {
206 ber_str2bv( c->argv[1], 0, 0, &bv );
207 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
208 if ( rc != LDAP_SUCCESS ) {
209 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
210 c->argv[0], rc, ldap_err2string( rc ));
211 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
212 return(ARG_BAD_CONF);
215 ch_free( c->value_ndn.bv_val );
216 ch_free( c->value_dn.bv_val );
218 } else { /* all numeric */
220 iarg = 0; larg = 0; barg = 0;
223 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
224 snprintf( c->msg, sizeof( c->msg ),
225 "<%s> unable to parse \"%s\" as int",
226 c->argv[0], c->argv[1] );
227 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
229 return(ARG_BAD_CONF);
233 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
234 snprintf( c->msg, sizeof( c->msg ),
235 "<%s> unable to parse \"%s\" as long",
236 c->argv[0], c->argv[1] );
237 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
239 return(ARG_BAD_CONF);
242 case ARG_BER_LEN_T: {
244 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
245 snprintf( c->msg, sizeof( c->msg ),
246 "<%s> unable to parse \"%s\" as ber_len_t",
247 c->argv[0], c->argv[1] );
248 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
250 return(ARG_BAD_CONF);
257 } else if ( !strcasecmp(c->argv[1], "on") ||
258 !strcasecmp(c->argv[1], "true") ||
259 !strcasecmp(c->argv[1], "yes") )
262 } else if ( !strcasecmp(c->argv[1], "off") ||
263 !strcasecmp(c->argv[1], "false") ||
264 !strcasecmp(c->argv[1], "no") )
268 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
270 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
272 return(ARG_BAD_CONF);
276 j = (arg_type & ARG_NONZERO) ? 1 : 0;
277 if(iarg < j && larg < j && barg < j ) {
278 larg = larg ? larg : (barg ? barg : iarg);
279 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
281 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
283 return(ARG_BAD_CONF);
287 case ARG_INT: c->value_int = iarg; break;
288 case ARG_LONG: c->value_long = larg; break;
289 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
295 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
299 arg_type = Conf->arg_type;
300 if(arg_type & ARG_MAGIC) {
301 if(!c->be) c->be = frontendDB;
303 rc = (*((ConfigDriver*)Conf->arg_item))(c);
305 if(c->be == frontendDB) c->be = NULL;
309 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
311 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
314 return(ARG_BAD_CONF);
318 if(arg_type & ARG_OFFSET) {
319 if (c->be && (!overlay_is_over(c->be) ||
320 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
321 ptr = c->be->be_private;
323 ptr = c->bi->bi_private;
325 snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
327 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
329 return(ARG_BAD_CONF);
331 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
332 } else if (arg_type & ARGS_TYPES) {
333 ptr = Conf->arg_item;
335 if(arg_type & ARGS_TYPES)
336 switch(arg_type & ARGS_TYPES) {
338 case ARG_INT: *(int*)ptr = c->value_int; break;
339 case ARG_LONG: *(long*)ptr = c->value_long; break;
340 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
342 char *cc = *(char**)ptr;
344 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
345 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
346 c->log, Conf->name, 0 );
347 return(ARG_BAD_CONF);
351 *(char **)ptr = c->value_string;
355 *(struct berval *)ptr = c->value_bv;
361 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
364 arg_type = Conf->arg_type;
365 if(arg_type == ARG_IGNORED) {
366 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
367 c->log, Conf->name, 0);
370 rc = config_check_vals( Conf, c, 0 );
372 return config_set_vals( Conf, c );
376 config_del_vals(ConfigTable *cf, ConfigArgs *c)
380 /* If there is no handler, just ignore it */
381 if ( cf->arg_type & ARG_MAGIC ) {
382 c->op = LDAP_MOD_DELETE;
383 c->type = cf->arg_type & ARGS_USERLAND;
384 rc = (*((ConfigDriver*)cf->arg_item))(c);
390 config_get_vals(ConfigTable *cf, ConfigArgs *c)
396 if ( cf->arg_type & ARG_IGNORED ) {
400 memset(&c->values, 0, sizeof(c->values));
401 c->rvalue_vals = NULL;
402 c->rvalue_nvals = NULL;
403 c->op = SLAP_CONFIG_EMIT;
404 c->type = cf->arg_type & ARGS_USERLAND;
406 if ( cf->arg_type & ARG_MAGIC ) {
407 rc = (*((ConfigDriver*)cf->arg_item))(c);
410 if ( cf->arg_type & ARG_OFFSET ) {
411 if (c->be && (!overlay_is_over(c->be) ||
412 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
413 ptr = c->be->be_private;
415 ptr = c->bi->bi_private;
418 ptr = (void *)((char *)ptr + (long)cf->arg_item);
423 switch(cf->arg_type & ARGS_TYPES) {
425 case ARG_INT: c->value_int = *(int *)ptr; break;
426 case ARG_LONG: c->value_long = *(long *)ptr; break;
427 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
430 c->value_string = ch_strdup(*(char **)ptr);
433 ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
436 if ( cf->arg_type & ARGS_TYPES) {
439 switch(cf->arg_type & ARGS_TYPES) {
440 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
441 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
442 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
443 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
444 c->value_int ? "TRUE" : "FALSE"); break;
446 if ( c->value_string && c->value_string[0]) {
447 ber_str2bv( c->value_string, 0, 0, &bv);
453 if ( !BER_BVISEMPTY( &c->value_bv )) {
463 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
466 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
467 ber_bvarray_add(&c->rvalue_vals, &bv);
468 } else if ( !BER_BVISNULL( &bv ) ) {
469 value_add_one(&c->rvalue_vals, &bv);
471 /* else: maybe c->rvalue_vals already set? */
477 init_config_attrs(ConfigTable *ct) {
478 LDAPAttributeType *at;
482 for (i=0; ct[i].name; i++ ) {
485 if ( !ct[i].attribute ) continue;
486 at = ldap_str2attributetype( ct[i].attribute,
487 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
489 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
490 ct[i].attribute, ldap_scherr2str(code), err );
494 code = at_add( at, 0, NULL, &err );
496 if ( code == SLAP_SCHERR_ATTR_DUP ) {
500 ldap_attributetype_free( at );
501 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
502 ct[i].attribute, scherr2str(code), err );
506 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
507 if ( freeit || code ) {
508 ldap_attributetype_free( at );
513 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
514 ct[i].attribute, err );
523 init_config_ocs( ConfigOCs *ocs ) {
526 for (i=0;ocs[i].co_def;i++) {
531 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
532 LDAP_SCHEMA_ALLOW_ALL );
534 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
535 ocs[i].co_def, ldap_scherr2str(code), err );
538 code = oc_add(oc,0,NULL,&err);
539 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
540 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
541 ocs[i].co_def, scherr2str(code), err );
542 ldap_objectclass_free(oc);
545 ocs[i].co_oc = oc_find(oc->oc_names[0]);
547 ldap_objectclass_free(oc);
554 /* Split an LDIF line into space-separated tokens. Words may be grouped
555 * by quotes. A quoted string may begin in the middle of a word, but must
556 * end at the end of the word (be followed by whitespace or EOS). Any other
557 * quotes are passed through unchanged. All other characters are passed
561 strtok_quote_ldif( char **line )
563 char *beg, *ptr, *quote=NULL;
571 while( isspace( (unsigned char) *ptr )) ptr++;
582 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
592 if ( isspace( (unsigned char) *ptr )) {
598 while ( quote < ptr ) {
606 while ( isspace( (unsigned char) *ptr )) ptr++;
613 config_parse_ldif( ConfigArgs *c )
616 c->tline = ch_strdup(c->line);
619 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
621 if ( c->argc >= c->argv_size ) {
622 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
625 c->argv_size += ARGS_STEP;
628 c->argv[c->argc] = NULL;
632 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
636 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
637 ct->ad->ad_cname.bv_val, valx );
639 c->argv[0] = ct->ad->ad_cname.bv_val;
641 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
642 c->argv[c->argc] = c->line;
644 c->argv[c->argc] = NULL;
647 config_parse_ldif( c );
649 rc = config_check_vals( ct, c, 1 );
654 rc = LDAP_CONSTRAINT_VIOLATION;
660 config_parse_add(ConfigTable *ct, ConfigArgs *c)
664 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
665 ct->ad->ad_cname.bv_val, c->valx );
667 c->argv[0] = ct->ad->ad_cname.bv_val;
669 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
670 c->argv[c->argc] = c->line;
672 c->argv[c->argc] = NULL;
675 config_parse_ldif( c );
677 c->op = LDAP_MOD_ADD;
678 rc = config_add_vals( ct, c );
685 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
693 c = ch_calloc( 1, sizeof( ConfigArgs ) );
699 memcpy( c, cf, sizeof( ConfigArgs ) );
701 c->depth = depth; /* XXX */
708 init_config_argv( c );
710 if ( stat( fname, &s ) != 0 ) {
712 Debug(LDAP_DEBUG_ANY,
713 "could not stat config file \"%s\": %s (%d)\n",
714 fname, strerror(errno), errno);
719 if ( !S_ISREG( s.st_mode ) ) {
721 Debug(LDAP_DEBUG_ANY,
722 "regular file expected, got \"%s\"\n",
728 fp = fopen( fname, "r" );
731 Debug(LDAP_DEBUG_ANY,
732 "could not open config file \"%s\": %s (%d)\n",
733 fname, strerror(errno), errno);
738 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
744 while ( fp_getline( fp, c ) ) {
745 /* skip comments and blank lines */
746 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
750 snprintf( c->log, sizeof( c->log ), "%s: line %d",
751 c->fname, c->lineno );
755 if ( fp_parse_line( c ) ) {
761 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
767 c->op = SLAP_CONFIG_ADD;
769 ct = config_find_keyword( cft, c );
771 rc = config_add_vals( ct, c );
774 if ( rc & ARGS_USERLAND ) {
775 /* XXX a usertype would be opaque here */
776 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
777 c->log, c->argv[0], 0);
781 } else if ( rc == ARG_BAD_CONF ) {
786 } else if ( c->bi && !c->be ) {
787 rc = SLAP_CONF_UNKNOWN;
788 if ( c->bi->bi_cf_ocs ) {
789 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
791 rc = config_add_vals( ct, c );
794 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
795 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
800 case SLAP_CONF_UNKNOWN:
801 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
802 "<%s> inside backend info definition.\n",
803 c->log, *c->argv, 0);
810 } else if ( c->be && c->be != frontendDB ) {
811 rc = SLAP_CONF_UNKNOWN;
812 if ( c->be->be_cf_ocs ) {
813 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
815 rc = config_add_vals( ct, c );
818 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
819 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
822 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
824 /* global overlays may need
825 * definitions inside other databases...
827 rc = (*frontendDB->be_config)( frontendDB,
828 c->fname, (int)c->lineno, c->argc, c->argv );
835 case SLAP_CONF_UNKNOWN:
836 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
837 "<%s> inside backend database definition.\n",
838 c->log, *c->argv, 0);
845 } else if ( frontendDB->be_config ) {
846 rc = (*frontendDB->be_config)( frontendDB,
847 c->fname, (int)c->lineno, c->argc, c->argv);
850 case SLAP_CONF_UNKNOWN:
851 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
852 "<%s> inside global database definition.\n",
853 c->log, *c->argv, 0);
862 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
863 "<%s> outside backend info and database definitions.\n",
864 c->log, *c->argv, 0);
880 /* restrictops, allows, disallows, requires, loglevel */
883 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
885 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
886 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
892 verb_to_mask(const char *word, slap_verbmasks *v) {
894 ber_str2bv( word, 0, 0, &bword );
895 return bverb_to_mask( &bword, v );
899 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
901 for(i = 1; i < argc; i++) {
902 j = verb_to_mask(argv[i], v);
903 if(BER_BVISNULL(&v[j].word)) return i;
904 while (!v[j].mask) j--;
910 /* Mask keywords that represent multiple bits should occur before single
911 * bit keywords in the verbmasks array.
914 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
918 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
919 if (!v[i].mask) continue;
920 if (( m & v[i].mask ) == v[i].mask ) {
921 value_add_one( bva, &v[i].word );
932 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
936 assert( *vp == NULL );
938 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
940 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
942 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
943 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
944 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
947 BER_BVZERO( &(*vp)[ i ].word );
953 slap_verbmasks_destroy( slap_verbmasks *v )
959 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
960 ch_free( v[ i ].word.bv_val );
969 slap_verbmasks_append(
973 slap_mask_t *ignore )
978 return LDAP_OPERATIONS_ERROR;
981 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
982 if ( !(*vp)[ i ].mask ) continue;
984 if ( ignore != NULL ) {
987 for ( j = 0; ignore[ j ] != 0; j++ ) {
988 if ( (*vp)[ i ].mask == ignore[ j ] ) {
994 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
995 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
996 /* already set; ignore */
1000 return LDAP_TYPE_OR_VALUE_EXISTS;
1003 if ( m & (*vp)[ i ].mask ) {
1005 return LDAP_CONSTRAINT_VIOLATION;
1010 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1011 ber_dupbv( &(*vp)[ i ].word, v );
1012 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1013 BER_BVZERO( &(*vp)[ i + 1 ].word );
1015 return LDAP_SUCCESS;
1019 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1022 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1023 if ( m == v[i].mask ) {
1034 static slap_verbmasks tlskey[] = {
1035 { BER_BVC("no"), SB_TLS_OFF },
1036 { BER_BVC("yes"), SB_TLS_ON },
1037 { BER_BVC("critical"), SB_TLS_CRITICAL },
1042 static slap_verbmasks methkey[] = {
1043 { BER_BVC("none"), LDAP_AUTH_NONE },
1044 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1045 #ifdef HAVE_CYRUS_SASL
1046 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1051 static slap_cf_aux_table bindkey[] = {
1052 { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1053 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1054 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1055 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1056 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1057 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1058 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1059 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1060 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1062 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1064 #define aux_TLS (bindkey+10) /* beginning of TLS keywords */
1066 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1067 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1068 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1069 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1070 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1071 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1072 #ifdef HAVE_OPENSSL_CRL
1073 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1076 { BER_BVNULL, 0, 0, 0, NULL }
1080 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1082 int rc = SLAP_CONF_UNKNOWN;
1083 slap_cf_aux_table *tab;
1085 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1086 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1091 unsigned long *ulptr;
1092 struct berval *bptr;
1093 const char *val = word + tab->key.bv_len;
1095 switch ( tab->type ) {
1097 cptr = (char **)((char *)dst + tab->off);
1098 *cptr = ch_strdup( val );
1103 bptr = (struct berval *)((char *)dst + tab->off);
1104 ber_str2bv( val, 0, 1, bptr );
1109 assert( tab->aux != NULL );
1110 iptr = (int *)((char *)dst + tab->off);
1113 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1114 if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1115 *iptr = tab->aux[j].mask;
1122 iptr = (int *)((char *)dst + tab->off);
1124 rc = lutil_atoix( iptr, val, 0 );
1128 uptr = (unsigned *)((char *)dst + tab->off);
1130 rc = lutil_atoux( uptr, val, 0 );
1134 lptr = (long *)((char *)dst + tab->off);
1136 rc = lutil_atolx( lptr, val, 0 );
1140 ulptr = (unsigned long *)((char *)dst + tab->off);
1142 rc = lutil_atoulx( ulptr, val, 0 );
1147 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1159 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1161 char buf[BUFSIZ], *ptr;
1162 slap_cf_aux_table *tab;
1166 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1171 unsigned long *ulptr;
1172 struct berval *bptr;
1174 cptr = (char **)((char *)src + tab->off);
1176 switch ( tab->type ) {
1178 bptr = (struct berval *)((char *)src + tab->off);
1179 cptr = &bptr->bv_val;
1183 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1184 if ( tab->quote ) *ptr++ = '"';
1185 ptr = lutil_strcopy( ptr, *cptr );
1186 if ( tab->quote ) *ptr++ = '"';
1191 assert( tab->aux != NULL );
1192 iptr = (int *)((char *)src + tab->off);
1194 for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1195 if ( *iptr == tab->aux[i].mask ) {
1197 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1198 ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1205 iptr = (int *)((char *)src + tab->off);
1207 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1208 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1212 uptr = (unsigned *)((char *)src + tab->off);
1214 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1215 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1219 lptr = (long *)((char *)src + tab->off);
1221 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1222 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1226 ulptr = (unsigned long *)((char *)src + tab->off);
1228 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1229 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1237 tmp.bv_len = ptr - buf;
1238 ber_dupbv( bv, &tmp );
1243 bindconf_parse( const char *word, slap_bindconf *bc )
1246 /* Detect TLS config changes explicitly */
1247 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1248 bc->sb_tls_do_init = 1;
1252 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1256 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1258 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1261 void bindconf_free( slap_bindconf *bc ) {
1262 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1263 ch_free( bc->sb_uri.bv_val );
1264 BER_BVZERO( &bc->sb_uri );
1266 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1267 ch_free( bc->sb_binddn.bv_val );
1268 BER_BVZERO( &bc->sb_binddn );
1270 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1271 ch_free( bc->sb_cred.bv_val );
1272 BER_BVZERO( &bc->sb_cred );
1274 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1275 ch_free( bc->sb_saslmech.bv_val );
1276 BER_BVZERO( &bc->sb_saslmech );
1278 if ( bc->sb_secprops ) {
1279 ch_free( bc->sb_secprops );
1280 bc->sb_secprops = NULL;
1282 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1283 ch_free( bc->sb_realm.bv_val );
1284 BER_BVZERO( &bc->sb_realm );
1286 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1287 ch_free( bc->sb_authcId.bv_val );
1288 BER_BVZERO( &bc->sb_authcId );
1290 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1291 ch_free( bc->sb_authzId.bv_val );
1292 BER_BVZERO( &bc->sb_authzId );
1295 if ( bc->sb_tls_ctx ) {
1296 SSL_CTX_free( bc->sb_tls_ctx );
1297 bc->sb_tls_ctx = NULL;
1299 if ( bc->sb_tls_cert ) {
1300 ch_free( bc->sb_tls_cert );
1301 bc->sb_tls_cert = NULL;
1303 if ( bc->sb_tls_key ) {
1304 ch_free( bc->sb_tls_key );
1305 bc->sb_tls_key = NULL;
1307 if ( bc->sb_tls_cacert ) {
1308 ch_free( bc->sb_tls_cacert );
1309 bc->sb_tls_cacert = NULL;
1311 if ( bc->sb_tls_cacertdir ) {
1312 ch_free( bc->sb_tls_cacertdir );
1313 bc->sb_tls_cacertdir = NULL;
1315 if ( bc->sb_tls_reqcert ) {
1316 ch_free( bc->sb_tls_reqcert );
1317 bc->sb_tls_reqcert = NULL;
1319 if ( bc->sb_tls_cipher_suite ) {
1320 ch_free( bc->sb_tls_cipher_suite );
1321 bc->sb_tls_cipher_suite = NULL;
1323 #ifdef HAVE_OPENSSL_CRL
1324 if ( bc->sb_tls_crlcheck ) {
1325 ch_free( bc->sb_tls_crlcheck );
1326 bc->sb_tls_crlcheck = NULL;
1338 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1339 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1340 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1341 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1342 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1346 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1348 int i, rc, newctx = 0, res = 0;
1349 char *ptr = (char *)bc, **word;
1351 bc->sb_tls_do_init = 0;
1353 for (i=0; bindtlsopts[i].opt; i++) {
1354 word = (char **)(ptr + bindtlsopts[i].offset);
1356 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1358 Debug( LDAP_DEBUG_ANY,
1359 "bindconf_tls_set: failed to set %s to %s\n",
1360 bindtlsopts[i].key, *word, 0 );
1366 if ( bc->sb_tls_reqcert ) {
1367 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1368 bc->sb_tls_reqcert );
1370 Debug( LDAP_DEBUG_ANY,
1371 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1372 bc->sb_tls_reqcert, 0, 0 );
1377 #ifdef HAVE_OPENSSL_CRL
1378 if ( bc->sb_tls_crlcheck ) {
1379 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1380 bc->sb_tls_crlcheck );
1382 Debug( LDAP_DEBUG_ANY,
1383 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1384 bc->sb_tls_crlcheck, 0, 0 );
1393 if ( bc->sb_tls_ctx ) {
1394 SSL_CTX_free( bc->sb_tls_ctx );
1395 bc->sb_tls_ctx = NULL;
1397 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1401 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1409 * connect to a client using the bindconf data
1410 * note: should move "version" into bindconf...
1413 slap_client_connect( LDAP **ldp, slap_bindconf *sb, int version )
1418 /* Init connection to master */
1419 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1420 if ( rc != LDAP_SUCCESS ) {
1421 Debug( LDAP_DEBUG_ANY,
1422 "slap_client_connect: "
1423 "ldap_initialize(%s) failed (%d)\n",
1424 sb->sb_uri.bv_val, rc, 0 );
1428 if ( version != 0 ) {
1429 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1430 (const void *)&version );
1434 if ( sb->sb_tls_do_init ) {
1435 rc = bindconf_tls_set( sb, ld );
1437 } else if ( sb->sb_tls_ctx ) {
1438 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1443 Debug( LDAP_DEBUG_ANY,
1444 "slap_client_connect: "
1445 "URI=%s TLS context initialization failed (%d)\n",
1446 sb->sb_uri.bv_val, rc, 0 );
1453 rc = ldap_start_tls_s( ld, NULL, NULL );
1454 if ( rc != LDAP_SUCCESS ) {
1455 Debug( LDAP_DEBUG_ANY,
1456 "slap_client_connect: URI=%s "
1457 "%s, ldap_start_tls failed (%d)\n",
1459 sb->sb_tls == SB_TLS_CRITICAL ?
1460 "Error" : "Warning",
1462 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1468 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1469 #ifdef HAVE_CYRUS_SASL
1472 if ( sb->sb_secprops != NULL ) {
1473 rc = ldap_set_option( ld,
1474 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1476 if( rc != LDAP_OPT_SUCCESS ) {
1477 Debug( LDAP_DEBUG_ANY,
1478 "slap_client_connect: "
1479 "error, ldap_set_option "
1480 "(%s,SECPROPS,\"%s\") failed!\n",
1481 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1486 defaults = lutil_sasl_defaults( ld,
1487 sb->sb_saslmech.bv_val,
1488 sb->sb_realm.bv_val,
1489 sb->sb_authcId.bv_val,
1491 sb->sb_authzId.bv_val );
1493 rc = ldap_sasl_interactive_bind_s( ld,
1494 sb->sb_binddn.bv_val,
1495 sb->sb_saslmech.bv_val,
1498 lutil_sasl_interact,
1501 lutil_sasl_freedefs( defaults );
1503 /* FIXME: different error behaviors according to
1505 * 2) on err policy : exit, retry, backoff ...
1507 if ( rc != LDAP_SUCCESS ) {
1508 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1510 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1511 "ldap_sasl_interactive_bind_s failed (%d)\n",
1512 sb->sb_uri.bv_val, rc, 0 );
1514 /* FIXME (see above comment) */
1515 /* if Kerberos credentials cache is not active, retry */
1516 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1517 rc == LDAP_LOCAL_ERROR )
1519 rc = LDAP_SERVER_DOWN;
1524 #else /* HAVE_CYRUS_SASL */
1525 /* Should never get here, we trapped this at config time */
1527 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1532 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1533 rc = ldap_sasl_bind_s( ld,
1534 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1535 &sb->sb_cred, NULL, NULL, NULL );
1536 if ( rc != LDAP_SUCCESS ) {
1537 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1539 "ldap_sasl_bind_s failed (%d)\n",
1540 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1548 ldap_unbind_ext( ld, NULL, NULL );
1559 /* -------------------------------------- */
1563 strtok_quote( char *line, char *sep, char **quote_ptr )
1570 if ( line != NULL ) {
1573 while ( *next && strchr( sep, *next ) ) {
1577 if ( *next == '\0' ) {
1583 for ( inquote = 0; *next; ) {
1591 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1597 next + 1, strlen( next + 1 ) + 1 );
1598 next++; /* dont parse the escaped character */
1603 if ( strchr( sep, *next ) != NULL ) {
1617 static char buf[BUFSIZ];
1619 static size_t lmax, lcur;
1621 #define CATLINE( buf ) \
1623 size_t len = strlen( buf ); \
1624 while ( lcur + len + 1 > lmax ) { \
1626 line = (char *) ch_realloc( line, lmax ); \
1628 strcpy( line + lcur, buf ); \
1633 fp_getline_init(ConfigArgs *c) {
1639 fp_getline( FILE *fp, ConfigArgs *c )
1647 /* avoid stack of bufs */
1648 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1654 while ( fgets( buf, sizeof( buf ), fp ) ) {
1655 p = strchr( buf, '\n' );
1657 if ( p > buf && p[-1] == '\r' ) {
1665 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1672 if ( !isspace( (unsigned char)buf[0] ) ) {
1683 return(line[0] ? 1 : 0);
1687 fp_parse_line(ConfigArgs *c)
1690 static char *const hide[] = {
1691 "rootpw", "replica", "syncrepl", /* in slapd */
1692 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
1693 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
1694 "pseudorootpw", /* in back-meta */
1695 "dbpasswd", /* in back-sql */
1699 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1701 c->tline = ch_strdup(c->line);
1702 token = strtok_quote(c->tline, " \t", "e_ptr);
1704 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1705 if(quote_ptr) *quote_ptr = ' ';
1706 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1707 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1708 if(quote_ptr) *quote_ptr = '\0';
1710 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
1711 if(c->argc >= c->argv_size) {
1713 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1715 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1719 c->argv_size += ARGS_STEP;
1723 c->argv[c->argc++] = token;
1725 c->argv[c->argc] = NULL;
1732 ucdata_unload( UCDATA_ALL );
1734 /* NOTE: in case of early exit, frontendDB can be NULL */
1735 if ( frontendDB->be_schemandn.bv_val )
1736 free( frontendDB->be_schemandn.bv_val );
1737 if ( frontendDB->be_schemadn.bv_val )
1738 free( frontendDB->be_schemadn.bv_val );
1739 if ( frontendDB->be_acl )
1740 acl_destroy( frontendDB->be_acl, NULL );
1743 if ( slapd_args_file )
1744 free ( slapd_args_file );
1745 if ( slapd_pid_file )
1746 free ( slapd_pid_file );
1747 if ( default_passwd_hash )
1748 ldap_charray_free( default_passwd_hash );
1752 slap_str2clist( char ***out, char *in, const char *brkstr )
1760 /* find last element in list */
1761 for (i = 0; *out && (*out)[i]; i++);
1763 /* protect the input string from strtok */
1764 str = ch_strdup( in );
1766 if ( *str == '\0' ) {
1771 /* Count words in string */
1773 for ( s = str; *s; s++ ) {
1774 if ( strchr( brkstr, *s ) != NULL ) {
1779 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1781 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1783 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1785 *new = ch_strdup( s );
1794 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1795 int argc, char **argv )
1797 ConfigArgs c = { 0 };
1808 c.op = SLAP_CONFIG_ADD;
1809 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1811 rc = SLAP_CONF_UNKNOWN;
1812 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1814 rc = config_add_vals( ct, &c );