]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
da68df480892adc1bc8062f29c8972548e8c9ffc
[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 && c->be->be_cf_table ) {
472                         rc = parse_config_table( c->be->be_cf_table, c );
473
474                         if ( rc ) {
475                                 switch(rc) {
476                                 case SLAP_CONF_UNKNOWN:
477                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
478                                                 "unknown directive <%s> inside backend database definition (ignored)\n",
479                                                 c->log, *c->argv, 0);
480                                         continue;
481                                 default:
482                                         goto badline;
483                                 }
484                         }
485
486                 } else if ( c->be && c->be->be_config ) {
487                         rc = (*c->be->be_config)(c->be, c->fname, c->lineno, c->argc, c->argv);
488                         if ( rc ) {
489                                 switch(rc) {
490                                 case SLAP_CONF_UNKNOWN:
491                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
492                                                 "unknown directive <%s> inside backend database definition (ignored)\n",
493                                                 c->log, *c->argv, 0);
494                                         continue;
495                                 default:
496                                         goto badline;
497                                 }
498                         }
499
500                 } else if ( frontendDB->be_config ) {
501                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
502                         if ( rc ) {
503                                 switch(rc) {
504                                 case SLAP_CONF_UNKNOWN:
505                                         Debug( LDAP_DEBUG_CONFIG, "%s: "
506                                                 "unknown directive <%s> inside global database definition (ignored)\n",
507                                                 c->log, *c->argv, 0);
508                                         continue;
509                                 default:
510                                         goto badline;
511                                 }
512                         }
513                         
514                 } else {
515                         Debug(LDAP_DEBUG_CONFIG, "%s: "
516                                 "unknown directive <%s> outside backend info and database definitions (ignored)\n",
517                                 c->log, *c->argv, 0);
518                         continue;
519
520                 }
521         }
522
523         fclose(fp);
524
525         if ( BER_BVISNULL( &frontendDB->be_schemadn ) ) {
526                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
527                         &frontendDB->be_schemadn );
528                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
529                 if ( rc != LDAP_SUCCESS ) {
530                         Debug(LDAP_DEBUG_ANY, "%s: "
531                                 "unable to normalize default schema DN \"%s\"\n",
532                                 c->log, frontendDB->be_schemadn.bv_val, 0 );
533                         /* must not happen */
534                         assert( 0 );
535                 }
536         }
537
538         ch_free(c->argv);
539         ch_free(c);
540         return(0);
541
542 badline:
543         fclose(fp);
544         ch_free(c->argv);
545         ch_free(c);
546         return(1);
547 }
548
549 /* restrictops, allows, disallows, requires, loglevel */
550
551 int
552 verb_to_mask(const char *word, slap_verbmasks *v) {
553         int i;
554         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
555                 if(!strcasecmp(word, v[i].word.bv_val))
556                         break;
557         return(i);
558 }
559
560 int
561 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
562         int i, j;
563         for(i = 1; i < argc; i++) {
564                 j = verb_to_mask(argv[i], v);
565                 if(BER_BVISNULL(&v[j].word)) return(1);
566                 while (!v[j].mask) j--;
567                 *m |= v[j].mask;
568         }
569         return(0);
570 }
571
572 int
573 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
574         int i, j;
575         struct berval bv;
576
577         if (!m) return 1;
578         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
579                 if (!v[i].mask) continue;
580                 if (( m & v[i].mask ) == v[i].mask ) {
581                         value_add_one( bva, &v[i].word );
582                 }
583         }
584         return 0;
585 }
586
587 static slap_verbmasks tlskey[] = {
588         { BER_BVC("no"),                SB_TLS_OFF },
589         { BER_BVC("yes"),               SB_TLS_ON },
590         { BER_BVC("critical"),  SB_TLS_CRITICAL },
591         { BER_BVNULL, 0 }
592 };
593
594 static slap_verbmasks methkey[] = {
595         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
596 #ifdef HAVE_CYRUS_SASL
597         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
598 #endif
599         { BER_BVNULL, 0 }
600 };
601
602 typedef struct cf_aux_table {
603         struct berval key;
604         int off;
605         int quote;
606         slap_verbmasks *aux;
607 } cf_aux_table;
608
609 static cf_aux_table bindkey[] = {
610         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 0, tlskey },
611         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 0, methkey },
612         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 1, NULL },
613         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 1, NULL },
614         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 0, NULL },
615         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 0, NULL },
616         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 0, NULL },
617         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 0, NULL },
618         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 1, NULL },
619         { BER_BVNULL, 0, 0, NULL }
620 };
621
622 int bindconf_parse( const char *word, slap_bindconf *bc ) {
623         int i, rc = 0;
624         char **cptr;
625         cf_aux_table *tab;
626
627         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
628                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
629                         cptr = (char **)((char *)bc + tab->off);
630                         if ( tab->aux ) {
631                                 int j;
632                                 rc = 1;
633                                 for (j=0; !BER_BVISNULL(&tab->aux[j].word); j++) {
634                                         if (!strcasecmp(word+tab->key.bv_len, tab->aux[j].word.bv_val)) {
635                                                 int *ptr = (int *)cptr;
636                                                 *ptr = tab->aux[j].mask;
637                                                 rc = 0;
638                                         }
639                                 }
640                                 if (rc ) {
641                                         Debug(LDAP_DEBUG_ANY, "invalid bind config value %s\n",
642                                                 word, 0, 0 );
643                                 }
644                                 return rc;
645                         }
646                         *cptr = ch_strdup(word+tab->key.bv_len);
647                         return 0;
648                 }
649         }
650         return rc;
651 }
652
653 int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
654         char buf[BUFSIZ], *ptr;
655         cf_aux_table *tab;
656         char **cptr;
657         struct berval tmp;
658
659         ptr = buf;
660         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
661                 cptr = (char **)((char *)bc + tab->off);
662                 if ( tab->aux ) {
663                         int *ip = (int *)cptr, i;
664                         for ( i=0; !BER_BVISNULL(&tab->aux[i].word); i++ ) {
665                                 if ( *ip == tab->aux[i].mask ) {
666                                         *ptr++ = ' ';
667                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
668                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
669                                         break;
670                                 }
671                         }
672                 } else if ( *cptr ) {
673                         *ptr++ = ' ';
674                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
675                         if ( tab->quote ) *ptr++ = '"';
676                         ptr = lutil_strcopy( ptr, *cptr );
677                         if ( tab->quote ) *ptr++ = '"';
678                 }
679         }
680         tmp.bv_val = buf;
681         tmp.bv_len = ptr - buf;
682         ber_dupbv( bv, &tmp );
683         return 0;
684 }
685
686 void bindconf_free( slap_bindconf *bc ) {
687         if ( bc->sb_binddn ) {
688                 ch_free( bc->sb_binddn );
689         }
690         if ( bc->sb_cred ) {
691                 ch_free( bc->sb_cred );
692         }
693         if ( bc->sb_saslmech ) {
694                 ch_free( bc->sb_saslmech );
695         }
696         if ( bc->sb_secprops ) {
697                 ch_free( bc->sb_secprops );
698         }
699         if ( bc->sb_realm ) {
700                 ch_free( bc->sb_realm );
701         }
702         if ( bc->sb_authcId ) {
703                 ch_free( bc->sb_authcId );
704         }
705         if ( bc->sb_authzId ) {
706                 ch_free( bc->sb_authzId );
707         }
708 }
709
710
711 /* -------------------------------------- */
712
713
714 static char *
715 strtok_quote( char *line, char *sep )
716 {
717         int             inquote;
718         char            *tmp;
719         static char     *next;
720
721         strtok_quote_ptr = NULL;
722         if ( line != NULL ) {
723                 next = line;
724         }
725         while ( *next && strchr( sep, *next ) ) {
726                 next++;
727         }
728
729         if ( *next == '\0' ) {
730                 next = NULL;
731                 return( NULL );
732         }
733         tmp = next;
734
735         for ( inquote = 0; *next; ) {
736                 switch ( *next ) {
737                 case '"':
738                         if ( inquote ) {
739                                 inquote = 0;
740                         } else {
741                                 inquote = 1;
742                         }
743                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
744                         break;
745
746                 case '\\':
747                         if ( next[1] )
748                                 AC_MEMCPY( next,
749                                             next + 1, strlen( next + 1 ) + 1 );
750                         next++;         /* dont parse the escaped character */
751                         break;
752
753                 default:
754                         if ( ! inquote ) {
755                                 if ( strchr( sep, *next ) != NULL ) {
756                                         strtok_quote_ptr = next;
757                                         *next++ = '\0';
758                                         return( tmp );
759                                 }
760                         }
761                         next++;
762                         break;
763                 }
764         }
765
766         return( tmp );
767 }
768
769 static char     buf[BUFSIZ];
770 static char     *line;
771 static size_t lmax, lcur;
772
773 #define CATLINE( buf ) \
774         do { \
775                 size_t len = strlen( buf ); \
776                 while ( lcur + len + 1 > lmax ) { \
777                         lmax += BUFSIZ; \
778                         line = (char *) ch_realloc( line, lmax ); \
779                 } \
780                 strcpy( line + lcur, buf ); \
781                 lcur += len; \
782         } while( 0 )
783
784 static void
785 fp_getline_init(ConfigArgs *c) {
786         c->lineno = -1;
787         buf[0] = '\0';
788 }
789
790 static int
791 fp_getline( FILE *fp, ConfigArgs *c )
792 {
793         char    *p;
794
795         lcur = 0;
796         CATLINE(buf);
797         c->lineno++;
798
799         /* avoid stack of bufs */
800         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
801                 buf[0] = '\0';
802                 c->line = line;
803                 return(1);
804         }
805
806         while ( fgets( buf, sizeof( buf ), fp ) ) {
807                 p = strchr( buf, '\n' );
808                 if ( p ) {
809                         if ( p > buf && p[-1] == '\r' ) {
810                                 --p;
811                         }
812                         *p = '\0';
813                 }
814                 /* XXX ugly */
815                 c->line = line;
816                 if ( line[0]
817                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
818                                 && p[-1] != '\\' )
819                 {
820                         p[0] = '\0';
821                         lcur--;
822                         
823                 } else {
824                         if ( !isspace( (unsigned char)buf[0] ) ) {
825                                 return(1);
826                         }
827                         buf[0] = ' ';
828                 }
829                 CATLINE(buf);
830                 c->lineno++;
831         }
832
833         buf[0] = '\0';
834         c->line = line;
835         return(line[0] ? 1 : 0);
836 }
837
838 static int
839 fp_parse_line(ConfigArgs *c)
840 {
841         char *token;
842         char *tline = ch_strdup(c->line);
843         char *hide[] = { "rootpw", "replica", "bindpw", "pseudorootpw", "dbpasswd", '\0' };
844         int i;
845
846         c->argc = 0;
847         token = strtok_quote(tline, " \t");
848
849         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
850         if(strtok_quote_ptr) *strtok_quote_ptr = ' ';
851         Debug(LDAP_DEBUG_CONFIG, "line %lu (%s%s)\n", c->lineno, hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
852         if(strtok_quote_ptr) *strtok_quote_ptr = '\0';
853
854         for(; token; token = strtok_quote(NULL, " \t")) {
855                 if(c->argc == c->argv_size - 1) {
856                         char **tmp;
857                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
858                         if(!tmp) {
859                                 Debug(LDAP_DEBUG_ANY, "line %lu: out of memory\n", c->lineno, 0, 0);
860                                 return -1;
861                         }
862                         c->argv = tmp;
863                         c->argv_size += ARGS_STEP;
864                 }
865                 c->argv[c->argc++] = token;
866         }
867         c->argv[c->argc] = NULL;
868         return(0);
869 }
870
871 void
872 config_destroy( )
873 {
874         ucdata_unload( UCDATA_ALL );
875         if ( frontendDB ) {
876                 /* NOTE: in case of early exit, frontendDB can be NULL */
877                 if ( frontendDB->be_schemandn.bv_val )
878                         free( frontendDB->be_schemandn.bv_val );
879                 if ( frontendDB->be_schemadn.bv_val )
880                         free( frontendDB->be_schemadn.bv_val );
881                 if ( frontendDB->be_acl )
882                         acl_destroy( frontendDB->be_acl, NULL );
883         }
884         free( line );
885         if ( slapd_args_file )
886                 free ( slapd_args_file );
887         if ( slapd_pid_file )
888                 free ( slapd_pid_file );
889         if ( default_passwd_hash )
890                 ldap_charray_free( default_passwd_hash );
891 }
892
893 char **
894 slap_str2clist( char ***out, char *in, const char *brkstr )
895 {
896         char    *str;
897         char    *s;
898         char    *lasts;
899         int     i, j;
900         char    **new;
901
902         /* find last element in list */
903         for (i = 0; *out && (*out)[i]; i++);
904
905         /* protect the input string from strtok */
906         str = ch_strdup( in );
907
908         if ( *str == '\0' ) {
909                 free( str );
910                 return( *out );
911         }
912
913         /* Count words in string */
914         j=1;
915         for ( s = str; *s; s++ ) {
916                 if ( strchr( brkstr, *s ) != NULL ) {
917                         j++;
918                 }
919         }
920
921         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
922         new = *out + i;
923         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
924                 s != NULL;
925                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
926         {
927                 *new = ch_strdup( s );
928                 new++;
929         }
930
931         *new = NULL;
932         free( str );
933         return( *out );
934 }