2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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.
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.
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/>.
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"
32 static void test_ejs_exception(const char *reason)
34 Ejs *ep = ejsPtr(eid);
35 ejsSetErrorMsg(eid, "%s", reason);
36 fprintf(stderr, "%s", ep->error);
40 static int test_run_ejs(char *script)
45 TALLOC_CTX *mem_ctx = talloc_new(NULL);
46 struct MprVar *return_var;
50 if (ejsOpen(NULL, NULL, NULL) != 0) {
51 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
56 smb_setup_ejs_functions(test_ejs_exception);
58 if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
59 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
64 mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
67 if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
68 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
69 if (ejs_error == 0) ejs_error = 127;
73 return_var = ejsGetReturnValue(eid);
74 ejs_error = mprVarToNumber(return_var);
82 static NTSTATUS provision_bare_ejs(TALLOC_CTX *mem_ctx,
83 struct loadparm_context *lp_ctx,
84 struct provision_settings *settings)
89 struct ldb_context *ldb;
91 DEBUG(0,("Provision for Become-DC test using EJS\n"));
93 DEBUG(0,("New Server[%s] in Site[%s]\n", settings->dns_name,
94 settings->site_name));
96 DEBUG(0,("DSA Instance [%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)));
103 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
104 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
105 lp_private_dir(lp_ctx),
107 settings->secrets_ldb,
108 settings->secrets_keytab));
110 DEBUG(0,("Schema Partition[%s => %s]\n",
111 settings->schema_dn_str, settings->schemadn_ldb));
113 DEBUG(0,("Config Partition[%s => %s]\n",
114 settings->config_dn_str, settings->configdn_ldb));
116 DEBUG(0,("Domain Partition[%s => %s]\n",
117 settings->domain_dn_str, settings->domaindn_ldb));
119 ejs = talloc_asprintf(mem_ctx,
120 "libinclude(\"base.js\");\n"
121 "libinclude(\"provision.js\");\n"
123 "function message() { print(vsprintf(arguments)); }\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"
138 "subobj.KRBTGTPASS = \"_NOT_USED_\";\n"
139 "subobj.MACHINEPASS = \"%s\";\n"
140 "subobj.ADMINPASS = \"_NOT_USED_\";\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"
149 "var system_session = system_session();\n"
151 "var ok = provision_become_dc(subobj, message, true, paths, system_session);\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);
174 ret = test_run_ejs(ejs);
176 DEBUG(0,("Failed to run ejs script: %d:\n%s",
179 return NT_STATUS_FOOBAR;
183 DEBUG(0,("Open the SAM LDB with system credentials: %s\n",
184 settings->samdb_ldb));
186 ldb = ldb_wrap_connect(mem_ctx, lp_ctx, settings->samdb_ldb,
187 system_session(mem_ctx, lp_ctx),
190 DEBUG(0,("Failed to open '%s'\n",
191 settings->samdb_ldb));
192 return NT_STATUS_INTERNAL_DB_ERROR;
195 ok = samdb_set_ntds_invocation_id(ldb, settings->invocation_id);
197 DEBUG(0,("Failed to set cached ntds invocationId\n"));
198 return NT_STATUS_FOOBAR;
200 ok = samdb_set_ntds_objectGUID(ldb, settings->ntds_guid);
202 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
203 return NT_STATUS_FOOBAR;
209 #include "param/param.h"
211 #include "scripting/python/modules.h"
213 static NTSTATUS provision_bare_py(TALLOC_CTX *mem_ctx,
214 struct loadparm_context *lp_ctx,
215 struct provision_settings *settings)
218 PyObject *provision_mod, *provision_dict, *provision_fn, *result, *parameters;
219 struct ldb_context *ldb;
221 DEBUG(0,("Provision for Become-DC test using python\n"));
223 py_load_samba_modules();
225 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
227 provision_mod = PyImport_Import(PyString_FromString("samba.provision"));
229 if (provision_mod == NULL) {
231 DEBUG(0, ("Unable to import provision Python module.\n"));
232 return NT_STATUS_UNSUCCESSFUL;
235 provision_dict = PyModule_GetDict(provision_mod);
237 if (provision_dict == NULL) {
238 DEBUG(0, ("Unable to get dictionary for provision module\n"));
239 return NT_STATUS_UNSUCCESSFUL;
242 provision_fn = PyDict_GetItemString(provision_dict, "provision");
243 if (provision_fn == NULL) {
245 DEBUG(0, ("Unable to get provision function\n"));
246 return NT_STATUS_UNSUCCESSFUL;
249 DEBUG(0,("New Server[%s] in Site[%s]\n", settings->dns_name,
250 settings->site_name));
252 DEBUG(0,("DSA Instance [%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)));
259 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
260 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
261 lp_private_dir(lp_ctx),
263 settings->secrets_ldb,
264 settings->secrets_keytab));
266 DEBUG(0,("Schema Partition[%s => %s]\n",
267 settings->schema_dn_str, settings->schemadn_ldb));
269 DEBUG(0,("Config Partition[%s => %s]\n",
270 settings->config_dn_str, settings->configdn_ldb));
272 DEBUG(0,("Domain Partition[%s => %s]\n",
273 settings->domain_dn_str, settings->domaindn_ldb));
275 parameters = PyDict_New();
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));
310 result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
312 Py_DECREF(parameters);
314 if (result == NULL) {
317 return NT_STATUS_UNSUCCESSFUL;
320 DEBUG(0,("Open the SAM LDB with system credentials: %s\n",
321 settings->samdb_ldb));
323 ldb = ldb_wrap_connect(mem_ctx, lp_ctx, settings->samdb_ldb,
324 system_session(mem_ctx, lp_ctx),
327 DEBUG(0,("Failed to open '%s'\n", settings->samdb_ldb));
328 return NT_STATUS_INTERNAL_DB_ERROR;
331 ok = samdb_set_ntds_invocation_id(ldb, settings->invocation_id);
333 DEBUG(0,("Failed to set cached ntds invocationId\n"));
334 return NT_STATUS_FOOBAR;
336 ok = samdb_set_ntds_objectGUID(ldb, settings->ntds_guid);
338 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
339 return NT_STATUS_FOOBAR;
345 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
346 struct provision_settings *settings)
348 if (getenv("PROVISION_EJS")) {
349 return provision_bare_ejs(mem_ctx, lp_ctx, settings);
351 return provision_bare_py(mem_ctx, lp_ctx, settings);