d7c8f479e648008061690baa2e127f3574096516
[samba.git] / source4 / torture / util_provision.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "auth/auth.h"
22 #include "lib/ldb_wrap.h"
23 #include "torture/util.h"
24
25 #include "param/param.h"
26 #include <Python.h>
27 #include "scripting/python/modules.h"
28
29 NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
30                                                 struct provision_settings *settings)
31 {
32         bool ok;
33         PyObject *provision_mod, *provision_dict, *provision_fn, *result, *parameters;
34         struct ldb_context *ldb;
35         char *sam_ldb_path;
36         
37         DEBUG(0,("Provision for Become-DC test using python\n"));
38
39         py_load_samba_modules();
40         Py_Initialize();
41         py_update_path("bin"); /* FIXME: Can't assume this is always the case */
42
43         provision_mod = PyImport_Import(PyString_FromString("samba.provision"));
44
45         if (provision_mod == NULL) {
46                 PyErr_Print();
47                 DEBUG(0, ("Unable to import provision Python module.\n"));
48                 return NT_STATUS_UNSUCCESSFUL;
49         }
50
51         provision_dict = PyModule_GetDict(provision_mod);
52
53         if (provision_dict == NULL) {
54                 DEBUG(0, ("Unable to get dictionary for provision module\n"));
55                 return NT_STATUS_UNSUCCESSFUL;
56         }
57
58         provision_fn = PyDict_GetItemString(provision_dict, "provision_become_dc");
59         if (provision_fn == NULL) {
60                 PyErr_Print();
61                 DEBUG(0, ("Unable to get provision_become_dc function\n"));
62                 return NT_STATUS_UNSUCCESSFUL;
63         }
64         
65         DEBUG(0,("New Server[%s] in Site[%s]\n", settings->dns_name, 
66                          settings->site_name));
67
68         DEBUG(0,("DSA Instance [%s]\n"
69                 "\tobjectGUID[%s]\n"
70                 "\tinvocationId[%s]\n",
71                 settings->ntds_dn_str,
72                 settings->ntds_guid == NULL?"None":GUID_string(mem_ctx, settings->ntds_guid),
73                 settings->invocation_id == NULL?"None":GUID_string(mem_ctx, settings->invocation_id)));
74
75         DEBUG(0,("Pathes under targetdir[%s]\n",
76                  settings->targetdir));
77         parameters = PyDict_New();
78
79         PyDict_SetItemString(parameters, "rootdn", 
80                                                  PyString_FromString(settings->root_dn_str));
81         if (settings->targetdir != NULL)
82                 PyDict_SetItemString(parameters, "targetdir", 
83                                                          PyString_FromString(settings->targetdir));
84         PyDict_SetItemString(parameters, "setup_dir", 
85                              PyString_FromString("setup"));
86         PyDict_SetItemString(parameters, "hostname", 
87                                                  PyString_FromString(settings->netbios_name));
88         PyDict_SetItemString(parameters, "domain", 
89                                                  PyString_FromString(settings->domain));
90         PyDict_SetItemString(parameters, "realm", 
91                                                  PyString_FromString(settings->realm));
92         if (settings->root_dn_str)
93                 PyDict_SetItemString(parameters, "rootdn", 
94                                      PyString_FromString(settings->root_dn_str));
95
96         if (settings->domain_dn_str) 
97                 PyDict_SetItemString(parameters, "domaindn", 
98                                      PyString_FromString(settings->domain_dn_str));
99
100         if (settings->schema_dn_str) 
101                 PyDict_SetItemString(parameters, "schemadn", 
102                                      PyString_FromString(settings->schema_dn_str));
103         
104         if (settings->config_dn_str) 
105                 PyDict_SetItemString(parameters, "configdn", 
106                                      PyString_FromString(settings->config_dn_str));
107         
108         if (settings->site_name) 
109                 PyDict_SetItemString(parameters, "sitename", 
110                                      PyString_FromString(settings->site_name));
111
112         PyDict_SetItemString(parameters, "machinepass", 
113                              PyString_FromString(settings->machine_password));
114
115         result = PyEval_CallObjectWithKeywords(provision_fn, NULL, parameters);
116
117         Py_DECREF(parameters);
118
119         if (result == NULL) {
120                 PyErr_Print();
121                 PyErr_Clear();
122                 return NT_STATUS_UNSUCCESSFUL;
123         }
124
125         return NT_STATUS_OK;
126 }