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;
43 staticforward PyTypeObject PySamu;
44 staticforward PyTypeObject PyPDB;
46 static PyObject *py_pdb_error;
48 void initpassdb(void);
51 /************************** PIDL Autogeneratd ******************************/
53 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
55 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
56 PyObject *py_logon_time;
58 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
62 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
64 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
66 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
67 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
73 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
75 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
76 PyObject *py_logoff_time;
78 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
79 return py_logoff_time;
82 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
84 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
86 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
87 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
93 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
95 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
96 PyObject *py_kickoff_time;
98 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
99 return py_kickoff_time;
102 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
104 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
106 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
107 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
113 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
115 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
116 PyObject *py_bad_password_time;
118 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
119 return py_bad_password_time;
122 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
124 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
126 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
127 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
133 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
135 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
136 PyObject *py_pass_last_set_time;
138 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
139 return py_pass_last_set_time;
142 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
144 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
146 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
147 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
153 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
155 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
156 PyObject *py_pass_can_change_time;
158 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
159 return py_pass_can_change_time;
162 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
164 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
166 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
167 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
173 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
175 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
176 PyObject *py_pass_must_change_time;
178 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
179 return py_pass_must_change_time;
182 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
184 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
186 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
187 if (!pdb_set_pass_must_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
193 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
195 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
196 PyObject *py_username;
197 const char *username;
199 username = pdb_get_username(sam_acct);
200 if (username == NULL) {
204 py_username = PyString_FromString(username);
208 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
210 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
212 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
213 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
219 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
221 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
225 domain = pdb_get_domain(sam_acct);
226 if (domain == NULL) {
230 py_domain = PyString_FromString(domain);
234 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
236 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
238 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
239 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
245 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
247 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
248 PyObject *py_nt_username;
249 const char *nt_username;
251 nt_username = pdb_get_nt_username(sam_acct);
252 if (nt_username == NULL) {
256 py_nt_username = PyString_FromString(nt_username);
257 return py_nt_username;
260 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
262 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
264 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
265 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
271 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
273 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
274 PyObject *py_full_name;
275 const char *full_name;
277 full_name = pdb_get_fullname(sam_acct);
278 if (full_name == NULL) {
282 py_full_name = PyString_FromString(full_name);
286 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
288 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
290 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
291 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
297 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
299 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
300 PyObject *py_home_dir;
301 const char *home_dir;
303 home_dir = pdb_get_homedir(sam_acct);
304 if (home_dir == NULL) {
308 py_home_dir = PyString_FromString(home_dir);
312 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
314 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
316 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
317 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
323 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
325 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
326 PyObject *py_dir_drive;
327 const char *dir_drive;
329 dir_drive = pdb_get_dir_drive(sam_acct);
330 if (dir_drive == NULL) {
334 py_dir_drive = PyString_FromString(dir_drive);
338 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
340 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
342 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
343 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
349 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
351 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
352 PyObject *py_logon_script;
353 const char *logon_script;
355 logon_script = pdb_get_logon_script(sam_acct);
356 if (logon_script == NULL) {
360 py_logon_script = PyString_FromString(logon_script);
361 return py_logon_script;
364 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
366 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
368 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
369 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
375 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
377 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
378 PyObject *py_profile_path;
379 const char *profile_path;
381 profile_path = pdb_get_profile_path(sam_acct);
382 if (profile_path == NULL) {
386 py_profile_path = PyString_FromString(profile_path);
387 return py_profile_path;
390 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
392 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
394 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
395 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
401 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
403 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
404 PyObject *py_acct_desc;
405 const char *acct_desc;
407 acct_desc = pdb_get_acct_desc(sam_acct);
408 if (acct_desc == NULL) {
412 py_acct_desc = PyString_FromString(acct_desc);
416 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
418 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
420 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
421 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
427 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
429 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
430 PyObject *py_workstations;
431 const char *workstations;
433 workstations = pdb_get_workstations(sam_acct);
434 if (workstations == NULL) {
438 py_workstations = PyString_FromString(workstations);
439 return py_workstations;
442 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
444 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
446 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
447 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
453 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
455 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
456 PyObject *py_comment;
459 comment = pdb_get_comment(sam_acct);
460 if (comment == NULL) {
464 py_comment = PyString_FromString(comment);
468 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
470 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
472 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
473 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
479 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
481 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
482 PyObject *py_munged_dial;
483 const char *munged_dial;
485 munged_dial = pdb_get_munged_dial(sam_acct);
486 if (munged_dial == NULL) {
490 py_munged_dial = PyString_FromString(munged_dial);
491 return py_munged_dial;
494 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
496 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
498 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
499 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
505 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
507 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
508 PyObject *py_user_sid;
509 const struct dom_sid *user_sid;
510 struct dom_sid *copy_user_sid;
513 user_sid = pdb_get_user_sid(sam_acct);
514 if(user_sid == NULL) {
518 mem_ctx = talloc_new(NULL);
519 if (mem_ctx == NULL) {
523 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
524 if (copy_user_sid == NULL) {
526 talloc_free(mem_ctx);
530 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
532 talloc_free(mem_ctx);
537 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
539 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
541 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
542 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
548 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
550 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
551 PyObject *py_group_sid;
552 const struct dom_sid *group_sid;
553 struct dom_sid *copy_group_sid;
556 group_sid = pdb_get_group_sid(sam_acct);
557 if (group_sid == NULL) {
561 mem_ctx = talloc_new(NULL);
562 if (mem_ctx == NULL) {
566 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
567 if (copy_group_sid == NULL) {
569 talloc_free(mem_ctx);
573 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
575 talloc_free(mem_ctx);
580 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
582 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
584 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
585 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
591 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
593 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
597 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
602 py_lm_pw = PyString_FromString(lm_pw);
606 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
608 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
610 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
611 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
617 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
619 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
623 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
628 py_nt_pw = PyString_FromString(nt_pw);
632 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
634 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
636 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
642 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
644 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
645 PyObject *py_nt_pw_his;
646 const char *nt_pw_his;
649 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
650 if (nt_pw_his == NULL) {
654 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len);
658 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
660 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
665 PyString_AsStringAndSize(value, &nt_pw_his, &len);
667 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
673 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
675 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
676 PyObject *py_plaintext_pw;
677 const char *plaintext_pw;
679 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
680 if (plaintext_pw == NULL) {
684 py_plaintext_pw = PyString_FromString(plaintext_pw);
685 return py_plaintext_pw;
688 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
690 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
692 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
698 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
700 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
701 PyObject *py_acct_ctrl;
703 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
707 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
709 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
711 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
712 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
718 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
720 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
721 PyObject *py_logon_divs;
723 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
724 return py_logon_divs;
727 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
729 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
731 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
732 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
738 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
740 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
741 PyObject *py_hours_len;
743 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
747 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
749 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
751 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
752 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
758 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
760 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
765 hours = (const char *)pdb_get_hours(sam_acct);
770 if ((py_hours = PyList_New(MAX_HOURS_LEN)) == NULL) {
775 for (i=0; i<MAX_HOURS_LEN; i++) {
776 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
781 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
783 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
789 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
791 hours_len = PyList_GET_SIZE(value);
793 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
799 for (i=0; i < hours_len; i++) {
800 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
801 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
804 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
813 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
815 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
816 PyObject *py_bad_password_count;
818 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
819 return py_bad_password_count;
822 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
824 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
826 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
827 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
833 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
835 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
836 PyObject *py_logon_count;
838 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
839 return py_logon_count;
842 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
844 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
846 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
847 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
853 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
855 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
856 PyObject *py_country_code;
858 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
859 return py_country_code;
862 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
864 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
866 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
867 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
873 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
875 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
876 PyObject *py_code_page;
878 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
882 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
884 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
886 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
887 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
893 static PyGetSetDef py_samu_getsetters[] = {
894 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
895 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
896 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
897 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
898 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
899 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
900 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
901 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
902 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
903 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
904 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
905 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
906 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
907 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
908 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
909 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
910 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
911 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
912 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
913 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
914 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
915 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
916 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
917 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
918 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
919 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
920 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
921 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
922 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
923 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
924 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
925 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
926 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
931 /************************** PIDL Autogeneratd ******************************/
933 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
935 struct samu *sam_acct;
937 sam_acct = samu_new(NULL);
943 return pytalloc_steal(type, sam_acct);
946 static PyTypeObject PySamu = {
947 .tp_name = "passdb.samu",
948 .tp_basicsize = sizeof(pytalloc_Object),
949 .tp_getset = py_samu_getsetters,
951 .tp_new = py_samu_new,
952 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
953 .tp_doc = "samu() -> samu object\n",
957 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
959 struct pdb_methods *methods;
960 struct pdb_domain_info *domain_info;
961 PyObject *py_domain_info;
964 methods = pytalloc_get_ptr(self);
966 if ((tframe = talloc_stackframe()) == NULL) {
971 domain_info = methods->get_domain_info(methods, tframe);
976 if ((py_domain_info = PyDict_New()) == NULL) {
981 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
982 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->name));
983 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->name));
991 return py_domain_info;
995 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
998 const char *username;
999 struct pdb_methods *methods;
1000 struct samu *sam_acct;
1001 PyObject *py_sam_acct;
1004 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1008 methods = pytalloc_get_ptr(self);
1010 if ((tframe = talloc_stackframe()) == NULL) {
1015 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1016 if (py_sam_acct == NULL) {
1018 talloc_free(tframe);
1021 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1023 status = methods->getsampwnam(methods, sam_acct, username);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1027 NT_STATUS_V(status),
1028 get_friendly_nt_error_msg(status));
1029 Py_DECREF(py_sam_acct);
1030 talloc_free(tframe);
1034 talloc_free(tframe);
1038 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1041 struct pdb_methods *methods;
1042 struct samu *sam_acct;
1043 PyObject *py_sam_acct;
1045 PyObject *py_user_sid;
1047 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1051 methods = pytalloc_get_ptr(self);
1053 if ((tframe = talloc_stackframe()) == NULL) {
1058 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1059 if (py_sam_acct == NULL) {
1061 talloc_free(tframe);
1064 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1066 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1067 if (!NT_STATUS_IS_OK(status)) {
1068 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1069 NT_STATUS_V(status),
1070 get_friendly_nt_error_msg(status));
1071 Py_DECREF(py_sam_acct);
1072 talloc_free(tframe);
1076 talloc_free(tframe);
1080 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1083 struct pdb_methods *methods;
1084 const char *username;
1085 unsigned int acct_flags;
1089 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1093 methods = pytalloc_get_ptr(self);
1095 if ((tframe = talloc_stackframe()) == NULL) {
1100 status = methods->create_user(methods, tframe, username, acct_flags, &rid);
1101 if (!NT_STATUS_IS_OK(status)) {
1102 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1104 NT_STATUS_V(status),
1105 get_friendly_nt_error_msg(status));
1106 talloc_free(tframe);
1110 talloc_free(tframe);
1111 return PyInt_FromLong(rid);
1114 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1117 struct pdb_methods *methods;
1119 struct samu *sam_acct;
1120 PyObject *py_sam_acct;
1122 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1126 methods = pytalloc_get_ptr(self);
1128 if ((tframe = talloc_stackframe()) == NULL) {
1133 sam_acct = pytalloc_get_ptr(py_sam_acct);
1135 status = methods->delete_user(methods, tframe, sam_acct);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1138 NT_STATUS_V(status),
1139 get_friendly_nt_error_msg(status));
1140 talloc_free(tframe);
1144 talloc_free(tframe);
1148 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1151 struct pdb_methods *methods;
1153 struct samu *sam_acct;
1154 PyObject *py_sam_acct;
1156 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1160 methods = pytalloc_get_ptr(self);
1162 if ((tframe = talloc_stackframe()) == NULL) {
1167 sam_acct = pytalloc_get_ptr(py_sam_acct);
1169 status = methods->add_sam_account(methods, sam_acct);
1170 if (!NT_STATUS_IS_OK(status)) {
1171 PyErr_Format(py_pdb_error, "Unable to add sam account, (%d,%s)",
1172 NT_STATUS_V(status),
1173 get_friendly_nt_error_msg(status));
1174 talloc_free(tframe);
1178 talloc_free(tframe);
1182 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1185 struct pdb_methods *methods;
1187 struct samu *sam_acct;
1188 PyObject *py_sam_acct;
1190 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1194 methods = pytalloc_get_ptr(self);
1196 if ((tframe = talloc_stackframe()) == NULL) {
1201 sam_acct = pytalloc_get_ptr(py_sam_acct);
1203 status = methods->update_sam_account(methods, sam_acct);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1206 NT_STATUS_V(status),
1207 get_friendly_nt_error_msg(status));
1208 talloc_free(tframe);
1212 talloc_free(tframe);
1216 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1219 struct pdb_methods *methods;
1221 struct samu *sam_acct;
1222 PyObject *py_sam_acct;
1224 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1228 methods = pytalloc_get_ptr(self);
1230 if ((tframe = talloc_stackframe()) == NULL) {
1235 sam_acct = pytalloc_get_ptr(py_sam_acct);
1237 status = methods->delete_sam_account(methods, sam_acct);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1240 NT_STATUS_V(status),
1241 get_friendly_nt_error_msg(status));
1242 talloc_free(tframe);
1246 talloc_free(tframe);
1250 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1253 struct pdb_methods *methods;
1255 struct samu *sam_acct;
1256 const char *new_username;
1257 PyObject *py_sam_acct;
1259 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1264 methods = pytalloc_get_ptr(self);
1266 if ((tframe = talloc_stackframe()) == NULL) {
1271 sam_acct = pytalloc_get_ptr(py_sam_acct);
1273 status = methods->rename_sam_account(methods, sam_acct, new_username);
1274 if (!NT_STATUS_IS_OK(status)) {
1275 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1276 NT_STATUS_V(status),
1277 get_friendly_nt_error_msg(status));
1278 talloc_free(tframe);
1282 talloc_free(tframe);
1286 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
1289 struct pdb_methods *methods;
1291 unsigned int acct_flags;
1292 struct pdb_search *search;
1293 struct samr_displayentry *entry;
1294 PyObject *py_userlist, *py_dict;
1296 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
1300 methods = pytalloc_get_ptr(self);
1302 if ((tframe = talloc_stackframe()) == NULL) {
1307 search = talloc_zero(tframe, struct pdb_search);
1308 if (search == NULL) {
1310 talloc_free(tframe);
1314 if (!methods->search_users(methods, search, acct_flags)) {
1315 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
1316 NT_STATUS_V(status),
1317 get_friendly_nt_error_msg(status));
1318 talloc_free(tframe);
1322 entry = talloc_zero(tframe, struct samr_displayentry);
1323 if (entry == NULL) {
1325 talloc_free(tframe);
1329 py_userlist = PyList_New(0);
1330 if (py_userlist == NULL) {
1332 talloc_free(tframe);
1336 while (search->next_entry(search, entry)) {
1337 py_dict = PyDict_New();
1338 if (py_dict == NULL) {
1341 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
1342 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
1343 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
1344 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
1345 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
1346 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
1347 PyList_Append(py_userlist, py_dict);
1350 search->search_end(search);
1352 talloc_free(tframe);
1357 static PyMethodDef py_pdb_methods[] = {
1358 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
1359 "domain_info() -> str\n\n \
1360 Get domain for the database." },
1361 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
1362 "getsampwnam(username) -> samu object\n\n \
1363 Get user information." },
1364 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
1365 "getsampwsid(sid) -> samu object\n\n \
1366 Get user information from user_sid (dcerpc.security.dom_sid object)." },
1367 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
1368 "create_user(username, acct_flags) -> rid\n\n \
1369 Create user. acct_flags are samr account control flags." },
1370 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
1371 "delete_user(samu object) -> None\n\n \
1373 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
1374 "add_sam_account(samu object) -> None\n\n \
1375 Add SAM account." },
1376 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
1377 "update_sam_account(samu object) -> None\n\n \
1378 Update SAM account." },
1379 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
1380 "delete_sam_account(samu object) -> None\n\n \
1381 Delete SAM account." },
1382 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
1383 "rename_sam_account(samu object1, new_username) -> None\n\n \
1384 Rename SAM account." },
1385 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
1386 "search_users(acct_flags) -> List\n\n \
1387 Search users. acct_flags are samr account control flags.\n \
1388 Each entry in the list is a dictionary with keys - \
1389 idx, rid, acct_flags, account_name, fullname, description." },
1394 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1396 const char *url = NULL;
1399 struct pdb_methods *methods;
1401 if (!PyArg_ParseTuple(args, "s", &url)) {
1405 /* Initalize list of methods */
1406 status = make_pdb_method_name(&methods, url);
1407 if (!NT_STATUS_IS_OK(status)) {
1408 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
1410 NT_STATUS_V(status),
1411 get_friendly_nt_error_msg(status));
1415 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
1424 static PyTypeObject PyPDB = {
1425 .tp_name = "passdb.PDB",
1426 .tp_basicsize = sizeof(pytalloc_Object),
1427 .tp_new = py_pdb_new,
1428 .tp_flags = Py_TPFLAGS_DEFAULT,
1429 .tp_methods = py_pdb_methods,
1430 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
1435 * Return a list of passdb backends
1437 static PyObject *py_passdb_backends(PyObject *self)
1440 const struct pdb_init_function_entry *entry;
1443 if ((tframe = talloc_stackframe()) == NULL) {
1448 entry = pdb_get_backends();
1453 if((py_blist = PyList_New(0)) == NULL) {
1459 PyList_Append(py_blist, PyString_FromString(entry->name));
1460 entry = entry->next;
1463 talloc_free(tframe);
1469 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
1471 const char *smb_config;
1474 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
1478 if ((tframe = talloc_stackframe()) == NULL) {
1483 /* Load smbconf parameters */
1484 if (!lp_load_global(smb_config)) {
1485 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
1489 talloc_free(tframe);
1495 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
1497 const char *private_dir;
1500 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
1504 if ((tframe = talloc_stackframe()) == NULL) {
1509 /* Initialize secrets database */
1510 if (!secrets_init_path(private_dir)) {
1511 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
1516 talloc_free(tframe);
1522 static PyMethodDef py_passdb_methods[] = {
1523 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
1524 "get_backends() -> list\n\n \
1525 Get a list of password database backends supported." },
1526 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
1527 "set_smb_config(path) -> None\n\n \
1528 Set path to smb.conf file to load configuration parameters." },
1529 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
1530 "set_secrets_dir(private_dir) -> None\n\n \
1531 Set path to private directory to load secrets database from non-default location." },
1535 void initpassdb(void)
1538 char exception_name[] = "passdb.error";
1540 PyTypeObject *talloc_type = pytalloc_GetObjectType();
1541 if (talloc_type == NULL) {
1545 PyPDB.tp_base = talloc_type;
1546 if (PyType_Ready(&PyPDB) < 0) {
1550 PySamu.tp_base = talloc_type;
1551 if (PyType_Ready(&PySamu) < 0) {
1555 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
1560 /* Create new exception for passdb module */
1561 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
1562 Py_INCREF(py_pdb_error);
1563 PyModule_AddObject(m, "error", py_pdb_error);
1566 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
1568 /* Import dom_sid type from dcerpc.security */
1569 mod = PyImport_ImportModule("samba.dcerpc.security");
1574 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
1576 if (dom_sid_Type == NULL) {
1580 /* FIXME: Load passdb backends
1581 * Currently there is no equivalent public function for lazy_initialize_passdb()