2 Python interface to passdb
4 Copyright (C) Amitay Isaacs 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/talloc_stack.h"
24 #include "libcli/security/security.h"
28 /* There's no Py_ssize_t in 2.4, apparently */
29 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
30 typedef int Py_ssize_t;
31 typedef inquiry lenfunc;
32 typedef intargfunc ssizeargfunc;
35 #ifndef Py_RETURN_NONE
36 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
39 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
40 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
44 #define PY_CHECK_TYPE(type, var, fail) \
45 if (!PyObject_TypeCheck(var, type)) {\
46 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
52 static PyTypeObject *dom_sid_Type = NULL;
53 static PyTypeObject *security_Type = NULL;
54 static PyTypeObject *guid_Type = NULL;
56 staticforward PyTypeObject PySamu;
57 staticforward PyTypeObject PyGroupmap;
58 staticforward PyTypeObject PyPDB;
60 static PyObject *py_pdb_error;
62 void initpassdb(void);
65 /************************** PIDL Autogeneratd ******************************/
67 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
69 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
70 PyObject *py_logon_time;
72 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
76 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
78 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
80 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
81 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
87 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
89 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
90 PyObject *py_logoff_time;
92 py_logoff_time = PyInt_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 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
100 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
101 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
107 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
109 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
110 PyObject *py_kickoff_time;
112 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
113 return py_kickoff_time;
116 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
118 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
120 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
121 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
127 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
129 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
130 PyObject *py_bad_password_time;
132 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
133 return py_bad_password_time;
136 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
138 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
140 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
141 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
147 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
149 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
150 PyObject *py_pass_last_set_time;
152 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
153 return py_pass_last_set_time;
156 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
158 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
160 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
161 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
167 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
169 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
170 PyObject *py_pass_can_change_time;
172 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
173 return py_pass_can_change_time;
176 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
178 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
180 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
181 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
187 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
189 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
190 PyObject *py_pass_must_change_time;
192 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
193 return py_pass_must_change_time;
196 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
198 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
200 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
201 if (!pdb_set_pass_must_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
207 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
209 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
210 PyObject *py_username;
211 const char *username;
213 username = pdb_get_username(sam_acct);
214 if (username == NULL) {
218 py_username = PyString_FromString(username);
222 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
224 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
226 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
227 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
233 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
235 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
239 domain = pdb_get_domain(sam_acct);
240 if (domain == NULL) {
244 py_domain = PyString_FromString(domain);
248 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
250 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
252 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
253 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
259 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
261 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
262 PyObject *py_nt_username;
263 const char *nt_username;
265 nt_username = pdb_get_nt_username(sam_acct);
266 if (nt_username == NULL) {
270 py_nt_username = PyString_FromString(nt_username);
271 return py_nt_username;
274 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
276 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
278 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
279 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
285 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
287 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
288 PyObject *py_full_name;
289 const char *full_name;
291 full_name = pdb_get_fullname(sam_acct);
292 if (full_name == NULL) {
296 py_full_name = PyString_FromString(full_name);
300 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
302 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
304 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
305 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
311 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
313 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
314 PyObject *py_home_dir;
315 const char *home_dir;
317 home_dir = pdb_get_homedir(sam_acct);
318 if (home_dir == NULL) {
322 py_home_dir = PyString_FromString(home_dir);
326 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
328 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
330 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
331 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
337 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
339 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
340 PyObject *py_dir_drive;
341 const char *dir_drive;
343 dir_drive = pdb_get_dir_drive(sam_acct);
344 if (dir_drive == NULL) {
348 py_dir_drive = PyString_FromString(dir_drive);
352 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
354 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
356 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
357 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
363 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
365 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
366 PyObject *py_logon_script;
367 const char *logon_script;
369 logon_script = pdb_get_logon_script(sam_acct);
370 if (logon_script == NULL) {
374 py_logon_script = PyString_FromString(logon_script);
375 return py_logon_script;
378 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
380 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
382 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
383 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
389 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
391 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
392 PyObject *py_profile_path;
393 const char *profile_path;
395 profile_path = pdb_get_profile_path(sam_acct);
396 if (profile_path == NULL) {
400 py_profile_path = PyString_FromString(profile_path);
401 return py_profile_path;
404 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
406 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
408 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
409 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
415 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
417 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
418 PyObject *py_acct_desc;
419 const char *acct_desc;
421 acct_desc = pdb_get_acct_desc(sam_acct);
422 if (acct_desc == NULL) {
426 py_acct_desc = PyString_FromString(acct_desc);
430 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
432 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
434 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
435 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
441 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
443 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
444 PyObject *py_workstations;
445 const char *workstations;
447 workstations = pdb_get_workstations(sam_acct);
448 if (workstations == NULL) {
452 py_workstations = PyString_FromString(workstations);
453 return py_workstations;
456 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
458 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
460 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
461 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
467 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
469 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
470 PyObject *py_comment;
473 comment = pdb_get_comment(sam_acct);
474 if (comment == NULL) {
478 py_comment = PyString_FromString(comment);
482 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
484 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
486 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
487 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
493 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
495 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
496 PyObject *py_munged_dial;
497 const char *munged_dial;
499 munged_dial = pdb_get_munged_dial(sam_acct);
500 if (munged_dial == NULL) {
504 py_munged_dial = PyString_FromString(munged_dial);
505 return py_munged_dial;
508 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
510 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
512 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
513 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
519 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
521 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
522 PyObject *py_user_sid;
523 const struct dom_sid *user_sid;
524 struct dom_sid *copy_user_sid;
527 user_sid = pdb_get_user_sid(sam_acct);
528 if(user_sid == NULL) {
532 mem_ctx = talloc_new(NULL);
533 if (mem_ctx == NULL) {
537 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
538 if (copy_user_sid == NULL) {
540 talloc_free(mem_ctx);
544 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
546 talloc_free(mem_ctx);
551 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
553 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
555 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
556 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
562 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
564 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
565 PyObject *py_group_sid;
566 const struct dom_sid *group_sid;
567 struct dom_sid *copy_group_sid;
570 mem_ctx = talloc_stackframe();
571 if (mem_ctx == NULL) {
576 group_sid = pdb_get_group_sid(sam_acct);
577 if (group_sid == NULL) {
581 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
582 if (copy_group_sid == NULL) {
584 talloc_free(mem_ctx);
588 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
590 talloc_free(mem_ctx);
595 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
597 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
599 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
600 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
606 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
608 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
612 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
617 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
621 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
623 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
625 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
626 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
632 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
634 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
638 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
643 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
647 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
649 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
651 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
657 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
659 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
660 PyObject *py_nt_pw_his;
661 const char *nt_pw_his;
664 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
665 if (nt_pw_his == NULL) {
669 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
673 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
675 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
680 PyString_AsStringAndSize(value, &nt_pw_his, &len);
681 hist_len = len / PW_HISTORY_ENTRY_LEN;
682 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
688 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
690 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
691 PyObject *py_plaintext_pw;
692 const char *plaintext_pw;
694 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
695 if (plaintext_pw == NULL) {
699 py_plaintext_pw = PyString_FromString(plaintext_pw);
700 return py_plaintext_pw;
703 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
705 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
707 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
713 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
715 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
716 PyObject *py_acct_ctrl;
718 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
722 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
724 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
726 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
727 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
733 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
735 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
736 PyObject *py_logon_divs;
738 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
739 return py_logon_divs;
742 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
744 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
746 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
747 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
753 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
755 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
756 PyObject *py_hours_len;
758 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
762 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
764 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
766 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
767 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
773 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
775 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
780 hours = (const char *)pdb_get_hours(sam_acct);
785 hours_len = pdb_get_hours_len(sam_acct);
786 if ((py_hours = PyList_New(hours_len)) == NULL) {
791 for (i=0; i<hours_len; i++) {
792 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
797 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
799 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
805 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
807 hours_len = PyList_GET_SIZE(value);
809 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
815 for (i=0; i < hours_len; i++) {
816 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
817 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
820 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
829 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
831 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
832 PyObject *py_bad_password_count;
834 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
835 return py_bad_password_count;
838 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
840 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
842 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
843 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
849 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
851 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
852 PyObject *py_logon_count;
854 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
855 return py_logon_count;
858 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
860 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
862 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
863 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
869 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
871 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
872 PyObject *py_country_code;
874 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
875 return py_country_code;
878 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
880 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
882 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
883 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
889 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
891 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
892 PyObject *py_code_page;
894 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
898 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
900 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
902 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
903 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
909 static PyGetSetDef py_samu_getsetters[] = {
910 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
911 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
912 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
913 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
914 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
915 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
916 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
917 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
918 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
919 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
920 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
921 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
922 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
923 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
924 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
925 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
926 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
927 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
928 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
929 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
930 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
931 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
932 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
933 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
934 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
935 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
936 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
937 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
938 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
939 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
940 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
941 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
942 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
947 /************************** PIDL Autogeneratd ******************************/
949 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
951 struct samu *sam_acct;
953 sam_acct = samu_new(NULL);
959 return pytalloc_steal(type, sam_acct);
962 static PyTypeObject PySamu = {
963 .tp_name = "passdb.Samu",
964 .tp_basicsize = sizeof(pytalloc_Object),
965 .tp_getset = py_samu_getsetters,
967 .tp_new = py_samu_new,
968 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
969 .tp_doc = "Samu() -> samu object\n",
973 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
975 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
978 py_gid = PyInt_FromLong(group_map->gid);
982 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
984 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
986 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
987 group_map->gid = PyInt_AsLong(value);
991 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
993 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
995 struct dom_sid *group_sid;
998 mem_ctx = talloc_new(NULL);
999 if (mem_ctx == NULL) {
1004 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1005 if (group_sid == NULL) {
1007 talloc_free(mem_ctx);
1011 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1013 talloc_free(mem_ctx);
1018 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1020 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1022 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1023 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1027 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1029 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1030 PyObject *py_sid_name_use;
1032 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1033 return py_sid_name_use;
1036 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1038 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1040 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1041 group_map->sid_name_use = PyInt_AsLong(value);
1045 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1047 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1048 PyObject *py_nt_name;
1049 if (group_map->nt_name == NULL) {
1050 py_nt_name = Py_None;
1051 Py_INCREF(py_nt_name);
1053 py_nt_name = PyString_FromString(group_map->nt_name);
1058 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1060 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1062 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1063 if (value == Py_None) {
1064 fstrcpy(group_map->nt_name, NULL);
1066 fstrcpy(group_map->nt_name, PyString_AsString(value));
1071 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1073 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1074 PyObject *py_comment;
1075 if (group_map->comment == NULL) {
1076 py_comment = Py_None;
1077 Py_INCREF(py_comment);
1079 py_comment = PyString_FromString(group_map->comment);
1084 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1086 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1088 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1089 if (value == Py_None) {
1090 fstrcpy(group_map->comment, NULL);
1092 fstrcpy(group_map->comment, PyString_AsString(value));
1097 static PyGetSetDef py_groupmap_getsetters[] = {
1098 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1099 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1100 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1101 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1102 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1106 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1108 GROUP_MAP *group_map;
1109 TALLOC_CTX *mem_ctx;
1110 PyObject *py_group_map;
1112 mem_ctx = talloc_new(NULL);
1113 if (mem_ctx == NULL) {
1118 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1119 if (group_map == NULL) {
1121 talloc_free(mem_ctx);
1125 py_group_map = pytalloc_steal(type, group_map);
1126 if (py_group_map == NULL) {
1128 talloc_free(mem_ctx);
1132 talloc_free(mem_ctx);
1134 return py_group_map;
1138 static PyTypeObject PyGroupmap = {
1139 .tp_name = "passdb.Groupmap",
1140 .tp_basicsize = sizeof(pytalloc_Object),
1141 .tp_getset = py_groupmap_getsetters,
1143 .tp_new = py_groupmap_new,
1144 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1145 .tp_doc = "Groupmap() -> group map object\n",
1149 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1151 struct pdb_methods *methods;
1152 struct pdb_domain_info *domain_info;
1153 PyObject *py_domain_info;
1155 struct dom_sid *sid;
1158 methods = pytalloc_get_ptr(self);
1160 if ((tframe = talloc_stackframe()) == NULL) {
1165 domain_info = methods->get_domain_info(methods, tframe);
1166 if (! domain_info) {
1170 sid = dom_sid_dup(tframe, &domain_info->sid);
1173 talloc_free(tframe);
1177 guid = talloc(tframe, struct GUID);
1180 talloc_free(tframe);
1183 *guid = domain_info->guid;
1185 if ((py_domain_info = PyDict_New()) == NULL) {
1190 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1191 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->name));
1192 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->name));
1193 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1194 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1196 talloc_free(tframe);
1198 return py_domain_info;
1202 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1205 const char *username;
1206 struct pdb_methods *methods;
1207 struct samu *sam_acct;
1208 PyObject *py_sam_acct;
1211 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1215 methods = pytalloc_get_ptr(self);
1217 if ((tframe = talloc_stackframe()) == NULL) {
1222 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1223 if (py_sam_acct == NULL) {
1225 talloc_free(tframe);
1228 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1230 status = methods->getsampwnam(methods, sam_acct, username);
1231 if (!NT_STATUS_IS_OK(status)) {
1232 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1234 NT_STATUS_V(status),
1235 get_friendly_nt_error_msg(status));
1236 Py_DECREF(py_sam_acct);
1237 talloc_free(tframe);
1241 talloc_free(tframe);
1245 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1248 struct pdb_methods *methods;
1249 struct samu *sam_acct;
1250 PyObject *py_sam_acct;
1252 PyObject *py_user_sid;
1254 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1258 methods = pytalloc_get_ptr(self);
1260 if ((tframe = talloc_stackframe()) == NULL) {
1265 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1266 if (py_sam_acct == NULL) {
1268 talloc_free(tframe);
1271 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1273 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1274 if (!NT_STATUS_IS_OK(status)) {
1275 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1276 NT_STATUS_V(status),
1277 get_friendly_nt_error_msg(status));
1278 Py_DECREF(py_sam_acct);
1279 talloc_free(tframe);
1283 talloc_free(tframe);
1287 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1290 struct pdb_methods *methods;
1291 const char *username;
1292 unsigned int acct_flags;
1296 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1300 methods = pytalloc_get_ptr(self);
1302 if ((tframe = talloc_stackframe()) == NULL) {
1307 status = methods->create_user(methods, tframe, username, acct_flags, &rid);
1308 if (!NT_STATUS_IS_OK(status)) {
1309 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1311 NT_STATUS_V(status),
1312 get_friendly_nt_error_msg(status));
1313 talloc_free(tframe);
1317 talloc_free(tframe);
1318 return PyInt_FromLong(rid);
1321 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1324 struct pdb_methods *methods;
1326 struct samu *sam_acct;
1327 PyObject *py_sam_acct;
1329 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1333 methods = pytalloc_get_ptr(self);
1335 if ((tframe = talloc_stackframe()) == NULL) {
1340 sam_acct = pytalloc_get_ptr(py_sam_acct);
1342 status = methods->delete_user(methods, tframe, sam_acct);
1343 if (!NT_STATUS_IS_OK(status)) {
1344 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1345 NT_STATUS_V(status),
1346 get_friendly_nt_error_msg(status));
1347 talloc_free(tframe);
1351 talloc_free(tframe);
1355 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1358 struct pdb_methods *methods;
1360 struct samu *sam_acct;
1361 PyObject *py_sam_acct;
1363 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1367 methods = pytalloc_get_ptr(self);
1369 if ((tframe = talloc_stackframe()) == NULL) {
1374 sam_acct = pytalloc_get_ptr(py_sam_acct);
1376 status = methods->add_sam_account(methods, sam_acct);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1380 NT_STATUS_V(status),
1381 get_friendly_nt_error_msg(status));
1382 talloc_free(tframe);
1386 talloc_free(tframe);
1390 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1393 struct pdb_methods *methods;
1395 struct samu *sam_acct;
1396 PyObject *py_sam_acct;
1398 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1402 methods = pytalloc_get_ptr(self);
1404 if ((tframe = talloc_stackframe()) == NULL) {
1409 sam_acct = pytalloc_get_ptr(py_sam_acct);
1411 status = methods->update_sam_account(methods, sam_acct);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1414 NT_STATUS_V(status),
1415 get_friendly_nt_error_msg(status));
1416 talloc_free(tframe);
1420 talloc_free(tframe);
1424 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1427 struct pdb_methods *methods;
1429 struct samu *sam_acct;
1430 PyObject *py_sam_acct;
1432 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1436 methods = pytalloc_get_ptr(self);
1438 if ((tframe = talloc_stackframe()) == NULL) {
1443 sam_acct = pytalloc_get_ptr(py_sam_acct);
1445 status = methods->delete_sam_account(methods, sam_acct);
1446 if (!NT_STATUS_IS_OK(status)) {
1447 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1448 NT_STATUS_V(status),
1449 get_friendly_nt_error_msg(status));
1450 talloc_free(tframe);
1454 talloc_free(tframe);
1458 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1461 struct pdb_methods *methods;
1463 struct samu *sam_acct;
1464 const char *new_username;
1465 PyObject *py_sam_acct;
1467 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1472 methods = pytalloc_get_ptr(self);
1474 if ((tframe = talloc_stackframe()) == NULL) {
1479 sam_acct = pytalloc_get_ptr(py_sam_acct);
1481 status = methods->rename_sam_account(methods, sam_acct, new_username);
1482 if (!NT_STATUS_IS_OK(status)) {
1483 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1484 NT_STATUS_V(status),
1485 get_friendly_nt_error_msg(status));
1486 talloc_free(tframe);
1490 talloc_free(tframe);
1495 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1498 struct pdb_methods *methods;
1500 GROUP_MAP *group_map;
1501 struct dom_sid *domain_sid;
1502 PyObject *py_domain_sid, *py_group_map;
1504 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1508 methods = pytalloc_get_ptr(self);
1510 if ((tframe = talloc_stackframe()) == NULL) {
1515 domain_sid = pytalloc_get_ptr(py_domain_sid);
1517 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1518 if (py_group_map == NULL) {
1520 talloc_free(tframe);
1524 group_map = pytalloc_get_ptr(py_group_map);
1526 status = methods->getgrsid(methods, group_map, *domain_sid);
1527 if (!NT_STATUS_IS_OK(status)) {
1528 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1529 NT_STATUS_V(status),
1530 get_friendly_nt_error_msg(status));
1531 talloc_free(tframe);
1535 talloc_free(tframe);
1536 return py_group_map;
1540 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1543 struct pdb_methods *methods;
1545 GROUP_MAP *group_map;
1546 PyObject *py_group_map;
1547 unsigned int gid_value;
1549 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1553 methods = pytalloc_get_ptr(self);
1555 if ((tframe = talloc_stackframe()) == NULL) {
1560 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1561 if (py_group_map == NULL) {
1563 talloc_free(tframe);
1567 group_map = pytalloc_get_ptr(py_group_map);
1569 status = methods->getgrgid(methods, group_map, gid_value);
1570 if (!NT_STATUS_IS_OK(status)) {
1571 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1572 NT_STATUS_V(status),
1573 get_friendly_nt_error_msg(status));
1574 talloc_free(tframe);
1578 talloc_free(tframe);
1579 return py_group_map;
1583 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1586 struct pdb_methods *methods;
1588 GROUP_MAP *group_map;
1589 PyObject *py_group_map;
1590 const char *groupname;
1592 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1596 methods = pytalloc_get_ptr(self);
1598 if ((tframe = talloc_stackframe()) == NULL) {
1603 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1604 if (py_group_map == NULL) {
1606 talloc_free(tframe);
1610 group_map = pytalloc_get_ptr(py_group_map);
1612 status = methods->getgrnam(methods, group_map, groupname);
1613 if (!NT_STATUS_IS_OK(status)) {
1614 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1615 NT_STATUS_V(status),
1616 get_friendly_nt_error_msg(status));
1617 talloc_free(tframe);
1621 talloc_free(tframe);
1622 return py_group_map;
1626 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1629 struct pdb_methods *methods;
1631 const char *groupname;
1634 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1638 methods = pytalloc_get_ptr(self);
1640 if ((tframe = talloc_stackframe()) == NULL) {
1645 status = methods->create_dom_group(methods, tframe, groupname, &group_rid);
1646 if (!NT_STATUS_IS_OK(status)) {
1647 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1649 NT_STATUS_V(status),
1650 get_friendly_nt_error_msg(status));
1651 talloc_free(tframe);
1655 talloc_free(tframe);
1656 return PyInt_FromLong(group_rid);
1660 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1663 struct pdb_methods *methods;
1665 unsigned int group_rid;
1667 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1671 methods = pytalloc_get_ptr(self);
1673 if ((tframe = talloc_stackframe()) == NULL) {
1678 status = methods->delete_dom_group(methods, tframe, group_rid);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1682 NT_STATUS_V(status),
1683 get_friendly_nt_error_msg(status));
1684 talloc_free(tframe);
1688 talloc_free(tframe);
1693 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1696 struct pdb_methods *methods;
1698 PyObject *py_group_map;
1699 GROUP_MAP *group_map;
1701 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1705 methods = pytalloc_get_ptr(self);
1707 if ((tframe = talloc_stackframe()) == NULL) {
1712 group_map = pytalloc_get_ptr(py_group_map);
1714 status = methods->add_group_mapping_entry(methods, group_map);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1717 NT_STATUS_V(status),
1718 get_friendly_nt_error_msg(status));
1719 talloc_free(tframe);
1723 talloc_free(tframe);
1728 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1731 struct pdb_methods *methods;
1733 PyObject *py_group_map;
1734 GROUP_MAP *group_map;
1736 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1740 methods = pytalloc_get_ptr(self);
1742 if ((tframe = talloc_stackframe()) == NULL) {
1747 group_map = pytalloc_get_ptr(py_group_map);
1749 status = methods->update_group_mapping_entry(methods, group_map);
1750 if (!NT_STATUS_IS_OK(status)) {
1751 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1752 NT_STATUS_V(status),
1753 get_friendly_nt_error_msg(status));
1754 talloc_free(tframe);
1758 talloc_free(tframe);
1763 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1766 struct pdb_methods *methods;
1768 PyObject *py_group_sid;
1769 struct dom_sid *group_sid;
1771 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1775 methods = pytalloc_get_ptr(self);
1777 if ((tframe = talloc_stackframe()) == NULL) {
1782 group_sid = pytalloc_get_ptr(py_group_sid);
1784 status = methods->delete_group_mapping_entry(methods, *group_sid);
1785 if (!NT_STATUS_IS_OK(status)) {
1786 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1787 NT_STATUS_V(status),
1788 get_friendly_nt_error_msg(status));
1789 talloc_free(tframe);
1793 talloc_free(tframe);
1798 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1801 struct pdb_methods *methods;
1803 enum lsa_SidType sid_name_use;
1804 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1806 PyObject *py_domain_sid;
1807 struct dom_sid *domain_sid = NULL;
1808 GROUP_MAP *gmap, *group_map;
1810 PyObject *py_gmap_list, *py_group_map;
1813 py_domain_sid = Py_None;
1816 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1817 &lsa_sidtype_value, &unix_only)) {
1821 methods = pytalloc_get_ptr(self);
1823 if ((tframe = talloc_stackframe()) == NULL) {
1828 sid_name_use = lsa_sidtype_value;
1830 if (py_domain_sid != Py_None) {
1831 domain_sid = pytalloc_get_ptr(py_domain_sid);
1834 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1835 &gmap, &num_entries, unix_only);
1836 if (!NT_STATUS_IS_OK(status)) {
1837 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1838 NT_STATUS_V(status),
1839 get_friendly_nt_error_msg(status));
1840 talloc_free(tframe);
1844 py_gmap_list = PyList_New(0);
1845 if (py_gmap_list == NULL) {
1847 talloc_free(tframe);
1851 for(i=0; i<num_entries; i++) {
1852 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1854 group_map = pytalloc_get_ptr(py_group_map);
1855 *group_map = gmap[i];
1857 PyList_Append(py_gmap_list, py_group_map);
1862 talloc_free(tframe);
1864 return py_gmap_list;
1868 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1871 struct pdb_methods *methods;
1873 PyObject *py_group_sid;
1874 struct dom_sid *group_sid;
1875 uint32_t *member_rids;
1877 PyObject *py_sid_list;
1878 struct dom_sid *domain_sid, *member_sid;
1881 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1885 methods = pytalloc_get_ptr(self);
1887 if ((tframe = talloc_stackframe()) == NULL) {
1892 group_sid = pytalloc_get_ptr(py_group_sid);
1894 status = methods->enum_group_members(methods, tframe, group_sid,
1895 &member_rids, &num_members);
1896 if (!NT_STATUS_IS_OK(status)) {
1897 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1898 NT_STATUS_V(status),
1899 get_friendly_nt_error_msg(status));
1900 talloc_free(tframe);
1904 py_sid_list = PyList_New(0);
1905 if (py_sid_list == NULL) {
1907 talloc_free(tframe);
1911 domain_sid = get_global_sam_sid();
1913 for(i=0; i<num_members; i++) {
1914 member_sid = dom_sid_add_rid(tframe, domain_sid, member_rids[i]);
1915 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
1918 talloc_free(tframe);
1924 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
1927 struct pdb_methods *methods;
1929 uint32_t group_rid, member_rid;
1931 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
1935 methods = pytalloc_get_ptr(self);
1937 if ((tframe = talloc_stackframe()) == NULL) {
1942 status = methods->add_groupmem(methods, tframe, group_rid, member_rid);
1943 if (!NT_STATUS_IS_OK(status)) {
1944 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
1945 NT_STATUS_V(status),
1946 get_friendly_nt_error_msg(status));
1947 talloc_free(tframe);
1951 talloc_free(tframe);
1956 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
1959 struct pdb_methods *methods;
1961 uint32_t group_rid, member_rid;
1963 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
1967 methods = pytalloc_get_ptr(self);
1969 if ((tframe = talloc_stackframe()) == NULL) {
1974 status = methods->del_groupmem(methods, tframe, group_rid, member_rid);
1975 if (!NT_STATUS_IS_OK(status)) {
1976 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1977 NT_STATUS_V(status),
1978 get_friendly_nt_error_msg(status));
1979 talloc_free(tframe);
1983 talloc_free(tframe);
1988 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
1991 struct pdb_methods *methods;
1993 const char *alias_name;
1996 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2000 methods = pytalloc_get_ptr(self);
2002 if ((tframe = talloc_stackframe()) == NULL) {
2007 status = methods->create_alias(methods, alias_name, &rid);
2008 if (!NT_STATUS_IS_OK(status)) {
2009 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2011 NT_STATUS_V(status),
2012 get_friendly_nt_error_msg(status));
2013 talloc_free(tframe);
2017 talloc_free(tframe);
2019 return PyInt_FromLong(rid);
2023 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2026 struct pdb_methods *methods;
2028 PyObject *py_alias_sid;
2029 struct dom_sid *alias_sid;
2031 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2035 methods = pytalloc_get_ptr(self);
2037 if ((tframe = talloc_stackframe()) == NULL) {
2042 alias_sid = pytalloc_get_ptr(py_alias_sid);
2044 status = methods->delete_alias(methods, alias_sid);
2045 if (!NT_STATUS_IS_OK(status)) {
2046 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2047 NT_STATUS_V(status),
2048 get_friendly_nt_error_msg(status));
2049 talloc_free(tframe);
2053 talloc_free(tframe);
2058 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2061 struct pdb_methods *methods;
2063 PyObject *py_alias_sid;
2064 struct dom_sid *alias_sid;
2065 struct acct_info alias_info;
2066 PyObject *py_alias_info;
2068 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2072 methods = pytalloc_get_ptr(self);
2074 if ((tframe = talloc_stackframe()) == NULL) {
2079 alias_sid = pytalloc_get_ptr(py_alias_sid);
2081 status = methods->get_aliasinfo(methods, alias_sid, &alias_info);
2082 if (!NT_STATUS_IS_OK(status)) {
2083 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2084 NT_STATUS_V(status),
2085 get_friendly_nt_error_msg(status));
2086 talloc_free(tframe);
2090 py_alias_info = PyDict_New();
2091 if (py_alias_info == NULL) {
2093 talloc_free(tframe);
2097 PyDict_SetItemString(py_alias_info, "acct_name", PyString_FromString(alias_info.acct_name));
2098 PyDict_SetItemString(py_alias_info, "acct_desc", PyString_FromString(alias_info.acct_desc));
2099 PyDict_SetItemString(py_alias_info, "rid", PyInt_FromLong(alias_info.rid));
2101 talloc_free(tframe);
2103 return py_alias_info;
2107 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2110 struct pdb_methods *methods;
2112 PyObject *py_alias_sid, *py_alias_info;
2113 struct dom_sid *alias_sid;
2114 struct acct_info alias_info;
2116 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2121 methods = pytalloc_get_ptr(self);
2123 if ((tframe = talloc_stackframe()) == NULL) {
2128 alias_sid = pytalloc_get_ptr(py_alias_sid);
2130 fstrcpy(alias_info.acct_name, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2131 fstrcpy(alias_info.acct_desc, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2133 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2134 if (!NT_STATUS_IS_OK(status)) {
2135 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2136 NT_STATUS_V(status),
2137 get_friendly_nt_error_msg(status));
2138 talloc_free(tframe);
2142 talloc_free(tframe);
2147 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2150 struct pdb_methods *methods;
2152 PyObject *py_alias_sid, *py_member_sid;
2153 struct dom_sid *alias_sid, *member_sid;
2155 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2156 dom_sid_Type, &py_member_sid)) {
2160 methods = pytalloc_get_ptr(self);
2162 if ((tframe = talloc_stackframe()) == NULL) {
2167 alias_sid = pytalloc_get_ptr(py_alias_sid);
2168 member_sid = pytalloc_get_ptr(py_member_sid);
2170 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2171 if (!NT_STATUS_IS_OK(status)) {
2172 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2173 NT_STATUS_V(status),
2174 get_friendly_nt_error_msg(status));
2175 talloc_free(tframe);
2179 talloc_free(tframe);
2184 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2187 struct pdb_methods *methods;
2189 PyObject *py_alias_sid, *py_member_sid;
2190 const struct dom_sid *alias_sid, *member_sid;
2192 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2193 dom_sid_Type, &py_member_sid)) {
2197 methods = pytalloc_get_ptr(self);
2199 if ((tframe = talloc_stackframe()) == NULL) {
2204 alias_sid = pytalloc_get_ptr(py_alias_sid);
2205 member_sid = pytalloc_get_ptr(py_member_sid);
2207 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2208 if (!NT_STATUS_IS_OK(status)) {
2209 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2210 NT_STATUS_V(status),
2211 get_friendly_nt_error_msg(status));
2212 talloc_free(tframe);
2216 talloc_free(tframe);
2221 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2224 struct pdb_methods *methods;
2226 PyObject *py_alias_sid;
2227 struct dom_sid *alias_sid, *member_sid;
2228 PyObject *py_member_list, *py_member_sid;
2232 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2236 methods = pytalloc_get_ptr(self);
2238 if ((tframe = talloc_stackframe()) == NULL) {
2243 alias_sid = pytalloc_get_ptr(py_alias_sid);
2245 status = methods->enum_aliasmem(methods, alias_sid, tframe, &member_sid, &num_members);
2246 if (!NT_STATUS_IS_OK(status)) {
2247 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2248 NT_STATUS_V(status),
2249 get_friendly_nt_error_msg(status));
2250 talloc_free(tframe);
2254 py_member_list = PyList_New(0);
2255 if (py_member_list == NULL) {
2257 talloc_free(tframe);
2261 for(i=0; i<num_members; i++) {
2262 py_member_sid = pytalloc_steal(dom_sid_Type, &member_sid[i]);
2263 if (py_member_sid) {
2264 PyList_Append(py_member_list, py_member_sid);
2268 talloc_free(tframe);
2270 return py_member_list;
2274 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2277 struct pdb_methods *methods;
2279 PyObject *py_acct_policy;
2283 enum pdb_policy_type type;
2285 methods = pytalloc_get_ptr(self);
2287 if ((tframe = talloc_stackframe()) == NULL) {
2292 py_acct_policy = PyDict_New();
2293 if (py_acct_policy == NULL) {
2298 account_policy_names_list(tframe, &names, &count);
2299 for (i=0; i<count; i++) {
2300 type = account_policy_name_to_typenum(names[i]);
2301 status = methods->get_account_policy(methods, type, &value);
2302 if (NT_STATUS_IS_OK(status)) {
2303 PyDict_SetItemString(py_acct_policy, names[i], PyInt_FromLong(value));
2307 talloc_free(tframe);
2309 return py_acct_policy;
2313 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2316 struct pdb_methods *methods;
2318 PyObject *py_acct_policy, *py_value;
2321 enum pdb_policy_type type;
2323 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2327 methods = pytalloc_get_ptr(self);
2329 if ((tframe = talloc_stackframe()) == NULL) {
2334 account_policy_names_list(tframe, &names, &count);
2335 for (i=0; i<count; i++) {
2336 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2337 type = account_policy_name_to_typenum(names[i]);
2338 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2339 if (!NT_STATUS_IS_OK(status)) {
2340 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2342 NT_STATUS_V(status),
2343 get_friendly_nt_error_msg(status));
2349 talloc_free(tframe);
2354 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2357 struct pdb_methods *methods;
2359 unsigned int acct_flags;
2360 struct pdb_search *search;
2361 struct samr_displayentry *entry;
2362 PyObject *py_userlist, *py_dict;
2364 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2368 methods = pytalloc_get_ptr(self);
2370 if ((tframe = talloc_stackframe()) == NULL) {
2375 search = talloc_zero(tframe, struct pdb_search);
2376 if (search == NULL) {
2378 talloc_free(tframe);
2382 if (!methods->search_users(methods, search, acct_flags)) {
2383 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
2384 NT_STATUS_V(status),
2385 get_friendly_nt_error_msg(status));
2386 talloc_free(tframe);
2390 entry = talloc_zero(tframe, struct samr_displayentry);
2391 if (entry == NULL) {
2393 talloc_free(tframe);
2397 py_userlist = PyList_New(0);
2398 if (py_userlist == NULL) {
2400 talloc_free(tframe);
2404 while (search->next_entry(search, entry)) {
2405 py_dict = PyDict_New();
2406 if (py_dict == NULL) {
2409 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2410 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2411 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2412 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2413 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2414 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2415 PyList_Append(py_userlist, py_dict);
2418 search->search_end(search);
2420 talloc_free(tframe);
2426 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2429 struct pdb_methods *methods;
2431 struct pdb_search *search;
2432 struct samr_displayentry *entry;
2433 PyObject *py_grouplist, *py_dict;
2435 methods = pytalloc_get_ptr(self);
2437 if ((tframe = talloc_stackframe()) == NULL) {
2442 search = talloc_zero(tframe, struct pdb_search);
2443 if (search == NULL) {
2445 talloc_free(tframe);
2449 if (!methods->search_groups(methods, search)) {
2450 PyErr_Format(py_pdb_error, "Unable to search groups, (%d,%s)",
2451 NT_STATUS_V(status),
2452 get_friendly_nt_error_msg(status));
2453 talloc_free(tframe);
2457 entry = talloc_zero(tframe, struct samr_displayentry);
2458 if (entry == NULL) {
2460 talloc_free(tframe);
2464 py_grouplist = PyList_New(0);
2465 if (py_grouplist == NULL) {
2467 talloc_free(tframe);
2471 while (search->next_entry(search, entry)) {
2472 py_dict = PyDict_New();
2473 if (py_dict == NULL) {
2476 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2477 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2478 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2479 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2480 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2481 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2482 PyList_Append(py_grouplist, py_dict);
2485 search->search_end(search);
2487 talloc_free(tframe);
2489 return py_grouplist;
2493 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2495 struct pdb_methods *methods;
2497 struct pdb_search *search;
2498 struct samr_displayentry *entry;
2499 PyObject *py_aliaslist, *py_dict;
2500 PyObject *py_domain_sid;
2501 struct dom_sid *domain_sid = NULL;
2503 py_domain_sid = Py_None;
2506 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2510 methods = pytalloc_get_ptr(self);
2512 if ((tframe = talloc_stackframe()) == NULL) {
2517 if (py_domain_sid != Py_None) {
2518 domain_sid = pytalloc_get_ptr(py_domain_sid);
2521 search = talloc_zero(tframe, struct pdb_search);
2522 if (search == NULL) {
2524 talloc_free(tframe);
2528 if (!methods->search_aliases(methods, search, domain_sid)) {
2529 PyErr_Format(py_pdb_error, "Unable to search aliases");
2530 talloc_free(tframe);
2534 entry = talloc_zero(tframe, struct samr_displayentry);
2535 if (entry == NULL) {
2537 talloc_free(tframe);
2541 py_aliaslist = PyList_New(0);
2542 if (py_aliaslist == NULL) {
2544 talloc_free(tframe);
2548 while (search->next_entry(search, entry)) {
2549 py_dict = PyDict_New();
2550 if (py_dict == NULL) {
2553 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2554 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2555 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2556 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2557 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2558 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2559 PyList_Append(py_aliaslist, py_dict);
2562 search->search_end(search);
2564 talloc_free(tframe);
2566 return py_aliaslist;
2570 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2572 struct pdb_methods *methods;
2575 struct dom_sid user_sid, *copy_user_sid;
2576 PyObject *py_user_sid;
2578 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2582 methods = pytalloc_get_ptr(self);
2584 if ((tframe = talloc_stackframe()) == NULL) {
2589 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2590 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2591 talloc_free(tframe);
2595 copy_user_sid = dom_sid_dup(tframe, &user_sid);
2596 if (copy_user_sid == NULL) {
2598 talloc_free(tframe);
2602 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2604 talloc_free(tframe);
2610 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2612 struct pdb_methods *methods;
2615 struct dom_sid group_sid, *copy_group_sid;
2616 PyObject *py_group_sid;
2618 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2622 methods = pytalloc_get_ptr(self);
2624 if ((tframe = talloc_stackframe()) == NULL) {
2629 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2630 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2631 talloc_free(tframe);
2635 copy_group_sid = dom_sid_dup(tframe, &group_sid);
2636 if (copy_group_sid == NULL) {
2638 talloc_free(tframe);
2642 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2644 talloc_free(tframe);
2646 return py_group_sid;
2650 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2652 struct pdb_methods *methods;
2655 struct dom_sid *sid;
2657 enum lsa_SidType type;
2659 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2663 methods = pytalloc_get_ptr(self);
2665 if ((tframe = talloc_stackframe()) == NULL) {
2670 sid = pytalloc_get_ptr(py_sid);
2672 if (!methods->sid_to_id(methods, sid, &id, &type)) {
2673 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2674 talloc_free(tframe);
2678 talloc_free(tframe);
2680 return Py_BuildValue("(II)", id.uid, type);
2684 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2686 struct pdb_methods *methods;
2690 methods = pytalloc_get_ptr(self);
2692 if ((tframe = talloc_stackframe()) == NULL) {
2697 if (!methods->new_rid(methods, &rid)) {
2698 PyErr_Format(py_pdb_error, "Unable to get new rid");
2699 talloc_free(tframe);
2703 talloc_free(tframe);
2705 return PyInt_FromLong(rid);
2709 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2711 struct pdb_methods *methods;
2715 struct dom_sid sid, *copy_sid;
2717 time_t last_set_time;
2720 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2724 methods = pytalloc_get_ptr(self);
2726 if ((tframe = talloc_stackframe()) == NULL) {
2731 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2732 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2733 talloc_free(tframe);
2737 copy_sid = dom_sid_dup(tframe, &sid);
2738 if (copy_sid == NULL) {
2740 talloc_free(tframe);
2744 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2745 if (py_sid == NULL) {
2747 talloc_free(tframe);
2751 talloc_free(tframe);
2753 py_value = PyDict_New();
2754 if (py_value == NULL) {
2759 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2760 PyDict_SetItemString(py_value, "sid", py_sid);
2761 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2767 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2769 struct pdb_methods *methods;
2773 const struct dom_sid *domain_sid;
2774 PyObject *py_domain_sid;
2776 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2777 dom_sid_Type, &py_domain_sid)) {
2781 methods = pytalloc_get_ptr(self);
2783 if ((tframe = talloc_stackframe()) == NULL) {
2788 domain_sid = pytalloc_get_ptr(py_domain_sid);
2790 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2791 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2792 talloc_free(tframe);
2800 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2802 struct pdb_methods *methods;
2806 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2810 methods = pytalloc_get_ptr(self);
2812 if ((tframe = talloc_stackframe()) == NULL) {
2817 if (!methods->del_trusteddom_pw(methods, domain)) {
2818 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2819 talloc_free(tframe);
2827 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2830 struct pdb_methods *methods;
2832 uint32_t num_domains;
2833 struct trustdom_info **domains;
2834 PyObject *py_domain_list, *py_dict;
2837 methods = pytalloc_get_ptr(self);
2839 if ((tframe = talloc_stackframe()) == NULL) {
2844 status = methods->enum_trusteddoms(methods, tframe, &num_domains, &domains);
2845 if (!NT_STATUS_IS_OK(status)) {
2846 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2847 NT_STATUS_V(status),
2848 get_friendly_nt_error_msg(status));
2849 talloc_free(tframe);
2853 py_domain_list = PyList_New(0);
2854 if (py_domain_list == NULL) {
2856 talloc_free(tframe);
2860 for(i=0; i<num_domains; i++) {
2861 py_dict = PyDict_New();
2863 PyDict_SetItemString(py_dict, "name",
2864 PyString_FromString(domains[i]->name));
2865 PyDict_SetItemString(py_dict, "sid",
2866 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2869 PyList_Append(py_domain_list, py_dict);
2872 talloc_free(tframe);
2874 return py_domain_list;
2878 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2881 struct pdb_methods *methods;
2884 struct pdb_trusted_domain *td;
2885 PyObject *py_domain_info;
2887 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2891 methods = pytalloc_get_ptr(self);
2893 if ((tframe = talloc_stackframe()) == NULL) {
2898 status = methods->get_trusted_domain(methods, tframe, domain, &td);
2899 if (!NT_STATUS_IS_OK(status)) {
2900 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2901 NT_STATUS_V(status),
2902 get_friendly_nt_error_msg(status));
2903 talloc_free(tframe);
2907 py_domain_info = PyDict_New();
2908 if (py_domain_info == NULL) {
2910 talloc_free(tframe);
2914 PyDict_SetItemString(py_domain_info, "domain_name",
2915 PyString_FromString(td->domain_name));
2916 PyDict_SetItemString(py_domain_info, "netbios_name",
2917 PyString_FromString(td->netbios_name));
2918 PyDict_SetItemString(py_domain_info, "security_identifier",
2919 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2920 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2921 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2922 td->trust_auth_incoming.length));
2923 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2924 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2925 td->trust_auth_outgoing.length));
2926 PyDict_SetItemString(py_domain_info, "trust_direction",
2927 PyInt_FromLong(td->trust_direction));
2928 PyDict_SetItemString(py_domain_info, "trust_type",
2929 PyInt_FromLong(td->trust_type));
2930 PyDict_SetItemString(py_domain_info, "trust_attributes",
2931 PyInt_FromLong(td->trust_attributes));
2932 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2933 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2934 td->trust_forest_trust_info.length));
2936 talloc_free(tframe);
2938 return py_domain_info;
2942 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
2945 struct pdb_methods *methods;
2947 PyObject *py_domain_sid;
2948 struct dom_sid *domain_sid;
2949 struct pdb_trusted_domain *td;
2950 PyObject *py_domain_info;
2952 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
2956 methods = pytalloc_get_ptr(self);
2958 if ((tframe = talloc_stackframe()) == NULL) {
2963 domain_sid = pytalloc_get_ptr(py_domain_sid);
2965 status = methods->get_trusted_domain_by_sid(methods, tframe, domain_sid, &td);
2966 if (!NT_STATUS_IS_OK(status)) {
2967 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2968 NT_STATUS_V(status),
2969 get_friendly_nt_error_msg(status));
2970 talloc_free(tframe);
2974 py_domain_info = PyDict_New();
2975 if (py_domain_info == NULL) {
2977 talloc_free(tframe);
2981 PyDict_SetItemString(py_domain_info, "domain_name",
2982 PyString_FromString(td->domain_name));
2983 PyDict_SetItemString(py_domain_info, "netbios_name",
2984 PyString_FromString(td->netbios_name));
2985 PyDict_SetItemString(py_domain_info, "security_identifier",
2986 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2987 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2988 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2989 td->trust_auth_incoming.length));
2990 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2991 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2992 td->trust_auth_outgoing.length));
2993 PyDict_SetItemString(py_domain_info, "trust_direction",
2994 PyInt_FromLong(td->trust_direction));
2995 PyDict_SetItemString(py_domain_info, "trust_type",
2996 PyInt_FromLong(td->trust_type));
2997 PyDict_SetItemString(py_domain_info, "trust_attributes",
2998 PyInt_FromLong(td->trust_attributes));
2999 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3000 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3001 td->trust_forest_trust_info.length));
3003 talloc_free(tframe);
3005 return py_domain_info;
3009 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3012 struct pdb_methods *methods;
3015 PyObject *py_td_info;
3016 struct pdb_trusted_domain td_info;
3020 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3024 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3025 td_info.domain_name = PyString_AsString(py_tmp);
3027 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3028 td_info.netbios_name = PyString_AsString(py_tmp);
3030 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3031 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3033 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3034 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3035 td_info.trust_auth_incoming.length = len;
3037 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3038 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3039 td_info.trust_auth_outgoing.length = len;
3041 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3042 td_info.trust_direction = PyInt_AsLong(py_tmp);
3044 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3045 td_info.trust_type = PyInt_AsLong(py_tmp);
3047 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3048 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3050 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3051 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3052 td_info.trust_forest_trust_info.length = len;
3054 methods = pytalloc_get_ptr(self);
3056 if ((tframe = talloc_stackframe()) == NULL) {
3061 status = methods->set_trusted_domain(methods, domain, &td_info);
3062 if (!NT_STATUS_IS_OK(status)) {
3063 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3064 NT_STATUS_V(status),
3065 get_friendly_nt_error_msg(status));
3066 talloc_free(tframe);
3070 talloc_free(tframe);
3076 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3079 struct pdb_methods *methods;
3083 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3087 methods = pytalloc_get_ptr(self);
3089 if ((tframe = talloc_stackframe()) == NULL) {
3094 status = methods->del_trusted_domain(methods, domain);
3095 if (!NT_STATUS_IS_OK(status)) {
3096 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3097 NT_STATUS_V(status),
3098 get_friendly_nt_error_msg(status));
3099 talloc_free(tframe);
3103 talloc_free(tframe);
3109 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3112 struct pdb_methods *methods;
3114 uint32_t num_domains;
3115 struct pdb_trusted_domain **td_info, *td;
3116 PyObject *py_td_info, *py_domain_info;
3119 methods = pytalloc_get_ptr(self);
3121 if ((tframe = talloc_stackframe()) == NULL) {
3126 status = methods->enum_trusted_domains(methods, tframe, &num_domains, &td_info);
3127 if (!NT_STATUS_IS_OK(status)) {
3128 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3129 NT_STATUS_V(status),
3130 get_friendly_nt_error_msg(status));
3131 talloc_free(tframe);
3135 py_td_info = PyList_New(0);
3136 if (py_td_info == NULL) {
3138 talloc_free(tframe);
3142 for (i=0; i<num_domains; i++) {
3144 py_domain_info = PyDict_New();
3145 if (py_domain_info == NULL) {
3147 Py_DECREF(py_td_info);
3148 talloc_free(tframe);
3154 PyDict_SetItemString(py_domain_info, "domain_name",
3155 PyString_FromString(td->domain_name));
3156 PyDict_SetItemString(py_domain_info, "netbios_name",
3157 PyString_FromString(td->netbios_name));
3158 PyDict_SetItemString(py_domain_info, "security_identifier",
3159 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3160 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3161 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3162 td->trust_auth_incoming.length));
3163 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3164 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3165 td->trust_auth_outgoing.length));
3166 PyDict_SetItemString(py_domain_info, "trust_direction",
3167 PyInt_FromLong(td->trust_direction));
3168 PyDict_SetItemString(py_domain_info, "trust_type",
3169 PyInt_FromLong(td->trust_type));
3170 PyDict_SetItemString(py_domain_info, "trust_attributes",
3171 PyInt_FromLong(td->trust_attributes));
3172 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3173 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3174 td->trust_forest_trust_info.length));
3175 PyList_Append(py_td_info, py_domain_info);
3178 talloc_free(tframe);
3184 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3187 struct pdb_methods *methods;
3189 const char *secret_name;
3190 DATA_BLOB secret_current, secret_old;
3191 NTTIME secret_current_lastchange, secret_old_lastchange;
3193 struct security_descriptor *sd;
3194 PyObject *py_secret;
3196 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3200 methods = pytalloc_get_ptr(self);
3202 if ((tframe = talloc_stackframe()) == NULL) {
3207 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3208 if (py_sd == NULL) {
3210 talloc_free(tframe);
3213 sd = pytalloc_get_ptr(py_sd);
3215 status = methods->get_secret(methods, tframe, secret_name,
3217 &secret_current_lastchange,
3219 &secret_old_lastchange,
3221 if (!NT_STATUS_IS_OK(status)) {
3222 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3224 NT_STATUS_V(status),
3225 get_friendly_nt_error_msg(status));
3226 talloc_free(tframe);
3230 py_secret = PyDict_New();
3231 if (py_secret == NULL) {
3234 talloc_free(tframe);
3238 PyDict_SetItemString(py_secret, "secret_current",
3239 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3240 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3241 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3242 PyDict_SetItemString(py_secret, "secret_old",
3243 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3244 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3245 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3246 PyDict_SetItemString(py_secret, "sd", py_sd);
3248 talloc_free(tframe);
3254 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3257 struct pdb_methods *methods;
3259 const char *secret_name;
3260 PyObject *py_secret;
3261 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3262 DATA_BLOB secret_current, secret_old;
3263 struct security_descriptor *sd;
3266 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3270 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3271 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3272 py_sd = PyDict_GetItemString(py_secret, "sd");
3274 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3275 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3276 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3278 methods = pytalloc_get_ptr(self);
3280 if ((tframe = talloc_stackframe()) == NULL) {
3285 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3286 secret_current.length = len;
3287 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3288 secret_current.length = len;
3289 sd = pytalloc_get_ptr(py_sd);
3291 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3292 if (!NT_STATUS_IS_OK(status)) {
3293 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3295 NT_STATUS_V(status),
3296 get_friendly_nt_error_msg(status));
3297 talloc_free(tframe);
3301 talloc_free(tframe);
3307 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3310 struct pdb_methods *methods;
3312 const char *secret_name;
3314 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3318 methods = pytalloc_get_ptr(self);
3320 if ((tframe = talloc_stackframe()) == NULL) {
3325 status = methods->delete_secret(methods, secret_name);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3329 NT_STATUS_V(status),
3330 get_friendly_nt_error_msg(status));
3331 talloc_free(tframe);
3335 talloc_free(tframe);
3340 static PyMethodDef py_pdb_methods[] = {
3341 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3342 "domain_info() -> str\n\n \
3343 Get domain information for the database." },
3344 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3345 "getsampwnam(username) -> samu object\n\n \
3346 Get user information by name." },
3347 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3348 "getsampwsid(user_sid) -> samu object\n\n \
3349 Get user information by sid (dcerpc.security.dom_sid object)." },
3350 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3351 "create_user(username, acct_flags) -> rid\n\n \
3352 Create user. acct_flags are samr account control flags." },
3353 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3354 "delete_user(samu object) -> None\n\n \
3356 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3357 "add_sam_account(samu object) -> None\n\n \
3358 Add SAM account." },
3359 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3360 "update_sam_account(samu object) -> None\n\n \
3361 Update SAM account." },
3362 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3363 "delete_sam_account(samu object) -> None\n\n \
3364 Delete SAM account." },
3365 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3366 "rename_sam_account(samu object1, new_username) -> None\n\n \
3367 Rename SAM account." },
3368 /* update_login_attempts */
3369 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3370 "getgrsid(group_sid) -> groupmap object\n\n \
3371 Get group information by sid (dcerpc.security.dom_sid object)." },
3372 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3373 "getgrsid(gid) -> groupmap object\n\n \
3374 Get group information by gid." },
3375 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3376 "getgrsid(groupname) -> groupmap object\n\n \
3377 Get group information by name." },
3378 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3379 "create_dom_group(groupname) -> group_rid\n\n \
3380 Create new domain group by name." },
3381 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3382 "delete_dom_group(group_rid) -> None\n\n \
3383 Delete domain group identified by rid" },
3384 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3385 "add_group_mapping_entry(groupmap) -> None\n \
3386 Add group mapping entry for groupmap object." },
3387 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3388 "update_group_mapping_entry(groupmap) -> None\n\n \
3389 Update group mapping entry for groupmap object." },
3390 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3391 "delete_group_mapping_entry(groupmap) -> None\n\n \
3392 Delete group mapping entry for groupmap object." },
3393 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3394 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3395 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3396 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3397 "enum_group_members(group_sid) -> List\n\n \
3398 Return list of users (dom_sid object) in group." },
3399 /* enum_group_memberships */
3400 /* set_unix_primary_group */
3401 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3402 "add_groupmem(group_rid, member_rid) -> None\n\n \
3403 Add user to group." },
3404 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3405 "del_groupmem(group_rid, member_rid) -> None\n\n \
3406 Remove user from from group." },
3407 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3408 "create_alias(alias_name) -> alias_rid\n\n \
3409 Create alias entry." },
3410 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3411 "delete_alias(alias_sid) -> None\n\n \
3412 Delete alias entry." },
3413 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3414 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3415 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3416 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3417 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3418 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3419 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3420 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3421 Add user to alias entry." },
3422 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3423 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3424 Remove a user from alias entry." },
3425 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3426 "enum_aliasmem(alias_sid) -> List\n\n \
3427 Return a list of members (dom_sid object) for alias entry." },
3428 /* enum_alias_memberships */
3431 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3432 "get_account_policy() -> Mapping\n\n \
3433 Get account policy information as a dictionary." },
3434 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3435 "get_account_policy(Mapping) -> None\n\n \
3436 Set account policy settings from a dicionary." },
3438 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3439 "search_users(acct_flags) -> List\n\n \
3440 Search users. acct_flags are samr account control flags.\n \
3441 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3442 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3443 "search_groups() -> List\n\n \
3444 Search unix only groups. \n \
3445 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3446 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3447 "search_aliases([domain_sid]) -> List\n\n \
3448 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3449 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3450 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3451 "uid_to_sid(uid) -> sid\n\n \
3452 Return sid for given user id." },
3453 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3454 "gid_to_sid(gid) -> sid\n\n \
3455 Return sid for given group id." },
3456 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3457 "sid_to_id(sid) -> Tuple\n\n \
3458 Return id and type for given sid." },
3460 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3461 "new_rid() -> rid\n\n \
3463 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3464 "get_trusteddom_pw(domain) -> Mapping\n\n \
3465 Get trusted domain password, sid and last set time in a dictionary." },
3466 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3467 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3468 Set trusted domain password." },
3469 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3470 "del_trusteddom_pw(domain) -> None\n\n \
3471 Delete trusted domain password." },
3472 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3473 "enum_trusteddoms() -> List\n\n \
3474 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3475 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3476 "get_trusted_domain(domain) -> Mapping\n\n \
3477 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." },
3478 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3479 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3480 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" },
3481 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3482 "set_trusted_domain(domain, Mapping) -> None\n\n \
3483 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." },
3484 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3485 "del_trusted_domain(domain) -> None\n\n \
3486 Delete trusted domain." },
3487 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3488 "enum_trusted_domains() -> List\n\n \
3489 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." },
3490 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3491 "get_secret(secret_name) -> Mapping\n\n \
3492 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3493 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3494 "set_secret(secret_name, Mapping) -> None\n\n \
3495 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3496 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3497 "delete_secret(secret_name) -> None\n\n \
3498 Delete secret information for secret_name." },
3503 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3505 const char *url = NULL;
3508 struct pdb_methods *methods;
3510 if (!PyArg_ParseTuple(args, "s", &url)) {
3514 /* Initalize list of methods */
3515 status = make_pdb_method_name(&methods, url);
3516 if (!NT_STATUS_IS_OK(status)) {
3517 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3519 NT_STATUS_V(status),
3520 get_friendly_nt_error_msg(status));
3524 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3533 static PyTypeObject PyPDB = {
3534 .tp_name = "passdb.PDB",
3535 .tp_basicsize = sizeof(pytalloc_Object),
3536 .tp_new = py_pdb_new,
3537 .tp_flags = Py_TPFLAGS_DEFAULT,
3538 .tp_methods = py_pdb_methods,
3539 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3544 * Return a list of passdb backends
3546 static PyObject *py_passdb_backends(PyObject *self)
3549 const struct pdb_init_function_entry *entry;
3552 if ((tframe = talloc_stackframe()) == NULL) {
3557 entry = pdb_get_backends();
3562 if((py_blist = PyList_New(0)) == NULL) {
3568 PyList_Append(py_blist, PyString_FromString(entry->name));
3569 entry = entry->next;
3572 talloc_free(tframe);
3578 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3580 const char *smb_config;
3583 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3587 if ((tframe = talloc_stackframe()) == NULL) {
3592 /* Load smbconf parameters */
3593 if (!lp_load_global(smb_config)) {
3594 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3598 talloc_free(tframe);
3604 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3606 const char *private_dir;
3609 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3613 if ((tframe = talloc_stackframe()) == NULL) {
3618 /* Initialize secrets database */
3619 if (!secrets_init_path(private_dir)) {
3620 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3625 talloc_free(tframe);
3630 static PyObject *py_get_global_sam_sid(PyObject *self)
3632 struct dom_sid *domain_sid, *domain_sid_copy;
3634 PyObject *py_dom_sid;
3636 tframe = talloc_stackframe();
3637 if (tframe == NULL) {
3642 domain_sid = get_global_sam_sid();
3644 domain_sid_copy = dom_sid_dup(tframe, domain_sid);
3645 if (domain_sid_copy == NULL) {
3647 talloc_free(tframe);
3651 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3653 talloc_free(tframe);
3659 static PyMethodDef py_passdb_methods[] = {
3660 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3661 "get_backends() -> list\n\n \
3662 Get a list of password database backends supported." },
3663 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3664 "set_smb_config(path) -> None\n\n \
3665 Set path to smb.conf file to load configuration parameters." },
3666 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3667 "set_secrets_dir(private_dir) -> None\n\n \
3668 Set path to private directory to load secrets database from non-default location." },
3669 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3670 "get_global_sam_sid() -> dom_sid\n\n \
3671 Return domain SID." },
3675 void initpassdb(void)
3678 char exception_name[] = "passdb.error";
3680 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3681 if (talloc_type == NULL) {
3685 PyPDB.tp_base = talloc_type;
3686 if (PyType_Ready(&PyPDB) < 0) {
3690 PySamu.tp_base = talloc_type;
3691 if (PyType_Ready(&PySamu) < 0) {
3695 PyGroupmap.tp_base = talloc_type;
3696 if (PyType_Ready(&PyGroupmap) < 0) {
3700 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3705 /* Create new exception for passdb module */
3706 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3707 Py_INCREF(py_pdb_error);
3708 PyModule_AddObject(m, "error", py_pdb_error);
3711 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3714 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3716 Py_INCREF(&PyGroupmap);
3717 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3719 /* Import dom_sid type from dcerpc.security */
3720 mod = PyImport_ImportModule("samba.dcerpc.security");
3725 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3726 if (dom_sid_Type == NULL) {
3730 /* Import security_descriptor type from dcerpc.security */
3731 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3733 if (security_Type == NULL) {
3737 /* Import GUID type from dcerpc.misc */
3738 mod = PyImport_ImportModule("samba.dcerpc.misc");
3743 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3745 if (guid_Type == NULL) {