dbwrap: Fix a enum conversion
[Samba.git] / source3 / passdb / py_passdb.c
blobdec45c3a5c44b428372a9c154320e0cf677c940f
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_TYPE /* Py_TYPE is only available on Python > 2.6 */
37 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
38 #endif
40 #ifndef PY_CHECK_TYPE
41 #define PY_CHECK_TYPE(type, var, fail) \
42 if (!PyObject_TypeCheck(var, type)) {\
43 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
44 fail; \
46 #endif
49 static PyTypeObject *dom_sid_Type = NULL;
50 static PyTypeObject *security_Type = NULL;
51 static PyTypeObject *guid_Type = NULL;
53 staticforward PyTypeObject PySamu;
54 staticforward PyTypeObject PyGroupmap;
55 staticforward PyTypeObject PyPDB;
57 static PyObject *py_pdb_error;
59 void initpassdb(void);
62 /************************** PIDL Autogeneratd ******************************/
64 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
66 TALLOC_CTX *frame = talloc_stackframe();
67 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
68 PyObject *py_logon_time;
70 py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
71 talloc_free(frame);
72 return py_logon_time;
75 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
77 TALLOC_CTX *frame = talloc_stackframe();
78 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
80 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
81 if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
82 talloc_free(frame);
83 return -1;
85 talloc_free(frame);
86 return 0;
89 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
91 TALLOC_CTX *frame = talloc_stackframe();
92 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
93 PyObject *py_logoff_time;
95 py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
96 talloc_free(frame);
97 return py_logoff_time;
100 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
102 TALLOC_CTX *frame = talloc_stackframe();
103 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
105 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
106 if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
107 talloc_free(frame);
108 return -1;
110 talloc_free(frame);
111 return 0;
114 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
116 TALLOC_CTX *frame = talloc_stackframe();
117 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
118 PyObject *py_kickoff_time;
120 py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
121 talloc_free(frame);
122 return py_kickoff_time;
125 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
127 TALLOC_CTX *frame = talloc_stackframe();
128 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
130 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
131 if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
132 talloc_free(frame);
133 return -1;
135 talloc_free(frame);
136 return 0;
139 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
141 TALLOC_CTX *frame = talloc_stackframe();
142 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
143 PyObject *py_bad_password_time;
145 py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
146 talloc_free(frame);
147 return py_bad_password_time;
150 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
152 TALLOC_CTX *frame = talloc_stackframe();
153 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
155 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
156 if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
157 talloc_free(frame);
158 return -1;
160 talloc_free(frame);
161 return 0;
164 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
166 TALLOC_CTX *frame = talloc_stackframe();
167 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
168 PyObject *py_pass_last_set_time;
170 py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
171 talloc_free(frame);
172 return py_pass_last_set_time;
175 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
177 TALLOC_CTX *frame = talloc_stackframe();
178 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
180 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
181 if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
182 talloc_free(frame);
183 return -1;
185 talloc_free(frame);
186 return 0;
189 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
191 TALLOC_CTX *frame = talloc_stackframe();
192 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
193 PyObject *py_pass_can_change_time;
195 py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
196 talloc_free(frame);
197 return py_pass_can_change_time;
200 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
202 TALLOC_CTX *frame = talloc_stackframe();
203 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
205 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
206 if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
207 talloc_free(frame);
208 return -1;
210 talloc_free(frame);
211 return 0;
214 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
216 TALLOC_CTX *frame = talloc_stackframe();
217 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
218 PyObject *py_pass_must_change_time;
220 py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
221 talloc_free(frame);
222 return py_pass_must_change_time;
225 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
227 TALLOC_CTX *frame = talloc_stackframe();
228 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
230 /* TODO: make this not a get/set or give a better exception */
231 talloc_free(frame);
232 return -1;
235 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
237 TALLOC_CTX *frame = talloc_stackframe();
238 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
239 PyObject *py_username;
240 const char *username;
242 username = pdb_get_username(sam_acct);
243 if (username == NULL) {
244 Py_RETURN_NONE;
247 py_username = PyString_FromString(username);
248 talloc_free(frame);
249 return py_username;
252 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
254 TALLOC_CTX *frame = talloc_stackframe();
255 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
257 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
258 if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
259 talloc_free(frame);
260 return -1;
262 talloc_free(frame);
263 return 0;
266 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
268 TALLOC_CTX *frame = talloc_stackframe();
269 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
270 PyObject *py_domain;
271 const char *domain;
273 domain = pdb_get_domain(sam_acct);
274 if (domain == NULL) {
275 Py_RETURN_NONE;
278 py_domain = PyString_FromString(domain);
279 talloc_free(frame);
280 return py_domain;
283 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
285 TALLOC_CTX *frame = talloc_stackframe();
286 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
288 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
289 if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
290 talloc_free(frame);
291 return -1;
293 talloc_free(frame);
294 return 0;
297 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
299 TALLOC_CTX *frame = talloc_stackframe();
300 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
301 PyObject *py_nt_username;
302 const char *nt_username;
304 nt_username = pdb_get_nt_username(sam_acct);
305 if (nt_username == NULL) {
306 Py_RETURN_NONE;
309 py_nt_username = PyString_FromString(nt_username);
310 talloc_free(frame);
311 return py_nt_username;
314 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
316 TALLOC_CTX *frame = talloc_stackframe();
317 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
319 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
320 if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
321 talloc_free(frame);
322 return -1;
324 talloc_free(frame);
325 return 0;
328 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
330 TALLOC_CTX *frame = talloc_stackframe();
331 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
332 PyObject *py_full_name;
333 const char *full_name;
335 full_name = pdb_get_fullname(sam_acct);
336 if (full_name == NULL) {
337 Py_RETURN_NONE;
340 py_full_name = PyString_FromString(full_name);
341 talloc_free(frame);
342 return py_full_name;
345 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
347 TALLOC_CTX *frame = talloc_stackframe();
348 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
350 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
351 if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
352 talloc_free(frame);
353 return -1;
355 talloc_free(frame);
356 return 0;
359 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
361 TALLOC_CTX *frame = talloc_stackframe();
362 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
363 PyObject *py_home_dir;
364 const char *home_dir;
366 home_dir = pdb_get_homedir(sam_acct);
367 if (home_dir == NULL) {
368 Py_RETURN_NONE;
371 py_home_dir = PyString_FromString(home_dir);
372 talloc_free(frame);
373 return py_home_dir;
376 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
378 TALLOC_CTX *frame = talloc_stackframe();
379 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
381 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
382 if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
383 talloc_free(frame);
384 return -1;
386 talloc_free(frame);
387 return 0;
390 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
392 TALLOC_CTX *frame = talloc_stackframe();
393 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
394 PyObject *py_dir_drive;
395 const char *dir_drive;
397 dir_drive = pdb_get_dir_drive(sam_acct);
398 if (dir_drive == NULL) {
399 Py_RETURN_NONE;
402 py_dir_drive = PyString_FromString(dir_drive);
403 talloc_free(frame);
404 return py_dir_drive;
407 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
409 TALLOC_CTX *frame = talloc_stackframe();
410 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
412 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
413 if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
414 talloc_free(frame);
415 return -1;
417 talloc_free(frame);
418 return 0;
421 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
423 TALLOC_CTX *frame = talloc_stackframe();
424 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
425 PyObject *py_logon_script;
426 const char *logon_script;
428 logon_script = pdb_get_logon_script(sam_acct);
429 if (logon_script == NULL) {
430 Py_RETURN_NONE;
433 py_logon_script = PyString_FromString(logon_script);
434 talloc_free(frame);
435 return py_logon_script;
438 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
440 TALLOC_CTX *frame = talloc_stackframe();
441 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
443 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
444 if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
445 talloc_free(frame);
446 return -1;
448 talloc_free(frame);
449 return 0;
452 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
454 TALLOC_CTX *frame = talloc_stackframe();
455 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
456 PyObject *py_profile_path;
457 const char *profile_path;
459 profile_path = pdb_get_profile_path(sam_acct);
460 if (profile_path == NULL) {
461 Py_RETURN_NONE;
464 py_profile_path = PyString_FromString(profile_path);
465 talloc_free(frame);
466 return py_profile_path;
469 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
471 TALLOC_CTX *frame = talloc_stackframe();
472 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
474 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
475 if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
476 talloc_free(frame);
477 return -1;
479 talloc_free(frame);
480 return 0;
483 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
485 TALLOC_CTX *frame = talloc_stackframe();
486 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
487 PyObject *py_acct_desc;
488 const char *acct_desc;
490 acct_desc = pdb_get_acct_desc(sam_acct);
491 if (acct_desc == NULL) {
492 Py_RETURN_NONE;
495 py_acct_desc = PyString_FromString(acct_desc);
496 talloc_free(frame);
497 return py_acct_desc;
500 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
502 TALLOC_CTX *frame = talloc_stackframe();
503 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
505 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
506 if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
507 talloc_free(frame);
508 return -1;
510 talloc_free(frame);
511 return 0;
514 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
516 TALLOC_CTX *frame = talloc_stackframe();
517 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
518 PyObject *py_workstations;
519 const char *workstations;
521 workstations = pdb_get_workstations(sam_acct);
522 if (workstations == NULL) {
523 Py_RETURN_NONE;
526 py_workstations = PyString_FromString(workstations);
527 talloc_free(frame);
528 return py_workstations;
531 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
533 TALLOC_CTX *frame = talloc_stackframe();
534 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
536 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
537 if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
538 talloc_free(frame);
539 return -1;
541 talloc_free(frame);
542 return 0;
545 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
547 TALLOC_CTX *frame = talloc_stackframe();
548 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
549 PyObject *py_comment;
550 const char *comment;
552 comment = pdb_get_comment(sam_acct);
553 if (comment == NULL) {
554 Py_RETURN_NONE;
557 py_comment = PyString_FromString(comment);
558 talloc_free(frame);
559 return py_comment;
562 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
564 TALLOC_CTX *frame = talloc_stackframe();
565 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
567 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
568 if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
569 talloc_free(frame);
570 return -1;
572 talloc_free(frame);
573 return 0;
576 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
578 TALLOC_CTX *frame = talloc_stackframe();
579 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
580 PyObject *py_munged_dial;
581 const char *munged_dial;
583 munged_dial = pdb_get_munged_dial(sam_acct);
584 if (munged_dial == NULL) {
585 Py_RETURN_NONE;
588 py_munged_dial = PyString_FromString(munged_dial);
589 talloc_free(frame);
590 return py_munged_dial;
593 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
595 TALLOC_CTX *frame = talloc_stackframe();
596 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
598 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
599 if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
600 talloc_free(frame);
601 return -1;
603 talloc_free(frame);
604 return 0;
607 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
609 TALLOC_CTX *frame = talloc_stackframe();
610 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
611 PyObject *py_user_sid;
612 const struct dom_sid *user_sid;
613 struct dom_sid *copy_user_sid;
614 TALLOC_CTX *mem_ctx;
616 user_sid = pdb_get_user_sid(sam_acct);
617 if(user_sid == NULL) {
618 Py_RETURN_NONE;
621 mem_ctx = talloc_new(NULL);
622 if (mem_ctx == NULL) {
623 PyErr_NoMemory();
624 talloc_free(frame);
625 return NULL;
627 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
628 if (copy_user_sid == NULL) {
629 PyErr_NoMemory();
630 talloc_free(mem_ctx);
631 talloc_free(frame);
632 return NULL;
635 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
637 talloc_free(mem_ctx);
639 talloc_free(frame);
640 return py_user_sid;
643 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
645 TALLOC_CTX *frame = talloc_stackframe();
646 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
648 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
649 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
650 talloc_free(frame);
651 return -1;
653 talloc_free(frame);
654 return 0;
657 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
659 TALLOC_CTX *frame = talloc_stackframe();
660 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
661 const struct dom_sid *group_sid;
662 struct dom_sid *copy_group_sid;
664 group_sid = pdb_get_group_sid(sam_acct);
665 if (group_sid == NULL) {
666 Py_RETURN_NONE;
669 copy_group_sid = dom_sid_dup(NULL, group_sid);
670 if (copy_group_sid == NULL) {
671 PyErr_NoMemory();
672 talloc_free(frame);
673 return NULL;
676 talloc_free(frame);
677 return pytalloc_steal(dom_sid_Type, copy_group_sid);
680 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
682 TALLOC_CTX *frame = talloc_stackframe();
683 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
685 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
686 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
687 talloc_free(frame);
688 return -1;
690 talloc_free(frame);
691 return 0;
694 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
696 TALLOC_CTX *frame = talloc_stackframe();
697 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
698 PyObject *py_lm_pw;
699 const char *lm_pw;
701 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
702 if (lm_pw == NULL) {
703 Py_RETURN_NONE;
706 py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
707 talloc_free(frame);
708 return py_lm_pw;
711 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
713 TALLOC_CTX *frame = talloc_stackframe();
714 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
716 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
717 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
718 talloc_free(frame);
719 return -1;
721 talloc_free(frame);
722 return 0;
725 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
727 TALLOC_CTX *frame = talloc_stackframe();
728 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
729 PyObject *py_nt_pw;
730 const char *nt_pw;
732 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
733 if (nt_pw == NULL) {
734 Py_RETURN_NONE;
737 py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
738 talloc_free(frame);
739 return py_nt_pw;
742 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
744 TALLOC_CTX *frame = talloc_stackframe();
745 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
747 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
748 talloc_free(frame);
749 return -1;
751 talloc_free(frame);
752 return 0;
755 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
757 TALLOC_CTX *frame = talloc_stackframe();
758 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
759 PyObject *py_nt_pw_his;
760 const char *nt_pw_his;
761 uint32_t hist_len;
763 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
764 if (nt_pw_his == NULL) {
765 Py_RETURN_NONE;
768 py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
769 talloc_free(frame);
770 return py_nt_pw_his;
773 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
775 TALLOC_CTX *frame = talloc_stackframe();
776 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
777 char *nt_pw_his;
778 Py_ssize_t len;
779 uint32_t hist_len;
781 PyString_AsStringAndSize(value, &nt_pw_his, &len);
782 hist_len = len / PW_HISTORY_ENTRY_LEN;
783 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
784 talloc_free(frame);
785 return -1;
787 talloc_free(frame);
788 return 0;
791 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
793 TALLOC_CTX *frame = talloc_stackframe();
794 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
795 PyObject *py_plaintext_pw;
796 const char *plaintext_pw;
798 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
799 if (plaintext_pw == NULL) {
800 Py_RETURN_NONE;
803 py_plaintext_pw = PyString_FromString(plaintext_pw);
804 talloc_free(frame);
805 return py_plaintext_pw;
808 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
810 TALLOC_CTX *frame = talloc_stackframe();
811 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
813 if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
814 talloc_free(frame);
815 return -1;
817 talloc_free(frame);
818 return 0;
821 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
823 TALLOC_CTX *frame = talloc_stackframe();
824 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
825 PyObject *py_acct_ctrl;
827 py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
828 talloc_free(frame);
829 return py_acct_ctrl;
832 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
834 TALLOC_CTX *frame = talloc_stackframe();
835 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
837 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
838 if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
839 talloc_free(frame);
840 return -1;
842 talloc_free(frame);
843 return 0;
846 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
848 TALLOC_CTX *frame = talloc_stackframe();
849 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
850 PyObject *py_logon_divs;
852 py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
853 talloc_free(frame);
854 return py_logon_divs;
857 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
859 TALLOC_CTX *frame = talloc_stackframe();
860 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
862 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
863 if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
864 talloc_free(frame);
865 return -1;
867 talloc_free(frame);
868 return 0;
871 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
873 TALLOC_CTX *frame = talloc_stackframe();
874 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
875 PyObject *py_hours_len;
877 py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
878 talloc_free(frame);
879 return py_hours_len;
882 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
884 TALLOC_CTX *frame = talloc_stackframe();
885 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
887 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
888 if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
889 talloc_free(frame);
890 return -1;
892 talloc_free(frame);
893 return 0;
896 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
898 TALLOC_CTX *frame = talloc_stackframe();
899 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
900 PyObject *py_hours;
901 const char *hours;
902 int hours_len, i;
904 hours = (const char *)pdb_get_hours(sam_acct);
905 if(! hours) {
906 Py_RETURN_NONE;
909 hours_len = pdb_get_hours_len(sam_acct);
910 if ((py_hours = PyList_New(hours_len)) == NULL) {
911 PyErr_NoMemory();
912 talloc_free(frame);
913 return NULL;
916 for (i=0; i<hours_len; i++) {
917 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
919 talloc_free(frame);
920 return py_hours;
923 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
925 TALLOC_CTX *frame = talloc_stackframe();
926 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
927 int i;
928 uint8_t *hours;
929 int hours_len;
930 bool status;
932 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
934 hours_len = PyList_GET_SIZE(value);
936 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
937 if (!hours) {
938 PyErr_NoMemory();
939 talloc_free(frame);
940 return -1;
943 for (i=0; i < hours_len; i++) {
944 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
945 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
948 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
949 talloc_free(hours);
951 if(! status) {
952 talloc_free(frame);
953 return -1;
955 talloc_free(frame);
956 return 0;
959 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
961 TALLOC_CTX *frame = talloc_stackframe();
962 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
963 PyObject *py_bad_password_count;
965 py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
966 talloc_free(frame);
967 return py_bad_password_count;
970 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
972 TALLOC_CTX *frame = talloc_stackframe();
973 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
975 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
976 if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
977 talloc_free(frame);
978 return -1;
980 talloc_free(frame);
981 return 0;
984 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
986 TALLOC_CTX *frame = talloc_stackframe();
987 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
988 PyObject *py_logon_count;
990 py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
991 talloc_free(frame);
992 return py_logon_count;
995 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
997 TALLOC_CTX *frame = talloc_stackframe();
998 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1000 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1001 if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1002 talloc_free(frame);
1003 return -1;
1005 talloc_free(frame);
1006 return 0;
1009 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
1011 TALLOC_CTX *frame = talloc_stackframe();
1012 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1013 PyObject *py_country_code;
1015 py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
1016 talloc_free(frame);
1017 return py_country_code;
1020 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
1022 TALLOC_CTX *frame = talloc_stackframe();
1023 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1025 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1026 if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1027 talloc_free(frame);
1028 return -1;
1030 talloc_free(frame);
1031 return 0;
1034 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
1036 TALLOC_CTX *frame = talloc_stackframe();
1037 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1038 PyObject *py_code_page;
1040 py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
1041 talloc_free(frame);
1042 return py_code_page;
1045 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
1047 TALLOC_CTX *frame = talloc_stackframe();
1048 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1050 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1051 if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1052 talloc_free(frame);
1053 return -1;
1055 talloc_free(frame);
1056 return 0;
1059 static PyGetSetDef py_samu_getsetters[] = {
1060 { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
1061 { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
1062 { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
1063 { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
1064 { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
1065 { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
1066 { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
1067 { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
1068 { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
1069 { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
1070 { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
1071 { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
1072 { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
1073 { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
1074 { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
1075 { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
1076 { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
1077 { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
1078 { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
1079 { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
1080 { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
1081 { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
1082 { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
1083 { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
1084 { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
1085 { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
1086 { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
1087 { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
1088 { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
1089 { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
1090 { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
1091 { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
1092 { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
1093 { NULL }
1097 /************************** PIDL Autogeneratd ******************************/
1099 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1101 TALLOC_CTX *frame = talloc_stackframe();
1102 struct samu *sam_acct;
1104 sam_acct = samu_new(NULL);
1105 if (!sam_acct) {
1106 PyErr_NoMemory();
1107 talloc_free(frame);
1108 return NULL;
1111 talloc_free(frame);
1112 return pytalloc_steal(type, sam_acct);
1115 static PyTypeObject PySamu = {
1116 .tp_name = "passdb.Samu",
1117 .tp_basicsize = sizeof(pytalloc_Object),
1118 .tp_getset = py_samu_getsetters,
1119 .tp_methods = NULL,
1120 .tp_new = py_samu_new,
1121 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1122 .tp_doc = "Samu() -> samu object\n",
1126 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1128 TALLOC_CTX *frame = talloc_stackframe();
1129 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1130 PyObject *py_gid;
1132 py_gid = Py_BuildValue("i", group_map->gid);
1133 talloc_free(frame);
1134 return py_gid;
1137 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1139 TALLOC_CTX *frame = talloc_stackframe();
1140 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1142 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1143 group_map->gid = PyInt_AsLong(value);
1144 talloc_free(frame);
1145 return 0;
1148 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1150 TALLOC_CTX *frame = talloc_stackframe();
1151 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1152 PyObject *py_sid;
1153 struct dom_sid *group_sid;
1154 TALLOC_CTX *mem_ctx;
1156 mem_ctx = talloc_new(NULL);
1157 if (mem_ctx == NULL) {
1158 PyErr_NoMemory();
1159 talloc_free(frame);
1160 return NULL;
1163 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1164 if (group_sid == NULL) {
1165 PyErr_NoMemory();
1166 talloc_free(mem_ctx);
1167 talloc_free(frame);
1168 return NULL;
1171 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1173 talloc_free(mem_ctx);
1175 talloc_free(frame);
1176 return py_sid;
1179 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1181 TALLOC_CTX *frame = talloc_stackframe();
1182 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1184 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1185 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1186 talloc_free(frame);
1187 return 0;
1190 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1192 TALLOC_CTX *frame = talloc_stackframe();
1193 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1194 PyObject *py_sid_name_use;
1196 py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1197 talloc_free(frame);
1198 return py_sid_name_use;
1201 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1203 TALLOC_CTX *frame = talloc_stackframe();
1204 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1206 PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1207 group_map->sid_name_use = PyInt_AsLong(value);
1208 talloc_free(frame);
1209 return 0;
1212 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1214 TALLOC_CTX *frame = talloc_stackframe();
1215 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1216 PyObject *py_nt_name;
1217 if (group_map->nt_name == NULL) {
1218 py_nt_name = Py_None;
1219 Py_INCREF(py_nt_name);
1220 } else {
1221 py_nt_name = PyString_FromString(group_map->nt_name);
1223 talloc_free(frame);
1224 return py_nt_name;
1227 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1229 TALLOC_CTX *frame = talloc_stackframe();
1230 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1232 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1233 if (value == Py_None) {
1234 fstrcpy(group_map->nt_name, NULL);
1235 } else {
1236 fstrcpy(group_map->nt_name, PyString_AsString(value));
1238 talloc_free(frame);
1239 return 0;
1242 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1244 TALLOC_CTX *frame = talloc_stackframe();
1245 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1246 PyObject *py_comment;
1247 if (group_map->comment == NULL) {
1248 py_comment = Py_None;
1249 Py_INCREF(py_comment);
1250 } else {
1251 py_comment = PyString_FromString(group_map->comment);
1253 talloc_free(frame);
1254 return py_comment;
1257 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1259 TALLOC_CTX *frame = talloc_stackframe();
1260 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1262 PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1263 if (value == Py_None) {
1264 fstrcpy(group_map->comment, NULL);
1265 } else {
1266 fstrcpy(group_map->comment, PyString_AsString(value));
1268 talloc_free(frame);
1269 return 0;
1272 static PyGetSetDef py_groupmap_getsetters[] = {
1273 { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1274 { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1275 { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1276 { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1277 { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1278 { NULL }
1281 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1283 TALLOC_CTX *frame = talloc_stackframe();
1284 GROUP_MAP *group_map;
1285 TALLOC_CTX *mem_ctx;
1286 PyObject *py_group_map;
1288 mem_ctx = talloc_new(NULL);
1289 if (mem_ctx == NULL) {
1290 PyErr_NoMemory();
1291 talloc_free(frame);
1292 return NULL;
1295 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1296 if (group_map == NULL) {
1297 PyErr_NoMemory();
1298 talloc_free(mem_ctx);
1299 talloc_free(frame);
1300 return NULL;
1303 py_group_map = pytalloc_steal(type, group_map);
1304 if (py_group_map == NULL) {
1305 PyErr_NoMemory();
1306 talloc_free(mem_ctx);
1307 talloc_free(frame);
1308 return NULL;
1311 talloc_free(mem_ctx);
1313 talloc_free(frame);
1314 return py_group_map;
1318 static PyTypeObject PyGroupmap = {
1319 .tp_name = "passdb.Groupmap",
1320 .tp_basicsize = sizeof(pytalloc_Object),
1321 .tp_getset = py_groupmap_getsetters,
1322 .tp_methods = NULL,
1323 .tp_new = py_groupmap_new,
1324 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1325 .tp_doc = "Groupmap() -> group map object\n",
1329 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1331 TALLOC_CTX *frame = talloc_stackframe();
1332 struct pdb_methods *methods;
1333 struct pdb_domain_info *domain_info;
1334 PyObject *py_domain_info;
1335 struct dom_sid *sid;
1336 struct GUID *guid;
1338 methods = pytalloc_get_ptr(self);
1340 domain_info = methods->get_domain_info(methods, frame);
1341 if (! domain_info) {
1342 Py_RETURN_NONE;
1345 sid = dom_sid_dup(frame, &domain_info->sid);
1346 if (sid == NULL) {
1347 PyErr_NoMemory();
1348 talloc_free(frame);
1349 return NULL;
1352 guid = talloc(frame, struct GUID);
1353 if (guid == NULL) {
1354 PyErr_NoMemory();
1355 talloc_free(frame);
1356 return NULL;
1358 *guid = domain_info->guid;
1360 if ((py_domain_info = PyDict_New()) == NULL) {
1361 PyErr_NoMemory();
1362 talloc_free(frame);
1363 return NULL;
1366 PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1367 PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->dns_domain));
1368 PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->dns_forest));
1369 PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1370 PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1372 talloc_free(frame);
1373 return py_domain_info;
1377 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1379 TALLOC_CTX *frame = talloc_stackframe();
1380 NTSTATUS status;
1381 const char *username;
1382 struct pdb_methods *methods;
1383 struct samu *sam_acct;
1384 PyObject *py_sam_acct;
1386 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1387 talloc_free(frame);
1388 return NULL;
1391 methods = pytalloc_get_ptr(self);
1393 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1394 if (py_sam_acct == NULL) {
1395 PyErr_NoMemory();
1396 talloc_free(frame);
1397 return NULL;
1399 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1401 status = methods->getsampwnam(methods, sam_acct, username);
1402 if (!NT_STATUS_IS_OK(status)) {
1403 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1404 username,
1405 NT_STATUS_V(status),
1406 get_friendly_nt_error_msg(status));
1407 Py_DECREF(py_sam_acct);
1408 talloc_free(frame);
1409 return NULL;
1412 talloc_free(frame);
1413 return py_sam_acct;
1416 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1418 TALLOC_CTX *frame = talloc_stackframe();
1419 NTSTATUS status;
1420 struct pdb_methods *methods;
1421 struct samu *sam_acct;
1422 PyObject *py_sam_acct;
1423 PyObject *py_user_sid;
1425 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1426 talloc_free(frame);
1427 return NULL;
1430 methods = pytalloc_get_ptr(self);
1432 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1433 if (py_sam_acct == NULL) {
1434 PyErr_NoMemory();
1435 talloc_free(frame);
1436 return NULL;
1438 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1440 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1441 if (!NT_STATUS_IS_OK(status)) {
1442 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1443 NT_STATUS_V(status),
1444 get_friendly_nt_error_msg(status));
1445 Py_DECREF(py_sam_acct);
1446 talloc_free(frame);
1447 return NULL;
1450 talloc_free(frame);
1451 return py_sam_acct;
1454 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1456 TALLOC_CTX *frame = talloc_stackframe();
1457 NTSTATUS status;
1458 struct pdb_methods *methods;
1459 const char *username;
1460 unsigned int acct_flags;
1461 unsigned int rid;
1463 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1464 talloc_free(frame);
1465 return NULL;
1468 methods = pytalloc_get_ptr(self);
1470 status = methods->create_user(methods, frame, username, acct_flags, &rid);
1471 if (!NT_STATUS_IS_OK(status)) {
1472 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1473 username,
1474 NT_STATUS_V(status),
1475 get_friendly_nt_error_msg(status));
1476 talloc_free(frame);
1477 return NULL;
1480 talloc_free(frame);
1481 return PyInt_FromLong(rid);
1484 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1486 TALLOC_CTX *frame = talloc_stackframe();
1487 NTSTATUS status;
1488 struct pdb_methods *methods;
1489 struct samu *sam_acct;
1490 PyObject *py_sam_acct;
1492 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1493 talloc_free(frame);
1494 return NULL;
1497 methods = pytalloc_get_ptr(self);
1499 sam_acct = pytalloc_get_ptr(py_sam_acct);
1501 status = methods->delete_user(methods, frame, sam_acct);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1504 NT_STATUS_V(status),
1505 get_friendly_nt_error_msg(status));
1506 talloc_free(frame);
1507 return NULL;
1510 Py_RETURN_NONE;
1511 talloc_free(frame);
1514 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1516 TALLOC_CTX *frame = talloc_stackframe();
1517 NTSTATUS status;
1518 struct pdb_methods *methods;
1519 struct samu *sam_acct;
1520 PyObject *py_sam_acct;
1522 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1523 talloc_free(frame);
1524 return NULL;
1527 methods = pytalloc_get_ptr(self);
1529 sam_acct = pytalloc_get_ptr(py_sam_acct);
1531 status = methods->add_sam_account(methods, sam_acct);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1534 sam_acct->username,
1535 NT_STATUS_V(status),
1536 get_friendly_nt_error_msg(status));
1537 talloc_free(frame);
1538 return NULL;
1541 Py_RETURN_NONE;
1542 talloc_free(frame);
1545 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1547 TALLOC_CTX *frame = talloc_stackframe();
1548 NTSTATUS status;
1549 struct pdb_methods *methods;
1550 struct samu *sam_acct;
1551 PyObject *py_sam_acct;
1553 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1554 talloc_free(frame);
1555 return NULL;
1558 methods = pytalloc_get_ptr(self);
1560 sam_acct = pytalloc_get_ptr(py_sam_acct);
1562 status = methods->update_sam_account(methods, sam_acct);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1565 NT_STATUS_V(status),
1566 get_friendly_nt_error_msg(status));
1567 talloc_free(frame);
1568 return NULL;
1571 Py_RETURN_NONE;
1572 talloc_free(frame);
1575 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1577 TALLOC_CTX *frame = talloc_stackframe();
1578 NTSTATUS status;
1579 struct pdb_methods *methods;
1580 struct samu *sam_acct;
1581 PyObject *py_sam_acct;
1583 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1584 talloc_free(frame);
1585 return NULL;
1588 methods = pytalloc_get_ptr(self);
1590 sam_acct = pytalloc_get_ptr(py_sam_acct);
1592 status = methods->delete_sam_account(methods, sam_acct);
1593 if (!NT_STATUS_IS_OK(status)) {
1594 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1595 NT_STATUS_V(status),
1596 get_friendly_nt_error_msg(status));
1597 talloc_free(frame);
1598 return NULL;
1601 Py_RETURN_NONE;
1602 talloc_free(frame);
1605 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1607 TALLOC_CTX *frame = talloc_stackframe();
1608 NTSTATUS status;
1609 struct pdb_methods *methods;
1610 struct samu *sam_acct;
1611 const char *new_username;
1612 PyObject *py_sam_acct;
1614 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1615 &new_username)) {
1616 talloc_free(frame);
1617 return NULL;
1620 methods = pytalloc_get_ptr(self);
1622 sam_acct = pytalloc_get_ptr(py_sam_acct);
1624 status = methods->rename_sam_account(methods, sam_acct, new_username);
1625 if (!NT_STATUS_IS_OK(status)) {
1626 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1627 NT_STATUS_V(status),
1628 get_friendly_nt_error_msg(status));
1629 talloc_free(frame);
1630 return NULL;
1633 Py_RETURN_NONE;
1634 talloc_free(frame);
1638 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1640 TALLOC_CTX *frame = talloc_stackframe();
1641 NTSTATUS status;
1642 struct pdb_methods *methods;
1643 GROUP_MAP *group_map;
1644 struct dom_sid *domain_sid;
1645 PyObject *py_domain_sid, *py_group_map;
1647 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1648 talloc_free(frame);
1649 return NULL;
1652 methods = pytalloc_get_ptr(self);
1654 domain_sid = pytalloc_get_ptr(py_domain_sid);
1656 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1657 if (py_group_map == NULL) {
1658 PyErr_NoMemory();
1659 talloc_free(frame);
1660 return NULL;
1663 group_map = pytalloc_get_ptr(py_group_map);
1665 status = methods->getgrsid(methods, group_map, *domain_sid);
1666 if (!NT_STATUS_IS_OK(status)) {
1667 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1668 NT_STATUS_V(status),
1669 get_friendly_nt_error_msg(status));
1670 talloc_free(frame);
1671 return NULL;
1674 talloc_free(frame);
1675 return py_group_map;
1679 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1681 TALLOC_CTX *frame = talloc_stackframe();
1682 NTSTATUS status;
1683 struct pdb_methods *methods;
1684 GROUP_MAP *group_map;
1685 PyObject *py_group_map;
1686 unsigned int gid_value;
1688 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1689 talloc_free(frame);
1690 return NULL;
1693 methods = pytalloc_get_ptr(self);
1695 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1696 if (py_group_map == NULL) {
1697 PyErr_NoMemory();
1698 talloc_free(frame);
1699 return NULL;
1702 group_map = pytalloc_get_ptr(py_group_map);
1704 status = methods->getgrgid(methods, group_map, gid_value);
1705 if (!NT_STATUS_IS_OK(status)) {
1706 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1707 NT_STATUS_V(status),
1708 get_friendly_nt_error_msg(status));
1709 talloc_free(frame);
1710 return NULL;
1713 talloc_free(frame);
1714 return py_group_map;
1718 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1720 TALLOC_CTX *frame = talloc_stackframe();
1721 NTSTATUS status;
1722 struct pdb_methods *methods;
1723 GROUP_MAP *group_map;
1724 PyObject *py_group_map;
1725 const char *groupname;
1727 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1728 talloc_free(frame);
1729 return NULL;
1732 methods = pytalloc_get_ptr(self);
1734 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1735 if (py_group_map == NULL) {
1736 PyErr_NoMemory();
1737 talloc_free(frame);
1738 return NULL;
1741 group_map = pytalloc_get_ptr(py_group_map);
1743 status = methods->getgrnam(methods, group_map, groupname);
1744 if (!NT_STATUS_IS_OK(status)) {
1745 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1746 NT_STATUS_V(status),
1747 get_friendly_nt_error_msg(status));
1748 talloc_free(frame);
1749 return NULL;
1752 talloc_free(frame);
1753 return py_group_map;
1757 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1759 TALLOC_CTX *frame = talloc_stackframe();
1760 NTSTATUS status;
1761 struct pdb_methods *methods;
1762 const char *groupname;
1763 uint32_t group_rid;
1765 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1766 talloc_free(frame);
1767 return NULL;
1770 methods = pytalloc_get_ptr(self);
1772 status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1773 if (!NT_STATUS_IS_OK(status)) {
1774 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1775 groupname,
1776 NT_STATUS_V(status),
1777 get_friendly_nt_error_msg(status));
1778 talloc_free(frame);
1779 return NULL;
1782 talloc_free(frame);
1783 return PyInt_FromLong(group_rid);
1787 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1789 TALLOC_CTX *frame = talloc_stackframe();
1790 NTSTATUS status;
1791 struct pdb_methods *methods;
1792 unsigned int group_rid;
1794 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1795 talloc_free(frame);
1796 return NULL;
1799 methods = pytalloc_get_ptr(self);
1801 status = methods->delete_dom_group(methods, frame, group_rid);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1804 group_rid,
1805 NT_STATUS_V(status),
1806 get_friendly_nt_error_msg(status));
1807 talloc_free(frame);
1808 return NULL;
1811 Py_RETURN_NONE;
1812 talloc_free(frame);
1816 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1818 TALLOC_CTX *frame = talloc_stackframe();
1819 NTSTATUS status;
1820 struct pdb_methods *methods;
1821 PyObject *py_group_map;
1822 GROUP_MAP *group_map;
1824 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1825 talloc_free(frame);
1826 return NULL;
1829 methods = pytalloc_get_ptr(self);
1831 group_map = pytalloc_get_ptr(py_group_map);
1833 status = methods->add_group_mapping_entry(methods, group_map);
1834 if (!NT_STATUS_IS_OK(status)) {
1835 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1836 NT_STATUS_V(status),
1837 get_friendly_nt_error_msg(status));
1838 talloc_free(frame);
1839 return NULL;
1842 Py_RETURN_NONE;
1843 talloc_free(frame);
1847 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1849 TALLOC_CTX *frame = talloc_stackframe();
1850 NTSTATUS status;
1851 struct pdb_methods *methods;
1852 PyObject *py_group_map;
1853 GROUP_MAP *group_map;
1855 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1856 talloc_free(frame);
1857 return NULL;
1860 methods = pytalloc_get_ptr(self);
1862 group_map = pytalloc_get_ptr(py_group_map);
1864 status = methods->update_group_mapping_entry(methods, group_map);
1865 if (!NT_STATUS_IS_OK(status)) {
1866 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1867 NT_STATUS_V(status),
1868 get_friendly_nt_error_msg(status));
1869 talloc_free(frame);
1870 return NULL;
1873 Py_RETURN_NONE;
1874 talloc_free(frame);
1878 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1880 TALLOC_CTX *frame = talloc_stackframe();
1881 NTSTATUS status;
1882 struct pdb_methods *methods;
1883 PyObject *py_group_sid;
1884 struct dom_sid *group_sid;
1886 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1887 talloc_free(frame);
1888 return NULL;
1891 methods = pytalloc_get_ptr(self);
1893 group_sid = pytalloc_get_ptr(py_group_sid);
1895 status = methods->delete_group_mapping_entry(methods, *group_sid);
1896 if (!NT_STATUS_IS_OK(status)) {
1897 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1898 NT_STATUS_V(status),
1899 get_friendly_nt_error_msg(status));
1900 talloc_free(frame);
1901 return NULL;
1904 Py_RETURN_NONE;
1905 talloc_free(frame);
1909 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1911 TALLOC_CTX *frame = talloc_stackframe();
1912 NTSTATUS status;
1913 struct pdb_methods *methods;
1914 enum lsa_SidType sid_name_use;
1915 int lsa_sidtype_value = SID_NAME_UNKNOWN;
1916 int unix_only = 0;
1917 PyObject *py_domain_sid;
1918 struct dom_sid *domain_sid = NULL;
1919 GROUP_MAP **gmap = NULL;
1920 GROUP_MAP *group_map;
1921 size_t num_entries;
1922 PyObject *py_gmap_list, *py_group_map;
1923 int i;
1925 py_domain_sid = Py_None;
1926 Py_INCREF(Py_None);
1928 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1929 &lsa_sidtype_value, &unix_only)) {
1930 talloc_free(frame);
1931 return NULL;
1934 methods = pytalloc_get_ptr(self);
1936 sid_name_use = lsa_sidtype_value;
1938 if (py_domain_sid != Py_None) {
1939 domain_sid = pytalloc_get_ptr(py_domain_sid);
1942 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1943 &gmap, &num_entries, unix_only);
1944 if (!NT_STATUS_IS_OK(status)) {
1945 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1946 NT_STATUS_V(status),
1947 get_friendly_nt_error_msg(status));
1948 talloc_free(frame);
1949 return NULL;
1952 py_gmap_list = PyList_New(0);
1953 if (py_gmap_list == NULL) {
1954 PyErr_NoMemory();
1955 talloc_free(frame);
1956 return NULL;
1959 for(i=0; i<num_entries; i++) {
1960 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1961 if (py_group_map) {
1962 group_map = pytalloc_get_ptr(py_group_map);
1963 *group_map = *gmap[i];
1964 talloc_steal(group_map, gmap[i]->nt_name);
1965 talloc_steal(group_map, gmap[i]->comment);
1967 PyList_Append(py_gmap_list, py_group_map);
1971 talloc_free(gmap);
1973 talloc_free(frame);
1974 return py_gmap_list;
1978 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1980 TALLOC_CTX *frame = talloc_stackframe();
1981 NTSTATUS status;
1982 struct pdb_methods *methods;
1983 PyObject *py_group_sid;
1984 struct dom_sid *group_sid;
1985 uint32_t *member_rids;
1986 size_t num_members;
1987 PyObject *py_sid_list;
1988 struct dom_sid *domain_sid, *member_sid;
1989 int i;
1991 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1992 talloc_free(frame);
1993 return NULL;
1996 methods = pytalloc_get_ptr(self);
1998 group_sid = pytalloc_get_ptr(py_group_sid);
2000 status = methods->enum_group_members(methods, frame, group_sid,
2001 &member_rids, &num_members);
2002 if (!NT_STATUS_IS_OK(status)) {
2003 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
2004 NT_STATUS_V(status),
2005 get_friendly_nt_error_msg(status));
2006 talloc_free(frame);
2007 return NULL;
2010 py_sid_list = PyList_New(0);
2011 if (py_sid_list == NULL) {
2012 PyErr_NoMemory();
2013 talloc_free(frame);
2014 return NULL;
2017 domain_sid = get_global_sam_sid();
2019 for(i=0; i<num_members; i++) {
2020 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2021 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
2024 talloc_free(frame);
2025 return py_sid_list;
2029 static PyObject *py_pdb_enum_group_memberships(pytalloc_Object *self, PyObject *args)
2031 TALLOC_CTX *frame = talloc_stackframe();
2032 NTSTATUS status;
2033 struct pdb_methods *methods;
2034 int i;
2036 struct samu *sam_acct;
2037 PyObject *py_sam_acct;
2038 PyObject *py_sid_list;
2039 struct dom_sid *user_group_sids = NULL;
2040 gid_t *user_group_ids = NULL;
2041 uint32_t num_groups = 0;
2043 if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2044 talloc_free(frame);
2045 return NULL;
2048 methods = pytalloc_get_ptr(self);
2050 sam_acct = pytalloc_get_ptr(py_sam_acct);
2052 status = methods->enum_group_memberships(methods, frame, sam_acct,
2053 &user_group_sids, &user_group_ids, &num_groups);
2054 if (!NT_STATUS_IS_OK(status)) {
2055 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2056 NT_STATUS_V(status),
2057 get_friendly_nt_error_msg(status));
2058 talloc_free(frame);
2059 return NULL;
2062 py_sid_list = PyList_New(0);
2063 if (py_sid_list == NULL) {
2064 PyErr_NoMemory();
2065 talloc_free(frame);
2066 return NULL;
2069 for(i=0; i<num_groups; i++) {
2070 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
2073 talloc_free(frame);
2074 return py_sid_list;
2078 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
2080 TALLOC_CTX *frame = talloc_stackframe();
2081 NTSTATUS status;
2082 struct pdb_methods *methods;
2083 uint32_t group_rid, member_rid;
2085 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2086 talloc_free(frame);
2087 return NULL;
2090 methods = pytalloc_get_ptr(self);
2092 status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2093 if (!NT_STATUS_IS_OK(status)) {
2094 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2095 NT_STATUS_V(status),
2096 get_friendly_nt_error_msg(status));
2097 talloc_free(frame);
2098 return NULL;
2101 Py_RETURN_NONE;
2102 talloc_free(frame);
2106 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
2108 TALLOC_CTX *frame = talloc_stackframe();
2109 NTSTATUS status;
2110 struct pdb_methods *methods;
2111 uint32_t group_rid, member_rid;
2113 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2114 talloc_free(frame);
2115 return NULL;
2118 methods = pytalloc_get_ptr(self);
2120 status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2121 if (!NT_STATUS_IS_OK(status)) {
2122 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2123 NT_STATUS_V(status),
2124 get_friendly_nt_error_msg(status));
2125 talloc_free(frame);
2126 return NULL;
2129 Py_RETURN_NONE;
2130 talloc_free(frame);
2134 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
2136 TALLOC_CTX *frame = talloc_stackframe();
2137 NTSTATUS status;
2138 struct pdb_methods *methods;
2139 const char *alias_name;
2140 uint32_t rid;
2142 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2143 talloc_free(frame);
2144 return NULL;
2147 methods = pytalloc_get_ptr(self);
2149 status = methods->create_alias(methods, alias_name, &rid);
2150 if (!NT_STATUS_IS_OK(status)) {
2151 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2152 alias_name,
2153 NT_STATUS_V(status),
2154 get_friendly_nt_error_msg(status));
2155 talloc_free(frame);
2156 return NULL;
2159 talloc_free(frame);
2160 return PyInt_FromLong(rid);
2164 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2166 TALLOC_CTX *frame = talloc_stackframe();
2167 NTSTATUS status;
2168 struct pdb_methods *methods;
2169 PyObject *py_alias_sid;
2170 struct dom_sid *alias_sid;
2172 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2173 talloc_free(frame);
2174 return NULL;
2177 methods = pytalloc_get_ptr(self);
2179 alias_sid = pytalloc_get_ptr(py_alias_sid);
2181 status = methods->delete_alias(methods, alias_sid);
2182 if (!NT_STATUS_IS_OK(status)) {
2183 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2184 NT_STATUS_V(status),
2185 get_friendly_nt_error_msg(status));
2186 talloc_free(frame);
2187 return NULL;
2190 Py_RETURN_NONE;
2191 talloc_free(frame);
2195 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2197 TALLOC_CTX *frame = talloc_stackframe();
2198 NTSTATUS status;
2199 struct pdb_methods *methods;
2200 PyObject *py_alias_sid;
2201 struct dom_sid *alias_sid;
2202 struct acct_info *alias_info;
2203 PyObject *py_alias_info;
2205 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2206 talloc_free(frame);
2207 return NULL;
2210 methods = pytalloc_get_ptr(self);
2212 alias_sid = pytalloc_get_ptr(py_alias_sid);
2214 alias_info = talloc_zero(frame, struct acct_info);
2215 if (!alias_info) {
2216 PyErr_NoMemory();
2217 talloc_free(frame);
2218 return NULL;
2221 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2222 if (!NT_STATUS_IS_OK(status)) {
2223 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2224 NT_STATUS_V(status),
2225 get_friendly_nt_error_msg(status));
2226 talloc_free(frame);
2227 return NULL;
2230 py_alias_info = PyDict_New();
2231 if (py_alias_info == NULL) {
2232 PyErr_NoMemory();
2233 talloc_free(frame);
2234 return NULL;
2237 PyDict_SetItemString(py_alias_info, "acct_name",
2238 PyString_FromString(alias_info->acct_name));
2239 PyDict_SetItemString(py_alias_info, "acct_desc",
2240 PyString_FromString(alias_info->acct_desc));
2241 PyDict_SetItemString(py_alias_info, "rid",
2242 PyInt_FromLong(alias_info->rid));
2244 talloc_free(frame);
2245 return py_alias_info;
2249 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2251 TALLOC_CTX *frame = talloc_stackframe();
2252 NTSTATUS status;
2253 struct pdb_methods *methods;
2254 PyObject *py_alias_sid, *py_alias_info;
2255 struct dom_sid *alias_sid;
2256 struct acct_info alias_info;
2258 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2259 &py_alias_info)) {
2260 talloc_free(frame);
2261 return NULL;
2264 methods = pytalloc_get_ptr(self);
2266 alias_sid = pytalloc_get_ptr(py_alias_sid);
2268 alias_info.acct_name = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2269 if (alias_info.acct_name == NULL) {
2270 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2271 talloc_free(frame);
2272 return NULL;
2274 alias_info.acct_desc = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2275 if (alias_info.acct_desc == NULL) {
2276 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2277 talloc_free(frame);
2278 return NULL;
2281 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2282 if (!NT_STATUS_IS_OK(status)) {
2283 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2284 NT_STATUS_V(status),
2285 get_friendly_nt_error_msg(status));
2286 talloc_free(frame);
2287 return NULL;
2290 Py_RETURN_NONE;
2291 talloc_free(frame);
2295 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2297 TALLOC_CTX *frame = talloc_stackframe();
2298 NTSTATUS status;
2299 struct pdb_methods *methods;
2300 PyObject *py_alias_sid, *py_member_sid;
2301 struct dom_sid *alias_sid, *member_sid;
2303 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2304 dom_sid_Type, &py_member_sid)) {
2305 talloc_free(frame);
2306 return NULL;
2309 methods = pytalloc_get_ptr(self);
2311 alias_sid = pytalloc_get_ptr(py_alias_sid);
2312 member_sid = pytalloc_get_ptr(py_member_sid);
2314 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2315 if (!NT_STATUS_IS_OK(status)) {
2316 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2317 NT_STATUS_V(status),
2318 get_friendly_nt_error_msg(status));
2319 talloc_free(frame);
2320 return NULL;
2323 Py_RETURN_NONE;
2324 talloc_free(frame);
2328 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2330 TALLOC_CTX *frame = talloc_stackframe();
2331 NTSTATUS status;
2332 struct pdb_methods *methods;
2333 PyObject *py_alias_sid, *py_member_sid;
2334 const struct dom_sid *alias_sid, *member_sid;
2336 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2337 dom_sid_Type, &py_member_sid)) {
2338 talloc_free(frame);
2339 return NULL;
2342 methods = pytalloc_get_ptr(self);
2344 alias_sid = pytalloc_get_ptr(py_alias_sid);
2345 member_sid = pytalloc_get_ptr(py_member_sid);
2347 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2348 if (!NT_STATUS_IS_OK(status)) {
2349 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2350 NT_STATUS_V(status),
2351 get_friendly_nt_error_msg(status));
2352 talloc_free(frame);
2353 return NULL;
2356 Py_RETURN_NONE;
2357 talloc_free(frame);
2361 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2363 TALLOC_CTX *frame = talloc_stackframe();
2364 NTSTATUS status;
2365 struct pdb_methods *methods;
2366 PyObject *py_alias_sid;
2367 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2368 PyObject *py_member_list, *py_member_sid;
2369 size_t num_members;
2370 int i;
2372 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2373 talloc_free(frame);
2374 return NULL;
2377 methods = pytalloc_get_ptr(self);
2379 alias_sid = pytalloc_get_ptr(py_alias_sid);
2381 status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2382 if (!NT_STATUS_IS_OK(status)) {
2383 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2384 NT_STATUS_V(status),
2385 get_friendly_nt_error_msg(status));
2386 talloc_free(frame);
2387 return NULL;
2390 py_member_list = PyList_New(0);
2391 if (py_member_list == NULL) {
2392 PyErr_NoMemory();
2393 talloc_free(frame);
2394 return NULL;
2397 for(i=0; i<num_members; i++) {
2398 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2399 if (py_member_sid == NULL) {
2400 PyErr_NoMemory();
2401 talloc_free(frame);
2402 return NULL;
2404 tmp_sid = pytalloc_get_ptr(py_member_sid);
2405 *tmp_sid = member_sid[i];
2406 PyList_Append(py_member_list, py_member_sid);
2409 talloc_free(frame);
2410 return py_member_list;
2414 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2416 TALLOC_CTX *frame = talloc_stackframe();
2417 NTSTATUS status;
2418 struct pdb_methods *methods;
2419 PyObject *py_acct_policy;
2420 uint32_t value;
2421 const char **names;
2422 int count, i;
2423 enum pdb_policy_type type;
2425 methods = pytalloc_get_ptr(self);
2427 py_acct_policy = PyDict_New();
2428 if (py_acct_policy == NULL) {
2429 PyErr_NoMemory();
2430 talloc_free(frame);
2431 return NULL;
2434 account_policy_names_list(frame, &names, &count);
2435 for (i=0; i<count; i++) {
2436 type = account_policy_name_to_typenum(names[i]);
2437 status = methods->get_account_policy(methods, type, &value);
2438 if (NT_STATUS_IS_OK(status)) {
2439 PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2443 talloc_free(frame);
2444 return py_acct_policy;
2448 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2450 TALLOC_CTX *frame = talloc_stackframe();
2451 NTSTATUS status;
2452 struct pdb_methods *methods;
2453 PyObject *py_acct_policy, *py_value;
2454 const char **names;
2455 int count, i;
2456 enum pdb_policy_type type;
2458 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2459 talloc_free(frame);
2460 return NULL;
2463 methods = pytalloc_get_ptr(self);
2465 account_policy_names_list(frame, &names, &count);
2466 for (i=0; i<count; i++) {
2467 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2468 type = account_policy_name_to_typenum(names[i]);
2469 status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2470 if (!NT_STATUS_IS_OK(status)) {
2471 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2472 names[i],
2473 NT_STATUS_V(status),
2474 get_friendly_nt_error_msg(status));
2479 Py_RETURN_NONE;
2480 talloc_free(frame);
2483 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2485 TALLOC_CTX *frame = talloc_stackframe();
2486 struct pdb_methods *methods;
2487 unsigned int acct_flags;
2488 struct pdb_search *search;
2489 struct samr_displayentry *entry;
2490 PyObject *py_userlist, *py_dict;
2492 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2493 talloc_free(frame);
2494 return NULL;
2497 methods = pytalloc_get_ptr(self);
2499 search = talloc_zero(frame, struct pdb_search);
2500 if (search == NULL) {
2501 PyErr_NoMemory();
2502 talloc_free(frame);
2503 return NULL;
2506 if (!methods->search_users(methods, search, acct_flags)) {
2507 PyErr_Format(py_pdb_error, "Unable to search users");
2508 talloc_free(frame);
2509 return NULL;
2512 entry = talloc_zero(frame, struct samr_displayentry);
2513 if (entry == NULL) {
2514 PyErr_NoMemory();
2515 talloc_free(frame);
2516 return NULL;
2519 py_userlist = PyList_New(0);
2520 if (py_userlist == NULL) {
2521 PyErr_NoMemory();
2522 talloc_free(frame);
2523 return NULL;
2526 while (search->next_entry(search, entry)) {
2527 py_dict = PyDict_New();
2528 if (py_dict == NULL) {
2529 PyErr_NoMemory();
2530 } else {
2531 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2532 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2533 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2534 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2535 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2536 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2537 PyList_Append(py_userlist, py_dict);
2540 search->search_end(search);
2542 talloc_free(frame);
2543 return py_userlist;
2547 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2549 TALLOC_CTX *frame = talloc_stackframe();
2550 struct pdb_methods *methods;
2551 struct pdb_search *search;
2552 struct samr_displayentry *entry;
2553 PyObject *py_grouplist, *py_dict;
2555 methods = pytalloc_get_ptr(self);
2557 search = talloc_zero(frame, struct pdb_search);
2558 if (search == NULL) {
2559 PyErr_NoMemory();
2560 talloc_free(frame);
2561 return NULL;
2564 if (!methods->search_groups(methods, search)) {
2565 PyErr_Format(py_pdb_error, "Unable to search groups");
2566 talloc_free(frame);
2567 return NULL;
2570 entry = talloc_zero(frame, struct samr_displayentry);
2571 if (entry == NULL) {
2572 PyErr_NoMemory();
2573 talloc_free(frame);
2574 return NULL;
2577 py_grouplist = PyList_New(0);
2578 if (py_grouplist == NULL) {
2579 PyErr_NoMemory();
2580 talloc_free(frame);
2581 return NULL;
2584 while (search->next_entry(search, entry)) {
2585 py_dict = PyDict_New();
2586 if (py_dict == NULL) {
2587 PyErr_NoMemory();
2588 } else {
2589 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2590 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2591 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2592 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2593 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2594 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2595 PyList_Append(py_grouplist, py_dict);
2598 search->search_end(search);
2600 talloc_free(frame);
2601 return py_grouplist;
2605 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2607 TALLOC_CTX *frame = talloc_stackframe();
2608 struct pdb_methods *methods;
2609 struct pdb_search *search;
2610 struct samr_displayentry *entry;
2611 PyObject *py_aliaslist, *py_dict;
2612 PyObject *py_domain_sid;
2613 struct dom_sid *domain_sid = NULL;
2615 py_domain_sid = Py_None;
2616 Py_INCREF(Py_None);
2618 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2619 talloc_free(frame);
2620 return NULL;
2623 methods = pytalloc_get_ptr(self);
2625 if (py_domain_sid != Py_None) {
2626 domain_sid = pytalloc_get_ptr(py_domain_sid);
2629 search = talloc_zero(frame, struct pdb_search);
2630 if (search == NULL) {
2631 PyErr_NoMemory();
2632 talloc_free(frame);
2633 return NULL;
2636 if (!methods->search_aliases(methods, search, domain_sid)) {
2637 PyErr_Format(py_pdb_error, "Unable to search aliases");
2638 talloc_free(frame);
2639 return NULL;
2642 entry = talloc_zero(frame, struct samr_displayentry);
2643 if (entry == NULL) {
2644 PyErr_NoMemory();
2645 talloc_free(frame);
2646 return NULL;
2649 py_aliaslist = PyList_New(0);
2650 if (py_aliaslist == NULL) {
2651 PyErr_NoMemory();
2652 talloc_free(frame);
2653 return NULL;
2656 while (search->next_entry(search, entry)) {
2657 py_dict = PyDict_New();
2658 if (py_dict == NULL) {
2659 PyErr_NoMemory();
2660 } else {
2661 PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2662 PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2663 PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2664 PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2665 PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2666 PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2667 PyList_Append(py_aliaslist, py_dict);
2670 search->search_end(search);
2672 talloc_free(frame);
2673 return py_aliaslist;
2677 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2679 TALLOC_CTX *frame = talloc_stackframe();
2680 struct pdb_methods *methods;
2681 unsigned int uid;
2682 struct dom_sid user_sid, *copy_user_sid;
2683 PyObject *py_user_sid;
2685 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2686 talloc_free(frame);
2687 return NULL;
2690 methods = pytalloc_get_ptr(self);
2692 if (!methods->uid_to_sid(methods, uid, &user_sid)) {
2693 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2694 talloc_free(frame);
2695 return NULL;
2698 copy_user_sid = dom_sid_dup(frame, &user_sid);
2699 if (copy_user_sid == NULL) {
2700 PyErr_NoMemory();
2701 talloc_free(frame);
2702 return NULL;
2705 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2707 talloc_free(frame);
2708 return py_user_sid;
2712 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2714 TALLOC_CTX *frame = talloc_stackframe();
2715 struct pdb_methods *methods;
2716 unsigned int gid;
2717 struct dom_sid group_sid, *copy_group_sid;
2718 PyObject *py_group_sid;
2720 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2721 talloc_free(frame);
2722 return NULL;
2725 methods = pytalloc_get_ptr(self);
2727 if (!methods->gid_to_sid(methods, gid, &group_sid)) {
2728 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2729 talloc_free(frame);
2730 return NULL;
2733 copy_group_sid = dom_sid_dup(frame, &group_sid);
2734 if (copy_group_sid == NULL) {
2735 PyErr_NoMemory();
2736 talloc_free(frame);
2737 return NULL;
2740 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2742 talloc_free(frame);
2743 return py_group_sid;
2747 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2749 TALLOC_CTX *frame = talloc_stackframe();
2750 struct pdb_methods *methods;
2751 PyObject *py_sid;
2752 struct dom_sid *sid;
2753 struct unixid id;
2755 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2756 talloc_free(frame);
2757 return NULL;
2760 methods = pytalloc_get_ptr(self);
2762 sid = pytalloc_get_ptr(py_sid);
2764 if (!methods->sid_to_id(methods, sid, &id)) {
2765 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2766 talloc_free(frame);
2767 return NULL;
2770 talloc_free(frame);
2771 return Py_BuildValue("(II)", id.id, id.type);
2775 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2777 TALLOC_CTX *frame = talloc_stackframe();
2778 struct pdb_methods *methods;
2779 uint32_t rid;
2781 methods = pytalloc_get_ptr(self);
2783 if (!methods->new_rid(methods, &rid)) {
2784 PyErr_Format(py_pdb_error, "Unable to get new rid");
2785 talloc_free(frame);
2786 return NULL;
2789 talloc_free(frame);
2790 return PyInt_FromLong(rid);
2794 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2796 TALLOC_CTX *frame = talloc_stackframe();
2797 struct pdb_methods *methods;
2798 const char *domain;
2799 char *pwd;
2800 struct dom_sid sid, *copy_sid;
2801 PyObject *py_sid;
2802 time_t last_set_time;
2803 PyObject *py_value;
2805 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2806 talloc_free(frame);
2807 return NULL;
2810 methods = pytalloc_get_ptr(self);
2812 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2813 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2814 talloc_free(frame);
2815 return NULL;
2818 copy_sid = dom_sid_dup(frame, &sid);
2819 if (copy_sid == NULL) {
2820 PyErr_NoMemory();
2821 talloc_free(frame);
2822 return NULL;
2825 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2826 if (py_sid == NULL) {
2827 PyErr_NoMemory();
2828 talloc_free(frame);
2829 return NULL;
2832 py_value = PyDict_New();
2833 if (py_value == NULL) {
2834 PyErr_NoMemory();
2835 talloc_free(frame);
2836 return NULL;
2839 PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2840 PyDict_SetItemString(py_value, "sid", py_sid);
2841 PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2843 talloc_free(frame);
2844 return py_value;
2848 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2850 TALLOC_CTX *frame = talloc_stackframe();
2851 struct pdb_methods *methods;
2852 const char *domain;
2853 const char *pwd;
2854 const struct dom_sid *domain_sid;
2855 PyObject *py_domain_sid;
2857 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2858 dom_sid_Type, &py_domain_sid)) {
2859 talloc_free(frame);
2860 return NULL;
2863 methods = pytalloc_get_ptr(self);
2865 domain_sid = pytalloc_get_ptr(py_domain_sid);
2867 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2868 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2869 talloc_free(frame);
2870 return NULL;
2873 Py_RETURN_NONE;
2874 talloc_free(frame);
2878 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2880 TALLOC_CTX *frame = talloc_stackframe();
2881 struct pdb_methods *methods;
2882 const char *domain;
2884 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2885 talloc_free(frame);
2886 return NULL;
2889 methods = pytalloc_get_ptr(self);
2891 if (!methods->del_trusteddom_pw(methods, domain)) {
2892 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2893 talloc_free(frame);
2894 return NULL;
2897 Py_RETURN_NONE;
2898 talloc_free(frame);
2902 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2904 TALLOC_CTX *frame = talloc_stackframe();
2905 NTSTATUS status;
2906 struct pdb_methods *methods;
2907 uint32_t num_domains;
2908 struct trustdom_info **domains;
2909 PyObject *py_domain_list, *py_dict;
2910 int i;
2912 methods = pytalloc_get_ptr(self);
2914 status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
2915 if (!NT_STATUS_IS_OK(status)) {
2916 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2917 NT_STATUS_V(status),
2918 get_friendly_nt_error_msg(status));
2919 talloc_free(frame);
2920 return NULL;
2923 py_domain_list = PyList_New(0);
2924 if (py_domain_list == NULL) {
2925 PyErr_NoMemory();
2926 talloc_free(frame);
2927 return NULL;
2930 for(i=0; i<num_domains; i++) {
2931 py_dict = PyDict_New();
2932 if (py_dict) {
2933 PyDict_SetItemString(py_dict, "name",
2934 PyString_FromString(domains[i]->name));
2935 PyDict_SetItemString(py_dict, "sid",
2936 pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2939 PyList_Append(py_domain_list, py_dict);
2942 talloc_free(frame);
2943 return py_domain_list;
2947 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2949 TALLOC_CTX *frame = talloc_stackframe();
2950 NTSTATUS status;
2951 struct pdb_methods *methods;
2952 const char *domain;
2953 struct pdb_trusted_domain *td;
2954 PyObject *py_domain_info;
2956 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2957 talloc_free(frame);
2958 return NULL;
2961 methods = pytalloc_get_ptr(self);
2963 status = methods->get_trusted_domain(methods, frame, domain, &td);
2964 if (!NT_STATUS_IS_OK(status)) {
2965 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2966 NT_STATUS_V(status),
2967 get_friendly_nt_error_msg(status));
2968 talloc_free(frame);
2969 return NULL;
2972 py_domain_info = PyDict_New();
2973 if (py_domain_info == NULL) {
2974 PyErr_NoMemory();
2975 talloc_free(frame);
2976 return NULL;
2979 PyDict_SetItemString(py_domain_info, "domain_name",
2980 PyString_FromString(td->domain_name));
2981 PyDict_SetItemString(py_domain_info, "netbios_name",
2982 PyString_FromString(td->netbios_name));
2983 PyDict_SetItemString(py_domain_info, "security_identifier",
2984 pytalloc_steal(dom_sid_Type, &td->security_identifier));
2985 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2986 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2987 td->trust_auth_incoming.length));
2988 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2989 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2990 td->trust_auth_outgoing.length));
2991 PyDict_SetItemString(py_domain_info, "trust_direction",
2992 PyInt_FromLong(td->trust_direction));
2993 PyDict_SetItemString(py_domain_info, "trust_type",
2994 PyInt_FromLong(td->trust_type));
2995 PyDict_SetItemString(py_domain_info, "trust_attributes",
2996 PyInt_FromLong(td->trust_attributes));
2997 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2998 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2999 td->trust_forest_trust_info.length));
3001 talloc_free(frame);
3002 return py_domain_info;
3006 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
3008 TALLOC_CTX *frame = talloc_stackframe();
3009 NTSTATUS status;
3010 struct pdb_methods *methods;
3011 PyObject *py_domain_sid;
3012 struct dom_sid *domain_sid;
3013 struct pdb_trusted_domain *td;
3014 PyObject *py_domain_info;
3016 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3017 talloc_free(frame);
3018 return NULL;
3021 methods = pytalloc_get_ptr(self);
3023 domain_sid = pytalloc_get_ptr(py_domain_sid);
3025 status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3026 if (!NT_STATUS_IS_OK(status)) {
3027 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3028 NT_STATUS_V(status),
3029 get_friendly_nt_error_msg(status));
3030 talloc_free(frame);
3031 return NULL;
3034 py_domain_info = PyDict_New();
3035 if (py_domain_info == NULL) {
3036 PyErr_NoMemory();
3037 talloc_free(frame);
3038 return NULL;
3041 PyDict_SetItemString(py_domain_info, "domain_name",
3042 PyString_FromString(td->domain_name));
3043 PyDict_SetItemString(py_domain_info, "netbios_name",
3044 PyString_FromString(td->netbios_name));
3045 PyDict_SetItemString(py_domain_info, "security_identifier",
3046 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3047 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3048 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3049 td->trust_auth_incoming.length));
3050 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3051 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3052 td->trust_auth_outgoing.length));
3053 PyDict_SetItemString(py_domain_info, "trust_direction",
3054 PyInt_FromLong(td->trust_direction));
3055 PyDict_SetItemString(py_domain_info, "trust_type",
3056 PyInt_FromLong(td->trust_type));
3057 PyDict_SetItemString(py_domain_info, "trust_attributes",
3058 PyInt_FromLong(td->trust_attributes));
3059 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3060 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3061 td->trust_forest_trust_info.length));
3063 talloc_free(frame);
3064 return py_domain_info;
3068 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3070 TALLOC_CTX *frame = talloc_stackframe();
3071 NTSTATUS status;
3072 struct pdb_methods *methods;
3073 const char *domain;
3074 PyObject *py_td_info;
3075 struct pdb_trusted_domain td_info;
3076 PyObject *py_tmp;
3077 Py_ssize_t len;
3079 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3080 talloc_free(frame);
3081 return NULL;
3084 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3085 td_info.domain_name = PyString_AsString(py_tmp);
3087 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3088 td_info.netbios_name = PyString_AsString(py_tmp);
3090 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3091 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3093 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3094 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3095 td_info.trust_auth_incoming.length = len;
3097 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3098 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3099 td_info.trust_auth_outgoing.length = len;
3101 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3102 td_info.trust_direction = PyInt_AsLong(py_tmp);
3104 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3105 td_info.trust_type = PyInt_AsLong(py_tmp);
3107 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3108 td_info.trust_attributes = PyInt_AsLong(py_tmp);
3110 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3111 PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3112 td_info.trust_forest_trust_info.length = len;
3114 methods = pytalloc_get_ptr(self);
3116 status = methods->set_trusted_domain(methods, domain, &td_info);
3117 if (!NT_STATUS_IS_OK(status)) {
3118 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3119 NT_STATUS_V(status),
3120 get_friendly_nt_error_msg(status));
3121 talloc_free(frame);
3122 return NULL;
3125 Py_RETURN_NONE;
3126 talloc_free(frame);
3130 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3132 TALLOC_CTX *frame = talloc_stackframe();
3133 NTSTATUS status;
3134 struct pdb_methods *methods;
3135 const char *domain;
3137 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3138 talloc_free(frame);
3139 return NULL;
3142 methods = pytalloc_get_ptr(self);
3144 status = methods->del_trusted_domain(methods, domain);
3145 if (!NT_STATUS_IS_OK(status)) {
3146 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3147 NT_STATUS_V(status),
3148 get_friendly_nt_error_msg(status));
3149 talloc_free(frame);
3150 return NULL;
3153 Py_RETURN_NONE;
3154 talloc_free(frame);
3158 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3160 TALLOC_CTX *frame = talloc_stackframe();
3161 NTSTATUS status;
3162 struct pdb_methods *methods;
3163 uint32_t num_domains;
3164 struct pdb_trusted_domain **td_info, *td;
3165 PyObject *py_td_info, *py_domain_info;
3166 int i;
3168 methods = pytalloc_get_ptr(self);
3170 status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3171 if (!NT_STATUS_IS_OK(status)) {
3172 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3173 NT_STATUS_V(status),
3174 get_friendly_nt_error_msg(status));
3175 talloc_free(frame);
3176 return NULL;
3179 py_td_info = PyList_New(0);
3180 if (py_td_info == NULL) {
3181 PyErr_NoMemory();
3182 talloc_free(frame);
3183 return NULL;
3186 for (i=0; i<num_domains; i++) {
3188 py_domain_info = PyDict_New();
3189 if (py_domain_info == NULL) {
3190 PyErr_NoMemory();
3191 Py_DECREF(py_td_info);
3192 talloc_free(frame);
3193 return NULL;
3196 td = td_info[i];
3198 PyDict_SetItemString(py_domain_info, "domain_name",
3199 PyString_FromString(td->domain_name));
3200 PyDict_SetItemString(py_domain_info, "netbios_name",
3201 PyString_FromString(td->netbios_name));
3202 PyDict_SetItemString(py_domain_info, "security_identifier",
3203 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3204 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3205 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3206 td->trust_auth_incoming.length));
3207 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3208 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3209 td->trust_auth_outgoing.length));
3210 PyDict_SetItemString(py_domain_info, "trust_direction",
3211 PyInt_FromLong(td->trust_direction));
3212 PyDict_SetItemString(py_domain_info, "trust_type",
3213 PyInt_FromLong(td->trust_type));
3214 PyDict_SetItemString(py_domain_info, "trust_attributes",
3215 PyInt_FromLong(td->trust_attributes));
3216 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3217 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3218 td->trust_forest_trust_info.length));
3219 PyList_Append(py_td_info, py_domain_info);
3222 talloc_free(frame);
3223 return py_td_info;
3227 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3229 TALLOC_CTX *frame = talloc_stackframe();
3230 NTSTATUS status;
3231 struct pdb_methods *methods;
3232 const char *secret_name;
3233 DATA_BLOB secret_current, secret_old;
3234 NTTIME secret_current_lastchange, secret_old_lastchange;
3235 PyObject *py_sd;
3236 struct security_descriptor *sd;
3237 PyObject *py_secret;
3239 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3240 talloc_free(frame);
3241 return NULL;
3244 methods = pytalloc_get_ptr(self);
3246 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3247 if (py_sd == NULL) {
3248 PyErr_NoMemory();
3249 talloc_free(frame);
3250 return NULL;
3252 sd = pytalloc_get_ptr(py_sd);
3254 status = methods->get_secret(methods, frame, secret_name,
3255 &secret_current,
3256 &secret_current_lastchange,
3257 &secret_old,
3258 &secret_old_lastchange,
3259 &sd);
3260 if (!NT_STATUS_IS_OK(status)) {
3261 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3262 secret_name,
3263 NT_STATUS_V(status),
3264 get_friendly_nt_error_msg(status));
3265 talloc_free(frame);
3266 return NULL;
3269 py_secret = PyDict_New();
3270 if (py_secret == NULL) {
3271 PyErr_NoMemory();
3272 Py_DECREF(py_sd);
3273 talloc_free(frame);
3274 return NULL;
3277 PyDict_SetItemString(py_secret, "secret_current",
3278 PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3279 PyDict_SetItemString(py_secret, "secret_current_lastchange",
3280 PyLong_FromUnsignedLongLong(secret_current_lastchange));
3281 PyDict_SetItemString(py_secret, "secret_old",
3282 PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3283 PyDict_SetItemString(py_secret, "secret_old_lastchange",
3284 PyLong_FromUnsignedLongLong(secret_old_lastchange));
3285 PyDict_SetItemString(py_secret, "sd", py_sd);
3287 talloc_free(frame);
3288 return py_secret;
3292 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3294 TALLOC_CTX *frame = talloc_stackframe();
3295 NTSTATUS status;
3296 struct pdb_methods *methods;
3297 const char *secret_name;
3298 PyObject *py_secret;
3299 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3300 DATA_BLOB secret_current, secret_old;
3301 struct security_descriptor *sd;
3302 Py_ssize_t len;
3304 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3305 talloc_free(frame);
3306 return NULL;
3309 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3310 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3311 py_sd = PyDict_GetItemString(py_secret, "sd");
3313 PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3314 PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3315 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3317 methods = pytalloc_get_ptr(self);
3319 PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3320 secret_current.length = len;
3321 PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3322 secret_current.length = len;
3323 sd = pytalloc_get_ptr(py_sd);
3325 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3328 secret_name,
3329 NT_STATUS_V(status),
3330 get_friendly_nt_error_msg(status));
3331 talloc_free(frame);
3332 return NULL;
3335 Py_RETURN_NONE;
3336 talloc_free(frame);
3340 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3342 TALLOC_CTX *frame = talloc_stackframe();
3343 NTSTATUS status;
3344 struct pdb_methods *methods;
3345 const char *secret_name;
3347 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3348 talloc_free(frame);
3349 return NULL;
3352 methods = pytalloc_get_ptr(self);
3354 status = methods->delete_secret(methods, secret_name);
3355 if (!NT_STATUS_IS_OK(status)) {
3356 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3357 secret_name,
3358 NT_STATUS_V(status),
3359 get_friendly_nt_error_msg(status));
3360 talloc_free(frame);
3361 return NULL;
3364 Py_RETURN_NONE;
3365 talloc_free(frame);
3368 static PyMethodDef py_pdb_methods[] = {
3369 { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3370 "domain_info() -> str\n\n \
3371 Get domain information for the database." },
3372 { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3373 "getsampwnam(username) -> samu object\n\n \
3374 Get user information by name." },
3375 { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3376 "getsampwsid(user_sid) -> samu object\n\n \
3377 Get user information by sid (dcerpc.security.dom_sid object)." },
3378 { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3379 "create_user(username, acct_flags) -> rid\n\n \
3380 Create user. acct_flags are samr account control flags." },
3381 { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3382 "delete_user(samu object) -> None\n\n \
3383 Delete user." },
3384 { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3385 "add_sam_account(samu object) -> None\n\n \
3386 Add SAM account." },
3387 { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3388 "update_sam_account(samu object) -> None\n\n \
3389 Update SAM account." },
3390 { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3391 "delete_sam_account(samu object) -> None\n\n \
3392 Delete SAM account." },
3393 { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3394 "rename_sam_account(samu object1, new_username) -> None\n\n \
3395 Rename SAM account." },
3396 /* update_login_attempts */
3397 { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3398 "getgrsid(group_sid) -> groupmap object\n\n \
3399 Get group information by sid (dcerpc.security.dom_sid object)." },
3400 { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3401 "getgrsid(gid) -> groupmap object\n\n \
3402 Get group information by gid." },
3403 { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3404 "getgrsid(groupname) -> groupmap object\n\n \
3405 Get group information by name." },
3406 { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3407 "create_dom_group(groupname) -> group_rid\n\n \
3408 Create new domain group by name." },
3409 { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3410 "delete_dom_group(group_rid) -> None\n\n \
3411 Delete domain group identified by rid" },
3412 { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3413 "add_group_mapping_entry(groupmap) -> None\n \
3414 Add group mapping entry for groupmap object." },
3415 { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3416 "update_group_mapping_entry(groupmap) -> None\n\n \
3417 Update group mapping entry for groupmap object." },
3418 { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3419 "delete_group_mapping_entry(groupmap) -> None\n\n \
3420 Delete group mapping entry for groupmap object." },
3421 { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3422 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3423 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3424 { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3425 "enum_group_members(group_sid) -> List\n\n \
3426 Return list of users (dom_sid object) in group." },
3427 { "enum_group_memberships", (PyCFunction)py_pdb_enum_group_memberships, METH_VARARGS,
3428 "enum_group_memberships(samu object) -> List\n\n \
3429 Return list of groups (dom_sid object) this user is part of." },
3430 /* set_unix_primary_group */
3431 { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3432 "add_groupmem(group_rid, member_rid) -> None\n\n \
3433 Add user to group." },
3434 { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3435 "del_groupmem(group_rid, member_rid) -> None\n\n \
3436 Remove user from from group." },
3437 { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3438 "create_alias(alias_name) -> alias_rid\n\n \
3439 Create alias entry." },
3440 { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3441 "delete_alias(alias_sid) -> None\n\n \
3442 Delete alias entry." },
3443 { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3444 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3445 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3446 { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3447 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3448 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3449 { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3450 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3451 Add user to alias entry." },
3452 { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3453 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3454 Remove a user from alias entry." },
3455 { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3456 "enum_aliasmem(alias_sid) -> List\n\n \
3457 Return a list of members (dom_sid object) for alias entry." },
3458 /* enum_alias_memberships */
3459 /* lookup_rids */
3460 /* lookup_names */
3461 { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3462 "get_account_policy() -> Mapping\n\n \
3463 Get account policy information as a dictionary." },
3464 { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3465 "get_account_policy(Mapping) -> None\n\n \
3466 Set account policy settings from a dicionary." },
3467 /* get_seq_num */
3468 { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3469 "search_users(acct_flags) -> List\n\n \
3470 Search users. acct_flags are samr account control flags.\n \
3471 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3472 { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3473 "search_groups() -> List\n\n \
3474 Search unix only groups. \n \
3475 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3476 { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3477 "search_aliases([domain_sid]) -> List\n\n \
3478 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3479 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3480 { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3481 "uid_to_sid(uid) -> sid\n\n \
3482 Return sid for given user id." },
3483 { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3484 "gid_to_sid(gid) -> sid\n\n \
3485 Return sid for given group id." },
3486 { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3487 "sid_to_id(sid) -> Tuple\n\n \
3488 Return id and type for given sid." },
3489 /* capabilities */
3490 { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3491 "new_rid() -> rid\n\n \
3492 Get a new rid." },
3493 { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3494 "get_trusteddom_pw(domain) -> Mapping\n\n \
3495 Get trusted domain password, sid and last set time in a dictionary." },
3496 { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3497 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3498 Set trusted domain password." },
3499 { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3500 "del_trusteddom_pw(domain) -> None\n\n \
3501 Delete trusted domain password." },
3502 { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3503 "enum_trusteddoms() -> List\n\n \
3504 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3505 { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3506 "get_trusted_domain(domain) -> Mapping\n\n \
3507 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." },
3508 { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3509 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3510 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" },
3511 { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3512 "set_trusted_domain(domain, Mapping) -> None\n\n \
3513 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." },
3514 { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3515 "del_trusted_domain(domain) -> None\n\n \
3516 Delete trusted domain." },
3517 { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3518 "enum_trusted_domains() -> List\n\n \
3519 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." },
3520 { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3521 "get_secret(secret_name) -> Mapping\n\n \
3522 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3523 { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3524 "set_secret(secret_name, Mapping) -> None\n\n \
3525 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3526 { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3527 "delete_secret(secret_name) -> None\n\n \
3528 Delete secret information for secret_name." },
3529 { NULL },
3533 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3535 TALLOC_CTX *frame = talloc_stackframe();
3536 const char *url = NULL;
3537 PyObject *pypdb;
3538 NTSTATUS status;
3539 struct pdb_methods *methods;
3541 if (!PyArg_ParseTuple(args, "s", &url)) {
3542 talloc_free(frame);
3543 return NULL;
3546 /* Initalize list of methods */
3547 status = make_pdb_method_name(&methods, url);
3548 if (!NT_STATUS_IS_OK(status)) {
3549 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3550 url,
3551 NT_STATUS_V(status),
3552 get_friendly_nt_error_msg(status));
3553 talloc_free(frame);
3554 return NULL;
3557 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3558 PyErr_NoMemory();
3559 talloc_free(frame);
3560 return NULL;
3563 talloc_free(frame);
3564 return pypdb;
3568 static PyTypeObject PyPDB = {
3569 .tp_name = "passdb.PDB",
3570 .tp_basicsize = sizeof(pytalloc_Object),
3571 .tp_new = py_pdb_new,
3572 .tp_flags = Py_TPFLAGS_DEFAULT,
3573 .tp_methods = py_pdb_methods,
3574 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3579 * Return a list of passdb backends
3581 static PyObject *py_passdb_backends(PyObject *self)
3583 TALLOC_CTX *frame = talloc_stackframe();
3584 PyObject *py_blist;
3585 const struct pdb_init_function_entry *entry;
3587 entry = pdb_get_backends();
3588 if(! entry) {
3589 Py_RETURN_NONE;
3592 if((py_blist = PyList_New(0)) == NULL) {
3593 PyErr_NoMemory();
3594 talloc_free(frame);
3595 return NULL;
3598 while(entry) {
3599 PyList_Append(py_blist, PyString_FromString(entry->name));
3600 entry = entry->next;
3603 talloc_free(frame);
3604 return py_blist;
3608 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3610 TALLOC_CTX *frame = talloc_stackframe();
3611 const char *smb_config;
3613 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3614 talloc_free(frame);
3615 return NULL;
3618 /* Load smbconf parameters */
3619 if (!lp_load_global(smb_config)) {
3620 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3621 talloc_free(frame);
3622 return NULL;
3625 Py_RETURN_NONE;
3626 talloc_free(frame);
3630 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3632 TALLOC_CTX *frame = talloc_stackframe();
3633 const char *private_dir;
3635 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3636 talloc_free(frame);
3637 return NULL;
3640 /* Initialize secrets database */
3641 if (!secrets_init_path(private_dir, lp_use_ntdb())) {
3642 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3643 private_dir);
3644 talloc_free(frame);
3645 return NULL;
3648 talloc_free(frame);
3649 Py_RETURN_NONE;
3652 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3654 TALLOC_CTX *frame = talloc_stackframe();
3656 /* Initialize secrets database */
3657 if (!initialize_password_db(true, NULL)) {
3658 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3659 talloc_free(frame);
3660 return NULL;
3663 talloc_free(frame);
3664 Py_RETURN_NONE;
3667 static PyObject *py_get_global_sam_sid(PyObject *self)
3669 TALLOC_CTX *frame = talloc_stackframe();
3670 struct dom_sid *domain_sid, *domain_sid_copy;
3671 PyObject *py_dom_sid;
3673 domain_sid = get_global_sam_sid();
3675 domain_sid_copy = dom_sid_dup(frame, domain_sid);
3676 if (domain_sid_copy == NULL) {
3677 PyErr_NoMemory();
3678 talloc_free(frame);
3679 return NULL;
3682 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3684 talloc_free(frame);
3685 return py_dom_sid;
3689 static PyMethodDef py_passdb_methods[] = {
3690 { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3691 "get_backends() -> list\n\n \
3692 Get a list of password database backends supported." },
3693 { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3694 "set_smb_config(path) -> None\n\n \
3695 Set path to smb.conf file to load configuration parameters." },
3696 { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3697 "set_secrets_dir(private_dir) -> None\n\n \
3698 Set path to private directory to load secrets database from non-default location." },
3699 { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3700 "get_global_sam_sid() -> dom_sid\n\n \
3701 Return domain SID." },
3702 { "reload_static_pdb", (PyCFunction)py_reload_static_pdb, METH_NOARGS,
3703 "reload_static_pdb() -> None\n\n \
3704 Re-initalise the static pdb used internally. Needed if 'passdb backend' is changed." },
3705 { NULL },
3708 void initpassdb(void)
3710 TALLOC_CTX *frame = talloc_stackframe();
3711 PyObject *m, *mod;
3712 char exception_name[] = "passdb.error";
3714 PyTypeObject *talloc_type = pytalloc_GetObjectType();
3715 if (talloc_type == NULL) {
3716 talloc_free(frame);
3717 return;
3720 PyPDB.tp_base = talloc_type;
3721 if (PyType_Ready(&PyPDB) < 0) {
3722 talloc_free(frame);
3723 return;
3726 PySamu.tp_base = talloc_type;
3727 if (PyType_Ready(&PySamu) < 0) {
3728 talloc_free(frame);
3729 return;
3732 PyGroupmap.tp_base = talloc_type;
3733 if (PyType_Ready(&PyGroupmap) < 0) {
3734 talloc_free(frame);
3735 return;
3738 m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3739 if (m == NULL) {
3740 talloc_free(frame);
3741 return;
3744 /* Create new exception for passdb module */
3745 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3746 Py_INCREF(py_pdb_error);
3747 PyModule_AddObject(m, "error", py_pdb_error);
3749 Py_INCREF(&PyPDB);
3750 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3752 Py_INCREF(&PySamu);
3753 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3755 Py_INCREF(&PyGroupmap);
3756 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3758 /* Import dom_sid type from dcerpc.security */
3759 mod = PyImport_ImportModule("samba.dcerpc.security");
3760 if (mod == NULL) {
3761 talloc_free(frame);
3762 return;
3765 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3766 if (dom_sid_Type == NULL) {
3767 talloc_free(frame);
3768 return;
3771 /* Import security_descriptor type from dcerpc.security */
3772 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3773 Py_DECREF(mod);
3774 if (security_Type == NULL) {
3775 talloc_free(frame);
3776 return;
3779 /* Import GUID type from dcerpc.misc */
3780 mod = PyImport_ImportModule("samba.dcerpc.misc");
3781 if (mod == NULL) {
3782 talloc_free(frame);
3783 return;
3786 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3787 Py_DECREF(mod);
3788 if (guid_Type == NULL) {
3789 talloc_free(frame);
3790 return;
3792 talloc_free(frame);