]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
document option '-F'
[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-2005 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 "slap.h"
38 #ifdef LDAP_SLAPI
39 #include "slapi/slapi.h"
40 #endif
41 #include "lutil.h"
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif /* HAVE_LIMITS_H */
45 #ifndef PATH_MAX
46 #define PATH_MAX 4096
47 #endif /* ! PATH_MAX */
48 #include "config.h"
49
50 #define ARGS_STEP       512
51
52 /*
53  * defaults for various global variables
54  */
55 slap_mask_t             global_allows = 0;
56 slap_mask_t             global_disallows = 0;
57 int             global_gentlehup = 0;
58 int             global_idletimeout = 0;
59 char    *global_host = NULL;
60 char    *global_realm = NULL;
61 char            *ldap_srvtab = "";
62 char            **default_passwd_hash = NULL;
63 struct berval default_search_base = BER_BVNULL;
64 struct berval default_search_nbase = BER_BVNULL;
65
66 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
67 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
68
69 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
70 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
71
72 char   *slapd_pid_file  = NULL;
73 char   *slapd_args_file = NULL;
74
75 int use_reverse_lookup = 0;
76
77 #ifdef LDAP_SLAPI
78 int slapi_plugins_used = 0;
79 #endif
80
81 static int fp_getline(FILE *fp, ConfigArgs *c);
82 static void fp_getline_init(ConfigArgs *c);
83 static int fp_parse_line(ConfigArgs *c);
84
85 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
86
87 int read_config_file(const char *fname, int depth, ConfigArgs *cf);
88
89 ConfigArgs *
90 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
91 {
92         ConfigArgs *c;
93         c = ch_calloc( 1, sizeof( ConfigArgs ) );
94         if ( c == NULL ) return(NULL);
95         c->be     = be; 
96         c->fname  = fname;
97         c->argc   = argc;
98         c->argv   = argv; 
99         c->lineno = lineno;
100         snprintf( c->log, sizeof( c->log ), "%s: line %lu", fname, lineno );
101         return(c);
102 }
103
104 void
105 init_config_argv( ConfigArgs *c )
106 {
107         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
108         c->argv_size = ARGS_STEP + 1;
109 }
110
111 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
112         int i;
113
114         for(i = 0; Conf[i].name; i++)
115                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
116                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
117         if ( !Conf[i].name ) return NULL;
118         return Conf+i;
119 }
120
121 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
122         int rc, arg_user, arg_type, iarg;
123         long larg;
124         ber_len_t barg;
125         
126         arg_type = Conf->arg_type;
127         if(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         if((arg_type & ARG_DN) && c->argc == 1) {
133                 c->argc = 2;
134                 c->argv[1] = "";
135         }
136         if(Conf->min_args && (c->argc < Conf->min_args)) {
137                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> missing <%s> argument\n",
138                         c->log, Conf->name, Conf->what);
139                 return(ARG_BAD_CONF);
140         }
141         if(Conf->max_args && (c->argc > Conf->max_args)) {
142                 Debug(LDAP_DEBUG_CONFIG, "%s: extra cruft after <%s> in <%s> line (ignored)\n",
143                         c->log, Conf->what, Conf->name);
144         }
145         if((arg_type & ARG_DB) && !c->be) {
146                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> allowed only within database declaration\n",
147                         c->log, Conf->name, 0);
148                 return(ARG_BAD_CONF);
149         }
150         if((arg_type & ARG_PRE_BI) && c->bi) {
151                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> must appear before any backend %sdeclaration\n",
152                         c->log, Conf->name, ((arg_type & ARG_PRE_DB)
153                         ? "or database " : "") );
154                 return(ARG_BAD_CONF);
155         }
156         if((arg_type & ARG_PRE_DB) && c->be && c->be != frontendDB) {
157                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> must appear before any database declaration\n",
158                         c->log, Conf->name, 0);
159                 return(ARG_BAD_CONF);
160         }
161         if((arg_type & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
162                 Debug(LDAP_DEBUG_CONFIG, "%s: old <%s> format not supported\n",
163                         c->log, Conf->name, 0);
164                 return(ARG_BAD_CONF);
165         }
166         if((arg_type & ARGS_POINTER) && !Conf->arg_item && !(arg_type & ARG_OFFSET)) {
167                 Debug(LDAP_DEBUG_CONFIG, "%s: null arg_item for <%s>\n",
168                         c->log, Conf->name, 0);
169                 return(ARG_BAD_CONF);
170         }
171         c->type = arg_user = (arg_type & ARGS_USERLAND);
172         memset(&c->values, 0, sizeof(c->values));
173         if(arg_type & ARGS_NUMERIC) {
174                 int j;
175                 iarg = 0; larg = 0; barg = 0;
176                 switch(arg_type & ARGS_NUMERIC) {
177                         case ARG_INT:           iarg = atoi(c->argv[1]);                break;
178                         case ARG_LONG:          larg = strtol(c->argv[1], NULL, 0);     break;
179                         case ARG_BER_LEN_T:     barg = (ber_len_t)atol(c->argv[1]);     break;
180                         case ARG_ON_OFF:
181                                 if(c->argc == 1) {
182                                         iarg = 1;
183                                 } else if(!strcasecmp(c->argv[1], "on") ||
184                                         !strcasecmp(c->argv[1], "true")) {
185                                         iarg = 1;
186                                 } else if(!strcasecmp(c->argv[1], "off") ||
187                                         !strcasecmp(c->argv[1], "false")) {
188                                         iarg = 0;
189                                 } else {
190                                         Debug(LDAP_DEBUG_CONFIG, "%s: ignoring ", c->log, 0, 0);
191                                         Debug(LDAP_DEBUG_CONFIG, "invalid %s value (%s) in <%s> line\n",
192                                                 Conf->what, c->argv[1], Conf->name);
193                                         return(0);
194                                 }
195                                 break;
196                 }
197                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
198                 if(iarg < j && larg < j && barg < j ) {
199                         larg = larg ? larg : (barg ? barg : iarg);
200                         Debug(LDAP_DEBUG_CONFIG, "%s: " , c->log, 0, 0);
201                         Debug(LDAP_DEBUG_CONFIG, "invalid %s value (%ld) in <%s> line\n", Conf->what, larg, Conf->name);
202                         return(ARG_BAD_CONF);
203                 }
204                 switch(arg_type & ARGS_NUMERIC) {
205                         case ARG_ON_OFF:
206                         case ARG_INT:           c->value_int = iarg;            break;
207                         case ARG_LONG:          c->value_long = larg;           break;
208                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
209                 }
210         } else if(arg_type & ARG_STRING) {
211                 if ( !check_only )
212                         c->value_string = ch_strdup(c->argv[1]);
213         } else if(arg_type & ARG_BERVAL) {
214                 if ( !check_only )
215                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
216         } else if(arg_type & ARG_DN) {
217                 struct berval bv;
218                 ber_str2bv( c->argv[1], 0, 0, &bv );
219                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
220                 if ( rc != LDAP_SUCCESS ) {
221                         Debug(LDAP_DEBUG_CONFIG, "%s: " , c->log, 0, 0);
222                         Debug(LDAP_DEBUG_CONFIG, "%s DN is invalid %d (%s)\n",
223                                 Conf->name, rc, ldap_err2string( rc ));
224                         return(ARG_BAD_CONF);
225                 }
226                 if ( check_only ) {
227                         ch_free( c->value_ndn.bv_val );
228                         ch_free( c->value_dn.bv_val );
229                 }
230         }
231         return 0;
232 }
233
234 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
235         int rc, arg_type;
236         void *ptr;
237
238         arg_type = Conf->arg_type;
239         if(arg_type & ARG_MAGIC) {
240                 if(!c->be) c->be = frontendDB;
241                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
242 #if 0
243                 if(c->be == frontendDB) c->be = NULL;
244 #endif
245                 if(rc) {
246                         Debug(LDAP_DEBUG_CONFIG, "%s: handler for <%s> exited with %d!\n",
247                                 c->log, Conf->name, rc);
248                         return(ARG_BAD_CONF);
249                 }
250                 return(0);
251         }
252         if(arg_type & ARG_OFFSET) {
253                 if (c->be)
254                         ptr = c->be->be_private;
255                 else if (c->bi)
256                         ptr = c->bi->bi_private;
257                 else {
258                         Debug(LDAP_DEBUG_CONFIG, "%s: offset for <%s> missing base pointer!\n",
259                                 c->log, Conf->name, 0);
260                         return(ARG_BAD_CONF);
261                 }
262                 ptr = (void *)((char *)ptr + (int)Conf->arg_item);
263         } else if (arg_type & ARGS_POINTER) {
264                 ptr = Conf->arg_item;
265         }
266         if(arg_type & ARGS_POINTER)
267                 switch(arg_type & ARGS_POINTER) {
268                         case ARG_ON_OFF:
269                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
270                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
271                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
272                         case ARG_STRING: {
273                                 char *cc = *(char**)ptr;
274                                 if(cc) {
275                                         if (arg_type & ARG_UNIQUE) {
276                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
277                                                         c->log, Conf->name, 0 );
278                                                 return(ARG_BAD_CONF);
279                                         }
280                                         ch_free(cc);
281                                 }
282                                 *(char **)ptr = c->value_string;
283                                 break;
284                                 }
285                         case ARG_BERVAL:
286                                 *(struct berval *)ptr = c->value_bv;
287                                 break;
288                 }
289         return(0);
290 }
291
292 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
293         int rc, arg_type;
294
295         arg_type = Conf->arg_type;
296         if(arg_type == ARG_IGNORED) {
297                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
298                         c->log, Conf->name, 0);
299                 return(0);
300         }
301         rc = config_check_vals( Conf, c, 0 );
302         if ( rc ) return rc;
303         return config_set_vals( Conf, c );
304 }
305
306 int
307 config_del_vals(ConfigTable *cf, ConfigArgs *c)
308 {
309         int rc = 0;
310
311         return rc;
312 }
313
314 int
315 config_get_vals(ConfigTable *cf, ConfigArgs *c)
316 {
317         int rc = 0;
318         struct berval bv;
319         void *ptr;
320
321         if ( cf->arg_type & ARG_IGNORED ) {
322                 return 1;
323         }
324
325         memset(&c->values, 0, sizeof(c->values));
326         c->rvalue_vals = NULL;
327         c->rvalue_nvals = NULL;
328         c->op = SLAP_CONFIG_EMIT;
329         c->type = cf->arg_type & ARGS_USERLAND;
330
331         if ( cf->arg_type & ARG_MAGIC ) {
332                 rc = (*((ConfigDriver*)cf->arg_item))(c);
333                 if ( rc ) return rc;
334         } else {
335                 if ( cf->arg_type & ARG_OFFSET ) {
336                         if ( c->be )
337                                 ptr = c->be->be_private;
338                         else if ( c->bi )
339                                 ptr = c->bi->bi_private;
340                         else
341                                 return 1;
342                         ptr = (void *)((char *)ptr + (int)cf->arg_item);
343                 } else {
344                         ptr = cf->arg_item;
345                 }
346                 
347                 switch(cf->arg_type & ARGS_POINTER) {
348                 case ARG_ON_OFF:
349                 case ARG_INT:   c->value_int = *(int *)ptr; break;
350                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
351                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
352                 case ARG_STRING:
353                         if ( *(char **)ptr )
354                                 c->value_string = ch_strdup(*(char **)ptr);
355                         break;
356                 case ARG_BERVAL:
357                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
358                 }
359         }
360         if ( cf->arg_type & ARGS_POINTER) {
361                 bv.bv_val = c->log;
362                 switch(cf->arg_type & ARGS_POINTER) {
363                 case ARG_INT: bv.bv_len = sprintf(bv.bv_val, "%d", c->value_int); break;
364                 case ARG_LONG: bv.bv_len = sprintf(bv.bv_val, "%ld", c->value_long); break;
365                 case ARG_BER_LEN_T: bv.bv_len = sprintf(bv.bv_val, "%ld", c->value_ber_t); break;
366                 case ARG_ON_OFF: bv.bv_len = sprintf(bv.bv_val, "%s",
367                         c->value_int ? "TRUE" : "FALSE"); break;
368                 case ARG_STRING:
369                         if ( c->value_string && c->value_string[0]) {
370                                 ber_str2bv( c->value_string, 0, 0, &bv);
371                         } else {
372                                 return 1;
373                         }
374                         break;
375                 case ARG_BERVAL:
376                         if ( !BER_BVISEMPTY( &c->value_bv )) {
377                                 bv = c->value_bv;
378                         } else {
379                                 return 1;
380                         }
381                         break;
382                 }
383                 if (( cf->arg_type & ARGS_POINTER ) == ARG_STRING )
384                         ber_bvarray_add(&c->rvalue_vals, &bv);
385                 else
386                         value_add_one(&c->rvalue_vals, &bv);
387         }
388         return rc;
389 }
390
391 int
392 init_config_attrs(ConfigTable *ct) {
393         LDAPAttributeType *at;
394         int i, code;
395         const char *err;
396
397         for (i=0; ct[i].name; i++ ) {
398                 if ( !ct[i].attribute ) continue;
399                 at = ldap_str2attributetype( ct[i].attribute,
400                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
401                 if ( !at ) {
402                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
403                                 ct[i].attribute, ldap_scherr2str(code), err );
404                         return code;
405                 }
406                 code = at_add( at, 0, NULL, &err );
407                 if ( code && code != SLAP_SCHERR_ATTR_DUP ) {
408                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
409                                 ct[i].attribute, scherr2str(code), err );
410                         return code;
411                 }
412                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
413                 if ( code ) {
414                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
415                                 ct[i].attribute, err );
416                         return code;
417                 }
418                 ldap_memfree( at );
419         }
420
421         return 0;
422 }
423
424 int
425 init_config_ocs( ConfigOCs *ocs ) {
426         int i;
427
428         for (i=0;ocs[i].def;i++) {
429                 LDAPObjectClass *oc;
430                 int code;
431                 const char *err;
432
433                 oc = ldap_str2objectclass( ocs[i].def, &code, &err,
434                         LDAP_SCHEMA_ALLOW_ALL );
435                 if ( !oc ) {
436                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
437                                 ocs[i].def, ldap_scherr2str(code), err );
438                         return code;
439                 }
440                 code = oc_add(oc,0,NULL,&err);
441                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
442                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
443                                 ocs[i].def, scherr2str(code), err );
444                         return code;
445                 }
446                 if ( ocs[i].oc ) {
447                         *ocs[i].oc = oc_find(oc->oc_names[0]);
448                 }
449                 ldap_memfree(oc);
450         }
451         return 0;
452 }
453
454 int
455 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
456 {
457         int rc = 0;
458
459         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
460                 ct->ad->ad_cname.bv_val, valx );
461         c->argc = 1;
462         c->argv[0] = ct->ad->ad_cname.bv_val;
463         if ( fp_parse_line( c ) ) {
464                 rc = 1;
465         } else {
466                 rc = config_check_vals( ct, c, 1 );
467         }
468
469         ch_free( c->tline );
470         return rc;
471 }
472
473 int
474 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx)
475 {
476         int rc = 0;
477
478         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
479                 ct->ad->ad_cname.bv_val, valx );
480         c->argc = 1;
481         c->argv[0] = ct->ad->ad_cname.bv_val;
482         if ( fp_parse_line( c ) ) {
483                 rc = 1;
484         } else {
485                 c->op = LDAP_MOD_ADD;
486                 rc = config_add_vals( ct, c );
487         }
488
489         ch_free( c->tline );
490         return rc;
491 }
492
493 int
494 read_config_file(const char *fname, int depth, ConfigArgs *cf)
495 {
496         FILE *fp;
497         ConfigTable *ct;
498         ConfigArgs *c;
499         int rc;
500
501         c = ch_calloc( 1, sizeof( ConfigArgs ) );
502         if ( c == NULL ) {
503                 return 1;
504         }
505
506         if ( depth ) {
507                 memcpy( c, cf, sizeof( ConfigArgs ) );
508         } else {
509                 c->depth = depth; /* XXX */
510                 c->bi = NULL;
511                 c->be = NULL;
512         }
513
514         c->fname = fname;
515         init_config_argv( c );
516
517         fp = fopen( fname, "r" );
518         if ( fp == NULL ) {
519                 ldap_syslog = 1;
520                 Debug(LDAP_DEBUG_ANY,
521                     "could not open config file \"%s\": %s (%d)\n",
522                     fname, strerror(errno), errno);
523                 return(1);
524         }
525
526         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
527
528         fp_getline_init(c);
529
530         c->tline = NULL;
531
532         while ( fp_getline( fp, c ) ) {
533                 /* skip comments and blank lines */
534                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
535                         continue;
536                 }
537
538                 snprintf( c->log, sizeof( c->log ), "%s: line %lu",
539                                 c->fname, c->lineno );
540
541                 c->argc = 0;
542                 ch_free( c->tline );
543                 if ( fp_parse_line( c ) ) {
544                         rc = 1;
545                         goto leave;
546                 }
547
548                 if ( c->argc < 1 ) {
549                         Debug(LDAP_DEBUG_CONFIG, "%s: bad config line (ignored)\n", c->log, 0, 0);
550                         continue;
551                 }
552
553                 c->op = SLAP_CONFIG_ADD;
554
555                 ct = config_find_keyword( config_back_cf_table, c );
556                 if ( ct ) {
557                         rc = config_add_vals( ct, c );
558                         if ( !rc ) continue;
559
560                         if ( rc & ARGS_USERLAND ) {
561                                 /* XXX a usertype would be opaque here */
562                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
563                                         c->log, c->argv[0], 0);
564                                 rc = 1;
565                                 goto leave;
566
567                         } else if ( rc == ARG_BAD_CONF ) {
568                                 rc = 1;
569                                 goto leave;
570                         }
571                         
572                 } else if ( c->bi ) {
573                         rc = SLAP_CONF_UNKNOWN;
574                         if ( c->bi->bi_cf_table ) {
575                                 ct = config_find_keyword( c->bi->bi_cf_table, c );
576                                 if ( ct ) {
577                                         rc = config_add_vals( ct, c );
578                                 }
579                         }
580                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
581                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
582                                         c->argc, c->argv);
583                         }
584                         if ( rc ) {
585                                 switch(rc) {
586                                 case SLAP_CONF_UNKNOWN:
587                                         Debug(LDAP_DEBUG_CONFIG, "%s: "
588                                                 "unknown directive <%s> inside backend info definition (ignored)\n",
589                                                 c->log, *c->argv, 0);
590                                         continue;
591                                 default:
592                                         rc = 1;
593                                         goto leave;
594                                 }
595                         }
596
597                 } else if ( c->be ) {
598                         rc = SLAP_CONF_UNKNOWN;
599                         if ( c->be->be_cf_table ) {
600                                 ct = config_find_keyword( c->be->be_cf_table, c );
601                                 if ( ct ) {
602                                         rc = config_add_vals( ct, c );
603                                 }
604                         }
605                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
606                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
607                                         c->argc, c->argv);
608                         }
609                         if ( rc ) {
610                                 switch(rc) {
611                                 case SLAP_CONF_UNKNOWN:
612                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
613                                                 "unknown directive <%s> inside backend database "
614                                                 "definition (ignored)\n",
615                                                 c->log, *c->argv, 0);
616                                         continue;
617                                 default:
618                                         rc = 1;
619                                         goto leave;
620                                 }
621                         }
622
623                 } else if ( frontendDB->be_config ) {
624                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
625                         if ( rc ) {
626                                 switch(rc) {
627                                 case SLAP_CONF_UNKNOWN:
628                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
629                                                 "unknown directive <%s> inside global database definition (ignored)\n",
630                                                 c->log, *c->argv, 0);
631                                         continue;
632                                 default:
633                                         rc = 1;
634                                         goto leave;
635                                 }
636                         }
637                         
638                 } else {
639                         Debug(LDAP_DEBUG_CONFIG, "%s: "
640                                 "unknown directive <%s> outside backend info and database definitions (ignored)\n",
641                                 c->log, *c->argv, 0);
642                         continue;
643
644                 }
645         }
646
647         if ( BER_BVISNULL( &frontendDB->be_schemadn ) ) {
648                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
649                         &frontendDB->be_schemadn );
650                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
651                 if ( rc != LDAP_SUCCESS ) {
652                         Debug(LDAP_DEBUG_ANY, "%s: "
653                                 "unable to normalize default schema DN \"%s\"\n",
654                                 c->log, frontendDB->be_schemadn.bv_val, 0 );
655                         /* must not happen */
656                         assert( 0 );
657                 }
658         }
659         rc = 0;
660
661 leave:
662         ch_free(c->tline);
663         fclose(fp);
664         ch_free(c->argv);
665         ch_free(c);
666         return(rc);
667 }
668
669 /* restrictops, allows, disallows, requires, loglevel */
670
671 int
672 verb_to_mask(const char *word, slap_verbmasks *v) {
673         int i;
674         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
675                 if(!strcasecmp(word, v[i].word.bv_val))
676                         break;
677         return(i);
678 }
679
680 int
681 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
682         int i, j;
683         for(i = 1; i < argc; i++) {
684                 j = verb_to_mask(argv[i], v);
685                 if(BER_BVISNULL(&v[j].word)) return(1);
686                 while (!v[j].mask) j--;
687                 *m |= v[j].mask;
688         }
689         return(0);
690 }
691
692 int
693 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
694         int i;
695
696         if (!m) return 1;
697         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
698                 if (!v[i].mask) continue;
699                 if (( m & v[i].mask ) == v[i].mask ) {
700                         value_add_one( bva, &v[i].word );
701                 }
702         }
703         return 0;
704 }
705
706 static slap_verbmasks tlskey[] = {
707         { BER_BVC("no"),                SB_TLS_OFF },
708         { BER_BVC("yes"),               SB_TLS_ON },
709         { BER_BVC("critical"),  SB_TLS_CRITICAL },
710         { BER_BVNULL, 0 }
711 };
712
713 static slap_verbmasks methkey[] = {
714         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
715 #ifdef HAVE_CYRUS_SASL
716         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
717 #endif
718         { BER_BVNULL, 0 }
719 };
720
721 typedef struct cf_aux_table {
722         struct berval key;
723         int off;
724         char type;
725         char quote;
726         slap_verbmasks *aux;
727 } cf_aux_table;
728
729 static cf_aux_table bindkey[] = {
730         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
731         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
732         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
733         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
734         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 's', 0, NULL },
735         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
736         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 's', 0, NULL },
737         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 's', 0, NULL },
738         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
739         { BER_BVNULL, 0, 0, 0, NULL }
740 };
741
742 int bindconf_parse( const char *word, slap_bindconf *bc ) {
743         int rc = 0;
744         cf_aux_table *tab;
745
746         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
747                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
748                         char **cptr;
749                         int *iptr, j;
750                         struct berval *bptr;
751                         const char *val = word + tab->key.bv_len;
752
753                         switch ( tab->type ) {
754                         case 's':
755                                 cptr = (char **)((char *)bc + tab->off);
756                                 *cptr = ch_strdup( val );
757                                 break;
758
759                         case 'b':
760                                 bptr = (struct berval *)((char *)bc + tab->off);
761                                 ber_str2bv( val, 0, 1, bptr );
762                                 break;
763
764                         case 'i':
765                                 assert( tab->aux );
766                                 iptr = (int *)((char *)bc + tab->off);
767
768                                 rc = 1;
769                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
770                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
771                                                 *iptr = tab->aux[j].mask;
772                                                 rc = 0;
773                                         }
774                                 }
775                                 break;
776                         }
777
778                         if ( rc ) {
779                                 Debug( LDAP_DEBUG_ANY, "invalid bind config value %s\n",
780                                         word, 0, 0 );
781                         }
782                         
783                         return rc;
784                 }
785         }
786
787         return rc;
788 }
789
790 int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
791         char buf[BUFSIZ], *ptr;
792         cf_aux_table *tab;
793         struct berval tmp;
794
795         ptr = buf;
796         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
797                 char **cptr;
798                 int *iptr, i;
799                 struct berval *bptr;
800
801                 cptr = (char **)((char *)bc + tab->off);
802
803                 switch ( tab->type ) {
804                 case 'b':
805                         bptr = (struct berval *)((char *)bc + tab->off);
806                         cptr = &bptr->bv_val;
807                 case 's':
808                         if ( *cptr ) {
809                                 *ptr++ = ' ';
810                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
811                                 if ( tab->quote ) *ptr++ = '"';
812                                 ptr = lutil_strcopy( ptr, *cptr );
813                                 if ( tab->quote ) *ptr++ = '"';
814                         }
815                         break;
816
817                 case 'i':
818                         assert( tab->aux );
819                 
820                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
821                                 if ( *iptr == tab->aux[i].mask ) {
822                                         *ptr++ = ' ';
823                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
824                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
825                                         break;
826                                 }
827                         }
828                         break;
829                 }
830         }
831         tmp.bv_val = buf;
832         tmp.bv_len = ptr - buf;
833         ber_dupbv( bv, &tmp );
834         return 0;
835 }
836
837 void bindconf_free( slap_bindconf *bc ) {
838         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
839                 ch_free( bc->sb_binddn.bv_val );
840         }
841         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
842                 ch_free( bc->sb_cred.bv_val );
843         }
844         if ( bc->sb_saslmech ) {
845                 ch_free( bc->sb_saslmech );
846         }
847         if ( bc->sb_secprops ) {
848                 ch_free( bc->sb_secprops );
849         }
850         if ( bc->sb_realm ) {
851                 ch_free( bc->sb_realm );
852         }
853         if ( bc->sb_authcId ) {
854                 ch_free( bc->sb_authcId );
855         }
856         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
857                 ch_free( bc->sb_authzId.bv_val );
858         }
859 }
860
861
862 /* -------------------------------------- */
863
864
865 static char *
866 strtok_quote( char *line, char *sep, char **quote_ptr )
867 {
868         int             inquote;
869         char            *tmp;
870         static char     *next;
871
872         *quote_ptr = NULL;
873         if ( line != NULL ) {
874                 next = line;
875         }
876         while ( *next && strchr( sep, *next ) ) {
877                 next++;
878         }
879
880         if ( *next == '\0' ) {
881                 next = NULL;
882                 return( NULL );
883         }
884         tmp = next;
885
886         for ( inquote = 0; *next; ) {
887                 switch ( *next ) {
888                 case '"':
889                         if ( inquote ) {
890                                 inquote = 0;
891                         } else {
892                                 inquote = 1;
893                         }
894                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
895                         break;
896
897                 case '\\':
898                         if ( next[1] )
899                                 AC_MEMCPY( next,
900                                             next + 1, strlen( next + 1 ) + 1 );
901                         next++;         /* dont parse the escaped character */
902                         break;
903
904                 default:
905                         if ( ! inquote ) {
906                                 if ( strchr( sep, *next ) != NULL ) {
907                                         *quote_ptr = next;
908                                         *next++ = '\0';
909                                         return( tmp );
910                                 }
911                         }
912                         next++;
913                         break;
914                 }
915         }
916
917         return( tmp );
918 }
919
920 static char     buf[BUFSIZ];
921 static char     *line;
922 static size_t lmax, lcur;
923
924 #define CATLINE( buf ) \
925         do { \
926                 size_t len = strlen( buf ); \
927                 while ( lcur + len + 1 > lmax ) { \
928                         lmax += BUFSIZ; \
929                         line = (char *) ch_realloc( line, lmax ); \
930                 } \
931                 strcpy( line + lcur, buf ); \
932                 lcur += len; \
933         } while( 0 )
934
935 static void
936 fp_getline_init(ConfigArgs *c) {
937         c->lineno = -1;
938         buf[0] = '\0';
939 }
940
941 static int
942 fp_getline( FILE *fp, ConfigArgs *c )
943 {
944         char    *p;
945
946         lcur = 0;
947         CATLINE(buf);
948         c->lineno++;
949
950         /* avoid stack of bufs */
951         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
952                 buf[0] = '\0';
953                 c->line = line;
954                 return(1);
955         }
956
957         while ( fgets( buf, sizeof( buf ), fp ) ) {
958                 p = strchr( buf, '\n' );
959                 if ( p ) {
960                         if ( p > buf && p[-1] == '\r' ) {
961                                 --p;
962                         }
963                         *p = '\0';
964                 }
965                 /* XXX ugly */
966                 c->line = line;
967                 if ( line[0]
968                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
969                                 && p[-1] != '\\' )
970                 {
971                         p[0] = '\0';
972                         lcur--;
973                         
974                 } else {
975                         if ( !isspace( (unsigned char)buf[0] ) ) {
976                                 return(1);
977                         }
978                         buf[0] = ' ';
979                 }
980                 CATLINE(buf);
981                 c->lineno++;
982         }
983
984         buf[0] = '\0';
985         c->line = line;
986         return(line[0] ? 1 : 0);
987 }
988
989 static int
990 fp_parse_line(ConfigArgs *c)
991 {
992         char *token;
993         char *hide[] = { "rootpw", "replica", "bindpw", "pseudorootpw", "dbpasswd", '\0' };
994         char *quote_ptr;
995         int i;
996
997         c->tline = ch_strdup(c->line);
998         token = strtok_quote(c->tline, " \t", &quote_ptr);
999
1000         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1001         if(quote_ptr) *quote_ptr = ' ';
1002         Debug(LDAP_DEBUG_CONFIG, "line %lu (%s%s)\n", c->lineno,
1003                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1004         if(quote_ptr) *quote_ptr = '\0';
1005
1006         for(; token; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1007                 if(c->argc == c->argv_size - 1) {
1008                         char **tmp;
1009                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1010                         if(!tmp) {
1011                                 Debug(LDAP_DEBUG_ANY, "line %lu: out of memory\n", c->lineno, 0, 0);
1012                                 return -1;
1013                         }
1014                         c->argv = tmp;
1015                         c->argv_size += ARGS_STEP;
1016                 }
1017                 c->argv[c->argc++] = token;
1018         }
1019         c->argv[c->argc] = NULL;
1020         return(0);
1021 }
1022
1023 void
1024 config_destroy( )
1025 {
1026         ucdata_unload( UCDATA_ALL );
1027         if ( frontendDB ) {
1028                 /* NOTE: in case of early exit, frontendDB can be NULL */
1029                 if ( frontendDB->be_schemandn.bv_val )
1030                         free( frontendDB->be_schemandn.bv_val );
1031                 if ( frontendDB->be_schemadn.bv_val )
1032                         free( frontendDB->be_schemadn.bv_val );
1033                 if ( frontendDB->be_acl )
1034                         acl_destroy( frontendDB->be_acl, NULL );
1035         }
1036         free( line );
1037         if ( slapd_args_file )
1038                 free ( slapd_args_file );
1039         if ( slapd_pid_file )
1040                 free ( slapd_pid_file );
1041         if ( default_passwd_hash )
1042                 ldap_charray_free( default_passwd_hash );
1043 }
1044
1045 char **
1046 slap_str2clist( char ***out, char *in, const char *brkstr )
1047 {
1048         char    *str;
1049         char    *s;
1050         char    *lasts;
1051         int     i, j;
1052         char    **new;
1053
1054         /* find last element in list */
1055         for (i = 0; *out && (*out)[i]; i++);
1056
1057         /* protect the input string from strtok */
1058         str = ch_strdup( in );
1059
1060         if ( *str == '\0' ) {
1061                 free( str );
1062                 return( *out );
1063         }
1064
1065         /* Count words in string */
1066         j=1;
1067         for ( s = str; *s; s++ ) {
1068                 if ( strchr( brkstr, *s ) != NULL ) {
1069                         j++;
1070                 }
1071         }
1072
1073         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1074         new = *out + i;
1075         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1076                 s != NULL;
1077                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1078         {
1079                 *new = ch_strdup( s );
1080                 new++;
1081         }
1082
1083         *new = NULL;
1084         free( str );
1085         return( *out );
1086 }
1087
1088 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1089         int argc, char **argv )
1090 {
1091         ConfigArgs c = { 0 };
1092         ConfigTable *ct;
1093         int rc;
1094
1095         c.be = be;
1096         c.fname = fname;
1097         c.lineno = lineno;
1098         c.argc = argc;
1099         c.argv = argv;
1100         sprintf( c.log, "%s: line %lu", fname, lineno );
1101
1102         rc = SLAP_CONF_UNKNOWN;
1103         ct = config_find_keyword( be->be_cf_table, &c );
1104         if ( ct )
1105                 rc = config_add_vals( ct, &c );
1106         return rc;
1107 }