]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
more cleanup; add the frontend to the set of monitored databases; handle exceptional...
[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-2006 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 "lutil_ldap.h"
47 #include "config.h"
48
49 #ifdef HAVE_TLS
50 #include <openssl/ssl.h>
51 #endif
52
53 #define ARGS_STEP       512
54
55 /*
56  * defaults for various global variables
57  */
58 slap_mask_t             global_allows = 0;
59 slap_mask_t             global_disallows = 0;
60 int             global_gentlehup = 0;
61 int             global_idletimeout = 0;
62 char    *global_host = NULL;
63 char    *global_realm = NULL;
64 char            *ldap_srvtab = "";
65 char            **default_passwd_hash = NULL;
66 struct berval default_search_base = BER_BVNULL;
67 struct berval default_search_nbase = BER_BVNULL;
68
69 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
70 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
71
72 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
73 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
74
75 char   *slapd_pid_file  = NULL;
76 char   *slapd_args_file = NULL;
77
78 int use_reverse_lookup = 0;
79
80 #ifdef LDAP_SLAPI
81 int slapi_plugins_used = 0;
82 #endif
83
84 static int fp_getline(FILE *fp, ConfigArgs *c);
85 static void fp_getline_init(ConfigArgs *c);
86 static int fp_parse_line(ConfigArgs *c);
87
88 static char     *strtok_quote(char *line, char *sep, char **quote_ptr);
89 static char *strtok_quote_ldif(char **line);
90
91 ConfigArgs *
92 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv )
93 {
94         ConfigArgs *c;
95         c = ch_calloc( 1, sizeof( ConfigArgs ) );
96         if ( c == NULL ) return(NULL);
97         c->be     = be; 
98         c->fname  = fname;
99         c->argc   = argc;
100         c->argv   = argv; 
101         c->lineno = lineno;
102         snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno );
103         return(c);
104 }
105
106 void
107 init_config_argv( ConfigArgs *c )
108 {
109         c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) );
110         c->argv_size = ARGS_STEP + 1;
111 }
112
113 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) {
114         int i;
115
116         for(i = 0; Conf[i].name; i++)
117                 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) ||
118                         (!strcasecmp(c->argv[0], Conf[i].name)) ) break;
119         if ( !Conf[i].name ) return NULL;
120         return Conf+i;
121 }
122
123 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) {
124         int rc, arg_user, arg_type, arg_syn, iarg;
125         long larg;
126         ber_len_t barg;
127         
128         if(Conf->arg_type == ARG_IGNORED) {
129                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
130                         c->log, Conf->name, 0);
131                 return(0);
132         }
133         arg_type = Conf->arg_type & ARGS_TYPES;
134         arg_user = Conf->arg_type & ARGS_USERLAND;
135         arg_syn = Conf->arg_type & ARGS_SYNTAX;
136
137         if((arg_type == ARG_DN) && c->argc == 1) {
138                 c->argc = 2;
139                 c->argv[1] = "";
140         }
141         if(Conf->min_args && (c->argc < Conf->min_args)) {
142                 snprintf( c->msg, sizeof( c->msg ), "<%s> missing <%s> argument",
143                         c->argv[0], Conf->what );
144                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n", c->log, c->msg, 0 );
145                 return(ARG_BAD_CONF);
146         }
147         if(Conf->max_args && (c->argc > Conf->max_args)) {
148                 char    *ignored = " ignored";
149
150                 snprintf( c->msg, sizeof( c->msg ), "<%s> extra cruft after <%s>",
151                         c->argv[0], Conf->what );
152
153                 ignored = "";
154                 Debug(LDAP_DEBUG_CONFIG, "%s: %s%s.\n",
155                                 c->log, c->msg, ignored );
156                 return(ARG_BAD_CONF);
157         }
158         if((arg_syn & ARG_DB) && !c->be) {
159                 snprintf( c->msg, sizeof( c->msg ), "<%s> only allowed within database declaration",
160                         c->argv[0] );
161                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
162                         c->log, c->msg, 0);
163                 return(ARG_BAD_CONF);
164         }
165         if((arg_syn & ARG_PRE_BI) && c->bi) {
166                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any backend %sdeclaration",
167                         c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" );
168                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
169                         c->log, c->msg, 0 );
170                 return(ARG_BAD_CONF);
171         }
172         if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) {
173                 snprintf( c->msg, sizeof( c->msg ), "<%s> must occur before any database declaration",
174                         c->argv[0] );
175                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword %s\n",
176                         c->log, c->msg, 0);
177                 return(ARG_BAD_CONF);
178         }
179         if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) {
180                 snprintf( c->msg, sizeof( c->msg ), "<%s> old format not supported", c->argv[0] );
181                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
182                         c->log, c->msg, 0);
183                 return(ARG_BAD_CONF);
184         }
185         if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) {
186                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid config_table, arg_item is NULL",
187                         c->argv[0] );
188                 Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
189                         c->log, c->msg, 0);
190                 return(ARG_BAD_CONF);
191         }
192         c->type = arg_user;
193         memset(&c->values, 0, sizeof(c->values));
194         if(arg_type == ARG_STRING) {
195                 if ( !check_only )
196                         c->value_string = ch_strdup(c->argv[1]);
197         } else if(arg_type == ARG_BERVAL) {
198                 if ( !check_only )
199                         ber_str2bv( c->argv[1], 0, 1, &c->value_bv );
200         } else if(arg_type == ARG_DN) {
201                 struct berval bv;
202                 ber_str2bv( c->argv[1], 0, 0, &bv );
203                 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL );
204                 if ( rc != LDAP_SUCCESS ) {
205                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid DN %d (%s)",
206                                 c->argv[0], rc, ldap_err2string( rc ));
207                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n" , c->log, c->msg, 0);
208                         return(ARG_BAD_CONF);
209                 }
210                 if ( check_only ) {
211                         ch_free( c->value_ndn.bv_val );
212                         ch_free( c->value_dn.bv_val );
213                 }
214         } else {        /* all numeric */
215                 int j;
216                 iarg = 0; larg = 0; barg = 0;
217                 switch(arg_type) {
218                         case ARG_INT:
219                                 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) {
220                                         snprintf( c->msg, sizeof( c->msg ),
221                                                 "<%s> unable to parse \"%s\" as int",
222                                                 c->argv[0], c->argv[1] );
223                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
224                                                 c->log, c->msg, 0);
225                                         return(ARG_BAD_CONF);
226                                 }
227                                 break;
228                         case ARG_LONG:
229                                 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) {
230                                         snprintf( c->msg, sizeof( c->msg ),
231                                                 "<%s> unable to parse \"%s\" as long",
232                                                 c->argv[0], c->argv[1] );
233                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
234                                                 c->log, c->msg, 0);
235                                         return(ARG_BAD_CONF);
236                                 }
237                                 break;
238                         case ARG_BER_LEN_T: {
239                                 unsigned long   l;
240                                 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) {
241                                         snprintf( c->msg, sizeof( c->msg ),
242                                                 "<%s> unable to parse \"%s\" as ber_len_t",
243                                                 c->argv[0], c->argv[1] );
244                                         Debug(LDAP_DEBUG_CONFIG, "%s: %s\n",
245                                                 c->log, c->msg, 0);
246                                         return(ARG_BAD_CONF);
247                                 }
248                                 barg = (ber_len_t)l;
249                                 } break;
250                         case ARG_ON_OFF:
251                                 if (c->argc == 1) {
252                                         iarg = 1;
253                                 } else if ( !strcasecmp(c->argv[1], "on") ||
254                                         !strcasecmp(c->argv[1], "true") ||
255                                         !strcasecmp(c->argv[1], "yes") )
256                                 {
257                                         iarg = 1;
258                                 } else if ( !strcasecmp(c->argv[1], "off") ||
259                                         !strcasecmp(c->argv[1], "false") ||
260                                         !strcasecmp(c->argv[1], "no") )
261                                 {
262                                         iarg = 0;
263                                 } else {
264                                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
265                                                 c->argv[0] );
266                                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
267                                                 c->log, c->msg, 0 );
268                                         return(ARG_BAD_CONF);
269                                 }
270                                 break;
271                 }
272                 j = (arg_type & ARG_NONZERO) ? 1 : 0;
273                 if(iarg < j && larg < j && barg < j ) {
274                         larg = larg ? larg : (barg ? barg : iarg);
275                         snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value",
276                                 c->argv[0] );
277                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
278                                 c->log, c->msg, 0 );
279                         return(ARG_BAD_CONF);
280                 }
281                 switch(arg_type) {
282                         case ARG_ON_OFF:
283                         case ARG_INT:           c->value_int = iarg;            break;
284                         case ARG_LONG:          c->value_long = larg;           break;
285                         case ARG_BER_LEN_T:     c->value_ber_t = barg;          break;
286                 }
287         }
288         return 0;
289 }
290
291 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) {
292         int rc, arg_type;
293         void *ptr = NULL;
294
295         arg_type = Conf->arg_type;
296         if(arg_type & ARG_MAGIC) {
297                 if(!c->be) c->be = frontendDB;
298                 c->msg[0] = '\0';
299                 rc = (*((ConfigDriver*)Conf->arg_item))(c);
300 #if 0
301                 if(c->be == frontendDB) c->be = NULL;
302 #endif
303                 if(rc) {
304                         if ( !c->msg[0] ) {
305                                 snprintf( c->msg, sizeof( c->msg ), "<%s> handler exited with %d",
306                                         c->argv[0], rc );
307                                 Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
308                                         c->log, c->msg, 0 );
309                         }
310                         return(ARG_BAD_CONF);
311                 }
312                 return(0);
313         }
314         if(arg_type & ARG_OFFSET) {
315                 if (c->be && (!overlay_is_over(c->be) || 
316                         ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
317                         ptr = c->be->be_private;
318                 else if (c->bi)
319                         ptr = c->bi->bi_private;
320                 else {
321                         snprintf( c->msg, sizeof( c->msg ), "<%s> offset is missing base pointer",
322                                 c->argv[0] );
323                         Debug(LDAP_DEBUG_CONFIG, "%s: %s!\n",
324                                 c->log, c->msg, 0);
325                         return(ARG_BAD_CONF);
326                 }
327                 ptr = (void *)((char *)ptr + (long)Conf->arg_item);
328         } else if (arg_type & ARGS_TYPES) {
329                 ptr = Conf->arg_item;
330         }
331         if(arg_type & ARGS_TYPES)
332                 switch(arg_type & ARGS_TYPES) {
333                         case ARG_ON_OFF:
334                         case ARG_INT:           *(int*)ptr = c->value_int;                      break;
335                         case ARG_LONG:          *(long*)ptr = c->value_long;                    break;
336                         case ARG_BER_LEN_T:     *(ber_len_t*)ptr = c->value_ber_t;                      break;
337                         case ARG_STRING: {
338                                 char *cc = *(char**)ptr;
339                                 if(cc) {
340                                         if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) {
341                                                 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n",
342                                                         c->log, Conf->name, 0 );
343                                                 return(ARG_BAD_CONF);
344                                         }
345                                         ch_free(cc);
346                                 }
347                                 *(char **)ptr = c->value_string;
348                                 break;
349                                 }
350                         case ARG_BERVAL:
351                                 *(struct berval *)ptr = c->value_bv;
352                                 break;
353                 }
354         return(0);
355 }
356
357 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) {
358         int rc, arg_type;
359
360         arg_type = Conf->arg_type;
361         if(arg_type == ARG_IGNORED) {
362                 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n",
363                         c->log, Conf->name, 0);
364                 return(0);
365         }
366         rc = config_check_vals( Conf, c, 0 );
367         if ( rc ) return rc;
368         return config_set_vals( Conf, c );
369 }
370
371 int
372 config_del_vals(ConfigTable *cf, ConfigArgs *c)
373 {
374         int rc = 0;
375
376         /* If there is no handler, just ignore it */
377         if ( cf->arg_type & ARG_MAGIC ) {
378                 c->op = LDAP_MOD_DELETE;
379                 c->type = cf->arg_type & ARGS_USERLAND;
380                 rc = (*((ConfigDriver*)cf->arg_item))(c);
381         }
382         return rc;
383 }
384
385 int
386 config_get_vals(ConfigTable *cf, ConfigArgs *c)
387 {
388         int rc = 0;
389         struct berval bv;
390         void *ptr;
391
392         if ( cf->arg_type & ARG_IGNORED ) {
393                 return 1;
394         }
395
396         memset(&c->values, 0, sizeof(c->values));
397         c->rvalue_vals = NULL;
398         c->rvalue_nvals = NULL;
399         c->op = SLAP_CONFIG_EMIT;
400         c->type = cf->arg_type & ARGS_USERLAND;
401
402         if ( cf->arg_type & ARG_MAGIC ) {
403                 rc = (*((ConfigDriver*)cf->arg_item))(c);
404                 if ( rc ) return rc;
405         } else {
406                 if ( cf->arg_type & ARG_OFFSET ) {
407                         if (c->be && (!overlay_is_over(c->be) || 
408                                 ((slap_overinfo *)c->be->bd_info)->oi_orig == c->bi))
409                                 ptr = c->be->be_private;
410                         else if ( c->bi )
411                                 ptr = c->bi->bi_private;
412                         else
413                                 return 1;
414                         ptr = (void *)((char *)ptr + (long)cf->arg_item);
415                 } else {
416                         ptr = cf->arg_item;
417                 }
418                 
419                 switch(cf->arg_type & ARGS_TYPES) {
420                 case ARG_ON_OFF:
421                 case ARG_INT:   c->value_int = *(int *)ptr; break;
422                 case ARG_LONG:  c->value_long = *(long *)ptr; break;
423                 case ARG_BER_LEN_T:     c->value_ber_t = *(ber_len_t *)ptr; break;
424                 case ARG_STRING:
425                         if ( *(char **)ptr )
426                                 c->value_string = ch_strdup(*(char **)ptr);
427                         break;
428                 case ARG_BERVAL:
429                         ber_dupbv( &c->value_bv, (struct berval *)ptr ); break;
430                 }
431         }
432         if ( cf->arg_type & ARGS_TYPES) {
433                 bv.bv_len = 0;
434                 bv.bv_val = c->log;
435                 switch(cf->arg_type & ARGS_TYPES) {
436                 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break;
437                 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break;
438                 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break;
439                 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s",
440                         c->value_int ? "TRUE" : "FALSE"); break;
441                 case ARG_STRING:
442                         if ( c->value_string && c->value_string[0]) {
443                                 ber_str2bv( c->value_string, 0, 0, &bv);
444                         } else {
445                                 return 1;
446                         }
447                         break;
448                 case ARG_BERVAL:
449                         if ( !BER_BVISEMPTY( &c->value_bv )) {
450                                 bv = c->value_bv;
451                         } else {
452                                 return 1;
453                         }
454                         break;
455                 default:
456                         bv.bv_val = NULL;
457                         break;
458                 }
459                 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) {
460                         return 1;
461                 }
462                 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) {
463                         ber_bvarray_add(&c->rvalue_vals, &bv);
464                 } else if ( !BER_BVISNULL( &bv ) ) {
465                         value_add_one(&c->rvalue_vals, &bv);
466                 }
467                 /* else: maybe c->rvalue_vals already set? */
468         }
469         return rc;
470 }
471
472 int
473 init_config_attrs(ConfigTable *ct) {
474         LDAPAttributeType *at;
475         int i, code;
476         const char *err;
477
478         for (i=0; ct[i].name; i++ ) {
479                 int             freeit = 0;
480
481                 if ( !ct[i].attribute ) continue;
482                 at = ldap_str2attributetype( ct[i].attribute,
483                         &code, &err, LDAP_SCHEMA_ALLOW_ALL );
484                 if ( !at ) {
485                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
486                                 ct[i].attribute, ldap_scherr2str(code), err );
487                         return code;
488                 }
489
490                 code = at_add( at, 0, NULL, &err );
491                 if ( code ) {
492                         if ( code == SLAP_SCHERR_ATTR_DUP ) {
493                                 freeit = 1;
494
495                         } else {
496                                 ldap_attributetype_free( at );
497                                 fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
498                                         ct[i].attribute, scherr2str(code), err );
499                                 return code;
500                         }
501                 }
502                 code = slap_str2ad( at->at_names[0], &ct[i].ad, &err );
503                 if ( freeit || code ) {
504                         ldap_attributetype_free( at );
505                 } else {
506                         ldap_memfree( at );
507                 }
508                 if ( code ) {
509                         fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n",
510                                 ct[i].attribute, err );
511                         return code;
512                 } else {
513 #ifndef LDAP_DEVEL
514                         ct[i].ad->ad_type->sat_flags |= SLAP_AT_HIDE;
515 #endif
516                 }
517         }
518
519         return 0;
520 }
521
522 int
523 init_config_ocs( ConfigOCs *ocs ) {
524         int i;
525
526         for (i=0;ocs[i].co_def;i++) {
527                 LDAPObjectClass *oc;
528                 int code;
529                 const char *err;
530
531                 oc = ldap_str2objectclass( ocs[i].co_def, &code, &err,
532                         LDAP_SCHEMA_ALLOW_ALL );
533                 if ( !oc ) {
534                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
535                                 ocs[i].co_def, ldap_scherr2str(code), err );
536                         return code;
537                 }
538                 code = oc_add(oc,0,NULL,&err);
539                 if ( code && code != SLAP_SCHERR_CLASS_DUP ) {
540                         fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n",
541                                 ocs[i].co_def, scherr2str(code), err );
542                         ldap_objectclass_free(oc);
543                         return code;
544                 }
545                 ocs[i].co_oc = oc_find(oc->oc_names[0]);
546                 if ( code ) {
547                         ldap_objectclass_free(oc);
548                 } else {
549                         ldap_memfree(oc);
550 #ifndef LDAP_DEVEL
551                         ocs[i].co_oc->soc_flags |= SLAP_OC_HIDE;
552 #endif
553                 }
554         }
555         return 0;
556 }
557
558 /* Split an LDIF line into space-separated tokens. Words may be grouped
559  * by quotes. A quoted string may begin in the middle of a word, but must
560  * end at the end of the word (be followed by whitespace or EOS). Any other
561  * quotes are passed through unchanged. All other characters are passed
562  * through unchanged.
563  */
564 static char *
565 strtok_quote_ldif( char **line )
566 {
567         char *beg, *ptr, *quote=NULL;
568         int inquote=0;
569
570         ptr = *line;
571
572         if ( !ptr || !*ptr )
573                 return NULL;
574
575         while( isspace( (unsigned char) *ptr )) ptr++;
576
577         if ( *ptr == '"' ) {
578                 inquote = 1;
579                 ptr++;
580         }
581
582         beg = ptr;
583
584         for (;*ptr;ptr++) {
585                 if ( *ptr == '"' ) {
586                         if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
587                                 *ptr++ = '\0';
588                                 break;
589                         }
590                         inquote = 1;
591                         quote = ptr;
592                         continue;
593                 }
594                 if ( inquote )
595                         continue;
596                 if ( isspace( (unsigned char) *ptr )) {
597                         *ptr++ = '\0';
598                         break;
599                 }
600         }
601         if ( quote ) {
602                 while ( quote < ptr ) {
603                         *quote = quote[1];
604                         quote++;
605                 }
606         }
607         if ( !*ptr ) {
608                 *line = NULL;
609         } else {
610                 while ( isspace( (unsigned char) *ptr )) ptr++;
611                 *line = ptr;
612         }
613         return beg;
614 }
615
616 static void
617 config_parse_ldif( ConfigArgs *c )
618 {
619         char *next;
620         c->tline = ch_strdup(c->line);
621         next = c->tline;
622
623         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
624                 c->argc++;
625                 if ( c->argc >= c->argv_size ) {
626                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
627                                 sizeof( *c->argv ));
628                         c->argv = tmp;
629                         c->argv_size += ARGS_STEP;
630                 }
631         }
632         c->argv[c->argc] = NULL;
633 }
634
635 int
636 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
637 {
638         int     rc = 0;
639
640         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
641                 ct->ad->ad_cname.bv_val, valx );
642         c->argc = 1;
643         c->argv[0] = ct->ad->ad_cname.bv_val;
644
645         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
646                 c->argv[c->argc] = c->line;
647                 c->argc++;
648                 c->argv[c->argc] = NULL;
649                 c->tline = NULL;
650         } else {
651                 config_parse_ldif( c );
652         }
653         rc = config_check_vals( ct, c, 1 );
654         ch_free( c->tline );
655         c->tline = NULL;
656
657         if ( rc )
658                 rc = LDAP_CONSTRAINT_VIOLATION;
659
660         return rc;
661 }
662
663 int
664 config_parse_add(ConfigTable *ct, ConfigArgs *c)
665 {
666         int     rc = 0;
667
668         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
669                 ct->ad->ad_cname.bv_val, c->valx );
670         c->argc = 1;
671         c->argv[0] = ct->ad->ad_cname.bv_val;
672
673         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
674                 c->argv[c->argc] = c->line;
675                 c->argc++;
676                 c->argv[c->argc] = NULL;
677                 c->tline = NULL;
678         } else {
679                 config_parse_ldif( c );
680         }
681         c->op = LDAP_MOD_ADD;
682         rc = config_add_vals( ct, c );
683         ch_free( c->tline );
684
685         return rc;
686 }
687
688 int
689 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
690 {
691         FILE *fp;
692         ConfigTable *ct;
693         ConfigArgs *c;
694         int rc;
695         struct stat s;
696
697         c = ch_calloc( 1, sizeof( ConfigArgs ) );
698         if ( c == NULL ) {
699                 return 1;
700         }
701
702         if ( depth ) {
703                 memcpy( c, cf, sizeof( ConfigArgs ) );
704         } else {
705                 c->depth = depth; /* XXX */
706                 c->bi = NULL;
707                 c->be = NULL;
708         }
709
710         c->valx = -1;
711         c->fname = fname;
712         init_config_argv( c );
713
714         if ( stat( fname, &s ) != 0 ) {
715                 ldap_syslog = 1;
716                 Debug(LDAP_DEBUG_ANY,
717                     "could not stat config file \"%s\": %s (%d)\n",
718                     fname, strerror(errno), errno);
719                 ch_free( c );
720                 return(1);
721         }
722
723         if ( !S_ISREG( s.st_mode ) ) {
724                 ldap_syslog = 1;
725                 Debug(LDAP_DEBUG_ANY,
726                     "regular file expected, got \"%s\"\n",
727                     fname, 0, 0 );
728                 ch_free( c );
729                 return(1);
730         }
731
732         fp = fopen( fname, "r" );
733         if ( fp == NULL ) {
734                 ldap_syslog = 1;
735                 Debug(LDAP_DEBUG_ANY,
736                     "could not open config file \"%s\": %s (%d)\n",
737                     fname, strerror(errno), errno);
738                 ch_free( c );
739                 return(1);
740         }
741
742         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
743
744         fp_getline_init(c);
745
746         c->tline = NULL;
747
748         while ( fp_getline( fp, c ) ) {
749                 /* skip comments and blank lines */
750                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
751                         continue;
752                 }
753
754                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
755                                 c->fname, c->lineno );
756
757                 c->argc = 0;
758                 ch_free( c->tline );
759                 if ( fp_parse_line( c ) ) {
760                         rc = 1;
761                         goto done;
762                 }
763
764                 if ( c->argc < 1 ) {
765                         Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
766                                 c->log, 0, 0);
767                         rc = 1;
768                         goto done;
769                 }
770
771                 c->op = SLAP_CONFIG_ADD;
772
773                 ct = config_find_keyword( cft, c );
774                 if ( ct ) {
775                         rc = config_add_vals( ct, c );
776                         if ( !rc ) continue;
777
778                         if ( rc & ARGS_USERLAND ) {
779                                 /* XXX a usertype would be opaque here */
780                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
781                                         c->log, c->argv[0], 0);
782                                 rc = 1;
783                                 goto done;
784
785                         } else if ( rc == ARG_BAD_CONF ) {
786                                 rc = 1;
787                                 goto done;
788                         }
789                         
790                 } else if ( c->bi && !c->be ) {
791                         rc = SLAP_CONF_UNKNOWN;
792                         if ( c->bi->bi_cf_ocs ) {
793                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
794                                 if ( ct ) {
795                                         rc = config_add_vals( ct, c );
796                                 }
797                         }
798                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
799                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
800                                         c->argc, c->argv);
801                         }
802                         if ( rc ) {
803                                 switch(rc) {
804                                 case SLAP_CONF_UNKNOWN:
805                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
806                                                 "<%s> inside backend info definition.\n",
807                                                 c->log, *c->argv, 0);
808                                 default:
809                                         rc = 1;
810                                         goto done;
811                                 }
812                         }
813
814                 } else if ( c->be && c->be != frontendDB ) {
815                         rc = SLAP_CONF_UNKNOWN;
816                         if ( c->be->be_cf_ocs ) {
817                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
818                                 if ( ct ) {
819                                         rc = config_add_vals( ct, c );
820                                 }
821                         }
822                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
823                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
824                                         c->argc, c->argv);
825                         }
826                         if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
827                         {
828                                 /* global overlays may need 
829                                  * definitions inside other databases...
830                                  */
831                                 rc = (*frontendDB->be_config)( frontendDB,
832                                         c->fname, (int)c->lineno, c->argc, c->argv );
833                         }
834
835                         switch ( rc ) {
836                         case 0:
837                                 break;
838
839                         case SLAP_CONF_UNKNOWN:
840                                 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
841                                         "<%s> inside backend database definition.\n",
842                                         c->log, *c->argv, 0);
843                                 
844                         default:
845                                 rc = 1;
846                                 goto done;
847                         }
848
849                 } else if ( frontendDB->be_config ) {
850                         rc = (*frontendDB->be_config)( frontendDB,
851                                 c->fname, (int)c->lineno, c->argc, c->argv);
852                         if ( rc ) {
853                                 switch(rc) {
854                                 case SLAP_CONF_UNKNOWN:
855                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
856                                                 "<%s> inside global database definition.\n",
857                                                 c->log, *c->argv, 0);
858
859                                 default:
860                                         rc = 1;
861                                         goto done;
862                                 }
863                         }
864                         
865                 } else {
866                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
867                                 "<%s> outside backend info and database definitions.\n",
868                                 c->log, *c->argv, 0);
869                         rc = 1;
870                         goto done;
871                 }
872         }
873
874         rc = 0;
875
876 done:
877         ch_free(c->tline);
878         fclose(fp);
879         ch_free(c->argv);
880         ch_free(c);
881         return(rc);
882 }
883
884 /* restrictops, allows, disallows, requires, loglevel */
885
886 int
887 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
888         int i;
889         for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
890                 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
891         }
892         return(i);
893 }
894
895 int
896 verb_to_mask(const char *word, slap_verbmasks *v) {
897         struct berval   bword;
898         ber_str2bv( word, 0, 0, &bword );
899         return bverb_to_mask( &bword, v );
900 }
901
902 int
903 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
904         int i, j;
905         for(i = 1; i < argc; i++) {
906                 j = verb_to_mask(argv[i], v);
907                 if(BER_BVISNULL(&v[j].word)) return i;
908                 while (!v[j].mask) j--;
909                 *m |= v[j].mask;
910         }
911         return(0);
912 }
913
914 /* Mask keywords that represent multiple bits should occur before single
915  * bit keywords in the verbmasks array.
916  */
917 int
918 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
919         int i, rc = 1;
920
921         if (m) {
922                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
923                         if (!v[i].mask) continue;
924                         if (( m & v[i].mask ) == v[i].mask ) {
925                                 value_add_one( bva, &v[i].word );
926                                 rc = 0;
927                                 m ^= v[i].mask;
928                                 if ( !m ) break;
929                         }
930                 }
931         }
932         return rc;
933 }
934
935 int
936 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
937 {
938         int             i;
939
940         assert( *vp == NULL );
941
942         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
943
944         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
945
946         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
947                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
948                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
949         }
950
951         BER_BVZERO( &(*vp)[ i ].word );
952
953         return 0;               
954 }
955
956 int
957 slap_verbmasks_destroy( slap_verbmasks *v )
958 {
959         int             i;
960
961         assert( v != NULL );
962
963         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
964                 ch_free( v[ i ].word.bv_val );
965         }
966
967         ch_free( v );
968
969         return 0;
970 }
971
972 int
973 slap_verbmasks_append(
974         slap_verbmasks  **vp,
975         slap_mask_t     m,
976         struct berval   *v,
977         slap_mask_t     *ignore )
978 {
979         int     i;
980
981         if ( !m ) {
982                 return LDAP_OPERATIONS_ERROR;
983         }
984
985         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
986                 if ( !(*vp)[ i ].mask ) continue;
987
988                 if ( ignore != NULL ) {
989                         int     j;
990
991                         for ( j = 0; ignore[ j ] != 0; j++ ) {
992                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
993                                         goto check_next;
994                                 }
995                         }
996                 }
997
998                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
999                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
1000                                 /* already set; ignore */
1001                                 return LDAP_SUCCESS;
1002                         }
1003                         /* conflicts */
1004                         return LDAP_TYPE_OR_VALUE_EXISTS;
1005                 }
1006
1007                 if ( m & (*vp)[ i ].mask ) {
1008                         /* conflicts */
1009                         return LDAP_CONSTRAINT_VIOLATION;
1010                 }
1011 check_next:;
1012         }
1013
1014         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
1015         ber_dupbv( &(*vp)[ i ].word, v );
1016         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
1017         BER_BVZERO( &(*vp)[ i + 1 ].word );
1018
1019         return LDAP_SUCCESS;
1020 }
1021
1022 int
1023 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
1024         int i;
1025
1026         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
1027                 if ( m == v[i].mask ) {
1028                         if ( bv != NULL ) {
1029                                 *bv = v[i].word;
1030                         }
1031                         return i;
1032                 }
1033         }
1034         return -1;
1035 }
1036
1037 #ifdef HAVE_TLS
1038 static slap_verbmasks tlskey[] = {
1039         { BER_BVC("no"),        SB_TLS_OFF },
1040         { BER_BVC("yes"),       SB_TLS_ON },
1041         { BER_BVC("critical"),  SB_TLS_CRITICAL },
1042         { BER_BVNULL, 0 }
1043 };
1044 #endif
1045
1046 static slap_verbmasks methkey[] = {
1047         { BER_BVC("none"),      LDAP_AUTH_NONE },
1048         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
1049 #ifdef HAVE_CYRUS_SASL
1050         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1051 #endif
1052         { BER_BVNULL, 0 }
1053 };
1054
1055 static slap_cf_aux_table bindkey[] = {
1056         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1057         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1058         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1059         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1060         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1061         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1062         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1063         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1064         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1065 #ifdef HAVE_TLS
1066         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1067
1068 #define aux_TLS (bindkey+10)    /* beginning of TLS keywords */
1069
1070         { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1071         { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1072         { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1073         { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1074         { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1075         { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1076 #ifdef HAVE_OPENSSL_CRL
1077         { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1078 #endif
1079 #endif
1080         { BER_BVNULL, 0, 0, 0, NULL }
1081 };
1082
1083 int
1084 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1085 {
1086         int rc = SLAP_CONF_UNKNOWN;
1087         slap_cf_aux_table *tab;
1088
1089         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1090                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1091                         char **cptr;
1092                         int *iptr, j;
1093                         unsigned *uptr;
1094                         long *lptr;
1095                         unsigned long *ulptr;
1096                         struct berval *bptr;
1097                         const char *val = word + tab->key.bv_len;
1098
1099                         switch ( tab->type ) {
1100                         case 's':
1101                                 cptr = (char **)((char *)dst + tab->off);
1102                                 *cptr = ch_strdup( val );
1103                                 rc = 0;
1104                                 break;
1105
1106                         case 'b':
1107                                 bptr = (struct berval *)((char *)dst + tab->off);
1108                                 ber_str2bv( val, 0, 1, bptr );
1109                                 rc = 0;
1110                                 break;
1111
1112                         case 'd':
1113                                 assert( tab->aux != NULL );
1114                                 iptr = (int *)((char *)dst + tab->off);
1115
1116                                 rc = 1;
1117                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1118                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1119                                                 *iptr = tab->aux[j].mask;
1120                                                 rc = 0;
1121                                         }
1122                                 }
1123                                 break;
1124
1125                         case 'i':
1126                                 iptr = (int *)((char *)dst + tab->off);
1127
1128                                 rc = lutil_atoix( iptr, val, 0 );
1129                                 break;
1130
1131                         case 'u':
1132                                 uptr = (unsigned *)((char *)dst + tab->off);
1133
1134                                 rc = lutil_atoux( uptr, val, 0 );
1135                                 break;
1136
1137                         case 'I':
1138                                 lptr = (long *)((char *)dst + tab->off);
1139
1140                                 rc = lutil_atolx( lptr, val, 0 );
1141                                 break;
1142
1143                         case 'U':
1144                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1145
1146                                 rc = lutil_atoulx( ulptr, val, 0 );
1147                                 break;
1148                         }
1149
1150                         if ( rc ) {
1151                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1152                                         tabmsg, word, 0 );
1153                         }
1154                         
1155                         return rc;
1156                 }
1157         }
1158
1159         return rc;
1160 }
1161
1162 int
1163 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1164 {
1165         char buf[AC_LINE_MAX], *ptr;
1166         slap_cf_aux_table *tab;
1167         struct berval tmp;
1168
1169         ptr = buf;
1170         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1171                 char **cptr;
1172                 int *iptr, i;
1173                 unsigned *uptr;
1174                 long *lptr;
1175                 unsigned long *ulptr;
1176                 struct berval *bptr;
1177
1178                 cptr = (char **)((char *)src + tab->off);
1179
1180                 switch ( tab->type ) {
1181                 case 'b':
1182                         bptr = (struct berval *)((char *)src + tab->off);
1183                         cptr = &bptr->bv_val;
1184                 case 's':
1185                         if ( *cptr ) {
1186                                 *ptr++ = ' ';
1187                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1188                                 if ( tab->quote ) *ptr++ = '"';
1189                                 ptr = lutil_strcopy( ptr, *cptr );
1190                                 if ( tab->quote ) *ptr++ = '"';
1191                         }
1192                         break;
1193
1194                 case 'd':
1195                         assert( tab->aux != NULL );
1196                         iptr = (int *)((char *)src + tab->off);
1197                 
1198                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1199                                 if ( *iptr == tab->aux[i].mask ) {
1200                                         *ptr++ = ' ';
1201                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1202                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1203                                         break;
1204                                 }
1205                         }
1206                         break;
1207
1208                 case 'i':
1209                         iptr = (int *)((char *)src + tab->off);
1210                         *ptr++ = ' ';
1211                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1212                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1213                         break;
1214
1215                 case 'u':
1216                         uptr = (unsigned *)((char *)src + tab->off);
1217                         *ptr++ = ' ';
1218                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1219                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1220                         break;
1221
1222                 case 'I':
1223                         lptr = (long *)((char *)src + tab->off);
1224                         *ptr++ = ' ';
1225                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1226                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1227                         break;
1228
1229                 case 'U':
1230                         ulptr = (unsigned long *)((char *)src + tab->off);
1231                         *ptr++ = ' ';
1232                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1233                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1234                         break;
1235
1236                 default:
1237                         assert( 0 );
1238                 }
1239         }
1240         tmp.bv_val = buf;
1241         tmp.bv_len = ptr - buf;
1242         ber_dupbv( bv, &tmp );
1243         return 0;
1244 }
1245
1246 int
1247 bindconf_parse( const char *word, slap_bindconf *bc )
1248 {
1249 #ifdef HAVE_TLS
1250         /* Detect TLS config changes explicitly */
1251         if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1252                 bc->sb_tls_do_init = 1;
1253                 return 0;
1254         }
1255 #endif
1256         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1257 }
1258
1259 int
1260 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1261 {
1262         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1263 }
1264
1265 void bindconf_free( slap_bindconf *bc ) {
1266         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1267                 ch_free( bc->sb_uri.bv_val );
1268                 BER_BVZERO( &bc->sb_uri );
1269         }
1270         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1271                 ch_free( bc->sb_binddn.bv_val );
1272                 BER_BVZERO( &bc->sb_binddn );
1273         }
1274         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1275                 ch_free( bc->sb_cred.bv_val );
1276                 BER_BVZERO( &bc->sb_cred );
1277         }
1278         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1279                 ch_free( bc->sb_saslmech.bv_val );
1280                 BER_BVZERO( &bc->sb_saslmech );
1281         }
1282         if ( bc->sb_secprops ) {
1283                 ch_free( bc->sb_secprops );
1284                 bc->sb_secprops = NULL;
1285         }
1286         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1287                 ch_free( bc->sb_realm.bv_val );
1288                 BER_BVZERO( &bc->sb_realm );
1289         }
1290         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1291                 ch_free( bc->sb_authcId.bv_val );
1292                 BER_BVZERO( &bc->sb_authcId );
1293         }
1294         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1295                 ch_free( bc->sb_authzId.bv_val );
1296                 BER_BVZERO( &bc->sb_authzId );
1297         }
1298 #ifdef HAVE_TLS
1299 #if 0
1300         if ( bc->sb_tls_ctx ) {
1301                 SSL_CTX_free( bc->sb_tls_ctx );
1302                 bc->sb_tls_ctx = NULL;
1303         }
1304 #endif
1305         if ( bc->sb_tls_cert ) {
1306                 ch_free( bc->sb_tls_cert );
1307                 bc->sb_tls_cert = NULL;
1308         }
1309         if ( bc->sb_tls_key ) {
1310                 ch_free( bc->sb_tls_key );
1311                 bc->sb_tls_key = NULL;
1312         }
1313         if ( bc->sb_tls_cacert ) {
1314                 ch_free( bc->sb_tls_cacert );
1315                 bc->sb_tls_cacert = NULL;
1316         }
1317         if ( bc->sb_tls_cacertdir ) {
1318                 ch_free( bc->sb_tls_cacertdir );
1319                 bc->sb_tls_cacertdir = NULL;
1320         }
1321         if ( bc->sb_tls_reqcert ) {
1322                 ch_free( bc->sb_tls_reqcert );
1323                 bc->sb_tls_reqcert = NULL;
1324         }
1325         if ( bc->sb_tls_cipher_suite ) {
1326                 ch_free( bc->sb_tls_cipher_suite );
1327                 bc->sb_tls_cipher_suite = NULL;
1328         }
1329 #ifdef HAVE_OPENSSL_CRL
1330         if ( bc->sb_tls_crlcheck ) {
1331                 ch_free( bc->sb_tls_crlcheck );
1332                 bc->sb_tls_crlcheck = NULL;
1333         }
1334 #endif
1335 #endif
1336 }
1337
1338 #ifdef HAVE_TLS
1339 static struct {
1340         const char *key;
1341         size_t offset;
1342         int opt;
1343 } bindtlsopts[] = {
1344         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1345         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1346         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1347         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1348         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1349         {0, 0}
1350 };
1351
1352 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1353 {
1354         int i, rc, newctx = 0, res = 0;
1355         char *ptr = (char *)bc, **word;
1356
1357         bc->sb_tls_do_init = 0;
1358
1359         for (i=0; bindtlsopts[i].opt; i++) {
1360                 word = (char **)(ptr + bindtlsopts[i].offset);
1361                 if ( *word ) {
1362                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1363                         if ( rc ) {
1364                                 Debug( LDAP_DEBUG_ANY,
1365                                         "bindconf_tls_set: failed to set %s to %s\n",
1366                                                 bindtlsopts[i].key, *word, 0 );
1367                                 res = -1;
1368                         } else
1369                                 newctx = 1;
1370                 }
1371         }
1372         if ( bc->sb_tls_reqcert ) {
1373                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1374                         bc->sb_tls_reqcert );
1375                 if ( rc ) {
1376                         Debug( LDAP_DEBUG_ANY,
1377                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1378                                         bc->sb_tls_reqcert, 0, 0 );
1379                         res = -1;
1380                 } else
1381                         newctx = 1;
1382         }
1383 #ifdef HAVE_OPENSSL_CRL
1384         if ( bc->sb_tls_crlcheck ) {
1385                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1386                         bc->sb_tls_crlcheck );
1387                 if ( rc ) {
1388                         Debug( LDAP_DEBUG_ANY,
1389                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1390                                         bc->sb_tls_crlcheck, 0, 0 );
1391                         res = -1;
1392                 } else
1393                         newctx = 1;
1394         }
1395 #endif
1396         if ( newctx ) {
1397                 int opt = 0;
1398
1399                 if ( bc->sb_tls_ctx ) {
1400                         SSL_CTX_free( bc->sb_tls_ctx );
1401                         bc->sb_tls_ctx = NULL;
1402                 }
1403                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1404                 if ( rc )
1405                         res = rc;
1406                 else
1407                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1408         }
1409         
1410         return res;
1411 }
1412 #endif
1413
1414 /*
1415  * connect to a client using the bindconf data
1416  * note: should move "version" into bindconf...
1417  */
1418 int
1419 slap_client_connect( LDAP **ldp, slap_bindconf *sb, int version )
1420 {
1421         LDAP            *ld = NULL;
1422         int             rc;
1423
1424         /* Init connection to master */
1425         rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1426         if ( rc != LDAP_SUCCESS ) {
1427                 Debug( LDAP_DEBUG_ANY,
1428                         "slap_client_connect: "
1429                         "ldap_initialize(%s) failed (%d)\n",
1430                         sb->sb_uri.bv_val, rc, 0 );
1431                 return rc;
1432         }
1433
1434         if ( version != 0 ) {
1435                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1436                         (const void *)&version );
1437         }
1438
1439 #ifdef HAVE_TLS
1440         if ( sb->sb_tls_do_init ) {
1441                 rc = bindconf_tls_set( sb, ld );
1442
1443         } else if ( sb->sb_tls_ctx ) {
1444                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1445                         sb->sb_tls_ctx );
1446         }
1447
1448         if ( rc ) {
1449                 Debug( LDAP_DEBUG_ANY,
1450                         "slap_client_connect: "
1451                         "URI=%s TLS context initialization failed (%d)\n",
1452                         sb->sb_uri.bv_val, rc, 0 );
1453                 return rc;
1454         }
1455 #endif
1456
1457         /* Bind */
1458         if ( sb->sb_tls ) {
1459                 rc = ldap_start_tls_s( ld, NULL, NULL );
1460                 if ( rc != LDAP_SUCCESS ) {
1461                         Debug( LDAP_DEBUG_ANY,
1462                                 "slap_client_connect: URI=%s "
1463                                 "%s, ldap_start_tls failed (%d)\n",
1464                                 sb->sb_uri.bv_val,
1465                                 sb->sb_tls == SB_TLS_CRITICAL ?
1466                                         "Error" : "Warning",
1467                                 rc );
1468                         if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1469                                 goto done;
1470                         }
1471                 }
1472         }
1473
1474         if ( sb->sb_method == LDAP_AUTH_SASL ) {
1475 #ifdef HAVE_CYRUS_SASL
1476                 void *defaults;
1477
1478                 if ( sb->sb_secprops != NULL ) {
1479                         rc = ldap_set_option( ld,
1480                                 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1481
1482                         if( rc != LDAP_OPT_SUCCESS ) {
1483                                 Debug( LDAP_DEBUG_ANY,
1484                                         "slap_client_connect: "
1485                                         "error, ldap_set_option "
1486                                         "(%s,SECPROPS,\"%s\") failed!\n",
1487                                         sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1488                                 goto done;
1489                         }
1490                 }
1491
1492                 defaults = lutil_sasl_defaults( ld,
1493                         sb->sb_saslmech.bv_val,
1494                         sb->sb_realm.bv_val,
1495                         sb->sb_authcId.bv_val,
1496                         sb->sb_cred.bv_val,
1497                         sb->sb_authzId.bv_val );
1498
1499                 rc = ldap_sasl_interactive_bind_s( ld,
1500                                 sb->sb_binddn.bv_val,
1501                                 sb->sb_saslmech.bv_val,
1502                                 NULL, NULL,
1503                                 LDAP_SASL_QUIET,
1504                                 lutil_sasl_interact,
1505                                 defaults );
1506
1507                 lutil_sasl_freedefs( defaults );
1508
1509                 /* FIXME: different error behaviors according to
1510                  *      1) return code
1511                  *      2) on err policy : exit, retry, backoff ...
1512                  */
1513                 if ( rc != LDAP_SUCCESS ) {
1514                         static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1515
1516                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1517                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
1518                                 sb->sb_uri.bv_val, rc, 0 );
1519
1520                         /* FIXME (see above comment) */
1521                         /* if Kerberos credentials cache is not active, retry */
1522                         if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1523                                 rc == LDAP_LOCAL_ERROR )
1524                         {
1525                                 rc = LDAP_SERVER_DOWN;
1526                         }
1527
1528                         goto done;
1529                 }
1530 #else /* HAVE_CYRUS_SASL */
1531                 /* Should never get here, we trapped this at config time */
1532                 assert(0);
1533                 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1534                 rc = LDAP_OTHER;
1535                 goto done;
1536 #endif
1537
1538         } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1539                 rc = ldap_sasl_bind_s( ld,
1540                         sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1541                         &sb->sb_cred, NULL, NULL, NULL );
1542                 if ( rc != LDAP_SUCCESS ) {
1543                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1544                                 "URI=%s DN=\"%s\" "
1545                                 "ldap_sasl_bind_s failed (%d)\n",
1546                                 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1547                         goto done;
1548                 }
1549         }
1550
1551 done:;
1552         if ( rc ) {
1553                 if ( ld ) {
1554                         ldap_unbind_ext( ld, NULL, NULL );
1555                         *ldp = NULL;
1556                 }
1557
1558         } else {
1559                 *ldp = ld;
1560         }
1561
1562         return rc;
1563 }
1564
1565 /* -------------------------------------- */
1566
1567
1568 static char *
1569 strtok_quote( char *line, char *sep, char **quote_ptr )
1570 {
1571         int             inquote;
1572         char            *tmp;
1573         static char     *next;
1574
1575         *quote_ptr = NULL;
1576         if ( line != NULL ) {
1577                 next = line;
1578         }
1579         while ( *next && strchr( sep, *next ) ) {
1580                 next++;
1581         }
1582
1583         if ( *next == '\0' ) {
1584                 next = NULL;
1585                 return( NULL );
1586         }
1587         tmp = next;
1588
1589         for ( inquote = 0; *next; ) {
1590                 switch ( *next ) {
1591                 case '"':
1592                         if ( inquote ) {
1593                                 inquote = 0;
1594                         } else {
1595                                 inquote = 1;
1596                         }
1597                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1598                         break;
1599
1600                 case '\\':
1601                         if ( next[1] )
1602                                 AC_MEMCPY( next,
1603                                             next + 1, strlen( next + 1 ) + 1 );
1604                         next++;         /* dont parse the escaped character */
1605                         break;
1606
1607                 default:
1608                         if ( ! inquote ) {
1609                                 if ( strchr( sep, *next ) != NULL ) {
1610                                         *quote_ptr = next;
1611                                         *next++ = '\0';
1612                                         return( tmp );
1613                                 }
1614                         }
1615                         next++;
1616                         break;
1617                 }
1618         }
1619
1620         return( tmp );
1621 }
1622
1623 static char     buf[AC_LINE_MAX];
1624 static char     *line;
1625 static size_t lmax, lcur;
1626
1627 #define CATLINE( buf ) \
1628         do { \
1629                 size_t len = strlen( buf ); \
1630                 while ( lcur + len + 1 > lmax ) { \
1631                         lmax += AC_LINE_MAX; \
1632                         line = (char *) ch_realloc( line, lmax ); \
1633                 } \
1634                 strcpy( line + lcur, buf ); \
1635                 lcur += len; \
1636         } while( 0 )
1637
1638 static void
1639 fp_getline_init(ConfigArgs *c) {
1640         c->lineno = -1;
1641         buf[0] = '\0';
1642 }
1643
1644 static int
1645 fp_getline( FILE *fp, ConfigArgs *c )
1646 {
1647         char    *p;
1648
1649         lcur = 0;
1650         CATLINE(buf);
1651         c->lineno++;
1652
1653         /* avoid stack of bufs */
1654         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1655                 buf[0] = '\0';
1656                 c->line = line;
1657                 return(1);
1658         }
1659
1660         while ( fgets( buf, sizeof( buf ), fp ) ) {
1661                 p = strchr( buf, '\n' );
1662                 if ( p ) {
1663                         if ( p > buf && p[-1] == '\r' ) {
1664                                 --p;
1665                         }
1666                         *p = '\0';
1667                 }
1668                 /* XXX ugly */
1669                 c->line = line;
1670                 if ( line[0]
1671                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1672                                 && p[-1] != '\\' )
1673                 {
1674                         p[0] = '\0';
1675                         lcur--;
1676                         
1677                 } else {
1678                         if ( !isspace( (unsigned char)buf[0] ) ) {
1679                                 return(1);
1680                         }
1681                         buf[0] = ' ';
1682                 }
1683                 CATLINE(buf);
1684                 c->lineno++;
1685         }
1686
1687         buf[0] = '\0';
1688         c->line = line;
1689         return(line[0] ? 1 : 0);
1690 }
1691
1692 static int
1693 fp_parse_line(ConfigArgs *c)
1694 {
1695         char *token;
1696         static char *const hide[] = {
1697                 "rootpw", "replica", "syncrepl",  /* in slapd */
1698                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1699                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1700                 "pseudorootpw",  /* in back-meta */
1701                 "dbpasswd",  /* in back-sql */
1702                 NULL
1703         };
1704         char *quote_ptr;
1705         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1706
1707         c->tline = ch_strdup(c->line);
1708         token = strtok_quote(c->tline, " \t", &quote_ptr);
1709
1710         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1711         if(quote_ptr) *quote_ptr = ' ';
1712         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1713                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1714         if(quote_ptr) *quote_ptr = '\0';
1715
1716         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1717                 if(c->argc >= c->argv_size) {
1718                         char **tmp;
1719                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1720                         if(!tmp) {
1721                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1722                                 return -1;
1723                         }
1724                         c->argv = tmp;
1725                         c->argv_size += ARGS_STEP;
1726                 }
1727                 if(token == NULL)
1728                         break;
1729                 c->argv[c->argc++] = token;
1730         }
1731         c->argv[c->argc] = NULL;
1732         return(0);
1733 }
1734
1735 void
1736 config_destroy( )
1737 {
1738         ucdata_unload( UCDATA_ALL );
1739         if ( frontendDB ) {
1740                 /* NOTE: in case of early exit, frontendDB can be NULL */
1741                 if ( frontendDB->be_schemandn.bv_val )
1742                         free( frontendDB->be_schemandn.bv_val );
1743                 if ( frontendDB->be_schemadn.bv_val )
1744                         free( frontendDB->be_schemadn.bv_val );
1745                 if ( frontendDB->be_acl )
1746                         acl_destroy( frontendDB->be_acl, NULL );
1747         }
1748         free( line );
1749         if ( slapd_args_file )
1750                 free ( slapd_args_file );
1751         if ( slapd_pid_file )
1752                 free ( slapd_pid_file );
1753         if ( default_passwd_hash )
1754                 ldap_charray_free( default_passwd_hash );
1755 }
1756
1757 char **
1758 slap_str2clist( char ***out, char *in, const char *brkstr )
1759 {
1760         char    *str;
1761         char    *s;
1762         char    *lasts;
1763         int     i, j;
1764         char    **new;
1765
1766         /* find last element in list */
1767         for (i = 0; *out && (*out)[i]; i++);
1768
1769         /* protect the input string from strtok */
1770         str = ch_strdup( in );
1771
1772         if ( *str == '\0' ) {
1773                 free( str );
1774                 return( *out );
1775         }
1776
1777         /* Count words in string */
1778         j=1;
1779         for ( s = str; *s; s++ ) {
1780                 if ( strchr( brkstr, *s ) != NULL ) {
1781                         j++;
1782                 }
1783         }
1784
1785         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1786         new = *out + i;
1787         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1788                 s != NULL;
1789                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1790         {
1791                 *new = ch_strdup( s );
1792                 new++;
1793         }
1794
1795         *new = NULL;
1796         free( str );
1797         return( *out );
1798 }
1799
1800 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1801         int argc, char **argv )
1802 {
1803         ConfigArgs c = { 0 };
1804         ConfigTable *ct;
1805         int rc;
1806
1807         c.be = be;
1808         c.fname = fname;
1809         c.lineno = lineno;
1810         c.argc = argc;
1811         c.argv = argv;
1812         c.valx = -1;
1813         c.line = line;
1814         c.op = SLAP_CONFIG_ADD;
1815         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1816
1817         rc = SLAP_CONF_UNKNOWN;
1818         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1819         if ( ct )
1820                 rc = config_add_vals( ct, &c );
1821         return rc;
1822 }