Line data Source code
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
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 :
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"
33 :
34 256 : static bool dict_insert(PyObject* dict,
35 : const char* key,
36 : PyObject* value)
37 : {
38 256 : if (value == NULL) {
39 0 : return false;
40 : }
41 256 : if (PyDict_SetItemString(dict, key, value) == -1) {
42 0 : Py_XDECREF(value);
43 0 : return false;
44 : }
45 256 : Py_XDECREF(value);
46 256 : return true;
47 : }
48 :
49 11 : static PyObject *provision_module(void)
50 : {
51 11 : PyObject *name = PyUnicode_FromString("samba.provision");
52 11 : PyObject *mod = NULL;
53 11 : if (name == NULL)
54 0 : return NULL;
55 11 : mod = PyImport_Import(name);
56 11 : Py_CLEAR(name);
57 11 : return mod;
58 : }
59 :
60 70 : static PyObject *schema_module(void)
61 : {
62 70 : PyObject *name = PyUnicode_FromString("samba.schema");
63 70 : PyObject *mod = NULL;
64 70 : if (name == NULL)
65 0 : return NULL;
66 70 : mod = PyImport_Import(name);
67 70 : Py_CLEAR(name);
68 70 : return mod;
69 : }
70 :
71 7 : static PyObject *ldb_module(void)
72 : {
73 7 : PyObject *name = PyUnicode_FromString("ldb");
74 7 : PyObject *mod = NULL;
75 7 : if (name == NULL)
76 0 : return NULL;
77 7 : mod = PyImport_Import(name);
78 7 : Py_CLEAR(name);
79 7 : return mod;
80 : }
81 :
82 7 : static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
83 : {
84 : PyLdbObject *ret;
85 7 : PyObject *ldb_mod = ldb_module();
86 : PyTypeObject *ldb_ctx_type;
87 7 : if (ldb_mod == NULL)
88 0 : return NULL;
89 :
90 7 : ldb_ctx_type = (PyTypeObject *)PyObject_GetAttrString(ldb_mod, "Ldb");
91 :
92 7 : ret = (PyLdbObject *)ldb_ctx_type->tp_alloc(ldb_ctx_type, 0);
93 7 : if (ret == NULL) {
94 0 : PyErr_NoMemory();
95 0 : Py_XDECREF(ldb_ctx_type);
96 0 : return NULL;
97 : }
98 7 : ret->mem_ctx = talloc_new(NULL);
99 7 : ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
100 7 : Py_XDECREF(ldb_ctx_type);
101 7 : return (PyObject *)ret;
102 : }
103 :
104 4 : NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
105 : struct provision_settings *settings,
106 : struct provision_result *result)
107 : {
108 : const char *configfile;
109 4 : PyObject *provision_mod = NULL, *provision_dict = NULL;
110 4 : PyObject *provision_fn = NULL, *py_result = NULL;
111 4 : PyObject *parameters = NULL, *py_lp_ctx = NULL, *py_domaindn = NULL;
112 :
113 : struct ldb_context *samdb;
114 4 : NTSTATUS status = NT_STATUS_OK;
115 :
116 4 : DEBUG(0,("Provision for Become-DC test using python\n"));
117 :
118 4 : Py_Initialize();
119 4 : py_update_path(); /* Put the samba path at the start of sys.path */
120 :
121 4 : provision_mod = provision_module();
122 :
123 4 : if (provision_mod == NULL) {
124 0 : PyErr_Print();
125 0 : DEBUG(0, ("Unable to import provision Python module.\n"));
126 0 : return NT_STATUS_UNSUCCESSFUL;
127 : }
128 :
129 4 : provision_dict = PyModule_GetDict(provision_mod);
130 :
131 4 : if (provision_dict == NULL) {
132 0 : DEBUG(0, ("Unable to get dictionary for provision module\n"));
133 0 : return NT_STATUS_UNSUCCESSFUL;
134 : }
135 :
136 4 : provision_fn = PyDict_GetItemString(provision_dict, "provision_become_dc");
137 4 : if (provision_fn == NULL) {
138 0 : PyErr_Print();
139 0 : DEBUG(0, ("Unable to get provision_become_dc function\n"));
140 0 : return NT_STATUS_UNSUCCESSFUL;
141 : }
142 :
143 4 : DEBUG(0,("New Server in Site[%s]\n",
144 : settings->site_name));
145 :
146 4 : DEBUG(0,("DSA Instance [%s]\n"
147 : "\tinvocationId[%s]\n",
148 : settings->ntds_dn_str,
149 : settings->invocation_id == NULL?"None":GUID_string(mem_ctx, settings->invocation_id)));
150 :
151 4 : DEBUG(0,("Paths under targetdir[%s]\n",
152 : settings->targetdir));
153 4 : parameters = PyDict_New();
154 :
155 4 : configfile = lpcfg_configfile(lp_ctx);
156 4 : if (configfile != NULL) {
157 4 : if (!dict_insert(parameters, "smbconf",
158 : PyUnicode_FromString(configfile))) {
159 0 : status = NT_STATUS_UNSUCCESSFUL;
160 0 : goto out;
161 : }
162 : }
163 :
164 4 : if (!dict_insert(parameters,
165 : "rootdn",
166 : PyUnicode_FromString(settings->root_dn_str))) {
167 0 : status = NT_STATUS_UNSUCCESSFUL;
168 0 : goto out;
169 : }
170 4 : if (settings->targetdir != NULL) {
171 4 : if (!dict_insert(parameters,
172 : "targetdir",
173 : PyUnicode_FromString(settings->targetdir))) {
174 0 : status = NT_STATUS_UNSUCCESSFUL;
175 0 : goto out;
176 : }
177 : }
178 4 : if (!dict_insert(parameters,
179 : "hostname",
180 : PyUnicode_FromString(settings->netbios_name))) {
181 0 : status = NT_STATUS_UNSUCCESSFUL;
182 0 : goto out;
183 : }
184 4 : if (!dict_insert(parameters,
185 : "domain",
186 : PyUnicode_FromString(settings->domain))) {
187 0 : status = NT_STATUS_UNSUCCESSFUL;
188 0 : goto out;
189 : }
190 4 : if (!dict_insert(parameters,
191 : "realm",
192 : PyUnicode_FromString(settings->realm))) {
193 0 : status = NT_STATUS_UNSUCCESSFUL;
194 0 : goto out;
195 : }
196 4 : if (settings->root_dn_str) {
197 4 : if (!dict_insert(parameters,
198 : "rootdn",
199 : PyUnicode_FromString(settings->root_dn_str))) {
200 0 : status = NT_STATUS_UNSUCCESSFUL;
201 0 : goto out;
202 : }
203 : }
204 :
205 4 : if (settings->domain_dn_str) {
206 4 : if (!dict_insert(parameters,
207 : "domaindn",
208 : PyUnicode_FromString(settings->domain_dn_str))) {
209 0 : status = NT_STATUS_UNSUCCESSFUL;
210 0 : goto out;
211 : }
212 : }
213 :
214 4 : if (settings->schema_dn_str) {
215 4 : if (!dict_insert(parameters,
216 : "schemadn",
217 : PyUnicode_FromString(settings->schema_dn_str))) {
218 0 : status = NT_STATUS_UNSUCCESSFUL;
219 0 : goto out;
220 : }
221 : }
222 4 : if (settings->config_dn_str) {
223 4 : if (!dict_insert(parameters,
224 : "configdn",
225 : PyUnicode_FromString(settings->config_dn_str))) {
226 0 : status = NT_STATUS_UNSUCCESSFUL;
227 0 : goto out;
228 : }
229 : }
230 4 : if (settings->server_dn_str) {
231 4 : if (!dict_insert(parameters,
232 : "serverdn",
233 : PyUnicode_FromString(settings->server_dn_str))) {
234 0 : status = NT_STATUS_UNSUCCESSFUL;
235 0 : goto out;
236 : }
237 : }
238 4 : if (settings->site_name) {
239 4 : if (!dict_insert(parameters,
240 : "sitename",
241 : PyUnicode_FromString(settings->site_name))) {
242 0 : status = NT_STATUS_UNSUCCESSFUL;
243 0 : goto out;
244 : }
245 : }
246 :
247 4 : if (!dict_insert(parameters,
248 : "machinepass",
249 : PyUnicode_FromString(settings->machine_password))){
250 0 : status = NT_STATUS_UNSUCCESSFUL;
251 0 : goto out;
252 : }
253 :
254 4 : if (!dict_insert(parameters,
255 : "debuglevel",
256 4 : PyLong_FromLong(DEBUGLEVEL))) {
257 0 : status = NT_STATUS_UNSUCCESSFUL;
258 0 : goto out;
259 : }
260 :
261 4 : if (!dict_insert(parameters,
262 : "use_ntvfs",
263 4 : PyLong_FromLong(settings->use_ntvfs))) {
264 0 : status = NT_STATUS_UNSUCCESSFUL;
265 0 : goto out;
266 : }
267 :
268 4 : py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
269 :
270 4 : if (py_result == NULL) {
271 0 : status = NT_STATUS_UNSUCCESSFUL;
272 0 : goto out;
273 : }
274 :
275 4 : py_domaindn = PyObject_GetAttrString(py_result, "domaindn");
276 4 : result->domaindn = talloc_strdup(mem_ctx, PyUnicode_AsUTF8(py_domaindn));
277 :
278 : /* FIXME paths */
279 4 : py_lp_ctx = PyObject_GetAttrString(py_result, "lp");
280 4 : if (py_lp_ctx == NULL) {
281 0 : DEBUG(0, ("Missing 'lp' attribute"));
282 0 : status = NT_STATUS_UNSUCCESSFUL;
283 0 : goto out;
284 : }
285 4 : result->lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
286 :
287 4 : samdb = pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb"));
288 4 : if (samdb == NULL) {
289 0 : DEBUG(0, ("Missing 'samdb' attribute"));
290 0 : status = NT_STATUS_UNSUCCESSFUL;
291 0 : goto out;
292 : }
293 4 : result->samdb = samdb;
294 4 : status = NT_STATUS_OK;
295 4 : out:
296 4 : Py_CLEAR(parameters);
297 4 : Py_CLEAR(provision_mod);
298 4 : Py_CLEAR(provision_fn);
299 4 : Py_CLEAR(provision_dict);
300 4 : Py_CLEAR(py_result);
301 4 : Py_CLEAR(py_lp_ctx);
302 4 : Py_CLEAR(py_domaindn);
303 4 : if (!NT_STATUS_IS_OK(status)) {
304 0 : PyErr_Print();
305 0 : PyErr_Clear();
306 : }
307 4 : return status;
308 : }
309 :
310 7 : static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
311 : {
312 7 : PyObject *mod_security = NULL, *dom_sid_Type = NULL, *result = NULL;
313 :
314 7 : mod_security = PyImport_ImportModule("samba.dcerpc.security");
315 7 : if (mod_security == NULL) {
316 0 : return NULL;
317 : }
318 :
319 7 : dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
320 7 : if (dom_sid_Type == NULL) {
321 0 : Py_DECREF(mod_security);
322 0 : return NULL;
323 : }
324 :
325 7 : result = pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
326 7 : Py_DECREF(mod_security);
327 7 : Py_DECREF(dom_sid_Type);
328 7 : return result;
329 : }
330 :
331 7 : NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
332 : struct tevent_context *event_ctx,
333 : struct provision_store_self_join_settings *settings,
334 : const char **error_string)
335 : {
336 : int ret;
337 7 : PyObject *provision_mod = NULL, *provision_dict = NULL;
338 7 : PyObject *provision_fn = NULL, *py_result = NULL;
339 7 : PyObject *parameters = NULL;
340 7 : struct ldb_context *ldb = NULL;
341 7 : TALLOC_CTX *tmp_mem = talloc_new(mem_ctx);
342 :
343 7 : NTSTATUS status = NT_STATUS_OK;
344 7 : *error_string = NULL;
345 :
346 7 : if (!tmp_mem) {
347 0 : status = NT_STATUS_UNSUCCESSFUL;
348 0 : goto out;
349 : }
350 :
351 : /* Create/Open the secrets database */
352 7 : ldb = secrets_db_create(tmp_mem, lp_ctx);
353 7 : if (!ldb) {
354 : *error_string
355 0 : = talloc_asprintf(mem_ctx,
356 : "Could not open secrets database");
357 0 : status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
358 0 : goto out;
359 : }
360 :
361 7 : ret = ldb_transaction_start(ldb);
362 :
363 7 : if (ret != LDB_SUCCESS) {
364 : *error_string
365 0 : = talloc_asprintf(mem_ctx,
366 : "Could not start transaction on secrets database: %s", ldb_errstring(ldb));
367 0 : status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
368 0 : goto out;
369 : }
370 :
371 7 : Py_Initialize();
372 7 : py_update_path(); /* Put the samba path at the start of sys.path */
373 7 : provision_mod = provision_module();
374 :
375 7 : if (provision_mod == NULL) {
376 : *error_string
377 0 : = talloc_asprintf(mem_ctx, "Unable to import provision Python module.");
378 0 : status = NT_STATUS_UNSUCCESSFUL;
379 0 : goto out;
380 : }
381 :
382 7 : provision_dict = PyModule_GetDict(provision_mod);
383 :
384 7 : if (provision_dict == NULL) {
385 : *error_string
386 0 : = talloc_asprintf(mem_ctx, "Unable to get dictionary for provision module");
387 0 : status = NT_STATUS_UNSUCCESSFUL;
388 0 : goto out;
389 : }
390 :
391 7 : provision_fn = PyDict_GetItemString(provision_dict, "secretsdb_self_join");
392 7 : if (provision_fn == NULL) {
393 : *error_string
394 0 : = talloc_asprintf(mem_ctx, "Unable to get provision_become_dc function");
395 0 : status = NT_STATUS_UNSUCCESSFUL;
396 0 : goto out;
397 : }
398 :
399 7 : parameters = PyDict_New();
400 :
401 7 : if(!dict_insert(parameters,
402 : "secretsdb",
403 : PyLdb_FromLdbContext(ldb))){
404 0 : status = NT_STATUS_UNSUCCESSFUL;
405 0 : goto out;
406 : }
407 7 : if (!dict_insert(parameters,
408 : "domain",
409 : PyUnicode_FromString(settings->domain_name))) {
410 0 : status = NT_STATUS_UNSUCCESSFUL;
411 0 : goto out;
412 : }
413 7 : if (settings->realm != NULL) {
414 7 : if (!dict_insert(parameters,
415 : "realm",
416 : PyUnicode_FromString(settings->realm))) {
417 0 : status = NT_STATUS_UNSUCCESSFUL;
418 0 : goto out;
419 : }
420 : }
421 7 : if (!dict_insert(parameters,
422 : "machinepass",
423 : PyUnicode_FromString(settings->machine_password))) {
424 0 : status = NT_STATUS_UNSUCCESSFUL;
425 0 : goto out;
426 : }
427 7 : if (!dict_insert(parameters,
428 : "netbiosname",
429 : PyUnicode_FromString(settings->netbios_name))) {
430 0 : status = NT_STATUS_UNSUCCESSFUL;
431 0 : goto out;
432 : }
433 :
434 :
435 7 : if (!dict_insert(parameters,
436 : "domainsid",
437 : py_dom_sid_FromSid(settings->domain_sid))) {
438 0 : status = NT_STATUS_UNSUCCESSFUL;
439 0 : goto out;
440 : }
441 :
442 7 : if (!dict_insert(parameters,
443 : "secure_channel_type",
444 7 : PyLong_FromLong(settings->secure_channel_type))) {
445 0 : status = NT_STATUS_UNSUCCESSFUL;
446 0 : goto out;
447 : }
448 :
449 7 : if (!dict_insert(parameters,
450 : "key_version_number",
451 7 : PyLong_FromLong(settings->key_version_number))) {
452 0 : status = NT_STATUS_UNSUCCESSFUL;
453 0 : goto out;
454 : }
455 :
456 7 : py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
457 :
458 7 : if (py_result == NULL) {
459 0 : ldb_transaction_cancel(ldb);
460 0 : status = NT_STATUS_UNSUCCESSFUL;
461 0 : goto out;
462 : }
463 :
464 7 : ret = ldb_transaction_commit(ldb);
465 7 : if (ret != LDB_SUCCESS) {
466 : *error_string
467 0 : = talloc_asprintf(mem_ctx,
468 : "Could not commit transaction on secrets database: %s", ldb_errstring(ldb));
469 0 : status = NT_STATUS_INTERNAL_DB_ERROR;
470 0 : goto out;
471 : }
472 :
473 7 : status = NT_STATUS_OK;
474 7 : out:
475 7 : talloc_free(tmp_mem);
476 7 : Py_CLEAR(parameters);
477 7 : Py_CLEAR(provision_mod);
478 7 : Py_CLEAR(provision_dict);
479 7 : Py_CLEAR(py_result);
480 7 : if (!NT_STATUS_IS_OK(status)) {
481 0 : PyErr_Print();
482 0 : PyErr_Clear();
483 : }
484 7 : return status;
485 : }
486 :
487 :
488 70 : struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx,
489 : struct loadparm_context *lp_ctx,
490 : const char *schema_dn,
491 : DATA_BLOB *override_prefixmap)
492 : {
493 : PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
494 70 : PyObject *py_ldb = NULL;
495 70 : struct ldb_context *ldb_result = NULL;
496 70 : Py_Initialize();
497 70 : py_update_path(); /* Put the samba path at the start of sys.path */
498 :
499 70 : schema_mod = schema_module();
500 :
501 70 : if (schema_mod == NULL) {
502 0 : PyErr_Print();
503 0 : DEBUG(0, ("Unable to import schema Python module.\n"));
504 0 : return NULL;
505 : }
506 :
507 70 : schema_dict = PyModule_GetDict(schema_mod);
508 :
509 70 : if (schema_dict == NULL) {
510 0 : DEBUG(0, ("Unable to get dictionary for schema module\n"));
511 0 : return NULL;
512 : }
513 :
514 70 : schema_fn = PyDict_GetItemString(schema_dict, "ldb_with_schema");
515 70 : if (schema_fn == NULL) {
516 0 : PyErr_Print();
517 0 : DEBUG(0, ("Unable to get schema_get_ldb function\n"));
518 0 : return NULL;
519 : }
520 :
521 70 : parameters = PyDict_New();
522 :
523 70 : if (schema_dn) {
524 70 : if (!dict_insert(parameters,
525 : "schemadn",
526 : PyUnicode_FromString(schema_dn))) {
527 0 : return NULL;
528 : }
529 : }
530 :
531 70 : if (override_prefixmap) {
532 138 : if (!dict_insert(parameters,
533 : "override_prefixmap",
534 : PyBytes_FromStringAndSize(
535 70 : (const char *)override_prefixmap->data,
536 70 : override_prefixmap->length))) {
537 0 : return NULL;
538 : }
539 : }
540 :
541 70 : py_result = PyEval_CallObjectWithKeywords(schema_fn, NULL, parameters);
542 :
543 70 : Py_DECREF(parameters);
544 :
545 70 : if (py_result == NULL) {
546 0 : PyErr_Print();
547 0 : PyErr_Clear();
548 0 : return NULL;
549 : }
550 :
551 70 : py_ldb = PyObject_GetAttrString(py_result, "ldb");
552 70 : Py_DECREF(py_result);
553 70 : ldb_result = pyldb_Ldb_AsLdbContext(py_ldb);
554 70 : if (talloc_reference(mem_ctx, ldb_result) == NULL) {
555 0 : ldb_result = NULL;
556 : }
557 70 : Py_DECREF(py_ldb);
558 70 : return ldb_result;
559 : }
|