Start to rework provision for LDAP backends
[samba.git] / source4 / torture / util_provision.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "dsdb/samdb/samdb.h"
22 #include "lib/appweb/ejs/ejs.h"
23 #include "lib/appweb/ejs/ejsInternal.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "auth/auth.h"
26 #include "lib/ldb_wrap.h"
27 #include "torture/util.h"
28
29 static EjsId eid;
30 static int ejs_error;
31
32 static void test_ejs_exception(const char *reason)
33 {
34         Ejs *ep = ejsPtr(eid);
35         ejsSetErrorMsg(eid, "%s", reason);
36         fprintf(stderr, "%s", ep->error);
37         ejs_error = 127;
38 }
39
40 static int test_run_ejs(char *script)
41 {
42         EjsHandle handle = 0;
43         MprVar result;
44         char *emsg;
45         TALLOC_CTX *mem_ctx = talloc_new(NULL);
46         struct MprVar *return_var;
47
48         mprSetCtx(mem_ctx);
49
50         if (ejsOpen(NULL, NULL, NULL) != 0) {
51                 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
52                 ejs_error = 127;
53                 goto failed;
54         }
55
56         smb_setup_ejs_functions(test_ejs_exception);
57
58         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
59                 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
60                 ejs_error = 127;
61                 goto failed;
62         }
63
64         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
65
66         /* run the script */
67         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
68                 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
69                 if (ejs_error == 0) ejs_error = 127;
70                 goto failed;
71         }
72
73         return_var = ejsGetReturnValue(eid);
74         ejs_error = mprVarToNumber(return_var);
75
76 failed:
77         ejsClose();
78         talloc_free(mem_ctx);
79         return ejs_error;
80 }
81
82 static NTSTATUS provision_bare_ejs(TALLOC_CTX *mem_ctx, 
83                                                                    struct loadparm_context *lp_ctx,
84                                                                    struct provision_settings *settings)
85 {
86         char *ejs;
87         int ret;
88         bool ok;
89         struct ldb_context *ldb;
90
91         DEBUG(0,("Provision for Become-DC test using EJS\n"));
92
93         DEBUG(0,("New Server[%s] in Site[%s]\n", settings->dns_name, 
94                          settings->site_name));
95
96         DEBUG(0,("DSA Instance [%s]\n"
97                 "\tobjectGUID[%s]\n"
98                 "\tinvocationId[%s]\n",
99                 settings->ntds_dn_str,
100                 GUID_string(mem_ctx, settings->ntds_guid),
101                 GUID_string(mem_ctx, settings->invocation_id)));
102
103         DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
104                  "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
105                 lp_private_dir(lp_ctx),
106                 settings->samdb_ldb,
107                 settings->secrets_ldb,
108                 settings->secrets_keytab));
109
110         DEBUG(0,("Schema Partition[%s => %s]\n",
111                 settings->schema_dn_str, settings->schemadn_ldb));
112
113         DEBUG(0,("Config Partition[%s => %s]\n",
114                 settings->config_dn_str, settings->configdn_ldb));
115
116         DEBUG(0,("Domain Partition[%s => %s]\n",
117                 settings->domain_dn_str, settings->domaindn_ldb));
118
119         ejs = talloc_asprintf(mem_ctx,
120                 "libinclude(\"base.js\");\n"
121                 "libinclude(\"provision.js\");\n"
122                 "\n"
123                 "function message() { print(vsprintf(arguments)); }\n"
124                 "\n"
125                 "var subobj = provision_guess();\n"
126                 "subobj.ROOTDN       = \"%s\";\n"
127                 "subobj.DOMAINDN     = \"%s\";\n"
128                 "subobj.DOMAINDN_LDB = \"%s\";\n"
129                 "subobj.CONFIGDN     = \"%s\";\n"
130                 "subobj.CONFIGDN_LDB = \"%s\";\n"
131                 "subobj.SCHEMADN     = \"%s\";\n"
132                 "subobj.SCHEMADN_LDB = \"%s\";\n"
133                 "subobj.HOSTNAME     = \"%s\";\n"
134                 "subobj.REALM        = \"%s\";\n"
135                 "subobj.DOMAIN       = \"%s\";\n"
136                 "subobj.DEFAULTSITE  = \"%s\";\n"
137                 "\n"
138                 "subobj.KRBTGTPASS   = \"_NOT_USED_\";\n"
139                 "subobj.MACHINEPASS  = \"%s\";\n"
140                 "subobj.ADMINPASS    = \"_NOT_USED_\";\n"
141                 "\n"
142                 "var paths = provision_default_paths(subobj);\n"
143                 "paths.samdb = \"%s\";\n"
144                 "paths.secrets = \"%s\";\n"
145                 "paths.templates = \"%s\";\n"
146                 "paths.keytab = \"%s\";\n"
147                 "paths.dns_keytab = \"%s\";\n"
148                 "\n"
149                 "var system_session = system_session();\n"
150                 "\n"
151                 "var ok = provision_become_dc(subobj, message, true, paths, system_session);\n"
152                 "assert(ok);\n"
153                 "\n"
154                 "return 0;\n",
155                 settings->root_dn_str,          /* subobj.ROOTDN */
156                 settings->domain_dn_str,                /* subobj.DOMAINDN */
157                 settings->domaindn_ldb,         /* subobj.DOMAINDN_LDB */
158                 settings->config_dn_str,        /* subobj.CONFIGDN */
159                 settings->configdn_ldb,         /* subobj.CONFIGDN_LDB */
160                 settings->schema_dn_str,        /* subobj.SCHEMADN */
161                 settings->schemadn_ldb,         /* subobj.SCHEMADN_LDB */
162                 settings->netbios_name, /* subobj.HOSTNAME */
163                 settings->realm,/* subobj.REALM */
164                 settings->domain,/* subobj.DOMAIN */
165                 settings->site_name,            /* subobj.DEFAULTSITE */
166                 settings->machine_password,/* subobj.MACHINEPASS */
167                 settings->samdb_ldb,            /* paths.samdb */
168                 settings->templates_ldb,                /* paths.templates */
169                 settings->secrets_ldb,          /* paths.secrets */
170                 settings->secrets_keytab,               /* paths.keytab */
171                 settings->dns_keytab);          /* paths.dns_keytab */
172         NT_STATUS_HAVE_NO_MEMORY(ejs);
173
174         ret = test_run_ejs(ejs);
175         if (ret != 0) {
176                 DEBUG(0,("Failed to run ejs script: %d:\n%s",
177                         ret, ejs));
178                 talloc_free(ejs);
179                 return NT_STATUS_FOOBAR;
180         }
181         talloc_free(ejs);
182
183         DEBUG(0,("Open the SAM LDB with system credentials: %s\n", 
184                  settings->samdb_ldb));
185
186         ldb = ldb_wrap_connect(mem_ctx, lp_ctx, settings->samdb_ldb,
187                                   system_session(mem_ctx, lp_ctx),
188                                   NULL, 0, NULL);
189         if (!ldb) {
190                 DEBUG(0,("Failed to open '%s'\n",
191                         settings->samdb_ldb));
192                 return NT_STATUS_INTERNAL_DB_ERROR;
193         }
194
195         ok = samdb_set_ntds_invocation_id(ldb, settings->invocation_id);
196         if (!ok) {
197                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
198                 return NT_STATUS_FOOBAR;
199         }
200         ok = samdb_set_ntds_objectGUID(ldb, settings->ntds_guid);
201         if (!ok) {
202                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
203                 return NT_STATUS_FOOBAR;
204         }
205
206         return NT_STATUS_OK;
207 }
208
209 #include "param/param.h"
210 #include <Python.h>
211 #include "scripting/python/modules.h"
212
213 static NTSTATUS provision_bare_py(TALLOC_CTX *mem_ctx, 
214                                                                   struct loadparm_context *lp_ctx,
215                                                                   struct provision_settings *settings)
216 {
217         bool ok;
218         PyObject *provision_mod, *provision_dict, *provision_fn, *result, *parameters;
219         struct ldb_context *ldb;
220
221         DEBUG(0,("Provision for Become-DC test using python\n"));
222
223         py_load_samba_modules();
224         Py_Initialize();
225         py_update_path("bin"); /* FIXME: Can't assume this is always the case */
226
227         provision_mod = PyImport_Import(PyString_FromString("samba.provision"));
228
229         if (provision_mod == NULL) {
230                 PyErr_Print();
231                 DEBUG(0, ("Unable to import provision Python module.\n"));
232                 return NT_STATUS_UNSUCCESSFUL;
233         }
234
235         provision_dict = PyModule_GetDict(provision_mod);
236
237         if (provision_dict == NULL) {
238                 DEBUG(0, ("Unable to get dictionary for provision module\n"));
239                 return NT_STATUS_UNSUCCESSFUL;
240         }
241
242         provision_fn = PyDict_GetItemString(provision_dict, "provision");
243         if (provision_fn == NULL) {
244                 PyErr_Print();
245                 DEBUG(0, ("Unable to get provision function\n"));
246                 return NT_STATUS_UNSUCCESSFUL;
247         }
248         
249         DEBUG(0,("New Server[%s] in Site[%s]\n", settings->dns_name, 
250                          settings->site_name));
251
252         DEBUG(0,("DSA Instance [%s]\n"
253                 "\tobjectGUID[%s]\n"
254                 "\tinvocationId[%s]\n",
255                 settings->ntds_dn_str,
256                 settings->ntds_guid == NULL?"None":GUID_string(mem_ctx, settings->ntds_guid),
257                 settings->invocation_id == NULL?"None":GUID_string(mem_ctx, settings->invocation_id)));
258
259         DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
260                  "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
261                 lp_private_dir(lp_ctx),
262                 settings->samdb_ldb,
263                 settings->secrets_ldb,
264                 settings->secrets_keytab));
265
266         DEBUG(0,("Schema Partition[%s => %s]\n",
267                 settings->schema_dn_str, settings->schemadn_ldb));
268
269         DEBUG(0,("Config Partition[%s => %s]\n",
270                 settings->config_dn_str, settings->configdn_ldb));
271
272         DEBUG(0,("Domain Partition[%s => %s]\n",
273                 settings->domain_dn_str, settings->domaindn_ldb));
274
275         parameters = PyDict_New();
276
277         PyDict_SetItemString(parameters, "rootdn", 
278                                                  PyString_FromString(settings->root_dn_str));
279         if (settings->domaindn_ldb != NULL)
280                 PyDict_SetItemString(parameters, "domaindn_ldb", 
281                                                          PyString_FromString(settings->domaindn_ldb));
282         if (settings->config_dn_str != NULL)
283                 PyDict_SetItemString(parameters, "configdn", 
284                                                          PyString_FromString(settings->config_dn_str));
285         if (settings->configdn_ldb != NULL)
286                 PyDict_SetItemString(parameters, "configdn_ldb", 
287                                                          PyString_FromString(settings->configdn_ldb));
288         if (settings->schema_dn_str != NULL)
289                 PyDict_SetItemString(parameters, "schema_dn_str", 
290                                                          PyString_FromString(settings->schema_dn_str));
291         if (settings->schemadn_ldb != NULL)
292                 PyDict_SetItemString(parameters, "schemadn_ldb", 
293                                                          PyString_FromString(settings->schemadn_ldb));
294         PyDict_SetItemString(parameters, "hostname", 
295                                                  PyString_FromString(settings->netbios_name));
296         PyDict_SetItemString(parameters, "sitename", 
297                                                  PyString_FromString(settings->site_name));
298         PyDict_SetItemString(parameters, "machinepass", 
299                                                  PyString_FromString(settings->machine_password));
300         if (settings->samdb_ldb != NULL)
301                 PyDict_SetItemString(parameters, "samdb", 
302                                                          PyString_FromString(settings->samdb_ldb));
303         if (settings->secrets_ldb != NULL)
304                 PyDict_SetItemString(parameters, "secrets_ldb", 
305                                                          PyString_FromString(settings->secrets_ldb));
306         if (settings->secrets_keytab != NULL)
307                 PyDict_SetItemString(parameters, "secrets_keytab", 
308                                                          PyString_FromString(settings->secrets_keytab));
309
310         result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
311
312         Py_DECREF(parameters);
313
314         if (result == NULL) {
315                 PyErr_Print();
316                 PyErr_Clear();
317                 return NT_STATUS_UNSUCCESSFUL;
318         }
319
320         DEBUG(0,("Open the SAM LDB with system credentials: %s\n", 
321                  settings->samdb_ldb));
322
323         ldb = ldb_wrap_connect(mem_ctx, lp_ctx, settings->samdb_ldb,
324                                   system_session(mem_ctx, lp_ctx),
325                                   NULL, 0, NULL);
326         if (!ldb) {
327                 DEBUG(0,("Failed to open '%s'\n", settings->samdb_ldb));
328                 return NT_STATUS_INTERNAL_DB_ERROR;
329         }
330
331         ok = samdb_set_ntds_invocation_id(ldb, settings->invocation_id);
332         if (!ok) {
333                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
334                 return NT_STATUS_FOOBAR;
335         }
336         ok = samdb_set_ntds_objectGUID(ldb, settings->ntds_guid);
337         if (!ok) {
338                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
339                 return NT_STATUS_FOOBAR;
340         }
341
342         return NT_STATUS_OK;
343 }
344
345 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
346                                                 struct provision_settings *settings)
347 {
348         if (getenv("PROVISION_EJS")) {
349                 return provision_bare_ejs(mem_ctx, lp_ctx, settings);
350         } else {
351                 return provision_bare_py(mem_ctx, lp_ctx, settings);
352         }
353 }
354
355