tevent: expose tevent_context_init_ops
[Samba/gebeck_regimport.git] / source3 / passdb / py_passdb.c
blob194a7d47a8fdec043561f381d6b7876de7c44292
1 /*
2 Python interface to passdb
4 Copyright (C) Amitay Isaacs 2011
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 <Python.h>
21 #include <pytalloc.h>
22 #include "includes.h"
23 #include "lib/util/talloc_stack.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/idmap.h"
26 #include "passdb.h"
27 #include "secrets.h"
29 /* There's no Py_ssize_t in 2.4, apparently */
30 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
31 typedef int Py_ssize_t;
32 typedef inquiry lenfunc;
33 typedef intargfunc ssizeargfunc;
34 #endif
36 #ifndef Py_RETURN_NONE
37 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
38 #endif
40 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
41 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
42 #endif
44 #ifndef PY_CHECK_TYPE
45 #define PY_CHECK_TYPE(type, var, fail) \
46 if (!PyObject_TypeCheck(var, type)) {\
47 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
48 fail; \
50 #endif
53 static PyTypeObject *dom_sid_Type = NULL;
54 static PyTypeObject *security_Type = NULL;
55 static PyTypeObject *guid_Type = NULL;
57 staticforward PyTypeObject PySamu;
58 staticforward PyTypeObject PyGroupmap;
59 staticforward PyTypeObject PyPDB;
61 static PyObject *py_pdb_error;
63 void initpassdb(void);
66 /************************** PIDL Autogeneratd ******************************/
68 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
70 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
71 PyObject *py_logon_time;
73 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
74 return py_logon_time;
77 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
79 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
81 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
82 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
83 return -1;
85 return 0;
88 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
90 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
91 PyObject *py_logoff_time;
93 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
94 return py_logoff_time;
97 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
99 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
101 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
102 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
103 return -1;
105 return 0;
108 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
110 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
111 PyObject *py_kickoff_time;
113 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
114 return py_kickoff_time;
117 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
119 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
121 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
122 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
123 return -1;
125 return 0;
128 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
130 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
131 PyObject *py_bad_password_time;
133 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
134 return py_bad_password_time;
137 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
139 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
141 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
142 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
143 return -1;
145 return 0;
148 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
150 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
151 PyObject *py_pass_last_set_time;
153 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
154 return py_pass_last_set_time;
157 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
159 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
161 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
162 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
163 return -1;
165 return 0;
168 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
170 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
171 PyObject *py_pass_can_change_time;
173 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
174 return py_pass_can_change_time;
177 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
179 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
181 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
182 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
183 return -1;
185 return 0;
188 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
190 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
191 PyObject *py_pass_must_change_time;
193 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
194 return py_pass_must_change_time;
197 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
199 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
201 /* TODO: make this not a get/set or give a better exception */
202 return -1;
205 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
207 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
208 PyObject *py_username;
209 const char *username;
211 username = pdb_get_username(sam_acct);
212 if (username == NULL) {
213 Py_RETURN_NONE;
216 py_username = PyString_FromString(username);
217 return py_username;
220 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
222 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
224 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
225 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
226 return -1;
228 return 0;
231 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
233 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
234 PyObject *py_domain;
235 const char *domain;
237 domain = pdb_get_domain(sam_acct);
238 if (domain == NULL) {
239 Py_RETURN_NONE;
242 py_domain = PyString_FromString(domain);
243 return py_domain;
246 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
248 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
250 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
251 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
252 return -1;
254 return 0;
257 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
259 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
260 PyObject *py_nt_username;
261 const char *nt_username;
263 nt_username = pdb_get_nt_username(sam_acct);
264 if (nt_username == NULL) {
265 Py_RETURN_NONE;
268 py_nt_username = PyString_FromString(nt_username);
269 return py_nt_username;
272 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
274 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
276 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
277 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
278 return -1;
280 return 0;
283 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
285 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
286 PyObject *py_full_name;
287 const char *full_name;
289 full_name = pdb_get_fullname(sam_acct);
290 if (full_name == NULL) {
291 Py_RETURN_NONE;
294 py_full_name = PyString_FromString(full_name);
295 return py_full_name;
298 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
300 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
302 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
303 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
304 return -1;
306 return 0;
309 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
311 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
312 PyObject *py_home_dir;
313 const char *home_dir;
315 home_dir = pdb_get_homedir(sam_acct);
316 if (home_dir == NULL) {
317 Py_RETURN_NONE;
320 py_home_dir = PyString_FromString(home_dir);
321 return py_home_dir;
324 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
326 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
328 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
329 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
330 return -1;
332 return 0;
335 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
337 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
338 PyObject *py_dir_drive;
339 const char *dir_drive;
341 dir_drive = pdb_get_dir_drive(sam_acct);
342 if (dir_drive == NULL) {
343 Py_RETURN_NONE;
346 py_dir_drive = PyString_FromString(dir_drive);
347 return py_dir_drive;
350 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
352 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
354 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
355 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
356 return -1;
358 return 0;
361 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
363 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
364 PyObject *py_logon_script;
365 const char *logon_script;
367 logon_script = pdb_get_logon_script(sam_acct);
368 if (logon_script == NULL) {
369 Py_RETURN_NONE;
372 py_logon_script = PyString_FromString(logon_script);
373 return py_logon_script;
376 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
378 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
380 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
381 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
382 return -1;
384 return 0;
387 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
389 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
390 PyObject *py_profile_path;
391 const char *profile_path;
393 profile_path = pdb_get_profile_path(sam_acct);
394 if (profile_path == NULL) {
395 Py_RETURN_NONE;
398 py_profile_path = PyString_FromString(profile_path);
399 return py_profile_path;
402 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
404 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
406 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
407 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
408 return -1;
410 return 0;
413 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
415 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
416 PyObject *py_acct_desc;
417 const char *acct_desc;
419 acct_desc = pdb_get_acct_desc(sam_acct);
420 if (acct_desc == NULL) {
421 Py_RETURN_NONE;
424 py_acct_desc = PyString_FromString(acct_desc);
425 return py_acct_desc;
428 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
430 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
432 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
433 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
434 return -1;
436 return 0;
439 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
441 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
442 PyObject *py_workstations;
443 const char *workstations;
445 workstations = pdb_get_workstations(sam_acct);
446 if (workstations == NULL) {
447 Py_RETURN_NONE;
450 py_workstations = PyString_FromString(workstations);
451 return py_workstations;
454 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
456 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
458 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
459 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
460 return -1;
462 return 0;
465 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
467 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
468 PyObject *py_comment;
469 const char *comment;
471 comment = pdb_get_comment(sam_acct);
472 if (comment == NULL) {
473 Py_RETURN_NONE;
476 py_comment = PyString_FromString(comment);
477 return py_comment;
480 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
482 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
484 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
485 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
486 return -1;
488 return 0;
491 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
493 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
494 PyObject *py_munged_dial;
495 const char *munged_dial;
497 munged_dial = pdb_get_munged_dial(sam_acct);
498 if (munged_dial == NULL) {
499 Py_RETURN_NONE;
502 py_munged_dial = PyString_FromString(munged_dial);
503 return py_munged_dial;
506 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
508 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
510 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
511 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
512 return -1;
514 return 0;
517 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
519 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
520 PyObject *py_user_sid;
521 const struct dom_sid *user_sid;
522 struct dom_sid *copy_user_sid;
523 TALLOC_CTX *mem_ctx;
525 user_sid = pdb_get_user_sid(sam_acct);
526 if(user_sid == NULL) {
527 Py_RETURN_NONE;
530 mem_ctx = talloc_new(NULL);
531 if (mem_ctx == NULL) {
532 PyErr_NoMemory();
533 return NULL;
535 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
536 if (copy_user_sid == NULL) {
537 PyErr_NoMemory();
538 talloc_free(mem_ctx);
539 return NULL;
542 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
544 talloc_free(mem_ctx);
546 return py_user_sid;
549 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
551 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
553 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
554 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
555 return -1;
557 return 0;
560 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
562 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
563 PyObject *py_group_sid;
564 const struct dom_sid *group_sid;
565 struct dom_sid *copy_group_sid;
566 TALLOC_CTX *mem_ctx;
568 mem_ctx = talloc_stackframe();
569 if (mem_ctx == NULL) {
570 PyErr_NoMemory();
571 return NULL;
574 group_sid = pdb_get_group_sid(sam_acct);
575 if (group_sid == NULL) {
576 Py_RETURN_NONE;
579 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
580 if (copy_group_sid == NULL) {
581 PyErr_NoMemory();
582 talloc_free(mem_ctx);
583 return NULL;
586 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
588 talloc_free(mem_ctx);
590 return py_group_sid;
593 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
595 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
597 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
598 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
599 return -1;
601 return 0;
604 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
606 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
607 PyObject *py_lm_pw;
608 const char *lm_pw;
610 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
611 if (lm_pw == NULL) {
612 Py_RETURN_NONE;
615 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
616 return py_lm_pw;
619 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
621 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
623 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
624 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
625 return -1;
627 return 0;
630 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
632 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
633 PyObject *py_nt_pw;
634 const char *nt_pw;
636 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
637 if (nt_pw == NULL) {
638 Py_RETURN_NONE;
641 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
642 return py_nt_pw;
645 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
647 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
649 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
650 return -1;
652 return 0;
655 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
657 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
658 PyObject *py_nt_pw_his;
659 const char *nt_pw_his;
660 uint32_t hist_len;
662 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
663 if (nt_pw_his == NULL) {
664 Py_RETURN_NONE;
667 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
668 return py_nt_pw_his;
671 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
673 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
674 char *nt_pw_his;
675 Py_ssize_t len;
676 uint32_t hist_len;
678 PyString_AsStringAndSize(value, &nt_pw_his, &len);
679 hist_len = len / PW_HISTORY_ENTRY_LEN;
680 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
681 return -1;
683 return 0;
686 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
688 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
689 PyObject *py_plaintext_pw;
690 const char *plaintext_pw;
692 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
693 if (plaintext_pw == NULL) {
694 Py_RETURN_NONE;
697 py_plaintext_pw = PyString_FromString(plaintext_pw);
698 return py_plaintext_pw;
701 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
703 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
705 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
706 return -1;
708 return 0;
711 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
713 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
714 PyObject *py_acct_ctrl;
716 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
717 return py_acct_ctrl;
720 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
722 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
724 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
725 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
726 return -1;
728 return 0;
731 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
733 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
734 PyObject *py_logon_divs;
736 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
737 return py_logon_divs;
740 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
742 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
744 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
745 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
746 return -1;
748 return 0;
751 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
753 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
754 PyObject *py_hours_len;
756 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
757 return py_hours_len;
760 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
762 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
764 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
765 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
766 return -1;
768 return 0;
771 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
773 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
774 PyObject *py_hours;
775 const char *hours;
776 int hours_len, i;
778 hours = (const char *)pdb_get_hours(sam_acct);
779 if(! hours) {
780 Py_RETURN_NONE;
783 hours_len = pdb_get_hours_len(sam_acct);
784 if ((py_hours = PyList_New(hours_len)) == NULL) {
785 PyErr_NoMemory();
786 return NULL;
789 for (i=0; i<hours_len; i++) {
790 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
792 return py_hours;
795 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
797 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
798 int i;
799 uint8_t *hours;
800 int hours_len;
801 bool status;
803 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
805 hours_len = PyList_GET_SIZE(value);
807 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
808 if (!hours) {
809 PyErr_NoMemory();
810 return -1;
813 for (i=0; i < hours_len; i++) {
814 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
815 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
818 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
819 talloc_free(hours);
821 if(! status) {
822 return -1;
824 return 0;
827 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
829 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
830 PyObject *py_bad_password_count;
832 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
833 return py_bad_password_count;
836 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
838 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
840 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
841 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
842 return -1;
844 return 0;
847 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
849 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
850 PyObject *py_logon_count;
852 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
853 return py_logon_count;
856 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
858 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
860 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
861 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
862 return -1;
864 return 0;
867 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
869 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
870 PyObject *py_country_code;
872 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
873 return py_country_code;
876 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
878 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
880 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
881 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
882 return -1;
884 return 0;
887 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
889 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
890 PyObject *py_code_page;
892 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
893 return py_code_page;
896 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
898 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
900 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
901 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
902 return -1;
904 return 0;
907 static PyGetSetDef py_samu_getsetters[] = {
908 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
909 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
910 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
911 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
912 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
913 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
914 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
915 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
916 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
917 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
918 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
919 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
920 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
921 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
922 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
923 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
924 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
925 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
926 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
927 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
928 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
929 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
930 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
931 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
932 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
933 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
934 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
935 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
936 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
937 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
938 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
939 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
940 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
941 { NULL }
945 /************************** PIDL Autogeneratd ******************************/
947 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
949 struct samu *sam_acct;
951 sam_acct = samu_new(NULL);
952 if (!sam_acct) {
953 PyErr_NoMemory();
954 return NULL;
957 return pytalloc_steal(type, sam_acct);
960 static PyTypeObject PySamu = {
961 .tp_name = "passdb.Samu",
962 .tp_basicsize = sizeof(pytalloc_Object),
963 .tp_getset = py_samu_getsetters,
964 .tp_methods = NULL,
965 .tp_new = py_samu_new,
966 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
967 .tp_doc = "Samu() -> samu object\n",
971 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
973 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
974 PyObject *py_gid;
976 py_gid = Py_BuildValue("i", group_map->gid);
977 return py_gid;
980 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
982 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
984 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
985 group_map->gid = PyInt_AsLong(value);
986 return 0;
989 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
991 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
992 PyObject *py_sid;
993 struct dom_sid *group_sid;
994 TALLOC_CTX *mem_ctx;
996 mem_ctx = talloc_new(NULL);
997 if (mem_ctx == NULL) {
998 PyErr_NoMemory();
999 return NULL;
1002 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1003 if (group_sid == NULL) {
1004 PyErr_NoMemory();
1005 talloc_free(mem_ctx);
1006 return NULL;
1009 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1011 talloc_free(mem_ctx);
1013 return py_sid;
1016 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1018 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1020 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1021 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1022 return 0;
1025 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1027 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1028 PyObject *py_sid_name_use;
1030 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1031 return py_sid_name_use;
1034 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1036 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1038 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1039 group_map->sid_name_use = PyInt_AsLong(value);
1040 return 0;
1043 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1045 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1046 PyObject *py_nt_name;
1047 if (group_map->nt_name == NULL) {
1048 py_nt_name = Py_None;
1049 Py_INCREF(py_nt_name);
1050 } else {
1051 py_nt_name = PyString_FromString(group_map->nt_name);
1053 return py_nt_name;
1056 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1058 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1060 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1061 if (value == Py_None) {
1062 fstrcpy(group_map->nt_name, NULL);
1063 } else {
1064 fstrcpy(group_map->nt_name, PyString_AsString(value));
1066 return 0;
1069 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1071 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1072 PyObject *py_comment;
1073 if (group_map->comment == NULL) {
1074 py_comment = Py_None;
1075 Py_INCREF(py_comment);
1076 } else {
1077 py_comment = PyString_FromString(group_map->comment);
1079 return py_comment;
1082 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1084 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1086 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1087 if (value == Py_None) {
1088 fstrcpy(group_map->comment, NULL);
1089 } else {
1090 fstrcpy(group_map->comment, PyString_AsString(value));
1092 return 0;
1095 static PyGetSetDef py_groupmap_getsetters[] = {
1096 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1097 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1098 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1099 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1100 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1101 { NULL }
1104 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1106 GROUP_MAP *group_map;
1107 TALLOC_CTX *mem_ctx;
1108 PyObject *py_group_map;
1110 mem_ctx = talloc_new(NULL);
1111 if (mem_ctx == NULL) {
1112 PyErr_NoMemory();
1113 return NULL;
1116 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1117 if (group_map == NULL) {
1118 PyErr_NoMemory();
1119 talloc_free(mem_ctx);
1120 return NULL;
1123 py_group_map = pytalloc_steal(type, group_map);
1124 if (py_group_map == NULL) {
1125 PyErr_NoMemory();
1126 talloc_free(mem_ctx);
1127 return NULL;
1130 talloc_free(mem_ctx);
1132 return py_group_map;
1136 static PyTypeObject PyGroupmap = {
1137 .tp_name = "passdb.Groupmap",
1138 .tp_basicsize = sizeof(pytalloc_Object),
1139 .tp_getset = py_groupmap_getsetters,
1140 .tp_methods = NULL,
1141 .tp_new = py_groupmap_new,
1142 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1143 .tp_doc = "Groupmap() -> group map object\n",
1147 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1149 struct pdb_methods *methods;
1150 struct pdb_domain_info *domain_info;
1151 PyObject *py_domain_info;
1152 TALLOC_CTX *tframe;
1153 struct dom_sid *sid;
1154 struct GUID *guid;
1156 methods = pytalloc_get_ptr(self);
1158 if ((tframe = talloc_stackframe()) == NULL) {
1159 PyErr_NoMemory();
1160 return NULL;
1163 domain_info = methods->get_domain_info(methods, tframe);
1164 if (! domain_info) {
1165 Py_RETURN_NONE;
1168 sid = dom_sid_dup(tframe, &domain_info->sid);
1169 if (sid == NULL) {
1170 PyErr_NoMemory();
1171 talloc_free(tframe);
1172 return NULL;
1175 guid = talloc(tframe, struct GUID);
1176 if (guid == NULL) {
1177 PyErr_NoMemory();
1178 talloc_free(tframe);
1179 return NULL;
1181 *guid = domain_info->guid;
1183 if ((py_domain_info = PyDict_New()) == NULL) {
1184 PyErr_NoMemory();
1185 return NULL;
1188 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1189 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->name));
1190 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->name));
1191 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1192 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1194 talloc_free(tframe);
1196 return py_domain_info;
1200 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1202 NTSTATUS status;
1203 const char *username;
1204 struct pdb_methods *methods;
1205 struct samu *sam_acct;
1206 PyObject *py_sam_acct;
1207 TALLOC_CTX *tframe;
1209 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1210 return NULL;
1213 methods = pytalloc_get_ptr(self);
1215 if ((tframe = talloc_stackframe()) == NULL) {
1216 PyErr_NoMemory();
1217 return NULL;
1220 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1221 if (py_sam_acct == NULL) {
1222 PyErr_NoMemory();
1223 talloc_free(tframe);
1224 return NULL;
1226 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1228 status = methods->getsampwnam(methods, sam_acct, username);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1231 username,
1232 NT_STATUS_V(status),
1233 get_friendly_nt_error_msg(status));
1234 Py_DECREF(py_sam_acct);
1235 talloc_free(tframe);
1236 return NULL;
1239 talloc_free(tframe);
1240 return py_sam_acct;
1243 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1245 NTSTATUS status;
1246 struct pdb_methods *methods;
1247 struct samu *sam_acct;
1248 PyObject *py_sam_acct;
1249 TALLOC_CTX *tframe;
1250 PyObject *py_user_sid;
1252 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1253 return NULL;
1256 methods = pytalloc_get_ptr(self);
1258 if ((tframe = talloc_stackframe()) == NULL) {
1259 PyErr_NoMemory();
1260 return NULL;
1263 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1264 if (py_sam_acct == NULL) {
1265 PyErr_NoMemory();
1266 talloc_free(tframe);
1267 return NULL;
1269 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1271 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1272 if (!NT_STATUS_IS_OK(status)) {
1273 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1274 NT_STATUS_V(status),
1275 get_friendly_nt_error_msg(status));
1276 Py_DECREF(py_sam_acct);
1277 talloc_free(tframe);
1278 return NULL;
1281 talloc_free(tframe);
1282 return py_sam_acct;
1285 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1287 NTSTATUS status;
1288 struct pdb_methods *methods;
1289 const char *username;
1290 unsigned int acct_flags;
1291 unsigned int rid;
1292 TALLOC_CTX *tframe;
1294 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1295 return NULL;
1298 methods = pytalloc_get_ptr(self);
1300 if ((tframe = talloc_stackframe()) == NULL) {
1301 PyErr_NoMemory();
1302 return NULL;
1305 status = methods->create_user(methods, tframe, username, acct_flags, &rid);
1306 if (!NT_STATUS_IS_OK(status)) {
1307 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1308 username,
1309 NT_STATUS_V(status),
1310 get_friendly_nt_error_msg(status));
1311 talloc_free(tframe);
1312 return NULL;
1315 talloc_free(tframe);
1316 return PyInt_FromLong(rid);
1319 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1321 NTSTATUS status;
1322 struct pdb_methods *methods;
1323 TALLOC_CTX *tframe;
1324 struct samu *sam_acct;
1325 PyObject *py_sam_acct;
1327 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1328 return NULL;
1331 methods = pytalloc_get_ptr(self);
1333 if ((tframe = talloc_stackframe()) == NULL) {
1334 PyErr_NoMemory();
1335 return NULL;
1338 sam_acct = pytalloc_get_ptr(py_sam_acct);
1340 status = methods->delete_user(methods, tframe, sam_acct);
1341 if (!NT_STATUS_IS_OK(status)) {
1342 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1343 NT_STATUS_V(status),
1344 get_friendly_nt_error_msg(status));
1345 talloc_free(tframe);
1346 return NULL;
1349 talloc_free(tframe);
1350 Py_RETURN_NONE;
1353 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1355 NTSTATUS status;
1356 struct pdb_methods *methods;
1357 TALLOC_CTX *tframe;
1358 struct samu *sam_acct;
1359 PyObject *py_sam_acct;
1361 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1362 return NULL;
1365 methods = pytalloc_get_ptr(self);
1367 if ((tframe = talloc_stackframe()) == NULL) {
1368 PyErr_NoMemory();
1369 return NULL;
1372 sam_acct = pytalloc_get_ptr(py_sam_acct);
1374 status = methods->add_sam_account(methods, sam_acct);
1375 if (!NT_STATUS_IS_OK(status)) {
1376 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1377 sam_acct->username,
1378 NT_STATUS_V(status),
1379 get_friendly_nt_error_msg(status));
1380 talloc_free(tframe);
1381 return NULL;
1384 talloc_free(tframe);
1385 Py_RETURN_NONE;
1388 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1390 NTSTATUS status;
1391 struct pdb_methods *methods;
1392 TALLOC_CTX *tframe;
1393 struct samu *sam_acct;
1394 PyObject *py_sam_acct;
1396 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1397 return NULL;
1400 methods = pytalloc_get_ptr(self);
1402 if ((tframe = talloc_stackframe()) == NULL) {
1403 PyErr_NoMemory();
1404 return NULL;
1407 sam_acct = pytalloc_get_ptr(py_sam_acct);
1409 status = methods->update_sam_account(methods, sam_acct);
1410 if (!NT_STATUS_IS_OK(status)) {
1411 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1412 NT_STATUS_V(status),
1413 get_friendly_nt_error_msg(status));
1414 talloc_free(tframe);
1415 return NULL;
1418 talloc_free(tframe);
1419 Py_RETURN_NONE;
1422 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1424 NTSTATUS status;
1425 struct pdb_methods *methods;
1426 TALLOC_CTX *tframe;
1427 struct samu *sam_acct;
1428 PyObject *py_sam_acct;
1430 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1431 return NULL;
1434 methods = pytalloc_get_ptr(self);
1436 if ((tframe = talloc_stackframe()) == NULL) {
1437 PyErr_NoMemory();
1438 return NULL;
1441 sam_acct = pytalloc_get_ptr(py_sam_acct);
1443 status = methods->delete_sam_account(methods, sam_acct);
1444 if (!NT_STATUS_IS_OK(status)) {
1445 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1446 NT_STATUS_V(status),
1447 get_friendly_nt_error_msg(status));
1448 talloc_free(tframe);
1449 return NULL;
1452 talloc_free(tframe);
1453 Py_RETURN_NONE;
1456 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1458 NTSTATUS status;
1459 struct pdb_methods *methods;
1460 TALLOC_CTX *tframe;
1461 struct samu *sam_acct;
1462 const char *new_username;
1463 PyObject *py_sam_acct;
1465 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1466 &new_username)) {
1467 return NULL;
1470 methods = pytalloc_get_ptr(self);
1472 if ((tframe = talloc_stackframe()) == NULL) {
1473 PyErr_NoMemory();
1474 return NULL;
1477 sam_acct = pytalloc_get_ptr(py_sam_acct);
1479 status = methods->rename_sam_account(methods, sam_acct, new_username);
1480 if (!NT_STATUS_IS_OK(status)) {
1481 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1482 NT_STATUS_V(status),
1483 get_friendly_nt_error_msg(status));
1484 talloc_free(tframe);
1485 return NULL;
1488 talloc_free(tframe);
1489 Py_RETURN_NONE;
1493 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1495 NTSTATUS status;
1496 struct pdb_methods *methods;
1497 TALLOC_CTX *tframe;
1498 GROUP_MAP *group_map;
1499 struct dom_sid *domain_sid;
1500 PyObject *py_domain_sid, *py_group_map;
1502 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1503 return NULL;
1506 methods = pytalloc_get_ptr(self);
1508 if ((tframe = talloc_stackframe()) == NULL) {
1509 PyErr_NoMemory();
1510 return NULL;
1513 domain_sid = pytalloc_get_ptr(py_domain_sid);
1515 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1516 if (py_group_map == NULL) {
1517 PyErr_NoMemory();
1518 talloc_free(tframe);
1519 return NULL;
1522 group_map = pytalloc_get_ptr(py_group_map);
1524 status = methods->getgrsid(methods, group_map, *domain_sid);
1525 if (!NT_STATUS_IS_OK(status)) {
1526 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1527 NT_STATUS_V(status),
1528 get_friendly_nt_error_msg(status));
1529 talloc_free(tframe);
1530 return NULL;
1533 talloc_free(tframe);
1534 return py_group_map;
1538 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1540 NTSTATUS status;
1541 struct pdb_methods *methods;
1542 TALLOC_CTX *tframe;
1543 GROUP_MAP *group_map;
1544 PyObject *py_group_map;
1545 unsigned int gid_value;
1547 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1548 return NULL;
1551 methods = pytalloc_get_ptr(self);
1553 if ((tframe = talloc_stackframe()) == NULL) {
1554 PyErr_NoMemory();
1555 return NULL;
1558 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1559 if (py_group_map == NULL) {
1560 PyErr_NoMemory();
1561 talloc_free(tframe);
1562 return NULL;
1565 group_map = pytalloc_get_ptr(py_group_map);
1567 status = methods->getgrgid(methods, group_map, gid_value);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1570 NT_STATUS_V(status),
1571 get_friendly_nt_error_msg(status));
1572 talloc_free(tframe);
1573 return NULL;
1576 talloc_free(tframe);
1577 return py_group_map;
1581 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1583 NTSTATUS status;
1584 struct pdb_methods *methods;
1585 TALLOC_CTX *tframe;
1586 GROUP_MAP *group_map;
1587 PyObject *py_group_map;
1588 const char *groupname;
1590 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1591 return NULL;
1594 methods = pytalloc_get_ptr(self);
1596 if ((tframe = talloc_stackframe()) == NULL) {
1597 PyErr_NoMemory();
1598 return NULL;
1601 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1602 if (py_group_map == NULL) {
1603 PyErr_NoMemory();
1604 talloc_free(tframe);
1605 return NULL;
1608 group_map = pytalloc_get_ptr(py_group_map);
1610 status = methods->getgrnam(methods, group_map, groupname);
1611 if (!NT_STATUS_IS_OK(status)) {
1612 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1613 NT_STATUS_V(status),
1614 get_friendly_nt_error_msg(status));
1615 talloc_free(tframe);
1616 return NULL;
1619 talloc_free(tframe);
1620 return py_group_map;
1624 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1626 NTSTATUS status;
1627 struct pdb_methods *methods;
1628 TALLOC_CTX *tframe;
1629 const char *groupname;
1630 uint32_t group_rid;
1632 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1633 return NULL;
1636 methods = pytalloc_get_ptr(self);
1638 if ((tframe = talloc_stackframe()) == NULL) {
1639 PyErr_NoMemory();
1640 return NULL;
1643 status = methods->create_dom_group(methods, tframe, groupname, &group_rid);
1644 if (!NT_STATUS_IS_OK(status)) {
1645 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1646 groupname,
1647 NT_STATUS_V(status),
1648 get_friendly_nt_error_msg(status));
1649 talloc_free(tframe);
1650 return NULL;
1653 talloc_free(tframe);
1654 return PyInt_FromLong(group_rid);
1658 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1660 NTSTATUS status;
1661 struct pdb_methods *methods;
1662 TALLOC_CTX *tframe;
1663 unsigned int group_rid;
1665 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1666 return NULL;
1669 methods = pytalloc_get_ptr(self);
1671 if ((tframe = talloc_stackframe()) == NULL) {
1672 PyErr_NoMemory();
1673 return NULL;
1676 status = methods->delete_dom_group(methods, tframe, group_rid);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1679 group_rid,
1680 NT_STATUS_V(status),
1681 get_friendly_nt_error_msg(status));
1682 talloc_free(tframe);
1683 return NULL;
1686 talloc_free(tframe);
1687 Py_RETURN_NONE;
1691 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1693 NTSTATUS status;
1694 struct pdb_methods *methods;
1695 TALLOC_CTX *tframe;
1696 PyObject *py_group_map;
1697 GROUP_MAP *group_map;
1699 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1700 return NULL;
1703 methods = pytalloc_get_ptr(self);
1705 if ((tframe = talloc_stackframe()) == NULL) {
1706 PyErr_NoMemory();
1707 return NULL;
1710 group_map = pytalloc_get_ptr(py_group_map);
1712 status = methods->add_group_mapping_entry(methods, group_map);
1713 if (!NT_STATUS_IS_OK(status)) {
1714 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1715 NT_STATUS_V(status),
1716 get_friendly_nt_error_msg(status));
1717 talloc_free(tframe);
1718 return NULL;
1721 talloc_free(tframe);
1722 Py_RETURN_NONE;
1726 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1728 NTSTATUS status;
1729 struct pdb_methods *methods;
1730 TALLOC_CTX *tframe;
1731 PyObject *py_group_map;
1732 GROUP_MAP *group_map;
1734 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1735 return NULL;
1738 methods = pytalloc_get_ptr(self);
1740 if ((tframe = talloc_stackframe()) == NULL) {
1741 PyErr_NoMemory();
1742 return NULL;
1745 group_map = pytalloc_get_ptr(py_group_map);
1747 status = methods->update_group_mapping_entry(methods, group_map);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1750 NT_STATUS_V(status),
1751 get_friendly_nt_error_msg(status));
1752 talloc_free(tframe);
1753 return NULL;
1756 talloc_free(tframe);
1757 Py_RETURN_NONE;
1761 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1763 NTSTATUS status;
1764 struct pdb_methods *methods;
1765 TALLOC_CTX *tframe;
1766 PyObject *py_group_sid;
1767 struct dom_sid *group_sid;
1769 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1770 return NULL;
1773 methods = pytalloc_get_ptr(self);
1775 if ((tframe = talloc_stackframe()) == NULL) {
1776 PyErr_NoMemory();
1777 return NULL;
1780 group_sid = pytalloc_get_ptr(py_group_sid);
1782 status = methods->delete_group_mapping_entry(methods, *group_sid);
1783 if (!NT_STATUS_IS_OK(status)) {
1784 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1785 NT_STATUS_V(status),
1786 get_friendly_nt_error_msg(status));
1787 talloc_free(tframe);
1788 return NULL;
1791 talloc_free(tframe);
1792 Py_RETURN_NONE;
1796 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1798 NTSTATUS status;
1799 struct pdb_methods *methods;
1800 TALLOC_CTX *tframe;
1801 enum lsa_SidType sid_name_use;
1802 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1803 int unix_only = 0;
1804 PyObject *py_domain_sid;
1805 struct dom_sid *domain_sid = NULL;
1806 GROUP_MAP **gmap = NULL;
1807 GROUP_MAP *group_map;
1808 size_t num_entries;
1809 PyObject *py_gmap_list, *py_group_map;
1810 int i;
1812 py_domain_sid = Py_None;
1813 Py_INCREF(Py_None);
1815 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1816 &lsa_sidtype_value, &unix_only)) {
1817 return NULL;
1820 methods = pytalloc_get_ptr(self);
1822 if ((tframe = talloc_stackframe()) == NULL) {
1823 PyErr_NoMemory();
1824 return NULL;
1827 sid_name_use = lsa_sidtype_value;
1829 if (py_domain_sid != Py_None) {
1830 domain_sid = pytalloc_get_ptr(py_domain_sid);
1833 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1834 &gmap, &num_entries, unix_only);
1835 if (!NT_STATUS_IS_OK(status)) {
1836 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1837 NT_STATUS_V(status),
1838 get_friendly_nt_error_msg(status));
1839 talloc_free(tframe);
1840 return NULL;
1843 py_gmap_list = PyList_New(0);
1844 if (py_gmap_list == NULL) {
1845 PyErr_NoMemory();
1846 talloc_free(tframe);
1847 return NULL;
1850 for(i=0; i<num_entries; i++) {
1851 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1852 if (py_group_map) {
1853 group_map = pytalloc_get_ptr(py_group_map);
1854 *group_map = *gmap[i];
1855 talloc_steal(group_map, gmap[i]->nt_name);
1856 talloc_steal(group_map, gmap[i]->comment);
1858 PyList_Append(py_gmap_list, py_group_map);
1862 talloc_free(gmap);
1863 talloc_free(tframe);
1865 return py_gmap_list;
1869 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1871 NTSTATUS status;
1872 struct pdb_methods *methods;
1873 TALLOC_CTX *tframe;
1874 PyObject *py_group_sid;
1875 struct dom_sid *group_sid;
1876 uint32_t *member_rids;
1877 size_t num_members;
1878 PyObject *py_sid_list;
1879 struct dom_sid *domain_sid, *member_sid;
1880 int i;
1882 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1883 return NULL;
1886 methods = pytalloc_get_ptr(self);
1888 if ((tframe = talloc_stackframe()) == NULL) {
1889 PyErr_NoMemory();
1890 return NULL;
1893 group_sid = pytalloc_get_ptr(py_group_sid);
1895 status = methods->enum_group_members(methods, tframe, group_sid,
1896 &member_rids, &num_members);
1897 if (!NT_STATUS_IS_OK(status)) {
1898 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1899 NT_STATUS_V(status),
1900 get_friendly_nt_error_msg(status));
1901 talloc_free(tframe);
1902 return NULL;
1905 py_sid_list = PyList_New(0);
1906 if (py_sid_list == NULL) {
1907 PyErr_NoMemory();
1908 talloc_free(tframe);
1909 return NULL;
1912 domain_sid = get_global_sam_sid();
1914 for(i=0; i<num_members; i++) {
1915 member_sid = dom_sid_add_rid(tframe, domain_sid, member_rids[i]);
1916 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
1919 talloc_free(tframe);
1921 return py_sid_list;
1925 static PyObject *py_pdb_enum_group_memberships(pytalloc_Object *self, PyObject *args)
1927 NTSTATUS status;
1928 struct pdb_methods *methods;
1929 TALLOC_CTX *tframe;
1930 int i;
1932 struct samu *sam_acct;
1933 PyObject *py_sam_acct;
1934 PyObject *py_sid_list;
1935 struct dom_sid *user_group_sids = NULL;
1936 gid_t *user_group_ids = NULL;
1937 uint32_t num_groups = 0;
1939 if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
1940 return NULL;
1943 methods = pytalloc_get_ptr(self);
1945 if ((tframe = talloc_stackframe()) == NULL) {
1946 PyErr_NoMemory();
1947 return NULL;
1950 sam_acct = pytalloc_get_ptr(py_sam_acct);
1952 status = methods->enum_group_memberships(methods, tframe, sam_acct,
1953 &user_group_sids, &user_group_ids, &num_groups);
1954 if (!NT_STATUS_IS_OK(status)) {
1955 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
1956 NT_STATUS_V(status),
1957 get_friendly_nt_error_msg(status));
1958 talloc_free(tframe);
1959 return NULL;
1962 py_sid_list = PyList_New(0);
1963 if (py_sid_list == NULL) {
1964 PyErr_NoMemory();
1965 talloc_free(tframe);
1966 return NULL;
1969 for(i=0; i<num_groups; i++) {
1970 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
1973 talloc_free(tframe);
1975 return py_sid_list;
1979 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
1981 NTSTATUS status;
1982 struct pdb_methods *methods;
1983 TALLOC_CTX *tframe;
1984 uint32_t group_rid, member_rid;
1986 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
1987 return NULL;
1990 methods = pytalloc_get_ptr(self);
1992 if ((tframe = talloc_stackframe()) == NULL) {
1993 PyErr_NoMemory();
1994 return NULL;
1997 status = methods->add_groupmem(methods, tframe, group_rid, member_rid);
1998 if (!NT_STATUS_IS_OK(status)) {
1999 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2000 NT_STATUS_V(status),
2001 get_friendly_nt_error_msg(status));
2002 talloc_free(tframe);
2003 return NULL;
2006 talloc_free(tframe);
2007 Py_RETURN_NONE;
2011 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
2013 NTSTATUS status;
2014 struct pdb_methods *methods;
2015 TALLOC_CTX *tframe;
2016 uint32_t group_rid, member_rid;
2018 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2019 return NULL;
2022 methods = pytalloc_get_ptr(self);
2024 if ((tframe = talloc_stackframe()) == NULL) {
2025 PyErr_NoMemory();
2026 return NULL;
2029 status = methods->del_groupmem(methods, tframe, group_rid, member_rid);
2030 if (!NT_STATUS_IS_OK(status)) {
2031 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2032 NT_STATUS_V(status),
2033 get_friendly_nt_error_msg(status));
2034 talloc_free(tframe);
2035 return NULL;
2038 talloc_free(tframe);
2039 Py_RETURN_NONE;
2043 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
2045 NTSTATUS status;
2046 struct pdb_methods *methods;
2047 TALLOC_CTX *tframe;
2048 const char *alias_name;
2049 uint32_t rid;
2051 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2052 return NULL;
2055 methods = pytalloc_get_ptr(self);
2057 if ((tframe = talloc_stackframe()) == NULL) {
2058 PyErr_NoMemory();
2059 return NULL;
2062 status = methods->create_alias(methods, alias_name, &rid);
2063 if (!NT_STATUS_IS_OK(status)) {
2064 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2065 alias_name,
2066 NT_STATUS_V(status),
2067 get_friendly_nt_error_msg(status));
2068 talloc_free(tframe);
2069 return NULL;
2072 talloc_free(tframe);
2074 return PyInt_FromLong(rid);
2078 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2080 NTSTATUS status;
2081 struct pdb_methods *methods;
2082 TALLOC_CTX *tframe;
2083 PyObject *py_alias_sid;
2084 struct dom_sid *alias_sid;
2086 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2087 return NULL;
2090 methods = pytalloc_get_ptr(self);
2092 if ((tframe = talloc_stackframe()) == NULL) {
2093 PyErr_NoMemory();
2094 return NULL;
2097 alias_sid = pytalloc_get_ptr(py_alias_sid);
2099 status = methods->delete_alias(methods, alias_sid);
2100 if (!NT_STATUS_IS_OK(status)) {
2101 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2102 NT_STATUS_V(status),
2103 get_friendly_nt_error_msg(status));
2104 talloc_free(tframe);
2105 return NULL;
2108 talloc_free(tframe);
2109 Py_RETURN_NONE;
2113 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2115 NTSTATUS status;
2116 struct pdb_methods *methods;
2117 TALLOC_CTX *tframe;
2118 PyObject *py_alias_sid;
2119 struct dom_sid *alias_sid;
2120 struct acct_info *alias_info;
2121 PyObject *py_alias_info;
2123 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2124 return NULL;
2127 methods = pytalloc_get_ptr(self);
2129 if ((tframe = talloc_stackframe()) == NULL) {
2130 PyErr_NoMemory();
2131 return NULL;
2134 alias_sid = pytalloc_get_ptr(py_alias_sid);
2136 alias_info = talloc_zero(tframe, struct acct_info);
2137 if (!alias_info) {
2138 PyErr_NoMemory();
2139 return NULL;
2142 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2143 if (!NT_STATUS_IS_OK(status)) {
2144 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2145 NT_STATUS_V(status),
2146 get_friendly_nt_error_msg(status));
2147 talloc_free(tframe);
2148 return NULL;
2151 py_alias_info = PyDict_New();
2152 if (py_alias_info == NULL) {
2153 PyErr_NoMemory();
2154 talloc_free(tframe);
2155 return NULL;
2158 PyDict_SetItemString(py_alias_info, "acct_name",
2159 PyString_FromString(alias_info->acct_name));
2160 PyDict_SetItemString(py_alias_info, "acct_desc",
2161 PyString_FromString(alias_info->acct_desc));
2162 PyDict_SetItemString(py_alias_info, "rid",
2163 PyInt_FromLong(alias_info->rid));
2165 talloc_free(tframe);
2167 return py_alias_info;
2171 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2173 NTSTATUS status;
2174 struct pdb_methods *methods;
2175 TALLOC_CTX *tframe;
2176 PyObject *py_alias_sid, *py_alias_info;
2177 struct dom_sid *alias_sid;
2178 struct acct_info alias_info;
2180 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2181 &py_alias_info)) {
2182 return NULL;
2185 methods = pytalloc_get_ptr(self);
2187 if ((tframe = talloc_stackframe()) == NULL) {
2188 PyErr_NoMemory();
2189 return NULL;
2192 alias_sid = pytalloc_get_ptr(py_alias_sid);
2194 fstrcpy(alias_info.acct_name, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2195 fstrcpy(alias_info.acct_desc, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2197 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2198 if (!NT_STATUS_IS_OK(status)) {
2199 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2200 NT_STATUS_V(status),
2201 get_friendly_nt_error_msg(status));
2202 talloc_free(tframe);
2203 return NULL;
2206 talloc_free(tframe);
2207 Py_RETURN_NONE;
2211 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2213 NTSTATUS status;
2214 struct pdb_methods *methods;
2215 TALLOC_CTX *tframe;
2216 PyObject *py_alias_sid, *py_member_sid;
2217 struct dom_sid *alias_sid, *member_sid;
2219 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2220 dom_sid_Type, &py_member_sid)) {
2221 return NULL;
2224 methods = pytalloc_get_ptr(self);
2226 if ((tframe = talloc_stackframe()) == NULL) {
2227 PyErr_NoMemory();
2228 return NULL;
2231 alias_sid = pytalloc_get_ptr(py_alias_sid);
2232 member_sid = pytalloc_get_ptr(py_member_sid);
2234 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2235 if (!NT_STATUS_IS_OK(status)) {
2236 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2237 NT_STATUS_V(status),
2238 get_friendly_nt_error_msg(status));
2239 talloc_free(tframe);
2240 return NULL;
2243 talloc_free(tframe);
2244 Py_RETURN_NONE;
2248 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2250 NTSTATUS status;
2251 struct pdb_methods *methods;
2252 TALLOC_CTX *tframe;
2253 PyObject *py_alias_sid, *py_member_sid;
2254 const struct dom_sid *alias_sid, *member_sid;
2256 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2257 dom_sid_Type, &py_member_sid)) {
2258 return NULL;
2261 methods = pytalloc_get_ptr(self);
2263 if ((tframe = talloc_stackframe()) == NULL) {
2264 PyErr_NoMemory();
2265 return NULL;
2268 alias_sid = pytalloc_get_ptr(py_alias_sid);
2269 member_sid = pytalloc_get_ptr(py_member_sid);
2271 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2272 if (!NT_STATUS_IS_OK(status)) {
2273 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2274 NT_STATUS_V(status),
2275 get_friendly_nt_error_msg(status));
2276 talloc_free(tframe);
2277 return NULL;
2280 talloc_free(tframe);
2281 Py_RETURN_NONE;
2285 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2287 NTSTATUS status;
2288 struct pdb_methods *methods;
2289 TALLOC_CTX *tframe;
2290 PyObject *py_alias_sid;
2291 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2292 PyObject *py_member_list, *py_member_sid;
2293 size_t num_members;
2294 int i;
2296 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2297 return NULL;
2300 methods = pytalloc_get_ptr(self);
2302 if ((tframe = talloc_stackframe()) == NULL) {
2303 PyErr_NoMemory();
2304 return NULL;
2307 alias_sid = pytalloc_get_ptr(py_alias_sid);
2309 status = methods->enum_aliasmem(methods, alias_sid, tframe, &member_sid, &num_members);
2310 if (!NT_STATUS_IS_OK(status)) {
2311 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2312 NT_STATUS_V(status),
2313 get_friendly_nt_error_msg(status));
2314 talloc_free(tframe);
2315 return NULL;
2318 py_member_list = PyList_New(0);
2319 if (py_member_list == NULL) {
2320 PyErr_NoMemory();
2321 talloc_free(tframe);
2322 return NULL;
2325 for(i=0; i<num_members; i++) {
2326 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2327 if (py_member_sid == NULL) {
2328 PyErr_NoMemory();
2329 talloc_free(tframe);
2330 return NULL;
2332 tmp_sid = pytalloc_get_ptr(py_member_sid);
2333 *tmp_sid = member_sid[i];
2334 PyList_Append(py_member_list, py_member_sid);
2337 talloc_free(tframe);
2339 return py_member_list;
2343 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2345 NTSTATUS status;
2346 struct pdb_methods *methods;
2347 TALLOC_CTX *tframe;
2348 PyObject *py_acct_policy;
2349 uint32_t value;
2350 const char **names;
2351 int count, i;
2352 enum pdb_policy_type type;
2354 methods = pytalloc_get_ptr(self);
2356 if ((tframe = talloc_stackframe()) == NULL) {
2357 PyErr_NoMemory();
2358 return NULL;
2361 py_acct_policy = PyDict_New();
2362 if (py_acct_policy == NULL) {
2363 PyErr_NoMemory();
2364 return NULL;
2367 account_policy_names_list(tframe, &names, &count);
2368 for (i=0; i<count; i++) {
2369 type = account_policy_name_to_typenum(names[i]);
2370 status = methods->get_account_policy(methods, type, &value);
2371 if (NT_STATUS_IS_OK(status)) {
2372 PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2376 talloc_free(tframe);
2378 return py_acct_policy;
2382 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2384 NTSTATUS status;
2385 struct pdb_methods *methods;
2386 TALLOC_CTX *tframe;
2387 PyObject *py_acct_policy, *py_value;
2388 const char **names;
2389 int count, i;
2390 enum pdb_policy_type type;
2392 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2393 return NULL;
2396 methods = pytalloc_get_ptr(self);
2398 if ((tframe = talloc_stackframe()) == NULL) {
2399 PyErr_NoMemory();
2400 return NULL;
2403 account_policy_names_list(tframe, &names, &count);
2404 for (i=0; i<count; i++) {
2405 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2406 type = account_policy_name_to_typenum(names[i]);
2407 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2408 if (!NT_STATUS_IS_OK(status)) {
2409 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2410 names[i],
2411 NT_STATUS_V(status),
2412 get_friendly_nt_error_msg(status));
2418 talloc_free(tframe);
2420 Py_RETURN_NONE;
2423 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2425 NTSTATUS status;
2426 struct pdb_methods *methods;
2427 TALLOC_CTX *tframe;
2428 unsigned int acct_flags;
2429 struct pdb_search *search;
2430 struct samr_displayentry *entry;
2431 PyObject *py_userlist, *py_dict;
2433 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2434 return NULL;
2437 methods = pytalloc_get_ptr(self);
2439 if ((tframe = talloc_stackframe()) == NULL) {
2440 PyErr_NoMemory();
2441 return NULL;
2444 search = talloc_zero(tframe, struct pdb_search);
2445 if (search == NULL) {
2446 PyErr_NoMemory();
2447 talloc_free(tframe);
2448 return NULL;
2451 if (!methods->search_users(methods, search, acct_flags)) {
2452 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
2453 NT_STATUS_V(status),
2454 get_friendly_nt_error_msg(status));
2455 talloc_free(tframe);
2456 return NULL;
2459 entry = talloc_zero(tframe, struct samr_displayentry);
2460 if (entry == NULL) {
2461 PyErr_NoMemory();
2462 talloc_free(tframe);
2463 return NULL;
2466 py_userlist = PyList_New(0);
2467 if (py_userlist == NULL) {
2468 PyErr_NoMemory();
2469 talloc_free(tframe);
2470 return NULL;
2473 while (search->next_entry(search, entry)) {
2474 py_dict = PyDict_New();
2475 if (py_dict == NULL) {
2476 PyErr_NoMemory();
2477 } else {
2478 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2479 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2480 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2481 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2482 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2483 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2484 PyList_Append(py_userlist, py_dict);
2487 search->search_end(search);
2489 talloc_free(tframe);
2491 return py_userlist;
2495 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2497 NTSTATUS status;
2498 struct pdb_methods *methods;
2499 TALLOC_CTX *tframe;
2500 struct pdb_search *search;
2501 struct samr_displayentry *entry;
2502 PyObject *py_grouplist, *py_dict;
2504 methods = pytalloc_get_ptr(self);
2506 if ((tframe = talloc_stackframe()) == NULL) {
2507 PyErr_NoMemory();
2508 return NULL;
2511 search = talloc_zero(tframe, struct pdb_search);
2512 if (search == NULL) {
2513 PyErr_NoMemory();
2514 talloc_free(tframe);
2515 return NULL;
2518 if (!methods->search_groups(methods, search)) {
2519 PyErr_Format(py_pdb_error, "Unable to search groups, (%d,%s)",
2520 NT_STATUS_V(status),
2521 get_friendly_nt_error_msg(status));
2522 talloc_free(tframe);
2523 return NULL;
2526 entry = talloc_zero(tframe, struct samr_displayentry);
2527 if (entry == NULL) {
2528 PyErr_NoMemory();
2529 talloc_free(tframe);
2530 return NULL;
2533 py_grouplist = PyList_New(0);
2534 if (py_grouplist == NULL) {
2535 PyErr_NoMemory();
2536 talloc_free(tframe);
2537 return NULL;
2540 while (search->next_entry(search, entry)) {
2541 py_dict = PyDict_New();
2542 if (py_dict == NULL) {
2543 PyErr_NoMemory();
2544 } else {
2545 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2546 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2547 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2548 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2549 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2550 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2551 PyList_Append(py_grouplist, py_dict);
2554 search->search_end(search);
2556 talloc_free(tframe);
2558 return py_grouplist;
2562 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2564 struct pdb_methods *methods;
2565 TALLOC_CTX *tframe;
2566 struct pdb_search *search;
2567 struct samr_displayentry *entry;
2568 PyObject *py_aliaslist, *py_dict;
2569 PyObject *py_domain_sid;
2570 struct dom_sid *domain_sid = NULL;
2572 py_domain_sid = Py_None;
2573 Py_INCREF(Py_None);
2575 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2576 return NULL;
2579 methods = pytalloc_get_ptr(self);
2581 if ((tframe = talloc_stackframe()) == NULL) {
2582 PyErr_NoMemory();
2583 return NULL;
2586 if (py_domain_sid != Py_None) {
2587 domain_sid = pytalloc_get_ptr(py_domain_sid);
2590 search = talloc_zero(tframe, struct pdb_search);
2591 if (search == NULL) {
2592 PyErr_NoMemory();
2593 talloc_free(tframe);
2594 return NULL;
2597 if (!methods->search_aliases(methods, search, domain_sid)) {
2598 PyErr_Format(py_pdb_error, "Unable to search aliases");
2599 talloc_free(tframe);
2600 return NULL;
2603 entry = talloc_zero(tframe, struct samr_displayentry);
2604 if (entry == NULL) {
2605 PyErr_NoMemory();
2606 talloc_free(tframe);
2607 return NULL;
2610 py_aliaslist = PyList_New(0);
2611 if (py_aliaslist == NULL) {
2612 PyErr_NoMemory();
2613 talloc_free(tframe);
2614 return NULL;
2617 while (search->next_entry(search, entry)) {
2618 py_dict = PyDict_New();
2619 if (py_dict == NULL) {
2620 PyErr_NoMemory();
2621 } else {
2622 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2623 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2624 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2625 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2626 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2627 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2628 PyList_Append(py_aliaslist, py_dict);
2631 search->search_end(search);
2633 talloc_free(tframe);
2635 return py_aliaslist;
2639 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2641 struct pdb_methods *methods;
2642 TALLOC_CTX *tframe;
2643 unsigned int uid;
2644 struct dom_sid user_sid, *copy_user_sid;
2645 PyObject *py_user_sid;
2647 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2648 return NULL;
2651 methods = pytalloc_get_ptr(self);
2653 if ((tframe = talloc_stackframe()) == NULL) {
2654 PyErr_NoMemory();
2655 return NULL;
2658 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2659 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2660 talloc_free(tframe);
2661 return NULL;
2664 copy_user_sid = dom_sid_dup(tframe, &user_sid);
2665 if (copy_user_sid == NULL) {
2666 PyErr_NoMemory();
2667 talloc_free(tframe);
2668 return NULL;
2671 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2673 talloc_free(tframe);
2675 return py_user_sid;
2679 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2681 struct pdb_methods *methods;
2682 TALLOC_CTX *tframe;
2683 unsigned int gid;
2684 struct dom_sid group_sid, *copy_group_sid;
2685 PyObject *py_group_sid;
2687 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2688 return NULL;
2691 methods = pytalloc_get_ptr(self);
2693 if ((tframe = talloc_stackframe()) == NULL) {
2694 PyErr_NoMemory();
2695 return NULL;
2698 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2699 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2700 talloc_free(tframe);
2701 return NULL;
2704 copy_group_sid = dom_sid_dup(tframe, &group_sid);
2705 if (copy_group_sid == NULL) {
2706 PyErr_NoMemory();
2707 talloc_free(tframe);
2708 return NULL;
2711 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2713 talloc_free(tframe);
2715 return py_group_sid;
2719 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2721 struct pdb_methods *methods;
2722 TALLOC_CTX *tframe;
2723 PyObject *py_sid;
2724 struct dom_sid *sid;
2725 struct unixid id;
2727 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2728 return NULL;
2731 methods = pytalloc_get_ptr(self);
2733 if ((tframe = talloc_stackframe()) == NULL) {
2734 PyErr_NoMemory();
2735 return NULL;
2738 sid = pytalloc_get_ptr(py_sid);
2740 if (!methods->sid_to_id(methods, sid, &id)) {
2741 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2742 talloc_free(tframe);
2743 return NULL;
2746 talloc_free(tframe);
2748 return Py_BuildValue("(II)", id.id, id.type);
2752 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2754 struct pdb_methods *methods;
2755 TALLOC_CTX *tframe;
2756 uint32_t rid;
2758 methods = pytalloc_get_ptr(self);
2760 if ((tframe = talloc_stackframe()) == NULL) {
2761 PyErr_NoMemory();
2762 return NULL;
2765 if (!methods->new_rid(methods, &rid)) {
2766 PyErr_Format(py_pdb_error, "Unable to get new rid");
2767 talloc_free(tframe);
2768 return NULL;
2771 talloc_free(tframe);
2773 return PyInt_FromLong(rid);
2777 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2779 struct pdb_methods *methods;
2780 TALLOC_CTX *tframe;
2781 const char *domain;
2782 char *pwd;
2783 struct dom_sid sid, *copy_sid;
2784 PyObject *py_sid;
2785 time_t last_set_time;
2786 PyObject *py_value;
2788 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2789 return NULL;
2792 methods = pytalloc_get_ptr(self);
2794 if ((tframe = talloc_stackframe()) == NULL) {
2795 PyErr_NoMemory();
2796 return NULL;
2799 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2800 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2801 talloc_free(tframe);
2802 return NULL;
2805 copy_sid = dom_sid_dup(tframe, &sid);
2806 if (copy_sid == NULL) {
2807 PyErr_NoMemory();
2808 talloc_free(tframe);
2809 return NULL;
2812 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2813 if (py_sid == NULL) {
2814 PyErr_NoMemory();
2815 talloc_free(tframe);
2816 return NULL;
2819 talloc_free(tframe);
2821 py_value = PyDict_New();
2822 if (py_value == NULL) {
2823 PyErr_NoMemory();
2824 return NULL;
2827 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2828 PyDict_SetItemString(py_value, "sid", py_sid);
2829 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2831 return py_value;
2835 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2837 struct pdb_methods *methods;
2838 TALLOC_CTX *tframe;
2839 const char *domain;
2840 const char *pwd;
2841 const struct dom_sid *domain_sid;
2842 PyObject *py_domain_sid;
2844 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2845 dom_sid_Type, &py_domain_sid)) {
2846 return NULL;
2849 methods = pytalloc_get_ptr(self);
2851 if ((tframe = talloc_stackframe()) == NULL) {
2852 PyErr_NoMemory();
2853 return NULL;
2856 domain_sid = pytalloc_get_ptr(py_domain_sid);
2858 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2859 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2860 talloc_free(tframe);
2861 return NULL;
2864 Py_RETURN_NONE;
2868 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2870 struct pdb_methods *methods;
2871 TALLOC_CTX *tframe;
2872 const char *domain;
2874 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2875 return NULL;
2878 methods = pytalloc_get_ptr(self);
2880 if ((tframe = talloc_stackframe()) == NULL) {
2881 PyErr_NoMemory();
2882 return NULL;
2885 if (!methods->del_trusteddom_pw(methods, domain)) {
2886 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2887 talloc_free(tframe);
2888 return NULL;
2891 Py_RETURN_NONE;
2895 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2897 NTSTATUS status;
2898 struct pdb_methods *methods;
2899 TALLOC_CTX *tframe;
2900 uint32_t num_domains;
2901 struct trustdom_info **domains;
2902 PyObject *py_domain_list, *py_dict;
2903 int i;
2905 methods = pytalloc_get_ptr(self);
2907 if ((tframe = talloc_stackframe()) == NULL) {
2908 PyErr_NoMemory();
2909 return NULL;
2912 status = methods->enum_trusteddoms(methods, tframe, &num_domains, &domains);
2913 if (!NT_STATUS_IS_OK(status)) {
2914 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2915 NT_STATUS_V(status),
2916 get_friendly_nt_error_msg(status));
2917 talloc_free(tframe);
2918 return NULL;
2921 py_domain_list = PyList_New(0);
2922 if (py_domain_list == NULL) {
2923 PyErr_NoMemory();
2924 talloc_free(tframe);
2925 return NULL;
2928 for(i=0; i<num_domains; i++) {
2929 py_dict = PyDict_New();
2930 if (py_dict) {
2931 PyDict_SetItemString(py_dict, "name",
2932 PyString_FromString(domains[i]->name));
2933 PyDict_SetItemString(py_dict, "sid",
2934 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2937 PyList_Append(py_domain_list, py_dict);
2940 talloc_free(tframe);
2942 return py_domain_list;
2946 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2948 NTSTATUS status;
2949 struct pdb_methods *methods;
2950 TALLOC_CTX *tframe;
2951 const char *domain;
2952 struct pdb_trusted_domain *td;
2953 PyObject *py_domain_info;
2955 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2956 return NULL;
2959 methods = pytalloc_get_ptr(self);
2961 if ((tframe = talloc_stackframe()) == NULL) {
2962 PyErr_NoMemory();
2963 return NULL;
2966 status = methods->get_trusted_domain(methods, tframe, domain, &td);
2967 if (!NT_STATUS_IS_OK(status)) {
2968 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2969 NT_STATUS_V(status),
2970 get_friendly_nt_error_msg(status));
2971 talloc_free(tframe);
2972 return NULL;
2975 py_domain_info = PyDict_New();
2976 if (py_domain_info == NULL) {
2977 PyErr_NoMemory();
2978 talloc_free(tframe);
2979 return NULL;
2982 PyDict_SetItemString(py_domain_info, "domain_name",
2983 PyString_FromString(td->domain_name));
2984 PyDict_SetItemString(py_domain_info, "netbios_name",
2985 PyString_FromString(td->netbios_name));
2986 PyDict_SetItemString(py_domain_info, "security_identifier",
2987 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2988 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2989 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2990 td->trust_auth_incoming.length));
2991 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2992 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2993 td->trust_auth_outgoing.length));
2994 PyDict_SetItemString(py_domain_info, "trust_direction",
2995 PyInt_FromLong(td->trust_direction));
2996 PyDict_SetItemString(py_domain_info, "trust_type",
2997 PyInt_FromLong(td->trust_type));
2998 PyDict_SetItemString(py_domain_info, "trust_attributes",
2999 PyInt_FromLong(td->trust_attributes));
3000 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3001 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3002 td->trust_forest_trust_info.length));
3004 talloc_free(tframe);
3006 return py_domain_info;
3010 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
3012 NTSTATUS status;
3013 struct pdb_methods *methods;
3014 TALLOC_CTX *tframe;
3015 PyObject *py_domain_sid;
3016 struct dom_sid *domain_sid;
3017 struct pdb_trusted_domain *td;
3018 PyObject *py_domain_info;
3020 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3021 return NULL;
3024 methods = pytalloc_get_ptr(self);
3026 if ((tframe = talloc_stackframe()) == NULL) {
3027 PyErr_NoMemory();
3028 return NULL;
3031 domain_sid = pytalloc_get_ptr(py_domain_sid);
3033 status = methods->get_trusted_domain_by_sid(methods, tframe, domain_sid, &td);
3034 if (!NT_STATUS_IS_OK(status)) {
3035 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3036 NT_STATUS_V(status),
3037 get_friendly_nt_error_msg(status));
3038 talloc_free(tframe);
3039 return NULL;
3042 py_domain_info = PyDict_New();
3043 if (py_domain_info == NULL) {
3044 PyErr_NoMemory();
3045 talloc_free(tframe);
3046 return NULL;
3049 PyDict_SetItemString(py_domain_info, "domain_name",
3050 PyString_FromString(td->domain_name));
3051 PyDict_SetItemString(py_domain_info, "netbios_name",
3052 PyString_FromString(td->netbios_name));
3053 PyDict_SetItemString(py_domain_info, "security_identifier",
3054 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3055 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3056 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3057 td->trust_auth_incoming.length));
3058 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3059 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3060 td->trust_auth_outgoing.length));
3061 PyDict_SetItemString(py_domain_info, "trust_direction",
3062 PyInt_FromLong(td->trust_direction));
3063 PyDict_SetItemString(py_domain_info, "trust_type",
3064 PyInt_FromLong(td->trust_type));
3065 PyDict_SetItemString(py_domain_info, "trust_attributes",
3066 PyInt_FromLong(td->trust_attributes));
3067 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3068 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3069 td->trust_forest_trust_info.length));
3071 talloc_free(tframe);
3073 return py_domain_info;
3077 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3079 NTSTATUS status;
3080 struct pdb_methods *methods;
3081 TALLOC_CTX *tframe;
3082 const char *domain;
3083 PyObject *py_td_info;
3084 struct pdb_trusted_domain td_info;
3085 PyObject *py_tmp;
3086 Py_ssize_t len;
3088 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3089 return NULL;
3092 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3093 td_info.domain_name = PyString_AsString(py_tmp);
3095 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3096 td_info.netbios_name = PyString_AsString(py_tmp);
3098 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3099 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3101 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3102 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3103 td_info.trust_auth_incoming.length = len;
3105 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3106 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3107 td_info.trust_auth_outgoing.length = len;
3109 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3110 td_info.trust_direction = PyInt_AsLong(py_tmp);
3112 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3113 td_info.trust_type = PyInt_AsLong(py_tmp);
3115 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3116 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3118 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3119 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3120 td_info.trust_forest_trust_info.length = len;
3122 methods = pytalloc_get_ptr(self);
3124 if ((tframe = talloc_stackframe()) == NULL) {
3125 PyErr_NoMemory();
3126 return NULL;
3129 status = methods->set_trusted_domain(methods, domain, &td_info);
3130 if (!NT_STATUS_IS_OK(status)) {
3131 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3132 NT_STATUS_V(status),
3133 get_friendly_nt_error_msg(status));
3134 talloc_free(tframe);
3135 return NULL;
3138 talloc_free(tframe);
3140 Py_RETURN_NONE;
3144 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3146 NTSTATUS status;
3147 struct pdb_methods *methods;
3148 TALLOC_CTX *tframe;
3149 const char *domain;
3151 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3152 return NULL;
3155 methods = pytalloc_get_ptr(self);
3157 if ((tframe = talloc_stackframe()) == NULL) {
3158 PyErr_NoMemory();
3159 return NULL;
3162 status = methods->del_trusted_domain(methods, domain);
3163 if (!NT_STATUS_IS_OK(status)) {
3164 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3165 NT_STATUS_V(status),
3166 get_friendly_nt_error_msg(status));
3167 talloc_free(tframe);
3168 return NULL;
3171 talloc_free(tframe);
3173 Py_RETURN_NONE;
3177 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3179 NTSTATUS status;
3180 struct pdb_methods *methods;
3181 TALLOC_CTX *tframe;
3182 uint32_t num_domains;
3183 struct pdb_trusted_domain **td_info, *td;
3184 PyObject *py_td_info, *py_domain_info;
3185 int i;
3187 methods = pytalloc_get_ptr(self);
3189 if ((tframe = talloc_stackframe()) == NULL) {
3190 PyErr_NoMemory();
3191 return NULL;
3194 status = methods->enum_trusted_domains(methods, tframe, &num_domains, &td_info);
3195 if (!NT_STATUS_IS_OK(status)) {
3196 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3197 NT_STATUS_V(status),
3198 get_friendly_nt_error_msg(status));
3199 talloc_free(tframe);
3200 return NULL;
3203 py_td_info = PyList_New(0);
3204 if (py_td_info == NULL) {
3205 PyErr_NoMemory();
3206 talloc_free(tframe);
3207 return NULL;
3210 for (i=0; i<num_domains; i++) {
3212 py_domain_info = PyDict_New();
3213 if (py_domain_info == NULL) {
3214 PyErr_NoMemory();
3215 Py_DECREF(py_td_info);
3216 talloc_free(tframe);
3217 return NULL;
3220 td = td_info[i];
3222 PyDict_SetItemString(py_domain_info, "domain_name",
3223 PyString_FromString(td->domain_name));
3224 PyDict_SetItemString(py_domain_info, "netbios_name",
3225 PyString_FromString(td->netbios_name));
3226 PyDict_SetItemString(py_domain_info, "security_identifier",
3227 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3228 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3229 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3230 td->trust_auth_incoming.length));
3231 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3232 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3233 td->trust_auth_outgoing.length));
3234 PyDict_SetItemString(py_domain_info, "trust_direction",
3235 PyInt_FromLong(td->trust_direction));
3236 PyDict_SetItemString(py_domain_info, "trust_type",
3237 PyInt_FromLong(td->trust_type));
3238 PyDict_SetItemString(py_domain_info, "trust_attributes",
3239 PyInt_FromLong(td->trust_attributes));
3240 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3241 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3242 td->trust_forest_trust_info.length));
3243 PyList_Append(py_td_info, py_domain_info);
3246 talloc_free(tframe);
3248 return py_td_info;
3252 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3254 NTSTATUS status;
3255 struct pdb_methods *methods;
3256 TALLOC_CTX *tframe;
3257 const char *secret_name;
3258 DATA_BLOB secret_current, secret_old;
3259 NTTIME secret_current_lastchange, secret_old_lastchange;
3260 PyObject *py_sd;
3261 struct security_descriptor *sd;
3262 PyObject *py_secret;
3264 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3265 return NULL;
3268 methods = pytalloc_get_ptr(self);
3270 if ((tframe = talloc_stackframe()) == NULL) {
3271 PyErr_NoMemory();
3272 return NULL;
3275 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3276 if (py_sd == NULL) {
3277 PyErr_NoMemory();
3278 talloc_free(tframe);
3279 return NULL;
3281 sd = pytalloc_get_ptr(py_sd);
3283 status = methods->get_secret(methods, tframe, secret_name,
3284 &secret_current,
3285 &secret_current_lastchange,
3286 &secret_old,
3287 &secret_old_lastchange,
3288 &sd);
3289 if (!NT_STATUS_IS_OK(status)) {
3290 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3291 secret_name,
3292 NT_STATUS_V(status),
3293 get_friendly_nt_error_msg(status));
3294 talloc_free(tframe);
3295 return NULL;
3298 py_secret = PyDict_New();
3299 if (py_secret == NULL) {
3300 PyErr_NoMemory();
3301 Py_DECREF(py_sd);
3302 talloc_free(tframe);
3303 return NULL;
3306 PyDict_SetItemString(py_secret, "secret_current",
3307 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3308 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3309 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3310 PyDict_SetItemString(py_secret, "secret_old",
3311 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3312 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3313 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3314 PyDict_SetItemString(py_secret, "sd", py_sd);
3316 talloc_free(tframe);
3318 return py_secret;
3322 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3324 NTSTATUS status;
3325 struct pdb_methods *methods;
3326 TALLOC_CTX *tframe;
3327 const char *secret_name;
3328 PyObject *py_secret;
3329 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3330 DATA_BLOB secret_current, secret_old;
3331 struct security_descriptor *sd;
3332 Py_ssize_t len;
3334 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3335 return NULL;
3338 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3339 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3340 py_sd = PyDict_GetItemString(py_secret, "sd");
3342 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3343 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3344 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3346 methods = pytalloc_get_ptr(self);
3348 if ((tframe = talloc_stackframe()) == NULL) {
3349 PyErr_NoMemory();
3350 return NULL;
3353 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3354 secret_current.length = len;
3355 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3356 secret_current.length = len;
3357 sd = pytalloc_get_ptr(py_sd);
3359 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3360 if (!NT_STATUS_IS_OK(status)) {
3361 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3362 secret_name,
3363 NT_STATUS_V(status),
3364 get_friendly_nt_error_msg(status));
3365 talloc_free(tframe);
3366 return NULL;
3369 talloc_free(tframe);
3371 Py_RETURN_NONE;
3375 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3377 NTSTATUS status;
3378 struct pdb_methods *methods;
3379 TALLOC_CTX *tframe;
3380 const char *secret_name;
3382 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3383 return NULL;
3386 methods = pytalloc_get_ptr(self);
3388 if ((tframe = talloc_stackframe()) == NULL) {
3389 PyErr_NoMemory();
3390 return NULL;
3393 status = methods->delete_secret(methods, secret_name);
3394 if (!NT_STATUS_IS_OK(status)) {
3395 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3396 secret_name,
3397 NT_STATUS_V(status),
3398 get_friendly_nt_error_msg(status));
3399 talloc_free(tframe);
3400 return NULL;
3403 talloc_free(tframe);
3405 Py_RETURN_NONE;
3408 static PyMethodDef py_pdb_methods[] = {
3409 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3410 "domain_info() -> str\n\n \
3411 Get domain information for the database." },
3412 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3413 "getsampwnam(username) -> samu object\n\n \
3414 Get user information by name." },
3415 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3416 "getsampwsid(user_sid) -> samu object\n\n \
3417 Get user information by sid (dcerpc.security.dom_sid object)." },
3418 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3419 "create_user(username, acct_flags) -> rid\n\n \
3420 Create user. acct_flags are samr account control flags." },
3421 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3422 "delete_user(samu object) -> None\n\n \
3423 Delete user." },
3424 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3425 "add_sam_account(samu object) -> None\n\n \
3426 Add SAM account." },
3427 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3428 "update_sam_account(samu object) -> None\n\n \
3429 Update SAM account." },
3430 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3431 "delete_sam_account(samu object) -> None\n\n \
3432 Delete SAM account." },
3433 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3434 "rename_sam_account(samu object1, new_username) -> None\n\n \
3435 Rename SAM account." },
3436 /* update_login_attempts */
3437 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3438 "getgrsid(group_sid) -> groupmap object\n\n \
3439 Get group information by sid (dcerpc.security.dom_sid object)." },
3440 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3441 "getgrsid(gid) -> groupmap object\n\n \
3442 Get group information by gid." },
3443 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3444 "getgrsid(groupname) -> groupmap object\n\n \
3445 Get group information by name." },
3446 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3447 "create_dom_group(groupname) -> group_rid\n\n \
3448 Create new domain group by name." },
3449 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3450 "delete_dom_group(group_rid) -> None\n\n \
3451 Delete domain group identified by rid" },
3452 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3453 "add_group_mapping_entry(groupmap) -> None\n \
3454 Add group mapping entry for groupmap object." },
3455 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3456 "update_group_mapping_entry(groupmap) -> None\n\n \
3457 Update group mapping entry for groupmap object." },
3458 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3459 "delete_group_mapping_entry(groupmap) -> None\n\n \
3460 Delete group mapping entry for groupmap object." },
3461 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3462 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3463 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3464 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3465 "enum_group_members(group_sid) -> List\n\n \
3466 Return list of users (dom_sid object) in group." },
3467 { "enum_group_memberships", (PyCFunction)py_pdb_enum_group_memberships, METH_VARARGS,
3468 "enum_group_memberships(samu object) -> List\n\n \
3469 Return list of groups (dom_sid object) this user is part of." },
3470 /* set_unix_primary_group */
3471 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3472 "add_groupmem(group_rid, member_rid) -> None\n\n \
3473 Add user to group." },
3474 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3475 "del_groupmem(group_rid, member_rid) -> None\n\n \
3476 Remove user from from group." },
3477 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3478 "create_alias(alias_name) -> alias_rid\n\n \
3479 Create alias entry." },
3480 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3481 "delete_alias(alias_sid) -> None\n\n \
3482 Delete alias entry." },
3483 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3484 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3485 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3486 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3487 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3488 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3489 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3490 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3491 Add user to alias entry." },
3492 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3493 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3494 Remove a user from alias entry." },
3495 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3496 "enum_aliasmem(alias_sid) -> List\n\n \
3497 Return a list of members (dom_sid object) for alias entry." },
3498 /* enum_alias_memberships */
3499 /* lookup_rids */
3500 /* lookup_names */
3501 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3502 "get_account_policy() -> Mapping\n\n \
3503 Get account policy information as a dictionary." },
3504 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3505 "get_account_policy(Mapping) -> None\n\n \
3506 Set account policy settings from a dicionary." },
3507 /* get_seq_num */
3508 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3509 "search_users(acct_flags) -> List\n\n \
3510 Search users. acct_flags are samr account control flags.\n \
3511 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3512 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3513 "search_groups() -> List\n\n \
3514 Search unix only groups. \n \
3515 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3516 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3517 "search_aliases([domain_sid]) -> List\n\n \
3518 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3519 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3520 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3521 "uid_to_sid(uid) -> sid\n\n \
3522 Return sid for given user id." },
3523 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3524 "gid_to_sid(gid) -> sid\n\n \
3525 Return sid for given group id." },
3526 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3527 "sid_to_id(sid) -> Tuple\n\n \
3528 Return id and type for given sid." },
3529 /* capabilities */
3530 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3531 "new_rid() -> rid\n\n \
3532 Get a new rid." },
3533 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3534 "get_trusteddom_pw(domain) -> Mapping\n\n \
3535 Get trusted domain password, sid and last set time in a dictionary." },
3536 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3537 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3538 Set trusted domain password." },
3539 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3540 "del_trusteddom_pw(domain) -> None\n\n \
3541 Delete trusted domain password." },
3542 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3543 "enum_trusteddoms() -> List\n\n \
3544 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3545 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3546 "get_trusted_domain(domain) -> Mapping\n\n \
3547 Get trusted domain information by name. Information is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info." },
3548 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3549 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3550 Get trusted domain information by sid. Information is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info" },
3551 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3552 "set_trusted_domain(domain, Mapping) -> None\n\n \
3553 Set trusted domain information for domain. Mapping is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info." },
3554 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3555 "del_trusted_domain(domain) -> None\n\n \
3556 Delete trusted domain." },
3557 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3558 "enum_trusted_domains() -> List\n\n \
3559 Get list of trusted domains. Each entry is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info." },
3560 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3561 "get_secret(secret_name) -> Mapping\n\n \
3562 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3563 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3564 "set_secret(secret_name, Mapping) -> None\n\n \
3565 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3566 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3567 "delete_secret(secret_name) -> None\n\n \
3568 Delete secret information for secret_name." },
3569 { NULL },
3573 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3575 const char *url = NULL;
3576 PyObject *pypdb;
3577 NTSTATUS status;
3578 struct pdb_methods *methods;
3580 if (!PyArg_ParseTuple(args, "s", &url)) {
3581 return NULL;
3584 /* Initalize list of methods */
3585 status = make_pdb_method_name(&methods, url);
3586 if (!NT_STATUS_IS_OK(status)) {
3587 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3588 url,
3589 NT_STATUS_V(status),
3590 get_friendly_nt_error_msg(status));
3591 return NULL;
3594 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3595 PyErr_NoMemory();
3596 return NULL;
3599 return pypdb;
3603 static PyTypeObject PyPDB = {
3604 .tp_name = "passdb.PDB",
3605 .tp_basicsize = sizeof(pytalloc_Object),
3606 .tp_new = py_pdb_new,
3607 .tp_flags = Py_TPFLAGS_DEFAULT,
3608 .tp_methods = py_pdb_methods,
3609 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3614 * Return a list of passdb backends
3616 static PyObject *py_passdb_backends(PyObject *self)
3618 PyObject *py_blist;
3619 const struct pdb_init_function_entry *entry;
3620 TALLOC_CTX *tframe;
3622 if ((tframe = talloc_stackframe()) == NULL) {
3623 PyErr_NoMemory();
3624 return NULL;
3627 entry = pdb_get_backends();
3628 if(! entry) {
3629 Py_RETURN_NONE;
3632 if((py_blist = PyList_New(0)) == NULL) {
3633 PyErr_NoMemory();
3634 return NULL;
3637 while(entry) {
3638 PyList_Append(py_blist, PyString_FromString(entry->name));
3639 entry = entry->next;
3642 talloc_free(tframe);
3644 return py_blist;
3648 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3650 const char *smb_config;
3651 TALLOC_CTX *tframe;
3653 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3654 return NULL;
3657 if ((tframe = talloc_stackframe()) == NULL) {
3658 PyErr_NoMemory();
3659 return NULL;
3662 /* Load smbconf parameters */
3663 if (!lp_load_global(smb_config)) {
3664 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3665 return NULL;
3668 talloc_free(tframe);
3670 Py_RETURN_NONE;
3674 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3676 const char *private_dir;
3677 TALLOC_CTX *tframe;
3679 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3680 return NULL;
3683 if ((tframe = talloc_stackframe()) == NULL) {
3684 PyErr_NoMemory();
3685 return NULL;
3688 /* Initialize secrets database */
3689 if (!secrets_init_path(private_dir)) {
3690 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3691 private_dir);
3692 return NULL;
3695 talloc_free(tframe);
3697 Py_RETURN_NONE;
3700 static PyObject *py_get_global_sam_sid(PyObject *self)
3702 struct dom_sid *domain_sid, *domain_sid_copy;
3703 TALLOC_CTX *tframe;
3704 PyObject *py_dom_sid;
3706 tframe = talloc_stackframe();
3707 if (tframe == NULL) {
3708 PyErr_NoMemory();
3709 return NULL;
3712 domain_sid = get_global_sam_sid();
3714 domain_sid_copy = dom_sid_dup(tframe, domain_sid);
3715 if (domain_sid_copy == NULL) {
3716 PyErr_NoMemory();
3717 talloc_free(tframe);
3718 return NULL;
3721 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3723 talloc_free(tframe);
3725 return py_dom_sid;
3729 static PyMethodDef py_passdb_methods[] = {
3730 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3731 "get_backends() -> list\n\n \
3732 Get a list of password database backends supported." },
3733 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3734 "set_smb_config(path) -> None\n\n \
3735 Set path to smb.conf file to load configuration parameters." },
3736 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3737 "set_secrets_dir(private_dir) -> None\n\n \
3738 Set path to private directory to load secrets database from non-default location." },
3739 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3740 "get_global_sam_sid() -> dom_sid\n\n \
3741 Return domain SID." },
3742 { NULL },
3745 void initpassdb(void)
3747 PyObject *m, *mod;
3748 char exception_name[] = "passdb.error";
3750 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3751 if (talloc_type == NULL) {
3752 return;
3755 PyPDB.tp_base = talloc_type;
3756 if (PyType_Ready(&PyPDB) < 0) {
3757 return;
3760 PySamu.tp_base = talloc_type;
3761 if (PyType_Ready(&PySamu) < 0) {
3762 return;
3765 PyGroupmap.tp_base = talloc_type;
3766 if (PyType_Ready(&PyGroupmap) < 0) {
3767 return;
3770 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3771 if (m == NULL) {
3772 return;
3775 /* Create new exception for passdb module */
3776 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3777 Py_INCREF(py_pdb_error);
3778 PyModule_AddObject(m, "error", py_pdb_error);
3780 Py_INCREF(&PyPDB);
3781 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3783 Py_INCREF(&PySamu);
3784 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3786 Py_INCREF(&PyGroupmap);
3787 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3789 /* Import dom_sid type from dcerpc.security */
3790 mod = PyImport_ImportModule("samba.dcerpc.security");
3791 if (mod == NULL) {
3792 return;
3795 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3796 if (dom_sid_Type == NULL) {
3797 return;
3800 /* Import security_descriptor type from dcerpc.security */
3801 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3802 Py_DECREF(mod);
3803 if (security_Type == NULL) {
3804 return;
3807 /* Import GUID type from dcerpc.misc */
3808 mod = PyImport_ImportModule("samba.dcerpc.misc");
3809 if (mod == NULL) {
3810 return;
3813 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3814 Py_DECREF(mod);
3815 if (guid_Type == NULL) {
3816 return;