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 <ldb.h>
|
---|
23 | #include <pyldb.h>
|
---|
24 | #include "includes.h"
|
---|
25 | #include "librpc/ndr/libndr.h"
|
---|
26 | #include "param/provision.h"
|
---|
27 | #include "param/secrets.h"
|
---|
28 | #include <pytalloc.h>
|
---|
29 | #include "python/modules.h"
|
---|
30 | #include "param/pyparam.h"
|
---|
31 | #include "dynconfig/dynconfig.h"
|
---|
32 |
|
---|
33 | static PyObject *provision_module(void)
|
---|
34 | {
|
---|
35 | PyObject *name = PyString_FromString("samba.provision");
|
---|
36 | if (name == NULL)
|
---|
37 | return NULL;
|
---|
38 | return PyImport_Import(name);
|
---|
39 | }
|
---|
40 |
|
---|
41 | static PyObject *schema_module(void)
|
---|
42 | {
|
---|
43 | PyObject *name = PyString_FromString("samba.schema");
|
---|
44 | if (name == NULL)
|
---|
45 | return NULL;
|
---|
46 | return PyImport_Import(name);
|
---|
47 | }
|
---|
48 |
|
---|
49 | static PyObject *ldb_module(void)
|
---|
50 | {
|
---|
51 | PyObject *name = PyString_FromString("ldb");
|
---|
52 | if (name == NULL)
|
---|
53 | return NULL;
|
---|
54 | return PyImport_Import(name);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
|
---|
58 | {
|
---|
59 | PyLdbObject *ret;
|
---|
60 | PyObject *ldb_mod = ldb_module();
|
---|
61 | PyTypeObject *ldb_ctx_type;
|
---|
62 | if (ldb_mod == NULL)
|
---|
63 | return NULL;
|
---|
64 |
|
---|
65 | ldb_ctx_type = (PyTypeObject *)PyObject_GetAttrString(ldb_mod, "Ldb");
|
---|
66 |
|
---|
67 | ret = (PyLdbObject *)ldb_ctx_type->tp_alloc(ldb_ctx_type, 0);
|
---|
68 | if (ret == NULL) {
|
---|
69 | PyErr_NoMemory();
|
---|
70 | return NULL;
|
---|
71 | }
|
---|
72 | ret->mem_ctx = talloc_new(NULL);
|
---|
73 | ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
|
---|
74 | return (PyObject *)ret;
|
---|
75 | }
|
---|
76 |
|
---|
77 | NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
|
---|
78 | struct provision_settings *settings,
|
---|
79 | struct provision_result *result)
|
---|
80 | {
|
---|
81 | const char *configfile;
|
---|
82 | PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_lp_ctx;
|
---|
83 |
|
---|
84 | DEBUG(0,("Provision for Become-DC test using python\n"));
|
---|
85 |
|
---|
86 | Py_Initialize();
|
---|
87 | py_update_path(); /* Put the samba path at the start of sys.path */
|
---|
88 |
|
---|
89 | provision_mod = provision_module();
|
---|
90 |
|
---|
91 | if (provision_mod == NULL) {
|
---|
92 | PyErr_Print();
|
---|
93 | DEBUG(0, ("Unable to import provision Python module.\n"));
|
---|
94 | return NT_STATUS_UNSUCCESSFUL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | provision_dict = PyModule_GetDict(provision_mod);
|
---|
98 |
|
---|
99 | if (provision_dict == NULL) {
|
---|
100 | DEBUG(0, ("Unable to get dictionary for provision module\n"));
|
---|
101 | return NT_STATUS_UNSUCCESSFUL;
|
---|
102 | }
|
---|
103 |
|
---|
104 | provision_fn = PyDict_GetItemString(provision_dict, "provision_become_dc");
|
---|
105 | if (provision_fn == NULL) {
|
---|
106 | PyErr_Print();
|
---|
107 | DEBUG(0, ("Unable to get provision_become_dc function\n"));
|
---|
108 | return NT_STATUS_UNSUCCESSFUL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | DEBUG(0,("New Server in Site[%s]\n",
|
---|
112 | settings->site_name));
|
---|
113 |
|
---|
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)));
|
---|
118 |
|
---|
119 | DEBUG(0,("Paths under targetdir[%s]\n",
|
---|
120 | settings->targetdir));
|
---|
121 | parameters = PyDict_New();
|
---|
122 |
|
---|
123 | configfile = lpcfg_configfile(lp_ctx);
|
---|
124 | if (configfile != NULL) {
|
---|
125 | PyDict_SetItemString(parameters, "smbconf",
|
---|
126 | PyString_FromString(configfile));
|
---|
127 | }
|
---|
128 |
|
---|
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 | PyDict_SetItemString(parameters, "hostname",
|
---|
135 | PyString_FromString(settings->netbios_name));
|
---|
136 | PyDict_SetItemString(parameters, "domain",
|
---|
137 | PyString_FromString(settings->domain));
|
---|
138 | PyDict_SetItemString(parameters, "realm",
|
---|
139 | PyString_FromString(settings->realm));
|
---|
140 | if (settings->root_dn_str)
|
---|
141 | PyDict_SetItemString(parameters, "rootdn",
|
---|
142 | PyString_FromString(settings->root_dn_str));
|
---|
143 |
|
---|
144 | if (settings->domain_dn_str)
|
---|
145 | PyDict_SetItemString(parameters, "domaindn",
|
---|
146 | PyString_FromString(settings->domain_dn_str));
|
---|
147 |
|
---|
148 | if (settings->schema_dn_str)
|
---|
149 | PyDict_SetItemString(parameters, "schemadn",
|
---|
150 | PyString_FromString(settings->schema_dn_str));
|
---|
151 |
|
---|
152 | if (settings->config_dn_str)
|
---|
153 | PyDict_SetItemString(parameters, "configdn",
|
---|
154 | PyString_FromString(settings->config_dn_str));
|
---|
155 |
|
---|
156 | if (settings->server_dn_str)
|
---|
157 | PyDict_SetItemString(parameters, "serverdn",
|
---|
158 | PyString_FromString(settings->server_dn_str));
|
---|
159 |
|
---|
160 | if (settings->site_name)
|
---|
161 | PyDict_SetItemString(parameters, "sitename",
|
---|
162 | PyString_FromString(settings->site_name));
|
---|
163 |
|
---|
164 | PyDict_SetItemString(parameters, "machinepass",
|
---|
165 | PyString_FromString(settings->machine_password));
|
---|
166 |
|
---|
167 |
|
---|
168 | PyDict_SetItemString(parameters, "debuglevel", PyInt_FromLong(DEBUGLEVEL));
|
---|
169 |
|
---|
170 | PyDict_SetItemString(parameters, "use_ntvfs", PyInt_FromLong(settings->use_ntvfs));
|
---|
171 |
|
---|
172 | py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
|
---|
173 |
|
---|
174 | Py_DECREF(parameters);
|
---|
175 |
|
---|
176 | if (py_result == NULL) {
|
---|
177 | PyErr_Print();
|
---|
178 | PyErr_Clear();
|
---|
179 | return NT_STATUS_UNSUCCESSFUL;
|
---|
180 | }
|
---|
181 |
|
---|
182 | result->domaindn = talloc_strdup(mem_ctx, PyString_AsString(PyObject_GetAttrString(py_result, "domaindn")));
|
---|
183 |
|
---|
184 | /* FIXME paths */
|
---|
185 | py_lp_ctx = PyObject_GetAttrString(py_result, "lp");
|
---|
186 | if (py_lp_ctx == NULL) {
|
---|
187 | DEBUG(0, ("Missing 'lp' attribute"));
|
---|
188 | return NT_STATUS_UNSUCCESSFUL;
|
---|
189 | }
|
---|
190 | result->lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
|
---|
191 | result->samdb = pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb"));
|
---|
192 |
|
---|
193 | return NT_STATUS_OK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
|
---|
197 | {
|
---|
198 | PyObject *mod_security, *dom_sid_Type;
|
---|
199 |
|
---|
200 | mod_security = PyImport_ImportModule("samba.dcerpc.security");
|
---|
201 | if (mod_security == NULL)
|
---|
202 | return NULL;
|
---|
203 |
|
---|
204 | dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
|
---|
205 | if (dom_sid_Type == NULL)
|
---|
206 | return NULL;
|
---|
207 |
|
---|
208 | return pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
|
---|
209 | }
|
---|
210 |
|
---|
211 | NTSTATUS provision_store_self_join(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
|
---|
212 | struct tevent_context *event_ctx,
|
---|
213 | struct provision_store_self_join_settings *settings,
|
---|
214 | const char **error_string)
|
---|
215 | {
|
---|
216 | int ret;
|
---|
217 | PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_sid;
|
---|
218 | struct ldb_context *ldb;
|
---|
219 | TALLOC_CTX *tmp_mem = talloc_new(mem_ctx);
|
---|
220 | if (!tmp_mem) {
|
---|
221 | return NT_STATUS_NO_MEMORY;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /* Open the secrets database */
|
---|
225 | ldb = secrets_db_connect(tmp_mem, lp_ctx);
|
---|
226 | if (!ldb) {
|
---|
227 | *error_string
|
---|
228 | = talloc_asprintf(mem_ctx,
|
---|
229 | "Could not open secrets database");
|
---|
230 | talloc_free(tmp_mem);
|
---|
231 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
232 | }
|
---|
233 |
|
---|
234 | ret = ldb_transaction_start(ldb);
|
---|
235 |
|
---|
236 | if (ret != LDB_SUCCESS) {
|
---|
237 | *error_string
|
---|
238 | = talloc_asprintf(mem_ctx,
|
---|
239 | "Could not start transaction on secrets database: %s", ldb_errstring(ldb));
|
---|
240 | talloc_free(tmp_mem);
|
---|
241 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
242 | }
|
---|
243 |
|
---|
244 | Py_Initialize();
|
---|
245 | py_update_path(); /* Put the samba path at the start of sys.path */
|
---|
246 | provision_mod = provision_module();
|
---|
247 |
|
---|
248 | if (provision_mod == NULL) {
|
---|
249 | PyErr_Print();
|
---|
250 | *error_string
|
---|
251 | = talloc_asprintf(mem_ctx, "Unable to import provision Python module.");
|
---|
252 | talloc_free(tmp_mem);
|
---|
253 | return NT_STATUS_UNSUCCESSFUL;
|
---|
254 | }
|
---|
255 |
|
---|
256 | provision_dict = PyModule_GetDict(provision_mod);
|
---|
257 |
|
---|
258 | if (provision_dict == NULL) {
|
---|
259 | *error_string
|
---|
260 | = talloc_asprintf(mem_ctx, "Unable to get dictionary for provision module");
|
---|
261 | talloc_free(tmp_mem);
|
---|
262 | return NT_STATUS_UNSUCCESSFUL;
|
---|
263 | }
|
---|
264 |
|
---|
265 | provision_fn = PyDict_GetItemString(provision_dict, "secretsdb_self_join");
|
---|
266 | if (provision_fn == NULL) {
|
---|
267 | PyErr_Print();
|
---|
268 | *error_string
|
---|
269 | = talloc_asprintf(mem_ctx, "Unable to get provision_become_dc function");
|
---|
270 | talloc_free(tmp_mem);
|
---|
271 | return NT_STATUS_UNSUCCESSFUL;
|
---|
272 | }
|
---|
273 |
|
---|
274 | parameters = PyDict_New();
|
---|
275 |
|
---|
276 | PyDict_SetItemString(parameters, "secretsdb",
|
---|
277 | PyLdb_FromLdbContext(ldb));
|
---|
278 | PyDict_SetItemString(parameters, "domain",
|
---|
279 | PyString_FromString(settings->domain_name));
|
---|
280 | if (settings->realm != NULL) {
|
---|
281 | PyDict_SetItemString(parameters, "realm",
|
---|
282 | PyString_FromString(settings->realm));
|
---|
283 | }
|
---|
284 | PyDict_SetItemString(parameters, "machinepass",
|
---|
285 | PyString_FromString(settings->machine_password));
|
---|
286 | PyDict_SetItemString(parameters, "netbiosname",
|
---|
287 | PyString_FromString(settings->netbios_name));
|
---|
288 |
|
---|
289 | py_sid = py_dom_sid_FromSid(settings->domain_sid);
|
---|
290 | if (py_sid == NULL) {
|
---|
291 | Py_DECREF(parameters);
|
---|
292 | goto failure;
|
---|
293 | }
|
---|
294 |
|
---|
295 | PyDict_SetItemString(parameters, "domainsid",
|
---|
296 | py_sid);
|
---|
297 |
|
---|
298 | PyDict_SetItemString(parameters, "secure_channel_type",
|
---|
299 | PyInt_FromLong(settings->secure_channel_type));
|
---|
300 |
|
---|
301 | PyDict_SetItemString(parameters, "key_version_number",
|
---|
302 | PyInt_FromLong(settings->key_version_number));
|
---|
303 |
|
---|
304 | py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
|
---|
305 |
|
---|
306 | Py_DECREF(parameters);
|
---|
307 |
|
---|
308 | if (py_result == NULL) {
|
---|
309 | goto failure;
|
---|
310 | }
|
---|
311 |
|
---|
312 | ret = ldb_transaction_commit(ldb);
|
---|
313 | if (ret != LDB_SUCCESS) {
|
---|
314 | *error_string
|
---|
315 | = talloc_asprintf(mem_ctx,
|
---|
316 | "Could not commit transaction on secrets database: %s", ldb_errstring(ldb));
|
---|
317 | talloc_free(tmp_mem);
|
---|
318 | return NT_STATUS_INTERNAL_DB_ERROR;
|
---|
319 | }
|
---|
320 |
|
---|
321 | talloc_free(tmp_mem);
|
---|
322 |
|
---|
323 | return NT_STATUS_OK;
|
---|
324 |
|
---|
325 | failure:
|
---|
326 | ldb_transaction_cancel(ldb);
|
---|
327 | talloc_free(tmp_mem);
|
---|
328 |
|
---|
329 | PyErr_Print();
|
---|
330 | PyErr_Clear();
|
---|
331 | return NT_STATUS_UNSUCCESSFUL;
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | struct ldb_context *provision_get_schema(TALLOC_CTX *mem_ctx,
|
---|
336 | struct loadparm_context *lp_ctx,
|
---|
337 | const char *schema_dn,
|
---|
338 | DATA_BLOB *override_prefixmap)
|
---|
339 | {
|
---|
340 | PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
|
---|
341 |
|
---|
342 | Py_Initialize();
|
---|
343 | py_update_path(); /* Put the samba path at the start of sys.path */
|
---|
344 |
|
---|
345 | schema_mod = schema_module();
|
---|
346 |
|
---|
347 | if (schema_mod == NULL) {
|
---|
348 | PyErr_Print();
|
---|
349 | DEBUG(0, ("Unable to import schema Python module.\n"));
|
---|
350 | return NULL;
|
---|
351 | }
|
---|
352 |
|
---|
353 | schema_dict = PyModule_GetDict(schema_mod);
|
---|
354 |
|
---|
355 | if (schema_dict == NULL) {
|
---|
356 | DEBUG(0, ("Unable to get dictionary for schema module\n"));
|
---|
357 | return NULL;
|
---|
358 | }
|
---|
359 |
|
---|
360 | schema_fn = PyDict_GetItemString(schema_dict, "ldb_with_schema");
|
---|
361 | if (schema_fn == NULL) {
|
---|
362 | PyErr_Print();
|
---|
363 | DEBUG(0, ("Unable to get schema_get_ldb function\n"));
|
---|
364 | return NULL;
|
---|
365 | }
|
---|
366 |
|
---|
367 | parameters = PyDict_New();
|
---|
368 |
|
---|
369 | if (schema_dn) {
|
---|
370 | PyDict_SetItemString(parameters, "schemadn",
|
---|
371 | PyString_FromString(schema_dn));
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (override_prefixmap) {
|
---|
375 | PyDict_SetItemString(parameters, "override_prefixmap",
|
---|
376 | PyString_FromStringAndSize((const char *)override_prefixmap->data,
|
---|
377 | override_prefixmap->length));
|
---|
378 | }
|
---|
379 |
|
---|
380 | py_result = PyEval_CallObjectWithKeywords(schema_fn, NULL, parameters);
|
---|
381 |
|
---|
382 | Py_DECREF(parameters);
|
---|
383 |
|
---|
384 | if (py_result == NULL) {
|
---|
385 | PyErr_Print();
|
---|
386 | PyErr_Clear();
|
---|
387 | return NULL;
|
---|
388 | }
|
---|
389 |
|
---|
390 | return pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "ldb"));
|
---|
391 | }
|
---|