]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
dbc62367e15e59e385484fc19affbd80d19ddadd
[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         int i, code;
475
476         for (i=0; ct[i].name; i++ ) {
477                 if ( !ct[i].attribute ) continue;
478                 code = register_at( ct[i].attribute, &ct[i].ad, 1 );
479                 if ( code ) {
480                         fprintf( stderr, "init_config_attrs: register_at failed\n" );
481                         return code;
482                 }
483 #ifndef LDAP_DEVEL
484                 ct[i].ad->ad_type->sat_flags |= SLAP_AT_HIDE;
485 #endif
486         }
487
488         return 0;
489 }
490
491 int
492 init_config_ocs( ConfigOCs *ocs ) {
493         int i, code;
494
495         for (i=0;ocs[i].co_def;i++) {
496                 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 );
497                 if ( code ) {
498                         fprintf( stderr, "init_config_ocs: register_oc failed\n" );
499                         return code;
500                 }
501 #ifndef LDAP_DEVEL
502                 ocs[i].co_oc->soc_flags |= SLAP_OC_HIDE;
503 #endif
504         }
505         return 0;
506 }
507
508 /* Split an LDIF line into space-separated tokens. Words may be grouped
509  * by quotes. A quoted string may begin in the middle of a word, but must
510  * end at the end of the word (be followed by whitespace or EOS). Any other
511  * quotes are passed through unchanged. All other characters are passed
512  * through unchanged.
513  */
514 static char *
515 strtok_quote_ldif( char **line )
516 {
517         char *beg, *ptr, *quote=NULL;
518         int inquote=0;
519
520         ptr = *line;
521
522         if ( !ptr || !*ptr )
523                 return NULL;
524
525         while( isspace( (unsigned char) *ptr )) ptr++;
526
527         if ( *ptr == '"' ) {
528                 inquote = 1;
529                 ptr++;
530         }
531
532         beg = ptr;
533
534         for (;*ptr;ptr++) {
535                 if ( *ptr == '"' ) {
536                         if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) {
537                                 *ptr++ = '\0';
538                                 break;
539                         }
540                         inquote = 1;
541                         quote = ptr;
542                         continue;
543                 }
544                 if ( inquote )
545                         continue;
546                 if ( isspace( (unsigned char) *ptr )) {
547                         *ptr++ = '\0';
548                         break;
549                 }
550         }
551         if ( quote ) {
552                 while ( quote < ptr ) {
553                         *quote = quote[1];
554                         quote++;
555                 }
556         }
557         if ( !*ptr ) {
558                 *line = NULL;
559         } else {
560                 while ( isspace( (unsigned char) *ptr )) ptr++;
561                 *line = ptr;
562         }
563         return beg;
564 }
565
566 static void
567 config_parse_ldif( ConfigArgs *c )
568 {
569         char *next;
570         c->tline = ch_strdup(c->line);
571         next = c->tline;
572
573         while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) {
574                 c->argc++;
575                 if ( c->argc >= c->argv_size ) {
576                         char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) *
577                                 sizeof( *c->argv ));
578                         c->argv = tmp;
579                         c->argv_size += ARGS_STEP;
580                 }
581         }
582         c->argv[c->argc] = NULL;
583 }
584
585 int
586 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx)
587 {
588         int     rc = 0;
589
590         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
591                 ct->ad->ad_cname.bv_val, valx );
592         c->argc = 1;
593         c->argv[0] = ct->ad->ad_cname.bv_val;
594
595         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
596                 c->argv[c->argc] = c->line;
597                 c->argc++;
598                 c->argv[c->argc] = NULL;
599                 c->tline = NULL;
600         } else {
601                 config_parse_ldif( c );
602         }
603         rc = config_check_vals( ct, c, 1 );
604         ch_free( c->tline );
605         c->tline = NULL;
606
607         if ( rc )
608                 rc = LDAP_CONSTRAINT_VIOLATION;
609
610         return rc;
611 }
612
613 int
614 config_parse_add(ConfigTable *ct, ConfigArgs *c)
615 {
616         int     rc = 0;
617
618         snprintf( c->log, sizeof( c->log ), "%s: value #%d",
619                 ct->ad->ad_cname.bv_val, c->valx );
620         c->argc = 1;
621         c->argv[0] = ct->ad->ad_cname.bv_val;
622
623         if ( ( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) {
624                 c->argv[c->argc] = c->line;
625                 c->argc++;
626                 c->argv[c->argc] = NULL;
627                 c->tline = NULL;
628         } else {
629                 config_parse_ldif( c );
630         }
631         c->op = LDAP_MOD_ADD;
632         rc = config_add_vals( ct, c );
633         ch_free( c->tline );
634
635         return rc;
636 }
637
638 int
639 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft)
640 {
641         FILE *fp;
642         ConfigTable *ct;
643         ConfigArgs *c;
644         int rc;
645         struct stat s;
646
647         c = ch_calloc( 1, sizeof( ConfigArgs ) );
648         if ( c == NULL ) {
649                 return 1;
650         }
651
652         if ( depth ) {
653                 memcpy( c, cf, sizeof( ConfigArgs ) );
654         } else {
655                 c->depth = depth; /* XXX */
656                 c->bi = NULL;
657                 c->be = NULL;
658         }
659
660         c->valx = -1;
661         c->fname = fname;
662         init_config_argv( c );
663
664         if ( stat( fname, &s ) != 0 ) {
665                 ldap_syslog = 1;
666                 Debug(LDAP_DEBUG_ANY,
667                     "could not stat config file \"%s\": %s (%d)\n",
668                     fname, strerror(errno), errno);
669                 ch_free( c );
670                 return(1);
671         }
672
673         if ( !S_ISREG( s.st_mode ) ) {
674                 ldap_syslog = 1;
675                 Debug(LDAP_DEBUG_ANY,
676                     "regular file expected, got \"%s\"\n",
677                     fname, 0, 0 );
678                 ch_free( c );
679                 return(1);
680         }
681
682         fp = fopen( fname, "r" );
683         if ( fp == NULL ) {
684                 ldap_syslog = 1;
685                 Debug(LDAP_DEBUG_ANY,
686                     "could not open config file \"%s\": %s (%d)\n",
687                     fname, strerror(errno), errno);
688                 ch_free( c );
689                 return(1);
690         }
691
692         Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0);
693
694         fp_getline_init(c);
695
696         c->tline = NULL;
697
698         while ( fp_getline( fp, c ) ) {
699                 /* skip comments and blank lines */
700                 if ( c->line[0] == '#' || c->line[0] == '\0' ) {
701                         continue;
702                 }
703
704                 snprintf( c->log, sizeof( c->log ), "%s: line %d",
705                                 c->fname, c->lineno );
706
707                 c->argc = 0;
708                 ch_free( c->tline );
709                 if ( fp_parse_line( c ) ) {
710                         rc = 1;
711                         goto done;
712                 }
713
714                 if ( c->argc < 1 ) {
715                         Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n",
716                                 c->log, 0, 0);
717                         rc = 1;
718                         goto done;
719                 }
720
721                 c->op = SLAP_CONFIG_ADD;
722
723                 ct = config_find_keyword( cft, c );
724                 if ( ct ) {
725                         rc = config_add_vals( ct, c );
726                         if ( !rc ) continue;
727
728                         if ( rc & ARGS_USERLAND ) {
729                                 /* XXX a usertype would be opaque here */
730                                 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n",
731                                         c->log, c->argv[0], 0);
732                                 rc = 1;
733                                 goto done;
734
735                         } else if ( rc == ARG_BAD_CONF ) {
736                                 rc = 1;
737                                 goto done;
738                         }
739                         
740                 } else if ( c->bi && !c->be ) {
741                         rc = SLAP_CONF_UNKNOWN;
742                         if ( c->bi->bi_cf_ocs ) {
743                                 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c );
744                                 if ( ct ) {
745                                         rc = config_add_vals( ct, c );
746                                 }
747                         }
748                         if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) {
749                                 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno,
750                                         c->argc, c->argv);
751                         }
752                         if ( rc ) {
753                                 switch(rc) {
754                                 case SLAP_CONF_UNKNOWN:
755                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
756                                                 "<%s> inside backend info definition.\n",
757                                                 c->log, *c->argv, 0);
758                                 default:
759                                         rc = 1;
760                                         goto done;
761                                 }
762                         }
763
764                 } else if ( c->be && c->be != frontendDB ) {
765                         rc = SLAP_CONF_UNKNOWN;
766                         if ( c->be->be_cf_ocs ) {
767                                 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c );
768                                 if ( ct ) {
769                                         rc = config_add_vals( ct, c );
770                                 }
771                         }
772                         if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) {
773                                 rc = (*c->be->be_config)(c->be, c->fname, c->lineno,
774                                         c->argc, c->argv);
775                         }
776                         if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) )
777                         {
778                                 /* global overlays may need 
779                                  * definitions inside other databases...
780                                  */
781                                 rc = (*frontendDB->be_config)( frontendDB,
782                                         c->fname, (int)c->lineno, c->argc, c->argv );
783                         }
784
785                         switch ( rc ) {
786                         case 0:
787                                 break;
788
789                         case SLAP_CONF_UNKNOWN:
790                                 Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
791                                         "<%s> inside backend database definition.\n",
792                                         c->log, *c->argv, 0);
793                                 
794                         default:
795                                 rc = 1;
796                                 goto done;
797                         }
798
799                 } else if ( frontendDB->be_config ) {
800                         rc = (*frontendDB->be_config)( frontendDB,
801                                 c->fname, (int)c->lineno, c->argc, c->argv);
802                         if ( rc ) {
803                                 switch(rc) {
804                                 case SLAP_CONF_UNKNOWN:
805                                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
806                                                 "<%s> inside global database definition.\n",
807                                                 c->log, *c->argv, 0);
808
809                                 default:
810                                         rc = 1;
811                                         goto done;
812                                 }
813                         }
814                         
815                 } else {
816                         Debug( LDAP_DEBUG_ANY, "%s: unknown directive "
817                                 "<%s> outside backend info and database definitions.\n",
818                                 c->log, *c->argv, 0);
819                         rc = 1;
820                         goto done;
821                 }
822         }
823
824         rc = 0;
825
826 done:
827         ch_free(c->tline);
828         fclose(fp);
829         ch_free(c->argv);
830         ch_free(c);
831         return(rc);
832 }
833
834 /* restrictops, allows, disallows, requires, loglevel */
835
836 int
837 bverb_to_mask(struct berval *bword, slap_verbmasks *v) {
838         int i;
839         for(i = 0; !BER_BVISNULL(&v[i].word); i++) {
840                 if(!ber_bvstrcasecmp(bword, &v[i].word)) break;
841         }
842         return(i);
843 }
844
845 int
846 verb_to_mask(const char *word, slap_verbmasks *v) {
847         struct berval   bword;
848         ber_str2bv( word, 0, 0, &bword );
849         return bverb_to_mask( &bword, v );
850 }
851
852 int
853 verbs_to_mask(int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m) {
854         int i, j;
855         for(i = 1; i < argc; i++) {
856                 j = verb_to_mask(argv[i], v);
857                 if(BER_BVISNULL(&v[j].word)) return i;
858                 while (!v[j].mask) j--;
859                 *m |= v[j].mask;
860         }
861         return(0);
862 }
863
864 /* Mask keywords that represent multiple bits should occur before single
865  * bit keywords in the verbmasks array.
866  */
867 int
868 mask_to_verbs(slap_verbmasks *v, slap_mask_t m, BerVarray *bva) {
869         int i, rc = 1;
870
871         if (m) {
872                 for (i=0; !BER_BVISNULL(&v[i].word); i++) {
873                         if (!v[i].mask) continue;
874                         if (( m & v[i].mask ) == v[i].mask ) {
875                                 value_add_one( bva, &v[i].word );
876                                 rc = 0;
877                                 m ^= v[i].mask;
878                                 if ( !m ) break;
879                         }
880                 }
881         }
882         return rc;
883 }
884
885 int
886 slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
887 {
888         int             i;
889
890         assert( *vp == NULL );
891
892         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
893
894         *vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
895
896         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
897                 ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
898                 *((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
899         }
900
901         BER_BVZERO( &(*vp)[ i ].word );
902
903         return 0;               
904 }
905
906 int
907 slap_verbmasks_destroy( slap_verbmasks *v )
908 {
909         int             i;
910
911         assert( v != NULL );
912
913         for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
914                 ch_free( v[ i ].word.bv_val );
915         }
916
917         ch_free( v );
918
919         return 0;
920 }
921
922 int
923 slap_verbmasks_append(
924         slap_verbmasks  **vp,
925         slap_mask_t     m,
926         struct berval   *v,
927         slap_mask_t     *ignore )
928 {
929         int     i;
930
931         if ( !m ) {
932                 return LDAP_OPERATIONS_ERROR;
933         }
934
935         for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
936                 if ( !(*vp)[ i ].mask ) continue;
937
938                 if ( ignore != NULL ) {
939                         int     j;
940
941                         for ( j = 0; ignore[ j ] != 0; j++ ) {
942                                 if ( (*vp)[ i ].mask == ignore[ j ] ) {
943                                         goto check_next;
944                                 }
945                         }
946                 }
947
948                 if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
949                         if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
950                                 /* already set; ignore */
951                                 return LDAP_SUCCESS;
952                         }
953                         /* conflicts */
954                         return LDAP_TYPE_OR_VALUE_EXISTS;
955                 }
956
957                 if ( m & (*vp)[ i ].mask ) {
958                         /* conflicts */
959                         return LDAP_CONSTRAINT_VIOLATION;
960                 }
961 check_next:;
962         }
963
964         *vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
965         ber_dupbv( &(*vp)[ i ].word, v );
966         *((slap_mask_t *)&(*vp)[ i ].mask) = m;
967         BER_BVZERO( &(*vp)[ i + 1 ].word );
968
969         return LDAP_SUCCESS;
970 }
971
972 int
973 enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
974         int i;
975
976         for (i=0; !BER_BVISNULL(&v[i].word); i++) {
977                 if ( m == v[i].mask ) {
978                         if ( bv != NULL ) {
979                                 *bv = v[i].word;
980                         }
981                         return i;
982                 }
983         }
984         return -1;
985 }
986
987 #ifdef HAVE_TLS
988 static slap_verbmasks tlskey[] = {
989         { BER_BVC("no"),        SB_TLS_OFF },
990         { BER_BVC("yes"),       SB_TLS_ON },
991         { BER_BVC("critical"),  SB_TLS_CRITICAL },
992         { BER_BVNULL, 0 }
993 };
994 #endif
995
996 static slap_verbmasks methkey[] = {
997         { BER_BVC("none"),      LDAP_AUTH_NONE },
998         { BER_BVC("simple"),    LDAP_AUTH_SIMPLE },
999 #ifdef HAVE_CYRUS_SASL
1000         { BER_BVC("sasl"),      LDAP_AUTH_SASL },
1001 #endif
1002         { BER_BVNULL, 0 }
1003 };
1004
1005 static slap_verbmasks versionkey[] = {
1006         { BER_BVC("2"),         LDAP_VERSION2 },
1007         { BER_BVC("3"),         LDAP_VERSION3 },
1008         { BER_BVNULL, 0 }
1009 };
1010
1011 static slap_cf_aux_table bindkey[] = {
1012         { BER_BVC("uri="), offsetof(slap_bindconf, sb_uri), 'b', 1, NULL },
1013         { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'd', 0, versionkey },
1014         { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
1015         { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
1016         { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL },
1017         { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL },
1018         { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL },
1019         { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL },
1020         { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 0, NULL },
1021         { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, NULL },
1022 #ifdef HAVE_TLS
1023         { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
1024
1025 #define aux_TLS (bindkey+10)    /* beginning of TLS keywords */
1026
1027         { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL },
1028         { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL },
1029         { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL },
1030         { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL },
1031         { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 1, NULL },
1032         { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 1, NULL },
1033 #ifdef HAVE_OPENSSL_CRL
1034         { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 1, NULL },
1035 #endif
1036 #endif
1037         { BER_BVNULL, 0, 0, 0, NULL }
1038 };
1039
1040 int
1041 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
1042 {
1043         int rc = SLAP_CONF_UNKNOWN;
1044         slap_cf_aux_table *tab;
1045
1046         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1047                 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
1048                         char **cptr;
1049                         int *iptr, j;
1050                         unsigned *uptr;
1051                         long *lptr;
1052                         unsigned long *ulptr;
1053                         struct berval *bptr;
1054                         const char *val = word + tab->key.bv_len;
1055
1056                         switch ( tab->type ) {
1057                         case 's':
1058                                 cptr = (char **)((char *)dst + tab->off);
1059                                 *cptr = ch_strdup( val );
1060                                 rc = 0;
1061                                 break;
1062
1063                         case 'b':
1064                                 bptr = (struct berval *)((char *)dst + tab->off);
1065                                 ber_str2bv( val, 0, 1, bptr );
1066                                 rc = 0;
1067                                 break;
1068
1069                         case 'd':
1070                                 assert( tab->aux != NULL );
1071                                 iptr = (int *)((char *)dst + tab->off);
1072
1073                                 rc = 1;
1074                                 for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
1075                                         if ( !strcasecmp( val, tab->aux[j].word.bv_val ) ) {
1076                                                 *iptr = tab->aux[j].mask;
1077                                                 rc = 0;
1078                                         }
1079                                 }
1080                                 break;
1081
1082                         case 'i':
1083                                 iptr = (int *)((char *)dst + tab->off);
1084
1085                                 rc = lutil_atoix( iptr, val, 0 );
1086                                 break;
1087
1088                         case 'u':
1089                                 uptr = (unsigned *)((char *)dst + tab->off);
1090
1091                                 rc = lutil_atoux( uptr, val, 0 );
1092                                 break;
1093
1094                         case 'I':
1095                                 lptr = (long *)((char *)dst + tab->off);
1096
1097                                 rc = lutil_atolx( lptr, val, 0 );
1098                                 break;
1099
1100                         case 'U':
1101                                 ulptr = (unsigned long *)((char *)dst + tab->off);
1102
1103                                 rc = lutil_atoulx( ulptr, val, 0 );
1104                                 break;
1105                         }
1106
1107                         if ( rc ) {
1108                                 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
1109                                         tabmsg, word, 0 );
1110                         }
1111                         
1112                         return rc;
1113                 }
1114         }
1115
1116         return rc;
1117 }
1118
1119 int
1120 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
1121 {
1122         char buf[AC_LINE_MAX], *ptr;
1123         slap_cf_aux_table *tab;
1124         struct berval tmp;
1125
1126         ptr = buf;
1127         for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
1128                 char **cptr;
1129                 int *iptr, i;
1130                 unsigned *uptr;
1131                 long *lptr;
1132                 unsigned long *ulptr;
1133                 struct berval *bptr;
1134
1135                 cptr = (char **)((char *)src + tab->off);
1136
1137                 switch ( tab->type ) {
1138                 case 'b':
1139                         bptr = (struct berval *)((char *)src + tab->off);
1140                         cptr = &bptr->bv_val;
1141                 case 's':
1142                         if ( *cptr ) {
1143                                 *ptr++ = ' ';
1144                                 ptr = lutil_strcopy( ptr, tab->key.bv_val );
1145                                 if ( tab->quote ) *ptr++ = '"';
1146                                 ptr = lutil_strcopy( ptr, *cptr );
1147                                 if ( tab->quote ) *ptr++ = '"';
1148                         }
1149                         break;
1150
1151                 case 'd':
1152                         assert( tab->aux != NULL );
1153                         iptr = (int *)((char *)src + tab->off);
1154                 
1155                         for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
1156                                 if ( *iptr == tab->aux[i].mask ) {
1157                                         *ptr++ = ' ';
1158                                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1159                                         ptr = lutil_strcopy( ptr, tab->aux[i].word.bv_val );
1160                                         break;
1161                                 }
1162                         }
1163                         break;
1164
1165                 case 'i':
1166                         iptr = (int *)((char *)src + tab->off);
1167                         *ptr++ = ' ';
1168                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1169                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
1170                         break;
1171
1172                 case 'u':
1173                         uptr = (unsigned *)((char *)src + tab->off);
1174                         *ptr++ = ' ';
1175                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1176                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
1177                         break;
1178
1179                 case 'I':
1180                         lptr = (long *)((char *)src + tab->off);
1181                         *ptr++ = ' ';
1182                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1183                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr );
1184                         break;
1185
1186                 case 'U':
1187                         ulptr = (unsigned long *)((char *)src + tab->off);
1188                         *ptr++ = ' ';
1189                         ptr = lutil_strcopy( ptr, tab->key.bv_val );
1190                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr );
1191                         break;
1192
1193                 default:
1194                         assert( 0 );
1195                 }
1196         }
1197         tmp.bv_val = buf;
1198         tmp.bv_len = ptr - buf;
1199         ber_dupbv( bv, &tmp );
1200         return 0;
1201 }
1202
1203 int
1204 bindconf_parse( const char *word, slap_bindconf *bc )
1205 {
1206 #ifdef HAVE_TLS
1207         /* Detect TLS config changes explicitly */
1208         if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) {
1209                 bc->sb_tls_do_init = 1;
1210                 return 0;
1211         }
1212 #endif
1213         return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
1214 }
1215
1216 int
1217 bindconf_unparse( slap_bindconf *bc, struct berval *bv )
1218 {
1219         return slap_cf_aux_table_unparse( bc, bv, bindkey );
1220 }
1221
1222 void bindconf_free( slap_bindconf *bc ) {
1223         if ( !BER_BVISNULL( &bc->sb_uri ) ) {
1224                 ch_free( bc->sb_uri.bv_val );
1225                 BER_BVZERO( &bc->sb_uri );
1226         }
1227         if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
1228                 ch_free( bc->sb_binddn.bv_val );
1229                 BER_BVZERO( &bc->sb_binddn );
1230         }
1231         if ( !BER_BVISNULL( &bc->sb_cred ) ) {
1232                 ch_free( bc->sb_cred.bv_val );
1233                 BER_BVZERO( &bc->sb_cred );
1234         }
1235         if ( !BER_BVISNULL( &bc->sb_saslmech ) ) {
1236                 ch_free( bc->sb_saslmech.bv_val );
1237                 BER_BVZERO( &bc->sb_saslmech );
1238         }
1239         if ( bc->sb_secprops ) {
1240                 ch_free( bc->sb_secprops );
1241                 bc->sb_secprops = NULL;
1242         }
1243         if ( !BER_BVISNULL( &bc->sb_realm ) ) {
1244                 ch_free( bc->sb_realm.bv_val );
1245                 BER_BVZERO( &bc->sb_realm );
1246         }
1247         if ( !BER_BVISNULL( &bc->sb_authcId ) ) {
1248                 ch_free( bc->sb_authcId.bv_val );
1249                 BER_BVZERO( &bc->sb_authcId );
1250         }
1251         if ( !BER_BVISNULL( &bc->sb_authzId ) ) {
1252                 ch_free( bc->sb_authzId.bv_val );
1253                 BER_BVZERO( &bc->sb_authzId );
1254         }
1255 #ifdef HAVE_TLS
1256 #if 0
1257         if ( bc->sb_tls_ctx ) {
1258                 SSL_CTX_free( bc->sb_tls_ctx );
1259                 bc->sb_tls_ctx = NULL;
1260         }
1261 #endif
1262         if ( bc->sb_tls_cert ) {
1263                 ch_free( bc->sb_tls_cert );
1264                 bc->sb_tls_cert = NULL;
1265         }
1266         if ( bc->sb_tls_key ) {
1267                 ch_free( bc->sb_tls_key );
1268                 bc->sb_tls_key = NULL;
1269         }
1270         if ( bc->sb_tls_cacert ) {
1271                 ch_free( bc->sb_tls_cacert );
1272                 bc->sb_tls_cacert = NULL;
1273         }
1274         if ( bc->sb_tls_cacertdir ) {
1275                 ch_free( bc->sb_tls_cacertdir );
1276                 bc->sb_tls_cacertdir = NULL;
1277         }
1278         if ( bc->sb_tls_reqcert ) {
1279                 ch_free( bc->sb_tls_reqcert );
1280                 bc->sb_tls_reqcert = NULL;
1281         }
1282         if ( bc->sb_tls_cipher_suite ) {
1283                 ch_free( bc->sb_tls_cipher_suite );
1284                 bc->sb_tls_cipher_suite = NULL;
1285         }
1286 #ifdef HAVE_OPENSSL_CRL
1287         if ( bc->sb_tls_crlcheck ) {
1288                 ch_free( bc->sb_tls_crlcheck );
1289                 bc->sb_tls_crlcheck = NULL;
1290         }
1291 #endif
1292 #endif
1293 }
1294
1295 #ifdef HAVE_TLS
1296 static struct {
1297         const char *key;
1298         size_t offset;
1299         int opt;
1300 } bindtlsopts[] = {
1301         { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE },
1302         { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE },
1303         { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE },
1304         { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR },
1305         { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE },
1306         {0, 0}
1307 };
1308
1309 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld )
1310 {
1311         int i, rc, newctx = 0, res = 0;
1312         char *ptr = (char *)bc, **word;
1313
1314         bc->sb_tls_do_init = 0;
1315
1316         for (i=0; bindtlsopts[i].opt; i++) {
1317                 word = (char **)(ptr + bindtlsopts[i].offset);
1318                 if ( *word ) {
1319                         rc = ldap_set_option( ld, bindtlsopts[i].opt, *word );
1320                         if ( rc ) {
1321                                 Debug( LDAP_DEBUG_ANY,
1322                                         "bindconf_tls_set: failed to set %s to %s\n",
1323                                                 bindtlsopts[i].key, *word, 0 );
1324                                 res = -1;
1325                         } else
1326                                 newctx = 1;
1327                 }
1328         }
1329         if ( bc->sb_tls_reqcert ) {
1330                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT,
1331                         bc->sb_tls_reqcert );
1332                 if ( rc ) {
1333                         Debug( LDAP_DEBUG_ANY,
1334                                 "bindconf_tls_set: failed to set tls_reqcert to %s\n",
1335                                         bc->sb_tls_reqcert, 0, 0 );
1336                         res = -1;
1337                 } else
1338                         newctx = 1;
1339         }
1340 #ifdef HAVE_OPENSSL_CRL
1341         if ( bc->sb_tls_crlcheck ) {
1342                 rc = ldap_int_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK,
1343                         bc->sb_tls_crlcheck );
1344                 if ( rc ) {
1345                         Debug( LDAP_DEBUG_ANY,
1346                                 "bindconf_tls_set: failed to set tls_crlcheck to %s\n",
1347                                         bc->sb_tls_crlcheck, 0, 0 );
1348                         res = -1;
1349                 } else
1350                         newctx = 1;
1351         }
1352 #endif
1353         if ( newctx ) {
1354                 int opt = 0;
1355
1356                 if ( bc->sb_tls_ctx ) {
1357                         SSL_CTX_free( bc->sb_tls_ctx );
1358                         bc->sb_tls_ctx = NULL;
1359                 }
1360                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
1361                 if ( rc )
1362                         res = rc;
1363                 else
1364                         ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx );
1365         }
1366         
1367         return res;
1368 }
1369 #endif
1370
1371 /*
1372  * connect to a client using the bindconf data
1373  * note: should move "version" into bindconf...
1374  */
1375 int
1376 slap_client_connect( LDAP **ldp, slap_bindconf *sb )
1377 {
1378         LDAP            *ld = NULL;
1379         int             rc;
1380
1381         /* Init connection to master */
1382         rc = ldap_initialize( &ld, sb->sb_uri.bv_val );
1383         if ( rc != LDAP_SUCCESS ) {
1384                 Debug( LDAP_DEBUG_ANY,
1385                         "slap_client_connect: "
1386                         "ldap_initialize(%s) failed (%d)\n",
1387                         sb->sb_uri.bv_val, rc, 0 );
1388                 return rc;
1389         }
1390
1391         if ( sb->sb_version != 0 ) {
1392                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
1393                         (const void *)&sb->sb_version );
1394         }
1395
1396 #ifdef HAVE_TLS
1397         if ( sb->sb_tls_do_init ) {
1398                 rc = bindconf_tls_set( sb, ld );
1399
1400         } else if ( sb->sb_tls_ctx ) {
1401                 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX,
1402                         sb->sb_tls_ctx );
1403         }
1404
1405         if ( rc ) {
1406                 Debug( LDAP_DEBUG_ANY,
1407                         "slap_client_connect: "
1408                         "URI=%s TLS context initialization failed (%d)\n",
1409                         sb->sb_uri.bv_val, rc, 0 );
1410                 return rc;
1411         }
1412 #endif
1413
1414         /* Bind */
1415         if ( sb->sb_tls ) {
1416                 rc = ldap_start_tls_s( ld, NULL, NULL );
1417                 if ( rc != LDAP_SUCCESS ) {
1418                         Debug( LDAP_DEBUG_ANY,
1419                                 "slap_client_connect: URI=%s "
1420                                 "%s, ldap_start_tls failed (%d)\n",
1421                                 sb->sb_uri.bv_val,
1422                                 sb->sb_tls == SB_TLS_CRITICAL ?
1423                                         "Error" : "Warning",
1424                                 rc );
1425                         if ( sb->sb_tls == SB_TLS_CRITICAL ) {
1426                                 goto done;
1427                         }
1428                 }
1429         }
1430
1431         if ( sb->sb_method == LDAP_AUTH_SASL ) {
1432 #ifdef HAVE_CYRUS_SASL
1433                 void *defaults;
1434
1435                 if ( sb->sb_secprops != NULL ) {
1436                         rc = ldap_set_option( ld,
1437                                 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops);
1438
1439                         if( rc != LDAP_OPT_SUCCESS ) {
1440                                 Debug( LDAP_DEBUG_ANY,
1441                                         "slap_client_connect: "
1442                                         "error, ldap_set_option "
1443                                         "(%s,SECPROPS,\"%s\") failed!\n",
1444                                         sb->sb_uri.bv_val, sb->sb_secprops, 0 );
1445                                 goto done;
1446                         }
1447                 }
1448
1449                 defaults = lutil_sasl_defaults( ld,
1450                         sb->sb_saslmech.bv_val,
1451                         sb->sb_realm.bv_val,
1452                         sb->sb_authcId.bv_val,
1453                         sb->sb_cred.bv_val,
1454                         sb->sb_authzId.bv_val );
1455
1456                 rc = ldap_sasl_interactive_bind_s( ld,
1457                                 sb->sb_binddn.bv_val,
1458                                 sb->sb_saslmech.bv_val,
1459                                 NULL, NULL,
1460                                 LDAP_SASL_QUIET,
1461                                 lutil_sasl_interact,
1462                                 defaults );
1463
1464                 lutil_sasl_freedefs( defaults );
1465
1466                 /* FIXME: different error behaviors according to
1467                  *      1) return code
1468                  *      2) on err policy : exit, retry, backoff ...
1469                  */
1470                 if ( rc != LDAP_SUCCESS ) {
1471                         static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
1472
1473                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s "
1474                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
1475                                 sb->sb_uri.bv_val, rc, 0 );
1476
1477                         /* FIXME (see above comment) */
1478                         /* if Kerberos credentials cache is not active, retry */
1479                         if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 &&
1480                                 rc == LDAP_LOCAL_ERROR )
1481                         {
1482                                 rc = LDAP_SERVER_DOWN;
1483                         }
1484
1485                         goto done;
1486                 }
1487 #else /* HAVE_CYRUS_SASL */
1488                 /* Should never get here, we trapped this at config time */
1489                 assert(0);
1490                 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
1491                 rc = LDAP_OTHER;
1492                 goto done;
1493 #endif
1494
1495         } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) {
1496                 rc = ldap_sasl_bind_s( ld,
1497                         sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE,
1498                         &sb->sb_cred, NULL, NULL, NULL );
1499                 if ( rc != LDAP_SUCCESS ) {
1500                         Debug( LDAP_DEBUG_ANY, "slap_client_connect: "
1501                                 "URI=%s DN=\"%s\" "
1502                                 "ldap_sasl_bind_s failed (%d)\n",
1503                                 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc );
1504                         goto done;
1505                 }
1506         }
1507
1508 done:;
1509         if ( rc ) {
1510                 if ( ld ) {
1511                         ldap_unbind_ext( ld, NULL, NULL );
1512                         *ldp = NULL;
1513                 }
1514
1515         } else {
1516                 *ldp = ld;
1517         }
1518
1519         return rc;
1520 }
1521
1522 /* -------------------------------------- */
1523
1524
1525 static char *
1526 strtok_quote( char *line, char *sep, char **quote_ptr )
1527 {
1528         int             inquote;
1529         char            *tmp;
1530         static char     *next;
1531
1532         *quote_ptr = NULL;
1533         if ( line != NULL ) {
1534                 next = line;
1535         }
1536         while ( *next && strchr( sep, *next ) ) {
1537                 next++;
1538         }
1539
1540         if ( *next == '\0' ) {
1541                 next = NULL;
1542                 return( NULL );
1543         }
1544         tmp = next;
1545
1546         for ( inquote = 0; *next; ) {
1547                 switch ( *next ) {
1548                 case '"':
1549                         if ( inquote ) {
1550                                 inquote = 0;
1551                         } else {
1552                                 inquote = 1;
1553                         }
1554                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
1555                         break;
1556
1557                 case '\\':
1558                         if ( next[1] )
1559                                 AC_MEMCPY( next,
1560                                             next + 1, strlen( next + 1 ) + 1 );
1561                         next++;         /* dont parse the escaped character */
1562                         break;
1563
1564                 default:
1565                         if ( ! inquote ) {
1566                                 if ( strchr( sep, *next ) != NULL ) {
1567                                         *quote_ptr = next;
1568                                         *next++ = '\0';
1569                                         return( tmp );
1570                                 }
1571                         }
1572                         next++;
1573                         break;
1574                 }
1575         }
1576
1577         return( tmp );
1578 }
1579
1580 static char     buf[AC_LINE_MAX];
1581 static char     *line;
1582 static size_t lmax, lcur;
1583
1584 #define CATLINE( buf ) \
1585         do { \
1586                 size_t len = strlen( buf ); \
1587                 while ( lcur + len + 1 > lmax ) { \
1588                         lmax += AC_LINE_MAX; \
1589                         line = (char *) ch_realloc( line, lmax ); \
1590                 } \
1591                 strcpy( line + lcur, buf ); \
1592                 lcur += len; \
1593         } while( 0 )
1594
1595 static void
1596 fp_getline_init(ConfigArgs *c) {
1597         c->lineno = -1;
1598         buf[0] = '\0';
1599 }
1600
1601 static int
1602 fp_getline( FILE *fp, ConfigArgs *c )
1603 {
1604         char    *p;
1605
1606         lcur = 0;
1607         CATLINE(buf);
1608         c->lineno++;
1609
1610         /* avoid stack of bufs */
1611         if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) {
1612                 buf[0] = '\0';
1613                 c->line = line;
1614                 return(1);
1615         }
1616
1617         while ( fgets( buf, sizeof( buf ), fp ) ) {
1618                 p = strchr( buf, '\n' );
1619                 if ( p ) {
1620                         if ( p > buf && p[-1] == '\r' ) {
1621                                 --p;
1622                         }
1623                         *p = '\0';
1624                 }
1625                 /* XXX ugly */
1626                 c->line = line;
1627                 if ( line[0]
1628                                 && ( p = line + strlen( line ) - 1 )[0] == '\\'
1629                                 && p[-1] != '\\' )
1630                 {
1631                         p[0] = '\0';
1632                         lcur--;
1633                         
1634                 } else {
1635                         if ( !isspace( (unsigned char)buf[0] ) ) {
1636                                 return(1);
1637                         }
1638                         buf[0] = ' ';
1639                 }
1640                 CATLINE(buf);
1641                 c->lineno++;
1642         }
1643
1644         buf[0] = '\0';
1645         c->line = line;
1646         return(line[0] ? 1 : 0);
1647 }
1648
1649 static int
1650 fp_parse_line(ConfigArgs *c)
1651 {
1652         char *token;
1653         static char *const hide[] = {
1654                 "rootpw", "replica", "syncrepl",  /* in slapd */
1655                 "acl-bind", "acl-method", "idassert-bind",  /* in back-ldap */
1656                 "acl-passwd", "bindpw",  /* in back-<ldap/meta> */
1657                 "pseudorootpw",  /* in back-meta */
1658                 "dbpasswd",  /* in back-sql */
1659                 NULL
1660         };
1661         char *quote_ptr;
1662         int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1;
1663
1664         c->tline = ch_strdup(c->line);
1665         token = strtok_quote(c->tline, " \t", &quote_ptr);
1666
1667         if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break;
1668         if(quote_ptr) *quote_ptr = ' ';
1669         Debug(LDAP_DEBUG_CONFIG, "line %d (%s%s)\n", c->lineno,
1670                 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : "");
1671         if(quote_ptr) *quote_ptr = '\0';
1672
1673         for(;; token = strtok_quote(NULL, " \t", &quote_ptr)) {
1674                 if(c->argc >= c->argv_size) {
1675                         char **tmp;
1676                         tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv));
1677                         if(!tmp) {
1678                                 Debug(LDAP_DEBUG_ANY, "line %d: out of memory\n", c->lineno, 0, 0);
1679                                 return -1;
1680                         }
1681                         c->argv = tmp;
1682                         c->argv_size += ARGS_STEP;
1683                 }
1684                 if(token == NULL)
1685                         break;
1686                 c->argv[c->argc++] = token;
1687         }
1688         c->argv[c->argc] = NULL;
1689         return(0);
1690 }
1691
1692 void
1693 config_destroy( )
1694 {
1695         ucdata_unload( UCDATA_ALL );
1696         if ( frontendDB ) {
1697                 /* NOTE: in case of early exit, frontendDB can be NULL */
1698                 if ( frontendDB->be_schemandn.bv_val )
1699                         free( frontendDB->be_schemandn.bv_val );
1700                 if ( frontendDB->be_schemadn.bv_val )
1701                         free( frontendDB->be_schemadn.bv_val );
1702                 if ( frontendDB->be_acl )
1703                         acl_destroy( frontendDB->be_acl, NULL );
1704         }
1705         free( line );
1706         if ( slapd_args_file )
1707                 free ( slapd_args_file );
1708         if ( slapd_pid_file )
1709                 free ( slapd_pid_file );
1710         if ( default_passwd_hash )
1711                 ldap_charray_free( default_passwd_hash );
1712 }
1713
1714 char **
1715 slap_str2clist( char ***out, char *in, const char *brkstr )
1716 {
1717         char    *str;
1718         char    *s;
1719         char    *lasts;
1720         int     i, j;
1721         char    **new;
1722
1723         /* find last element in list */
1724         for (i = 0; *out && (*out)[i]; i++);
1725
1726         /* protect the input string from strtok */
1727         str = ch_strdup( in );
1728
1729         if ( *str == '\0' ) {
1730                 free( str );
1731                 return( *out );
1732         }
1733
1734         /* Count words in string */
1735         j=1;
1736         for ( s = str; *s; s++ ) {
1737                 if ( strchr( brkstr, *s ) != NULL ) {
1738                         j++;
1739                 }
1740         }
1741
1742         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1743         new = *out + i;
1744         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1745                 s != NULL;
1746                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1747         {
1748                 *new = ch_strdup( s );
1749                 new++;
1750         }
1751
1752         *new = NULL;
1753         free( str );
1754         return( *out );
1755 }
1756
1757 int config_generic_wrapper( Backend *be, const char *fname, int lineno,
1758         int argc, char **argv )
1759 {
1760         ConfigArgs c = { 0 };
1761         ConfigTable *ct;
1762         int rc;
1763
1764         c.be = be;
1765         c.fname = fname;
1766         c.lineno = lineno;
1767         c.argc = argc;
1768         c.argv = argv;
1769         c.valx = -1;
1770         c.line = line;
1771         c.op = SLAP_CONFIG_ADD;
1772         snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno );
1773
1774         rc = SLAP_CONF_UNKNOWN;
1775         ct = config_find_keyword( be->be_cf_ocs->co_table, &c );
1776         if ( ct )
1777                 rc = config_add_vals( ct, &c );
1778         return rc;
1779 }