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