smbd: use fsp->conn->session_info for the initial delete-on-close token
[Samba.git] / source3 / passdb / py_passdb.c
blobeb9239700c14652f1879e4c264b94190e43b8cb6
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 "python/py3compat.h"
24 #include "lib/util/talloc_stack.h"
25 #include "libcli/security/security.h"
26 #include "librpc/gen_ndr/idmap.h"
27 #include "passdb.h"
28 #include "secrets.h"
29 #include "idmap.h"
30 #include "lib/util/string_wrappers.h"
32 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
33 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
34 #endif
36 #ifndef PY_CHECK_TYPE
37 #define PY_CHECK_TYPE(type, var, fail) \
38 if (!PyObject_TypeCheck(var, type)) {\
39 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
40 fail; \
42 #endif
45 static PyTypeObject *dom_sid_Type = NULL;
46 static PyTypeObject *security_Type = NULL;
47 static PyTypeObject *guid_Type = NULL;
49 static PyTypeObject PySamu;
50 static PyTypeObject PyGroupmap;
51 static PyTypeObject PyPDB;
53 static PyObject *py_pdb_error;
55 void initpassdb(void);
58 /************************** PIDL Autogeneratd ******************************/
60 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
62 TALLOC_CTX *frame = talloc_stackframe();
63 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
64 PyObject *py_logon_time;
66 py_logon_time = PyLong_FromLong(pdb_get_logon_time(sam_acct));
67 talloc_free(frame);
68 return py_logon_time;
71 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
73 TALLOC_CTX *frame = talloc_stackframe();
74 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
76 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
77 if (!pdb_set_logon_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
78 talloc_free(frame);
79 return -1;
81 talloc_free(frame);
82 return 0;
85 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
87 TALLOC_CTX *frame = talloc_stackframe();
88 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
89 PyObject *py_logoff_time;
91 py_logoff_time = PyLong_FromLong(pdb_get_logoff_time(sam_acct));
92 talloc_free(frame);
93 return py_logoff_time;
96 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
98 TALLOC_CTX *frame = talloc_stackframe();
99 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
101 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
102 if (!pdb_set_logoff_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
103 talloc_free(frame);
104 return -1;
106 talloc_free(frame);
107 return 0;
110 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
112 TALLOC_CTX *frame = talloc_stackframe();
113 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
114 PyObject *py_kickoff_time;
116 py_kickoff_time = PyLong_FromLong(pdb_get_kickoff_time(sam_acct));
117 talloc_free(frame);
118 return py_kickoff_time;
121 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
126 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
127 if (!pdb_set_kickoff_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
128 talloc_free(frame);
129 return -1;
131 talloc_free(frame);
132 return 0;
135 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
137 TALLOC_CTX *frame = talloc_stackframe();
138 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
139 PyObject *py_bad_password_time;
141 py_bad_password_time = PyLong_FromLong(pdb_get_bad_password_time(sam_acct));
142 talloc_free(frame);
143 return py_bad_password_time;
146 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
148 TALLOC_CTX *frame = talloc_stackframe();
149 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
151 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
152 if (!pdb_set_bad_password_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
153 talloc_free(frame);
154 return -1;
156 talloc_free(frame);
157 return 0;
160 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
162 TALLOC_CTX *frame = talloc_stackframe();
163 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
164 PyObject *py_pass_last_set_time;
166 py_pass_last_set_time = PyLong_FromLong(pdb_get_pass_last_set_time(sam_acct));
167 talloc_free(frame);
168 return py_pass_last_set_time;
171 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
173 TALLOC_CTX *frame = talloc_stackframe();
174 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
176 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
177 if (!pdb_set_pass_last_set_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
178 talloc_free(frame);
179 return -1;
181 talloc_free(frame);
182 return 0;
185 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
187 TALLOC_CTX *frame = talloc_stackframe();
188 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
189 PyObject *py_pass_can_change_time;
191 py_pass_can_change_time = PyLong_FromLong(pdb_get_pass_can_change_time(sam_acct));
192 talloc_free(frame);
193 return py_pass_can_change_time;
196 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
198 TALLOC_CTX *frame = talloc_stackframe();
199 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
201 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
202 if (!pdb_set_pass_can_change_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
203 talloc_free(frame);
204 return -1;
206 talloc_free(frame);
207 return 0;
210 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
212 TALLOC_CTX *frame = talloc_stackframe();
213 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
214 PyObject *py_pass_must_change_time;
216 py_pass_must_change_time = PyLong_FromLong(pdb_get_pass_must_change_time(sam_acct));
217 talloc_free(frame);
218 return py_pass_must_change_time;
221 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
223 TALLOC_CTX *frame = talloc_stackframe();
224 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
226 /* TODO: make this not a get/set or give a better exception */
227 talloc_free(frame);
228 return -1;
231 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
233 TALLOC_CTX *frame = talloc_stackframe();
234 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
235 PyObject *py_username;
236 const char *username;
238 username = pdb_get_username(sam_acct);
239 if (username == NULL) {
240 Py_RETURN_NONE;
243 py_username = PyUnicode_FromString(username);
244 talloc_free(frame);
245 return py_username;
248 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
250 TALLOC_CTX *frame = talloc_stackframe();
251 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
253 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
254 if (!pdb_set_username(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
255 talloc_free(frame);
256 return -1;
258 talloc_free(frame);
259 return 0;
262 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
264 TALLOC_CTX *frame = talloc_stackframe();
265 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
266 PyObject *py_domain;
267 const char *domain;
269 domain = pdb_get_domain(sam_acct);
270 if (domain == NULL) {
271 Py_RETURN_NONE;
274 py_domain = PyUnicode_FromString(domain);
275 talloc_free(frame);
276 return py_domain;
279 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
281 TALLOC_CTX *frame = talloc_stackframe();
282 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
284 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
285 if (!pdb_set_domain(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
286 talloc_free(frame);
287 return -1;
289 talloc_free(frame);
290 return 0;
293 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
295 TALLOC_CTX *frame = talloc_stackframe();
296 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
297 PyObject *py_nt_username;
298 const char *nt_username;
300 nt_username = pdb_get_nt_username(sam_acct);
301 if (nt_username == NULL) {
302 Py_RETURN_NONE;
305 py_nt_username = PyUnicode_FromString(nt_username);
306 talloc_free(frame);
307 return py_nt_username;
310 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
312 TALLOC_CTX *frame = talloc_stackframe();
313 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
315 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
316 if (!pdb_set_nt_username(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
317 talloc_free(frame);
318 return -1;
320 talloc_free(frame);
321 return 0;
324 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
326 TALLOC_CTX *frame = talloc_stackframe();
327 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
328 PyObject *py_full_name;
329 const char *full_name;
331 full_name = pdb_get_fullname(sam_acct);
332 if (full_name == NULL) {
333 Py_RETURN_NONE;
336 py_full_name = PyUnicode_FromString(full_name);
337 talloc_free(frame);
338 return py_full_name;
341 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
343 TALLOC_CTX *frame = talloc_stackframe();
344 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
346 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
347 if (!pdb_set_fullname(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
348 talloc_free(frame);
349 return -1;
351 talloc_free(frame);
352 return 0;
355 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
357 TALLOC_CTX *frame = talloc_stackframe();
358 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
359 PyObject *py_home_dir;
360 const char *home_dir;
362 home_dir = pdb_get_homedir(sam_acct);
363 if (home_dir == NULL) {
364 Py_RETURN_NONE;
367 py_home_dir = PyUnicode_FromString(home_dir);
368 talloc_free(frame);
369 return py_home_dir;
372 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
374 TALLOC_CTX *frame = talloc_stackframe();
375 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
377 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
378 if (!pdb_set_homedir(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
379 talloc_free(frame);
380 return -1;
382 talloc_free(frame);
383 return 0;
386 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
388 TALLOC_CTX *frame = talloc_stackframe();
389 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
390 PyObject *py_dir_drive;
391 const char *dir_drive;
393 dir_drive = pdb_get_dir_drive(sam_acct);
394 if (dir_drive == NULL) {
395 Py_RETURN_NONE;
398 py_dir_drive = PyUnicode_FromString(dir_drive);
399 talloc_free(frame);
400 return py_dir_drive;
403 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
405 TALLOC_CTX *frame = talloc_stackframe();
406 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
408 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
409 if (!pdb_set_dir_drive(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
410 talloc_free(frame);
411 return -1;
413 talloc_free(frame);
414 return 0;
417 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
419 TALLOC_CTX *frame = talloc_stackframe();
420 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
421 PyObject *py_logon_script;
422 const char *logon_script;
424 logon_script = pdb_get_logon_script(sam_acct);
425 if (logon_script == NULL) {
426 Py_RETURN_NONE;
429 py_logon_script = PyUnicode_FromString(logon_script);
430 talloc_free(frame);
431 return py_logon_script;
434 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
436 TALLOC_CTX *frame = talloc_stackframe();
437 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
439 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
440 if (!pdb_set_logon_script(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
441 talloc_free(frame);
442 return -1;
444 talloc_free(frame);
445 return 0;
448 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
450 TALLOC_CTX *frame = talloc_stackframe();
451 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
452 PyObject *py_profile_path;
453 const char *profile_path;
455 profile_path = pdb_get_profile_path(sam_acct);
456 if (profile_path == NULL) {
457 Py_RETURN_NONE;
460 py_profile_path = PyUnicode_FromString(profile_path);
461 talloc_free(frame);
462 return py_profile_path;
465 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
467 TALLOC_CTX *frame = talloc_stackframe();
468 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
470 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
471 if (!pdb_set_profile_path(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
472 talloc_free(frame);
473 return -1;
475 talloc_free(frame);
476 return 0;
479 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
481 TALLOC_CTX *frame = talloc_stackframe();
482 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
483 PyObject *py_acct_desc;
484 const char *acct_desc;
486 acct_desc = pdb_get_acct_desc(sam_acct);
487 if (acct_desc == NULL) {
488 Py_RETURN_NONE;
491 py_acct_desc = PyUnicode_FromString(acct_desc);
492 talloc_free(frame);
493 return py_acct_desc;
496 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
498 TALLOC_CTX *frame = talloc_stackframe();
499 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
501 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
502 if (!pdb_set_acct_desc(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
503 talloc_free(frame);
504 return -1;
506 talloc_free(frame);
507 return 0;
510 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
512 TALLOC_CTX *frame = talloc_stackframe();
513 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
514 PyObject *py_workstations;
515 const char *workstations;
517 workstations = pdb_get_workstations(sam_acct);
518 if (workstations == NULL) {
519 Py_RETURN_NONE;
522 py_workstations = PyUnicode_FromString(workstations);
523 talloc_free(frame);
524 return py_workstations;
527 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
529 TALLOC_CTX *frame = talloc_stackframe();
530 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
532 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
533 if (!pdb_set_workstations(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
534 talloc_free(frame);
535 return -1;
537 talloc_free(frame);
538 return 0;
541 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
543 TALLOC_CTX *frame = talloc_stackframe();
544 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
545 PyObject *py_comment;
546 const char *comment;
548 comment = pdb_get_comment(sam_acct);
549 if (comment == NULL) {
550 Py_RETURN_NONE;
553 py_comment = PyUnicode_FromString(comment);
554 talloc_free(frame);
555 return py_comment;
558 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
560 TALLOC_CTX *frame = talloc_stackframe();
561 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
563 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
564 if (!pdb_set_comment(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
565 talloc_free(frame);
566 return -1;
568 talloc_free(frame);
569 return 0;
572 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
574 TALLOC_CTX *frame = talloc_stackframe();
575 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
576 PyObject *py_munged_dial;
577 const char *munged_dial;
579 munged_dial = pdb_get_munged_dial(sam_acct);
580 if (munged_dial == NULL) {
581 Py_RETURN_NONE;
584 py_munged_dial = PyUnicode_FromString(munged_dial);
585 talloc_free(frame);
586 return py_munged_dial;
589 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
591 TALLOC_CTX *frame = talloc_stackframe();
592 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
594 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
595 if (!pdb_set_munged_dial(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
596 talloc_free(frame);
597 return -1;
599 talloc_free(frame);
600 return 0;
603 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
605 TALLOC_CTX *frame = talloc_stackframe();
606 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
607 PyObject *py_user_sid;
608 const struct dom_sid *user_sid;
609 struct dom_sid *copy_user_sid;
610 TALLOC_CTX *mem_ctx;
612 user_sid = pdb_get_user_sid(sam_acct);
613 if(user_sid == NULL) {
614 Py_RETURN_NONE;
617 mem_ctx = talloc_new(NULL);
618 if (mem_ctx == NULL) {
619 PyErr_NoMemory();
620 talloc_free(frame);
621 return NULL;
623 copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
624 if (copy_user_sid == NULL) {
625 PyErr_NoMemory();
626 talloc_free(mem_ctx);
627 talloc_free(frame);
628 return NULL;
631 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
633 talloc_free(mem_ctx);
635 talloc_free(frame);
636 return py_user_sid;
639 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
641 TALLOC_CTX *frame = talloc_stackframe();
642 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
644 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
645 if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
646 talloc_free(frame);
647 return -1;
649 talloc_free(frame);
650 return 0;
653 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
655 TALLOC_CTX *frame = talloc_stackframe();
656 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
657 const struct dom_sid *group_sid;
658 struct dom_sid *copy_group_sid;
660 group_sid = pdb_get_group_sid(sam_acct);
661 if (group_sid == NULL) {
662 Py_RETURN_NONE;
665 copy_group_sid = dom_sid_dup(NULL, group_sid);
666 if (copy_group_sid == NULL) {
667 PyErr_NoMemory();
668 talloc_free(frame);
669 return NULL;
672 talloc_free(frame);
673 return pytalloc_steal(dom_sid_Type, copy_group_sid);
676 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
678 TALLOC_CTX *frame = talloc_stackframe();
679 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
681 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
682 if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
683 talloc_free(frame);
684 return -1;
686 talloc_free(frame);
687 return 0;
690 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
692 TALLOC_CTX *frame = talloc_stackframe();
693 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
694 PyObject *py_lm_pw;
695 const char *lm_pw;
697 lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
698 if (lm_pw == NULL) {
699 Py_RETURN_NONE;
702 py_lm_pw = PyBytes_FromStringAndSize(lm_pw, LM_HASH_LEN);
703 talloc_free(frame);
704 return py_lm_pw;
707 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
709 TALLOC_CTX *frame = talloc_stackframe();
710 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
712 PY_CHECK_TYPE(&PyBytes_Type, value, return -1;);
713 if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyBytes_AsString(value), PDB_CHANGED)) {
714 talloc_free(frame);
715 return -1;
717 talloc_free(frame);
718 return 0;
721 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
723 TALLOC_CTX *frame = talloc_stackframe();
724 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
725 PyObject *py_nt_pw;
726 const char *nt_pw;
728 nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
729 if (nt_pw == NULL) {
730 Py_RETURN_NONE;
733 py_nt_pw = PyBytes_FromStringAndSize(nt_pw, NT_HASH_LEN);
734 talloc_free(frame);
735 return py_nt_pw;
738 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
740 TALLOC_CTX *frame = talloc_stackframe();
741 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
743 if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyBytes_AsString(value), PDB_CHANGED)) {
744 talloc_free(frame);
745 return -1;
747 talloc_free(frame);
748 return 0;
751 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
753 TALLOC_CTX *frame = talloc_stackframe();
754 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
755 PyObject *py_nt_pw_his;
756 const char *nt_pw_his;
757 uint32_t hist_len;
759 nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
760 if (nt_pw_his == NULL) {
761 Py_RETURN_NONE;
764 py_nt_pw_his = PyBytes_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
765 talloc_free(frame);
766 return py_nt_pw_his;
769 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
771 TALLOC_CTX *frame = talloc_stackframe();
772 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
773 char *nt_pw_his;
774 Py_ssize_t len;
775 uint32_t hist_len;
777 PyBytes_AsStringAndSize(value, &nt_pw_his, &len);
778 hist_len = len / PW_HISTORY_ENTRY_LEN;
779 if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
780 talloc_free(frame);
781 return -1;
783 talloc_free(frame);
784 return 0;
787 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
789 TALLOC_CTX *frame = talloc_stackframe();
790 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
791 PyObject *py_plaintext_pw;
792 const char *plaintext_pw;
794 plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
795 if (plaintext_pw == NULL) {
796 Py_RETURN_NONE;
799 py_plaintext_pw = PyUnicode_FromString(plaintext_pw);
800 talloc_free(frame);
801 return py_plaintext_pw;
804 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
806 TALLOC_CTX *frame = talloc_stackframe();
807 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
809 if (!pdb_set_plaintext_passwd(sam_acct, PyUnicode_AsUTF8(value))) {
810 talloc_free(frame);
811 return -1;
813 talloc_free(frame);
814 return 0;
817 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
819 TALLOC_CTX *frame = talloc_stackframe();
820 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
821 PyObject *py_acct_ctrl;
823 py_acct_ctrl = PyLong_FromLong(pdb_get_acct_ctrl(sam_acct));
824 talloc_free(frame);
825 return py_acct_ctrl;
828 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
830 TALLOC_CTX *frame = talloc_stackframe();
831 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
833 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
834 if (!pdb_set_acct_ctrl(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
835 talloc_free(frame);
836 return -1;
838 talloc_free(frame);
839 return 0;
842 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
844 TALLOC_CTX *frame = talloc_stackframe();
845 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
846 PyObject *py_logon_divs;
848 py_logon_divs = PyLong_FromLong(pdb_get_logon_divs(sam_acct));
849 talloc_free(frame);
850 return py_logon_divs;
853 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
855 TALLOC_CTX *frame = talloc_stackframe();
856 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
858 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
859 if (!pdb_set_logon_divs(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
860 talloc_free(frame);
861 return -1;
863 talloc_free(frame);
864 return 0;
867 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
869 TALLOC_CTX *frame = talloc_stackframe();
870 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
871 PyObject *py_hours_len;
873 py_hours_len = PyLong_FromLong(pdb_get_hours_len(sam_acct));
874 talloc_free(frame);
875 return py_hours_len;
878 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
880 TALLOC_CTX *frame = talloc_stackframe();
881 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
883 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
884 if (!pdb_set_hours_len(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
885 talloc_free(frame);
886 return -1;
888 talloc_free(frame);
889 return 0;
892 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
894 TALLOC_CTX *frame = talloc_stackframe();
895 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
896 PyObject *py_hours;
897 const char *hours;
898 int hours_len, i;
900 hours = (const char *)pdb_get_hours(sam_acct);
901 if(! hours) {
902 Py_RETURN_NONE;
905 hours_len = pdb_get_hours_len(sam_acct);
906 if ((py_hours = PyList_New(hours_len)) == NULL) {
907 PyErr_NoMemory();
908 talloc_free(frame);
909 return NULL;
912 for (i=0; i<hours_len; i++) {
913 PyList_SetItem(py_hours, i, PyLong_FromLong(hours[i]));
915 talloc_free(frame);
916 return py_hours;
919 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
921 TALLOC_CTX *frame = talloc_stackframe();
922 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
923 int i;
924 uint8_t *hours;
925 int hours_len;
926 bool status;
928 PY_CHECK_TYPE(&PyList_Type, value, return -1;);
930 hours_len = PyList_GET_SIZE(value);
932 hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
933 if (!hours) {
934 PyErr_NoMemory();
935 talloc_free(frame);
936 return -1;
939 for (i=0; i < hours_len; i++) {
940 PY_CHECK_TYPE(&PyLong_Type, PyList_GET_ITEM(value,i), return -1;);
941 hours[i] = PyLong_AsLong(PyList_GET_ITEM(value, i));
944 status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
945 talloc_free(hours);
947 if(! status) {
948 talloc_free(frame);
949 return -1;
951 talloc_free(frame);
952 return 0;
955 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
957 TALLOC_CTX *frame = talloc_stackframe();
958 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
959 PyObject *py_bad_password_count;
961 py_bad_password_count = PyLong_FromLong(pdb_get_bad_password_count(sam_acct));
962 talloc_free(frame);
963 return py_bad_password_count;
966 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
968 TALLOC_CTX *frame = talloc_stackframe();
969 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
971 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
972 if (!pdb_set_bad_password_count(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
973 talloc_free(frame);
974 return -1;
976 talloc_free(frame);
977 return 0;
980 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
982 TALLOC_CTX *frame = talloc_stackframe();
983 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
984 PyObject *py_logon_count;
986 py_logon_count = PyLong_FromLong(pdb_get_logon_count(sam_acct));
987 talloc_free(frame);
988 return py_logon_count;
991 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
993 TALLOC_CTX *frame = talloc_stackframe();
994 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
996 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
997 if (!pdb_set_logon_count(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
998 talloc_free(frame);
999 return -1;
1001 talloc_free(frame);
1002 return 0;
1005 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
1007 TALLOC_CTX *frame = talloc_stackframe();
1008 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1009 PyObject *py_country_code;
1011 py_country_code = PyLong_FromLong(pdb_get_country_code(sam_acct));
1012 talloc_free(frame);
1013 return py_country_code;
1016 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
1018 TALLOC_CTX *frame = talloc_stackframe();
1019 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1021 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1022 if (!pdb_set_country_code(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
1023 talloc_free(frame);
1024 return -1;
1026 talloc_free(frame);
1027 return 0;
1030 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
1032 TALLOC_CTX *frame = talloc_stackframe();
1033 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1034 PyObject *py_code_page;
1036 py_code_page = PyLong_FromLong(pdb_get_code_page(sam_acct));
1037 talloc_free(frame);
1038 return py_code_page;
1041 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
1043 TALLOC_CTX *frame = talloc_stackframe();
1044 struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1046 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1047 if (!pdb_set_code_page(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
1048 talloc_free(frame);
1049 return -1;
1051 talloc_free(frame);
1052 return 0;
1055 static PyGetSetDef py_samu_getsetters[] = {
1057 .name = discard_const_p(char, "logon_time"),
1058 .get = py_samu_get_logon_time,
1059 .set = py_samu_set_logon_time,
1062 .name = discard_const_p(char, "logoff_time"),
1063 .get = py_samu_get_logoff_time,
1064 .set = py_samu_set_logoff_time,
1067 .name = discard_const_p(char, "kickoff_time"),
1068 .get = py_samu_get_kickoff_time,
1069 .set = py_samu_set_kickoff_time,
1072 .name = discard_const_p(char, "bad_password_time"),
1073 .get = py_samu_get_bad_password_time,
1074 .set = py_samu_set_bad_password_time,
1077 .name = discard_const_p(char, "pass_last_set_time"),
1078 .get = py_samu_get_pass_last_set_time,
1079 .set = py_samu_set_pass_last_set_time,
1082 .name = discard_const_p(char, "pass_can_change_time"),
1083 .get = py_samu_get_pass_can_change_time,
1084 .set = py_samu_set_pass_can_change_time,
1087 .name = discard_const_p(char, "pass_must_change_time"),
1088 .get = py_samu_get_pass_must_change_time,
1089 .set = py_samu_set_pass_must_change_time,
1092 .name = discard_const_p(char, "username"),
1093 .get = py_samu_get_username,
1094 .set = py_samu_set_username,
1097 .name = discard_const_p(char, "domain"),
1098 .get = py_samu_get_domain,
1099 .set = py_samu_set_domain,
1102 .name = discard_const_p(char, "nt_username"),
1103 .get = py_samu_get_nt_username,
1104 .set = py_samu_set_nt_username,
1107 .name = discard_const_p(char, "full_name"),
1108 .get = py_samu_get_full_name,
1109 .set = py_samu_set_full_name,
1112 .name = discard_const_p(char, "home_dir"),
1113 .get = py_samu_get_home_dir,
1114 .set = py_samu_set_home_dir,
1117 .name = discard_const_p(char, "dir_drive"),
1118 .get = py_samu_get_dir_drive,
1119 .set = py_samu_set_dir_drive,
1122 .name = discard_const_p(char, "logon_script"),
1123 .get = py_samu_get_logon_script,
1124 .set = py_samu_set_logon_script,
1127 .name = discard_const_p(char, "profile_path"),
1128 .get = py_samu_get_profile_path,
1129 .set = py_samu_set_profile_path,
1132 .name = discard_const_p(char, "acct_desc"),
1133 .get = py_samu_get_acct_desc,
1134 .set = py_samu_set_acct_desc,
1137 .name = discard_const_p(char, "workstations"),
1138 .get = py_samu_get_workstations,
1139 .set = py_samu_set_workstations,
1142 .name = discard_const_p(char, "comment"),
1143 .get = py_samu_get_comment,
1144 .set = py_samu_set_comment,
1147 .name = discard_const_p(char, "munged_dial"),
1148 .get = py_samu_get_munged_dial,
1149 .set = py_samu_set_munged_dial,
1152 .name = discard_const_p(char, "user_sid"),
1153 .get = py_samu_get_user_sid,
1154 .set = py_samu_set_user_sid,
1157 .name = discard_const_p(char, "group_sid"),
1158 .get = py_samu_get_group_sid,
1159 .set = py_samu_set_group_sid,
1162 .name = discard_const_p(char, "lanman_passwd"),
1163 .get = py_samu_get_lanman_passwd,
1164 .set = py_samu_set_lanman_passwd,
1167 .name = discard_const_p(char, "nt_passwd"),
1168 .get = py_samu_get_nt_passwd,
1169 .set = py_samu_set_nt_passwd,
1172 .name = discard_const_p(char, "pw_history"),
1173 .get = py_samu_get_pw_history,
1174 .set = py_samu_set_pw_history,
1177 .name = discard_const_p(char, "plaintext_passwd"),
1178 .get = py_samu_get_plaintext_passwd,
1179 .set = py_samu_set_plaintext_passwd,
1182 .name = discard_const_p(char, "acct_ctrl"),
1183 .get = py_samu_get_acct_ctrl,
1184 .set = py_samu_set_acct_ctrl,
1187 .name = discard_const_p(char, "logon_divs"),
1188 .get = py_samu_get_logon_divs,
1189 .set = py_samu_set_logon_divs,
1192 .name = discard_const_p(char, "hours_len"),
1193 .get = py_samu_get_hours_len,
1194 .set = py_samu_set_hours_len,
1197 .name = discard_const_p(char, "hours"),
1198 .get = py_samu_get_hours,
1199 .set = py_samu_set_hours,
1202 .name = discard_const_p(char, "bad_password_count"),
1203 .get = py_samu_get_bad_password_count,
1204 .set = py_samu_set_bad_password_count,
1207 .name = discard_const_p(char, "logon_count"),
1208 .get = py_samu_get_logon_count,
1209 .set = py_samu_set_logon_count,
1212 .name = discard_const_p(char, "country_code"),
1213 .get = py_samu_get_country_code,
1214 .set = py_samu_set_country_code,
1217 .name = discard_const_p(char, "code_page"),
1218 .get = py_samu_get_code_page,
1219 .set = py_samu_set_code_page,
1222 .name = NULL,
1227 /************************** PIDL Autogeneratd ******************************/
1229 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1231 TALLOC_CTX *frame = talloc_stackframe();
1232 struct samu *sam_acct;
1234 sam_acct = samu_new(NULL);
1235 if (!sam_acct) {
1236 PyErr_NoMemory();
1237 talloc_free(frame);
1238 return NULL;
1241 talloc_free(frame);
1242 return pytalloc_steal(type, sam_acct);
1245 static PyTypeObject PySamu = {
1246 .tp_name = "passdb.Samu",
1247 .tp_getset = py_samu_getsetters,
1248 .tp_methods = NULL,
1249 .tp_new = py_samu_new,
1250 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1251 .tp_doc = "Samu() -> samu object\n",
1255 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1257 TALLOC_CTX *frame = talloc_stackframe();
1258 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1259 PyObject *py_gid;
1261 py_gid = Py_BuildValue("i", group_map->gid);
1262 talloc_free(frame);
1263 return py_gid;
1266 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1268 TALLOC_CTX *frame = talloc_stackframe();
1269 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1271 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1272 group_map->gid = PyLong_AsLong(value);
1273 talloc_free(frame);
1274 return 0;
1277 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1279 TALLOC_CTX *frame = talloc_stackframe();
1280 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1281 PyObject *py_sid;
1282 struct dom_sid *group_sid;
1283 TALLOC_CTX *mem_ctx;
1285 mem_ctx = talloc_new(NULL);
1286 if (mem_ctx == NULL) {
1287 PyErr_NoMemory();
1288 talloc_free(frame);
1289 return NULL;
1292 group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1293 if (group_sid == NULL) {
1294 PyErr_NoMemory();
1295 talloc_free(mem_ctx);
1296 talloc_free(frame);
1297 return NULL;
1300 py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1302 talloc_free(mem_ctx);
1304 talloc_free(frame);
1305 return py_sid;
1308 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1310 TALLOC_CTX *frame = talloc_stackframe();
1311 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1313 PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1314 group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1315 talloc_free(frame);
1316 return 0;
1319 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1321 TALLOC_CTX *frame = talloc_stackframe();
1322 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1323 PyObject *py_sid_name_use;
1325 py_sid_name_use = PyLong_FromLong(group_map->sid_name_use);
1326 talloc_free(frame);
1327 return py_sid_name_use;
1330 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1332 TALLOC_CTX *frame = talloc_stackframe();
1333 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1335 PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
1336 group_map->sid_name_use = PyLong_AsLong(value);
1337 talloc_free(frame);
1338 return 0;
1341 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1343 TALLOC_CTX *frame = talloc_stackframe();
1344 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1345 PyObject *py_nt_name;
1346 if (group_map->nt_name == NULL) {
1347 py_nt_name = Py_None;
1348 Py_INCREF(py_nt_name);
1349 } else {
1350 py_nt_name = PyUnicode_FromString(group_map->nt_name);
1352 talloc_free(frame);
1353 return py_nt_name;
1356 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1358 TALLOC_CTX *frame = talloc_stackframe();
1359 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1361 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
1362 if (value == Py_None) {
1363 fstrcpy(group_map->nt_name, NULL);
1364 } else {
1365 fstrcpy(group_map->nt_name, PyUnicode_AsUTF8(value));
1367 talloc_free(frame);
1368 return 0;
1371 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1373 TALLOC_CTX *frame = talloc_stackframe();
1374 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1375 PyObject *py_comment;
1376 if (group_map->comment == NULL) {
1377 py_comment = Py_None;
1378 Py_INCREF(py_comment);
1379 } else {
1380 py_comment = PyUnicode_FromString(group_map->comment);
1382 talloc_free(frame);
1383 return py_comment;
1386 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1388 TALLOC_CTX *frame = talloc_stackframe();
1389 GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1391 PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
1392 if (value == Py_None) {
1393 fstrcpy(group_map->comment, NULL);
1394 } else {
1395 fstrcpy(group_map->comment, PyUnicode_AsUTF8(value));
1397 talloc_free(frame);
1398 return 0;
1401 static PyGetSetDef py_groupmap_getsetters[] = {
1403 .name = discard_const_p(char, "gid"),
1404 .get = py_groupmap_get_gid,
1405 .set = py_groupmap_set_gid,
1408 .name = discard_const_p(char, "sid"),
1409 .get = py_groupmap_get_sid,
1410 .set = py_groupmap_set_sid,
1413 .name = discard_const_p(char, "sid_name_use"),
1414 .get = py_groupmap_get_sid_name_use,
1415 .set = py_groupmap_set_sid_name_use,
1418 .name = discard_const_p(char, "nt_name"),
1419 .get = py_groupmap_get_nt_name,
1420 .set = py_groupmap_set_nt_name,
1423 .name = discard_const_p(char, "comment"),
1424 .get = py_groupmap_get_comment,
1425 .set = py_groupmap_set_comment,
1428 .name = NULL,
1432 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1434 TALLOC_CTX *frame = talloc_stackframe();
1435 GROUP_MAP *group_map;
1436 TALLOC_CTX *mem_ctx;
1437 PyObject *py_group_map;
1439 mem_ctx = talloc_new(NULL);
1440 if (mem_ctx == NULL) {
1441 PyErr_NoMemory();
1442 talloc_free(frame);
1443 return NULL;
1446 group_map = talloc_zero(mem_ctx, GROUP_MAP);
1447 if (group_map == NULL) {
1448 PyErr_NoMemory();
1449 talloc_free(mem_ctx);
1450 talloc_free(frame);
1451 return NULL;
1454 py_group_map = pytalloc_steal(type, group_map);
1455 if (py_group_map == NULL) {
1456 PyErr_NoMemory();
1457 talloc_free(mem_ctx);
1458 talloc_free(frame);
1459 return NULL;
1462 talloc_free(mem_ctx);
1464 talloc_free(frame);
1465 return py_group_map;
1469 static PyTypeObject PyGroupmap = {
1470 .tp_name = "passdb.Groupmap",
1471 .tp_getset = py_groupmap_getsetters,
1472 .tp_methods = NULL,
1473 .tp_new = py_groupmap_new,
1474 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1475 .tp_doc = "Groupmap() -> group map object\n",
1479 static PyObject *py_pdb_domain_info(PyObject *self, PyObject *args)
1481 TALLOC_CTX *frame = talloc_stackframe();
1482 struct pdb_methods *methods;
1483 struct pdb_domain_info *domain_info;
1484 PyObject *py_domain_info;
1485 struct dom_sid *sid;
1486 struct GUID *guid;
1487 PyObject *py_dom_sid = NULL;
1488 PyObject *py_guid = NULL;
1490 methods = pytalloc_get_ptr(self);
1492 domain_info = methods->get_domain_info(methods, frame);
1493 if (! domain_info) {
1494 Py_RETURN_NONE;
1497 sid = dom_sid_dup(frame, &domain_info->sid);
1498 if (sid == NULL) {
1499 PyErr_NoMemory();
1500 talloc_free(frame);
1501 return NULL;
1504 guid = talloc(frame, struct GUID);
1505 if (guid == NULL) {
1506 PyErr_NoMemory();
1507 talloc_free(frame);
1508 return NULL;
1510 *guid = domain_info->guid;
1512 py_dom_sid = pytalloc_steal(dom_sid_Type, sid);
1513 py_guid = pytalloc_steal(guid_Type, guid);
1515 py_domain_info = Py_BuildValue(
1516 "{s:s, s:s, s:s, s:O, s:O}",
1517 "name", domain_info->name,
1518 "dns_domain", domain_info->dns_domain,
1519 "dns_forest", domain_info->dns_forest,
1520 "dom_sid", py_dom_sid,
1521 "guid", py_guid);
1524 Py_CLEAR(py_dom_sid);
1525 Py_CLEAR(py_guid);
1526 talloc_free(frame);
1527 return py_domain_info;
1531 static PyObject *py_pdb_getsampwnam(PyObject *self, PyObject *args)
1533 TALLOC_CTX *frame = talloc_stackframe();
1534 NTSTATUS status;
1535 const char *username;
1536 struct pdb_methods *methods;
1537 struct samu *sam_acct;
1538 PyObject *py_sam_acct;
1540 if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1541 talloc_free(frame);
1542 return NULL;
1545 methods = pytalloc_get_ptr(self);
1547 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1548 if (py_sam_acct == NULL) {
1549 PyErr_NoMemory();
1550 talloc_free(frame);
1551 return NULL;
1553 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1555 status = methods->getsampwnam(methods, sam_acct, username);
1556 if (!NT_STATUS_IS_OK(status)) {
1557 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1558 username,
1559 NT_STATUS_V(status),
1560 get_friendly_nt_error_msg(status));
1561 Py_DECREF(py_sam_acct);
1562 talloc_free(frame);
1563 return NULL;
1566 talloc_free(frame);
1567 return py_sam_acct;
1570 static PyObject *py_pdb_getsampwsid(PyObject *self, PyObject *args)
1572 TALLOC_CTX *frame = talloc_stackframe();
1573 NTSTATUS status;
1574 struct pdb_methods *methods;
1575 struct samu *sam_acct;
1576 PyObject *py_sam_acct;
1577 PyObject *py_user_sid;
1579 if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1580 talloc_free(frame);
1581 return NULL;
1584 methods = pytalloc_get_ptr(self);
1586 py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1587 if (py_sam_acct == NULL) {
1588 PyErr_NoMemory();
1589 talloc_free(frame);
1590 return NULL;
1592 sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1594 status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1595 if (!NT_STATUS_IS_OK(status)) {
1596 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1597 NT_STATUS_V(status),
1598 get_friendly_nt_error_msg(status));
1599 Py_DECREF(py_sam_acct);
1600 talloc_free(frame);
1601 return NULL;
1604 talloc_free(frame);
1605 return py_sam_acct;
1608 static PyObject *py_pdb_create_user(PyObject *self, PyObject *args)
1610 TALLOC_CTX *frame = talloc_stackframe();
1611 NTSTATUS status;
1612 struct pdb_methods *methods;
1613 const char *username;
1614 unsigned int acct_flags;
1615 unsigned int rid;
1617 if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1618 talloc_free(frame);
1619 return NULL;
1622 methods = pytalloc_get_ptr(self);
1624 status = methods->create_user(methods, frame, username, acct_flags, &rid);
1625 if (!NT_STATUS_IS_OK(status)) {
1626 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1627 username,
1628 NT_STATUS_V(status),
1629 get_friendly_nt_error_msg(status));
1630 talloc_free(frame);
1631 return NULL;
1634 talloc_free(frame);
1635 return PyLong_FromLong(rid);
1638 static PyObject *py_pdb_delete_user(PyObject *self, PyObject *args)
1640 TALLOC_CTX *frame = talloc_stackframe();
1641 NTSTATUS status;
1642 struct pdb_methods *methods;
1643 struct samu *sam_acct;
1644 PyObject *py_sam_acct;
1646 if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1647 talloc_free(frame);
1648 return NULL;
1651 methods = pytalloc_get_ptr(self);
1653 sam_acct = pytalloc_get_ptr(py_sam_acct);
1655 status = methods->delete_user(methods, frame, sam_acct);
1656 if (!NT_STATUS_IS_OK(status)) {
1657 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1658 NT_STATUS_V(status),
1659 get_friendly_nt_error_msg(status));
1660 talloc_free(frame);
1661 return NULL;
1664 talloc_free(frame);
1665 Py_RETURN_NONE;
1668 static PyObject *py_pdb_add_sam_account(PyObject *self, PyObject *args)
1670 TALLOC_CTX *frame = talloc_stackframe();
1671 NTSTATUS status;
1672 struct pdb_methods *methods;
1673 struct samu *sam_acct;
1674 PyObject *py_sam_acct;
1676 if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1677 talloc_free(frame);
1678 return NULL;
1681 methods = pytalloc_get_ptr(self);
1683 sam_acct = pytalloc_get_ptr(py_sam_acct);
1685 status = methods->add_sam_account(methods, sam_acct);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1688 sam_acct->username,
1689 NT_STATUS_V(status),
1690 get_friendly_nt_error_msg(status));
1691 talloc_free(frame);
1692 return NULL;
1695 talloc_free(frame);
1696 Py_RETURN_NONE;
1699 static PyObject *py_pdb_update_sam_account(PyObject *self, PyObject *args)
1701 TALLOC_CTX *frame = talloc_stackframe();
1702 NTSTATUS status;
1703 struct pdb_methods *methods;
1704 struct samu *sam_acct;
1705 PyObject *py_sam_acct;
1707 if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1708 talloc_free(frame);
1709 return NULL;
1712 methods = pytalloc_get_ptr(self);
1714 sam_acct = pytalloc_get_ptr(py_sam_acct);
1716 status = methods->update_sam_account(methods, sam_acct);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1719 NT_STATUS_V(status),
1720 get_friendly_nt_error_msg(status));
1721 talloc_free(frame);
1722 return NULL;
1725 talloc_free(frame);
1726 Py_RETURN_NONE;
1729 static PyObject *py_pdb_delete_sam_account(PyObject *self, PyObject *args)
1731 TALLOC_CTX *frame = talloc_stackframe();
1732 NTSTATUS status;
1733 struct pdb_methods *methods;
1734 struct samu *sam_acct;
1735 PyObject *py_sam_acct;
1737 if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1738 talloc_free(frame);
1739 return NULL;
1742 methods = pytalloc_get_ptr(self);
1744 sam_acct = pytalloc_get_ptr(py_sam_acct);
1746 status = methods->delete_sam_account(methods, sam_acct);
1747 if (!NT_STATUS_IS_OK(status)) {
1748 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1749 NT_STATUS_V(status),
1750 get_friendly_nt_error_msg(status));
1751 talloc_free(frame);
1752 return NULL;
1755 talloc_free(frame);
1756 Py_RETURN_NONE;
1759 static PyObject *py_pdb_rename_sam_account(PyObject *self, PyObject *args)
1761 TALLOC_CTX *frame = talloc_stackframe();
1762 NTSTATUS status;
1763 struct pdb_methods *methods;
1764 struct samu *sam_acct;
1765 const char *new_username;
1766 PyObject *py_sam_acct;
1768 if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1769 &new_username)) {
1770 talloc_free(frame);
1771 return NULL;
1774 methods = pytalloc_get_ptr(self);
1776 sam_acct = pytalloc_get_ptr(py_sam_acct);
1778 status = methods->rename_sam_account(methods, sam_acct, new_username);
1779 if (!NT_STATUS_IS_OK(status)) {
1780 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1781 NT_STATUS_V(status),
1782 get_friendly_nt_error_msg(status));
1783 talloc_free(frame);
1784 return NULL;
1787 talloc_free(frame);
1788 Py_RETURN_NONE;
1792 static PyObject *py_pdb_getgrsid(PyObject *self, PyObject *args)
1794 TALLOC_CTX *frame = talloc_stackframe();
1795 NTSTATUS status;
1796 struct pdb_methods *methods;
1797 GROUP_MAP *group_map;
1798 struct dom_sid *domain_sid;
1799 PyObject *py_domain_sid, *py_group_map;
1801 if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1802 talloc_free(frame);
1803 return NULL;
1806 methods = pytalloc_get_ptr(self);
1808 domain_sid = pytalloc_get_ptr(py_domain_sid);
1810 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1811 if (py_group_map == NULL) {
1812 PyErr_NoMemory();
1813 talloc_free(frame);
1814 return NULL;
1817 group_map = pytalloc_get_ptr(py_group_map);
1819 status = methods->getgrsid(methods, group_map, *domain_sid);
1820 if (!NT_STATUS_IS_OK(status)) {
1821 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1822 NT_STATUS_V(status),
1823 get_friendly_nt_error_msg(status));
1824 talloc_free(frame);
1825 return NULL;
1828 talloc_free(frame);
1829 return py_group_map;
1833 static PyObject *py_pdb_getgrgid(PyObject *self, PyObject *args)
1835 TALLOC_CTX *frame = talloc_stackframe();
1836 NTSTATUS status;
1837 struct pdb_methods *methods;
1838 GROUP_MAP *group_map;
1839 PyObject *py_group_map;
1840 unsigned int gid_value;
1842 if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1843 talloc_free(frame);
1844 return NULL;
1847 methods = pytalloc_get_ptr(self);
1849 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1850 if (py_group_map == NULL) {
1851 PyErr_NoMemory();
1852 talloc_free(frame);
1853 return NULL;
1856 group_map = pytalloc_get_ptr(py_group_map);
1858 status = methods->getgrgid(methods, group_map, gid_value);
1859 if (!NT_STATUS_IS_OK(status)) {
1860 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1861 NT_STATUS_V(status),
1862 get_friendly_nt_error_msg(status));
1863 talloc_free(frame);
1864 return NULL;
1867 talloc_free(frame);
1868 return py_group_map;
1872 static PyObject *py_pdb_getgrnam(PyObject *self, PyObject *args)
1874 TALLOC_CTX *frame = talloc_stackframe();
1875 NTSTATUS status;
1876 struct pdb_methods *methods;
1877 GROUP_MAP *group_map;
1878 PyObject *py_group_map;
1879 const char *groupname;
1881 if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1882 talloc_free(frame);
1883 return NULL;
1886 methods = pytalloc_get_ptr(self);
1888 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1889 if (py_group_map == NULL) {
1890 PyErr_NoMemory();
1891 talloc_free(frame);
1892 return NULL;
1895 group_map = pytalloc_get_ptr(py_group_map);
1897 status = methods->getgrnam(methods, group_map, groupname);
1898 if (!NT_STATUS_IS_OK(status)) {
1899 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1900 NT_STATUS_V(status),
1901 get_friendly_nt_error_msg(status));
1902 talloc_free(frame);
1903 return NULL;
1906 talloc_free(frame);
1907 return py_group_map;
1911 static PyObject *py_pdb_create_dom_group(PyObject *self, PyObject *args)
1913 TALLOC_CTX *frame = talloc_stackframe();
1914 NTSTATUS status;
1915 struct pdb_methods *methods;
1916 const char *groupname;
1917 uint32_t group_rid;
1919 if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1920 talloc_free(frame);
1921 return NULL;
1924 methods = pytalloc_get_ptr(self);
1926 status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1927 if (!NT_STATUS_IS_OK(status)) {
1928 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1929 groupname,
1930 NT_STATUS_V(status),
1931 get_friendly_nt_error_msg(status));
1932 talloc_free(frame);
1933 return NULL;
1936 talloc_free(frame);
1937 return PyLong_FromLong(group_rid);
1941 static PyObject *py_pdb_delete_dom_group(PyObject *self, PyObject *args)
1943 TALLOC_CTX *frame = talloc_stackframe();
1944 NTSTATUS status;
1945 struct pdb_methods *methods;
1946 unsigned int group_rid;
1948 if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1949 talloc_free(frame);
1950 return NULL;
1953 methods = pytalloc_get_ptr(self);
1955 status = methods->delete_dom_group(methods, frame, group_rid);
1956 if (!NT_STATUS_IS_OK(status)) {
1957 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1958 group_rid,
1959 NT_STATUS_V(status),
1960 get_friendly_nt_error_msg(status));
1961 talloc_free(frame);
1962 return NULL;
1965 talloc_free(frame);
1966 Py_RETURN_NONE;
1970 static PyObject *py_pdb_add_group_mapping_entry(PyObject *self, PyObject *args)
1972 TALLOC_CTX *frame = talloc_stackframe();
1973 NTSTATUS status;
1974 struct pdb_methods *methods;
1975 PyObject *py_group_map;
1976 GROUP_MAP *group_map;
1978 if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1979 talloc_free(frame);
1980 return NULL;
1983 methods = pytalloc_get_ptr(self);
1985 group_map = pytalloc_get_ptr(py_group_map);
1987 status = methods->add_group_mapping_entry(methods, group_map);
1988 if (!NT_STATUS_IS_OK(status)) {
1989 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1990 NT_STATUS_V(status),
1991 get_friendly_nt_error_msg(status));
1992 talloc_free(frame);
1993 return NULL;
1996 talloc_free(frame);
1997 Py_RETURN_NONE;
2001 static PyObject *py_pdb_update_group_mapping_entry(PyObject *self, PyObject *args)
2003 TALLOC_CTX *frame = talloc_stackframe();
2004 NTSTATUS status;
2005 struct pdb_methods *methods;
2006 PyObject *py_group_map;
2007 GROUP_MAP *group_map;
2009 if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
2010 talloc_free(frame);
2011 return NULL;
2014 methods = pytalloc_get_ptr(self);
2016 group_map = pytalloc_get_ptr(py_group_map);
2018 status = methods->update_group_mapping_entry(methods, group_map);
2019 if (!NT_STATUS_IS_OK(status)) {
2020 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
2021 NT_STATUS_V(status),
2022 get_friendly_nt_error_msg(status));
2023 talloc_free(frame);
2024 return NULL;
2027 talloc_free(frame);
2028 Py_RETURN_NONE;
2032 static PyObject *py_pdb_delete_group_mapping_entry(PyObject *self, PyObject *args)
2034 TALLOC_CTX *frame = talloc_stackframe();
2035 NTSTATUS status;
2036 struct pdb_methods *methods;
2037 PyObject *py_group_sid;
2038 struct dom_sid *group_sid;
2040 if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
2041 talloc_free(frame);
2042 return NULL;
2045 methods = pytalloc_get_ptr(self);
2047 group_sid = pytalloc_get_ptr(py_group_sid);
2049 status = methods->delete_group_mapping_entry(methods, *group_sid);
2050 if (!NT_STATUS_IS_OK(status)) {
2051 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
2052 NT_STATUS_V(status),
2053 get_friendly_nt_error_msg(status));
2054 talloc_free(frame);
2055 return NULL;
2058 talloc_free(frame);
2059 Py_RETURN_NONE;
2063 static PyObject *py_pdb_enum_group_mapping(PyObject *self, PyObject *args)
2065 TALLOC_CTX *frame = talloc_stackframe();
2066 NTSTATUS status;
2067 struct pdb_methods *methods;
2068 enum lsa_SidType sid_name_use;
2069 int lsa_sidtype_value = SID_NAME_UNKNOWN;
2070 int unix_only = 0;
2071 PyObject *py_domain_sid = Py_None;
2072 struct dom_sid *domain_sid = NULL;
2073 GROUP_MAP **gmap = NULL;
2074 GROUP_MAP *group_map;
2075 size_t i, num_entries;
2076 PyObject *py_gmap_list, *py_group_map;
2078 Py_INCREF(Py_None);
2080 if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
2081 &lsa_sidtype_value, &unix_only)) {
2082 talloc_free(frame);
2083 return NULL;
2086 methods = pytalloc_get_ptr(self);
2088 sid_name_use = lsa_sidtype_value;
2090 if (py_domain_sid != Py_None) {
2091 domain_sid = pytalloc_get_ptr(py_domain_sid);
2094 status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
2095 &gmap, &num_entries, unix_only);
2096 if (!NT_STATUS_IS_OK(status)) {
2097 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
2098 NT_STATUS_V(status),
2099 get_friendly_nt_error_msg(status));
2100 talloc_free(frame);
2101 return NULL;
2104 py_gmap_list = PyList_New(0);
2105 if (py_gmap_list == NULL) {
2106 PyErr_NoMemory();
2107 talloc_free(frame);
2108 return NULL;
2111 for(i=0; i<num_entries; i++) {
2112 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
2113 if (py_group_map) {
2114 int res = 0;
2115 group_map = pytalloc_get_ptr(py_group_map);
2116 *group_map = *gmap[i];
2117 talloc_steal(group_map, gmap[i]->nt_name);
2118 talloc_steal(group_map, gmap[i]->comment);
2120 res = PyList_Append(py_gmap_list, py_group_map);
2121 Py_CLEAR(py_group_map);
2122 if (res == -1) {
2123 Py_CLEAR(py_gmap_list);
2124 talloc_free(frame);
2125 return NULL;
2130 talloc_free(gmap);
2132 talloc_free(frame);
2133 return py_gmap_list;
2137 static PyObject *py_pdb_enum_group_members(PyObject *self, PyObject *args)
2139 TALLOC_CTX *frame = talloc_stackframe();
2140 NTSTATUS status;
2141 struct pdb_methods *methods;
2142 PyObject *py_group_sid;
2143 struct dom_sid *group_sid;
2144 uint32_t *member_rids;
2145 size_t i, num_members;
2146 PyObject *py_sid_list;
2147 struct dom_sid *domain_sid, *member_sid;
2149 if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
2150 talloc_free(frame);
2151 return NULL;
2154 methods = pytalloc_get_ptr(self);
2156 group_sid = pytalloc_get_ptr(py_group_sid);
2158 status = methods->enum_group_members(methods, frame, group_sid,
2159 &member_rids, &num_members);
2160 if (!NT_STATUS_IS_OK(status)) {
2161 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
2162 NT_STATUS_V(status),
2163 get_friendly_nt_error_msg(status));
2164 talloc_free(frame);
2165 return NULL;
2168 py_sid_list = PyList_New(0);
2169 if (py_sid_list == NULL) {
2170 PyErr_NoMemory();
2171 talloc_free(frame);
2172 return NULL;
2175 domain_sid = get_global_sam_sid();
2177 for(i=0; i<num_members; i++) {
2178 int res = 0;
2179 PyObject *py_member_sid = NULL;
2180 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2181 py_member_sid = pytalloc_steal(dom_sid_Type, member_sid);
2182 res = PyList_Append(py_sid_list,
2183 py_member_sid);
2184 Py_CLEAR(py_member_sid);
2185 if (res == -1) {
2186 talloc_free(frame);
2187 Py_CLEAR(py_sid_list);
2188 return NULL;
2192 talloc_free(frame);
2193 return py_sid_list;
2197 static PyObject *py_pdb_enum_group_memberships(PyObject *self, PyObject *args)
2199 TALLOC_CTX *frame = talloc_stackframe();
2200 NTSTATUS status;
2201 struct pdb_methods *methods;
2202 uint32_t i;
2204 struct samu *sam_acct;
2205 PyObject *py_sam_acct;
2206 PyObject *py_sid_list;
2207 struct dom_sid *user_group_sids = NULL;
2208 gid_t *user_group_ids = NULL;
2209 uint32_t num_groups = 0;
2211 if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2212 talloc_free(frame);
2213 return NULL;
2216 methods = pytalloc_get_ptr(self);
2218 sam_acct = pytalloc_get_ptr(py_sam_acct);
2220 status = methods->enum_group_memberships(methods, frame, sam_acct,
2221 &user_group_sids, &user_group_ids, &num_groups);
2222 if (!NT_STATUS_IS_OK(status)) {
2223 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2224 NT_STATUS_V(status),
2225 get_friendly_nt_error_msg(status));
2226 talloc_free(frame);
2227 return NULL;
2230 py_sid_list = PyList_New(0);
2231 if (py_sid_list == NULL) {
2232 PyErr_NoMemory();
2233 talloc_free(frame);
2234 return NULL;
2237 for(i=0; i<num_groups; i++) {
2238 PyObject *py_sid =
2239 pytalloc_steal(dom_sid_Type,
2240 dom_sid_dup(NULL, &user_group_sids[i]));
2241 PyList_Append(py_sid_list, py_sid);
2242 Py_CLEAR(py_sid);
2245 talloc_free(frame);
2246 return py_sid_list;
2250 static PyObject *py_pdb_add_groupmem(PyObject *self, PyObject *args)
2252 TALLOC_CTX *frame = talloc_stackframe();
2253 NTSTATUS status;
2254 struct pdb_methods *methods;
2255 uint32_t group_rid, member_rid;
2257 if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2258 talloc_free(frame);
2259 return NULL;
2262 methods = pytalloc_get_ptr(self);
2264 status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2265 if (!NT_STATUS_IS_OK(status)) {
2266 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2267 NT_STATUS_V(status),
2268 get_friendly_nt_error_msg(status));
2269 talloc_free(frame);
2270 return NULL;
2273 talloc_free(frame);
2274 Py_RETURN_NONE;
2278 static PyObject *py_pdb_del_groupmem(PyObject *self, PyObject *args)
2280 TALLOC_CTX *frame = talloc_stackframe();
2281 NTSTATUS status;
2282 struct pdb_methods *methods;
2283 uint32_t group_rid, member_rid;
2285 if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2286 talloc_free(frame);
2287 return NULL;
2290 methods = pytalloc_get_ptr(self);
2292 status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2293 if (!NT_STATUS_IS_OK(status)) {
2294 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2295 NT_STATUS_V(status),
2296 get_friendly_nt_error_msg(status));
2297 talloc_free(frame);
2298 return NULL;
2301 talloc_free(frame);
2302 Py_RETURN_NONE;
2306 static PyObject *py_pdb_create_alias(PyObject *self, PyObject *args)
2308 TALLOC_CTX *frame = talloc_stackframe();
2309 NTSTATUS status;
2310 struct pdb_methods *methods;
2311 const char *alias_name;
2312 uint32_t rid;
2314 if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2315 talloc_free(frame);
2316 return NULL;
2319 methods = pytalloc_get_ptr(self);
2321 status = methods->create_alias(methods, alias_name, &rid);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2324 alias_name,
2325 NT_STATUS_V(status),
2326 get_friendly_nt_error_msg(status));
2327 talloc_free(frame);
2328 return NULL;
2331 talloc_free(frame);
2332 return PyLong_FromLong(rid);
2336 static PyObject *py_pdb_delete_alias(PyObject *self, PyObject *args)
2338 TALLOC_CTX *frame = talloc_stackframe();
2339 NTSTATUS status;
2340 struct pdb_methods *methods;
2341 PyObject *py_alias_sid;
2342 struct dom_sid *alias_sid;
2344 if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2345 talloc_free(frame);
2346 return NULL;
2349 methods = pytalloc_get_ptr(self);
2351 alias_sid = pytalloc_get_ptr(py_alias_sid);
2353 status = methods->delete_alias(methods, alias_sid);
2354 if (!NT_STATUS_IS_OK(status)) {
2355 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2356 NT_STATUS_V(status),
2357 get_friendly_nt_error_msg(status));
2358 talloc_free(frame);
2359 return NULL;
2362 talloc_free(frame);
2363 Py_RETURN_NONE;
2367 static PyObject *py_pdb_get_aliasinfo(PyObject *self, PyObject *args)
2369 TALLOC_CTX *frame = talloc_stackframe();
2370 NTSTATUS status;
2371 struct pdb_methods *methods;
2372 PyObject *py_alias_sid;
2373 struct dom_sid *alias_sid;
2374 struct acct_info *alias_info;
2375 PyObject *py_alias_info;
2377 if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2378 talloc_free(frame);
2379 return NULL;
2382 methods = pytalloc_get_ptr(self);
2384 alias_sid = pytalloc_get_ptr(py_alias_sid);
2386 alias_info = talloc_zero(frame, struct acct_info);
2387 if (!alias_info) {
2388 PyErr_NoMemory();
2389 talloc_free(frame);
2390 return NULL;
2393 status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2394 if (!NT_STATUS_IS_OK(status)) {
2395 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2396 NT_STATUS_V(status),
2397 get_friendly_nt_error_msg(status));
2398 talloc_free(frame);
2399 return NULL;
2402 py_alias_info = Py_BuildValue(
2403 "{s:s, s:s, s:l}",
2404 "acct_name", alias_info->acct_name,
2405 "acct_desc", alias_info->acct_desc,
2406 "rid", alias_info->rid);
2408 talloc_free(frame);
2409 return py_alias_info;
2413 static PyObject *py_pdb_set_aliasinfo(PyObject *self, PyObject *args)
2415 TALLOC_CTX *frame = talloc_stackframe();
2416 NTSTATUS status;
2417 struct pdb_methods *methods;
2418 PyObject *py_alias_sid, *py_alias_info;
2419 struct dom_sid *alias_sid;
2420 struct acct_info alias_info;
2422 if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2423 &py_alias_info)) {
2424 talloc_free(frame);
2425 return NULL;
2428 methods = pytalloc_get_ptr(self);
2430 alias_sid = pytalloc_get_ptr(py_alias_sid);
2432 alias_info.acct_name = talloc_strdup(frame, PyUnicode_AsUTF8(PyDict_GetItemString(py_alias_info, "acct_name")));
2433 if (alias_info.acct_name == NULL) {
2434 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2435 talloc_free(frame);
2436 return NULL;
2438 alias_info.acct_desc = talloc_strdup(frame, PyUnicode_AsUTF8(PyDict_GetItemString(py_alias_info, "acct_desc")));
2439 if (alias_info.acct_desc == NULL) {
2440 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2441 talloc_free(frame);
2442 return NULL;
2445 status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2446 if (!NT_STATUS_IS_OK(status)) {
2447 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2448 NT_STATUS_V(status),
2449 get_friendly_nt_error_msg(status));
2450 talloc_free(frame);
2451 return NULL;
2454 talloc_free(frame);
2455 Py_RETURN_NONE;
2459 static PyObject *py_pdb_add_aliasmem(PyObject *self, PyObject *args)
2461 TALLOC_CTX *frame = talloc_stackframe();
2462 NTSTATUS status;
2463 struct pdb_methods *methods;
2464 PyObject *py_alias_sid, *py_member_sid;
2465 struct dom_sid *alias_sid, *member_sid;
2467 if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2468 dom_sid_Type, &py_member_sid)) {
2469 talloc_free(frame);
2470 return NULL;
2473 methods = pytalloc_get_ptr(self);
2475 alias_sid = pytalloc_get_ptr(py_alias_sid);
2476 member_sid = pytalloc_get_ptr(py_member_sid);
2478 status = methods->add_aliasmem(methods, alias_sid, member_sid);
2479 if (!NT_STATUS_IS_OK(status)) {
2480 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2481 NT_STATUS_V(status),
2482 get_friendly_nt_error_msg(status));
2483 talloc_free(frame);
2484 return NULL;
2487 talloc_free(frame);
2488 Py_RETURN_NONE;
2492 static PyObject *py_pdb_del_aliasmem(PyObject *self, PyObject *args)
2494 TALLOC_CTX *frame = talloc_stackframe();
2495 NTSTATUS status;
2496 struct pdb_methods *methods;
2497 PyObject *py_alias_sid, *py_member_sid;
2498 const struct dom_sid *alias_sid, *member_sid;
2500 if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2501 dom_sid_Type, &py_member_sid)) {
2502 talloc_free(frame);
2503 return NULL;
2506 methods = pytalloc_get_ptr(self);
2508 alias_sid = pytalloc_get_ptr(py_alias_sid);
2509 member_sid = pytalloc_get_ptr(py_member_sid);
2511 status = methods->del_aliasmem(methods, alias_sid, member_sid);
2512 if (!NT_STATUS_IS_OK(status)) {
2513 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2514 NT_STATUS_V(status),
2515 get_friendly_nt_error_msg(status));
2516 talloc_free(frame);
2517 return NULL;
2520 talloc_free(frame);
2521 Py_RETURN_NONE;
2525 static PyObject *py_pdb_enum_aliasmem(PyObject *self, PyObject *args)
2527 TALLOC_CTX *frame = talloc_stackframe();
2528 NTSTATUS status;
2529 struct pdb_methods *methods;
2530 PyObject *py_alias_sid;
2531 struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2532 PyObject *py_member_list, *py_member_sid;
2533 size_t i, num_members;
2535 if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2536 talloc_free(frame);
2537 return NULL;
2540 methods = pytalloc_get_ptr(self);
2542 alias_sid = pytalloc_get_ptr(py_alias_sid);
2544 status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2545 if (!NT_STATUS_IS_OK(status)) {
2546 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2547 NT_STATUS_V(status),
2548 get_friendly_nt_error_msg(status));
2549 talloc_free(frame);
2550 return NULL;
2553 py_member_list = PyList_New(0);
2554 if (py_member_list == NULL) {
2555 PyErr_NoMemory();
2556 talloc_free(frame);
2557 return NULL;
2560 for(i=0; i<num_members; i++) {
2561 int res = 0;
2562 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2563 if (py_member_sid == NULL) {
2564 PyErr_NoMemory();
2565 Py_CLEAR(py_member_list);
2566 talloc_free(frame);
2567 return NULL;
2569 tmp_sid = pytalloc_get_ptr(py_member_sid);
2570 *tmp_sid = member_sid[i];
2571 res = PyList_Append(py_member_list, py_member_sid);
2572 Py_CLEAR(py_member_sid);
2573 if (res == -1) {
2574 Py_CLEAR(py_member_list);
2575 talloc_free(frame);
2576 return NULL;
2580 talloc_free(frame);
2581 return py_member_list;
2585 static PyObject *py_pdb_get_account_policy(PyObject *self, PyObject *unused)
2587 TALLOC_CTX *frame = talloc_stackframe();
2588 NTSTATUS status;
2589 struct pdb_methods *methods;
2590 PyObject *py_acct_policy;
2591 uint32_t value;
2592 const char **names;
2593 int count, i;
2594 enum pdb_policy_type type;
2596 methods = pytalloc_get_ptr(self);
2598 py_acct_policy = PyDict_New();
2599 if (py_acct_policy == NULL) {
2600 PyErr_NoMemory();
2601 talloc_free(frame);
2602 return NULL;
2605 account_policy_names_list(frame, &names, &count);
2606 for (i=0; i<count; i++) {
2607 type = account_policy_name_to_typenum(names[i]);
2608 status = methods->get_account_policy(methods, type, &value);
2609 if (NT_STATUS_IS_OK(status)) {
2610 int res = 0;
2611 PyObject *py_value = Py_BuildValue("i", value);
2612 if (py_value == NULL) {
2613 Py_CLEAR(py_acct_policy);
2614 break;
2616 res = PyDict_SetItemString(py_acct_policy,
2617 names[i],
2618 py_value);
2619 Py_CLEAR(py_value);
2620 if (res == -1) {
2621 Py_CLEAR(py_acct_policy);
2622 break;
2627 talloc_free(frame);
2628 return py_acct_policy;
2632 static PyObject *py_pdb_set_account_policy(PyObject *self, PyObject *args)
2634 TALLOC_CTX *frame = talloc_stackframe();
2635 NTSTATUS status;
2636 struct pdb_methods *methods;
2637 PyObject *py_acct_policy, *py_value;
2638 const char **names;
2639 int count, i;
2640 enum pdb_policy_type type;
2642 if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2643 talloc_free(frame);
2644 return NULL;
2647 methods = pytalloc_get_ptr(self);
2649 account_policy_names_list(frame, &names, &count);
2650 for (i=0; i<count; i++) {
2651 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2652 type = account_policy_name_to_typenum(names[i]);
2653 status = methods->set_account_policy(methods, type, PyLong_AsLong(py_value));
2654 if (!NT_STATUS_IS_OK(status)) {
2655 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2656 names[i],
2657 NT_STATUS_V(status),
2658 get_friendly_nt_error_msg(status));
2663 talloc_free(frame);
2664 Py_RETURN_NONE;
2667 static PyObject *py_pdb_search_users(PyObject *self, PyObject *args)
2669 TALLOC_CTX *frame = talloc_stackframe();
2670 struct pdb_methods *methods;
2671 unsigned int acct_flags;
2672 struct pdb_search *search;
2673 struct samr_displayentry *entry;
2674 PyObject *py_userlist, *py_dict;
2676 if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2677 talloc_free(frame);
2678 return NULL;
2681 methods = pytalloc_get_ptr(self);
2683 search = talloc_zero(frame, struct pdb_search);
2684 if (search == NULL) {
2685 PyErr_NoMemory();
2686 talloc_free(frame);
2687 return NULL;
2690 if (!methods->search_users(methods, search, acct_flags)) {
2691 PyErr_Format(py_pdb_error, "Unable to search users");
2692 talloc_free(frame);
2693 return NULL;
2696 entry = talloc_zero(frame, struct samr_displayentry);
2697 if (entry == NULL) {
2698 PyErr_NoMemory();
2699 talloc_free(frame);
2700 return NULL;
2703 py_userlist = PyList_New(0);
2704 if (py_userlist == NULL) {
2705 PyErr_NoMemory();
2706 talloc_free(frame);
2707 return NULL;
2710 while (search->next_entry(search, entry)) {
2711 int res = 1;
2712 py_dict = Py_BuildValue(
2713 "{s:l, s:l, s:l, s:s, s:s, s:s}",
2714 "idx", entry->idx,
2715 "rid", entry->rid,
2716 "acct_flags", entry->acct_flags,
2717 "account_name", entry->account_name,
2718 "fullname", entry->fullname,
2719 "description", entry->description);
2720 if (py_dict == NULL) {
2721 Py_CLEAR(py_userlist);
2722 goto out;
2725 res = PyList_Append(py_userlist, py_dict);
2726 Py_CLEAR(py_dict);
2727 if (res == -1) {
2728 Py_CLEAR(py_userlist);
2729 goto out;
2732 search->search_end(search);
2734 out:
2735 talloc_free(frame);
2736 return py_userlist;
2740 static PyObject *py_pdb_search_groups(PyObject *self, PyObject *unused)
2742 TALLOC_CTX *frame = talloc_stackframe();
2743 struct pdb_methods *methods;
2744 struct pdb_search *search;
2745 struct samr_displayentry *entry;
2746 PyObject *py_grouplist, *py_dict;
2748 methods = pytalloc_get_ptr(self);
2750 search = talloc_zero(frame, struct pdb_search);
2751 if (search == NULL) {
2752 PyErr_NoMemory();
2753 talloc_free(frame);
2754 return NULL;
2757 if (!methods->search_groups(methods, search)) {
2758 PyErr_Format(py_pdb_error, "Unable to search groups");
2759 talloc_free(frame);
2760 return NULL;
2763 entry = talloc_zero(frame, struct samr_displayentry);
2764 if (entry == NULL) {
2765 PyErr_NoMemory();
2766 talloc_free(frame);
2767 return NULL;
2770 py_grouplist = PyList_New(0);
2771 if (py_grouplist == NULL) {
2772 PyErr_NoMemory();
2773 talloc_free(frame);
2774 return NULL;
2777 while (search->next_entry(search, entry)) {
2778 int res = 0;
2779 py_dict = Py_BuildValue(
2780 "{s:l, s:l, s:l, s:s, s:s, s:s}",
2781 "idx", entry->idx,
2782 "rid", entry->rid,
2783 "acct_flags", entry->acct_flags,
2784 "account_name", entry->account_name,
2785 "fullname", entry->fullname,
2786 "description", entry->description);
2788 if (py_dict == NULL) {
2789 Py_CLEAR(py_grouplist);
2790 goto out;
2793 res = PyList_Append(py_grouplist, py_dict);
2794 Py_CLEAR(py_dict);
2795 if (res == -1) {
2796 Py_CLEAR(py_grouplist);
2797 goto out;
2800 search->search_end(search);
2801 out:
2802 talloc_free(frame);
2803 return py_grouplist;
2807 static PyObject *py_pdb_search_aliases(PyObject *self, PyObject *args)
2809 TALLOC_CTX *frame = talloc_stackframe();
2810 struct pdb_methods *methods;
2811 struct pdb_search *search;
2812 struct samr_displayentry *entry;
2813 PyObject *py_aliaslist, *py_dict;
2814 PyObject *py_domain_sid = Py_None;
2815 struct dom_sid *domain_sid = NULL;
2817 Py_INCREF(Py_None);
2819 if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2820 talloc_free(frame);
2821 return NULL;
2824 methods = pytalloc_get_ptr(self);
2826 if (py_domain_sid != Py_None) {
2827 domain_sid = pytalloc_get_ptr(py_domain_sid);
2830 search = talloc_zero(frame, struct pdb_search);
2831 if (search == NULL) {
2832 PyErr_NoMemory();
2833 talloc_free(frame);
2834 return NULL;
2837 if (!methods->search_aliases(methods, search, domain_sid)) {
2838 PyErr_Format(py_pdb_error, "Unable to search aliases");
2839 talloc_free(frame);
2840 return NULL;
2843 entry = talloc_zero(frame, struct samr_displayentry);
2844 if (entry == NULL) {
2845 PyErr_NoMemory();
2846 talloc_free(frame);
2847 return NULL;
2850 py_aliaslist = PyList_New(0);
2851 if (py_aliaslist == NULL) {
2852 PyErr_NoMemory();
2853 talloc_free(frame);
2854 return NULL;
2857 while (search->next_entry(search, entry)) {
2858 int res = 0;
2860 py_dict = Py_BuildValue(
2861 "{s:l, s:l, s:l, s:s, s:s, s:s}",
2862 "idx", entry->idx,
2863 "rid", entry->rid,
2864 "acct_flags", entry->acct_flags,
2865 "account_name", entry->account_name,
2866 "fullname", entry->fullname,
2867 "description", entry->description);
2869 if (py_dict == NULL) {
2870 Py_CLEAR(py_aliaslist);
2871 goto out;
2873 res = PyList_Append(py_aliaslist, py_dict);
2874 Py_CLEAR(py_dict);
2875 if (res == -1) {
2876 Py_CLEAR(py_aliaslist);
2877 goto out;
2880 search->search_end(search);
2881 out:
2882 talloc_free(frame);
2883 return py_aliaslist;
2887 static PyObject *py_pdb_uid_to_sid(PyObject *self, PyObject *args)
2889 TALLOC_CTX *frame = talloc_stackframe();
2890 struct pdb_methods *methods;
2891 struct unixid id;
2892 unsigned int uid;
2893 struct dom_sid user_sid, *copy_user_sid;
2894 PyObject *py_user_sid;
2896 if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2897 talloc_free(frame);
2898 return NULL;
2901 methods = pytalloc_get_ptr(self);
2903 id.id = uid;
2904 id.type = ID_TYPE_UID;
2906 if (!methods->id_to_sid(methods, &id, &user_sid)) {
2907 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2908 talloc_free(frame);
2909 return NULL;
2912 copy_user_sid = dom_sid_dup(frame, &user_sid);
2913 if (copy_user_sid == NULL) {
2914 PyErr_NoMemory();
2915 talloc_free(frame);
2916 return NULL;
2919 py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2921 talloc_free(frame);
2922 return py_user_sid;
2926 static PyObject *py_pdb_gid_to_sid(PyObject *self, PyObject *args)
2928 TALLOC_CTX *frame = talloc_stackframe();
2929 struct pdb_methods *methods;
2930 struct unixid id;
2931 unsigned int gid;
2932 struct dom_sid group_sid, *copy_group_sid;
2933 PyObject *py_group_sid;
2935 if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2936 talloc_free(frame);
2937 return NULL;
2940 id.id = gid;
2941 id.type = ID_TYPE_GID;
2943 methods = pytalloc_get_ptr(self);
2945 if (!methods->id_to_sid(methods, &id, &group_sid)) {
2946 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2947 talloc_free(frame);
2948 return NULL;
2951 copy_group_sid = dom_sid_dup(frame, &group_sid);
2952 if (copy_group_sid == NULL) {
2953 PyErr_NoMemory();
2954 talloc_free(frame);
2955 return NULL;
2958 py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2960 talloc_free(frame);
2961 return py_group_sid;
2965 static PyObject *py_pdb_sid_to_id(PyObject *self, PyObject *args)
2967 TALLOC_CTX *frame = talloc_stackframe();
2968 struct pdb_methods *methods;
2969 PyObject *py_sid;
2970 struct dom_sid *sid;
2971 struct unixid id;
2973 if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2974 talloc_free(frame);
2975 return NULL;
2978 methods = pytalloc_get_ptr(self);
2980 sid = pytalloc_get_ptr(py_sid);
2982 if (!methods->sid_to_id(methods, sid, &id)) {
2983 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2984 talloc_free(frame);
2985 return NULL;
2988 talloc_free(frame);
2989 return Py_BuildValue("(II)", id.id, id.type);
2993 static PyObject *py_pdb_new_rid(PyObject *self, PyObject *unused)
2995 TALLOC_CTX *frame = talloc_stackframe();
2996 struct pdb_methods *methods;
2997 uint32_t rid;
2999 methods = pytalloc_get_ptr(self);
3001 if (!methods->new_rid(methods, &rid)) {
3002 PyErr_Format(py_pdb_error, "Unable to get new rid");
3003 talloc_free(frame);
3004 return NULL;
3007 talloc_free(frame);
3008 return PyLong_FromLong(rid);
3012 static PyObject *py_pdb_get_trusteddom_pw(PyObject *self, PyObject *args)
3014 TALLOC_CTX *frame = talloc_stackframe();
3015 struct pdb_methods *methods;
3016 const char *domain;
3017 char *pwd;
3018 struct dom_sid sid, *copy_sid;
3019 PyObject *py_sid;
3020 time_t last_set_time;
3021 PyObject *py_value;
3023 if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
3024 talloc_free(frame);
3025 return NULL;
3028 methods = pytalloc_get_ptr(self);
3030 if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
3031 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
3032 talloc_free(frame);
3033 return NULL;
3036 copy_sid = dom_sid_dup(frame, &sid);
3037 if (copy_sid == NULL) {
3038 PyErr_NoMemory();
3039 talloc_free(frame);
3040 return NULL;
3043 py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
3044 if (py_sid == NULL) {
3045 PyErr_NoMemory();
3046 talloc_free(frame);
3047 return NULL;
3050 py_value = Py_BuildValue(
3051 "{s:s, s:O, s:l}",
3052 "pwd", pwd,
3053 "sid", py_sid,
3054 "last_set_tim", last_set_time);
3056 Py_CLEAR(py_sid);
3057 talloc_free(frame);
3058 return py_value;
3062 static PyObject *py_pdb_set_trusteddom_pw(PyObject *self, PyObject *args)
3064 TALLOC_CTX *frame = talloc_stackframe();
3065 struct pdb_methods *methods;
3066 const char *domain;
3067 const char *pwd;
3068 const struct dom_sid *domain_sid;
3069 PyObject *py_domain_sid;
3071 if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
3072 dom_sid_Type, &py_domain_sid)) {
3073 talloc_free(frame);
3074 return NULL;
3077 methods = pytalloc_get_ptr(self);
3079 domain_sid = pytalloc_get_ptr(py_domain_sid);
3081 if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
3082 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
3083 talloc_free(frame);
3084 return NULL;
3087 talloc_free(frame);
3088 Py_RETURN_NONE;
3092 static PyObject *py_pdb_del_trusteddom_pw(PyObject *self, PyObject *args)
3094 TALLOC_CTX *frame = talloc_stackframe();
3095 struct pdb_methods *methods;
3096 const char *domain;
3098 if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
3099 talloc_free(frame);
3100 return NULL;
3103 methods = pytalloc_get_ptr(self);
3105 if (!methods->del_trusteddom_pw(methods, domain)) {
3106 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
3107 talloc_free(frame);
3108 return NULL;
3111 talloc_free(frame);
3112 Py_RETURN_NONE;
3116 static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
3118 TALLOC_CTX *frame = talloc_stackframe();
3119 NTSTATUS status;
3120 struct pdb_methods *methods;
3121 uint32_t i, num_domains;
3122 struct trustdom_info **domains;
3123 PyObject *py_domain_list, *py_dict;
3125 methods = pytalloc_get_ptr(self);
3127 status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
3128 if (!NT_STATUS_IS_OK(status)) {
3129 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
3130 NT_STATUS_V(status),
3131 get_friendly_nt_error_msg(status));
3132 talloc_free(frame);
3133 return NULL;
3136 py_domain_list = PyList_New(0);
3137 if (py_domain_list == NULL) {
3138 PyErr_NoMemory();
3139 talloc_free(frame);
3140 return NULL;
3143 for(i=0; i<num_domains; i++) {
3144 int res = 0;
3145 PyObject *py_sid =
3146 pytalloc_steal(dom_sid_Type, &domains[i]->sid);
3147 py_dict = Py_BuildValue(
3148 "{s:s, s:O}",
3149 "name", domains[i]->name,
3150 "sid", py_sid);
3151 Py_CLEAR(py_sid);
3152 if (py_dict == NULL) {
3153 DBG_ERR("Failed to insert entry to dict\n");
3154 Py_CLEAR(py_domain_list);
3155 break;
3158 res = PyList_Append(py_domain_list, py_dict);
3159 Py_CLEAR(py_dict);
3160 if (res == -1) {
3161 Py_CLEAR(py_domain_list);
3162 break;
3166 talloc_free(frame);
3167 return py_domain_list;
3171 static PyObject *py_pdb_get_trusted_domain(PyObject *self, PyObject *args)
3173 TALLOC_CTX *frame = talloc_stackframe();
3174 NTSTATUS status;
3175 struct pdb_methods *methods;
3176 const char *domain;
3177 struct pdb_trusted_domain *td;
3178 PyObject *py_domain_info;
3179 PyObject *py_sid = NULL;
3181 if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
3182 talloc_free(frame);
3183 return NULL;
3186 methods = pytalloc_get_ptr(self);
3188 status = methods->get_trusted_domain(methods, frame, domain, &td);
3189 if (!NT_STATUS_IS_OK(status)) {
3190 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3191 NT_STATUS_V(status),
3192 get_friendly_nt_error_msg(status));
3193 talloc_free(frame);
3194 return NULL;
3197 py_sid = pytalloc_steal(dom_sid_Type, &td->security_identifier);
3199 py_domain_info = Py_BuildValue(
3200 "{s:s, s:s, s:O,"
3201 " s:"PYARG_BYTES_LEN","
3202 " s:"PYARG_BYTES_LEN","
3203 " s:l, s:l, s:l,"
3204 " s:"PYARG_BYTES_LEN"}",
3205 "domain_name", td->domain_name,
3206 "netbios_name", td->netbios_name,
3207 "security_identifier", py_sid,
3208 "trust_auth_incoming",
3209 (const char *)td->trust_auth_incoming.data,
3210 td->trust_auth_incoming.length,
3211 "trust_auth_outgoing",
3212 (const char *)td->trust_auth_outgoing.data,
3213 td->trust_auth_outgoing.length,
3214 "trust_direction", td->trust_direction,
3215 "trust_type", td->trust_type,
3216 "trust_attributes", td->trust_attributes,
3217 "trust_forest_trust_info",
3218 (const char *)td->trust_forest_trust_info.data,
3219 td->trust_forest_trust_info.length);
3220 Py_CLEAR(py_sid);
3222 talloc_free(frame);
3223 return py_domain_info;
3227 static PyObject *py_pdb_get_trusted_domain_by_sid(PyObject *self, PyObject *args)
3229 TALLOC_CTX *frame = talloc_stackframe();
3230 NTSTATUS status;
3231 struct pdb_methods *methods;
3232 PyObject *py_domain_sid;
3233 struct dom_sid *domain_sid;
3234 struct pdb_trusted_domain *td;
3235 PyObject *py_domain_info;
3236 PyObject *py_sid = NULL;
3238 if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3239 talloc_free(frame);
3240 return NULL;
3243 methods = pytalloc_get_ptr(self);
3245 domain_sid = pytalloc_get_ptr(py_domain_sid);
3247 status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3248 if (!NT_STATUS_IS_OK(status)) {
3249 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3250 NT_STATUS_V(status),
3251 get_friendly_nt_error_msg(status));
3252 talloc_free(frame);
3253 return NULL;
3256 py_sid = pytalloc_steal(dom_sid_Type, &td->security_identifier);
3258 py_domain_info = Py_BuildValue(
3259 "{s:s, s:s, s:O,"
3260 " s:"PYARG_BYTES_LEN","
3261 " s:"PYARG_BYTES_LEN","
3262 " s:l, s:l, s:l,"
3263 " s:"PYARG_BYTES_LEN"}",
3264 "domain_name", td->domain_name,
3265 "netbios_name", td->netbios_name,
3266 "security_identifier", py_sid,
3267 "trust_auth_incoming",
3268 (const char *)td->trust_auth_incoming.data,
3269 td->trust_auth_incoming.length,
3270 "trust_auth_outgoing",
3271 (const char *)td->trust_auth_outgoing.data,
3272 td->trust_auth_outgoing.length,
3273 "trust_direction", td->trust_direction,
3274 "trust_type", td->trust_type,
3275 "trust_attributes", td->trust_attributes,
3276 "trust_forest_trust_info",
3277 (const char *)td->trust_forest_trust_info.data,
3278 td->trust_forest_trust_info.length);
3279 Py_CLEAR(py_sid);
3281 talloc_free(frame);
3282 return py_domain_info;
3286 static PyObject *py_pdb_set_trusted_domain(PyObject *self, PyObject *args)
3288 TALLOC_CTX *frame = talloc_stackframe();
3289 NTSTATUS status;
3290 struct pdb_methods *methods;
3291 const char *domain;
3292 PyObject *py_td_info;
3293 struct pdb_trusted_domain td_info;
3294 PyObject *py_tmp;
3295 Py_ssize_t len;
3297 if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3298 talloc_free(frame);
3299 return NULL;
3302 py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3303 td_info.domain_name = discard_const_p(char, PyUnicode_AsUTF8(py_tmp));
3305 py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3306 td_info.netbios_name = discard_const_p(char, PyUnicode_AsUTF8(py_tmp));
3308 py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3309 td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3311 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3312 PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3313 td_info.trust_auth_incoming.length = len;
3315 py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3316 PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3317 td_info.trust_auth_outgoing.length = len;
3319 py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3320 td_info.trust_direction = PyLong_AsLong(py_tmp);
3322 py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3323 td_info.trust_type = PyLong_AsLong(py_tmp);
3325 py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3326 td_info.trust_attributes = PyLong_AsLong(py_tmp);
3328 py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3329 PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3330 td_info.trust_forest_trust_info.length = len;
3332 methods = pytalloc_get_ptr(self);
3334 status = methods->set_trusted_domain(methods, domain, &td_info);
3335 if (!NT_STATUS_IS_OK(status)) {
3336 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3337 NT_STATUS_V(status),
3338 get_friendly_nt_error_msg(status));
3339 talloc_free(frame);
3340 return NULL;
3343 talloc_free(frame);
3344 Py_RETURN_NONE;
3348 static PyObject *py_pdb_del_trusted_domain(PyObject *self, PyObject *args)
3350 TALLOC_CTX *frame = talloc_stackframe();
3351 NTSTATUS status;
3352 struct pdb_methods *methods;
3353 const char *domain;
3355 if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3356 talloc_free(frame);
3357 return NULL;
3360 methods = pytalloc_get_ptr(self);
3362 status = methods->del_trusted_domain(methods, domain);
3363 if (!NT_STATUS_IS_OK(status)) {
3364 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3365 NT_STATUS_V(status),
3366 get_friendly_nt_error_msg(status));
3367 talloc_free(frame);
3368 return NULL;
3371 talloc_free(frame);
3372 Py_RETURN_NONE;
3376 static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
3378 TALLOC_CTX *frame = talloc_stackframe();
3379 NTSTATUS status;
3380 struct pdb_methods *methods;
3381 uint32_t i, num_domains;
3382 struct pdb_trusted_domain **td_info;
3383 PyObject *py_td_info, *py_domain_info;
3385 methods = pytalloc_get_ptr(self);
3387 status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3388 if (!NT_STATUS_IS_OK(status)) {
3389 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3390 NT_STATUS_V(status),
3391 get_friendly_nt_error_msg(status));
3392 talloc_free(frame);
3393 return NULL;
3396 py_td_info = PyList_New(0);
3397 if (py_td_info == NULL) {
3398 PyErr_NoMemory();
3399 talloc_free(frame);
3400 return NULL;
3403 for (i=0; i<num_domains; i++) {
3404 int res = 0;
3405 struct pdb_trusted_domain *td = td_info[i];
3406 PyObject *py_sid =
3407 pytalloc_steal(dom_sid_Type, &td->security_identifier);
3409 py_domain_info = Py_BuildValue(
3410 "{s:s, s:s, s:O,"
3411 " s:"PYARG_BYTES_LEN","
3412 " s:"PYARG_BYTES_LEN","
3413 " s:l, s:l, s:l,"
3414 " s:"PYARG_BYTES_LEN"}",
3415 "domain_name", td->domain_name,
3416 "netbios_name", td->netbios_name,
3417 "security_identifier", py_sid,
3418 "trust_auth_incoming",
3419 (const char *)td->trust_auth_incoming.data,
3420 td->trust_auth_incoming.length,
3421 "trust_auth_outgoing",
3422 (const char *)td->trust_auth_outgoing.data,
3423 td->trust_auth_outgoing.length,
3424 "trust_direction", td->trust_direction,
3425 "trust_type", td->trust_type,
3426 "trust_attributes", td->trust_attributes,
3427 "trust_forest_trust_info",
3428 (const char *)td->trust_forest_trust_info.data,
3429 td->trust_forest_trust_info.length);
3430 Py_CLEAR(py_sid);
3432 if (py_domain_info == NULL) {
3433 Py_CLEAR(py_td_info);
3434 break;
3436 res = PyList_Append(py_td_info, py_domain_info);
3437 Py_CLEAR(py_domain_info);
3438 if (res == -1) {
3439 Py_CLEAR(py_td_info);
3440 break;
3444 talloc_free(frame);
3445 return py_td_info;
3449 static PyObject *py_pdb_get_secret(PyObject *self, PyObject *args)
3451 TALLOC_CTX *frame = talloc_stackframe();
3452 NTSTATUS status;
3453 struct pdb_methods *methods;
3454 const char *secret_name;
3455 DATA_BLOB secret_current, secret_old;
3456 NTTIME secret_current_lastchange, secret_old_lastchange;
3457 PyObject *py_sd;
3458 struct security_descriptor *sd;
3459 PyObject *py_secret;
3461 if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3462 talloc_free(frame);
3463 return NULL;
3466 methods = pytalloc_get_ptr(self);
3468 py_sd = pytalloc_new(struct security_descriptor, security_Type);
3469 if (py_sd == NULL) {
3470 PyErr_NoMemory();
3471 talloc_free(frame);
3472 return NULL;
3474 sd = pytalloc_get_ptr(py_sd);
3476 status = methods->get_secret(methods, frame, secret_name,
3477 &secret_current,
3478 &secret_current_lastchange,
3479 &secret_old,
3480 &secret_old_lastchange,
3481 &sd);
3482 if (!NT_STATUS_IS_OK(status)) {
3483 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3484 secret_name,
3485 NT_STATUS_V(status),
3486 get_friendly_nt_error_msg(status));
3487 talloc_free(frame);
3488 return NULL;
3491 py_secret = Py_BuildValue(
3492 "{s:"PYARG_BYTES_LEN","
3493 " s:K"
3494 " s:"PYARG_BYTES_LEN","
3495 " s:K, s:O}",
3496 "secret_current", (const char*)secret_current.data,
3497 secret_current.length,
3498 "secret_current_lastchange", secret_current_lastchange,
3499 "secret_old", (const char*)secret_old.data,
3500 secret_old.length,
3501 "secret_old_lastchange", secret_old_lastchange,
3502 "sd", py_sd);
3504 Py_CLEAR(py_sd);
3505 if (py_secret == NULL) {
3506 talloc_free(frame);
3507 return NULL;
3510 talloc_free(frame);
3511 return py_secret;
3515 static PyObject *py_pdb_set_secret(PyObject *self, PyObject *args)
3517 TALLOC_CTX *frame = talloc_stackframe();
3518 NTSTATUS status;
3519 struct pdb_methods *methods;
3520 const char *secret_name;
3521 PyObject *py_secret;
3522 PyObject *py_secret_cur, *py_secret_old, *py_sd;
3523 DATA_BLOB secret_current, secret_old;
3524 struct security_descriptor *sd;
3525 Py_ssize_t len;
3527 if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3528 talloc_free(frame);
3529 return NULL;
3532 py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3533 py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3534 py_sd = PyDict_GetItemString(py_secret, "sd");
3536 PY_CHECK_TYPE(&PyBytes_Type, py_secret_cur, return NULL;);
3537 PY_CHECK_TYPE(&PyBytes_Type, py_secret_old, return NULL;);
3538 PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3540 methods = pytalloc_get_ptr(self);
3542 PyBytes_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3543 secret_current.length = len;
3544 PyBytes_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3545 secret_current.length = len;
3546 sd = pytalloc_get_ptr(py_sd);
3548 status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3549 if (!NT_STATUS_IS_OK(status)) {
3550 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3551 secret_name,
3552 NT_STATUS_V(status),
3553 get_friendly_nt_error_msg(status));
3554 talloc_free(frame);
3555 return NULL;
3558 talloc_free(frame);
3559 Py_RETURN_NONE;
3563 static PyObject *py_pdb_delete_secret(PyObject *self, PyObject *args)
3565 TALLOC_CTX *frame = talloc_stackframe();
3566 NTSTATUS status;
3567 struct pdb_methods *methods;
3568 const char *secret_name;
3570 if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3571 talloc_free(frame);
3572 return NULL;
3575 methods = pytalloc_get_ptr(self);
3577 status = methods->delete_secret(methods, secret_name);
3578 if (!NT_STATUS_IS_OK(status)) {
3579 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3580 secret_name,
3581 NT_STATUS_V(status),
3582 get_friendly_nt_error_msg(status));
3583 talloc_free(frame);
3584 return NULL;
3587 talloc_free(frame);
3588 Py_RETURN_NONE;
3591 static PyMethodDef py_pdb_methods[] = {
3592 { "domain_info", py_pdb_domain_info, METH_NOARGS,
3593 "domain_info() -> str\n\n \
3594 Get domain information for the database." },
3595 { "getsampwnam", py_pdb_getsampwnam, METH_VARARGS,
3596 "getsampwnam(username) -> samu object\n\n \
3597 Get user information by name." },
3598 { "getsampwsid", py_pdb_getsampwsid, METH_VARARGS,
3599 "getsampwsid(user_sid) -> samu object\n\n \
3600 Get user information by sid (dcerpc.security.dom_sid object)." },
3601 { "create_user", py_pdb_create_user, METH_VARARGS,
3602 "create_user(username, acct_flags) -> rid\n\n \
3603 Create user. acct_flags are samr account control flags." },
3604 { "delete_user", py_pdb_delete_user, METH_VARARGS,
3605 "delete_user(samu object) -> None\n\n \
3606 Delete user." },
3607 { "add_sam_account", py_pdb_add_sam_account, METH_VARARGS,
3608 "add_sam_account(samu object) -> None\n\n \
3609 Add SAM account." },
3610 { "update_sam_account", py_pdb_update_sam_account, METH_VARARGS,
3611 "update_sam_account(samu object) -> None\n\n \
3612 Update SAM account." },
3613 { "delete_sam_account", py_pdb_delete_sam_account, METH_VARARGS,
3614 "delete_sam_account(samu object) -> None\n\n \
3615 Delete SAM account." },
3616 { "rename_sam_account", py_pdb_rename_sam_account, METH_VARARGS,
3617 "rename_sam_account(samu object1, new_username) -> None\n\n \
3618 Rename SAM account." },
3619 /* update_login_attempts */
3620 { "getgrsid", py_pdb_getgrsid, METH_VARARGS,
3621 "getgrsid(group_sid) -> groupmap object\n\n \
3622 Get group information by sid (dcerpc.security.dom_sid object)." },
3623 { "getgrgid", py_pdb_getgrgid, METH_VARARGS,
3624 "getgrsid(gid) -> groupmap object\n\n \
3625 Get group information by gid." },
3626 { "getgrnam", py_pdb_getgrnam, METH_VARARGS,
3627 "getgrsid(groupname) -> groupmap object\n\n \
3628 Get group information by name." },
3629 { "create_dom_group", py_pdb_create_dom_group, METH_VARARGS,
3630 "create_dom_group(groupname) -> group_rid\n\n \
3631 Create new domain group by name." },
3632 { "delete_dom_group", py_pdb_delete_dom_group, METH_VARARGS,
3633 "delete_dom_group(group_rid) -> None\n\n \
3634 Delete domain group identified by rid" },
3635 { "add_group_mapping_entry", py_pdb_add_group_mapping_entry, METH_VARARGS,
3636 "add_group_mapping_entry(groupmap) -> None\n \
3637 Add group mapping entry for groupmap object." },
3638 { "update_group_mapping_entry", py_pdb_update_group_mapping_entry, METH_VARARGS,
3639 "update_group_mapping_entry(groupmap) -> None\n\n \
3640 Update group mapping entry for groupmap object." },
3641 { "delete_group_mapping_entry", py_pdb_delete_group_mapping_entry, METH_VARARGS,
3642 "delete_group_mapping_entry(groupmap) -> None\n\n \
3643 Delete group mapping entry for groupmap object." },
3644 { "enum_group_mapping", py_pdb_enum_group_mapping, METH_VARARGS,
3645 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3646 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3647 { "enum_group_members", py_pdb_enum_group_members, METH_VARARGS,
3648 "enum_group_members(group_sid) -> List\n\n \
3649 Return list of users (dom_sid object) in group." },
3650 { "enum_group_memberships", py_pdb_enum_group_memberships, METH_VARARGS,
3651 "enum_group_memberships(samu object) -> List\n\n \
3652 Return list of groups (dom_sid object) this user is part of." },
3653 /* set_unix_primary_group */
3654 { "add_groupmem", py_pdb_add_groupmem, METH_VARARGS,
3655 "add_groupmem(group_rid, member_rid) -> None\n\n \
3656 Add user to group." },
3657 { "del_groupmem", py_pdb_del_groupmem, METH_VARARGS,
3658 "del_groupmem(group_rid, member_rid) -> None\n\n \
3659 Remove user from from group." },
3660 { "create_alias", py_pdb_create_alias, METH_VARARGS,
3661 "create_alias(alias_name) -> alias_rid\n\n \
3662 Create alias entry." },
3663 { "delete_alias", py_pdb_delete_alias, METH_VARARGS,
3664 "delete_alias(alias_sid) -> None\n\n \
3665 Delete alias entry." },
3666 { "get_aliasinfo", py_pdb_get_aliasinfo, METH_VARARGS,
3667 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3668 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3669 { "set_aliasinfo", py_pdb_set_aliasinfo, METH_VARARGS,
3670 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3671 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3672 { "add_aliasmem", py_pdb_add_aliasmem, METH_VARARGS,
3673 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3674 Add user to alias entry." },
3675 { "del_aliasmem", py_pdb_del_aliasmem, METH_VARARGS,
3676 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3677 Remove a user from alias entry." },
3678 { "enum_aliasmem", py_pdb_enum_aliasmem, METH_VARARGS,
3679 "enum_aliasmem(alias_sid) -> List\n\n \
3680 Return a list of members (dom_sid object) for alias entry." },
3681 /* enum_alias_memberships */
3682 /* lookup_rids */
3683 /* lookup_names */
3684 { "get_account_policy", py_pdb_get_account_policy, METH_NOARGS,
3685 "get_account_policy() -> Mapping\n\n \
3686 Get account policy information as a dictionary." },
3687 { "set_account_policy", py_pdb_set_account_policy, METH_VARARGS,
3688 "get_account_policy(Mapping) -> None\n\n \
3689 Set account policy settings from a dicionary." },
3690 /* get_seq_num */
3691 { "search_users", py_pdb_search_users, METH_VARARGS,
3692 "search_users(acct_flags) -> List\n\n \
3693 Search users. acct_flags are samr account control flags.\n \
3694 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3695 { "search_groups", py_pdb_search_groups, METH_NOARGS,
3696 "search_groups() -> List\n\n \
3697 Search unix only groups. \n \
3698 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3699 { "search_aliases", py_pdb_search_aliases, METH_VARARGS,
3700 "search_aliases([domain_sid]) -> List\n\n \
3701 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3702 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3703 { "uid_to_sid", py_pdb_uid_to_sid, METH_VARARGS,
3704 "uid_to_sid(uid) -> sid\n\n \
3705 Return sid for given user id." },
3706 { "gid_to_sid", py_pdb_gid_to_sid, METH_VARARGS,
3707 "gid_to_sid(gid) -> sid\n\n \
3708 Return sid for given group id." },
3709 { "sid_to_id", py_pdb_sid_to_id, METH_VARARGS,
3710 "sid_to_id(sid) -> Tuple\n\n \
3711 Return id and type for given sid." },
3712 /* capabilities */
3713 { "new_rid", py_pdb_new_rid, METH_NOARGS,
3714 "new_rid() -> rid\n\n \
3715 Get a new rid." },
3716 { "get_trusteddom_pw", py_pdb_get_trusteddom_pw, METH_VARARGS,
3717 "get_trusteddom_pw(domain) -> Mapping\n\n \
3718 Get trusted domain password, sid and last set time in a dictionary." },
3719 { "set_trusteddom_pw", py_pdb_set_trusteddom_pw, METH_VARARGS,
3720 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3721 Set trusted domain password." },
3722 { "del_trusteddom_pw", py_pdb_del_trusteddom_pw, METH_VARARGS,
3723 "del_trusteddom_pw(domain) -> None\n\n \
3724 Delete trusted domain password." },
3725 { "enum_trusteddoms", py_pdb_enum_trusteddoms, METH_NOARGS,
3726 "enum_trusteddoms() -> List\n\n \
3727 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3728 { "get_trusted_domain", py_pdb_get_trusted_domain, METH_VARARGS,
3729 "get_trusted_domain(domain) -> Mapping\n\n \
3730 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." },
3731 { "get_trusted_domain_by_sid", py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3732 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3733 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" },
3734 { "set_trusted_domain", py_pdb_set_trusted_domain, METH_VARARGS,
3735 "set_trusted_domain(domain, Mapping) -> None\n\n \
3736 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." },
3737 { "del_trusted_domain", py_pdb_del_trusted_domain, METH_VARARGS,
3738 "del_trusted_domain(domain) -> None\n\n \
3739 Delete trusted domain." },
3740 { "enum_trusted_domains", py_pdb_enum_trusted_domains, METH_VARARGS,
3741 "enum_trusted_domains() -> List\n\n \
3742 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." },
3743 { "get_secret", py_pdb_get_secret, METH_VARARGS,
3744 "get_secret(secret_name) -> Mapping\n\n \
3745 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3746 { "set_secret", py_pdb_set_secret, METH_VARARGS,
3747 "set_secret(secret_name, Mapping) -> None\n\n \
3748 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3749 { "delete_secret", py_pdb_delete_secret, METH_VARARGS,
3750 "delete_secret(secret_name) -> None\n\n \
3751 Delete secret information for secret_name." },
3752 {0},
3756 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3758 TALLOC_CTX *frame = talloc_stackframe();
3759 const char *url = NULL;
3760 PyObject *pypdb;
3761 NTSTATUS status;
3762 struct pdb_methods *methods;
3764 if (!PyArg_ParseTuple(args, "s", &url)) {
3765 talloc_free(frame);
3766 return NULL;
3769 /* Initialize list of methods */
3770 status = make_pdb_method_name(&methods, url);
3771 if (!NT_STATUS_IS_OK(status)) {
3772 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3773 url,
3774 NT_STATUS_V(status),
3775 get_friendly_nt_error_msg(status));
3776 talloc_free(frame);
3777 return NULL;
3780 if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3781 PyErr_NoMemory();
3782 talloc_free(frame);
3783 return NULL;
3786 talloc_free(frame);
3787 return pypdb;
3791 static PyTypeObject PyPDB = {
3792 .tp_name = "passdb.PDB",
3793 .tp_new = py_pdb_new,
3794 .tp_flags = Py_TPFLAGS_DEFAULT,
3795 .tp_methods = py_pdb_methods,
3796 .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3801 * Return a list of passdb backends
3803 static PyObject *py_passdb_backends(PyObject *self, PyObject *unused)
3805 TALLOC_CTX *frame = talloc_stackframe();
3806 PyObject *py_blist;
3807 const struct pdb_init_function_entry *entry;
3809 entry = pdb_get_backends();
3810 if(! entry) {
3811 Py_RETURN_NONE;
3814 if((py_blist = PyList_New(0)) == NULL) {
3815 PyErr_NoMemory();
3816 talloc_free(frame);
3817 return NULL;
3820 while(entry) {
3821 int res = 0;
3822 PyObject *entry_name = PyUnicode_FromString(entry->name);
3823 if (entry_name) {
3824 res = PyList_Append(py_blist, entry_name);
3825 } else {
3826 Py_CLEAR(entry_name);
3827 Py_CLEAR(py_blist);
3828 break;
3830 Py_CLEAR(entry_name);
3831 if (res == -1) {
3832 Py_CLEAR(py_blist);
3833 break;
3835 entry = entry->next;
3838 talloc_free(frame);
3839 return py_blist;
3843 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3845 TALLOC_CTX *frame = talloc_stackframe();
3846 const char *smb_config;
3848 if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3849 talloc_free(frame);
3850 return NULL;
3853 /* Load smbconf parameters */
3854 if (!lp_load_global(smb_config)) {
3855 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3856 talloc_free(frame);
3857 return NULL;
3860 talloc_free(frame);
3861 Py_RETURN_NONE;
3865 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3867 TALLOC_CTX *frame = talloc_stackframe();
3868 const char *private_dir;
3870 if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3871 talloc_free(frame);
3872 return NULL;
3875 /* Initialize secrets database */
3876 if (!secrets_init_path(private_dir)) {
3877 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3878 private_dir);
3879 talloc_free(frame);
3880 return NULL;
3883 talloc_free(frame);
3884 Py_RETURN_NONE;
3887 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3889 TALLOC_CTX *frame = talloc_stackframe();
3891 /* Initialize secrets database */
3892 if (!initialize_password_db(true, NULL)) {
3893 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3894 talloc_free(frame);
3895 return NULL;
3898 talloc_free(frame);
3899 Py_RETURN_NONE;
3902 static PyObject *py_get_domain_sid(PyObject *self, PyObject *unused)
3904 TALLOC_CTX *frame = talloc_stackframe();
3905 struct dom_sid domain_sid, *domain_sid_copy;
3906 PyObject *py_dom_sid = Py_None;
3907 bool ret = false;
3909 ret = secrets_fetch_domain_sid(lp_workgroup(), &domain_sid);
3910 if (!ret) {
3911 talloc_free(frame);
3912 return PyErr_NoMemory();
3915 domain_sid_copy = dom_sid_dup(frame, &domain_sid);
3916 if (domain_sid_copy == NULL) {
3917 talloc_free(frame);
3918 return PyErr_NoMemory();
3921 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3923 talloc_free(frame);
3924 return py_dom_sid;
3927 static PyObject *py_get_global_sam_sid(PyObject *self, PyObject *unused)
3929 TALLOC_CTX *frame = talloc_stackframe();
3930 struct dom_sid *domain_sid, *domain_sid_copy;
3931 PyObject *py_dom_sid;
3933 domain_sid = get_global_sam_sid();
3935 domain_sid_copy = dom_sid_dup(frame, domain_sid);
3936 if (domain_sid_copy == NULL) {
3937 PyErr_NoMemory();
3938 talloc_free(frame);
3939 return NULL;
3942 py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3944 talloc_free(frame);
3945 return py_dom_sid;
3949 static PyMethodDef py_passdb_methods[] = {
3950 { "get_backends", py_passdb_backends, METH_NOARGS,
3951 "get_backends() -> list\n\n \
3952 Get a list of password database backends supported." },
3953 { "set_smb_config", py_set_smb_config, METH_VARARGS,
3954 "set_smb_config(path) -> None\n\n \
3955 Set path to smb.conf file to load configuration parameters." },
3956 { "set_secrets_dir", py_set_secrets_dir, METH_VARARGS,
3957 "set_secrets_dir(private_dir) -> None\n\n \
3958 Set path to private directory to load secrets database from non-default location." },
3959 { "get_global_sam_sid", py_get_global_sam_sid, METH_NOARGS,
3960 "get_global_sam_sid() -> dom_sid\n\n \
3961 Return domain SID." },
3962 { "get_domain_sid", py_get_domain_sid, METH_NOARGS,
3963 "get_domain_sid() -> dom_sid\n\n \
3964 Return domain SID from secrets database." },
3965 { "reload_static_pdb", py_reload_static_pdb, METH_NOARGS,
3966 "reload_static_pdb() -> None\n\n \
3967 Re-initialise the static pdb used internally. Needed if 'passdb backend' is changed." },
3968 {0},
3971 static struct PyModuleDef moduledef = {
3972 PyModuleDef_HEAD_INIT,
3973 .m_name = "passdb",
3974 .m_doc = "SAMBA Password Database",
3975 .m_size = -1,
3976 .m_methods = py_passdb_methods,
3979 MODULE_INIT_FUNC(passdb)
3981 TALLOC_CTX *frame = talloc_stackframe();
3982 PyObject *m = NULL, *mod = NULL;
3983 char exception_name[] = "passdb.error";
3985 if (pytalloc_BaseObject_PyType_Ready(&PyPDB) < 0) {
3986 talloc_free(frame);
3987 return NULL;
3990 if (pytalloc_BaseObject_PyType_Ready(&PySamu) < 0) {
3991 talloc_free(frame);
3992 return NULL;
3995 if (pytalloc_BaseObject_PyType_Ready(&PyGroupmap) < 0) {
3996 talloc_free(frame);
3997 return NULL;
4000 m = PyModule_Create(&moduledef);
4001 if (m == NULL) {
4002 talloc_free(frame);
4003 return NULL;
4006 /* Create new exception for passdb module */
4007 py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
4008 Py_INCREF(py_pdb_error);
4009 PyModule_AddObject(m, "error", py_pdb_error);
4011 Py_INCREF(&PyPDB);
4012 PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
4014 Py_INCREF(&PySamu);
4015 PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
4017 Py_INCREF(&PyGroupmap);
4018 PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
4020 /* Import dom_sid type from dcerpc.security */
4021 mod = PyImport_ImportModule("samba.dcerpc.security");
4022 if (mod == NULL) {
4023 talloc_free(frame);
4024 return NULL;
4027 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
4028 if (dom_sid_Type == NULL) {
4029 Py_DECREF(mod);
4030 talloc_free(frame);
4031 return NULL;
4034 /* Import security_descriptor type from dcerpc.security */
4035 security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
4036 Py_DECREF(mod);
4037 if (security_Type == NULL) {
4038 Py_DECREF(dom_sid_Type);
4039 talloc_free(frame);
4040 return NULL;
4043 /* Import GUID type from dcerpc.misc */
4044 mod = PyImport_ImportModule("samba.dcerpc.misc");
4045 if (mod == NULL) {
4046 Py_DECREF(security_Type);
4047 Py_DECREF(dom_sid_Type);
4048 talloc_free(frame);
4049 return NULL;
4052 guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
4053 Py_DECREF(mod);
4054 if (guid_Type == NULL) {
4055 Py_DECREF(security_Type);
4056 Py_DECREF(dom_sid_Type);
4057 talloc_free(frame);
4058 return NULL;
4060 talloc_free(frame);
4061 return m;