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