summaryrefslogtreecommitdiff
path: root/source4/torture/util_provision.c
blob: 5873b9f4854cc4eb4b25e4efda1e0e1d15af40db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* 
   Unix SMB/CIFS implementation.
   Samba utility functions
   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "dsdb/samdb/samdb.h"
#include "lib/appweb/ejs/ejs.h"
#include "lib/appweb/ejs/ejsInternal.h"
#include "scripting/ejs/smbcalls.h"
#include "auth/auth.h"
#include "lib/ldb_wrap.h"

static EjsId eid;
static int ejs_error;

static void test_ejs_exception(const char *reason)
{
	Ejs *ep = ejsPtr(eid);
	ejsSetErrorMsg(eid, "%s", reason);
	fprintf(stderr, "%s", ep->error);
	ejs_error = 127;
}

static int test_run_ejs(char *script)
{
	EjsHandle handle = 0;
	MprVar result;
	char *emsg;
	TALLOC_CTX *mem_ctx = talloc_new(NULL);
	struct MprVar *return_var;

	mprSetCtx(mem_ctx);

	if (ejsOpen(NULL, NULL, NULL) != 0) {
		d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
		ejs_error = 127;
		goto failed;
	}

	smb_setup_ejs_functions(test_ejs_exception);

	if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
		d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
		ejs_error = 127;
		goto failed;
	}

	mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));

	/* run the script */
	if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
		d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
		if (ejs_error == 0) ejs_error = 127;
		goto failed;
	}

	return_var = ejsGetReturnValue(eid);
	ejs_error = mprVarToNumber(return_var);

failed:
	ejsClose();
	talloc_free(mem_ctx);
	return ejs_error;
}

static NTSTATUS provision_bare_ejs(TALLOC_CTX *mem_ctx, 
								   struct loadparm_context *lp_ctx,
								   const char *dns_name,
								   const char *site_name,
								   const char *root_dn_str,
								   const char *domain_dn_str,
								   const char *config_dn_str,
								   const char *schema_dn_str,
								   const struct GUID *invocation_id,
								   const char *netbios_name,
								   const char *realm,
								   const char *domain,
								   const struct GUID *ntds_guid,
								   const char *ntds_dn_str,
								   const char *machine_password,
								   const char *samdb_ldb,
								   const char *secrets_ldb,
								   const char *secrets_keytab, 
								   const char *schemadn_ldb,
								   const char *configdn_ldb,
								   const char *domaindn_ldb,
								   const char *templates_ldb,
								   const char *dns_keytab)
{
	char *ejs;
	int ret;
	bool ok;
	struct ldb_context *ldb;

	DEBUG(0,("Provision for Become-DC test using EJS\n"));

	DEBUG(0,("New Server[%s] in Site[%s]\n", dns_name, site_name));

	DEBUG(0,("DSA Instance [%s]\n"
		"\tobjectGUID[%s]\n"
		"\tinvocationId[%s]\n",
		ntds_dn_str,
		GUID_string(mem_ctx, ntds_guid),
		GUID_string(mem_ctx, invocation_id)));

	DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
		 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
		lp_private_dir(lp_ctx),
		samdb_ldb,
		secrets_ldb,
		secrets_keytab));

	DEBUG(0,("Schema Partition[%s => %s]\n",
		schema_dn_str, schemadn_ldb));

	DEBUG(0,("Config Partition[%s => %s]\n",
		config_dn_str, configdn_ldb));

	DEBUG(0,("Domain Partition[%s => %s]\n",
		domain_dn_str, domaindn_ldb));

	ejs = talloc_asprintf(mem_ctx,
		"libinclude(\"base.js\");\n"
		"libinclude(\"provision.js\");\n"
		"\n"
		"function message() { print(vsprintf(arguments)); }\n"
		"\n"
		"var subobj = provision_guess();\n"
		"subobj.ROOTDN       = \"%s\";\n"
		"subobj.DOMAINDN     = \"%s\";\n"
		"subobj.DOMAINDN_LDB = \"%s\";\n"
		"subobj.CONFIGDN     = \"%s\";\n"
		"subobj.CONFIGDN_LDB = \"%s\";\n"
		"subobj.SCHEMADN     = \"%s\";\n"
		"subobj.SCHEMADN_LDB = \"%s\";\n"
		"subobj.HOSTNAME     = \"%s\";\n"
		"subobj.REALM        = \"%s\";\n"
		"subobj.DOMAIN       = \"%s\";\n"
		"subobj.DEFAULTSITE  = \"%s\";\n"
		"\n"
		"subobj.KRBTGTPASS   = \"_NOT_USED_\";\n"
		"subobj.MACHINEPASS  = \"%s\";\n"
		"subobj.ADMINPASS    = \"_NOT_USED_\";\n"
		"\n"
		"var paths = provision_default_paths(subobj);\n"
		"paths.samdb = \"%s\";\n"
		"paths.secrets = \"%s\";\n"
		"paths.templates = \"%s\";\n"
		"paths.keytab = \"%s\";\n"
		"paths.dns_keytab = \"%s\";\n"
		"\n"
		"var system_session = system_session();\n"
		"\n"
		"var ok = provision_become_dc(subobj, message, true, paths, system_session);\n"
		"assert(ok);\n"
		"\n"
		"return 0;\n",
		root_dn_str,		/* subobj.ROOTDN */
		domain_dn_str,		/* subobj.DOMAINDN */
		domaindn_ldb,		/* subobj.DOMAINDN_LDB */
		config_dn_str,	/* subobj.CONFIGDN */
		configdn_ldb,		/* subobj.CONFIGDN_LDB */
		schema_dn_str,	/* subobj.SCHEMADN */
		schemadn_ldb,		/* subobj.SCHEMADN_LDB */
		netbios_name,	/* subobj.HOSTNAME */
		realm,/* subobj.REALM */
		domain,/* subobj.DOMAIN */
		site_name,		/* subobj.DEFAULTSITE */
		machine_password,/* subobj.MACHINEPASS */
		samdb_ldb,		/* paths.samdb */
		templates_ldb,		/* paths.templates */
		secrets_ldb,		/* paths.secrets */
		secrets_keytab,	        /* paths.keytab */
		dns_keytab);	        /* paths.dns_keytab */
	NT_STATUS_HAVE_NO_MEMORY(ejs);

	ret = test_run_ejs(ejs);
	if (ret != 0) {
		DEBUG(0,("Failed to run ejs script: %d:\n%s",
			ret, ejs));
		talloc_free(ejs);
		return NT_STATUS_FOOBAR;
	}
	talloc_free(ejs);

	DEBUG(0,("Open the SAM LDB with system credentials: %s\n", 
		 samdb_ldb));

	ldb = ldb_wrap_connect(mem_ctx, lp_ctx, samdb_ldb,
				  system_session(mem_ctx, lp_ctx),
				  NULL, 0, NULL);
	if (!ldb) {
		DEBUG(0,("Failed to open '%s'\n",
			samdb_ldb));
		return NT_STATUS_INTERNAL_DB_ERROR;
	}

	ok = samdb_set_ntds_invocation_id(ldb, invocation_id);
	if (!ok) {
		DEBUG(0,("Failed to set cached ntds invocationId\n"));
		return NT_STATUS_FOOBAR;
	}
	ok = samdb_set_ntds_objectGUID(ldb, ntds_guid);
	if (!ok) {
		DEBUG(0,("Failed to set cached ntds objectGUID\n"));
		return NT_STATUS_FOOBAR;
	}

	return NT_STATUS_OK;
}

