s4: Let the "setpassword" script finally use the "samdb_set_password" routine
[Samba/aatanasov.git] / source4 / scripting / python / pyglue.c
blob3e6233b4c42a683000cb3a813223ac46a88aaefc
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4 Copyright (C) Matthias Dieter Wallnöfer 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "ldb.h"
22 #include "ldb_errors.h"
23 #include "ldb_wrap.h"
24 #include "param/param.h"
25 #include "auth/credentials/credentials.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "lib/ldb-samba/ldif_handlers.h"
28 #include "librpc/ndr/libndr.h"
29 #include "version.h"
30 #include <Python.h>
31 #include "lib/ldb/pyldb.h"
32 #include "libcli/util/pyerrors.h"
33 #include "libcli/security/security.h"
34 #include "auth/pyauth.h"
35 #include "param/pyparam.h"
36 #include "auth/credentials/pycredentials.h"
38 #ifndef Py_RETURN_NONE
39 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
40 #endif
42 /* FIXME: These should be in a header file somewhere, once we finish moving
43 * away from SWIG .. */
44 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
45 /* if (!PyLdb_Check(py_ldb)) { \
46 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
47 return NULL; \
48 } */\
49 ldb = PyLdb_AsLdbContext(py_ldb);
51 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
53 if (ret == LDB_ERR_PYTHON_EXCEPTION)
54 return; /* Python exception should already be set, just keep that */
56 PyErr_SetObject(error,
57 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
58 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
61 static PyObject *py_ldb_get_exception(void)
63 PyObject *mod = PyImport_ImportModule("ldb");
64 if (mod == NULL)
65 return NULL;
67 return PyObject_GetAttrString(mod, "LdbError");
70 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
72 int len;
73 PyObject *ret;
74 char *retstr;
75 if (!PyArg_ParseTuple(args, "i", &len))
76 return NULL;
78 retstr = generate_random_str(NULL, len);
79 ret = PyString_FromString(retstr);
80 talloc_free(retstr);
81 return ret;
84 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
86 time_t t;
87 NTTIME nt;
88 if (!PyArg_ParseTuple(args, "I", &t))
89 return NULL;
91 unix_to_nt_time(&nt, t);
93 return PyInt_FromLong((uint64_t)nt);
96 static PyObject *py_set_debug_level(PyObject *self, PyObject *args)
98 unsigned level;
99 if (!PyArg_ParseTuple(args, "I", &level))
100 return NULL;
101 (DEBUGLEVEL) = level;
102 Py_RETURN_NONE;
105 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
107 PyObject *py_creds, *py_ldb;
108 struct cli_credentials *creds;
109 struct ldb_context *ldb;
110 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds))
111 return NULL;
113 PyErr_LDB_OR_RAISE(py_ldb, ldb);
115 creds = cli_credentials_from_py_object(py_creds);
116 if (creds == NULL) {
117 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
118 return NULL;
121 ldb_set_opaque(ldb, "credentials", creds);
123 Py_RETURN_NONE;
126 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
128 PyObject *py_lp_ctx, *py_ldb;
129 struct loadparm_context *lp_ctx;
130 struct ldb_context *ldb;
131 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx))
132 return NULL;
134 PyErr_LDB_OR_RAISE(py_ldb, ldb);
136 lp_ctx = lp_from_py_object(py_lp_ctx);
137 if (lp_ctx == NULL) {
138 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
139 return NULL;
142 ldb_set_opaque(ldb, "loadparm", lp_ctx);
144 Py_RETURN_NONE;
148 static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
150 PyObject *py_session_info, *py_ldb;
151 struct auth_session_info *info;
152 struct ldb_context *ldb;
153 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info))
154 return NULL;
156 PyErr_LDB_OR_RAISE(py_ldb, ldb);
157 /*if (!PyAuthSession_Check(py_session_info)) {
158 PyErr_SetString(PyExc_TypeError, "Expected session info object");
159 return NULL;
162 info = PyAuthSession_AsSession(py_session_info);
164 ldb_set_opaque(ldb, "sessionInfo", info);
166 Py_RETURN_NONE;
169 static PyObject *py_ldb_set_utf8_casefold(PyObject *self, PyObject *args)
171 PyObject *py_ldb;
172 struct ldb_context *ldb;
174 if (!PyArg_ParseTuple(args, "O", &py_ldb))
175 return NULL;
177 PyErr_LDB_OR_RAISE(py_ldb, ldb);
179 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
181 Py_RETURN_NONE;
184 static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args)
186 PyObject *py_ldb, *py_sid;
187 struct ldb_context *ldb;
188 struct dom_sid *sid;
189 bool ret;
191 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid))
192 return NULL;
194 PyErr_LDB_OR_RAISE(py_ldb, ldb);
196 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
198 ret = samdb_set_domain_sid(ldb, sid);
199 if (!ret) {
200 PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed");
201 return NULL;
203 Py_RETURN_NONE;
206 static PyObject *py_samdb_get_domain_sid(PyLdbObject *self, PyObject *args)
208 PyObject *py_ldb;
209 struct ldb_context *ldb;
210 const struct dom_sid *sid;
211 PyObject *ret;
212 char *retstr;
214 if (!PyArg_ParseTuple(args, "O", &py_ldb))
215 return NULL;
217 PyErr_LDB_OR_RAISE(py_ldb, ldb);
219 sid = samdb_domain_sid(ldb);
220 if (!sid) {
221 PyErr_SetString(PyExc_RuntimeError, "samdb_domain_sid failed");
222 return NULL;
225 retstr = dom_sid_string(NULL, sid);
226 ret = PyString_FromString(retstr);
227 talloc_free(retstr);
229 return ret;
232 static PyObject *py_samdb_set_password(PyLdbObject *self, PyObject *args,
233 PyObject *kwargs)
235 PyObject *py_sam, *py_user_dn, *py_dom_dn, *py_mod, *py_user_change;
236 char *new_password;
237 bool user_change;
238 DATA_BLOB new_pwd_blob;
239 struct ldb_context *sam_ctx;
240 struct ldb_dn *user_dn, *dom_dn;
241 struct ldb_message *mod;
242 TALLOC_CTX *mem_ctx;
243 NTSTATUS status;
244 const char * const kwnames[] = { "samdb", "user_dn", "dom_dn", "mod",
245 "new_password", "user_change", NULL };
247 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOOsO",
248 discard_const_p(char *, kwnames),
249 &py_sam, &py_user_dn, &py_dom_dn, &py_mod, &new_password,
250 &py_user_change))
251 return NULL;
253 sam_ctx = PyLdb_AsLdbContext(py_sam);
255 mem_ctx = talloc_new(NULL);
256 if (mem_ctx == NULL) {
257 PyErr_NoMemory();
260 if (!PyObject_AsDn(mem_ctx, py_user_dn, sam_ctx, &user_dn)) {
261 PyErr_SetString(PyExc_RuntimeError, "user_dn invalid!");
262 return NULL;
265 if (!PyObject_AsDn(mem_ctx, py_dom_dn, sam_ctx, &dom_dn)) {
266 PyErr_SetString(PyExc_RuntimeError, "dom_dn invalid!");
267 return NULL;
270 mod = PyLdbMessage_AsMessage(py_mod);
272 user_change = PyInt_AsLong(py_user_change);
274 new_pwd_blob.data = (uint8_t *) new_password;
275 new_pwd_blob.length = strlen((char *) new_pwd_blob.data);
277 status = samdb_set_password(sam_ctx, mem_ctx, user_dn, dom_dn, mod,
278 &new_pwd_blob, NULL, NULL, user_change, NULL, NULL);
280 talloc_free(mem_ctx);
282 PyErr_NTSTATUS_IS_ERR_RAISE(status);
283 Py_RETURN_NONE;
286 static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args)
288 PyObject *py_ldb;
289 struct ldb_context *ldb;
290 int ret;
292 if (!PyArg_ParseTuple(args, "O", &py_ldb))
293 return NULL;
295 PyErr_LDB_OR_RAISE(py_ldb, ldb);
296 ret = ldb_register_samba_handlers(ldb);
298 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
299 Py_RETURN_NONE;
302 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
304 PyObject *py_ldb, *py_guid;
305 bool ret;
306 struct GUID guid;
307 struct ldb_context *ldb;
308 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid))
309 return NULL;
311 PyErr_LDB_OR_RAISE(py_ldb, ldb);
312 GUID_from_string(PyString_AsString(py_guid), &guid);
314 ret = samdb_set_ntds_invocation_id(ldb, &guid);
315 if (!ret) {
316 PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed");
317 return NULL;
319 Py_RETURN_NONE;
322 static PyObject *py_dsdb_set_opaque_integer(PyObject *self, PyObject *args)
324 PyObject *py_ldb;
325 int value;
326 int *old_val, *new_val;
327 char *py_opaque_name, *opaque_name_talloc;
328 struct ldb_context *ldb;
329 TALLOC_CTX *tmp_ctx;
331 if (!PyArg_ParseTuple(args, "Osi", &py_ldb, &py_opaque_name, &value))
332 return NULL;
334 PyErr_LDB_OR_RAISE(py_ldb, ldb);
336 /* see if we have a cached copy */
337 old_val = (int *)ldb_get_opaque(ldb,
338 py_opaque_name);
340 if (old_val) {
341 *old_val = value;
342 Py_RETURN_NONE;
345 tmp_ctx = talloc_new(ldb);
346 if (tmp_ctx == NULL) {
347 goto failed;
350 new_val = talloc(tmp_ctx, int);
351 if (!new_val) {
352 goto failed;
355 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
356 if (!opaque_name_talloc) {
357 goto failed;
360 *new_val = value;
362 /* cache the domain_sid in the ldb */
363 if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
364 goto failed;
367 talloc_steal(ldb, new_val);
368 talloc_steal(ldb, opaque_name_talloc);
369 talloc_free(tmp_ctx);
371 Py_RETURN_NONE;
373 failed:
374 talloc_free(tmp_ctx);
375 PyErr_SetString(PyExc_RuntimeError, "Failed to set opaque integer into the ldb!\n");
376 return NULL;
379 static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args)
381 PyObject *py_ldb;
382 struct ldb_context *ldb;
383 int ret;
384 if (!PyArg_ParseTuple(args, "O", &py_ldb))
385 return NULL;
387 PyErr_LDB_OR_RAISE(py_ldb, ldb);
389 ret = dsdb_set_global_schema(ldb);
390 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
392 Py_RETURN_NONE;
395 static PyObject *py_dsdb_set_schema_from_ldif(PyObject *self, PyObject *args)
397 WERROR result;
398 char *pf, *df;
399 PyObject *py_ldb;
400 struct ldb_context *ldb;
402 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df))
403 return NULL;
405 PyErr_LDB_OR_RAISE(py_ldb, ldb);
407 result = dsdb_set_schema_from_ldif(ldb, pf, df);
408 PyErr_WERROR_IS_ERR_RAISE(result);
410 Py_RETURN_NONE;
413 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self, PyObject *args)
415 char *target_str, *mapping;
416 PyObject *py_ldb;
417 struct ldb_context *ldb;
418 PyObject *ret;
419 char *retstr;
421 if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
422 return NULL;
424 PyErr_LDB_OR_RAISE(py_ldb, ldb);
426 retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
427 if (!retstr) {
428 PyErr_SetString(PyExc_RuntimeError, "dsdb_convert_schema_to_openldap failed");
429 return NULL;
431 ret = PyString_FromString(retstr);
432 talloc_free(retstr);
433 return ret;
436 static PyObject *py_dsdb_write_prefixes_from_schema_to_ldb(PyObject *self, PyObject *args)
438 PyObject *py_ldb;
439 struct ldb_context *ldb;
440 WERROR result;
441 struct dsdb_schema *schema;
443 if (!PyArg_ParseTuple(args, "O", &py_ldb))
444 return NULL;
446 PyErr_LDB_OR_RAISE(py_ldb, ldb);
448 schema = dsdb_get_schema(ldb);
449 if (!schema) {
450 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on ldb!\n");
451 return NULL;
454 result = dsdb_write_prefixes_from_schema_to_ldb(NULL, ldb, schema);
455 PyErr_WERROR_IS_ERR_RAISE(result);
457 Py_RETURN_NONE;
460 static PyObject *py_dsdb_set_schema_from_ldb(PyObject *self, PyObject *args)
462 PyObject *py_ldb;
463 struct ldb_context *ldb;
464 PyObject *py_from_ldb;
465 struct ldb_context *from_ldb;
466 struct dsdb_schema *schema;
467 int ret;
468 if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_from_ldb))
469 return NULL;
471 PyErr_LDB_OR_RAISE(py_ldb, ldb);
473 PyErr_LDB_OR_RAISE(py_from_ldb, from_ldb);
475 schema = dsdb_get_schema(from_ldb);
476 if (!schema) {
477 PyErr_SetString(PyExc_RuntimeError, "Failed to set find a schema on 'from' ldb!\n");
478 return NULL;
481 ret = dsdb_reference_schema(ldb, schema, true);
482 PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_get_exception(), ret, ldb);
484 Py_RETURN_NONE;
487 static PyObject *py_dom_sid_to_rid(PyLdbObject *self, PyObject *args)
489 PyObject *py_sid;
490 struct dom_sid *sid;
491 uint32_t rid;
492 NTSTATUS status;
494 if(!PyArg_ParseTuple(args, "O", &py_sid))
495 return NULL;
497 sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
499 status = dom_sid_split_rid(NULL, (const struct dom_sid *)sid, NULL,
500 &rid);
501 if (!NT_STATUS_IS_OK(status)) {
502 PyErr_SetString(PyExc_RuntimeError, "dom_sid_split_rid failed");
503 return NULL;
506 return PyInt_FromLong(rid);
509 static PyMethodDef py_misc_methods[] = {
510 { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
511 "random_password(len) -> string\n"
512 "Generate random password with specified length." },
513 { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
514 "unix2nttime(timestamp) -> nttime" },
515 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
516 "ldb_set_credentials(ldb, credentials) -> None\n"
517 "Set credentials to use when connecting." },
518 { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
519 "ldb_set_session_info(ldb, session_info)\n"
520 "Set session info to use when connecting." },
521 { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
522 "ldb_set_loadparm(ldb, session_info)\n"
523 "Set loadparm context to use when connecting." },
524 { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS,
525 "samdb_set_domain_sid(samdb, sid)\n"
526 "Set SID of domain to use." },
527 { "samdb_get_domain_sid", (PyCFunction)py_samdb_get_domain_sid, METH_VARARGS,
528 "samdb_get_domain_sid(samdb)\n"
529 "Get SID of domain in use." },
530 { "samdb_set_password", (PyCFunction)py_samdb_set_password,
531 METH_VARARGS|METH_KEYWORDS,
532 "samdb_set_password(samdb, user_dn, dom_dn, mod, new_password, user_change)\n"
533 "Set the password of a user" },
534 { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS,
535 "ldb_register_samba_handlers(ldb)\n"
536 "Register Samba-specific LDB modules and schemas." },
537 { "ldb_set_utf8_casefold", (PyCFunction)py_ldb_set_utf8_casefold, METH_VARARGS,
538 "ldb_set_utf8_casefold(ldb)\n"
539 "Set the right Samba casefolding function for UTF8 charset." },
540 { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
541 NULL },
542 { "dsdb_set_opaque_integer", (PyCFunction)py_dsdb_set_opaque_integer, METH_VARARGS,
543 NULL },
544 { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS,
545 NULL },
546 { "dsdb_set_schema_from_ldif", (PyCFunction)py_dsdb_set_schema_from_ldif, METH_VARARGS,
547 NULL },
548 { "dsdb_write_prefixes_from_schema_to_ldb", (PyCFunction)py_dsdb_write_prefixes_from_schema_to_ldb, METH_VARARGS,
549 NULL },
550 { "dsdb_set_schema_from_ldb", (PyCFunction)py_dsdb_set_schema_from_ldb, METH_VARARGS,
551 NULL },
552 { "dsdb_convert_schema_to_openldap", (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS,
553 NULL },
554 { "dom_sid_to_rid", (PyCFunction)py_dom_sid_to_rid, METH_VARARGS,
555 NULL },
556 { "set_debug_level", (PyCFunction)py_set_debug_level, METH_VARARGS,
557 "set debug level" },
558 { NULL }
561 void initglue(void)
563 PyObject *m;
565 m = Py_InitModule3("glue", py_misc_methods,
566 "Python bindings for miscellaneous Samba functions.");
567 if (m == NULL)
568 return;
570 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
572 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2000", PyInt_FromLong(DS_BEHAVIOR_WIN2000));
573 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2003_INTERIM", PyInt_FromLong(DS_BEHAVIOR_WIN2003_INTERIM));
574 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2003", PyInt_FromLong(DS_BEHAVIOR_WIN2003));
575 PyModule_AddObject(m, "DS_BEHAVIOR_WIN2008", PyInt_FromLong(DS_BEHAVIOR_WIN2008));