nsswitch: Fix checking for config.h #define in nsstest.h
[gd/samba-autobuild/.git] / source4 / param / provision.c
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 static PyObject *provision_module(void)
35 {
36         PyObject *name = PyStr_FromString("samba.provision");
37         if (name == NULL)
38                 return NULL;
39         return PyImport_Import(name);
40 }
41
42 static PyObject *schema_module(void)
43 {
44         PyObject *name = PyStr_FromString("samba.schema");
45         if (name == NULL)
46                 return NULL;
47         return PyImport_Import(name);
48 }
49
50 static PyObject *ldb_module(void)
51 {
52         PyObject *name = PyStr_FromString("ldb");
53         if (name == NULL)
54                 return NULL;
55         return PyImport_Import(name);
56 }
57
58 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
59 {
60         PyLdbObject *ret;
61         PyObject *ldb_mod = ldb_module();
62         PyTypeObject *ldb_ctx_type;
63         if (ldb_mod == NULL)
64                 return NULL;
65
66         ldb_ctx_type = (PyTypeObject *)PyObject_GetAttrString(ldb_mod, "Ldb");
67
68         ret = (PyLdbObject *)ldb_ctx_type->tp_alloc(ldb_ctx_type, 0);
69         if (ret == NULL) {
70                 PyErr_NoMemory();
71                 return NULL;
72         }
73         ret->mem_ctx = talloc_new(NULL);
74         ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
75         return (PyObject *)ret;
76 }
77
78 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
79                         struct provision_settings *settings, 
80                         struct provision_result *result)
81 {
82         const char *configfile;
83         PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_lp_ctx;
84         
85         DEBUG(0,("Provision for Become-DC test using python\n"));
86
87         Py_Initialize();
88         py_update_path(); /* Put the samba path at the start of sys.path */
89
90         provision_mod = provision_module();
91
92         if (provision_mod == NULL) {
93                 PyErr_Print();
94                 DEBUG(0, ("Unable to import provision Python module.\n"));
95                 return NT_STATUS_UNSUCCESSFUL;
96         }
97
98         provision_dict = PyModule_GetDict(provision_mod);
99
100         if (provision_dict == NULL) {
101                 DEBUG(0, ("Unable to get dictionary for provision module\n"));
102                 return NT_STATUS_UNSUCCESSFUL;
103         }
104
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;
110         }
111         
112         DEBUG(0,("New Server in Site[%s]\n", 
113                  settings->site_name));
114
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)));
119
120         DEBUG(0,("Paths under targetdir[%s]\n",
121                  settings->targetdir));
122         parameters = PyDict_New();
123
124         configfile = lpcfg_configfile(lp_ctx);
125         if (configfile != NULL) {
126                 PyDict_SetItemString(parameters, "smbconf", 
127                                      PyStr_FromString(configfile));
128         }
129
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));
144
145         if (settings->domain_dn_str) 
146                 PyDict_SetItemString(parameters, "domaindn", 
147                                      PyStr_FromString(settings->domain_dn_str));
148
149         if (settings->schema_dn_str) 
150                 PyDict_SetItemString(parameters, "schemadn", 
151                                      PyStr_FromString(settings->schema_dn_str));
152         
153         if (settings->config_dn_str) 
154                 PyDict_SetItemString(parameters, "configdn", 
155                                      PyStr_FromString(settings->config_dn_str));
156         
157         if (settings->server_dn_str) 
158                 PyDict_SetItemString(parameters, "serverdn", 
159                                      PyStr_FromString(settings->server_dn_str));
160         
161         if (settings->site_name) 
162                 PyDict_SetItemString(parameters, "sitename", 
163                                      PyStr_FromString(settings->site_name));
164
165         PyDict_SetItemString(parameters, "machinepass", 
166                              PyStr_FromString(settings->machine_password));
167
168         
169         PyDict_SetItemString(parameters, "debuglevel", PyInt_FromLong(DEBUGLEVEL));
170
171         PyDict_SetItemString(parameters, "use_ntvfs", PyInt_FromLong(settings->use_ntvfs));
172
173         py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
174
175         Py_DECREF(parameters);
176
177         if (py_result == NULL) {
178                 PyErr_Print();
179                 PyErr_Clear();
180                 return NT_STATUS_UNSUCCESSFUL;
181         }
182
183         result->domaindn = talloc_strdup(mem_ctx, PyStr_AsString(PyObject_GetAttrString(py_result, "domaindn")));
184
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;
190         }
191         result->lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
192         result->samdb = pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb"));
193
194         return NT_STATUS_OK;
195 }
196
197 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
198 {
199         PyObject *mod_security, *dom_sid_Type;
200
201         mod_security = PyImport_ImportModule("samba.dcerpc.security");
202         if (mod_security == NULL)
203                 return NULL;
204
205         dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
206         if (dom_sid_Type == NULL)
207                 return NULL;
208
209         return pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
210 }
211
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)
216 {
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);
221
222         *error_string = NULL;
223
224         if (!tmp_mem) {
225                 return NT_STATUS_NO_MEMORY;
226         }
227
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;
236         }
237
238         ret = ldb_transaction_start(ldb);
239
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;
246         }
247
248         Py_Initialize();
249         py_update_path(); /* Put the samba path at the start of sys.path */
250         provision_mod = provision_module();
251
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;
258         }
259
260         provision_dict = PyModule_GetDict(provision_mod);
261
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;
267         }
268
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;
276         }
277         
278         parameters = PyDict_New();
279
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));
287         }
288         PyDict_SetItemString(parameters, "machinepass", 
289                              PyStr_FromString(settings->machine_password));
290         PyDict_SetItemString(parameters, "netbiosname", 
291                              PyStr_FromString(settings->netbios_name));
292
293         py_sid = py_dom_sid_FromSid(settings->domain_sid);
294         if (py_sid == NULL) {
295                 Py_DECREF(parameters);
296                 goto failure;
297         }
298
299         PyDict_SetItemString(parameters, "domainsid", 
300                              py_sid);
301
302         PyDict_SetItemString(parameters, "secure_channel_type", 
303                        PyInt_FromLong(settings->secure_channel_type));
304
305         PyDict_SetItemString(parameters, "key_version_number", 
306                        PyInt_FromLong(settings->key_version_number));
307
308         py_result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
309
310         Py_DECREF(parameters);
311
312         if (py_result == NULL) {
313                 goto failure;
314         }
315
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;
323         }
324
325         talloc_free(tmp_mem);
326
327         return NT_STATUS_OK;
328
329 failure:
330         ldb_transaction_cancel(ldb);
331         talloc_free(tmp_mem);
332
333         PyErr_Print();
334         PyErr_Clear();
335         return NT_STATUS_UNSUCCESSFUL;
336 }
337
338
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)
343 {
344         PyObject *schema_mod, *schema_dict, *schema_fn, *py_result, *parameters;
345         
346         Py_Initialize();
347         py_update_path(); /* Put the samba path at the start of sys.path */
348
349         schema_mod = schema_module();
350
351         if (schema_mod == NULL) {
352                 PyErr_Print();
353                 DEBUG(0, ("Unable to import schema Python module.\n"));
354                 return NULL;
355         }
356
357         schema_dict = PyModule_GetDict(schema_mod);
358
359         if (schema_dict == NULL) {
360                 DEBUG(0, ("Unable to get dictionary for schema module\n"));
361                 return NULL;
362         }
363
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;
369         }
370         
371         parameters = PyDict_New();
372
373         if (schema_dn) {
374                 PyDict_SetItemString(parameters, "schemadn",
375                                      PyStr_FromString(schema_dn));
376         }
377
378         if (override_prefixmap) {
379                 PyDict_SetItemString(parameters, "override_prefixmap",
380                                      PyBytes_FromStringAndSize((const char *)override_prefixmap->data,
381                                                                 override_prefixmap->length));
382         }
383
384         py_result = PyEval_CallObjectWithKeywords(schema_fn, NULL, parameters);
385
386         Py_DECREF(parameters);
387
388         if (py_result == NULL) {
389                 PyErr_Print();
390                 PyErr_Clear();
391                 return NULL;
392         }
393
394         return pyldb_Ldb_AsLdbContext(PyObject_GetAttrString(py_result, "ldb"));
395 }