]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
7738724f32fcf74a2a3ef6f0ecc0d6459d23a3c6
[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 <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include "slap.h"
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45 #include "lutil.h"
46 #include "config.h"
47
48 #define ARGS_STEP       512
49
50 /*
51  * defaults for various global variables
52  */
53 slap_mask_t             global_allows = 0;
54 slap_mask_t             global_disallows = 0;
55 int             global_gentlehup = 0;
56 int             global_idletimeout = 0;
57 char    *global_host = NULL;
58 char    *global_realm = NULL;
59 char            *ldap_srvtab = "";
60 char            **default_passwd_hash = NULL;
61 struct berval default_search_base = BER_BVNULL;
62 struct berval default_search_nbase = BER_BVNULL;
63
64 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
65 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
66
67 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
68 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
69
70 char   *slapd_pid_file  = NULL;
71 char   *slapd_args_file = NULL;
72
73 int use_reverse_lookup = 0;
74
75 #ifdef LDAP_SLAPI
76 int slapi_plugins_used = 0;
77 #endif
78
79 static int fp_getline(FILE *fp, ConfigArgs *c);
80 static void fp_getline_init(ConfigArgs *c);
81 static int fp_parse_line(ConfigArgs *c);
82
83 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
84 static char *strtok_quote_ldif(char **line);
85
86 ConfigArgs *
87 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
88 {
89         ConfigArgs *c;
90         c = ch_calloc( 1, sizeof( ConfigArgs ) );
91         if ( c == NULL ) return(NULL);
92         c->be     = be; 
93         c->fname  = fname;
94         c->argc   = argc;
95         c->argv   = argv; 
96         c->lineno = lineno;
97         snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
98         return(c);
99 }
100
101 void
102 init_config_argv( ConfigArgs *c )
103 {
104         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
105         c->argv_size = ARGS_STEP + 1;
106 }
107
108 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
109         int i;
110
111         for(i = 0; Conf[i].name; i++)
112                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
113                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
114         if ( !Conf[i].name ) return NULL;
115         return Conf+i;
116 }
117
118 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
119         int rc, arg_user, arg_type, iarg;
120         long larg;
121         ber_len_t barg;
122         
123         arg_type = Conf->arg_type;
124         if(arg_type == ARG_IGNORED) {
125                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
126                         c->log, Conf->name, 0);
127                 return(0);
128         }
129         if((arg_type & ARG_DN) && c->argc == 1) {
130                 c->argc = 2;
131                 c->argv[1] = "";
132         }
133         if(Conf->min_args && (c->argc < Conf->min_args)) {
134                 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
135                         c->argv[0], Conf->what );
136                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
137                 return(ARG_BAD_CONF);
138         }
139         if(Conf->max_args && (c->argc > Conf->max_args)) {
140                 char    *ignored = " ignored";
141
142                 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
143                         c->argv[0], Conf->what );
144
145 #ifdef LDAP_DEVEL
146                 ignored = "";
147 #endif /* LDAP_DEVEL */
148                 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
149                                 c->log, c->msg, ignored );
150 #ifdef LDAP_DEVEL
151                 return(ARG_BAD_CONF);
152 #endif /* LDAP_DEVEL */
153         }
154         if((arg_type & ARG_DB) && !c->be) {
155                 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
156                         c->argv[0] );
157                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
158                         c->log, c->msg, 0);
159                 return(ARG_BAD_CONF);
160         }
161         if((arg_type & ARG_PRE_BI) && c->bi) {
162                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
163                         c->argv[0], (arg_type & ARG_PRE_DB) ? "or database " : "" );
164                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
165                         c->log, c->msg, 0 );
166                 return(ARG_BAD_CONF);
167         }
168         if((arg_type & ARG_PRE_DB) && c->be && c->be != frontendDB) {
169                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
170                         c->argv[0] );
171                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
172                         c->log, c->msg, 0);
173                 return(ARG_BAD_CONF);
174         }
175         if((arg_type & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
176                 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
177                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
178                         c->log, c->msg, 0);
179                 return(ARG_BAD_CONF);
180         }
181         if((arg_type & ARGS_POINTER) && !Conf->arg_item && !(arg_type & ARG_OFFSET)) {
182                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
183                         c->argv[0] );
184                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
185                         c->log, c->msg, 0);
186                 return(ARG_BAD_CONF);
187         }
188         c->type = arg_user = (arg_type & ARGS_USERLAND);
189         memset(&c->values, 0, sizeof(c->values));
190         if(arg_type & ARGS_NUMERIC) {
191                 int j;
192                 iarg = 0; larg = 0; barg = 0;
193                 switch(arg_type & ARGS_NUMERIC) {
194                         case ARG_INT:           iarg = strtol(c->argv[1], NULL, 0); break;
195                         case ARG_LONG:          larg = strtol(c->argv[1], NULL, 0);     break;
196                         case ARG_BER_LEN_T:     barg = (ber_len_t)atol(c->argv[1]);     break;
197                         case ARG_ON_OFF:
198                                 if(c->argc == 1) {
199                                         iarg = 1;
200                                 } else if(!strcasecmp(c->argv[1], "on") ||
201                                         !strcasecmp(c->argv[1], "true")) {
202                                         iarg = 1;
203                                 } else if(!strcasecmp(c->argv[1], "off") ||
204                                         !strcasecmp(c->argv[1], "false")) {
205                                         iarg = 0;
206                                 } else {
207                                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value, ignored",
208                                                 c->argv[0] );
209                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
210                                                 c->log, c->msg, 0 );
211                                         return(0);
212                                 }
213                                 break;
214                 }
215                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
216                 if(iarg < j && larg < j && barg < j ) {
217                         larg = larg ? larg : (barg ? barg : iarg);
218                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value, ignored",
219                                 c->argv[0] );
220                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
221                                 c->log, c->msg, 0 );
222                         return(ARG_BAD_CONF);
223                 }
224                 switch(arg_type & ARGS_NUMERIC) {
225                         case ARG_ON_OFF:
226                         case ARG_INT:           c->value_int = iarg;            break;
227                         case ARG_LONG:          c->value_long = larg;           break;
228                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
229                 }
230         } else if(arg_type & ARG_STRING) {
231                 if ( !check_only )
232                         c->value_string = ch_strdup(c->argv[1]);
233         } else if(arg_type & ARG_BERVAL) {
234                 if ( !check_only )
235                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
236         } else if(arg_type & ARG_DN) {
237                 struct berval bv;
238                 ber_str2bv( c->argv[1], 0, 0, &bv );
239                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
240                 if ( rc != LDAP_SUCCESS ) {
241                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
242                                 c->argv[0], rc, ldap_err2string( rc ));
243                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
244                         return(ARG_BAD_CONF);
245                 }
246                 if ( check_only ) {
247                         ch_free( c->value_ndn.bv_val );
248                         ch_free( c->value_dn.bv_val );
249                 }
250         }
251         return 0;
252 }
253
254 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
255         int rc, arg_type;
256         void *ptr = NULL;
257
258         arg_type = Conf->arg_type;
259         if(arg_type & ARG_MAGIC) {
260                 if(!c->be) c->be = frontendDB;
261                 c->msg[0] = '\0';
262                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
263 #if 0
264                 if(c->be == frontendDB) c->be = NULL;
265 #endif
266                 if(rc) {
267                         if ( !c->msg[0] ) {
268                                 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
269                                         c->argv[0], rc );
270                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
271                                         c->log, c->msg, 0 );
272                         }
273                         return(ARG_BAD_CONF);
274                 }
275                 return(0);
276         }
277         if(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                         snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
284                                 c->argv[0] );
285                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
286                                 c->log, c->msg, 0);
287                         return(ARG_BAD_CONF);
288                 }
289                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
290         } else if (arg_type & ARGS_POINTER) {
291                 ptr = Conf->arg_item;
292         }
293         if(arg_type & ARGS_POINTER)
294                 switch(arg_type & ARGS_POINTER) {
295                         case ARG_ON_OFF:
296                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
297                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
298                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
299                         case ARG_STRING: {
300                                 char *cc = *(char**)ptr;
301                                 if(cc) {
302                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
303                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
304                                                         c->log, Conf->name, 0 );
305                                                 return(ARG_BAD_CONF);
306                                         }
307                                         ch_free(cc);
308                                 }
309                                 *(char **)ptr = c->value_string;
310                                 break;
311                                 }
312                         case ARG_BERVAL:
313                                 *(struct berval *)ptr = c->value_bv;
314                                 break;
315                 }
316         return(0);
317 }
318
319 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
320         int rc, arg_type;
321
322         arg_type = Conf->arg_type;
323         if(arg_type == ARG_IGNORED) {
324                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
325                         c->log, Conf->name, 0);
326                 return(0);
327         }
328         rc = config_check_vals( Conf, c, 0 );
329         if ( rc ) return rc;
330         return config_set_vals( Conf, c );
331 }
332
333 int
334 config_del_vals(ConfigTable *cf, ConfigArgs *c)
335 {
336         int rc = 0;
337
338         /* If there is no handler, just ignore it */
339         if ( cf->arg_type & ARG_MAGIC ) {
340                 c->op = LDAP_MOD_DELETE;
341                 c->type = cf->arg_type & ARGS_USERLAND;
342                 rc = (*((ConfigDriver*)cf->arg_item))(c);
343         }
344         return rc;
345 }
346
347 int
348 config_get_vals(ConfigTable *cf, ConfigArgs *c)
349 {
350         int rc = 0;
351         struct berval bv;
352         void *ptr;
353
354         if ( cf->arg_type & ARG_IGNORED ) {
355                 return 1;
356         }
357
358         memset(&c->values, 0, sizeof(c->values));
359         c->rvalue_vals = NULL;
360         c->rvalue_nvals = NULL;
361         c->op = SLAP_CONFIG_EMIT;
362         c->type = cf->arg_type & ARGS_USERLAND;
363
364         if ( cf->arg_type & ARG_MAGIC ) {
365                 rc = (*((ConfigDriver*)cf->arg_item))(c);
366                 if ( rc ) return rc;
367         } else {
368                 if ( cf->arg_type & ARG_OFFSET ) {
369                         if ( c->be )
370                                 ptr = c->be->be_private;
371                         else if ( c->bi )
372                                 ptr = c->bi->bi_private;
373                         else
374                                 return 1;
375                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
376                 } else {
377                         ptr = cf->arg_item;
378                 }
379                 
380                 switch(cf->arg_type & ARGS_POINTER) {
381                 case ARG_ON_OFF:
382                 case ARG_INT:   c->value_int = *(int *)ptr; break;
383                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
384                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
385                 case ARG_STRING:
386                         if ( *(char **)ptr )
387                                 c->value_string = ch_strdup(*(char **)ptr);
388                         break;
389                 case ARG_BERVAL:
390                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
391                 }
392         }
393         if ( cf->arg_type & ARGS_POINTER) {
394                 bv.bv_val = c->log;
395                 switch(cf->arg_type & ARGS_POINTER) {
396                 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
397                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
398                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
399                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
400                         c->value_int ? "TRUE" : "FALSE"); break;
401                 case ARG_STRING:
402                         if ( c->value_string && c->value_string[0]) {
403                                 ber_str2bv( c->value_string, 0, 0, &bv);
404                         } else {
405                                 return 1;
406                         }
407                         break;
408                 case ARG_BERVAL:
409                         if ( !BER_BVISEMPTY( &c->value_bv )) {
410                                 bv = c->value_bv;
411                         } else {
412                                 return 1;
413                         }
414                         break;
415                 }
416                 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
417                         return 1;
418                 }
419                 if (( cf->arg_type & ARGS_POINTER ) == ARG_STRING )
420                         ber_bvarray_add(&c->rvalue_vals, &bv);
421                 else
422                         value_add_one(&c->rvalue_vals, &bv);
423         }
424         return rc;
425 }
426
427 int
428 init_config_attrs(ConfigTable *ct) {
429         LDAPAttributeType *at;
430         int i, code;
431         const char *err;
432
433         for (i=0; ct[i].name; i++ ) {
434                 int             freeit = 0;
435
436                 if ( !ct[i].attribute ) continue;
437                 at = ldap_str2attributetype( ct[i].attribute,
438                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
439                 if ( !at ) {
440                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
441                                 ct[i].attribute, ldap_scherr2str(code), err );
442                         return code;
443                 }
444
445                 code = at_add( at, 0, NULL, &err );
446                 if ( code ) {
447                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
448                                 freeit = 1;
449
450                         } else {
451                                 ldap_attributetype_free( at );
452                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
453                                         ct[i].attribute, scherr2str(code), err );
454                                 return code;
455                         }
456                 }
457                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
458                 if ( freeit ) {
459                         ldap_attributetype_free( at );
460                 } else {
461                         ldap_memfree( at );
462                 }
463                 if ( code ) {
464                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
465                                 ct[i].attribute, err );
466                         return code;
467                 }
468         }
469
470         return 0;
471 }
472
473 int
474 init_config_ocs( ConfigOCs *ocs ) {
475         int i;
476
477         for (i=0;ocs[i].co_def;i++) {
478                 LDAPObjectClass *oc;
479                 int code;
480                 const char *err;
481
482                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
483                         LDAP_SCHEMA_ALLOW_ALL );
484                 if ( !oc ) {
485                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
486                                 ocs[i].co_def, ldap_scherr2str(code), err );
487                         return code;
488                 }
489                 code = oc_add(oc,0,NULL,&err);
490                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
491                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
492                                 ocs[i].co_def, scherr2str(code), err );
493                         return code;
494                 }
495                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
496                 ldap_memfree(oc);
497         }
498         return 0;
499 }
500
501 /* Split an LDIF line into space-separated tokens. Words may be grouped
502  * by quotes. A quoted string may begin in the middle of a word, but must
503  * end at the end of the word (be followed by whitespace or EOS). Any other
504  * quotes are passed through unchanged. All other characters are passed
505  * through unchanged.
506  */
507 static char *
508 strtok_quote_ldif( char **line )
509 {
510         char *beg, *ptr, *quote=NULL;
511         int inquote=0;
512
513         ptr = *line;
514
515         if ( !ptr || !*ptr )
516                 return NULL;
517
518         while( isspace( *ptr )) ptr++;
519
520         if ( *ptr == '"' ) {
521                 inquote = 1;
522                 ptr++;
523         }
524
525         beg = ptr;
526
527         for (;*ptr;ptr++) {
528                 if ( *ptr == '"' ) {
529                         if ( inquote && ( !ptr[1] || isspace(ptr[1]))) {
530                                 *ptr++ = '\0';
531                                 break;
532                         }
533                         inquote = 1;
534                         quote = ptr;
535                         continue;
536                 }
537                 if ( inquote )
538                         continue;
539                 if ( isspace( *ptr )) {
540                         *ptr++ = '\0';
541                         break;
542                 }
543         }
544         if ( quote ) {
545                 while ( quote < ptr ) {
546                         *quote = quote[1];
547                         quote++;
548                 }
549         }
550         if ( !*ptr ) {
551                 *line = NULL;
552         } else {
553                 while ( isspace( *ptr )) ptr++;
554                 *line = ptr;
555         }
556         return beg;
557 }
558
559 static void
560 config_parse_ldif( ConfigArgs *c )
561 {
562         char *next;
563         c->tline = ch_strdup(c->line);
564         next = c->tline;
565
566         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
567                 c->argc++;
568                 if ( c->argc >= c->argv_size ) {
569                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
570                                 sizeof( *c->argv ));
571                         c->argv = tmp;
572                         c->argv_size += ARGS_STEP;
573                 }
574         }
575         c->argv[c->argc] = NULL;
576 }
577
578 int
579 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
580 {
581         int     rc = 0;
582
583         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
584                 ct->ad->ad_cname.bv_val, valx );
585         c->argc = 1;
586         c->argv[0] = ct->ad->ad_cname.bv_val;
587
588         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
589                 c->argv[c->argc] = c->line;
590                 c->argc++;
591                 c->argv[c->argc] = NULL;
592                 c->tline = NULL;
593         } else {
594                 config_parse_ldif( c );
595         }
596         rc = config_check_vals( ct, c, 1 );
597         ch_free( c->tline );
598
599         if ( rc )
600                 rc = LDAP_CONSTRAINT_VIOLATION;
601
602         return rc;
603 }
604
605 int
606 config_parse_add(ConfigTable *ct, ConfigArgs *c)
607 {
608         int     rc = 0;
609
610         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
611                 ct->ad->ad_cname.bv_val, c->valx );
612         c->argc = 1;
613         c->argv[0] = ct->ad->ad_cname.bv_val;
614
615         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
616                 c->argv[c->argc] = c->line;
617                 c->argc++;
618                 c->argv[c->argc] = NULL;
619                 c->tline = NULL;
620         } else {
621                 config_parse_ldif( c );
622         }
623         c->op = LDAP_MOD_ADD;
624         rc = config_add_vals( ct, c );
625         ch_free( c->tline );
626
627         return rc;
628 }
629
630 int
631 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
632 {
633         FILE *fp;
634         ConfigTable *ct;
635         ConfigArgs *c;
636         int rc;
637         struct stat s;
638
639         c = ch_calloc( 1, sizeof( ConfigArgs ) );
640         if ( c == NULL ) {
641                 return 1;
642         }
643
644         if ( depth ) {
645                 memcpy( c, cf, sizeof( ConfigArgs ) );
646         } else {
647                 c->depth = depth; /* XXX */
648                 c->bi = NULL;
649                 c->be = NULL;
650         }
651
652         c->valx = -1;
653         c->fname = fname;
654         init_config_argv( c );
655
656         if ( stat( fname, &s ) != 0 ) {
657                 ldap_syslog = 1;
658                 Debug(LDAP_DEBUG_ANY,
659                     "could not stat config file \"%s\": %s (%d)\n",
660                     fname, strerror(errno), errno);
661                 return(1);
662         }
663
664         if ( !S_ISREG( s.st_mode ) ) {
665                 ldap_syslog = 1;
666                 Debug(LDAP_DEBUG_ANY,
667                     "regular file expected, got \"%s\"\n",
668                     fname, 0, 0 );
669                 return(1);
670         }
671
672         fp = fopen( fname, "r" );
673         if ( fp == NULL ) {
674                 ldap_syslog = 1;
675                 Debug(LDAP_DEBUG_ANY,
676                     "could not open config file \"%s\": %s (%d)\n",
677                     fname, strerror(errno), errno);
678                 return(1);
679         }
680
681         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
682
683         fp_getline_init(c);
684
685         c->tline = NULL;
686
687         while ( fp_getline( fp, c ) ) {
688                 /* skip comments and blank lines */
689                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
690                         continue;
691                 }
692
693                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
694                                 c->fname, c->lineno );
695
696                 c->argc = 0;
697                 ch_free( c->tline );
698                 if ( fp_parse_line( c ) ) {
699                         rc = 1;
700                         goto done;
701                 }
702
703                 if ( c->argc < 1 ) {
704                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line" 
705                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
706                                 c->log, 0, 0);
707 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
708                         rc = 1;
709                         goto done;
710 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
711                         continue;
712 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
713                 }
714
715                 c->op = SLAP_CONFIG_ADD;
716
717                 ct = config_find_keyword( cft, c );
718                 if ( ct ) {
719                         rc = config_add_vals( ct, c );
720                         if ( !rc ) continue;
721
722                         if ( rc & ARGS_USERLAND ) {
723                                 /* XXX a usertype would be opaque here */
724                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
725                                         c->log, c->argv[0], 0);
726                                 rc = 1;
727                                 goto done;
728
729                         } else if ( rc == ARG_BAD_CONF ) {
730                                 rc = 1;
731                                 goto done;
732                         }
733                         
734                 } else if ( c->bi && !c->be ) {
735                         rc = SLAP_CONF_UNKNOWN;
736                         if ( c->bi->bi_cf_ocs ) {
737                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
738                                 if ( ct ) {
739                                         rc = config_add_vals( ct, c );
740                                 }
741                         }
742                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
743                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
744                                         c->argc, c->argv);
745                         }
746                         if ( rc ) {
747                                 switch(rc) {
748                                 case SLAP_CONF_UNKNOWN:
749                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
750                                                 "unknown directive <%s> inside backend info definition"
751                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
752                                                 c->log, *c->argv, 0);
753 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
754                                         continue;
755 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
756                                 default:
757                                         rc = 1;
758                                         goto done;
759                                 }
760                         }
761
762                 } else if ( c->be ) {
763                         rc = SLAP_CONF_UNKNOWN;
764                         if ( c->be->be_cf_ocs ) {
765                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
766                                 if ( ct ) {
767                                         rc = config_add_vals( ct, c );
768                                 }
769                         }
770                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
771                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
772                                         c->argc, c->argv);
773                         }
774                         if ( rc ) {
775                                 switch(rc) {
776                                 case SLAP_CONF_UNKNOWN:
777                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
778                                                 "unknown directive <%s> inside backend database "
779                                                 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
780                                                 c->log, *c->argv, 0);
781 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
782                                         continue;
783 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
784                                 default:
785                                         rc = 1;
786                                         goto done;
787                                 }
788                         }
789
790                 } else if ( frontendDB->be_config ) {
791                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
792                         if ( rc ) {
793                                 switch(rc) {
794                                 case SLAP_CONF_UNKNOWN:
795                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
796                                                 "unknown directive <%s> inside global database definition"
797                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
798                                                 c->log, *c->argv, 0);
799 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
800                                         continue;
801 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
802                                 default:
803                                         rc = 1;
804                                         goto done;
805                                 }
806                         }
807                         
808                 } else {
809                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
810                                 "unknown directive <%s> outside backend info and database definitions"
811                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
812                                 c->log, *c->argv, 0);
813 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
814                         rc = 1;
815                         goto done;
816 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
817                         continue;
818 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
819                 }
820         }
821
822         rc = 0;
823
824 done:
825         ch_free(c->tline);
826         fclose(fp);
827         ch_free(c->argv);
828         ch_free(c);
829         return(rc);
830 }
831
832 /* restrictops, allows, disallows, requires, loglevel */
833
834 int
835 verb_to_mask(const char *word, slap_verbmasks *v) {
836         int i;
837         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
838                 if(!strcasecmp(word, v[i].word.bv_val))
839                         break;
840         return(i);
841 }
842
843 int
844 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
845         int i, j;
846         for(i = 1; i < argc; i++) {
847                 j = verb_to_mask(argv[i], v);
848                 if(BER_BVISNULL(&v[j].word)) return i;
849                 while (!v[j].mask) j--;
850                 *m |= v[j].mask;
851         }
852         return(0);
853 }
854
855 /* Mask keywords that represent multiple bits should occur before single
856  * bit keywords in the verbmasks array.
857  */
858 int
859 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
860         int i, rc = 1;
861
862         if (m) {
863                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
864                         if (!v[i].mask) continue;
865                         if (( m & v[i].mask ) == v[i].mask ) {
866                                 value_add_one( bva, &v[i].word );
867                                 rc = 0;
868                                 m ^= v[i].mask;
869                                 if ( !m ) break;
870                         }
871                 }
872         }
873         return rc;
874 }
875
876 int
877 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
878 {
879         int             i;
880
881         assert( *vp == NULL );
882
883         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ )
884                 ;
885
886         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
887
888         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
889                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
890                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
891         }
892
893         BER_BVZERO( &(*vp)[ i ].word );
894
895         return 0;               
896 }
897
898 int
899 slap_verbmasks_destroy( slap_verbmasks *v )
900 {
901         int             i;
902
903         assert( v != NULL );
904
905         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
906                 ch_free( v[ i ].word.bv_val );
907         }
908
909         ch_free( v );
910
911         return 0;
912 }
913
914 int
915 slap_verbmasks_append(
916         slap_verbmasks  **vp,
917         slap_mask_t     m,
918         struct berval   *v,
919         slap_mask_t     *ignore )
920 {
921         int     i;
922
923         if ( !m ) {
924                 return LDAP_OPERATIONS_ERROR;
925         }
926
927         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
928                 if ( !(*vp)[ i ].mask ) continue;
929
930                 if ( ignore != NULL ) {
931                         int     j;
932
933                         for ( j = 0; ignore[ j ] != 0; j++ ) {
934                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
935                                         goto check_next;
936                                 }
937                         }
938                 }
939
940                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
941                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
942                                 /* already set; ignore */
943                                 return LDAP_SUCCESS;
944                         }
945                         /* conflicts */
946                         return LDAP_TYPE_OR_VALUE_EXISTS;
947                 }
948
949                 if ( m & (*vp)[ i ].mask ) {
950                         /* conflicts */
951                         return LDAP_CONSTRAINT_VIOLATION;
952                 }
953 check_next:;
954         }
955
956         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
957         ber_dupbv( &(*vp)[ i ].word, v );
958         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
959         BER_BVZERO( &(*vp)[ i + 1 ].word );
960
961         return LDAP_SUCCESS;
962 }
963
964 int
965 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
966         int i;
967
968         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
969                 if ( m == v[i].mask ) {
970                         if ( bv != NULL ) {
971                                 *bv = v[i].word;
972                         }
973                         return i;
974                 }
975         }
976         return -1;
977 }
978
979 static slap_verbmasks tlskey[] = {
980         { BER_BVC("no"),        SB_TLS_OFF },
981         { BER_BVC("yes"),       SB_TLS_ON },
982         { BER_BVC("critical"),  SB_TLS_CRITICAL },
983         { BER_BVNULL, 0 }
984 };
985
986 static slap_verbmasks methkey[] = {
987         { BER_BVC("none"),      LDAP_AUTH_NONE },
988         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
989 #ifdef HAVE_CYRUS_SASL
990         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
991 #endif
992         { BER_BVNULL, 0 }
993 };
994
995 static slap_cf_aux_table bindkey[] = {
996         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
997         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
998         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
999         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1000         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1001         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1002         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1003         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1004         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1005         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1006         { BER_BVNULL, 0, 0, 0, NULL }
1007 };
1008
1009 int
1010 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1011 {
1012         int rc = 0;
1013         slap_cf_aux_table *tab;
1014
1015         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1016                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1017                         char **cptr, *next;
1018                         int *iptr, j;
1019                         unsigned *uptr;
1020                         struct berval *bptr;
1021                         const char *val = word + tab->key.bv_len;
1022
1023                         switch ( tab->type ) {
1024                         case 's':
1025                                 cptr = (char **)((char *)dst + tab->off);
1026                                 *cptr = ch_strdup( val );
1027                                 break;
1028
1029                         case 'b':
1030                                 bptr = (struct berval *)((char *)dst + tab->off);
1031                                 ber_str2bv( val, 0, 1, bptr );
1032                                 break;
1033
1034                         case 'd':
1035                                 assert( tab->aux != NULL );
1036                                 iptr = (int *)((char *)dst + tab->off);
1037
1038                                 rc = 1;
1039                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1040                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1041                                                 *iptr = tab->aux[j].mask;
1042                                                 rc = 0;
1043                                         }
1044                                 }
1045                                 break;
1046
1047                         case 'i':
1048                                 iptr = (int *)((char *)dst + tab->off);
1049
1050                                 *iptr = strtol( val, &next, 0 );
1051                                 if ( next == val || next[ 0 ] != '\0' ) {
1052                                         rc = 1;
1053                                 }
1054                                 break;
1055
1056                         case 'u':
1057                                 uptr = (unsigned *)((char *)dst + tab->off);
1058
1059                                 *uptr = strtoul( val, &next, 0 );
1060                                 if ( next == val || next[ 0 ] != '\0' ) {
1061                                         rc = 1;
1062                                 }
1063                                 break;
1064                         }
1065
1066                         if ( rc ) {
1067                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1068                                         tabmsg, word, 0 );
1069                         }
1070                         
1071                         return rc;
1072                 }
1073         }
1074
1075         return rc;
1076 }
1077
1078 int
1079 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1080 {
1081         char buf[BUFSIZ], *ptr;
1082         slap_cf_aux_table *tab;
1083         struct berval tmp;
1084
1085         ptr = buf;
1086         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1087                 char **cptr;
1088                 int *iptr, i;
1089                 unsigned *uptr;
1090                 struct berval *bptr;
1091
1092                 cptr = (char **)((char *)src + tab->off);
1093
1094                 switch ( tab->type ) {
1095                 case 'b':
1096                         bptr = (struct berval *)((char *)src + tab->off);
1097                         cptr = &bptr->bv_val;
1098                 case 's':
1099                         if ( *cptr ) {
1100                                 *ptr++ = ' ';
1101                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1102                                 if ( tab->quote ) *ptr++ = '"';
1103                                 ptr = lutil_strcopy( ptr, *cptr );
1104                                 if ( tab->quote ) *ptr++ = '"';
1105                         }
1106                         break;
1107
1108                 case 'd':
1109                         assert( tab->aux != NULL );
1110                         iptr = (int *)((char *)src + tab->off);
1111                 
1112                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1113                                 if ( *iptr == tab->aux[i].mask ) {
1114                                         *ptr++ = ' ';
1115                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1116                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1117                                         break;
1118                                 }
1119                         }
1120                         break;
1121
1122                 case 'i':
1123                         iptr = (int *)((char *)src + tab->off);
1124                         *ptr++ = ' ';
1125                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1126                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1127                         break;
1128
1129                 case 'u':
1130                         uptr = (unsigned *)((char *)src + tab->off);
1131                         *ptr++ = ' ';
1132                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1133                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1134                         break;
1135                 }
1136         }
1137         tmp.bv_val = buf;
1138         tmp.bv_len = ptr - buf;
1139         ber_dupbv( bv, &tmp );
1140         return 0;
1141 }
1142
1143 int
1144 bindconf_parse( const char *word, slap_bindconf *bc )
1145 {
1146         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1147 }
1148
1149 int
1150 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1151 {
1152         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1153 }
1154
1155 void bindconf_free( slap_bindconf *bc ) {
1156         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1157                 ch_free( bc->sb_uri.bv_val );
1158                 BER_BVZERO( &bc->sb_uri );
1159         }
1160         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1161                 ch_free( bc->sb_binddn.bv_val );
1162                 BER_BVZERO( &bc->sb_binddn );
1163         }
1164         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1165                 ch_free( bc->sb_cred.bv_val );
1166                 BER_BVZERO( &bc->sb_cred );
1167         }
1168         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1169                 ch_free( bc->sb_saslmech.bv_val );
1170                 BER_BVZERO( &bc->sb_saslmech );
1171         }
1172         if ( bc->sb_secprops ) {
1173                 ch_free( bc->sb_secprops );
1174                 bc->sb_secprops = NULL;
1175         }
1176         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1177                 ch_free( bc->sb_realm.bv_val );
1178                 BER_BVZERO( &bc->sb_realm );
1179         }
1180         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1181                 ch_free( bc->sb_authcId.bv_val );
1182                 BER_BVZERO( &bc->sb_authcId );
1183         }
1184         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1185                 ch_free( bc->sb_authzId.bv_val );
1186                 BER_BVZERO( &bc->sb_authzId );
1187         }
1188 }
1189
1190
1191 /* -------------------------------------- */
1192
1193
1194 static char *
1195 strtok_quote( char *line, char *sep, char **quote_ptr )
1196 {
1197         int             inquote;
1198         char            *tmp;
1199         static char     *next;
1200
1201         *quote_ptr = NULL;
1202         if ( line != NULL ) {
1203                 next = line;
1204         }
1205         while ( *next && strchr( sep, *next ) ) {
1206                 next++;
1207         }
1208
1209         if ( *next == '\0' ) {
1210                 next = NULL;
1211                 return( NULL );
1212         }
1213         tmp = next;
1214
1215         for ( inquote = 0; *next; ) {
1216                 switch ( *next ) {
1217                 case '"':
1218                         if ( inquote ) {
1219                                 inquote = 0;
1220                         } else {
1221                                 inquote = 1;
1222                         }
1223                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1224                         break;
1225
1226                 case '\\':
1227                         if ( next[1] )
1228                                 AC_MEMCPY( next,
1229                                             next + 1, strlen( next + 1 ) + 1 );
1230                         next++;         /* dont parse the escaped character */
1231                         break;
1232
1233                 default:
1234                         if ( ! inquote ) {
1235                                 if ( strchr( sep, *next ) != NULL ) {
1236                                         *quote_ptr = next;
1237                                         *next++ = '\0';
1238                                         return( tmp );
1239                                 }
1240                         }
1241                         next++;
1242                         break;
1243                 }
1244         }
1245
1246         return( tmp );
1247 }
1248
1249 static char     buf[BUFSIZ];
1250 static char     *line;
1251 static size_t lmax, lcur;
1252
1253 #define CATLINE( buf ) \
1254         do { \
1255                 size_t len = strlen( buf ); \
1256                 while ( lcur + len + 1 > lmax ) { \
1257                         lmax += BUFSIZ; \
1258                         line = (char *) ch_realloc( line, lmax ); \
1259                 } \
1260                 strcpy( line + lcur, buf ); \
1261                 lcur += len; \
1262         } while( 0 )
1263
1264 static void
1265 fp_getline_init(ConfigArgs *c) {
1266         c->lineno = -1;
1267         buf[0] = '\0';
1268 }
1269
1270 static int
1271 fp_getline( FILE *fp, ConfigArgs *c )
1272 {
1273         char    *p;
1274
1275         lcur = 0;
1276         CATLINE(buf);
1277         c->lineno++;
1278
1279         /* avoid stack of bufs */
1280         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1281                 buf[0] = '\0';
1282                 c->line = line;
1283                 return(1);
1284         }
1285
1286         while ( fgets( buf, sizeof( buf ), fp ) ) {
1287                 p = strchr( buf, '\n' );
1288                 if ( p ) {
1289                         if ( p > buf && p[-1] == '\r' ) {
1290                                 --p;
1291                         }
1292                         *p = '\0';
1293                 }
1294                 /* XXX ugly */
1295                 c->line = line;
1296                 if ( line[0]
1297                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1298                                 && p[-1] != '\\' )
1299                 {
1300                         p[0] = '\0';
1301                         lcur--;
1302                         
1303                 } else {
1304                         if ( !isspace( (unsigned char)buf[0] ) ) {
1305                                 return(1);
1306                         }
1307                         buf[0] = ' ';
1308                 }
1309                 CATLINE(buf);
1310                 c->lineno++;
1311         }
1312
1313         buf[0] = '\0';
1314         c->line = line;
1315         return(line[0] ? 1 : 0);
1316 }
1317
1318 static int
1319 fp_parse_line(ConfigArgs *c)
1320 {
1321         char *token;
1322         static char *const hide[] = {
1323                 "rootpw", "replica", "syncrepl",  /* in slapd */
1324                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1325                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1326                 "pseudorootpw",  /* in back-meta */
1327                 "dbpasswd",  /* in back-sql */
1328                 NULL
1329         };
1330         char *quote_ptr;
1331         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1332
1333         c->tline = ch_strdup(c->line);
1334         token = strtok_quote(c->tline, " \t", &quote_ptr);
1335
1336         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1337         if(quote_ptr) *quote_ptr = ' ';
1338         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1339                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1340         if(quote_ptr) *quote_ptr = '\0';
1341
1342         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1343                 if(c->argc >= c->argv_size) {
1344                         char **tmp;
1345                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1346                         if(!tmp) {
1347                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1348                                 return -1;
1349                         }
1350                         c->argv = tmp;
1351                         c->argv_size += ARGS_STEP;
1352                 }
1353                 if(token == NULL)
1354                         break;
1355                 c->argv[c->argc++] = token;
1356         }
1357         c->argv[c->argc] = NULL;
1358         return(0);
1359 }
1360
1361 void
1362 config_destroy( )
1363 {
1364         ucdata_unload( UCDATA_ALL );
1365         if ( frontendDB ) {
1366                 /* NOTE: in case of early exit, frontendDB can be NULL */
1367                 if ( frontendDB->be_schemandn.bv_val )
1368                         free( frontendDB->be_schemandn.bv_val );
1369                 if ( frontendDB->be_schemadn.bv_val )
1370                         free( frontendDB->be_schemadn.bv_val );
1371                 if ( frontendDB->be_acl )
1372                         acl_destroy( frontendDB->be_acl, NULL );
1373         }
1374         free( line );
1375         if ( slapd_args_file )
1376                 free ( slapd_args_file );
1377         if ( slapd_pid_file )
1378                 free ( slapd_pid_file );
1379         if ( default_passwd_hash )
1380                 ldap_charray_free( default_passwd_hash );
1381 }
1382
1383 char **
1384 slap_str2clist( char ***out, char *in, const char *brkstr )
1385 {
1386         char    *str;
1387         char    *s;
1388         char    *lasts;
1389         int     i, j;
1390         char    **new;
1391
1392         /* find last element in list */
1393         for (i = 0; *out && (*out)[i]; i++);
1394
1395         /* protect the input string from strtok */
1396         str = ch_strdup( in );
1397
1398         if ( *str == '\0' ) {
1399                 free( str );
1400                 return( *out );
1401         }
1402
1403         /* Count words in string */
1404         j=1;
1405         for ( s = str; *s; s++ ) {
1406                 if ( strchr( brkstr, *s ) != NULL ) {
1407                         j++;
1408                 }
1409         }
1410
1411         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1412         new = *out + i;
1413         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1414                 s != NULL;
1415                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1416         {
1417                 *new = ch_strdup( s );
1418                 new++;
1419         }
1420
1421         *new = NULL;
1422         free( str );
1423         return( *out );
1424 }
1425
1426 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1427         int argc, char **argv )
1428 {
1429         ConfigArgs c = { 0 };
1430         ConfigTable *ct;
1431         int rc;
1432
1433         c.be = be;
1434         c.fname = fname;
1435         c.lineno = lineno;
1436         c.argc = argc;
1437         c.argv = argv;
1438         c.valx = -1;
1439         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1440
1441         rc = SLAP_CONF_UNKNOWN;
1442         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1443         if ( ct )
1444                 rc = config_add_vals( ct, &c );
1445         return rc;
1446 }