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