]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
ae8c5feb654d009d1a68a25279208d27caa77638
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
35 #include <ac/errno.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include "slap.h"
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45 #include "lutil.h"
46 #include "config.h"
47
48 #ifdef HAVE_TLS
49 #include <openssl/ssl.h>
50 #endif
51
52 #define ARGS_STEP       512
53
54 /*
55  * defaults for various global variables
56  */
57 slap_mask_t             global_allows = 0;
58 slap_mask_t             global_disallows = 0;
59 int             global_gentlehup = 0;
60 int             global_idletimeout = 0;
61 char    *global_host = NULL;
62 char    *global_realm = NULL;
63 char            *ldap_srvtab = "";
64 char            **default_passwd_hash = NULL;
65 struct berval default_search_base = BER_BVNULL;
66 struct berval default_search_nbase = BER_BVNULL;
67
68 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
69 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
70
71 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
72 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
73
74 char   *slapd_pid_file  = NULL;
75 char   *slapd_args_file = NULL;
76
77 int use_reverse_lookup = 0;
78
79 #ifdef LDAP_SLAPI
80 int slapi_plugins_used = 0;
81 #endif
82
83 static int fp_getline(FILE *fp, ConfigArgs *c);
84 static void fp_getline_init(ConfigArgs *c);
85 static int fp_parse_line(ConfigArgs *c);
86
87 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
88 static char *strtok_quote_ldif(char **line);
89
90 ConfigArgs *
91 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
92 {
93         ConfigArgs *c;
94         c = ch_calloc( 1, sizeof( ConfigArgs ) );
95         if ( c == NULL ) return(NULL);
96         c->be     = be; 
97         c->fname  = fname;
98         c->argc   = argc;
99         c->argv   = argv; 
100         c->lineno = lineno;
101         snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
102         return(c);
103 }
104
105 void
106 init_config_argv( ConfigArgs *c )
107 {
108         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
109         c->argv_size = ARGS_STEP + 1;
110 }
111
112 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
113         int i;
114
115         for(i = 0; Conf[i].name; i++)
116                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
117                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
118         if ( !Conf[i].name ) return NULL;
119         return Conf+i;
120 }
121
122 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
123         int rc, arg_user, arg_type, arg_syn, iarg;
124         long larg;
125         ber_len_t barg;
126         
127         if(Conf->arg_type == ARG_IGNORED) {
128                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
129                         c->log, Conf->name, 0);
130                 return(0);
131         }
132         arg_type = Conf->arg_type & ARGS_TYPES;
133         arg_user = Conf->arg_type & ARGS_USERLAND;
134         arg_syn = Conf->arg_type & ARGS_SYNTAX;
135
136         if((arg_type == ARG_DN) && c->argc == 1) {
137                 c->argc = 2;
138                 c->argv[1] = "";
139         }
140         if(Conf->min_args && (c->argc < Conf->min_args)) {
141                 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
142                         c->argv[0], Conf->what );
143                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
144                 return(ARG_BAD_CONF);
145         }
146         if(Conf->max_args && (c->argc > Conf->max_args)) {
147                 char    *ignored = " ignored";
148
149                 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
150                         c->argv[0], Conf->what );
151
152 #ifdef LDAP_DEVEL
153                 ignored = "";
154 #endif /* LDAP_DEVEL */
155                 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
156                                 c->log, c->msg, ignored );
157 #ifdef LDAP_DEVEL
158                 return(ARG_BAD_CONF);
159 #endif /* LDAP_DEVEL */
160         }
161         if((arg_syn & ARG_DB) && !c->be) {
162                 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
163                         c->argv[0] );
164                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
165                         c->log, c->msg, 0);
166                 return(ARG_BAD_CONF);
167         }
168         if((arg_syn & ARG_PRE_BI) && c->bi) {
169                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
170                         c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
171                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
172                         c->log, c->msg, 0 );
173                 return(ARG_BAD_CONF);
174         }
175         if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
176                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
177                         c->argv[0] );
178                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
179                         c->log, c->msg, 0);
180                 return(ARG_BAD_CONF);
181         }
182         if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
183                 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
184                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
185                         c->log, c->msg, 0);
186                 return(ARG_BAD_CONF);
187         }
188         if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
189                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
190                         c->argv[0] );
191                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
192                         c->log, c->msg, 0);
193                 return(ARG_BAD_CONF);
194         }
195         c->type = arg_user;
196         memset(&c->values, 0, sizeof(c->values));
197         if(arg_type == ARG_STRING) {
198                 if ( !check_only )
199                         c->value_string = ch_strdup(c->argv[1]);
200         } else if(arg_type == ARG_BERVAL) {
201                 if ( !check_only )
202                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
203         } else if(arg_type == ARG_DN) {
204                 struct berval bv;
205                 ber_str2bv( c->argv[1], 0, 0, &bv );
206                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
207                 if ( rc != LDAP_SUCCESS ) {
208                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
209                                 c->argv[0], rc, ldap_err2string( rc ));
210                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
211                         return(ARG_BAD_CONF);
212                 }
213                 if ( check_only ) {
214                         ch_free( c->value_ndn.bv_val );
215                         ch_free( c->value_dn.bv_val );
216                 }
217         } else {        /* all numeric */
218                 int j;
219                 iarg = 0; larg = 0; barg = 0;
220                 switch(arg_type) {
221                         case ARG_INT:
222                                 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
223                                         snprintf( c->msg, sizeof( c->msg ),
224                                                 "<%s> unable to parse \"%s\" as int",
225                                                 c->argv[0], c->argv[1] );
226                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
227                                                 c->log, c->msg, 0);
228                                         return(ARG_BAD_CONF);
229                                 }
230                                 break;
231                         case ARG_LONG:
232                                 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
233                                         snprintf( c->msg, sizeof( c->msg ),
234                                                 "<%s> unable to parse \"%s\" as long",
235                                                 c->argv[0], c->argv[1] );
236                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
237                                                 c->log, c->msg, 0);
238                                         return(ARG_BAD_CONF);
239                                 }
240                                 break;
241                         case ARG_BER_LEN_T: {
242                                 unsigned long   l;
243                                 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
244                                         snprintf( c->msg, sizeof( c->msg ),
245                                                 "<%s> unable to parse \"%s\" as ber_len_t",
246                                                 c->argv[0], c->argv[1] );
247                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
248                                                 c->log, c->msg, 0);
249                                         return(ARG_BAD_CONF);
250                                 }
251                                 barg = (ber_len_t)l;
252                                 } break;
253                         case ARG_ON_OFF:
254                                 if (c->argc == 1) {
255                                         iarg = 1;
256                                 } else if ( !strcasecmp(c->argv[1], "on") ||
257                                         !strcasecmp(c->argv[1], "true") ||
258                                         !strcasecmp(c->argv[1], "yes") )
259                                 {
260                                         iarg = 1;
261                                 } else if ( !strcasecmp(c->argv[1], "off") ||
262                                         !strcasecmp(c->argv[1], "false") ||
263                                         !strcasecmp(c->argv[1], "no") )
264                                 {
265                                         iarg = 0;
266                                 } else {
267                                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
268                                                 c->argv[0] );
269                                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
270                                                 c->log, c->msg, 0 );
271                                         return(ARG_BAD_CONF);
272                                 }
273                                 break;
274                 }
275                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
276                 if(iarg < j && larg < j && barg < j ) {
277                         larg = larg ? larg : (barg ? barg : iarg);
278                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
279                                 c->argv[0] );
280                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
281                                 c->log, c->msg, 0 );
282                         return(ARG_BAD_CONF);
283                 }
284                 switch(arg_type) {
285                         case ARG_ON_OFF:
286                         case ARG_INT:           c->value_int = iarg;            break;
287                         case ARG_LONG:          c->value_long = larg;           break;
288                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
289                 }
290         }
291         return 0;
292 }
293
294 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
295         int rc, arg_type;
296         void *ptr = NULL;
297
298         arg_type = Conf->arg_type;
299         if(arg_type & ARG_MAGIC) {
300                 if(!c->be) c->be = frontendDB;
301                 c->msg[0] = '\0';
302                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
303 #if 0
304                 if(c->be == frontendDB) c->be = NULL;
305 #endif
306                 if(rc) {
307                         if ( !c->msg[0] ) {
308                                 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
309                                         c->argv[0], rc );
310                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
311                                         c->log, c->msg, 0 );
312                         }
313                         return(ARG_BAD_CONF);
314                 }
315                 return(0);
316         }
317         if(arg_type & ARG_OFFSET) {
318                 if (c->be && (!overlay_is_over(c->be) || 
319                         ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
320                         ptr = c->be->be_private;
321                 else if (c->bi)
322                         ptr = c->bi->bi_private;
323                 else {
324                         snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
325                                 c->argv[0] );
326                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
327                                 c->log, c->msg, 0);
328                         return(ARG_BAD_CONF);
329                 }
330                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
331         } else if (arg_type & ARGS_TYPES) {
332                 ptr = Conf->arg_item;
333         }
334         if(arg_type & ARGS_TYPES)
335                 switch(arg_type & ARGS_TYPES) {
336                         case ARG_ON_OFF:
337                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
338                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
339                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
340                         case ARG_STRING: {
341                                 char *cc = *(char**)ptr;
342                                 if(cc) {
343                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
344                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
345                                                         c->log, Conf->name, 0 );
346                                                 return(ARG_BAD_CONF);
347                                         }
348                                         ch_free(cc);
349                                 }
350                                 *(char **)ptr = c->value_string;
351                                 break;
352                                 }
353                         case ARG_BERVAL:
354                                 *(struct berval *)ptr = c->value_bv;
355                                 break;
356                 }
357         return(0);
358 }
359
360 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
361         int rc, arg_type;
362
363         arg_type = Conf->arg_type;
364         if(arg_type == ARG_IGNORED) {
365                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
366                         c->log, Conf->name, 0);
367                 return(0);
368         }
369         rc = config_check_vals( Conf, c, 0 );
370         if ( rc ) return rc;
371         return config_set_vals( Conf, c );
372 }
373
374 int
375 config_del_vals(ConfigTable *cf, ConfigArgs *c)
376 {
377         int rc = 0;
378
379         /* If there is no handler, just ignore it */
380         if ( cf->arg_type & ARG_MAGIC ) {
381                 c->op = LDAP_MOD_DELETE;
382                 c->type = cf->arg_type & ARGS_USERLAND;
383                 rc = (*((ConfigDriver*)cf->arg_item))(c);
384         }
385         return rc;
386 }
387
388 int
389 config_get_vals(ConfigTable *cf, ConfigArgs *c)
390 {
391         int rc = 0;
392         struct berval bv;
393         void *ptr;
394
395         if ( cf->arg_type & ARG_IGNORED ) {
396                 return 1;
397         }
398
399         memset(&c->values, 0, sizeof(c->values));
400         c->rvalue_vals = NULL;
401         c->rvalue_nvals = NULL;
402         c->op = SLAP_CONFIG_EMIT;
403         c->type = cf->arg_type & ARGS_USERLAND;
404
405         if ( cf->arg_type & ARG_MAGIC ) {
406                 rc = (*((ConfigDriver*)cf->arg_item))(c);
407                 if ( rc ) return rc;
408         } else {
409                 if ( cf->arg_type & ARG_OFFSET ) {
410                         if (c->be && (!overlay_is_over(c->be) || 
411                                 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
412                                 ptr = c->be->be_private;
413                         else if ( c->bi )
414                                 ptr = c->bi->bi_private;
415                         else
416                                 return 1;
417                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
418                 } else {
419                         ptr = cf->arg_item;
420                 }
421                 
422                 switch(cf->arg_type & ARGS_TYPES) {
423                 case ARG_ON_OFF:
424                 case ARG_INT:   c->value_int = *(int *)ptr; break;
425                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
426                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
427                 case ARG_STRING:
428                         if ( *(char **)ptr )
429                                 c->value_string = ch_strdup(*(char **)ptr);
430                         break;
431                 case ARG_BERVAL:
432                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
433                 }
434         }
435         if ( cf->arg_type & ARGS_TYPES) {
436                 bv.bv_len = 0;
437                 bv.bv_val = c->log;
438                 switch(cf->arg_type & ARGS_TYPES) {
439                 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
440                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
441                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
442                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
443                         c->value_int ? "TRUE" : "FALSE"); break;
444                 case ARG_STRING:
445                         if ( c->value_string && c->value_string[0]) {
446                                 ber_str2bv( c->value_string, 0, 0, &bv);
447                         } else {
448                                 return 1;
449                         }
450                         break;
451                 case ARG_BERVAL:
452                         if ( !BER_BVISEMPTY( &c->value_bv )) {
453                                 bv = c->value_bv;
454                         } else {
455                                 return 1;
456                         }
457                         break;
458                 default:
459                         bv.bv_val = NULL;
460                         break;
461                 }
462                 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
463                         return 1;
464                 }
465                 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
466                         ber_bvarray_add(&c->rvalue_vals, &bv);
467                 } else if ( !BER_BVISNULL( &bv ) ) {
468                         value_add_one(&c->rvalue_vals, &bv);
469                 }
470                 /* else: maybe c->rvalue_vals already set? */
471         }
472         return rc;
473 }
474
475 int
476 init_config_attrs(ConfigTable *ct) {
477         LDAPAttributeType *at;
478         int i, code;
479         const char *err;
480
481         for (i=0; ct[i].name; i++ ) {
482                 int             freeit = 0;
483
484                 if ( !ct[i].attribute ) continue;
485                 at = ldap_str2attributetype( ct[i].attribute,
486                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
487                 if ( !at ) {
488                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
489                                 ct[i].attribute, ldap_scherr2str(code), err );
490                         return code;
491                 }
492
493                 code = at_add( at, 0, NULL, &err );
494                 if ( code ) {
495                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
496                                 freeit = 1;
497
498                         } else {
499                                 ldap_attributetype_free( at );
500                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
501                                         ct[i].attribute, scherr2str(code), err );
502                                 return code;
503                         }
504                 }
505                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
506                 if ( freeit || code ) {
507                         ldap_attributetype_free( at );
508                 } else {
509                         ldap_memfree( at );
510                 }
511                 if ( code ) {
512                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
513                                 ct[i].attribute, err );
514                         return code;
515                 }
516         }
517
518         return 0;
519 }
520
521 int
522 init_config_ocs( ConfigOCs *ocs ) {
523         int i;
524
525         for (i=0;ocs[i].co_def;i++) {
526                 LDAPObjectClass *oc;
527                 int code;
528                 const char *err;
529
530                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
531                         LDAP_SCHEMA_ALLOW_ALL );
532                 if ( !oc ) {
533                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
534                                 ocs[i].co_def, ldap_scherr2str(code), err );
535                         return code;
536                 }
537                 code = oc_add(oc,0,NULL,&err);
538                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
539                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
540                                 ocs[i].co_def, scherr2str(code), err );
541                         ldap_objectclass_free(oc);
542                         return code;
543                 }
544                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
545                 if ( code )
546                         ldap_objectclass_free(oc);
547                 else
548                         ldap_memfree(oc);
549         }
550         return 0;
551 }
552
553 /* Split an LDIF line into space-separated tokens. Words may be grouped
554  * by quotes. A quoted string may begin in the middle of a word, but must
555  * end at the end of the word (be followed by whitespace or EOS). Any other
556  * quotes are passed through unchanged. All other characters are passed
557  * through unchanged.
558  */
559 static char *
560 strtok_quote_ldif( char **line )
561 {
562         char *beg, *ptr, *quote=NULL;
563         int inquote=0;
564
565         ptr = *line;
566
567         if ( !ptr || !*ptr )
568                 return NULL;
569
570         while( isspace( (unsigned char) *ptr )) ptr++;
571
572         if ( *ptr == '"' ) {
573                 inquote = 1;
574                 ptr++;
575         }
576
577         beg = ptr;
578
579         for (;*ptr;ptr++) {
580                 if ( *ptr == '"' ) {
581                         if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
582                                 *ptr++ = '\0';
583                                 break;
584                         }
585                         inquote = 1;
586                         quote = ptr;
587                         continue;
588                 }
589                 if ( inquote )
590                         continue;
591                 if ( isspace( (unsigned char) *ptr )) {
592                         *ptr++ = '\0';
593                         break;
594                 }
595         }
596         if ( quote ) {
597                 while ( quote < ptr ) {
598                         *quote = quote[1];
599                         quote++;
600                 }
601         }
602         if ( !*ptr ) {
603                 *line = NULL;
604         } else {
605                 while ( isspace( (unsigned char) *ptr )) ptr++;
606                 *line = ptr;
607         }
608         return beg;
609 }
610
611 static void
612 config_parse_ldif( ConfigArgs *c )
613 {
614         char *next;
615         c->tline = ch_strdup(c->line);
616         next = c->tline;
617
618         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
619                 c->argc++;
620                 if ( c->argc >= c->argv_size ) {
621                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
622                                 sizeof( *c->argv ));
623                         c->argv = tmp;
624                         c->argv_size += ARGS_STEP;
625                 }
626         }
627         c->argv[c->argc] = NULL;
628 }
629
630 int
631 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
632 {
633         int     rc = 0;
634
635         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
636                 ct->ad->ad_cname.bv_val, valx );
637         c->argc = 1;
638         c->argv[0] = ct->ad->ad_cname.bv_val;
639
640         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
641                 c->argv[c->argc] = c->line;
642                 c->argc++;
643                 c->argv[c->argc] = NULL;
644                 c->tline = NULL;
645         } else {
646                 config_parse_ldif( c );
647         }
648         rc = config_check_vals( ct, c, 1 );
649         ch_free( c->tline );
650         c->tline = NULL;
651
652         if ( rc )
653                 rc = LDAP_CONSTRAINT_VIOLATION;
654
655         return rc;
656 }
657
658 int
659 config_parse_add(ConfigTable *ct, ConfigArgs *c)
660 {
661         int     rc = 0;
662
663         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
664                 ct->ad->ad_cname.bv_val, c->valx );
665         c->argc = 1;
666         c->argv[0] = ct->ad->ad_cname.bv_val;
667
668         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
669                 c->argv[c->argc] = c->line;
670                 c->argc++;
671                 c->argv[c->argc] = NULL;
672                 c->tline = NULL;
673         } else {
674                 config_parse_ldif( c );
675         }
676         c->op = LDAP_MOD_ADD;
677         rc = config_add_vals( ct, c );
678         ch_free( c->tline );
679
680         return rc;
681 }
682
683 int
684 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
685 {
686         FILE *fp;
687         ConfigTable *ct;
688         ConfigArgs *c;
689         int rc;
690         struct stat s;
691
692         c = ch_calloc( 1, sizeof( ConfigArgs ) );
693         if ( c == NULL ) {
694                 return 1;
695         }
696
697         if ( depth ) {
698                 memcpy( c, cf, sizeof( ConfigArgs ) );
699         } else {
700                 c->depth = depth; /* XXX */
701                 c->bi = NULL;
702                 c->be = NULL;
703         }
704
705         c->valx = -1;
706         c->fname = fname;
707         init_config_argv( c );
708
709         if ( stat( fname, &s ) != 0 ) {
710                 ldap_syslog = 1;
711                 Debug(LDAP_DEBUG_ANY,
712                     "could not stat config file \"%s\": %s (%d)\n",
713                     fname, strerror(errno), errno);
714                 ch_free( c );
715                 return(1);
716         }
717
718         if ( !S_ISREG( s.st_mode ) ) {
719                 ldap_syslog = 1;
720                 Debug(LDAP_DEBUG_ANY,
721                     "regular file expected, got \"%s\"\n",
722                     fname, 0, 0 );
723                 ch_free( c );
724                 return(1);
725         }
726
727         fp = fopen( fname, "r" );
728         if ( fp == NULL ) {
729                 ldap_syslog = 1;
730                 Debug(LDAP_DEBUG_ANY,
731                     "could not open config file \"%s\": %s (%d)\n",
732                     fname, strerror(errno), errno);
733                 ch_free( c );
734                 return(1);
735         }
736
737         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
738
739         fp_getline_init(c);
740
741         c->tline = NULL;
742
743         while ( fp_getline( fp, c ) ) {
744                 /* skip comments and blank lines */
745                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
746                         continue;
747                 }
748
749                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
750                                 c->fname, c->lineno );
751
752                 c->argc = 0;
753                 ch_free( c->tline );
754                 if ( fp_parse_line( c ) ) {
755                         rc = 1;
756                         goto done;
757                 }
758
759                 if ( c->argc < 1 ) {
760                         Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
761                                 c->log, 0, 0);
762                         rc = 1;
763                         goto done;
764                 }
765
766                 c->op = SLAP_CONFIG_ADD;
767
768                 ct = config_find_keyword( cft, c );
769                 if ( ct ) {
770                         rc = config_add_vals( ct, c );
771                         if ( !rc ) continue;
772
773                         if ( rc & ARGS_USERLAND ) {
774                                 /* XXX a usertype would be opaque here */
775                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
776                                         c->log, c->argv[0], 0);
777                                 rc = 1;
778                                 goto done;
779
780                         } else if ( rc == ARG_BAD_CONF ) {
781                                 rc = 1;
782                                 goto done;
783                         }
784                         
785                 } else if ( c->bi && !c->be ) {
786                         rc = SLAP_CONF_UNKNOWN;
787                         if ( c->bi->bi_cf_ocs ) {
788                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
789                                 if ( ct ) {
790                                         rc = config_add_vals( ct, c );
791                                 }
792                         }
793                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
794                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
795                                         c->argc, c->argv);
796                         }
797                         if ( rc ) {
798                                 switch(rc) {
799                                 case SLAP_CONF_UNKNOWN:
800                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
801                                                 "<%s> inside backend info definition.\n",
802                                                 c->log, *c->argv, 0);
803                                 default:
804                                         rc = 1;
805                                         goto done;
806                                 }
807                         }
808
809                 } else if ( c->be && c->be != frontendDB ) {
810                         rc = SLAP_CONF_UNKNOWN;
811                         if ( c->be->be_cf_ocs ) {
812                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
813                                 if ( ct ) {
814                                         rc = config_add_vals( ct, c );
815                                 }
816                         }
817                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
818                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
819                                         c->argc, c->argv);
820                         }
821                         if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
822                         {
823                                 /* global overlays may need 
824                                  * definitions inside other databases...
825                                  */
826                                 rc = (*frontendDB->be_config)( frontendDB,
827                                         c->fname, (int)c->lineno, c->argc, c->argv );
828                         }
829
830                         switch ( rc ) {
831                         case 0:
832                                 break;
833
834                         case SLAP_CONF_UNKNOWN:
835                                 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
836                                         "<%s> inside backend database definition.\n",
837                                         c->log, *c->argv, 0);
838                                 
839                         default:
840                                 rc = 1;
841                                 goto done;
842                         }
843
844                 } else if ( frontendDB->be_config ) {
845                         rc = (*frontendDB->be_config)( frontendDB,
846                                 c->fname, (int)c->lineno, c->argc, c->argv);
847                         if ( rc ) {
848                                 switch(rc) {
849                                 case SLAP_CONF_UNKNOWN:
850                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
851                                                 "<%s> inside global database definition.\n",
852                                                 c->log, *c->argv, 0);
853
854                                 default:
855                                         rc = 1;
856                                         goto done;
857                                 }
858                         }
859                         
860                 } else {
861                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
862                                 "<%s> outside backend info and database definitions.\n",
863                                 c->log, *c->argv, 0);
864                         rc = 1;
865                         goto done;
866                 }
867         }
868
869         rc = 0;
870
871 done:
872         ch_free(c->tline);
873         fclose(fp);
874         ch_free(c->argv);
875         ch_free(c);
876         return(rc);
877 }
878
879 /* restrictops, allows, disallows, requires, loglevel */
880
881 int
882 verb_to_mask(const char *word, slap_verbmasks *v) {
883         int i;
884         for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
885                 if(!strcasecmp(word, v[i].word.bv_val)) break;
886         }
887         return(i);
888 }
889
890 int
891 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
892         int i, j;
893         for(i = 1; i < argc; i++) {
894                 j = verb_to_mask(argv[i], v);
895                 if(BER_BVISNULL(&v[j].word)) return i;
896                 while (!v[j].mask) j--;
897                 *m |= v[j].mask;
898         }
899         return(0);
900 }
901
902 /* Mask keywords that represent multiple bits should occur before single
903  * bit keywords in the verbmasks array.
904  */
905 int
906 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
907         int i, rc = 1;
908
909         if (m) {
910                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
911                         if (!v[i].mask) continue;
912                         if (( m & v[i].mask ) == v[i].mask ) {
913                                 value_add_one( bva, &v[i].word );
914                                 rc = 0;
915                                 m ^= v[i].mask;
916                                 if ( !m ) break;
917                         }
918                 }
919         }
920         return rc;
921 }
922
923 int
924 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
925 {
926         int             i;
927
928         assert( *vp == NULL );
929
930         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
931
932         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
933
934         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
935                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
936                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
937         }
938
939         BER_BVZERO( &(*vp)[ i ].word );
940
941         return 0;               
942 }
943
944 int
945 slap_verbmasks_destroy( slap_verbmasks *v )
946 {
947         int             i;
948
949         assert( v != NULL );
950
951         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
952                 ch_free( v[ i ].word.bv_val );
953         }
954
955         ch_free( v );
956
957         return 0;
958 }
959
960 int
961 slap_verbmasks_append(
962         slap_verbmasks  **vp,
963         slap_mask_t     m,
964         struct berval   *v,
965         slap_mask_t     *ignore )
966 {
967         int     i;
968
969         if ( !m ) {
970                 return LDAP_OPERATIONS_ERROR;
971         }
972
973         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
974                 if ( !(*vp)[ i ].mask ) continue;
975
976                 if ( ignore != NULL ) {
977                         int     j;
978
979                         for ( j = 0; ignore[ j ] != 0; j++ ) {
980                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
981                                         goto check_next;
982                                 }
983                         }
984                 }
985
986                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
987                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
988                                 /* already set; ignore */
989                                 return LDAP_SUCCESS;
990                         }
991                         /* conflicts */
992                         return LDAP_TYPE_OR_VALUE_EXISTS;
993                 }
994
995                 if ( m & (*vp)[ i ].mask ) {
996                         /* conflicts */
997                         return LDAP_CONSTRAINT_VIOLATION;
998                 }
999 check_next:;
1000         }
1001
1002         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1003         ber_dupbv( &(*vp)[ i ].word, v );
1004         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1005         BER_BVZERO( &(*vp)[ i + 1 ].word );
1006
1007         return LDAP_SUCCESS;
1008 }
1009
1010 int
1011 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1012         int i;
1013
1014         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1015                 if ( m == v[i].mask ) {
1016                         if ( bv != NULL ) {
1017                                 *bv = v[i].word;
1018                         }
1019                         return i;
1020                 }
1021         }
1022         return -1;
1023 }
1024
1025 #ifdef HAVE_TLS
1026 static slap_verbmasks tlskey[] = {
1027         { BER_BVC("no"),        SB_TLS_OFF },
1028         { BER_BVC("yes"),       SB_TLS_ON },
1029         { BER_BVC("critical"),  SB_TLS_CRITICAL },
1030         { BER_BVNULL, 0 }
1031 };
1032 #endif
1033
1034 static slap_verbmasks methkey[] = {
1035         { BER_BVC("none"),      LDAP_AUTH_NONE },
1036         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
1037 #ifdef HAVE_CYRUS_SASL
1038         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1039 #endif
1040         { BER_BVNULL, 0 }
1041 };
1042
1043 static slap_cf_aux_table bindkey[] = {
1044         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1045         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1046         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1047         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1048         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1049         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1050         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1051         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1052         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1053 #ifdef HAVE_TLS
1054         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1055
1056 #define aux_TLS (bindkey+10)    /* beginning of TLS keywords */
1057
1058         { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1059         { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1060         { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1061         { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1062         { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1063         { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1064 #ifdef HAVE_OPENSSL_CRL
1065         { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1066 #endif
1067 #endif
1068         { BER_BVNULL, 0, 0, 0, NULL }
1069 };
1070
1071 int
1072 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1073 {
1074         int rc = SLAP_CONF_UNKNOWN;
1075         slap_cf_aux_table *tab;
1076
1077         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1078                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1079                         char **cptr;
1080                         int *iptr, j;
1081                         unsigned *uptr;
1082                         long *lptr;
1083                         unsigned long *ulptr;
1084                         struct berval *bptr;
1085                         const char *val = word + tab->key.bv_len;
1086
1087                         switch ( tab->type ) {
1088                         case 's':
1089                                 cptr = (char **)((char *)dst + tab->off);
1090                                 *cptr = ch_strdup( val );
1091                                 rc = 0;
1092                                 break;
1093
1094                         case 'b':
1095                                 bptr = (struct berval *)((char *)dst + tab->off);
1096                                 ber_str2bv( val, 0, 1, bptr );
1097                                 rc = 0;
1098                                 break;
1099
1100                         case 'd':
1101                                 assert( tab->aux != NULL );
1102                                 iptr = (int *)((char *)dst + tab->off);
1103
1104                                 rc = 1;
1105                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1106                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1107                                                 *iptr = tab->aux[j].mask;
1108                                                 rc = 0;
1109                                         }
1110                                 }
1111                                 break;
1112
1113                         case 'i':
1114                                 iptr = (int *)((char *)dst + tab->off);
1115
1116                                 rc = lutil_atoix( iptr, val, 0 );
1117                                 break;
1118
1119                         case 'u':
1120                                 uptr = (unsigned *)((char *)dst + tab->off);
1121
1122                                 rc = lutil_atoux( uptr, val, 0 );
1123                                 break;
1124
1125                         case 'I':
1126                                 lptr = (long *)((char *)dst + tab->off);
1127
1128                                 rc = lutil_atolx( lptr, val, 0 );
1129                                 break;
1130
1131                         case 'U':
1132                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1133
1134                                 rc = lutil_atoulx( ulptr, val, 0 );
1135                                 break;
1136                         }
1137
1138                         if ( rc ) {
1139                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1140                                         tabmsg, word, 0 );
1141                         }
1142                         
1143                         return rc;
1144                 }
1145         }
1146
1147         return rc;
1148 }
1149
1150 int
1151 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1152 {
1153         char buf[BUFSIZ], *ptr;
1154         slap_cf_aux_table *tab;
1155         struct berval tmp;
1156
1157         ptr = buf;
1158         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1159                 char **cptr;
1160                 int *iptr, i;
1161                 unsigned *uptr;
1162                 long *lptr;
1163                 unsigned long *ulptr;
1164                 struct berval *bptr;
1165
1166                 cptr = (char **)((char *)src + tab->off);
1167
1168                 switch ( tab->type ) {
1169                 case 'b':
1170                         bptr = (struct berval *)((char *)src + tab->off);
1171                         cptr = &bptr->bv_val;
1172                 case 's':
1173                         if ( *cptr ) {
1174                                 *ptr++ = ' ';
1175                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1176                                 if ( tab->quote ) *ptr++ = '"';
1177                                 ptr = lutil_strcopy( ptr, *cptr );
1178                                 if ( tab->quote ) *ptr++ = '"';
1179                         }
1180                         break;
1181
1182                 case 'd':
1183                         assert( tab->aux != NULL );
1184                         iptr = (int *)((char *)src + tab->off);
1185                 
1186                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1187                                 if ( *iptr == tab->aux[i].mask ) {
1188                                         *ptr++ = ' ';
1189                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1190                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1191                                         break;
1192                                 }
1193                         }
1194                         break;
1195
1196                 case 'i':
1197                         iptr = (int *)((char *)src + tab->off);
1198                         *ptr++ = ' ';
1199                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1200                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1201                         break;
1202
1203                 case 'u':
1204                         uptr = (unsigned *)((char *)src + tab->off);
1205                         *ptr++ = ' ';
1206                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1207                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1208                         break;
1209
1210                 case 'I':
1211                         lptr = (long *)((char *)src + tab->off);
1212                         *ptr++ = ' ';
1213                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1214                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1215                         break;
1216
1217                 case 'U':
1218                         ulptr = (unsigned long *)((char *)src + tab->off);
1219                         *ptr++ = ' ';
1220                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1221                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1222                         break;
1223
1224                 default:
1225                         assert( 0 );
1226                 }
1227         }
1228         tmp.bv_val = buf;
1229         tmp.bv_len = ptr - buf;
1230         ber_dupbv( bv, &tmp );
1231         return 0;
1232 }
1233
1234 int
1235 bindconf_parse( const char *word, slap_bindconf *bc )
1236 {
1237 #ifdef HAVE_TLS
1238         /* Detect TLS config changes explicitly */
1239         if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1240                 bc->sb_tls_do_init = 1;
1241                 return 0;
1242         }
1243 #endif
1244         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1245 }
1246
1247 int
1248 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1249 {
1250         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1251 }
1252
1253 void bindconf_free( slap_bindconf *bc ) {
1254         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1255                 ch_free( bc->sb_uri.bv_val );
1256                 BER_BVZERO( &bc->sb_uri );
1257         }
1258         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1259                 ch_free( bc->sb_binddn.bv_val );
1260                 BER_BVZERO( &bc->sb_binddn );
1261         }
1262         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1263                 ch_free( bc->sb_cred.bv_val );
1264                 BER_BVZERO( &bc->sb_cred );
1265         }
1266         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1267                 ch_free( bc->sb_saslmech.bv_val );
1268                 BER_BVZERO( &bc->sb_saslmech );
1269         }
1270         if ( bc->sb_secprops ) {
1271                 ch_free( bc->sb_secprops );
1272                 bc->sb_secprops = NULL;
1273         }
1274         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1275                 ch_free( bc->sb_realm.bv_val );
1276                 BER_BVZERO( &bc->sb_realm );
1277         }
1278         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1279                 ch_free( bc->sb_authcId.bv_val );
1280                 BER_BVZERO( &bc->sb_authcId );
1281         }
1282         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1283                 ch_free( bc->sb_authzId.bv_val );
1284                 BER_BVZERO( &bc->sb_authzId );
1285         }
1286 #ifdef HAVE_TLS
1287         if ( bc->sb_tls_ctx ) {
1288                 SSL_CTX_free( bc->sb_tls_ctx );
1289                 bc->sb_tls_ctx = NULL;
1290         }
1291         if ( bc->sb_tls_cert ) {
1292                 ch_free( bc->sb_tls_cert );
1293                 bc->sb_tls_cert = NULL;
1294         }
1295         if ( bc->sb_tls_key ) {
1296                 ch_free( bc->sb_tls_key );
1297                 bc->sb_tls_key = NULL;
1298         }
1299         if ( bc->sb_tls_cacert ) {
1300                 ch_free( bc->sb_tls_cacert );
1301                 bc->sb_tls_cacert = NULL;
1302         }
1303         if ( bc->sb_tls_cacertdir ) {
1304                 ch_free( bc->sb_tls_cacertdir );
1305                 bc->sb_tls_cacertdir = NULL;
1306         }
1307         if ( bc->sb_tls_reqcert ) {
1308                 ch_free( bc->sb_tls_reqcert );
1309                 bc->sb_tls_reqcert = NULL;
1310         }
1311         if ( bc->sb_tls_cipher_suite ) {
1312                 ch_free( bc->sb_tls_cipher_suite );
1313                 bc->sb_tls_cipher_suite = NULL;
1314         }
1315 #ifdef HAVE_OPENSSL_CRL
1316         if ( bc->sb_tls_crlcheck ) {
1317                 ch_free( bc->sb_tls_crlcheck );
1318                 bc->sb_tls_crlcheck = NULL;
1319         }
1320 #endif
1321 #endif
1322 }
1323
1324 #ifdef HAVE_TLS
1325 static struct {
1326         const char *key;
1327         size_t offset;
1328         int opt;
1329 } bindtlsopts[] = {
1330         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1331         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1332         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1333         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1334         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1335         {0, 0}
1336 };
1337
1338 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1339 {
1340         int i, rc, newctx = 0, res = 0;
1341         char *ptr = (char *)bc, **word;
1342
1343         bc->sb_tls_do_init = 0;
1344
1345         for (i=0; bindtlsopts[i].opt; i++) {
1346                 word = (char **)(ptr + bindtlsopts[i].offset);
1347                 if ( *word ) {
1348                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1349                         if ( rc ) {
1350                                 Debug( LDAP_DEBUG_ANY,
1351                                         "bindconf_tls_set: failed to set %s to %s\n",
1352                                                 bindtlsopts[i].key, *word, 0 );
1353                                 res = -1;
1354                         } else
1355                                 newctx = 1;
1356                 }
1357         }
1358         if ( bc->sb_tls_reqcert ) {
1359                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1360                         bc->sb_tls_reqcert );
1361                 if ( rc ) {
1362                         Debug( LDAP_DEBUG_ANY,
1363                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1364                                         bc->sb_tls_reqcert, 0, 0 );
1365                         res = -1;
1366                 } else
1367                         newctx = 1;
1368         }
1369 #ifdef HAVE_OPENSSL_CRL
1370         if ( bc->sb_tls_crlcheck ) {
1371                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1372                         bc->sb_tls_crlcheck );
1373                 if ( rc ) {
1374                         Debug( LDAP_DEBUG_ANY,
1375                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1376                                         bc->sb_tls_crlcheck, 0, 0 );
1377                         res = -1;
1378                 } else
1379                         newctx = 1;
1380         }
1381 #endif
1382         if ( newctx ) {
1383                 int opt = 0;
1384
1385                 if ( bc->sb_tls_ctx ) {
1386                         SSL_CTX_free( bc->sb_tls_ctx );
1387                         bc->sb_tls_ctx = NULL;
1388                 }
1389                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1390                 if ( rc )
1391                         res = rc;
1392                 else
1393                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1394         }
1395         
1396         return res;
1397 }
1398 #endif
1399
1400 /* -------------------------------------- */
1401
1402
1403 static char *
1404 strtok_quote( char *line, char *sep, char **quote_ptr )
1405 {
1406         int             inquote;
1407         char            *tmp;
1408         static char     *next;
1409
1410         *quote_ptr = NULL;
1411         if ( line != NULL ) {
1412                 next = line;
1413         }
1414         while ( *next && strchr( sep, *next ) ) {
1415                 next++;
1416         }
1417
1418         if ( *next == '\0' ) {
1419                 next = NULL;
1420                 return( NULL );
1421         }
1422         tmp = next;
1423
1424         for ( inquote = 0; *next; ) {
1425                 switch ( *next ) {
1426                 case '"':
1427                         if ( inquote ) {
1428                                 inquote = 0;
1429                         } else {
1430                                 inquote = 1;
1431                         }
1432                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1433                         break;
1434
1435                 case '\\':
1436                         if ( next[1] )
1437                                 AC_MEMCPY( next,
1438                                             next + 1, strlen( next + 1 ) + 1 );
1439                         next++;         /* dont parse the escaped character */
1440                         break;
1441
1442                 default:
1443                         if ( ! inquote ) {
1444                                 if ( strchr( sep, *next ) != NULL ) {
1445                                         *quote_ptr = next;
1446                                         *next++ = '\0';
1447                                         return( tmp );
1448                                 }
1449                         }
1450                         next++;
1451                         break;
1452                 }
1453         }
1454
1455         return( tmp );
1456 }
1457
1458 static char     buf[BUFSIZ];
1459 static char     *line;
1460 static size_t lmax, lcur;
1461
1462 #define CATLINE( buf ) \
1463         do { \
1464                 size_t len = strlen( buf ); \
1465                 while ( lcur + len + 1 > lmax ) { \
1466                         lmax += BUFSIZ; \
1467                         line = (char *) ch_realloc( line, lmax ); \
1468                 } \
1469                 strcpy( line + lcur, buf ); \
1470                 lcur += len; \
1471         } while( 0 )
1472
1473 static void
1474 fp_getline_init(ConfigArgs *c) {
1475         c->lineno = -1;
1476         buf[0] = '\0';
1477 }
1478
1479 static int
1480 fp_getline( FILE *fp, ConfigArgs *c )
1481 {
1482         char    *p;
1483
1484         lcur = 0;
1485         CATLINE(buf);
1486         c->lineno++;
1487
1488         /* avoid stack of bufs */
1489         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1490                 buf[0] = '\0';
1491                 c->line = line;
1492                 return(1);
1493         }
1494
1495         while ( fgets( buf, sizeof( buf ), fp ) ) {
1496                 p = strchr( buf, '\n' );
1497                 if ( p ) {
1498                         if ( p > buf && p[-1] == '\r' ) {
1499                                 --p;
1500                         }
1501                         *p = '\0';
1502                 }
1503                 /* XXX ugly */
1504                 c->line = line;
1505                 if ( line[0]
1506                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1507                                 && p[-1] != '\\' )
1508                 {
1509                         p[0] = '\0';
1510                         lcur--;
1511                         
1512                 } else {
1513                         if ( !isspace( (unsigned char)buf[0] ) ) {
1514                                 return(1);
1515                         }
1516                         buf[0] = ' ';
1517                 }
1518                 CATLINE(buf);
1519                 c->lineno++;
1520         }
1521
1522         buf[0] = '\0';
1523         c->line = line;
1524         return(line[0] ? 1 : 0);
1525 }
1526
1527 static int
1528 fp_parse_line(ConfigArgs *c)
1529 {
1530         char *token;
1531         static char *const hide[] = {
1532                 "rootpw", "replica", "syncrepl",  /* in slapd */
1533                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1534                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1535                 "pseudorootpw",  /* in back-meta */
1536                 "dbpasswd",  /* in back-sql */
1537                 NULL
1538         };
1539         char *quote_ptr;
1540         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1541
1542         c->tline = ch_strdup(c->line);
1543         token = strtok_quote(c->tline, " \t", &quote_ptr);
1544
1545         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1546         if(quote_ptr) *quote_ptr = ' ';
1547         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1548                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1549         if(quote_ptr) *quote_ptr = '\0';
1550
1551         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1552                 if(c->argc >= c->argv_size) {
1553                         char **tmp;
1554                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1555                         if(!tmp) {
1556                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1557                                 return -1;
1558                         }
1559                         c->argv = tmp;
1560                         c->argv_size += ARGS_STEP;
1561                 }
1562                 if(token == NULL)
1563                         break;
1564                 c->argv[c->argc++] = token;
1565         }
1566         c->argv[c->argc] = NULL;
1567         return(0);
1568 }
1569
1570 void
1571 config_destroy( )
1572 {
1573         ucdata_unload( UCDATA_ALL );
1574         if ( frontendDB ) {
1575                 /* NOTE: in case of early exit, frontendDB can be NULL */
1576                 if ( frontendDB->be_schemandn.bv_val )
1577                         free( frontendDB->be_schemandn.bv_val );
1578                 if ( frontendDB->be_schemadn.bv_val )
1579                         free( frontendDB->be_schemadn.bv_val );
1580                 if ( frontendDB->be_acl )
1581                         acl_destroy( frontendDB->be_acl, NULL );
1582         }
1583         free( line );
1584         if ( slapd_args_file )
1585                 free ( slapd_args_file );
1586         if ( slapd_pid_file )
1587                 free ( slapd_pid_file );
1588         if ( default_passwd_hash )
1589                 ldap_charray_free( default_passwd_hash );
1590 }
1591
1592 char **
1593 slap_str2clist( char ***out, char *in, const char *brkstr )
1594 {
1595         char    *str;
1596         char    *s;
1597         char    *lasts;
1598         int     i, j;
1599         char    **new;
1600
1601         /* find last element in list */
1602         for (i = 0; *out && (*out)[i]; i++);
1603
1604         /* protect the input string from strtok */
1605         str = ch_strdup( in );
1606
1607         if ( *str == '\0' ) {
1608                 free( str );
1609                 return( *out );
1610         }
1611
1612         /* Count words in string */
1613         j=1;
1614         for ( s = str; *s; s++ ) {
1615                 if ( strchr( brkstr, *s ) != NULL ) {
1616                         j++;
1617                 }
1618         }
1619
1620         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1621         new = *out + i;
1622         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1623                 s != NULL;
1624                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1625         {
1626                 *new = ch_strdup( s );
1627                 new++;
1628         }
1629
1630         *new = NULL;
1631         free( str );
1632         return( *out );
1633 }
1634
1635 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1636         int argc, char **argv )
1637 {
1638         ConfigArgs c = { 0 };
1639         ConfigTable *ct;
1640         int rc;
1641
1642         c.be = be;
1643         c.fname = fname;
1644         c.lineno = lineno;
1645         c.argc = argc;
1646         c.argv = argv;
1647         c.valx = -1;
1648         c.line = line;
1649         c.op = SLAP_CONFIG_ADD;
1650         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1651
1652         rc = SLAP_CONF_UNKNOWN;
1653         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1654         if ( ct )
1655                 rc = config_add_vals( ct, &c );
1656         return rc;
1657 }