net: Add and fix some German translation
[Samba/gebeck_regimport.git] / source4 / param / provision.c
blob71405507ba87be2d1b080347ae786bd9468be8d9
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 "includes.h"
23 #include "auth/auth.h"
24 #include "ldb/include/ldb.h"
25 #include "ldb_errors.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "librpc/ndr/libndr.h"
29 #include "param/param.h"
30 #include "param/provision.h"
31 #include "param/secrets.h"
32 #include "lib/talloc/pytalloc.h"
33 #include "librpc/rpc/pyrpc.h"
34 #include "scripting/python/modules.h"
35 #include "lib/ldb/pyldb.h"
36 #include "param/pyparam.h"
37 #include "dynconfig/dynconfig.h"
39 static PyObject *provision_module(void)
41 PyObject *name = PyString_FromString("samba.provision");
42 if (name == NULL)
43 return NULL;
44 return PyImport_Import(name);
47 static PyObject *schema_module(void)
49 PyObject *name = PyString_FromString("samba.schema");
50 if (name == NULL)
51 return NULL;
52 return PyImport_Import(name);
55 static PyObject *ldb_module(void)
57 PyObject *name = PyString_FromString("ldb");
58 if (name == NULL)
59 return NULL;
60 return PyImport_Import(name);
63 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
65 PyLdbObject *ret;
66 PyObject *ldb_mod = ldb_module();
67 PyTypeObject *ldb_ctx_type;
68 if (ldb_mod == NULL)
69 return NULL;
71 ldb_ctx_type = (PyTypeObject *)PyObject_GetAttrString(ldb_mod, "Ldb");
73 ret = (PyLdbObject *)ldb_ctx_type->tp_alloc(ldb_ctx_type, 0);
74 if (ret == NULL) {
75 PyErr_NoMemory();
76 return NULL;
78 ret->mem_ctx = talloc_new(NULL);
79 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
80 return (PyObject *)ret;
83 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
84 struct provision_settings *settings,
85 struct provision_result *result)
87 const char *configfile;
88 PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_lp_ctx;
90 DEBUG(0,("Provision for Become-DC test using python\n"));
92 Py_Initialize();
93 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
95 provision_mod = provision_module();
97 if (provision_mod == NULL) {
98 PyErr_Print();
99 DEBUG(0, ("Unable to import provision Python module.\n"));
100 return NT_STATUS_UNSUCCESSFUL;
103 provision_dict = PyModule_GetDict(provision_mod);
105 if (provision_dict == NULL) {
106 DEBUG(0, ("Unable to get dictionary for provision module\n"));
107 return NT_STATUS_UNSUCCESSFUL;
110 provision_fn = PyDict_GetItemString(provision_dict, "provision_become_dc");
111 if (provision_fn == NULL) {
112 PyErr_Print();
113 DEBUG(0, ("Unable to get provision_become_dc function\n"));
114 return NT_STATUS_UNSUCCESSFUL;
117 DEBUG(0,("New Server in Site[%s]\n",
118 settings->site_name));
120 DEBUG(0,("DSA Instance [%s]\n"
121 "\tinvocationId[%s]\n",
122 settings->ntds_dn_str,
123 settings->invocation_id == NULL?"None":GUID_string(mem_ctx, settings->invocation_id)));
125 DEBUG(0,("Paths under targetdir[%s]\n",
126 settings->targetdir));
127 parameters = PyDict_New();
129 configfile = lpcfg_configfile(lp_ctx);
130 if (configfile != NULL) {
131 PyDict_SetItemString(parameters, "smbconf",
132 PyString_FromString(configfile));
135 PyDict_SetItemString(parameters, "rootdn",
136 PyString_FromString(settings->root_dn_str));
137 if (settings->targetdir != NULL)
138 PyDict_SetItemString(parameters, "targetdir",
139 PyString_FromString(settings->targetdir));
140 if (file_exist("setup/provision.smb.conf.dc")) {
141 PyDict_SetItemString(parameters, "setup_dir",
142 PyString_FromString("setup"));
143 } else {
144 PyDict_SetItemString(parameters, "setup_dir",
145 PyString_FromString(dyn_SETUPDIR));
147 PyDict_SetItemString(parameters, "hostname",
148 PyString_FromString(settings->netbios_name));
149 PyDict_SetItemString(parameters, "domain",
150 PyString_FromString(settings->domain));
151 PyDict_SetItemString(parameters, "realm",
152 PyString_FromString(settings->realm));
153 if (settings->root_dn_str)
154 PyDict_SetItemString(parameters, "rootdn",
155 PyString_FromString(settings->root_dn_str));
157 if (settings->domain_dn_str)
158 PyDict_SetItemString(parameters, "domaindn",
159 PyString_FromString(settings->domain_dn_str));
161 if (settings->schema_dn_str)
162 PyDict_SetItemString(parameters, "schemadn",
163 PyString_FromString(settings->schema_dn_str));
165 if (settings->config_dn_str)
166 PyDict_SetItemString(parameters, "configdn",
167 PyString_FromString(settings->config_dn_str));
169 if (settings->server_dn_str)
170 PyDict_SetItemString(parameters, "serverdn",
171 PyString_FromString(settings->server_dn_str));
173 if (settings->site_name)
174 PyDict_SetItemString(parameters, "sitename",
175 PyString_FromString(settings->site_name));
177 PyDict_SetItemString(parameters, "machinepass",
178 PyString_FromString(settings->machine_password));
181 PyDict_SetItemString(parameters, "debuglevel", PyInt_FromLong(DEBUGLEVEL));
183 py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
185 Py_DECREF(parameters);
187 if (py_result == NULL) {
188 PyErr_Print();
189 PyErr_Clear();
190 return NT_STATUS_UNSUCCESSFUL;
193 result->domaindn = talloc_strdup(mem_ctx, PyString_AsString(PyObject_GetAttrString(py_result, "domaindn")));
195 /* FIXME paths */
196 py_lp_ctx = PyObject_GetAttrString(py_result, "lp");
197 if (py_lp_ctx == NULL) {
198 DEBUG(0, ("Missing 'lp' attribute"));
199 return NT_STATUS_UNSUCCESSFUL;
201 result->lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
202 result->samdb = PyLdb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb"));
204 return NT_STATUS_OK;
207 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
209 PyObject *mod_security, *dom_sid_Type;
211 mod_security = PyImport_ImportModule("samba.dcerpc.security");
212 if (mod_security == NULL)
213 return NULL;
215 dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
216 if (dom_sid_Type == NULL)
217 return NULL;
219 return py_talloc_reference((PyTypeObject *)dom_sid_Type, sid);
222 NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
223 struct tevent_context *event_ctx,
224 struct provision_store_self_join_settings *settings,
225 const char **error_string)
227 int ret;
228 PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_sid;
229 struct ldb_context *ldb;
230 TALLOC_CTX *tmp_mem = talloc_new(mem_ctx);
231 if (!tmp_mem) {
232 return NT_STATUS_NO_MEMORY;
235 /* Open the secrets database */
236 ldb = secrets_db_connect(tmp_mem, lp_ctx);
237 if (!ldb) {
238 *error_string
239 = talloc_asprintf(mem_ctx,
240 "Could not open secrets database");
241 talloc_free(tmp_mem);
242 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
245 ret = ldb_transaction_start(ldb);
247 if (ret != LDB_SUCCESS) {
248 *error_string
249 = talloc_asprintf(mem_ctx,
250 "Could not start transaction on secrets database: %s", ldb_errstring(ldb));
251 talloc_free(tmp_mem);
252 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
255 Py_Initialize();
256 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
257 provision_mod = provision_module();
259 if (provision_mod == NULL) {
260 PyErr_Print();
261 *error_string
262 = talloc_asprintf(mem_ctx, "Unable to import provision Python module.");
263 talloc_free(tmp_mem);
264 return NT_STATUS_UNSUCCESSFUL;
267 provision_dict = PyModule_GetDict(provision_mod);
269 if (provision_dict == NULL) {
270 *error_string
271 = talloc_asprintf(mem_ctx, "Unable to get dictionary for provision module");
272 talloc_free(tmp_mem);
273 return NT_STATUS_UNSUCCESSFUL;
276 provision_fn = PyDict_GetItemString(provision_dict, "secretsdb_self_join");
277 if (provision_fn == NULL) {
278 PyErr_Print();
279 *error_string
280 = talloc_asprintf(mem_ctx, "Unable to get provision_become_dc function");
281 talloc_free(tmp_mem);
282 return NT_STATUS_UNSUCCESSFUL;
285 parameters = PyDict_New();
287 PyDict_SetItemString(parameters, "secretsdb",
288 PyLdb_FromLdbContext(ldb));
289 PyDict_SetItemString(parameters, "domain",
290 PyString_FromString(settings->domain_name));
291 if (settings->realm != NULL) {
292 PyDict_SetItemString(parameters, "realm",
293 PyString_FromString(settings->realm));
295 PyDict_SetItemString(parameters, "machinepass",
296 PyString_FromString(settings->machine_password));
297 PyDict_SetItemString(parameters, "netbiosname",
298 PyString_FromString(settings->netbios_name));
300 py_sid = py_dom_sid_FromSid(settings->domain_sid);
301 if (py_sid == NULL) {
302 Py_DECREF(parameters);
303 goto failure;
306 PyDict_SetItemString(parameters, "domainsid",
307 py_sid);
309 PyDict_SetItemString(parameters, "secure_channel_type",
310 PyInt_FromLong(settings->secure_channel_type));
312 PyDict_SetItemString(parameters, "key_version_number",
313 PyInt_FromLong(settings->key_version_number));
315 py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
317 Py_DECREF(parameters);
319 if (py_result == NULL) {
320 goto failure;
323 ret = ldb_transaction_commit(ldb);
324 if (ret != LDB_SUCCESS) {
325 *error_string
326 = talloc_asprintf(mem_ctx,
327 "Could not commit transaction on secrets database: %s", ldb_errstring(ldb));
328 talloc_free(tmp_mem);
329 return NT_STATUS_INTERNAL_DB_ERROR;
332 talloc_free(tmp_mem);
334 return NT_STATUS_OK;
336 failure:
337 ldb_transaction_cancel(ldb);
338 talloc_free(tmp_mem);
340 PyErr_Print();
341 PyErr_Clear();
342 return NT_STATUS_UNSUCCESSFUL;
346 struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
347 DATA_BLOB *override_prefixmap)
349 const char *setupdir;
350 PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
352 DEBUG(0,("Schema for DRS tests using python\n"));
354 Py_Initialize();
355 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
357 schema_mod = schema_module();
359 if (schema_mod == NULL) {
360 PyErr_Print();
361 DEBUG(0, ("Unable to import schema Python module.\n"));
362 return NULL;
365 schema_dict = PyModule_GetDict(schema_mod);
367 if (schema_dict == NULL) {
368 DEBUG(0, ("Unable to get dictionary for schema module\n"));
369 return NULL;
372 schema_fn = PyDict_GetItemString(schema_dict, "ldb_with_schema");
373 if (schema_fn == NULL) {
374 PyErr_Print();
375 DEBUG(0, ("Unable to get schema_get_ldb function\n"));
376 return NULL;
379 parameters = PyDict_New();
381 setupdir = lpcfg_setupdir(lp_ctx);
382 PyDict_SetItemString(parameters, "setup_dir",
383 PyString_FromString(setupdir));
384 if (override_prefixmap) {
385 PyDict_SetItemString(parameters, "override_prefixmap",
386 PyString_FromStringAndSize((const char *)override_prefixmap->data,
387 override_prefixmap->length));
390 py_result = PyEval_CallObjectWithKeywords(schema_fn, NULL, parameters);
392 Py_DECREF(parameters);
394 if (py_result == NULL) {
395 PyErr_Print();
396 PyErr_Clear();
397 return NULL;
400 return PyLdb_AsLdbContext(PyObject_GetAttrString(py_result, "ldb"));