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