Avoid using private libnet test structure inside util_provision.
[jelmer/samba4-debian.git] / source / 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
27 static EjsId eid;
28 static int ejs_error;
29
30 static void test_ejs_exception(const char *reason)
31 {
32         Ejs *ep = ejsPtr(eid);
33         ejsSetErrorMsg(eid, "%s", reason);
34         fprintf(stderr, "%s", ep->error);
35         ejs_error = 127;
36 }
37
38 static int test_run_ejs(char *script)
39 {
40         EjsHandle handle = 0;
41         MprVar result;
42         char *emsg;
43         TALLOC_CTX *mem_ctx = talloc_new(NULL);
44         struct MprVar *return_var;
45
46         mprSetCtx(mem_ctx);
47
48         if (ejsOpen(NULL, NULL, NULL) != 0) {
49                 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
50                 ejs_error = 127;
51                 goto failed;
52         }
53
54         smb_setup_ejs_functions(test_ejs_exception);
55
56         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
57                 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
58                 ejs_error = 127;
59                 goto failed;
60         }
61
62         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
63
64         /* run the script */
65         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
66                 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
67                 if (ejs_error == 0) ejs_error = 127;
68                 goto failed;
69         }
70
71         return_var = ejsGetReturnValue(eid);
72         ejs_error = mprVarToNumber(return_var);
73
74 failed:
75         ejsClose();
76         talloc_free(mem_ctx);
77         return ejs_error;
78 }
79
80 static NTSTATUS provision_bare_ejs(TALLOC_CTX *mem_ctx, 
81                                                                    struct loadparm_context *lp_ctx,
82                                                                    const char *dns_name,
83                                                                    const char *site_name,
84                                                                    const char *root_dn_str,
85                                                                    const char *domain_dn_str,
86                                                                    const char *config_dn_str,
87                                                                    const char *schema_dn_str,
88                                                                    const struct GUID *invocation_id,
89                                                                    const char *netbios_name,
90                                                                    const char *realm,
91                                                                    const char *domain)
92 {
93         char *ejs;
94         int ret;
95         bool ok;
96         struct ldb_context *ldb;
97
98         DEBUG(0,("Provision for Become-DC test using EJS\n"));
99
100         DEBUG(0,("New Server[%s] in Site[%s]\n", dns_name, site_name));
101
102         DEBUG(0,("DSA Instance [%s]\n"
103                 "\tobjectGUID[%s]\n"
104                 "\tinvocationId[%s]\n",
105                 p->dest_dsa->ntds_dn_str,
106                 GUID_string(mem_ctx, &p->dest_dsa->ntds_guid),
107                 GUID_string(mem_ctx, invocation_id)));
108
109         DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
110                  "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
111                 lp_private_dir(lp_ctx),
112                 s->path.samdb_ldb,
113                 s->path.secrets_ldb,
114                 s->path.secrets_keytab));
115
116         DEBUG(0,("Schema Partition[%s => %s]\n",
117                 schema_dn_str, s->path.schemadn_ldb));
118
119         DEBUG(0,("Config Partition[%s => %s]\n",
120                 config_dn_str, s->path.configdn_ldb));
121
122         DEBUG(0,("Domain Partition[%s => %s]\n",
123                 domain_dn_str, s->path.domaindn_ldb));
124
125         ejs = talloc_asprintf(mem_ctx,
126                 "libinclude(\"base.js\");\n"
127                 "libinclude(\"provision.js\");\n"
128                 "\n"
129                 "function message() { print(vsprintf(arguments)); }\n"
130                 "\n"
131                 "var subobj = provision_guess();\n"
132                 "subobj.ROOTDN       = \"%s\";\n"
133                 "subobj.DOMAINDN     = \"%s\";\n"
134                 "subobj.DOMAINDN_LDB = \"%s\";\n"
135                 "subobj.CONFIGDN     = \"%s\";\n"
136                 "subobj.CONFIGDN_LDB = \"%s\";\n"
137                 "subobj.SCHEMADN     = \"%s\";\n"
138                 "subobj.SCHEMADN_LDB = \"%s\";\n"
139                 "subobj.HOSTNAME     = \"%s\";\n"
140                 "subobj.REALM        = \"%s\";\n"
141                 "subobj.DOMAIN       = \"%s\";\n"
142                 "subobj.DEFAULTSITE  = \"%s\";\n"
143                 "\n"
144                 "subobj.KRBTGTPASS   = \"_NOT_USED_\";\n"
145                 "subobj.MACHINEPASS  = \"%s\";\n"
146                 "subobj.ADMINPASS    = \"_NOT_USED_\";\n"
147                 "\n"
148                 "var paths = provision_default_paths(subobj);\n"
149                 "paths.samdb = \"%s\";\n"
150                 "paths.secrets = \"%s\";\n"
151                 "paths.templates = \"%s\";\n"
152                 "paths.keytab = \"%s\";\n"
153                 "paths.dns_keytab = \"%s\";\n"
154                 "\n"
155                 "var system_session = system_session();\n"
156                 "\n"
157                 "var ok = provision_become_dc(subobj, message, true, paths, system_session);\n"
158                 "assert(ok);\n"
159                 "\n"
160                 "return 0;\n",
161                 root_dn_str,            /* subobj.ROOTDN */
162                 domain_dn_str,          /* subobj.DOMAINDN */
163                 s->path.domaindn_ldb,           /* subobj.DOMAINDN_LDB */
164                 config_dn_str,  /* subobj.CONFIGDN */
165                 s->path.configdn_ldb,           /* subobj.CONFIGDN_LDB */
166                 schema_dn_str,  /* subobj.SCHEMADN */
167                 s->path.schemadn_ldb,           /* subobj.SCHEMADN_LDB */
168                 netbios_name,   /* subobj.HOSTNAME */
169                 realm,/* subobj.REALM */
170                 domain,/* subobj.DOMAIN */
171                 site_name,              /* subobj.DEFAULTSITE */
172                 cli_credentials_get_password(s->machine_account),/* subobj.MACHINEPASS */
173                 s->path.samdb_ldb,              /* paths.samdb */
174                 s->path.templates_ldb,          /* paths.templates */
175                 s->path.secrets_ldb,            /* paths.secrets */
176                 s->path.secrets_keytab,         /* paths.keytab */
177                 s->path.dns_keytab);            /* paths.dns_keytab */
178         NT_STATUS_HAVE_NO_MEMORY(ejs);
179
180         ret = test_run_ejs(ejs);
181         if (ret != 0) {
182                 DEBUG(0,("Failed to run ejs script: %d:\n%s",
183                         ret, ejs));
184                 talloc_free(ejs);
185                 return NT_STATUS_FOOBAR;
186         }
187         talloc_free(ejs);
188
189         talloc_free(ldb);
190
191         DEBUG(0,("Open the SAM LDB with system credentials: %s\n", 
192                  s->path.samdb_ldb));
193
194         ldb = ldb_wrap_connect(mem_ctx, lp_ctx, s->path.samdb_ldb,
195                                   system_session(mem_ctx, lp_ctx),
196                                   NULL, 0, NULL);
197         if (!ldb) {
198                 DEBUG(0,("Failed to open '%s'\n",
199                         s->path.samdb_ldb));
200                 return NT_STATUS_INTERNAL_DB_ERROR;
201         }
202
203         ok = samdb_set_ntds_invocation_id(ldb, invocation_id);
204         if (!ok) {
205                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
206                 return NT_STATUS_FOOBAR;
207         }
208         ok = samdb_set_ntds_objectGUID(ldb, &p->dest_dsa->ntds_guid);
209         if (!ok) {
210                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
211                 return NT_STATUS_FOOBAR;
212         }
213
214         return NT_STATUS_OK;
215 }
216
217 #include "param/param.h"
218 #include <Python.h>
219 #include "scripting/python/modules.h"
220
221 static NTSTATUS provision_bare_py(TALLOC_CTX *mem_ctx, 
222                                                                   struct loadparm_context *lp_ctx,
223                                                                   const char *dns_name,
224                                                                   const char *site_name,
225                                                                   const char *root_dn_str,
226                                                                   const char *domain_dn_str,
227                                                                   const char *config_dn_str,
228                                                                   const char *schema_dn_str,
229                                                                   const struct GUID *invocation_id,
230                                                                   const char *domain)
231 {
232         bool ok;
233         PyObject *provision_fn, *result, *parameters;
234
235         DEBUG(0,("Provision for Become-DC test using PYTHON\n"));
236
237         py_load_samba_modules();
238         Py_Initialize();
239
240         py_update_path("bin"); /* FIXME: Can't assume this always runs in source/... */
241
242         provision_fn = PyImport_Import(PyString_FromString("samba.provision.provision"));
243
244         if (provision_fn == NULL) {
245                 DEBUG(0, ("Unable to import provision Python module.\n"));
246                 return NT_STATUS_UNSUCCESSFUL;
247         }
248         
249         DEBUG(0,("New Server[%s] in Site[%s]\n", dns_name, site_name));
250
251         DEBUG(0,("DSA Instance [%s]\n"
252                 "\tobjectGUID[%s]\n"
253                 "\tinvocationId[%s]\n",
254                 p->dest_dsa->ntds_dn_str,
255                 GUID_string(mem_ctx, &p->dest_dsa->ntds_guid),
256                 GUID_string(mem_ctx, invocation_id)));
257
258         DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
259                  "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
260                 lp_private_dir(lp_ctx),
261                 s->path.samdb_ldb,
262                 s->path.secrets_ldb,
263                 s->path.secrets_keytab));
264
265         DEBUG(0,("Schema Partition[%s => %s]\n",
266                 schema_dn_str, s->path.schemadn_ldb));
267
268         DEBUG(0,("Config Partition[%s => %s]\n",
269                 config_dn_str, s->path.configdn_ldb));
270
271         DEBUG(0,("Domain Partition[%s => %s]\n",
272                 domain_dn_str, s->path.domaindn_ldb));
273
274         parameters = PyDict_New();
275
276         PyDict_SetItemString(parameters, "rootdn", PyString_FromString(root_dn_str));
277         PyDict_SetItemString(parameters, "domaindn", PyString_FromString(domain_dn_str));
278         PyDict_SetItemString(parameters, "domaindn_ldb", PyString_FromString(s->path.domaindn_ldb));
279         PyDict_SetItemString(parameters, "configdn", PyString_FromString(config_dn_str));
280         PyDict_SetItemString(parameters, "configdn_ldb", PyString_FromString(s->path.configdn_ldb));
281         PyDict_SetItemString(parameters, "schema_dn_str", PyString_FromString(schema_dn_str));
282         PyDict_SetItemString(parameters, "schemadn_ldb", PyString_FromString(s->path.schemadn_ldb));
283         PyDict_SetItemString(parameters, "netbios_name", PyString_FromString(netbios_name));
284         PyDict_SetItemString(parameters, "dnsname", PyString_FromString(dns_name));
285         PyDict_SetItemString(parameters, "defaultsite", PyString_FromString(site_name));
286         PyDict_SetItemString(parameters, "machinepass", PyString_FromString(cli_credentials_get_password(s->machine_account)));
287         PyDict_SetItemString(parameters, "samdb", PyString_FromString(s->path.samdb_ldb));
288         PyDict_SetItemString(parameters, "secrets_ldb", PyString_FromString(s->path.secrets_ldb));
289         PyDict_SetItemString(parameters, "secrets_keytab", PyString_FromString(s->path.secrets_keytab));
290
291         result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
292
293         Py_DECREF(parameters);
294
295         if (result == NULL) {
296                 PyErr_Print();
297                 PyErr_Clear();
298                 return NT_STATUS_UNSUCCESSFUL;
299         }
300
301         talloc_free(ldb);
302
303         DEBUG(0,("Open the SAM LDB with system credentials: %s\n", 
304                  s->path.samdb_ldb));
305
306         ldb = ldb_wrap_connect(s, lp_ctx, s->path.samdb_ldb,
307                                   system_session(s, lp_ctx),
308                                   NULL, 0, NULL);
309         if (!ldb) {
310                 DEBUG(0,("Failed to open '%s'\n",
311                         s->path.samdb_ldb));
312                 return NT_STATUS_INTERNAL_DB_ERROR;
313         }
314
315         ok = samdb_set_ntds_invocation_id(ldb, invocation_id);
316         if (!ok) {
317                 DEBUG(0,("Failed to set cached ntds invocationId\n"));
318                 return NT_STATUS_FOOBAR;
319         }
320         ok = samdb_set_ntds_objectGUID(ldb, &p->dest_dsa->ntds_guid);
321         if (!ok) {
322                 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
323                 return NT_STATUS_FOOBAR;
324         }
325
326         return NT_STATUS_OK;
327 }
328
329 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
330                                                 const char *dns_name, const char *site_name,
331                                                 const char *root_dn_str, const char *domain_dn_str,
332                                                 const char *config_dn_str, const char *schema_dn_str,
333                                                 const struct GUID *invocation_id, 
334                                                 const char *netbios_name, const char *realm,
335                                                 const char *domain)
336 {
337         if (getenv("PROVISION_EJS")) {
338                 provision_bare_ejs(mem_ctx, lp_ctx, dns_name, site_name, root_dn_str,
339                                                    domain_dn_str, config_dn_str, schema_dn_str,
340                                                    invocation_id, netbios_name, realm, domain);
341         } else {
342                 provision_bare_py(mem_ctx, lp_ctx, dns_name, site_name, root_dn_str,
343                                                   domain_dn_str, config_dn_str, schema_dn_str,
344                                                   invocation_id, netbios_name, realm, domain);
345         }
346 }
347
348