1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 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"
51 * defaults for various global variables
53 slap_mask_t global_allows = 0;
54 slap_mask_t global_disallows = 0;
55 int global_gentlehup = 0;
56 int global_idletimeout = 0;
57 char *global_host = NULL;
58 char *global_realm = NULL;
59 char *ldap_srvtab = "";
60 char **default_passwd_hash = NULL;
61 struct berval default_search_base = BER_BVNULL;
62 struct berval default_search_nbase = BER_BVNULL;
64 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
65 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
67 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
68 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
70 char *slapd_pid_file = NULL;
71 char *slapd_args_file = NULL;
73 int use_reverse_lookup = 0;
76 int slapi_plugins_used = 0;
79 static int fp_getline(FILE *fp, ConfigArgs *c);
80 static void fp_getline_init(ConfigArgs *c);
81 static int fp_parse_line(ConfigArgs *c);
83 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
84 static char *strtok_quote_ldif(char **line);
87 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
90 c = ch_calloc( 1, sizeof( ConfigArgs ) );
91 if ( c == NULL ) return(NULL);
97 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
102 init_config_argv( ConfigArgs *c )
104 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
105 c->argv_size = ARGS_STEP + 1;
108 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
111 for(i = 0; Conf[i].name; i++)
112 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
113 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
114 if ( !Conf[i].name ) return NULL;
118 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
119 int rc, arg_user, arg_type, iarg;
123 arg_type = Conf->arg_type;
124 if(arg_type == ARG_IGNORED) {
125 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
126 c->log, Conf->name, 0);
129 if((arg_type & ARG_DN) && c->argc == 1) {
133 if(Conf->min_args && (c->argc < Conf->min_args)) {
134 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
135 c->argv[0], Conf->what );
136 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
137 return(ARG_BAD_CONF);
139 if(Conf->max_args && (c->argc > Conf->max_args)) {
140 char *ignored = " ignored";
142 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
143 c->argv[0], Conf->what );
147 #endif /* LDAP_DEVEL */
148 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
149 c->log, c->msg, ignored );
151 return(ARG_BAD_CONF);
152 #endif /* LDAP_DEVEL */
154 if((arg_type & ARG_DB) && !c->be) {
155 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
157 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
159 return(ARG_BAD_CONF);
161 if((arg_type & ARG_PRE_BI) && c->bi) {
162 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
163 c->argv[0], (arg_type & ARG_PRE_DB) ? "or database " : "" );
164 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
166 return(ARG_BAD_CONF);
168 if((arg_type & ARG_PRE_DB) && c->be && c->be != frontendDB) {
169 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
171 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
173 return(ARG_BAD_CONF);
175 if((arg_type & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
176 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
177 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
179 return(ARG_BAD_CONF);
181 if((arg_type & ARGS_POINTER) && !Conf->arg_item && !(arg_type & ARG_OFFSET)) {
182 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
184 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
186 return(ARG_BAD_CONF);
188 c->type = arg_user = (arg_type & ARGS_USERLAND);
189 memset(&c->values, 0, sizeof(c->values));
190 if(arg_type & ARGS_NUMERIC) {
192 iarg = 0; larg = 0; barg = 0;
193 switch(arg_type & ARGS_NUMERIC) {
195 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
196 snprintf( c->msg, sizeof( c->msg ),
197 "<%s> unable to parse \"%s\" as int",
198 c->argv[0], c->argv[1] );
199 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
201 return(ARG_BAD_CONF);
205 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
206 snprintf( c->msg, sizeof( c->msg ),
207 "<%s> unable to parse \"%s\" as long",
208 c->argv[0], c->argv[1] );
209 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
211 return(ARG_BAD_CONF);
214 case ARG_BER_LEN_T: {
216 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
217 snprintf( c->msg, sizeof( c->msg ),
218 "<%s> unable to parse \"%s\" as ber_len_t",
219 c->argv[0], c->argv[1] );
220 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
222 return(ARG_BAD_CONF);
229 } else if ( !strcasecmp(c->argv[1], "on") ||
230 !strcasecmp(c->argv[1], "true") ||
231 !strcasecmp(c->argv[1], "yes") )
234 } else if ( !strcasecmp(c->argv[1], "off") ||
235 !strcasecmp(c->argv[1], "false") ||
236 !strcasecmp(c->argv[1], "no") )
240 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value, ignored",
242 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
248 j = (arg_type & ARG_NONZERO) ? 1 : 0;
249 if(iarg < j && larg < j && barg < j ) {
250 larg = larg ? larg : (barg ? barg : iarg);
251 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value, ignored",
253 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
255 return(ARG_BAD_CONF);
257 switch(arg_type & ARGS_NUMERIC) {
259 case ARG_INT: c->value_int = iarg; break;
260 case ARG_LONG: c->value_long = larg; break;
261 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
263 } else if(arg_type & ARG_STRING) {
265 c->value_string = ch_strdup(c->argv[1]);
266 } else if(arg_type & ARG_BERVAL) {
268 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
269 } else if(arg_type & ARG_DN) {
271 ber_str2bv( c->argv[1], 0, 0, &bv );
272 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
273 if ( rc != LDAP_SUCCESS ) {
274 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
275 c->argv[0], rc, ldap_err2string( rc ));
276 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
277 return(ARG_BAD_CONF);
280 ch_free( c->value_ndn.bv_val );
281 ch_free( c->value_dn.bv_val );
287 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
291 arg_type = Conf->arg_type;
292 if(arg_type & ARG_MAGIC) {
293 if(!c->be) c->be = frontendDB;
295 rc = (*((ConfigDriver*)Conf->arg_item))(c);
297 if(c->be == frontendDB) c->be = NULL;
301 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
303 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
306 return(ARG_BAD_CONF);
310 if(arg_type & ARG_OFFSET) {
311 if (c->be && c->table == Cft_Database)
312 ptr = c->be->be_private;
314 ptr = c->bi->bi_private;
316 snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
318 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
320 return(ARG_BAD_CONF);
322 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
323 } else if (arg_type & ARGS_POINTER) {
324 ptr = Conf->arg_item;
326 if(arg_type & ARGS_POINTER)
327 switch(arg_type & ARGS_POINTER) {
329 case ARG_INT: *(int*)ptr = c->value_int; break;
330 case ARG_LONG: *(long*)ptr = c->value_long; break;
331 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
333 char *cc = *(char**)ptr;
335 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
336 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
337 c->log, Conf->name, 0 );
338 return(ARG_BAD_CONF);
342 *(char **)ptr = c->value_string;
346 *(struct berval *)ptr = c->value_bv;
352 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
355 arg_type = Conf->arg_type;
356 if(arg_type == ARG_IGNORED) {
357 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
358 c->log, Conf->name, 0);
361 rc = config_check_vals( Conf, c, 0 );
363 return config_set_vals( Conf, c );
367 config_del_vals(ConfigTable *cf, ConfigArgs *c)
371 /* If there is no handler, just ignore it */
372 if ( cf->arg_type & ARG_MAGIC ) {
373 c->op = LDAP_MOD_DELETE;
374 c->type = cf->arg_type & ARGS_USERLAND;
375 rc = (*((ConfigDriver*)cf->arg_item))(c);
381 config_get_vals(ConfigTable *cf, ConfigArgs *c)
387 if ( cf->arg_type & ARG_IGNORED ) {
391 memset(&c->values, 0, sizeof(c->values));
392 c->rvalue_vals = NULL;
393 c->rvalue_nvals = NULL;
394 c->op = SLAP_CONFIG_EMIT;
395 c->type = cf->arg_type & ARGS_USERLAND;
397 if ( cf->arg_type & ARG_MAGIC ) {
398 rc = (*((ConfigDriver*)cf->arg_item))(c);
401 if ( cf->arg_type & ARG_OFFSET ) {
402 if (c->be && c->table == Cft_Database)
403 ptr = c->be->be_private;
405 ptr = c->bi->bi_private;
408 ptr = (void *)((char *)ptr + (long)cf->arg_item);
413 switch(cf->arg_type & ARGS_POINTER) {
415 case ARG_INT: c->value_int = *(int *)ptr; break;
416 case ARG_LONG: c->value_long = *(long *)ptr; break;
417 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
420 c->value_string = ch_strdup(*(char **)ptr);
423 ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
426 if ( cf->arg_type & ARGS_POINTER) {
428 switch(cf->arg_type & ARGS_POINTER) {
429 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
430 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
431 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
432 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
433 c->value_int ? "TRUE" : "FALSE"); break;
435 if ( c->value_string && c->value_string[0]) {
436 ber_str2bv( c->value_string, 0, 0, &bv);
442 if ( !BER_BVISEMPTY( &c->value_bv )) {
449 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
452 if (( cf->arg_type & ARGS_POINTER ) == ARG_STRING )
453 ber_bvarray_add(&c->rvalue_vals, &bv);
455 value_add_one(&c->rvalue_vals, &bv);
461 init_config_attrs(ConfigTable *ct) {
462 LDAPAttributeType *at;
466 for (i=0; ct[i].name; i++ ) {
469 if ( !ct[i].attribute ) continue;
470 at = ldap_str2attributetype( ct[i].attribute,
471 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
473 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
474 ct[i].attribute, ldap_scherr2str(code), err );
478 code = at_add( at, 0, NULL, &err );
480 if ( code == SLAP_SCHERR_ATTR_DUP ) {
484 ldap_attributetype_free( at );
485 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
486 ct[i].attribute, scherr2str(code), err );
490 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
492 ldap_attributetype_free( at );
497 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
498 ct[i].attribute, err );
507 init_config_ocs( ConfigOCs *ocs ) {
510 for (i=0;ocs[i].co_def;i++) {
515 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
516 LDAP_SCHEMA_ALLOW_ALL );
518 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
519 ocs[i].co_def, ldap_scherr2str(code), err );
522 code = oc_add(oc,0,NULL,&err);
523 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
524 fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
525 ocs[i].co_def, scherr2str(code), err );
528 ocs[i].co_oc = oc_find(oc->oc_names[0]);
534 /* Split an LDIF line into space-separated tokens. Words may be grouped
535 * by quotes. A quoted string may begin in the middle of a word, but must
536 * end at the end of the word (be followed by whitespace or EOS). Any other
537 * quotes are passed through unchanged. All other characters are passed
541 strtok_quote_ldif( char **line )
543 char *beg, *ptr, *quote=NULL;
551 while( isspace( *ptr )) ptr++;
562 if ( inquote && ( !ptr[1] || isspace(ptr[1]))) {
572 if ( isspace( *ptr )) {
578 while ( quote < ptr ) {
586 while ( isspace( *ptr )) ptr++;
593 config_parse_ldif( ConfigArgs *c )
596 c->tline = ch_strdup(c->line);
599 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
601 if ( c->argc >= c->argv_size ) {
602 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
605 c->argv_size += ARGS_STEP;
608 c->argv[c->argc] = NULL;
612 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
616 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
617 ct->ad->ad_cname.bv_val, valx );
619 c->argv[0] = ct->ad->ad_cname.bv_val;
621 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
622 c->argv[c->argc] = c->line;
624 c->argv[c->argc] = NULL;
627 config_parse_ldif( c );
629 rc = config_check_vals( ct, c, 1 );
634 rc = LDAP_CONSTRAINT_VIOLATION;
640 config_parse_add(ConfigTable *ct, ConfigArgs *c)
644 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
645 ct->ad->ad_cname.bv_val, c->valx );
647 c->argv[0] = ct->ad->ad_cname.bv_val;
649 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
650 c->argv[c->argc] = c->line;
652 c->argv[c->argc] = NULL;
655 config_parse_ldif( c );
657 c->op = LDAP_MOD_ADD;
658 rc = config_add_vals( ct, c );
665 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
673 c = ch_calloc( 1, sizeof( ConfigArgs ) );
679 memcpy( c, cf, sizeof( ConfigArgs ) );
681 c->depth = depth; /* XXX */
688 init_config_argv( c );
690 if ( stat( fname, &s ) != 0 ) {
692 Debug(LDAP_DEBUG_ANY,
693 "could not stat config file \"%s\": %s (%d)\n",
694 fname, strerror(errno), errno);
698 if ( !S_ISREG( s.st_mode ) ) {
700 Debug(LDAP_DEBUG_ANY,
701 "regular file expected, got \"%s\"\n",
706 fp = fopen( fname, "r" );
709 Debug(LDAP_DEBUG_ANY,
710 "could not open config file \"%s\": %s (%d)\n",
711 fname, strerror(errno), errno);
715 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
721 while ( fp_getline( fp, c ) ) {
722 /* skip comments and blank lines */
723 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
727 snprintf( c->log, sizeof( c->log ), "%s: line %d",
728 c->fname, c->lineno );
732 if ( fp_parse_line( c ) ) {
738 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line"
739 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
741 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
744 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
746 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
749 c->op = SLAP_CONFIG_ADD;
751 ct = config_find_keyword( cft, c );
753 c->table = Cft_Global;
754 rc = config_add_vals( ct, c );
757 if ( rc & ARGS_USERLAND ) {
758 /* XXX a usertype would be opaque here */
759 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
760 c->log, c->argv[0], 0);
764 } else if ( rc == ARG_BAD_CONF ) {
769 } else if ( c->bi && !c->be ) {
770 rc = SLAP_CONF_UNKNOWN;
771 if ( c->bi->bi_cf_ocs ) {
772 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
774 c->table = c->bi->bi_cf_ocs->co_type;
775 rc = config_add_vals( ct, c );
778 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
779 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
784 case SLAP_CONF_UNKNOWN:
785 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
786 "unknown directive <%s> inside backend info definition"
787 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
788 c->log, *c->argv, 0);
789 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
791 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
798 } else if ( c->be ) {
799 rc = SLAP_CONF_UNKNOWN;
800 if ( c->be->be_cf_ocs ) {
801 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
803 c->table = c->be->be_cf_ocs->co_type;
804 rc = config_add_vals( ct, c );
807 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
808 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
813 case SLAP_CONF_UNKNOWN:
814 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
815 "unknown directive <%s> inside backend database "
816 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
817 c->log, *c->argv, 0);
818 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
820 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
827 } else if ( frontendDB->be_config ) {
828 rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
831 case SLAP_CONF_UNKNOWN:
832 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
833 "unknown directive <%s> inside global database definition"
834 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
835 c->log, *c->argv, 0);
836 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
838 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
846 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
847 "unknown directive <%s> outside backend info and database definitions"
848 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
849 c->log, *c->argv, 0);
850 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
853 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
855 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
873 /* restrictops, allows, disallows, requires, loglevel */
876 verb_to_mask(const char *word, slap_verbmasks *v) {
878 for(i = 0; !BER_BVISNULL(&v[i].word); i++)
879 if(!strcasecmp(word, v[i].word.bv_val))
885 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
887 for(i = 1; i < argc; i++) {
888 j = verb_to_mask(argv[i], v);
889 if(BER_BVISNULL(&v[j].word)) return i;
890 while (!v[j].mask) j--;
896 /* Mask keywords that represent multiple bits should occur before single
897 * bit keywords in the verbmasks array.
900 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
904 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
905 if (!v[i].mask) continue;
906 if (( m & v[i].mask ) == v[i].mask ) {
907 value_add_one( bva, &v[i].word );
918 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
922 assert( *vp == NULL );
924 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ )
927 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
929 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
930 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
931 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
934 BER_BVZERO( &(*vp)[ i ].word );
940 slap_verbmasks_destroy( slap_verbmasks *v )
946 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
947 ch_free( v[ i ].word.bv_val );
956 slap_verbmasks_append(
960 slap_mask_t *ignore )
965 return LDAP_OPERATIONS_ERROR;
968 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
969 if ( !(*vp)[ i ].mask ) continue;
971 if ( ignore != NULL ) {
974 for ( j = 0; ignore[ j ] != 0; j++ ) {
975 if ( (*vp)[ i ].mask == ignore[ j ] ) {
981 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
982 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
983 /* already set; ignore */
987 return LDAP_TYPE_OR_VALUE_EXISTS;
990 if ( m & (*vp)[ i ].mask ) {
992 return LDAP_CONSTRAINT_VIOLATION;
997 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
998 ber_dupbv( &(*vp)[ i ].word, v );
999 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1000 BER_BVZERO( &(*vp)[ i + 1 ].word );
1002 return LDAP_SUCCESS;
1006 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1009 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1010 if ( m == v[i].mask ) {
1020 static slap_verbmasks tlskey[] = {
1021 { BER_BVC("no"), SB_TLS_OFF },
1022 { BER_BVC("yes"), SB_TLS_ON },
1023 { BER_BVC("critical"), SB_TLS_CRITICAL },
1027 static slap_verbmasks methkey[] = {
1028 { BER_BVC("none"), LDAP_AUTH_NONE },
1029 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1030 #ifdef HAVE_CYRUS_SASL
1031 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1036 static slap_cf_aux_table bindkey[] = {
1037 { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1038 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1039 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1040 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, dnNormalize },
1041 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1042 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1043 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1044 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1045 #ifndef SLAP_AUTHZ_SYNTAX
1046 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1047 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1048 #else /* SLAP_AUTHZ_SYNTAX */
1049 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, authzNormalize },
1050 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, authzNormalize },
1051 #endif /* SLAP_AUTHZ_SYNTAX */
1053 { BER_BVNULL, 0, 0, 0, NULL }
1057 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1060 slap_cf_aux_table *tab;
1062 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1063 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1068 unsigned long *ulptr;
1069 struct berval *bptr;
1070 const char *val = word + tab->key.bv_len;
1072 switch ( tab->type ) {
1074 cptr = (char **)((char *)dst + tab->off);
1075 *cptr = ch_strdup( val );
1079 bptr = (struct berval *)((char *)dst + tab->off);
1080 if ( tab->aux != NULL ) {
1082 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1084 ber_str2bv( val, 0, 0, &dn );
1085 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1088 ber_str2bv( val, 0, 1, bptr );
1094 iptr = (int *)((char *)dst + tab->off);
1096 if ( tab->aux != NULL ) {
1097 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1099 assert( aux != NULL );
1102 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1103 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1104 *iptr = aux[j].mask;
1111 rc = lutil_atoix( iptr, val, 0 );
1116 uptr = (unsigned *)((char *)dst + tab->off);
1118 rc = lutil_atoux( uptr, val, 0 );
1122 lptr = (long *)((char *)dst + tab->off);
1124 rc = lutil_atolx( lptr, val, 0 );
1128 ulptr = (unsigned long *)((char *)dst + tab->off);
1130 rc = lutil_atoulx( ulptr, val, 0 );
1135 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1147 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1149 char buf[AC_LINE_MAX], *ptr;
1150 slap_cf_aux_table *tab;
1154 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1159 unsigned long *ulptr;
1160 struct berval *bptr;
1162 cptr = (char **)((char *)src + tab->off);
1164 switch ( tab->type ) {
1166 bptr = (struct berval *)((char *)src + tab->off);
1167 cptr = &bptr->bv_val;
1172 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1173 if ( tab->quote ) *ptr++ = '"';
1174 ptr = lutil_strcopy( ptr, *cptr );
1175 if ( tab->quote ) *ptr++ = '"';
1180 iptr = (int *)((char *)src + tab->off);
1182 if ( tab->aux != NULL ) {
1183 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1185 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1186 if ( *iptr == aux[i].mask ) {
1188 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1189 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1196 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1197 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1202 uptr = (unsigned *)((char *)src + tab->off);
1204 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1205 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1209 lptr = (long *)((char *)src + tab->off);
1211 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1212 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1216 ulptr = (unsigned long *)((char *)src + tab->off);
1218 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1219 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1227 tmp.bv_len = ptr - buf;
1228 ber_dupbv( bv, &tmp );
1233 bindconf_parse( const char *word, slap_bindconf *bc )
1235 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1239 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1241 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1244 void bindconf_free( slap_bindconf *bc ) {
1245 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1246 ch_free( bc->sb_uri.bv_val );
1247 BER_BVZERO( &bc->sb_uri );
1249 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1250 ch_free( bc->sb_binddn.bv_val );
1251 BER_BVZERO( &bc->sb_binddn );
1253 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1254 ch_free( bc->sb_cred.bv_val );
1255 BER_BVZERO( &bc->sb_cred );
1257 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1258 ch_free( bc->sb_saslmech.bv_val );
1259 BER_BVZERO( &bc->sb_saslmech );
1261 if ( bc->sb_secprops ) {
1262 ch_free( bc->sb_secprops );
1263 bc->sb_secprops = NULL;
1265 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1266 ch_free( bc->sb_realm.bv_val );
1267 BER_BVZERO( &bc->sb_realm );
1269 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1270 ch_free( bc->sb_authcId.bv_val );
1271 BER_BVZERO( &bc->sb_authcId );
1273 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1274 ch_free( bc->sb_authzId.bv_val );
1275 BER_BVZERO( &bc->sb_authzId );
1280 /* -------------------------------------- */
1284 strtok_quote( char *line, char *sep, char **quote_ptr )
1291 if ( line != NULL ) {
1294 while ( *next && strchr( sep, *next ) ) {
1298 if ( *next == '\0' ) {
1304 for ( inquote = 0; *next; ) {
1312 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1318 next + 1, strlen( next + 1 ) + 1 );
1319 next++; /* dont parse the escaped character */
1324 if ( strchr( sep, *next ) != NULL ) {
1338 static char buf[AC_LINE_MAX];
1340 static size_t lmax, lcur;
1342 #define CATLINE( buf ) \
1344 size_t len = strlen( buf ); \
1345 while ( lcur + len + 1 > lmax ) { \
1346 lmax += AC_LINE_MAX; \
1347 line = (char *) ch_realloc( line, lmax ); \
1349 strcpy( line + lcur, buf ); \
1354 fp_getline_init(ConfigArgs *c) {
1360 fp_getline( FILE *fp, ConfigArgs *c )
1368 /* avoid stack of bufs */
1369 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1375 while ( fgets( buf, sizeof( buf ), fp ) ) {
1376 p = strchr( buf, '\n' );
1378 if ( p > buf && p[-1] == '\r' ) {
1386 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1393 if ( !isspace( (unsigned char)buf[0] ) ) {
1404 return(line[0] ? 1 : 0);
1408 fp_parse_line(ConfigArgs *c)
1411 static char *const hide[] = {
1412 "rootpw", "replica", "syncrepl", /* in slapd */
1413 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
1414 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
1415 "pseudorootpw", /* in back-meta */
1416 "dbpasswd", /* in back-sql */
1420 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1422 c->tline = ch_strdup(c->line);
1423 token = strtok_quote(c->tline, " \t", "e_ptr);
1425 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1426 if(quote_ptr) *quote_ptr = ' ';
1427 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1428 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1429 if(quote_ptr) *quote_ptr = '\0';
1431 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
1432 if(c->argc >= c->argv_size) {
1434 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1436 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1440 c->argv_size += ARGS_STEP;
1444 c->argv[c->argc++] = token;
1446 c->argv[c->argc] = NULL;
1453 ucdata_unload( UCDATA_ALL );
1455 /* NOTE: in case of early exit, frontendDB can be NULL */
1456 if ( frontendDB->be_schemandn.bv_val )
1457 free( frontendDB->be_schemandn.bv_val );
1458 if ( frontendDB->be_schemadn.bv_val )
1459 free( frontendDB->be_schemadn.bv_val );
1460 if ( frontendDB->be_acl )
1461 acl_destroy( frontendDB->be_acl, NULL );
1464 if ( slapd_args_file )
1465 free ( slapd_args_file );
1466 if ( slapd_pid_file )
1467 free ( slapd_pid_file );
1468 if ( default_passwd_hash )
1469 ldap_charray_free( default_passwd_hash );
1473 slap_str2clist( char ***out, char *in, const char *brkstr )
1481 /* find last element in list */
1482 for (i = 0; *out && (*out)[i]; i++);
1484 /* protect the input string from strtok */
1485 str = ch_strdup( in );
1487 if ( *str == '\0' ) {
1492 /* Count words in string */
1494 for ( s = str; *s; s++ ) {
1495 if ( strchr( brkstr, *s ) != NULL ) {
1500 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1502 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1504 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1506 *new = ch_strdup( s );
1515 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1516 int argc, char **argv )
1518 ConfigArgs c = { 0 };
1529 c.op = SLAP_CONFIG_ADD;
1530 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1532 rc = SLAP_CONF_UNKNOWN;
1533 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1535 c.table = be->be_cf_ocs->co_type;
1536 rc = config_add_vals( ct, &c );