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