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 "python/py3compat.h"
24 #include "lib/util/talloc_stack.h"
25 #include "libcli/security/security.h"
26 #include "librpc/gen_ndr/idmap.h"
30 #include "lib/util/string_wrappers.h"
32 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
33 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
37 #define PY_CHECK_TYPE(type, var, fail) \
38 if (!PyObject_TypeCheck(var, type)) {\
39 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
45 static PyTypeObject *dom_sid_Type = NULL;
46 static PyTypeObject *security_Type = NULL;
47 static PyTypeObject *guid_Type = NULL;
49 static PyTypeObject PySamu;
50 static PyTypeObject PyGroupmap;
51 static PyTypeObject PyPDB;
53 static PyObject *py_pdb_error;
55 void initpassdb(void);
58 /************************** PIDL Autogeneratd ******************************/
60 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
62 TALLOC_CTX *frame = talloc_stackframe();
63 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
64 PyObject *py_logon_time;
66 py_logon_time = PyLong_FromLong(pdb_get_logon_time(sam_acct));
71 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
73 TALLOC_CTX *frame = talloc_stackframe();
74 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
76 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
77 if (!pdb_set_logon_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
85 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
87 TALLOC_CTX *frame = talloc_stackframe();
88 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
89 PyObject *py_logoff_time;
91 py_logoff_time = PyLong_FromLong(pdb_get_logoff_time(sam_acct));
93 return py_logoff_time;
96 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
98 TALLOC_CTX *frame = talloc_stackframe();
99 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
101 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
102 if (!pdb_set_logoff_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
110 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
112 TALLOC_CTX *frame = talloc_stackframe();
113 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
114 PyObject *py_kickoff_time;
116 py_kickoff_time = PyLong_FromLong(pdb_get_kickoff_time(sam_acct));
118 return py_kickoff_time;
121 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
126 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
127 if (!pdb_set_kickoff_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
135 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
137 TALLOC_CTX *frame = talloc_stackframe();
138 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
139 PyObject *py_bad_password_time;
141 py_bad_password_time = PyLong_FromLong(pdb_get_bad_password_time(sam_acct));
143 return py_bad_password_time;
146 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
148 TALLOC_CTX *frame = talloc_stackframe();
149 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
151 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
152 if (!pdb_set_bad_password_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
160 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
162 TALLOC_CTX *frame = talloc_stackframe();
163 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
164 PyObject *py_pass_last_set_time;
166 py_pass_last_set_time = PyLong_FromLong(pdb_get_pass_last_set_time(sam_acct));
168 return py_pass_last_set_time;
171 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
173 TALLOC_CTX *frame = talloc_stackframe();
174 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
176 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
177 if (!pdb_set_pass_last_set_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
185 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
187 TALLOC_CTX *frame = talloc_stackframe();
188 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
189 PyObject *py_pass_can_change_time;
191 py_pass_can_change_time = PyLong_FromLong(pdb_get_pass_can_change_time(sam_acct));
193 return py_pass_can_change_time;
196 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
198 TALLOC_CTX *frame = talloc_stackframe();
199 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
201 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
202 if (!pdb_set_pass_can_change_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
210 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
212 TALLOC_CTX *frame = talloc_stackframe();
213 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
214 PyObject *py_pass_must_change_time;
216 py_pass_must_change_time = PyLong_FromLong(pdb_get_pass_must_change_time(sam_acct));
218 return py_pass_must_change_time;
221 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
223 TALLOC_CTX *frame = talloc_stackframe();
224 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
226 /* TODO: make this not a get/set or give a better exception */
231 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
233 TALLOC_CTX *frame = talloc_stackframe();
234 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
235 PyObject *py_username;
236 const char *username;
238 username = pdb_get_username(sam_acct);
239 if (username == NULL) {
243 py_username = PyUnicode_FromString(username);
248 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
250 TALLOC_CTX *frame = talloc_stackframe();
251 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
253 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
254 if (!pdb_set_username(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
262 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
264 TALLOC_CTX *frame = talloc_stackframe();
265 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
269 domain = pdb_get_domain(sam_acct);
270 if (domain == NULL) {
274 py_domain = PyUnicode_FromString(domain);
279 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
281 TALLOC_CTX *frame = talloc_stackframe();
282 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
284 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
285 if (!pdb_set_domain(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
293 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
295 TALLOC_CTX *frame = talloc_stackframe();
296 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
297 PyObject *py_nt_username;
298 const char *nt_username;
300 nt_username = pdb_get_nt_username(sam_acct);
301 if (nt_username == NULL) {
305 py_nt_username = PyUnicode_FromString(nt_username);
307 return py_nt_username;
310 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
312 TALLOC_CTX *frame = talloc_stackframe();
313 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
315 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
316 if (!pdb_set_nt_username(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
324 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
326 TALLOC_CTX *frame = talloc_stackframe();
327 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
328 PyObject *py_full_name;
329 const char *full_name;
331 full_name = pdb_get_fullname(sam_acct);
332 if (full_name == NULL) {
336 py_full_name = PyUnicode_FromString(full_name);
341 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
343 TALLOC_CTX *frame = talloc_stackframe();
344 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
346 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
347 if (!pdb_set_fullname(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
355 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
357 TALLOC_CTX *frame = talloc_stackframe();
358 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
359 PyObject *py_home_dir;
360 const char *home_dir;
362 home_dir = pdb_get_homedir(sam_acct);
363 if (home_dir == NULL) {
367 py_home_dir = PyUnicode_FromString(home_dir);
372 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
374 TALLOC_CTX *frame = talloc_stackframe();
375 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
377 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
378 if (!pdb_set_homedir(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
386 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
388 TALLOC_CTX *frame = talloc_stackframe();
389 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
390 PyObject *py_dir_drive;
391 const char *dir_drive;
393 dir_drive = pdb_get_dir_drive(sam_acct);
394 if (dir_drive == NULL) {
398 py_dir_drive = PyUnicode_FromString(dir_drive);
403 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
405 TALLOC_CTX *frame = talloc_stackframe();
406 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
408 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
409 if (!pdb_set_dir_drive(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
417 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
419 TALLOC_CTX *frame = talloc_stackframe();
420 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
421 PyObject *py_logon_script;
422 const char *logon_script;
424 logon_script = pdb_get_logon_script(sam_acct);
425 if (logon_script == NULL) {
429 py_logon_script = PyUnicode_FromString(logon_script);
431 return py_logon_script;
434 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
436 TALLOC_CTX *frame = talloc_stackframe();
437 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
439 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
440 if (!pdb_set_logon_script(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
448 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
450 TALLOC_CTX *frame = talloc_stackframe();
451 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
452 PyObject *py_profile_path;
453 const char *profile_path;
455 profile_path = pdb_get_profile_path(sam_acct);
456 if (profile_path == NULL) {
460 py_profile_path = PyUnicode_FromString(profile_path);
462 return py_profile_path;
465 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
467 TALLOC_CTX *frame = talloc_stackframe();
468 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
470 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
471 if (!pdb_set_profile_path(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
479 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
481 TALLOC_CTX *frame = talloc_stackframe();
482 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
483 PyObject *py_acct_desc;
484 const char *acct_desc;
486 acct_desc = pdb_get_acct_desc(sam_acct);
487 if (acct_desc == NULL) {
491 py_acct_desc = PyUnicode_FromString(acct_desc);
496 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
498 TALLOC_CTX *frame = talloc_stackframe();
499 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
501 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
502 if (!pdb_set_acct_desc(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
510 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
512 TALLOC_CTX *frame = talloc_stackframe();
513 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
514 PyObject *py_workstations;
515 const char *workstations;
517 workstations = pdb_get_workstations(sam_acct);
518 if (workstations == NULL) {
522 py_workstations = PyUnicode_FromString(workstations);
524 return py_workstations;
527 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
529 TALLOC_CTX *frame = talloc_stackframe();
530 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
532 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
533 if (!pdb_set_workstations(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
541 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
543 TALLOC_CTX *frame = talloc_stackframe();
544 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
545 PyObject *py_comment;
548 comment = pdb_get_comment(sam_acct);
549 if (comment == NULL) {
553 py_comment = PyUnicode_FromString(comment);
558 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
560 TALLOC_CTX *frame = talloc_stackframe();
561 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
563 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
564 if (!pdb_set_comment(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
572 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
574 TALLOC_CTX *frame = talloc_stackframe();
575 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
576 PyObject *py_munged_dial;
577 const char *munged_dial;
579 munged_dial = pdb_get_munged_dial(sam_acct);
580 if (munged_dial == NULL) {
584 py_munged_dial = PyUnicode_FromString(munged_dial);
586 return py_munged_dial;
589 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
591 TALLOC_CTX *frame = talloc_stackframe();
592 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
594 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
595 if (!pdb_set_munged_dial(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
603 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
605 TALLOC_CTX *frame = talloc_stackframe();
606 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
607 PyObject *py_user_sid;
608 const struct dom_sid *user_sid;
609 struct dom_sid *copy_user_sid;
612 user_sid = pdb_get_user_sid(sam_acct);
613 if(user_sid == NULL) {
617 mem_ctx = talloc_new(NULL);
618 if (mem_ctx == NULL) {
623 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
624 if (copy_user_sid == NULL) {
626 talloc_free(mem_ctx);
631 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
633 talloc_free(mem_ctx);
639 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
641 TALLOC_CTX *frame = talloc_stackframe();
642 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
644 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
645 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
653 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
655 TALLOC_CTX *frame = talloc_stackframe();
656 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
657 const struct dom_sid *group_sid;
658 struct dom_sid *copy_group_sid;
660 group_sid = pdb_get_group_sid(sam_acct);
661 if (group_sid == NULL) {
665 copy_group_sid = dom_sid_dup(NULL, group_sid);
666 if (copy_group_sid == NULL) {
673 return pytalloc_steal(dom_sid_Type, copy_group_sid);
676 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
678 TALLOC_CTX *frame = talloc_stackframe();
679 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
681 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
682 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
690 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
692 TALLOC_CTX *frame = talloc_stackframe();
693 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
697 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
702 py_lm_pw = PyBytes_FromStringAndSize(lm_pw, LM_HASH_LEN);
707 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
709 TALLOC_CTX *frame = talloc_stackframe();
710 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
712 PY_CHECK_TYPE(&PyBytes_Type, value, return -1;);
713 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyBytes_AsString(value), PDB_CHANGED)) {
721 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
723 TALLOC_CTX *frame = talloc_stackframe();
724 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
728 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
733 py_nt_pw = PyBytes_FromStringAndSize(nt_pw, NT_HASH_LEN);
738 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
740 TALLOC_CTX *frame = talloc_stackframe();
741 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
743 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyBytes_AsString(value), PDB_CHANGED)) {
751 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
753 TALLOC_CTX *frame = talloc_stackframe();
754 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
755 PyObject *py_nt_pw_his;
756 const char *nt_pw_his;
759 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
760 if (nt_pw_his == NULL) {
764 py_nt_pw_his = PyBytes_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
769 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
771 TALLOC_CTX *frame = talloc_stackframe();
772 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
777 PyBytes_AsStringAndSize(value, &nt_pw_his, &len);
778 hist_len = len / PW_HISTORY_ENTRY_LEN;
779 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
787 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
789 TALLOC_CTX *frame = talloc_stackframe();
790 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
791 PyObject *py_plaintext_pw;
792 const char *plaintext_pw;
794 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
795 if (plaintext_pw == NULL) {
799 py_plaintext_pw = PyUnicode_FromString(plaintext_pw);
801 return py_plaintext_pw;
804 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
806 TALLOC_CTX *frame = talloc_stackframe();
807 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
809 if (!pdb_set_plaintext_passwd(sam_acct, PyUnicode_AsUTF8(value))) {
817 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
819 TALLOC_CTX *frame = talloc_stackframe();
820 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
821 PyObject *py_acct_ctrl;
823 py_acct_ctrl = PyLong_FromLong(pdb_get_acct_ctrl(sam_acct));
828 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
830 TALLOC_CTX *frame = talloc_stackframe();
831 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
833 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
834 if (!pdb_set_acct_ctrl(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
842 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
844 TALLOC_CTX *frame = talloc_stackframe();
845 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
846 PyObject *py_logon_divs;
848 py_logon_divs = PyLong_FromLong(pdb_get_logon_divs(sam_acct));
850 return py_logon_divs;
853 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
855 TALLOC_CTX *frame = talloc_stackframe();
856 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
858 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
859 if (!pdb_set_logon_divs(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
867 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
869 TALLOC_CTX *frame = talloc_stackframe();
870 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
871 PyObject *py_hours_len;
873 py_hours_len = PyLong_FromLong(pdb_get_hours_len(sam_acct));
878 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
880 TALLOC_CTX *frame = talloc_stackframe();
881 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
883 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
884 if (!pdb_set_hours_len(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
892 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
894 TALLOC_CTX *frame = talloc_stackframe();
895 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
900 hours = (const char *)pdb_get_hours(sam_acct);
905 hours_len = pdb_get_hours_len(sam_acct);
906 if ((py_hours = PyList_New(hours_len)) == NULL) {
912 for (i=0; i<hours_len; i++) {
913 PyList_SetItem(py_hours, i, PyLong_FromLong(hours[i]));
919 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
921 TALLOC_CTX *frame = talloc_stackframe();
922 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
928 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
930 hours_len = PyList_GET_SIZE(value);
932 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
939 for (i=0; i < hours_len; i++) {
940 PY_CHECK_TYPE(&PyLong_Type, PyList_GET_ITEM(value,i), return -1;);
941 hours[i] = PyLong_AsLong(PyList_GET_ITEM(value, i));
944 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
955 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
957 TALLOC_CTX *frame = talloc_stackframe();
958 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
959 PyObject *py_bad_password_count;
961 py_bad_password_count = PyLong_FromLong(pdb_get_bad_password_count(sam_acct));
963 return py_bad_password_count;
966 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
968 TALLOC_CTX *frame = talloc_stackframe();
969 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
971 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
972 if (!pdb_set_bad_password_count(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
980 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
982 TALLOC_CTX *frame = talloc_stackframe();
983 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
984 PyObject *py_logon_count;
986 py_logon_count = PyLong_FromLong(pdb_get_logon_count(sam_acct));
988 return py_logon_count;
991 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
993 TALLOC_CTX *frame = talloc_stackframe();
994 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
996 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
997 if (!pdb_set_logon_count(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
1005 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
1007 TALLOC_CTX *frame = talloc_stackframe();
1008 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1009 PyObject *py_country_code;
1011 py_country_code = PyLong_FromLong(pdb_get_country_code(sam_acct));
1013 return py_country_code;
1016 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
1018 TALLOC_CTX *frame = talloc_stackframe();
1019 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1021 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1022 if (!pdb_set_country_code(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
1030 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
1032 TALLOC_CTX *frame = talloc_stackframe();
1033 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1034 PyObject *py_code_page;
1036 py_code_page = PyLong_FromLong(pdb_get_code_page(sam_acct));
1038 return py_code_page;
1041 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
1043 TALLOC_CTX *frame = talloc_stackframe();
1044 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1046 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1047 if (!pdb_set_code_page(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
1055 static PyGetSetDef py_samu_getsetters[] = {
1057 .name = discard_const_p(char, "logon_time"),
1058 .get = py_samu_get_logon_time,
1059 .set = py_samu_set_logon_time,
1062 .name = discard_const_p(char, "logoff_time"),
1063 .get = py_samu_get_logoff_time,
1064 .set = py_samu_set_logoff_time,
1067 .name = discard_const_p(char, "kickoff_time"),
1068 .get = py_samu_get_kickoff_time,
1069 .set = py_samu_set_kickoff_time,
1072 .name = discard_const_p(char, "bad_password_time"),
1073 .get = py_samu_get_bad_password_time,
1074 .set = py_samu_set_bad_password_time,
1077 .name = discard_const_p(char, "pass_last_set_time"),
1078 .get = py_samu_get_pass_last_set_time,
1079 .set = py_samu_set_pass_last_set_time,
1082 .name = discard_const_p(char, "pass_can_change_time"),
1083 .get = py_samu_get_pass_can_change_time,
1084 .set = py_samu_set_pass_can_change_time,
1087 .name = discard_const_p(char, "pass_must_change_time"),
1088 .get = py_samu_get_pass_must_change_time,
1089 .set = py_samu_set_pass_must_change_time,
1092 .name = discard_const_p(char, "username"),
1093 .get = py_samu_get_username,
1094 .set = py_samu_set_username,
1097 .name = discard_const_p(char, "domain"),
1098 .get = py_samu_get_domain,
1099 .set = py_samu_set_domain,
1102 .name = discard_const_p(char, "nt_username"),
1103 .get = py_samu_get_nt_username,
1104 .set = py_samu_set_nt_username,
1107 .name = discard_const_p(char, "full_name"),
1108 .get = py_samu_get_full_name,
1109 .set = py_samu_set_full_name,
1112 .name = discard_const_p(char, "home_dir"),
1113 .get = py_samu_get_home_dir,
1114 .set = py_samu_set_home_dir,
1117 .name = discard_const_p(char, "dir_drive"),
1118 .get = py_samu_get_dir_drive,
1119 .set = py_samu_set_dir_drive,
1122 .name = discard_const_p(char, "logon_script"),
1123 .get = py_samu_get_logon_script,
1124 .set = py_samu_set_logon_script,
1127 .name = discard_const_p(char, "profile_path"),
1128 .get = py_samu_get_profile_path,
1129 .set = py_samu_set_profile_path,
1132 .name = discard_const_p(char, "acct_desc"),
1133 .get = py_samu_get_acct_desc,
1134 .set = py_samu_set_acct_desc,
1137 .name = discard_const_p(char, "workstations"),
1138 .get = py_samu_get_workstations,
1139 .set = py_samu_set_workstations,
1142 .name = discard_const_p(char, "comment"),
1143 .get = py_samu_get_comment,
1144 .set = py_samu_set_comment,
1147 .name = discard_const_p(char, "munged_dial"),
1148 .get = py_samu_get_munged_dial,
1149 .set = py_samu_set_munged_dial,
1152 .name = discard_const_p(char, "user_sid"),
1153 .get = py_samu_get_user_sid,
1154 .set = py_samu_set_user_sid,
1157 .name = discard_const_p(char, "group_sid"),
1158 .get = py_samu_get_group_sid,
1159 .set = py_samu_set_group_sid,
1162 .name = discard_const_p(char, "lanman_passwd"),
1163 .get = py_samu_get_lanman_passwd,
1164 .set = py_samu_set_lanman_passwd,
1167 .name = discard_const_p(char, "nt_passwd"),
1168 .get = py_samu_get_nt_passwd,
1169 .set = py_samu_set_nt_passwd,
1172 .name = discard_const_p(char, "pw_history"),
1173 .get = py_samu_get_pw_history,
1174 .set = py_samu_set_pw_history,
1177 .name = discard_const_p(char, "plaintext_passwd"),
1178 .get = py_samu_get_plaintext_passwd,
1179 .set = py_samu_set_plaintext_passwd,
1182 .name = discard_const_p(char, "acct_ctrl"),
1183 .get = py_samu_get_acct_ctrl,
1184 .set = py_samu_set_acct_ctrl,
1187 .name = discard_const_p(char, "logon_divs"),
1188 .get = py_samu_get_logon_divs,
1189 .set = py_samu_set_logon_divs,
1192 .name = discard_const_p(char, "hours_len"),
1193 .get = py_samu_get_hours_len,
1194 .set = py_samu_set_hours_len,
1197 .name = discard_const_p(char, "hours"),
1198 .get = py_samu_get_hours,
1199 .set = py_samu_set_hours,
1202 .name = discard_const_p(char, "bad_password_count"),
1203 .get = py_samu_get_bad_password_count,
1204 .set = py_samu_set_bad_password_count,
1207 .name = discard_const_p(char, "logon_count"),
1208 .get = py_samu_get_logon_count,
1209 .set = py_samu_set_logon_count,
1212 .name = discard_const_p(char, "country_code"),
1213 .get = py_samu_get_country_code,
1214 .set = py_samu_set_country_code,
1217 .name = discard_const_p(char, "code_page"),
1218 .get = py_samu_get_code_page,
1219 .set = py_samu_set_code_page,
1227 /************************** PIDL Autogeneratd ******************************/
1229 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1231 TALLOC_CTX *frame = talloc_stackframe();
1232 struct samu *sam_acct;
1234 sam_acct = samu_new(NULL);
1242 return pytalloc_steal(type, sam_acct);
1245 static PyTypeObject PySamu = {
1246 .tp_name = "passdb.Samu",
1247 .tp_getset = py_samu_getsetters,
1249 .tp_new = py_samu_new,
1250 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1251 .tp_doc = "Samu() -> samu object\n",
1255 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1257 TALLOC_CTX *frame = talloc_stackframe();
1258 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1261 py_gid = Py_BuildValue("i", group_map->gid);
1266 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1268 TALLOC_CTX *frame = talloc_stackframe();
1269 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1271 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1272 group_map->gid = PyLong_AsLong(value);
1277 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1279 TALLOC_CTX *frame = talloc_stackframe();
1280 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1282 struct dom_sid *group_sid;
1283 TALLOC_CTX *mem_ctx;
1285 mem_ctx = talloc_new(NULL);
1286 if (mem_ctx == NULL) {
1292 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1293 if (group_sid == NULL) {
1295 talloc_free(mem_ctx);
1300 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1302 talloc_free(mem_ctx);
1308 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1310 TALLOC_CTX *frame = talloc_stackframe();
1311 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1313 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1314 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1319 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1321 TALLOC_CTX *frame = talloc_stackframe();
1322 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1323 PyObject *py_sid_name_use;
1325 py_sid_name_use = PyLong_FromLong(group_map->sid_name_use);
1327 return py_sid_name_use;
1330 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1332 TALLOC_CTX *frame = talloc_stackframe();
1333 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1335 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1336 group_map->sid_name_use = PyLong_AsLong(value);
1341 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1343 TALLOC_CTX *frame = talloc_stackframe();
1344 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1345 PyObject *py_nt_name;
1346 if (group_map->nt_name == NULL) {
1347 py_nt_name = Py_None;
1348 Py_INCREF(py_nt_name);
1350 py_nt_name = PyUnicode_FromString(group_map->nt_name);
1356 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1358 TALLOC_CTX *frame = talloc_stackframe();
1359 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1361 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
1362 if (group_map->nt_name != NULL) {
1363 TALLOC_FREE(group_map->nt_name);
1365 if (value == Py_None) {
1366 group_map->nt_name = talloc_strdup(group_map, "");
1368 group_map->nt_name = talloc_strdup(group_map,
1369 PyUnicode_AsUTF8(value));
1372 if (group_map->nt_name == NULL) {
1378 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1380 TALLOC_CTX *frame = talloc_stackframe();
1381 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1382 PyObject *py_comment;
1383 if (group_map->comment == NULL) {
1384 py_comment = Py_None;
1385 Py_INCREF(py_comment);
1387 py_comment = PyUnicode_FromString(group_map->comment);
1393 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1395 TALLOC_CTX *frame = talloc_stackframe();
1396 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1398 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
1399 if (group_map->comment != NULL) {
1400 TALLOC_FREE(group_map->comment);
1402 if (value == Py_None) {
1403 group_map->comment = talloc_strdup(group_map, "");
1405 group_map->comment = talloc_strdup(group_map,
1406 PyUnicode_AsUTF8(value));
1409 if (group_map->comment == NULL) {
1415 static PyGetSetDef py_groupmap_getsetters[] = {
1417 .name = discard_const_p(char, "gid"),
1418 .get = py_groupmap_get_gid,
1419 .set = py_groupmap_set_gid,
1422 .name = discard_const_p(char, "sid"),
1423 .get = py_groupmap_get_sid,
1424 .set = py_groupmap_set_sid,
1427 .name = discard_const_p(char, "sid_name_use"),
1428 .get = py_groupmap_get_sid_name_use,
1429 .set = py_groupmap_set_sid_name_use,
1432 .name = discard_const_p(char, "nt_name"),
1433 .get = py_groupmap_get_nt_name,
1434 .set = py_groupmap_set_nt_name,
1437 .name = discard_const_p(char, "comment"),
1438 .get = py_groupmap_get_comment,
1439 .set = py_groupmap_set_comment,
1446 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1448 TALLOC_CTX *frame = talloc_stackframe();
1449 GROUP_MAP *group_map;
1450 TALLOC_CTX *mem_ctx;
1451 PyObject *py_group_map;
1453 mem_ctx = talloc_new(NULL);
1454 if (mem_ctx == NULL) {
1460 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1461 if (group_map == NULL) {
1463 talloc_free(mem_ctx);
1468 py_group_map = pytalloc_steal(type, group_map);
1469 if (py_group_map == NULL) {
1471 talloc_free(mem_ctx);
1476 talloc_free(mem_ctx);
1479 return py_group_map;
1483 static PyTypeObject PyGroupmap = {
1484 .tp_name = "passdb.Groupmap",
1485 .tp_getset = py_groupmap_getsetters,
1487 .tp_new = py_groupmap_new,
1488 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1489 .tp_doc = "Groupmap() -> group map object\n",
1493 static PyObject *py_pdb_domain_info(PyObject *self, PyObject *args)
1495 TALLOC_CTX *frame = talloc_stackframe();
1496 struct pdb_methods *methods;
1497 struct pdb_domain_info *domain_info;
1498 PyObject *py_domain_info;
1499 struct dom_sid *sid;
1501 PyObject *py_dom_sid = NULL;
1502 PyObject *py_guid = NULL;
1504 methods = pytalloc_get_ptr(self);
1506 domain_info = methods->get_domain_info(methods, frame);
1507 if (! domain_info) {
1511 sid = dom_sid_dup(frame, &domain_info->sid);
1518 guid = talloc(frame, struct GUID);
1524 *guid = domain_info->guid;
1526 py_dom_sid = pytalloc_steal(dom_sid_Type, sid);
1527 py_guid = pytalloc_steal(guid_Type, guid);
1529 py_domain_info = Py_BuildValue(
1530 "{s:s, s:s, s:s, s:O, s:O}",
1531 "name", domain_info->name,
1532 "dns_domain", domain_info->dns_domain,
1533 "dns_forest", domain_info->dns_forest,
1534 "dom_sid", py_dom_sid,
1538 Py_CLEAR(py_dom_sid);
1541 return py_domain_info;
1545 static PyObject *py_pdb_getsampwnam(PyObject *self, PyObject *args)
1547 TALLOC_CTX *frame = talloc_stackframe();
1549 const char *username;
1550 struct pdb_methods *methods;
1551 struct samu *sam_acct;
1552 PyObject *py_sam_acct;
1554 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1559 methods = pytalloc_get_ptr(self);
1561 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1562 if (py_sam_acct == NULL) {
1567 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1569 status = methods->getsampwnam(methods, sam_acct, username);
1570 if (!NT_STATUS_IS_OK(status)) {
1571 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1573 NT_STATUS_V(status),
1574 get_friendly_nt_error_msg(status));
1575 Py_DECREF(py_sam_acct);
1584 static PyObject *py_pdb_getsampwsid(PyObject *self, PyObject *args)
1586 TALLOC_CTX *frame = talloc_stackframe();
1588 struct pdb_methods *methods;
1589 struct samu *sam_acct;
1590 PyObject *py_sam_acct;
1591 PyObject *py_user_sid;
1593 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1598 methods = pytalloc_get_ptr(self);
1600 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1601 if (py_sam_acct == NULL) {
1606 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1608 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1609 if (!NT_STATUS_IS_OK(status)) {
1610 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1611 NT_STATUS_V(status),
1612 get_friendly_nt_error_msg(status));
1613 Py_DECREF(py_sam_acct);
1622 static PyObject *py_pdb_create_user(PyObject *self, PyObject *args)
1624 TALLOC_CTX *frame = talloc_stackframe();
1626 struct pdb_methods *methods;
1627 const char *username;
1628 unsigned int acct_flags;
1631 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1636 methods = pytalloc_get_ptr(self);
1638 status = methods->create_user(methods, frame, username, acct_flags, &rid);
1639 if (!NT_STATUS_IS_OK(status)) {
1640 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1642 NT_STATUS_V(status),
1643 get_friendly_nt_error_msg(status));
1649 return PyLong_FromLong(rid);
1652 static PyObject *py_pdb_delete_user(PyObject *self, PyObject *args)
1654 TALLOC_CTX *frame = talloc_stackframe();
1656 struct pdb_methods *methods;
1657 struct samu *sam_acct;
1658 PyObject *py_sam_acct;
1660 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1665 methods = pytalloc_get_ptr(self);
1667 sam_acct = pytalloc_get_ptr(py_sam_acct);
1669 status = methods->delete_user(methods, frame, sam_acct);
1670 if (!NT_STATUS_IS_OK(status)) {
1671 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1672 NT_STATUS_V(status),
1673 get_friendly_nt_error_msg(status));
1682 static PyObject *py_pdb_add_sam_account(PyObject *self, PyObject *args)
1684 TALLOC_CTX *frame = talloc_stackframe();
1686 struct pdb_methods *methods;
1687 struct samu *sam_acct;
1688 PyObject *py_sam_acct;
1690 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1695 methods = pytalloc_get_ptr(self);
1697 sam_acct = pytalloc_get_ptr(py_sam_acct);
1699 status = methods->add_sam_account(methods, sam_acct);
1700 if (!NT_STATUS_IS_OK(status)) {
1701 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1703 NT_STATUS_V(status),
1704 get_friendly_nt_error_msg(status));
1713 static PyObject *py_pdb_update_sam_account(PyObject *self, PyObject *args)
1715 TALLOC_CTX *frame = talloc_stackframe();
1717 struct pdb_methods *methods;
1718 struct samu *sam_acct;
1719 PyObject *py_sam_acct;
1721 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1726 methods = pytalloc_get_ptr(self);
1728 sam_acct = pytalloc_get_ptr(py_sam_acct);
1730 status = methods->update_sam_account(methods, sam_acct);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1733 NT_STATUS_V(status),
1734 get_friendly_nt_error_msg(status));
1743 static PyObject *py_pdb_delete_sam_account(PyObject *self, PyObject *args)
1745 TALLOC_CTX *frame = talloc_stackframe();
1747 struct pdb_methods *methods;
1748 struct samu *sam_acct;
1749 PyObject *py_sam_acct;
1751 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1756 methods = pytalloc_get_ptr(self);
1758 sam_acct = pytalloc_get_ptr(py_sam_acct);
1760 status = methods->delete_sam_account(methods, sam_acct);
1761 if (!NT_STATUS_IS_OK(status)) {
1762 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1763 NT_STATUS_V(status),
1764 get_friendly_nt_error_msg(status));
1773 static PyObject *py_pdb_rename_sam_account(PyObject *self, PyObject *args)
1775 TALLOC_CTX *frame = talloc_stackframe();
1777 struct pdb_methods *methods;
1778 struct samu *sam_acct;
1779 const char *new_username;
1780 PyObject *py_sam_acct;
1782 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1788 methods = pytalloc_get_ptr(self);
1790 sam_acct = pytalloc_get_ptr(py_sam_acct);
1792 status = methods->rename_sam_account(methods, sam_acct, new_username);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1795 NT_STATUS_V(status),
1796 get_friendly_nt_error_msg(status));
1806 static PyObject *py_pdb_getgrsid(PyObject *self, PyObject *args)
1808 TALLOC_CTX *frame = talloc_stackframe();
1810 struct pdb_methods *methods;
1811 GROUP_MAP *group_map;
1812 struct dom_sid *domain_sid;
1813 PyObject *py_domain_sid, *py_group_map;
1815 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1820 methods = pytalloc_get_ptr(self);
1822 domain_sid = pytalloc_get_ptr(py_domain_sid);
1824 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1825 if (py_group_map == NULL) {
1831 group_map = pytalloc_get_ptr(py_group_map);
1833 status = methods->getgrsid(methods, group_map, *domain_sid);
1834 if (!NT_STATUS_IS_OK(status)) {
1835 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1836 NT_STATUS_V(status),
1837 get_friendly_nt_error_msg(status));
1843 return py_group_map;
1847 static PyObject *py_pdb_getgrgid(PyObject *self, PyObject *args)
1849 TALLOC_CTX *frame = talloc_stackframe();
1851 struct pdb_methods *methods;
1852 GROUP_MAP *group_map;
1853 PyObject *py_group_map;
1854 unsigned int gid_value;
1856 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1861 methods = pytalloc_get_ptr(self);
1863 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1864 if (py_group_map == NULL) {
1870 group_map = pytalloc_get_ptr(py_group_map);
1872 status = methods->getgrgid(methods, group_map, gid_value);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1875 NT_STATUS_V(status),
1876 get_friendly_nt_error_msg(status));
1882 return py_group_map;
1886 static PyObject *py_pdb_getgrnam(PyObject *self, PyObject *args)
1888 TALLOC_CTX *frame = talloc_stackframe();
1890 struct pdb_methods *methods;
1891 GROUP_MAP *group_map;
1892 PyObject *py_group_map;
1893 const char *groupname;
1895 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1900 methods = pytalloc_get_ptr(self);
1902 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1903 if (py_group_map == NULL) {
1909 group_map = pytalloc_get_ptr(py_group_map);
1911 status = methods->getgrnam(methods, group_map, groupname);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1914 NT_STATUS_V(status),
1915 get_friendly_nt_error_msg(status));
1921 return py_group_map;
1925 static PyObject *py_pdb_create_dom_group(PyObject *self, PyObject *args)
1927 TALLOC_CTX *frame = talloc_stackframe();
1929 struct pdb_methods *methods;
1930 const char *groupname;
1933 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1938 methods = pytalloc_get_ptr(self);
1940 status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1941 if (!NT_STATUS_IS_OK(status)) {
1942 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1944 NT_STATUS_V(status),
1945 get_friendly_nt_error_msg(status));
1951 return PyLong_FromLong(group_rid);
1955 static PyObject *py_pdb_delete_dom_group(PyObject *self, PyObject *args)
1957 TALLOC_CTX *frame = talloc_stackframe();
1959 struct pdb_methods *methods;
1960 unsigned int group_rid;
1962 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1967 methods = pytalloc_get_ptr(self);
1969 status = methods->delete_dom_group(methods, frame, group_rid);
1970 if (!NT_STATUS_IS_OK(status)) {
1971 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1973 NT_STATUS_V(status),
1974 get_friendly_nt_error_msg(status));
1984 static PyObject *py_pdb_add_group_mapping_entry(PyObject *self, PyObject *args)
1986 TALLOC_CTX *frame = talloc_stackframe();
1988 struct pdb_methods *methods;
1989 PyObject *py_group_map;
1990 GROUP_MAP *group_map;
1992 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1997 methods = pytalloc_get_ptr(self);
1999 group_map = pytalloc_get_ptr(py_group_map);
2001 status = methods->add_group_mapping_entry(methods, group_map);
2002 if (!NT_STATUS_IS_OK(status)) {
2003 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
2004 NT_STATUS_V(status),
2005 get_friendly_nt_error_msg(status));
2015 static PyObject *py_pdb_update_group_mapping_entry(PyObject *self, PyObject *args)
2017 TALLOC_CTX *frame = talloc_stackframe();
2019 struct pdb_methods *methods;
2020 PyObject *py_group_map;
2021 GROUP_MAP *group_map;
2023 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
2028 methods = pytalloc_get_ptr(self);
2030 group_map = pytalloc_get_ptr(py_group_map);
2032 status = methods->update_group_mapping_entry(methods, group_map);
2033 if (!NT_STATUS_IS_OK(status)) {
2034 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
2035 NT_STATUS_V(status),
2036 get_friendly_nt_error_msg(status));
2046 static PyObject *py_pdb_delete_group_mapping_entry(PyObject *self, PyObject *args)
2048 TALLOC_CTX *frame = talloc_stackframe();
2050 struct pdb_methods *methods;
2051 PyObject *py_group_sid;
2052 struct dom_sid *group_sid;
2054 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
2059 methods = pytalloc_get_ptr(self);
2061 group_sid = pytalloc_get_ptr(py_group_sid);
2063 status = methods->delete_group_mapping_entry(methods, *group_sid);
2064 if (!NT_STATUS_IS_OK(status)) {
2065 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
2066 NT_STATUS_V(status),
2067 get_friendly_nt_error_msg(status));
2077 static PyObject *py_pdb_enum_group_mapping(PyObject *self, PyObject *args)
2079 TALLOC_CTX *frame = talloc_stackframe();
2081 struct pdb_methods *methods;
2082 enum lsa_SidType sid_name_use;
2083 int lsa_sidtype_value = SID_NAME_UNKNOWN;
2085 PyObject *py_domain_sid = Py_None;
2086 struct dom_sid *domain_sid = NULL;
2087 GROUP_MAP **gmap = NULL;
2088 GROUP_MAP *group_map;
2089 size_t i, num_entries;
2090 PyObject *py_gmap_list, *py_group_map;
2092 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
2093 &lsa_sidtype_value, &unix_only)) {
2098 methods = pytalloc_get_ptr(self);
2100 sid_name_use = lsa_sidtype_value;
2102 if (py_domain_sid != Py_None) {
2103 domain_sid = pytalloc_get_ptr(py_domain_sid);
2106 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
2107 &gmap, &num_entries, unix_only);
2108 if (!NT_STATUS_IS_OK(status)) {
2109 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
2110 NT_STATUS_V(status),
2111 get_friendly_nt_error_msg(status));
2116 py_gmap_list = PyList_New(0);
2117 if (py_gmap_list == NULL) {
2123 for(i=0; i<num_entries; i++) {
2124 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
2127 group_map = pytalloc_get_ptr(py_group_map);
2128 *group_map = *gmap[i];
2129 talloc_steal(group_map, gmap[i]->nt_name);
2130 talloc_steal(group_map, gmap[i]->comment);
2132 res = PyList_Append(py_gmap_list, py_group_map);
2133 Py_CLEAR(py_group_map);
2135 Py_CLEAR(py_gmap_list);
2145 return py_gmap_list;
2149 static PyObject *py_pdb_enum_group_members(PyObject *self, PyObject *args)
2151 TALLOC_CTX *frame = talloc_stackframe();
2153 struct pdb_methods *methods;
2154 PyObject *py_group_sid;
2155 struct dom_sid *group_sid;
2156 uint32_t *member_rids;
2157 size_t i, num_members;
2158 PyObject *py_sid_list;
2159 struct dom_sid *domain_sid, *member_sid;
2161 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
2166 methods = pytalloc_get_ptr(self);
2168 group_sid = pytalloc_get_ptr(py_group_sid);
2170 status = methods->enum_group_members(methods, frame, group_sid,
2171 &member_rids, &num_members);
2172 if (!NT_STATUS_IS_OK(status)) {
2173 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
2174 NT_STATUS_V(status),
2175 get_friendly_nt_error_msg(status));
2180 py_sid_list = PyList_New(0);
2181 if (py_sid_list == NULL) {
2187 domain_sid = get_global_sam_sid();
2189 for(i=0; i<num_members; i++) {
2191 PyObject *py_member_sid = NULL;
2192 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2193 py_member_sid = pytalloc_steal(dom_sid_Type, member_sid);
2194 res = PyList_Append(py_sid_list,
2196 Py_CLEAR(py_member_sid);
2199 Py_CLEAR(py_sid_list);
2209 static PyObject *py_pdb_enum_group_memberships(PyObject *self, PyObject *args)
2211 TALLOC_CTX *frame = talloc_stackframe();
2213 struct pdb_methods *methods;
2216 struct samu *sam_acct;
2217 PyObject *py_sam_acct;
2218 PyObject *py_sid_list;
2219 struct dom_sid *user_group_sids = NULL;
2220 gid_t *user_group_ids = NULL;
2221 uint32_t num_groups = 0;
2223 if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2228 methods = pytalloc_get_ptr(self);
2230 sam_acct = pytalloc_get_ptr(py_sam_acct);
2232 status = methods->enum_group_memberships(methods, frame, sam_acct,
2233 &user_group_sids, &user_group_ids, &num_groups);
2234 if (!NT_STATUS_IS_OK(status)) {
2235 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2236 NT_STATUS_V(status),
2237 get_friendly_nt_error_msg(status));
2242 py_sid_list = PyList_New(0);
2243 if (py_sid_list == NULL) {
2249 for(i=0; i<num_groups; i++) {
2251 pytalloc_steal(dom_sid_Type,
2252 dom_sid_dup(NULL, &user_group_sids[i]));
2253 PyList_Append(py_sid_list, py_sid);
2262 static PyObject *py_pdb_add_groupmem(PyObject *self, PyObject *args)
2264 TALLOC_CTX *frame = talloc_stackframe();
2266 struct pdb_methods *methods;
2267 uint32_t group_rid, member_rid;
2269 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2274 methods = pytalloc_get_ptr(self);
2276 status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2277 if (!NT_STATUS_IS_OK(status)) {
2278 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2279 NT_STATUS_V(status),
2280 get_friendly_nt_error_msg(status));
2290 static PyObject *py_pdb_del_groupmem(PyObject *self, PyObject *args)
2292 TALLOC_CTX *frame = talloc_stackframe();
2294 struct pdb_methods *methods;
2295 uint32_t group_rid, member_rid;
2297 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2302 methods = pytalloc_get_ptr(self);
2304 status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2305 if (!NT_STATUS_IS_OK(status)) {
2306 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2307 NT_STATUS_V(status),
2308 get_friendly_nt_error_msg(status));
2318 static PyObject *py_pdb_create_alias(PyObject *self, PyObject *args)
2320 TALLOC_CTX *frame = talloc_stackframe();
2322 struct pdb_methods *methods;
2323 const char *alias_name;
2326 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2331 methods = pytalloc_get_ptr(self);
2333 status = methods->create_alias(methods, alias_name, &rid);
2334 if (!NT_STATUS_IS_OK(status)) {
2335 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2337 NT_STATUS_V(status),
2338 get_friendly_nt_error_msg(status));
2344 return PyLong_FromLong(rid);
2348 static PyObject *py_pdb_delete_alias(PyObject *self, PyObject *args)
2350 TALLOC_CTX *frame = talloc_stackframe();
2352 struct pdb_methods *methods;
2353 PyObject *py_alias_sid;
2354 struct dom_sid *alias_sid;
2356 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2361 methods = pytalloc_get_ptr(self);
2363 alias_sid = pytalloc_get_ptr(py_alias_sid);
2365 status = methods->delete_alias(methods, alias_sid);
2366 if (!NT_STATUS_IS_OK(status)) {
2367 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2368 NT_STATUS_V(status),
2369 get_friendly_nt_error_msg(status));
2379 static PyObject *py_pdb_get_aliasinfo(PyObject *self, PyObject *args)
2381 TALLOC_CTX *frame = talloc_stackframe();
2383 struct pdb_methods *methods;
2384 PyObject *py_alias_sid;
2385 struct dom_sid *alias_sid;
2386 struct acct_info *alias_info;
2387 PyObject *py_alias_info;
2389 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2394 methods = pytalloc_get_ptr(self);
2396 alias_sid = pytalloc_get_ptr(py_alias_sid);
2398 alias_info = talloc_zero(frame, struct acct_info);
2405 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2406 if (!NT_STATUS_IS_OK(status)) {
2407 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2408 NT_STATUS_V(status),
2409 get_friendly_nt_error_msg(status));
2414 py_alias_info = Py_BuildValue(
2416 "acct_name", alias_info->acct_name,
2417 "acct_desc", alias_info->acct_desc,
2418 "rid", alias_info->rid);
2421 return py_alias_info;
2425 static PyObject *py_pdb_set_aliasinfo(PyObject *self, PyObject *args)
2427 TALLOC_CTX *frame = talloc_stackframe();
2429 struct pdb_methods *methods;
2430 PyObject *py_alias_sid, *py_alias_info;
2431 struct dom_sid *alias_sid;
2432 struct acct_info alias_info;
2434 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2440 methods = pytalloc_get_ptr(self);
2442 alias_sid = pytalloc_get_ptr(py_alias_sid);
2444 alias_info.acct_name = talloc_strdup(frame, PyUnicode_AsUTF8(PyDict_GetItemString(py_alias_info, "acct_name")));
2445 if (alias_info.acct_name == NULL) {
2446 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2450 alias_info.acct_desc = talloc_strdup(frame, PyUnicode_AsUTF8(PyDict_GetItemString(py_alias_info, "acct_desc")));
2451 if (alias_info.acct_desc == NULL) {
2452 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2457 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2458 if (!NT_STATUS_IS_OK(status)) {
2459 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2460 NT_STATUS_V(status),
2461 get_friendly_nt_error_msg(status));
2471 static PyObject *py_pdb_add_aliasmem(PyObject *self, PyObject *args)
2473 TALLOC_CTX *frame = talloc_stackframe();
2475 struct pdb_methods *methods;
2476 PyObject *py_alias_sid, *py_member_sid;
2477 struct dom_sid *alias_sid, *member_sid;
2479 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2480 dom_sid_Type, &py_member_sid)) {
2485 methods = pytalloc_get_ptr(self);
2487 alias_sid = pytalloc_get_ptr(py_alias_sid);
2488 member_sid = pytalloc_get_ptr(py_member_sid);
2490 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2491 if (!NT_STATUS_IS_OK(status)) {
2492 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2493 NT_STATUS_V(status),
2494 get_friendly_nt_error_msg(status));
2504 static PyObject *py_pdb_del_aliasmem(PyObject *self, PyObject *args)
2506 TALLOC_CTX *frame = talloc_stackframe();
2508 struct pdb_methods *methods;
2509 PyObject *py_alias_sid, *py_member_sid;
2510 const struct dom_sid *alias_sid, *member_sid;
2512 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2513 dom_sid_Type, &py_member_sid)) {
2518 methods = pytalloc_get_ptr(self);
2520 alias_sid = pytalloc_get_ptr(py_alias_sid);
2521 member_sid = pytalloc_get_ptr(py_member_sid);
2523 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2524 if (!NT_STATUS_IS_OK(status)) {
2525 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2526 NT_STATUS_V(status),
2527 get_friendly_nt_error_msg(status));
2537 static PyObject *py_pdb_enum_aliasmem(PyObject *self, PyObject *args)
2539 TALLOC_CTX *frame = talloc_stackframe();
2541 struct pdb_methods *methods;
2542 PyObject *py_alias_sid;
2543 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2544 PyObject *py_member_list, *py_member_sid;
2545 size_t i, num_members;
2547 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2552 methods = pytalloc_get_ptr(self);
2554 alias_sid = pytalloc_get_ptr(py_alias_sid);
2556 status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2557 if (!NT_STATUS_IS_OK(status)) {
2558 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2559 NT_STATUS_V(status),
2560 get_friendly_nt_error_msg(status));
2565 py_member_list = PyList_New(0);
2566 if (py_member_list == NULL) {
2572 for(i=0; i<num_members; i++) {
2574 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2575 if (py_member_sid == NULL) {
2577 Py_CLEAR(py_member_list);
2581 tmp_sid = pytalloc_get_ptr(py_member_sid);
2582 *tmp_sid = member_sid[i];
2583 res = PyList_Append(py_member_list, py_member_sid);
2584 Py_CLEAR(py_member_sid);
2586 Py_CLEAR(py_member_list);
2593 return py_member_list;
2597 static PyObject *py_pdb_get_account_policy(PyObject *self, PyObject *unused)
2599 TALLOC_CTX *frame = talloc_stackframe();
2601 struct pdb_methods *methods;
2602 PyObject *py_acct_policy;
2606 enum pdb_policy_type type;
2608 methods = pytalloc_get_ptr(self);
2610 py_acct_policy = PyDict_New();
2611 if (py_acct_policy == NULL) {
2617 account_policy_names_list(frame, &names, &count);
2618 for (i=0; i<count; i++) {
2619 type = account_policy_name_to_typenum(names[i]);
2620 status = methods->get_account_policy(methods, type, &value);
2621 if (NT_STATUS_IS_OK(status)) {
2623 PyObject *py_value = Py_BuildValue("i", value);
2624 if (py_value == NULL) {
2625 Py_CLEAR(py_acct_policy);
2628 res = PyDict_SetItemString(py_acct_policy,
2633 Py_CLEAR(py_acct_policy);
2640 return py_acct_policy;
2644 static PyObject *py_pdb_set_account_policy(PyObject *self, PyObject *args)
2646 TALLOC_CTX *frame = talloc_stackframe();
2648 struct pdb_methods *methods;
2649 PyObject *py_acct_policy, *py_value;
2652 enum pdb_policy_type type;
2654 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2659 methods = pytalloc_get_ptr(self);
2661 account_policy_names_list(frame, &names, &count);
2662 for (i=0; i<count; i++) {
2663 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2664 type = account_policy_name_to_typenum(names[i]);
2665 status = methods->set_account_policy(methods, type, PyLong_AsLong(py_value));
2666 if (!NT_STATUS_IS_OK(status)) {
2667 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2669 NT_STATUS_V(status),
2670 get_friendly_nt_error_msg(status));
2679 static PyObject *py_pdb_search_users(PyObject *self, PyObject *args)
2681 TALLOC_CTX *frame = talloc_stackframe();
2682 struct pdb_methods *methods;
2683 unsigned int acct_flags;
2684 struct pdb_search *search;
2685 struct samr_displayentry *entry;
2686 PyObject *py_userlist, *py_dict;
2688 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2693 methods = pytalloc_get_ptr(self);
2695 search = talloc_zero(frame, struct pdb_search);
2696 if (search == NULL) {
2702 if (!methods->search_users(methods, search, acct_flags)) {
2703 PyErr_Format(py_pdb_error, "Unable to search users");
2708 entry = talloc_zero(frame, struct samr_displayentry);
2709 if (entry == NULL) {
2715 py_userlist = PyList_New(0);
2716 if (py_userlist == NULL) {
2722 while (search->next_entry(search, entry)) {
2724 py_dict = Py_BuildValue(
2725 "{s:l, s:l, s:l, s:s, s:s, s:s}",
2728 "acct_flags", entry->acct_flags,
2729 "account_name", entry->account_name,
2730 "fullname", entry->fullname,
2731 "description", entry->description);
2732 if (py_dict == NULL) {
2733 Py_CLEAR(py_userlist);
2737 res = PyList_Append(py_userlist, py_dict);
2740 Py_CLEAR(py_userlist);
2744 search->search_end(search);
2752 static PyObject *py_pdb_search_groups(PyObject *self, PyObject *unused)
2754 TALLOC_CTX *frame = talloc_stackframe();
2755 struct pdb_methods *methods;
2756 struct pdb_search *search;
2757 struct samr_displayentry *entry;
2758 PyObject *py_grouplist, *py_dict;
2760 methods = pytalloc_get_ptr(self);
2762 search = talloc_zero(frame, struct pdb_search);
2763 if (search == NULL) {
2769 if (!methods->search_groups(methods, search)) {
2770 PyErr_Format(py_pdb_error, "Unable to search groups");
2775 entry = talloc_zero(frame, struct samr_displayentry);
2776 if (entry == NULL) {
2782 py_grouplist = PyList_New(0);
2783 if (py_grouplist == NULL) {
2789 while (search->next_entry(search, entry)) {
2791 py_dict = Py_BuildValue(
2792 "{s:l, s:l, s:l, s:s, s:s, s:s}",
2795 "acct_flags", entry->acct_flags,
2796 "account_name", entry->account_name,
2797 "fullname", entry->fullname,
2798 "description", entry->description);
2800 if (py_dict == NULL) {
2801 Py_CLEAR(py_grouplist);
2805 res = PyList_Append(py_grouplist, py_dict);
2808 Py_CLEAR(py_grouplist);
2812 search->search_end(search);
2815 return py_grouplist;
2819 static PyObject *py_pdb_search_aliases(PyObject *self, PyObject *args)
2821 TALLOC_CTX *frame = talloc_stackframe();
2822 struct pdb_methods *methods;
2823 struct pdb_search *search;
2824 struct samr_displayentry *entry;
2825 PyObject *py_aliaslist, *py_dict;
2826 PyObject *py_domain_sid = Py_None;
2827 struct dom_sid *domain_sid = NULL;
2829 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2834 methods = pytalloc_get_ptr(self);
2836 if (py_domain_sid != Py_None) {
2837 domain_sid = pytalloc_get_ptr(py_domain_sid);
2840 search = talloc_zero(frame, struct pdb_search);
2841 if (search == NULL) {
2847 if (!methods->search_aliases(methods, search, domain_sid)) {
2848 PyErr_Format(py_pdb_error, "Unable to search aliases");
2853 entry = talloc_zero(frame, struct samr_displayentry);
2854 if (entry == NULL) {
2860 py_aliaslist = PyList_New(0);
2861 if (py_aliaslist == NULL) {
2867 while (search->next_entry(search, entry)) {
2870 py_dict = Py_BuildValue(
2871 "{s:l, s:l, s:l, s:s, s:s, s:s}",
2874 "acct_flags", entry->acct_flags,
2875 "account_name", entry->account_name,
2876 "fullname", entry->fullname,
2877 "description", entry->description);
2879 if (py_dict == NULL) {
2880 Py_CLEAR(py_aliaslist);
2883 res = PyList_Append(py_aliaslist, py_dict);
2886 Py_CLEAR(py_aliaslist);
2890 search->search_end(search);
2893 return py_aliaslist;
2897 static PyObject *py_pdb_uid_to_sid(PyObject *self, PyObject *args)
2899 TALLOC_CTX *frame = talloc_stackframe();
2900 struct pdb_methods *methods;
2903 struct dom_sid user_sid, *copy_user_sid;
2904 PyObject *py_user_sid;
2906 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2911 methods = pytalloc_get_ptr(self);
2914 id.type = ID_TYPE_UID;
2916 if (!methods->id_to_sid(methods, &id, &user_sid)) {
2917 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2922 copy_user_sid = dom_sid_dup(frame, &user_sid);
2923 if (copy_user_sid == NULL) {
2929 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2936 static PyObject *py_pdb_gid_to_sid(PyObject *self, PyObject *args)
2938 TALLOC_CTX *frame = talloc_stackframe();
2939 struct pdb_methods *methods;
2942 struct dom_sid group_sid, *copy_group_sid;
2943 PyObject *py_group_sid;
2945 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2951 id.type = ID_TYPE_GID;
2953 methods = pytalloc_get_ptr(self);
2955 if (!methods->id_to_sid(methods, &id, &group_sid)) {
2956 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2961 copy_group_sid = dom_sid_dup(frame, &group_sid);
2962 if (copy_group_sid == NULL) {
2968 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2971 return py_group_sid;
2975 static PyObject *py_pdb_sid_to_id(PyObject *self, PyObject *args)
2977 TALLOC_CTX *frame = talloc_stackframe();
2978 struct pdb_methods *methods;
2980 struct dom_sid *sid;
2983 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2988 methods = pytalloc_get_ptr(self);
2990 sid = pytalloc_get_ptr(py_sid);
2992 if (!methods->sid_to_id(methods, sid, &id)) {
2993 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2999 return Py_BuildValue("(II)", id.id, id.type);
3003 static PyObject *py_pdb_new_rid(PyObject *self, PyObject *unused)
3005 TALLOC_CTX *frame = talloc_stackframe();
3006 struct pdb_methods *methods;
3009 methods = pytalloc_get_ptr(self);
3011 if (!methods->new_rid(methods, &rid)) {
3012 PyErr_Format(py_pdb_error, "Unable to get new rid");
3018 return PyLong_FromLong(rid);
3022 static PyObject *py_pdb_get_trusteddom_pw(PyObject *self, PyObject *args)
3024 TALLOC_CTX *frame = talloc_stackframe();
3025 struct pdb_methods *methods;
3028 struct dom_sid sid, *copy_sid;
3030 time_t last_set_time;
3033 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
3038 methods = pytalloc_get_ptr(self);
3040 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
3041 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
3046 copy_sid = dom_sid_dup(frame, &sid);
3047 if (copy_sid == NULL) {
3053 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
3054 if (py_sid == NULL) {
3060 py_value = Py_BuildValue(
3064 "last_set_tim", last_set_time);
3072 static PyObject *py_pdb_set_trusteddom_pw(PyObject *self, PyObject *args)
3074 TALLOC_CTX *frame = talloc_stackframe();
3075 struct pdb_methods *methods;
3078 const struct dom_sid *domain_sid;
3079 PyObject *py_domain_sid;
3081 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
3082 dom_sid_Type, &py_domain_sid)) {
3087 methods = pytalloc_get_ptr(self);
3089 domain_sid = pytalloc_get_ptr(py_domain_sid);
3091 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
3092 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
3102 static PyObject *py_pdb_del_trusteddom_pw(PyObject *self, PyObject *args)
3104 TALLOC_CTX *frame = talloc_stackframe();
3105 struct pdb_methods *methods;
3108 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
3113 methods = pytalloc_get_ptr(self);
3115 if (!methods->del_trusteddom_pw(methods, domain)) {
3116 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
3126 static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
3128 TALLOC_CTX *frame = talloc_stackframe();
3130 struct pdb_methods *methods;
3131 uint32_t i, num_domains;
3132 struct trustdom_info **domains;
3133 PyObject *py_domain_list, *py_dict;
3135 methods = pytalloc_get_ptr(self);
3137 status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
3138 if (!NT_STATUS_IS_OK(status)) {
3139 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
3140 NT_STATUS_V(status),
3141 get_friendly_nt_error_msg(status));
3146 py_domain_list = PyList_New(0);
3147 if (py_domain_list == NULL) {
3153 for(i=0; i<num_domains; i++) {
3156 pytalloc_steal(dom_sid_Type, &domains[i]->sid);
3157 py_dict = Py_BuildValue(
3159 "name", domains[i]->name,
3162 if (py_dict == NULL) {
3163 DBG_ERR("Failed to insert entry to dict\n");
3164 Py_CLEAR(py_domain_list);
3168 res = PyList_Append(py_domain_list, py_dict);
3171 Py_CLEAR(py_domain_list);
3177 return py_domain_list;
3181 static PyObject *py_pdb_get_trusted_domain(PyObject *self, PyObject *args)
3183 TALLOC_CTX *frame = talloc_stackframe();
3185 struct pdb_methods *methods;
3187 struct pdb_trusted_domain *td;
3188 PyObject *py_domain_info;
3189 PyObject *py_sid = NULL;
3191 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
3196 methods = pytalloc_get_ptr(self);
3198 status = methods->get_trusted_domain(methods, frame, domain, &td);
3199 if (!NT_STATUS_IS_OK(status)) {
3200 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3201 NT_STATUS_V(status),
3202 get_friendly_nt_error_msg(status));
3207 py_sid = pytalloc_steal(dom_sid_Type, &td->security_identifier);
3209 py_domain_info = Py_BuildValue(
3211 " s:"PYARG_BYTES_LEN","
3212 " s:"PYARG_BYTES_LEN","
3214 " s:"PYARG_BYTES_LEN"}",
3215 "domain_name", td->domain_name,
3216 "netbios_name", td->netbios_name,
3217 "security_identifier", py_sid,
3218 "trust_auth_incoming",
3219 (const char *)td->trust_auth_incoming.data,
3220 td->trust_auth_incoming.length,
3221 "trust_auth_outgoing",
3222 (const char *)td->trust_auth_outgoing.data,
3223 td->trust_auth_outgoing.length,
3224 "trust_direction", td->trust_direction,
3225 "trust_type", td->trust_type,
3226 "trust_attributes", td->trust_attributes,
3227 "trust_forest_trust_info",
3228 (const char *)td->trust_forest_trust_info.data,
3229 td->trust_forest_trust_info.length);
3233 return py_domain_info;
3237 static PyObject *py_pdb_get_trusted_domain_by_sid(PyObject *self, PyObject *args)
3239 TALLOC_CTX *frame = talloc_stackframe();
3241 struct pdb_methods *methods;
3242 PyObject *py_domain_sid;
3243 struct dom_sid *domain_sid;
3244 struct pdb_trusted_domain *td;
3245 PyObject *py_domain_info;
3246 PyObject *py_sid = NULL;
3248 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3253 methods = pytalloc_get_ptr(self);
3255 domain_sid = pytalloc_get_ptr(py_domain_sid);
3257 status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3258 if (!NT_STATUS_IS_OK(status)) {
3259 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3260 NT_STATUS_V(status),
3261 get_friendly_nt_error_msg(status));
3266 py_sid = pytalloc_steal(dom_sid_Type, &td->security_identifier);
3268 py_domain_info = Py_BuildValue(
3270 " s:"PYARG_BYTES_LEN","
3271 " s:"PYARG_BYTES_LEN","
3273 " s:"PYARG_BYTES_LEN"}",
3274 "domain_name", td->domain_name,
3275 "netbios_name", td->netbios_name,
3276 "security_identifier", py_sid,
3277 "trust_auth_incoming",
3278 (const char *)td->trust_auth_incoming.data,
3279 td->trust_auth_incoming.length,
3280 "trust_auth_outgoing",
3281 (const char *)td->trust_auth_outgoing.data,
3282 td->trust_auth_outgoing.length,
3283 "trust_direction", td->trust_direction,
3284 "trust_type", td->trust_type,
3285 "trust_attributes", td->trust_attributes,
3286 "trust_forest_trust_info",
3287 (const char *)td->trust_forest_trust_info.data,
3288 td->trust_forest_trust_info.length);
3292 return py_domain_info;
3296 static PyObject *py_pdb_set_trusted_domain(PyObject *self, PyObject *args)
3298 TALLOC_CTX *frame = talloc_stackframe();
3300 struct pdb_methods *methods;
3302 PyObject *py_td_info;
3303 struct pdb_trusted_domain td_info;
3307 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3312 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3313 td_info.domain_name = discard_const_p(char, PyUnicode_AsUTF8(py_tmp));
3315 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3316 td_info.netbios_name = discard_const_p(char, PyUnicode_AsUTF8(py_tmp));
3318 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3319 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3321 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3322 PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3323 td_info.trust_auth_incoming.length = len;
3325 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3326 PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3327 td_info.trust_auth_outgoing.length = len;
3329 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3330 td_info.trust_direction = PyLong_AsLong(py_tmp);
3332 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3333 td_info.trust_type = PyLong_AsLong(py_tmp);
3335 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3336 td_info.trust_attributes = PyLong_AsLong(py_tmp);
3338 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3339 PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3340 td_info.trust_forest_trust_info.length = len;
3342 methods = pytalloc_get_ptr(self);
3344 status = methods->set_trusted_domain(methods, domain, &td_info);
3345 if (!NT_STATUS_IS_OK(status)) {
3346 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3347 NT_STATUS_V(status),
3348 get_friendly_nt_error_msg(status));
3358 static PyObject *py_pdb_del_trusted_domain(PyObject *self, PyObject *args)
3360 TALLOC_CTX *frame = talloc_stackframe();
3362 struct pdb_methods *methods;
3365 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3370 methods = pytalloc_get_ptr(self);
3372 status = methods->del_trusted_domain(methods, domain);
3373 if (!NT_STATUS_IS_OK(status)) {
3374 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3375 NT_STATUS_V(status),
3376 get_friendly_nt_error_msg(status));
3386 static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
3388 TALLOC_CTX *frame = talloc_stackframe();
3390 struct pdb_methods *methods;
3391 uint32_t i, num_domains;
3392 struct pdb_trusted_domain **td_info;
3393 PyObject *py_td_info, *py_domain_info;
3395 methods = pytalloc_get_ptr(self);
3397 status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3398 if (!NT_STATUS_IS_OK(status)) {
3399 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3400 NT_STATUS_V(status),
3401 get_friendly_nt_error_msg(status));
3406 py_td_info = PyList_New(0);
3407 if (py_td_info == NULL) {
3413 for (i=0; i<num_domains; i++) {
3415 struct pdb_trusted_domain *td = td_info[i];
3417 pytalloc_steal(dom_sid_Type, &td->security_identifier);
3419 py_domain_info = Py_BuildValue(
3421 " s:"PYARG_BYTES_LEN","
3422 " s:"PYARG_BYTES_LEN","
3424 " s:"PYARG_BYTES_LEN"}",
3425 "domain_name", td->domain_name,
3426 "netbios_name", td->netbios_name,
3427 "security_identifier", py_sid,
3428 "trust_auth_incoming",
3429 (const char *)td->trust_auth_incoming.data,
3430 td->trust_auth_incoming.length,
3431 "trust_auth_outgoing",
3432 (const char *)td->trust_auth_outgoing.data,
3433 td->trust_auth_outgoing.length,
3434 "trust_direction", td->trust_direction,
3435 "trust_type", td->trust_type,
3436 "trust_attributes", td->trust_attributes,
3437 "trust_forest_trust_info",
3438 (const char *)td->trust_forest_trust_info.data,
3439 td->trust_forest_trust_info.length);
3442 if (py_domain_info == NULL) {
3443 Py_CLEAR(py_td_info);
3446 res = PyList_Append(py_td_info, py_domain_info);
3447 Py_CLEAR(py_domain_info);
3449 Py_CLEAR(py_td_info);
3459 static PyObject *py_pdb_get_secret(PyObject *self, PyObject *args)
3461 TALLOC_CTX *frame = talloc_stackframe();
3463 struct pdb_methods *methods;
3464 const char *secret_name;
3465 DATA_BLOB secret_current, secret_old;
3466 NTTIME secret_current_lastchange, secret_old_lastchange;
3468 struct security_descriptor *sd;
3469 PyObject *py_secret;
3471 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3476 methods = pytalloc_get_ptr(self);
3478 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3479 if (py_sd == NULL) {
3484 sd = pytalloc_get_ptr(py_sd);
3486 status = methods->get_secret(methods, frame, secret_name,
3488 &secret_current_lastchange,
3490 &secret_old_lastchange,
3492 if (!NT_STATUS_IS_OK(status)) {
3493 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3495 NT_STATUS_V(status),
3496 get_friendly_nt_error_msg(status));
3501 py_secret = Py_BuildValue(
3502 "{s:"PYARG_BYTES_LEN","
3504 " s:"PYARG_BYTES_LEN","
3506 "secret_current", (const char*)secret_current.data,
3507 secret_current.length,
3508 "secret_current_lastchange", secret_current_lastchange,
3509 "secret_old", (const char*)secret_old.data,
3511 "secret_old_lastchange", secret_old_lastchange,
3515 if (py_secret == NULL) {
3525 static PyObject *py_pdb_set_secret(PyObject *self, PyObject *args)
3527 TALLOC_CTX *frame = talloc_stackframe();
3529 struct pdb_methods *methods;
3530 const char *secret_name;
3531 PyObject *py_secret;
3532 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3533 DATA_BLOB secret_current, secret_old;
3534 struct security_descriptor *sd;
3537 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3542 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3543 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3544 py_sd = PyDict_GetItemString(py_secret, "sd");
3546 PY_CHECK_TYPE(&PyBytes_Type, py_secret_cur, return NULL;);
3547 PY_CHECK_TYPE(&PyBytes_Type, py_secret_old, return NULL;);
3548 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3550 methods = pytalloc_get_ptr(self);
3552 PyBytes_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3553 secret_current.length = len;
3554 PyBytes_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3555 secret_current.length = len;
3556 sd = pytalloc_get_ptr(py_sd);
3558 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3559 if (!NT_STATUS_IS_OK(status)) {
3560 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3562 NT_STATUS_V(status),
3563 get_friendly_nt_error_msg(status));
3573 static PyObject *py_pdb_delete_secret(PyObject *self, PyObject *args)
3575 TALLOC_CTX *frame = talloc_stackframe();
3577 struct pdb_methods *methods;
3578 const char *secret_name;
3580 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3585 methods = pytalloc_get_ptr(self);
3587 status = methods->delete_secret(methods, secret_name);
3588 if (!NT_STATUS_IS_OK(status)) {
3589 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3591 NT_STATUS_V(status),
3592 get_friendly_nt_error_msg(status));
3601 static PyMethodDef py_pdb_methods[] = {
3602 { "domain_info", py_pdb_domain_info, METH_NOARGS,
3603 "domain_info() -> str\n\n \
3604 Get domain information for the database." },
3605 { "getsampwnam", py_pdb_getsampwnam, METH_VARARGS,
3606 "getsampwnam(username) -> samu object\n\n \
3607 Get user information by name." },
3608 { "getsampwsid", py_pdb_getsampwsid, METH_VARARGS,
3609 "getsampwsid(user_sid) -> samu object\n\n \
3610 Get user information by sid (dcerpc.security.dom_sid object)." },
3611 { "create_user", py_pdb_create_user, METH_VARARGS,
3612 "create_user(username, acct_flags) -> rid\n\n \
3613 Create user. acct_flags are samr account control flags." },
3614 { "delete_user", py_pdb_delete_user, METH_VARARGS,
3615 "delete_user(samu object) -> None\n\n \
3617 { "add_sam_account", py_pdb_add_sam_account, METH_VARARGS,
3618 "add_sam_account(samu object) -> None\n\n \
3619 Add SAM account." },
3620 { "update_sam_account", py_pdb_update_sam_account, METH_VARARGS,
3621 "update_sam_account(samu object) -> None\n\n \
3622 Update SAM account." },
3623 { "delete_sam_account", py_pdb_delete_sam_account, METH_VARARGS,
3624 "delete_sam_account(samu object) -> None\n\n \
3625 Delete SAM account." },
3626 { "rename_sam_account", py_pdb_rename_sam_account, METH_VARARGS,
3627 "rename_sam_account(samu object1, new_username) -> None\n\n \
3628 Rename SAM account." },
3629 /* update_login_attempts */
3630 { "getgrsid", py_pdb_getgrsid, METH_VARARGS,
3631 "getgrsid(group_sid) -> groupmap object\n\n \
3632 Get group information by sid (dcerpc.security.dom_sid object)." },
3633 { "getgrgid", py_pdb_getgrgid, METH_VARARGS,
3634 "getgrsid(gid) -> groupmap object\n\n \
3635 Get group information by gid." },
3636 { "getgrnam", py_pdb_getgrnam, METH_VARARGS,
3637 "getgrsid(groupname) -> groupmap object\n\n \
3638 Get group information by name." },
3639 { "create_dom_group", py_pdb_create_dom_group, METH_VARARGS,
3640 "create_dom_group(groupname) -> group_rid\n\n \
3641 Create new domain group by name." },
3642 { "delete_dom_group", py_pdb_delete_dom_group, METH_VARARGS,
3643 "delete_dom_group(group_rid) -> None\n\n \
3644 Delete domain group identified by rid" },
3645 { "add_group_mapping_entry", py_pdb_add_group_mapping_entry, METH_VARARGS,
3646 "add_group_mapping_entry(groupmap) -> None\n \
3647 Add group mapping entry for groupmap object." },
3648 { "update_group_mapping_entry", py_pdb_update_group_mapping_entry, METH_VARARGS,
3649 "update_group_mapping_entry(groupmap) -> None\n\n \
3650 Update group mapping entry for groupmap object." },
3651 { "delete_group_mapping_entry", py_pdb_delete_group_mapping_entry, METH_VARARGS,
3652 "delete_group_mapping_entry(groupmap) -> None\n\n \
3653 Delete group mapping entry for groupmap object." },
3654 { "enum_group_mapping", py_pdb_enum_group_mapping, METH_VARARGS,
3655 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3656 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3657 { "enum_group_members", py_pdb_enum_group_members, METH_VARARGS,
3658 "enum_group_members(group_sid) -> List\n\n \
3659 Return list of users (dom_sid object) in group." },
3660 { "enum_group_memberships", py_pdb_enum_group_memberships, METH_VARARGS,
3661 "enum_group_memberships(samu object) -> List\n\n \
3662 Return list of groups (dom_sid object) this user is part of." },
3663 /* set_unix_primary_group */
3664 { "add_groupmem", py_pdb_add_groupmem, METH_VARARGS,
3665 "add_groupmem(group_rid, member_rid) -> None\n\n \
3666 Add user to group." },
3667 { "del_groupmem", py_pdb_del_groupmem, METH_VARARGS,
3668 "del_groupmem(group_rid, member_rid) -> None\n\n \
3669 Remove user from from group." },
3670 { "create_alias", py_pdb_create_alias, METH_VARARGS,
3671 "create_alias(alias_name) -> alias_rid\n\n \
3672 Create alias entry." },
3673 { "delete_alias", py_pdb_delete_alias, METH_VARARGS,
3674 "delete_alias(alias_sid) -> None\n\n \
3675 Delete alias entry." },
3676 { "get_aliasinfo", py_pdb_get_aliasinfo, METH_VARARGS,
3677 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3678 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3679 { "set_aliasinfo", py_pdb_set_aliasinfo, METH_VARARGS,
3680 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3681 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3682 { "add_aliasmem", py_pdb_add_aliasmem, METH_VARARGS,
3683 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3684 Add user to alias entry." },
3685 { "del_aliasmem", py_pdb_del_aliasmem, METH_VARARGS,
3686 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3687 Remove a user from alias entry." },
3688 { "enum_aliasmem", py_pdb_enum_aliasmem, METH_VARARGS,
3689 "enum_aliasmem(alias_sid) -> List\n\n \
3690 Return a list of members (dom_sid object) for alias entry." },
3691 /* enum_alias_memberships */
3694 { "get_account_policy", py_pdb_get_account_policy, METH_NOARGS,
3695 "get_account_policy() -> Mapping\n\n \
3696 Get account policy information as a dictionary." },
3697 { "set_account_policy", py_pdb_set_account_policy, METH_VARARGS,
3698 "get_account_policy(Mapping) -> None\n\n \
3699 Set account policy settings from a dicionary." },
3701 { "search_users", py_pdb_search_users, METH_VARARGS,
3702 "search_users(acct_flags) -> List\n\n \
3703 Search users. acct_flags are samr account control flags.\n \
3704 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3705 { "search_groups", py_pdb_search_groups, METH_NOARGS,
3706 "search_groups() -> List\n\n \
3707 Search unix only groups. \n \
3708 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3709 { "search_aliases", py_pdb_search_aliases, METH_VARARGS,
3710 "search_aliases([domain_sid]) -> List\n\n \
3711 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3712 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3713 { "uid_to_sid", py_pdb_uid_to_sid, METH_VARARGS,
3714 "uid_to_sid(uid) -> sid\n\n \
3715 Return sid for given user id." },
3716 { "gid_to_sid", py_pdb_gid_to_sid, METH_VARARGS,
3717 "gid_to_sid(gid) -> sid\n\n \
3718 Return sid for given group id." },
3719 { "sid_to_id", py_pdb_sid_to_id, METH_VARARGS,
3720 "sid_to_id(sid) -> Tuple\n\n \
3721 Return id and type for given sid." },
3723 { "new_rid", py_pdb_new_rid, METH_NOARGS,
3724 "new_rid() -> rid\n\n \
3726 { "get_trusteddom_pw", py_pdb_get_trusteddom_pw, METH_VARARGS,
3727 "get_trusteddom_pw(domain) -> Mapping\n\n \
3728 Get trusted domain password, sid and last set time in a dictionary." },
3729 { "set_trusteddom_pw", py_pdb_set_trusteddom_pw, METH_VARARGS,
3730 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3731 Set trusted domain password." },
3732 { "del_trusteddom_pw", py_pdb_del_trusteddom_pw, METH_VARARGS,
3733 "del_trusteddom_pw(domain) -> None\n\n \
3734 Delete trusted domain password." },
3735 { "enum_trusteddoms", py_pdb_enum_trusteddoms, METH_NOARGS,
3736 "enum_trusteddoms() -> List\n\n \
3737 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3738 { "get_trusted_domain", py_pdb_get_trusted_domain, METH_VARARGS,
3739 "get_trusted_domain(domain) -> Mapping\n\n \
3740 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." },
3741 { "get_trusted_domain_by_sid", py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3742 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3743 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" },
3744 { "set_trusted_domain", py_pdb_set_trusted_domain, METH_VARARGS,
3745 "set_trusted_domain(domain, Mapping) -> None\n\n \
3746 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." },
3747 { "del_trusted_domain", py_pdb_del_trusted_domain, METH_VARARGS,
3748 "del_trusted_domain(domain) -> None\n\n \
3749 Delete trusted domain." },
3750 { "enum_trusted_domains", py_pdb_enum_trusted_domains, METH_VARARGS,
3751 "enum_trusted_domains() -> List\n\n \
3752 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." },
3753 { "get_secret", py_pdb_get_secret, METH_VARARGS,
3754 "get_secret(secret_name) -> Mapping\n\n \
3755 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3756 { "set_secret", py_pdb_set_secret, METH_VARARGS,
3757 "set_secret(secret_name, Mapping) -> None\n\n \
3758 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3759 { "delete_secret", py_pdb_delete_secret, METH_VARARGS,
3760 "delete_secret(secret_name) -> None\n\n \
3761 Delete secret information for secret_name." },
3766 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3768 TALLOC_CTX *frame = talloc_stackframe();
3769 const char *url = NULL;
3772 struct pdb_methods *methods;
3774 if (!PyArg_ParseTuple(args, "s", &url)) {
3779 /* Initialize list of methods */
3780 status = make_pdb_method_name(&methods, url);
3781 if (!NT_STATUS_IS_OK(status)) {
3782 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3784 NT_STATUS_V(status),
3785 get_friendly_nt_error_msg(status));
3790 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3801 static PyTypeObject PyPDB = {
3802 .tp_name = "passdb.PDB",
3803 .tp_new = py_pdb_new,
3804 .tp_flags = Py_TPFLAGS_DEFAULT,
3805 .tp_methods = py_pdb_methods,
3806 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3811 * Return a list of passdb backends
3813 static PyObject *py_passdb_backends(PyObject *self, PyObject *unused)
3815 TALLOC_CTX *frame = talloc_stackframe();
3817 const struct pdb_init_function_entry *entry;
3819 entry = pdb_get_backends();
3824 if((py_blist = PyList_New(0)) == NULL) {
3832 PyObject *entry_name = PyUnicode_FromString(entry->name);
3834 res = PyList_Append(py_blist, entry_name);
3836 Py_CLEAR(entry_name);
3840 Py_CLEAR(entry_name);
3845 entry = entry->next;
3853 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3855 TALLOC_CTX *frame = talloc_stackframe();
3856 const char *smb_config;
3858 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3863 /* Load smbconf parameters */
3864 if (!lp_load_global(smb_config)) {
3865 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3875 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3877 TALLOC_CTX *frame = talloc_stackframe();
3878 const char *private_dir;
3880 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3885 /* Initialize secrets database */
3886 if (!secrets_init_path(private_dir)) {
3887 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3897 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3899 TALLOC_CTX *frame = talloc_stackframe();
3901 /* Initialize secrets database */
3902 if (!initialize_password_db(true, NULL)) {
3903 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3912 static PyObject *py_get_domain_sid(PyObject *self, PyObject *unused)
3914 TALLOC_CTX *frame = talloc_stackframe();
3915 struct dom_sid domain_sid, *domain_sid_copy;
3916 PyObject *py_dom_sid = Py_None;
3919 ret = secrets_fetch_domain_sid(lp_workgroup(), &domain_sid);
3922 return PyErr_NoMemory();
3925 domain_sid_copy = dom_sid_dup(frame, &domain_sid);
3926 if (domain_sid_copy == NULL) {
3928 return PyErr_NoMemory();
3931 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3937 static PyObject *py_get_global_sam_sid(PyObject *self, PyObject *unused)
3939 TALLOC_CTX *frame = talloc_stackframe();
3940 struct dom_sid *domain_sid, *domain_sid_copy;
3941 PyObject *py_dom_sid;
3943 domain_sid = get_global_sam_sid();
3945 domain_sid_copy = dom_sid_dup(frame, domain_sid);
3946 if (domain_sid_copy == NULL) {
3952 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3959 static PyMethodDef py_passdb_methods[] = {
3960 { "get_backends", py_passdb_backends, METH_NOARGS,
3961 "get_backends() -> list\n\n \
3962 Get a list of password database backends supported." },
3963 { "set_smb_config", py_set_smb_config, METH_VARARGS,
3964 "set_smb_config(path) -> None\n\n \
3965 Set path to smb.conf file to load configuration parameters." },
3966 { "set_secrets_dir", py_set_secrets_dir, METH_VARARGS,
3967 "set_secrets_dir(private_dir) -> None\n\n \
3968 Set path to private directory to load secrets database from non-default location." },
3969 { "get_global_sam_sid", py_get_global_sam_sid, METH_NOARGS,
3970 "get_global_sam_sid() -> dom_sid\n\n \
3971 Return domain SID." },
3972 { "get_domain_sid", py_get_domain_sid, METH_NOARGS,
3973 "get_domain_sid() -> dom_sid\n\n \
3974 Return domain SID from secrets database." },
3975 { "reload_static_pdb", py_reload_static_pdb, METH_NOARGS,
3976 "reload_static_pdb() -> None\n\n \
3977 Re-initialise the static pdb used internally. Needed if 'passdb backend' is changed." },
3981 static struct PyModuleDef moduledef = {
3982 PyModuleDef_HEAD_INIT,
3984 .m_doc = "SAMBA Password Database",
3986 .m_methods = py_passdb_methods,
3989 MODULE_INIT_FUNC(passdb)
3991 TALLOC_CTX *frame = talloc_stackframe();
3992 PyObject *m = NULL, *mod = NULL;
3993 char exception_name[] = "passdb.error";
3995 if (pytalloc_BaseObject_PyType_Ready(&PyPDB) < 0) {
4000 if (pytalloc_BaseObject_PyType_Ready(&PySamu) < 0) {
4005 if (pytalloc_BaseObject_PyType_Ready(&PyGroupmap) < 0) {
4010 m = PyModule_Create(&moduledef);
4016 /* Create new exception for passdb module */
4017 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
4018 Py_INCREF(py_pdb_error);
4019 PyModule_AddObject(m, "error", py_pdb_error);
4022 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
4025 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
4027 Py_INCREF(&PyGroupmap);
4028 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
4030 /* Import dom_sid type from dcerpc.security */
4031 mod = PyImport_ImportModule("samba.dcerpc.security");
4037 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
4038 if (dom_sid_Type == NULL) {
4044 /* Import security_descriptor type from dcerpc.security */
4045 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
4047 if (security_Type == NULL) {
4048 Py_DECREF(dom_sid_Type);
4053 /* Import GUID type from dcerpc.misc */
4054 mod = PyImport_ImportModule("samba.dcerpc.misc");
4056 Py_DECREF(security_Type);
4057 Py_DECREF(dom_sid_Type);
4062 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
4064 if (guid_Type == NULL) {
4065 Py_DECREF(security_Type);
4066 Py_DECREF(dom_sid_Type);