talloc: Fix copy&paste errors
[Samba/gebeck_regimport.git] / source3 / passdb / py_passdb.c
blobd0ef5677382608a685c58dac41efeb2f0e36980f
1 /*
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/>.
20 #include <Python.h>
21 #include <pytalloc.h>
22 #include "includes.h"
23 #include "lib/util/talloc_stack.h"
24 #include "libcli/security/security.h"
25 #include "passdb.h"
26 #include "secrets.h"
28 /* There's no Py_ssize_t in 2.4, apparently */
29 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
30 typedef int Py_ssize_t;
31 typedef inquiry lenfunc;
32 typedef intargfunc ssizeargfunc;
33 #endif
35 #ifndef Py_RETURN_NONE
36 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
37 #endif
39 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
40 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
41 #endif
43 #ifndef PY_CHECK_TYPE
44 #define PY_CHECK_TYPE(type, var, fail) \
45 if (!PyObject_TypeCheck(var, type)) {\
46 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
47 fail; \
49 #endif
52 static PyTypeObject *dom_sid_Type = NULL;
53 static PyTypeObject *security_Type = NULL;
54 static PyTypeObject *guid_Type = NULL;
56 staticforward PyTypeObject PySamu;
57 staticforward PyTypeObject PyGroupmap;
58 staticforward PyTypeObject PyPDB;
60 static PyObject *py_pdb_error;
62 void initpassdb(void);
65 /************************** PIDL Autogeneratd ******************************/
67 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
69 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
70 PyObject *py_logon_time;
72 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
73 return py_logon_time;
76 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
78 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
80 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
81 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
82 return -1;
84 return 0;
87 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
89 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
90 PyObject *py_logoff_time;
92 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
93 return py_logoff_time;
96 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
98 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
100 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
101 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
102 return -1;
104 return 0;
107 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
109 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
110 PyObject *py_kickoff_time;
112 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
113 return py_kickoff_time;
116 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
118 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
120 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
121 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
122 return -1;
124 return 0;
127 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
129 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
130 PyObject *py_bad_password_time;
132 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
133 return py_bad_password_time;
136 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
138 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
140 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
141 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
142 return -1;
144 return 0;
147 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
149 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
150 PyObject *py_pass_last_set_time;
152 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
153 return py_pass_last_set_time;
156 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
158 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
160 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
161 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
162 return -1;
164 return 0;
167 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
169 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
170 PyObject *py_pass_can_change_time;
172 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
173 return py_pass_can_change_time;
176 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
178 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
180 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
181 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
182 return -1;
184 return 0;
187 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
189 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
190 PyObject *py_pass_must_change_time;
192 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
193 return py_pass_must_change_time;
196 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
198 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
200 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
202 /* TODO: make this not a get/set or give a better exception */
203 return -1;
206 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
208 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
209 PyObject *py_username;
210 const char *username;
212 username = pdb_get_username(sam_acct);
213 if (username == NULL) {
214 Py_RETURN_NONE;
217 py_username = PyString_FromString(username);
218 return py_username;
221 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
223 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
225 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
226 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
227 return -1;
229 return 0;
232 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
234 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
235 PyObject *py_domain;
236 const char *domain;
238 domain = pdb_get_domain(sam_acct);
239 if (domain == NULL) {
240 Py_RETURN_NONE;
243 py_domain = PyString_FromString(domain);
244 return py_domain;
247 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
249 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
251 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
252 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
253 return -1;
255 return 0;
258 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
260 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
261 PyObject *py_nt_username;
262 const char *nt_username;
264 nt_username = pdb_get_nt_username(sam_acct);
265 if (nt_username == NULL) {
266 Py_RETURN_NONE;
269 py_nt_username = PyString_FromString(nt_username);
270 return py_nt_username;
273 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
275 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
277 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
278 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
279 return -1;
281 return 0;
284 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
286 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
287 PyObject *py_full_name;
288 const char *full_name;
290 full_name = pdb_get_fullname(sam_acct);
291 if (full_name == NULL) {
292 Py_RETURN_NONE;
295 py_full_name = PyString_FromString(full_name);
296 return py_full_name;
299 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
301 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
303 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
304 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
305 return -1;
307 return 0;
310 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
312 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
313 PyObject *py_home_dir;
314 const char *home_dir;
316 home_dir = pdb_get_homedir(sam_acct);
317 if (home_dir == NULL) {
318 Py_RETURN_NONE;
321 py_home_dir = PyString_FromString(home_dir);
322 return py_home_dir;
325 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
327 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
329 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
330 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
331 return -1;
333 return 0;
336 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
338 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
339 PyObject *py_dir_drive;
340 const char *dir_drive;
342 dir_drive = pdb_get_dir_drive(sam_acct);
343 if (dir_drive == NULL) {
344 Py_RETURN_NONE;
347 py_dir_drive = PyString_FromString(dir_drive);
348 return py_dir_drive;
351 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
353 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
355 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
356 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
357 return -1;
359 return 0;
362 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
364 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
365 PyObject *py_logon_script;
366 const char *logon_script;
368 logon_script = pdb_get_logon_script(sam_acct);
369 if (logon_script == NULL) {
370 Py_RETURN_NONE;
373 py_logon_script = PyString_FromString(logon_script);
374 return py_logon_script;
377 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
379 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
381 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
382 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
383 return -1;
385 return 0;
388 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
390 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
391 PyObject *py_profile_path;
392 const char *profile_path;
394 profile_path = pdb_get_profile_path(sam_acct);
395 if (profile_path == NULL) {
396 Py_RETURN_NONE;
399 py_profile_path = PyString_FromString(profile_path);
400 return py_profile_path;
403 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
405 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
407 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
408 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
409 return -1;
411 return 0;
414 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
416 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
417 PyObject *py_acct_desc;
418 const char *acct_desc;
420 acct_desc = pdb_get_acct_desc(sam_acct);
421 if (acct_desc == NULL) {
422 Py_RETURN_NONE;
425 py_acct_desc = PyString_FromString(acct_desc);
426 return py_acct_desc;
429 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
431 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
433 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
434 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
435 return -1;
437 return 0;
440 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
442 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
443 PyObject *py_workstations;
444 const char *workstations;
446 workstations = pdb_get_workstations(sam_acct);
447 if (workstations == NULL) {
448 Py_RETURN_NONE;
451 py_workstations = PyString_FromString(workstations);
452 return py_workstations;
455 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
457 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
459 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
460 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
461 return -1;
463 return 0;
466 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
468 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
469 PyObject *py_comment;
470 const char *comment;
472 comment = pdb_get_comment(sam_acct);
473 if (comment == NULL) {
474 Py_RETURN_NONE;
477 py_comment = PyString_FromString(comment);
478 return py_comment;
481 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
483 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
485 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
486 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
487 return -1;
489 return 0;
492 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
494 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
495 PyObject *py_munged_dial;
496 const char *munged_dial;
498 munged_dial = pdb_get_munged_dial(sam_acct);
499 if (munged_dial == NULL) {
500 Py_RETURN_NONE;
503 py_munged_dial = PyString_FromString(munged_dial);
504 return py_munged_dial;
507 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
509 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
511 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
512 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
513 return -1;
515 return 0;
518 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
520 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
521 PyObject *py_user_sid;
522 const struct dom_sid *user_sid;
523 struct dom_sid *copy_user_sid;
524 TALLOC_CTX *mem_ctx;
526 user_sid = pdb_get_user_sid(sam_acct);
527 if(user_sid == NULL) {
528 Py_RETURN_NONE;
531 mem_ctx = talloc_new(NULL);
532 if (mem_ctx == NULL) {
533 PyErr_NoMemory();
534 return NULL;
536 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
537 if (copy_user_sid == NULL) {
538 PyErr_NoMemory();
539 talloc_free(mem_ctx);
540 return NULL;
543 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
545 talloc_free(mem_ctx);
547 return py_user_sid;
550 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
552 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
554 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
555 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
556 return -1;
558 return 0;
561 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
563 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
564 PyObject *py_group_sid;
565 const struct dom_sid *group_sid;
566 struct dom_sid *copy_group_sid;
567 TALLOC_CTX *mem_ctx;
569 mem_ctx = talloc_stackframe();
570 if (mem_ctx == NULL) {
571 PyErr_NoMemory();
572 return NULL;
575 group_sid = pdb_get_group_sid(sam_acct);
576 if (group_sid == NULL) {
577 Py_RETURN_NONE;
580 copy_group_sid = dom_sid_dup(mem_ctx, group_sid);
581 if (copy_group_sid == NULL) {
582 PyErr_NoMemory();
583 talloc_free(mem_ctx);
584 return NULL;
587 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
589 talloc_free(mem_ctx);
591 return py_group_sid;
594 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
596 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
598 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
599 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
600 return -1;
602 return 0;
605 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
607 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
608 PyObject *py_lm_pw;
609 const char *lm_pw;
611 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
612 if (lm_pw == NULL) {
613 Py_RETURN_NONE;
616 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
617 return py_lm_pw;
620 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
622 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
624 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
625 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
626 return -1;
628 return 0;
631 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
633 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
634 PyObject *py_nt_pw;
635 const char *nt_pw;
637 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
638 if (nt_pw == NULL) {
639 Py_RETURN_NONE;
642 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
643 return py_nt_pw;
646 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
648 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
650 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
651 return -1;
653 return 0;
656 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
658 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
659 PyObject *py_nt_pw_his;
660 const char *nt_pw_his;
661 uint32_t hist_len;
663 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
664 if (nt_pw_his == NULL) {
665 Py_RETURN_NONE;
668 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
669 return py_nt_pw_his;
672 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
674 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
675 char *nt_pw_his;
676 Py_ssize_t len;
677 uint32_t hist_len;
679 PyString_AsStringAndSize(value, &nt_pw_his, &len);
680 hist_len = len / PW_HISTORY_ENTRY_LEN;
681 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
682 return -1;
684 return 0;
687 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
689 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
690 PyObject *py_plaintext_pw;
691 const char *plaintext_pw;
693 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
694 if (plaintext_pw == NULL) {
695 Py_RETURN_NONE;
698 py_plaintext_pw = PyString_FromString(plaintext_pw);
699 return py_plaintext_pw;
702 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
704 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
706 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
707 return -1;
709 return 0;
712 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
714 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
715 PyObject *py_acct_ctrl;
717 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
718 return py_acct_ctrl;
721 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
723 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
725 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
726 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
727 return -1;
729 return 0;
732 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
734 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
735 PyObject *py_logon_divs;
737 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
738 return py_logon_divs;
741 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
743 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
745 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
746 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
747 return -1;
749 return 0;
752 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
754 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
755 PyObject *py_hours_len;
757 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
758 return py_hours_len;
761 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
763 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
765 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
766 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
767 return -1;
769 return 0;
772 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
774 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
775 PyObject *py_hours;
776 const char *hours;
777 int hours_len, i;
779 hours = (const char *)pdb_get_hours(sam_acct);
780 if(! hours) {
781 Py_RETURN_NONE;
784 hours_len = pdb_get_hours_len(sam_acct);
785 if ((py_hours = PyList_New(hours_len)) == NULL) {
786 PyErr_NoMemory();
787 return NULL;
790 for (i=0; i<hours_len; i++) {
791 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
793 return py_hours;
796 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
798 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
799 int i;
800 uint8_t *hours;
801 int hours_len;
802 bool status;
804 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
806 hours_len = PyList_GET_SIZE(value);
808 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
809 if (!hours) {
810 PyErr_NoMemory();
811 return -1;
814 for (i=0; i < hours_len; i++) {
815 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
816 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
819 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
820 talloc_free(hours);
822 if(! status) {
823 return -1;
825 return 0;
828 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
830 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
831 PyObject *py_bad_password_count;
833 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
834 return py_bad_password_count;
837 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
839 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
841 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
842 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
843 return -1;
845 return 0;
848 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
850 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
851 PyObject *py_logon_count;
853 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
854 return py_logon_count;
857 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
859 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
861 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
862 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
863 return -1;
865 return 0;
868 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
870 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
871 PyObject *py_country_code;
873 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
874 return py_country_code;
877 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
879 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
881 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
882 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
883 return -1;
885 return 0;
888 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
890 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
891 PyObject *py_code_page;
893 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
894 return py_code_page;
897 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
899 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
901 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
902 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
903 return -1;
905 return 0;
908 static PyGetSetDef py_samu_getsetters[] = {
909 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
910 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
911 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
912 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
913 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
914 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
915 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
916 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
917 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
918 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
919 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
920 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
921 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
922 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
923 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
924 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
925 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
926 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
927 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
928 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
929 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
930 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
931 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
932 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
933 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
934 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
935 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
936 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
937 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
938 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
939 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
940 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
941 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
942 { NULL }
946 /************************** PIDL Autogeneratd ******************************/
948 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
950 struct samu *sam_acct;
952 sam_acct = samu_new(NULL);
953 if (!sam_acct) {
954 PyErr_NoMemory();
955 return NULL;
958 return pytalloc_steal(type, sam_acct);
961 static PyTypeObject PySamu = {
962 .tp_name = "passdb.Samu",
963 .tp_basicsize = sizeof(pytalloc_Object),
964 .tp_getset = py_samu_getsetters,
965 .tp_methods = NULL,
966 .tp_new = py_samu_new,
967 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
968 .tp_doc = "Samu() -> samu object\n",
972 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
974 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
975 PyObject *py_gid;
977 py_gid = Py_BuildValue("i", group_map->gid);
978 return py_gid;
981 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
983 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
985 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
986 group_map->gid = PyInt_AsLong(value);
987 return 0;
990 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
992 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
993 PyObject *py_sid;
994 struct dom_sid *group_sid;
995 TALLOC_CTX *mem_ctx;
997 mem_ctx = talloc_new(NULL);
998 if (mem_ctx == NULL) {
999 PyErr_NoMemory();
1000 return NULL;
1003 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1004 if (group_sid == NULL) {
1005 PyErr_NoMemory();
1006 talloc_free(mem_ctx);
1007 return NULL;
1010 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1012 talloc_free(mem_ctx);
1014 return py_sid;
1017 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1019 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1021 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1022 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1023 return 0;
1026 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1028 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1029 PyObject *py_sid_name_use;
1031 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1032 return py_sid_name_use;
1035 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1037 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1039 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1040 group_map->sid_name_use = PyInt_AsLong(value);
1041 return 0;
1044 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1046 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1047 PyObject *py_nt_name;
1048 if (group_map->nt_name == NULL) {
1049 py_nt_name = Py_None;
1050 Py_INCREF(py_nt_name);
1051 } else {
1052 py_nt_name = PyString_FromString(group_map->nt_name);
1054 return py_nt_name;
1057 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1059 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1061 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1062 if (value == Py_None) {
1063 fstrcpy(group_map->nt_name, NULL);
1064 } else {
1065 fstrcpy(group_map->nt_name, PyString_AsString(value));
1067 return 0;
1070 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1072 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1073 PyObject *py_comment;
1074 if (group_map->comment == NULL) {
1075 py_comment = Py_None;
1076 Py_INCREF(py_comment);
1077 } else {
1078 py_comment = PyString_FromString(group_map->comment);
1080 return py_comment;
1083 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1085 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1087 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1088 if (value == Py_None) {
1089 fstrcpy(group_map->comment, NULL);
1090 } else {
1091 fstrcpy(group_map->comment, PyString_AsString(value));
1093 return 0;
1096 static PyGetSetDef py_groupmap_getsetters[] = {
1097 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1098 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1099 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1100 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1101 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1102 { NULL }
1105 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1107 GROUP_MAP *group_map;
1108 TALLOC_CTX *mem_ctx;
1109 PyObject *py_group_map;
1111 mem_ctx = talloc_new(NULL);
1112 if (mem_ctx == NULL) {
1113 PyErr_NoMemory();
1114 return NULL;
1117 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1118 if (group_map == NULL) {
1119 PyErr_NoMemory();
1120 talloc_free(mem_ctx);
1121 return NULL;
1124 py_group_map = pytalloc_steal(type, group_map);
1125 if (py_group_map == NULL) {
1126 PyErr_NoMemory();
1127 talloc_free(mem_ctx);
1128 return NULL;
1131 talloc_free(mem_ctx);
1133 return py_group_map;
1137 static PyTypeObject PyGroupmap = {
1138 .tp_name = "passdb.Groupmap",
1139 .tp_basicsize = sizeof(pytalloc_Object),
1140 .tp_getset = py_groupmap_getsetters,
1141 .tp_methods = NULL,
1142 .tp_new = py_groupmap_new,
1143 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1144 .tp_doc = "Groupmap() -> group map object\n",
1148 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1150 struct pdb_methods *methods;
1151 struct pdb_domain_info *domain_info;
1152 PyObject *py_domain_info;
1153 TALLOC_CTX *tframe;
1154 struct dom_sid *sid;
1155 struct GUID *guid;
1157 methods = pytalloc_get_ptr(self);
1159 if ((tframe = talloc_stackframe()) == NULL) {
1160 PyErr_NoMemory();
1161 return NULL;
1164 domain_info = methods->get_domain_info(methods, tframe);
1165 if (! domain_info) {
1166 Py_RETURN_NONE;
1169 sid = dom_sid_dup(tframe, &domain_info->sid);
1170 if (sid == NULL) {
1171 PyErr_NoMemory();
1172 talloc_free(tframe);
1173 return NULL;
1176 guid = talloc(tframe, struct GUID);
1177 if (guid == NULL) {
1178 PyErr_NoMemory();
1179 talloc_free(tframe);
1180 return NULL;
1182 *guid = domain_info->guid;
1184 if ((py_domain_info = PyDict_New()) == NULL) {
1185 PyErr_NoMemory();
1186 return NULL;
1189 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1190 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->name));
1191 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->name));
1192 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1193 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1195 talloc_free(tframe);
1197 return py_domain_info;
1201 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1203 NTSTATUS status;
1204 const char *username;
1205 struct pdb_methods *methods;
1206 struct samu *sam_acct;
1207 PyObject *py_sam_acct;
1208 TALLOC_CTX *tframe;
1210 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1211 return NULL;
1214 methods = pytalloc_get_ptr(self);
1216 if ((tframe = talloc_stackframe()) == NULL) {
1217 PyErr_NoMemory();
1218 return NULL;
1221 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1222 if (py_sam_acct == NULL) {
1223 PyErr_NoMemory();
1224 talloc_free(tframe);
1225 return NULL;
1227 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1229 status = methods->getsampwnam(methods, sam_acct, username);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1232 username,
1233 NT_STATUS_V(status),
1234 get_friendly_nt_error_msg(status));
1235 Py_DECREF(py_sam_acct);
1236 talloc_free(tframe);
1237 return NULL;
1240 talloc_free(tframe);
1241 return py_sam_acct;
1244 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1246 NTSTATUS status;
1247 struct pdb_methods *methods;
1248 struct samu *sam_acct;
1249 PyObject *py_sam_acct;
1250 TALLOC_CTX *tframe;
1251 PyObject *py_user_sid;
1253 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1254 return NULL;
1257 methods = pytalloc_get_ptr(self);
1259 if ((tframe = talloc_stackframe()) == NULL) {
1260 PyErr_NoMemory();
1261 return NULL;
1264 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1265 if (py_sam_acct == NULL) {
1266 PyErr_NoMemory();
1267 talloc_free(tframe);
1268 return NULL;
1270 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1272 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1273 if (!NT_STATUS_IS_OK(status)) {
1274 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1275 NT_STATUS_V(status),
1276 get_friendly_nt_error_msg(status));
1277 Py_DECREF(py_sam_acct);
1278 talloc_free(tframe);
1279 return NULL;
1282 talloc_free(tframe);
1283 return py_sam_acct;
1286 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1288 NTSTATUS status;
1289 struct pdb_methods *methods;
1290 const char *username;
1291 unsigned int acct_flags;
1292 unsigned int rid;
1293 TALLOC_CTX *tframe;
1295 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1296 return NULL;
1299 methods = pytalloc_get_ptr(self);
1301 if ((tframe = talloc_stackframe()) == NULL) {
1302 PyErr_NoMemory();
1303 return NULL;
1306 status = methods->create_user(methods, tframe, username, acct_flags, &rid);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1309 username,
1310 NT_STATUS_V(status),
1311 get_friendly_nt_error_msg(status));
1312 talloc_free(tframe);
1313 return NULL;
1316 talloc_free(tframe);
1317 return PyInt_FromLong(rid);
1320 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1322 NTSTATUS status;
1323 struct pdb_methods *methods;
1324 TALLOC_CTX *tframe;
1325 struct samu *sam_acct;
1326 PyObject *py_sam_acct;
1328 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1329 return NULL;
1332 methods = pytalloc_get_ptr(self);
1334 if ((tframe = talloc_stackframe()) == NULL) {
1335 PyErr_NoMemory();
1336 return NULL;
1339 sam_acct = pytalloc_get_ptr(py_sam_acct);
1341 status = methods->delete_user(methods, tframe, sam_acct);
1342 if (!NT_STATUS_IS_OK(status)) {
1343 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1344 NT_STATUS_V(status),
1345 get_friendly_nt_error_msg(status));
1346 talloc_free(tframe);
1347 return NULL;
1350 talloc_free(tframe);
1351 Py_RETURN_NONE;
1354 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1356 NTSTATUS status;
1357 struct pdb_methods *methods;
1358 TALLOC_CTX *tframe;
1359 struct samu *sam_acct;
1360 PyObject *py_sam_acct;
1362 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1363 return NULL;
1366 methods = pytalloc_get_ptr(self);
1368 if ((tframe = talloc_stackframe()) == NULL) {
1369 PyErr_NoMemory();
1370 return NULL;
1373 sam_acct = pytalloc_get_ptr(py_sam_acct);
1375 status = methods->add_sam_account(methods, sam_acct);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1378 sam_acct->username,
1379 NT_STATUS_V(status),
1380 get_friendly_nt_error_msg(status));
1381 talloc_free(tframe);
1382 return NULL;
1385 talloc_free(tframe);
1386 Py_RETURN_NONE;
1389 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1391 NTSTATUS status;
1392 struct pdb_methods *methods;
1393 TALLOC_CTX *tframe;
1394 struct samu *sam_acct;
1395 PyObject *py_sam_acct;
1397 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1398 return NULL;
1401 methods = pytalloc_get_ptr(self);
1403 if ((tframe = talloc_stackframe()) == NULL) {
1404 PyErr_NoMemory();
1405 return NULL;
1408 sam_acct = pytalloc_get_ptr(py_sam_acct);
1410 status = methods->update_sam_account(methods, sam_acct);
1411 if (!NT_STATUS_IS_OK(status)) {
1412 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1413 NT_STATUS_V(status),
1414 get_friendly_nt_error_msg(status));
1415 talloc_free(tframe);
1416 return NULL;
1419 talloc_free(tframe);
1420 Py_RETURN_NONE;
1423 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1425 NTSTATUS status;
1426 struct pdb_methods *methods;
1427 TALLOC_CTX *tframe;
1428 struct samu *sam_acct;
1429 PyObject *py_sam_acct;
1431 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1432 return NULL;
1435 methods = pytalloc_get_ptr(self);
1437 if ((tframe = talloc_stackframe()) == NULL) {
1438 PyErr_NoMemory();
1439 return NULL;
1442 sam_acct = pytalloc_get_ptr(py_sam_acct);
1444 status = methods->delete_sam_account(methods, sam_acct);
1445 if (!NT_STATUS_IS_OK(status)) {
1446 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1447 NT_STATUS_V(status),
1448 get_friendly_nt_error_msg(status));
1449 talloc_free(tframe);
1450 return NULL;
1453 talloc_free(tframe);
1454 Py_RETURN_NONE;
1457 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1459 NTSTATUS status;
1460 struct pdb_methods *methods;
1461 TALLOC_CTX *tframe;
1462 struct samu *sam_acct;
1463 const char *new_username;
1464 PyObject *py_sam_acct;
1466 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1467 &new_username)) {
1468 return NULL;
1471 methods = pytalloc_get_ptr(self);
1473 if ((tframe = talloc_stackframe()) == NULL) {
1474 PyErr_NoMemory();
1475 return NULL;
1478 sam_acct = pytalloc_get_ptr(py_sam_acct);
1480 status = methods->rename_sam_account(methods, sam_acct, new_username);
1481 if (!NT_STATUS_IS_OK(status)) {
1482 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1483 NT_STATUS_V(status),
1484 get_friendly_nt_error_msg(status));
1485 talloc_free(tframe);
1486 return NULL;
1489 talloc_free(tframe);
1490 Py_RETURN_NONE;
1494 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1496 NTSTATUS status;
1497 struct pdb_methods *methods;
1498 TALLOC_CTX *tframe;
1499 GROUP_MAP *group_map;
1500 struct dom_sid *domain_sid;
1501 PyObject *py_domain_sid, *py_group_map;
1503 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1504 return NULL;
1507 methods = pytalloc_get_ptr(self);
1509 if ((tframe = talloc_stackframe()) == NULL) {
1510 PyErr_NoMemory();
1511 return NULL;
1514 domain_sid = pytalloc_get_ptr(py_domain_sid);
1516 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1517 if (py_group_map == NULL) {
1518 PyErr_NoMemory();
1519 talloc_free(tframe);
1520 return NULL;
1523 group_map = pytalloc_get_ptr(py_group_map);
1525 status = methods->getgrsid(methods, group_map, *domain_sid);
1526 if (!NT_STATUS_IS_OK(status)) {
1527 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1528 NT_STATUS_V(status),
1529 get_friendly_nt_error_msg(status));
1530 talloc_free(tframe);
1531 return NULL;
1534 talloc_free(tframe);
1535 return py_group_map;
1539 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1541 NTSTATUS status;
1542 struct pdb_methods *methods;
1543 TALLOC_CTX *tframe;
1544 GROUP_MAP *group_map;
1545 PyObject *py_group_map;
1546 unsigned int gid_value;
1548 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1549 return NULL;
1552 methods = pytalloc_get_ptr(self);
1554 if ((tframe = talloc_stackframe()) == NULL) {
1555 PyErr_NoMemory();
1556 return NULL;
1559 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1560 if (py_group_map == NULL) {
1561 PyErr_NoMemory();
1562 talloc_free(tframe);
1563 return NULL;
1566 group_map = pytalloc_get_ptr(py_group_map);
1568 status = methods->getgrgid(methods, group_map, gid_value);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1571 NT_STATUS_V(status),
1572 get_friendly_nt_error_msg(status));
1573 talloc_free(tframe);
1574 return NULL;
1577 talloc_free(tframe);
1578 return py_group_map;
1582 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1584 NTSTATUS status;
1585 struct pdb_methods *methods;
1586 TALLOC_CTX *tframe;
1587 GROUP_MAP *group_map;
1588 PyObject *py_group_map;
1589 const char *groupname;
1591 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1592 return NULL;
1595 methods = pytalloc_get_ptr(self);
1597 if ((tframe = talloc_stackframe()) == NULL) {
1598 PyErr_NoMemory();
1599 return NULL;
1602 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1603 if (py_group_map == NULL) {
1604 PyErr_NoMemory();
1605 talloc_free(tframe);
1606 return NULL;
1609 group_map = pytalloc_get_ptr(py_group_map);
1611 status = methods->getgrnam(methods, group_map, groupname);
1612 if (!NT_STATUS_IS_OK(status)) {
1613 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1614 NT_STATUS_V(status),
1615 get_friendly_nt_error_msg(status));
1616 talloc_free(tframe);
1617 return NULL;
1620 talloc_free(tframe);
1621 return py_group_map;
1625 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1627 NTSTATUS status;
1628 struct pdb_methods *methods;
1629 TALLOC_CTX *tframe;
1630 const char *groupname;
1631 uint32_t group_rid;
1633 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1634 return NULL;
1637 methods = pytalloc_get_ptr(self);
1639 if ((tframe = talloc_stackframe()) == NULL) {
1640 PyErr_NoMemory();
1641 return NULL;
1644 status = methods->create_dom_group(methods, tframe, groupname, &group_rid);
1645 if (!NT_STATUS_IS_OK(status)) {
1646 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1647 groupname,
1648 NT_STATUS_V(status),
1649 get_friendly_nt_error_msg(status));
1650 talloc_free(tframe);
1651 return NULL;
1654 talloc_free(tframe);
1655 return PyInt_FromLong(group_rid);
1659 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1661 NTSTATUS status;
1662 struct pdb_methods *methods;
1663 TALLOC_CTX *tframe;
1664 unsigned int group_rid;
1666 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1667 return NULL;
1670 methods = pytalloc_get_ptr(self);
1672 if ((tframe = talloc_stackframe()) == NULL) {
1673 PyErr_NoMemory();
1674 return NULL;
1677 status = methods->delete_dom_group(methods, tframe, group_rid);
1678 if (!NT_STATUS_IS_OK(status)) {
1679 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1680 group_rid,
1681 NT_STATUS_V(status),
1682 get_friendly_nt_error_msg(status));
1683 talloc_free(tframe);
1684 return NULL;
1687 talloc_free(tframe);
1688 Py_RETURN_NONE;
1692 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1694 NTSTATUS status;
1695 struct pdb_methods *methods;
1696 TALLOC_CTX *tframe;
1697 PyObject *py_group_map;
1698 GROUP_MAP *group_map;
1700 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1701 return NULL;
1704 methods = pytalloc_get_ptr(self);
1706 if ((tframe = talloc_stackframe()) == NULL) {
1707 PyErr_NoMemory();
1708 return NULL;
1711 group_map = pytalloc_get_ptr(py_group_map);
1713 status = methods->add_group_mapping_entry(methods, group_map);
1714 if (!NT_STATUS_IS_OK(status)) {
1715 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1716 NT_STATUS_V(status),
1717 get_friendly_nt_error_msg(status));
1718 talloc_free(tframe);
1719 return NULL;
1722 talloc_free(tframe);
1723 Py_RETURN_NONE;
1727 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1729 NTSTATUS status;
1730 struct pdb_methods *methods;
1731 TALLOC_CTX *tframe;
1732 PyObject *py_group_map;
1733 GROUP_MAP *group_map;
1735 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1736 return NULL;
1739 methods = pytalloc_get_ptr(self);
1741 if ((tframe = talloc_stackframe()) == NULL) {
1742 PyErr_NoMemory();
1743 return NULL;
1746 group_map = pytalloc_get_ptr(py_group_map);
1748 status = methods->update_group_mapping_entry(methods, group_map);
1749 if (!NT_STATUS_IS_OK(status)) {
1750 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1751 NT_STATUS_V(status),
1752 get_friendly_nt_error_msg(status));
1753 talloc_free(tframe);
1754 return NULL;
1757 talloc_free(tframe);
1758 Py_RETURN_NONE;
1762 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1764 NTSTATUS status;
1765 struct pdb_methods *methods;
1766 TALLOC_CTX *tframe;
1767 PyObject *py_group_sid;
1768 struct dom_sid *group_sid;
1770 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1771 return NULL;
1774 methods = pytalloc_get_ptr(self);
1776 if ((tframe = talloc_stackframe()) == NULL) {
1777 PyErr_NoMemory();
1778 return NULL;
1781 group_sid = pytalloc_get_ptr(py_group_sid);
1783 status = methods->delete_group_mapping_entry(methods, *group_sid);
1784 if (!NT_STATUS_IS_OK(status)) {
1785 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1786 NT_STATUS_V(status),
1787 get_friendly_nt_error_msg(status));
1788 talloc_free(tframe);
1789 return NULL;
1792 talloc_free(tframe);
1793 Py_RETURN_NONE;
1797 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1799 NTSTATUS status;
1800 struct pdb_methods *methods;
1801 TALLOC_CTX *tframe;
1802 enum lsa_SidType sid_name_use;
1803 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1804 int unix_only = 0;
1805 PyObject *py_domain_sid;
1806 struct dom_sid *domain_sid = NULL;
1807 GROUP_MAP **gmap = NULL;
1808 GROUP_MAP *group_map;
1809 size_t num_entries;
1810 PyObject *py_gmap_list, *py_group_map;
1811 int i;
1813 py_domain_sid = Py_None;
1814 Py_INCREF(Py_None);
1816 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1817 &lsa_sidtype_value, &unix_only)) {
1818 return NULL;
1821 methods = pytalloc_get_ptr(self);
1823 if ((tframe = talloc_stackframe()) == NULL) {
1824 PyErr_NoMemory();
1825 return NULL;
1828 sid_name_use = lsa_sidtype_value;
1830 if (py_domain_sid != Py_None) {
1831 domain_sid = pytalloc_get_ptr(py_domain_sid);
1834 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1835 &gmap, &num_entries, unix_only);
1836 if (!NT_STATUS_IS_OK(status)) {
1837 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1838 NT_STATUS_V(status),
1839 get_friendly_nt_error_msg(status));
1840 talloc_free(tframe);
1841 return NULL;
1844 py_gmap_list = PyList_New(0);
1845 if (py_gmap_list == NULL) {
1846 PyErr_NoMemory();
1847 talloc_free(tframe);
1848 return NULL;
1851 for(i=0; i<num_entries; i++) {
1852 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1853 if (py_group_map) {
1854 group_map = pytalloc_get_ptr(py_group_map);
1855 *group_map = *gmap[i];
1856 talloc_steal(group_map, gmap[i]->nt_name);
1857 talloc_steal(group_map, gmap[i]->comment);
1859 PyList_Append(py_gmap_list, py_group_map);
1863 talloc_free(gmap);
1864 talloc_free(tframe);
1866 return py_gmap_list;
1870 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1872 NTSTATUS status;
1873 struct pdb_methods *methods;
1874 TALLOC_CTX *tframe;
1875 PyObject *py_group_sid;
1876 struct dom_sid *group_sid;
1877 uint32_t *member_rids;
1878 size_t num_members;
1879 PyObject *py_sid_list;
1880 struct dom_sid *domain_sid, *member_sid;
1881 int i;
1883 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1884 return NULL;
1887 methods = pytalloc_get_ptr(self);
1889 if ((tframe = talloc_stackframe()) == NULL) {
1890 PyErr_NoMemory();
1891 return NULL;
1894 group_sid = pytalloc_get_ptr(py_group_sid);
1896 status = methods->enum_group_members(methods, tframe, group_sid,
1897 &member_rids, &num_members);
1898 if (!NT_STATUS_IS_OK(status)) {
1899 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1900 NT_STATUS_V(status),
1901 get_friendly_nt_error_msg(status));
1902 talloc_free(tframe);
1903 return NULL;
1906 py_sid_list = PyList_New(0);
1907 if (py_sid_list == NULL) {
1908 PyErr_NoMemory();
1909 talloc_free(tframe);
1910 return NULL;
1913 domain_sid = get_global_sam_sid();
1915 for(i=0; i<num_members; i++) {
1916 member_sid = dom_sid_add_rid(tframe, domain_sid, member_rids[i]);
1917 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
1920 talloc_free(tframe);
1922 return py_sid_list;
1926 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
1928 NTSTATUS status;
1929 struct pdb_methods *methods;
1930 TALLOC_CTX *tframe;
1931 uint32_t group_rid, member_rid;
1933 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
1934 return NULL;
1937 methods = pytalloc_get_ptr(self);
1939 if ((tframe = talloc_stackframe()) == NULL) {
1940 PyErr_NoMemory();
1941 return NULL;
1944 status = methods->add_groupmem(methods, tframe, group_rid, member_rid);
1945 if (!NT_STATUS_IS_OK(status)) {
1946 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
1947 NT_STATUS_V(status),
1948 get_friendly_nt_error_msg(status));
1949 talloc_free(tframe);
1950 return NULL;
1953 talloc_free(tframe);
1954 Py_RETURN_NONE;
1958 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
1960 NTSTATUS status;
1961 struct pdb_methods *methods;
1962 TALLOC_CTX *tframe;
1963 uint32_t group_rid, member_rid;
1965 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
1966 return NULL;
1969 methods = pytalloc_get_ptr(self);
1971 if ((tframe = talloc_stackframe()) == NULL) {
1972 PyErr_NoMemory();
1973 return NULL;
1976 status = methods->del_groupmem(methods, tframe, group_rid, member_rid);
1977 if (!NT_STATUS_IS_OK(status)) {
1978 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1979 NT_STATUS_V(status),
1980 get_friendly_nt_error_msg(status));
1981 talloc_free(tframe);
1982 return NULL;
1985 talloc_free(tframe);
1986 Py_RETURN_NONE;
1990 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
1992 NTSTATUS status;
1993 struct pdb_methods *methods;
1994 TALLOC_CTX *tframe;
1995 const char *alias_name;
1996 uint32_t rid;
1998 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
1999 return NULL;
2002 methods = pytalloc_get_ptr(self);
2004 if ((tframe = talloc_stackframe()) == NULL) {
2005 PyErr_NoMemory();
2006 return NULL;
2009 status = methods->create_alias(methods, alias_name, &rid);
2010 if (!NT_STATUS_IS_OK(status)) {
2011 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2012 alias_name,
2013 NT_STATUS_V(status),
2014 get_friendly_nt_error_msg(status));
2015 talloc_free(tframe);
2016 return NULL;
2019 talloc_free(tframe);
2021 return PyInt_FromLong(rid);
2025 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2027 NTSTATUS status;
2028 struct pdb_methods *methods;
2029 TALLOC_CTX *tframe;
2030 PyObject *py_alias_sid;
2031 struct dom_sid *alias_sid;
2033 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2034 return NULL;
2037 methods = pytalloc_get_ptr(self);
2039 if ((tframe = talloc_stackframe()) == NULL) {
2040 PyErr_NoMemory();
2041 return NULL;
2044 alias_sid = pytalloc_get_ptr(py_alias_sid);
2046 status = methods->delete_alias(methods, alias_sid);
2047 if (!NT_STATUS_IS_OK(status)) {
2048 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2049 NT_STATUS_V(status),
2050 get_friendly_nt_error_msg(status));
2051 talloc_free(tframe);
2052 return NULL;
2055 talloc_free(tframe);
2056 Py_RETURN_NONE;
2060 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2062 NTSTATUS status;
2063 struct pdb_methods *methods;
2064 TALLOC_CTX *tframe;
2065 PyObject *py_alias_sid;
2066 struct dom_sid *alias_sid;
2067 struct acct_info *alias_info;
2068 PyObject *py_alias_info;
2070 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2071 return NULL;
2074 methods = pytalloc_get_ptr(self);
2076 if ((tframe = talloc_stackframe()) == NULL) {
2077 PyErr_NoMemory();
2078 return NULL;
2081 alias_sid = pytalloc_get_ptr(py_alias_sid);
2083 alias_info = talloc_zero(tframe, struct acct_info);
2084 if (!alias_info) {
2085 PyErr_NoMemory();
2086 return NULL;
2089 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2090 if (!NT_STATUS_IS_OK(status)) {
2091 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2092 NT_STATUS_V(status),
2093 get_friendly_nt_error_msg(status));
2094 talloc_free(tframe);
2095 return NULL;
2098 py_alias_info = PyDict_New();
2099 if (py_alias_info == NULL) {
2100 PyErr_NoMemory();
2101 talloc_free(tframe);
2102 return NULL;
2105 PyDict_SetItemString(py_alias_info, "acct_name",
2106 PyString_FromString(alias_info->acct_name));
2107 PyDict_SetItemString(py_alias_info, "acct_desc",
2108 PyString_FromString(alias_info->acct_desc));
2109 PyDict_SetItemString(py_alias_info, "rid",
2110 PyInt_FromLong(alias_info->rid));
2112 talloc_free(tframe);
2114 return py_alias_info;
2118 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2120 NTSTATUS status;
2121 struct pdb_methods *methods;
2122 TALLOC_CTX *tframe;
2123 PyObject *py_alias_sid, *py_alias_info;
2124 struct dom_sid *alias_sid;
2125 struct acct_info alias_info;
2127 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2128 &py_alias_info)) {
2129 return NULL;
2132 methods = pytalloc_get_ptr(self);
2134 if ((tframe = talloc_stackframe()) == NULL) {
2135 PyErr_NoMemory();
2136 return NULL;
2139 alias_sid = pytalloc_get_ptr(py_alias_sid);
2141 fstrcpy(alias_info.acct_name, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2142 fstrcpy(alias_info.acct_desc, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2144 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2145 if (!NT_STATUS_IS_OK(status)) {
2146 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2147 NT_STATUS_V(status),
2148 get_friendly_nt_error_msg(status));
2149 talloc_free(tframe);
2150 return NULL;
2153 talloc_free(tframe);
2154 Py_RETURN_NONE;
2158 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2160 NTSTATUS status;
2161 struct pdb_methods *methods;
2162 TALLOC_CTX *tframe;
2163 PyObject *py_alias_sid, *py_member_sid;
2164 struct dom_sid *alias_sid, *member_sid;
2166 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2167 dom_sid_Type, &py_member_sid)) {
2168 return NULL;
2171 methods = pytalloc_get_ptr(self);
2173 if ((tframe = talloc_stackframe()) == NULL) {
2174 PyErr_NoMemory();
2175 return NULL;
2178 alias_sid = pytalloc_get_ptr(py_alias_sid);
2179 member_sid = pytalloc_get_ptr(py_member_sid);
2181 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2182 if (!NT_STATUS_IS_OK(status)) {
2183 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2184 NT_STATUS_V(status),
2185 get_friendly_nt_error_msg(status));
2186 talloc_free(tframe);
2187 return NULL;
2190 talloc_free(tframe);
2191 Py_RETURN_NONE;
2195 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2197 NTSTATUS status;
2198 struct pdb_methods *methods;
2199 TALLOC_CTX *tframe;
2200 PyObject *py_alias_sid, *py_member_sid;
2201 const struct dom_sid *alias_sid, *member_sid;
2203 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2204 dom_sid_Type, &py_member_sid)) {
2205 return NULL;
2208 methods = pytalloc_get_ptr(self);
2210 if ((tframe = talloc_stackframe()) == NULL) {
2211 PyErr_NoMemory();
2212 return NULL;
2215 alias_sid = pytalloc_get_ptr(py_alias_sid);
2216 member_sid = pytalloc_get_ptr(py_member_sid);
2218 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2219 if (!NT_STATUS_IS_OK(status)) {
2220 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2221 NT_STATUS_V(status),
2222 get_friendly_nt_error_msg(status));
2223 talloc_free(tframe);
2224 return NULL;
2227 talloc_free(tframe);
2228 Py_RETURN_NONE;
2232 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2234 NTSTATUS status;
2235 struct pdb_methods *methods;
2236 TALLOC_CTX *tframe;
2237 PyObject *py_alias_sid;
2238 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2239 PyObject *py_member_list, *py_member_sid;
2240 size_t num_members;
2241 int i;
2243 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2244 return NULL;
2247 methods = pytalloc_get_ptr(self);
2249 if ((tframe = talloc_stackframe()) == NULL) {
2250 PyErr_NoMemory();
2251 return NULL;
2254 alias_sid = pytalloc_get_ptr(py_alias_sid);
2256 status = methods->enum_aliasmem(methods, alias_sid, tframe, &member_sid, &num_members);
2257 if (!NT_STATUS_IS_OK(status)) {
2258 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2259 NT_STATUS_V(status),
2260 get_friendly_nt_error_msg(status));
2261 talloc_free(tframe);
2262 return NULL;
2265 py_member_list = PyList_New(0);
2266 if (py_member_list == NULL) {
2267 PyErr_NoMemory();
2268 talloc_free(tframe);
2269 return NULL;
2272 for(i=0; i<num_members; i++) {
2273 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2274 if (py_member_sid == NULL) {
2275 PyErr_NoMemory();
2276 talloc_free(tframe);
2277 return NULL;
2279 tmp_sid = pytalloc_get_ptr(py_member_sid);
2280 *tmp_sid = member_sid[i];
2281 PyList_Append(py_member_list, py_member_sid);
2284 talloc_free(tframe);
2286 return py_member_list;
2290 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2292 NTSTATUS status;
2293 struct pdb_methods *methods;
2294 TALLOC_CTX *tframe;
2295 PyObject *py_acct_policy;
2296 uint32_t value;
2297 const char **names;
2298 int count, i;
2299 enum pdb_policy_type type;
2301 methods = pytalloc_get_ptr(self);
2303 if ((tframe = talloc_stackframe()) == NULL) {
2304 PyErr_NoMemory();
2305 return NULL;
2308 py_acct_policy = PyDict_New();
2309 if (py_acct_policy == NULL) {
2310 PyErr_NoMemory();
2311 return NULL;
2314 account_policy_names_list(tframe, &names, &count);
2315 for (i=0; i<count; i++) {
2316 type = account_policy_name_to_typenum(names[i]);
2317 status = methods->get_account_policy(methods, type, &value);
2318 if (NT_STATUS_IS_OK(status)) {
2319 PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2323 talloc_free(tframe);
2325 return py_acct_policy;
2329 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2331 NTSTATUS status;
2332 struct pdb_methods *methods;
2333 TALLOC_CTX *tframe;
2334 PyObject *py_acct_policy, *py_value;
2335 const char **names;
2336 int count, i;
2337 enum pdb_policy_type type;
2339 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2340 return NULL;
2343 methods = pytalloc_get_ptr(self);
2345 if ((tframe = talloc_stackframe()) == NULL) {
2346 PyErr_NoMemory();
2347 return NULL;
2350 account_policy_names_list(tframe, &names, &count);
2351 for (i=0; i<count; i++) {
2352 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2353 type = account_policy_name_to_typenum(names[i]);
2354 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2355 if (!NT_STATUS_IS_OK(status)) {
2356 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2357 names[i],
2358 NT_STATUS_V(status),
2359 get_friendly_nt_error_msg(status));
2365 talloc_free(tframe);
2367 Py_RETURN_NONE;
2370 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2372 NTSTATUS status;
2373 struct pdb_methods *methods;
2374 TALLOC_CTX *tframe;
2375 unsigned int acct_flags;
2376 struct pdb_search *search;
2377 struct samr_displayentry *entry;
2378 PyObject *py_userlist, *py_dict;
2380 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2381 return NULL;
2384 methods = pytalloc_get_ptr(self);
2386 if ((tframe = talloc_stackframe()) == NULL) {
2387 PyErr_NoMemory();
2388 return NULL;
2391 search = talloc_zero(tframe, struct pdb_search);
2392 if (search == NULL) {
2393 PyErr_NoMemory();
2394 talloc_free(tframe);
2395 return NULL;
2398 if (!methods->search_users(methods, search, acct_flags)) {
2399 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
2400 NT_STATUS_V(status),
2401 get_friendly_nt_error_msg(status));
2402 talloc_free(tframe);
2403 return NULL;
2406 entry = talloc_zero(tframe, struct samr_displayentry);
2407 if (entry == NULL) {
2408 PyErr_NoMemory();
2409 talloc_free(tframe);
2410 return NULL;
2413 py_userlist = PyList_New(0);
2414 if (py_userlist == NULL) {
2415 PyErr_NoMemory();
2416 talloc_free(tframe);
2417 return NULL;
2420 while (search->next_entry(search, entry)) {
2421 py_dict = PyDict_New();
2422 if (py_dict == NULL) {
2423 PyErr_NoMemory();
2424 } else {
2425 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2426 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2427 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2428 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2429 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2430 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2431 PyList_Append(py_userlist, py_dict);
2434 search->search_end(search);
2436 talloc_free(tframe);
2438 return py_userlist;
2442 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2444 NTSTATUS status;
2445 struct pdb_methods *methods;
2446 TALLOC_CTX *tframe;
2447 struct pdb_search *search;
2448 struct samr_displayentry *entry;
2449 PyObject *py_grouplist, *py_dict;
2451 methods = pytalloc_get_ptr(self);
2453 if ((tframe = talloc_stackframe()) == NULL) {
2454 PyErr_NoMemory();
2455 return NULL;
2458 search = talloc_zero(tframe, struct pdb_search);
2459 if (search == NULL) {
2460 PyErr_NoMemory();
2461 talloc_free(tframe);
2462 return NULL;
2465 if (!methods->search_groups(methods, search)) {
2466 PyErr_Format(py_pdb_error, "Unable to search groups, (%d,%s)",
2467 NT_STATUS_V(status),
2468 get_friendly_nt_error_msg(status));
2469 talloc_free(tframe);
2470 return NULL;
2473 entry = talloc_zero(tframe, struct samr_displayentry);
2474 if (entry == NULL) {
2475 PyErr_NoMemory();
2476 talloc_free(tframe);
2477 return NULL;
2480 py_grouplist = PyList_New(0);
2481 if (py_grouplist == NULL) {
2482 PyErr_NoMemory();
2483 talloc_free(tframe);
2484 return NULL;
2487 while (search->next_entry(search, entry)) {
2488 py_dict = PyDict_New();
2489 if (py_dict == NULL) {
2490 PyErr_NoMemory();
2491 } else {
2492 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2493 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2494 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2495 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2496 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2497 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2498 PyList_Append(py_grouplist, py_dict);
2501 search->search_end(search);
2503 talloc_free(tframe);
2505 return py_grouplist;
2509 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2511 struct pdb_methods *methods;
2512 TALLOC_CTX *tframe;
2513 struct pdb_search *search;
2514 struct samr_displayentry *entry;
2515 PyObject *py_aliaslist, *py_dict;
2516 PyObject *py_domain_sid;
2517 struct dom_sid *domain_sid = NULL;
2519 py_domain_sid = Py_None;
2520 Py_INCREF(Py_None);
2522 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2523 return NULL;
2526 methods = pytalloc_get_ptr(self);
2528 if ((tframe = talloc_stackframe()) == NULL) {
2529 PyErr_NoMemory();
2530 return NULL;
2533 if (py_domain_sid != Py_None) {
2534 domain_sid = pytalloc_get_ptr(py_domain_sid);
2537 search = talloc_zero(tframe, struct pdb_search);
2538 if (search == NULL) {
2539 PyErr_NoMemory();
2540 talloc_free(tframe);
2541 return NULL;
2544 if (!methods->search_aliases(methods, search, domain_sid)) {
2545 PyErr_Format(py_pdb_error, "Unable to search aliases");
2546 talloc_free(tframe);
2547 return NULL;
2550 entry = talloc_zero(tframe, struct samr_displayentry);
2551 if (entry == NULL) {
2552 PyErr_NoMemory();
2553 talloc_free(tframe);
2554 return NULL;
2557 py_aliaslist = PyList_New(0);
2558 if (py_aliaslist == NULL) {
2559 PyErr_NoMemory();
2560 talloc_free(tframe);
2561 return NULL;
2564 while (search->next_entry(search, entry)) {
2565 py_dict = PyDict_New();
2566 if (py_dict == NULL) {
2567 PyErr_NoMemory();
2568 } else {
2569 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2570 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2571 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2572 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2573 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2574 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2575 PyList_Append(py_aliaslist, py_dict);
2578 search->search_end(search);
2580 talloc_free(tframe);
2582 return py_aliaslist;
2586 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2588 struct pdb_methods *methods;
2589 TALLOC_CTX *tframe;
2590 unsigned int uid;
2591 struct dom_sid user_sid, *copy_user_sid;
2592 PyObject *py_user_sid;
2594 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2595 return NULL;
2598 methods = pytalloc_get_ptr(self);
2600 if ((tframe = talloc_stackframe()) == NULL) {
2601 PyErr_NoMemory();
2602 return NULL;
2605 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2606 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2607 talloc_free(tframe);
2608 return NULL;
2611 copy_user_sid = dom_sid_dup(tframe, &user_sid);
2612 if (copy_user_sid == NULL) {
2613 PyErr_NoMemory();
2614 talloc_free(tframe);
2615 return NULL;
2618 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2620 talloc_free(tframe);
2622 return py_user_sid;
2626 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2628 struct pdb_methods *methods;
2629 TALLOC_CTX *tframe;
2630 unsigned int gid;
2631 struct dom_sid group_sid, *copy_group_sid;
2632 PyObject *py_group_sid;
2634 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2635 return NULL;
2638 methods = pytalloc_get_ptr(self);
2640 if ((tframe = talloc_stackframe()) == NULL) {
2641 PyErr_NoMemory();
2642 return NULL;
2645 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2646 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2647 talloc_free(tframe);
2648 return NULL;
2651 copy_group_sid = dom_sid_dup(tframe, &group_sid);
2652 if (copy_group_sid == NULL) {
2653 PyErr_NoMemory();
2654 talloc_free(tframe);
2655 return NULL;
2658 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2660 talloc_free(tframe);
2662 return py_group_sid;
2666 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2668 struct pdb_methods *methods;
2669 TALLOC_CTX *tframe;
2670 PyObject *py_sid;
2671 struct dom_sid *sid;
2672 uid_t uid = -1;
2673 gid_t gid = -1;
2674 enum lsa_SidType type;
2676 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2677 return NULL;
2680 methods = pytalloc_get_ptr(self);
2682 if ((tframe = talloc_stackframe()) == NULL) {
2683 PyErr_NoMemory();
2684 return NULL;
2687 sid = pytalloc_get_ptr(py_sid);
2689 if (!methods->sid_to_id(methods, sid, &uid, &gid, &type)) {
2690 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2691 talloc_free(tframe);
2692 return NULL;
2695 talloc_free(tframe);
2697 return Py_BuildValue("(II)", (uid != -1)?uid:gid, type);
2701 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2703 struct pdb_methods *methods;
2704 TALLOC_CTX *tframe;
2705 uint32_t rid;
2707 methods = pytalloc_get_ptr(self);
2709 if ((tframe = talloc_stackframe()) == NULL) {
2710 PyErr_NoMemory();
2711 return NULL;
2714 if (!methods->new_rid(methods, &rid)) {
2715 PyErr_Format(py_pdb_error, "Unable to get new rid");
2716 talloc_free(tframe);
2717 return NULL;
2720 talloc_free(tframe);
2722 return PyInt_FromLong(rid);
2726 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2728 struct pdb_methods *methods;
2729 TALLOC_CTX *tframe;
2730 const char *domain;
2731 char *pwd;
2732 struct dom_sid sid, *copy_sid;
2733 PyObject *py_sid;
2734 time_t last_set_time;
2735 PyObject *py_value;
2737 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2738 return NULL;
2741 methods = pytalloc_get_ptr(self);
2743 if ((tframe = talloc_stackframe()) == NULL) {
2744 PyErr_NoMemory();
2745 return NULL;
2748 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2749 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2750 talloc_free(tframe);
2751 return NULL;
2754 copy_sid = dom_sid_dup(tframe, &sid);
2755 if (copy_sid == NULL) {
2756 PyErr_NoMemory();
2757 talloc_free(tframe);
2758 return NULL;
2761 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2762 if (py_sid == NULL) {
2763 PyErr_NoMemory();
2764 talloc_free(tframe);
2765 return NULL;
2768 talloc_free(tframe);
2770 py_value = PyDict_New();
2771 if (py_value == NULL) {
2772 PyErr_NoMemory();
2773 return NULL;
2776 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2777 PyDict_SetItemString(py_value, "sid", py_sid);
2778 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2780 return py_value;
2784 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2786 struct pdb_methods *methods;
2787 TALLOC_CTX *tframe;
2788 const char *domain;
2789 const char *pwd;
2790 const struct dom_sid *domain_sid;
2791 PyObject *py_domain_sid;
2793 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2794 dom_sid_Type, &py_domain_sid)) {
2795 return NULL;
2798 methods = pytalloc_get_ptr(self);
2800 if ((tframe = talloc_stackframe()) == NULL) {
2801 PyErr_NoMemory();
2802 return NULL;
2805 domain_sid = pytalloc_get_ptr(py_domain_sid);
2807 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2808 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2809 talloc_free(tframe);
2810 return NULL;
2813 Py_RETURN_NONE;
2817 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2819 struct pdb_methods *methods;
2820 TALLOC_CTX *tframe;
2821 const char *domain;
2823 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2824 return NULL;
2827 methods = pytalloc_get_ptr(self);
2829 if ((tframe = talloc_stackframe()) == NULL) {
2830 PyErr_NoMemory();
2831 return NULL;
2834 if (!methods->del_trusteddom_pw(methods, domain)) {
2835 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2836 talloc_free(tframe);
2837 return NULL;
2840 Py_RETURN_NONE;
2844 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2846 NTSTATUS status;
2847 struct pdb_methods *methods;
2848 TALLOC_CTX *tframe;
2849 uint32_t num_domains;
2850 struct trustdom_info **domains;
2851 PyObject *py_domain_list, *py_dict;
2852 int i;
2854 methods = pytalloc_get_ptr(self);
2856 if ((tframe = talloc_stackframe()) == NULL) {
2857 PyErr_NoMemory();
2858 return NULL;
2861 status = methods->enum_trusteddoms(methods, tframe, &num_domains, &domains);
2862 if (!NT_STATUS_IS_OK(status)) {
2863 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2864 NT_STATUS_V(status),
2865 get_friendly_nt_error_msg(status));
2866 talloc_free(tframe);
2867 return NULL;
2870 py_domain_list = PyList_New(0);
2871 if (py_domain_list == NULL) {
2872 PyErr_NoMemory();
2873 talloc_free(tframe);
2874 return NULL;
2877 for(i=0; i<num_domains; i++) {
2878 py_dict = PyDict_New();
2879 if (py_dict) {
2880 PyDict_SetItemString(py_dict, "name",
2881 PyString_FromString(domains[i]->name));
2882 PyDict_SetItemString(py_dict, "sid",
2883 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2886 PyList_Append(py_domain_list, py_dict);
2889 talloc_free(tframe);
2891 return py_domain_list;
2895 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2897 NTSTATUS status;
2898 struct pdb_methods *methods;
2899 TALLOC_CTX *tframe;
2900 const char *domain;
2901 struct pdb_trusted_domain *td;
2902 PyObject *py_domain_info;
2904 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2905 return NULL;
2908 methods = pytalloc_get_ptr(self);
2910 if ((tframe = talloc_stackframe()) == NULL) {
2911 PyErr_NoMemory();
2912 return NULL;
2915 status = methods->get_trusted_domain(methods, tframe, domain, &td);
2916 if (!NT_STATUS_IS_OK(status)) {
2917 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2918 NT_STATUS_V(status),
2919 get_friendly_nt_error_msg(status));
2920 talloc_free(tframe);
2921 return NULL;
2924 py_domain_info = PyDict_New();
2925 if (py_domain_info == NULL) {
2926 PyErr_NoMemory();
2927 talloc_free(tframe);
2928 return NULL;
2931 PyDict_SetItemString(py_domain_info, "domain_name",
2932 PyString_FromString(td->domain_name));
2933 PyDict_SetItemString(py_domain_info, "netbios_name",
2934 PyString_FromString(td->netbios_name));
2935 PyDict_SetItemString(py_domain_info, "security_identifier",
2936 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2937 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2938 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2939 td->trust_auth_incoming.length));
2940 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2941 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2942 td->trust_auth_outgoing.length));
2943 PyDict_SetItemString(py_domain_info, "trust_direction",
2944 PyInt_FromLong(td->trust_direction));
2945 PyDict_SetItemString(py_domain_info, "trust_type",
2946 PyInt_FromLong(td->trust_type));
2947 PyDict_SetItemString(py_domain_info, "trust_attributes",
2948 PyInt_FromLong(td->trust_attributes));
2949 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2950 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2951 td->trust_forest_trust_info.length));
2953 talloc_free(tframe);
2955 return py_domain_info;
2959 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
2961 NTSTATUS status;
2962 struct pdb_methods *methods;
2963 TALLOC_CTX *tframe;
2964 PyObject *py_domain_sid;
2965 struct dom_sid *domain_sid;
2966 struct pdb_trusted_domain *td;
2967 PyObject *py_domain_info;
2969 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
2970 return NULL;
2973 methods = pytalloc_get_ptr(self);
2975 if ((tframe = talloc_stackframe()) == NULL) {
2976 PyErr_NoMemory();
2977 return NULL;
2980 domain_sid = pytalloc_get_ptr(py_domain_sid);
2982 status = methods->get_trusted_domain_by_sid(methods, tframe, domain_sid, &td);
2983 if (!NT_STATUS_IS_OK(status)) {
2984 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2985 NT_STATUS_V(status),
2986 get_friendly_nt_error_msg(status));
2987 talloc_free(tframe);
2988 return NULL;
2991 py_domain_info = PyDict_New();
2992 if (py_domain_info == NULL) {
2993 PyErr_NoMemory();
2994 talloc_free(tframe);
2995 return NULL;
2998 PyDict_SetItemString(py_domain_info, "domain_name",
2999 PyString_FromString(td->domain_name));
3000 PyDict_SetItemString(py_domain_info, "netbios_name",
3001 PyString_FromString(td->netbios_name));
3002 PyDict_SetItemString(py_domain_info, "security_identifier",
3003 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3004 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3005 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3006 td->trust_auth_incoming.length));
3007 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3008 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3009 td->trust_auth_outgoing.length));
3010 PyDict_SetItemString(py_domain_info, "trust_direction",
3011 PyInt_FromLong(td->trust_direction));
3012 PyDict_SetItemString(py_domain_info, "trust_type",
3013 PyInt_FromLong(td->trust_type));
3014 PyDict_SetItemString(py_domain_info, "trust_attributes",
3015 PyInt_FromLong(td->trust_attributes));
3016 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3017 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3018 td->trust_forest_trust_info.length));
3020 talloc_free(tframe);
3022 return py_domain_info;
3026 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3028 NTSTATUS status;
3029 struct pdb_methods *methods;
3030 TALLOC_CTX *tframe;
3031 const char *domain;
3032 PyObject *py_td_info;
3033 struct pdb_trusted_domain td_info;
3034 PyObject *py_tmp;
3035 Py_ssize_t len;
3037 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3038 return NULL;
3041 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3042 td_info.domain_name = PyString_AsString(py_tmp);
3044 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3045 td_info.netbios_name = PyString_AsString(py_tmp);
3047 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3048 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3050 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3051 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3052 td_info.trust_auth_incoming.length = len;
3054 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3055 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3056 td_info.trust_auth_outgoing.length = len;
3058 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3059 td_info.trust_direction = PyInt_AsLong(py_tmp);
3061 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3062 td_info.trust_type = PyInt_AsLong(py_tmp);
3064 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3065 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3067 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3068 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3069 td_info.trust_forest_trust_info.length = len;
3071 methods = pytalloc_get_ptr(self);
3073 if ((tframe = talloc_stackframe()) == NULL) {
3074 PyErr_NoMemory();
3075 return NULL;
3078 status = methods->set_trusted_domain(methods, domain, &td_info);
3079 if (!NT_STATUS_IS_OK(status)) {
3080 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3081 NT_STATUS_V(status),
3082 get_friendly_nt_error_msg(status));
3083 talloc_free(tframe);
3084 return NULL;
3087 talloc_free(tframe);
3089 Py_RETURN_NONE;
3093 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3095 NTSTATUS status;
3096 struct pdb_methods *methods;
3097 TALLOC_CTX *tframe;
3098 const char *domain;
3100 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3101 return NULL;
3104 methods = pytalloc_get_ptr(self);
3106 if ((tframe = talloc_stackframe()) == NULL) {
3107 PyErr_NoMemory();
3108 return NULL;
3111 status = methods->del_trusted_domain(methods, domain);
3112 if (!NT_STATUS_IS_OK(status)) {
3113 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3114 NT_STATUS_V(status),
3115 get_friendly_nt_error_msg(status));
3116 talloc_free(tframe);
3117 return NULL;
3120 talloc_free(tframe);
3122 Py_RETURN_NONE;
3126 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3128 NTSTATUS status;
3129 struct pdb_methods *methods;
3130 TALLOC_CTX *tframe;
3131 uint32_t num_domains;
3132 struct pdb_trusted_domain **td_info, *td;
3133 PyObject *py_td_info, *py_domain_info;
3134 int i;
3136 methods = pytalloc_get_ptr(self);
3138 if ((tframe = talloc_stackframe()) == NULL) {
3139 PyErr_NoMemory();
3140 return NULL;
3143 status = methods->enum_trusted_domains(methods, tframe, &num_domains, &td_info);
3144 if (!NT_STATUS_IS_OK(status)) {
3145 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3146 NT_STATUS_V(status),
3147 get_friendly_nt_error_msg(status));
3148 talloc_free(tframe);
3149 return NULL;
3152 py_td_info = PyList_New(0);
3153 if (py_td_info == NULL) {
3154 PyErr_NoMemory();
3155 talloc_free(tframe);
3156 return NULL;
3159 for (i=0; i<num_domains; i++) {
3161 py_domain_info = PyDict_New();
3162 if (py_domain_info == NULL) {
3163 PyErr_NoMemory();
3164 Py_DECREF(py_td_info);
3165 talloc_free(tframe);
3166 return NULL;
3169 td = td_info[i];
3171 PyDict_SetItemString(py_domain_info, "domain_name",
3172 PyString_FromString(td->domain_name));
3173 PyDict_SetItemString(py_domain_info, "netbios_name",
3174 PyString_FromString(td->netbios_name));
3175 PyDict_SetItemString(py_domain_info, "security_identifier",
3176 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3177 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3178 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3179 td->trust_auth_incoming.length));
3180 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3181 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3182 td->trust_auth_outgoing.length));
3183 PyDict_SetItemString(py_domain_info, "trust_direction",
3184 PyInt_FromLong(td->trust_direction));
3185 PyDict_SetItemString(py_domain_info, "trust_type",
3186 PyInt_FromLong(td->trust_type));
3187 PyDict_SetItemString(py_domain_info, "trust_attributes",
3188 PyInt_FromLong(td->trust_attributes));
3189 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3190 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3191 td->trust_forest_trust_info.length));
3192 PyList_Append(py_td_info, py_domain_info);
3195 talloc_free(tframe);
3197 return py_td_info;
3201 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3203 NTSTATUS status;
3204 struct pdb_methods *methods;
3205 TALLOC_CTX *tframe;
3206 const char *secret_name;
3207 DATA_BLOB secret_current, secret_old;
3208 NTTIME secret_current_lastchange, secret_old_lastchange;
3209 PyObject *py_sd;
3210 struct security_descriptor *sd;
3211 PyObject *py_secret;
3213 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3214 return NULL;
3217 methods = pytalloc_get_ptr(self);
3219 if ((tframe = talloc_stackframe()) == NULL) {
3220 PyErr_NoMemory();
3221 return NULL;
3224 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3225 if (py_sd == NULL) {
3226 PyErr_NoMemory();
3227 talloc_free(tframe);
3228 return NULL;
3230 sd = pytalloc_get_ptr(py_sd);
3232 status = methods->get_secret(methods, tframe, secret_name,
3233 &secret_current,
3234 &secret_current_lastchange,
3235 &secret_old,
3236 &secret_old_lastchange,
3237 &sd);
3238 if (!NT_STATUS_IS_OK(status)) {
3239 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3240 secret_name,
3241 NT_STATUS_V(status),
3242 get_friendly_nt_error_msg(status));
3243 talloc_free(tframe);
3244 return NULL;
3247 py_secret = PyDict_New();
3248 if (py_secret == NULL) {
3249 PyErr_NoMemory();
3250 Py_DECREF(py_sd);
3251 talloc_free(tframe);
3252 return NULL;
3255 PyDict_SetItemString(py_secret, "secret_current",
3256 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3257 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3258 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3259 PyDict_SetItemString(py_secret, "secret_old",
3260 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3261 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3262 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3263 PyDict_SetItemString(py_secret, "sd", py_sd);
3265 talloc_free(tframe);
3267 return py_secret;
3271 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3273 NTSTATUS status;
3274 struct pdb_methods *methods;
3275 TALLOC_CTX *tframe;
3276 const char *secret_name;
3277 PyObject *py_secret;
3278 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3279 DATA_BLOB secret_current, secret_old;
3280 struct security_descriptor *sd;
3281 Py_ssize_t len;
3283 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3284 return NULL;
3287 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3288 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3289 py_sd = PyDict_GetItemString(py_secret, "sd");
3291 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3292 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3293 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3295 methods = pytalloc_get_ptr(self);
3297 if ((tframe = talloc_stackframe()) == NULL) {
3298 PyErr_NoMemory();
3299 return NULL;
3302 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3303 secret_current.length = len;
3304 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3305 secret_current.length = len;
3306 sd = pytalloc_get_ptr(py_sd);
3308 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3309 if (!NT_STATUS_IS_OK(status)) {
3310 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3311 secret_name,
3312 NT_STATUS_V(status),
3313 get_friendly_nt_error_msg(status));
3314 talloc_free(tframe);
3315 return NULL;
3318 talloc_free(tframe);
3320 Py_RETURN_NONE;
3324 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3326 NTSTATUS status;
3327 struct pdb_methods *methods;
3328 TALLOC_CTX *tframe;
3329 const char *secret_name;
3331 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3332 return NULL;
3335 methods = pytalloc_get_ptr(self);
3337 if ((tframe = talloc_stackframe()) == NULL) {
3338 PyErr_NoMemory();
3339 return NULL;
3342 status = methods->delete_secret(methods, secret_name);
3343 if (!NT_STATUS_IS_OK(status)) {
3344 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3345 secret_name,
3346 NT_STATUS_V(status),
3347 get_friendly_nt_error_msg(status));
3348 talloc_free(tframe);
3349 return NULL;
3352 talloc_free(tframe);
3354 Py_RETURN_NONE;
3357 static PyMethodDef py_pdb_methods[] = {
3358 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3359 "domain_info() -> str\n\n \
3360 Get domain information for the database." },
3361 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3362 "getsampwnam(username) -> samu object\n\n \
3363 Get user information by name." },
3364 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3365 "getsampwsid(user_sid) -> samu object\n\n \
3366 Get user information by sid (dcerpc.security.dom_sid object)." },
3367 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3368 "create_user(username, acct_flags) -> rid\n\n \
3369 Create user. acct_flags are samr account control flags." },
3370 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3371 "delete_user(samu object) -> None\n\n \
3372 Delete user." },
3373 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3374 "add_sam_account(samu object) -> None\n\n \
3375 Add SAM account." },
3376 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3377 "update_sam_account(samu object) -> None\n\n \
3378 Update SAM account." },
3379 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3380 "delete_sam_account(samu object) -> None\n\n \
3381 Delete SAM account." },
3382 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3383 "rename_sam_account(samu object1, new_username) -> None\n\n \
3384 Rename SAM account." },
3385 /* update_login_attempts */
3386 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3387 "getgrsid(group_sid) -> groupmap object\n\n \
3388 Get group information by sid (dcerpc.security.dom_sid object)." },
3389 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3390 "getgrsid(gid) -> groupmap object\n\n \
3391 Get group information by gid." },
3392 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3393 "getgrsid(groupname) -> groupmap object\n\n \
3394 Get group information by name." },
3395 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3396 "create_dom_group(groupname) -> group_rid\n\n \
3397 Create new domain group by name." },
3398 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3399 "delete_dom_group(group_rid) -> None\n\n \
3400 Delete domain group identified by rid" },
3401 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3402 "add_group_mapping_entry(groupmap) -> None\n \
3403 Add group mapping entry for groupmap object." },
3404 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3405 "update_group_mapping_entry(groupmap) -> None\n\n \
3406 Update group mapping entry for groupmap object." },
3407 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3408 "delete_group_mapping_entry(groupmap) -> None\n\n \
3409 Delete group mapping entry for groupmap object." },
3410 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3411 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3412 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3413 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3414 "enum_group_members(group_sid) -> List\n\n \
3415 Return list of users (dom_sid object) in group." },
3416 /* enum_group_memberships */
3417 /* set_unix_primary_group */
3418 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3419 "add_groupmem(group_rid, member_rid) -> None\n\n \
3420 Add user to group." },
3421 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3422 "del_groupmem(group_rid, member_rid) -> None\n\n \
3423 Remove user from from group." },
3424 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3425 "create_alias(alias_name) -> alias_rid\n\n \
3426 Create alias entry." },
3427 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3428 "delete_alias(alias_sid) -> None\n\n \
3429 Delete alias entry." },
3430 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3431 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3432 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3433 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3434 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3435 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3436 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3437 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3438 Add user to alias entry." },
3439 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3440 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3441 Remove a user from alias entry." },
3442 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3443 "enum_aliasmem(alias_sid) -> List\n\n \
3444 Return a list of members (dom_sid object) for alias entry." },
3445 /* enum_alias_memberships */
3446 /* lookup_rids */
3447 /* lookup_names */
3448 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3449 "get_account_policy() -> Mapping\n\n \
3450 Get account policy information as a dictionary." },
3451 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3452 "get_account_policy(Mapping) -> None\n\n \
3453 Set account policy settings from a dicionary." },
3454 /* get_seq_num */
3455 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3456 "search_users(acct_flags) -> List\n\n \
3457 Search users. acct_flags are samr account control flags.\n \
3458 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3459 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3460 "search_groups() -> List\n\n \
3461 Search unix only groups. \n \
3462 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3463 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3464 "search_aliases([domain_sid]) -> List\n\n \
3465 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3466 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3467 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3468 "uid_to_sid(uid) -> sid\n\n \
3469 Return sid for given user id." },
3470 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3471 "gid_to_sid(gid) -> sid\n\n \
3472 Return sid for given group id." },
3473 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3474 "sid_to_id(sid) -> Tuple\n\n \
3475 Return id and type for given sid." },
3476 /* capabilities */
3477 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3478 "new_rid() -> rid\n\n \
3479 Get a new rid." },
3480 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3481 "get_trusteddom_pw(domain) -> Mapping\n\n \
3482 Get trusted domain password, sid and last set time in a dictionary." },
3483 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3484 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3485 Set trusted domain password." },
3486 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3487 "del_trusteddom_pw(domain) -> None\n\n \
3488 Delete trusted domain password." },
3489 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3490 "enum_trusteddoms() -> List\n\n \
3491 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3492 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3493 "get_trusted_domain(domain) -> Mapping\n\n \
3494 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." },
3495 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3496 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3497 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" },
3498 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3499 "set_trusted_domain(domain, Mapping) -> None\n\n \
3500 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." },
3501 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3502 "del_trusted_domain(domain) -> None\n\n \
3503 Delete trusted domain." },
3504 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3505 "enum_trusted_domains() -> List\n\n \
3506 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." },
3507 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3508 "get_secret(secret_name) -> Mapping\n\n \
3509 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3510 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3511 "set_secret(secret_name, Mapping) -> None\n\n \
3512 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3513 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3514 "delete_secret(secret_name) -> None\n\n \
3515 Delete secret information for secret_name." },
3516 { NULL },
3520 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3522 const char *url = NULL;
3523 PyObject *pypdb;
3524 NTSTATUS status;
3525 struct pdb_methods *methods;
3527 if (!PyArg_ParseTuple(args, "s", &url)) {
3528 return NULL;
3531 /* Initalize list of methods */
3532 status = make_pdb_method_name(&methods, url);
3533 if (!NT_STATUS_IS_OK(status)) {
3534 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3535 url,
3536 NT_STATUS_V(status),
3537 get_friendly_nt_error_msg(status));
3538 return NULL;
3541 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3542 PyErr_NoMemory();
3543 return NULL;
3546 return pypdb;
3550 static PyTypeObject PyPDB = {
3551 .tp_name = "passdb.PDB",
3552 .tp_basicsize = sizeof(pytalloc_Object),
3553 .tp_new = py_pdb_new,
3554 .tp_flags = Py_TPFLAGS_DEFAULT,
3555 .tp_methods = py_pdb_methods,
3556 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3561 * Return a list of passdb backends
3563 static PyObject *py_passdb_backends(PyObject *self)
3565 PyObject *py_blist;
3566 const struct pdb_init_function_entry *entry;
3567 TALLOC_CTX *tframe;
3569 if ((tframe = talloc_stackframe()) == NULL) {
3570 PyErr_NoMemory();
3571 return NULL;
3574 entry = pdb_get_backends();
3575 if(! entry) {
3576 Py_RETURN_NONE;
3579 if((py_blist = PyList_New(0)) == NULL) {
3580 PyErr_NoMemory();
3581 return NULL;
3584 while(entry) {
3585 PyList_Append(py_blist, PyString_FromString(entry->name));
3586 entry = entry->next;
3589 talloc_free(tframe);
3591 return py_blist;
3595 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3597 const char *smb_config;
3598 TALLOC_CTX *tframe;
3600 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3601 return NULL;
3604 if ((tframe = talloc_stackframe()) == NULL) {
3605 PyErr_NoMemory();
3606 return NULL;
3609 /* Load smbconf parameters */
3610 if (!lp_load_global(smb_config)) {
3611 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3612 return NULL;
3615 talloc_free(tframe);
3617 Py_RETURN_NONE;
3621 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3623 const char *private_dir;
3624 TALLOC_CTX *tframe;
3626 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3627 return NULL;
3630 if ((tframe = talloc_stackframe()) == NULL) {
3631 PyErr_NoMemory();
3632 return NULL;
3635 /* Initialize secrets database */
3636 if (!secrets_init_path(private_dir)) {
3637 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3638 private_dir);
3639 return NULL;
3642 talloc_free(tframe);
3644 Py_RETURN_NONE;
3647 static PyObject *py_get_global_sam_sid(PyObject *self)
3649 struct dom_sid *domain_sid, *domain_sid_copy;
3650 TALLOC_CTX *tframe;
3651 PyObject *py_dom_sid;
3653 tframe = talloc_stackframe();
3654 if (tframe == NULL) {
3655 PyErr_NoMemory();
3656 return NULL;
3659 domain_sid = get_global_sam_sid();
3661 domain_sid_copy = dom_sid_dup(tframe, domain_sid);
3662 if (domain_sid_copy == NULL) {
3663 PyErr_NoMemory();
3664 talloc_free(tframe);
3665 return NULL;
3668 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3670 talloc_free(tframe);
3672 return py_dom_sid;
3676 static PyMethodDef py_passdb_methods[] = {
3677 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3678 "get_backends() -> list\n\n \
3679 Get a list of password database backends supported." },
3680 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3681 "set_smb_config(path) -> None\n\n \
3682 Set path to smb.conf file to load configuration parameters." },
3683 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3684 "set_secrets_dir(private_dir) -> None\n\n \
3685 Set path to private directory to load secrets database from non-default location." },
3686 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3687 "get_global_sam_sid() -> dom_sid\n\n \
3688 Return domain SID." },
3689 { NULL },
3692 void initpassdb(void)
3694 PyObject *m, *mod;
3695 char exception_name[] = "passdb.error";
3697 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3698 if (talloc_type == NULL) {
3699 return;
3702 PyPDB.tp_base = talloc_type;
3703 if (PyType_Ready(&PyPDB) < 0) {
3704 return;
3707 PySamu.tp_base = talloc_type;
3708 if (PyType_Ready(&PySamu) < 0) {
3709 return;
3712 PyGroupmap.tp_base = talloc_type;
3713 if (PyType_Ready(&PyGroupmap) < 0) {
3714 return;
3717 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3718 if (m == NULL) {
3719 return;
3722 /* Create new exception for passdb module */
3723 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3724 Py_INCREF(py_pdb_error);
3725 PyModule_AddObject(m, "error", py_pdb_error);
3727 Py_INCREF(&PyPDB);
3728 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3730 Py_INCREF(&PySamu);
3731 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3733 Py_INCREF(&PyGroupmap);
3734 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3736 /* Import dom_sid type from dcerpc.security */
3737 mod = PyImport_ImportModule("samba.dcerpc.security");
3738 if (mod == NULL) {
3739 return;
3742 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3743 if (dom_sid_Type == NULL) {
3744 return;
3747 /* Import security_descriptor type from dcerpc.security */
3748 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3749 Py_DECREF(mod);
3750 if (security_Type == NULL) {
3751 return;
3754 /* Import GUID type from dcerpc.misc */
3755 mod = PyImport_ImportModule("samba.dcerpc.misc");
3756 if (mod == NULL) {
3757 return;
3760 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3761 Py_DECREF(mod);
3762 if (guid_Type == NULL) {
3763 return;