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