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"
30 static void test_ejs_exception(const char *reason)
32 Ejs *ep = ejsPtr(eid);
33 ejsSetErrorMsg(eid, "%s", reason);
34 fprintf(stderr, "%s", ep->error);
38 static int test_run_ejs(char *script)
43 TALLOC_CTX *mem_ctx = talloc_new(NULL);
44 struct MprVar *return_var;
48 if (ejsOpen(NULL, NULL, NULL) != 0) {
49 d_printf("ejsOpen(): unable to initialise EJS subsystem\n");
54 smb_setup_ejs_functions(test_ejs_exception);
56 if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
57 d_printf("smbscript: ejsOpenEngine(): unable to initialise an EJS engine\n");
62 mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", NULL));
65 if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
66 d_printf("smbscript: ejsEvalScript(): %s\n", emsg);
67 if (ejs_error == 0) ejs_error = 127;
71 return_var = ejsGetReturnValue(eid);
72 ejs_error = mprVarToNumber(return_var);
80 static NTSTATUS provision_bare_ejs(TALLOC_CTX *mem_ctx,
81 struct loadparm_context *lp_ctx,
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,
96 struct ldb_context *ldb;
98 DEBUG(0,("Provision for Become-DC test using EJS\n"));
100 DEBUG(0,("New Server[%s] in Site[%s]\n", dns_name, site_name));
102 DEBUG(0,("DSA Instance [%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)));
109 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
110 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
111 lp_private_dir(lp_ctx),
114 s->path.secrets_keytab));
116 DEBUG(0,("Schema Partition[%s => %s]\n",
117 schema_dn_str, s->path.schemadn_ldb));
119 DEBUG(0,("Config Partition[%s => %s]\n",
120 config_dn_str, s->path.configdn_ldb));
122 DEBUG(0,("Domain Partition[%s => %s]\n",
123 domain_dn_str, s->path.domaindn_ldb));
125 ejs = talloc_asprintf(mem_ctx,
126 "libinclude(\"base.js\");\n"
127 "libinclude(\"provision.js\");\n"
129 "function message() { print(vsprintf(arguments)); }\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"
144 "subobj.KRBTGTPASS = \"_NOT_USED_\";\n"
145 "subobj.MACHINEPASS = \"%s\";\n"
146 "subobj.ADMINPASS = \"_NOT_USED_\";\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"
155 "var system_session = system_session();\n"
157 "var ok = provision_become_dc(subobj, message, true, paths, system_session);\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);
180 ret = test_run_ejs(ejs);
182 DEBUG(0,("Failed to run ejs script: %d:\n%s",
185 return NT_STATUS_FOOBAR;
191 DEBUG(0,("Open the SAM LDB with system credentials: %s\n",
194 ldb = ldb_wrap_connect(mem_ctx, lp_ctx, s->path.samdb_ldb,
195 system_session(mem_ctx, lp_ctx),
198 DEBUG(0,("Failed to open '%s'\n",
200 return NT_STATUS_INTERNAL_DB_ERROR;
203 ok = samdb_set_ntds_invocation_id(ldb, invocation_id);
205 DEBUG(0,("Failed to set cached ntds invocationId\n"));
206 return NT_STATUS_FOOBAR;
208 ok = samdb_set_ntds_objectGUID(ldb, &p->dest_dsa->ntds_guid);
210 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
211 return NT_STATUS_FOOBAR;
217 #include "param/param.h"
219 #include "scripting/python/modules.h"
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,
233 PyObject *provision_fn, *result, *parameters;
235 DEBUG(0,("Provision for Become-DC test using PYTHON\n"));
237 py_load_samba_modules();
240 py_update_path("bin"); /* FIXME: Can't assume this always runs in source/... */
242 provision_fn = PyImport_Import(PyString_FromString("samba.provision.provision"));
244 if (provision_fn == NULL) {
245 DEBUG(0, ("Unable to import provision Python module.\n"));
246 return NT_STATUS_UNSUCCESSFUL;
249 DEBUG(0,("New Server[%s] in Site[%s]\n", dns_name, site_name));
251 DEBUG(0,("DSA Instance [%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)));
258 DEBUG(0,("Pathes under PRIVATEDIR[%s]\n"
259 "SAMDB[%s] SECRETS[%s] KEYTAB[%s]\n",
260 lp_private_dir(lp_ctx),
263 s->path.secrets_keytab));
265 DEBUG(0,("Schema Partition[%s => %s]\n",
266 schema_dn_str, s->path.schemadn_ldb));
268 DEBUG(0,("Config Partition[%s => %s]\n",
269 config_dn_str, s->path.configdn_ldb));
271 DEBUG(0,("Domain Partition[%s => %s]\n",
272 domain_dn_str, s->path.domaindn_ldb));
274 parameters = PyDict_New();
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));
291 result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
293 Py_DECREF(parameters);
295 if (result == NULL) {
298 return NT_STATUS_UNSUCCESSFUL;
303 DEBUG(0,("Open the SAM LDB with system credentials: %s\n",
306 ldb = ldb_wrap_connect(s, lp_ctx, s->path.samdb_ldb,
307 system_session(s, lp_ctx),
310 DEBUG(0,("Failed to open '%s'\n",
312 return NT_STATUS_INTERNAL_DB_ERROR;
315 ok = samdb_set_ntds_invocation_id(ldb, invocation_id);
317 DEBUG(0,("Failed to set cached ntds invocationId\n"));
318 return NT_STATUS_FOOBAR;
320 ok = samdb_set_ntds_objectGUID(ldb, &p->dest_dsa->ntds_guid);
322 DEBUG(0,("Failed to set cached ntds objectGUID\n"));
323 return NT_STATUS_FOOBAR;
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,
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);
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);