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
33 #define PY_CHECK_TYPE(type, var, fail) \
34 if (!PyObject_TypeCheck(var, type)) {\
35 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
41 static PyTypeObject *dom_sid_Type = NULL;
42 static PyTypeObject *guid_Type = NULL;
44 staticforward PyTypeObject PySamu;
45 staticforward PyTypeObject PyPDB;
47 static PyObject *py_pdb_error;
49 void initpassdb(void);
52 /************************** PIDL Autogeneratd ******************************/
54 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
56 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
57 PyObject *py_logon_time;
59 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
63 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
65 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
67 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
68 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
74 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
76 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
77 PyObject *py_logoff_time;
79 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
80 return py_logoff_time;
83 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
85 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
87 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
88 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
94 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
96 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
97 PyObject *py_kickoff_time;
99 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
100 return py_kickoff_time;
103 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
105 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
107 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
108 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
114 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
116 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
117 PyObject *py_bad_password_time;
119 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
120 return py_bad_password_time;
123 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
125 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
127 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
128 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
134 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
136 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
137 PyObject *py_pass_last_set_time;
139 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
140 return py_pass_last_set_time;
143 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
145 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
147 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
148 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
154 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
156 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
157 PyObject *py_pass_can_change_time;
159 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
160 return py_pass_can_change_time;
163 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
165 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
167 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
168 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
174 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
176 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
177 PyObject *py_pass_must_change_time;
179 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
180 return py_pass_must_change_time;
183 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
185 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
187 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
188 if (!pdb_set_pass_must_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
194 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
196 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
197 PyObject *py_username;
198 const char *username;
200 username = pdb_get_username(sam_acct);
201 if (username == NULL) {
205 py_username = PyString_FromString(username);
209 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
211 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
213 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
214 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
220 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
222 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
226 domain = pdb_get_domain(sam_acct);
227 if (domain == NULL) {
231 py_domain = PyString_FromString(domain);
235 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
237 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
239 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
240 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
246 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
248 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
249 PyObject *py_nt_username;
250 const char *nt_username;
252 nt_username = pdb_get_nt_username(sam_acct);
253 if (nt_username == NULL) {
257 py_nt_username = PyString_FromString(nt_username);
258 return py_nt_username;
261 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
263 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
265 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
266 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
272 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
274 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
275 PyObject *py_full_name;
276 const char *full_name;
278 full_name = pdb_get_fullname(sam_acct);
279 if (full_name == NULL) {
283 py_full_name = PyString_FromString(full_name);
287 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
289 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
291 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
292 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
298 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
300 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
301 PyObject *py_home_dir;
302 const char *home_dir;
304 home_dir = pdb_get_homedir(sam_acct);
305 if (home_dir == NULL) {
309 py_home_dir = PyString_FromString(home_dir);
313 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
315 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
317 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
318 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
324 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
326 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
327 PyObject *py_dir_drive;
328 const char *dir_drive;
330 dir_drive = pdb_get_dir_drive(sam_acct);
331 if (dir_drive == NULL) {
335 py_dir_drive = PyString_FromString(dir_drive);
339 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
341 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
343 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
344 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
350 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
352 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
353 PyObject *py_logon_script;
354 const char *logon_script;
356 logon_script = pdb_get_logon_script(sam_acct);
357 if (logon_script == NULL) {
361 py_logon_script = PyString_FromString(logon_script);
362 return py_logon_script;
365 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
367 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
369 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
370 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
376 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
378 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
379 PyObject *py_profile_path;
380 const char *profile_path;
382 profile_path = pdb_get_profile_path(sam_acct);
383 if (profile_path == NULL) {
387 py_profile_path = PyString_FromString(profile_path);
388 return py_profile_path;
391 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
393 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
395 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
396 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
402 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
404 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
405 PyObject *py_acct_desc;
406 const char *acct_desc;
408 acct_desc = pdb_get_acct_desc(sam_acct);
409 if (acct_desc == NULL) {
413 py_acct_desc = PyString_FromString(acct_desc);
417 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
419 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
421 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
422 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
428 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
430 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
431 PyObject *py_workstations;
432 const char *workstations;
434 workstations = pdb_get_workstations(sam_acct);
435 if (workstations == NULL) {
439 py_workstations = PyString_FromString(workstations);
440 return py_workstations;
443 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
445 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
447 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
448 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
454 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
456 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
457 PyObject *py_comment;
460 comment = pdb_get_comment(sam_acct);
461 if (comment == NULL) {
465 py_comment = PyString_FromString(comment);
469 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
471 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
473 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
474 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
480 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
482 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
483 PyObject *py_munged_dial;
484 const char *munged_dial;
486 munged_dial = pdb_get_munged_dial(sam_acct);
487 if (munged_dial == NULL) {
491 py_munged_dial = PyString_FromString(munged_dial);
492 return py_munged_dial;
495 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
497 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
499 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
500 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
506 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
508 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
509 PyObject *py_user_sid;
510 const struct dom_sid *user_sid;
511 struct dom_sid *copy_user_sid;
514 user_sid = pdb_get_user_sid(sam_acct);
515 if(user_sid == NULL) {
519 mem_ctx = talloc_new(NULL);
520 if (mem_ctx == NULL) {
524 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
525 if (copy_user_sid == NULL) {
527 talloc_free(mem_ctx);
531 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
533 talloc_free(mem_ctx);
538 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
540 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
542 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
543 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
549 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
551 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
552 PyObject *py_group_sid;
553 const struct dom_sid *group_sid;
554 struct dom_sid *copy_group_sid;
557 group_sid = pdb_get_group_sid(sam_acct);
558 if (group_sid == NULL) {
562 mem_ctx = talloc_new(NULL);
563 if (mem_ctx == NULL) {
567 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
568 if (copy_group_sid == NULL) {
570 talloc_free(mem_ctx);
574 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
576 talloc_free(mem_ctx);
581 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
583 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
585 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
586 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
592 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
594 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
598 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
603 py_lm_pw = PyString_FromString(lm_pw);
607 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
609 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
611 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
612 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
618 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
620 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
624 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
629 py_nt_pw = PyString_FromString(nt_pw);
633 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
635 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
637 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
643 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
645 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
646 PyObject *py_nt_pw_his;
647 const char *nt_pw_his;
650 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
651 if (nt_pw_his == NULL) {
655 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len);
659 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
661 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
666 PyString_AsStringAndSize(value, &nt_pw_his, &len);
668 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
674 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
676 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
677 PyObject *py_plaintext_pw;
678 const char *plaintext_pw;
680 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
681 if (plaintext_pw == NULL) {
685 py_plaintext_pw = PyString_FromString(plaintext_pw);
686 return py_plaintext_pw;
689 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
691 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
693 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
699 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
701 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
702 PyObject *py_acct_ctrl;
704 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
708 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
710 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
712 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
713 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
719 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
721 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
722 PyObject *py_logon_divs;
724 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
725 return py_logon_divs;
728 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
730 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
732 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
733 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
739 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
741 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
742 PyObject *py_hours_len;
744 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
748 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
750 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
752 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
753 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
759 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
761 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
766 hours = (const char *)pdb_get_hours(sam_acct);
771 if ((py_hours = PyList_New(MAX_HOURS_LEN)) == NULL) {
776 for (i=0; i<MAX_HOURS_LEN; i++) {
777 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
782 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
784 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
790 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
792 hours_len = PyList_GET_SIZE(value);
794 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
800 for (i=0; i < hours_len; i++) {
801 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
802 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
805 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
814 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
816 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
817 PyObject *py_bad_password_count;
819 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
820 return py_bad_password_count;
823 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
825 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
827 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
828 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
834 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
836 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
837 PyObject *py_logon_count;
839 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
840 return py_logon_count;
843 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
845 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
847 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
848 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
854 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
856 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
857 PyObject *py_country_code;
859 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
860 return py_country_code;
863 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
865 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
867 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
868 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
874 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
876 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
877 PyObject *py_code_page;
879 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
883 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
885 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
887 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
888 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
894 static PyGetSetDef py_samu_getsetters[] = {
895 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
896 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
897 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
898 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
899 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
900 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
901 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
902 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
903 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
904 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
905 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
906 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
907 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
908 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
909 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
910 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
911 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
912 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
913 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
914 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
915 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
916 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
917 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
918 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
919 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
920 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
921 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
922 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
923 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
924 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
925 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
926 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
927 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
932 /************************** PIDL Autogeneratd ******************************/
934 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
936 struct samu *sam_acct;
938 sam_acct = samu_new(NULL);
944 return pytalloc_steal(type, sam_acct);
947 static PyTypeObject PySamu = {
948 .tp_name = "passdb.samu",
949 .tp_basicsize = sizeof(pytalloc_Object),
950 .tp_getset = py_samu_getsetters,
952 .tp_new = py_samu_new,
953 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
954 .tp_doc = "samu() -> samu object\n",
958 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
960 struct pdb_methods *methods;
961 struct pdb_domain_info *domain_info;
962 PyObject *py_domain_info;
967 methods = pytalloc_get_ptr(self);
969 if ((tframe = talloc_stackframe()) == NULL) {
974 domain_info = methods->get_domain_info(methods, tframe);
979 sid = dom_sid_dup(tframe, &domain_info->sid);
986 guid = talloc(tframe, struct GUID);
992 *guid = domain_info->guid;
994 if ((py_domain_info = PyDict_New()) == NULL) {
999 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1000 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->name));
1001 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->name));
1002 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1003 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1005 talloc_free(tframe);
1007 return py_domain_info;
1011 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1014 const char *username;
1015 struct pdb_methods *methods;
1016 struct samu *sam_acct;
1017 PyObject *py_sam_acct;
1020 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1024 methods = pytalloc_get_ptr(self);
1026 if ((tframe = talloc_stackframe()) == NULL) {
1031 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1032 if (py_sam_acct == NULL) {
1034 talloc_free(tframe);
1037 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1039 status = methods->getsampwnam(methods, sam_acct, username);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1043 NT_STATUS_V(status),
1044 get_friendly_nt_error_msg(status));
1045 Py_DECREF(py_sam_acct);
1046 talloc_free(tframe);
1050 talloc_free(tframe);
1054 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1057 struct pdb_methods *methods;
1058 struct samu *sam_acct;
1059 PyObject *py_sam_acct;
1061 PyObject *py_user_sid;
1063 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1067 methods = pytalloc_get_ptr(self);
1069 if ((tframe = talloc_stackframe()) == NULL) {
1074 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1075 if (py_sam_acct == NULL) {
1077 talloc_free(tframe);
1080 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1082 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1083 if (!NT_STATUS_IS_OK(status)) {
1084 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1085 NT_STATUS_V(status),
1086 get_friendly_nt_error_msg(status));
1087 Py_DECREF(py_sam_acct);
1088 talloc_free(tframe);
1092 talloc_free(tframe);
1096 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1099 struct pdb_methods *methods;
1100 const char *username;
1101 unsigned int acct_flags;
1105 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1109 methods = pytalloc_get_ptr(self);
1111 if ((tframe = talloc_stackframe()) == NULL) {
1116 status = methods->create_user(methods, tframe, username, acct_flags, &rid);
1117 if (!NT_STATUS_IS_OK(status)) {
1118 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1120 NT_STATUS_V(status),
1121 get_friendly_nt_error_msg(status));
1122 talloc_free(tframe);
1126 talloc_free(tframe);
1127 return PyInt_FromLong(rid);
1130 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1133 struct pdb_methods *methods;
1135 struct samu *sam_acct;
1136 PyObject *py_sam_acct;
1138 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1142 methods = pytalloc_get_ptr(self);
1144 if ((tframe = talloc_stackframe()) == NULL) {
1149 sam_acct = pytalloc_get_ptr(py_sam_acct);
1151 status = methods->delete_user(methods, tframe, sam_acct);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1154 NT_STATUS_V(status),
1155 get_friendly_nt_error_msg(status));
1156 talloc_free(tframe);
1160 talloc_free(tframe);
1164 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1167 struct pdb_methods *methods;
1169 struct samu *sam_acct;
1170 PyObject *py_sam_acct;
1172 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1176 methods = pytalloc_get_ptr(self);
1178 if ((tframe = talloc_stackframe()) == NULL) {
1183 sam_acct = pytalloc_get_ptr(py_sam_acct);
1185 status = methods->add_sam_account(methods, sam_acct);
1186 if (!NT_STATUS_IS_OK(status)) {
1187 PyErr_Format(py_pdb_error, "Unable to add sam account, (%d,%s)",
1188 NT_STATUS_V(status),
1189 get_friendly_nt_error_msg(status));
1190 talloc_free(tframe);
1194 talloc_free(tframe);
1198 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1201 struct pdb_methods *methods;
1203 struct samu *sam_acct;
1204 PyObject *py_sam_acct;
1206 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1210 methods = pytalloc_get_ptr(self);
1212 if ((tframe = talloc_stackframe()) == NULL) {
1217 sam_acct = pytalloc_get_ptr(py_sam_acct);
1219 status = methods->update_sam_account(methods, sam_acct);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1222 NT_STATUS_V(status),
1223 get_friendly_nt_error_msg(status));
1224 talloc_free(tframe);
1228 talloc_free(tframe);
1232 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1235 struct pdb_methods *methods;
1237 struct samu *sam_acct;
1238 PyObject *py_sam_acct;
1240 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1244 methods = pytalloc_get_ptr(self);
1246 if ((tframe = talloc_stackframe()) == NULL) {
1251 sam_acct = pytalloc_get_ptr(py_sam_acct);
1253 status = methods->delete_sam_account(methods, sam_acct);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1256 NT_STATUS_V(status),
1257 get_friendly_nt_error_msg(status));
1258 talloc_free(tframe);
1262 talloc_free(tframe);
1266 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1269 struct pdb_methods *methods;
1271 struct samu *sam_acct;
1272 const char *new_username;
1273 PyObject *py_sam_acct;
1275 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1280 methods = pytalloc_get_ptr(self);
1282 if ((tframe = talloc_stackframe()) == NULL) {
1287 sam_acct = pytalloc_get_ptr(py_sam_acct);
1289 status = methods->rename_sam_account(methods, sam_acct, new_username);
1290 if (!NT_STATUS_IS_OK(status)) {
1291 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1292 NT_STATUS_V(status),
1293 get_friendly_nt_error_msg(status));
1294 talloc_free(tframe);
1298 talloc_free(tframe);
1302 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
1305 struct pdb_methods *methods;
1307 unsigned int acct_flags;
1308 struct pdb_search *search;
1309 struct samr_displayentry *entry;
1310 PyObject *py_userlist, *py_dict;
1312 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
1316 methods = pytalloc_get_ptr(self);
1318 if ((tframe = talloc_stackframe()) == NULL) {
1323 search = talloc_zero(tframe, struct pdb_search);
1324 if (search == NULL) {
1326 talloc_free(tframe);
1330 if (!methods->search_users(methods, search, acct_flags)) {
1331 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
1332 NT_STATUS_V(status),
1333 get_friendly_nt_error_msg(status));
1334 talloc_free(tframe);
1338 entry = talloc_zero(tframe, struct samr_displayentry);
1339 if (entry == NULL) {
1341 talloc_free(tframe);
1345 py_userlist = PyList_New(0);
1346 if (py_userlist == NULL) {
1348 talloc_free(tframe);
1352 while (search->next_entry(search, entry)) {
1353 py_dict = PyDict_New();
1354 if (py_dict == NULL) {
1357 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
1358 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
1359 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
1360 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
1361 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
1362 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
1363 PyList_Append(py_userlist, py_dict);
1366 search->search_end(search);
1368 talloc_free(tframe);
1373 static PyMethodDef py_pdb_methods[] = {
1374 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
1375 "domain_info() -> str\n\n \
1376 Get domain for the database." },
1377 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
1378 "getsampwnam(username) -> samu object\n\n \
1379 Get user information." },
1380 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
1381 "getsampwsid(sid) -> samu object\n\n \
1382 Get user information from user_sid (dcerpc.security.dom_sid object)." },
1383 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
1384 "create_user(username, acct_flags) -> rid\n\n \
1385 Create user. acct_flags are samr account control flags." },
1386 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
1387 "delete_user(samu object) -> None\n\n \
1389 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
1390 "add_sam_account(samu object) -> None\n\n \
1391 Add SAM account." },
1392 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
1393 "update_sam_account(samu object) -> None\n\n \
1394 Update SAM account." },
1395 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
1396 "delete_sam_account(samu object) -> None\n\n \
1397 Delete SAM account." },
1398 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
1399 "rename_sam_account(samu object1, new_username) -> None\n\n \
1400 Rename SAM account." },
1401 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
1402 "search_users(acct_flags) -> List\n\n \
1403 Search users. acct_flags are samr account control flags.\n \
1404 Each entry in the list is a dictionary with keys - \
1405 idx, rid, acct_flags, account_name, fullname, description." },
1410 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1412 const char *url = NULL;
1415 struct pdb_methods *methods;
1417 if (!PyArg_ParseTuple(args, "s", &url)) {
1421 /* Initalize list of methods */
1422 status = make_pdb_method_name(&methods, url);
1423 if (!NT_STATUS_IS_OK(status)) {
1424 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
1426 NT_STATUS_V(status),
1427 get_friendly_nt_error_msg(status));
1431 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
1440 static PyTypeObject PyPDB = {
1441 .tp_name = "passdb.PDB",
1442 .tp_basicsize = sizeof(pytalloc_Object),
1443 .tp_new = py_pdb_new,
1444 .tp_flags = Py_TPFLAGS_DEFAULT,
1445 .tp_methods = py_pdb_methods,
1446 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
1451 * Return a list of passdb backends
1453 static PyObject *py_passdb_backends(PyObject *self)
1456 const struct pdb_init_function_entry *entry;
1459 if ((tframe = talloc_stackframe()) == NULL) {
1464 entry = pdb_get_backends();
1469 if((py_blist = PyList_New(0)) == NULL) {
1475 PyList_Append(py_blist, PyString_FromString(entry->name));
1476 entry = entry->next;
1479 talloc_free(tframe);
1485 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
1487 const char *smb_config;
1490 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
1494 if ((tframe = talloc_stackframe()) == NULL) {
1499 /* Load smbconf parameters */
1500 if (!lp_load_global(smb_config)) {
1501 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
1505 talloc_free(tframe);
1511 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
1513 const char *private_dir;
1516 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
1520 if ((tframe = talloc_stackframe()) == NULL) {
1525 /* Initialize secrets database */
1526 if (!secrets_init_path(private_dir)) {
1527 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
1532 talloc_free(tframe);
1537 static PyObject *py_get_global_sam_sid(PyObject *self)
1539 struct dom_sid *domain_sid, *domain_sid_copy;
1541 PyObject *py_dom_sid;
1543 tframe = talloc_stackframe();
1544 if (tframe == NULL) {
1549 domain_sid = get_global_sam_sid();
1551 domain_sid_copy = dom_sid_dup(tframe, domain_sid);
1552 if (domain_sid_copy == NULL) {
1554 talloc_free(tframe);
1558 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
1560 talloc_free(tframe);
1566 static PyMethodDef py_passdb_methods[] = {
1567 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
1568 "get_backends() -> list\n\n \
1569 Get a list of password database backends supported." },
1570 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
1571 "set_smb_config(path) -> None\n\n \
1572 Set path to smb.conf file to load configuration parameters." },
1573 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
1574 "set_secrets_dir(private_dir) -> None\n\n \
1575 Set path to private directory to load secrets database from non-default location." },
1576 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
1577 "get_global_sam_sid() -> dom_sid\n\n \
1578 Return domain SID." },
1582 void initpassdb(void)
1585 char exception_name[] = "passdb.error";
1587 PyTypeObject *talloc_type = pytalloc_GetObjectType();
1588 if (talloc_type == NULL) {
1592 PyPDB.tp_base = talloc_type;
1593 if (PyType_Ready(&PyPDB) < 0) {
1597 PySamu.tp_base = talloc_type;
1598 if (PyType_Ready(&PySamu) < 0) {
1602 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
1607 /* Create new exception for passdb module */
1608 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
1609 Py_INCREF(py_pdb_error);
1610 PyModule_AddObject(m, "error", py_pdb_error);
1613 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
1615 /* Import dom_sid type from dcerpc.security */
1616 mod = PyImport_ImportModule("samba.dcerpc.security");
1621 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
1623 if (dom_sid_Type == NULL) {
1627 /* Import GUID type from dcerpc.misc */
1628 mod = PyImport_ImportModule("samba.dcerpc.misc");
1633 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
1635 if (guid_Type == NULL) {