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/>.
23 #include "lib/util/talloc_stack.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/idmap.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;
36 #ifndef Py_RETURN_NONE
37 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
40 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
41 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_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); \
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));
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)) {
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)) {
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)) {
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)) {
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)) {
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)) {
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 */
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) {
216 py_username = PyString_FromString(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)) {
231 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
233 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
237 domain = pdb_get_domain(sam_acct);
238 if (domain == NULL) {
242 py_domain = PyString_FromString(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)) {
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) {
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)) {
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) {
294 py_full_name = PyString_FromString(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)) {
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) {
320 py_home_dir = PyString_FromString(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)) {
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) {
346 py_dir_drive = PyString_FromString(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)) {
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) {
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)) {
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) {
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)) {
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) {
424 py_acct_desc = PyString_FromString(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)) {
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) {
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)) {
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;
471 comment = pdb_get_comment(sam_acct);
472 if (comment == NULL) {
476 py_comment = PyString_FromString(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)) {
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) {
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)) {
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;
525 user_sid = pdb_get_user_sid(sam_acct);
526 if(user_sid == NULL) {
530 mem_ctx = talloc_new(NULL);
531 if (mem_ctx == NULL) {
535 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
536 if (copy_user_sid == NULL) {
538 talloc_free(mem_ctx);
542 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
544 talloc_free(mem_ctx);
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)) {
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;
568 mem_ctx = talloc_stackframe();
569 if (mem_ctx == NULL) {
574 group_sid = pdb_get_group_sid(sam_acct);
575 if (group_sid == NULL) {
579 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
580 if (copy_group_sid == NULL) {
582 talloc_free(mem_ctx);
586 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
588 talloc_free(mem_ctx);
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)) {
604 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
606 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
610 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
615 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
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)) {
630 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
632 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
636 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
641 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
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)) {
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;
662 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
663 if (nt_pw_his == NULL) {
667 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
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);
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)) {
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) {
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))) {
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));
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)) {
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)) {
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));
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)) {
771 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
773 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
778 hours = (const char *)pdb_get_hours(sam_acct);
783 hours_len = pdb_get_hours_len(sam_acct);
784 if ((py_hours = PyList_New(hours_len)) == NULL) {
789 for (i=0; i<hours_len; i++) {
790 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
795 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
797 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
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);
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);
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)) {
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)) {
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)) {
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));
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)) {
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 },
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);
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,
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);
976 py_gid = Py_BuildValue("i", group_map->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);
989 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
991 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
993 struct dom_sid *group_sid;
996 mem_ctx = talloc_new(NULL);
997 if (mem_ctx == NULL) {
1002 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1003 if (group_sid == NULL) {
1005 talloc_free(mem_ctx);
1009 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1011 talloc_free(mem_ctx);
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);
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);
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);
1051 py_nt_name = PyString_FromString(group_map->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);
1064 fstrcpy(group_map->nt_name, PyString_AsString(value));
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);
1077 py_comment = PyString_FromString(group_map->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);
1090 fstrcpy(group_map->comment, PyString_AsString(value));
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 },
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) {
1116 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1117 if (group_map == NULL) {
1119 talloc_free(mem_ctx);
1123 py_group_map = pytalloc_steal(type, group_map);
1124 if (py_group_map == NULL) {
1126 talloc_free(mem_ctx);
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,
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;
1153 struct dom_sid *sid;
1156 methods = pytalloc_get_ptr(self);
1158 if ((tframe = talloc_stackframe()) == NULL) {
1163 domain_info = methods->get_domain_info(methods, tframe);
1164 if (! domain_info) {
1168 sid = dom_sid_dup(tframe, &domain_info->sid);
1171 talloc_free(tframe);
1175 guid = talloc(tframe, struct GUID);
1178 talloc_free(tframe);
1181 *guid = domain_info->guid;
1183 if ((py_domain_info = PyDict_New()) == 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)
1203 const char *username;
1204 struct pdb_methods *methods;
1205 struct samu *sam_acct;
1206 PyObject *py_sam_acct;
1209 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1213 methods = pytalloc_get_ptr(self);
1215 if ((tframe = talloc_stackframe()) == NULL) {
1220 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1221 if (py_sam_acct == NULL) {
1223 talloc_free(tframe);
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)",
1232 NT_STATUS_V(status),
1233 get_friendly_nt_error_msg(status));
1234 Py_DECREF(py_sam_acct);
1235 talloc_free(tframe);
1239 talloc_free(tframe);
1243 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1246 struct pdb_methods *methods;
1247 struct samu *sam_acct;
1248 PyObject *py_sam_acct;
1250 PyObject *py_user_sid;
1252 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1256 methods = pytalloc_get_ptr(self);
1258 if ((tframe = talloc_stackframe()) == NULL) {
1263 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1264 if (py_sam_acct == NULL) {
1266 talloc_free(tframe);
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);
1281 talloc_free(tframe);
1285 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1288 struct pdb_methods *methods;
1289 const char *username;
1290 unsigned int acct_flags;
1294 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1298 methods = pytalloc_get_ptr(self);
1300 if ((tframe = talloc_stackframe()) == 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)",
1309 NT_STATUS_V(status),
1310 get_friendly_nt_error_msg(status));
1311 talloc_free(tframe);
1315 talloc_free(tframe);
1316 return PyInt_FromLong(rid);
1319 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1322 struct pdb_methods *methods;
1324 struct samu *sam_acct;
1325 PyObject *py_sam_acct;
1327 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1331 methods = pytalloc_get_ptr(self);
1333 if ((tframe = talloc_stackframe()) == 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);
1349 talloc_free(tframe);
1353 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1356 struct pdb_methods *methods;
1358 struct samu *sam_acct;
1359 PyObject *py_sam_acct;
1361 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1365 methods = pytalloc_get_ptr(self);
1367 if ((tframe = talloc_stackframe()) == 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)",
1378 NT_STATUS_V(status),
1379 get_friendly_nt_error_msg(status));
1380 talloc_free(tframe);
1384 talloc_free(tframe);
1388 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1391 struct pdb_methods *methods;
1393 struct samu *sam_acct;
1394 PyObject *py_sam_acct;
1396 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1400 methods = pytalloc_get_ptr(self);
1402 if ((tframe = talloc_stackframe()) == 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);
1418 talloc_free(tframe);
1422 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1425 struct pdb_methods *methods;
1427 struct samu *sam_acct;
1428 PyObject *py_sam_acct;
1430 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1434 methods = pytalloc_get_ptr(self);
1436 if ((tframe = talloc_stackframe()) == 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);
1452 talloc_free(tframe);
1456 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1459 struct pdb_methods *methods;
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,
1470 methods = pytalloc_get_ptr(self);
1472 if ((tframe = talloc_stackframe()) == 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);
1488 talloc_free(tframe);
1493 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1496 struct pdb_methods *methods;
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)) {
1506 methods = pytalloc_get_ptr(self);
1508 if ((tframe = talloc_stackframe()) == 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) {
1518 talloc_free(tframe);
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);
1533 talloc_free(tframe);
1534 return py_group_map;
1538 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1541 struct pdb_methods *methods;
1543 GROUP_MAP *group_map;
1544 PyObject *py_group_map;
1545 unsigned int gid_value;
1547 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1551 methods = pytalloc_get_ptr(self);
1553 if ((tframe = talloc_stackframe()) == NULL) {
1558 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1559 if (py_group_map == NULL) {
1561 talloc_free(tframe);
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);
1576 talloc_free(tframe);
1577 return py_group_map;
1581 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1584 struct pdb_methods *methods;
1586 GROUP_MAP *group_map;
1587 PyObject *py_group_map;
1588 const char *groupname;
1590 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1594 methods = pytalloc_get_ptr(self);
1596 if ((tframe = talloc_stackframe()) == NULL) {
1601 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1602 if (py_group_map == NULL) {
1604 talloc_free(tframe);
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);
1619 talloc_free(tframe);
1620 return py_group_map;
1624 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1627 struct pdb_methods *methods;
1629 const char *groupname;
1632 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1636 methods = pytalloc_get_ptr(self);
1638 if ((tframe = talloc_stackframe()) == 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)",
1647 NT_STATUS_V(status),
1648 get_friendly_nt_error_msg(status));
1649 talloc_free(tframe);
1653 talloc_free(tframe);
1654 return PyInt_FromLong(group_rid);
1658 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1661 struct pdb_methods *methods;
1663 unsigned int group_rid;
1665 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1669 methods = pytalloc_get_ptr(self);
1671 if ((tframe = talloc_stackframe()) == 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)",
1680 NT_STATUS_V(status),
1681 get_friendly_nt_error_msg(status));
1682 talloc_free(tframe);
1686 talloc_free(tframe);
1691 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1694 struct pdb_methods *methods;
1696 PyObject *py_group_map;
1697 GROUP_MAP *group_map;
1699 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1703 methods = pytalloc_get_ptr(self);
1705 if ((tframe = talloc_stackframe()) == 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);
1721 talloc_free(tframe);
1726 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1729 struct pdb_methods *methods;
1731 PyObject *py_group_map;
1732 GROUP_MAP *group_map;
1734 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1738 methods = pytalloc_get_ptr(self);
1740 if ((tframe = talloc_stackframe()) == 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);
1756 talloc_free(tframe);
1761 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1764 struct pdb_methods *methods;
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)) {
1773 methods = pytalloc_get_ptr(self);
1775 if ((tframe = talloc_stackframe()) == 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);
1791 talloc_free(tframe);
1796 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1799 struct pdb_methods *methods;
1801 enum lsa_SidType sid_name_use;
1802 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1804 PyObject *py_domain_sid;
1805 struct dom_sid *domain_sid = NULL;
1806 GROUP_MAP **gmap = NULL;
1807 GROUP_MAP *group_map;
1809 PyObject *py_gmap_list, *py_group_map;
1812 py_domain_sid = Py_None;
1815 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1816 &lsa_sidtype_value, &unix_only)) {
1820 methods = pytalloc_get_ptr(self);
1822 if ((tframe = talloc_stackframe()) == 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);
1843 py_gmap_list = PyList_New(0);
1844 if (py_gmap_list == NULL) {
1846 talloc_free(tframe);
1850 for(i=0; i<num_entries; i++) {
1851 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
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);
1863 talloc_free(tframe);
1865 return py_gmap_list;
1869 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1872 struct pdb_methods *methods;
1874 PyObject *py_group_sid;
1875 struct dom_sid *group_sid;
1876 uint32_t *member_rids;
1878 PyObject *py_sid_list;
1879 struct dom_sid *domain_sid, *member_sid;
1882 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1886 methods = pytalloc_get_ptr(self);
1888 if ((tframe = talloc_stackframe()) == 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);
1905 py_sid_list = PyList_New(0);
1906 if (py_sid_list == NULL) {
1908 talloc_free(tframe);
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);
1925 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
1928 struct pdb_methods *methods;
1930 uint32_t group_rid, member_rid;
1932 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
1936 methods = pytalloc_get_ptr(self);
1938 if ((tframe = talloc_stackframe()) == NULL) {
1943 status = methods->add_groupmem(methods, tframe, group_rid, member_rid);
1944 if (!NT_STATUS_IS_OK(status)) {
1945 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
1946 NT_STATUS_V(status),
1947 get_friendly_nt_error_msg(status));
1948 talloc_free(tframe);
1952 talloc_free(tframe);
1957 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
1960 struct pdb_methods *methods;
1962 uint32_t group_rid, member_rid;
1964 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
1968 methods = pytalloc_get_ptr(self);
1970 if ((tframe = talloc_stackframe()) == NULL) {
1975 status = methods->del_groupmem(methods, tframe, group_rid, member_rid);
1976 if (!NT_STATUS_IS_OK(status)) {
1977 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1978 NT_STATUS_V(status),
1979 get_friendly_nt_error_msg(status));
1980 talloc_free(tframe);
1984 talloc_free(tframe);
1989 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
1992 struct pdb_methods *methods;
1994 const char *alias_name;
1997 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2001 methods = pytalloc_get_ptr(self);
2003 if ((tframe = talloc_stackframe()) == NULL) {
2008 status = methods->create_alias(methods, alias_name, &rid);
2009 if (!NT_STATUS_IS_OK(status)) {
2010 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2012 NT_STATUS_V(status),
2013 get_friendly_nt_error_msg(status));
2014 talloc_free(tframe);
2018 talloc_free(tframe);
2020 return PyInt_FromLong(rid);
2024 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2027 struct pdb_methods *methods;
2029 PyObject *py_alias_sid;
2030 struct dom_sid *alias_sid;
2032 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2036 methods = pytalloc_get_ptr(self);
2038 if ((tframe = talloc_stackframe()) == NULL) {
2043 alias_sid = pytalloc_get_ptr(py_alias_sid);
2045 status = methods->delete_alias(methods, alias_sid);
2046 if (!NT_STATUS_IS_OK(status)) {
2047 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2048 NT_STATUS_V(status),
2049 get_friendly_nt_error_msg(status));
2050 talloc_free(tframe);
2054 talloc_free(tframe);
2059 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2062 struct pdb_methods *methods;
2064 PyObject *py_alias_sid;
2065 struct dom_sid *alias_sid;
2066 struct acct_info *alias_info;
2067 PyObject *py_alias_info;
2069 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2073 methods = pytalloc_get_ptr(self);
2075 if ((tframe = talloc_stackframe()) == NULL) {
2080 alias_sid = pytalloc_get_ptr(py_alias_sid);
2082 alias_info = talloc_zero(tframe, struct acct_info);
2088 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2089 if (!NT_STATUS_IS_OK(status)) {
2090 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2091 NT_STATUS_V(status),
2092 get_friendly_nt_error_msg(status));
2093 talloc_free(tframe);
2097 py_alias_info = PyDict_New();
2098 if (py_alias_info == NULL) {
2100 talloc_free(tframe);
2104 PyDict_SetItemString(py_alias_info, "acct_name",
2105 PyString_FromString(alias_info->acct_name));
2106 PyDict_SetItemString(py_alias_info, "acct_desc",
2107 PyString_FromString(alias_info->acct_desc));
2108 PyDict_SetItemString(py_alias_info, "rid",
2109 PyInt_FromLong(alias_info->rid));
2111 talloc_free(tframe);
2113 return py_alias_info;
2117 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2120 struct pdb_methods *methods;
2122 PyObject *py_alias_sid, *py_alias_info;
2123 struct dom_sid *alias_sid;
2124 struct acct_info alias_info;
2126 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2131 methods = pytalloc_get_ptr(self);
2133 if ((tframe = talloc_stackframe()) == NULL) {
2138 alias_sid = pytalloc_get_ptr(py_alias_sid);
2140 fstrcpy(alias_info.acct_name, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2141 fstrcpy(alias_info.acct_desc, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2143 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2144 if (!NT_STATUS_IS_OK(status)) {
2145 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2146 NT_STATUS_V(status),
2147 get_friendly_nt_error_msg(status));
2148 talloc_free(tframe);
2152 talloc_free(tframe);
2157 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2160 struct pdb_methods *methods;
2162 PyObject *py_alias_sid, *py_member_sid;
2163 struct dom_sid *alias_sid, *member_sid;
2165 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2166 dom_sid_Type, &py_member_sid)) {
2170 methods = pytalloc_get_ptr(self);
2172 if ((tframe = talloc_stackframe()) == NULL) {
2177 alias_sid = pytalloc_get_ptr(py_alias_sid);
2178 member_sid = pytalloc_get_ptr(py_member_sid);
2180 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2181 if (!NT_STATUS_IS_OK(status)) {
2182 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2183 NT_STATUS_V(status),
2184 get_friendly_nt_error_msg(status));
2185 talloc_free(tframe);
2189 talloc_free(tframe);
2194 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2197 struct pdb_methods *methods;
2199 PyObject *py_alias_sid, *py_member_sid;
2200 const struct dom_sid *alias_sid, *member_sid;
2202 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2203 dom_sid_Type, &py_member_sid)) {
2207 methods = pytalloc_get_ptr(self);
2209 if ((tframe = talloc_stackframe()) == NULL) {
2214 alias_sid = pytalloc_get_ptr(py_alias_sid);
2215 member_sid = pytalloc_get_ptr(py_member_sid);
2217 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2218 if (!NT_STATUS_IS_OK(status)) {
2219 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2220 NT_STATUS_V(status),
2221 get_friendly_nt_error_msg(status));
2222 talloc_free(tframe);
2226 talloc_free(tframe);
2231 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2234 struct pdb_methods *methods;
2236 PyObject *py_alias_sid;
2237 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2238 PyObject *py_member_list, *py_member_sid;
2242 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2246 methods = pytalloc_get_ptr(self);
2248 if ((tframe = talloc_stackframe()) == NULL) {
2253 alias_sid = pytalloc_get_ptr(py_alias_sid);
2255 status = methods->enum_aliasmem(methods, alias_sid, tframe, &member_sid, &num_members);
2256 if (!NT_STATUS_IS_OK(status)) {
2257 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2258 NT_STATUS_V(status),
2259 get_friendly_nt_error_msg(status));
2260 talloc_free(tframe);
2264 py_member_list = PyList_New(0);
2265 if (py_member_list == NULL) {
2267 talloc_free(tframe);
2271 for(i=0; i<num_members; i++) {
2272 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2273 if (py_member_sid == NULL) {
2275 talloc_free(tframe);
2278 tmp_sid = pytalloc_get_ptr(py_member_sid);
2279 *tmp_sid = member_sid[i];
2280 PyList_Append(py_member_list, py_member_sid);
2283 talloc_free(tframe);
2285 return py_member_list;
2289 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2292 struct pdb_methods *methods;
2294 PyObject *py_acct_policy;
2298 enum pdb_policy_type type;
2300 methods = pytalloc_get_ptr(self);
2302 if ((tframe = talloc_stackframe()) == NULL) {
2307 py_acct_policy = PyDict_New();
2308 if (py_acct_policy == NULL) {
2313 account_policy_names_list(tframe, &names, &count);
2314 for (i=0; i<count; i++) {
2315 type = account_policy_name_to_typenum(names[i]);
2316 status = methods->get_account_policy(methods, type, &value);
2317 if (NT_STATUS_IS_OK(status)) {
2318 PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2322 talloc_free(tframe);
2324 return py_acct_policy;
2328 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2331 struct pdb_methods *methods;
2333 PyObject *py_acct_policy, *py_value;
2336 enum pdb_policy_type type;
2338 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2342 methods = pytalloc_get_ptr(self);
2344 if ((tframe = talloc_stackframe()) == NULL) {
2349 account_policy_names_list(tframe, &names, &count);
2350 for (i=0; i<count; i++) {
2351 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2352 type = account_policy_name_to_typenum(names[i]);
2353 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2354 if (!NT_STATUS_IS_OK(status)) {
2355 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2357 NT_STATUS_V(status),
2358 get_friendly_nt_error_msg(status));
2364 talloc_free(tframe);
2369 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2372 struct pdb_methods *methods;
2374 unsigned int acct_flags;
2375 struct pdb_search *search;
2376 struct samr_displayentry *entry;
2377 PyObject *py_userlist, *py_dict;
2379 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2383 methods = pytalloc_get_ptr(self);
2385 if ((tframe = talloc_stackframe()) == NULL) {
2390 search = talloc_zero(tframe, struct pdb_search);
2391 if (search == NULL) {
2393 talloc_free(tframe);
2397 if (!methods->search_users(methods, search, acct_flags)) {
2398 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
2399 NT_STATUS_V(status),
2400 get_friendly_nt_error_msg(status));
2401 talloc_free(tframe);
2405 entry = talloc_zero(tframe, struct samr_displayentry);
2406 if (entry == NULL) {
2408 talloc_free(tframe);
2412 py_userlist = PyList_New(0);
2413 if (py_userlist == NULL) {
2415 talloc_free(tframe);
2419 while (search->next_entry(search, entry)) {
2420 py_dict = PyDict_New();
2421 if (py_dict == NULL) {
2424 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2425 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2426 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2427 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2428 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2429 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2430 PyList_Append(py_userlist, py_dict);
2433 search->search_end(search);
2435 talloc_free(tframe);
2441 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2444 struct pdb_methods *methods;
2446 struct pdb_search *search;
2447 struct samr_displayentry *entry;
2448 PyObject *py_grouplist, *py_dict;
2450 methods = pytalloc_get_ptr(self);
2452 if ((tframe = talloc_stackframe()) == NULL) {
2457 search = talloc_zero(tframe, struct pdb_search);
2458 if (search == NULL) {
2460 talloc_free(tframe);
2464 if (!methods->search_groups(methods, search)) {
2465 PyErr_Format(py_pdb_error, "Unable to search groups, (%d,%s)",
2466 NT_STATUS_V(status),
2467 get_friendly_nt_error_msg(status));
2468 talloc_free(tframe);
2472 entry = talloc_zero(tframe, struct samr_displayentry);
2473 if (entry == NULL) {
2475 talloc_free(tframe);
2479 py_grouplist = PyList_New(0);
2480 if (py_grouplist == NULL) {
2482 talloc_free(tframe);
2486 while (search->next_entry(search, entry)) {
2487 py_dict = PyDict_New();
2488 if (py_dict == NULL) {
2491 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2492 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2493 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2494 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2495 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2496 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2497 PyList_Append(py_grouplist, py_dict);
2500 search->search_end(search);
2502 talloc_free(tframe);
2504 return py_grouplist;
2508 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2510 struct pdb_methods *methods;
2512 struct pdb_search *search;
2513 struct samr_displayentry *entry;
2514 PyObject *py_aliaslist, *py_dict;
2515 PyObject *py_domain_sid;
2516 struct dom_sid *domain_sid = NULL;
2518 py_domain_sid = Py_None;
2521 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2525 methods = pytalloc_get_ptr(self);
2527 if ((tframe = talloc_stackframe()) == NULL) {
2532 if (py_domain_sid != Py_None) {
2533 domain_sid = pytalloc_get_ptr(py_domain_sid);
2536 search = talloc_zero(tframe, struct pdb_search);
2537 if (search == NULL) {
2539 talloc_free(tframe);
2543 if (!methods->search_aliases(methods, search, domain_sid)) {
2544 PyErr_Format(py_pdb_error, "Unable to search aliases");
2545 talloc_free(tframe);
2549 entry = talloc_zero(tframe, struct samr_displayentry);
2550 if (entry == NULL) {
2552 talloc_free(tframe);
2556 py_aliaslist = PyList_New(0);
2557 if (py_aliaslist == NULL) {
2559 talloc_free(tframe);
2563 while (search->next_entry(search, entry)) {
2564 py_dict = PyDict_New();
2565 if (py_dict == NULL) {
2568 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2569 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2570 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2571 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2572 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2573 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2574 PyList_Append(py_aliaslist, py_dict);
2577 search->search_end(search);
2579 talloc_free(tframe);
2581 return py_aliaslist;
2585 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2587 struct pdb_methods *methods;
2590 struct dom_sid user_sid, *copy_user_sid;
2591 PyObject *py_user_sid;
2593 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2597 methods = pytalloc_get_ptr(self);
2599 if ((tframe = talloc_stackframe()) == NULL) {
2604 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2605 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2606 talloc_free(tframe);
2610 copy_user_sid = dom_sid_dup(tframe, &user_sid);
2611 if (copy_user_sid == NULL) {
2613 talloc_free(tframe);
2617 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2619 talloc_free(tframe);
2625 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2627 struct pdb_methods *methods;
2630 struct dom_sid group_sid, *copy_group_sid;
2631 PyObject *py_group_sid;
2633 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2637 methods = pytalloc_get_ptr(self);
2639 if ((tframe = talloc_stackframe()) == NULL) {
2644 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2645 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2646 talloc_free(tframe);
2650 copy_group_sid = dom_sid_dup(tframe, &group_sid);
2651 if (copy_group_sid == NULL) {
2653 talloc_free(tframe);
2657 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2659 talloc_free(tframe);
2661 return py_group_sid;
2665 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2667 struct pdb_methods *methods;
2670 struct dom_sid *sid;
2673 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2677 methods = pytalloc_get_ptr(self);
2679 if ((tframe = talloc_stackframe()) == NULL) {
2684 sid = pytalloc_get_ptr(py_sid);
2686 if (!methods->sid_to_id(methods, sid, &id)) {
2687 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2688 talloc_free(tframe);
2692 talloc_free(tframe);
2694 return Py_BuildValue("(II)", id.id, id.type);
2698 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2700 struct pdb_methods *methods;
2704 methods = pytalloc_get_ptr(self);
2706 if ((tframe = talloc_stackframe()) == NULL) {
2711 if (!methods->new_rid(methods, &rid)) {
2712 PyErr_Format(py_pdb_error, "Unable to get new rid");
2713 talloc_free(tframe);
2717 talloc_free(tframe);
2719 return PyInt_FromLong(rid);
2723 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2725 struct pdb_methods *methods;
2729 struct dom_sid sid, *copy_sid;
2731 time_t last_set_time;
2734 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2738 methods = pytalloc_get_ptr(self);
2740 if ((tframe = talloc_stackframe()) == NULL) {
2745 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2746 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2747 talloc_free(tframe);
2751 copy_sid = dom_sid_dup(tframe, &sid);
2752 if (copy_sid == NULL) {
2754 talloc_free(tframe);
2758 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2759 if (py_sid == NULL) {
2761 talloc_free(tframe);
2765 talloc_free(tframe);
2767 py_value = PyDict_New();
2768 if (py_value == NULL) {
2773 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2774 PyDict_SetItemString(py_value, "sid", py_sid);
2775 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2781 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2783 struct pdb_methods *methods;
2787 const struct dom_sid *domain_sid;
2788 PyObject *py_domain_sid;
2790 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2791 dom_sid_Type, &py_domain_sid)) {
2795 methods = pytalloc_get_ptr(self);
2797 if ((tframe = talloc_stackframe()) == NULL) {
2802 domain_sid = pytalloc_get_ptr(py_domain_sid);
2804 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2805 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2806 talloc_free(tframe);
2814 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2816 struct pdb_methods *methods;
2820 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2824 methods = pytalloc_get_ptr(self);
2826 if ((tframe = talloc_stackframe()) == NULL) {
2831 if (!methods->del_trusteddom_pw(methods, domain)) {
2832 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2833 talloc_free(tframe);
2841 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2844 struct pdb_methods *methods;
2846 uint32_t num_domains;
2847 struct trustdom_info **domains;
2848 PyObject *py_domain_list, *py_dict;
2851 methods = pytalloc_get_ptr(self);
2853 if ((tframe = talloc_stackframe()) == NULL) {
2858 status = methods->enum_trusteddoms(methods, tframe, &num_domains, &domains);
2859 if (!NT_STATUS_IS_OK(status)) {
2860 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2861 NT_STATUS_V(status),
2862 get_friendly_nt_error_msg(status));
2863 talloc_free(tframe);
2867 py_domain_list = PyList_New(0);
2868 if (py_domain_list == NULL) {
2870 talloc_free(tframe);
2874 for(i=0; i<num_domains; i++) {
2875 py_dict = PyDict_New();
2877 PyDict_SetItemString(py_dict, "name",
2878 PyString_FromString(domains[i]->name));
2879 PyDict_SetItemString(py_dict, "sid",
2880 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2883 PyList_Append(py_domain_list, py_dict);
2886 talloc_free(tframe);
2888 return py_domain_list;
2892 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2895 struct pdb_methods *methods;
2898 struct pdb_trusted_domain *td;
2899 PyObject *py_domain_info;
2901 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2905 methods = pytalloc_get_ptr(self);
2907 if ((tframe = talloc_stackframe()) == NULL) {
2912 status = methods->get_trusted_domain(methods, tframe, domain, &td);
2913 if (!NT_STATUS_IS_OK(status)) {
2914 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2915 NT_STATUS_V(status),
2916 get_friendly_nt_error_msg(status));
2917 talloc_free(tframe);
2921 py_domain_info = PyDict_New();
2922 if (py_domain_info == NULL) {
2924 talloc_free(tframe);
2928 PyDict_SetItemString(py_domain_info, "domain_name",
2929 PyString_FromString(td->domain_name));
2930 PyDict_SetItemString(py_domain_info, "netbios_name",
2931 PyString_FromString(td->netbios_name));
2932 PyDict_SetItemString(py_domain_info, "security_identifier",
2933 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2934 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2935 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2936 td->trust_auth_incoming.length));
2937 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2938 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2939 td->trust_auth_outgoing.length));
2940 PyDict_SetItemString(py_domain_info, "trust_direction",
2941 PyInt_FromLong(td->trust_direction));
2942 PyDict_SetItemString(py_domain_info, "trust_type",
2943 PyInt_FromLong(td->trust_type));
2944 PyDict_SetItemString(py_domain_info, "trust_attributes",
2945 PyInt_FromLong(td->trust_attributes));
2946 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2947 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2948 td->trust_forest_trust_info.length));
2950 talloc_free(tframe);
2952 return py_domain_info;
2956 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
2959 struct pdb_methods *methods;
2961 PyObject *py_domain_sid;
2962 struct dom_sid *domain_sid;
2963 struct pdb_trusted_domain *td;
2964 PyObject *py_domain_info;
2966 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
2970 methods = pytalloc_get_ptr(self);
2972 if ((tframe = talloc_stackframe()) == NULL) {
2977 domain_sid = pytalloc_get_ptr(py_domain_sid);
2979 status = methods->get_trusted_domain_by_sid(methods, tframe, domain_sid, &td);
2980 if (!NT_STATUS_IS_OK(status)) {
2981 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2982 NT_STATUS_V(status),
2983 get_friendly_nt_error_msg(status));
2984 talloc_free(tframe);
2988 py_domain_info = PyDict_New();
2989 if (py_domain_info == NULL) {
2991 talloc_free(tframe);
2995 PyDict_SetItemString(py_domain_info, "domain_name",
2996 PyString_FromString(td->domain_name));
2997 PyDict_SetItemString(py_domain_info, "netbios_name",
2998 PyString_FromString(td->netbios_name));
2999 PyDict_SetItemString(py_domain_info, "security_identifier",
3000 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3001 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3002 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3003 td->trust_auth_incoming.length));
3004 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3005 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3006 td->trust_auth_outgoing.length));
3007 PyDict_SetItemString(py_domain_info, "trust_direction",
3008 PyInt_FromLong(td->trust_direction));
3009 PyDict_SetItemString(py_domain_info, "trust_type",
3010 PyInt_FromLong(td->trust_type));
3011 PyDict_SetItemString(py_domain_info, "trust_attributes",
3012 PyInt_FromLong(td->trust_attributes));
3013 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3014 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3015 td->trust_forest_trust_info.length));
3017 talloc_free(tframe);
3019 return py_domain_info;
3023 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3026 struct pdb_methods *methods;
3029 PyObject *py_td_info;
3030 struct pdb_trusted_domain td_info;
3034 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3038 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3039 td_info.domain_name = PyString_AsString(py_tmp);
3041 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3042 td_info.netbios_name = PyString_AsString(py_tmp);
3044 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3045 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3047 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3048 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3049 td_info.trust_auth_incoming.length = len;
3051 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3052 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3053 td_info.trust_auth_outgoing.length = len;
3055 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3056 td_info.trust_direction = PyInt_AsLong(py_tmp);
3058 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3059 td_info.trust_type = PyInt_AsLong(py_tmp);
3061 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3062 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3064 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3065 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3066 td_info.trust_forest_trust_info.length = len;
3068 methods = pytalloc_get_ptr(self);
3070 if ((tframe = talloc_stackframe()) == NULL) {
3075 status = methods->set_trusted_domain(methods, domain, &td_info);
3076 if (!NT_STATUS_IS_OK(status)) {
3077 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3078 NT_STATUS_V(status),
3079 get_friendly_nt_error_msg(status));
3080 talloc_free(tframe);
3084 talloc_free(tframe);
3090 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3093 struct pdb_methods *methods;
3097 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3101 methods = pytalloc_get_ptr(self);
3103 if ((tframe = talloc_stackframe()) == NULL) {
3108 status = methods->del_trusted_domain(methods, domain);
3109 if (!NT_STATUS_IS_OK(status)) {
3110 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3111 NT_STATUS_V(status),
3112 get_friendly_nt_error_msg(status));
3113 talloc_free(tframe);
3117 talloc_free(tframe);
3123 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3126 struct pdb_methods *methods;
3128 uint32_t num_domains;
3129 struct pdb_trusted_domain **td_info, *td;
3130 PyObject *py_td_info, *py_domain_info;
3133 methods = pytalloc_get_ptr(self);
3135 if ((tframe = talloc_stackframe()) == NULL) {
3140 status = methods->enum_trusted_domains(methods, tframe, &num_domains, &td_info);
3141 if (!NT_STATUS_IS_OK(status)) {
3142 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3143 NT_STATUS_V(status),
3144 get_friendly_nt_error_msg(status));
3145 talloc_free(tframe);
3149 py_td_info = PyList_New(0);
3150 if (py_td_info == NULL) {
3152 talloc_free(tframe);
3156 for (i=0; i<num_domains; i++) {
3158 py_domain_info = PyDict_New();
3159 if (py_domain_info == NULL) {
3161 Py_DECREF(py_td_info);
3162 talloc_free(tframe);
3168 PyDict_SetItemString(py_domain_info, "domain_name",
3169 PyString_FromString(td->domain_name));
3170 PyDict_SetItemString(py_domain_info, "netbios_name",
3171 PyString_FromString(td->netbios_name));
3172 PyDict_SetItemString(py_domain_info, "security_identifier",
3173 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3174 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3175 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3176 td->trust_auth_incoming.length));
3177 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3178 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3179 td->trust_auth_outgoing.length));
3180 PyDict_SetItemString(py_domain_info, "trust_direction",
3181 PyInt_FromLong(td->trust_direction));
3182 PyDict_SetItemString(py_domain_info, "trust_type",
3183 PyInt_FromLong(td->trust_type));
3184 PyDict_SetItemString(py_domain_info, "trust_attributes",
3185 PyInt_FromLong(td->trust_attributes));
3186 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3187 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3188 td->trust_forest_trust_info.length));
3189 PyList_Append(py_td_info, py_domain_info);
3192 talloc_free(tframe);
3198 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3201 struct pdb_methods *methods;
3203 const char *secret_name;
3204 DATA_BLOB secret_current, secret_old;
3205 NTTIME secret_current_lastchange, secret_old_lastchange;
3207 struct security_descriptor *sd;
3208 PyObject *py_secret;
3210 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3214 methods = pytalloc_get_ptr(self);
3216 if ((tframe = talloc_stackframe()) == NULL) {
3221 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3222 if (py_sd == NULL) {
3224 talloc_free(tframe);
3227 sd = pytalloc_get_ptr(py_sd);
3229 status = methods->get_secret(methods, tframe, secret_name,
3231 &secret_current_lastchange,
3233 &secret_old_lastchange,
3235 if (!NT_STATUS_IS_OK(status)) {
3236 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3238 NT_STATUS_V(status),
3239 get_friendly_nt_error_msg(status));
3240 talloc_free(tframe);
3244 py_secret = PyDict_New();
3245 if (py_secret == NULL) {
3248 talloc_free(tframe);
3252 PyDict_SetItemString(py_secret, "secret_current",
3253 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3254 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3255 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3256 PyDict_SetItemString(py_secret, "secret_old",
3257 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3258 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3259 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3260 PyDict_SetItemString(py_secret, "sd", py_sd);
3262 talloc_free(tframe);
3268 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3271 struct pdb_methods *methods;
3273 const char *secret_name;
3274 PyObject *py_secret;
3275 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3276 DATA_BLOB secret_current, secret_old;
3277 struct security_descriptor *sd;
3280 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3284 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3285 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3286 py_sd = PyDict_GetItemString(py_secret, "sd");
3288 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3289 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3290 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3292 methods = pytalloc_get_ptr(self);
3294 if ((tframe = talloc_stackframe()) == NULL) {
3299 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3300 secret_current.length = len;
3301 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3302 secret_current.length = len;
3303 sd = pytalloc_get_ptr(py_sd);
3305 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3306 if (!NT_STATUS_IS_OK(status)) {
3307 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3309 NT_STATUS_V(status),
3310 get_friendly_nt_error_msg(status));
3311 talloc_free(tframe);
3315 talloc_free(tframe);
3321 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3324 struct pdb_methods *methods;
3326 const char *secret_name;
3328 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3332 methods = pytalloc_get_ptr(self);
3334 if ((tframe = talloc_stackframe()) == NULL) {
3339 status = methods->delete_secret(methods, secret_name);
3340 if (!NT_STATUS_IS_OK(status)) {
3341 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3343 NT_STATUS_V(status),
3344 get_friendly_nt_error_msg(status));
3345 talloc_free(tframe);
3349 talloc_free(tframe);
3354 static PyMethodDef py_pdb_methods[] = {
3355 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3356 "domain_info() -> str\n\n \
3357 Get domain information for the database." },
3358 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3359 "getsampwnam(username) -> samu object\n\n \
3360 Get user information by name." },
3361 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3362 "getsampwsid(user_sid) -> samu object\n\n \
3363 Get user information by sid (dcerpc.security.dom_sid object)." },
3364 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3365 "create_user(username, acct_flags) -> rid\n\n \
3366 Create user. acct_flags are samr account control flags." },
3367 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3368 "delete_user(samu object) -> None\n\n \
3370 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3371 "add_sam_account(samu object) -> None\n\n \
3372 Add SAM account." },
3373 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3374 "update_sam_account(samu object) -> None\n\n \
3375 Update SAM account." },
3376 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3377 "delete_sam_account(samu object) -> None\n\n \
3378 Delete SAM account." },
3379 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3380 "rename_sam_account(samu object1, new_username) -> None\n\n \
3381 Rename SAM account." },
3382 /* update_login_attempts */
3383 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3384 "getgrsid(group_sid) -> groupmap object\n\n \
3385 Get group information by sid (dcerpc.security.dom_sid object)." },
3386 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3387 "getgrsid(gid) -> groupmap object\n\n \
3388 Get group information by gid." },
3389 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3390 "getgrsid(groupname) -> groupmap object\n\n \
3391 Get group information by name." },
3392 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3393 "create_dom_group(groupname) -> group_rid\n\n \
3394 Create new domain group by name." },
3395 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3396 "delete_dom_group(group_rid) -> None\n\n \
3397 Delete domain group identified by rid" },
3398 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3399 "add_group_mapping_entry(groupmap) -> None\n \
3400 Add group mapping entry for groupmap object." },
3401 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3402 "update_group_mapping_entry(groupmap) -> None\n\n \
3403 Update group mapping entry for groupmap object." },
3404 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3405 "delete_group_mapping_entry(groupmap) -> None\n\n \
3406 Delete group mapping entry for groupmap object." },
3407 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3408 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3409 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3410 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3411 "enum_group_members(group_sid) -> List\n\n \
3412 Return list of users (dom_sid object) in group." },
3413 /* enum_group_memberships */
3414 /* set_unix_primary_group */
3415 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3416 "add_groupmem(group_rid, member_rid) -> None\n\n \
3417 Add user to group." },
3418 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3419 "del_groupmem(group_rid, member_rid) -> None\n\n \
3420 Remove user from from group." },
3421 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3422 "create_alias(alias_name) -> alias_rid\n\n \
3423 Create alias entry." },
3424 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3425 "delete_alias(alias_sid) -> None\n\n \
3426 Delete alias entry." },
3427 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3428 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3429 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3430 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3431 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3432 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3433 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3434 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3435 Add user to alias entry." },
3436 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3437 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3438 Remove a user from alias entry." },
3439 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3440 "enum_aliasmem(alias_sid) -> List\n\n \
3441 Return a list of members (dom_sid object) for alias entry." },
3442 /* enum_alias_memberships */
3445 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3446 "get_account_policy() -> Mapping\n\n \
3447 Get account policy information as a dictionary." },
3448 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3449 "get_account_policy(Mapping) -> None\n\n \
3450 Set account policy settings from a dicionary." },
3452 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3453 "search_users(acct_flags) -> List\n\n \
3454 Search users. acct_flags are samr account control flags.\n \
3455 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3456 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3457 "search_groups() -> List\n\n \
3458 Search unix only groups. \n \
3459 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3460 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3461 "search_aliases([domain_sid]) -> List\n\n \
3462 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3463 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3464 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3465 "uid_to_sid(uid) -> sid\n\n \
3466 Return sid for given user id." },
3467 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3468 "gid_to_sid(gid) -> sid\n\n \
3469 Return sid for given group id." },
3470 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3471 "sid_to_id(sid) -> Tuple\n\n \
3472 Return id and type for given sid." },
3474 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3475 "new_rid() -> rid\n\n \
3477 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3478 "get_trusteddom_pw(domain) -> Mapping\n\n \
3479 Get trusted domain password, sid and last set time in a dictionary." },
3480 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3481 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3482 Set trusted domain password." },
3483 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3484 "del_trusteddom_pw(domain) -> None\n\n \
3485 Delete trusted domain password." },
3486 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3487 "enum_trusteddoms() -> List\n\n \
3488 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3489 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3490 "get_trusted_domain(domain) -> Mapping\n\n \
3491 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." },
3492 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3493 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3494 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" },
3495 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3496 "set_trusted_domain(domain, Mapping) -> None\n\n \
3497 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." },
3498 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3499 "del_trusted_domain(domain) -> None\n\n \
3500 Delete trusted domain." },
3501 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3502 "enum_trusted_domains() -> List\n\n \
3503 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." },
3504 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3505 "get_secret(secret_name) -> Mapping\n\n \
3506 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3507 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3508 "set_secret(secret_name, Mapping) -> None\n\n \
3509 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3510 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3511 "delete_secret(secret_name) -> None\n\n \
3512 Delete secret information for secret_name." },
3517 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3519 const char *url = NULL;
3522 struct pdb_methods *methods;
3524 if (!PyArg_ParseTuple(args, "s", &url)) {
3528 /* Initalize list of methods */
3529 status = make_pdb_method_name(&methods, url);
3530 if (!NT_STATUS_IS_OK(status)) {
3531 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3533 NT_STATUS_V(status),
3534 get_friendly_nt_error_msg(status));
3538 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3547 static PyTypeObject PyPDB = {
3548 .tp_name = "passdb.PDB",
3549 .tp_basicsize = sizeof(pytalloc_Object),
3550 .tp_new = py_pdb_new,
3551 .tp_flags = Py_TPFLAGS_DEFAULT,
3552 .tp_methods = py_pdb_methods,
3553 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3558 * Return a list of passdb backends
3560 static PyObject *py_passdb_backends(PyObject *self)
3563 const struct pdb_init_function_entry *entry;
3566 if ((tframe = talloc_stackframe()) == NULL) {
3571 entry = pdb_get_backends();
3576 if((py_blist = PyList_New(0)) == NULL) {
3582 PyList_Append(py_blist, PyString_FromString(entry->name));
3583 entry = entry->next;
3586 talloc_free(tframe);
3592 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3594 const char *smb_config;
3597 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3601 if ((tframe = talloc_stackframe()) == NULL) {
3606 /* Load smbconf parameters */
3607 if (!lp_load_global(smb_config)) {
3608 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3612 talloc_free(tframe);
3618 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3620 const char *private_dir;
3623 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3627 if ((tframe = talloc_stackframe()) == NULL) {
3632 /* Initialize secrets database */
3633 if (!secrets_init_path(private_dir)) {
3634 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3639 talloc_free(tframe);
3644 static PyObject *py_get_global_sam_sid(PyObject *self)
3646 struct dom_sid *domain_sid, *domain_sid_copy;
3648 PyObject *py_dom_sid;
3650 tframe = talloc_stackframe();
3651 if (tframe == NULL) {
3656 domain_sid = get_global_sam_sid();
3658 domain_sid_copy = dom_sid_dup(tframe, domain_sid);
3659 if (domain_sid_copy == NULL) {
3661 talloc_free(tframe);
3665 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3667 talloc_free(tframe);
3673 static PyMethodDef py_passdb_methods[] = {
3674 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3675 "get_backends() -> list\n\n \
3676 Get a list of password database backends supported." },
3677 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3678 "set_smb_config(path) -> None\n\n \
3679 Set path to smb.conf file to load configuration parameters." },
3680 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3681 "set_secrets_dir(private_dir) -> None\n\n \
3682 Set path to private directory to load secrets database from non-default location." },
3683 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3684 "get_global_sam_sid() -> dom_sid\n\n \
3685 Return domain SID." },
3689 void initpassdb(void)
3692 char exception_name[] = "passdb.error";
3694 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3695 if (talloc_type == NULL) {
3699 PyPDB.tp_base = talloc_type;
3700 if (PyType_Ready(&PyPDB) < 0) {
3704 PySamu.tp_base = talloc_type;
3705 if (PyType_Ready(&PySamu) < 0) {
3709 PyGroupmap.tp_base = talloc_type;
3710 if (PyType_Ready(&PyGroupmap) < 0) {
3714 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3719 /* Create new exception for passdb module */
3720 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3721 Py_INCREF(py_pdb_error);
3722 PyModule_AddObject(m, "error", py_pdb_error);
3725 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3728 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3730 Py_INCREF(&PyGroupmap);
3731 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3733 /* Import dom_sid type from dcerpc.security */
3734 mod = PyImport_ImportModule("samba.dcerpc.security");
3739 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3740 if (dom_sid_Type == NULL) {
3744 /* Import security_descriptor type from dcerpc.security */
3745 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3747 if (security_Type == NULL) {
3751 /* Import GUID type from dcerpc.misc */
3752 mod = PyImport_ImportModule("samba.dcerpc.misc");
3757 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3759 if (guid_Type == NULL) {