]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
0faa3a50838d7eabf9549088ac256f5c0f16d933
[openldap] / servers / slapd / bconfig.c
1 /* bconfig.c - the config backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005 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 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Howard Chu for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/errno.h>
27
28 #include "slap.h"
29
30 #ifdef LDAP_SLAPI
31 #include "slapi/slapi.h"
32 #endif
33
34 #include <lutil.h>
35
36 #include "config.h"
37
38 static struct berval config_rdn = BER_BVC("cn=config");
39 static struct berval schema_rdn = BER_BVC("cn=schema");
40
41 #ifdef SLAPD_MODULES
42 typedef struct modpath_s {
43         struct modpath_s *mp_next;
44         struct berval mp_path;
45         BerVarray mp_loads;
46 } ModPaths;
47
48 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
49 #endif
50
51 typedef struct ConfigFile {
52         struct ConfigFile *c_sibs;
53         struct ConfigFile *c_kids;
54         struct berval c_file;
55         AttributeType *c_at_head, *c_at_tail;
56         ContentRule *c_cr_head, *c_cr_tail;
57         ObjectClass *c_oc_head, *c_oc_tail;
58         OidMacro *c_om_head, *c_om_tail;
59         BerVarray c_dseFiles;
60 } ConfigFile;
61
62 typedef struct CfOcInfo {
63         struct berval *co_name;
64         ConfigTable *co_table;
65         ConfigType co_type;
66 } CfOcInfo;
67
68 typedef struct CfEntryInfo {
69         struct CfEntryInfo *ce_parent;
70         struct CfEntryInfo *ce_sibs;
71         struct CfEntryInfo *ce_kids;
72         Entry *ce_entry;
73         ConfigType ce_type;
74         BackendInfo *ce_bi;
75         BackendDB *ce_be;
76 } CfEntryInfo;
77
78 typedef struct {
79         ConfigFile *cb_config;
80         CfEntryInfo *cb_root;
81         BackendDB       *cb_be; /* config backend */
82         BackendDB       cb_db;  /* underlying database */
83         int             cb_got_ldif;
84 } CfBackInfo;
85
86 /* These do nothing in slapd, they're kept only to make them
87  * editable here.
88  */
89 static char *replica_pidFile, *replica_argsFile;
90 static int replicationInterval;
91
92 static char     *passwd_salt;
93 static char     *logfileName;
94 static BerVarray authz_rewrites;
95
96 static struct berval cfdir;
97
98 /* Private state */
99 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
100         *cfAd_include;
101
102 static ObjectClass *cfOc_schema, *cfOc_global, *cfOc_backend, *cfOc_database,
103         *cfOc_include, *cfOc_overlay, *cfOc_module;
104
105 static ConfigFile cf_prv, *cfn = &cf_prv;
106
107 static Avlnode *CfOcTree;
108
109 static int add_syncrepl LDAP_P(( Backend *, char **, int ));
110 static int parse_syncrepl_line LDAP_P(( char **, int, syncinfo_t *));
111 static void syncrepl_unparse LDAP_P (( syncinfo_t *, struct berval *));
112 static int config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs );
113
114 static ConfigDriver config_fname;
115 static ConfigDriver config_cfdir;
116 static ConfigDriver config_generic;
117 static ConfigDriver config_search_base;
118 static ConfigDriver config_passwd_hash;
119 static ConfigDriver config_schema_dn;
120 static ConfigDriver config_sizelimit;
121 static ConfigDriver config_timelimit;
122 static ConfigDriver config_limits; 
123 static ConfigDriver config_overlay;
124 static ConfigDriver config_suffix; 
125 static ConfigDriver config_deref_depth;
126 static ConfigDriver config_rootdn;
127 static ConfigDriver config_rootpw;
128 static ConfigDriver config_restrict;
129 static ConfigDriver config_allows;
130 static ConfigDriver config_disallows;
131 static ConfigDriver config_requires;
132 static ConfigDriver config_security;
133 static ConfigDriver config_referral;
134 static ConfigDriver config_loglevel;
135 static ConfigDriver config_syncrepl;
136 static ConfigDriver config_replica;
137 static ConfigDriver config_updatedn;
138 static ConfigDriver config_updateref;
139 static ConfigDriver config_include;
140 #ifdef HAVE_TLS
141 static ConfigDriver config_tls_option;
142 static ConfigDriver config_tls_config;
143 #endif
144
145 enum {
146         CFG_ACL = 1,
147         CFG_BACKEND,
148         CFG_DATABASE,
149         CFG_TLS_RAND,
150         CFG_TLS_CIPHER,
151         CFG_TLS_CERT_FILE,
152         CFG_TLS_CERT_KEY,
153         CFG_TLS_CA_PATH,
154         CFG_TLS_CA_FILE,
155         CFG_TLS_VERIFY,
156         CFG_TLS_CRLCHECK,
157         CFG_SIZE,
158         CFG_TIME,
159         CFG_CONCUR,
160         CFG_THREADS,
161         CFG_SALT,
162         CFG_LIMITS,
163         CFG_RO,
164         CFG_REWRITE,
165         CFG_DEPTH,
166         CFG_OID,
167         CFG_OC,
168         CFG_DIT,
169         CFG_ATTR,
170         CFG_ATOPT,
171         CFG_CHECK,
172         CFG_AUDITLOG,
173         CFG_REPLOG,
174         CFG_ROOTDSE,
175         CFG_LOGFILE,
176         CFG_PLUGIN,
177         CFG_MODLOAD,
178         CFG_MODPATH,
179         CFG_LASTMOD,
180         CFG_AZPOLICY,
181         CFG_AZREGEXP,
182         CFG_SASLSECP,
183         CFG_SSTR_IF_MAX,
184         CFG_SSTR_IF_MIN,
185 };
186
187 typedef struct {
188         char *name, *oid;
189 } OidRec;
190
191 static OidRec OidMacros[] = {
192         /* OpenLDAProot:666.11.1 */
193         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
194         { "OLcfgAt", "OLcfg:3" },
195         { "OLcfgOc", "OLcfg:4" },
196         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
197         { "OMsInteger", "OMsyn:2" },
198         { "OMsBoolean", "OMsyn:7" },
199         { "OMsDN", "OMsyn:12" },
200         { "OMsDirectoryString", "OMsyn:15" },
201         { "OMsOctetString", "OMsyn:40" },
202         { NULL, NULL }
203 };
204
205 /* alphabetical ordering */
206
207 ConfigTable config_back_cf_table[] = {
208         /* This attr is read-only */
209         { "", "", 0, 0, 0, ARG_MAGIC,
210                 &config_fname, "( OLcfgAt:78 NAME 'olcConfigFile' "
211                         "DESC 'File for slapd configuration directives' "
212                         "EQUALITY caseIgnoreMatch "
213                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
214         { "", "", 0, 0, 0, ARG_MAGIC,
215                 &config_cfdir, "( OLcfgAt:79 NAME 'olcConfigDir' "
216                         "DESC 'Directory for slapd configuration backend' "
217                         "EQUALITY caseIgnoreMatch "
218                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
219         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
220                 &config_generic, "( OLcfgAt:1 NAME 'olcAccess' "
221                         "DESC 'Access Control List' "
222                         "EQUALITY caseIgnoreMatch "
223                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
224         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
225                 &config_allows, "( OLcfgAt:2 NAME 'olcAllows' "
226                         "DESC 'Allowed set of deprecated features' "
227                         "EQUALITY caseIgnoreMatch "
228                         "SYNTAX OMsDirectoryString )", NULL, NULL },
229         { "argsfile", "file", 2, 2, 0, ARG_STRING,
230                 &slapd_args_file, "( OLcfgAt:3 NAME 'olcArgsFile' "
231                         "DESC 'File for slapd command line options' "
232                         "EQUALITY caseIgnoreMatch "
233                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
234         { "attribute",  "attribute", 2, 0, 9, ARG_PAREN|ARG_MAGIC|CFG_ATTR,
235                 &config_generic, "( OLcfgAt:4 NAME 'olcAttributeTypes' "
236                         "DESC 'OpenLDAP attributeTypes' "
237                         "EQUALITY caseIgnoreMatch "
238                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
239                                 NULL, NULL },
240         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
241                 &config_generic, "( OLcfgAt:5 NAME 'olcAttributeOptions' "
242                         "EQUALITY caseIgnoreMatch "
243                         "SYNTAX OMsDirectoryString )", NULL, NULL },
244         { "authid-rewrite", NULL, 2, 0, 0,
245 #ifdef SLAP_AUTH_REWRITE
246                 ARG_MAGIC|CFG_REWRITE, &config_generic,
247 #else
248                 ARG_IGNORED, NULL,
249 #endif
250                  "( OLcfgAt:6 NAME 'olcAuthIDRewrite' "
251                         "EQUALITY caseIgnoreMatch "
252                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
253         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
254                 &config_generic, "( OLcfgAt:7 NAME 'olcAuthzPolicy' "
255                         "EQUALITY caseIgnoreMatch "
256                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
257         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
258                 &config_generic, "( OLcfgAt:8 NAME 'olcAuthzRegexp' "
259                         "EQUALITY caseIgnoreMatch "
260                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
261         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
262                 &config_generic, "( OLcfgAt:9 NAME 'olcBackend' "
263                         "DESC 'A type of backend' "
264                         "EQUALITY caseIgnoreMatch "
265                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
266         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
267                 &config_generic, "( OLcfgAt:10 NAME 'olcConcurrency' "
268                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
269         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
270                 &slap_conn_max_pending, "( OLcfgAt:11 NAME 'olcConnMaxPending' "
271                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
272         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
273                 &slap_conn_max_pending_auth, "( OLcfgAt:12 NAME 'olcConnMaxPendingAuth' "
274                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
275         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
276                 &config_generic, "( OLcfgAt:13 NAME 'olcDatabase' "
277                         "DESC 'The backend type for a database instance' "
278                         "SUP olcBackend X-ORDERED 'VALUES' )", NULL, NULL },
279         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_MAGIC,
280                 &config_search_base, "( OLcfgAt:14 NAME 'olcDefaultSearchBase' "
281                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
282         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
283                 &config_disallows, "( OLcfgAt:15 NAME 'olcDisallows' "
284                         "EQUALITY caseIgnoreMatch "
285                         "SYNTAX OMsDirectoryString )", NULL, NULL },
286         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT,
287                 &config_generic, "( OLcfgAt:16 NAME 'olcDitContentRules' "
288                         "DESC 'OpenLDAP DIT content rules' "
289                         "EQUALITY caseIgnoreMatch "
290                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
291                         NULL, NULL },
292         { "gentlehup", "on|off", 2, 2, 0,
293 #ifdef SIGHUP
294                 ARG_ON_OFF, &global_gentlehup,
295 #else
296                 ARG_IGNORED, NULL,
297 #endif
298                 "( OLcfgAt:17 NAME 'olcGentleHUP' "
299                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
300         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
301                 &global_idletimeout, "( OLcfgAt:18 NAME 'olcIdleTimeout' "
302                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
303 /* XXX -- special case? */
304         { "include", "file", 2, 2, 0, ARG_MAGIC,
305                 &config_include, "( OLcfgAt:19 NAME 'olcInclude' "
306                         "SUP labeledURI )", NULL, NULL },
307         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
308                 &config_generic, "( OLcfgAt:20 NAME 'olcIndexSubstrIfMinLen' "
309                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
310         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
311                 &config_generic, "( OLcfgAt:21 NAME 'olcIndexSubstrIfMaxLen' "
312                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
313         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
314                 &index_substr_any_len, "( OLcfgAt:22 NAME 'olcIndexSubstrAnyLen' "
315                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
316         { "index_substr_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
317                 &index_substr_any_step, "( OLcfgAt:23 NAME 'olcIndexSubstrAnyStep' "
318                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
319         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
320                 &config_generic, "( OLcfgAt:24 NAME 'olcLastMod' "
321                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
322         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
323                 &config_generic, "( OLcfgAt:25 NAME 'olcLimits' "
324                         "SYNTAX OMsDirectoryString )", NULL, NULL },
325         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
326                 &local_ssf, "( OLcfgAt:26 NAME 'olcLocalSSF' "
327                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
328         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
329                 &config_generic, "( OLcfgAt:27 NAME 'olcLogFile' "
330                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
331         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
332                 &config_loglevel, "( OLcfgAt:28 NAME 'olcLogLevel' "
333                         "SYNTAX OMsDirectoryString )", NULL, NULL },
334         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
335                 &config_generic, "( OLcfgAt:29 NAME 'olcMaxDerefDepth' "
336                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
337         { "moduleload", "file", 2, 0, 0,
338 #ifdef SLAPD_MODULES
339                 ARG_MAGIC|CFG_MODLOAD, &config_generic,
340 #else
341                 ARG_IGNORED, NULL,
342 #endif
343                 "( OLcfgAt:30 NAME 'olcModuleLoad' "
344                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
345         { "modulepath", "path", 2, 2, 0,
346 #ifdef SLAPD_MODULES
347                 ARG_MAGIC|CFG_MODPATH, &config_generic,
348 #else
349                 ARG_IGNORED, NULL,
350 #endif
351                 "( OLcfgAt:31 NAME 'olcModulePath' "
352                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
353         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
354                 &config_generic, "( OLcfgAt:32 NAME 'olcObjectClasses' "
355                 "DESC 'OpenLDAP object classes' "
356                 "EQUALITY caseIgnoreMatch "
357                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
358                         NULL, NULL },
359         { "objectidentifier", NULL,     0, 0, 0, ARG_MAGIC|CFG_OID,
360                 &config_generic, "( OLcfgAt:33 NAME 'olcObjectIdentifier' "
361                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
362         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
363                 &config_overlay, "( OLcfgAt:34 NAME 'olcOverlay' "
364                         "SUP olcDatabase X-ORDERED 'VALUES' )", NULL, NULL },
365         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
366                 &config_generic, "( OLcfgAt:35 NAME 'olcPasswordCryptSaltFormat' "
367                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
368         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
369                 &config_passwd_hash, "( OLcfgAt:36 NAME 'olcPasswordHash' "
370                         "SYNTAX OMsDirectoryString )", NULL, NULL },
371         { "pidfile", "file", 2, 2, 0, ARG_STRING,
372                 &slapd_pid_file, "( OLcfgAt:37 NAME 'olcPidFile' "
373                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
374         { "plugin", NULL, 0, 0, 0,
375 #ifdef LDAP_SLAPI
376                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
377 #else
378                 ARG_IGNORED, NULL,
379 #endif
380                 "( OLcfgAt:38 NAME 'olcPlugin' "
381                         "SYNTAX OMsDirectoryString )", NULL, NULL },
382         { "pluginlog", "filename", 2, 2, 0,
383 #ifdef LDAP_SLAPI
384                 ARG_STRING, &slapi_log_file,
385 #else
386                 ARG_IGNORED, NULL,
387 #endif
388                 "( OLcfgAt:39 NAME 'olcPluginLogFile' "
389                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
390         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
391                 &config_generic, "( OLcfgAt:40 NAME 'olcReadOnly' "
392                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
393         { "referral", "url", 2, 2, 0, ARG_MAGIC,
394                 &config_referral, "( OLcfgAt:41 NAME 'olcReferral' "
395                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
396         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
397                 &config_replica, "( OLcfgAt:42 NAME 'olcReplica' "
398                         "SUP labeledURI )", NULL, NULL },
399         { "replica-argsfile", NULL, 0, 0, 0, ARG_STRING,
400                 &replica_argsFile, "( OLcfgAt:43 NAME 'olcReplicaArgsFile' "
401                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
402         { "replica-pidfile", NULL, 0, 0, 0, ARG_STRING,
403                 &replica_pidFile, "( OLcfgAt:44 NAME 'olcReplicaPidFile' "
404                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
405         { "replicationInterval", NULL, 0, 0, 0, ARG_INT,
406                 &replicationInterval, "( OLcfgAt:45 NAME 'olcReplicationInterval' "
407                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
408         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC|ARG_STRING|CFG_REPLOG,
409                 &config_generic, "( OLcfgAt:46 NAME 'olcReplogFile' "
410                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
411         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
412                 &config_requires, "( OLcfgAt:47 NAME 'olcRequires' "
413                         "SYNTAX OMsDirectoryString )", NULL, NULL },
414         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
415                 &config_restrict, "( OLcfgAt:48 NAME 'olcRestrict' "
416                         "SYNTAX OMsDirectoryString )", NULL, NULL },
417         { "reverse-lookup", "on|off", 2, 2, 0,
418 #ifdef SLAPD_RLOOKUPS
419                 ARG_ON_OFF, &use_reverse_lookup,
420 #else
421                 ARG_IGNORED, NULL,
422 #endif
423                 "( OLcfgAt:49 NAME 'olcReverseLookup' "
424                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
425         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
426                 &config_rootdn, "( OLcfgAt:50 NAME 'olcRootDN' "
427                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
428         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
429                 &config_generic, "( OLcfgAt:51 NAME 'olcRootDSE' "
430                         "SYNTAX OMsDirectoryString )", NULL, NULL },
431         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
432                 &config_rootpw, "( OLcfgAt:52 NAME 'olcRootPW' "
433                         "SYNTAX OMsOctetString SINGLE-VALUE )", NULL, NULL },
434         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
435                 &config_generic, NULL, NULL, NULL },
436         { "sasl-host", "host", 2, 2, 0,
437 #ifdef HAVE_CYRUS_SASL
438                 ARG_STRING|ARG_UNIQUE, &global_host,
439 #else
440                 ARG_IGNORED, NULL,
441 #endif
442                 "( OLcfgAt:53 NAME 'olcSaslHost' "
443                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
444         { "sasl-realm", "realm", 2, 2, 0,
445 #ifdef HAVE_CYRUS_SASL
446                 ARG_STRING|ARG_UNIQUE, &global_realm,
447 #else
448                 ARG_IGNORED, NULL,
449 #endif
450                 "( OLcfgAt:54 NAME 'olcSaslRealm' "
451                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
452         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
453                 &config_generic, NULL, NULL, NULL },
454         { "sasl-secprops", "properties", 2, 2, 0,
455 #ifdef HAVE_CYRUS_SASL
456                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
457 #else
458                 ARG_IGNORED, NULL,
459 #endif
460                 "( OLcfgAt:56 NAME 'olcSaslSecProps' "
461                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
462         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
463                 &config_generic, NULL, NULL, NULL },
464         { "schemacheck", "on|off", 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|CFG_CHECK,
465                 &config_generic, "( OLcfgAt:57 NAME 'olcSchemaCheck' "
466                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
467         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_MAGIC,
468                 &config_schema_dn, "( OLcfgAt:58 NAME 'olcSchemaDN' "
469                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
470         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
471                 &config_security, "( OLcfgAt:59 NAME 'olcSecurity' "
472                         "SYNTAX OMsDirectoryString )", NULL, NULL },
473         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_SIZE,
474                 &config_sizelimit, "( OLcfgAt:60 NAME 'olcSizeLimit' "
475                         "SYNTAX OMsInteger )", NULL, NULL },
476         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
477                 &sockbuf_max_incoming, "( OLcfgAt:61 NAME 'olcSockbufMaxIncoming' "
478                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
479         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
480                 &sockbuf_max_incoming_auth, "( OLcfgAt:62 NAME 'olcSockbufMaxIncomingAuth' "
481                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
482         { "srvtab", "file", 2, 2, 0,
483 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
484                 ARG_STRING, &ldap_srvtab,
485 #else
486                 ARG_IGNORED, NULL,
487 #endif
488                 "( OLcfgAt:63 NAME 'olcSrvtab' "
489                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
490         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_MAGIC,
491                 &config_suffix, "( OLcfgAt:64 NAME 'olcSuffix' "
492                         "SYNTAX OMsDN )", NULL, NULL },
493         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
494                 &config_syncrepl, "( OLcfgAt:65 NAME 'olcSyncrepl' "
495                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
496         { "threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_THREADS,
497                 &config_generic, "( OLcfgAt:66 NAME 'olcThreads' "
498                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
499         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_TIME,
500                 &config_timelimit, "( OLcfgAt:67 NAME 'olcTimeLimit' "
501                         "SYNTAX OMsInteger )", NULL, NULL },
502         { "TLSCACertificateFile", NULL, 0, 0, 0,
503 #ifdef HAVE_TLS
504                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
505 #else
506                 ARG_IGNORED, NULL,
507 #endif
508                 "( OLcfgAt:68 NAME 'olcTLSCACertificateFile' "
509                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
510         { "TLSCACertificatePath", NULL, 0, 0, 0,
511 #ifdef HAVE_TLS
512                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
513 #else
514                 ARG_IGNORED, NULL,
515 #endif
516                 "( OLcfgAt:69 NAME 'olcTLSCACertificatePath' "
517                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
518         { "TLSCertificateFile", NULL, 0, 0, 0,
519 #ifdef HAVE_TLS
520                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
521 #else
522                 ARG_IGNORED, NULL,
523 #endif
524                 "( OLcfgAt:70 NAME 'olcTLSCertificateFile' "
525                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
526         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
527 #ifdef HAVE_TLS
528                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
529 #else
530                 ARG_IGNORED, NULL,
531 #endif
532                 "( OLcfgAt:71 NAME 'olcTLSCertificateKeyFile' "
533                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
534         { "TLSCipherSuite",     NULL, 0, 0, 0,
535 #ifdef HAVE_TLS
536                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
537 #else
538                 ARG_IGNORED, NULL,
539 #endif
540                 "( OLcfgAt:72 NAME 'olcTLSCipherSuite' "
541                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
542         { "TLSCRLCheck", NULL, 0, 0, 0,
543 #ifdef HAVE_TLS
544                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
545 #else
546                 ARG_IGNORED, NULL,
547 #endif
548                 "( OLcfgAt:73 NAME 'olcTLSCRLCheck' "
549                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
550         { "TLSRandFile", NULL, 0, 0, 0,
551 #ifdef HAVE_TLS
552                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
553 #else
554                 ARG_IGNORED, NULL,
555 #endif
556                 "( OLcfgAt:74 NAME 'olcTLSRandFile' "
557                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
558         { "TLSVerifyClient", NULL, 0, 0, 0,
559 #ifdef HAVE_TLS
560                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
561 #else
562                 ARG_IGNORED, NULL,
563 #endif
564                 "( OLcfgAt:75 NAME 'olcTLSVerifyClient' "
565                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
566         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
567                 NULL, NULL, NULL, NULL },
568         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_MAGIC,
569                 &config_updatedn, "( OLcfgAt:76 NAME 'olcUpdateDN' "
570                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
571         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
572                 &config_updateref, "( OLcfgAt:77 NAME 'olcUpdateRef' "
573                         "SUP labeledURI )", NULL, NULL },
574         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
575                 NULL, NULL, NULL, NULL }
576 };
577
578 static ConfigOCs cf_ocs[] = {
579         { "( OLcfgOc:1 "
580                 "NAME 'olcConfig' "
581                 "DESC 'OpenLDAP configuration object' "
582                 "ABSTRACT SUP top "
583                 "MAY cn )", Cft_Abstract, NULL },
584         { "( OLcfgOc:2 "
585                 "NAME 'olcGlobal' "
586                 "DESC 'OpenLDAP Global configuration options' "
587                 "SUP olcConfig STRUCTURAL "
588                 "MAY ( olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
589                  "olcAttributeOptions $ olcAttributeTypes $ olcAuthIDRewrite $ "
590                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
591                  "olcConnMaxPending $ olcConnMaxPendingAuth $ olcDefaultSearchBase $ "
592                  "olcDisallows $ olcDitContentRules $ olcGentleHUP $ olcIdleTimeout $ "
593                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
594                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
595                  "olcLogLevel $ olcModulePath $ olcObjectClasses $ "
596                  "olcObjectIdentifier $ "
597                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
598                  "olcPlugin $ olcPluginLogFile $ olcReadOnly $ olcReferral $ "
599                  "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
600                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
601                  "olcRootDSE $ olcRootPW $ "
602                  "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
603                  "olcSchemaCheck $ olcSchemaDN $ olcSecurity $ olcSizeLimit $ "
604                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
605                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
606                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
607                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
608                  "olcTLSRandFile $ olcTLSVerifyClient ) )", Cft_Global, &cfOc_global },
609         { "( OLcfgOc:3 "
610                 "NAME 'olcSchemaConfig' "
611                 "DESC 'OpenLDAP schema object' "
612                 "SUP olcConfig STRUCTURAL "
613                 "MAY ( olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
614                  "olcDitContentRules ) )", Cft_Schema, &cfOc_schema },
615         { "( OLcfgOc:4 "
616                 "NAME 'olcBackendConfig' "
617                 "DESC 'OpenLDAP Backend-specific options' "
618                 "SUP olcConfig STRUCTURAL "
619                 "MAY ( olcBackend ) )", Cft_Backend, &cfOc_backend },
620         { "( OLcfgOc:5 "
621                 "NAME 'olcDatabaseConfig' "
622                 "DESC 'OpenLDAP Database-specific options' "
623                 "SUP olcConfig STRUCTURAL "
624                 "MAY ( olcDatabase $ olcAccess $ olcLastMod $ olcLimits $ "
625                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
626                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
627                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSuffix $ olcSyncrepl $ "
628                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef ) )",
629                         Cft_Database, &cfOc_database },
630         { "( OLcfgOc:6 "
631                 "NAME 'olcOverlayConfig' "
632                 "DESC 'OpenLDAP Overlay-specific options' "
633                 "SUP olcConfig STRUCTURAL "
634                 "MAY ( olcOverlay ) )", Cft_Overlay, &cfOc_overlay },
635         { "( OLcfgOc:7 "
636                 "NAME 'olcIncludeFile' "
637                 "DESC 'OpenLDAP configuration include file' "
638                 "SUP olcConfig STRUCTURAL "
639                 "MAY ( olcInclude $ olcConfigFile $ olcRootDSE ) )",
640                 Cft_Include, &cfOc_include },
641 #ifdef SLAPD_MODULES
642         { "( OLcfgOc:8 "
643                 "NAME 'olcModuleList' "
644                 "DESC 'OpenLDAP dynamic module info' "
645                 "SUP olcConfig STRUCTURAL "
646                 "MUST olcModuleLoad  )",
647                 Cft_Module, &cfOc_module },
648 #endif
649         { NULL, 0, NULL }
650 };
651
652 static int
653 config_generic(ConfigArgs *c) {
654         char *p;
655         int i;
656
657         if ( c->op == SLAP_CONFIG_EMIT ) {
658                 int rc = 0;
659                 switch(c->type) {
660                 case CFG_CONCUR:
661                         c->value_int = ldap_pvt_thread_get_concurrency();
662                         break;
663                 case CFG_THREADS:
664                         c->value_int = connection_pool_max;
665                         break;
666                 case CFG_SALT:
667                         if ( passwd_salt )
668                                 c->value_string = ch_strdup( passwd_salt );
669                         else
670                                 rc = 1;
671                         break;
672                 case CFG_LIMITS:
673                         if ( c->be->be_limits ) {
674                                 char buf[4096*3];
675                                 struct berval bv;
676                                 int i;
677
678                                 for ( i=0; c->be->be_limits[i]; i++ ) {
679                                         bv.bv_len = sprintf( buf, "{%d}", i );
680                                         bv.bv_val = buf+bv.bv_len;
681                                         limits_unparse( c->be->be_limits[i], &bv );
682                                         bv.bv_len += bv.bv_val - buf;
683                                         bv.bv_val = buf;
684                                         value_add_one( &c->rvalue_vals, &bv );
685                                 }
686                         }
687                         if ( !c->rvalue_vals ) rc = 1;
688                         break;
689                 case CFG_RO:
690                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) != 0;
691                         break;
692                 case CFG_AZPOLICY:
693                         c->value_string = ch_strdup( slap_sasl_getpolicy());
694                         break;
695                 case CFG_AZREGEXP:
696                         slap_sasl_regexp_unparse( &c->rvalue_vals );
697                         if ( !c->rvalue_vals ) rc = 1;
698                         break;
699 #ifdef HAVE_CYRUS_SASL
700                 case CFG_SASLSECP: {
701                         struct berval bv = BER_BVNULL;
702                         slap_sasl_secprops_unparse( &bv );
703                         if ( !BER_BVISNULL( &bv )) {
704                                 ber_bvarray_add( &c->rvalue_vals, &bv );
705                         } else {
706                                 rc = 1;
707                         }
708                         }
709                         break;
710 #endif
711                 case CFG_DEPTH:
712                         c->value_int = c->be->be_max_deref_depth;
713                         break;
714                 case CFG_OID: {
715                         ConfigFile *cf = c->private;
716                         if ( !cf )
717                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
718                         else if ( cf->c_om_head )
719                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
720                                         cf->c_om_tail, 0 );
721                         if ( !c->rvalue_vals )
722                                 rc = 1;
723                         }
724                         break;
725                 case CFG_OC: {
726                         ConfigFile *cf = c->private;
727                         if ( !cf )
728                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
729                         else if ( cf->c_oc_head )
730                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
731                                         cf->c_oc_tail, 0 );
732                         if ( !c->rvalue_vals )
733                                 rc = 1;
734                         }
735                         break;
736                 case CFG_ATTR: {
737                         ConfigFile *cf = c->private;
738                         if ( !cf )
739                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
740                         else if ( cf->c_at_head )
741                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
742                                         cf->c_at_tail, 0 );
743                         if ( !c->rvalue_vals )
744                                 rc = 1;
745                         }
746                         break;
747                 case CFG_DIT: {
748                         ConfigFile *cf = c->private;
749                         if ( !cf )
750                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
751                         else if ( cf->c_cr_head )
752                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
753                                         cf->c_cr_tail, 0 );
754                         if ( !c->rvalue_vals )
755                                 rc = 1;
756                         }
757                         break;
758                         
759                 case CFG_CHECK:
760                         c->value_int = global_schemacheck;
761                         break;
762                 case CFG_ACL: {
763                         AccessControl *a;
764                         char *src, *dst, ibuf[11];
765                         struct berval bv, abv;
766                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
767                                 abv.bv_len = sprintf( ibuf, "{%x}", i );
768                                 acl_unparse( a, &bv );
769                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
770                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
771                                 /* Turn TAB / EOL into plain space */
772                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
773                                         if (isspace(*src)) *dst++ = ' ';
774                                         else *dst++ = *src;
775                                 }
776                                 *dst = '\0';
777                                 if (dst[-1] == ' ') {
778                                         dst--;
779                                         *dst = '\0';
780                                 }
781                                 abv.bv_len = dst - abv.bv_val;
782                                 ber_bvarray_add( &c->rvalue_vals, &abv );
783                         }
784                         rc = (!i);
785                         break;
786                 }
787                 case CFG_REPLOG:
788                         if ( c->be->be_replogfile )
789                                 c->value_string = ch_strdup( c->be->be_replogfile );
790                         break;
791                 case CFG_ROOTDSE: {
792                         ConfigFile *cf = c->private;
793                         if ( cf->c_dseFiles ) {
794                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
795                         } else {
796                                 rc = 1;
797                         }
798                         }
799                         break;
800                 case CFG_LOGFILE:
801                         if ( logfileName )
802                                 c->value_string = ch_strdup( logfileName );
803                         else
804                                 rc = 1;
805                         break;
806                 case CFG_LASTMOD:
807                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
808                         break;
809                 case CFG_SSTR_IF_MAX:
810                         c->value_int = index_substr_if_maxlen;
811                         break;
812                 case CFG_SSTR_IF_MIN:
813                         c->value_int = index_substr_if_minlen;
814                         break;
815 #ifdef SLAPD_MODULES
816                 case CFG_MODLOAD: {
817                         ModPaths *mp = c->private;
818                         if (mp->mp_loads) {
819                                 int i;
820                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
821                                         struct berval bv;
822                                         bv.bv_val = c->log;
823                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
824                                                 mp->mp_loads[i].bv_val );
825                                         value_add_one( &c->rvalue_vals, &bv );
826                                 }
827                         }
828
829                         rc = c->rvalue_vals ? 0 : 1;
830                         }
831                         break;
832                 case CFG_MODPATH: {
833                         ModPaths *mp;
834                         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
835                                 struct berval bv;
836                                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
837                                         continue;
838                                 bv.bv_val = c->log;
839                                 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
840                                         mp->mp_path.bv_val );
841                                 value_add_one( &c->rvalue_vals, &bv );
842                         }
843                         rc = c->rvalue_vals ? 0 : 1;
844                         }
845                         break;
846 #endif
847 #ifdef LDAP_SLAPI
848                 case CFG_PLUGIN:
849                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
850                         if ( !c->rvalue_vals ) rc = 1;
851                         break;
852 #endif
853 #ifdef SLAP_AUTH_REWRITE
854                 case CFG_REWRITE:
855                         if ( authz_rewrites ) {
856                                 struct berval bv, idx;
857                                 char ibuf[32];
858                                 int i;
859
860                                 idx.bv_val = ibuf;
861                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
862                                         idx.bv_len = sprintf( idx.bv_val, "{%d}", i );
863                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
864                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
865                                         strcpy( bv.bv_val, idx.bv_val );
866                                         strcpy( bv.bv_val+idx.bv_len, authz_rewrites[i].bv_val );
867                                         ber_bvarray_add( &c->rvalue_vals, &bv );
868                                 }
869                         }
870                         if ( !c->rvalue_vals ) rc = 1;
871                         break;
872 #endif
873                 default:
874                         rc = 1;
875                 }
876                 return rc;
877         }
878
879         p = strchr(c->line,'(' /*')'*/);
880         switch(c->type) {
881                 case CFG_BACKEND:
882                         if(!(c->bi = backend_info(c->argv[1]))) {
883                                 Debug(LDAP_DEBUG_ANY, "%s: "
884                                         "backend %s failed init!\n", c->log, c->argv[1], 0);
885                                 return(1);
886                         }
887                         break;
888
889                 case CFG_DATABASE:
890                         c->bi = NULL;
891                         /* NOTE: config is always the first backend!
892                          */
893                         if ( !strcasecmp( c->argv[1], "config" )) {
894                                 c->be = backendDB;
895                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
896                                 c->be = frontendDB;
897                         } else if(!(c->be = backend_db_init(c->argv[1]))) {
898                                 Debug(LDAP_DEBUG_ANY, "%s: "
899                                         "database %s failed init!\n", c->log, c->argv[1], 0);
900                                 return(1);
901                         }
902                         break;
903
904                 case CFG_CONCUR:
905                         ldap_pvt_thread_set_concurrency(c->value_int);
906                         break;
907
908                 case CFG_THREADS:
909                         ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
910                         connection_pool_max = c->value_int;     /* save for reference */
911                         break;
912
913                 case CFG_SALT:
914                         if ( passwd_salt ) ch_free( passwd_salt );
915                         passwd_salt = c->value_string;
916                         lutil_salt_format(passwd_salt);
917                         break;
918
919                 case CFG_LIMITS:
920                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
921                                 return(1);
922                         break;
923
924                 case CFG_RO:
925                         if(c->value_int)
926                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
927                         else
928                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
929                         break;
930
931                 case CFG_AZPOLICY:
932                         ch_free(c->value_string);
933                         if (slap_sasl_setpolicy( c->argv[1] )) {
934                                 Debug(LDAP_DEBUG_ANY, "%s: unable to parse value \"%s\" in"
935                                         " \"authz-policy <policy>\"\n",
936                                         c->log, c->argv[1], 0 );
937                                 return(1);
938                         }
939                         break;
940                 
941                 case CFG_AZREGEXP:
942                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
943                                 return(1);
944                         break;
945                                 
946 #ifdef HAVE_CYRUS_SASL
947                 case CFG_SASLSECP:
948                         {
949                         char *txt = slap_sasl_secprops( c->argv[1] );
950                         if ( txt ) {
951                                 Debug(LDAP_DEBUG_ANY, "%s: sasl-secprops: %s\n",
952                                         c->log, txt, 0 );
953                                 return(1);
954                         }
955                         break;
956                         }
957 #endif
958
959                 case CFG_DEPTH:
960                         c->be->be_max_deref_depth = c->value_int;
961                         break;
962
963                 case CFG_OID: {
964                         OidMacro *om;
965
966                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1, &om))
967                                 return(1);
968                         if (!cfn->c_om_head) cfn->c_om_head = om;
969                         cfn->c_om_tail = om;
970                         }
971                         break;
972
973                 case CFG_OC: {
974                         ObjectClass *oc;
975
976                         if(parse_oc(c->fname, c->lineno, p, c->argv, &oc)) return(1);
977                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
978                         cfn->c_oc_tail = oc;
979                         }
980                         break;
981
982                 case CFG_DIT: {
983                         ContentRule *cr;
984
985                         if(parse_cr(c->fname, c->lineno, p, c->argv, &cr)) return(1);
986                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
987                         cfn->c_cr_tail = cr;
988                         }
989                         break;
990
991                 case CFG_ATTR: {
992                         AttributeType *at;
993
994                         if(parse_at(c->fname, c->lineno, p, c->argv, &at)) return(1);
995                         if (!cfn->c_at_head) cfn->c_at_head = at;
996                         cfn->c_at_tail = at;
997                         }
998                         break;
999
1000                 case CFG_ATOPT:
1001                         ad_define_option(NULL, NULL, 0);
1002                         for(i = 1; i < c->argc; i++)
1003                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1004                                         return(1);
1005                         break;
1006
1007                 case CFG_CHECK:
1008                         global_schemacheck = c->value_int;
1009                         if(!global_schemacheck) Debug(LDAP_DEBUG_ANY, "%s: "
1010                                 "schema checking disabled! your mileage may vary!\n",
1011                                 c->log, 0, 0);
1012                         break;
1013
1014                 case CFG_ACL:
1015                         parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv);
1016                         break;
1017
1018                 case CFG_REPLOG:
1019                         if(SLAP_MONITOR(c->be)) {
1020                                 Debug(LDAP_DEBUG_ANY, "%s: "
1021                                         "\"replogfile\" should not be used "
1022                                         "inside monitor database\n",
1023                                         c->log, 0, 0);
1024                                 return(0);      /* FIXME: should this be an error? */
1025                         }
1026
1027                         c->be->be_replogfile = c->value_string;
1028                         break;
1029
1030                 case CFG_ROOTDSE:
1031                         if(read_root_dse_file(c->argv[1])) {
1032                                 Debug(LDAP_DEBUG_ANY, "%s: "
1033                                         "could not read \"rootDSE <filename>\" line\n",
1034                                         c->log, 0, 0);
1035                                 return(1);
1036                         }
1037                         {
1038                                 struct berval bv;
1039                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1040                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1041                         }
1042                         break;
1043
1044                 case CFG_LOGFILE: {
1045                                 FILE *logfile;
1046                                 if ( logfileName ) ch_free( logfileName );
1047                                 logfileName = c->value_string;
1048                                 logfile = fopen(logfileName, "w");
1049                                 if(logfile) lutil_debug_file(logfile);
1050                         } break;
1051
1052                 case CFG_LASTMOD:
1053                         if(SLAP_NOLASTMODCMD(c->be)) {
1054                                 Debug(LDAP_DEBUG_ANY, "%s: "
1055                                         "lastmod not available for %s databases\n",
1056                                         c->log, c->be->bd_info->bi_type, 0);
1057                                 return(1);
1058                         }
1059                         if(c->value_int)
1060                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1061                         else
1062                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1063                         break;
1064
1065                 case CFG_SSTR_IF_MAX:
1066                         if (c->value_int < index_substr_if_minlen) {
1067                                 Debug(LDAP_DEBUG_ANY, "%s: "
1068                                         "invalid max value (%d)\n",
1069                                         c->log, c->value_int, 0 );
1070                                 return(1);
1071                         }
1072                         index_substr_if_maxlen = c->value_int;
1073                         break;
1074
1075                 case CFG_SSTR_IF_MIN:
1076                         if (c->value_int > index_substr_if_maxlen) {
1077                                 Debug(LDAP_DEBUG_ANY, "%s: "
1078                                         "invalid min value (%d)\n",
1079                                         c->log, c->value_int, 0 );
1080                                 return(1);
1081                         }
1082                         index_substr_if_minlen = c->value_int;
1083                         break;
1084
1085 #ifdef SLAPD_MODULES
1086                 case CFG_MODLOAD:
1087                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1088                                 return(1);
1089                         /* Record this load on the current path */
1090                         {
1091                                 struct berval bv;
1092                                 ber_str2bv(c->line, 0, 1, &bv);
1093                                 ber_bvarray_add( &modcur->mp_loads, &bv );
1094                         }
1095                         break;
1096
1097                 case CFG_MODPATH:
1098                         if(module_path(c->argv[1])) return(1);
1099                         /* Record which path was used with each module */
1100                         {
1101                                 ModPaths *mp;
1102
1103                                 if (!modpaths.mp_loads) {
1104                                         mp = &modpaths;
1105                                 } else {
1106                                         mp = ch_malloc( sizeof( ModPaths ));
1107                                         modlast->mp_next = mp;
1108                                 }
1109                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1110                                 mp->mp_next = NULL;
1111                                 mp->mp_loads = NULL;
1112                                 modlast = mp;
1113                                 if ( c->op == SLAP_CONFIG_ADD )
1114                                         modcur = mp;
1115                         }
1116                         
1117                         break;
1118 #endif
1119
1120 #ifdef LDAP_SLAPI
1121                 case CFG_PLUGIN:
1122                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1123                                 return(1);
1124                         slapi_plugins_used++;
1125                         break;
1126 #endif
1127
1128 #ifdef SLAP_AUTH_REWRITE
1129                 case CFG_REWRITE: {
1130                         struct berval bv;
1131                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1132                                 return(1);
1133                         ber_str2bv( c->line, 0, 1, &bv );
1134                         ber_bvarray_add( &authz_rewrites, &bv );
1135                         }
1136                         break;
1137 #endif
1138
1139
1140                 default:
1141                         Debug(LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d"
1142                                 "(ignored)\n", c->log, c->type, 0);
1143
1144         }
1145         return(0);
1146 }
1147
1148
1149 static int
1150 config_fname(ConfigArgs *c) {
1151         if(c->op == SLAP_CONFIG_EMIT) {
1152                 if (c->private) {
1153                         ConfigFile *cf = c->private;
1154                         value_add_one( &c->rvalue_vals, &cf->c_file );
1155                         return 0;
1156                 }
1157                 return 1;
1158         }
1159         return(0);
1160 }
1161
1162 static int
1163 config_cfdir(ConfigArgs *c) {
1164         if(c->op == SLAP_CONFIG_EMIT) {
1165                 value_add_one( &c->rvalue_vals, &cfdir );
1166                 return 0;
1167         }
1168         return(0);
1169 }
1170
1171 static int
1172 config_search_base(ConfigArgs *c) {
1173         struct berval dn;
1174
1175         if(c->op == SLAP_CONFIG_EMIT) {
1176                 int rc = 1;
1177                 if (!BER_BVISEMPTY(&default_search_base)) {
1178                         value_add_one(&c->rvalue_vals, &default_search_base);
1179                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1180                         rc = 0;
1181                 }
1182                 return rc;
1183         }
1184
1185         if(c->bi || c->be != frontendDB) {
1186                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1187                         "prior to any backend or database definition\n",
1188                         c->log, 0, 0);
1189                 return(1);
1190         }
1191
1192         if(default_search_nbase.bv_len) {
1193                 Debug(LDAP_DEBUG_ANY, "%s: "
1194                         "default search base \"%s\" already defined "
1195                         "(discarding old)\n",
1196                         c->log, default_search_base.bv_val, 0);
1197                 free(default_search_base.bv_val);
1198                 free(default_search_nbase.bv_val);
1199         }
1200
1201         default_search_base = c->value_dn;
1202         default_search_nbase = c->value_ndn;
1203         return(0);
1204 }
1205
1206 static int
1207 config_passwd_hash(ConfigArgs *c) {
1208         int i;
1209         if (c->op == SLAP_CONFIG_EMIT) {
1210                 struct berval bv;
1211                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1212                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1213                         value_add_one(&c->rvalue_vals, &bv);
1214                 }
1215                 return i ? 0 : 1;
1216         }
1217         if(default_passwd_hash) {
1218                 Debug(LDAP_DEBUG_ANY, "%s: "
1219                         "already set default password_hash\n",
1220                         c->log, 0, 0);
1221                 return(1);
1222         }
1223         for(i = 1; i < c->argc; i++) {
1224                 if(!lutil_passwd_scheme(c->argv[i])) {
1225                         Debug(LDAP_DEBUG_ANY, "%s: "
1226                                 "password scheme \"%s\" not available\n",
1227                                 c->log, c->argv[i], 0);
1228                 } else {
1229                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1230                 }
1231                 if(!default_passwd_hash) {
1232                         Debug(LDAP_DEBUG_ANY, "%s: no valid hashes found\n",
1233                                 c->log, 0, 0 );
1234                         return(1);
1235                 }
1236         }
1237         return(0);
1238 }
1239
1240 static int
1241 config_schema_dn(ConfigArgs *c) {
1242         if ( c->op == SLAP_CONFIG_EMIT ) {
1243                 int rc = 1;
1244                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
1245                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1246                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1247                         rc = 0;
1248                 }
1249                 return rc;
1250         }
1251         c->be->be_schemadn = c->value_dn;
1252         c->be->be_schemandn = c->value_ndn;
1253         return(0);
1254 }
1255
1256 static int
1257 config_sizelimit(ConfigArgs *c) {
1258         int i, rc = 0;
1259         char *next;
1260         struct slap_limits_set *lim = &c->be->be_def_limit;
1261         if (c->op == SLAP_CONFIG_EMIT) {
1262                 char buf[8192];
1263                 struct berval bv;
1264                 bv.bv_val = buf;
1265                 bv.bv_len = 0;
1266                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1267                 if ( !BER_BVISEMPTY( &bv ))
1268                         value_add_one( &c->rvalue_vals, &bv );
1269                 else
1270                         rc = 1;
1271                 return rc;
1272         }
1273         for(i = 1; i < c->argc; i++) {
1274                 if(!strncasecmp(c->argv[i], "size", 4)) {
1275                         rc = limits_parse_one(c->argv[i], lim);
1276                         if ( rc ) {
1277                                 Debug(LDAP_DEBUG_ANY, "%s: "
1278                                         "unable to parse value \"%s\" in \"sizelimit <limit>\" line\n",
1279                                         c->log, c->argv[i], 0);
1280                                 return(1);
1281                         }
1282                 } else {
1283                         if(!strcasecmp(c->argv[i], "unlimited")) {
1284                                 lim->lms_s_soft = -1;
1285                         } else {
1286                                 lim->lms_s_soft = strtol(c->argv[i], &next, 0);
1287                                 if(next == c->argv[i]) {
1288                                         Debug(LDAP_DEBUG_ANY, "%s: "
1289                                                 "unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
1290                                                 c->log, c->argv[i], 0);
1291                                         return(1);
1292                                 } else if(next[0] != '\0') {
1293                                         Debug(LDAP_DEBUG_ANY, "%s: "
1294                                                 "trailing chars \"%s\" in \"sizelimit <limit>\" line (ignored)\n",
1295                                                 c->log, next, 0);
1296                                 }
1297                         }
1298                         lim->lms_s_hard = 0;
1299                 }
1300         }
1301         return(0);
1302 }
1303
1304 static int
1305 config_timelimit(ConfigArgs *c) {
1306         int i, rc = 0;
1307         char *next;
1308         struct slap_limits_set *lim = &c->be->be_def_limit;
1309         if (c->op == SLAP_CONFIG_EMIT) {
1310                 char buf[8192];
1311                 struct berval bv;
1312                 bv.bv_val = buf;
1313                 bv.bv_len = 0;
1314                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1315                 if ( !BER_BVISEMPTY( &bv ))
1316                         value_add_one( &c->rvalue_vals, &bv );
1317                 else
1318                         rc = 1;
1319                 return rc;
1320         }
1321         for(i = 1; i < c->argc; i++) {
1322                 if(!strncasecmp(c->argv[i], "time", 4)) {
1323                         rc = limits_parse_one(c->argv[i], lim);
1324                         if ( rc ) {
1325                                 Debug(LDAP_DEBUG_ANY, "%s: "
1326                                         "unable to parse value \"%s\" in \"timelimit <limit>\" line\n",
1327                                         c->log, c->argv[i], 0);
1328                                 return(1);
1329                         }
1330                 } else {
1331                         if(!strcasecmp(c->argv[i], "unlimited")) {
1332                                 lim->lms_t_soft = -1;
1333                         } else {
1334                                 lim->lms_t_soft = strtol(c->argv[i], &next, 0);
1335                                 if(next == c->argv[i]) {
1336                                         Debug(LDAP_DEBUG_ANY, "%s: "
1337                                                 "unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
1338                                                 c->log, c->argv[i], 0);
1339                                         return(1);
1340                                 } else if(next[0] != '\0') {
1341                                         Debug(LDAP_DEBUG_ANY, "%s: "
1342                                                 "trailing chars \"%s\" in \"timelimit <limit>\" line (ignored)\n",
1343                                                 c->log, next, 0);
1344                                 }
1345                         }
1346                         lim->lms_t_hard = 0;
1347                 }
1348         }
1349         return(0);
1350 }
1351
1352 static int
1353 config_overlay(ConfigArgs *c) {
1354         if (c->op == SLAP_CONFIG_EMIT) {
1355                 return 1;
1356         }
1357         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1358                 /* log error */
1359                 Debug(LDAP_DEBUG_ANY, "%s: (optional) %s overlay \"%s\" configuration failed (ignored)\n",
1360                         c->log, c->be == frontendDB ? "global " : "", c->argv[1][1]);
1361         } else if(overlay_config(c->be, c->argv[1])) {
1362                 return(1);
1363         }
1364         return(0);
1365 }
1366
1367 static int
1368 config_suffix(ConfigArgs *c) {
1369         Backend *tbe;
1370         struct berval pdn, ndn;
1371         int rc;
1372
1373         if (c->be == frontendDB) return 1;
1374
1375         if (c->op == SLAP_CONFIG_EMIT) {
1376                 if (!BER_BVISNULL( &c->be->be_suffix[0] )) {
1377                         value_add( &c->rvalue_vals, c->be->be_suffix );
1378                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1379                         return 0;
1380                 } else {
1381                         return 1;
1382                 }
1383         }
1384 #ifdef SLAPD_MONITOR_DN
1385         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1386                 Debug(LDAP_DEBUG_ANY, "%s: "
1387                         "\"%s\" is reserved for monitoring slapd\n",
1388                         c->log, SLAPD_MONITOR_DN, 0);
1389                 return(1);
1390         }
1391 #endif
1392
1393         pdn = c->value_dn;
1394         ndn = c->value_ndn;
1395         tbe = select_backend(&ndn, 0, 0);
1396         if(tbe == c->be) {
1397                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by this backend! (ignored)\n",
1398                         c->log, 0, 0);
1399                 free(pdn.bv_val);
1400                 free(ndn.bv_val);
1401         } else if(tbe) {
1402                 Debug(LDAP_DEBUG_ANY, "%s: suffix already served by a preceding backend \"%s\"\n",
1403                         c->log, tbe->be_suffix[0].bv_val, 0);
1404                 free(pdn.bv_val);
1405                 free(ndn.bv_val);
1406                 return(1);
1407         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1408                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1409                         "base provided \"%s\" (assuming okay)\n",
1410                         c->log, default_search_base.bv_val, 0);
1411         }
1412         ber_bvarray_add(&c->be->be_suffix, &pdn);
1413         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1414         return(0);
1415 }
1416
1417 static int
1418 config_rootdn(ConfigArgs *c) {
1419         if (c->op == SLAP_CONFIG_EMIT) {
1420                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1421                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1422                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1423                         return 0;
1424                 } else {
1425                         return 1;
1426                 }
1427         }
1428         c->be->be_rootdn = c->value_dn;
1429         c->be->be_rootndn = c->value_ndn;
1430         return(0);
1431 }
1432
1433 static int
1434 config_rootpw(ConfigArgs *c) {
1435         Backend *tbe;
1436         if (c->op == SLAP_CONFIG_EMIT) {
1437                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1438                         ber_dupbv( &c->value_bv, &c->be->be_rootpw);
1439                         return 0;
1440                 }
1441                 return 1;
1442         }
1443
1444         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1445         if(tbe != c->be) {
1446                 Debug(LDAP_DEBUG_ANY, "%s: "
1447                         "rootpw can only be set when rootdn is under suffix\n",
1448                         c->log, 0, 0);
1449                 return(1);
1450         }
1451         c->be->be_rootpw = c->value_bv;
1452         return(0);
1453 }
1454
1455 static int
1456 config_restrict(ConfigArgs *c) {
1457         slap_mask_t restrictops = 0;
1458         int i;
1459         slap_verbmasks restrictable_ops[] = {
1460                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
1461                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
1462                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
1463                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
1464                 { BER_BVC("modrdn"),            0 },
1465                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
1466                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
1467                 { BER_BVC("compare"),   SLAP_RESTRICT_OP_COMPARE },
1468                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
1469                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
1470                 { BER_BVC("extended"),  SLAP_RESTRICT_OP_EXTENDED },
1471                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
1472                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1473                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
1474                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
1475                 { BER_BVNULL,   0 }
1476         };
1477
1478         if (c->op == SLAP_CONFIG_EMIT) {
1479                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
1480                         &c->rvalue_vals );
1481         }
1482         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
1483         if ( i ) {
1484                 Debug(LDAP_DEBUG_ANY, "%s: "
1485                         "unknown operation %s in \"restrict <features>\" line\n",
1486                         c->log, c->argv[i], 0);
1487                 return(1);
1488         }
1489         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1490                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1491         c->be->be_restrictops |= restrictops;
1492         return(0);
1493 }
1494
1495 static int
1496 config_allows(ConfigArgs *c) {
1497         slap_mask_t allows = 0;
1498         int i;
1499         slap_verbmasks allowable_ops[] = {
1500                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
1501                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
1502                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
1503                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
1504                 { BER_BVNULL,   0 }
1505         };
1506         if (c->op == SLAP_CONFIG_EMIT) {
1507                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
1508         }
1509         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
1510         if ( i ) {
1511                 Debug(LDAP_DEBUG_ANY, "%s: "
1512                         "unknown feature %s in \"allow <features>\" line\n",
1513                         c->log, c->argv[i], 0);
1514                 return(1);
1515         }
1516         global_allows |= allows;
1517         return(0);
1518 }
1519
1520 static int
1521 config_disallows(ConfigArgs *c) {
1522         slap_mask_t disallows = 0;
1523         int i;
1524         slap_verbmasks disallowable_ops[] = {
1525                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
1526                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
1527                 { BER_BVC("bind_krb4"),         SLAP_DISALLOW_BIND_KRBV4 },
1528                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
1529                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
1530                 { BER_BVNULL, 0 }
1531         };
1532         if (c->op == SLAP_CONFIG_EMIT) {
1533                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
1534         }
1535         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
1536         if ( i ) {
1537                 Debug(LDAP_DEBUG_ANY, "%s: "
1538                         "unknown feature %s in \"disallow <features>\" line\n",
1539                         c->log, c->argv[i], 0);
1540                 return(1);
1541         }
1542         global_disallows |= disallows;
1543         return(0);
1544 }
1545
1546 static int
1547 config_requires(ConfigArgs *c) {
1548         slap_mask_t requires = 0;
1549         int i;
1550         slap_verbmasks requires_ops[] = {
1551                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
1552                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
1553                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
1554                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
1555                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
1556                 { BER_BVNULL, 0 }
1557         };
1558         if (c->op == SLAP_CONFIG_EMIT) {
1559                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
1560         }
1561         i = verbs_to_mask(c->argc, c->argv, requires_ops, &requires);
1562         if ( i ) {
1563                 Debug(LDAP_DEBUG_ANY, "%s: "
1564                         "unknown feature %s in \"require <features>\" line\n",
1565                         c->log, c->argv[i], 0);
1566                 return(1);
1567         }
1568         c->be->be_requires = requires;
1569         return(0);
1570 }
1571
1572 static int
1573 config_loglevel(ConfigArgs *c) {
1574         int i;
1575         char *next;
1576         slap_verbmasks loglevel_ops[] = {
1577                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
1578                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
1579                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
1580                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
1581                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
1582                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
1583                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
1584                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
1585                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
1586                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
1587                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
1588                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
1589                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
1590                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
1591                 { BER_BVC("Any"),       -1 },
1592                 { BER_BVNULL,   0 }
1593         };
1594
1595         if (c->op == SLAP_CONFIG_EMIT) {
1596                 return mask_to_verbs( loglevel_ops, ldap_syslog, &c->rvalue_vals );
1597         }
1598
1599         ldap_syslog = 0;
1600
1601         for( i=1; i < c->argc; i++ ) {
1602                 int     level;
1603
1604                 if ( isdigit( c->argv[i][0] ) ) {
1605                         level = strtol( c->argv[i], &next, 10 );
1606                         if ( next == NULL || next[0] != '\0' ) {
1607                                 Debug( LDAP_DEBUG_ANY,
1608                                         "%s: unable to parse level \"%s\" "
1609                                         "in \"loglevel <level> [...]\" line.\n",
1610                                         c->log, c->argv[i], 0);
1611                                 return( 1 );
1612                         }
1613                 } else {
1614                         int j = verb_to_mask(c->argv[i], loglevel_ops);
1615                         if(BER_BVISNULL(&loglevel_ops[j].word)) {
1616                                 Debug( LDAP_DEBUG_ANY,
1617                                         "%s: unknown level \"%s\" "
1618                                         "in \"loglevel <level> [...]\" line.\n",
1619                                         c->log, c->argv[i], 0);
1620                                 return( 1 );
1621                         }
1622                         level = loglevel_ops[j].mask;
1623                 }
1624                 ldap_syslog |= level;
1625         }
1626         return(0);
1627 }
1628
1629 static int
1630 config_syncrepl(ConfigArgs *c) {
1631         if (c->op == SLAP_CONFIG_EMIT) {
1632                 if ( c->be->be_syncinfo ) {
1633                         struct berval bv;
1634                         syncrepl_unparse( c->be->be_syncinfo, &bv ); 
1635                         ber_bvarray_add( &c->rvalue_vals, &bv );
1636                         return 0;
1637                 }
1638                 return 1;
1639         }
1640         if(SLAP_SHADOW(c->be)) {
1641                 Debug(LDAP_DEBUG_ANY, "%s: "
1642                         "syncrepl: database already shadowed.\n",
1643                         c->log, 0, 0);
1644                 return(1);
1645         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
1646                 return(1);
1647         }
1648         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
1649         return(0);
1650 }
1651
1652 static int
1653 config_referral(ConfigArgs *c) {
1654         struct berval vals[2];
1655         if (c->op == SLAP_CONFIG_EMIT) {
1656                 if ( default_referral ) {
1657                         value_add( &c->rvalue_vals, default_referral );
1658                         return 0;
1659                 } else {
1660                         return 1;
1661                 }
1662         }
1663         if(validate_global_referral(c->argv[1])) {
1664                 Debug(LDAP_DEBUG_ANY, "%s: "
1665                         "invalid URL (%s) in \"referral\" line.\n",
1666                         c->log, c->argv[1], 0);
1667                 return(1);
1668         }
1669
1670         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1671         vals[1].bv_val = NULL; vals[1].bv_len = 0;
1672         if(value_add(&default_referral, vals)) return(LDAP_OTHER);
1673         return(0);
1674 }
1675
1676 static struct {
1677         struct berval key;
1678         int off;
1679 } sec_keys[] = {
1680         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
1681         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
1682         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
1683         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
1684         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
1685         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
1686         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
1687         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
1688         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
1689         { BER_BVNULL, 0 }
1690 };
1691
1692 static int
1693 config_security(ConfigArgs *c) {
1694         slap_ssf_set_t *set = &c->be->be_ssf_set;
1695         char *next;
1696         int i, j;
1697         if (c->op == SLAP_CONFIG_EMIT) {
1698                 char numbuf[32];
1699                 struct berval bv;
1700                 slap_ssf_t *tgt;
1701                 int rc = 1;
1702
1703                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
1704                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
1705                         if ( *tgt ) {
1706                                 rc = 0;
1707                                 bv.bv_len = sprintf( numbuf, "%u", *tgt );
1708                                 bv.bv_len += sec_keys[i].key.bv_len;
1709                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
1710                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
1711                                 strcpy( next, numbuf );
1712                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1713                         }
1714                 }
1715                 return rc;
1716         }
1717         for(i = 1; i < c->argc; i++) {
1718                 slap_ssf_t *tgt = NULL;
1719                 char *src;
1720                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
1721                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
1722                                 sec_keys[j].key.bv_len)) {
1723                                 src = c->argv[i] + sec_keys[j].key.bv_len;
1724                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
1725                                 break;
1726                         }
1727                 }
1728                 if ( !tgt ) {
1729                         Debug(LDAP_DEBUG_ANY, "%s: "
1730                                 "unknown factor %s in \"security <factors>\" line\n",
1731                                 c->log, c->argv[i], 0);
1732                         return(1);
1733                 }
1734
1735                 *tgt = strtol(src, &next, 10);
1736                 if(next == NULL || next[0] != '\0' ) {
1737                         Debug(LDAP_DEBUG_ANY, "%s: "
1738                                 "unable to parse factor \"%s\" in \"security <factors>\" line\n",
1739                                 c->log, c->argv[i], 0);
1740                         return(1);
1741                 }
1742         }
1743         return(0);
1744 }
1745
1746 static char *
1747 anlist_unparse( AttributeName *an, char *ptr ) {
1748         int comma = 0;
1749
1750         for (; !BER_BVISNULL( &an->an_name ); an++) {
1751                 if ( comma ) *ptr++ = ',';
1752                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
1753                 comma = 1;
1754         }
1755         return ptr;
1756 }
1757
1758 static void
1759 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
1760 {
1761         int len;
1762         char *ptr;
1763         struct berval bc = {0};
1764         char numbuf[32];
1765
1766         len = sprintf(numbuf, "{%d}", i );
1767
1768         len += strlen( ri->ri_uri ) + STRLENOF("uri=");
1769         if ( ri->ri_nsuffix ) {
1770                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1771                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
1772                 }
1773         }
1774         if ( ri->ri_attrs ) {
1775                 len += STRLENOF("attr");
1776                 if ( ri->ri_exclude ) len++;
1777                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
1778                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
1779                 }
1780         }
1781         bindconf_unparse( &ri->ri_bindconf, &bc );
1782         len += bc.bv_len;
1783
1784         bv->bv_val = ch_malloc(len + 1);
1785         bv->bv_len = len;
1786
1787         ptr = lutil_strcopy( bv->bv_val, numbuf );
1788         ptr = lutil_strcopy( ptr, "uri=" );
1789         ptr = lutil_strcopy( ptr, ri->ri_uri );
1790
1791         if ( ri->ri_nsuffix ) {
1792                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
1793                         ptr = lutil_strcopy( ptr, " suffix=\"" );
1794                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
1795                         *ptr++ = '"';
1796                 }
1797         }
1798         if ( ri->ri_attrs ) {
1799                 ptr = lutil_strcopy( ptr, "attr" );
1800                 if ( ri->ri_exclude ) *ptr++ = '!';
1801                 *ptr++ = '=';
1802                 ptr = anlist_unparse( ri->ri_attrs, ptr );
1803         }
1804         if ( bc.bv_val ) {
1805                 strcpy( ptr, bc.bv_val );
1806                 ch_free( bc.bv_val );
1807         }
1808 }
1809
1810 static int
1811 config_replica(ConfigArgs *c) {
1812         int i, nr = -1, len;
1813         char *replicahost, *replicauri;
1814         LDAPURLDesc *ludp;
1815
1816         if (c->op == SLAP_CONFIG_EMIT) {
1817                 if (c->be->be_replica) {
1818                         struct berval bv;
1819                         for (i=0;c->be->be_replica[i]; i++) {
1820                                 replica_unparse( c->be->be_replica[i], i, &bv );
1821                                 ber_bvarray_add( &c->rvalue_vals, &bv );
1822                         }
1823                         return 0;
1824                 }
1825                 return 1;
1826         }
1827         if(SLAP_MONITOR(c->be)) {
1828                 Debug(LDAP_DEBUG_ANY, "%s: "
1829                         "\"replica\" should not be used inside monitor database\n",
1830                         c->log, 0, 0);
1831                 return(0);      /* FIXME: should this be an error? */
1832         }
1833
1834         for(i = 1; i < c->argc; i++) {
1835                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
1836                         replicahost = c->argv[i] + STRLENOF("host=");
1837                         len = strlen( replicahost );
1838                         replicauri = ch_malloc( len + STRLENOF("ldap://") + 1 );
1839                         sprintf( replicauri, "ldap://%s", replicahost );
1840                         replicahost = replicauri + STRLENOF( "ldap://");
1841                         nr = add_replica_info(c->be, replicauri, replicahost);
1842                         break;
1843                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
1844                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
1845                                 Debug(LDAP_DEBUG_ANY, "%s: "
1846                                         "replica line contains invalid "
1847                                         "uri definition.\n", c->log, 0, 0);
1848                                 return(1);
1849                         }
1850                         if(!ludp->lud_host) {
1851                                 Debug(LDAP_DEBUG_ANY, "%s: "
1852                                         "replica line contains invalid "
1853                                         "uri definition - missing hostname.\n",
1854                                         c->log, 0, 0);
1855                                 return(1);
1856                         }
1857                         ldap_free_urldesc(ludp);
1858                         replicauri = c->argv[i] + STRLENOF("uri=");
1859                         replicauri = ch_strdup( replicauri );
1860                         replicahost = strchr( replicauri, '/' );
1861                         replicahost += 2;
1862                         nr = add_replica_info(c->be, replicauri, replicahost);
1863                         break;
1864                 }
1865         }
1866         if(i == c->argc) {
1867                 Debug(LDAP_DEBUG_ANY, "%s: "
1868                         "missing host or uri in \"replica\" line\n",
1869                         c->log, 0, 0);
1870                 return(1);
1871         } else if(nr == -1) {
1872                 Debug(LDAP_DEBUG_ANY, "%s: "
1873                         "unable to add replica \"%s\"\n",
1874                         c->log, replicauri, 0);
1875                 return(1);
1876         } else {
1877                 for(i = 1; i < c->argc; i++) {
1878                         if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
1879                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
1880                                         case 1:
1881                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1882                                                 "suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
1883                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
1884                                                 break;
1885                                         case 2:
1886                                                 Debug(LDAP_DEBUG_ANY, "%s: "
1887                                                 "unable to normalize suffix in \"replica\" line (ignored)\n",
1888                                                 c->log, 0, 0);
1889                                                 break;
1890                                 }
1891
1892                         } else if(!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))) {
1893                                 int exclude = 0;
1894                                 char *arg = c->argv[i] + STRLENOF("attr");
1895                                 if(arg[0] == '!') {
1896                                         arg++;
1897                                         exclude = 1;
1898                                 }
1899                                 if(arg[0] != '=') {
1900                                         continue;
1901                                 }
1902                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
1903                                         Debug(LDAP_DEBUG_ANY, "%s: "
1904                                                 "attribute \"%s\" in \"replica\" line is unknown\n",
1905                                                 c->log, arg + 1, 0);
1906                                         return(1);
1907                                 }
1908                         } else if ( bindconf_parse( c->argv[i],
1909                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
1910                                 return(1);
1911                         }
1912                 }
1913         }
1914         return(0);
1915 }
1916
1917 static int
1918 config_updatedn(ConfigArgs *c) {
1919         struct berval dn;
1920         int rc;
1921         if (c->op == SLAP_CONFIG_EMIT) {
1922                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
1923                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
1924                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
1925                         return 0;
1926                 }
1927                 return 1;
1928         }
1929         if(SLAP_SHADOW(c->be)) {
1930                 Debug(LDAP_DEBUG_ANY, "%s: "
1931                         "updatedn: database already shadowed.\n",
1932                         c->log, 0, 0);
1933                 return(1);
1934         }
1935
1936         ber_str2bv(c->argv[1], 0, 0, &dn);
1937
1938         rc = dnNormalize(0, NULL, NULL, &dn, &c->be->be_update_ndn, NULL);
1939
1940         if(rc != LDAP_SUCCESS) {
1941                 Debug(LDAP_DEBUG_ANY, "%s: "
1942                         "updatedn DN is invalid: %d (%s)\n",
1943                         c->log, rc, ldap_err2string( rc ));
1944                 return(1);
1945         }
1946
1947         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
1948         return(0);
1949 }
1950
1951 static int
1952 config_updateref(ConfigArgs *c) {
1953         struct berval vals[2];
1954         if (c->op == SLAP_CONFIG_EMIT) {
1955                 if ( c->be->be_update_refs ) {
1956                         value_add( &c->rvalue_vals, c->be->be_update_refs );
1957                         return 0;
1958                 } else {
1959                         return 1;
1960                 }
1961         }
1962         if(!SLAP_SHADOW(c->be)) {
1963                 Debug(LDAP_DEBUG_ANY, "%s: "
1964                         "updateref line must come after syncrepl or updatedn.\n",
1965                         c->log, 0, 0);
1966                 return(1);
1967         }
1968
1969         if(validate_global_referral(c->argv[1])) {
1970                 Debug(LDAP_DEBUG_ANY, "%s: "
1971                         "invalid URL (%s) in \"updateref\" line.\n",
1972                         c->log, c->argv[1], 0);
1973                 return(1);
1974         }
1975         ber_str2bv(c->argv[1], 0, 0, &vals[0]);
1976         vals[1].bv_val = NULL;
1977         if(value_add(&c->be->be_update_refs, vals)) return(LDAP_OTHER);
1978         return(0);
1979 }
1980
1981 static int
1982 config_include(ConfigArgs *c) {
1983         unsigned long savelineno = c->lineno;
1984         int rc;
1985         ConfigFile *cf;
1986         ConfigFile *cfsave = cfn;
1987         ConfigFile *cf2 = NULL;
1988         if (c->op == SLAP_CONFIG_EMIT) {
1989                 return 1;
1990         }
1991         cf = ch_calloc( 1, sizeof(ConfigFile));
1992         if ( cfn->c_kids ) {
1993                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
1994                 cf2->c_sibs = cf;
1995         } else {
1996                 cfn->c_kids = cf;
1997         }
1998         cfn = cf;
1999         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
2000         rc = read_config_file(c->argv[1], c->depth + 1, c);
2001         c->lineno = savelineno - 1;
2002         cfn = cfsave;
2003         if ( rc ) {
2004                 if ( cf2 ) cf2->c_sibs = NULL;
2005                 else cfn->c_kids = NULL;
2006                 ch_free( cf->c_file.bv_val );
2007                 ch_free( cf );
2008         }
2009         return(rc);
2010 }
2011
2012 #ifdef HAVE_TLS
2013 static int
2014 config_tls_option(ConfigArgs *c) {
2015         int flag;
2016         switch(c->type) {
2017         case CFG_TLS_RAND:              flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2018         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2019         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2020         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2021         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2022         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2023         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2024                                         "unknown tls_option <%x>\n",
2025                                         c->log, c->type, 0);
2026         }
2027         if (c->op == SLAP_CONFIG_EMIT) {
2028                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
2029         }
2030         ch_free(c->value_string);
2031         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2032 }
2033
2034 /* FIXME: this ought to be provided by libldap */
2035 static int
2036 config_tls_config(ConfigArgs *c) {
2037         int i, flag;
2038         slap_verbmasks crlkeys[] = {
2039                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
2040                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
2041                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
2042                 { BER_BVNULL, 0 }
2043         };
2044         slap_verbmasks vfykeys[] = {
2045                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
2046                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
2047                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
2048                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
2049                 { BER_BVNULL, 0 }
2050         }, *keys;
2051         switch(c->type) {
2052 #ifdef HAVE_OPENSSL_CRL
2053         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; keys = crlkeys;
2054                 break;
2055 #endif
2056         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; keys = vfykeys;
2057                 break;
2058         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2059                                         "unknown tls_option <%x>\n",
2060                                         c->log, c->type, 0);
2061         }
2062         if (c->op == SLAP_CONFIG_EMIT) {
2063                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
2064                 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
2065                         if (keys[i].mask == c->value_int) {
2066                                 c->value_string = ch_strdup( keys[i].word.bv_val );
2067                                 return 0;
2068                         }
2069                 }
2070                 return 1;
2071         }
2072         ch_free( c->value_string );
2073         if(isdigit((unsigned char)c->argv[1][0])) {
2074                 i = atoi(c->argv[1]);
2075                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
2076         } else {
2077                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
2078         }
2079 }
2080 #endif
2081
2082 static int
2083 add_syncrepl(
2084         Backend *be,
2085         char    **cargv,
2086         int     cargc
2087 )
2088 {
2089         syncinfo_t *si;
2090         int     rc = 0;
2091
2092         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2093
2094         if ( si == NULL ) {
2095                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2096                 return 1;
2097         }
2098
2099         si->si_bindconf.sb_tls = SB_TLS_OFF;
2100         si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2101         si->si_schemachecking = 0;
2102         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2103                 &si->si_filterstr );
2104         si->si_base.bv_val = NULL;
2105         si->si_scope = LDAP_SCOPE_SUBTREE;
2106         si->si_attrsonly = 0;
2107         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2108         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2109         si->si_attrs = NULL;
2110         si->si_allattrs = 0;
2111         si->si_allopattrs = 0;
2112         si->si_exattrs = NULL;
2113         si->si_type = LDAP_SYNC_REFRESH_ONLY;
2114         si->si_interval = 86400;
2115         si->si_retryinterval = NULL;
2116         si->si_retrynum_init = NULL;
2117         si->si_retrynum = NULL;
2118         si->si_manageDSAit = 0;
2119         si->si_tlimit = 0;
2120         si->si_slimit = 0;
2121
2122         si->si_presentlist = NULL;
2123         LDAP_LIST_INIT( &si->si_nonpresentlist );
2124         ldap_pvt_thread_mutex_init( &si->si_mutex );
2125
2126         rc = parse_syncrepl_line( cargv, cargc, si );
2127
2128         if ( rc < 0 ) {
2129                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
2130                 syncinfo_free( si );    
2131                 return 1;
2132         } else {
2133                 Debug( LDAP_DEBUG_CONFIG,
2134                         "Config: ** successfully added syncrepl \"%s\"\n",
2135                         BER_BVISNULL( &si->si_provideruri ) ?
2136                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
2137                 if ( !si->si_schemachecking ) {
2138                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2139                 }
2140                 si->si_be = be;
2141                 be->be_syncinfo = si;
2142                 return 0;
2143         }
2144 }
2145
2146 /* NOTE: used & documented in slapd.conf(5) */
2147 #define IDSTR                   "rid"
2148 #define PROVIDERSTR             "provider"
2149 #define TYPESTR                 "type"
2150 #define INTERVALSTR             "interval"
2151 #define SEARCHBASESTR           "searchbase"
2152 #define FILTERSTR               "filter"
2153 #define SCOPESTR                "scope"
2154 #define ATTRSSTR                "attrs"
2155 #define ATTRSONLYSTR            "attrsonly"
2156 #define SLIMITSTR               "sizelimit"
2157 #define TLIMITSTR               "timelimit"
2158 #define SCHEMASTR               "schemachecking"
2159
2160 /* FIXME: undocumented */
2161 #define OLDAUTHCSTR             "bindprincipal"
2162 #define EXATTRSSTR              "exattrs"
2163 #define RETRYSTR                "retry"
2164
2165 /* FIXME: unused */
2166 #define LASTMODSTR              "lastmod"
2167 #define LMGENSTR                "gen"
2168 #define LMNOSTR                 "no"
2169 #define LMREQSTR                "req"
2170 #define SRVTABSTR               "srvtab"
2171 #define SUFFIXSTR               "suffix"
2172 #define MANAGEDSAITSTR          "manageDSAit"
2173
2174 /* mandatory */
2175 #define GOT_ID                  0x0001
2176 #define GOT_PROVIDER            0x0002
2177
2178 /* check */
2179 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER)
2180
2181 static struct {
2182         struct berval key;
2183         int val;
2184 } scopes[] = {
2185         { BER_BVC("base"), LDAP_SCOPE_BASE },
2186         { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2187 #ifdef LDAP_SCOPE_SUBORDINATE
2188         { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2189         { BER_BVC("subordinate"), 0 },
2190 #endif
2191         { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2192         { BER_BVNULL, 0 }
2193 };
2194
2195 static int
2196 parse_syncrepl_line(
2197         char            **cargv,
2198         int             cargc,
2199         syncinfo_t      *si
2200 )
2201 {
2202         int     gots = 0;
2203         int     i;
2204         char    *val;
2205
2206         for ( i = 1; i < cargc; i++ ) {
2207                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2208                                         STRLENOF( IDSTR "=" ) ) )
2209                 {
2210                         int tmp;
2211                         /* '\0' string terminator accounts for '=' */
2212                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2213                         tmp= atoi( val );
2214                         if ( tmp >= 1000 || tmp < 0 ) {
2215                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2216                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2217                                 return -1;
2218                         }
2219                         si->si_rid = tmp;
2220                         gots |= GOT_ID;
2221                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2222                                         STRLENOF( PROVIDERSTR "=" ) ) )
2223                 {
2224                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2225                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2226                         gots |= GOT_PROVIDER;
2227                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2228                                         STRLENOF( SCHEMASTR "=" ) ) )
2229                 {
2230                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2231                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2232                                 si->si_schemachecking = 1;
2233                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2234                                 si->si_schemachecking = 0;
2235                         } else {
2236                                 si->si_schemachecking = 1;
2237                         }
2238                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2239                                         STRLENOF( FILTERSTR "=" ) ) )
2240                 {
2241                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2242                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2243                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2244                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2245                 {
2246                         struct berval   bv;
2247                         int             rc;
2248
2249                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2250                         if ( si->si_base.bv_val ) {
2251                                 ch_free( si->si_base.bv_val );
2252                         }
2253                         ber_str2bv( val, 0, 0, &bv );
2254                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2255                         if ( rc != LDAP_SUCCESS ) {
2256                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2257                                         val, rc, ldap_err2string( rc ) );
2258                                 return -1;
2259                         }
2260                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2261                                         STRLENOF( SCOPESTR "=" ) ) )
2262                 {
2263                         int j;
2264                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2265                         for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2266                                 if (!strncasecmp( val, scopes[j].key.bv_val,
2267                                         scopes[j].key.bv_len )) {
2268                                         while (!scopes[j].val) j--;
2269                                         si->si_scope = scopes[j].val;
2270                                         break;
2271                                 }
2272                         }
2273                         if ( BER_BVISNULL(&scopes[j].key) ) {
2274                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2275                                         "unknown scope \"%s\"\n", val);
2276                                 return -1;
2277                         }
2278                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2279                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2280                 {
2281                         si->si_attrsonly = 1;
2282                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2283                                         STRLENOF( ATTRSSTR "=" ) ) )
2284                 {
2285                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2286                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2287                                 char *attr_fname;
2288                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2289                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2290                                 if ( si->si_anlist == NULL ) {
2291                                         ch_free( attr_fname );
2292                                         return -1;
2293                                 }
2294                                 si->si_anfile = attr_fname;
2295                         } else {
2296                                 char *str, *s, *next;
2297                                 char delimstr[] = " ,\t";
2298                                 str = ch_strdup( val );
2299                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2300                                                 s != NULL;
2301                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2302                                 {
2303                                         if ( strlen(s) == 1 && *s == '*' ) {
2304                                                 si->si_allattrs = 1;
2305                                                 *(val + ( s - str )) = delimstr[0];
2306                                         }
2307                                         if ( strlen(s) == 1 && *s == '+' ) {
2308                                                 si->si_allopattrs = 1;
2309                                                 *(val + ( s - str )) = delimstr[0];
2310                                         }
2311                                 }
2312                                 ch_free( str );
2313                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2314                                 if ( si->si_anlist == NULL ) {
2315                                         return -1;
2316                                 }
2317                         }
2318                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2319                                         STRLENOF( EXATTRSSTR "=" ) ) )
2320                 {
2321                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2322                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2323                                 char *attr_fname;
2324                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2325                                 si->si_exanlist = file2anlist(
2326                                                                         si->si_exanlist, attr_fname, " ,\t" );
2327                                 if ( si->si_exanlist == NULL ) {
2328                                         ch_free( attr_fname );
2329                                         return -1;
2330                                 }
2331                                 ch_free( attr_fname );
2332                         } else {
2333                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2334                                 if ( si->si_exanlist == NULL ) {
2335                                         return -1;
2336                                 }
2337                         }
2338                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2339                                         STRLENOF( TYPESTR "=" ) ) )
2340                 {
2341                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2342                         if ( !strncasecmp( val, "refreshOnly",
2343                                                 STRLENOF("refreshOnly") ))
2344                         {
2345                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2346                         } else if ( !strncasecmp( val, "refreshAndPersist",
2347                                                 STRLENOF("refreshAndPersist") ))
2348                         {
2349                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2350                                 si->si_interval = 60;
2351                         } else {
2352                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2353                                         "unknown sync type \"%s\"\n", val);
2354                                 return -1;
2355                         }
2356                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2357                                         STRLENOF( INTERVALSTR "=" ) ) )
2358                 {
2359                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2360                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2361                                 si->si_interval = 0;
2362                         } else {
2363                                 char *hstr;
2364                                 char *mstr;
2365                                 char *dstr;
2366                                 char *sstr;
2367                                 int dd, hh, mm, ss;
2368                                 dstr = val;
2369                                 hstr = strchr( dstr, ':' );
2370                                 if ( hstr == NULL ) {
2371                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2372                                                 "invalid interval \"%s\"\n", val );
2373                                         return -1;
2374                                 }
2375                                 *hstr++ = '\0';
2376                                 mstr = strchr( hstr, ':' );
2377                                 if ( mstr == NULL ) {
2378                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2379                                                 "invalid interval \"%s\"\n", val );
2380                                         return -1;
2381                                 }
2382                                 *mstr++ = '\0';
2383                                 sstr = strchr( mstr, ':' );
2384                                 if ( sstr == NULL ) {
2385                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2386                                                 "invalid interval \"%s\"\n", val );
2387                                         return -1;
2388                                 }
2389                                 *sstr++ = '\0';
2390
2391                                 dd = atoi( dstr );
2392                                 hh = atoi( hstr );
2393                                 mm = atoi( mstr );
2394                                 ss = atoi( sstr );
2395                                 if (( hh > 24 ) || ( hh < 0 ) ||
2396                                         ( mm > 60 ) || ( mm < 0 ) ||
2397                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2398                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2399                                                 "invalid interval \"%s\"\n", val );
2400                                         return -1;
2401                                 }
2402                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2403                         }
2404                         if ( si->si_interval < 0 ) {
2405                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2406                                         "invalid interval \"%ld\"\n",
2407                                         (long) si->si_interval);
2408                                 return -1;
2409                         }
2410                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2411                                         STRLENOF( RETRYSTR "=" ) ) )
2412                 {
2413                         char **retry_list;
2414                         int j, k, n;
2415
2416                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2417                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2418                         retry_list[0] = NULL;
2419
2420                         slap_str2clist( &retry_list, val, " ,\t" );
2421
2422                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2423                         n = k / 2;
2424                         if ( k % 2 ) {
2425                                 fprintf( stderr,
2426                                                 "Error: incomplete syncrepl retry list\n" );
2427                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2428                                         ch_free( retry_list[k] );
2429                                 }
2430                                 ch_free( retry_list );
2431                                 exit( EXIT_FAILURE );
2432                         }
2433                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2434                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2435                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2436                         for ( j = 0; j < n; j++ ) {
2437                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2438                                 if ( *retry_list[j*2+1] == '+' ) {
2439                                         si->si_retrynum_init[j] = -1;
2440                                         si->si_retrynum[j] = -1;
2441                                         j++;
2442                                         break;
2443                                 } else {
2444                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2445                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2446                                 }
2447                         }
2448                         si->si_retrynum_init[j] = -2;
2449                         si->si_retrynum[j] = -2;
2450                         si->si_retryinterval[j] = 0;
2451                         
2452                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2453                                 ch_free( retry_list[k] );
2454                         }
2455                         ch_free( retry_list );
2456                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2457                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2458                 {
2459                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2460                         si->si_manageDSAit = atoi( val );
2461                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2462                                         STRLENOF( SLIMITSTR "=") ) )
2463                 {
2464                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2465                         si->si_slimit = atoi( val );
2466                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2467                                         STRLENOF( TLIMITSTR "=" ) ) )
2468                 {
2469                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2470                         si->si_tlimit = atoi( val );
2471                 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2472                         fprintf( stderr, "Error: parse_syncrepl_line: "
2473                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2474                         return -1;
2475                 }
2476         }
2477
2478         if ( gots != GOT_ALL ) {
2479                 fprintf( stderr,
2480                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2481                 return -1;
2482         }
2483
2484         return 0;
2485 }
2486
2487 static void
2488 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
2489 {
2490         struct berval bc;
2491         char buf[BUFSIZ*2], *ptr;
2492         int i, len;
2493
2494         bindconf_unparse( &si->si_bindconf, &bc );
2495         ptr = buf;
2496         ptr += sprintf( ptr, IDSTR "=%03d " PROVIDERSTR "=%s",
2497                 si->si_rid, si->si_provideruri.bv_val );
2498         if ( !BER_BVISNULL( &bc )) {
2499                 ptr = lutil_strcopy( ptr, bc.bv_val );
2500                 free( bc.bv_val );
2501         }
2502         if ( !BER_BVISEMPTY( &si->si_filterstr )) {
2503                 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
2504                 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
2505                 *ptr++ = '"';
2506         }
2507         if ( !BER_BVISNULL( &si->si_base )) {
2508                 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
2509                 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
2510                 *ptr++ = '"';
2511         }
2512         for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
2513                 if ( si->si_scope == scopes[i].val ) {
2514                         ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
2515                         ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
2516                         break;
2517                 }
2518         }
2519         if ( si->si_attrsonly ) {
2520                 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
2521         }
2522         if ( si->si_anfile ) {
2523                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
2524                 ptr = lutil_strcopy( ptr, si->si_anfile );
2525         } else if ( si->si_allattrs || si->si_allopattrs ||
2526                 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
2527                 char *old;
2528                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
2529                 old = ptr;
2530                 ptr = anlist_unparse( si->si_anlist, ptr );
2531                 if ( si->si_allattrs ) {
2532                         if ( old != ptr ) *ptr++ = ',';
2533                         *ptr++ = '*';
2534                 }
2535                 if ( si->si_allopattrs ) {
2536                         if ( old != ptr ) *ptr++ = ',';
2537                         *ptr++ = '+';
2538                 }
2539                 *ptr++ = '"';
2540         }
2541         if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
2542                 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
2543                 ptr = anlist_unparse( si->si_exanlist, ptr );
2544         }
2545         ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
2546         ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
2547         
2548         ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
2549         ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
2550                 "refreshAndPersist" : "refreshOnly" );
2551
2552         if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
2553                 int dd, hh, mm, ss;
2554
2555                 dd = si->si_interval;
2556                 ss = dd % 60;
2557                 dd /= 60;
2558                 mm = dd % 60;
2559                 dd /= 60;
2560                 hh = dd % 24;
2561                 dd /= 24;
2562                 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
2563                 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
2564         } else if ( si->si_retryinterval ) {
2565                 int space=0;
2566                 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
2567                 for (i=0; si->si_retryinterval[i]; i++) {
2568                         if ( space ) *ptr++ = ' ';
2569                         space = 1;
2570                         ptr += sprintf( ptr, "%d", si->si_retryinterval[i] );
2571                         if ( si->si_retrynum_init[i] == -1 )
2572                                 *ptr++ = '+';
2573                         else
2574                                 ptr += sprintf( ptr, "%d", si->si_retrynum_init );
2575                 }
2576                 *ptr++ = '"';
2577         }
2578
2579 #if 0 /* FIXME: unused in syncrepl.c, should remove it */
2580         ptr = lutil_strcopy( ptr, " " MANAGEDSAITSTR "=" );
2581         ptr += sprintf( ptr, "%d", si->si_manageDSAit );
2582 #endif
2583
2584         if ( si->si_slimit ) {
2585                 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
2586                 ptr += sprintf( ptr, "%d", si->si_slimit );
2587         }
2588
2589         if ( si->si_tlimit ) {
2590                 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
2591                 ptr += sprintf( ptr, "%d", si->si_tlimit );
2592         }
2593         bc.bv_len = ptr - buf;
2594         bc.bv_val = buf;
2595         ber_dupbv( bv, &bc );
2596 }
2597
2598 static CfEntryInfo *
2599 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
2600 {
2601         struct berval cdn;
2602         char *c;
2603
2604         if ( !root ) {
2605                 *last = NULL;
2606                 return NULL;
2607         }
2608
2609         if ( dn_match( &root->ce_entry->e_nname, dn ))
2610                 return root;
2611
2612         c = dn->bv_val+dn->bv_len;
2613         for (;*c != ',';c--);
2614
2615         while(root) {
2616                 *last = root;
2617                 for (--c;c>dn->bv_val && *c != ',';c--);
2618                 if ( *c == ',' )
2619                         c++;
2620                 cdn.bv_val = c;
2621                 cdn.bv_len = dn->bv_len - (c-dn->bv_val);
2622
2623                 root = root->ce_kids;
2624
2625                 for (;root;root=root->ce_sibs) {
2626                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
2627                                 if ( cdn.bv_val == dn->bv_val ) {
2628                                         return root;
2629                                 }
2630                                 break;
2631                         }
2632                 }
2633         }
2634         return root;
2635 }
2636
2637 static int
2638 config_ldif_resp( Operation *op, SlapReply *rs )
2639 {
2640         if ( rs->sr_type == REP_SEARCH ) {
2641                 CfBackInfo *cfb = op->o_callback->sc_private;
2642
2643                 cfb->cb_got_ldif = 1;
2644                 rs->sr_err = config_add_internal( cfb, rs->sr_entry, NULL );
2645         }
2646         return rs->sr_err;
2647 }
2648
2649 /* Configure and read the underlying back-ldif store */
2650 static int
2651 config_setup_ldif( BackendDB *be, const char *dir ) {
2652         CfBackInfo *cfb = be->be_private;
2653         ConfigArgs c = {0};
2654         ConfigTable *ct;
2655         char *argv[3];
2656         int rc;
2657         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
2658         Connection conn = {0};
2659         char opbuf[OPERATION_BUFFER_SIZE];
2660         Operation *op;
2661         SlapReply rs = {REP_RESULT};
2662         Filter filter = { LDAP_FILTER_PRESENT };
2663         struct berval filterstr = BER_BVC("(objectclass=*)");
2664
2665         cfb->cb_db.bd_info = backend_info( "ldif" );
2666         if ( !cfb->cb_db.bd_info )
2667                 return 0;       /* FIXME: eventually this will be a fatal error */
2668
2669         if ( cfb->cb_db.bd_info->bi_db_init( &cfb->cb_db )) return 1;
2670
2671         /* Mark that back-ldif type is in use */
2672         cfb->cb_db.bd_info->bi_nDB++;
2673
2674         cfb->cb_db.be_suffix = be->be_suffix;
2675         cfb->cb_db.be_nsuffix = be->be_nsuffix;
2676         cfb->cb_db.be_rootdn = be->be_rootdn;
2677         cfb->cb_db.be_rootndn = be->be_rootndn;
2678
2679         ber_str2bv( dir, 0, 1, &cfdir );
2680
2681         c.be = &cfb->cb_db;
2682         c.fname = "slapd";
2683         c.argc = 2;
2684         argv[0] = "directory";
2685         argv[1] = (char *)dir;
2686         argv[2] = NULL;
2687         c.argv = argv;
2688
2689         ct = config_find_keyword( c.be->be_cf_table, &c );
2690         if ( !ct )
2691                 return 1;
2692
2693         if ( config_add_vals( ct, &c ))
2694                 return 1;
2695
2696         if ( backend_startup_one( &cfb->cb_db ))
2697                 return 1;
2698
2699         op = (Operation *)opbuf;
2700         connection_fake_init( &conn, op, cfb );
2701
2702         filter.f_desc = slap_schema.si_ad_objectClass;
2703         
2704         op->o_tag = LDAP_REQ_SEARCH;
2705
2706         op->ors_filter = &filter;
2707         op->ors_filterstr = filterstr;
2708         op->ors_scope = LDAP_SCOPE_SUBTREE;
2709
2710         op->o_dn = be->be_rootdn;
2711         op->o_ndn = be->be_rootndn;
2712
2713         op->o_req_dn = be->be_suffix[0];
2714         op->o_req_ndn = be->be_nsuffix[0];
2715
2716         op->ors_tlimit = SLAP_NO_LIMIT;
2717         op->ors_slimit = SLAP_NO_LIMIT;
2718
2719         op->ors_attrs = slap_anlist_all_attributes;
2720         op->ors_attrsonly = 0;
2721
2722         op->o_callback = &cb;
2723         cb.sc_private = cfb;
2724
2725         op->o_bd = &cfb->cb_db;
2726         op->o_bd->be_search( op, &rs );
2727         
2728         return 0;
2729 }
2730
2731 static int
2732 CfOcInfo_cmp( const void *c1, const void *c2 ) {
2733         const CfOcInfo *co1 = c1;
2734         const CfOcInfo *co2 = c2;
2735
2736         return ber_bvcmp( co1->co_name, co2->co_name );
2737 }
2738
2739 int
2740 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
2741         int i;
2742         CfOcInfo *co;
2743
2744         i = init_config_attrs( ct );
2745         if ( i ) return i;
2746
2747         /* set up the objectclasses */
2748         i = init_config_ocs( ocs );
2749         if ( i ) return i;
2750
2751         for (i=0; ocs[i].def; i++) {
2752                 if ( ocs[i].oc ) {
2753                         co = ch_malloc( sizeof(CfOcInfo) );
2754                         co->co_name = &(*ocs[i].oc)->soc_cname;
2755                         co->co_table = ct;
2756                         co->co_type = ocs[i].cft;
2757                         avl_insert( &CfOcTree, co, CfOcInfo_cmp, avl_dup_error );
2758                 }
2759         }
2760         return 0;
2761 }
2762
2763 int
2764 read_config(const char *fname, const char *dir) {
2765         BackendDB *be;
2766         CfBackInfo *cfb;
2767
2768         /* Setup the config backend */
2769         be = backend_db_init( "config" );
2770         if ( !be )
2771                 return 1;
2772
2773         /* Setup the underlying back-ldif backend */
2774         if ( config_setup_ldif( be, dir ))
2775                 return 1;
2776
2777 #if 0   /* not yet
2778         /* If we read the config from back-ldif, nothing to do here */
2779         cfb = be->be_private;
2780         if ( cfb->cb_got_ldif )
2781                 return 0;
2782 #endif
2783         ber_str2bv( fname, 0, 1, &cf_prv.c_file );
2784
2785         return read_config_file(fname, 0, NULL);
2786 }
2787
2788 static int
2789 config_back_bind( Operation *op, SlapReply *rs )
2790 {
2791         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
2792                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
2793                 /* frontend sends result */
2794                 return LDAP_SUCCESS;
2795         }
2796
2797         rs->sr_err = LDAP_INVALID_CREDENTIALS;
2798         send_ldap_result( op, rs );
2799
2800         return rs->sr_err;
2801 }
2802
2803 static int
2804 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
2805 {
2806         int rc = 0;
2807
2808         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
2809         {
2810                 rs->sr_attrs = op->ors_attrs;
2811                 rs->sr_entry = ce->ce_entry;
2812                 rc = send_search_entry( op, rs );
2813         }
2814         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
2815                 if ( ce->ce_kids ) {
2816                         rc = config_send( op, rs, ce->ce_kids, 1 );
2817                         if ( rc ) return rc;
2818                 }
2819                 if ( depth ) {
2820                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
2821                                 rc = config_send( op, rs, ce, 0 );
2822                                 if ( rc ) break;
2823                         }
2824                 }
2825         }
2826         return rc;
2827 }
2828
2829 static ConfigTable *
2830 config_find_table( CfOcInfo *co, AttributeDescription *ad )
2831 {
2832         int i;
2833
2834         for (i=0; co->co_table[i].name; i++)
2835                 if ( co->co_table[i].ad == ad )
2836                         return &co->co_table[i];
2837         return NULL;
2838 }
2839
2840 /* Sort the values in an X-ORDERED attribute.
2841  * If the values have no index, leave them in their given order.
2842  * If the values have indexes, sort them and then strip the index.
2843  * If some are indexed and some are not, return Error.
2844  *
2845  * FIXME: This function probably belongs in the frontend somewhere,
2846  * like slap_mods_check.
2847  */
2848 static int
2849 sort_vals( Attribute *a )
2850 {
2851         int i;
2852         int index = 0, noindex = 0;
2853
2854         /* count attrs, look for index */
2855         for (i=0; a->a_vals[i].bv_val; i++) {
2856                 if ( a->a_vals[i].bv_val[0] == '{' ) {
2857                         char *ptr;
2858                         index = 1;
2859                         ptr = strchr( a->a_vals[i].bv_val, '}' );
2860                         if ( !ptr || !ptr[1] )
2861                                 return LDAP_INVALID_SYNTAX;
2862                         if ( noindex )
2863                                 return LDAP_INVALID_SYNTAX;
2864                 } else {
2865                         noindex = 1;
2866                         if ( index )
2867                                 return LDAP_INVALID_SYNTAX;
2868                 }
2869         }
2870
2871         if ( index ) {
2872                 int vals = i, *indexes, j, idx;
2873                 struct berval tmp, ntmp;
2874                 char *ptr;
2875
2876                 /* Strip index from normalized values */
2877                 if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
2878                         a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
2879                         BER_BVZERO(a->a_nvals+vals);
2880                         for ( i=0; i<vals; i++ ) {
2881                                 ptr = strchr(a->a_vals[i].bv_val, '}') + 1;
2882                                 a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
2883                                         (ptr - a->a_vals[i].bv_val);
2884                                 a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
2885                                 strcpy(a->a_nvals[i].bv_val, ptr );
2886                         }
2887                 } else {
2888                         for ( i=0; i<vals; i++ ) {
2889                                 ptr = strchr(a->a_nvals[i].bv_val, '}') + 1;
2890                                 a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
2891                                 strcpy(a->a_nvals[i].bv_val, ptr);
2892                         }
2893                 }
2894                                 
2895                 indexes = ch_malloc( vals * sizeof(int) );
2896                 for ( i=0; i<vals; i++)
2897                         indexes[i] = atoi(a->a_vals[i].bv_val+1);
2898
2899                 /* Insertion sort */
2900                 for ( i=1; i<vals; i++ ) {
2901                         idx = indexes[i];
2902                         tmp = a->a_vals[i];
2903                         ntmp = a->a_nvals[i];
2904                         j = i;
2905                         while ((j > 0) && (indexes[j-1] > idx)) {
2906                                 indexes[j] = indexes[j-1];
2907                                 a->a_vals[j] = a->a_vals[j-1];
2908                                 a->a_nvals[j] = a->a_nvals[j-1];
2909                                 j--;
2910                         }
2911                         indexes[j] = idx;
2912                         a->a_vals[j] = tmp;
2913                         a->a_nvals[j] = ntmp;
2914                 }
2915         }
2916         return 0;
2917 }
2918
2919 static int
2920 check_attr( ConfigTable *ct, ConfigArgs *ca, Attribute *a )
2921 {
2922         int i, rc = 0;
2923
2924         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
2925                 rc = sort_vals( a );
2926                 if ( rc )
2927                         return rc;
2928         }
2929         for ( i=0; a->a_nvals[i].bv_val; i++ ) {
2930                 ca->line = a->a_nvals[i].bv_val;
2931                 rc = config_parse_vals( ct, ca, i );
2932                 if ( rc )
2933                         break;
2934         }
2935         return rc;
2936 }
2937
2938 /* Parse an LDAP entry into config directives */
2939 static int
2940 config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs )
2941 {
2942         CfEntryInfo *ce, *last;
2943         CfOcInfo co, *coptr, **colst = NULL;
2944         Attribute *a, *oc_at, *type_attr;
2945         AttributeDescription *type_ad = NULL;
2946         int i, j, nocs, rc;
2947         ConfigArgs ca = {0};
2948         struct berval pdn;
2949         Entry *xe = NULL;
2950         ConfigTable *ct, *type_ct = NULL;
2951
2952         /* Make sure parent exists and entry does not */
2953         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
2954         if ( ce )
2955                 return LDAP_ALREADY_EXISTS;
2956
2957         dnParent( &e->e_nname, &pdn );
2958
2959         /* If last is NULL, the new entry is the root/suffix entry, 
2960          * otherwise last should be the parent.
2961          */
2962         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn )) {
2963                 if ( rs )
2964                         rs->sr_matched = last->ce_entry->e_name.bv_val;
2965                 return LDAP_NO_SUCH_OBJECT;
2966         }
2967
2968         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
2969         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
2970
2971         /* count the objectclasses */
2972         for ( i=0; oc_at->a_nvals[i].bv_val; i++ );
2973         nocs = i;
2974         colst = (CfOcInfo **)ch_malloc( nocs * sizeof(CfOcInfo *));
2975
2976         for ( i=0, j=0; i<nocs; i++) {
2977                 co.co_name = &oc_at->a_nvals[i];
2978                 coptr = avl_find( CfOcTree, &co, CfOcInfo_cmp );
2979                 
2980                 /* ignore non-config objectclasses. probably should be
2981                  * an error, general data doesn't belong here.
2982                  */
2983                 if ( !coptr ) continue;
2984
2985                 /* Ignore the root objectclass, it has no implementation.
2986                  */
2987                 if ( coptr->co_type == Cft_Abstract ) continue;
2988                 colst[j++] = coptr;
2989         }
2990         nocs = j;
2991
2992         /* Only the root can be Cft_Global, everything else must
2993          * have a parent. Only limited nesting arrangements are allowed.
2994          */
2995         switch( colst[0]->co_type ) {
2996         case Cft_Global:
2997                 if ( last )  {
2998                         rc = LDAP_CONSTRAINT_VIOLATION;
2999                         goto leave;
3000                 }
3001                 break;
3002         case Cft_Schema:
3003         case Cft_Backend:
3004         case Cft_Database:
3005         case Cft_Include:
3006                 if ( !last || ( last->ce_type != Cft_Global &&
3007                         last->ce_type != colst[0]->co_type )) {
3008                         rc = LDAP_CONSTRAINT_VIOLATION;
3009                         goto leave;
3010                 }
3011                 break;
3012         case Cft_Overlay:
3013                 if ( !last || ( last->ce_type != Cft_Global &&
3014                         last->ce_type != Cft_Database &&
3015                         last->ce_type != colst[0]->co_type )) {
3016                         rc = LDAP_CONSTRAINT_VIOLATION;
3017                         goto leave;
3018                 }
3019 #ifdef SLAPD_MODULES
3020         case Cft_Module:
3021                 if ( !last || last->ce_type != Cft_Global ) {
3022                         rc = LDAP_CONSTRAINT_VIOLATION;
3023                         goto leave;
3024                 }
3025 #endif
3026                 break;
3027         }
3028
3029         /* Parse all the values and check for simple syntax errors before
3030          * performing any set actions.
3031          */
3032         switch (colst[0]->co_type) {
3033         case Cft_Schema:
3034                 /* The cn=schema entry is all hardcoded, so never reparse it */
3035                 if (last->ce_type == Cft_Global )
3036                         goto ok;
3037                 /* FALLTHRU */
3038         case Cft_Global:
3039                 ca.be = cfb->cb_be;
3040                 break;
3041
3042         case Cft_Backend:
3043                 if ( last->ce_type == Cft_Backend )
3044                         ca.bi = last->ce_bi;
3045                 else
3046                         type_ad = cfAd_backend;
3047                 break;
3048         case Cft_Database:
3049                 if ( last->ce_type == Cft_Database ) {
3050                         ca.be = last->ce_be;
3051                 } else {
3052                         type_ad = cfAd_database;
3053                         /* dummy, just to get past check_attr */
3054                         ca.be = frontendDB;
3055                 }
3056                 break;
3057
3058         case Cft_Overlay:
3059                 ca.be = last->ce_be;
3060                 type_ad = cfAd_overlay;
3061
3062         case Cft_Include:
3063                 if ( !rs )      /* ignored */
3064                         break;
3065                 type_ad = cfAd_include;
3066 #ifdef SLAPD_MODULES
3067         case Cft_Module: {
3068                 ModPaths *mp;
3069                 char *ptr;
3070                 ptr = strchr( e->e_name.bv_val, '{' );
3071                 if ( !ptr ) {
3072                         rc = LDAP_NAMING_VIOLATION;
3073                         goto leave;
3074                 }
3075                 j = atoi(ptr+1);
3076                 for (i=0, mp=&modpaths; mp && i<j; mp=mp->mp_next);
3077                 /* There is no corresponding modpath for this load? */
3078                 if ( i != j ) {
3079                         rc = LDAP_NAMING_VIOLATION;
3080                         goto leave;
3081                 }
3082                 module_path( mp->mp_path.bv_val );
3083                 ca.private = mp;
3084                 }
3085                 break;
3086 #endif
3087         }
3088         init_config_argv( &ca );
3089         if ( type_ad ) {
3090                 type_attr = attr_find( e->e_attrs, type_ad );
3091                 if ( !type_attr ) {
3092                         rc = LDAP_OBJECT_CLASS_VIOLATION;
3093                         goto leave;
3094                 }
3095                 for ( i=0; i<nocs; i++ ) {
3096                         type_ct = config_find_table( colst[i], type_ad );
3097                         if ( type_ct ) break;
3098                 }
3099                 if ( !type_ct ) {
3100                         rc = LDAP_OBJECT_CLASS_VIOLATION;
3101                         goto leave;
3102                 }
3103                 rc = check_attr( type_ct, &ca, type_attr );
3104                 if ( rc ) goto leave;
3105         }
3106         for ( a=e->e_attrs; a; a=a->a_next ) {
3107                 if ( a == type_attr || a == oc_at ) continue;
3108                 ct = NULL;
3109                 for ( i=0; i<nocs; i++ ) {
3110                         ct = config_find_table( colst[i], a->a_desc );
3111                         if ( ct ) break;
3112                 }
3113                 if ( !ct ) continue;    /* user data? */
3114                 rc = check_attr( ct, &ca, a );
3115                 if ( rc ) goto leave;
3116         }
3117
3118         /* Basic syntax checks are OK. Do the actual settings. */
3119         if ( type_ct ) {
3120                 ca.line = type_attr->a_nvals[0].bv_val;
3121                 rc = config_parse_add( type_ct, &ca, 0 );
3122                 if ( rc ) goto leave;
3123         }
3124         for ( a=e->e_attrs; a; a=a->a_next ) {
3125                 if ( a == type_attr || a == oc_at ) continue;
3126                 ct = NULL;
3127                 for ( i=0; i<nocs; i++ ) {
3128                         ct = config_find_table( colst[i], a->a_desc );
3129                         if ( ct ) break;
3130                 }
3131                 if ( !ct ) continue;    /* user data? */
3132                 for (i=0; a->a_nvals[i].bv_val; i++) {
3133                         ca.line = a->a_nvals[i].bv_val;
3134                         rc = config_parse_add( ct, &ca, i );
3135                         if ( rc ) goto leave;
3136                 }
3137         }
3138 ok:
3139         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3140         ce->ce_parent = last;
3141         ce->ce_entry = entry_dup( e );
3142         ce->ce_entry->e_private = ce;
3143         ce->ce_type = colst[0]->co_type;
3144         if ( !last ) {
3145                 cfb->cb_root = ce;
3146         } else if ( last->ce_kids ) {
3147                 CfEntryInfo *c2;
3148
3149                 for (c2=last->ce_kids; c2 && c2->ce_sibs; c2 = c2->ce_sibs);
3150                 c2->ce_sibs = ce;
3151         } else {
3152                 last->ce_kids = ce;
3153         }
3154
3155 leave:
3156         ch_free( ca.argv );
3157         if ( colst ) ch_free( colst );
3158         return rc;
3159 }
3160
3161 /* Parse an LDAP entry into config directives, then store in underlying
3162  * database.
3163  */
3164 static int
3165 config_back_add( Operation *op, SlapReply *rs )
3166 {
3167         CfBackInfo *cfb;
3168         CfEntryInfo *ce, *last;
3169
3170         if ( !be_isroot( op ) ) {
3171                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3172                 send_ldap_result( op, rs );
3173         }
3174
3175         cfb = (CfBackInfo *)op->o_bd->be_private;
3176
3177         ldap_pvt_thread_pool_pause( &connection_pool );
3178
3179         /* Strategy:
3180          * 1) check for existence of entry
3181          * 2) perform internal add
3182          * 3) store entry in underlying database
3183          */
3184
3185         ldap_pvt_thread_pool_resume( &connection_pool );
3186
3187 out:
3188         send_ldap_result( op, rs );
3189         return rs->sr_err;
3190 }
3191
3192 static int
3193 config_back_modify( Operation *op, SlapReply *rs )
3194 {
3195         CfBackInfo *cfb;
3196         CfEntryInfo *ce, *last;
3197
3198         if ( !be_isroot( op ) ) {
3199                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3200                 send_ldap_result( op, rs );
3201         }
3202
3203         cfb = (CfBackInfo *)op->o_bd->be_private;
3204
3205         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3206         if ( !ce ) {
3207                 if ( last )
3208                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3209                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3210                 goto out;
3211         }
3212         ldap_pvt_thread_pool_pause( &connection_pool );
3213
3214         /* Strategy:
3215          * 1) perform the Modify on the cached Entry.
3216          * 2) verify that the Entry still satisfies the schema.
3217          * 3) perform the individual config operations.
3218          * 4) store Modified entry in underlying LDIF backend.
3219          */
3220         ldap_pvt_thread_pool_resume( &connection_pool );
3221 out:
3222         send_ldap_result( op, rs );
3223         return rs->sr_err;
3224 }
3225
3226 static int
3227 config_back_search( Operation *op, SlapReply *rs )
3228 {
3229         CfBackInfo *cfb;
3230         CfEntryInfo *ce, *last;
3231         int rc;
3232
3233         if ( !be_isroot( op ) ) {
3234                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3235                 send_ldap_result( op, rs );
3236         }
3237
3238         cfb = (CfBackInfo *)op->o_bd->be_private;
3239
3240         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3241         if ( !ce ) {
3242                 if ( last )
3243                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3244                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3245                 goto out;
3246         }
3247         switch ( op->ors_scope ) {
3248         case LDAP_SCOPE_BASE:
3249         case LDAP_SCOPE_SUBTREE:
3250                 config_send( op, rs, ce, 0 );
3251                 break;
3252                 
3253         case LDAP_SCOPE_ONELEVEL:
3254                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
3255                         config_send( op, rs, ce, 1 );
3256                 }
3257                 break;
3258         }
3259                 
3260         rs->sr_err = LDAP_SUCCESS;
3261 out:
3262         send_ldap_result( op, rs );
3263         return 0;
3264 }
3265
3266 static Entry *
3267 config_alloc_entry( CfEntryInfo *parent, struct berval *rdn )
3268 {
3269         Entry *e = ch_calloc( 1, sizeof(Entry) );
3270         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3271         struct berval pdn;
3272
3273         e->e_private = ce;
3274         ce->ce_entry = e;
3275         ce->ce_parent = parent;
3276         if ( parent ) {
3277                 pdn = parent->ce_entry->e_nname;
3278         } else {
3279                 BER_BVZERO( &pdn );
3280         }
3281
3282         build_new_dn( &e->e_name, &pdn, rdn, NULL );
3283         ber_dupbv( &e->e_nname, &e->e_name );
3284         return e;
3285 }
3286
3287 #define NO_TABLE        0
3288 #define BI_TABLE        1
3289 #define BE_TABLE        2
3290
3291 static int
3292 config_build_entry( ConfigArgs *c, Entry *e, ObjectClass *oc,
3293          struct berval *rdn, ConfigTable *ct, int table )
3294 {
3295         struct berval vals[2];
3296         struct berval ad_name;
3297         AttributeDescription *ad = NULL;
3298         int rc, i;
3299         char *ptr;
3300         const char *text;
3301         char textbuf[SLAP_TEXT_BUFLEN];
3302         size_t textlen = sizeof(textbuf);
3303         AttributeType **at;
3304         Attribute *oc_at;
3305
3306         BER_BVZERO( &vals[1] );
3307
3308         vals[0] = oc->soc_cname;
3309         attr_merge(e, slap_schema.si_ad_objectClass, vals, NULL );
3310         ptr = strchr(rdn->bv_val, '=');
3311         ad_name.bv_val = rdn->bv_val;
3312         ad_name.bv_len = ptr - rdn->bv_val;
3313         rc = slap_bv2ad( &ad_name, &ad, &text );
3314         if ( rc ) {
3315                 return rc;
3316         }
3317         vals[0].bv_val = ptr+1;
3318         vals[0].bv_len = rdn->bv_len - (vals[0].bv_val - rdn->bv_val);
3319         attr_merge(e, ad, vals, NULL );
3320
3321         for (at=oc->soc_required; at && *at; at++) {
3322                 /* Skip the naming attr */
3323                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
3324                         continue;
3325                 for (i=0;ct[i].name;i++) {
3326                         if (ct[i].ad == (*at)->sat_ad) {
3327                                 rc = config_get_vals(&ct[i], c);
3328                                 if (rc == LDAP_SUCCESS) {
3329                                         attr_merge(e, ct[i].ad, c->rvalue_vals, c->rvalue_nvals);
3330                                         ber_bvarray_free( c->rvalue_nvals );
3331                                         ber_bvarray_free( c->rvalue_vals );
3332                                 }
3333                                 break;
3334                         }
3335                 }
3336         }
3337
3338         for (at=oc->soc_allowed; at && *at; at++) {
3339                 /* Skip the naming attr */
3340                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
3341                         continue;
3342                 for (i=0;ct[i].name;i++) {
3343                         if (ct[i].ad == (*at)->sat_ad) {
3344                                 rc = config_get_vals(&ct[i], c);
3345                                 if (rc == LDAP_SUCCESS) {
3346                                         attr_merge(e, ct[i].ad, c->rvalue_vals, c->rvalue_nvals);
3347                                         ber_bvarray_free( c->rvalue_nvals );
3348                                         ber_bvarray_free( c->rvalue_vals );
3349                                 }
3350                                 break;
3351                         }
3352                 }
3353         }
3354
3355         if ( table ) {
3356                 if ( table == BI_TABLE )
3357                         ct = c->bi->bi_cf_table;
3358                 else
3359                         ct = c->be->be_cf_table;
3360                 for (;ct && ct->name;ct++) {
3361                         if (!ct->ad) continue;
3362                         rc = config_get_vals(ct, c);
3363                         if (rc == LDAP_SUCCESS) {
3364                                 attr_merge(e, ct->ad, c->rvalue_vals, c->rvalue_nvals);
3365                         }
3366                 }
3367         }
3368         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3369         rc = structural_class(oc_at->a_vals, vals, NULL, &text, textbuf, textlen);
3370         BER_BVZERO( &vals[1] );
3371         attr_merge(e, slap_schema.si_ad_structuralObjectClass, vals, NULL );
3372
3373         return 0;
3374 }
3375
3376 static void
3377 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
3378         Operation *op, SlapReply *rs )
3379 {
3380         Entry *e;
3381         ConfigFile *cf = c->private;
3382         CfEntryInfo *ce, *ceprev;
3383         char *ptr;
3384         struct berval bv;
3385
3386         if ( ceparent->ce_kids ) {
3387                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3388                         ceprev = ceprev->ce_sibs );
3389         }
3390
3391         for (; cf; cf=cf->c_sibs, c->depth++) {
3392                 c->value_dn.bv_val = c->log;
3393                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
3394                 if ( !bv.bv_val ) {
3395                         bv = cf->c_file;
3396                 } else {
3397                         bv.bv_val++;
3398                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
3399                 }
3400                 ptr = strchr( bv.bv_val, '.' );
3401                 if ( ptr )
3402                         bv.bv_len = ptr - bv.bv_val;
3403                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn={%02d}", c->depth);
3404                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
3405                         bv.bv_len );
3406                 c->value_dn.bv_len += bv.bv_len;
3407                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
3408
3409                 e = config_alloc_entry( ceparent, &c->value_dn );
3410                 c->private = cf;
3411                 config_build_entry( c, e, cfOc_schema, &c->value_dn,
3412                         c->bi->bi_cf_table, NO_TABLE );
3413                 ce = e->e_private;
3414                 ce->ce_type = Cft_Schema;
3415                 op->ora_e = e;
3416                 op->o_bd->be_add( op, rs );
3417                 ce->ce_bi = c->bi;
3418                 if ( !ceparent->ce_kids ) {
3419                         ceparent->ce_kids = ce;
3420                 } else {
3421                         ceprev->ce_sibs = ce;
3422                 }
3423                 ceprev = ce;
3424                 if ( cf->c_kids ) {
3425                         c->private = cf->c_kids;
3426                         config_build_schema_inc( c, ceparent, op, rs );
3427                 }
3428         }
3429 }
3430
3431 static CfEntryInfo *
3432 config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
3433         Operation *op, SlapReply *rs )
3434 {
3435         Entry *e;
3436         int i;
3437         ConfigFile *cf = c->private;
3438         CfEntryInfo *ce, *ceprev;
3439
3440         if ( ceparent->ce_kids ) {
3441                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3442                         ceprev = ceprev->ce_sibs );
3443         }
3444
3445         for (i=0; cf; cf=cf->c_sibs, i++) {
3446                 c->value_dn.bv_val = c->log;
3447                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=include{%02d}", i);
3448                 e = config_alloc_entry( ceparent, &c->value_dn );
3449                 c->private = cf;
3450                 config_build_entry( c, e, cfOc_include, &c->value_dn,
3451                         c->bi->bi_cf_table, NO_TABLE );
3452                 op->ora_e = e;
3453                 op->o_bd->be_add( op, rs );
3454                 ce = e->e_private;
3455                 ce->ce_type = Cft_Include;
3456                 ce->ce_bi = c->bi;
3457                 if ( !ceparent->ce_kids ) {
3458                         ceparent->ce_kids = ce;
3459                 } else {
3460                         ceprev->ce_sibs = ce;
3461                 }
3462                 ceprev = ce;
3463                 if ( cf->c_kids ) {
3464                         c->private = cf->c_kids;
3465                         config_build_includes( c, ce, op, rs );
3466                 }
3467         }
3468         return ce;
3469 }
3470
3471 #ifdef SLAPD_MODULES
3472
3473 static CfEntryInfo *
3474 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
3475         Operation *op, SlapReply *rs )
3476 {
3477         Entry *e;
3478         int i;
3479         CfEntryInfo *ce, *ceprev;
3480         ModPaths *mp;
3481
3482         if ( ceparent->ce_kids ) {
3483                 for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
3484                         ceprev = ceprev->ce_sibs );
3485         }
3486
3487         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
3488                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
3489                         continue;
3490                 c->value_dn.bv_val = c->log;
3491                 c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=module{%02d}", i);
3492                 e = config_alloc_entry( ceparent, &c->value_dn );
3493                 ce = e->e_private;
3494                 ce->ce_type = Cft_Include;
3495                 c->private = mp;
3496                 config_build_entry( c, e, cfOc_module, &c->value_dn,
3497                         c->bi->bi_cf_table, NO_TABLE );
3498                 op->ora_e = e;
3499                 op->o_bd->be_add( op, rs );
3500                 ce->ce_bi = c->bi;
3501                 if ( !ceparent->ce_kids ) {
3502                         ceparent->ce_kids = ce;
3503                 } else {
3504                         ceprev->ce_sibs = ce;
3505                 }
3506                 ceprev = ce;
3507         }
3508         return ce;
3509 }
3510 #endif
3511
3512 static int
3513 config_back_db_open( BackendDB *be )
3514 {
3515         CfBackInfo *cfb = be->be_private;
3516         struct berval rdn;
3517         Entry *e, *parent;
3518         CfEntryInfo *ce, *ceparent, *ceprev;
3519         int i, rc;
3520         BackendInfo *bi;
3521         BackendDB *bptr;
3522         ConfigArgs c;
3523         ConfigTable *ct;
3524         Connection conn = {0};
3525         char opbuf[OPERATION_BUFFER_SIZE];
3526         Operation *op;
3527         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
3528         SlapReply rs = {REP_RESULT};
3529
3530         /* If we read the config from back-ldif, nothing to do here */
3531         if ( cfb->cb_got_ldif )
3532                 return 0;
3533
3534         op = (Operation *)opbuf;
3535         connection_fake_init( &conn, op, cfb );
3536
3537         op->o_dn = be->be_rootdn;
3538         op->o_ndn = be->be_rootndn;
3539
3540         op->o_tag = LDAP_REQ_ADD;
3541         op->o_callback = &cb;
3542         op->o_bd = &cfb->cb_db;
3543
3544         /* create root of tree */
3545         rdn = config_rdn;
3546         e = config_alloc_entry( NULL, &rdn );
3547         ce = e->e_private;
3548         ce->ce_type = Cft_Global;
3549         cfb->cb_root = ce;
3550         c.be = be;
3551         c.bi = be->bd_info;
3552         c.private = cfb->cb_config;
3553         ct = c.bi->bi_cf_table;
3554         config_build_entry( &c, e, cfOc_global, &rdn, ct, NO_TABLE );
3555         op->ora_e = e;
3556         op->o_bd->be_add( op, &rs );
3557         ce->ce_bi = c.bi;
3558
3559         parent = e;
3560         ceparent = ce;
3561
3562         /* Create schema nodes... cn=schema will contain the hardcoded core
3563          * schema, read-only. Child objects will contain runtime loaded schema
3564          * files.  FIXME
3565          */
3566         rdn = schema_rdn;
3567         e = config_alloc_entry( ceparent, &rdn );
3568         ce = e->e_private;
3569         ce->ce_type = Cft_Schema;
3570         c.private = NULL;
3571         config_build_entry( &c, e, cfOc_schema, &rdn, ct, NO_TABLE );
3572         op->ora_e = e;
3573         op->o_bd->be_add( op, &rs );
3574         if ( !ceparent->ce_kids ) {
3575                 ceparent->ce_kids = ce;
3576         } else {
3577                 ceprev->ce_sibs = ce;
3578         }
3579         ceprev = ce;
3580
3581         /* Create includeFile nodes and schema nodes for included schema... */
3582         if ( cfb->cb_config->c_kids ) {
3583                 c.depth = 0;
3584                 c.private = cfb->cb_config->c_kids;
3585                 config_build_schema_inc( &c, ce, op, &rs );
3586                 c.private = cfb->cb_config->c_kids;
3587                 ceprev = config_build_includes( &c, ceparent, op, &rs );
3588         }
3589
3590 #ifdef SLAPD_MODULES
3591         /* Create Module nodes... */
3592         if ( modpaths.mp_loads ) {
3593                 ceprev = config_build_includes( &c, ceparent, op, &rs );
3594         }
3595 #endif
3596
3597         /* Create backend nodes. Skip if they don't provide a cf_table.
3598          * There usually aren't any of these.
3599          */
3600         
3601         c.line = 0;
3602         bi = backendInfo;
3603         for (i=0; i<nBackendInfo; i++, bi++) {
3604                 if (!bi->bi_cf_table) continue;
3605                 if (!bi->bi_private) continue;
3606
3607                 rdn.bv_val = c.log;
3608                 rdn.bv_len = sprintf(rdn.bv_val, "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
3609                 e = config_alloc_entry( ceparent, &rdn );
3610                 ce = e->e_private;
3611                 ce->ce_type = Cft_Backend;
3612                 ce->ce_bi = bi;
3613                 c.bi = bi;
3614                 config_build_entry( &c, e, cfOc_backend, &rdn, ct, BI_TABLE );
3615                 op->ora_e = e;
3616                 op->o_bd->be_add( op, &rs );
3617                 if ( !ceparent->ce_kids ) {
3618                         ceparent->ce_kids = ce;
3619                 } else {
3620                         ceprev->ce_sibs = ce;
3621                 }
3622                 ceprev = ce;
3623         }
3624
3625         /* Create database nodes... */
3626         for (i=0; i<nBackendDB; i++) {
3627                 slap_overinfo *oi = NULL;
3628                 if ( i == 0 ) {
3629                         bptr = frontendDB;
3630                 } else {
3631                         bptr = &backendDB[i];
3632                 }
3633                 if ( overlay_is_over( bptr )) {
3634                         oi = bptr->bd_info->bi_private;
3635                         bi = oi->oi_orig;
3636                 } else {
3637                         bi = bptr->bd_info;
3638                 }
3639                 rdn.bv_val = c.log;
3640                 rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s", cfAd_database->ad_cname.bv_val,
3641                         i, bi->bi_type);
3642                 e = config_alloc_entry( ceparent, &rdn );
3643                 ce = e->e_private;
3644                 c.be = bptr;
3645                 c.bi = bi;
3646                 ce->ce_type = Cft_Database;
3647                 ce->ce_be = c.be;
3648                 ce->ce_bi = c.bi;
3649                 config_build_entry( &c, e, cfOc_database, &rdn, ct, BE_TABLE );
3650                 op->ora_e = e;
3651                 op->o_bd->be_add( op, &rs );
3652                 if ( !ceparent->ce_kids ) {
3653                         ceparent->ce_kids = ce;
3654                 } else {
3655                         ceprev->ce_sibs = ce;
3656                 }
3657                 ceprev = ce;
3658                 /* Iterate through overlays */
3659                 if ( oi ) {
3660                         slap_overinst *on;
3661                         Entry *oe;
3662                         CfEntryInfo *opar = ce, *oprev = NULL;
3663                         int j;
3664
3665                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
3666                                 rdn.bv_val = c.log;
3667                                 rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s",
3668                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
3669                                 oe = config_alloc_entry( opar, &rdn );
3670                                 ce = oe->e_private;
3671                                 c.be = bptr;
3672                                 c.bi = &on->on_bi;
3673                                 ce->ce_type = Cft_Overlay;
3674                                 ce->ce_be = c.be;
3675                                 ce->ce_bi = c.bi;
3676                                 config_build_entry( &c, oe, cfOc_overlay, &rdn, ct, BI_TABLE );
3677                                 op->ora_e = oe;
3678                                 op->o_bd->be_add( op, &rs );
3679                                 if ( !opar->ce_kids ) {
3680                                         opar->ce_kids = ce;
3681                                 } else {
3682                                         oprev->ce_sibs = ce;
3683                                 }
3684                                 oprev = ce;
3685                         }
3686                 }
3687         }
3688
3689         return 0;
3690 }
3691
3692 static int
3693 config_back_db_destroy( Backend *be )
3694 {
3695         free( be->be_private );
3696         return 0;
3697 }
3698
3699 static int
3700 config_back_db_init( Backend *be )
3701 {
3702         struct berval dn;
3703         CfBackInfo *cfb;
3704
3705         cfb = ch_calloc( 1, sizeof(CfBackInfo));
3706         cfb->cb_config = &cf_prv;
3707         cfb->cb_be = be;
3708         be->be_private = cfb;
3709
3710         ber_dupbv( &be->be_rootdn, &config_rdn );
3711         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
3712         ber_dupbv( &dn, &be->be_rootdn );
3713         ber_bvarray_add( &be->be_suffix, &dn );
3714         ber_dupbv( &dn, &be->be_rootdn );
3715         ber_bvarray_add( &be->be_nsuffix, &dn );
3716
3717         /* Hide from namingContexts */
3718         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
3719
3720         return 0;
3721 }
3722
3723 static struct {
3724         char *name;
3725         AttributeDescription **desc;
3726 } ads[] = {
3727         { "backend", &cfAd_backend },
3728         { "database", &cfAd_database },
3729         { "include", &cfAd_include },
3730         { "overlay", &cfAd_overlay },
3731         { NULL, NULL, NULL }
3732 };
3733
3734 /* Notes:
3735  *   add / delete: all types that may be added or deleted must use an
3736  * X-ORDERED attributeType for their RDN. Adding and deleting entries
3737  * should automatically renumber the index of any siblings as needed,
3738  * so that no gaps in the numbering sequence exist after the add/delete
3739  * is completed.
3740  *   What can be added:
3741  *     schema objects
3742  *     backend objects for backend-specific config directives
3743  *     database objects
3744  *     overlay objects
3745  *
3746  *   delete: probably no support this time around.
3747  *
3748  *   modrdn: generally not done. Will be invoked automatically by add/
3749  * delete to update numbering sequence. Perform as an explicit operation
3750  * so that the renumbering effect may be replicated. Subtree rename must
3751  * be supported, since renumbering a database will affect all its child
3752  * overlays.
3753  *
3754  *  modify: must be fully supported. 
3755  */
3756
3757 int
3758 config_back_initialize( BackendInfo *bi )
3759 {
3760         ConfigTable *ct = config_back_cf_table;
3761         char *argv[4];
3762         int i;
3763
3764         bi->bi_open = 0;
3765         bi->bi_close = 0;
3766         bi->bi_config = 0;
3767         bi->bi_destroy = 0;
3768
3769         bi->bi_db_init = config_back_db_init;
3770         bi->bi_db_config = 0;
3771         bi->bi_db_open = config_back_db_open;
3772         bi->bi_db_close = 0;
3773         bi->bi_db_destroy = config_back_db_destroy;
3774
3775         bi->bi_op_bind = config_back_bind;
3776         bi->bi_op_unbind = 0;
3777         bi->bi_op_search = config_back_search;
3778         bi->bi_op_compare = 0;
3779         bi->bi_op_modify = config_back_modify;
3780         bi->bi_op_modrdn = 0;
3781         bi->bi_op_add = 0;
3782         bi->bi_op_delete = 0;
3783         bi->bi_op_abandon = 0;
3784
3785         bi->bi_extended = 0;
3786
3787         bi->bi_chk_referrals = 0;
3788
3789         bi->bi_connection_init = 0;
3790         bi->bi_connection_destroy = 0;
3791
3792         argv[3] = NULL;
3793         for (i=0; OidMacros[i].name; i++ ) {
3794                 argv[1] = OidMacros[i].name;
3795                 argv[2] = OidMacros[i].oid;
3796                 parse_oidm( "slapd", i, 3, argv, 0, NULL );
3797         }
3798
3799         bi->bi_cf_table = ct;
3800
3801         i = config_register_schema( ct, cf_ocs );
3802         if ( i ) return i;
3803
3804         /* set up the notable AttributeDescriptions */
3805         i = 0;
3806         for (;ct->name;ct++) {
3807                 if (strcmp(ct->name, ads[i].name)) continue;
3808                 *ads[i].desc = ct->ad;
3809                 i++;
3810                 if (!ads[i].name) break;
3811         }
3812
3813         return 0;
3814 }
3815