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/>.
22 #include "auth/auth.h"
23 #include "lib/ldb_wrap.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"
33 #include "lib/talloc/pytalloc.h"
34 #include "librpc/rpc/pyrpc.h"
35 #include "scripting/python/modules.h"
36 #include "lib/ldb/pyldb.h"
37 #include "param/pyparam.h"
39 static PyObject
*provision_module(void)
41 PyObject
*name
= PyString_FromString("samba.provision");
44 return PyImport_Import(name
);
47 NTSTATUS
provision_bare(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
,
48 struct provision_settings
*settings
,
49 struct provision_result
*result
)
51 const char *configfile
;
52 PyObject
*provision_mod
, *provision_dict
, *provision_fn
, *py_result
, *parameters
;
54 DEBUG(0,("Provision for Become-DC test using python\n"));
56 py_load_samba_modules();
58 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
60 provision_mod
= provision_module();
62 if (provision_mod
== NULL
) {
64 DEBUG(0, ("Unable to import provision Python module.\n"));
65 return NT_STATUS_UNSUCCESSFUL
;
68 provision_dict
= PyModule_GetDict(provision_mod
);
70 if (provision_dict
== NULL
) {
71 DEBUG(0, ("Unable to get dictionary for provision module\n"));
72 return NT_STATUS_UNSUCCESSFUL
;
75 provision_fn
= PyDict_GetItemString(provision_dict
, "provision_become_dc");
76 if (provision_fn
== NULL
) {
78 DEBUG(0, ("Unable to get provision_become_dc function\n"));
79 return NT_STATUS_UNSUCCESSFUL
;
82 DEBUG(0,("New Server in Site[%s]\n",
83 settings
->site_name
));
85 DEBUG(0,("DSA Instance [%s]\n"
86 "\tinvocationId[%s]\n",
87 settings
->ntds_dn_str
,
88 settings
->invocation_id
== NULL
?"None":GUID_string(mem_ctx
, settings
->invocation_id
)));
90 DEBUG(0,("Pathes under targetdir[%s]\n",
91 settings
->targetdir
));
92 parameters
= PyDict_New();
94 configfile
= lp_configfile(lp_ctx
);
95 if (configfile
!= NULL
) {
96 PyDict_SetItemString(parameters
, "smbconf",
97 PyString_FromString(configfile
));
100 PyDict_SetItemString(parameters
, "rootdn",
101 PyString_FromString(settings
->root_dn_str
));
102 if (settings
->targetdir
!= NULL
)
103 PyDict_SetItemString(parameters
, "targetdir",
104 PyString_FromString(settings
->targetdir
));
105 PyDict_SetItemString(parameters
, "setup_dir",
106 PyString_FromString("setup"));
107 PyDict_SetItemString(parameters
, "hostname",
108 PyString_FromString(settings
->netbios_name
));
109 PyDict_SetItemString(parameters
, "domain",
110 PyString_FromString(settings
->domain
));
111 PyDict_SetItemString(parameters
, "realm",
112 PyString_FromString(settings
->realm
));
113 if (settings
->root_dn_str
)
114 PyDict_SetItemString(parameters
, "rootdn",
115 PyString_FromString(settings
->root_dn_str
));
117 if (settings
->domain_dn_str
)
118 PyDict_SetItemString(parameters
, "domaindn",
119 PyString_FromString(settings
->domain_dn_str
));
121 if (settings
->schema_dn_str
)
122 PyDict_SetItemString(parameters
, "schemadn",
123 PyString_FromString(settings
->schema_dn_str
));
125 if (settings
->config_dn_str
)
126 PyDict_SetItemString(parameters
, "configdn",
127 PyString_FromString(settings
->config_dn_str
));
129 if (settings
->server_dn_str
)
130 PyDict_SetItemString(parameters
, "serverdn",
131 PyString_FromString(settings
->server_dn_str
));
133 if (settings
->site_name
)
134 PyDict_SetItemString(parameters
, "sitename",
135 PyString_FromString(settings
->site_name
));
137 PyDict_SetItemString(parameters
, "machinepass",
138 PyString_FromString(settings
->machine_password
));
141 PyDict_SetItemString(parameters
, "debuglevel", PyInt_FromLong(DEBUGLEVEL
));
143 py_result
= PyEval_CallObjectWithKeywords(provision_fn
, NULL
, parameters
);
145 Py_DECREF(parameters
);
147 if (py_result
== NULL
) {
150 return NT_STATUS_UNSUCCESSFUL
;
153 result
->domaindn
= talloc_strdup(mem_ctx
, PyString_AsString(PyObject_GetAttrString(py_result
, "domaindn")));
156 result
->lp_ctx
= lp_from_py_object(PyObject_GetAttrString(py_result
, "lp"));
157 result
->samdb
= PyLdb_AsLdbContext(PyObject_GetAttrString(py_result
, "samdb"));
162 extern void initldb(void);
164 static PyObject
*py_dom_sid_FromSid(struct dom_sid
*sid
)
166 PyObject
*mod_security
, *dom_sid_Type
;
168 mod_security
= PyImport_ImportModule("samba.dcerpc.security");
169 if (mod_security
== NULL
)
172 dom_sid_Type
= PyObject_GetAttrString(mod_security
, "dom_sid");
173 if (dom_sid_Type
== NULL
)
176 return py_talloc_reference((PyTypeObject
*)dom_sid_Type
, sid
);
179 NTSTATUS
provision_store_self_join(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
,
180 struct tevent_context
*event_ctx
,
181 struct provision_store_self_join_settings
*settings
,
182 const char **error_string
)
185 PyObject
*provision_mod
, *provision_dict
, *provision_fn
, *py_result
, *parameters
, *py_sid
;
186 struct ldb_context
*ldb
;
187 TALLOC_CTX
*tmp_mem
= talloc_new(mem_ctx
);
189 return NT_STATUS_NO_MEMORY
;
192 /* Open the secrets database */
193 ldb
= secrets_db_connect(tmp_mem
, event_ctx
, lp_ctx
);
196 = talloc_asprintf(mem_ctx
,
197 "Could not open secrets database");
198 talloc_free(tmp_mem
);
199 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
202 ret
= ldb_transaction_start(ldb
);
204 if (ret
!= LDB_SUCCESS
) {
206 = talloc_asprintf(mem_ctx
,
207 "Could not start transaction on secrets database: %s", ldb_errstring(ldb
));
208 talloc_free(tmp_mem
);
209 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
212 py_load_samba_modules();
214 py_update_path("bin"); /* FIXME: Can't assume this is always the case */
216 provision_mod
= provision_module();
218 if (provision_mod
== NULL
) {
221 = talloc_asprintf(mem_ctx
, "Unable to import provision Python module.");
222 talloc_free(tmp_mem
);
223 return NT_STATUS_UNSUCCESSFUL
;
226 provision_dict
= PyModule_GetDict(provision_mod
);
228 if (provision_dict
== NULL
) {
230 = talloc_asprintf(mem_ctx
, "Unable to get dictionary for provision module");
231 talloc_free(tmp_mem
);
232 return NT_STATUS_UNSUCCESSFUL
;
235 provision_fn
= PyDict_GetItemString(provision_dict
, "secretsdb_self_join");
236 if (provision_fn
== NULL
) {
239 = talloc_asprintf(mem_ctx
, "Unable to get provision_become_dc function");
240 talloc_free(tmp_mem
);
241 return NT_STATUS_UNSUCCESSFUL
;
244 parameters
= PyDict_New();
246 PyDict_SetItemString(parameters
, "secretsdb",
247 PyLdb_FromLdbContext(ldb
));
248 PyDict_SetItemString(parameters
, "domain",
249 PyString_FromString(settings
->domain_name
));
250 PyDict_SetItemString(parameters
, "domain",
251 PyString_FromString(settings
->domain_name
));
252 PyDict_SetItemString(parameters
, "realm",
253 PyString_FromString(settings
->realm
));
254 PyDict_SetItemString(parameters
, "machinepass",
255 PyString_FromString(settings
->machine_password
));
256 PyDict_SetItemString(parameters
, "netbiosname",
257 PyString_FromString(settings
->netbios_name
));
259 py_sid
= py_dom_sid_FromSid(settings
->domain_sid
);
260 if (py_sid
== NULL
) {
261 Py_DECREF(parameters
);
265 PyDict_SetItemString(parameters
, "domainsid",
268 PyDict_SetItemString(parameters
, "secure_channel_type",
269 PyInt_FromLong(settings
->secure_channel_type
));
271 PyDict_SetItemString(parameters
, "key_version_number",
272 PyInt_FromLong(settings
->key_version_number
));
274 py_result
= PyEval_CallObjectWithKeywords(provision_fn
, NULL
, parameters
);
276 Py_DECREF(parameters
);
278 if (py_result
== NULL
) {
282 ret
= ldb_transaction_commit(ldb
);
283 if (ret
!= LDB_SUCCESS
) {
285 = talloc_asprintf(mem_ctx
,
286 "Could not commit transaction on secrets database: %s", ldb_errstring(ldb
));
287 talloc_free(tmp_mem
);
288 return NT_STATUS_INTERNAL_DB_ERROR
;
291 talloc_free(tmp_mem
);
296 ldb_transaction_cancel(ldb
);
297 talloc_free(tmp_mem
);
301 return NT_STATUS_UNSUCCESSFUL
;