talloc_stack: Call talloc destructors while frame is still around
[Samba.git] / source4 / param / provision.c
blob4dab31f5c20a0bbb077fb3de07d81a6f63b7d96e
1 /*
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/>.
21 #include <Python.h>
22 #include "python/py3compat.h"
23 #include <ldb.h>
24 #include <pyldb.h>
25 #include "includes.h"
26 #include "librpc/ndr/libndr.h"
27 #include "param/provision.h"
28 #include "param/secrets.h"
29 #include <pytalloc.h>
30 #include "python/modules.h"
31 #include "param/pyparam.h"
32 #include "dynconfig/dynconfig.h"
34 static PyObject *provision_module(void)
36 PyObject *name = PyStr_FromString("samba.provision");
37 if (name == NULL)
38 return NULL;
39 return PyImport_Import(name);
42 static PyObject *schema_module(void)
44 PyObject *name = PyStr_FromString("samba.schema");
45 if (name == NULL)
46 return NULL;
47 return PyImport_Import(name);
50 static PyObject *ldb_module(void)
52 PyObject *name = PyStr_FromString("ldb");
53 if (name == NULL)
54 return NULL;
55 return PyImport_Import(name);
58 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
60 PyLdbObject *ret;
61 PyObject *ldb_mod = ldb_module();
62 PyTypeObject *ldb_ctx_type;
63 if (ldb_mod == NULL)
64 return NULL;
66 ldb_ctx_type = (PyTypeObject *)PyObject_GetAttrString(ldb_mod, "Ldb");
68 ret = (PyLdbObject *)ldb_ctx_type->tp_alloc(ldb_ctx_type, 0);
69 if (ret == NULL) {
70 PyErr_NoMemory();
71 return NULL;
73 ret->mem_ctx = talloc_new(NULL);
74 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
75 return (PyObject *)ret;
78 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
79 struct provision_settings *settings,
80 struct provision_result *result)
82 const char *configfile;
83 PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_lp_ctx;
85 DEBUG(0,("Provision for Become-DC test using python\n"));
87 Py_Initialize();
88 py_update_path(); /* Put the samba path at the start of sys.path */
90 provision_mod = provision_module();
92 if (provision_mod == NULL) {
93 PyErr_Print();
94 DEBUG(0, ("Unable to import provision Python module.\n"));
95 return NT_STATUS_UNSUCCESSFUL;
98 provision_dict = PyModule_GetDict(provision_mod);
100 if (provision_dict == NULL) {
101 DEBUG(0, ("Unable to get dictionary for provision module\n"));
102 return NT_STATUS_UNSUCCESSFUL;
105 provision_fn = PyDict_GetItemString(provision_dict, "provision_become_dc");
106 if (provision_fn == NULL) {
107 PyErr_Print();
108 DEBUG(0, ("Unable to get provision_become_dc function\n"));
109 return NT_STATUS_UNSUCCESSFUL;
112 DEBUG(0,("New Server in Site[%s]\n",
113 settings->site_name));
115 DEBUG(0,("DSA Instance [%s]\n"
116 "\tinvocationId[%s]\n",
117 settings->ntds_dn_str,
118 settings->invocation_id == NULL?"None":GUID_string(mem_ctx, settings->invocation_id)));
120 DEBUG(0,("Paths under targetdir[%s]\n",
121 settings->targetdir));
122 parameters = PyDict_New();
124 configfile = lpcfg_configfile(lp_ctx);
125 if (configfile != NULL) {
126 PyDict_SetItemString(parameters, "smbconf",
127 PyStr_FromString(configfile));
130 PyDict_SetItemString(parameters, "rootdn",
131 PyStr_FromString(settings->root_dn_str));
132 if (settings->targetdir != NULL)
133 PyDict_SetItemString(parameters, "targetdir",
134 PyStr_FromString(settings->targetdir));
135 PyDict_SetItemString(parameters, "hostname",
136 PyStr_FromString(settings->netbios_name));
137 PyDict_SetItemString(parameters, "domain",
138 PyStr_FromString(settings->domain));
139 PyDict_SetItemString(parameters, "realm",
140 PyStr_FromString(settings->realm));
141 if (settings->root_dn_str)
142 PyDict_SetItemString(parameters, "rootdn",
143 PyStr_FromString(settings->root_dn_str));
145 if (settings->domain_dn_str)
146 PyDict_SetItemString(parameters, "domaindn",
147 PyStr_FromString(settings->domain_dn_str));
149 if (settings->schema_dn_str)
150 PyDict_SetItemString(parameters, "schemadn",
151 PyStr_FromString(settings->schema_dn_str));
153 if (settings->config_dn_str)
154 PyDict_SetItemString(parameters, "configdn",
155 PyStr_FromString(settings->config_dn_str));
157 if (settings->server_dn_str)
158 PyDict_SetItemString(parameters, "serverdn",
159 PyStr_FromString(settings->server_dn_str));
161 if (settings->site_name)
162 PyDict_SetItemString(parameters, "sitename",
163 PyStr_FromString(settings->site_name));
165 PyDict_SetItemString(parameters, "machinepass",
166 PyStr_FromString(settings->machine_password));
169 PyDict_SetItemString(parameters, "debuglevel", PyInt_FromLong(DEBUGLEVEL));
171 PyDict_SetItemString(parameters, "use_ntvfs", PyInt_FromLong(settings->use_ntvfs));
173 py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
175 Py_DECREF(parameters);
177 if (py_result == NULL) {
178 PyErr_Print();
179 PyErr_Clear();
180 return NT_STATUS_UNSUCCESSFUL;
183 result->domaindn = talloc_strdup(mem_ctx, PyStr_AsString(PyObject_GetAttrString(py_result, "domaindn")));
185 /* FIXME paths */
186 py_lp_ctx = PyObject_GetAttrString(py_result, "lp");
187 if (py_lp_ctx == NULL) {
188 DEBUG(0, ("Missing 'lp' attribute"));
189 return NT_STATUS_UNSUCCESSFUL;
191 result->lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
192 result->samdb = pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb"));
194 return NT_STATUS_OK;
197 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
199 PyObject *mod_security, *dom_sid_Type;
201 mod_security = PyImport_ImportModule("samba.dcerpc.security");
202 if (mod_security == NULL)
203 return NULL;
205 dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
206 if (dom_sid_Type == NULL)
207 return NULL;
209 return pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
212 NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
213 struct tevent_context *event_ctx,
214 struct provision_store_self_join_settings *settings,
215 const char **error_string)
217 int ret;
218 PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_sid;
219 struct ldb_context *ldb;
220 TALLOC_CTX *tmp_mem = talloc_new(mem_ctx);
222 *error_string = NULL;
224 if (!tmp_mem) {
225 return NT_STATUS_NO_MEMORY;
228 /* Open the secrets database */
229 ldb = secrets_db_connect(tmp_mem, lp_ctx);
230 if (!ldb) {
231 *error_string
232 = talloc_asprintf(mem_ctx,
233 "Could not open secrets database");
234 talloc_free(tmp_mem);
235 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
238 ret = ldb_transaction_start(ldb);
240 if (ret != LDB_SUCCESS) {
241 *error_string
242 = talloc_asprintf(mem_ctx,
243 "Could not start transaction on secrets database: %s", ldb_errstring(ldb));
244 talloc_free(tmp_mem);
245 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
248 Py_Initialize();
249 py_update_path(); /* Put the samba path at the start of sys.path */
250 provision_mod = provision_module();
252 if (provision_mod == NULL) {
253 PyErr_Print();
254 *error_string
255 = talloc_asprintf(mem_ctx, "Unable to import provision Python module.");
256 talloc_free(tmp_mem);
257 return NT_STATUS_UNSUCCESSFUL;
260 provision_dict = PyModule_GetDict(provision_mod);
262 if (provision_dict == NULL) {
263 *error_string
264 = talloc_asprintf(mem_ctx, "Unable to get dictionary for provision module");
265 talloc_free(tmp_mem);
266 return NT_STATUS_UNSUCCESSFUL;
269 provision_fn = PyDict_GetItemString(provision_dict, "secretsdb_self_join");
270 if (provision_fn == NULL) {
271 PyErr_Print();
272 *error_string
273 = talloc_asprintf(mem_ctx, "Unable to get provision_become_dc function");
274 talloc_free(tmp_mem);
275 return NT_STATUS_UNSUCCESSFUL;
278 parameters = PyDict_New();
280 PyDict_SetItemString(parameters, "secretsdb",
281 PyLdb_FromLdbContext(ldb));
282 PyDict_SetItemString(parameters, "domain",
283 PyStr_FromString(settings->domain_name));
284 if (settings->realm != NULL) {
285 PyDict_SetItemString(parameters, "realm",
286 PyStr_FromString(settings->realm));
288 PyDict_SetItemString(parameters, "machinepass",
289 PyStr_FromString(settings->machine_password));
290 PyDict_SetItemString(parameters, "netbiosname",
291 PyStr_FromString(settings->netbios_name));
293 py_sid = py_dom_sid_FromSid(settings->domain_sid);
294 if (py_sid == NULL) {
295 Py_DECREF(parameters);
296 goto failure;
299 PyDict_SetItemString(parameters, "domainsid",
300 py_sid);
302 PyDict_SetItemString(parameters, "secure_channel_type",
303 PyInt_FromLong(settings->secure_channel_type));
305 PyDict_SetItemString(parameters, "key_version_number",
306 PyInt_FromLong(settings->key_version_number));
308 py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
310 Py_DECREF(parameters);
312 if (py_result == NULL) {
313 goto failure;
316 ret = ldb_transaction_commit(ldb);
317 if (ret != LDB_SUCCESS) {
318 *error_string
319 = talloc_asprintf(mem_ctx,
320 "Could not commit transaction on secrets database: %s", ldb_errstring(ldb));
321 talloc_free(tmp_mem);
322 return NT_STATUS_INTERNAL_DB_ERROR;
325 talloc_free(tmp_mem);
327 return NT_STATUS_OK;
329 failure:
330 ldb_transaction_cancel(ldb);
331 talloc_free(tmp_mem);
333 PyErr_Print();
334 PyErr_Clear();
335 return NT_STATUS_UNSUCCESSFUL;
339 struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx,
340 struct loadparm_context *lp_ctx,
341 const char *schema_dn,
342 DATA_BLOB *override_prefixmap)
344 PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
346 Py_Initialize();
347 py_update_path(); /* Put the samba path at the start of sys.path */
349 schema_mod = schema_module();
351 if (schema_mod == NULL) {
352 PyErr_Print();
353 DEBUG(0, ("Unable to import schema Python module.\n"));
354 return NULL;
357 schema_dict = PyModule_GetDict(schema_mod);
359 if (schema_dict == NULL) {
360 DEBUG(0, ("Unable to get dictionary for schema module\n"));
361 return NULL;
364 schema_fn = PyDict_GetItemString(schema_dict, "ldb_with_schema");
365 if (schema_fn == NULL) {
366 PyErr_Print();
367 DEBUG(0, ("Unable to get schema_get_ldb function\n"));
368 return NULL;
371 parameters = PyDict_New();
373 if (schema_dn) {
374 PyDict_SetItemString(parameters, "schemadn",
375 PyStr_FromString(schema_dn));
378 if (override_prefixmap) {
379 PyDict_SetItemString(parameters, "override_prefixmap",
380 PyStr_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) {
389 PyErr_Print();
390 PyErr_Clear();
391 return NULL;
394 return pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "ldb"));