]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
cbc9f21ec6ea39b916b3cf453864f6e239a63903
[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                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
452                                         ct[i].attribute, scherr2str(code), err );
453                                 return code;
454                         }
455                 }
456                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
457                 if ( freeit ) {
458                         ldap_attributetype_free( at );
459                 } else {
460                         ldap_memfree( at );
461                 }
462                 if ( code ) {
463                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
464                                 ct[i].attribute, err );
465                         return code;
466                 }
467         }
468
469         return 0;
470 }
471
472 int
473 init_config_ocs( ConfigOCs *ocs ) {
474         int i;
475
476         for (i=0;ocs[i].co_def;i++) {
477                 LDAPObjectClass *oc;
478                 int code;
479                 const char *err;
480
481                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
482                         LDAP_SCHEMA_ALLOW_ALL );
483                 if ( !oc ) {
484                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
485                                 ocs[i].co_def, ldap_scherr2str(code), err );
486                         return code;
487                 }
488                 code = oc_add(oc,0,NULL,&err);
489                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
490                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
491                                 ocs[i].co_def, scherr2str(code), err );
492                         return code;
493                 }
494                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
495                 ldap_memfree(oc);
496         }
497         return 0;
498 }
499
500 /* Split an LDIF line into space-separated tokens. Words may be grouped
501  * by quotes. A quoted string may begin in the middle of a word, but must
502  * end at the end of the word (be followed by whitespace or EOS). Any other
503  * quotes are passed through unchanged. All other characters are passed
504  * through unchanged.
505  */
506 static char *
507 strtok_quote_ldif( char **line )
508 {
509         char *beg, *ptr, *quote=NULL;
510         int inquote=0;
511
512         ptr = *line;
513
514         if ( !ptr || !*ptr )
515                 return NULL;
516
517         while( isspace( *ptr )) ptr++;
518
519         if ( *ptr == '"' ) {
520                 inquote = 1;
521                 ptr++;
522         }
523
524         beg = ptr;
525
526         for (;*ptr;ptr++) {
527                 if ( *ptr == '"' ) {
528                         if ( inquote && ( !ptr[1] || isspace(ptr[1]))) {
529                                 *ptr++ = '\0';
530                                 break;
531                         }
532                         inquote = 1;
533                         quote = ptr;
534                         continue;
535                 }
536                 if ( inquote )
537                         continue;
538                 if ( isspace( *ptr )) {
539                         *ptr++ = '\0';
540                         break;
541                 }
542         }
543         if ( quote ) {
544                 while ( quote < ptr ) {
545                         *quote = quote[1];
546                         quote++;
547                 }
548         }
549         if ( !*ptr ) {
550                 *line = NULL;
551         } else {
552                 while ( isspace( *ptr )) ptr++;
553                 *line = ptr;
554         }
555         return beg;
556 }
557
558 static void
559 config_parse_ldif( ConfigArgs *c )
560 {
561         char *next;
562         c->tline = ch_strdup(c->line);
563         next = c->tline;
564
565         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
566                 c->argc++;
567                 if ( c->argc >= c->argv_size ) {
568                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
569                                 sizeof( *c->argv ));
570                         c->argv = tmp;
571                         c->argv_size += ARGS_STEP;
572                 }
573         }
574         c->argv[c->argc] = NULL;
575 }
576
577 int
578 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
579 {
580         int     rc = 0;
581
582         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
583                 ct->ad->ad_cname.bv_val, valx );
584         c->argc = 1;
585         c->argv[0] = ct->ad->ad_cname.bv_val;
586
587         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
588                 c->argv[c->argc] = c->line;
589                 c->argc++;
590                 c->argv[c->argc] = NULL;
591                 c->tline = NULL;
592         } else {
593                 config_parse_ldif( c );
594         }
595         rc = config_check_vals( ct, c, 1 );
596         ch_free( c->tline );
597
598         if ( rc )
599                 rc = LDAP_CONSTRAINT_VIOLATION;
600
601         return rc;
602 }
603
604 int
605 config_parse_add(ConfigTable *ct, ConfigArgs *c)
606 {
607         int     rc = 0;
608
609         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
610                 ct->ad->ad_cname.bv_val, c->valx );
611         c->argc = 1;
612         c->argv[0] = ct->ad->ad_cname.bv_val;
613
614         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
615                 c->argv[c->argc] = c->line;
616                 c->argc++;
617                 c->argv[c->argc] = NULL;
618                 c->tline = NULL;
619         } else {
620                 config_parse_ldif( c );
621         }
622         c->op = LDAP_MOD_ADD;
623         rc = config_add_vals( ct, c );
624         ch_free( c->tline );
625
626         return rc;
627 }
628
629 int
630 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
631 {
632         FILE *fp;
633         ConfigTable *ct;
634         ConfigArgs *c;
635         int rc;
636         struct stat s;
637
638         c = ch_calloc( 1, sizeof( ConfigArgs ) );
639         if ( c == NULL ) {
640                 return 1;
641         }
642
643         if ( depth ) {
644                 memcpy( c, cf, sizeof( ConfigArgs ) );
645         } else {
646                 c->depth = depth; /* XXX */
647                 c->bi = NULL;
648                 c->be = NULL;
649         }
650
651         c->valx = -1;
652         c->fname = fname;
653         init_config_argv( c );
654
655         if ( stat( fname, &s ) != 0 ) {
656                 ldap_syslog = 1;
657                 Debug(LDAP_DEBUG_ANY,
658                     "could not stat config file \"%s\": %s (%d)\n",
659                     fname, strerror(errno), errno);
660                 return(1);
661         }
662
663         if ( !S_ISREG( s.st_mode ) ) {
664                 ldap_syslog = 1;
665                 Debug(LDAP_DEBUG_ANY,
666                     "regular file expected, got \"%s\"\n",
667                     fname, 0, 0 );
668                 return(1);
669         }
670
671         fp = fopen( fname, "r" );
672         if ( fp == NULL ) {
673                 ldap_syslog = 1;
674                 Debug(LDAP_DEBUG_ANY,
675                     "could not open config file \"%s\": %s (%d)\n",
676                     fname, strerror(errno), errno);
677                 return(1);
678         }
679
680         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
681
682         fp_getline_init(c);
683
684         c->tline = NULL;
685
686         while ( fp_getline( fp, c ) ) {
687                 /* skip comments and blank lines */
688                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
689                         continue;
690                 }
691
692                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
693                                 c->fname, c->lineno );
694
695                 c->argc = 0;
696                 ch_free( c->tline );
697                 if ( fp_parse_line( c ) ) {
698                         rc = 1;
699                         goto done;
700                 }
701
702                 if ( c->argc < 1 ) {
703                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line" 
704                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
705                                 c->log, 0, 0);
706 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
707                         rc = 1;
708                         goto done;
709 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
710                         continue;
711 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
712                 }
713
714                 c->op = SLAP_CONFIG_ADD;
715
716                 ct = config_find_keyword( cft, c );
717                 if ( ct ) {
718                         rc = config_add_vals( ct, c );
719                         if ( !rc ) continue;
720
721                         if ( rc & ARGS_USERLAND ) {
722                                 /* XXX a usertype would be opaque here */
723                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
724                                         c->log, c->argv[0], 0);
725                                 rc = 1;
726                                 goto done;
727
728                         } else if ( rc == ARG_BAD_CONF ) {
729                                 rc = 1;
730                                 goto done;
731                         }
732                         
733                 } else if ( c->bi && !c->be ) {
734                         rc = SLAP_CONF_UNKNOWN;
735                         if ( c->bi->bi_cf_ocs ) {
736                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
737                                 if ( ct ) {
738                                         rc = config_add_vals( ct, c );
739                                 }
740                         }
741                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
742                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
743                                         c->argc, c->argv);
744                         }
745                         if ( rc ) {
746                                 switch(rc) {
747                                 case SLAP_CONF_UNKNOWN:
748                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
749                                                 "unknown directive <%s> inside backend info definition"
750                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
751                                                 c->log, *c->argv, 0);
752 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
753                                         continue;
754 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
755                                 default:
756                                         rc = 1;
757                                         goto done;
758                                 }
759                         }
760
761                 } else if ( c->be ) {
762                         rc = SLAP_CONF_UNKNOWN;
763                         if ( c->be->be_cf_ocs ) {
764                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
765                                 if ( ct ) {
766                                         rc = config_add_vals( ct, c );
767                                 }
768                         }
769                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
770                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
771                                         c->argc, c->argv);
772                         }
773                         if ( rc ) {
774                                 switch(rc) {
775                                 case SLAP_CONF_UNKNOWN:
776                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
777                                                 "unknown directive <%s> inside backend database "
778                                                 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
779                                                 c->log, *c->argv, 0);
780 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
781                                         continue;
782 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
783                                 default:
784                                         rc = 1;
785                                         goto done;
786                                 }
787                         }
788
789                 } else if ( frontendDB->be_config ) {
790                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
791                         if ( rc ) {
792                                 switch(rc) {
793                                 case SLAP_CONF_UNKNOWN:
794                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
795                                                 "unknown directive <%s> inside global database definition"
796                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
797                                                 c->log, *c->argv, 0);
798 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
799                                         continue;
800 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
801                                 default:
802                                         rc = 1;
803                                         goto done;
804                                 }
805                         }
806                         
807                 } else {
808                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
809                                 "unknown directive <%s> outside backend info and database definitions"
810                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
811                                 c->log, *c->argv, 0);
812 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
813                         rc = 1;
814                         goto done;
815 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
816                         continue;
817 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
818                 }
819         }
820
821         rc = 0;
822
823 done:
824         ch_free(c->tline);
825         fclose(fp);
826         ch_free(c->argv);
827         ch_free(c);
828         return(rc);
829 }
830
831 /* restrictops, allows, disallows, requires, loglevel */
832
833 int
834 verb_to_mask(const char *word, slap_verbmasks *v) {
835         int i;
836         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
837                 if(!strcasecmp(word, v[i].word.bv_val))
838                         break;
839         return(i);
840 }
841
842 int
843 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
844         int i, j;
845         for(i = 1; i < argc; i++) {
846                 j = verb_to_mask(argv[i], v);
847                 if(BER_BVISNULL(&v[j].word)) return i;
848                 while (!v[j].mask) j--;
849                 *m |= v[j].mask;
850         }
851         return(0);
852 }
853
854 /* Mask keywords that represent multiple bits should occur before single
855  * bit keywords in the verbmasks array.
856  */
857 int
858 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
859         int i, rc = 1;
860
861         if (m) {
862                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
863                         if (!v[i].mask) continue;
864                         if (( m & v[i].mask ) == v[i].mask ) {
865                                 value_add_one( bva, &v[i].word );
866                                 rc = 0;
867                                 m ^= v[i].mask;
868                                 if ( !m ) break;
869                         }
870                 }
871         }
872         return rc;
873 }
874
875 int
876 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
877 {
878         int             i;
879
880         assert( *vp == NULL );
881
882         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ )
883                 ;
884
885         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
886
887         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
888                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
889                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
890         }
891
892         BER_BVZERO( &(*vp)[ i ].word );
893
894         return 0;               
895 }
896
897 int
898 slap_verbmasks_destroy( slap_verbmasks *v )
899 {
900         int             i;
901
902         assert( v != NULL );
903
904         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
905                 ch_free( v[ i ].word.bv_val );
906         }
907
908         ch_free( v );
909
910         return 0;
911 }
912
913 int
914 slap_verbmasks_append(
915         slap_verbmasks  **vp,
916         slap_mask_t     m,
917         struct berval   *v,
918         slap_mask_t     *ignore )
919 {
920         int     i;
921
922         if ( !m ) {
923                 return LDAP_OPERATIONS_ERROR;
924         }
925
926         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
927                 if ( !(*vp)[ i ].mask ) continue;
928
929                 if ( ignore != NULL ) {
930                         int     j;
931
932                         for ( j = 0; ignore[ j ] != 0; j++ ) {
933                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
934                                         goto check_next;
935                                 }
936                         }
937                 }
938
939                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
940                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
941                                 /* already set; ignore */
942                                 return LDAP_SUCCESS;
943                         }
944                         /* conflicts */
945                         return LDAP_TYPE_OR_VALUE_EXISTS;
946                 }
947
948                 if ( m & (*vp)[ i ].mask ) {
949                         /* conflicts */
950                         return LDAP_CONSTRAINT_VIOLATION;
951                 }
952 check_next:;
953         }
954
955         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
956         ber_dupbv( &(*vp)[ i ].word, v );
957         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
958         BER_BVZERO( &(*vp)[ i + 1 ].word );
959
960         return LDAP_SUCCESS;
961 }
962
963 int
964 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
965         int i;
966
967         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
968                 if ( m == v[i].mask ) {
969                         if ( bv != NULL ) {
970                                 *bv = v[i].word;
971                         }
972                         return i;
973                 }
974         }
975         return -1;
976 }
977
978 static slap_verbmasks tlskey[] = {
979         { BER_BVC("no"),        SB_TLS_OFF },
980         { BER_BVC("yes"),       SB_TLS_ON },
981         { BER_BVC("critical"),  SB_TLS_CRITICAL },
982         { BER_BVNULL, 0 }
983 };
984
985 static slap_verbmasks methkey[] = {
986         { BER_BVC("none"),      LDAP_AUTH_NONE },
987         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
988 #ifdef HAVE_CYRUS_SASL
989         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
990 #endif
991         { BER_BVNULL, 0 }
992 };
993
994 static slap_cf_aux_table bindkey[] = {
995         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
996         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
997         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
998         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
999         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1000         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1001         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1002         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1003         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1004         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1005         { BER_BVNULL, 0, 0, 0, NULL }
1006 };
1007
1008 int
1009 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1010 {
1011         int rc = 0;
1012         slap_cf_aux_table *tab;
1013
1014         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1015                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1016                         char **cptr, *next;
1017                         int *iptr, j;
1018                         unsigned *uptr;
1019                         struct berval *bptr;
1020                         const char *val = word + tab->key.bv_len;
1021
1022                         switch ( tab->type ) {
1023                         case 's':
1024                                 cptr = (char **)((char *)dst + tab->off);
1025                                 *cptr = ch_strdup( val );
1026                                 break;
1027
1028                         case 'b':
1029                                 bptr = (struct berval *)((char *)dst + tab->off);
1030                                 ber_str2bv( val, 0, 1, bptr );
1031                                 break;
1032
1033                         case 'd':
1034                                 assert( tab->aux != NULL );
1035                                 iptr = (int *)((char *)dst + tab->off);
1036
1037                                 rc = 1;
1038                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1039                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1040                                                 *iptr = tab->aux[j].mask;
1041                                                 rc = 0;
1042                                         }
1043                                 }
1044                                 break;
1045
1046                         case 'i':
1047                                 iptr = (int *)((char *)dst + tab->off);
1048
1049                                 *iptr = strtol( val, &next, 0 );
1050                                 if ( next == val || next[ 0 ] != '\0' ) {
1051                                         rc = 1;
1052                                 }
1053                                 break;
1054
1055                         case 'u':
1056                                 uptr = (unsigned *)((char *)dst + tab->off);
1057
1058                                 *uptr = strtoul( val, &next, 0 );
1059                                 if ( next == val || next[ 0 ] != '\0' ) {
1060                                         rc = 1;
1061                                 }
1062                                 break;
1063                         }
1064
1065                         if ( rc ) {
1066                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1067                                         tabmsg, word, 0 );
1068                         }
1069                         
1070                         return rc;
1071                 }
1072         }
1073
1074         return rc;
1075 }
1076
1077 int
1078 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1079 {
1080         char buf[BUFSIZ], *ptr;
1081         slap_cf_aux_table *tab;
1082         struct berval tmp;
1083
1084         ptr = buf;
1085         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1086                 char **cptr;
1087                 int *iptr, i;
1088                 unsigned *uptr;
1089                 struct berval *bptr;
1090
1091                 cptr = (char **)((char *)src + tab->off);
1092
1093                 switch ( tab->type ) {
1094                 case 'b':
1095                         bptr = (struct berval *)((char *)src + tab->off);
1096                         cptr = &bptr->bv_val;
1097                 case 's':
1098                         if ( *cptr ) {
1099                                 *ptr++ = ' ';
1100                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1101                                 if ( tab->quote ) *ptr++ = '"';
1102                                 ptr = lutil_strcopy( ptr, *cptr );
1103                                 if ( tab->quote ) *ptr++ = '"';
1104                         }
1105                         break;
1106
1107                 case 'd':
1108                         assert( tab->aux != NULL );
1109                         iptr = (int *)((char *)src + tab->off);
1110                 
1111                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1112                                 if ( *iptr == tab->aux[i].mask ) {
1113                                         *ptr++ = ' ';
1114                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1115                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1116                                         break;
1117                                 }
1118                         }
1119                         break;
1120
1121                 case 'i':
1122                         iptr = (int *)((char *)src + tab->off);
1123                         *ptr++ = ' ';
1124                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1125                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1126                         break;
1127
1128                 case 'u':
1129                         uptr = (unsigned *)((char *)src + tab->off);
1130                         *ptr++ = ' ';
1131                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1132                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1133                         break;
1134                 }
1135         }
1136         tmp.bv_val = buf;
1137         tmp.bv_len = ptr - buf;
1138         ber_dupbv( bv, &tmp );
1139         return 0;
1140 }
1141
1142 int
1143 bindconf_parse( const char *word, slap_bindconf *bc )
1144 {
1145         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1146 }
1147
1148 int
1149 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1150 {
1151         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1152 }
1153
1154 void bindconf_free( slap_bindconf *bc ) {
1155         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1156                 ch_free( bc->sb_uri.bv_val );
1157                 BER_BVZERO( &bc->sb_uri );
1158         }
1159         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1160                 ch_free( bc->sb_binddn.bv_val );
1161                 BER_BVZERO( &bc->sb_binddn );
1162         }
1163         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1164                 ch_free( bc->sb_cred.bv_val );
1165                 BER_BVZERO( &bc->sb_cred );
1166         }
1167         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1168                 ch_free( bc->sb_saslmech.bv_val );
1169                 BER_BVZERO( &bc->sb_saslmech );
1170         }
1171         if ( bc->sb_secprops ) {
1172                 ch_free( bc->sb_secprops );
1173                 bc->sb_secprops = NULL;
1174         }
1175         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1176                 ch_free( bc->sb_realm.bv_val );
1177                 BER_BVZERO( &bc->sb_realm );
1178         }
1179         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1180                 ch_free( bc->sb_authcId.bv_val );
1181                 BER_BVZERO( &bc->sb_authcId );
1182         }
1183         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1184                 ch_free( bc->sb_authzId.bv_val );
1185                 BER_BVZERO( &bc->sb_authzId );
1186         }
1187 }
1188
1189
1190 /* -------------------------------------- */
1191
1192
1193 static char *
1194 strtok_quote( char *line, char *sep, char **quote_ptr )
1195 {
1196         int             inquote;
1197         char            *tmp;
1198         static char     *next;
1199
1200         *quote_ptr = NULL;
1201         if ( line != NULL ) {
1202                 next = line;
1203         }
1204         while ( *next && strchr( sep, *next ) ) {
1205                 next++;
1206         }
1207
1208         if ( *next == '\0' ) {
1209                 next = NULL;
1210                 return( NULL );
1211         }
1212         tmp = next;
1213
1214         for ( inquote = 0; *next; ) {
1215                 switch ( *next ) {
1216                 case '"':
1217                         if ( inquote ) {
1218                                 inquote = 0;
1219                         } else {
1220                                 inquote = 1;
1221                         }
1222                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1223                         break;
1224
1225                 case '\\':
1226                         if ( next[1] )
1227                                 AC_MEMCPY( next,
1228                                             next + 1, strlen( next + 1 ) + 1 );
1229                         next++;         /* dont parse the escaped character */
1230                         break;
1231
1232                 default:
1233                         if ( ! inquote ) {
1234                                 if ( strchr( sep, *next ) != NULL ) {
1235                                         *quote_ptr = next;
1236                                         *next++ = '\0';
1237                                         return( tmp );
1238                                 }
1239                         }
1240                         next++;
1241                         break;
1242                 }
1243         }
1244
1245         return( tmp );
1246 }
1247
1248 static char     buf[BUFSIZ];
1249 static char     *line;
1250 static size_t lmax, lcur;
1251
1252 #define CATLINE( buf ) \
1253         do { \
1254                 size_t len = strlen( buf ); \
1255                 while ( lcur + len + 1 > lmax ) { \
1256                         lmax += BUFSIZ; \
1257                         line = (char *) ch_realloc( line, lmax ); \
1258                 } \
1259                 strcpy( line + lcur, buf ); \
1260                 lcur += len; \
1261         } while( 0 )
1262
1263 static void
1264 fp_getline_init(ConfigArgs *c) {
1265         c->lineno = -1;
1266         buf[0] = '\0';
1267 }
1268
1269 static int
1270 fp_getline( FILE *fp, ConfigArgs *c )
1271 {
1272         char    *p;
1273
1274         lcur = 0;
1275         CATLINE(buf);
1276         c->lineno++;
1277
1278         /* avoid stack of bufs */
1279         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1280                 buf[0] = '\0';
1281                 c->line = line;
1282                 return(1);
1283         }
1284
1285         while ( fgets( buf, sizeof( buf ), fp ) ) {
1286                 p = strchr( buf, '\n' );
1287                 if ( p ) {
1288                         if ( p > buf && p[-1] == '\r' ) {
1289                                 --p;
1290                         }
1291                         *p = '\0';
1292                 }
1293                 /* XXX ugly */
1294                 c->line = line;
1295                 if ( line[0]
1296                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1297                                 && p[-1] != '\\' )
1298                 {
1299                         p[0] = '\0';
1300                         lcur--;
1301                         
1302                 } else {
1303                         if ( !isspace( (unsigned char)buf[0] ) ) {
1304                                 return(1);
1305                         }
1306                         buf[0] = ' ';
1307                 }
1308                 CATLINE(buf);
1309                 c->lineno++;
1310         }
1311
1312         buf[0] = '\0';
1313         c->line = line;
1314         return(line[0] ? 1 : 0);
1315 }
1316
1317 static int
1318 fp_parse_line(ConfigArgs *c)
1319 {
1320         char *token;
1321         static char *const hide[] = {
1322                 "rootpw", "replica", "syncrepl",  /* in slapd */
1323                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1324                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1325                 "pseudorootpw",  /* in back-meta */
1326                 "dbpasswd",  /* in back-sql */
1327                 NULL
1328         };
1329         char *quote_ptr;
1330         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1331
1332         c->tline = ch_strdup(c->line);
1333         token = strtok_quote(c->tline, " \t", &quote_ptr);
1334
1335         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1336         if(quote_ptr) *quote_ptr = ' ';
1337         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1338                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1339         if(quote_ptr) *quote_ptr = '\0';
1340
1341         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1342                 if(c->argc >= c->argv_size) {
1343                         char **tmp;
1344                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1345                         if(!tmp) {
1346                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1347                                 return -1;
1348                         }
1349                         c->argv = tmp;
1350                         c->argv_size += ARGS_STEP;
1351                 }
1352                 if(token == NULL)
1353                         break;
1354                 c->argv[c->argc++] = token;
1355         }
1356         c->argv[c->argc] = NULL;
1357         return(0);
1358 }
1359
1360 void
1361 config_destroy( )
1362 {
1363         ucdata_unload( UCDATA_ALL );
1364         if ( frontendDB ) {
1365                 /* NOTE: in case of early exit, frontendDB can be NULL */
1366                 if ( frontendDB->be_schemandn.bv_val )
1367                         free( frontendDB->be_schemandn.bv_val );
1368                 if ( frontendDB->be_schemadn.bv_val )
1369                         free( frontendDB->be_schemadn.bv_val );
1370                 if ( frontendDB->be_acl )
1371                         acl_destroy( frontendDB->be_acl, NULL );
1372         }
1373         free( line );
1374         if ( slapd_args_file )
1375                 free ( slapd_args_file );
1376         if ( slapd_pid_file )
1377                 free ( slapd_pid_file );
1378         if ( default_passwd_hash )
1379                 ldap_charray_free( default_passwd_hash );
1380 }
1381
1382 char **
1383 slap_str2clist( char ***out, char *in, const char *brkstr )
1384 {
1385         char    *str;
1386         char    *s;
1387         char    *lasts;
1388         int     i, j;
1389         char    **new;
1390
1391         /* find last element in list */
1392         for (i = 0; *out && (*out)[i]; i++);
1393
1394         /* protect the input string from strtok */
1395         str = ch_strdup( in );
1396
1397         if ( *str == '\0' ) {
1398                 free( str );
1399                 return( *out );
1400         }
1401
1402         /* Count words in string */
1403         j=1;
1404         for ( s = str; *s; s++ ) {
1405                 if ( strchr( brkstr, *s ) != NULL ) {
1406                         j++;
1407                 }
1408         }
1409
1410         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1411         new = *out + i;
1412         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1413                 s != NULL;
1414                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1415         {
1416                 *new = ch_strdup( s );
1417                 new++;
1418         }
1419
1420         *new = NULL;
1421         free( str );
1422         return( *out );
1423 }
1424
1425 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1426         int argc, char **argv )
1427 {
1428         ConfigArgs c = { 0 };
1429         ConfigTable *ct;
1430         int rc;
1431
1432         c.be = be;
1433         c.fname = fname;
1434         c.lineno = lineno;
1435         c.argc = argc;
1436         c.argv = argv;
1437         c.valx = -1;
1438         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1439
1440         rc = SLAP_CONF_UNKNOWN;
1441         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1442         if ( ct )
1443                 rc = config_add_vals( ct, &c );
1444         return rc;
1445 }