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 #ifndef Py_RETURN_NONE
29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
32 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
33 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
37 #define PY_CHECK_TYPE(type, var, fail) \
38 if (!PyObject_TypeCheck(var, type)) {\
39 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
45 static PyTypeObject *dom_sid_Type = NULL;
46 static PyTypeObject *security_Type = NULL;
47 static PyTypeObject *guid_Type = NULL;
49 staticforward PyTypeObject PySamu;
50 staticforward PyTypeObject PyGroupmap;
51 staticforward PyTypeObject PyPDB;
53 static PyObject *py_pdb_error;
55 void initpassdb(void);
58 /************************** PIDL Autogeneratd ******************************/
60 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
62 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
63 PyObject *py_logon_time;
65 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
69 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
71 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
73 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
74 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
80 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
82 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
83 PyObject *py_logoff_time;
85 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
86 return py_logoff_time;
89 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
91 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
93 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
94 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
100 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
102 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
103 PyObject *py_kickoff_time;
105 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
106 return py_kickoff_time;
109 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
111 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
113 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
114 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
120 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
122 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
123 PyObject *py_bad_password_time;
125 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
126 return py_bad_password_time;
129 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
131 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
133 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
134 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
140 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
142 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
143 PyObject *py_pass_last_set_time;
145 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
146 return py_pass_last_set_time;
149 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
151 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
153 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
154 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
160 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
162 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
163 PyObject *py_pass_can_change_time;
165 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
166 return py_pass_can_change_time;
169 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
171 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
173 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
174 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
180 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
182 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
183 PyObject *py_pass_must_change_time;
185 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
186 return py_pass_must_change_time;
189 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
191 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
193 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
194 if (!pdb_set_pass_must_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
200 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
202 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
203 PyObject *py_username;
204 const char *username;
206 username = pdb_get_username(sam_acct);
207 if (username == NULL) {
211 py_username = PyString_FromString(username);
215 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
217 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
219 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
220 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
226 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
228 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
232 domain = pdb_get_domain(sam_acct);
233 if (domain == NULL) {
237 py_domain = PyString_FromString(domain);
241 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
243 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
245 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
246 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
252 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
254 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
255 PyObject *py_nt_username;
256 const char *nt_username;
258 nt_username = pdb_get_nt_username(sam_acct);
259 if (nt_username == NULL) {
263 py_nt_username = PyString_FromString(nt_username);
264 return py_nt_username;
267 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
269 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
271 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
272 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
278 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
280 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
281 PyObject *py_full_name;
282 const char *full_name;
284 full_name = pdb_get_fullname(sam_acct);
285 if (full_name == NULL) {
289 py_full_name = PyString_FromString(full_name);
293 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
295 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
297 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
298 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
304 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
306 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
307 PyObject *py_home_dir;
308 const char *home_dir;
310 home_dir = pdb_get_homedir(sam_acct);
311 if (home_dir == NULL) {
315 py_home_dir = PyString_FromString(home_dir);
319 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
321 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
323 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
324 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
330 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
332 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
333 PyObject *py_dir_drive;
334 const char *dir_drive;
336 dir_drive = pdb_get_dir_drive(sam_acct);
337 if (dir_drive == NULL) {
341 py_dir_drive = PyString_FromString(dir_drive);
345 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
347 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
349 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
350 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
356 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
358 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
359 PyObject *py_logon_script;
360 const char *logon_script;
362 logon_script = pdb_get_logon_script(sam_acct);
363 if (logon_script == NULL) {
367 py_logon_script = PyString_FromString(logon_script);
368 return py_logon_script;
371 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
373 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
375 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
376 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
382 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
384 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
385 PyObject *py_profile_path;
386 const char *profile_path;
388 profile_path = pdb_get_profile_path(sam_acct);
389 if (profile_path == NULL) {
393 py_profile_path = PyString_FromString(profile_path);
394 return py_profile_path;
397 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
399 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
401 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
402 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
408 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
410 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
411 PyObject *py_acct_desc;
412 const char *acct_desc;
414 acct_desc = pdb_get_acct_desc(sam_acct);
415 if (acct_desc == NULL) {
419 py_acct_desc = PyString_FromString(acct_desc);
423 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
425 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
427 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
428 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
434 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
436 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
437 PyObject *py_workstations;
438 const char *workstations;
440 workstations = pdb_get_workstations(sam_acct);
441 if (workstations == NULL) {
445 py_workstations = PyString_FromString(workstations);
446 return py_workstations;
449 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
451 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
453 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
454 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
460 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
462 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
463 PyObject *py_comment;
466 comment = pdb_get_comment(sam_acct);
467 if (comment == NULL) {
471 py_comment = PyString_FromString(comment);
475 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
477 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
479 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
480 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
486 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
488 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
489 PyObject *py_munged_dial;
490 const char *munged_dial;
492 munged_dial = pdb_get_munged_dial(sam_acct);
493 if (munged_dial == NULL) {
497 py_munged_dial = PyString_FromString(munged_dial);
498 return py_munged_dial;
501 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
503 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
505 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
506 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
512 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
514 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
515 PyObject *py_user_sid;
516 const struct dom_sid *user_sid;
517 struct dom_sid *copy_user_sid;
520 user_sid = pdb_get_user_sid(sam_acct);
521 if(user_sid == NULL) {
525 mem_ctx = talloc_new(NULL);
526 if (mem_ctx == NULL) {
530 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
531 if (copy_user_sid == NULL) {
533 talloc_free(mem_ctx);
537 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
539 talloc_free(mem_ctx);
544 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
546 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
548 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
549 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
555 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
557 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
558 PyObject *py_group_sid;
559 const struct dom_sid *group_sid;
560 struct dom_sid *copy_group_sid;
563 group_sid = pdb_get_group_sid(sam_acct);
564 if (group_sid == NULL) {
568 mem_ctx = talloc_new(NULL);
569 if (mem_ctx == NULL) {
573 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
574 if (copy_group_sid == NULL) {
576 talloc_free(mem_ctx);
580 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
582 talloc_free(mem_ctx);
587 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
589 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
591 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
592 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
598 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
600 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
604 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
609 py_lm_pw = PyString_FromString(lm_pw);
613 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
615 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
617 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
618 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
624 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
626 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
630 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
635 py_nt_pw = PyString_FromString(nt_pw);
639 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
641 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
643 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
649 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
651 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
652 PyObject *py_nt_pw_his;
653 const char *nt_pw_his;
656 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
657 if (nt_pw_his == NULL) {
661 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len);
665 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
667 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
672 PyString_AsStringAndSize(value, &nt_pw_his, &len);
674 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
680 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
682 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
683 PyObject *py_plaintext_pw;
684 const char *plaintext_pw;
686 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
687 if (plaintext_pw == NULL) {
691 py_plaintext_pw = PyString_FromString(plaintext_pw);
692 return py_plaintext_pw;
695 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
697 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
699 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
705 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
707 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
708 PyObject *py_acct_ctrl;
710 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
714 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
716 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
718 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
719 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
725 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
727 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
728 PyObject *py_logon_divs;
730 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
731 return py_logon_divs;
734 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
736 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
738 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
739 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
745 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
747 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
748 PyObject *py_hours_len;
750 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
754 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
756 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
758 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
759 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
765 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
767 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
772 hours = (const char *)pdb_get_hours(sam_acct);
777 if ((py_hours = PyList_New(MAX_HOURS_LEN)) == NULL) {
782 for (i=0; i<MAX_HOURS_LEN; i++) {
783 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
788 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
790 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
796 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
798 hours_len = PyList_GET_SIZE(value);
800 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
806 for (i=0; i < hours_len; i++) {
807 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
808 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
811 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
820 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
822 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
823 PyObject *py_bad_password_count;
825 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
826 return py_bad_password_count;
829 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
831 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
833 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
834 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
840 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
842 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
843 PyObject *py_logon_count;
845 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
846 return py_logon_count;
849 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
851 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
853 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
854 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
860 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
862 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
863 PyObject *py_country_code;
865 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
866 return py_country_code;
869 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
871 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
873 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
874 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
880 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
882 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
883 PyObject *py_code_page;
885 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
889 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
891 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
893 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
894 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
900 static PyGetSetDef py_samu_getsetters[] = {
901 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
902 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
903 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
904 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
905 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
906 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
907 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
908 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
909 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
910 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
911 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
912 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
913 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
914 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
915 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
916 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
917 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
918 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
919 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
920 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
921 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
922 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
923 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
924 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
925 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
926 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
927 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
928 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
929 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
930 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
931 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
932 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
933 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
938 /************************** PIDL Autogeneratd ******************************/
940 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
942 struct samu *sam_acct;
944 sam_acct = samu_new(NULL);
950 return pytalloc_steal(type, sam_acct);
953 static PyTypeObject PySamu = {
954 .tp_name = "passdb.Samu",
955 .tp_basicsize = sizeof(pytalloc_Object),
956 .tp_getset = py_samu_getsetters,
958 .tp_new = py_samu_new,
959 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
960 .tp_doc = "Samu() -> samu object\n",
964 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
966 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
969 py_gid = PyInt_FromLong(group_map->gid);
973 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
975 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
977 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
978 group_map->gid = PyInt_AsLong(value);
982 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
984 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
986 struct dom_sid *group_sid;
989 mem_ctx = talloc_new(NULL);
990 if (mem_ctx == NULL) {
995 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
996 if (group_sid == NULL) {
998 talloc_free(mem_ctx);
1002 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1004 talloc_free(mem_ctx);
1009 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1011 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1013 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1014 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1018 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1020 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1021 PyObject *py_sid_name_use;
1023 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1024 return py_sid_name_use;
1027 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1029 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1031 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1032 group_map->sid_name_use = PyInt_AsLong(value);
1036 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1038 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1039 PyObject *py_nt_name;
1040 if (group_map->nt_name == NULL) {
1041 py_nt_name = Py_None;
1042 Py_INCREF(py_nt_name);
1044 py_nt_name = PyString_FromString(group_map->nt_name);
1049 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1051 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1053 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1054 if (value == Py_None) {
1055 fstrcpy(group_map->nt_name, NULL);
1057 fstrcpy(group_map->nt_name, PyString_AsString(value));
1062 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1064 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1065 PyObject *py_comment;
1066 if (group_map->comment == NULL) {
1067 py_comment = Py_None;
1068 Py_INCREF(py_comment);
1070 py_comment = PyString_FromString(group_map->comment);
1075 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1077 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1079 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1080 if (value == Py_None) {
1081 fstrcpy(group_map->comment, NULL);
1083 fstrcpy(group_map->comment, PyString_AsString(value));
1088 static PyGetSetDef py_groupmap_getsetters[] = {
1089 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1090 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1091 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1092 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1093 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1097 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1099 GROUP_MAP *group_map;
1100 TALLOC_CTX *mem_ctx;
1101 PyObject *py_group_map;
1103 mem_ctx = talloc_new(NULL);
1104 if (mem_ctx == NULL) {
1109 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1110 if (group_map == NULL) {
1112 talloc_free(mem_ctx);
1116 py_group_map = pytalloc_steal(type, group_map);
1117 if (py_group_map == NULL) {
1119 talloc_free(mem_ctx);
1123 talloc_free(mem_ctx);
1125 return py_group_map;
1129 static PyTypeObject PyGroupmap = {
1130 .tp_name = "passdb.Groupmap",
1131 .tp_basicsize = sizeof(pytalloc_Object),
1132 .tp_getset = py_groupmap_getsetters,
1134 .tp_new = py_groupmap_new,
1135 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1136 .tp_doc = "Groupmap() -> group map object\n",
1140 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1142 struct pdb_methods *methods;
1143 struct pdb_domain_info *domain_info;
1144 PyObject *py_domain_info;
1146 struct dom_sid *sid;
1149 methods = pytalloc_get_ptr(self);
1151 if ((tframe = talloc_stackframe()) == NULL) {
1156 domain_info = methods->get_domain_info(methods, tframe);
1157 if (! domain_info) {
1161 sid = dom_sid_dup(tframe, &domain_info->sid);
1164 talloc_free(tframe);
1168 guid = talloc(tframe, struct GUID);
1171 talloc_free(tframe);
1174 *guid = domain_info->guid;
1176 if ((py_domain_info = PyDict_New()) == NULL) {
1181 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1182 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->name));
1183 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->name));
1184 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1185 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1187 talloc_free(tframe);
1189 return py_domain_info;
1193 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1196 const char *username;
1197 struct pdb_methods *methods;
1198 struct samu *sam_acct;
1199 PyObject *py_sam_acct;
1202 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1206 methods = pytalloc_get_ptr(self);
1208 if ((tframe = talloc_stackframe()) == NULL) {
1213 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1214 if (py_sam_acct == NULL) {
1216 talloc_free(tframe);
1219 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1221 status = methods->getsampwnam(methods, sam_acct, username);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1225 NT_STATUS_V(status),
1226 get_friendly_nt_error_msg(status));
1227 Py_DECREF(py_sam_acct);
1228 talloc_free(tframe);
1232 talloc_free(tframe);
1236 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1239 struct pdb_methods *methods;
1240 struct samu *sam_acct;
1241 PyObject *py_sam_acct;
1243 PyObject *py_user_sid;
1245 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1249 methods = pytalloc_get_ptr(self);
1251 if ((tframe = talloc_stackframe()) == NULL) {
1256 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1257 if (py_sam_acct == NULL) {
1259 talloc_free(tframe);
1262 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1264 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1265 if (!NT_STATUS_IS_OK(status)) {
1266 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1267 NT_STATUS_V(status),
1268 get_friendly_nt_error_msg(status));
1269 Py_DECREF(py_sam_acct);
1270 talloc_free(tframe);
1274 talloc_free(tframe);
1278 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1281 struct pdb_methods *methods;
1282 const char *username;
1283 unsigned int acct_flags;
1287 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1291 methods = pytalloc_get_ptr(self);
1293 if ((tframe = talloc_stackframe()) == NULL) {
1298 status = methods->create_user(methods, tframe, username, acct_flags, &rid);
1299 if (!NT_STATUS_IS_OK(status)) {
1300 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1302 NT_STATUS_V(status),
1303 get_friendly_nt_error_msg(status));
1304 talloc_free(tframe);
1308 talloc_free(tframe);
1309 return PyInt_FromLong(rid);
1312 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1315 struct pdb_methods *methods;
1317 struct samu *sam_acct;
1318 PyObject *py_sam_acct;
1320 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1324 methods = pytalloc_get_ptr(self);
1326 if ((tframe = talloc_stackframe()) == NULL) {
1331 sam_acct = pytalloc_get_ptr(py_sam_acct);
1333 status = methods->delete_user(methods, tframe, sam_acct);
1334 if (!NT_STATUS_IS_OK(status)) {
1335 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1336 NT_STATUS_V(status),
1337 get_friendly_nt_error_msg(status));
1338 talloc_free(tframe);
1342 talloc_free(tframe);
1346 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1349 struct pdb_methods *methods;
1351 struct samu *sam_acct;
1352 PyObject *py_sam_acct;
1354 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1358 methods = pytalloc_get_ptr(self);
1360 if ((tframe = talloc_stackframe()) == NULL) {
1365 sam_acct = pytalloc_get_ptr(py_sam_acct);
1367 status = methods->add_sam_account(methods, sam_acct);
1368 if (!NT_STATUS_IS_OK(status)) {
1369 PyErr_Format(py_pdb_error, "Unable to add sam account, (%d,%s)",
1370 NT_STATUS_V(status),
1371 get_friendly_nt_error_msg(status));
1372 talloc_free(tframe);
1376 talloc_free(tframe);
1380 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1383 struct pdb_methods *methods;
1385 struct samu *sam_acct;
1386 PyObject *py_sam_acct;
1388 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1392 methods = pytalloc_get_ptr(self);
1394 if ((tframe = talloc_stackframe()) == NULL) {
1399 sam_acct = pytalloc_get_ptr(py_sam_acct);
1401 status = methods->update_sam_account(methods, sam_acct);
1402 if (!NT_STATUS_IS_OK(status)) {
1403 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1404 NT_STATUS_V(status),
1405 get_friendly_nt_error_msg(status));
1406 talloc_free(tframe);
1410 talloc_free(tframe);
1414 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1417 struct pdb_methods *methods;
1419 struct samu *sam_acct;
1420 PyObject *py_sam_acct;
1422 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1426 methods = pytalloc_get_ptr(self);
1428 if ((tframe = talloc_stackframe()) == NULL) {
1433 sam_acct = pytalloc_get_ptr(py_sam_acct);
1435 status = methods->delete_sam_account(methods, sam_acct);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1438 NT_STATUS_V(status),
1439 get_friendly_nt_error_msg(status));
1440 talloc_free(tframe);
1444 talloc_free(tframe);
1448 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1451 struct pdb_methods *methods;
1453 struct samu *sam_acct;
1454 const char *new_username;
1455 PyObject *py_sam_acct;
1457 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1462 methods = pytalloc_get_ptr(self);
1464 if ((tframe = talloc_stackframe()) == NULL) {
1469 sam_acct = pytalloc_get_ptr(py_sam_acct);
1471 status = methods->rename_sam_account(methods, sam_acct, new_username);
1472 if (!NT_STATUS_IS_OK(status)) {
1473 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1474 NT_STATUS_V(status),
1475 get_friendly_nt_error_msg(status));
1476 talloc_free(tframe);
1480 talloc_free(tframe);
1485 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1488 struct pdb_methods *methods;
1490 GROUP_MAP *group_map;
1491 struct dom_sid *domain_sid;
1492 PyObject *py_domain_sid, *py_group_map;
1494 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1498 methods = pytalloc_get_ptr(self);
1500 if ((tframe = talloc_stackframe()) == NULL) {
1505 domain_sid = pytalloc_get_ptr(py_domain_sid);
1507 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1508 if (py_group_map == NULL) {
1510 talloc_free(tframe);
1514 group_map = pytalloc_get_ptr(py_group_map);
1516 status = methods->getgrsid(methods, group_map, *domain_sid);
1517 if (!NT_STATUS_IS_OK(status)) {
1518 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1519 NT_STATUS_V(status),
1520 get_friendly_nt_error_msg(status));
1521 talloc_free(tframe);
1525 talloc_free(tframe);
1526 return py_group_map;
1530 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1533 struct pdb_methods *methods;
1535 GROUP_MAP *group_map;
1536 PyObject *py_group_map;
1537 unsigned int gid_value;
1539 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1543 methods = pytalloc_get_ptr(self);
1545 if ((tframe = talloc_stackframe()) == NULL) {
1550 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1551 if (py_group_map == NULL) {
1553 talloc_free(tframe);
1557 group_map = pytalloc_get_ptr(py_group_map);
1559 status = methods->getgrgid(methods, group_map, gid_value);
1560 if (!NT_STATUS_IS_OK(status)) {
1561 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1562 NT_STATUS_V(status),
1563 get_friendly_nt_error_msg(status));
1564 talloc_free(tframe);
1568 talloc_free(tframe);
1569 return py_group_map;
1573 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1576 struct pdb_methods *methods;
1578 GROUP_MAP *group_map;
1579 PyObject *py_group_map;
1580 const char *groupname;
1582 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1586 methods = pytalloc_get_ptr(self);
1588 if ((tframe = talloc_stackframe()) == NULL) {
1593 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1594 if (py_group_map == NULL) {
1596 talloc_free(tframe);
1600 group_map = pytalloc_get_ptr(py_group_map);
1602 status = methods->getgrnam(methods, group_map, groupname);
1603 if (!NT_STATUS_IS_OK(status)) {
1604 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1605 NT_STATUS_V(status),
1606 get_friendly_nt_error_msg(status));
1607 talloc_free(tframe);
1611 talloc_free(tframe);
1612 return py_group_map;
1616 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1619 struct pdb_methods *methods;
1621 const char *groupname;
1624 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1628 methods = pytalloc_get_ptr(self);
1630 if ((tframe = talloc_stackframe()) == NULL) {
1635 status = methods->create_dom_group(methods, tframe, groupname, &group_rid);
1636 if (!NT_STATUS_IS_OK(status)) {
1637 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1639 NT_STATUS_V(status),
1640 get_friendly_nt_error_msg(status));
1641 talloc_free(tframe);
1645 talloc_free(tframe);
1646 return PyInt_FromLong(group_rid);
1650 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1653 struct pdb_methods *methods;
1655 unsigned int group_rid;
1657 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1661 methods = pytalloc_get_ptr(self);
1663 if ((tframe = talloc_stackframe()) == NULL) {
1668 status = methods->delete_dom_group(methods, tframe, group_rid);
1669 if (!NT_STATUS_IS_OK(status)) {
1670 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1672 NT_STATUS_V(status),
1673 get_friendly_nt_error_msg(status));
1674 talloc_free(tframe);
1678 talloc_free(tframe);
1683 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1686 struct pdb_methods *methods;
1688 PyObject *py_group_map;
1689 GROUP_MAP *group_map;
1691 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1695 methods = pytalloc_get_ptr(self);
1697 if ((tframe = talloc_stackframe()) == NULL) {
1702 group_map = pytalloc_get_ptr(py_group_map);
1704 status = methods->add_group_mapping_entry(methods, group_map);
1705 if (!NT_STATUS_IS_OK(status)) {
1706 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1707 NT_STATUS_V(status),
1708 get_friendly_nt_error_msg(status));
1709 talloc_free(tframe);
1713 talloc_free(tframe);
1718 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1721 struct pdb_methods *methods;
1723 PyObject *py_group_map;
1724 GROUP_MAP *group_map;
1726 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1730 methods = pytalloc_get_ptr(self);
1732 if ((tframe = talloc_stackframe()) == NULL) {
1737 group_map = pytalloc_get_ptr(py_group_map);
1739 status = methods->update_group_mapping_entry(methods, group_map);
1740 if (!NT_STATUS_IS_OK(status)) {
1741 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1742 NT_STATUS_V(status),
1743 get_friendly_nt_error_msg(status));
1744 talloc_free(tframe);
1748 talloc_free(tframe);
1753 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1756 struct pdb_methods *methods;
1758 PyObject *py_group_sid;
1759 struct dom_sid *group_sid;
1761 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1765 methods = pytalloc_get_ptr(self);
1767 if ((tframe = talloc_stackframe()) == NULL) {
1772 group_sid = pytalloc_get_ptr(py_group_sid);
1774 status = methods->delete_group_mapping_entry(methods, *group_sid);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1777 NT_STATUS_V(status),
1778 get_friendly_nt_error_msg(status));
1779 talloc_free(tframe);
1783 talloc_free(tframe);
1788 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1791 struct pdb_methods *methods;
1793 enum lsa_SidType sid_name_use;
1794 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1796 PyObject *py_domain_sid;
1797 struct dom_sid *domain_sid = NULL;
1798 GROUP_MAP *gmap, *group_map;
1800 PyObject *py_gmap_list, *py_group_map;
1803 py_domain_sid = Py_None;
1806 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1807 &lsa_sidtype_value, &unix_only)) {
1811 methods = pytalloc_get_ptr(self);
1813 if ((tframe = talloc_stackframe()) == NULL) {
1818 sid_name_use = lsa_sidtype_value;
1820 if (py_domain_sid != Py_None) {
1821 domain_sid = pytalloc_get_ptr(py_domain_sid);
1824 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1825 &gmap, &num_entries, unix_only);
1826 if (!NT_STATUS_IS_OK(status)) {
1827 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1828 NT_STATUS_V(status),
1829 get_friendly_nt_error_msg(status));
1830 talloc_free(tframe);
1834 py_gmap_list = PyList_New(0);
1835 if (py_gmap_list == NULL) {
1837 talloc_free(tframe);
1841 for(i=0; i<num_entries; i++) {
1842 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1844 group_map = pytalloc_get_ptr(py_group_map);
1845 *group_map = gmap[i];
1847 PyList_Append(py_gmap_list, py_group_map);
1852 talloc_free(tframe);
1854 return py_gmap_list;
1858 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1861 struct pdb_methods *methods;
1863 PyObject *py_group_sid;
1864 struct dom_sid *group_sid;
1865 uint32_t *member_rids;
1867 PyObject *py_sid_list;
1868 struct dom_sid *domain_sid, *member_sid;
1871 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1875 methods = pytalloc_get_ptr(self);
1877 if ((tframe = talloc_stackframe()) == NULL) {
1882 group_sid = pytalloc_get_ptr(py_group_sid);
1884 status = methods->enum_group_members(methods, tframe, group_sid,
1885 &member_rids, &num_members);
1886 if (!NT_STATUS_IS_OK(status)) {
1887 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1888 NT_STATUS_V(status),
1889 get_friendly_nt_error_msg(status));
1890 talloc_free(tframe);
1894 py_sid_list = PyList_New(0);
1895 if (py_sid_list == NULL) {
1897 talloc_free(tframe);
1901 domain_sid = get_global_sam_sid();
1903 for(i=0; i<num_members; i++) {
1904 member_sid = dom_sid_add_rid(tframe, domain_sid, member_rids[i]);
1905 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
1908 talloc_free(tframe);
1914 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
1917 struct pdb_methods *methods;
1919 uint32_t group_rid, member_rid;
1921 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
1925 methods = pytalloc_get_ptr(self);
1927 if ((tframe = talloc_stackframe()) == NULL) {
1932 status = methods->add_groupmem(methods, tframe, group_rid, member_rid);
1933 if (!NT_STATUS_IS_OK(status)) {
1934 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
1935 NT_STATUS_V(status),
1936 get_friendly_nt_error_msg(status));
1937 talloc_free(tframe);
1941 talloc_free(tframe);
1946 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
1949 struct pdb_methods *methods;
1951 uint32_t group_rid, member_rid;
1953 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
1957 methods = pytalloc_get_ptr(self);
1959 if ((tframe = talloc_stackframe()) == NULL) {
1964 status = methods->del_groupmem(methods, tframe, group_rid, member_rid);
1965 if (!NT_STATUS_IS_OK(status)) {
1966 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1967 NT_STATUS_V(status),
1968 get_friendly_nt_error_msg(status));
1969 talloc_free(tframe);
1973 talloc_free(tframe);
1978 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
1981 struct pdb_methods *methods;
1983 const char *alias_name;
1986 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
1990 methods = pytalloc_get_ptr(self);
1992 if ((tframe = talloc_stackframe()) == NULL) {
1997 status = methods->create_alias(methods, alias_name, &rid);
1998 if (!NT_STATUS_IS_OK(status)) {
1999 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2001 NT_STATUS_V(status),
2002 get_friendly_nt_error_msg(status));
2003 talloc_free(tframe);
2007 talloc_free(tframe);
2009 return PyInt_FromLong(rid);
2013 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2016 struct pdb_methods *methods;
2018 PyObject *py_alias_sid;
2019 struct dom_sid *alias_sid;
2021 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2025 methods = pytalloc_get_ptr(self);
2027 if ((tframe = talloc_stackframe()) == NULL) {
2032 alias_sid = pytalloc_get_ptr(py_alias_sid);
2034 status = methods->delete_alias(methods, alias_sid);
2035 if (!NT_STATUS_IS_OK(status)) {
2036 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2037 NT_STATUS_V(status),
2038 get_friendly_nt_error_msg(status));
2039 talloc_free(tframe);
2043 talloc_free(tframe);
2048 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2051 struct pdb_methods *methods;
2053 PyObject *py_alias_sid;
2054 struct dom_sid *alias_sid;
2055 struct acct_info alias_info;
2056 PyObject *py_alias_info;
2058 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2062 methods = pytalloc_get_ptr(self);
2064 if ((tframe = talloc_stackframe()) == NULL) {
2069 alias_sid = pytalloc_get_ptr(py_alias_sid);
2071 status = methods->get_aliasinfo(methods, alias_sid, &alias_info);
2072 if (!NT_STATUS_IS_OK(status)) {
2073 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2074 NT_STATUS_V(status),
2075 get_friendly_nt_error_msg(status));
2076 talloc_free(tframe);
2080 py_alias_info = PyDict_New();
2081 if (py_alias_info == NULL) {
2083 talloc_free(tframe);
2087 PyDict_SetItemString(py_alias_info, "acct_name", PyString_FromString(alias_info.acct_name));
2088 PyDict_SetItemString(py_alias_info, "acct_desc", PyString_FromString(alias_info.acct_desc));
2089 PyDict_SetItemString(py_alias_info, "rid", PyInt_FromLong(alias_info.rid));
2091 talloc_free(tframe);
2093 return py_alias_info;
2097 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2100 struct pdb_methods *methods;
2102 PyObject *py_alias_sid, *py_alias_info;
2103 struct dom_sid *alias_sid;
2104 struct acct_info alias_info;
2106 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2111 methods = pytalloc_get_ptr(self);
2113 if ((tframe = talloc_stackframe()) == NULL) {
2118 alias_sid = pytalloc_get_ptr(py_alias_sid);
2120 fstrcpy(alias_info.acct_name, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2121 fstrcpy(alias_info.acct_desc, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2123 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2124 if (!NT_STATUS_IS_OK(status)) {
2125 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2126 NT_STATUS_V(status),
2127 get_friendly_nt_error_msg(status));
2128 talloc_free(tframe);
2132 talloc_free(tframe);
2137 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2140 struct pdb_methods *methods;
2142 PyObject *py_alias_sid, *py_member_sid;
2143 struct dom_sid *alias_sid, *member_sid;
2145 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2146 dom_sid_Type, &py_member_sid)) {
2150 methods = pytalloc_get_ptr(self);
2152 if ((tframe = talloc_stackframe()) == NULL) {
2157 alias_sid = pytalloc_get_ptr(py_alias_sid);
2158 member_sid = pytalloc_get_ptr(py_member_sid);
2160 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2161 if (!NT_STATUS_IS_OK(status)) {
2162 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2163 NT_STATUS_V(status),
2164 get_friendly_nt_error_msg(status));
2165 talloc_free(tframe);
2169 talloc_free(tframe);
2174 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2177 struct pdb_methods *methods;
2179 PyObject *py_alias_sid, *py_member_sid;
2180 const struct dom_sid *alias_sid, *member_sid;
2182 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2183 dom_sid_Type, &py_member_sid)) {
2187 methods = pytalloc_get_ptr(self);
2189 if ((tframe = talloc_stackframe()) == NULL) {
2194 alias_sid = pytalloc_get_ptr(py_alias_sid);
2195 member_sid = pytalloc_get_ptr(py_member_sid);
2197 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2198 if (!NT_STATUS_IS_OK(status)) {
2199 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2200 NT_STATUS_V(status),
2201 get_friendly_nt_error_msg(status));
2202 talloc_free(tframe);
2206 talloc_free(tframe);
2211 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2214 struct pdb_methods *methods;
2216 PyObject *py_alias_sid;
2217 struct dom_sid *alias_sid, *member_sid;
2218 PyObject *py_member_list, *py_member_sid;
2222 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2226 methods = pytalloc_get_ptr(self);
2228 if ((tframe = talloc_stackframe()) == NULL) {
2233 alias_sid = pytalloc_get_ptr(py_alias_sid);
2235 status = methods->enum_aliasmem(methods, alias_sid, tframe, &member_sid, &num_members);
2236 if (!NT_STATUS_IS_OK(status)) {
2237 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2238 NT_STATUS_V(status),
2239 get_friendly_nt_error_msg(status));
2240 talloc_free(tframe);
2244 py_member_list = PyList_New(0);
2245 if (py_member_list == NULL) {
2247 talloc_free(tframe);
2251 for(i=0; i<num_members; i++) {
2252 py_member_sid = pytalloc_steal(dom_sid_Type, &member_sid[i]);
2253 if (py_member_sid) {
2254 PyList_Append(py_member_list, py_member_sid);
2258 talloc_free(tframe);
2260 return py_member_list;
2264 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2267 struct pdb_methods *methods;
2269 PyObject *py_acct_policy;
2273 enum pdb_policy_type type;
2275 methods = pytalloc_get_ptr(self);
2277 if ((tframe = talloc_stackframe()) == NULL) {
2282 py_acct_policy = PyDict_New();
2283 if (py_acct_policy == NULL) {
2288 account_policy_names_list(tframe, &names, &count);
2289 for (i=0; i<count; i++) {
2290 type = account_policy_name_to_typenum(names[i]);
2291 status = methods->get_account_policy(methods, type, &value);
2292 if (NT_STATUS_IS_OK(status)) {
2293 PyDict_SetItemString(py_acct_policy, names[i], PyInt_FromLong(value));
2297 talloc_free(tframe);
2299 return py_acct_policy;
2303 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2306 struct pdb_methods *methods;
2308 PyObject *py_acct_policy, *py_value;
2311 enum pdb_policy_type type;
2313 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2317 methods = pytalloc_get_ptr(self);
2319 if ((tframe = talloc_stackframe()) == NULL) {
2324 account_policy_names_list(tframe, &names, &count);
2325 for (i=0; i<count; i++) {
2326 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2327 type = account_policy_name_to_typenum(names[i]);
2328 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2329 if (!NT_STATUS_IS_OK(status)) {
2330 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2332 NT_STATUS_V(status),
2333 get_friendly_nt_error_msg(status));
2339 talloc_free(tframe);
2344 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2347 struct pdb_methods *methods;
2349 unsigned int acct_flags;
2350 struct pdb_search *search;
2351 struct samr_displayentry *entry;
2352 PyObject *py_userlist, *py_dict;
2354 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2358 methods = pytalloc_get_ptr(self);
2360 if ((tframe = talloc_stackframe()) == NULL) {
2365 search = talloc_zero(tframe, struct pdb_search);
2366 if (search == NULL) {
2368 talloc_free(tframe);
2372 if (!methods->search_users(methods, search, acct_flags)) {
2373 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
2374 NT_STATUS_V(status),
2375 get_friendly_nt_error_msg(status));
2376 talloc_free(tframe);
2380 entry = talloc_zero(tframe, struct samr_displayentry);
2381 if (entry == NULL) {
2383 talloc_free(tframe);
2387 py_userlist = PyList_New(0);
2388 if (py_userlist == NULL) {
2390 talloc_free(tframe);
2394 while (search->next_entry(search, entry)) {
2395 py_dict = PyDict_New();
2396 if (py_dict == NULL) {
2399 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2400 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2401 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2402 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2403 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2404 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2405 PyList_Append(py_userlist, py_dict);
2408 search->search_end(search);
2410 talloc_free(tframe);
2416 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2419 struct pdb_methods *methods;
2421 struct pdb_search *search;
2422 struct samr_displayentry *entry;
2423 PyObject *py_grouplist, *py_dict;
2425 methods = pytalloc_get_ptr(self);
2427 if ((tframe = talloc_stackframe()) == NULL) {
2432 search = talloc_zero(tframe, struct pdb_search);
2433 if (search == NULL) {
2435 talloc_free(tframe);
2439 if (!methods->search_groups(methods, search)) {
2440 PyErr_Format(py_pdb_error, "Unable to search groups, (%d,%s)",
2441 NT_STATUS_V(status),
2442 get_friendly_nt_error_msg(status));
2443 talloc_free(tframe);
2447 entry = talloc_zero(tframe, struct samr_displayentry);
2448 if (entry == NULL) {
2450 talloc_free(tframe);
2454 py_grouplist = PyList_New(0);
2455 if (py_grouplist == NULL) {
2457 talloc_free(tframe);
2461 while (search->next_entry(search, entry)) {
2462 py_dict = PyDict_New();
2463 if (py_dict == NULL) {
2466 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2467 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2468 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2469 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2470 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2471 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2472 PyList_Append(py_grouplist, py_dict);
2475 search->search_end(search);
2477 talloc_free(tframe);
2479 return py_grouplist;
2483 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2486 struct pdb_methods *methods;
2488 struct pdb_search *search;
2489 struct samr_displayentry *entry;
2490 PyObject *py_aliaslist, *py_dict;
2491 PyObject *py_domain_sid;
2492 struct dom_sid *dom_sid;
2494 if (!PyArg_ParseTuple(args, "O!:search_users", dom_sid_Type, &py_domain_sid)) {
2498 methods = pytalloc_get_ptr(self);
2500 if ((tframe = talloc_stackframe()) == NULL) {
2505 dom_sid = pytalloc_get_ptr(py_domain_sid);
2507 search = talloc_zero(tframe, struct pdb_search);
2508 if (search == NULL) {
2510 talloc_free(tframe);
2514 if (!methods->search_aliases(methods, search, dom_sid)) {
2515 PyErr_Format(py_pdb_error, "Unable to search aliases, (%d,%s)",
2516 NT_STATUS_V(status),
2517 get_friendly_nt_error_msg(status));
2518 talloc_free(tframe);
2522 entry = talloc_zero(tframe, struct samr_displayentry);
2523 if (entry == NULL) {
2525 talloc_free(tframe);
2529 py_aliaslist = PyList_New(0);
2530 if (py_aliaslist == NULL) {
2532 talloc_free(tframe);
2536 while (search->next_entry(search, entry)) {
2537 py_dict = PyDict_New();
2538 if (py_dict == NULL) {
2541 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2542 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2543 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2544 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2545 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2546 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2547 PyList_Append(py_aliaslist, py_dict);
2550 search->search_end(search);
2552 talloc_free(tframe);
2554 return py_aliaslist;
2558 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2560 struct pdb_methods *methods;
2563 struct dom_sid user_sid, *copy_user_sid;
2564 PyObject *py_user_sid;
2566 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2570 methods = pytalloc_get_ptr(self);
2572 if ((tframe = talloc_stackframe()) == NULL) {
2577 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2578 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2579 talloc_free(tframe);
2583 copy_user_sid = dom_sid_dup(tframe, &user_sid);
2584 if (copy_user_sid == NULL) {
2586 talloc_free(tframe);
2590 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2592 talloc_free(tframe);
2598 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2600 struct pdb_methods *methods;
2603 struct dom_sid group_sid, *copy_group_sid;
2604 PyObject *py_group_sid;
2606 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2610 methods = pytalloc_get_ptr(self);
2612 if ((tframe = talloc_stackframe()) == NULL) {
2617 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2618 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2619 talloc_free(tframe);
2623 copy_group_sid = dom_sid_dup(tframe, &group_sid);
2624 if (copy_group_sid == NULL) {
2626 talloc_free(tframe);
2630 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2632 talloc_free(tframe);
2634 return py_group_sid;
2638 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2640 struct pdb_methods *methods;
2643 struct dom_sid *sid;
2645 enum lsa_SidType type;
2647 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2651 methods = pytalloc_get_ptr(self);
2653 if ((tframe = talloc_stackframe()) == NULL) {
2658 sid = pytalloc_get_ptr(py_sid);
2660 if (!methods->sid_to_id(methods, sid, &id, &type)) {
2661 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2662 talloc_free(tframe);
2666 talloc_free(tframe);
2668 return Py_BuildValue("(II)", id.uid, type);
2672 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2674 struct pdb_methods *methods;
2678 methods = pytalloc_get_ptr(self);
2680 if ((tframe = talloc_stackframe()) == NULL) {
2685 if (!methods->new_rid(methods, &rid)) {
2686 PyErr_Format(py_pdb_error, "Unable to get new rid");
2687 talloc_free(tframe);
2691 talloc_free(tframe);
2693 return PyInt_FromLong(rid);
2697 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2699 struct pdb_methods *methods;
2703 struct dom_sid sid, *copy_sid;
2705 time_t last_set_time;
2708 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2712 methods = pytalloc_get_ptr(self);
2714 if ((tframe = talloc_stackframe()) == NULL) {
2719 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2720 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2721 talloc_free(tframe);
2725 copy_sid = dom_sid_dup(tframe, &sid);
2726 if (copy_sid == NULL) {
2728 talloc_free(tframe);
2732 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2733 if (py_sid == NULL) {
2735 talloc_free(tframe);
2739 talloc_free(tframe);
2741 py_value = PyDict_New();
2742 if (py_value == NULL) {
2747 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2748 PyDict_SetItemString(py_value, "sid", py_sid);
2749 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2755 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2757 struct pdb_methods *methods;
2761 const struct dom_sid *domain_sid;
2762 PyObject *py_domain_sid;
2764 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2765 dom_sid_Type, &py_domain_sid)) {
2769 methods = pytalloc_get_ptr(self);
2771 if ((tframe = talloc_stackframe()) == NULL) {
2776 domain_sid = pytalloc_get_ptr(py_domain_sid);
2778 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2779 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2780 talloc_free(tframe);
2788 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2790 struct pdb_methods *methods;
2794 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2798 methods = pytalloc_get_ptr(self);
2800 if ((tframe = talloc_stackframe()) == NULL) {
2805 if (!methods->del_trusteddom_pw(methods, domain)) {
2806 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2807 talloc_free(tframe);
2815 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2818 struct pdb_methods *methods;
2820 uint32_t num_domains;
2821 struct trustdom_info **domains;
2822 PyObject *py_domain_list, *py_dict;
2825 methods = pytalloc_get_ptr(self);
2827 if ((tframe = talloc_stackframe()) == NULL) {
2832 status = methods->enum_trusteddoms(methods, tframe, &num_domains, &domains);
2833 if (!NT_STATUS_IS_OK(status)) {
2834 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2835 NT_STATUS_V(status),
2836 get_friendly_nt_error_msg(status));
2837 talloc_free(tframe);
2841 py_domain_list = PyList_New(0);
2842 if (py_domain_list == NULL) {
2844 talloc_free(tframe);
2848 for(i=0; i<num_domains; i++) {
2849 py_dict = PyDict_New();
2851 PyDict_SetItemString(py_dict, "name",
2852 PyString_FromString(domains[i]->name));
2853 PyDict_SetItemString(py_dict, "sid",
2854 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2857 PyList_Append(py_domain_list, py_dict);
2860 talloc_free(tframe);
2862 return py_domain_list;
2866 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2869 struct pdb_methods *methods;
2872 struct pdb_trusted_domain *td;
2873 PyObject *py_domain_info;
2875 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2879 methods = pytalloc_get_ptr(self);
2881 if ((tframe = talloc_stackframe()) == NULL) {
2886 status = methods->get_trusted_domain(methods, tframe, domain, &td);
2887 if (!NT_STATUS_IS_OK(status)) {
2888 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2889 NT_STATUS_V(status),
2890 get_friendly_nt_error_msg(status));
2891 talloc_free(tframe);
2895 py_domain_info = PyDict_New();
2896 if (py_domain_info == NULL) {
2898 talloc_free(tframe);
2902 PyDict_SetItemString(py_domain_info, "domain_name",
2903 PyString_FromString(td->domain_name));
2904 PyDict_SetItemString(py_domain_info, "netbios_name",
2905 PyString_FromString(td->netbios_name));
2906 PyDict_SetItemString(py_domain_info, "security_identifier",
2907 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2908 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2909 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2910 td->trust_auth_incoming.length));
2911 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2912 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2913 td->trust_auth_outgoing.length));
2914 PyDict_SetItemString(py_domain_info, "trust_direction",
2915 PyInt_FromLong(td->trust_direction));
2916 PyDict_SetItemString(py_domain_info, "trust_type",
2917 PyInt_FromLong(td->trust_type));
2918 PyDict_SetItemString(py_domain_info, "trust_attributes",
2919 PyInt_FromLong(td->trust_attributes));
2920 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2921 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2922 td->trust_forest_trust_info.length));
2924 talloc_free(tframe);
2926 return py_domain_info;
2930 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
2933 struct pdb_methods *methods;
2935 PyObject *py_domain_sid;
2936 struct dom_sid *domain_sid;
2937 struct pdb_trusted_domain *td;
2938 PyObject *py_domain_info;
2940 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
2944 methods = pytalloc_get_ptr(self);
2946 if ((tframe = talloc_stackframe()) == NULL) {
2951 domain_sid = pytalloc_get_ptr(py_domain_sid);
2953 status = methods->get_trusted_domain_by_sid(methods, tframe, domain_sid, &td);
2954 if (!NT_STATUS_IS_OK(status)) {
2955 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2956 NT_STATUS_V(status),
2957 get_friendly_nt_error_msg(status));
2958 talloc_free(tframe);
2962 py_domain_info = PyDict_New();
2963 if (py_domain_info == NULL) {
2965 talloc_free(tframe);
2969 PyDict_SetItemString(py_domain_info, "domain_name",
2970 PyString_FromString(td->domain_name));
2971 PyDict_SetItemString(py_domain_info, "netbios_name",
2972 PyString_FromString(td->netbios_name));
2973 PyDict_SetItemString(py_domain_info, "security_identifier",
2974 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2975 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2976 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2977 td->trust_auth_incoming.length));
2978 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2979 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2980 td->trust_auth_outgoing.length));
2981 PyDict_SetItemString(py_domain_info, "trust_direction",
2982 PyInt_FromLong(td->trust_direction));
2983 PyDict_SetItemString(py_domain_info, "trust_type",
2984 PyInt_FromLong(td->trust_type));
2985 PyDict_SetItemString(py_domain_info, "trust_attributes",
2986 PyInt_FromLong(td->trust_attributes));
2987 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2988 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2989 td->trust_forest_trust_info.length));
2991 talloc_free(tframe);
2993 return py_domain_info;
2997 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3000 struct pdb_methods *methods;
3003 PyObject *py_td_info;
3004 struct pdb_trusted_domain td_info;
3008 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3012 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3013 td_info.domain_name = PyString_AsString(py_tmp);
3015 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3016 td_info.netbios_name = PyString_AsString(py_tmp);
3018 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3019 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3021 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3022 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3023 td_info.trust_auth_incoming.length = len;
3025 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3026 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3027 td_info.trust_auth_outgoing.length = len;
3029 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3030 td_info.trust_direction = PyInt_AsLong(py_tmp);
3032 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3033 td_info.trust_type = PyInt_AsLong(py_tmp);
3035 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3036 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3038 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3039 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3040 td_info.trust_forest_trust_info.length = len;
3042 methods = pytalloc_get_ptr(self);
3044 if ((tframe = talloc_stackframe()) == NULL) {
3049 status = methods->set_trusted_domain(methods, domain, &td_info);
3050 if (!NT_STATUS_IS_OK(status)) {
3051 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3052 NT_STATUS_V(status),
3053 get_friendly_nt_error_msg(status));
3054 talloc_free(tframe);
3058 talloc_free(tframe);
3064 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3067 struct pdb_methods *methods;
3071 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3075 methods = pytalloc_get_ptr(self);
3077 if ((tframe = talloc_stackframe()) == NULL) {
3082 status = methods->del_trusted_domain(methods, domain);
3083 if (!NT_STATUS_IS_OK(status)) {
3084 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3085 NT_STATUS_V(status),
3086 get_friendly_nt_error_msg(status));
3087 talloc_free(tframe);
3091 talloc_free(tframe);
3097 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3100 struct pdb_methods *methods;
3102 uint32_t num_domains;
3103 struct pdb_trusted_domain **td_info, *td;
3104 PyObject *py_td_info, *py_domain_info;
3107 methods = pytalloc_get_ptr(self);
3109 if ((tframe = talloc_stackframe()) == NULL) {
3114 status = methods->enum_trusted_domains(methods, tframe, &num_domains, &td_info);
3115 if (!NT_STATUS_IS_OK(status)) {
3116 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3117 NT_STATUS_V(status),
3118 get_friendly_nt_error_msg(status));
3119 talloc_free(tframe);
3123 py_td_info = PyList_New(0);
3124 if (py_td_info == NULL) {
3126 talloc_free(tframe);
3130 for (i=0; i<num_domains; i++) {
3132 py_domain_info = PyDict_New();
3133 if (py_domain_info == NULL) {
3135 Py_DECREF(py_td_info);
3136 talloc_free(tframe);
3142 PyDict_SetItemString(py_domain_info, "domain_name",
3143 PyString_FromString(td->domain_name));
3144 PyDict_SetItemString(py_domain_info, "netbios_name",
3145 PyString_FromString(td->netbios_name));
3146 PyDict_SetItemString(py_domain_info, "security_identifier",
3147 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3148 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3149 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3150 td->trust_auth_incoming.length));
3151 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3152 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3153 td->trust_auth_outgoing.length));
3154 PyDict_SetItemString(py_domain_info, "trust_direction",
3155 PyInt_FromLong(td->trust_direction));
3156 PyDict_SetItemString(py_domain_info, "trust_type",
3157 PyInt_FromLong(td->trust_type));
3158 PyDict_SetItemString(py_domain_info, "trust_attributes",
3159 PyInt_FromLong(td->trust_attributes));
3160 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3161 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3162 td->trust_forest_trust_info.length));
3163 PyList_Append(py_td_info, py_domain_info);
3166 talloc_free(tframe);
3172 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3175 struct pdb_methods *methods;
3177 const char *secret_name;
3178 DATA_BLOB secret_current, secret_old;
3179 NTTIME secret_current_lastchange, secret_old_lastchange;
3181 struct security_descriptor *sd;
3182 PyObject *py_secret;
3184 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3188 methods = pytalloc_get_ptr(self);
3190 if ((tframe = talloc_stackframe()) == NULL) {
3195 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3196 if (py_sd == NULL) {
3198 talloc_free(tframe);
3201 sd = pytalloc_get_ptr(py_sd);
3203 status = methods->get_secret(methods, tframe, secret_name,
3205 &secret_current_lastchange,
3207 &secret_old_lastchange,
3209 if (!NT_STATUS_IS_OK(status)) {
3210 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3212 NT_STATUS_V(status),
3213 get_friendly_nt_error_msg(status));
3214 talloc_free(tframe);
3218 py_secret = PyDict_New();
3219 if (py_secret == NULL) {
3222 talloc_free(tframe);
3226 PyDict_SetItemString(py_secret, "secret_current",
3227 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3228 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3229 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3230 PyDict_SetItemString(py_secret, "secret_old",
3231 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3232 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3233 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3234 PyDict_SetItemString(py_secret, "sd", py_sd);
3236 talloc_free(tframe);
3242 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3245 struct pdb_methods *methods;
3247 const char *secret_name;
3248 PyObject *py_secret;
3249 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3250 DATA_BLOB secret_current, secret_old;
3251 struct security_descriptor *sd;
3254 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3258 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3259 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3260 py_sd = PyDict_GetItemString(py_secret, "sd");
3262 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3263 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3264 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3266 methods = pytalloc_get_ptr(self);
3268 if ((tframe = talloc_stackframe()) == NULL) {
3273 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3274 secret_current.length = len;
3275 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3276 secret_current.length = len;
3277 sd = pytalloc_get_ptr(py_sd);
3279 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3280 if (!NT_STATUS_IS_OK(status)) {
3281 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3283 NT_STATUS_V(status),
3284 get_friendly_nt_error_msg(status));
3285 talloc_free(tframe);
3289 talloc_free(tframe);
3295 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3298 struct pdb_methods *methods;
3300 const char *secret_name;
3302 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3306 methods = pytalloc_get_ptr(self);
3308 if ((tframe = talloc_stackframe()) == NULL) {
3313 status = methods->delete_secret(methods, secret_name);
3314 if (!NT_STATUS_IS_OK(status)) {
3315 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3317 NT_STATUS_V(status),
3318 get_friendly_nt_error_msg(status));
3319 talloc_free(tframe);
3323 talloc_free(tframe);
3328 static PyMethodDef py_pdb_methods[] = {
3329 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3330 "domain_info() -> str\n\n \
3331 Get domain information for the database." },
3332 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3333 "getsampwnam(username) -> samu object\n\n \
3334 Get user information by name." },
3335 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3336 "getsampwsid(user_sid) -> samu object\n\n \
3337 Get user information by sid (dcerpc.security.dom_sid object)." },
3338 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3339 "create_user(username, acct_flags) -> rid\n\n \
3340 Create user. acct_flags are samr account control flags." },
3341 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3342 "delete_user(samu object) -> None\n\n \
3344 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3345 "add_sam_account(samu object) -> None\n\n \
3346 Add SAM account." },
3347 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3348 "update_sam_account(samu object) -> None\n\n \
3349 Update SAM account." },
3350 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3351 "delete_sam_account(samu object) -> None\n\n \
3352 Delete SAM account." },
3353 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3354 "rename_sam_account(samu object1, new_username) -> None\n\n \
3355 Rename SAM account." },
3356 /* update_login_attempts */
3357 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3358 "getgrsid(group_sid) -> groupmap object\n\n \
3359 Get group information by sid (dcerpc.security.dom_sid object)." },
3360 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3361 "getgrsid(gid) -> groupmap object\n\n \
3362 Get group information by gid." },
3363 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3364 "getgrsid(groupname) -> groupmap object\n\n \
3365 Get group information by name." },
3366 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3367 "create_dom_group(groupname) -> group_rid\n\n \
3368 Create new domain group by name." },
3369 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3370 "delete_dom_group(group_rid) -> None\n\n \
3371 Delete domain group identified by rid" },
3372 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3373 "add_group_mapping_entry(groupmap) -> None\n \
3374 Add group mapping entry for groupmap object." },
3375 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3376 "update_group_mapping_entry(groupmap) -> None\n\n \
3377 Update group mapping entry for groupmap object." },
3378 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3379 "delete_group_mapping_entry(groupmap) -> None\n\n \
3380 Delete group mapping entry for groupmap object." },
3381 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3382 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3383 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3384 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3385 "enum_group_members(group_sid) -> List\n\n \
3386 Return list of users (dom_sid object) in group." },
3387 /* enum_group_memberships */
3388 /* set_unix_primary_group */
3389 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3390 "add_groupmem(group_rid, member_rid) -> None\n\n \
3391 Add user to group." },
3392 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3393 "del_groupmem(group_rid, member_rid) -> None\n\n \
3394 Remove user from from group." },
3395 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3396 "create_alias(alias_name) -> alias_rid\n\n \
3397 Create alias entry." },
3398 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3399 "delete_alias(alias_sid) -> None\n\n \
3400 Delete alias entry." },
3401 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3402 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3403 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3404 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3405 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3406 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3407 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3408 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3409 Add user to alias entry." },
3410 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3411 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3412 Remove a user from alias entry." },
3413 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3414 "enum_aliasmem(alias_sid) -> List\n\n \
3415 Return a list of members (dom_sid object) for alias entry." },
3416 /* enum_alias_memberships */
3419 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3420 "get_account_policy() -> Mapping\n\n \
3421 Get account policy information as a dictionary." },
3422 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3423 "get_account_policy(Mapping) -> None\n\n \
3424 Set account policy settings from a dicionary." },
3426 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3427 "search_users(acct_flags) -> List\n\n \
3428 Search users. acct_flags are samr account control flags.\n \
3429 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3430 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3431 "search_groups() -> List\n\n \
3432 Search unix only groups. \n \
3433 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3434 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3435 "search_aliases(domain_sid) -> List\n\n \
3436 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3437 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3438 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3439 "uid_to_sid(uid) -> sid\n\n \
3440 Return sid for given user id." },
3441 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3442 "gid_to_sid(gid) -> sid\n\n \
3443 Return sid for given group id." },
3444 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3445 "sid_to_id(sid) -> Tuple\n\n \
3446 Return id and type for given sid." },
3448 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3449 "new_rid() -> rid\n\n \
3451 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3452 "get_trusteddom_pw(domain) -> Mapping\n\n \
3453 Get trusted domain password, sid and last set time in a dictionary." },
3454 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3455 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3456 Set trusted domain password." },
3457 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3458 "del_trusteddom_pw(domain) -> None\n\n \
3459 Delete trusted domain password." },
3460 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3461 "enum_trusteddoms() -> List\n\n \
3462 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3463 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3464 "get_trusted_domain(domain) -> Mapping\n\n \
3465 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." },
3466 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3467 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3468 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" },
3469 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3470 "set_trusted_domain(domain, Mapping) -> None\n\n \
3471 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." },
3472 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3473 "del_trusted_domain(domain) -> None\n\n \
3474 Delete trusted domain." },
3475 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3476 "enum_trusted_domains() -> List\n\n \
3477 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." },
3478 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3479 "get_secret(secret_name) -> Mapping\n\n \
3480 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3481 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3482 "set_secret(secret_name, Mapping) -> None\n\n \
3483 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3484 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3485 "delete_secret(secret_name) -> None\n\n \
3486 Delete secret information for secret_name." },
3491 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3493 const char *url = NULL;
3496 struct pdb_methods *methods;
3498 if (!PyArg_ParseTuple(args, "s", &url)) {
3502 /* Initalize list of methods */
3503 status = make_pdb_method_name(&methods, url);
3504 if (!NT_STATUS_IS_OK(status)) {
3505 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3507 NT_STATUS_V(status),
3508 get_friendly_nt_error_msg(status));
3512 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3521 static PyTypeObject PyPDB = {
3522 .tp_name = "passdb.PDB",
3523 .tp_basicsize = sizeof(pytalloc_Object),
3524 .tp_new = py_pdb_new,
3525 .tp_flags = Py_TPFLAGS_DEFAULT,
3526 .tp_methods = py_pdb_methods,
3527 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3532 * Return a list of passdb backends
3534 static PyObject *py_passdb_backends(PyObject *self)
3537 const struct pdb_init_function_entry *entry;
3540 if ((tframe = talloc_stackframe()) == NULL) {
3545 entry = pdb_get_backends();
3550 if((py_blist = PyList_New(0)) == NULL) {
3556 PyList_Append(py_blist, PyString_FromString(entry->name));
3557 entry = entry->next;
3560 talloc_free(tframe);
3566 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3568 const char *smb_config;
3571 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3575 if ((tframe = talloc_stackframe()) == NULL) {
3580 /* Load smbconf parameters */
3581 if (!lp_load_global(smb_config)) {
3582 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3586 talloc_free(tframe);
3592 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3594 const char *private_dir;
3597 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3601 if ((tframe = talloc_stackframe()) == NULL) {
3606 /* Initialize secrets database */
3607 if (!secrets_init_path(private_dir)) {
3608 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3613 talloc_free(tframe);
3618 static PyObject *py_get_global_sam_sid(PyObject *self)
3620 struct dom_sid *domain_sid, *domain_sid_copy;
3622 PyObject *py_dom_sid;
3624 tframe = talloc_stackframe();
3625 if (tframe == NULL) {
3630 domain_sid = get_global_sam_sid();
3632 domain_sid_copy = dom_sid_dup(tframe, domain_sid);
3633 if (domain_sid_copy == NULL) {
3635 talloc_free(tframe);
3639 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3641 talloc_free(tframe);
3647 static PyMethodDef py_passdb_methods[] = {
3648 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3649 "get_backends() -> list\n\n \
3650 Get a list of password database backends supported." },
3651 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3652 "set_smb_config(path) -> None\n\n \
3653 Set path to smb.conf file to load configuration parameters." },
3654 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3655 "set_secrets_dir(private_dir) -> None\n\n \
3656 Set path to private directory to load secrets database from non-default location." },
3657 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3658 "get_global_sam_sid() -> dom_sid\n\n \
3659 Return domain SID." },
3663 void initpassdb(void)
3666 char exception_name[] = "passdb.error";
3668 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3669 if (talloc_type == NULL) {
3673 PyPDB.tp_base = talloc_type;
3674 if (PyType_Ready(&PyPDB) < 0) {
3678 PySamu.tp_base = talloc_type;
3679 if (PyType_Ready(&PySamu) < 0) {
3683 PyGroupmap.tp_base = talloc_type;
3684 if (PyType_Ready(&PyGroupmap) < 0) {
3688 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3693 /* Create new exception for passdb module */
3694 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3695 Py_INCREF(py_pdb_error);
3696 PyModule_AddObject(m, "error", py_pdb_error);
3699 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3702 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3704 Py_INCREF(&PyGroupmap);
3705 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3707 /* Import dom_sid type from dcerpc.security */
3708 mod = PyImport_ImportModule("samba.dcerpc.security");
3713 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3714 if (dom_sid_Type == NULL) {
3718 /* Import security_descriptor type from dcerpc.security */
3719 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3721 if (security_Type == NULL) {
3725 /* Import GUID type from dcerpc.misc */
3726 mod = PyImport_ImportModule("samba.dcerpc.misc");
3731 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3733 if (guid_Type == NULL) {