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