#include "param/param.h"
#include <Python.h>
#include "scripting/python/modules.h"

static NTSTATUS provision_bare_py(TALLOC_CTX *mem_ctx, 
								  struct loadparm_context *lp_ctx,
								  const char *dns_name,
								  const char *site_name,
								  const char *root_dn_str,
								  const char *domain_dn_str,
								  const char *config_dn_str,
								  const char *schema_dn_str,
								  const struct GUID *invocation_id,
								  const char *netbios_name, const char *realm,
								  const char *domain,
								  const struct GUID *ntds_guid,
								  const char *ntds_dn_str,
								  const char *machine_password,
								  const char *samdb_ldb,
								  const char *secrets_ldb,
								  const char *secrets_keytab,
								  const char *schemadn_ldb,
								  const char *configdn_ldb,
								  const char *domaindn_ldb,
								  const char *templates_ldb,
								  const char *dns_keytab)
{
	bool ok;
	PyObject *provision_mod, *provision_dict, *provision_fn, *result, *parameters;
	struct ldb_context *ldb;

	DEBUG(0,("Provision for Become-DC test using PYTHON\n"));

	py_load_samba_modules();
	Py_Initialize();
	py_update_path("bin"); /* FIXME: Can't assume this is always the case */

	provision_mod = PyImport_Import(PyString_FromString("samba.provision"));

	if (provision_mod == NULL) {
		PyErr_Print();
		DEBUG(0, ("Unable to import provision Python module.\n"));
	      	return NT_STATUS_UNSUCCESSFUL;
	}

	provision_dict = PyModule_GetDict(provision_mod);

	if (provision_dict == NULL) {
		DEBUG(0, ("Unable to get dictionary for provision module\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}

	provision_fn = PyDict_GetItemString(provision_dict, "provision");
	if (provision_fn == NULL) {
		PyErr_Print();
		DEBUG(0, ("Unable to get provision function\n"));
		return NT_STATUS_UNSUCCESSFUL;
	}
	
	DEBUG(0,("New Server[%s] in Site[%s]\n", dns_name, site_name));

	DEBUG(0,("DSA Instance [%s]\n"
		"\tobjectGUID[%s]\n"
		"\tinvocationId[%s]\n",
		ntds_dn_str,
		ntds_guid == NULL?"None":GUID_string(mem_ctx, ntds_guid),
		invocation_id == NULL?"None":GUID_string(mem_ctx, invocation_id)));

	DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
		 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
		lp_private_dir(lp_ctx),
		samdb_ldb,
		secrets_ldb,
		secrets_keytab));

	DEBUG(0,("Schema Partition[%s => %s]\n",
		schema_dn_str, schemadn_ldb));

	DEBUG(0,("Config Partition[%s => %s]\n",
		config_dn_str, configdn_ldb));

	DEBUG(0,("Domain Partition[%s => %s]\n",
		domain_dn_str, domaindn_ldb));

	parameters = PyDict_New();

	PyDict_SetItemString(parameters, "rootdn", PyString_FromString(root_dn_str));
	if (domaindn_ldb != NULL)
		PyDict_SetItemString(parameters, "domaindn_ldb", 
							 PyString_FromString(domaindn_ldb));
	if (config_dn_str != NULL)
		PyDict_SetItemString(parameters, "configdn", 
							 PyString_FromString(config_dn_str));
	if (configdn_ldb != NULL)
		PyDict_SetItemString(parameters, "configdn_ldb", 
							 PyString_FromString(configdn_ldb));
	if (schema_dn_str != NULL)
		PyDict_SetItemString(parameters, "schema_dn_str", 
							 PyString_FromString(schema_dn_str));
	if (schemadn_ldb != NULL)
		PyDict_SetItemString(parameters, "schemadn_ldb", 
							 PyString_FromString(schemadn_ldb));
	PyDict_SetItemString(parameters, "hostname", PyString_FromString(netbios_name));
	PyDict_SetItemString(parameters, "sitename", PyString_FromString(site_name));
	PyDict_SetItemString(parameters, "machinepass", PyString_FromString(machine_password));
	if (samdb_ldb != NULL)
		PyDict_SetItemString(parameters, "samdb", 
							 PyString_FromString(samdb_ldb));
	if (secrets_ldb != NULL)
		PyDict_SetItemString(parameters, "secrets_ldb", 
							 PyString_FromString(secrets_ldb));
	if (secrets_keytab != NULL)
		PyDict_SetItemString(parameters, "secrets_keytab", 
							 PyString_FromString(secrets_keytab));

	result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);

	Py_DECREF(parameters);

	if (result == NULL) {
		PyErr_Print();
		PyErr_Clear();
		return NT_STATUS_UNSUCCESSFUL;
	}

	DEBUG(0,("Open the SAM LDB with system credentials: %s\n", 
		 samdb_ldb));

	ldb = ldb_wrap_connect(mem_ctx, lp_ctx, samdb_ldb,
				  system_session(mem_ctx, lp_ctx),
				  NULL, 0, NULL);
	if (!ldb) {
		DEBUG(0,("Failed to open '%s'\n",
			samdb_ldb));
		return NT_STATUS_INTERNAL_DB_ERROR;
	}

	ok = samdb_set_ntds_invocation_id(ldb, invocation_id);
	if (!ok) {
		DEBUG(0,("Failed to set cached ntds invocationId\n"));
		return NT_STATUS_FOOBAR;
	}
	ok = samdb_set_ntds_objectGUID(ldb, ntds_guid);
	if (!ok) {
		DEBUG(0,("Failed to set cached ntds objectGUID\n"));
		return NT_STATUS_FOOBAR;
	}

	return NT_STATUS_OK;
}

NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
						const char *dns_name, const char *site_name,
						const char *root_dn_str, const char *domain_dn_str,
						const char *config_dn_str, const char *schema_dn_str,
						const struct GUID *invocation_id, 
						const char *netbios_name, const char *realm,
						const char *domain, const struct GUID *ntds_guid,
						const char *ntds_dn_str, 
						const char *machine_password,
						const char *samdb_ldb,
						const char *secrets_ldb,
						const char *secrets_keytab,
						const char *schemadn_ldb,
						const char *configdn_ldb,
						const char *domaindn_ldb,
						const char *templates_ldb,
						const char *dns_keytab)
{
	if (getenv("PROVISION_EJS")) {
		return provision_bare_ejs(mem_ctx, lp_ctx, dns_name, site_name, 
								  root_dn_str, domain_dn_str, config_dn_str, 
								  schema_dn_str, invocation_id, netbios_name, 
								  realm, domain, ntds_guid, ntds_dn_str, 
								  machine_password, samdb_ldb, secrets_ldb,
								  secrets_keytab, schemadn_ldb, configdn_ldb,
								  domaindn_ldb, templates_ldb, 
								  dns_keytab);
	} else {
		return provision_bare_py(mem_ctx, lp_ctx, dns_name, site_name, 
								 root_dn_str, domain_dn_str, config_dn_str, 
								 schema_dn_str, invocation_id, netbios_name, 
								 realm, domain, ntds_guid, ntds_dn_str, 
								 machine_password, samdb_ldb, secrets_ldb,
								 secrets_keytab, schemadn_ldb, configdn_ldb,
								 domaindn_ldb, templates_ldb,
								 dns_keytab);
	}
}