Fix more introduced regressions in new bindings.
[ira/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 #include "libcli/security/security.h"
33 #include "auth/pyauth.h"
34
35 /* FIXME: These should be in a header file somewhere, once we finish moving
36  * away from SWIG .. */
37 extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
38 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
39
40 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
41         if (!PyLdb_Check(py_ldb)) { \
42                 /*PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
43                 return NULL; \ */ \
44         } \
45         ldb = PyLdb_AsLdbContext(py_ldb);
46
47
48 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
49 {
50         int len;
51         PyObject *ret;
52         char *retstr;
53         if (!PyArg_ParseTuple(args, "i", &len))
54                 return NULL;
55
56         retstr = generate_random_str(NULL, len);
57         ret = PyString_FromString(retstr);
58         talloc_free(retstr);
59         return ret;
60 }
61
62 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
63 {
64         time_t t;
65         NTTIME nt;
66         if (!PyArg_ParseTuple(args, "I", &t))
67                 return NULL;
68
69         unix_to_nt_time(&nt, t);
70
71         return PyInt_FromLong((uint64_t)nt);
72 }
73
74 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
75 {
76         PyObject *py_creds, *py_ldb;
77         struct cli_credentials *creds;
78         struct ldb_context *ldb;
79         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
80                 return NULL;
81
82         PyErr_LDB_OR_RAISE(py_ldb, ldb);
83         
84         creds = cli_credentials_from_py_object(py_creds);
85         if (creds == NULL) {
86                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
87                 return NULL;
88         }
89
90         ldb_set_opaque(ldb, "credentials", creds);
91
92         return Py_None;
93 }
94
95 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
96 {
97         PyObject *py_lp_ctx, *py_ldb;
98         struct loadparm_context *lp_ctx;
99         struct ldb_context *ldb;
100         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
101                 return NULL;
102
103         PyErr_LDB_OR_RAISE(py_ldb, ldb);
104
105         lp_ctx = lp_from_py_object(py_lp_ctx);
106         if (lp_ctx == NULL) {
107                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
108                 return NULL;
109         }
110
111         ldb_set_opaque(ldb, "loadparm", lp_ctx);
112
113         return Py_None;
114 }
115
116
117 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
118 {
119         PyObject *py_session_info, *py_ldb;
120         struct auth_session_info *info;
121         struct ldb_context *ldb;
122         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
123                 return NULL;
124
125         PyErr_LDB_OR_RAISE(py_ldb, ldb);
126         /*if (!PyAuthSession_Check(py_session_info)) {
127                 PyErr_SetString(PyExc_TypeError, "Expected session info object");
128                 return NULL;
129         }*/
130
131         info = PyAuthSession_AsSession(py_session_info);
132
133         ldb_set_opaque(ldb, "sessionInfo", info);
134
135         return Py_None;
136 }
137
138 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
139
140         PyObject *py_ldb, *py_sid;
141         struct ldb_context *ldb;
142         struct dom_sid *sid;
143         bool ret;
144
145         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
146                 return NULL;
147         
148         PyErr_LDB_OR_RAISE(py_ldb, ldb);
149
150         sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
151
152         ret = samdb_set_domain_sid(ldb, sid);
153         if (!ret) {
154                 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
155                 return NULL;
156         } 
157         return Py_None;
158 }
159
160 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
161 {
162         PyObject *py_ldb;
163         struct ldb_context *ldb;
164         int ret;
165
166         if (!PyArg_ParseTuple(args, "O", &py_ldb))
167                 return NULL;
168
169         PyErr_LDB_OR_RAISE(py_ldb, ldb);
170         ret = ldb_register_samba_handlers(ldb);
171
172         PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
173         return Py_None;
174 }
175
176 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
177 {
178         PyObject *py_ldb, *py_guid;
179         bool ret;
180         struct GUID guid;
181         struct ldb_context *ldb;
182         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
183                 return NULL;
184
185         PyErr_LDB_OR_RAISE(py_ldb, ldb);
186         GUID_from_string(PyString_AsString(py_guid), &guid);
187
188         ret = samdb_set_ntds_invocation_id(ldb, &guid);
189         if (!ret) {
190                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
191                 return NULL;
192         }
193         return Py_None;
194 }
195
196 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
197 {
198         PyObject *py_ldb;
199         struct ldb_context *ldb;
200         int ret;
201         if (!PyArg_ParseTuple(args, "O", &py_ldb))
202                 return NULL;
203
204         PyErr_LDB_OR_RAISE(py_ldb, ldb);
205
206         ret = dsdb_set_global_schema(ldb);
207         PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
208
209         return Py_None;
210 }
211
212 static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args)
213 {
214         WERROR result;
215         char *pf, *df;
216         PyObject *py_ldb;
217         struct ldb_context *ldb;
218
219         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
220                 return NULL;
221
222         PyErr_LDB_OR_RAISE(py_ldb, ldb);
223
224         result = dsdb_attach_schema_from_ldif_file(ldb, pf, df);
225         PyErr_WERROR_IS_ERR_RAISE(result);
226
227         return Py_None;
228 }
229
230 static PyMethodDef py_misc_methods[] = {
231         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
232                 "random_password(len) -> string\n"
233                 "Generate random password with specified length." },
234         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
235                 "unix2nttime(timestamp) -> nttime" },
236         { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, 
237                 "ldb_set_credentials(ldb, credentials) -> None\n"
238                 "Set credentials to use when connecting." },
239         { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
240                 "ldb_set_session_info(ldb, session_info)\n"
241                 "Set session info to use when connecting." },
242         { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
243                 "ldb_set_loadparm(ldb, session_info)\n"
244                 "Set loadparm context to use when connecting." },
245         { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
246                 "samdb_set_domain_sid(samdb, sid)\n"
247                 "Set SID of domain to use." },
248         { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
249                 "ldb_register_samba_handlers(ldb)\n"
250                 "Register Samba-specific LDB modules and schemas." },
251         { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
252                 NULL },
253         { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
254                 NULL },
255         { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS,
256                 NULL },
257         { NULL }
258 };
259
260 void initglue(void)
261 {
262         PyObject *m;
263
264         m = Py_InitModule3("glue", py_misc_methods, 
265                            "Python bindings for miscellaneous Samba functions.");
266         if (m == NULL)
267                 return;
268
269         PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
270 }
271