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