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