]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
Make ConfigArgs.lineno int like elsewhere instead of unsigned long
[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                 if ( !ct[i].attribute ) continue;
421                 at = ldap_str2attributetype( ct[i].attribute,
422                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
423                 if ( !at ) {
424                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
425                                 ct[i].attribute, ldap_scherr2str(code), err );
426                         return code;
427                 }
428                 code = at_add( at, 0, NULL, &err );
429                 if ( code && code != SLAP_SCHERR_ATTR_DUP ) {
430                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
431                                 ct[i].attribute, scherr2str(code), err );
432                         return code;
433                 }
434                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
435                 if ( code ) {
436                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
437                                 ct[i].attribute, err );
438                         return code;
439                 }
440                 ldap_memfree( at );
441         }
442
443         return 0;
444 }
445
446 int
447 init_config_ocs( ConfigOCs *ocs ) {
448         int i;
449
450         for (i=0;ocs[i].co_def;i++) {
451                 LDAPObjectClass *oc;
452                 int code;
453                 const char *err;
454
455                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
456                         LDAP_SCHEMA_ALLOW_ALL );
457                 if ( !oc ) {
458                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
459                                 ocs[i].co_def, ldap_scherr2str(code), err );
460                         return code;
461                 }
462                 code = oc_add(oc,0,NULL,&err);
463                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
464                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
465                                 ocs[i].co_def, scherr2str(code), err );
466                         return code;
467                 }
468                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
469                 ldap_memfree(oc);
470         }
471         return 0;
472 }
473
474 int
475 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
476 {
477         int     rc = 0;
478         char    *saveline = NULL;
479
480         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
481                 ct->ad->ad_cname.bv_val, valx );
482         c->argc = 1;
483         c->argv[0] = ct->ad->ad_cname.bv_val;
484
485         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
486                 ber_len_t       len;
487
488                 saveline = c->line;
489                 len = strlen( c->line );
490                 c->line = ch_malloc( len + STRLENOF( "\"\"" ) + 1 );
491                 sprintf( c->line, "\"%s\"", saveline );
492         }
493
494         if ( fp_parse_line( c ) ) {
495                 rc = 1;
496         } else {
497                 rc = config_check_vals( ct, c, 1 );
498         }
499
500         if ( saveline ) {
501                 ch_free( c->line );
502                 c->line = saveline;
503         }
504
505         if ( rc )
506                 rc = LDAP_CONSTRAINT_VIOLATION;
507
508         ch_free( c->tline );
509         return rc;
510 }
511
512 int
513 config_parse_add(ConfigTable *ct, ConfigArgs *c)
514 {
515         int     rc = 0;
516         char    *saveline = NULL;
517
518         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
519                 ct->ad->ad_cname.bv_val, c->valx );
520         c->argc = 1;
521         c->argv[0] = ct->ad->ad_cname.bv_val;
522
523         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
524                 ber_len_t       len;
525
526                 saveline = c->line;
527                 len = strlen( c->line );
528                         
529                 c->line = ch_malloc( len + STRLENOF( "\"\"" ) + 1 );
530                 sprintf( c->line, "\"%s\"", saveline );
531         }
532
533         if ( fp_parse_line( c ) ) {
534                 rc = 1;
535         } else {
536                 c->op = LDAP_MOD_ADD;
537                 rc = config_add_vals( ct, c );
538         }
539
540         if ( saveline ) {
541                 ch_free( c->line );
542                 c->line = saveline;
543         }
544
545         ch_free( c->tline );
546         return rc;
547 }
548
549 int
550 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
551 {
552         FILE *fp;
553         ConfigTable *ct;
554         ConfigArgs *c;
555         int rc;
556         struct stat s;
557
558         c = ch_calloc( 1, sizeof( ConfigArgs ) );
559         if ( c == NULL ) {
560                 return 1;
561         }
562
563         if ( depth ) {
564                 memcpy( c, cf, sizeof( ConfigArgs ) );
565         } else {
566                 c->depth = depth; /* XXX */
567                 c->bi = NULL;
568                 c->be = NULL;
569         }
570
571         c->valx = -1;
572         c->fname = fname;
573         init_config_argv( c );
574
575         if ( stat( fname, &s ) != 0 ) {
576                 ldap_syslog = 1;
577                 Debug(LDAP_DEBUG_ANY,
578                     "could not stat config file \"%s\": %s (%d)\n",
579                     fname, strerror(errno), errno);
580                 return(1);
581         }
582
583         if ( !S_ISREG( s.st_mode ) ) {
584                 ldap_syslog = 1;
585                 Debug(LDAP_DEBUG_ANY,
586                     "regular file expected, got \"%s\"\n",
587                     fname, 0, 0 );
588                 return(1);
589         }
590
591         fp = fopen( fname, "r" );
592         if ( fp == NULL ) {
593                 ldap_syslog = 1;
594                 Debug(LDAP_DEBUG_ANY,
595                     "could not open config file \"%s\": %s (%d)\n",
596                     fname, strerror(errno), errno);
597                 return(1);
598         }
599
600         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
601
602         fp_getline_init(c);
603
604         c->tline = NULL;
605
606         while ( fp_getline( fp, c ) ) {
607                 /* skip comments and blank lines */
608                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
609                         continue;
610                 }
611
612                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
613                                 c->fname, c->lineno );
614
615                 c->argc = 0;
616                 ch_free( c->tline );
617                 if ( fp_parse_line( c ) ) {
618                         rc = 1;
619                         goto leave;
620                 }
621
622                 if ( c->argc < 1 ) {
623                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: bad config line" 
624                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
625                                 c->log, 0, 0);
626 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
627                         rc = 1;
628                         goto leave;
629 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
630                         continue;
631 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
632                 }
633
634                 c->op = SLAP_CONFIG_ADD;
635
636                 ct = config_find_keyword( cft, c );
637                 if ( ct ) {
638                         rc = config_add_vals( ct, c );
639                         if ( !rc ) continue;
640
641                         if ( rc & ARGS_USERLAND ) {
642                                 /* XXX a usertype would be opaque here */
643                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
644                                         c->log, c->argv[0], 0);
645                                 rc = 1;
646                                 goto leave;
647
648                         } else if ( rc == ARG_BAD_CONF ) {
649                                 rc = 1;
650                                 goto leave;
651                         }
652                         
653                 } else if ( c->bi && !c->be ) {
654                         rc = SLAP_CONF_UNKNOWN;
655                         if ( c->bi->bi_cf_ocs ) {
656                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
657                                 if ( ct ) {
658                                         rc = config_add_vals( ct, c );
659                                 }
660                         }
661                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
662                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
663                                         c->argc, c->argv);
664                         }
665                         if ( rc ) {
666                                 switch(rc) {
667                                 case SLAP_CONF_UNKNOWN:
668                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
669                                                 "unknown directive <%s> inside backend info definition"
670                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
671                                                 c->log, *c->argv, 0);
672 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
673                                         continue;
674 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
675                                 default:
676                                         rc = 1;
677                                         goto leave;
678                                 }
679                         }
680
681                 } else if ( c->be ) {
682                         rc = SLAP_CONF_UNKNOWN;
683                         if ( c->be->be_cf_ocs ) {
684                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
685                                 if ( ct ) {
686                                         rc = config_add_vals( ct, c );
687                                 }
688                         }
689                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
690                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
691                                         c->argc, c->argv);
692                         }
693                         if ( rc ) {
694                                 switch(rc) {
695                                 case SLAP_CONF_UNKNOWN:
696                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
697                                                 "unknown directive <%s> inside backend database "
698                                                 "definition" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
699                                                 c->log, *c->argv, 0);
700 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
701                                         continue;
702 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
703                                 default:
704                                         rc = 1;
705                                         goto leave;
706                                 }
707                         }
708
709                 } else if ( frontendDB->be_config ) {
710                         rc = (*frontendDB->be_config)(frontendDB, c->fname, (int)c->lineno, c->argc, c->argv);
711                         if ( rc ) {
712                                 switch(rc) {
713                                 case SLAP_CONF_UNKNOWN:
714                                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
715                                                 "unknown directive <%s> inside global database definition"
716                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
717                                                 c->log, *c->argv, 0);
718 #ifndef SLAPD_CONF_UNKNOWN_BAILOUT
719                                         continue;
720 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
721                                 default:
722                                         rc = 1;
723                                         goto leave;
724                                 }
725                         }
726                         
727                 } else {
728                         Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
729                                 "unknown directive <%s> outside backend info and database definitions"
730                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
731                                 c->log, *c->argv, 0);
732 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
733                         rc = 1;
734                         goto leave;
735 #else /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
736                         continue;
737 #endif /* ! SLAPD_CONF_UNKNOWN_BAILOUT */
738                 }
739         }
740
741         rc = 0;
742
743 leave:
744         ch_free(c->tline);
745         fclose(fp);
746         ch_free(c->argv);
747         ch_free(c);
748         return(rc);
749 }
750
751 /* restrictops, allows, disallows, requires, loglevel */
752
753 int
754 verb_to_mask(const char *word, slap_verbmasks *v) {
755         int i;
756         for(i = 0; !BER_BVISNULL(&v[i].word); i++)
757                 if(!strcasecmp(word, v[i].word.bv_val))
758                         break;
759         return(i);
760 }
761
762 int
763 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
764         int i, j;
765         for(i = 1; i < argc; i++) {
766                 j = verb_to_mask(argv[i], v);
767                 if(BER_BVISNULL(&v[j].word)) return(1);
768                 while (!v[j].mask) j--;
769                 *m |= v[j].mask;
770         }
771         return(0);
772 }
773
774 /* Mask keywords that represent multiple bits should occur before single
775  * bit keywords in the verbmasks array.
776  */
777 int
778 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
779         int i;
780
781         if (!m) return 1;
782         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
783                 if (!v[i].mask) continue;
784                 if (( m & v[i].mask ) == v[i].mask ) {
785                         value_add_one( bva, &v[i].word );
786                         m ^= v[i].mask;
787                         if ( !m ) break;
788                 }
789         }
790         return 0;
791 }
792
793 int
794 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
795         int i;
796
797         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
798                 if ( m == v[i].mask ) {
799                         if ( bv != NULL ) {
800                                 *bv = v[i].word;
801                         }
802                         return i;
803                 }
804         }
805         return -1;
806 }
807
808 static slap_verbmasks tlskey[] = {
809         { BER_BVC("no"),        SB_TLS_OFF },
810         { BER_BVC("yes"),       SB_TLS_ON },
811         { BER_BVC("critical"),  SB_TLS_CRITICAL },
812         { BER_BVNULL, 0 }
813 };
814
815 static slap_verbmasks methkey[] = {
816         { BER_BVC("none"),      LDAP_AUTH_NONE },
817         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
818 #ifdef HAVE_CYRUS_SASL
819         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
820 #endif
821         { BER_BVNULL, 0 }
822 };
823
824 typedef struct cf_aux_table {
825         struct berval key;
826         int off;
827         char type;
828         char quote;
829         slap_verbmasks *aux;
830 } cf_aux_table;
831
832 static cf_aux_table bindkey[] = {
833         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
834         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
835         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
836         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
837         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
838         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
839         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
840         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
841         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
842         { BER_BVNULL, 0, 0, 0, NULL }
843 };
844
845 int bindconf_parse( const char *word, slap_bindconf *bc ) {
846         int rc = 0;
847         cf_aux_table *tab;
848
849         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
850                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
851                         char **cptr;
852                         int *iptr, j;
853                         struct berval *bptr;
854                         const char *val = word + tab->key.bv_len;
855
856                         switch ( tab->type ) {
857                         case 's':
858                                 cptr = (char **)((char *)bc + tab->off);
859                                 *cptr = ch_strdup( val );
860                                 break;
861
862                         case 'b':
863                                 bptr = (struct berval *)((char *)bc + tab->off);
864                                 ber_str2bv( val, 0, 1, bptr );
865                                 break;
866
867                         case 'd':
868                                 assert( tab->aux );
869                                 iptr = (int *)((char *)bc + tab->off);
870
871                                 rc = 1;
872                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
873                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
874                                                 *iptr = tab->aux[j].mask;
875                                                 rc = 0;
876                                         }
877                                 }
878                                 break;
879                         }
880
881                         if ( rc ) {
882                                 Debug( LDAP_DEBUG_ANY, "invalid bind config value %s\n",
883                                         word, 0, 0 );
884                         }
885                         
886                         return rc;
887                 }
888         }
889
890         return rc;
891 }
892
893 int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
894         char buf[BUFSIZ], *ptr;
895         cf_aux_table *tab;
896         struct berval tmp;
897
898         ptr = buf;
899         for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
900                 char **cptr;
901                 int *iptr, i;
902                 struct berval *bptr;
903
904                 cptr = (char **)((char *)bc + tab->off);
905
906                 switch ( tab->type ) {
907                 case 'b':
908                         bptr = (struct berval *)((char *)bc + tab->off);
909                         cptr = &bptr->bv_val;
910                 case 's':
911                         if ( *cptr ) {
912                                 *ptr++ = ' ';
913                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
914                                 if ( tab->quote ) *ptr++ = '"';
915                                 ptr = lutil_strcopy( ptr, *cptr );
916                                 if ( tab->quote ) *ptr++ = '"';
917                         }
918                         break;
919
920                 case 'd':
921                         assert( tab->aux );
922                         iptr = (int *)((char *)bc + tab->off);
923                 
924                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
925                                 if ( *iptr == tab->aux[i].mask ) {
926                                         *ptr++ = ' ';
927                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
928                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
929                                         break;
930                                 }
931                         }
932                         break;
933                 }
934         }
935         tmp.bv_val = buf;
936         tmp.bv_len = ptr - buf;
937         ber_dupbv( bv, &tmp );
938         return 0;
939 }
940
941 void bindconf_free( slap_bindconf *bc ) {
942         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
943                 ch_free( bc->sb_binddn.bv_val );
944                 BER_BVZERO( &bc->sb_binddn );
945         }
946         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
947                 ch_free( bc->sb_cred.bv_val );
948                 BER_BVZERO( &bc->sb_cred );
949         }
950         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
951                 ch_free( bc->sb_saslmech.bv_val );
952                 BER_BVZERO( &bc->sb_saslmech );
953         }
954         if ( bc->sb_secprops ) {
955                 ch_free( bc->sb_secprops );
956                 bc->sb_secprops = NULL;
957         }
958         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
959                 ch_free( bc->sb_realm.bv_val );
960                 BER_BVZERO( &bc->sb_realm );
961         }
962         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
963                 ch_free( bc->sb_authcId.bv_val );
964                 BER_BVZERO( &bc->sb_authcId );
965         }
966         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
967                 ch_free( bc->sb_authzId.bv_val );
968                 BER_BVZERO( &bc->sb_authzId );
969         }
970 }
971
972
973 /* -------------------------------------- */
974
975
976 static char *
977 strtok_quote( char *line, char *sep, char **quote_ptr )
978 {
979         int             inquote;
980         char            *tmp;
981         static char     *next;
982
983         *quote_ptr = NULL;
984         if ( line != NULL ) {
985                 next = line;
986         }
987         while ( *next && strchr( sep, *next ) ) {
988                 next++;
989         }
990
991         if ( *next == '\0' ) {
992                 next = NULL;
993                 return( NULL );
994         }
995         tmp = next;
996
997         for ( inquote = 0; *next; ) {
998                 switch ( *next ) {
999                 case '"':
1000                         if ( inquote ) {
1001                                 inquote = 0;
1002                         } else {
1003                                 inquote = 1;
1004                         }
1005                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1006                         break;
1007
1008                 case '\\':
1009                         if ( next[1] )
1010                                 AC_MEMCPY( next,
1011                                             next + 1, strlen( next + 1 ) + 1 );
1012                         next++;         /* dont parse the escaped character */
1013                         break;
1014
1015                 default:
1016                         if ( ! inquote ) {
1017                                 if ( strchr( sep, *next ) != NULL ) {
1018                                         *quote_ptr = next;
1019                                         *next++ = '\0';
1020                                         return( tmp );
1021                                 }
1022                         }
1023                         next++;
1024                         break;
1025                 }
1026         }
1027
1028         return( tmp );
1029 }
1030
1031 static char     buf[BUFSIZ];
1032 static char     *line;
1033 static size_t lmax, lcur;
1034
1035 #define CATLINE( buf ) \
1036         do { \
1037                 size_t len = strlen( buf ); \
1038                 while ( lcur + len + 1 > lmax ) { \
1039                         lmax += BUFSIZ; \
1040                         line = (char *) ch_realloc( line, lmax ); \
1041                 } \
1042                 strcpy( line + lcur, buf ); \
1043                 lcur += len; \
1044         } while( 0 )
1045
1046 static void
1047 fp_getline_init(ConfigArgs *c) {
1048         c->lineno = -1;
1049         buf[0] = '\0';
1050 }
1051
1052 static int
1053 fp_getline( FILE *fp, ConfigArgs *c )
1054 {
1055         char    *p;
1056
1057         lcur = 0;
1058         CATLINE(buf);
1059         c->lineno++;
1060
1061         /* avoid stack of bufs */
1062         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1063                 buf[0] = '\0';
1064                 c->line = line;
1065                 return(1);
1066         }
1067
1068         while ( fgets( buf, sizeof( buf ), fp ) ) {
1069                 p = strchr( buf, '\n' );
1070                 if ( p ) {
1071                         if ( p > buf && p[-1] == '\r' ) {
1072                                 --p;
1073                         }
1074                         *p = '\0';
1075                 }
1076                 /* XXX ugly */
1077                 c->line = line;
1078                 if ( line[0]
1079                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1080                                 && p[-1] != '\\' )
1081                 {
1082                         p[0] = '\0';
1083                         lcur--;
1084                         
1085                 } else {
1086                         if ( !isspace( (unsigned char)buf[0] ) ) {
1087                                 return(1);
1088                         }
1089                         buf[0] = ' ';
1090                 }
1091                 CATLINE(buf);
1092                 c->lineno++;
1093         }
1094
1095         buf[0] = '\0';
1096         c->line = line;
1097         return(line[0] ? 1 : 0);
1098 }
1099
1100 static int
1101 fp_parse_line(ConfigArgs *c)
1102 {
1103         char *token;
1104         static char *const hide[] = {
1105                 "rootpw", "replica", "syncrepl",  /* in slapd */
1106                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1107                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1108                 "pseudorootpw",  /* in back-meta */
1109                 "dbpasswd",  /* in back-sql */
1110                 NULL
1111         };
1112         char *quote_ptr;
1113         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1114
1115         c->tline = ch_strdup(c->line);
1116         token = strtok_quote(c->tline, " \t", &quote_ptr);
1117
1118         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1119         if(quote_ptr) *quote_ptr = ' ';
1120         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1121                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1122         if(quote_ptr) *quote_ptr = '\0';
1123
1124         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1125                 if(c->argc >= c->argv_size) {
1126                         char **tmp;
1127                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1128                         if(!tmp) {
1129                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1130                                 return -1;
1131                         }
1132                         c->argv = tmp;
1133                         c->argv_size += ARGS_STEP;
1134                 }
1135                 if(token == NULL)
1136                         break;
1137                 c->argv[c->argc++] = token;
1138         }
1139         c->argv[c->argc] = NULL;
1140         return(0);
1141 }
1142
1143 void
1144 config_destroy( )
1145 {
1146         ucdata_unload( UCDATA_ALL );
1147         if ( frontendDB ) {
1148                 /* NOTE: in case of early exit, frontendDB can be NULL */
1149                 if ( frontendDB->be_schemandn.bv_val )
1150                         free( frontendDB->be_schemandn.bv_val );
1151                 if ( frontendDB->be_schemadn.bv_val )
1152                         free( frontendDB->be_schemadn.bv_val );
1153                 if ( frontendDB->be_acl )
1154                         acl_destroy( frontendDB->be_acl, NULL );
1155         }
1156         free( line );
1157         if ( slapd_args_file )
1158                 free ( slapd_args_file );
1159         if ( slapd_pid_file )
1160                 free ( slapd_pid_file );
1161         if ( default_passwd_hash )
1162                 ldap_charray_free( default_passwd_hash );
1163 }
1164
1165 char **
1166 slap_str2clist( char ***out, char *in, const char *brkstr )
1167 {
1168         char    *str;
1169         char    *s;
1170         char    *lasts;
1171         int     i, j;
1172         char    **new;
1173
1174         /* find last element in list */
1175         for (i = 0; *out && (*out)[i]; i++);
1176
1177         /* protect the input string from strtok */
1178         str = ch_strdup( in );
1179
1180         if ( *str == '\0' ) {
1181                 free( str );
1182                 return( *out );
1183         }
1184
1185         /* Count words in string */
1186         j=1;
1187         for ( s = str; *s; s++ ) {
1188                 if ( strchr( brkstr, *s ) != NULL ) {
1189                         j++;
1190                 }
1191         }
1192
1193         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1194         new = *out + i;
1195         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1196                 s != NULL;
1197                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1198         {
1199                 *new = ch_strdup( s );
1200                 new++;
1201         }
1202
1203         *new = NULL;
1204         free( str );
1205         return( *out );
1206 }
1207
1208 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1209         int argc, char **argv )
1210 {
1211         ConfigArgs c = { 0 };
1212         ConfigTable *ct;
1213         int rc;
1214
1215         c.be = be;
1216         c.fname = fname;
1217         c.lineno = lineno;
1218         c.argc = argc;
1219         c.argv = argv;
1220         c.valx = -1;
1221         sprintf( c.log, "%s: line %d", fname, lineno );
1222
1223         rc = SLAP_CONF_UNKNOWN;
1224         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1225         if ( ct )
1226                 rc = config_add_vals( ct, &c );
1227         return rc;
1228 }