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