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