py: Fix initialisation of subtypes, fix segfaults.
[metze/samba/wip.git] / source4 / scripting / python / pyglue.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "includes.h"
20 #include "ldb.h"
21 #include "param/param.h"
22 #include "auth/credentials/credentials.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb-samba/ldif_handlers.h"
25 #include "librpc/ndr/libndr.h"
26 #include "version.h"
27 #include <Python.h>
28 #include "pyldb.h"
29 #include "libcli/util/pyerrors.h"
30 #include "librpc/gen_ndr/py_misc.h"
31 #include "librpc/gen_ndr/py_security.h"
32
33 /* FIXME: These should be in a header file somewhere, once we finish moving
34  * away from SWIG .. */
35 extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
36 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
37
38 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
39         if (!PyLdb_Check(py_ldb)) { \
40                 /*PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
41                 return NULL; \ */ \
42         } \
43         ldb = PyLdb_AsLdbContext(py_ldb);
44
45
46 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
47 {
48         int len;
49         PyObject *ret;
50         char *retstr;
51         if (!PyArg_ParseTuple(args, "i", &len))
52                 return NULL;
53
54         retstr = generate_random_str(NULL, len);
55         ret = PyString_FromString(retstr);
56         talloc_free(retstr);
57         return ret;
58 }
59
60 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
61 {
62         time_t t;
63         NTTIME nt;
64         if (!PyArg_ParseTuple(args, "I", &t))
65                 return NULL;
66
67         unix_to_nt_time(&nt, t);
68
69         return PyInt_FromLong((uint64_t)nt);
70 }
71
72 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
73 {
74         PyObject *py_creds, *py_ldb;
75         struct cli_credentials *creds;
76         struct ldb_context *ldb;
77         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
78                 return NULL;
79
80         PyErr_LDB_OR_RAISE(py_ldb, ldb);
81         
82         creds = cli_credentials_from_py_object(py_creds);
83         if (creds == NULL) {
84                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
85                 return NULL;
86         }
87
88         ldb_set_opaque(ldb, "credentials", creds);
89
90         return Py_None;
91 }
92
93 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
94 {
95         PyObject *py_lp_ctx, *py_ldb;
96         struct loadparm_context *lp_ctx;
97         struct ldb_context *ldb;
98         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
99                 return NULL;
100
101         PyErr_LDB_OR_RAISE(py_ldb, ldb);
102
103         lp_ctx = lp_from_py_object(py_lp_ctx);
104         if (lp_ctx == NULL) {
105                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
106                 return NULL;
107         }
108
109         ldb_set_opaque(ldb, "loadparm", lp_ctx);
110
111         return Py_None;
112 }
113
114
115 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
116 {
117         PyObject *py_session_info, *py_ldb;
118         struct auth_session_info *info;
119         struct ldb_context *ldb;
120         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
121                 return NULL;
122
123         PyErr_LDB_OR_RAISE(py_ldb, ldb);
124         /* FIXME: Magic py_session_info -> info */
125
126         ldb_set_opaque(ldb, "sessionInfo", info);
127
128         return Py_None;
129 }
130
131 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
132
133         PyObject *py_ldb, *py_sid;
134         struct ldb_context *ldb;
135         struct dom_sid *sid;
136         bool ret;
137
138         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
139                 return NULL;
140         
141         PyErr_LDB_OR_RAISE(py_ldb, ldb);
142
143         if (!dom_sid_Check(py_sid)) {
144                 PyErr_SetString(PyExc_TypeError, "expected SID");
145                 return NULL;
146         }
147
148         sid = py_talloc_get_ptr(py_sid);
149
150         ret = samdb_set_domain_sid(ldb, sid);
151         if (!ret) {
152                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
153                 return NULL;
154         } 
155         return Py_None;
156 }
157
158 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
159 {
160         PyObject *py_ldb;
161         struct ldb_context *ldb;
162         int ret;
163
164         if (!PyArg_ParseTuple(args, "O", &py_ldb))
165                 return NULL;
166
167         PyErr_LDB_OR_RAISE(py_ldb, ldb);
168         ret = ldb_register_samba_handlers(ldb);
169
170         PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
171         return Py_None;
172 }
173
174 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
175 {
176         PyObject *py_ldb, *py_guid;
177         bool ret;
178         struct GUID *guid;
179         struct ldb_context *ldb;
180         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
181                 return NULL;
182
183         PyErr_LDB_OR_RAISE(py_ldb, ldb);
184         if (!GUID_Check(py_guid)) {
185                 PyErr_SetString(PyExc_TypeError, "Expected GUID");
186                 return NULL;
187         }
188         guid = py_talloc_get_ptr(py_guid);
189
190         ret = samdb_set_ntds_invocation_id(ldb, guid);
191         if (!ret) {
192                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
193                 return NULL;
194         }
195         return Py_None;
196 }
197
198 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
199 {
200         PyObject *py_ldb;
201         struct ldb_context *ldb;
202         int ret;
203         if (!PyArg_ParseTuple(args, "O", &py_ldb))
204                 return NULL;
205
206         PyErr_LDB_OR_RAISE(py_ldb, ldb);
207
208         ret = dsdb_set_global_schema(ldb);
209         PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
210
211         return Py_None;
212 }
213
214 static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args)
215 {
216         WERROR result;
217         char *pf, *df;
218         PyObject *py_ldb;
219         struct ldb_context *ldb;
220
221         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
222                 return NULL;
223
224         PyErr_LDB_OR_RAISE(py_ldb, ldb);
225
226         result = dsdb_attach_schema_from_ldif_file(ldb, pf, df);
227         PyErr_WERROR_IS_ERR_RAISE(result);
228
229         return Py_None;
230 }
231
232 static PyMethodDef py_misc_methods[] = {
233         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
234                 "random_password(len) -> string\n"
235                 "Generate random password with specified length." },
236         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
237                 "unix2nttime(timestamp) -> nttime" },
238         { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, 
239                 "ldb_set_credentials(ldb, credentials) -> None\n"
240                 "Set credentials to use when connecting." },
241         { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
242                 "ldb_set_session_info(ldb, session_info)\n"
243                 "Set session info to use when connecting." },
244         { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
245                 "ldb_set_loadparm(ldb, session_info)\n"
246                 "Set loadparm context to use when connecting." },
247         { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
248                 "samdb_set_domain_sid(samdb, sid)\n"
249                 "Set SID of domain to use." },
250         { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
251                 "ldb_register_samba_handlers(ldb)\n"
252                 "Register Samba-specific LDB modules and schemas." },
253         { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
254                 NULL },
255         { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
256                 NULL },
257         { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS,
258                 NULL },
259         { NULL }
260 };
261
262 void initglue(void)
263 {
264         PyObject *m;
265
266         m = Py_InitModule3("glue", py_misc_methods, 
267                            "Python bindings for miscellaneous Samba functions.");
268         if (m == NULL)
269                 return;
270
271         PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
272 }
273