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"
30 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
31 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
35 #define PY_CHECK_TYPE(type, var, fail) \
36 if (!PyObject_TypeCheck(var, type)) {\
37 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
43 static PyTypeObject *dom_sid_Type = NULL;
44 static PyTypeObject *security_Type = NULL;
45 static PyTypeObject *guid_Type = NULL;
47 static PyTypeObject PySamu;
48 static PyTypeObject PyGroupmap;
49 static PyTypeObject PyPDB;
51 static PyObject *py_pdb_error;
53 void initpassdb(void);
56 /************************** PIDL Autogeneratd ******************************/
58 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
60 TALLOC_CTX *frame = talloc_stackframe();
61 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
62 PyObject *py_logon_time;
64 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
69 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
71 TALLOC_CTX *frame = talloc_stackframe();
72 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
74 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
75 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
83 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
85 TALLOC_CTX *frame = talloc_stackframe();
86 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
87 PyObject *py_logoff_time;
89 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
91 return py_logoff_time;
94 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
96 TALLOC_CTX *frame = talloc_stackframe();
97 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
99 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
100 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 TALLOC_CTX *frame = talloc_stackframe();
111 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
112 PyObject *py_kickoff_time;
114 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
116 return py_kickoff_time;
119 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
121 TALLOC_CTX *frame = talloc_stackframe();
122 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
124 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
125 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
133 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
135 TALLOC_CTX *frame = talloc_stackframe();
136 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
137 PyObject *py_bad_password_time;
139 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
141 return py_bad_password_time;
144 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
146 TALLOC_CTX *frame = talloc_stackframe();
147 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
149 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
150 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
158 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
160 TALLOC_CTX *frame = talloc_stackframe();
161 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
162 PyObject *py_pass_last_set_time;
164 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
166 return py_pass_last_set_time;
169 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
171 TALLOC_CTX *frame = talloc_stackframe();
172 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
174 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
175 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
183 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
185 TALLOC_CTX *frame = talloc_stackframe();
186 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
187 PyObject *py_pass_can_change_time;
189 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
191 return py_pass_can_change_time;
194 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
196 TALLOC_CTX *frame = talloc_stackframe();
197 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
199 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
200 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
208 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
210 TALLOC_CTX *frame = talloc_stackframe();
211 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
212 PyObject *py_pass_must_change_time;
214 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
216 return py_pass_must_change_time;
219 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
221 TALLOC_CTX *frame = talloc_stackframe();
222 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
224 /* TODO: make this not a get/set or give a better exception */
229 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
231 TALLOC_CTX *frame = talloc_stackframe();
232 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
233 PyObject *py_username;
234 const char *username;
236 username = pdb_get_username(sam_acct);
237 if (username == NULL) {
241 py_username = PyString_FromString(username);
246 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
248 TALLOC_CTX *frame = talloc_stackframe();
249 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
251 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
252 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
260 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
262 TALLOC_CTX *frame = talloc_stackframe();
263 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
267 domain = pdb_get_domain(sam_acct);
268 if (domain == NULL) {
272 py_domain = PyString_FromString(domain);
277 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
279 TALLOC_CTX *frame = talloc_stackframe();
280 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
282 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
283 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
291 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
293 TALLOC_CTX *frame = talloc_stackframe();
294 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
295 PyObject *py_nt_username;
296 const char *nt_username;
298 nt_username = pdb_get_nt_username(sam_acct);
299 if (nt_username == NULL) {
303 py_nt_username = PyString_FromString(nt_username);
305 return py_nt_username;
308 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
310 TALLOC_CTX *frame = talloc_stackframe();
311 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
313 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
314 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
322 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
324 TALLOC_CTX *frame = talloc_stackframe();
325 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
326 PyObject *py_full_name;
327 const char *full_name;
329 full_name = pdb_get_fullname(sam_acct);
330 if (full_name == NULL) {
334 py_full_name = PyString_FromString(full_name);
339 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
341 TALLOC_CTX *frame = talloc_stackframe();
342 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
344 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
345 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
353 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
355 TALLOC_CTX *frame = talloc_stackframe();
356 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
357 PyObject *py_home_dir;
358 const char *home_dir;
360 home_dir = pdb_get_homedir(sam_acct);
361 if (home_dir == NULL) {
365 py_home_dir = PyString_FromString(home_dir);
370 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
372 TALLOC_CTX *frame = talloc_stackframe();
373 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
375 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
376 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
384 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
386 TALLOC_CTX *frame = talloc_stackframe();
387 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
388 PyObject *py_dir_drive;
389 const char *dir_drive;
391 dir_drive = pdb_get_dir_drive(sam_acct);
392 if (dir_drive == NULL) {
396 py_dir_drive = PyString_FromString(dir_drive);
401 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
403 TALLOC_CTX *frame = talloc_stackframe();
404 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
406 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
407 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
415 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
417 TALLOC_CTX *frame = talloc_stackframe();
418 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
419 PyObject *py_logon_script;
420 const char *logon_script;
422 logon_script = pdb_get_logon_script(sam_acct);
423 if (logon_script == NULL) {
427 py_logon_script = PyString_FromString(logon_script);
429 return py_logon_script;
432 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
434 TALLOC_CTX *frame = talloc_stackframe();
435 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
437 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
438 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
446 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
448 TALLOC_CTX *frame = talloc_stackframe();
449 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
450 PyObject *py_profile_path;
451 const char *profile_path;
453 profile_path = pdb_get_profile_path(sam_acct);
454 if (profile_path == NULL) {
458 py_profile_path = PyString_FromString(profile_path);
460 return py_profile_path;
463 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
465 TALLOC_CTX *frame = talloc_stackframe();
466 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
468 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
469 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
477 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
479 TALLOC_CTX *frame = talloc_stackframe();
480 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
481 PyObject *py_acct_desc;
482 const char *acct_desc;
484 acct_desc = pdb_get_acct_desc(sam_acct);
485 if (acct_desc == NULL) {
489 py_acct_desc = PyString_FromString(acct_desc);
494 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
496 TALLOC_CTX *frame = talloc_stackframe();
497 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
499 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
500 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
508 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
510 TALLOC_CTX *frame = talloc_stackframe();
511 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
512 PyObject *py_workstations;
513 const char *workstations;
515 workstations = pdb_get_workstations(sam_acct);
516 if (workstations == NULL) {
520 py_workstations = PyString_FromString(workstations);
522 return py_workstations;
525 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
527 TALLOC_CTX *frame = talloc_stackframe();
528 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
530 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
531 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
539 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
541 TALLOC_CTX *frame = talloc_stackframe();
542 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
543 PyObject *py_comment;
546 comment = pdb_get_comment(sam_acct);
547 if (comment == NULL) {
551 py_comment = PyString_FromString(comment);
556 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
558 TALLOC_CTX *frame = talloc_stackframe();
559 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
561 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
562 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
570 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
572 TALLOC_CTX *frame = talloc_stackframe();
573 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
574 PyObject *py_munged_dial;
575 const char *munged_dial;
577 munged_dial = pdb_get_munged_dial(sam_acct);
578 if (munged_dial == NULL) {
582 py_munged_dial = PyString_FromString(munged_dial);
584 return py_munged_dial;
587 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
589 TALLOC_CTX *frame = talloc_stackframe();
590 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
592 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
593 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
601 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
603 TALLOC_CTX *frame = talloc_stackframe();
604 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
605 PyObject *py_user_sid;
606 const struct dom_sid *user_sid;
607 struct dom_sid *copy_user_sid;
610 user_sid = pdb_get_user_sid(sam_acct);
611 if(user_sid == NULL) {
615 mem_ctx = talloc_new(NULL);
616 if (mem_ctx == NULL) {
621 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
622 if (copy_user_sid == NULL) {
624 talloc_free(mem_ctx);
629 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
631 talloc_free(mem_ctx);
637 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
639 TALLOC_CTX *frame = talloc_stackframe();
640 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
642 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
643 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
651 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
653 TALLOC_CTX *frame = talloc_stackframe();
654 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
655 const struct dom_sid *group_sid;
656 struct dom_sid *copy_group_sid;
658 group_sid = pdb_get_group_sid(sam_acct);
659 if (group_sid == NULL) {
663 copy_group_sid = dom_sid_dup(NULL, group_sid);
664 if (copy_group_sid == NULL) {
671 return pytalloc_steal(dom_sid_Type, copy_group_sid);
674 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
676 TALLOC_CTX *frame = talloc_stackframe();
677 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
679 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
680 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
688 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
690 TALLOC_CTX *frame = talloc_stackframe();
691 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
695 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
700 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
705 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
707 TALLOC_CTX *frame = talloc_stackframe();
708 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
710 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
711 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
719 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
721 TALLOC_CTX *frame = talloc_stackframe();
722 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
726 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
731 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
736 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
738 TALLOC_CTX *frame = talloc_stackframe();
739 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
741 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
749 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
751 TALLOC_CTX *frame = talloc_stackframe();
752 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
753 PyObject *py_nt_pw_his;
754 const char *nt_pw_his;
757 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
758 if (nt_pw_his == NULL) {
762 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
767 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
769 TALLOC_CTX *frame = talloc_stackframe();
770 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
775 PyString_AsStringAndSize(value, &nt_pw_his, &len);
776 hist_len = len / PW_HISTORY_ENTRY_LEN;
777 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
785 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
787 TALLOC_CTX *frame = talloc_stackframe();
788 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
789 PyObject *py_plaintext_pw;
790 const char *plaintext_pw;
792 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
793 if (plaintext_pw == NULL) {
797 py_plaintext_pw = PyString_FromString(plaintext_pw);
799 return py_plaintext_pw;
802 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
804 TALLOC_CTX *frame = talloc_stackframe();
805 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
807 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
815 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
817 TALLOC_CTX *frame = talloc_stackframe();
818 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
819 PyObject *py_acct_ctrl;
821 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
826 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
828 TALLOC_CTX *frame = talloc_stackframe();
829 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
831 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
832 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
840 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
842 TALLOC_CTX *frame = talloc_stackframe();
843 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
844 PyObject *py_logon_divs;
846 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
848 return py_logon_divs;
851 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
853 TALLOC_CTX *frame = talloc_stackframe();
854 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
856 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
857 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
865 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
867 TALLOC_CTX *frame = talloc_stackframe();
868 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
869 PyObject *py_hours_len;
871 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
876 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
878 TALLOC_CTX *frame = talloc_stackframe();
879 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
881 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
882 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
890 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
892 TALLOC_CTX *frame = talloc_stackframe();
893 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
898 hours = (const char *)pdb_get_hours(sam_acct);
903 hours_len = pdb_get_hours_len(sam_acct);
904 if ((py_hours = PyList_New(hours_len)) == NULL) {
910 for (i=0; i<hours_len; i++) {
911 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
917 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
919 TALLOC_CTX *frame = talloc_stackframe();
920 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
926 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
928 hours_len = PyList_GET_SIZE(value);
930 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
937 for (i=0; i < hours_len; i++) {
938 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
939 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
942 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
953 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
955 TALLOC_CTX *frame = talloc_stackframe();
956 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
957 PyObject *py_bad_password_count;
959 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
961 return py_bad_password_count;
964 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
966 TALLOC_CTX *frame = talloc_stackframe();
967 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
969 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
970 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
978 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
980 TALLOC_CTX *frame = talloc_stackframe();
981 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
982 PyObject *py_logon_count;
984 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
986 return py_logon_count;
989 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
991 TALLOC_CTX *frame = talloc_stackframe();
992 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
994 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
995 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1003 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
1005 TALLOC_CTX *frame = talloc_stackframe();
1006 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1007 PyObject *py_country_code;
1009 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
1011 return py_country_code;
1014 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
1016 TALLOC_CTX *frame = talloc_stackframe();
1017 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1019 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1020 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1028 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
1030 TALLOC_CTX *frame = talloc_stackframe();
1031 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1032 PyObject *py_code_page;
1034 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
1036 return py_code_page;
1039 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
1041 TALLOC_CTX *frame = talloc_stackframe();
1042 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1044 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1045 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1053 static PyGetSetDef py_samu_getsetters[] = {
1054 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
1055 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
1056 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
1057 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
1058 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
1059 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
1060 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
1061 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
1062 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
1063 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
1064 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
1065 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
1066 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
1067 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
1068 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
1069 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
1070 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
1071 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
1072 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
1073 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
1074 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
1075 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
1076 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
1077 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
1078 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
1079 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
1080 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
1081 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
1082 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
1083 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
1084 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
1085 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
1086 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
1091 /************************** PIDL Autogeneratd ******************************/
1093 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1095 TALLOC_CTX *frame = talloc_stackframe();
1096 struct samu *sam_acct;
1098 sam_acct = samu_new(NULL);
1106 return pytalloc_steal(type, sam_acct);
1109 static PyTypeObject PySamu = {
1110 .tp_name = "passdb.Samu",
1111 .tp_basicsize = sizeof(pytalloc_Object),
1112 .tp_getset = py_samu_getsetters,
1114 .tp_new = py_samu_new,
1115 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1116 .tp_doc = "Samu() -> samu object\n",
1120 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1122 TALLOC_CTX *frame = talloc_stackframe();
1123 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1126 py_gid = Py_BuildValue("i", group_map->gid);
1131 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1133 TALLOC_CTX *frame = talloc_stackframe();
1134 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1136 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1137 group_map->gid = PyInt_AsLong(value);
1142 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1144 TALLOC_CTX *frame = talloc_stackframe();
1145 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1147 struct dom_sid *group_sid;
1148 TALLOC_CTX *mem_ctx;
1150 mem_ctx = talloc_new(NULL);
1151 if (mem_ctx == NULL) {
1157 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1158 if (group_sid == NULL) {
1160 talloc_free(mem_ctx);
1165 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1167 talloc_free(mem_ctx);
1173 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1175 TALLOC_CTX *frame = talloc_stackframe();
1176 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1178 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1179 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1184 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1186 TALLOC_CTX *frame = talloc_stackframe();
1187 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1188 PyObject *py_sid_name_use;
1190 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1192 return py_sid_name_use;
1195 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1197 TALLOC_CTX *frame = talloc_stackframe();
1198 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1200 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1201 group_map->sid_name_use = PyInt_AsLong(value);
1206 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1208 TALLOC_CTX *frame = talloc_stackframe();
1209 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1210 PyObject *py_nt_name;
1211 if (group_map->nt_name == NULL) {
1212 py_nt_name = Py_None;
1213 Py_INCREF(py_nt_name);
1215 py_nt_name = PyString_FromString(group_map->nt_name);
1221 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1223 TALLOC_CTX *frame = talloc_stackframe();
1224 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1226 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1227 if (value == Py_None) {
1228 fstrcpy(group_map->nt_name, NULL);
1230 fstrcpy(group_map->nt_name, PyString_AsString(value));
1236 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1238 TALLOC_CTX *frame = talloc_stackframe();
1239 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1240 PyObject *py_comment;
1241 if (group_map->comment == NULL) {
1242 py_comment = Py_None;
1243 Py_INCREF(py_comment);
1245 py_comment = PyString_FromString(group_map->comment);
1251 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1253 TALLOC_CTX *frame = talloc_stackframe();
1254 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1256 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1257 if (value == Py_None) {
1258 fstrcpy(group_map->comment, NULL);
1260 fstrcpy(group_map->comment, PyString_AsString(value));
1266 static PyGetSetDef py_groupmap_getsetters[] = {
1267 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1268 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1269 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1270 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1271 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1275 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1277 TALLOC_CTX *frame = talloc_stackframe();
1278 GROUP_MAP *group_map;
1279 TALLOC_CTX *mem_ctx;
1280 PyObject *py_group_map;
1282 mem_ctx = talloc_new(NULL);
1283 if (mem_ctx == NULL) {
1289 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1290 if (group_map == NULL) {
1292 talloc_free(mem_ctx);
1297 py_group_map = pytalloc_steal(type, group_map);
1298 if (py_group_map == NULL) {
1300 talloc_free(mem_ctx);
1305 talloc_free(mem_ctx);
1308 return py_group_map;
1312 static PyTypeObject PyGroupmap = {
1313 .tp_name = "passdb.Groupmap",
1314 .tp_basicsize = sizeof(pytalloc_Object),
1315 .tp_getset = py_groupmap_getsetters,
1317 .tp_new = py_groupmap_new,
1318 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1319 .tp_doc = "Groupmap() -> group map object\n",
1323 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1325 TALLOC_CTX *frame = talloc_stackframe();
1326 struct pdb_methods *methods;
1327 struct pdb_domain_info *domain_info;
1328 PyObject *py_domain_info;
1329 struct dom_sid *sid;
1332 methods = pytalloc_get_ptr(self);
1334 domain_info = methods->get_domain_info(methods, frame);
1335 if (! domain_info) {
1339 sid = dom_sid_dup(frame, &domain_info->sid);
1346 guid = talloc(frame, struct GUID);
1352 *guid = domain_info->guid;
1354 if ((py_domain_info = PyDict_New()) == NULL) {
1360 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1361 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->dns_domain));
1362 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->dns_forest));
1363 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1364 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1367 return py_domain_info;
1371 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1373 TALLOC_CTX *frame = talloc_stackframe();
1375 const char *username;
1376 struct pdb_methods *methods;
1377 struct samu *sam_acct;
1378 PyObject *py_sam_acct;
1380 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1385 methods = pytalloc_get_ptr(self);
1387 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1388 if (py_sam_acct == NULL) {
1393 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1395 status = methods->getsampwnam(methods, sam_acct, username);
1396 if (!NT_STATUS_IS_OK(status)) {
1397 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1399 NT_STATUS_V(status),
1400 get_friendly_nt_error_msg(status));
1401 Py_DECREF(py_sam_acct);
1410 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1412 TALLOC_CTX *frame = talloc_stackframe();
1414 struct pdb_methods *methods;
1415 struct samu *sam_acct;
1416 PyObject *py_sam_acct;
1417 PyObject *py_user_sid;
1419 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1424 methods = pytalloc_get_ptr(self);
1426 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1427 if (py_sam_acct == NULL) {
1432 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1434 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1435 if (!NT_STATUS_IS_OK(status)) {
1436 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1437 NT_STATUS_V(status),
1438 get_friendly_nt_error_msg(status));
1439 Py_DECREF(py_sam_acct);
1448 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1450 TALLOC_CTX *frame = talloc_stackframe();
1452 struct pdb_methods *methods;
1453 const char *username;
1454 unsigned int acct_flags;
1457 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1462 methods = pytalloc_get_ptr(self);
1464 status = methods->create_user(methods, frame, username, acct_flags, &rid);
1465 if (!NT_STATUS_IS_OK(status)) {
1466 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1468 NT_STATUS_V(status),
1469 get_friendly_nt_error_msg(status));
1475 return PyInt_FromLong(rid);
1478 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1480 TALLOC_CTX *frame = talloc_stackframe();
1482 struct pdb_methods *methods;
1483 struct samu *sam_acct;
1484 PyObject *py_sam_acct;
1486 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1491 methods = pytalloc_get_ptr(self);
1493 sam_acct = pytalloc_get_ptr(py_sam_acct);
1495 status = methods->delete_user(methods, frame, sam_acct);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1498 NT_STATUS_V(status),
1499 get_friendly_nt_error_msg(status));
1508 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1510 TALLOC_CTX *frame = talloc_stackframe();
1512 struct pdb_methods *methods;
1513 struct samu *sam_acct;
1514 PyObject *py_sam_acct;
1516 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1521 methods = pytalloc_get_ptr(self);
1523 sam_acct = pytalloc_get_ptr(py_sam_acct);
1525 status = methods->add_sam_account(methods, sam_acct);
1526 if (!NT_STATUS_IS_OK(status)) {
1527 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1529 NT_STATUS_V(status),
1530 get_friendly_nt_error_msg(status));
1539 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1541 TALLOC_CTX *frame = talloc_stackframe();
1543 struct pdb_methods *methods;
1544 struct samu *sam_acct;
1545 PyObject *py_sam_acct;
1547 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1552 methods = pytalloc_get_ptr(self);
1554 sam_acct = pytalloc_get_ptr(py_sam_acct);
1556 status = methods->update_sam_account(methods, sam_acct);
1557 if (!NT_STATUS_IS_OK(status)) {
1558 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1559 NT_STATUS_V(status),
1560 get_friendly_nt_error_msg(status));
1569 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1571 TALLOC_CTX *frame = talloc_stackframe();
1573 struct pdb_methods *methods;
1574 struct samu *sam_acct;
1575 PyObject *py_sam_acct;
1577 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1582 methods = pytalloc_get_ptr(self);
1584 sam_acct = pytalloc_get_ptr(py_sam_acct);
1586 status = methods->delete_sam_account(methods, sam_acct);
1587 if (!NT_STATUS_IS_OK(status)) {
1588 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1589 NT_STATUS_V(status),
1590 get_friendly_nt_error_msg(status));
1599 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1601 TALLOC_CTX *frame = talloc_stackframe();
1603 struct pdb_methods *methods;
1604 struct samu *sam_acct;
1605 const char *new_username;
1606 PyObject *py_sam_acct;
1608 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1614 methods = pytalloc_get_ptr(self);
1616 sam_acct = pytalloc_get_ptr(py_sam_acct);
1618 status = methods->rename_sam_account(methods, sam_acct, new_username);
1619 if (!NT_STATUS_IS_OK(status)) {
1620 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1621 NT_STATUS_V(status),
1622 get_friendly_nt_error_msg(status));
1632 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1634 TALLOC_CTX *frame = talloc_stackframe();
1636 struct pdb_methods *methods;
1637 GROUP_MAP *group_map;
1638 struct dom_sid *domain_sid;
1639 PyObject *py_domain_sid, *py_group_map;
1641 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1646 methods = pytalloc_get_ptr(self);
1648 domain_sid = pytalloc_get_ptr(py_domain_sid);
1650 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1651 if (py_group_map == NULL) {
1657 group_map = pytalloc_get_ptr(py_group_map);
1659 status = methods->getgrsid(methods, group_map, *domain_sid);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1662 NT_STATUS_V(status),
1663 get_friendly_nt_error_msg(status));
1669 return py_group_map;
1673 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1675 TALLOC_CTX *frame = talloc_stackframe();
1677 struct pdb_methods *methods;
1678 GROUP_MAP *group_map;
1679 PyObject *py_group_map;
1680 unsigned int gid_value;
1682 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1687 methods = pytalloc_get_ptr(self);
1689 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1690 if (py_group_map == NULL) {
1696 group_map = pytalloc_get_ptr(py_group_map);
1698 status = methods->getgrgid(methods, group_map, gid_value);
1699 if (!NT_STATUS_IS_OK(status)) {
1700 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1701 NT_STATUS_V(status),
1702 get_friendly_nt_error_msg(status));
1708 return py_group_map;
1712 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1714 TALLOC_CTX *frame = talloc_stackframe();
1716 struct pdb_methods *methods;
1717 GROUP_MAP *group_map;
1718 PyObject *py_group_map;
1719 const char *groupname;
1721 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1726 methods = pytalloc_get_ptr(self);
1728 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1729 if (py_group_map == NULL) {
1735 group_map = pytalloc_get_ptr(py_group_map);
1737 status = methods->getgrnam(methods, group_map, groupname);
1738 if (!NT_STATUS_IS_OK(status)) {
1739 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1740 NT_STATUS_V(status),
1741 get_friendly_nt_error_msg(status));
1747 return py_group_map;
1751 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1753 TALLOC_CTX *frame = talloc_stackframe();
1755 struct pdb_methods *methods;
1756 const char *groupname;
1759 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1764 methods = pytalloc_get_ptr(self);
1766 status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1767 if (!NT_STATUS_IS_OK(status)) {
1768 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1770 NT_STATUS_V(status),
1771 get_friendly_nt_error_msg(status));
1777 return PyInt_FromLong(group_rid);
1781 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1783 TALLOC_CTX *frame = talloc_stackframe();
1785 struct pdb_methods *methods;
1786 unsigned int group_rid;
1788 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1793 methods = pytalloc_get_ptr(self);
1795 status = methods->delete_dom_group(methods, frame, group_rid);
1796 if (!NT_STATUS_IS_OK(status)) {
1797 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1799 NT_STATUS_V(status),
1800 get_friendly_nt_error_msg(status));
1810 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1812 TALLOC_CTX *frame = talloc_stackframe();
1814 struct pdb_methods *methods;
1815 PyObject *py_group_map;
1816 GROUP_MAP *group_map;
1818 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1823 methods = pytalloc_get_ptr(self);
1825 group_map = pytalloc_get_ptr(py_group_map);
1827 status = methods->add_group_mapping_entry(methods, group_map);
1828 if (!NT_STATUS_IS_OK(status)) {
1829 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1830 NT_STATUS_V(status),
1831 get_friendly_nt_error_msg(status));
1841 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1843 TALLOC_CTX *frame = talloc_stackframe();
1845 struct pdb_methods *methods;
1846 PyObject *py_group_map;
1847 GROUP_MAP *group_map;
1849 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1854 methods = pytalloc_get_ptr(self);
1856 group_map = pytalloc_get_ptr(py_group_map);
1858 status = methods->update_group_mapping_entry(methods, group_map);
1859 if (!NT_STATUS_IS_OK(status)) {
1860 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1861 NT_STATUS_V(status),
1862 get_friendly_nt_error_msg(status));
1872 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1874 TALLOC_CTX *frame = talloc_stackframe();
1876 struct pdb_methods *methods;
1877 PyObject *py_group_sid;
1878 struct dom_sid *group_sid;
1880 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1885 methods = pytalloc_get_ptr(self);
1887 group_sid = pytalloc_get_ptr(py_group_sid);
1889 status = methods->delete_group_mapping_entry(methods, *group_sid);
1890 if (!NT_STATUS_IS_OK(status)) {
1891 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1892 NT_STATUS_V(status),
1893 get_friendly_nt_error_msg(status));
1903 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1905 TALLOC_CTX *frame = talloc_stackframe();
1907 struct pdb_methods *methods;
1908 enum lsa_SidType sid_name_use;
1909 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1911 PyObject *py_domain_sid;
1912 struct dom_sid *domain_sid = NULL;
1913 GROUP_MAP **gmap = NULL;
1914 GROUP_MAP *group_map;
1916 PyObject *py_gmap_list, *py_group_map;
1919 py_domain_sid = Py_None;
1922 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1923 &lsa_sidtype_value, &unix_only)) {
1928 methods = pytalloc_get_ptr(self);
1930 sid_name_use = lsa_sidtype_value;
1932 if (py_domain_sid != Py_None) {
1933 domain_sid = pytalloc_get_ptr(py_domain_sid);
1936 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1937 &gmap, &num_entries, unix_only);
1938 if (!NT_STATUS_IS_OK(status)) {
1939 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1940 NT_STATUS_V(status),
1941 get_friendly_nt_error_msg(status));
1946 py_gmap_list = PyList_New(0);
1947 if (py_gmap_list == NULL) {
1953 for(i=0; i<num_entries; i++) {
1954 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1956 group_map = pytalloc_get_ptr(py_group_map);
1957 *group_map = *gmap[i];
1958 talloc_steal(group_map, gmap[i]->nt_name);
1959 talloc_steal(group_map, gmap[i]->comment);
1961 PyList_Append(py_gmap_list, py_group_map);
1968 return py_gmap_list;
1972 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1974 TALLOC_CTX *frame = talloc_stackframe();
1976 struct pdb_methods *methods;
1977 PyObject *py_group_sid;
1978 struct dom_sid *group_sid;
1979 uint32_t *member_rids;
1981 PyObject *py_sid_list;
1982 struct dom_sid *domain_sid, *member_sid;
1985 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1990 methods = pytalloc_get_ptr(self);
1992 group_sid = pytalloc_get_ptr(py_group_sid);
1994 status = methods->enum_group_members(methods, frame, group_sid,
1995 &member_rids, &num_members);
1996 if (!NT_STATUS_IS_OK(status)) {
1997 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1998 NT_STATUS_V(status),
1999 get_friendly_nt_error_msg(status));
2004 py_sid_list = PyList_New(0);
2005 if (py_sid_list == NULL) {
2011 domain_sid = get_global_sam_sid();
2013 for(i=0; i<num_members; i++) {
2014 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2015 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
2023 static PyObject *py_pdb_enum_group_memberships(pytalloc_Object *self, PyObject *args)
2025 TALLOC_CTX *frame = talloc_stackframe();
2027 struct pdb_methods *methods;
2030 struct samu *sam_acct;
2031 PyObject *py_sam_acct;
2032 PyObject *py_sid_list;
2033 struct dom_sid *user_group_sids = NULL;
2034 gid_t *user_group_ids = NULL;
2035 uint32_t num_groups = 0;
2037 if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2042 methods = pytalloc_get_ptr(self);
2044 sam_acct = pytalloc_get_ptr(py_sam_acct);
2046 status = methods->enum_group_memberships(methods, frame, sam_acct,
2047 &user_group_sids, &user_group_ids, &num_groups);
2048 if (!NT_STATUS_IS_OK(status)) {
2049 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2050 NT_STATUS_V(status),
2051 get_friendly_nt_error_msg(status));
2056 py_sid_list = PyList_New(0);
2057 if (py_sid_list == NULL) {
2063 for(i=0; i<num_groups; i++) {
2064 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
2072 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
2074 TALLOC_CTX *frame = talloc_stackframe();
2076 struct pdb_methods *methods;
2077 uint32_t group_rid, member_rid;
2079 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2084 methods = pytalloc_get_ptr(self);
2086 status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2087 if (!NT_STATUS_IS_OK(status)) {
2088 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2089 NT_STATUS_V(status),
2090 get_friendly_nt_error_msg(status));
2100 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
2102 TALLOC_CTX *frame = talloc_stackframe();
2104 struct pdb_methods *methods;
2105 uint32_t group_rid, member_rid;
2107 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2112 methods = pytalloc_get_ptr(self);
2114 status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2115 if (!NT_STATUS_IS_OK(status)) {
2116 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2117 NT_STATUS_V(status),
2118 get_friendly_nt_error_msg(status));
2128 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
2130 TALLOC_CTX *frame = talloc_stackframe();
2132 struct pdb_methods *methods;
2133 const char *alias_name;
2136 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2141 methods = pytalloc_get_ptr(self);
2143 status = methods->create_alias(methods, alias_name, &rid);
2144 if (!NT_STATUS_IS_OK(status)) {
2145 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2147 NT_STATUS_V(status),
2148 get_friendly_nt_error_msg(status));
2154 return PyInt_FromLong(rid);
2158 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2160 TALLOC_CTX *frame = talloc_stackframe();
2162 struct pdb_methods *methods;
2163 PyObject *py_alias_sid;
2164 struct dom_sid *alias_sid;
2166 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2171 methods = pytalloc_get_ptr(self);
2173 alias_sid = pytalloc_get_ptr(py_alias_sid);
2175 status = methods->delete_alias(methods, alias_sid);
2176 if (!NT_STATUS_IS_OK(status)) {
2177 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2178 NT_STATUS_V(status),
2179 get_friendly_nt_error_msg(status));
2189 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2191 TALLOC_CTX *frame = talloc_stackframe();
2193 struct pdb_methods *methods;
2194 PyObject *py_alias_sid;
2195 struct dom_sid *alias_sid;
2196 struct acct_info *alias_info;
2197 PyObject *py_alias_info;
2199 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2204 methods = pytalloc_get_ptr(self);
2206 alias_sid = pytalloc_get_ptr(py_alias_sid);
2208 alias_info = talloc_zero(frame, struct acct_info);
2215 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2216 if (!NT_STATUS_IS_OK(status)) {
2217 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2218 NT_STATUS_V(status),
2219 get_friendly_nt_error_msg(status));
2224 py_alias_info = PyDict_New();
2225 if (py_alias_info == NULL) {
2231 PyDict_SetItemString(py_alias_info, "acct_name",
2232 PyString_FromString(alias_info->acct_name));
2233 PyDict_SetItemString(py_alias_info, "acct_desc",
2234 PyString_FromString(alias_info->acct_desc));
2235 PyDict_SetItemString(py_alias_info, "rid",
2236 PyInt_FromLong(alias_info->rid));
2239 return py_alias_info;
2243 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2245 TALLOC_CTX *frame = talloc_stackframe();
2247 struct pdb_methods *methods;
2248 PyObject *py_alias_sid, *py_alias_info;
2249 struct dom_sid *alias_sid;
2250 struct acct_info alias_info;
2252 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2258 methods = pytalloc_get_ptr(self);
2260 alias_sid = pytalloc_get_ptr(py_alias_sid);
2262 alias_info.acct_name = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2263 if (alias_info.acct_name == NULL) {
2264 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2268 alias_info.acct_desc = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2269 if (alias_info.acct_desc == NULL) {
2270 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2275 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2276 if (!NT_STATUS_IS_OK(status)) {
2277 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2278 NT_STATUS_V(status),
2279 get_friendly_nt_error_msg(status));
2289 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2291 TALLOC_CTX *frame = talloc_stackframe();
2293 struct pdb_methods *methods;
2294 PyObject *py_alias_sid, *py_member_sid;
2295 struct dom_sid *alias_sid, *member_sid;
2297 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2298 dom_sid_Type, &py_member_sid)) {
2303 methods = pytalloc_get_ptr(self);
2305 alias_sid = pytalloc_get_ptr(py_alias_sid);
2306 member_sid = pytalloc_get_ptr(py_member_sid);
2308 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2309 if (!NT_STATUS_IS_OK(status)) {
2310 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2311 NT_STATUS_V(status),
2312 get_friendly_nt_error_msg(status));
2322 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2324 TALLOC_CTX *frame = talloc_stackframe();
2326 struct pdb_methods *methods;
2327 PyObject *py_alias_sid, *py_member_sid;
2328 const struct dom_sid *alias_sid, *member_sid;
2330 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2331 dom_sid_Type, &py_member_sid)) {
2336 methods = pytalloc_get_ptr(self);
2338 alias_sid = pytalloc_get_ptr(py_alias_sid);
2339 member_sid = pytalloc_get_ptr(py_member_sid);
2341 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2342 if (!NT_STATUS_IS_OK(status)) {
2343 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2344 NT_STATUS_V(status),
2345 get_friendly_nt_error_msg(status));
2355 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2357 TALLOC_CTX *frame = talloc_stackframe();
2359 struct pdb_methods *methods;
2360 PyObject *py_alias_sid;
2361 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2362 PyObject *py_member_list, *py_member_sid;
2366 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2371 methods = pytalloc_get_ptr(self);
2373 alias_sid = pytalloc_get_ptr(py_alias_sid);
2375 status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2376 if (!NT_STATUS_IS_OK(status)) {
2377 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2378 NT_STATUS_V(status),
2379 get_friendly_nt_error_msg(status));
2384 py_member_list = PyList_New(0);
2385 if (py_member_list == NULL) {
2391 for(i=0; i<num_members; i++) {
2392 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2393 if (py_member_sid == NULL) {
2398 tmp_sid = pytalloc_get_ptr(py_member_sid);
2399 *tmp_sid = member_sid[i];
2400 PyList_Append(py_member_list, py_member_sid);
2404 return py_member_list;
2408 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2410 TALLOC_CTX *frame = talloc_stackframe();
2412 struct pdb_methods *methods;
2413 PyObject *py_acct_policy;
2417 enum pdb_policy_type type;
2419 methods = pytalloc_get_ptr(self);
2421 py_acct_policy = PyDict_New();
2422 if (py_acct_policy == NULL) {
2428 account_policy_names_list(frame, &names, &count);
2429 for (i=0; i<count; i++) {
2430 type = account_policy_name_to_typenum(names[i]);
2431 status = methods->get_account_policy(methods, type, &value);
2432 if (NT_STATUS_IS_OK(status)) {
2433 PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2438 return py_acct_policy;
2442 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2444 TALLOC_CTX *frame = talloc_stackframe();
2446 struct pdb_methods *methods;
2447 PyObject *py_acct_policy, *py_value;
2450 enum pdb_policy_type type;
2452 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2457 methods = pytalloc_get_ptr(self);
2459 account_policy_names_list(frame, &names, &count);
2460 for (i=0; i<count; i++) {
2461 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2462 type = account_policy_name_to_typenum(names[i]);
2463 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2464 if (!NT_STATUS_IS_OK(status)) {
2465 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2467 NT_STATUS_V(status),
2468 get_friendly_nt_error_msg(status));
2477 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2479 TALLOC_CTX *frame = talloc_stackframe();
2480 struct pdb_methods *methods;
2481 unsigned int acct_flags;
2482 struct pdb_search *search;
2483 struct samr_displayentry *entry;
2484 PyObject *py_userlist, *py_dict;
2486 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2491 methods = pytalloc_get_ptr(self);
2493 search = talloc_zero(frame, struct pdb_search);
2494 if (search == NULL) {
2500 if (!methods->search_users(methods, search, acct_flags)) {
2501 PyErr_Format(py_pdb_error, "Unable to search users");
2506 entry = talloc_zero(frame, struct samr_displayentry);
2507 if (entry == NULL) {
2513 py_userlist = PyList_New(0);
2514 if (py_userlist == NULL) {
2520 while (search->next_entry(search, entry)) {
2521 py_dict = PyDict_New();
2522 if (py_dict == NULL) {
2525 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2526 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2527 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2528 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2529 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2530 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2531 PyList_Append(py_userlist, py_dict);
2534 search->search_end(search);
2541 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2543 TALLOC_CTX *frame = talloc_stackframe();
2544 struct pdb_methods *methods;
2545 struct pdb_search *search;
2546 struct samr_displayentry *entry;
2547 PyObject *py_grouplist, *py_dict;
2549 methods = pytalloc_get_ptr(self);
2551 search = talloc_zero(frame, struct pdb_search);
2552 if (search == NULL) {
2558 if (!methods->search_groups(methods, search)) {
2559 PyErr_Format(py_pdb_error, "Unable to search groups");
2564 entry = talloc_zero(frame, struct samr_displayentry);
2565 if (entry == NULL) {
2571 py_grouplist = PyList_New(0);
2572 if (py_grouplist == NULL) {
2578 while (search->next_entry(search, entry)) {
2579 py_dict = PyDict_New();
2580 if (py_dict == NULL) {
2583 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2584 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2585 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2586 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2587 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2588 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2589 PyList_Append(py_grouplist, py_dict);
2592 search->search_end(search);
2595 return py_grouplist;
2599 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2601 TALLOC_CTX *frame = talloc_stackframe();
2602 struct pdb_methods *methods;
2603 struct pdb_search *search;
2604 struct samr_displayentry *entry;
2605 PyObject *py_aliaslist, *py_dict;
2606 PyObject *py_domain_sid;
2607 struct dom_sid *domain_sid = NULL;
2609 py_domain_sid = Py_None;
2612 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2617 methods = pytalloc_get_ptr(self);
2619 if (py_domain_sid != Py_None) {
2620 domain_sid = pytalloc_get_ptr(py_domain_sid);
2623 search = talloc_zero(frame, struct pdb_search);
2624 if (search == NULL) {
2630 if (!methods->search_aliases(methods, search, domain_sid)) {
2631 PyErr_Format(py_pdb_error, "Unable to search aliases");
2636 entry = talloc_zero(frame, struct samr_displayentry);
2637 if (entry == NULL) {
2643 py_aliaslist = PyList_New(0);
2644 if (py_aliaslist == NULL) {
2650 while (search->next_entry(search, entry)) {
2651 py_dict = PyDict_New();
2652 if (py_dict == NULL) {
2655 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2656 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2657 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2658 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2659 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2660 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2661 PyList_Append(py_aliaslist, py_dict);
2664 search->search_end(search);
2667 return py_aliaslist;
2671 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2673 TALLOC_CTX *frame = talloc_stackframe();
2674 struct pdb_methods *methods;
2677 struct dom_sid user_sid, *copy_user_sid;
2678 PyObject *py_user_sid;
2680 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2685 methods = pytalloc_get_ptr(self);
2688 id.type = ID_TYPE_UID;
2690 if (!methods->id_to_sid(methods, &id, &user_sid)) {
2691 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2696 copy_user_sid = dom_sid_dup(frame, &user_sid);
2697 if (copy_user_sid == NULL) {
2703 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2710 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2712 TALLOC_CTX *frame = talloc_stackframe();
2713 struct pdb_methods *methods;
2716 struct dom_sid group_sid, *copy_group_sid;
2717 PyObject *py_group_sid;
2719 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2725 id.type = ID_TYPE_GID;
2727 methods = pytalloc_get_ptr(self);
2729 if (!methods->id_to_sid(methods, &id, &group_sid)) {
2730 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2735 copy_group_sid = dom_sid_dup(frame, &group_sid);
2736 if (copy_group_sid == NULL) {
2742 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2745 return py_group_sid;
2749 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2751 TALLOC_CTX *frame = talloc_stackframe();
2752 struct pdb_methods *methods;
2754 struct dom_sid *sid;
2757 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2762 methods = pytalloc_get_ptr(self);
2764 sid = pytalloc_get_ptr(py_sid);
2766 if (!methods->sid_to_id(methods, sid, &id)) {
2767 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2773 return Py_BuildValue("(II)", id.id, id.type);
2777 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2779 TALLOC_CTX *frame = talloc_stackframe();
2780 struct pdb_methods *methods;
2783 methods = pytalloc_get_ptr(self);
2785 if (!methods->new_rid(methods, &rid)) {
2786 PyErr_Format(py_pdb_error, "Unable to get new rid");
2792 return PyInt_FromLong(rid);
2796 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2798 TALLOC_CTX *frame = talloc_stackframe();
2799 struct pdb_methods *methods;
2802 struct dom_sid sid, *copy_sid;
2804 time_t last_set_time;
2807 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2812 methods = pytalloc_get_ptr(self);
2814 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2815 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2820 copy_sid = dom_sid_dup(frame, &sid);
2821 if (copy_sid == NULL) {
2827 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2828 if (py_sid == NULL) {
2834 py_value = PyDict_New();
2835 if (py_value == NULL) {
2841 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2842 PyDict_SetItemString(py_value, "sid", py_sid);
2843 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2850 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2852 TALLOC_CTX *frame = talloc_stackframe();
2853 struct pdb_methods *methods;
2856 const struct dom_sid *domain_sid;
2857 PyObject *py_domain_sid;
2859 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2860 dom_sid_Type, &py_domain_sid)) {
2865 methods = pytalloc_get_ptr(self);
2867 domain_sid = pytalloc_get_ptr(py_domain_sid);
2869 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2870 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2880 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2882 TALLOC_CTX *frame = talloc_stackframe();
2883 struct pdb_methods *methods;
2886 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2891 methods = pytalloc_get_ptr(self);
2893 if (!methods->del_trusteddom_pw(methods, domain)) {
2894 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2904 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2906 TALLOC_CTX *frame = talloc_stackframe();
2908 struct pdb_methods *methods;
2909 uint32_t num_domains;
2910 struct trustdom_info **domains;
2911 PyObject *py_domain_list, *py_dict;
2914 methods = pytalloc_get_ptr(self);
2916 status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
2917 if (!NT_STATUS_IS_OK(status)) {
2918 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2919 NT_STATUS_V(status),
2920 get_friendly_nt_error_msg(status));
2925 py_domain_list = PyList_New(0);
2926 if (py_domain_list == NULL) {
2932 for(i=0; i<num_domains; i++) {
2933 py_dict = PyDict_New();
2935 PyDict_SetItemString(py_dict, "name",
2936 PyString_FromString(domains[i]->name));
2937 PyDict_SetItemString(py_dict, "sid",
2938 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2941 PyList_Append(py_domain_list, py_dict);
2945 return py_domain_list;
2949 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2951 TALLOC_CTX *frame = talloc_stackframe();
2953 struct pdb_methods *methods;
2955 struct pdb_trusted_domain *td;
2956 PyObject *py_domain_info;
2958 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2963 methods = pytalloc_get_ptr(self);
2965 status = methods->get_trusted_domain(methods, frame, domain, &td);
2966 if (!NT_STATUS_IS_OK(status)) {
2967 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2968 NT_STATUS_V(status),
2969 get_friendly_nt_error_msg(status));
2974 py_domain_info = PyDict_New();
2975 if (py_domain_info == NULL) {
2981 PyDict_SetItemString(py_domain_info, "domain_name",
2982 PyString_FromString(td->domain_name));
2983 PyDict_SetItemString(py_domain_info, "netbios_name",
2984 PyString_FromString(td->netbios_name));
2985 PyDict_SetItemString(py_domain_info, "security_identifier",
2986 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2987 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2988 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2989 td->trust_auth_incoming.length));
2990 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2991 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2992 td->trust_auth_outgoing.length));
2993 PyDict_SetItemString(py_domain_info, "trust_direction",
2994 PyInt_FromLong(td->trust_direction));
2995 PyDict_SetItemString(py_domain_info, "trust_type",
2996 PyInt_FromLong(td->trust_type));
2997 PyDict_SetItemString(py_domain_info, "trust_attributes",
2998 PyInt_FromLong(td->trust_attributes));
2999 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3000 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3001 td->trust_forest_trust_info.length));
3004 return py_domain_info;
3008 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
3010 TALLOC_CTX *frame = talloc_stackframe();
3012 struct pdb_methods *methods;
3013 PyObject *py_domain_sid;
3014 struct dom_sid *domain_sid;
3015 struct pdb_trusted_domain *td;
3016 PyObject *py_domain_info;
3018 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3023 methods = pytalloc_get_ptr(self);
3025 domain_sid = pytalloc_get_ptr(py_domain_sid);
3027 status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3028 if (!NT_STATUS_IS_OK(status)) {
3029 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3030 NT_STATUS_V(status),
3031 get_friendly_nt_error_msg(status));
3036 py_domain_info = PyDict_New();
3037 if (py_domain_info == NULL) {
3043 PyDict_SetItemString(py_domain_info, "domain_name",
3044 PyString_FromString(td->domain_name));
3045 PyDict_SetItemString(py_domain_info, "netbios_name",
3046 PyString_FromString(td->netbios_name));
3047 PyDict_SetItemString(py_domain_info, "security_identifier",
3048 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3049 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3050 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3051 td->trust_auth_incoming.length));
3052 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3053 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3054 td->trust_auth_outgoing.length));
3055 PyDict_SetItemString(py_domain_info, "trust_direction",
3056 PyInt_FromLong(td->trust_direction));
3057 PyDict_SetItemString(py_domain_info, "trust_type",
3058 PyInt_FromLong(td->trust_type));
3059 PyDict_SetItemString(py_domain_info, "trust_attributes",
3060 PyInt_FromLong(td->trust_attributes));
3061 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3062 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3063 td->trust_forest_trust_info.length));
3066 return py_domain_info;
3070 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3072 TALLOC_CTX *frame = talloc_stackframe();
3074 struct pdb_methods *methods;
3076 PyObject *py_td_info;
3077 struct pdb_trusted_domain td_info;
3081 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3086 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3087 td_info.domain_name = PyString_AsString(py_tmp);
3089 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3090 td_info.netbios_name = PyString_AsString(py_tmp);
3092 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3093 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3095 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3096 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3097 td_info.trust_auth_incoming.length = len;
3099 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3100 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3101 td_info.trust_auth_outgoing.length = len;
3103 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3104 td_info.trust_direction = PyInt_AsLong(py_tmp);
3106 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3107 td_info.trust_type = PyInt_AsLong(py_tmp);
3109 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3110 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3112 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3113 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3114 td_info.trust_forest_trust_info.length = len;
3116 methods = pytalloc_get_ptr(self);
3118 status = methods->set_trusted_domain(methods, domain, &td_info);
3119 if (!NT_STATUS_IS_OK(status)) {
3120 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3121 NT_STATUS_V(status),
3122 get_friendly_nt_error_msg(status));
3132 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3134 TALLOC_CTX *frame = talloc_stackframe();
3136 struct pdb_methods *methods;
3139 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3144 methods = pytalloc_get_ptr(self);
3146 status = methods->del_trusted_domain(methods, domain);
3147 if (!NT_STATUS_IS_OK(status)) {
3148 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3149 NT_STATUS_V(status),
3150 get_friendly_nt_error_msg(status));
3160 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3162 TALLOC_CTX *frame = talloc_stackframe();
3164 struct pdb_methods *methods;
3165 uint32_t num_domains;
3166 struct pdb_trusted_domain **td_info, *td;
3167 PyObject *py_td_info, *py_domain_info;
3170 methods = pytalloc_get_ptr(self);
3172 status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3173 if (!NT_STATUS_IS_OK(status)) {
3174 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3175 NT_STATUS_V(status),
3176 get_friendly_nt_error_msg(status));
3181 py_td_info = PyList_New(0);
3182 if (py_td_info == NULL) {
3188 for (i=0; i<num_domains; i++) {
3190 py_domain_info = PyDict_New();
3191 if (py_domain_info == NULL) {
3193 Py_DECREF(py_td_info);
3200 PyDict_SetItemString(py_domain_info, "domain_name",
3201 PyString_FromString(td->domain_name));
3202 PyDict_SetItemString(py_domain_info, "netbios_name",
3203 PyString_FromString(td->netbios_name));
3204 PyDict_SetItemString(py_domain_info, "security_identifier",
3205 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3206 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3207 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3208 td->trust_auth_incoming.length));
3209 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3210 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3211 td->trust_auth_outgoing.length));
3212 PyDict_SetItemString(py_domain_info, "trust_direction",
3213 PyInt_FromLong(td->trust_direction));
3214 PyDict_SetItemString(py_domain_info, "trust_type",
3215 PyInt_FromLong(td->trust_type));
3216 PyDict_SetItemString(py_domain_info, "trust_attributes",
3217 PyInt_FromLong(td->trust_attributes));
3218 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3219 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3220 td->trust_forest_trust_info.length));
3221 PyList_Append(py_td_info, py_domain_info);
3229 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3231 TALLOC_CTX *frame = talloc_stackframe();
3233 struct pdb_methods *methods;
3234 const char *secret_name;
3235 DATA_BLOB secret_current, secret_old;
3236 NTTIME secret_current_lastchange, secret_old_lastchange;
3238 struct security_descriptor *sd;
3239 PyObject *py_secret;
3241 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3246 methods = pytalloc_get_ptr(self);
3248 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3249 if (py_sd == NULL) {
3254 sd = pytalloc_get_ptr(py_sd);
3256 status = methods->get_secret(methods, frame, secret_name,
3258 &secret_current_lastchange,
3260 &secret_old_lastchange,
3262 if (!NT_STATUS_IS_OK(status)) {
3263 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3265 NT_STATUS_V(status),
3266 get_friendly_nt_error_msg(status));
3271 py_secret = PyDict_New();
3272 if (py_secret == NULL) {
3279 PyDict_SetItemString(py_secret, "secret_current",
3280 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3281 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3282 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3283 PyDict_SetItemString(py_secret, "secret_old",
3284 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3285 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3286 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3287 PyDict_SetItemString(py_secret, "sd", py_sd);
3294 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3296 TALLOC_CTX *frame = talloc_stackframe();
3298 struct pdb_methods *methods;
3299 const char *secret_name;
3300 PyObject *py_secret;
3301 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3302 DATA_BLOB secret_current, secret_old;
3303 struct security_descriptor *sd;
3306 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3311 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3312 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3313 py_sd = PyDict_GetItemString(py_secret, "sd");
3315 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3316 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3317 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3319 methods = pytalloc_get_ptr(self);
3321 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3322 secret_current.length = len;
3323 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3324 secret_current.length = len;
3325 sd = pytalloc_get_ptr(py_sd);
3327 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3328 if (!NT_STATUS_IS_OK(status)) {
3329 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3331 NT_STATUS_V(status),
3332 get_friendly_nt_error_msg(status));
3342 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3344 TALLOC_CTX *frame = talloc_stackframe();
3346 struct pdb_methods *methods;
3347 const char *secret_name;
3349 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3354 methods = pytalloc_get_ptr(self);
3356 status = methods->delete_secret(methods, secret_name);
3357 if (!NT_STATUS_IS_OK(status)) {
3358 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3360 NT_STATUS_V(status),
3361 get_friendly_nt_error_msg(status));
3370 static PyMethodDef py_pdb_methods[] = {
3371 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3372 "domain_info() -> str\n\n \
3373 Get domain information for the database." },
3374 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3375 "getsampwnam(username) -> samu object\n\n \
3376 Get user information by name." },
3377 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3378 "getsampwsid(user_sid) -> samu object\n\n \
3379 Get user information by sid (dcerpc.security.dom_sid object)." },
3380 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3381 "create_user(username, acct_flags) -> rid\n\n \
3382 Create user. acct_flags are samr account control flags." },
3383 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3384 "delete_user(samu object) -> None\n\n \
3386 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3387 "add_sam_account(samu object) -> None\n\n \
3388 Add SAM account." },
3389 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3390 "update_sam_account(samu object) -> None\n\n \
3391 Update SAM account." },
3392 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3393 "delete_sam_account(samu object) -> None\n\n \
3394 Delete SAM account." },
3395 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3396 "rename_sam_account(samu object1, new_username) -> None\n\n \
3397 Rename SAM account." },
3398 /* update_login_attempts */
3399 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3400 "getgrsid(group_sid) -> groupmap object\n\n \
3401 Get group information by sid (dcerpc.security.dom_sid object)." },
3402 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3403 "getgrsid(gid) -> groupmap object\n\n \
3404 Get group information by gid." },
3405 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3406 "getgrsid(groupname) -> groupmap object\n\n \
3407 Get group information by name." },
3408 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3409 "create_dom_group(groupname) -> group_rid\n\n \
3410 Create new domain group by name." },
3411 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3412 "delete_dom_group(group_rid) -> None\n\n \
3413 Delete domain group identified by rid" },
3414 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3415 "add_group_mapping_entry(groupmap) -> None\n \
3416 Add group mapping entry for groupmap object." },
3417 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3418 "update_group_mapping_entry(groupmap) -> None\n\n \
3419 Update group mapping entry for groupmap object." },
3420 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3421 "delete_group_mapping_entry(groupmap) -> None\n\n \
3422 Delete group mapping entry for groupmap object." },
3423 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3424 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3425 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3426 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3427 "enum_group_members(group_sid) -> List\n\n \
3428 Return list of users (dom_sid object) in group." },
3429 { "enum_group_memberships", (PyCFunction)py_pdb_enum_group_memberships, METH_VARARGS,
3430 "enum_group_memberships(samu object) -> List\n\n \
3431 Return list of groups (dom_sid object) this user is part of." },
3432 /* set_unix_primary_group */
3433 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3434 "add_groupmem(group_rid, member_rid) -> None\n\n \
3435 Add user to group." },
3436 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3437 "del_groupmem(group_rid, member_rid) -> None\n\n \
3438 Remove user from from group." },
3439 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3440 "create_alias(alias_name) -> alias_rid\n\n \
3441 Create alias entry." },
3442 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3443 "delete_alias(alias_sid) -> None\n\n \
3444 Delete alias entry." },
3445 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3446 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3447 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3448 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3449 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3450 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3451 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3452 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3453 Add user to alias entry." },
3454 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3455 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3456 Remove a user from alias entry." },
3457 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3458 "enum_aliasmem(alias_sid) -> List\n\n \
3459 Return a list of members (dom_sid object) for alias entry." },
3460 /* enum_alias_memberships */
3463 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3464 "get_account_policy() -> Mapping\n\n \
3465 Get account policy information as a dictionary." },
3466 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3467 "get_account_policy(Mapping) -> None\n\n \
3468 Set account policy settings from a dicionary." },
3470 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3471 "search_users(acct_flags) -> List\n\n \
3472 Search users. acct_flags are samr account control flags.\n \
3473 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3474 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3475 "search_groups() -> List\n\n \
3476 Search unix only groups. \n \
3477 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3478 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3479 "search_aliases([domain_sid]) -> List\n\n \
3480 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3481 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3482 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3483 "uid_to_sid(uid) -> sid\n\n \
3484 Return sid for given user id." },
3485 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3486 "gid_to_sid(gid) -> sid\n\n \
3487 Return sid for given group id." },
3488 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3489 "sid_to_id(sid) -> Tuple\n\n \
3490 Return id and type for given sid." },
3492 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3493 "new_rid() -> rid\n\n \
3495 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3496 "get_trusteddom_pw(domain) -> Mapping\n\n \
3497 Get trusted domain password, sid and last set time in a dictionary." },
3498 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3499 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3500 Set trusted domain password." },
3501 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3502 "del_trusteddom_pw(domain) -> None\n\n \
3503 Delete trusted domain password." },
3504 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3505 "enum_trusteddoms() -> List\n\n \
3506 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3507 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3508 "get_trusted_domain(domain) -> Mapping\n\n \
3509 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." },
3510 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3511 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3512 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" },
3513 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3514 "set_trusted_domain(domain, Mapping) -> None\n\n \
3515 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." },
3516 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3517 "del_trusted_domain(domain) -> None\n\n \
3518 Delete trusted domain." },
3519 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3520 "enum_trusted_domains() -> List\n\n \
3521 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." },
3522 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3523 "get_secret(secret_name) -> Mapping\n\n \
3524 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3525 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3526 "set_secret(secret_name, Mapping) -> None\n\n \
3527 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3528 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3529 "delete_secret(secret_name) -> None\n\n \
3530 Delete secret information for secret_name." },
3535 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3537 TALLOC_CTX *frame = talloc_stackframe();
3538 const char *url = NULL;
3541 struct pdb_methods *methods;
3543 if (!PyArg_ParseTuple(args, "s", &url)) {
3548 /* Initalize list of methods */
3549 status = make_pdb_method_name(&methods, url);
3550 if (!NT_STATUS_IS_OK(status)) {
3551 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3553 NT_STATUS_V(status),
3554 get_friendly_nt_error_msg(status));
3559 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3570 static PyTypeObject PyPDB = {
3571 .tp_name = "passdb.PDB",
3572 .tp_basicsize = sizeof(pytalloc_Object),
3573 .tp_new = py_pdb_new,
3574 .tp_flags = Py_TPFLAGS_DEFAULT,
3575 .tp_methods = py_pdb_methods,
3576 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3581 * Return a list of passdb backends
3583 static PyObject *py_passdb_backends(PyObject *self)
3585 TALLOC_CTX *frame = talloc_stackframe();
3587 const struct pdb_init_function_entry *entry;
3589 entry = pdb_get_backends();
3594 if((py_blist = PyList_New(0)) == NULL) {
3601 PyList_Append(py_blist, PyString_FromString(entry->name));
3602 entry = entry->next;
3610 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3612 TALLOC_CTX *frame = talloc_stackframe();
3613 const char *smb_config;
3615 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3620 /* Load smbconf parameters */
3621 if (!lp_load_global(smb_config)) {
3622 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3632 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3634 TALLOC_CTX *frame = talloc_stackframe();
3635 const char *private_dir;
3637 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3642 /* Initialize secrets database */
3643 if (!secrets_init_path(private_dir)) {
3644 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3654 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3656 TALLOC_CTX *frame = talloc_stackframe();
3658 /* Initialize secrets database */
3659 if (!initialize_password_db(true, NULL)) {
3660 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3669 static PyObject *py_get_global_sam_sid(PyObject *self)
3671 TALLOC_CTX *frame = talloc_stackframe();
3672 struct dom_sid *domain_sid, *domain_sid_copy;
3673 PyObject *py_dom_sid;
3675 domain_sid = get_global_sam_sid();
3677 domain_sid_copy = dom_sid_dup(frame, domain_sid);
3678 if (domain_sid_copy == NULL) {
3684 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3691 static PyMethodDef py_passdb_methods[] = {
3692 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3693 "get_backends() -> list\n\n \
3694 Get a list of password database backends supported." },
3695 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3696 "set_smb_config(path) -> None\n\n \
3697 Set path to smb.conf file to load configuration parameters." },
3698 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3699 "set_secrets_dir(private_dir) -> None\n\n \
3700 Set path to private directory to load secrets database from non-default location." },
3701 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3702 "get_global_sam_sid() -> dom_sid\n\n \
3703 Return domain SID." },
3704 { "reload_static_pdb", (PyCFunction)py_reload_static_pdb, METH_NOARGS,
3705 "reload_static_pdb() -> None\n\n \
3706 Re-initalise the static pdb used internally. Needed if 'passdb backend' is changed." },
3710 void initpassdb(void)
3712 TALLOC_CTX *frame = talloc_stackframe();
3714 char exception_name[] = "passdb.error";
3716 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3717 if (talloc_type == NULL) {
3722 PyPDB.tp_base = talloc_type;
3723 if (PyType_Ready(&PyPDB) < 0) {
3728 PySamu.tp_base = talloc_type;
3729 if (PyType_Ready(&PySamu) < 0) {
3734 PyGroupmap.tp_base = talloc_type;
3735 if (PyType_Ready(&PyGroupmap) < 0) {
3740 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3746 /* Create new exception for passdb module */
3747 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3748 Py_INCREF(py_pdb_error);
3749 PyModule_AddObject(m, "error", py_pdb_error);
3752 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3755 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3757 Py_INCREF(&PyGroupmap);
3758 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3760 /* Import dom_sid type from dcerpc.security */
3761 mod = PyImport_ImportModule("samba.dcerpc.security");
3767 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3768 if (dom_sid_Type == NULL) {
3773 /* Import security_descriptor type from dcerpc.security */
3774 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3776 if (security_Type == NULL) {
3781 /* Import GUID type from dcerpc.misc */
3782 mod = PyImport_ImportModule("samba.dcerpc.misc");
3788 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3790 if (guid_Type == NULL) {