1 /* config.c - configuration file handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 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>
41 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
50 #include "slapi/slapi.h"
53 #include "lutil_ldap.h"
59 * defaults for various global variables
61 slap_mask_t global_allows = 0;
62 slap_mask_t global_disallows = 0;
63 int global_gentlehup = 0;
64 int global_idletimeout = 0;
65 int global_writetimeout = 0;
66 char *global_host = NULL;
67 struct berval global_host_bv = BER_BVNULL;
68 char *global_realm = NULL;
69 char *sasl_host = NULL;
70 char **default_passwd_hash = NULL;
71 struct berval default_search_base = BER_BVNULL;
72 struct berval default_search_nbase = BER_BVNULL;
74 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
75 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
77 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
78 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
80 char *slapd_pid_file = NULL;
81 char *slapd_args_file = NULL;
83 int use_reverse_lookup = 0;
86 int slapi_plugins_used = 0;
89 static int fp_getline(FILE *fp, ConfigArgs *c);
90 static void fp_getline_init(ConfigArgs *c);
92 static char *strtok_quote(char *line, char *sep, char **quote_ptr);
93 static char *strtok_quote_ldif(char **line);
96 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
99 c = ch_calloc( 1, sizeof( ConfigArgs ) );
100 if ( c == NULL ) return(NULL);
106 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
111 init_config_argv( ConfigArgs *c )
113 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
114 c->argv_size = ARGS_STEP + 1;
117 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
120 for(i = 0; Conf[i].name; i++)
121 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
122 (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
123 if ( !Conf[i].name ) return NULL;
127 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
128 int rc, arg_user, arg_type, arg_syn, iarg;
133 if(Conf->arg_type == ARG_IGNORED) {
134 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
135 c->log, Conf->name, 0);
138 arg_type = Conf->arg_type & ARGS_TYPES;
139 arg_user = Conf->arg_type & ARGS_USERLAND;
140 arg_syn = Conf->arg_type & ARGS_SYNTAX;
142 if((arg_type == ARG_DN) && c->argc == 1) {
146 if(Conf->min_args && (c->argc < Conf->min_args)) {
147 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument",
148 c->argv[0], Conf->what );
149 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg, 0 );
150 return(ARG_BAD_CONF);
152 if(Conf->max_args && (c->argc > Conf->max_args)) {
153 char *ignored = " ignored";
155 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>",
156 c->argv[0], Conf->what );
159 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n",
160 c->log, c->cr_msg, ignored );
161 return(ARG_BAD_CONF);
163 if((arg_syn & ARG_DB) && !c->be) {
164 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration",
166 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
167 c->log, c->cr_msg, 0);
168 return(ARG_BAD_CONF);
170 if((arg_syn & ARG_PRE_BI) && c->bi) {
171 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration",
172 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
173 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
174 c->log, c->cr_msg, 0 );
175 return(ARG_BAD_CONF);
177 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
178 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration",
180 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n",
181 c->log, c->cr_msg, 0);
182 return(ARG_BAD_CONF);
184 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
185 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] );
186 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
187 c->log, c->cr_msg, 0);
188 return(ARG_BAD_CONF);
190 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
191 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL",
193 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
194 c->log, c->cr_msg, 0);
195 return(ARG_BAD_CONF);
198 memset(&c->values, 0, sizeof(c->values));
199 if(arg_type == ARG_STRING) {
201 c->value_string = ch_strdup(c->argv[1]);
202 } else if(arg_type == ARG_BERVAL) {
204 ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
205 } else if(arg_type == ARG_DN) {
207 ber_str2bv( c->argv[1], 0, 0, &bv );
208 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
209 if ( rc != LDAP_SUCCESS ) {
210 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)",
211 c->argv[0], rc, ldap_err2string( rc ));
212 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg, 0);
213 return(ARG_BAD_CONF);
216 ch_free( c->value_ndn.bv_val );
217 ch_free( c->value_dn.bv_val );
219 } else { /* all numeric */
221 iarg = 0; larg = 0; barg = 0;
224 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
225 snprintf( c->cr_msg, sizeof( c->cr_msg ),
226 "<%s> unable to parse \"%s\" as int",
227 c->argv[0], c->argv[1] );
228 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
229 c->log, c->cr_msg, 0);
230 return(ARG_BAD_CONF);
234 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) {
235 snprintf( c->cr_msg, sizeof( c->cr_msg ),
236 "<%s> unable to parse \"%s\" as unsigned int",
237 c->argv[0], c->argv[1] );
238 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
239 c->log, c->cr_msg, 0);
240 return(ARG_BAD_CONF);
244 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
245 snprintf( c->cr_msg, sizeof( c->cr_msg ),
246 "<%s> unable to parse \"%s\" as long",
247 c->argv[0], c->argv[1] );
248 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
249 c->log, c->cr_msg, 0);
250 return(ARG_BAD_CONF);
253 case ARG_BER_LEN_T: {
255 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
256 snprintf( c->cr_msg, sizeof( c->cr_msg ),
257 "<%s> unable to parse \"%s\" as ber_len_t",
258 c->argv[0], c->argv[1] );
259 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n",
260 c->log, c->cr_msg, 0);
261 return(ARG_BAD_CONF);
268 } else if ( !strcasecmp(c->argv[1], "on") ||
269 !strcasecmp(c->argv[1], "true") ||
270 !strcasecmp(c->argv[1], "yes") )
273 } else if ( !strcasecmp(c->argv[1], "off") ||
274 !strcasecmp(c->argv[1], "false") ||
275 !strcasecmp(c->argv[1], "no") )
279 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
281 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
282 c->log, c->cr_msg, 0 );
283 return(ARG_BAD_CONF);
287 j = (arg_type & ARG_NONZERO) ? 1 : 0;
288 if(iarg < j && larg < j && barg < j ) {
289 larg = larg ? larg : (barg ? barg : iarg);
290 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value",
292 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n",
293 c->log, c->cr_msg, 0 );
294 return(ARG_BAD_CONF);
298 case ARG_INT: c->value_int = iarg; break;
299 case ARG_UINT: c->value_uint = uiarg; break;
300 case ARG_LONG: c->value_long = larg; break;
301 case ARG_BER_LEN_T: c->value_ber_t = barg; break;
307 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
311 arg_type = Conf->arg_type;
312 if(arg_type & ARG_MAGIC) {
313 if(!c->be) c->be = frontendDB;
315 rc = (*((ConfigDriver*)Conf->arg_item))(c);
317 if(c->be == frontendDB) c->be = NULL;
320 if ( !c->cr_msg[0] ) {
321 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d",
323 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
324 c->log, c->cr_msg, 0 );
326 return(ARG_BAD_CONF);
330 if(arg_type & ARG_OFFSET) {
331 if (c->be && c->table == Cft_Database)
332 ptr = c->be->be_private;
334 ptr = c->bi->bi_private;
336 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer",
338 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
339 c->log, c->cr_msg, 0);
340 return(ARG_BAD_CONF);
342 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
343 } else if (arg_type & ARGS_TYPES) {
344 ptr = Conf->arg_item;
346 if(arg_type & ARGS_TYPES)
347 switch(arg_type & ARGS_TYPES) {
349 case ARG_INT: *(int*)ptr = c->value_int; break;
350 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break;
351 case ARG_LONG: *(long*)ptr = c->value_long; break;
352 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break;
354 char *cc = *(char**)ptr;
356 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
357 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
358 c->log, Conf->name, 0 );
359 return(ARG_BAD_CONF);
363 *(char **)ptr = c->value_string;
367 *(struct berval *)ptr = c->value_bv;
373 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
376 arg_type = Conf->arg_type;
377 if(arg_type == ARG_IGNORED) {
378 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
379 c->log, Conf->name, 0);
382 rc = config_check_vals( Conf, c, 0 );
384 return config_set_vals( Conf, c );
388 config_del_vals(ConfigTable *cf, ConfigArgs *c)
392 /* If there is no handler, just ignore it */
393 if ( cf->arg_type & ARG_MAGIC ) {
394 c->op = LDAP_MOD_DELETE;
395 c->type = cf->arg_type & ARGS_USERLAND;
396 rc = (*((ConfigDriver*)cf->arg_item))(c);
402 config_get_vals(ConfigTable *cf, ConfigArgs *c)
408 if ( cf->arg_type & ARG_IGNORED ) {
412 memset(&c->values, 0, sizeof(c->values));
413 c->rvalue_vals = NULL;
414 c->rvalue_nvals = NULL;
415 c->op = SLAP_CONFIG_EMIT;
416 c->type = cf->arg_type & ARGS_USERLAND;
418 if ( cf->arg_type & ARG_MAGIC ) {
419 rc = (*((ConfigDriver*)cf->arg_item))(c);
422 if ( cf->arg_type & ARG_OFFSET ) {
423 if (c->be && c->table == Cft_Database)
424 ptr = c->be->be_private;
426 ptr = c->bi->bi_private;
429 ptr = (void *)((char *)ptr + (long)cf->arg_item);
434 switch(cf->arg_type & ARGS_TYPES) {
436 case ARG_INT: c->value_int = *(int *)ptr; break;
437 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break;
438 case ARG_LONG: c->value_long = *(long *)ptr; break;
439 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break;
442 c->value_string = ch_strdup(*(char **)ptr);
445 ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
448 if ( cf->arg_type & ARGS_TYPES) {
451 switch(cf->arg_type & ARGS_TYPES) {
452 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
453 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break;
454 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
455 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
456 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
457 c->value_int ? "TRUE" : "FALSE"); break;
459 if ( c->value_string && c->value_string[0]) {
460 ber_str2bv( c->value_string, 0, 0, &bv);
466 if ( !BER_BVISEMPTY( &c->value_bv )) {
476 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
479 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
480 ber_bvarray_add(&c->rvalue_vals, &bv);
481 } else if ( !BER_BVISNULL( &bv ) ) {
482 value_add_one(&c->rvalue_vals, &bv);
484 /* else: maybe c->rvalue_vals already set? */
490 init_config_attrs(ConfigTable *ct) {
493 for (i=0; ct[i].name; i++ ) {
494 if ( !ct[i].attribute ) continue;
495 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
497 fprintf( stderr, "init_config_attrs: register_at failed\n" );
506 init_config_ocs( ConfigOCs *ocs ) {
509 for (i=0;ocs[i].co_def;i++) {
510 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
512 fprintf( stderr, "init_config_ocs: register_oc failed\n" );
519 /* Split an LDIF line into space-separated tokens. Words may be grouped
520 * by quotes. A quoted string may begin in the middle of a word, but must
521 * end at the end of the word (be followed by whitespace or EOS). Any other
522 * quotes are passed through unchanged. All other characters are passed
526 strtok_quote_ldif( char **line )
528 char *beg, *ptr, *quote=NULL;
536 while( isspace( (unsigned char) *ptr )) ptr++;
547 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
557 if ( isspace( (unsigned char) *ptr )) {
563 while ( quote < ptr ) {
571 while ( isspace( (unsigned char) *ptr )) ptr++;
578 config_parse_ldif( ConfigArgs *c )
581 c->tline = ch_strdup(c->line);
584 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
586 if ( c->argc >= c->argv_size ) {
587 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
590 c->argv_size += ARGS_STEP;
593 c->argv[c->argc] = NULL;
597 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
601 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
602 ct->ad->ad_cname.bv_val, valx );
604 c->argv[0] = ct->ad->ad_cname.bv_val;
606 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
607 c->argv[c->argc] = c->line;
609 c->argv[c->argc] = NULL;
612 config_parse_ldif( c );
614 rc = config_check_vals( ct, c, 1 );
619 rc = LDAP_CONSTRAINT_VIOLATION;
625 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
629 snprintf( c->log, sizeof( c->log ), "%s: value #%d",
630 ct->ad->ad_cname.bv_val, valx );
632 c->argv[0] = ct->ad->ad_cname.bv_val;
634 if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
635 c->argv[c->argc] = c->line;
637 c->argv[c->argc] = NULL;
640 config_parse_ldif( c );
642 c->op = LDAP_MOD_ADD;
643 rc = config_add_vals( ct, c );
650 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
658 c = ch_calloc( 1, sizeof( ConfigArgs ) );
664 memcpy( c, cf, sizeof( ConfigArgs ) );
666 c->depth = depth; /* XXX */
673 init_config_argv( c );
675 if ( stat( fname, &s ) != 0 ) {
677 Debug(LDAP_DEBUG_ANY,
678 "could not stat config file \"%s\": %s (%d)\n",
679 fname, strerror(errno), errno);
684 if ( !S_ISREG( s.st_mode ) ) {
686 Debug(LDAP_DEBUG_ANY,
687 "regular file expected, got \"%s\"\n",
693 fp = fopen( fname, "r" );
696 Debug(LDAP_DEBUG_ANY,
697 "could not open config file \"%s\": %s (%d)\n",
698 fname, strerror(errno), errno);
703 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
709 while ( fp_getline( fp, c ) ) {
710 /* skip comments and blank lines */
711 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
715 snprintf( c->log, sizeof( c->log ), "%s: line %d",
716 c->fname, c->lineno );
720 if ( config_fp_parse_line( c ) ) {
726 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
732 c->op = SLAP_CONFIG_ADD;
734 ct = config_find_keyword( cft, c );
736 c->table = Cft_Global;
737 rc = config_add_vals( ct, c );
740 if ( rc & ARGS_USERLAND ) {
741 /* XXX a usertype would be opaque here */
742 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
743 c->log, c->argv[0], 0);
747 } else if ( rc == ARG_BAD_CONF ) {
752 } else if ( c->bi && !c->be ) {
753 rc = SLAP_CONF_UNKNOWN;
754 if ( c->bi->bi_cf_ocs ) {
755 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
757 c->table = c->bi->bi_cf_ocs->co_type;
758 rc = config_add_vals( ct, c );
761 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
762 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
767 case SLAP_CONF_UNKNOWN:
768 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
769 "<%s> inside backend info definition.\n",
770 c->log, *c->argv, 0);
777 } else if ( c->be && c->be != frontendDB ) {
778 rc = SLAP_CONF_UNKNOWN;
779 if ( c->be->be_cf_ocs ) {
780 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
782 c->table = c->be->be_cf_ocs->co_type;
783 rc = config_add_vals( ct, c );
786 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
787 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
790 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
792 /* global overlays may need
793 * definitions inside other databases...
795 rc = (*frontendDB->be_config)( frontendDB,
796 c->fname, (int)c->lineno, c->argc, c->argv );
803 case SLAP_CONF_UNKNOWN:
804 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
805 "<%s> inside backend database definition.\n",
806 c->log, *c->argv, 0);
813 } else if ( frontendDB->be_config ) {
814 rc = (*frontendDB->be_config)( frontendDB,
815 c->fname, (int)c->lineno, c->argc, c->argv);
818 case SLAP_CONF_UNKNOWN:
819 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
820 "<%s> inside global database definition.\n",
821 c->log, *c->argv, 0);
830 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
831 "<%s> outside backend info and database definitions.\n",
832 c->log, *c->argv, 0);
852 /* restrictops, allows, disallows, requires, loglevel */
855 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
857 for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
858 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
864 verb_to_mask(const char *word, slap_verbmasks *v) {
866 ber_str2bv( word, 0, 0, &bword );
867 return bverb_to_mask( &bword, v );
871 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
873 for(i = 1; i < argc; i++) {
874 j = verb_to_mask(argv[i], v);
875 if(BER_BVISNULL(&v[j].word)) return i;
876 while (!v[j].mask) j--;
882 /* Mask keywords that represent multiple bits should occur before single
883 * bit keywords in the verbmasks array.
886 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
890 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
891 if (!v[i].mask) continue;
892 if (( m & v[i].mask ) == v[i].mask ) {
893 value_add_one( bva, &v[i].word );
904 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
908 assert( *vp == NULL );
910 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
912 *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
914 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
915 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
916 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
919 BER_BVZERO( &(*vp)[ i ].word );
925 slap_verbmasks_destroy( slap_verbmasks *v )
931 for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
932 ch_free( v[ i ].word.bv_val );
941 slap_verbmasks_append(
945 slap_mask_t *ignore )
950 return LDAP_OPERATIONS_ERROR;
953 for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
954 if ( !(*vp)[ i ].mask ) continue;
956 if ( ignore != NULL ) {
959 for ( j = 0; ignore[ j ] != 0; j++ ) {
960 if ( (*vp)[ i ].mask == ignore[ j ] ) {
966 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
967 if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
968 /* already set; ignore */
972 return LDAP_TYPE_OR_VALUE_EXISTS;
975 if ( m & (*vp)[ i ].mask ) {
977 return LDAP_CONSTRAINT_VIOLATION;
982 *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
983 ber_dupbv( &(*vp)[ i ].word, v );
984 *((slap_mask_t *)&(*vp)[ i ].mask) = m;
985 BER_BVZERO( &(*vp)[ i + 1 ].word );
991 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
994 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
995 if ( m == v[i].mask ) {
1005 /* register a new verbmask */
1007 slap_verbmask_register( slap_verbmasks *vm_, slap_verbmasks **vmp, struct berval *bv, int mask )
1009 slap_verbmasks *vm = *vmp;
1012 /* check for duplicate word */
1013 /* NOTE: we accept duplicate codes; the first occurrence will be used
1014 * when mapping from mask to verb */
1015 i = verb_to_mask( bv->bv_val, vm );
1016 if ( !BER_BVISNULL( &vm[ i ].word ) ) {
1020 for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
1024 /* first time: duplicate array */
1025 vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
1026 for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
1028 ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
1029 *((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
1033 vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
1036 ber_dupbv( &vm[ i ].word, bv );
1037 *((slap_mask_t*)&vm[ i ].mask) = mask;
1039 BER_BVZERO( &vm[ i+1 ].word );
1046 static slap_verbmasks slap_ldap_response_code_[] = {
1047 { BER_BVC("success"), LDAP_SUCCESS },
1049 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR },
1050 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR },
1051 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED },
1052 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED },
1053 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE },
1054 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE },
1056 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED },
1057 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED },
1058 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED },
1059 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED },
1060 #if 0 /* not LDAPv3 */
1061 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS },
1064 { BER_BVC("referral"), LDAP_REFERRAL },
1065 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED },
1066 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
1067 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED },
1068 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS },
1070 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE },
1071 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE },
1072 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING },
1073 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION },
1074 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS },
1075 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX },
1077 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT },
1078 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM },
1079 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX },
1080 #if 0 /* not LDAPv3 */
1081 { BER_BVC("isLeaf"), LDAP_IS_LEAF },
1083 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM },
1085 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE },
1086 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH },
1087 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS },
1088 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS },
1090 { BER_BVC("busy"), LDAP_BUSY },
1091 { BER_BVC("unavailable"), LDAP_UNAVAILABLE },
1092 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM },
1093 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT },
1095 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION },
1096 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION },
1097 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF },
1098 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN },
1099 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS },
1100 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS },
1101 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE },
1102 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS },
1104 { BER_BVC("other"), LDAP_OTHER },
1106 /* extension-specific */
1108 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED },
1109 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION },
1110 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA },
1111 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME },
1112 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED },
1114 { BER_BVC("cancelled"), LDAP_CANCELLED },
1115 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION },
1116 { BER_BVC("tooLate"), LDAP_TOO_LATE },
1117 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL },
1119 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED },
1121 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED },
1123 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED },
1125 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION },
1130 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_;
1133 slap_ldap_response_code_register( struct berval *bv, int err )
1135 return slap_verbmask_register( slap_ldap_response_code_,
1136 &slap_ldap_response_code, bv, err );
1140 static slap_verbmasks tlskey[] = {
1141 { BER_BVC("no"), SB_TLS_OFF },
1142 { BER_BVC("yes"), SB_TLS_ON },
1143 { BER_BVC("critical"), SB_TLS_CRITICAL },
1147 static slap_verbmasks crlkeys[] = {
1148 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE },
1149 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER },
1150 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL },
1154 static slap_verbmasks vfykeys[] = {
1155 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER },
1156 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND },
1157 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY },
1158 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD },
1163 static slap_verbmasks methkey[] = {
1164 { BER_BVC("none"), LDAP_AUTH_NONE },
1165 { BER_BVC("simple"), LDAP_AUTH_SIMPLE },
1166 #ifdef HAVE_CYRUS_SASL
1167 { BER_BVC("sasl"), LDAP_AUTH_SASL },
1172 static slap_verbmasks versionkey[] = {
1173 { BER_BVC("2"), LDAP_VERSION2 },
1174 { BER_BVC("3"), LDAP_VERSION3 },
1178 static slap_cf_aux_table bindkey[] = {
1179 { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1180 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey },
1181 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey },
1182 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL },
1183 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL },
1184 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize },
1185 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1186 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1187 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1188 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1189 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL },
1190 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize },
1192 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey },
1194 /* NOTE: replace "13" with the actual index
1195 * of the first TLS-related line */
1196 #define aux_TLS (bindkey+13) /* beginning of TLS keywords */
1198 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1199 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1200 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1201 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1202 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1203 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1204 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 1, NULL },
1205 #ifdef HAVE_OPENSSL_CRL
1206 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1209 { BER_BVNULL, 0, 0, 0, NULL }
1214 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux)
1215 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux)
1218 * 'U': unsigned long
1222 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1224 int rc = SLAP_CONF_UNKNOWN;
1225 slap_cf_aux_table *tab;
1227 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) {
1228 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) {
1233 unsigned long *ulptr;
1234 struct berval *bptr;
1235 const char *val = word + tab->key.bv_len;
1237 switch ( tab->type ) {
1239 cptr = (char **)((char *)dst + tab->off);
1240 *cptr = ch_strdup( val );
1245 bptr = (struct berval *)((char *)dst + tab->off);
1246 if ( tab->aux != NULL ) {
1248 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux;
1250 ber_str2bv( val, 0, 0, &dn );
1251 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL );
1254 ber_str2bv( val, 0, 1, bptr );
1260 iptr = (int *)((char *)dst + tab->off);
1262 if ( tab->aux != NULL ) {
1263 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1265 assert( aux != NULL );
1268 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) {
1269 if ( !strcasecmp( val, aux[j].word.bv_val ) ) {
1270 *iptr = aux[j].mask;
1277 rc = lutil_atoix( iptr, val, 0 );
1282 uptr = (unsigned *)((char *)dst + tab->off);
1284 rc = lutil_atoux( uptr, val, 0 );
1288 lptr = (long *)((char *)dst + tab->off);
1290 rc = lutil_atolx( lptr, val, 0 );
1294 ulptr = (unsigned long *)((char *)dst + tab->off);
1296 rc = lutil_atoulx( ulptr, val, 0 );
1301 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1313 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1315 char buf[AC_LINE_MAX], *ptr;
1316 slap_cf_aux_table *tab;
1320 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1325 unsigned long *ulptr;
1326 struct berval *bptr;
1328 cptr = (char **)((char *)src + tab->off);
1330 switch ( tab->type ) {
1332 bptr = (struct berval *)((char *)src + tab->off);
1333 cptr = &bptr->bv_val;
1338 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1339 if ( tab->quote ) *ptr++ = '"';
1340 ptr = lutil_strcopy( ptr, *cptr );
1341 if ( tab->quote ) *ptr++ = '"';
1346 iptr = (int *)((char *)src + tab->off);
1348 if ( tab->aux != NULL ) {
1349 slap_verbmasks *aux = (slap_verbmasks *)tab->aux;
1351 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) {
1352 if ( *iptr == aux[i].mask ) {
1354 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1355 ptr = lutil_strcopy( ptr, aux[i].word.bv_val );
1362 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1363 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1368 uptr = (unsigned *)((char *)src + tab->off);
1370 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1371 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1375 lptr = (long *)((char *)src + tab->off);
1377 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1378 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1382 ulptr = (unsigned long *)((char *)src + tab->off);
1384 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1385 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1393 tmp.bv_len = ptr - buf;
1394 ber_dupbv( bv, &tmp );
1399 slap_tls_get_config( LDAP *ld, int opt, char **val )
1402 slap_verbmasks *keys;
1407 case LDAP_OPT_X_TLS_CRLCHECK:
1410 case LDAP_OPT_X_TLS_REQUIRE_CERT:
1413 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
1415 ldap_pvt_tls_get_option( ld, opt, &ival );
1416 snprintf( buf, sizeof( buf ), "%d.%d",
1417 ( ival >> 8 ) & 0xff, ival & 0xff );
1418 *val = ch_strdup( buf );
1424 ldap_pvt_tls_get_option( ld, opt, &ival );
1425 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
1426 if (keys[i].mask == ival) {
1427 *val = ch_strdup( keys[i].word.bv_val );
1436 bindconf_tls_parse( const char *word, slap_bindconf *bc )
1439 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1440 bc->sb_tls_do_init = 1;
1448 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv )
1451 return slap_cf_aux_table_unparse( bc, bv, aux_TLS );
1457 bindconf_parse( const char *word, slap_bindconf *bc )
1460 /* Detect TLS config changes explicitly */
1461 if ( bindconf_tls_parse( word, bc ) == 0 ) {
1465 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1469 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1471 return slap_cf_aux_table_unparse( bc, bv, bindkey );
1474 void bindconf_free( slap_bindconf *bc ) {
1475 if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1476 ch_free( bc->sb_uri.bv_val );
1477 BER_BVZERO( &bc->sb_uri );
1479 if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1480 ch_free( bc->sb_binddn.bv_val );
1481 BER_BVZERO( &bc->sb_binddn );
1483 if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1484 ch_free( bc->sb_cred.bv_val );
1485 BER_BVZERO( &bc->sb_cred );
1487 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1488 ch_free( bc->sb_saslmech.bv_val );
1489 BER_BVZERO( &bc->sb_saslmech );
1491 if ( bc->sb_secprops ) {
1492 ch_free( bc->sb_secprops );
1493 bc->sb_secprops = NULL;
1495 if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1496 ch_free( bc->sb_realm.bv_val );
1497 BER_BVZERO( &bc->sb_realm );
1499 if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1500 ch_free( bc->sb_authcId.bv_val );
1501 BER_BVZERO( &bc->sb_authcId );
1503 if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1504 ch_free( bc->sb_authzId.bv_val );
1505 BER_BVZERO( &bc->sb_authzId );
1508 if ( bc->sb_tls_cert ) {
1509 ch_free( bc->sb_tls_cert );
1510 bc->sb_tls_cert = NULL;
1512 if ( bc->sb_tls_key ) {
1513 ch_free( bc->sb_tls_key );
1514 bc->sb_tls_key = NULL;
1516 if ( bc->sb_tls_cacert ) {
1517 ch_free( bc->sb_tls_cacert );
1518 bc->sb_tls_cacert = NULL;
1520 if ( bc->sb_tls_cacertdir ) {
1521 ch_free( bc->sb_tls_cacertdir );
1522 bc->sb_tls_cacertdir = NULL;
1524 if ( bc->sb_tls_reqcert ) {
1525 ch_free( bc->sb_tls_reqcert );
1526 bc->sb_tls_reqcert = NULL;
1528 if ( bc->sb_tls_cipher_suite ) {
1529 ch_free( bc->sb_tls_cipher_suite );
1530 bc->sb_tls_cipher_suite = NULL;
1532 if ( bc->sb_tls_protocol_min ) {
1533 ch_free( bc->sb_tls_protocol_min );
1534 bc->sb_tls_protocol_min = NULL;
1536 #ifdef HAVE_OPENSSL_CRL
1537 if ( bc->sb_tls_crlcheck ) {
1538 ch_free( bc->sb_tls_crlcheck );
1539 bc->sb_tls_crlcheck = NULL;
1546 bindconf_tls_defaults( slap_bindconf *bc )
1549 if ( bc->sb_tls_do_init ) {
1550 if ( !bc->sb_tls_cacert )
1551 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE,
1552 &bc->sb_tls_cacert );
1553 if ( !bc->sb_tls_cacertdir )
1554 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR,
1555 &bc->sb_tls_cacertdir );
1556 if ( !bc->sb_tls_cert )
1557 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE,
1559 if ( !bc->sb_tls_key )
1560 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE,
1562 if ( !bc->sb_tls_cipher_suite )
1563 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE,
1564 &bc->sb_tls_cipher_suite );
1565 if ( !bc->sb_tls_reqcert )
1566 bc->sb_tls_reqcert = ch_strdup("demand");
1567 #ifdef HAVE_OPENSSL_CRL
1568 if ( !bc->sb_tls_crlcheck )
1569 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK,
1570 &bc->sb_tls_crlcheck );
1582 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1583 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1584 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1585 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1586 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1587 { "tls_protocol_min", offsetof(slap_bindconf, sb_tls_protocol_min), LDAP_OPT_X_TLS_PROTOCOL_MIN },
1591 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1593 int i, rc, newctx = 0, res = 0;
1594 char *ptr = (char *)bc, **word;
1596 bc->sb_tls_do_init = 0;
1598 for (i=0; bindtlsopts[i].opt; i++) {
1599 word = (char **)(ptr + bindtlsopts[i].offset);
1601 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1603 Debug( LDAP_DEBUG_ANY,
1604 "bindconf_tls_set: failed to set %s to %s\n",
1605 bindtlsopts[i].key, *word, 0 );
1611 if ( bc->sb_tls_reqcert ) {
1612 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1613 bc->sb_tls_reqcert );
1615 Debug( LDAP_DEBUG_ANY,
1616 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1617 bc->sb_tls_reqcert, 0, 0 );
1622 if ( bc->sb_tls_protocol_min ) {
1623 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN,
1624 bc->sb_tls_protocol_min );
1626 Debug( LDAP_DEBUG_ANY,
1627 "bindconf_tls_set: failed to set tls_protocol_min to %s\n",
1628 bc->sb_tls_protocol_min, 0, 0 );
1633 #ifdef HAVE_OPENSSL_CRL
1634 if ( bc->sb_tls_crlcheck ) {
1635 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1636 bc->sb_tls_crlcheck );
1638 Debug( LDAP_DEBUG_ANY,
1639 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1640 bc->sb_tls_crlcheck, 0, 0 );
1649 if ( bc->sb_tls_ctx ) {
1650 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx );
1651 bc->sb_tls_ctx = NULL;
1653 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1657 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1665 * connect to a client using the bindconf data
1666 * note: should move "version" into bindconf...
1669 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1675 /* Init connection to master */
1676 rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1677 if ( rc != LDAP_SUCCESS ) {
1678 Debug( LDAP_DEBUG_ANY,
1679 "slap_client_connect: "
1680 "ldap_initialize(%s) failed (%d)\n",
1681 sb->sb_uri.bv_val, rc, 0 );
1685 if ( sb->sb_version != 0 ) {
1686 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1687 (const void *)&sb->sb_version );
1690 if ( sb->sb_timeout_api ) {
1691 tv.tv_sec = sb->sb_timeout_api;
1693 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv );
1696 if ( sb->sb_timeout_net ) {
1697 tv.tv_sec = sb->sb_timeout_net;
1699 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv );
1703 if ( sb->sb_tls_do_init ) {
1704 rc = bindconf_tls_set( sb, ld );
1706 } else if ( sb->sb_tls_ctx ) {
1707 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1712 Debug( LDAP_DEBUG_ANY,
1713 "slap_client_connect: "
1714 "URI=%s TLS context initialization failed (%d)\n",
1715 sb->sb_uri.bv_val, rc, 0 );
1722 rc = ldap_start_tls_s( ld, NULL, NULL );
1723 if ( rc != LDAP_SUCCESS ) {
1724 Debug( LDAP_DEBUG_ANY,
1725 "slap_client_connect: URI=%s "
1726 "%s, ldap_start_tls failed (%d)\n",
1728 sb->sb_tls == SB_TLS_CRITICAL ?
1729 "Error" : "Warning",
1731 if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1737 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1738 #ifdef HAVE_CYRUS_SASL
1741 if ( sb->sb_secprops != NULL ) {
1742 rc = ldap_set_option( ld,
1743 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1745 if( rc != LDAP_OPT_SUCCESS ) {
1746 Debug( LDAP_DEBUG_ANY,
1747 "slap_client_connect: "
1748 "error, ldap_set_option "
1749 "(%s,SECPROPS,\"%s\") failed!\n",
1750 sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1755 defaults = lutil_sasl_defaults( ld,
1756 sb->sb_saslmech.bv_val,
1757 sb->sb_realm.bv_val,
1758 sb->sb_authcId.bv_val,
1760 sb->sb_authzId.bv_val );
1761 if ( defaults == NULL ) {
1766 rc = ldap_sasl_interactive_bind_s( ld,
1767 sb->sb_binddn.bv_val,
1768 sb->sb_saslmech.bv_val,
1771 lutil_sasl_interact,
1774 lutil_sasl_freedefs( defaults );
1776 /* FIXME: different error behaviors according to
1778 * 2) on err policy : exit, retry, backoff ...
1780 if ( rc != LDAP_SUCCESS ) {
1781 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1783 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1784 "ldap_sasl_interactive_bind_s failed (%d)\n",
1785 sb->sb_uri.bv_val, rc, 0 );
1787 /* FIXME (see above comment) */
1788 /* if Kerberos credentials cache is not active, retry */
1789 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1790 rc == LDAP_LOCAL_ERROR )
1792 rc = LDAP_SERVER_DOWN;
1797 #else /* HAVE_CYRUS_SASL */
1798 /* Should never get here, we trapped this at config time */
1800 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1805 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1806 rc = ldap_sasl_bind_s( ld,
1807 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1808 &sb->sb_cred, NULL, NULL, NULL );
1809 if ( rc != LDAP_SUCCESS ) {
1810 Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1812 "ldap_sasl_bind_s failed (%d)\n",
1813 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1821 ldap_unbind_ext( ld, NULL, NULL );
1832 /* -------------------------------------- */
1836 strtok_quote( char *line, char *sep, char **quote_ptr )
1843 if ( line != NULL ) {
1846 while ( *next && strchr( sep, *next ) ) {
1850 if ( *next == '\0' ) {
1856 for ( inquote = 0; *next; ) {
1864 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1870 next + 1, strlen( next + 1 ) + 1 );
1871 next++; /* dont parse the escaped character */
1876 if ( strchr( sep, *next ) != NULL ) {
1890 static char buf[AC_LINE_MAX];
1892 static size_t lmax, lcur;
1894 #define CATLINE( buf ) \
1896 size_t len = strlen( buf ); \
1897 while ( lcur + len + 1 > lmax ) { \
1898 lmax += AC_LINE_MAX; \
1899 line = (char *) ch_realloc( line, lmax ); \
1901 strcpy( line + lcur, buf ); \
1906 fp_getline_init(ConfigArgs *c) {
1912 fp_getline( FILE *fp, ConfigArgs *c )
1920 /* avoid stack of bufs */
1921 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1927 while ( fgets( buf, sizeof( buf ), fp ) ) {
1928 p = strchr( buf, '\n' );
1930 if ( p > buf && p[-1] == '\r' ) {
1938 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1945 if ( !isspace( (unsigned char)buf[0] ) ) {
1956 return(line[0] ? 1 : 0);
1960 config_fp_parse_line(ConfigArgs *c)
1963 static char *const hide[] = {
1964 "rootpw", "replica", "syncrepl", /* in slapd */
1965 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */
1966 "acl-passwd", "bindpw", /* in back-<ldap/meta> */
1967 "pseudorootpw", /* in back-meta */
1968 "dbpasswd", /* in back-sql */
1972 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1974 c->tline = ch_strdup(c->line);
1975 token = strtok_quote(c->tline, " \t", "e_ptr);
1977 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1978 if(quote_ptr) *quote_ptr = ' ';
1979 Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1980 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1981 if(quote_ptr) *quote_ptr = '\0';
1983 for(;; token = strtok_quote(NULL, " \t", "e_ptr)) {
1984 if(c->argc >= c->argv_size) {
1986 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1988 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1992 c->argv_size += ARGS_STEP;
1996 c->argv[c->argc++] = token;
1998 c->argv[c->argc] = NULL;
2005 ucdata_unload( UCDATA_ALL );
2007 /* NOTE: in case of early exit, frontendDB can be NULL */
2008 if ( frontendDB->be_schemandn.bv_val )
2009 free( frontendDB->be_schemandn.bv_val );
2010 if ( frontendDB->be_schemadn.bv_val )
2011 free( frontendDB->be_schemadn.bv_val );
2012 if ( frontendDB->be_acl )
2013 acl_destroy( frontendDB->be_acl );
2016 if ( slapd_args_file )
2017 free ( slapd_args_file );
2018 if ( slapd_pid_file )
2019 free ( slapd_pid_file );
2020 if ( default_passwd_hash )
2021 ldap_charray_free( default_passwd_hash );
2025 slap_str2clist( char ***out, char *in, const char *brkstr )
2033 /* find last element in list */
2034 for (i = 0; *out && (*out)[i]; i++);
2036 /* protect the input string from strtok */
2037 str = ch_strdup( in );
2039 if ( *str == '\0' ) {
2044 /* Count words in string */
2046 for ( s = str; *s; s++ ) {
2047 if ( strchr( brkstr, *s ) != NULL ) {
2052 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2054 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2056 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2058 *new = ch_strdup( s );
2067 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
2068 int argc, char **argv )
2070 ConfigArgs c = { 0 };
2081 c.op = SLAP_CONFIG_ADD;
2082 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
2084 rc = SLAP_CONF_UNKNOWN;
2085 ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
2087 c.table = be->be_cf_ocs->co_type;
2088 rc = config_add_vals( ct, &c );