Add header for pyparam.
[idra/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 "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 "libcli/security/security.h"
31 #include "auth/pyauth.h"
32 #include "param/pyparam.h"
33
34 /* FIXME: These should be in a header file somewhere, once we finish moving
35  * away from SWIG .. */
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         /*if (!PyAuthSession_Check(py_session_info)) {
125                 PyErr_SetString(PyExc_TypeError, "Expected session info object");
126                 return NULL;
127         }*/
128
129         info = PyAuthSession_AsSession(py_session_info);
130
131         ldb_set_opaque(ldb, "sessionInfo", info);
132
133         return Py_None;
134 }
135
136 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
137
138         PyObject *py_ldb, *py_sid;
139         struct ldb_context *ldb;
140         struct dom_sid *sid;
141         bool ret;
142
143         if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
144                 return NULL;
145         
146         PyErr_LDB_OR_RAISE(py_ldb, ldb);
147
148         sid = dom_sid_parse_talloc(NULL, PyString_AsString(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         GUID_from_string(PyString_AsString(py_guid), &guid);
185
186         ret = samdb_set_ntds_invocation_id(ldb, &guid);
187         if (!ret) {
188                 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
189                 return NULL;
190         }
191         return Py_None;
192 }
193
194 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
195 {
196         PyObject *py_ldb;
197         struct ldb_context *ldb;
198         int ret;
199         if (!PyArg_ParseTuple(args, "O", &py_ldb))
200                 return NULL;
201
202         PyErr_LDB_OR_RAISE(py_ldb, ldb);
203
204         ret = dsdb_set_global_schema(ldb);
205         PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
206
207         return Py_None;
208 }
209
210 static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args)
211 {
212         WERROR result;
213         char *pf, *df;
214         PyObject *py_ldb;
215         struct ldb_context *ldb;
216
217         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
218                 return NULL;
219
220         PyErr_LDB_OR_RAISE(py_ldb, ldb);
221
222         result = dsdb_attach_schema_from_ldif_file(ldb, pf, df);
223         PyErr_WERROR_IS_ERR_RAISE(result);
224
225         return Py_None;
226 }
227
228 static PyMethodDef py_misc_methods[] = {
229         { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
230                 "random_password(len) -> string\n"
231                 "Generate random password with specified length." },
232         { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
233                 "unix2nttime(timestamp) -> nttime" },
234         { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, 
235                 "ldb_set_credentials(ldb, credentials) -> None\n"
236                 "Set credentials to use when connecting." },
237         { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
238                 "ldb_set_session_info(ldb, session_info)\n"
239                 "Set session info to use when connecting." },
240         { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
241                 "ldb_set_loadparm(ldb, session_info)\n"
242                 "Set loadparm context to use when connecting." },
243         { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
244                 "samdb_set_domain_sid(samdb, sid)\n"
245                 "Set SID of domain to use." },
246         { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
247                 "ldb_register_samba_handlers(ldb)\n"
248                 "Register Samba-specific LDB modules and schemas." },
249         { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
250                 NULL },
251         { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
252                 NULL },
253         { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS,
254                 NULL },
255         { NULL }
256 };
257
258 void initglue(void)
259 {
260         PyObject *m;
261
262         m = Py_InitModule3("glue", py_misc_methods, 
263                            "Python bindings for miscellaneous Samba functions.");
264         if (m == NULL)
265                 return;
266
267         PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
268 }
269