2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2009
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/ndr/libndr.h"
26 #include "param/provision.h"
27 #include "param/secrets.h"
28 #include "lib/talloc/pytalloc.h"
29 #include "scripting/python/modules.h"
30 #include "param/pyparam.h"
31 #include "dynconfig/dynconfig.h"
33 static PyObject *provision_module(void)
35 PyObject *name = PyString_FromString("samba.provision");
38 return PyImport_Import(name);
41 static PyObject *schema_module(void)
43 PyObject *name = PyString_FromString("samba.schema");
46 return PyImport_Import(name);
49 static PyObject *ldb_module(void)
51 PyObject *name = PyString_FromString("ldb");
54 return PyImport_Import(name);
57 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
60 PyObject *ldb_mod = ldb_module();
61 PyTypeObject *ldb_ctx_type;
65 ldb_ctx_type = (PyTypeObject *)PyObject_GetAttrString(ldb_mod, "Ldb");
67 ret = (PyLdbObject *)ldb_ctx_type->tp_alloc(ldb_ctx_type, 0);
72 ret->mem_ctx = talloc_new(NULL);
73 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
74 return (PyObject *)ret;
77 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
78 struct provision_settings *settings,
79 struct provision_result *result)
81 const char *configfile;
82 PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_lp_ctx;
84 DEBUG(0,("Provision for Become-DC test using python\n"));
87 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
89 provision_mod = provision_module();
91 if (provision_mod == NULL) {
93 DEBUG(0, ("Unable to import provision Python module.\n"));
94 return NT_STATUS_UNSUCCESSFUL;
97 provision_dict = PyModule_GetDict(provision_mod);
99 if (provision_dict == NULL) {
100 DEBUG(0, ("Unable to get dictionary for provision module\n"));
101 return NT_STATUS_UNSUCCESSFUL;
104 provision_fn = PyDict_GetItemString(provision_dict, "provision_become_dc");
105 if (provision_fn == NULL) {
107 DEBUG(0, ("Unable to get provision_become_dc function\n"));
108 return NT_STATUS_UNSUCCESSFUL;
111 DEBUG(0,("New Server in Site[%s]\n",
112 settings->site_name));
114 DEBUG(0,("DSA Instance [%s]\n"
115 "\tinvocationId[%s]\n",
116 settings->ntds_dn_str,
117 settings->invocation_id == NULL?"None":GUID_string(mem_ctx, settings->invocation_id)));
119 DEBUG(0,("Paths under targetdir[%s]\n",
120 settings->targetdir));
121 parameters = PyDict_New();
123 configfile = lpcfg_configfile(lp_ctx);
124 if (configfile != NULL) {
125 PyDict_SetItemString(parameters, "smbconf",
126 PyString_FromString(configfile));
129 PyDict_SetItemString(parameters, "rootdn",
130 PyString_FromString(settings->root_dn_str));
131 if (settings->targetdir != NULL)
132 PyDict_SetItemString(parameters, "targetdir",
133 PyString_FromString(settings->targetdir));
134 if (file_exist("setup/provision.smb.conf.dc")) {
135 PyDict_SetItemString(parameters, "setup_dir",
136 PyString_FromString("setup"));
138 PyDict_SetItemString(parameters, "setup_dir",
139 PyString_FromString(dyn_SETUPDIR));
141 PyDict_SetItemString(parameters, "hostname",
142 PyString_FromString(settings->netbios_name));
143 PyDict_SetItemString(parameters, "domain",
144 PyString_FromString(settings->domain));
145 PyDict_SetItemString(parameters, "realm",
146 PyString_FromString(settings->realm));
147 if (settings->root_dn_str)
148 PyDict_SetItemString(parameters, "rootdn",
149 PyString_FromString(settings->root_dn_str));
151 if (settings->domain_dn_str)
152 PyDict_SetItemString(parameters, "domaindn",
153 PyString_FromString(settings->domain_dn_str));
155 if (settings->schema_dn_str)
156 PyDict_SetItemString(parameters, "schemadn",
157 PyString_FromString(settings->schema_dn_str));
159 if (settings->config_dn_str)
160 PyDict_SetItemString(parameters, "configdn",
161 PyString_FromString(settings->config_dn_str));
163 if (settings->server_dn_str)
164 PyDict_SetItemString(parameters, "serverdn",
165 PyString_FromString(settings->server_dn_str));
167 if (settings->site_name)
168 PyDict_SetItemString(parameters, "sitename",
169 PyString_FromString(settings->site_name));
171 PyDict_SetItemString(parameters, "machinepass",
172 PyString_FromString(settings->machine_password));
175 PyDict_SetItemString(parameters, "debuglevel", PyInt_FromLong(DEBUGLEVEL));
177 py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
179 Py_DECREF(parameters);
181 if (py_result == NULL) {
184 return NT_STATUS_UNSUCCESSFUL;
187 result->domaindn = talloc_strdup(mem_ctx, PyString_AsString(PyObject_GetAttrString(py_result, "domaindn")));
190 py_lp_ctx = PyObject_GetAttrString(py_result, "lp");
191 if (py_lp_ctx == NULL) {
192 DEBUG(0, ("Missing 'lp' attribute"));
193 return NT_STATUS_UNSUCCESSFUL;
195 result->lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
196 result->samdb = PyLdb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb"));
201 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
203 PyObject *mod_security, *dom_sid_Type;
205 mod_security = PyImport_ImportModule("samba.dcerpc.security");
206 if (mod_security == NULL)
209 dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
210 if (dom_sid_Type == NULL)
213 return py_talloc_reference((PyTypeObject *)dom_sid_Type, sid);
216 NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
217 struct tevent_context *event_ctx,
218 struct provision_store_self_join_settings *settings,
219 const char **error_string)
222 PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_sid;
223 struct ldb_context *ldb;
224 TALLOC_CTX *tmp_mem = talloc_new(mem_ctx);
226 return NT_STATUS_NO_MEMORY;
229 /* Open the secrets database */
230 ldb = secrets_db_connect(tmp_mem, lp_ctx);
233 = talloc_asprintf(mem_ctx,
234 "Could not open secrets database");
235 talloc_free(tmp_mem);
236 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
239 ret = ldb_transaction_start(ldb);
241 if (ret != LDB_SUCCESS) {
243 = talloc_asprintf(mem_ctx,
244 "Could not start transaction on secrets database: %s", ldb_errstring(ldb));
245 talloc_free(tmp_mem);
246 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
250 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
251 provision_mod = provision_module();
253 if (provision_mod == NULL) {
256 = talloc_asprintf(mem_ctx, "Unable to import provision Python module.");
257 talloc_free(tmp_mem);
258 return NT_STATUS_UNSUCCESSFUL;
261 provision_dict = PyModule_GetDict(provision_mod);
263 if (provision_dict == NULL) {
265 = talloc_asprintf(mem_ctx, "Unable to get dictionary for provision module");
266 talloc_free(tmp_mem);
267 return NT_STATUS_UNSUCCESSFUL;
270 provision_fn = PyDict_GetItemString(provision_dict, "secretsdb_self_join");
271 if (provision_fn == NULL) {
274 = talloc_asprintf(mem_ctx, "Unable to get provision_become_dc function");
275 talloc_free(tmp_mem);
276 return NT_STATUS_UNSUCCESSFUL;
279 parameters = PyDict_New();
281 PyDict_SetItemString(parameters, "secretsdb",
282 PyLdb_FromLdbContext(ldb));
283 PyDict_SetItemString(parameters, "domain",
284 PyString_FromString(settings->domain_name));
285 if (settings->realm != NULL) {
286 PyDict_SetItemString(parameters, "realm",
287 PyString_FromString(settings->realm));
289 PyDict_SetItemString(parameters, "machinepass",
290 PyString_FromString(settings->machine_password));
291 PyDict_SetItemString(parameters, "netbiosname",
292 PyString_FromString(settings->netbios_name));
294 py_sid = py_dom_sid_FromSid(settings->domain_sid);
295 if (py_sid == NULL) {
296 Py_DECREF(parameters);
300 PyDict_SetItemString(parameters, "domainsid",
303 PyDict_SetItemString(parameters, "secure_channel_type",
304 PyInt_FromLong(settings->secure_channel_type));
306 PyDict_SetItemString(parameters, "key_version_number",
307 PyInt_FromLong(settings->key_version_number));
309 py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
311 Py_DECREF(parameters);
313 if (py_result == NULL) {
317 ret = ldb_transaction_commit(ldb);
318 if (ret != LDB_SUCCESS) {
320 = talloc_asprintf(mem_ctx,
321 "Could not commit transaction on secrets database: %s", ldb_errstring(ldb));
322 talloc_free(tmp_mem);
323 return NT_STATUS_INTERNAL_DB_ERROR;
326 talloc_free(tmp_mem);
331 ldb_transaction_cancel(ldb);
332 talloc_free(tmp_mem);
336 return NT_STATUS_UNSUCCESSFUL;
340 struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
341 DATA_BLOB *override_prefixmap)
343 const char *setupdir;
344 PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
346 DEBUG(0,("Schema for DRS tests using python\n"));
349 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
351 schema_mod = schema_module();
353 if (schema_mod == NULL) {
355 DEBUG(0, ("Unable to import schema Python module.\n"));
359 schema_dict = PyModule_GetDict(schema_mod);
361 if (schema_dict == NULL) {
362 DEBUG(0, ("Unable to get dictionary for schema module\n"));
366 schema_fn = PyDict_GetItemString(schema_dict, "ldb_with_schema");
367 if (schema_fn == NULL) {
369 DEBUG(0, ("Unable to get schema_get_ldb function\n"));
373 parameters = PyDict_New();
375 setupdir = lpcfg_setupdir(lp_ctx);
376 PyDict_SetItemString(parameters, "setup_dir",
377 PyString_FromString(setupdir));
378 if (override_prefixmap) {
379 PyDict_SetItemString(parameters, "override_prefixmap",
380 PyString_FromStringAndSize((const char *)override_prefixmap->data,
381 override_prefixmap->length));
384 py_result = PyEval_CallObjectWithKeywords(schema_fn, NULL, parameters);
386 Py_DECREF(parameters);
388 if (py_result == NULL) {
394 return PyLdb_AsLdbContext(PyObject_GetAttrString(py_result, "ldb"));