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