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