Change _talloc_total_mem_internal() to ignore memory allocated from a pool when calcu...
[Samba.git] / source3 / passdb / py_passdb.c
blobe1df75be45b51b63acc5ab4fa2d4a89166fb66ab
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 "librpc/gen_ndr/idmap.h"
26 #include "passdb.h"
27 #include "secrets.h"
29 /* There's no Py_ssize_t in 2.4, apparently */
30 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
31 typedef int Py_ssize_t;
32 typedef inquiry lenfunc;
33 typedef intargfunc ssizeargfunc;
34 #endif
36 #ifndef Py_RETURN_NONE
37 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
38 #endif
40 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
41 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
42 #endif
44 #ifndef PY_CHECK_TYPE
45 #define PY_CHECK_TYPE(type, var, fail) \
46 if (!PyObject_TypeCheck(var, type)) {\
47 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
48 fail; \
50 #endif
53 static PyTypeObject *dom_sid_Type = NULL;
54 static PyTypeObject *security_Type = NULL;
55 static PyTypeObject *guid_Type = NULL;
57 staticforward PyTypeObject PySamu;
58 staticforward PyTypeObject PyGroupmap;
59 staticforward PyTypeObject PyPDB;
61 static PyObject *py_pdb_error;
63 void initpassdb(void);
66 /************************** PIDL Autogeneratd ******************************/
68 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
70 TALLOC_CTX *frame = talloc_stackframe();
71 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
72 PyObject *py_logon_time;
74 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
75 talloc_free(frame);
76 return py_logon_time;
79 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
81 TALLOC_CTX *frame = talloc_stackframe();
82 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
84 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
85 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
86 talloc_free(frame);
87 return -1;
89 talloc_free(frame);
90 return 0;
93 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
95 TALLOC_CTX *frame = talloc_stackframe();
96 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
97 PyObject *py_logoff_time;
99 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
100 talloc_free(frame);
101 return py_logoff_time;
104 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
106 TALLOC_CTX *frame = talloc_stackframe();
107 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
109 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
110 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
111 talloc_free(frame);
112 return -1;
114 talloc_free(frame);
115 return 0;
118 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
120 TALLOC_CTX *frame = talloc_stackframe();
121 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
122 PyObject *py_kickoff_time;
124 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
125 talloc_free(frame);
126 return py_kickoff_time;
129 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
131 TALLOC_CTX *frame = talloc_stackframe();
132 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
134 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
135 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
136 talloc_free(frame);
137 return -1;
139 talloc_free(frame);
140 return 0;
143 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
145 TALLOC_CTX *frame = talloc_stackframe();
146 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
147 PyObject *py_bad_password_time;
149 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
150 talloc_free(frame);
151 return py_bad_password_time;
154 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
156 TALLOC_CTX *frame = talloc_stackframe();
157 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
159 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
160 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
161 talloc_free(frame);
162 return -1;
164 talloc_free(frame);
165 return 0;
168 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
170 TALLOC_CTX *frame = talloc_stackframe();
171 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
172 PyObject *py_pass_last_set_time;
174 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
175 talloc_free(frame);
176 return py_pass_last_set_time;
179 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
181 TALLOC_CTX *frame = talloc_stackframe();
182 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
184 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
185 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
186 talloc_free(frame);
187 return -1;
189 talloc_free(frame);
190 return 0;
193 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
195 TALLOC_CTX *frame = talloc_stackframe();
196 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
197 PyObject *py_pass_can_change_time;
199 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
200 talloc_free(frame);
201 return py_pass_can_change_time;
204 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
206 TALLOC_CTX *frame = talloc_stackframe();
207 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
209 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
210 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
211 talloc_free(frame);
212 return -1;
214 talloc_free(frame);
215 return 0;
218 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
220 TALLOC_CTX *frame = talloc_stackframe();
221 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
222 PyObject *py_pass_must_change_time;
224 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
225 talloc_free(frame);
226 return py_pass_must_change_time;
229 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
231 TALLOC_CTX *frame = talloc_stackframe();
232 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
234 /* TODO: make this not a get/set or give a better exception */
235 talloc_free(frame);
236 return -1;
239 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
241 TALLOC_CTX *frame = talloc_stackframe();
242 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
243 PyObject *py_username;
244 const char *username;
246 username = pdb_get_username(sam_acct);
247 if (username == NULL) {
248 Py_RETURN_NONE;
251 py_username = PyString_FromString(username);
252 talloc_free(frame);
253 return py_username;
256 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
258 TALLOC_CTX *frame = talloc_stackframe();
259 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
261 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
262 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
263 talloc_free(frame);
264 return -1;
266 talloc_free(frame);
267 return 0;
270 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
272 TALLOC_CTX *frame = talloc_stackframe();
273 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
274 PyObject *py_domain;
275 const char *domain;
277 domain = pdb_get_domain(sam_acct);
278 if (domain == NULL) {
279 Py_RETURN_NONE;
282 py_domain = PyString_FromString(domain);
283 talloc_free(frame);
284 return py_domain;
287 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
289 TALLOC_CTX *frame = talloc_stackframe();
290 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
292 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
293 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
294 talloc_free(frame);
295 return -1;
297 talloc_free(frame);
298 return 0;
301 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
303 TALLOC_CTX *frame = talloc_stackframe();
304 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
305 PyObject *py_nt_username;
306 const char *nt_username;
308 nt_username = pdb_get_nt_username(sam_acct);
309 if (nt_username == NULL) {
310 Py_RETURN_NONE;
313 py_nt_username = PyString_FromString(nt_username);
314 talloc_free(frame);
315 return py_nt_username;
318 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
320 TALLOC_CTX *frame = talloc_stackframe();
321 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
323 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
324 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
325 talloc_free(frame);
326 return -1;
328 talloc_free(frame);
329 return 0;
332 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
334 TALLOC_CTX *frame = talloc_stackframe();
335 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
336 PyObject *py_full_name;
337 const char *full_name;
339 full_name = pdb_get_fullname(sam_acct);
340 if (full_name == NULL) {
341 Py_RETURN_NONE;
344 py_full_name = PyString_FromString(full_name);
345 talloc_free(frame);
346 return py_full_name;
349 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
351 TALLOC_CTX *frame = talloc_stackframe();
352 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
354 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
355 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
356 talloc_free(frame);
357 return -1;
359 talloc_free(frame);
360 return 0;
363 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
365 TALLOC_CTX *frame = talloc_stackframe();
366 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
367 PyObject *py_home_dir;
368 const char *home_dir;
370 home_dir = pdb_get_homedir(sam_acct);
371 if (home_dir == NULL) {
372 Py_RETURN_NONE;
375 py_home_dir = PyString_FromString(home_dir);
376 talloc_free(frame);
377 return py_home_dir;
380 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
382 TALLOC_CTX *frame = talloc_stackframe();
383 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
385 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
386 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
387 talloc_free(frame);
388 return -1;
390 talloc_free(frame);
391 return 0;
394 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
396 TALLOC_CTX *frame = talloc_stackframe();
397 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
398 PyObject *py_dir_drive;
399 const char *dir_drive;
401 dir_drive = pdb_get_dir_drive(sam_acct);
402 if (dir_drive == NULL) {
403 Py_RETURN_NONE;
406 py_dir_drive = PyString_FromString(dir_drive);
407 talloc_free(frame);
408 return py_dir_drive;
411 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
413 TALLOC_CTX *frame = talloc_stackframe();
414 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
416 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
417 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
418 talloc_free(frame);
419 return -1;
421 talloc_free(frame);
422 return 0;
425 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
427 TALLOC_CTX *frame = talloc_stackframe();
428 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
429 PyObject *py_logon_script;
430 const char *logon_script;
432 logon_script = pdb_get_logon_script(sam_acct);
433 if (logon_script == NULL) {
434 Py_RETURN_NONE;
437 py_logon_script = PyString_FromString(logon_script);
438 talloc_free(frame);
439 return py_logon_script;
442 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
444 TALLOC_CTX *frame = talloc_stackframe();
445 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
447 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
448 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
449 talloc_free(frame);
450 return -1;
452 talloc_free(frame);
453 return 0;
456 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
458 TALLOC_CTX *frame = talloc_stackframe();
459 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
460 PyObject *py_profile_path;
461 const char *profile_path;
463 profile_path = pdb_get_profile_path(sam_acct);
464 if (profile_path == NULL) {
465 Py_RETURN_NONE;
468 py_profile_path = PyString_FromString(profile_path);
469 talloc_free(frame);
470 return py_profile_path;
473 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
475 TALLOC_CTX *frame = talloc_stackframe();
476 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
478 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
479 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
480 talloc_free(frame);
481 return -1;
483 talloc_free(frame);
484 return 0;
487 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
489 TALLOC_CTX *frame = talloc_stackframe();
490 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
491 PyObject *py_acct_desc;
492 const char *acct_desc;
494 acct_desc = pdb_get_acct_desc(sam_acct);
495 if (acct_desc == NULL) {
496 Py_RETURN_NONE;
499 py_acct_desc = PyString_FromString(acct_desc);
500 talloc_free(frame);
501 return py_acct_desc;
504 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
506 TALLOC_CTX *frame = talloc_stackframe();
507 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
509 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
510 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
511 talloc_free(frame);
512 return -1;
514 talloc_free(frame);
515 return 0;
518 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
520 TALLOC_CTX *frame = talloc_stackframe();
521 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
522 PyObject *py_workstations;
523 const char *workstations;
525 workstations = pdb_get_workstations(sam_acct);
526 if (workstations == NULL) {
527 Py_RETURN_NONE;
530 py_workstations = PyString_FromString(workstations);
531 talloc_free(frame);
532 return py_workstations;
535 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
537 TALLOC_CTX *frame = talloc_stackframe();
538 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
540 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
541 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
542 talloc_free(frame);
543 return -1;
545 talloc_free(frame);
546 return 0;
549 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
553 PyObject *py_comment;
554 const char *comment;
556 comment = pdb_get_comment(sam_acct);
557 if (comment == NULL) {
558 Py_RETURN_NONE;
561 py_comment = PyString_FromString(comment);
562 talloc_free(frame);
563 return py_comment;
566 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
568 TALLOC_CTX *frame = talloc_stackframe();
569 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
571 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
572 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
573 talloc_free(frame);
574 return -1;
576 talloc_free(frame);
577 return 0;
580 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
582 TALLOC_CTX *frame = talloc_stackframe();
583 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
584 PyObject *py_munged_dial;
585 const char *munged_dial;
587 munged_dial = pdb_get_munged_dial(sam_acct);
588 if (munged_dial == NULL) {
589 Py_RETURN_NONE;
592 py_munged_dial = PyString_FromString(munged_dial);
593 talloc_free(frame);
594 return py_munged_dial;
597 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
599 TALLOC_CTX *frame = talloc_stackframe();
600 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
602 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
603 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
604 talloc_free(frame);
605 return -1;
607 talloc_free(frame);
608 return 0;
611 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
613 TALLOC_CTX *frame = talloc_stackframe();
614 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
615 PyObject *py_user_sid;
616 const struct dom_sid *user_sid;
617 struct dom_sid *copy_user_sid;
618 TALLOC_CTX *mem_ctx;
620 user_sid = pdb_get_user_sid(sam_acct);
621 if(user_sid == NULL) {
622 Py_RETURN_NONE;
625 mem_ctx = talloc_new(NULL);
626 if (mem_ctx == NULL) {
627 PyErr_NoMemory();
628 talloc_free(frame);
629 return NULL;
631 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
632 if (copy_user_sid == NULL) {
633 PyErr_NoMemory();
634 talloc_free(mem_ctx);
635 talloc_free(frame);
636 return NULL;
639 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
641 talloc_free(mem_ctx);
643 talloc_free(frame);
644 return py_user_sid;
647 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
649 TALLOC_CTX *frame = talloc_stackframe();
650 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
652 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
653 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
654 talloc_free(frame);
655 return -1;
657 talloc_free(frame);
658 return 0;
661 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
663 TALLOC_CTX *frame = talloc_stackframe();
664 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
665 const struct dom_sid *group_sid;
666 struct dom_sid *copy_group_sid;
668 group_sid = pdb_get_group_sid(sam_acct);
669 if (group_sid == NULL) {
670 Py_RETURN_NONE;
673 copy_group_sid = dom_sid_dup(NULL, group_sid);
674 if (copy_group_sid == NULL) {
675 PyErr_NoMemory();
676 talloc_free(frame);
677 return NULL;
680 talloc_free(frame);
681 return pytalloc_steal(dom_sid_Type, copy_group_sid);
684 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
686 TALLOC_CTX *frame = talloc_stackframe();
687 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
689 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
690 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
691 talloc_free(frame);
692 return -1;
694 talloc_free(frame);
695 return 0;
698 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
700 TALLOC_CTX *frame = talloc_stackframe();
701 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
702 PyObject *py_lm_pw;
703 const char *lm_pw;
705 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
706 if (lm_pw == NULL) {
707 Py_RETURN_NONE;
710 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
711 talloc_free(frame);
712 return py_lm_pw;
715 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
717 TALLOC_CTX *frame = talloc_stackframe();
718 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
720 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
721 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
722 talloc_free(frame);
723 return -1;
725 talloc_free(frame);
726 return 0;
729 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
731 TALLOC_CTX *frame = talloc_stackframe();
732 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
733 PyObject *py_nt_pw;
734 const char *nt_pw;
736 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
737 if (nt_pw == NULL) {
738 Py_RETURN_NONE;
741 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
742 talloc_free(frame);
743 return py_nt_pw;
746 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
748 TALLOC_CTX *frame = talloc_stackframe();
749 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
751 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
752 talloc_free(frame);
753 return -1;
755 talloc_free(frame);
756 return 0;
759 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
761 TALLOC_CTX *frame = talloc_stackframe();
762 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
763 PyObject *py_nt_pw_his;
764 const char *nt_pw_his;
765 uint32_t hist_len;
767 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
768 if (nt_pw_his == NULL) {
769 Py_RETURN_NONE;
772 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
773 talloc_free(frame);
774 return py_nt_pw_his;
777 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
779 TALLOC_CTX *frame = talloc_stackframe();
780 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
781 char *nt_pw_his;
782 Py_ssize_t len;
783 uint32_t hist_len;
785 PyString_AsStringAndSize(value, &nt_pw_his, &len);
786 hist_len = len / PW_HISTORY_ENTRY_LEN;
787 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
788 talloc_free(frame);
789 return -1;
791 talloc_free(frame);
792 return 0;
795 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
797 TALLOC_CTX *frame = talloc_stackframe();
798 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
799 PyObject *py_plaintext_pw;
800 const char *plaintext_pw;
802 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
803 if (plaintext_pw == NULL) {
804 Py_RETURN_NONE;
807 py_plaintext_pw = PyString_FromString(plaintext_pw);
808 talloc_free(frame);
809 return py_plaintext_pw;
812 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
814 TALLOC_CTX *frame = talloc_stackframe();
815 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
817 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
818 talloc_free(frame);
819 return -1;
821 talloc_free(frame);
822 return 0;
825 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
827 TALLOC_CTX *frame = talloc_stackframe();
828 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
829 PyObject *py_acct_ctrl;
831 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
832 talloc_free(frame);
833 return py_acct_ctrl;
836 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
838 TALLOC_CTX *frame = talloc_stackframe();
839 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
841 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
842 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
843 talloc_free(frame);
844 return -1;
846 talloc_free(frame);
847 return 0;
850 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
852 TALLOC_CTX *frame = talloc_stackframe();
853 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
854 PyObject *py_logon_divs;
856 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
857 talloc_free(frame);
858 return py_logon_divs;
861 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
863 TALLOC_CTX *frame = talloc_stackframe();
864 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
866 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
867 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
868 talloc_free(frame);
869 return -1;
871 talloc_free(frame);
872 return 0;
875 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
877 TALLOC_CTX *frame = talloc_stackframe();
878 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
879 PyObject *py_hours_len;
881 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
882 talloc_free(frame);
883 return py_hours_len;
886 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
888 TALLOC_CTX *frame = talloc_stackframe();
889 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
891 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
892 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
893 talloc_free(frame);
894 return -1;
896 talloc_free(frame);
897 return 0;
900 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
902 TALLOC_CTX *frame = talloc_stackframe();
903 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
904 PyObject *py_hours;
905 const char *hours;
906 int hours_len, i;
908 hours = (const char *)pdb_get_hours(sam_acct);
909 if(! hours) {
910 Py_RETURN_NONE;
913 hours_len = pdb_get_hours_len(sam_acct);
914 if ((py_hours = PyList_New(hours_len)) == NULL) {
915 PyErr_NoMemory();
916 talloc_free(frame);
917 return NULL;
920 for (i=0; i<hours_len; i++) {
921 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
923 talloc_free(frame);
924 return py_hours;
927 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
929 TALLOC_CTX *frame = talloc_stackframe();
930 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
931 int i;
932 uint8_t *hours;
933 int hours_len;
934 bool status;
936 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
938 hours_len = PyList_GET_SIZE(value);
940 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
941 if (!hours) {
942 PyErr_NoMemory();
943 talloc_free(frame);
944 return -1;
947 for (i=0; i < hours_len; i++) {
948 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
949 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
952 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
953 talloc_free(hours);
955 if(! status) {
956 talloc_free(frame);
957 return -1;
959 talloc_free(frame);
960 return 0;
963 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
965 TALLOC_CTX *frame = talloc_stackframe();
966 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
967 PyObject *py_bad_password_count;
969 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
970 talloc_free(frame);
971 return py_bad_password_count;
974 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
976 TALLOC_CTX *frame = talloc_stackframe();
977 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
979 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
980 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
981 talloc_free(frame);
982 return -1;
984 talloc_free(frame);
985 return 0;
988 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
990 TALLOC_CTX *frame = talloc_stackframe();
991 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
992 PyObject *py_logon_count;
994 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
995 talloc_free(frame);
996 return py_logon_count;
999 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
1001 TALLOC_CTX *frame = talloc_stackframe();
1002 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1004 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1005 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1006 talloc_free(frame);
1007 return -1;
1009 talloc_free(frame);
1010 return 0;
1013 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
1015 TALLOC_CTX *frame = talloc_stackframe();
1016 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1017 PyObject *py_country_code;
1019 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
1020 talloc_free(frame);
1021 return py_country_code;
1024 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
1026 TALLOC_CTX *frame = talloc_stackframe();
1027 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1029 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1030 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1031 talloc_free(frame);
1032 return -1;
1034 talloc_free(frame);
1035 return 0;
1038 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
1040 TALLOC_CTX *frame = talloc_stackframe();
1041 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1042 PyObject *py_code_page;
1044 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
1045 talloc_free(frame);
1046 return py_code_page;
1049 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
1051 TALLOC_CTX *frame = talloc_stackframe();
1052 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1054 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1055 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1056 talloc_free(frame);
1057 return -1;
1059 talloc_free(frame);
1060 return 0;
1063 static PyGetSetDef py_samu_getsetters[] = {
1064 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
1065 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
1066 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
1067 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
1068 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
1069 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
1070 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
1071 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
1072 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
1073 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
1074 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
1075 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
1076 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
1077 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
1078 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
1079 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
1080 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
1081 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
1082 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
1083 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
1084 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
1085 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
1086 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
1087 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
1088 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
1089 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
1090 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
1091 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
1092 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
1093 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
1094 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
1095 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
1096 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
1097 { NULL }
1101 /************************** PIDL Autogeneratd ******************************/
1103 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1105 TALLOC_CTX *frame = talloc_stackframe();
1106 struct samu *sam_acct;
1108 sam_acct = samu_new(NULL);
1109 if (!sam_acct) {
1110 PyErr_NoMemory();
1111 talloc_free(frame);
1112 return NULL;
1115 talloc_free(frame);
1116 return pytalloc_steal(type, sam_acct);
1119 static PyTypeObject PySamu = {
1120 .tp_name = "passdb.Samu",
1121 .tp_basicsize = sizeof(pytalloc_Object),
1122 .tp_getset = py_samu_getsetters,
1123 .tp_methods = NULL,
1124 .tp_new = py_samu_new,
1125 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1126 .tp_doc = "Samu() -> samu object\n",
1130 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1132 TALLOC_CTX *frame = talloc_stackframe();
1133 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1134 PyObject *py_gid;
1136 py_gid = Py_BuildValue("i", group_map->gid);
1137 talloc_free(frame);
1138 return py_gid;
1141 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1143 TALLOC_CTX *frame = talloc_stackframe();
1144 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1146 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1147 group_map->gid = PyInt_AsLong(value);
1148 talloc_free(frame);
1149 return 0;
1152 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1154 TALLOC_CTX *frame = talloc_stackframe();
1155 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1156 PyObject *py_sid;
1157 struct dom_sid *group_sid;
1158 TALLOC_CTX *mem_ctx;
1160 mem_ctx = talloc_new(NULL);
1161 if (mem_ctx == NULL) {
1162 PyErr_NoMemory();
1163 talloc_free(frame);
1164 return NULL;
1167 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1168 if (group_sid == NULL) {
1169 PyErr_NoMemory();
1170 talloc_free(mem_ctx);
1171 talloc_free(frame);
1172 return NULL;
1175 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1177 talloc_free(mem_ctx);
1179 talloc_free(frame);
1180 return py_sid;
1183 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1185 TALLOC_CTX *frame = talloc_stackframe();
1186 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1188 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1189 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1190 talloc_free(frame);
1191 return 0;
1194 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1196 TALLOC_CTX *frame = talloc_stackframe();
1197 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1198 PyObject *py_sid_name_use;
1200 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1201 talloc_free(frame);
1202 return py_sid_name_use;
1205 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1207 TALLOC_CTX *frame = talloc_stackframe();
1208 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1210 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1211 group_map->sid_name_use = PyInt_AsLong(value);
1212 talloc_free(frame);
1213 return 0;
1216 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1218 TALLOC_CTX *frame = talloc_stackframe();
1219 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1220 PyObject *py_nt_name;
1221 if (group_map->nt_name == NULL) {
1222 py_nt_name = Py_None;
1223 Py_INCREF(py_nt_name);
1224 } else {
1225 py_nt_name = PyString_FromString(group_map->nt_name);
1227 talloc_free(frame);
1228 return py_nt_name;
1231 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1233 TALLOC_CTX *frame = talloc_stackframe();
1234 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1236 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1237 if (value == Py_None) {
1238 fstrcpy(group_map->nt_name, NULL);
1239 } else {
1240 fstrcpy(group_map->nt_name, PyString_AsString(value));
1242 talloc_free(frame);
1243 return 0;
1246 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1248 TALLOC_CTX *frame = talloc_stackframe();
1249 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1250 PyObject *py_comment;
1251 if (group_map->comment == NULL) {
1252 py_comment = Py_None;
1253 Py_INCREF(py_comment);
1254 } else {
1255 py_comment = PyString_FromString(group_map->comment);
1257 talloc_free(frame);
1258 return py_comment;
1261 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1263 TALLOC_CTX *frame = talloc_stackframe();
1264 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1266 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1267 if (value == Py_None) {
1268 fstrcpy(group_map->comment, NULL);
1269 } else {
1270 fstrcpy(group_map->comment, PyString_AsString(value));
1272 talloc_free(frame);
1273 return 0;
1276 static PyGetSetDef py_groupmap_getsetters[] = {
1277 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1278 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1279 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1280 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1281 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1282 { NULL }
1285 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1287 TALLOC_CTX *frame = talloc_stackframe();
1288 GROUP_MAP *group_map;
1289 TALLOC_CTX *mem_ctx;
1290 PyObject *py_group_map;
1292 mem_ctx = talloc_new(NULL);
1293 if (mem_ctx == NULL) {
1294 PyErr_NoMemory();
1295 talloc_free(frame);
1296 return NULL;
1299 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1300 if (group_map == NULL) {
1301 PyErr_NoMemory();
1302 talloc_free(mem_ctx);
1303 talloc_free(frame);
1304 return NULL;
1307 py_group_map = pytalloc_steal(type, group_map);
1308 if (py_group_map == NULL) {
1309 PyErr_NoMemory();
1310 talloc_free(mem_ctx);
1311 talloc_free(frame);
1312 return NULL;
1315 talloc_free(mem_ctx);
1317 talloc_free(frame);
1318 return py_group_map;
1322 static PyTypeObject PyGroupmap = {
1323 .tp_name = "passdb.Groupmap",
1324 .tp_basicsize = sizeof(pytalloc_Object),
1325 .tp_getset = py_groupmap_getsetters,
1326 .tp_methods = NULL,
1327 .tp_new = py_groupmap_new,
1328 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1329 .tp_doc = "Groupmap() -> group map object\n",
1333 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1335 TALLOC_CTX *frame = talloc_stackframe();
1336 struct pdb_methods *methods;
1337 struct pdb_domain_info *domain_info;
1338 PyObject *py_domain_info;
1339 struct dom_sid *sid;
1340 struct GUID *guid;
1342 methods = pytalloc_get_ptr(self);
1344 domain_info = methods->get_domain_info(methods, frame);
1345 if (! domain_info) {
1346 Py_RETURN_NONE;
1349 sid = dom_sid_dup(frame, &domain_info->sid);
1350 if (sid == NULL) {
1351 PyErr_NoMemory();
1352 talloc_free(frame);
1353 return NULL;
1356 guid = talloc(frame, struct GUID);
1357 if (guid == NULL) {
1358 PyErr_NoMemory();
1359 talloc_free(frame);
1360 return NULL;
1362 *guid = domain_info->guid;
1364 if ((py_domain_info = PyDict_New()) == NULL) {
1365 PyErr_NoMemory();
1366 talloc_free(frame);
1367 return NULL;
1370 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1371 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->dns_domain));
1372 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->dns_forest));
1373 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1374 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1376 talloc_free(frame);
1377 return py_domain_info;
1381 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1383 TALLOC_CTX *frame = talloc_stackframe();
1384 NTSTATUS status;
1385 const char *username;
1386 struct pdb_methods *methods;
1387 struct samu *sam_acct;
1388 PyObject *py_sam_acct;
1390 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1391 talloc_free(frame);
1392 return NULL;
1395 methods = pytalloc_get_ptr(self);
1397 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1398 if (py_sam_acct == NULL) {
1399 PyErr_NoMemory();
1400 talloc_free(frame);
1401 return NULL;
1403 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1405 status = methods->getsampwnam(methods, sam_acct, username);
1406 if (!NT_STATUS_IS_OK(status)) {
1407 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1408 username,
1409 NT_STATUS_V(status),
1410 get_friendly_nt_error_msg(status));
1411 Py_DECREF(py_sam_acct);
1412 talloc_free(frame);
1413 return NULL;
1416 talloc_free(frame);
1417 return py_sam_acct;
1420 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1422 TALLOC_CTX *frame = talloc_stackframe();
1423 NTSTATUS status;
1424 struct pdb_methods *methods;
1425 struct samu *sam_acct;
1426 PyObject *py_sam_acct;
1427 PyObject *py_user_sid;
1429 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1430 talloc_free(frame);
1431 return NULL;
1434 methods = pytalloc_get_ptr(self);
1436 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1437 if (py_sam_acct == NULL) {
1438 PyErr_NoMemory();
1439 talloc_free(frame);
1440 return NULL;
1442 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1444 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1445 if (!NT_STATUS_IS_OK(status)) {
1446 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1447 NT_STATUS_V(status),
1448 get_friendly_nt_error_msg(status));
1449 Py_DECREF(py_sam_acct);
1450 talloc_free(frame);
1451 return NULL;
1454 talloc_free(frame);
1455 return py_sam_acct;
1458 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1460 TALLOC_CTX *frame = talloc_stackframe();
1461 NTSTATUS status;
1462 struct pdb_methods *methods;
1463 const char *username;
1464 unsigned int acct_flags;
1465 unsigned int rid;
1467 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1468 talloc_free(frame);
1469 return NULL;
1472 methods = pytalloc_get_ptr(self);
1474 status = methods->create_user(methods, frame, username, acct_flags, &rid);
1475 if (!NT_STATUS_IS_OK(status)) {
1476 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1477 username,
1478 NT_STATUS_V(status),
1479 get_friendly_nt_error_msg(status));
1480 talloc_free(frame);
1481 return NULL;
1484 talloc_free(frame);
1485 return PyInt_FromLong(rid);
1488 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1490 TALLOC_CTX *frame = talloc_stackframe();
1491 NTSTATUS status;
1492 struct pdb_methods *methods;
1493 struct samu *sam_acct;
1494 PyObject *py_sam_acct;
1496 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1497 talloc_free(frame);
1498 return NULL;
1501 methods = pytalloc_get_ptr(self);
1503 sam_acct = pytalloc_get_ptr(py_sam_acct);
1505 status = methods->delete_user(methods, frame, sam_acct);
1506 if (!NT_STATUS_IS_OK(status)) {
1507 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1508 NT_STATUS_V(status),
1509 get_friendly_nt_error_msg(status));
1510 talloc_free(frame);
1511 return NULL;
1514 Py_RETURN_NONE;
1515 talloc_free(frame);
1518 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1520 TALLOC_CTX *frame = talloc_stackframe();
1521 NTSTATUS status;
1522 struct pdb_methods *methods;
1523 struct samu *sam_acct;
1524 PyObject *py_sam_acct;
1526 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1527 talloc_free(frame);
1528 return NULL;
1531 methods = pytalloc_get_ptr(self);
1533 sam_acct = pytalloc_get_ptr(py_sam_acct);
1535 status = methods->add_sam_account(methods, sam_acct);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1538 sam_acct->username,
1539 NT_STATUS_V(status),
1540 get_friendly_nt_error_msg(status));
1541 talloc_free(frame);
1542 return NULL;
1545 Py_RETURN_NONE;
1546 talloc_free(frame);
1549 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1551 TALLOC_CTX *frame = talloc_stackframe();
1552 NTSTATUS status;
1553 struct pdb_methods *methods;
1554 struct samu *sam_acct;
1555 PyObject *py_sam_acct;
1557 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1558 talloc_free(frame);
1559 return NULL;
1562 methods = pytalloc_get_ptr(self);
1564 sam_acct = pytalloc_get_ptr(py_sam_acct);
1566 status = methods->update_sam_account(methods, sam_acct);
1567 if (!NT_STATUS_IS_OK(status)) {
1568 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1569 NT_STATUS_V(status),
1570 get_friendly_nt_error_msg(status));
1571 talloc_free(frame);
1572 return NULL;
1575 Py_RETURN_NONE;
1576 talloc_free(frame);
1579 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1581 TALLOC_CTX *frame = talloc_stackframe();
1582 NTSTATUS status;
1583 struct pdb_methods *methods;
1584 struct samu *sam_acct;
1585 PyObject *py_sam_acct;
1587 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1588 talloc_free(frame);
1589 return NULL;
1592 methods = pytalloc_get_ptr(self);
1594 sam_acct = pytalloc_get_ptr(py_sam_acct);
1596 status = methods->delete_sam_account(methods, sam_acct);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1599 NT_STATUS_V(status),
1600 get_friendly_nt_error_msg(status));
1601 talloc_free(frame);
1602 return NULL;
1605 Py_RETURN_NONE;
1606 talloc_free(frame);
1609 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1611 TALLOC_CTX *frame = talloc_stackframe();
1612 NTSTATUS status;
1613 struct pdb_methods *methods;
1614 struct samu *sam_acct;
1615 const char *new_username;
1616 PyObject *py_sam_acct;
1618 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1619 &new_username)) {
1620 talloc_free(frame);
1621 return NULL;
1624 methods = pytalloc_get_ptr(self);
1626 sam_acct = pytalloc_get_ptr(py_sam_acct);
1628 status = methods->rename_sam_account(methods, sam_acct, new_username);
1629 if (!NT_STATUS_IS_OK(status)) {
1630 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1631 NT_STATUS_V(status),
1632 get_friendly_nt_error_msg(status));
1633 talloc_free(frame);
1634 return NULL;
1637 Py_RETURN_NONE;
1638 talloc_free(frame);
1642 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1644 TALLOC_CTX *frame = talloc_stackframe();
1645 NTSTATUS status;
1646 struct pdb_methods *methods;
1647 GROUP_MAP *group_map;
1648 struct dom_sid *domain_sid;
1649 PyObject *py_domain_sid, *py_group_map;
1651 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1652 talloc_free(frame);
1653 return NULL;
1656 methods = pytalloc_get_ptr(self);
1658 domain_sid = pytalloc_get_ptr(py_domain_sid);
1660 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1661 if (py_group_map == NULL) {
1662 PyErr_NoMemory();
1663 talloc_free(frame);
1664 return NULL;
1667 group_map = pytalloc_get_ptr(py_group_map);
1669 status = methods->getgrsid(methods, group_map, *domain_sid);
1670 if (!NT_STATUS_IS_OK(status)) {
1671 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1672 NT_STATUS_V(status),
1673 get_friendly_nt_error_msg(status));
1674 talloc_free(frame);
1675 return NULL;
1678 talloc_free(frame);
1679 return py_group_map;
1683 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1685 TALLOC_CTX *frame = talloc_stackframe();
1686 NTSTATUS status;
1687 struct pdb_methods *methods;
1688 GROUP_MAP *group_map;
1689 PyObject *py_group_map;
1690 unsigned int gid_value;
1692 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1693 talloc_free(frame);
1694 return NULL;
1697 methods = pytalloc_get_ptr(self);
1699 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1700 if (py_group_map == NULL) {
1701 PyErr_NoMemory();
1702 talloc_free(frame);
1703 return NULL;
1706 group_map = pytalloc_get_ptr(py_group_map);
1708 status = methods->getgrgid(methods, group_map, gid_value);
1709 if (!NT_STATUS_IS_OK(status)) {
1710 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1711 NT_STATUS_V(status),
1712 get_friendly_nt_error_msg(status));
1713 talloc_free(frame);
1714 return NULL;
1717 talloc_free(frame);
1718 return py_group_map;
1722 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1724 TALLOC_CTX *frame = talloc_stackframe();
1725 NTSTATUS status;
1726 struct pdb_methods *methods;
1727 GROUP_MAP *group_map;
1728 PyObject *py_group_map;
1729 const char *groupname;
1731 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1732 talloc_free(frame);
1733 return NULL;
1736 methods = pytalloc_get_ptr(self);
1738 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1739 if (py_group_map == NULL) {
1740 PyErr_NoMemory();
1741 talloc_free(frame);
1742 return NULL;
1745 group_map = pytalloc_get_ptr(py_group_map);
1747 status = methods->getgrnam(methods, group_map, groupname);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1750 NT_STATUS_V(status),
1751 get_friendly_nt_error_msg(status));
1752 talloc_free(frame);
1753 return NULL;
1756 talloc_free(frame);
1757 return py_group_map;
1761 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1763 TALLOC_CTX *frame = talloc_stackframe();
1764 NTSTATUS status;
1765 struct pdb_methods *methods;
1766 const char *groupname;
1767 uint32_t group_rid;
1769 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1770 talloc_free(frame);
1771 return NULL;
1774 methods = pytalloc_get_ptr(self);
1776 status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1779 groupname,
1780 NT_STATUS_V(status),
1781 get_friendly_nt_error_msg(status));
1782 talloc_free(frame);
1783 return NULL;
1786 talloc_free(frame);
1787 return PyInt_FromLong(group_rid);
1791 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1793 TALLOC_CTX *frame = talloc_stackframe();
1794 NTSTATUS status;
1795 struct pdb_methods *methods;
1796 unsigned int group_rid;
1798 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1799 talloc_free(frame);
1800 return NULL;
1803 methods = pytalloc_get_ptr(self);
1805 status = methods->delete_dom_group(methods, frame, group_rid);
1806 if (!NT_STATUS_IS_OK(status)) {
1807 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1808 group_rid,
1809 NT_STATUS_V(status),
1810 get_friendly_nt_error_msg(status));
1811 talloc_free(frame);
1812 return NULL;
1815 Py_RETURN_NONE;
1816 talloc_free(frame);
1820 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1822 TALLOC_CTX *frame = talloc_stackframe();
1823 NTSTATUS status;
1824 struct pdb_methods *methods;
1825 PyObject *py_group_map;
1826 GROUP_MAP *group_map;
1828 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1829 talloc_free(frame);
1830 return NULL;
1833 methods = pytalloc_get_ptr(self);
1835 group_map = pytalloc_get_ptr(py_group_map);
1837 status = methods->add_group_mapping_entry(methods, group_map);
1838 if (!NT_STATUS_IS_OK(status)) {
1839 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1840 NT_STATUS_V(status),
1841 get_friendly_nt_error_msg(status));
1842 talloc_free(frame);
1843 return NULL;
1846 Py_RETURN_NONE;
1847 talloc_free(frame);
1851 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1853 TALLOC_CTX *frame = talloc_stackframe();
1854 NTSTATUS status;
1855 struct pdb_methods *methods;
1856 PyObject *py_group_map;
1857 GROUP_MAP *group_map;
1859 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1860 talloc_free(frame);
1861 return NULL;
1864 methods = pytalloc_get_ptr(self);
1866 group_map = pytalloc_get_ptr(py_group_map);
1868 status = methods->update_group_mapping_entry(methods, group_map);
1869 if (!NT_STATUS_IS_OK(status)) {
1870 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1871 NT_STATUS_V(status),
1872 get_friendly_nt_error_msg(status));
1873 talloc_free(frame);
1874 return NULL;
1877 Py_RETURN_NONE;
1878 talloc_free(frame);
1882 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1884 TALLOC_CTX *frame = talloc_stackframe();
1885 NTSTATUS status;
1886 struct pdb_methods *methods;
1887 PyObject *py_group_sid;
1888 struct dom_sid *group_sid;
1890 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1891 talloc_free(frame);
1892 return NULL;
1895 methods = pytalloc_get_ptr(self);
1897 group_sid = pytalloc_get_ptr(py_group_sid);
1899 status = methods->delete_group_mapping_entry(methods, *group_sid);
1900 if (!NT_STATUS_IS_OK(status)) {
1901 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1902 NT_STATUS_V(status),
1903 get_friendly_nt_error_msg(status));
1904 talloc_free(frame);
1905 return NULL;
1908 Py_RETURN_NONE;
1909 talloc_free(frame);
1913 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1915 TALLOC_CTX *frame = talloc_stackframe();
1916 NTSTATUS status;
1917 struct pdb_methods *methods;
1918 enum lsa_SidType sid_name_use;
1919 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1920 int unix_only = 0;
1921 PyObject *py_domain_sid;
1922 struct dom_sid *domain_sid = NULL;
1923 GROUP_MAP **gmap = NULL;
1924 GROUP_MAP *group_map;
1925 size_t num_entries;
1926 PyObject *py_gmap_list, *py_group_map;
1927 int i;
1929 py_domain_sid = Py_None;
1930 Py_INCREF(Py_None);
1932 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1933 &lsa_sidtype_value, &unix_only)) {
1934 talloc_free(frame);
1935 return NULL;
1938 methods = pytalloc_get_ptr(self);
1940 sid_name_use = lsa_sidtype_value;
1942 if (py_domain_sid != Py_None) {
1943 domain_sid = pytalloc_get_ptr(py_domain_sid);
1946 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1947 &gmap, &num_entries, unix_only);
1948 if (!NT_STATUS_IS_OK(status)) {
1949 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1950 NT_STATUS_V(status),
1951 get_friendly_nt_error_msg(status));
1952 talloc_free(frame);
1953 return NULL;
1956 py_gmap_list = PyList_New(0);
1957 if (py_gmap_list == NULL) {
1958 PyErr_NoMemory();
1959 talloc_free(frame);
1960 return NULL;
1963 for(i=0; i<num_entries; i++) {
1964 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1965 if (py_group_map) {
1966 group_map = pytalloc_get_ptr(py_group_map);
1967 *group_map = *gmap[i];
1968 talloc_steal(group_map, gmap[i]->nt_name);
1969 talloc_steal(group_map, gmap[i]->comment);
1971 PyList_Append(py_gmap_list, py_group_map);
1975 talloc_free(gmap);
1977 talloc_free(frame);
1978 return py_gmap_list;
1982 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1984 TALLOC_CTX *frame = talloc_stackframe();
1985 NTSTATUS status;
1986 struct pdb_methods *methods;
1987 PyObject *py_group_sid;
1988 struct dom_sid *group_sid;
1989 uint32_t *member_rids;
1990 size_t num_members;
1991 PyObject *py_sid_list;
1992 struct dom_sid *domain_sid, *member_sid;
1993 int i;
1995 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1996 talloc_free(frame);
1997 return NULL;
2000 methods = pytalloc_get_ptr(self);
2002 group_sid = pytalloc_get_ptr(py_group_sid);
2004 status = methods->enum_group_members(methods, frame, group_sid,
2005 &member_rids, &num_members);
2006 if (!NT_STATUS_IS_OK(status)) {
2007 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
2008 NT_STATUS_V(status),
2009 get_friendly_nt_error_msg(status));
2010 talloc_free(frame);
2011 return NULL;
2014 py_sid_list = PyList_New(0);
2015 if (py_sid_list == NULL) {
2016 PyErr_NoMemory();
2017 talloc_free(frame);
2018 return NULL;
2021 domain_sid = get_global_sam_sid();
2023 for(i=0; i<num_members; i++) {
2024 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2025 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
2028 talloc_free(frame);
2029 return py_sid_list;
2033 static PyObject *py_pdb_enum_group_memberships(pytalloc_Object *self, PyObject *args)
2035 TALLOC_CTX *frame = talloc_stackframe();
2036 NTSTATUS status;
2037 struct pdb_methods *methods;
2038 int i;
2040 struct samu *sam_acct;
2041 PyObject *py_sam_acct;
2042 PyObject *py_sid_list;
2043 struct dom_sid *user_group_sids = NULL;
2044 gid_t *user_group_ids = NULL;
2045 uint32_t num_groups = 0;
2047 if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2048 talloc_free(frame);
2049 return NULL;
2052 methods = pytalloc_get_ptr(self);
2054 sam_acct = pytalloc_get_ptr(py_sam_acct);
2056 status = methods->enum_group_memberships(methods, frame, sam_acct,
2057 &user_group_sids, &user_group_ids, &num_groups);
2058 if (!NT_STATUS_IS_OK(status)) {
2059 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2060 NT_STATUS_V(status),
2061 get_friendly_nt_error_msg(status));
2062 talloc_free(frame);
2063 return NULL;
2066 py_sid_list = PyList_New(0);
2067 if (py_sid_list == NULL) {
2068 PyErr_NoMemory();
2069 talloc_free(frame);
2070 return NULL;
2073 for(i=0; i<num_groups; i++) {
2074 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
2077 talloc_free(frame);
2078 return py_sid_list;
2082 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
2084 TALLOC_CTX *frame = talloc_stackframe();
2085 NTSTATUS status;
2086 struct pdb_methods *methods;
2087 uint32_t group_rid, member_rid;
2089 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2090 talloc_free(frame);
2091 return NULL;
2094 methods = pytalloc_get_ptr(self);
2096 status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2097 if (!NT_STATUS_IS_OK(status)) {
2098 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2099 NT_STATUS_V(status),
2100 get_friendly_nt_error_msg(status));
2101 talloc_free(frame);
2102 return NULL;
2105 Py_RETURN_NONE;
2106 talloc_free(frame);
2110 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
2112 TALLOC_CTX *frame = talloc_stackframe();
2113 NTSTATUS status;
2114 struct pdb_methods *methods;
2115 uint32_t group_rid, member_rid;
2117 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2118 talloc_free(frame);
2119 return NULL;
2122 methods = pytalloc_get_ptr(self);
2124 status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2125 if (!NT_STATUS_IS_OK(status)) {
2126 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2127 NT_STATUS_V(status),
2128 get_friendly_nt_error_msg(status));
2129 talloc_free(frame);
2130 return NULL;
2133 Py_RETURN_NONE;
2134 talloc_free(frame);
2138 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
2140 TALLOC_CTX *frame = talloc_stackframe();
2141 NTSTATUS status;
2142 struct pdb_methods *methods;
2143 const char *alias_name;
2144 uint32_t rid;
2146 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2147 talloc_free(frame);
2148 return NULL;
2151 methods = pytalloc_get_ptr(self);
2153 status = methods->create_alias(methods, alias_name, &rid);
2154 if (!NT_STATUS_IS_OK(status)) {
2155 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2156 alias_name,
2157 NT_STATUS_V(status),
2158 get_friendly_nt_error_msg(status));
2159 talloc_free(frame);
2160 return NULL;
2163 talloc_free(frame);
2164 return PyInt_FromLong(rid);
2168 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2170 TALLOC_CTX *frame = talloc_stackframe();
2171 NTSTATUS status;
2172 struct pdb_methods *methods;
2173 PyObject *py_alias_sid;
2174 struct dom_sid *alias_sid;
2176 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2177 talloc_free(frame);
2178 return NULL;
2181 methods = pytalloc_get_ptr(self);
2183 alias_sid = pytalloc_get_ptr(py_alias_sid);
2185 status = methods->delete_alias(methods, alias_sid);
2186 if (!NT_STATUS_IS_OK(status)) {
2187 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2188 NT_STATUS_V(status),
2189 get_friendly_nt_error_msg(status));
2190 talloc_free(frame);
2191 return NULL;
2194 Py_RETURN_NONE;
2195 talloc_free(frame);
2199 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2201 TALLOC_CTX *frame = talloc_stackframe();
2202 NTSTATUS status;
2203 struct pdb_methods *methods;
2204 PyObject *py_alias_sid;
2205 struct dom_sid *alias_sid;
2206 struct acct_info *alias_info;
2207 PyObject *py_alias_info;
2209 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2210 talloc_free(frame);
2211 return NULL;
2214 methods = pytalloc_get_ptr(self);
2216 alias_sid = pytalloc_get_ptr(py_alias_sid);
2218 alias_info = talloc_zero(frame, struct acct_info);
2219 if (!alias_info) {
2220 PyErr_NoMemory();
2221 talloc_free(frame);
2222 return NULL;
2225 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2226 if (!NT_STATUS_IS_OK(status)) {
2227 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2228 NT_STATUS_V(status),
2229 get_friendly_nt_error_msg(status));
2230 talloc_free(frame);
2231 return NULL;
2234 py_alias_info = PyDict_New();
2235 if (py_alias_info == NULL) {
2236 PyErr_NoMemory();
2237 talloc_free(frame);
2238 return NULL;
2241 PyDict_SetItemString(py_alias_info, "acct_name",
2242 PyString_FromString(alias_info->acct_name));
2243 PyDict_SetItemString(py_alias_info, "acct_desc",
2244 PyString_FromString(alias_info->acct_desc));
2245 PyDict_SetItemString(py_alias_info, "rid",
2246 PyInt_FromLong(alias_info->rid));
2248 talloc_free(frame);
2249 return py_alias_info;
2253 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2255 TALLOC_CTX *frame = talloc_stackframe();
2256 NTSTATUS status;
2257 struct pdb_methods *methods;
2258 PyObject *py_alias_sid, *py_alias_info;
2259 struct dom_sid *alias_sid;
2260 struct acct_info alias_info;
2262 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2263 &py_alias_info)) {
2264 talloc_free(frame);
2265 return NULL;
2268 methods = pytalloc_get_ptr(self);
2270 alias_sid = pytalloc_get_ptr(py_alias_sid);
2272 alias_info.acct_name = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2273 if (alias_info.acct_name == NULL) {
2274 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2275 talloc_free(frame);
2276 return NULL;
2278 alias_info.acct_desc = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2279 if (alias_info.acct_desc == NULL) {
2280 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2281 talloc_free(frame);
2282 return NULL;
2285 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2286 if (!NT_STATUS_IS_OK(status)) {
2287 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2288 NT_STATUS_V(status),
2289 get_friendly_nt_error_msg(status));
2290 talloc_free(frame);
2291 return NULL;
2294 Py_RETURN_NONE;
2295 talloc_free(frame);
2299 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2301 TALLOC_CTX *frame = talloc_stackframe();
2302 NTSTATUS status;
2303 struct pdb_methods *methods;
2304 PyObject *py_alias_sid, *py_member_sid;
2305 struct dom_sid *alias_sid, *member_sid;
2307 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2308 dom_sid_Type, &py_member_sid)) {
2309 talloc_free(frame);
2310 return NULL;
2313 methods = pytalloc_get_ptr(self);
2315 alias_sid = pytalloc_get_ptr(py_alias_sid);
2316 member_sid = pytalloc_get_ptr(py_member_sid);
2318 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2319 if (!NT_STATUS_IS_OK(status)) {
2320 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2321 NT_STATUS_V(status),
2322 get_friendly_nt_error_msg(status));
2323 talloc_free(frame);
2324 return NULL;
2327 Py_RETURN_NONE;
2328 talloc_free(frame);
2332 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2334 TALLOC_CTX *frame = talloc_stackframe();
2335 NTSTATUS status;
2336 struct pdb_methods *methods;
2337 PyObject *py_alias_sid, *py_member_sid;
2338 const struct dom_sid *alias_sid, *member_sid;
2340 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2341 dom_sid_Type, &py_member_sid)) {
2342 talloc_free(frame);
2343 return NULL;
2346 methods = pytalloc_get_ptr(self);
2348 alias_sid = pytalloc_get_ptr(py_alias_sid);
2349 member_sid = pytalloc_get_ptr(py_member_sid);
2351 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2352 if (!NT_STATUS_IS_OK(status)) {
2353 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2354 NT_STATUS_V(status),
2355 get_friendly_nt_error_msg(status));
2356 talloc_free(frame);
2357 return NULL;
2360 Py_RETURN_NONE;
2361 talloc_free(frame);
2365 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2367 TALLOC_CTX *frame = talloc_stackframe();
2368 NTSTATUS status;
2369 struct pdb_methods *methods;
2370 PyObject *py_alias_sid;
2371 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2372 PyObject *py_member_list, *py_member_sid;
2373 size_t num_members;
2374 int i;
2376 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2377 talloc_free(frame);
2378 return NULL;
2381 methods = pytalloc_get_ptr(self);
2383 alias_sid = pytalloc_get_ptr(py_alias_sid);
2385 status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2386 if (!NT_STATUS_IS_OK(status)) {
2387 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2388 NT_STATUS_V(status),
2389 get_friendly_nt_error_msg(status));
2390 talloc_free(frame);
2391 return NULL;
2394 py_member_list = PyList_New(0);
2395 if (py_member_list == NULL) {
2396 PyErr_NoMemory();
2397 talloc_free(frame);
2398 return NULL;
2401 for(i=0; i<num_members; i++) {
2402 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2403 if (py_member_sid == NULL) {
2404 PyErr_NoMemory();
2405 talloc_free(frame);
2406 return NULL;
2408 tmp_sid = pytalloc_get_ptr(py_member_sid);
2409 *tmp_sid = member_sid[i];
2410 PyList_Append(py_member_list, py_member_sid);
2413 talloc_free(frame);
2414 return py_member_list;
2418 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2420 TALLOC_CTX *frame = talloc_stackframe();
2421 NTSTATUS status;
2422 struct pdb_methods *methods;
2423 PyObject *py_acct_policy;
2424 uint32_t value;
2425 const char **names;
2426 int count, i;
2427 enum pdb_policy_type type;
2429 methods = pytalloc_get_ptr(self);
2431 py_acct_policy = PyDict_New();
2432 if (py_acct_policy == NULL) {
2433 PyErr_NoMemory();
2434 talloc_free(frame);
2435 return NULL;
2438 account_policy_names_list(frame, &names, &count);
2439 for (i=0; i<count; i++) {
2440 type = account_policy_name_to_typenum(names[i]);
2441 status = methods->get_account_policy(methods, type, &value);
2442 if (NT_STATUS_IS_OK(status)) {
2443 PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2447 talloc_free(frame);
2448 return py_acct_policy;
2452 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2454 TALLOC_CTX *frame = talloc_stackframe();
2455 NTSTATUS status;
2456 struct pdb_methods *methods;
2457 PyObject *py_acct_policy, *py_value;
2458 const char **names;
2459 int count, i;
2460 enum pdb_policy_type type;
2462 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2463 talloc_free(frame);
2464 return NULL;
2467 methods = pytalloc_get_ptr(self);
2469 account_policy_names_list(frame, &names, &count);
2470 for (i=0; i<count; i++) {
2471 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2472 type = account_policy_name_to_typenum(names[i]);
2473 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2474 if (!NT_STATUS_IS_OK(status)) {
2475 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2476 names[i],
2477 NT_STATUS_V(status),
2478 get_friendly_nt_error_msg(status));
2483 Py_RETURN_NONE;
2484 talloc_free(frame);
2487 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2489 TALLOC_CTX *frame = talloc_stackframe();
2490 NTSTATUS status;
2491 struct pdb_methods *methods;
2492 unsigned int acct_flags;
2493 struct pdb_search *search;
2494 struct samr_displayentry *entry;
2495 PyObject *py_userlist, *py_dict;
2497 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2498 talloc_free(frame);
2499 return NULL;
2502 methods = pytalloc_get_ptr(self);
2504 search = talloc_zero(frame, struct pdb_search);
2505 if (search == NULL) {
2506 PyErr_NoMemory();
2507 talloc_free(frame);
2508 return NULL;
2511 if (!methods->search_users(methods, search, acct_flags)) {
2512 PyErr_Format(py_pdb_error, "Unable to search users, (%d,%s)",
2513 NT_STATUS_V(status),
2514 get_friendly_nt_error_msg(status));
2515 talloc_free(frame);
2516 return NULL;
2519 entry = talloc_zero(frame, struct samr_displayentry);
2520 if (entry == NULL) {
2521 PyErr_NoMemory();
2522 talloc_free(frame);
2523 return NULL;
2526 py_userlist = PyList_New(0);
2527 if (py_userlist == NULL) {
2528 PyErr_NoMemory();
2529 talloc_free(frame);
2530 return NULL;
2533 while (search->next_entry(search, entry)) {
2534 py_dict = PyDict_New();
2535 if (py_dict == NULL) {
2536 PyErr_NoMemory();
2537 } else {
2538 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2539 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2540 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2541 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2542 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2543 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2544 PyList_Append(py_userlist, py_dict);
2547 search->search_end(search);
2549 talloc_free(frame);
2550 return py_userlist;
2554 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2556 TALLOC_CTX *frame = talloc_stackframe();
2557 NTSTATUS status;
2558 struct pdb_methods *methods;
2559 struct pdb_search *search;
2560 struct samr_displayentry *entry;
2561 PyObject *py_grouplist, *py_dict;
2563 methods = pytalloc_get_ptr(self);
2565 search = talloc_zero(frame, struct pdb_search);
2566 if (search == NULL) {
2567 PyErr_NoMemory();
2568 talloc_free(frame);
2569 return NULL;
2572 if (!methods->search_groups(methods, search)) {
2573 PyErr_Format(py_pdb_error, "Unable to search groups, (%d,%s)",
2574 NT_STATUS_V(status),
2575 get_friendly_nt_error_msg(status));
2576 talloc_free(frame);
2577 return NULL;
2580 entry = talloc_zero(frame, struct samr_displayentry);
2581 if (entry == NULL) {
2582 PyErr_NoMemory();
2583 talloc_free(frame);
2584 return NULL;
2587 py_grouplist = PyList_New(0);
2588 if (py_grouplist == NULL) {
2589 PyErr_NoMemory();
2590 talloc_free(frame);
2591 return NULL;
2594 while (search->next_entry(search, entry)) {
2595 py_dict = PyDict_New();
2596 if (py_dict == NULL) {
2597 PyErr_NoMemory();
2598 } else {
2599 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2600 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2601 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2602 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2603 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2604 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2605 PyList_Append(py_grouplist, py_dict);
2608 search->search_end(search);
2610 talloc_free(frame);
2611 return py_grouplist;
2615 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2617 TALLOC_CTX *frame = talloc_stackframe();
2618 struct pdb_methods *methods;
2619 struct pdb_search *search;
2620 struct samr_displayentry *entry;
2621 PyObject *py_aliaslist, *py_dict;
2622 PyObject *py_domain_sid;
2623 struct dom_sid *domain_sid = NULL;
2625 py_domain_sid = Py_None;
2626 Py_INCREF(Py_None);
2628 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2629 talloc_free(frame);
2630 return NULL;
2633 methods = pytalloc_get_ptr(self);
2635 if (py_domain_sid != Py_None) {
2636 domain_sid = pytalloc_get_ptr(py_domain_sid);
2639 search = talloc_zero(frame, struct pdb_search);
2640 if (search == NULL) {
2641 PyErr_NoMemory();
2642 talloc_free(frame);
2643 return NULL;
2646 if (!methods->search_aliases(methods, search, domain_sid)) {
2647 PyErr_Format(py_pdb_error, "Unable to search aliases");
2648 talloc_free(frame);
2649 return NULL;
2652 entry = talloc_zero(frame, struct samr_displayentry);
2653 if (entry == NULL) {
2654 PyErr_NoMemory();
2655 talloc_free(frame);
2656 return NULL;
2659 py_aliaslist = PyList_New(0);
2660 if (py_aliaslist == NULL) {
2661 PyErr_NoMemory();
2662 talloc_free(frame);
2663 return NULL;
2666 while (search->next_entry(search, entry)) {
2667 py_dict = PyDict_New();
2668 if (py_dict == NULL) {
2669 PyErr_NoMemory();
2670 } else {
2671 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2672 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2673 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2674 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2675 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2676 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2677 PyList_Append(py_aliaslist, py_dict);
2680 search->search_end(search);
2682 talloc_free(frame);
2683 return py_aliaslist;
2687 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2689 TALLOC_CTX *frame = talloc_stackframe();
2690 struct pdb_methods *methods;
2691 unsigned int uid;
2692 struct dom_sid user_sid, *copy_user_sid;
2693 PyObject *py_user_sid;
2695 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2696 talloc_free(frame);
2697 return NULL;
2700 methods = pytalloc_get_ptr(self);
2702 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2703 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2704 talloc_free(frame);
2705 return NULL;
2708 copy_user_sid = dom_sid_dup(frame, &user_sid);
2709 if (copy_user_sid == NULL) {
2710 PyErr_NoMemory();
2711 talloc_free(frame);
2712 return NULL;
2715 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2717 talloc_free(frame);
2718 return py_user_sid;
2722 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2724 TALLOC_CTX *frame = talloc_stackframe();
2725 struct pdb_methods *methods;
2726 unsigned int gid;
2727 struct dom_sid group_sid, *copy_group_sid;
2728 PyObject *py_group_sid;
2730 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2731 talloc_free(frame);
2732 return NULL;
2735 methods = pytalloc_get_ptr(self);
2737 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2738 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2739 talloc_free(frame);
2740 return NULL;
2743 copy_group_sid = dom_sid_dup(frame, &group_sid);
2744 if (copy_group_sid == NULL) {
2745 PyErr_NoMemory();
2746 talloc_free(frame);
2747 return NULL;
2750 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2752 talloc_free(frame);
2753 return py_group_sid;
2757 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2759 TALLOC_CTX *frame = talloc_stackframe();
2760 struct pdb_methods *methods;
2761 PyObject *py_sid;
2762 struct dom_sid *sid;
2763 struct unixid id;
2765 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2766 talloc_free(frame);
2767 return NULL;
2770 methods = pytalloc_get_ptr(self);
2772 sid = pytalloc_get_ptr(py_sid);
2774 if (!methods->sid_to_id(methods, sid, &id)) {
2775 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2776 talloc_free(frame);
2777 return NULL;
2780 talloc_free(frame);
2781 return Py_BuildValue("(II)", id.id, id.type);
2785 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2787 TALLOC_CTX *frame = talloc_stackframe();
2788 struct pdb_methods *methods;
2789 uint32_t rid;
2791 methods = pytalloc_get_ptr(self);
2793 if (!methods->new_rid(methods, &rid)) {
2794 PyErr_Format(py_pdb_error, "Unable to get new rid");
2795 talloc_free(frame);
2796 return NULL;
2799 talloc_free(frame);
2800 return PyInt_FromLong(rid);
2804 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2806 TALLOC_CTX *frame = talloc_stackframe();
2807 struct pdb_methods *methods;
2808 const char *domain;
2809 char *pwd;
2810 struct dom_sid sid, *copy_sid;
2811 PyObject *py_sid;
2812 time_t last_set_time;
2813 PyObject *py_value;
2815 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2816 talloc_free(frame);
2817 return NULL;
2820 methods = pytalloc_get_ptr(self);
2822 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2823 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2824 talloc_free(frame);
2825 return NULL;
2828 copy_sid = dom_sid_dup(frame, &sid);
2829 if (copy_sid == NULL) {
2830 PyErr_NoMemory();
2831 talloc_free(frame);
2832 return NULL;
2835 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2836 if (py_sid == NULL) {
2837 PyErr_NoMemory();
2838 talloc_free(frame);
2839 return NULL;
2842 py_value = PyDict_New();
2843 if (py_value == NULL) {
2844 PyErr_NoMemory();
2845 talloc_free(frame);
2846 return NULL;
2849 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2850 PyDict_SetItemString(py_value, "sid", py_sid);
2851 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2853 talloc_free(frame);
2854 return py_value;
2858 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2860 TALLOC_CTX *frame = talloc_stackframe();
2861 struct pdb_methods *methods;
2862 const char *domain;
2863 const char *pwd;
2864 const struct dom_sid *domain_sid;
2865 PyObject *py_domain_sid;
2867 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2868 dom_sid_Type, &py_domain_sid)) {
2869 talloc_free(frame);
2870 return NULL;
2873 methods = pytalloc_get_ptr(self);
2875 domain_sid = pytalloc_get_ptr(py_domain_sid);
2877 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2878 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2879 talloc_free(frame);
2880 return NULL;
2883 Py_RETURN_NONE;
2884 talloc_free(frame);
2888 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2890 TALLOC_CTX *frame = talloc_stackframe();
2891 struct pdb_methods *methods;
2892 const char *domain;
2894 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2895 talloc_free(frame);
2896 return NULL;
2899 methods = pytalloc_get_ptr(self);
2901 if (!methods->del_trusteddom_pw(methods, domain)) {
2902 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2903 talloc_free(frame);
2904 return NULL;
2907 Py_RETURN_NONE;
2908 talloc_free(frame);
2912 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2914 TALLOC_CTX *frame = talloc_stackframe();
2915 NTSTATUS status;
2916 struct pdb_methods *methods;
2917 uint32_t num_domains;
2918 struct trustdom_info **domains;
2919 PyObject *py_domain_list, *py_dict;
2920 int i;
2922 methods = pytalloc_get_ptr(self);
2924 status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
2925 if (!NT_STATUS_IS_OK(status)) {
2926 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2927 NT_STATUS_V(status),
2928 get_friendly_nt_error_msg(status));
2929 talloc_free(frame);
2930 return NULL;
2933 py_domain_list = PyList_New(0);
2934 if (py_domain_list == NULL) {
2935 PyErr_NoMemory();
2936 talloc_free(frame);
2937 return NULL;
2940 for(i=0; i<num_domains; i++) {
2941 py_dict = PyDict_New();
2942 if (py_dict) {
2943 PyDict_SetItemString(py_dict, "name",
2944 PyString_FromString(domains[i]->name));
2945 PyDict_SetItemString(py_dict, "sid",
2946 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2949 PyList_Append(py_domain_list, py_dict);
2952 talloc_free(frame);
2953 return py_domain_list;
2957 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2959 TALLOC_CTX *frame = talloc_stackframe();
2960 NTSTATUS status;
2961 struct pdb_methods *methods;
2962 const char *domain;
2963 struct pdb_trusted_domain *td;
2964 PyObject *py_domain_info;
2966 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2967 talloc_free(frame);
2968 return NULL;
2971 methods = pytalloc_get_ptr(self);
2973 status = methods->get_trusted_domain(methods, frame, domain, &td);
2974 if (!NT_STATUS_IS_OK(status)) {
2975 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2976 NT_STATUS_V(status),
2977 get_friendly_nt_error_msg(status));
2978 talloc_free(frame);
2979 return NULL;
2982 py_domain_info = PyDict_New();
2983 if (py_domain_info == NULL) {
2984 PyErr_NoMemory();
2985 talloc_free(frame);
2986 return NULL;
2989 PyDict_SetItemString(py_domain_info, "domain_name",
2990 PyString_FromString(td->domain_name));
2991 PyDict_SetItemString(py_domain_info, "netbios_name",
2992 PyString_FromString(td->netbios_name));
2993 PyDict_SetItemString(py_domain_info, "security_identifier",
2994 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2995 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2996 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2997 td->trust_auth_incoming.length));
2998 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2999 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3000 td->trust_auth_outgoing.length));
3001 PyDict_SetItemString(py_domain_info, "trust_direction",
3002 PyInt_FromLong(td->trust_direction));
3003 PyDict_SetItemString(py_domain_info, "trust_type",
3004 PyInt_FromLong(td->trust_type));
3005 PyDict_SetItemString(py_domain_info, "trust_attributes",
3006 PyInt_FromLong(td->trust_attributes));
3007 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3008 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3009 td->trust_forest_trust_info.length));
3011 talloc_free(frame);
3012 return py_domain_info;
3016 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
3018 TALLOC_CTX *frame = talloc_stackframe();
3019 NTSTATUS status;
3020 struct pdb_methods *methods;
3021 PyObject *py_domain_sid;
3022 struct dom_sid *domain_sid;
3023 struct pdb_trusted_domain *td;
3024 PyObject *py_domain_info;
3026 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3027 talloc_free(frame);
3028 return NULL;
3031 methods = pytalloc_get_ptr(self);
3033 domain_sid = pytalloc_get_ptr(py_domain_sid);
3035 status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3036 if (!NT_STATUS_IS_OK(status)) {
3037 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3038 NT_STATUS_V(status),
3039 get_friendly_nt_error_msg(status));
3040 talloc_free(frame);
3041 return NULL;
3044 py_domain_info = PyDict_New();
3045 if (py_domain_info == NULL) {
3046 PyErr_NoMemory();
3047 talloc_free(frame);
3048 return NULL;
3051 PyDict_SetItemString(py_domain_info, "domain_name",
3052 PyString_FromString(td->domain_name));
3053 PyDict_SetItemString(py_domain_info, "netbios_name",
3054 PyString_FromString(td->netbios_name));
3055 PyDict_SetItemString(py_domain_info, "security_identifier",
3056 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3057 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3058 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3059 td->trust_auth_incoming.length));
3060 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3061 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3062 td->trust_auth_outgoing.length));
3063 PyDict_SetItemString(py_domain_info, "trust_direction",
3064 PyInt_FromLong(td->trust_direction));
3065 PyDict_SetItemString(py_domain_info, "trust_type",
3066 PyInt_FromLong(td->trust_type));
3067 PyDict_SetItemString(py_domain_info, "trust_attributes",
3068 PyInt_FromLong(td->trust_attributes));
3069 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3070 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3071 td->trust_forest_trust_info.length));
3073 talloc_free(frame);
3074 return py_domain_info;
3078 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3080 TALLOC_CTX *frame = talloc_stackframe();
3081 NTSTATUS status;
3082 struct pdb_methods *methods;
3083 const char *domain;
3084 PyObject *py_td_info;
3085 struct pdb_trusted_domain td_info;
3086 PyObject *py_tmp;
3087 Py_ssize_t len;
3089 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3090 talloc_free(frame);
3091 return NULL;
3094 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3095 td_info.domain_name = PyString_AsString(py_tmp);
3097 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3098 td_info.netbios_name = PyString_AsString(py_tmp);
3100 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3101 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3103 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3104 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3105 td_info.trust_auth_incoming.length = len;
3107 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3108 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3109 td_info.trust_auth_outgoing.length = len;
3111 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3112 td_info.trust_direction = PyInt_AsLong(py_tmp);
3114 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3115 td_info.trust_type = PyInt_AsLong(py_tmp);
3117 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3118 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3120 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3121 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3122 td_info.trust_forest_trust_info.length = len;
3124 methods = pytalloc_get_ptr(self);
3126 status = methods->set_trusted_domain(methods, domain, &td_info);
3127 if (!NT_STATUS_IS_OK(status)) {
3128 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3129 NT_STATUS_V(status),
3130 get_friendly_nt_error_msg(status));
3131 talloc_free(frame);
3132 return NULL;
3135 Py_RETURN_NONE;
3136 talloc_free(frame);
3140 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3142 TALLOC_CTX *frame = talloc_stackframe();
3143 NTSTATUS status;
3144 struct pdb_methods *methods;
3145 const char *domain;
3147 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3148 talloc_free(frame);
3149 return NULL;
3152 methods = pytalloc_get_ptr(self);
3154 status = methods->del_trusted_domain(methods, domain);
3155 if (!NT_STATUS_IS_OK(status)) {
3156 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3157 NT_STATUS_V(status),
3158 get_friendly_nt_error_msg(status));
3159 talloc_free(frame);
3160 return NULL;
3163 Py_RETURN_NONE;
3164 talloc_free(frame);
3168 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3170 TALLOC_CTX *frame = talloc_stackframe();
3171 NTSTATUS status;
3172 struct pdb_methods *methods;
3173 uint32_t num_domains;
3174 struct pdb_trusted_domain **td_info, *td;
3175 PyObject *py_td_info, *py_domain_info;
3176 int i;
3178 methods = pytalloc_get_ptr(self);
3180 status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3181 if (!NT_STATUS_IS_OK(status)) {
3182 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3183 NT_STATUS_V(status),
3184 get_friendly_nt_error_msg(status));
3185 talloc_free(frame);
3186 return NULL;
3189 py_td_info = PyList_New(0);
3190 if (py_td_info == NULL) {
3191 PyErr_NoMemory();
3192 talloc_free(frame);
3193 return NULL;
3196 for (i=0; i<num_domains; i++) {
3198 py_domain_info = PyDict_New();
3199 if (py_domain_info == NULL) {
3200 PyErr_NoMemory();
3201 Py_DECREF(py_td_info);
3202 talloc_free(frame);
3203 return NULL;
3206 td = td_info[i];
3208 PyDict_SetItemString(py_domain_info, "domain_name",
3209 PyString_FromString(td->domain_name));
3210 PyDict_SetItemString(py_domain_info, "netbios_name",
3211 PyString_FromString(td->netbios_name));
3212 PyDict_SetItemString(py_domain_info, "security_identifier",
3213 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3214 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3215 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3216 td->trust_auth_incoming.length));
3217 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3218 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3219 td->trust_auth_outgoing.length));
3220 PyDict_SetItemString(py_domain_info, "trust_direction",
3221 PyInt_FromLong(td->trust_direction));
3222 PyDict_SetItemString(py_domain_info, "trust_type",
3223 PyInt_FromLong(td->trust_type));
3224 PyDict_SetItemString(py_domain_info, "trust_attributes",
3225 PyInt_FromLong(td->trust_attributes));
3226 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3227 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3228 td->trust_forest_trust_info.length));
3229 PyList_Append(py_td_info, py_domain_info);
3232 talloc_free(frame);
3233 return py_td_info;
3237 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3239 TALLOC_CTX *frame = talloc_stackframe();
3240 NTSTATUS status;
3241 struct pdb_methods *methods;
3242 const char *secret_name;
3243 DATA_BLOB secret_current, secret_old;
3244 NTTIME secret_current_lastchange, secret_old_lastchange;
3245 PyObject *py_sd;
3246 struct security_descriptor *sd;
3247 PyObject *py_secret;
3249 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3250 talloc_free(frame);
3251 return NULL;
3254 methods = pytalloc_get_ptr(self);
3256 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3257 if (py_sd == NULL) {
3258 PyErr_NoMemory();
3259 talloc_free(frame);
3260 return NULL;
3262 sd = pytalloc_get_ptr(py_sd);
3264 status = methods->get_secret(methods, frame, secret_name,
3265 &secret_current,
3266 &secret_current_lastchange,
3267 &secret_old,
3268 &secret_old_lastchange,
3269 &sd);
3270 if (!NT_STATUS_IS_OK(status)) {
3271 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3272 secret_name,
3273 NT_STATUS_V(status),
3274 get_friendly_nt_error_msg(status));
3275 talloc_free(frame);
3276 return NULL;
3279 py_secret = PyDict_New();
3280 if (py_secret == NULL) {
3281 PyErr_NoMemory();
3282 Py_DECREF(py_sd);
3283 talloc_free(frame);
3284 return NULL;
3287 PyDict_SetItemString(py_secret, "secret_current",
3288 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3289 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3290 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3291 PyDict_SetItemString(py_secret, "secret_old",
3292 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3293 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3294 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3295 PyDict_SetItemString(py_secret, "sd", py_sd);
3297 talloc_free(frame);
3298 return py_secret;
3302 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3304 TALLOC_CTX *frame = talloc_stackframe();
3305 NTSTATUS status;
3306 struct pdb_methods *methods;
3307 const char *secret_name;
3308 PyObject *py_secret;
3309 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3310 DATA_BLOB secret_current, secret_old;
3311 struct security_descriptor *sd;
3312 Py_ssize_t len;
3314 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3315 talloc_free(frame);
3316 return NULL;
3319 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3320 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3321 py_sd = PyDict_GetItemString(py_secret, "sd");
3323 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3324 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3325 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3327 methods = pytalloc_get_ptr(self);
3329 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3330 secret_current.length = len;
3331 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3332 secret_current.length = len;
3333 sd = pytalloc_get_ptr(py_sd);
3335 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3336 if (!NT_STATUS_IS_OK(status)) {
3337 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3338 secret_name,
3339 NT_STATUS_V(status),
3340 get_friendly_nt_error_msg(status));
3341 talloc_free(frame);
3342 return NULL;
3345 Py_RETURN_NONE;
3346 talloc_free(frame);
3350 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3352 TALLOC_CTX *frame = talloc_stackframe();
3353 NTSTATUS status;
3354 struct pdb_methods *methods;
3355 const char *secret_name;
3357 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3358 talloc_free(frame);
3359 return NULL;
3362 methods = pytalloc_get_ptr(self);
3364 status = methods->delete_secret(methods, secret_name);
3365 if (!NT_STATUS_IS_OK(status)) {
3366 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3367 secret_name,
3368 NT_STATUS_V(status),
3369 get_friendly_nt_error_msg(status));
3370 talloc_free(frame);
3371 return NULL;
3374 Py_RETURN_NONE;
3375 talloc_free(frame);
3378 static PyMethodDef py_pdb_methods[] = {
3379 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3380 "domain_info() -> str\n\n \
3381 Get domain information for the database." },
3382 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3383 "getsampwnam(username) -> samu object\n\n \
3384 Get user information by name." },
3385 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3386 "getsampwsid(user_sid) -> samu object\n\n \
3387 Get user information by sid (dcerpc.security.dom_sid object)." },
3388 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3389 "create_user(username, acct_flags) -> rid\n\n \
3390 Create user. acct_flags are samr account control flags." },
3391 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3392 "delete_user(samu object) -> None\n\n \
3393 Delete user." },
3394 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3395 "add_sam_account(samu object) -> None\n\n \
3396 Add SAM account." },
3397 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3398 "update_sam_account(samu object) -> None\n\n \
3399 Update SAM account." },
3400 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3401 "delete_sam_account(samu object) -> None\n\n \
3402 Delete SAM account." },
3403 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3404 "rename_sam_account(samu object1, new_username) -> None\n\n \
3405 Rename SAM account." },
3406 /* update_login_attempts */
3407 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3408 "getgrsid(group_sid) -> groupmap object\n\n \
3409 Get group information by sid (dcerpc.security.dom_sid object)." },
3410 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3411 "getgrsid(gid) -> groupmap object\n\n \
3412 Get group information by gid." },
3413 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3414 "getgrsid(groupname) -> groupmap object\n\n \
3415 Get group information by name." },
3416 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3417 "create_dom_group(groupname) -> group_rid\n\n \
3418 Create new domain group by name." },
3419 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3420 "delete_dom_group(group_rid) -> None\n\n \
3421 Delete domain group identified by rid" },
3422 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3423 "add_group_mapping_entry(groupmap) -> None\n \
3424 Add group mapping entry for groupmap object." },
3425 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3426 "update_group_mapping_entry(groupmap) -> None\n\n \
3427 Update group mapping entry for groupmap object." },
3428 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3429 "delete_group_mapping_entry(groupmap) -> None\n\n \
3430 Delete group mapping entry for groupmap object." },
3431 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3432 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3433 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3434 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3435 "enum_group_members(group_sid) -> List\n\n \
3436 Return list of users (dom_sid object) in group." },
3437 { "enum_group_memberships", (PyCFunction)py_pdb_enum_group_memberships, METH_VARARGS,
3438 "enum_group_memberships(samu object) -> List\n\n \
3439 Return list of groups (dom_sid object) this user is part of." },
3440 /* set_unix_primary_group */
3441 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3442 "add_groupmem(group_rid, member_rid) -> None\n\n \
3443 Add user to group." },
3444 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3445 "del_groupmem(group_rid, member_rid) -> None\n\n \
3446 Remove user from from group." },
3447 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3448 "create_alias(alias_name) -> alias_rid\n\n \
3449 Create alias entry." },
3450 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3451 "delete_alias(alias_sid) -> None\n\n \
3452 Delete alias entry." },
3453 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3454 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3455 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3456 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3457 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3458 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3459 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3460 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3461 Add user to alias entry." },
3462 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3463 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3464 Remove a user from alias entry." },
3465 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3466 "enum_aliasmem(alias_sid) -> List\n\n \
3467 Return a list of members (dom_sid object) for alias entry." },
3468 /* enum_alias_memberships */
3469 /* lookup_rids */
3470 /* lookup_names */
3471 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3472 "get_account_policy() -> Mapping\n\n \
3473 Get account policy information as a dictionary." },
3474 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3475 "get_account_policy(Mapping) -> None\n\n \
3476 Set account policy settings from a dicionary." },
3477 /* get_seq_num */
3478 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3479 "search_users(acct_flags) -> List\n\n \
3480 Search users. acct_flags are samr account control flags.\n \
3481 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3482 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3483 "search_groups() -> List\n\n \
3484 Search unix only groups. \n \
3485 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3486 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3487 "search_aliases([domain_sid]) -> List\n\n \
3488 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3489 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3490 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3491 "uid_to_sid(uid) -> sid\n\n \
3492 Return sid for given user id." },
3493 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3494 "gid_to_sid(gid) -> sid\n\n \
3495 Return sid for given group id." },
3496 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3497 "sid_to_id(sid) -> Tuple\n\n \
3498 Return id and type for given sid." },
3499 /* capabilities */
3500 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3501 "new_rid() -> rid\n\n \
3502 Get a new rid." },
3503 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3504 "get_trusteddom_pw(domain) -> Mapping\n\n \
3505 Get trusted domain password, sid and last set time in a dictionary." },
3506 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3507 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3508 Set trusted domain password." },
3509 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3510 "del_trusteddom_pw(domain) -> None\n\n \
3511 Delete trusted domain password." },
3512 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3513 "enum_trusteddoms() -> List\n\n \
3514 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3515 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3516 "get_trusted_domain(domain) -> Mapping\n\n \
3517 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." },
3518 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3519 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3520 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" },
3521 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3522 "set_trusted_domain(domain, Mapping) -> None\n\n \
3523 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." },
3524 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3525 "del_trusted_domain(domain) -> None\n\n \
3526 Delete trusted domain." },
3527 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3528 "enum_trusted_domains() -> List\n\n \
3529 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." },
3530 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3531 "get_secret(secret_name) -> Mapping\n\n \
3532 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3533 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3534 "set_secret(secret_name, Mapping) -> None\n\n \
3535 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3536 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3537 "delete_secret(secret_name) -> None\n\n \
3538 Delete secret information for secret_name." },
3539 { NULL },
3543 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3545 TALLOC_CTX *frame = talloc_stackframe();
3546 const char *url = NULL;
3547 PyObject *pypdb;
3548 NTSTATUS status;
3549 struct pdb_methods *methods;
3551 if (!PyArg_ParseTuple(args, "s", &url)) {
3552 talloc_free(frame);
3553 return NULL;
3556 /* Initalize list of methods */
3557 status = make_pdb_method_name(&methods, url);
3558 if (!NT_STATUS_IS_OK(status)) {
3559 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3560 url,
3561 NT_STATUS_V(status),
3562 get_friendly_nt_error_msg(status));
3563 talloc_free(frame);
3564 return NULL;
3567 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3568 PyErr_NoMemory();
3569 talloc_free(frame);
3570 return NULL;
3573 talloc_free(frame);
3574 return pypdb;
3578 static PyTypeObject PyPDB = {
3579 .tp_name = "passdb.PDB",
3580 .tp_basicsize = sizeof(pytalloc_Object),
3581 .tp_new = py_pdb_new,
3582 .tp_flags = Py_TPFLAGS_DEFAULT,
3583 .tp_methods = py_pdb_methods,
3584 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3589 * Return a list of passdb backends
3591 static PyObject *py_passdb_backends(PyObject *self)
3593 TALLOC_CTX *frame = talloc_stackframe();
3594 PyObject *py_blist;
3595 const struct pdb_init_function_entry *entry;
3597 entry = pdb_get_backends();
3598 if(! entry) {
3599 Py_RETURN_NONE;
3602 if((py_blist = PyList_New(0)) == NULL) {
3603 PyErr_NoMemory();
3604 talloc_free(frame);
3605 return NULL;
3608 while(entry) {
3609 PyList_Append(py_blist, PyString_FromString(entry->name));
3610 entry = entry->next;
3613 talloc_free(frame);
3614 return py_blist;
3618 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3620 TALLOC_CTX *frame = talloc_stackframe();
3621 const char *smb_config;
3623 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3624 talloc_free(frame);
3625 return NULL;
3628 /* Load smbconf parameters */
3629 if (!lp_load_global(smb_config)) {
3630 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3631 talloc_free(frame);
3632 return NULL;
3635 Py_RETURN_NONE;
3636 talloc_free(frame);
3640 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3642 TALLOC_CTX *frame = talloc_stackframe();
3643 const char *private_dir;
3645 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3646 talloc_free(frame);
3647 return NULL;
3650 /* Initialize secrets database */
3651 if (!secrets_init_path(private_dir, lp_use_ntdb())) {
3652 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3653 private_dir);
3654 talloc_free(frame);
3655 return NULL;
3658 talloc_free(frame);
3659 Py_RETURN_NONE;
3662 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3664 TALLOC_CTX *frame = talloc_stackframe();
3666 /* Initialize secrets database */
3667 if (!initialize_password_db(true, NULL)) {
3668 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3669 talloc_free(frame);
3670 return NULL;
3673 talloc_free(frame);
3674 Py_RETURN_NONE;
3677 static PyObject *py_get_global_sam_sid(PyObject *self)
3679 TALLOC_CTX *frame = talloc_stackframe();
3680 struct dom_sid *domain_sid, *domain_sid_copy;
3681 PyObject *py_dom_sid;
3683 domain_sid = get_global_sam_sid();
3685 domain_sid_copy = dom_sid_dup(frame, domain_sid);
3686 if (domain_sid_copy == NULL) {
3687 PyErr_NoMemory();
3688 talloc_free(frame);
3689 return NULL;
3692 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3694 talloc_free(frame);
3695 return py_dom_sid;
3699 static PyMethodDef py_passdb_methods[] = {
3700 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3701 "get_backends() -> list\n\n \
3702 Get a list of password database backends supported." },
3703 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3704 "set_smb_config(path) -> None\n\n \
3705 Set path to smb.conf file to load configuration parameters." },
3706 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3707 "set_secrets_dir(private_dir) -> None\n\n \
3708 Set path to private directory to load secrets database from non-default location." },
3709 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3710 "get_global_sam_sid() -> dom_sid\n\n \
3711 Return domain SID." },
3712 { "reload_static_pdb", (PyCFunction)py_reload_static_pdb, METH_NOARGS,
3713 "reload_static_pdb() -> None\n\n \
3714 Re-initalise the static pdb used internally. Needed if 'passdb backend' is changed." },
3715 { NULL },
3718 void initpassdb(void)
3720 TALLOC_CTX *frame = talloc_stackframe();
3721 PyObject *m, *mod;
3722 char exception_name[] = "passdb.error";
3724 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3725 if (talloc_type == NULL) {
3726 talloc_free(frame);
3727 return;
3730 PyPDB.tp_base = talloc_type;
3731 if (PyType_Ready(&PyPDB) < 0) {
3732 talloc_free(frame);
3733 return;
3736 PySamu.tp_base = talloc_type;
3737 if (PyType_Ready(&PySamu) < 0) {
3738 talloc_free(frame);
3739 return;
3742 PyGroupmap.tp_base = talloc_type;
3743 if (PyType_Ready(&PyGroupmap) < 0) {
3744 talloc_free(frame);
3745 return;
3748 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3749 if (m == NULL) {
3750 talloc_free(frame);
3751 return;
3754 /* Create new exception for passdb module */
3755 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3756 Py_INCREF(py_pdb_error);
3757 PyModule_AddObject(m, "error", py_pdb_error);
3759 Py_INCREF(&PyPDB);
3760 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3762 Py_INCREF(&PySamu);
3763 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3765 Py_INCREF(&PyGroupmap);
3766 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3768 /* Import dom_sid type from dcerpc.security */
3769 mod = PyImport_ImportModule("samba.dcerpc.security");
3770 if (mod == NULL) {
3771 talloc_free(frame);
3772 return;
3775 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3776 if (dom_sid_Type == NULL) {
3777 talloc_free(frame);
3778 return;
3781 /* Import security_descriptor type from dcerpc.security */
3782 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3783 Py_DECREF(mod);
3784 if (security_Type == NULL) {
3785 talloc_free(frame);
3786 return;
3789 /* Import GUID type from dcerpc.misc */
3790 mod = PyImport_ImportModule("samba.dcerpc.misc");
3791 if (mod == NULL) {
3792 talloc_free(frame);
3793 return;
3796 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3797 Py_DECREF(mod);
3798 if (guid_Type == NULL) {
3799 talloc_free(frame);
3800 return;
3802 talloc_free(frame);