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