winbindd: Fix indentation
[sfrench/samba-autobuild/.git] / source3 / passdb / py_passdb.c
1 /*
2    Python interface to passdb
3
4    Copyright (C) Amitay Isaacs 2011
5
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.
10
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.
15
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/>.
18 */
19
20 #include <Python.h>
21 #include <pytalloc.h>
22 #include "includes.h"
23 #include "lib/util/talloc_stack.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/idmap.h"
26 #include "passdb.h"
27 #include "secrets.h"
28 #include "idmap.h"
29
30 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
31 #define Py_TYPE(ob)             (((PyObject*)(ob))->ob_type)
32 #endif
33
34 #ifndef PY_CHECK_TYPE
35 #define PY_CHECK_TYPE(type, var, fail) \
36         if (!PyObject_TypeCheck(var, type)) {\
37                 PyErr_Format(PyExc_TypeError, __location__ ": Expected type '%s' for '%s' of type '%s'", (type)->tp_name, #var, Py_TYPE(var)->tp_name); \
38                 fail; \
39         }
40 #endif
41
42
43 static PyTypeObject *dom_sid_Type = NULL;
44 static PyTypeObject *security_Type = NULL;
45 static PyTypeObject *guid_Type = NULL;
46
47 static PyTypeObject PySamu;
48 static PyTypeObject PyGroupmap;
49 static PyTypeObject PyPDB;
50
51 static PyObject *py_pdb_error;
52
53 void initpassdb(void);
54
55
56 /************************** PIDL Autogeneratd ******************************/
57
58 static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
59 {
60         TALLOC_CTX *frame = talloc_stackframe();
61         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
62         PyObject *py_logon_time;
63
64         py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
65         talloc_free(frame);
66         return py_logon_time;
67 }
68
69 static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
70 {
71         TALLOC_CTX *frame = talloc_stackframe();
72         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
73
74         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
75         if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
76                 talloc_free(frame);
77                 return -1;
78         }
79         talloc_free(frame);
80         return 0;
81 }
82
83 static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
84 {
85         TALLOC_CTX *frame = talloc_stackframe();
86         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
87         PyObject *py_logoff_time;
88
89         py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
90         talloc_free(frame);
91         return py_logoff_time;
92 }
93
94 static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure)
95 {
96         TALLOC_CTX *frame = talloc_stackframe();
97         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
98
99         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
100         if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
101                 talloc_free(frame);
102                 return -1;
103         }
104         talloc_free(frame);
105         return 0;
106 }
107
108 static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
109 {
110         TALLOC_CTX *frame = talloc_stackframe();
111         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
112         PyObject *py_kickoff_time;
113
114         py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
115         talloc_free(frame);
116         return py_kickoff_time;
117 }
118
119 static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closure)
120 {
121         TALLOC_CTX *frame = talloc_stackframe();
122         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
123
124         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
125         if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
126                 talloc_free(frame);
127                 return -1;
128         }
129         talloc_free(frame);
130         return 0;
131 }
132
133 static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
134 {
135         TALLOC_CTX *frame = talloc_stackframe();
136         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
137         PyObject *py_bad_password_time;
138
139         py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
140         talloc_free(frame);
141         return py_bad_password_time;
142 }
143
144 static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *closure)
145 {
146         TALLOC_CTX *frame = talloc_stackframe();
147         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
148
149         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
150         if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
151                 talloc_free(frame);
152                 return -1;
153         }
154         talloc_free(frame);
155         return 0;
156 }
157
158 static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
159 {
160         TALLOC_CTX *frame = talloc_stackframe();
161         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
162         PyObject *py_pass_last_set_time;
163
164         py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
165         talloc_free(frame);
166         return py_pass_last_set_time;
167 }
168
169 static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *closure)
170 {
171         TALLOC_CTX *frame = talloc_stackframe();
172         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
173
174         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
175         if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
176                 talloc_free(frame);
177                 return -1;
178         }
179         talloc_free(frame);
180         return 0;
181 }
182
183 static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
184 {
185         TALLOC_CTX *frame = talloc_stackframe();
186         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
187         PyObject *py_pass_can_change_time;
188
189         py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
190         talloc_free(frame);
191         return py_pass_can_change_time;
192 }
193
194 static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void *closure)
195 {
196         TALLOC_CTX *frame = talloc_stackframe();
197         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
198
199         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
200         if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
201                 talloc_free(frame);
202                 return -1;
203         }
204         talloc_free(frame);
205         return 0;
206 }
207
208 static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
209 {
210         TALLOC_CTX *frame = talloc_stackframe();
211         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
212         PyObject *py_pass_must_change_time;
213
214         py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
215         talloc_free(frame);
216         return py_pass_must_change_time;
217 }
218
219 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
220 {
221         TALLOC_CTX *frame = talloc_stackframe();
222         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
223
224         /* TODO: make this not a get/set or give a better exception */
225         talloc_free(frame);
226         return -1;
227 }
228
229 static PyObject *py_samu_get_username(PyObject *obj, void *closure)
230 {
231         TALLOC_CTX *frame = talloc_stackframe();
232         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
233         PyObject *py_username;
234         const char *username;
235
236         username = pdb_get_username(sam_acct);
237         if (username == NULL) {
238                 Py_RETURN_NONE;
239         }
240
241         py_username = PyString_FromString(username);
242         talloc_free(frame);
243         return py_username;
244 }
245
246 static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
247 {
248         TALLOC_CTX *frame = talloc_stackframe();
249         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
250
251         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
252         if (!pdb_set_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
253                 talloc_free(frame);
254                 return -1;
255         }
256         talloc_free(frame);
257         return 0;
258 }
259
260 static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
261 {
262         TALLOC_CTX *frame = talloc_stackframe();
263         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
264         PyObject *py_domain;
265         const char *domain;
266
267         domain = pdb_get_domain(sam_acct);
268         if (domain == NULL) {
269                 Py_RETURN_NONE;
270         }
271
272         py_domain = PyString_FromString(domain);
273         talloc_free(frame);
274         return py_domain;
275 }
276
277 static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
278 {
279         TALLOC_CTX *frame = talloc_stackframe();
280         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
281
282         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
283         if (!pdb_set_domain(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
284                 talloc_free(frame);
285                 return -1;
286         }
287         talloc_free(frame);
288         return 0;
289 }
290
291 static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
292 {
293         TALLOC_CTX *frame = talloc_stackframe();
294         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
295         PyObject *py_nt_username;
296         const char *nt_username;
297
298         nt_username = pdb_get_nt_username(sam_acct);
299         if (nt_username == NULL) {
300                 Py_RETURN_NONE;
301         }
302
303         py_nt_username = PyString_FromString(nt_username);
304         talloc_free(frame);
305         return py_nt_username;
306 }
307
308 static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure)
309 {
310         TALLOC_CTX *frame = talloc_stackframe();
311         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
312
313         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
314         if (!pdb_set_nt_username(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
315                 talloc_free(frame);
316                 return -1;
317         }
318         talloc_free(frame);
319         return 0;
320 }
321
322 static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
323 {
324         TALLOC_CTX *frame = talloc_stackframe();
325         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
326         PyObject *py_full_name;
327         const char *full_name;
328
329         full_name = pdb_get_fullname(sam_acct);
330         if (full_name == NULL) {
331                 Py_RETURN_NONE;
332         }
333
334         py_full_name = PyString_FromString(full_name);
335         talloc_free(frame);
336         return py_full_name;
337 }
338
339 static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
340 {
341         TALLOC_CTX *frame = talloc_stackframe();
342         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
343
344         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
345         if (!pdb_set_fullname(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
346                 talloc_free(frame);
347                 return -1;
348         }
349         talloc_free(frame);
350         return 0;
351 }
352
353 static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
354 {
355         TALLOC_CTX *frame = talloc_stackframe();
356         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
357         PyObject *py_home_dir;
358         const char *home_dir;
359
360         home_dir = pdb_get_homedir(sam_acct);
361         if (home_dir == NULL) {
362                 Py_RETURN_NONE;
363         }
364
365         py_home_dir = PyString_FromString(home_dir);
366         talloc_free(frame);
367         return py_home_dir;
368 }
369
370 static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
371 {
372         TALLOC_CTX *frame = talloc_stackframe();
373         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
374
375         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
376         if (!pdb_set_homedir(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
377                 talloc_free(frame);
378                 return -1;
379         }
380         talloc_free(frame);
381         return 0;
382 }
383
384 static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
385 {
386         TALLOC_CTX *frame = talloc_stackframe();
387         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
388         PyObject *py_dir_drive;
389         const char *dir_drive;
390
391         dir_drive = pdb_get_dir_drive(sam_acct);
392         if (dir_drive == NULL) {
393                 Py_RETURN_NONE;
394         }
395
396         py_dir_drive = PyString_FromString(dir_drive);
397         talloc_free(frame);
398         return py_dir_drive;
399 }
400
401 static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
402 {
403         TALLOC_CTX *frame = talloc_stackframe();
404         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
405
406         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
407         if (!pdb_set_dir_drive(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
408                 talloc_free(frame);
409                 return -1;
410         }
411         talloc_free(frame);
412         return 0;
413 }
414
415 static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
416 {
417         TALLOC_CTX *frame = talloc_stackframe();
418         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
419         PyObject *py_logon_script;
420         const char *logon_script;
421
422         logon_script = pdb_get_logon_script(sam_acct);
423         if (logon_script == NULL) {
424                 Py_RETURN_NONE;
425         }
426
427         py_logon_script = PyString_FromString(logon_script);
428         talloc_free(frame);
429         return py_logon_script;
430 }
431
432 static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closure)
433 {
434         TALLOC_CTX *frame = talloc_stackframe();
435         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
436
437         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
438         if (!pdb_set_logon_script(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
439                 talloc_free(frame);
440                 return -1;
441         }
442         talloc_free(frame);
443         return 0;
444 }
445
446 static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
447 {
448         TALLOC_CTX *frame = talloc_stackframe();
449         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
450         PyObject *py_profile_path;
451         const char *profile_path;
452
453         profile_path = pdb_get_profile_path(sam_acct);
454         if (profile_path == NULL) {
455                 Py_RETURN_NONE;
456         }
457
458         py_profile_path = PyString_FromString(profile_path);
459         talloc_free(frame);
460         return py_profile_path;
461 }
462
463 static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closure)
464 {
465         TALLOC_CTX *frame = talloc_stackframe();
466         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
467
468         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
469         if (!pdb_set_profile_path(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
470                 talloc_free(frame);
471                 return -1;
472         }
473         talloc_free(frame);
474         return 0;
475 }
476
477 static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
478 {
479         TALLOC_CTX *frame = talloc_stackframe();
480         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
481         PyObject *py_acct_desc;
482         const char *acct_desc;
483
484         acct_desc = pdb_get_acct_desc(sam_acct);
485         if (acct_desc == NULL) {
486                 Py_RETURN_NONE;
487         }
488
489         py_acct_desc = PyString_FromString(acct_desc);
490         talloc_free(frame);
491         return py_acct_desc;
492 }
493
494 static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
495 {
496         TALLOC_CTX *frame = talloc_stackframe();
497         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
498
499         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
500         if (!pdb_set_acct_desc(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
501                 talloc_free(frame);
502                 return -1;
503         }
504         talloc_free(frame);
505         return 0;
506 }
507
508 static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
509 {
510         TALLOC_CTX *frame = talloc_stackframe();
511         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
512         PyObject *py_workstations;
513         const char *workstations;
514
515         workstations = pdb_get_workstations(sam_acct);
516         if (workstations == NULL) {
517                 Py_RETURN_NONE;
518         }
519
520         py_workstations = PyString_FromString(workstations);
521         talloc_free(frame);
522         return py_workstations;
523 }
524
525 static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closure)
526 {
527         TALLOC_CTX *frame = talloc_stackframe();
528         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
529
530         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
531         if (!pdb_set_workstations(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
532                 talloc_free(frame);
533                 return -1;
534         }
535         talloc_free(frame);
536         return 0;
537 }
538
539 static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
540 {
541         TALLOC_CTX *frame = talloc_stackframe();
542         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
543         PyObject *py_comment;
544         const char *comment;
545
546         comment = pdb_get_comment(sam_acct);
547         if (comment == NULL) {
548                 Py_RETURN_NONE;
549         }
550
551         py_comment = PyString_FromString(comment);
552         talloc_free(frame);
553         return py_comment;
554 }
555
556 static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
557 {
558         TALLOC_CTX *frame = talloc_stackframe();
559         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
560
561         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
562         if (!pdb_set_comment(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
563                 talloc_free(frame);
564                 return -1;
565         }
566         talloc_free(frame);
567         return 0;
568 }
569
570 static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
571 {
572         TALLOC_CTX *frame = talloc_stackframe();
573         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
574         PyObject *py_munged_dial;
575         const char *munged_dial;
576
577         munged_dial = pdb_get_munged_dial(sam_acct);
578         if (munged_dial == NULL) {
579                 Py_RETURN_NONE;
580         }
581
582         py_munged_dial = PyString_FromString(munged_dial);
583         talloc_free(frame);
584         return py_munged_dial;
585 }
586
587 static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure)
588 {
589         TALLOC_CTX *frame = talloc_stackframe();
590         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
591
592         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
593         if (!pdb_set_munged_dial(sam_acct, PyString_AsString(value), PDB_CHANGED)) {
594                 talloc_free(frame);
595                 return -1;
596         }
597         talloc_free(frame);
598         return 0;
599 }
600
601 static PyObject *py_samu_get_user_sid(PyObject *obj, void *closure)
602 {
603         TALLOC_CTX *frame = talloc_stackframe();
604         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
605         PyObject *py_user_sid;
606         const struct dom_sid *user_sid;
607         struct dom_sid *copy_user_sid;
608         TALLOC_CTX *mem_ctx;
609
610         user_sid = pdb_get_user_sid(sam_acct);
611         if(user_sid == NULL) {
612                 Py_RETURN_NONE;
613         }
614
615         mem_ctx = talloc_new(NULL);
616         if (mem_ctx == NULL) {
617                 PyErr_NoMemory();
618                 talloc_free(frame);
619                 return NULL;
620         }
621         copy_user_sid = dom_sid_dup(mem_ctx, user_sid);
622         if (copy_user_sid == NULL) {
623                 PyErr_NoMemory();
624                 talloc_free(mem_ctx);
625                 talloc_free(frame);
626                 return NULL;
627         }
628
629         py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
630
631         talloc_free(mem_ctx);
632
633         talloc_free(frame);
634         return py_user_sid;
635 }
636
637 static int py_samu_set_user_sid(PyObject *obj, PyObject *value, void *closure)
638 {
639         TALLOC_CTX *frame = talloc_stackframe();
640         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
641
642         PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
643         if (!pdb_set_user_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
644                 talloc_free(frame);
645                 return -1;
646         }
647         talloc_free(frame);
648         return 0;
649 }
650
651 static PyObject *py_samu_get_group_sid(PyObject *obj, void *closure)
652 {
653         TALLOC_CTX *frame = talloc_stackframe();
654         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
655         const struct dom_sid *group_sid;
656         struct dom_sid *copy_group_sid;
657
658         group_sid = pdb_get_group_sid(sam_acct);
659         if (group_sid == NULL) {
660                 Py_RETURN_NONE;
661         }
662
663         copy_group_sid = dom_sid_dup(NULL, group_sid);
664         if (copy_group_sid == NULL) {
665                 PyErr_NoMemory();
666                 talloc_free(frame);
667                 return NULL;
668         }
669
670         talloc_free(frame);
671         return pytalloc_steal(dom_sid_Type, copy_group_sid);
672 }
673
674 static int py_samu_set_group_sid(PyObject *obj, PyObject *value, void *closure)
675 {
676         TALLOC_CTX *frame = talloc_stackframe();
677         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
678
679         PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
680         if (!pdb_set_group_sid(sam_acct, (struct dom_sid *)pytalloc_get_ptr(value), PDB_CHANGED)) {
681                 talloc_free(frame);
682                 return -1;
683         }
684         talloc_free(frame);
685         return 0;
686 }
687
688 static PyObject *py_samu_get_lanman_passwd(PyObject *obj, void *closure)
689 {
690         TALLOC_CTX *frame = talloc_stackframe();
691         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
692         PyObject *py_lm_pw;
693         const char *lm_pw;
694
695         lm_pw = (const char *)pdb_get_lanman_passwd(sam_acct);
696         if (lm_pw == NULL) {
697                 Py_RETURN_NONE;
698         }
699
700         py_lm_pw = PyString_FromStringAndSize(lm_pw, LM_HASH_LEN);
701         talloc_free(frame);
702         return py_lm_pw;
703 }
704
705 static int py_samu_set_lanman_passwd(PyObject *obj, PyObject *value, void *closure)
706 {
707         TALLOC_CTX *frame = talloc_stackframe();
708         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
709
710         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
711         if (!pdb_set_lanman_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
712                 talloc_free(frame);
713                 return -1;
714         }
715         talloc_free(frame);
716         return 0;
717 }
718
719 static PyObject *py_samu_get_nt_passwd(PyObject *obj, void *closure)
720 {
721         TALLOC_CTX *frame = talloc_stackframe();
722         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
723         PyObject *py_nt_pw;
724         const char *nt_pw;
725
726         nt_pw = (const char *)pdb_get_nt_passwd(sam_acct);
727         if (nt_pw == NULL) {
728                 Py_RETURN_NONE;
729         }
730
731         py_nt_pw = PyString_FromStringAndSize(nt_pw, NT_HASH_LEN);
732         talloc_free(frame);
733         return py_nt_pw;
734 }
735
736 static int py_samu_set_nt_passwd(PyObject *obj, PyObject *value, void *closure)
737 {
738         TALLOC_CTX *frame = talloc_stackframe();
739         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
740
741         if (!pdb_set_nt_passwd(sam_acct, (uint8_t *)PyString_AsString(value), PDB_CHANGED)) {
742                 talloc_free(frame);
743                 return -1;
744         }
745         talloc_free(frame);
746         return 0;
747 }
748
749 static PyObject *py_samu_get_pw_history(PyObject *obj, void *closure)
750 {
751         TALLOC_CTX *frame = talloc_stackframe();
752         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
753         PyObject *py_nt_pw_his;
754         const char *nt_pw_his;
755         uint32_t hist_len;
756
757         nt_pw_his = (const char *)pdb_get_pw_history(sam_acct, &hist_len);
758         if (nt_pw_his == NULL) {
759                 Py_RETURN_NONE;
760         }
761
762         py_nt_pw_his = PyString_FromStringAndSize(nt_pw_his, hist_len*PW_HISTORY_ENTRY_LEN);
763         talloc_free(frame);
764         return py_nt_pw_his;
765 }
766
767 static int py_samu_set_pw_history(PyObject *obj, PyObject *value, void *closure)
768 {
769         TALLOC_CTX *frame = talloc_stackframe();
770         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
771         char *nt_pw_his;
772         Py_ssize_t len;
773         uint32_t hist_len;
774
775         PyString_AsStringAndSize(value, &nt_pw_his, &len);
776         hist_len = len / PW_HISTORY_ENTRY_LEN;
777         if (!pdb_set_pw_history(sam_acct, (uint8_t *)nt_pw_his, hist_len, PDB_CHANGED)) {
778                 talloc_free(frame);
779                 return -1;
780         }
781         talloc_free(frame);
782         return 0;
783 }
784
785 static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
786 {
787         TALLOC_CTX *frame = talloc_stackframe();
788         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
789         PyObject *py_plaintext_pw;
790         const char *plaintext_pw;
791
792         plaintext_pw = pdb_get_plaintext_passwd(sam_acct);
793         if (plaintext_pw == NULL) {
794                 Py_RETURN_NONE;
795         }
796
797         py_plaintext_pw = PyString_FromString(plaintext_pw);
798         talloc_free(frame);
799         return py_plaintext_pw;
800 }
801
802 static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *closure)
803 {
804         TALLOC_CTX *frame = talloc_stackframe();
805         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
806
807         if (!pdb_set_plaintext_passwd(sam_acct, PyString_AsString(value))) {
808                 talloc_free(frame);
809                 return -1;
810         }
811         talloc_free(frame);
812         return 0;
813 }
814
815 static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
816 {
817         TALLOC_CTX *frame = talloc_stackframe();
818         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
819         PyObject *py_acct_ctrl;
820
821         py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
822         talloc_free(frame);
823         return py_acct_ctrl;
824 }
825
826 static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
827 {
828         TALLOC_CTX *frame = talloc_stackframe();
829         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
830
831         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
832         if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
833                 talloc_free(frame);
834                 return -1;
835         }
836         talloc_free(frame);
837         return 0;
838 }
839
840 static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
841 {
842         TALLOC_CTX *frame = talloc_stackframe();
843         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
844         PyObject *py_logon_divs;
845
846         py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
847         talloc_free(frame);
848         return py_logon_divs;
849 }
850
851 static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
852 {
853         TALLOC_CTX *frame = talloc_stackframe();
854         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
855
856         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
857         if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
858                 talloc_free(frame);
859                 return -1;
860         }
861         talloc_free(frame);
862         return 0;
863 }
864
865 static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
866 {
867         TALLOC_CTX *frame = talloc_stackframe();
868         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
869         PyObject *py_hours_len;
870
871         py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
872         talloc_free(frame);
873         return py_hours_len;
874 }
875
876 static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
877 {
878         TALLOC_CTX *frame = talloc_stackframe();
879         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
880
881         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
882         if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
883                 talloc_free(frame);
884                 return -1;
885         }
886         talloc_free(frame);
887         return 0;
888 }
889
890 static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
891 {
892         TALLOC_CTX *frame = talloc_stackframe();
893         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
894         PyObject *py_hours;
895         const char *hours;
896         int hours_len, i;
897
898         hours = (const char *)pdb_get_hours(sam_acct);
899         if(! hours) {
900                 Py_RETURN_NONE;
901         }
902
903         hours_len = pdb_get_hours_len(sam_acct);
904         if ((py_hours = PyList_New(hours_len)) == NULL) {
905                 PyErr_NoMemory();
906                 talloc_free(frame);
907                 return NULL;
908         }
909
910         for (i=0; i<hours_len; i++) {
911                 PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
912         }
913         talloc_free(frame);
914         return py_hours;
915 }
916
917 static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
918 {
919         TALLOC_CTX *frame = talloc_stackframe();
920         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
921         int i;
922         uint8_t *hours;
923         int hours_len;
924         bool status;
925
926         PY_CHECK_TYPE(&PyList_Type, value, return -1;);
927
928         hours_len = PyList_GET_SIZE(value);
929
930         hours = talloc_array(pytalloc_get_mem_ctx(obj), uint8_t, hours_len);
931         if (!hours) {
932                 PyErr_NoMemory();
933                 talloc_free(frame);
934                 return -1;
935         }
936
937         for (i=0; i < hours_len; i++) {
938                 PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
939                 hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
940         }
941
942         status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
943         talloc_free(hours);
944
945         if(! status) {
946                 talloc_free(frame);
947                 return -1;
948         }
949         talloc_free(frame);
950         return 0;
951 }
952
953 static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
954 {
955         TALLOC_CTX *frame = talloc_stackframe();
956         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
957         PyObject *py_bad_password_count;
958
959         py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
960         talloc_free(frame);
961         return py_bad_password_count;
962 }
963
964 static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *closure)
965 {
966         TALLOC_CTX *frame = talloc_stackframe();
967         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
968
969         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
970         if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
971                 talloc_free(frame);
972                 return -1;
973         }
974         talloc_free(frame);
975         return 0;
976 }
977
978 static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
979 {
980         TALLOC_CTX *frame = talloc_stackframe();
981         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
982         PyObject *py_logon_count;
983
984         py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
985         talloc_free(frame);
986         return py_logon_count;
987 }
988
989 static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure)
990 {
991         TALLOC_CTX *frame = talloc_stackframe();
992         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
993
994         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
995         if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
996                 talloc_free(frame);
997                 return -1;
998         }
999         talloc_free(frame);
1000         return 0;
1001 }
1002
1003 static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
1004 {
1005         TALLOC_CTX *frame = talloc_stackframe();
1006         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1007         PyObject *py_country_code;
1008
1009         py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
1010         talloc_free(frame);
1011         return py_country_code;
1012 }
1013
1014 static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closure)
1015 {
1016         TALLOC_CTX *frame = talloc_stackframe();
1017         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1018
1019         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1020         if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1021                 talloc_free(frame);
1022                 return -1;
1023         }
1024         talloc_free(frame);
1025         return 0;
1026 }
1027
1028 static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
1029 {
1030         TALLOC_CTX *frame = talloc_stackframe();
1031         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1032         PyObject *py_code_page;
1033
1034         py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
1035         talloc_free(frame);
1036         return py_code_page;
1037 }
1038
1039 static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
1040 {
1041         TALLOC_CTX *frame = talloc_stackframe();
1042         struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
1043
1044         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1045         if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
1046                 talloc_free(frame);
1047                 return -1;
1048         }
1049         talloc_free(frame);
1050         return 0;
1051 }
1052
1053 static PyGetSetDef py_samu_getsetters[] = {
1054         { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
1055         { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
1056         { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
1057         { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
1058         { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
1059         { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
1060         { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
1061         { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
1062         { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
1063         { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
1064         { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
1065         { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
1066         { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
1067         { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
1068         { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
1069         { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
1070         { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
1071         { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
1072         { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
1073         { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
1074         { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
1075         { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
1076         { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
1077         { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
1078         { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
1079         { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
1080         { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
1081         { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
1082         { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
1083         { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
1084         { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
1085         { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
1086         { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
1087         { NULL }
1088 };
1089
1090
1091 /************************** PIDL Autogeneratd ******************************/
1092
1093 static PyObject *py_samu_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1094 {
1095         TALLOC_CTX *frame = talloc_stackframe();
1096         struct samu *sam_acct;
1097
1098         sam_acct = samu_new(NULL);
1099         if (!sam_acct) {
1100                 PyErr_NoMemory();
1101                 talloc_free(frame);
1102                 return NULL;
1103         }
1104
1105         talloc_free(frame);
1106         return pytalloc_steal(type, sam_acct);
1107 }
1108
1109 static PyTypeObject PySamu = {
1110         .tp_name = "passdb.Samu",
1111         .tp_getset = py_samu_getsetters,
1112         .tp_methods = NULL,
1113         .tp_new = py_samu_new,
1114         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1115         .tp_doc = "Samu() -> samu object\n",
1116 };
1117
1118
1119 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1120 {
1121         TALLOC_CTX *frame = talloc_stackframe();
1122         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1123         PyObject *py_gid;
1124
1125         py_gid = Py_BuildValue("i", group_map->gid);
1126         talloc_free(frame);
1127         return py_gid;
1128 }
1129
1130 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1131 {
1132         TALLOC_CTX *frame = talloc_stackframe();
1133         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1134
1135         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1136         group_map->gid = PyInt_AsLong(value);
1137         talloc_free(frame);
1138         return 0;
1139 }
1140
1141 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1142 {
1143         TALLOC_CTX *frame = talloc_stackframe();
1144         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1145         PyObject *py_sid;
1146         struct dom_sid *group_sid;
1147         TALLOC_CTX *mem_ctx;
1148
1149         mem_ctx = talloc_new(NULL);
1150         if (mem_ctx == NULL) {
1151                 PyErr_NoMemory();
1152                 talloc_free(frame);
1153                 return NULL;
1154         }
1155
1156         group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1157         if (group_sid == NULL) {
1158                 PyErr_NoMemory();
1159                 talloc_free(mem_ctx);
1160                 talloc_free(frame);
1161                 return NULL;
1162         }
1163
1164         py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1165
1166         talloc_free(mem_ctx);
1167
1168         talloc_free(frame);
1169         return py_sid;
1170 }
1171
1172 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1173 {
1174         TALLOC_CTX *frame = talloc_stackframe();
1175         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1176
1177         PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1178         group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1179         talloc_free(frame);
1180         return 0;
1181 }
1182
1183 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1184 {
1185         TALLOC_CTX *frame = talloc_stackframe();
1186         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1187         PyObject *py_sid_name_use;
1188
1189         py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1190         talloc_free(frame);
1191         return py_sid_name_use;
1192 }
1193
1194 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1195 {
1196         TALLOC_CTX *frame = talloc_stackframe();
1197         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1198
1199         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1200         group_map->sid_name_use = PyInt_AsLong(value);
1201         talloc_free(frame);
1202         return 0;
1203 }
1204
1205 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1206 {
1207         TALLOC_CTX *frame = talloc_stackframe();
1208         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1209         PyObject *py_nt_name;
1210         if (group_map->nt_name == NULL) {
1211                 py_nt_name = Py_None;
1212                 Py_INCREF(py_nt_name);
1213         } else {
1214                 py_nt_name = PyString_FromString(group_map->nt_name);
1215         }
1216         talloc_free(frame);
1217         return py_nt_name;
1218 }
1219
1220 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1221 {
1222         TALLOC_CTX *frame = talloc_stackframe();
1223         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1224
1225         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1226         if (value == Py_None) {
1227                 fstrcpy(group_map->nt_name, NULL);
1228         } else {
1229                 fstrcpy(group_map->nt_name, PyString_AsString(value));
1230         }
1231         talloc_free(frame);
1232         return 0;
1233 }
1234
1235 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1236 {
1237         TALLOC_CTX *frame = talloc_stackframe();
1238         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1239         PyObject *py_comment;
1240         if (group_map->comment == NULL) {
1241                 py_comment = Py_None;
1242                 Py_INCREF(py_comment);
1243         } else {
1244                 py_comment = PyString_FromString(group_map->comment);
1245         }
1246         talloc_free(frame);
1247         return py_comment;
1248 }
1249
1250 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1251 {
1252         TALLOC_CTX *frame = talloc_stackframe();
1253         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1254
1255         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1256         if (value == Py_None) {
1257                 fstrcpy(group_map->comment, NULL);
1258         } else {
1259                 fstrcpy(group_map->comment, PyString_AsString(value));
1260         }
1261         talloc_free(frame);
1262         return 0;
1263 }
1264
1265 static PyGetSetDef py_groupmap_getsetters[] = {
1266         { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1267         { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1268         { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1269         { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1270         { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1271         { NULL }
1272 };
1273
1274 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1275 {
1276         TALLOC_CTX *frame = talloc_stackframe();
1277         GROUP_MAP *group_map;
1278         TALLOC_CTX *mem_ctx;
1279         PyObject *py_group_map;
1280
1281         mem_ctx = talloc_new(NULL);
1282         if (mem_ctx == NULL) {
1283                 PyErr_NoMemory();
1284                 talloc_free(frame);
1285                 return NULL;
1286         }
1287
1288         group_map = talloc_zero(mem_ctx, GROUP_MAP);
1289         if (group_map == NULL) {
1290                 PyErr_NoMemory();
1291                 talloc_free(mem_ctx);
1292                 talloc_free(frame);
1293                 return NULL;
1294         }
1295
1296         py_group_map = pytalloc_steal(type, group_map);
1297         if (py_group_map == NULL) {
1298                 PyErr_NoMemory();
1299                 talloc_free(mem_ctx);
1300                 talloc_free(frame);
1301                 return NULL;
1302         }
1303
1304         talloc_free(mem_ctx);
1305
1306         talloc_free(frame);
1307         return py_group_map;
1308 }
1309
1310
1311 static PyTypeObject PyGroupmap = {
1312         .tp_name = "passdb.Groupmap",
1313         .tp_getset = py_groupmap_getsetters,
1314         .tp_methods = NULL,
1315         .tp_new = py_groupmap_new,
1316         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1317         .tp_doc = "Groupmap() -> group map object\n",
1318 };
1319
1320
1321 static PyObject *py_pdb_domain_info(PyObject *self, PyObject *args)
1322 {
1323         TALLOC_CTX *frame = talloc_stackframe();
1324         struct pdb_methods *methods;
1325         struct pdb_domain_info *domain_info;
1326         PyObject *py_domain_info;
1327         struct dom_sid *sid;
1328         struct GUID *guid;
1329
1330         methods = pytalloc_get_ptr(self);
1331
1332         domain_info = methods->get_domain_info(methods, frame);
1333         if (! domain_info) {
1334                 Py_RETURN_NONE;
1335         }
1336
1337         sid = dom_sid_dup(frame, &domain_info->sid);
1338         if (sid == NULL) {
1339                 PyErr_NoMemory();
1340                 talloc_free(frame);
1341                 return NULL;
1342         }
1343
1344         guid = talloc(frame, struct GUID);
1345         if (guid == NULL) {
1346                 PyErr_NoMemory();
1347                 talloc_free(frame);
1348                 return NULL;
1349         }
1350         *guid = domain_info->guid;
1351
1352         if ((py_domain_info = PyDict_New()) == NULL) {
1353                 PyErr_NoMemory();
1354                 talloc_free(frame);
1355                 return NULL;
1356         }
1357
1358         PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1359         PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->dns_domain));
1360         PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->dns_forest));
1361         PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1362         PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1363
1364         talloc_free(frame);
1365         return py_domain_info;
1366 }
1367
1368
1369 static PyObject *py_pdb_getsampwnam(PyObject *self, PyObject *args)
1370 {
1371         TALLOC_CTX *frame = talloc_stackframe();
1372         NTSTATUS status;
1373         const char *username;
1374         struct pdb_methods *methods;
1375         struct samu *sam_acct;
1376         PyObject *py_sam_acct;
1377
1378         if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1379                 talloc_free(frame);
1380                 return NULL;
1381         }
1382
1383         methods = pytalloc_get_ptr(self);
1384
1385         py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1386         if (py_sam_acct == NULL) {
1387                 PyErr_NoMemory();
1388                 talloc_free(frame);
1389                 return NULL;
1390         }
1391         sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1392
1393         status = methods->getsampwnam(methods, sam_acct, username);
1394         if (!NT_STATUS_IS_OK(status)) {
1395                 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1396                                 username,
1397                                 NT_STATUS_V(status),
1398                                 get_friendly_nt_error_msg(status));
1399                 Py_DECREF(py_sam_acct);
1400                 talloc_free(frame);
1401                 return NULL;
1402         }
1403
1404         talloc_free(frame);
1405         return py_sam_acct;
1406 }
1407
1408 static PyObject *py_pdb_getsampwsid(PyObject *self, PyObject *args)
1409 {
1410         TALLOC_CTX *frame = talloc_stackframe();
1411         NTSTATUS status;
1412         struct pdb_methods *methods;
1413         struct samu *sam_acct;
1414         PyObject *py_sam_acct;
1415         PyObject *py_user_sid;
1416
1417         if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1418                 talloc_free(frame);
1419                 return NULL;
1420         }
1421
1422         methods = pytalloc_get_ptr(self);
1423
1424         py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1425         if (py_sam_acct == NULL) {
1426                 PyErr_NoMemory();
1427                 talloc_free(frame);
1428                 return NULL;
1429         }
1430         sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1431
1432         status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1433         if (!NT_STATUS_IS_OK(status)) {
1434                 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1435                                 NT_STATUS_V(status),
1436                                 get_friendly_nt_error_msg(status));
1437                 Py_DECREF(py_sam_acct);
1438                 talloc_free(frame);
1439                 return NULL;
1440         }
1441
1442         talloc_free(frame);
1443         return py_sam_acct;
1444 }
1445
1446 static PyObject *py_pdb_create_user(PyObject *self, PyObject *args)
1447 {
1448         TALLOC_CTX *frame = talloc_stackframe();
1449         NTSTATUS status;
1450         struct pdb_methods *methods;
1451         const char *username;
1452         unsigned int acct_flags;
1453         unsigned int rid;
1454
1455         if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1456                 talloc_free(frame);
1457                 return NULL;
1458         }
1459
1460         methods = pytalloc_get_ptr(self);
1461
1462         status = methods->create_user(methods, frame, username, acct_flags, &rid);
1463         if (!NT_STATUS_IS_OK(status)) {
1464                 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1465                                 username,
1466                                 NT_STATUS_V(status),
1467                                 get_friendly_nt_error_msg(status));
1468                 talloc_free(frame);
1469                 return NULL;
1470         }
1471
1472         talloc_free(frame);
1473         return PyInt_FromLong(rid);
1474 }
1475
1476 static PyObject *py_pdb_delete_user(PyObject *self, PyObject *args)
1477 {
1478         TALLOC_CTX *frame = talloc_stackframe();
1479         NTSTATUS status;
1480         struct pdb_methods *methods;
1481         struct samu *sam_acct;
1482         PyObject *py_sam_acct;
1483
1484         if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1485                 talloc_free(frame);
1486                 return NULL;
1487         }
1488
1489         methods = pytalloc_get_ptr(self);
1490
1491         sam_acct = pytalloc_get_ptr(py_sam_acct);
1492
1493         status = methods->delete_user(methods, frame, sam_acct);
1494         if (!NT_STATUS_IS_OK(status)) {
1495                 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1496                                 NT_STATUS_V(status),
1497                                 get_friendly_nt_error_msg(status));
1498                 talloc_free(frame);
1499                 return NULL;
1500         }
1501
1502         talloc_free(frame);
1503         Py_RETURN_NONE;
1504 }
1505
1506 static PyObject *py_pdb_add_sam_account(PyObject *self, PyObject *args)
1507 {
1508         TALLOC_CTX *frame = talloc_stackframe();
1509         NTSTATUS status;
1510         struct pdb_methods *methods;
1511         struct samu *sam_acct;
1512         PyObject *py_sam_acct;
1513
1514         if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1515                 talloc_free(frame);
1516                 return NULL;
1517         }
1518
1519         methods = pytalloc_get_ptr(self);
1520
1521         sam_acct = pytalloc_get_ptr(py_sam_acct);
1522
1523         status = methods->add_sam_account(methods, sam_acct);
1524         if (!NT_STATUS_IS_OK(status)) {
1525                 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1526                                 sam_acct->username,
1527                                 NT_STATUS_V(status),
1528                                 get_friendly_nt_error_msg(status));
1529                 talloc_free(frame);
1530                 return NULL;
1531         }
1532
1533         talloc_free(frame);
1534         Py_RETURN_NONE;
1535 }
1536
1537 static PyObject *py_pdb_update_sam_account(PyObject *self, PyObject *args)
1538 {
1539         TALLOC_CTX *frame = talloc_stackframe();
1540         NTSTATUS status;
1541         struct pdb_methods *methods;
1542         struct samu *sam_acct;
1543         PyObject *py_sam_acct;
1544
1545         if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1546                 talloc_free(frame);
1547                 return NULL;
1548         }
1549
1550         methods = pytalloc_get_ptr(self);
1551
1552         sam_acct = pytalloc_get_ptr(py_sam_acct);
1553
1554         status = methods->update_sam_account(methods, sam_acct);
1555         if (!NT_STATUS_IS_OK(status)) {
1556                 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1557                                 NT_STATUS_V(status),
1558                                 get_friendly_nt_error_msg(status));
1559                 talloc_free(frame);
1560                 return NULL;
1561         }
1562
1563         talloc_free(frame);
1564         Py_RETURN_NONE;
1565 }
1566
1567 static PyObject *py_pdb_delete_sam_account(PyObject *self, PyObject *args)
1568 {
1569         TALLOC_CTX *frame = talloc_stackframe();
1570         NTSTATUS status;
1571         struct pdb_methods *methods;
1572         struct samu *sam_acct;
1573         PyObject *py_sam_acct;
1574
1575         if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1576                 talloc_free(frame);
1577                 return NULL;
1578         }
1579
1580         methods = pytalloc_get_ptr(self);
1581
1582         sam_acct = pytalloc_get_ptr(py_sam_acct);
1583
1584         status = methods->delete_sam_account(methods, sam_acct);
1585         if (!NT_STATUS_IS_OK(status)) {
1586                 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1587                                 NT_STATUS_V(status),
1588                                 get_friendly_nt_error_msg(status));
1589                 talloc_free(frame);
1590                 return NULL;
1591         }
1592
1593         talloc_free(frame);
1594         Py_RETURN_NONE;
1595 }
1596
1597 static PyObject *py_pdb_rename_sam_account(PyObject *self, PyObject *args)
1598 {
1599         TALLOC_CTX *frame = talloc_stackframe();
1600         NTSTATUS status;
1601         struct pdb_methods *methods;
1602         struct samu *sam_acct;
1603         const char *new_username;
1604         PyObject *py_sam_acct;
1605
1606         if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1607                                         &new_username)) {
1608                 talloc_free(frame);
1609                 return NULL;
1610         }
1611
1612         methods = pytalloc_get_ptr(self);
1613
1614         sam_acct = pytalloc_get_ptr(py_sam_acct);
1615
1616         status = methods->rename_sam_account(methods, sam_acct, new_username);
1617         if (!NT_STATUS_IS_OK(status)) {
1618                 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1619                                 NT_STATUS_V(status),
1620                                 get_friendly_nt_error_msg(status));
1621                 talloc_free(frame);
1622                 return NULL;
1623         }
1624
1625         talloc_free(frame);
1626         Py_RETURN_NONE;
1627 }
1628
1629
1630 static PyObject *py_pdb_getgrsid(PyObject *self, PyObject *args)
1631 {
1632         TALLOC_CTX *frame = talloc_stackframe();
1633         NTSTATUS status;
1634         struct pdb_methods *methods;
1635         GROUP_MAP *group_map;
1636         struct dom_sid *domain_sid;
1637         PyObject *py_domain_sid, *py_group_map;
1638
1639         if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1640                 talloc_free(frame);
1641                 return NULL;
1642         }
1643
1644         methods = pytalloc_get_ptr(self);
1645
1646         domain_sid = pytalloc_get_ptr(py_domain_sid);
1647
1648         py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1649         if (py_group_map == NULL) {
1650                 PyErr_NoMemory();
1651                 talloc_free(frame);
1652                 return NULL;
1653         }
1654
1655         group_map = pytalloc_get_ptr(py_group_map);
1656
1657         status = methods->getgrsid(methods, group_map, *domain_sid);
1658         if (!NT_STATUS_IS_OK(status)) {
1659                 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1660                                 NT_STATUS_V(status),
1661                                 get_friendly_nt_error_msg(status));
1662                 talloc_free(frame);
1663                 return NULL;
1664         }
1665
1666         talloc_free(frame);
1667         return py_group_map;
1668 }
1669
1670
1671 static PyObject *py_pdb_getgrgid(PyObject *self, PyObject *args)
1672 {
1673         TALLOC_CTX *frame = talloc_stackframe();
1674         NTSTATUS status;
1675         struct pdb_methods *methods;
1676         GROUP_MAP *group_map;
1677         PyObject *py_group_map;
1678         unsigned int gid_value;
1679
1680         if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1681                 talloc_free(frame);
1682                 return NULL;
1683         }
1684
1685         methods = pytalloc_get_ptr(self);
1686
1687         py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1688         if (py_group_map == NULL) {
1689                 PyErr_NoMemory();
1690                 talloc_free(frame);
1691                 return NULL;
1692         }
1693
1694         group_map = pytalloc_get_ptr(py_group_map);
1695
1696         status = methods->getgrgid(methods, group_map, gid_value);
1697         if (!NT_STATUS_IS_OK(status)) {
1698                 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1699                                 NT_STATUS_V(status),
1700                                 get_friendly_nt_error_msg(status));
1701                 talloc_free(frame);
1702                 return NULL;
1703         }
1704
1705         talloc_free(frame);
1706         return py_group_map;
1707 }
1708
1709
1710 static PyObject *py_pdb_getgrnam(PyObject *self, PyObject *args)
1711 {
1712         TALLOC_CTX *frame = talloc_stackframe();
1713         NTSTATUS status;
1714         struct pdb_methods *methods;
1715         GROUP_MAP *group_map;
1716         PyObject *py_group_map;
1717         const char *groupname;
1718
1719         if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1720                 talloc_free(frame);
1721                 return NULL;
1722         }
1723
1724         methods = pytalloc_get_ptr(self);
1725
1726         py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1727         if (py_group_map == NULL) {
1728                 PyErr_NoMemory();
1729                 talloc_free(frame);
1730                 return NULL;
1731         }
1732
1733         group_map = pytalloc_get_ptr(py_group_map);
1734
1735         status = methods->getgrnam(methods, group_map, groupname);
1736         if (!NT_STATUS_IS_OK(status)) {
1737                 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1738                                 NT_STATUS_V(status),
1739                                 get_friendly_nt_error_msg(status));
1740                 talloc_free(frame);
1741                 return NULL;
1742         }
1743
1744         talloc_free(frame);
1745         return py_group_map;
1746 }
1747
1748
1749 static PyObject *py_pdb_create_dom_group(PyObject *self, PyObject *args)
1750 {
1751         TALLOC_CTX *frame = talloc_stackframe();
1752         NTSTATUS status;
1753         struct pdb_methods *methods;
1754         const char *groupname;
1755         uint32_t group_rid;
1756
1757         if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1758                 talloc_free(frame);
1759                 return NULL;
1760         }
1761
1762         methods = pytalloc_get_ptr(self);
1763
1764         status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1765         if (!NT_STATUS_IS_OK(status)) {
1766                 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1767                                 groupname,
1768                                 NT_STATUS_V(status),
1769                                 get_friendly_nt_error_msg(status));
1770                 talloc_free(frame);
1771                 return NULL;
1772         }
1773
1774         talloc_free(frame);
1775         return PyInt_FromLong(group_rid);
1776 }
1777
1778
1779 static PyObject *py_pdb_delete_dom_group(PyObject *self, PyObject *args)
1780 {
1781         TALLOC_CTX *frame = talloc_stackframe();
1782         NTSTATUS status;
1783         struct pdb_methods *methods;
1784         unsigned int group_rid;
1785
1786         if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1787                 talloc_free(frame);
1788                 return NULL;
1789         }
1790
1791         methods = pytalloc_get_ptr(self);
1792
1793         status = methods->delete_dom_group(methods, frame, group_rid);
1794         if (!NT_STATUS_IS_OK(status)) {
1795                 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1796                                 group_rid,
1797                                 NT_STATUS_V(status),
1798                                 get_friendly_nt_error_msg(status));
1799                 talloc_free(frame);
1800                 return NULL;
1801         }
1802
1803         talloc_free(frame);
1804         Py_RETURN_NONE;
1805 }
1806
1807
1808 static PyObject *py_pdb_add_group_mapping_entry(PyObject *self, PyObject *args)
1809 {
1810         TALLOC_CTX *frame = talloc_stackframe();
1811         NTSTATUS status;
1812         struct pdb_methods *methods;
1813         PyObject *py_group_map;
1814         GROUP_MAP *group_map;
1815
1816         if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1817                 talloc_free(frame);
1818                 return NULL;
1819         }
1820
1821         methods = pytalloc_get_ptr(self);
1822
1823         group_map = pytalloc_get_ptr(py_group_map);
1824
1825         status = methods->add_group_mapping_entry(methods, group_map);
1826         if (!NT_STATUS_IS_OK(status)) {
1827                 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1828                                 NT_STATUS_V(status),
1829                                 get_friendly_nt_error_msg(status));
1830                 talloc_free(frame);
1831                 return NULL;
1832         }
1833
1834         talloc_free(frame);
1835         Py_RETURN_NONE;
1836 }
1837
1838
1839 static PyObject *py_pdb_update_group_mapping_entry(PyObject *self, PyObject *args)
1840 {
1841         TALLOC_CTX *frame = talloc_stackframe();
1842         NTSTATUS status;
1843         struct pdb_methods *methods;
1844         PyObject *py_group_map;
1845         GROUP_MAP *group_map;
1846
1847         if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1848                 talloc_free(frame);
1849                 return NULL;
1850         }
1851
1852         methods = pytalloc_get_ptr(self);
1853
1854         group_map = pytalloc_get_ptr(py_group_map);
1855
1856         status = methods->update_group_mapping_entry(methods, group_map);
1857         if (!NT_STATUS_IS_OK(status)) {
1858                 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1859                                 NT_STATUS_V(status),
1860                                 get_friendly_nt_error_msg(status));
1861                 talloc_free(frame);
1862                 return NULL;
1863         }
1864
1865         talloc_free(frame);
1866         Py_RETURN_NONE;
1867 }
1868
1869
1870 static PyObject *py_pdb_delete_group_mapping_entry(PyObject *self, PyObject *args)
1871 {
1872         TALLOC_CTX *frame = talloc_stackframe();
1873         NTSTATUS status;
1874         struct pdb_methods *methods;
1875         PyObject *py_group_sid;
1876         struct dom_sid *group_sid;
1877
1878         if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1879                 talloc_free(frame);
1880                 return NULL;
1881         }
1882
1883         methods = pytalloc_get_ptr(self);
1884
1885         group_sid = pytalloc_get_ptr(py_group_sid);
1886
1887         status = methods->delete_group_mapping_entry(methods, *group_sid);
1888         if (!NT_STATUS_IS_OK(status)) {
1889                 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1890                                 NT_STATUS_V(status),
1891                                 get_friendly_nt_error_msg(status));
1892                 talloc_free(frame);
1893                 return NULL;
1894         }
1895
1896         talloc_free(frame);
1897         Py_RETURN_NONE;
1898 }
1899
1900
1901 static PyObject *py_pdb_enum_group_mapping(PyObject *self, PyObject *args)
1902 {
1903         TALLOC_CTX *frame = talloc_stackframe();
1904         NTSTATUS status;
1905         struct pdb_methods *methods;
1906         enum lsa_SidType sid_name_use;
1907         int lsa_sidtype_value = SID_NAME_UNKNOWN;
1908         int unix_only = 0;
1909         PyObject *py_domain_sid;
1910         struct dom_sid *domain_sid = NULL;
1911         GROUP_MAP **gmap = NULL;
1912         GROUP_MAP *group_map;
1913         size_t num_entries;
1914         PyObject *py_gmap_list, *py_group_map;
1915         int i;
1916
1917         py_domain_sid = Py_None;
1918         Py_INCREF(Py_None);
1919
1920         if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1921                                         &lsa_sidtype_value, &unix_only)) {
1922                 talloc_free(frame);
1923                 return NULL;
1924         }
1925
1926         methods = pytalloc_get_ptr(self);
1927
1928         sid_name_use = lsa_sidtype_value;
1929
1930         if (py_domain_sid != Py_None) {
1931                 domain_sid = pytalloc_get_ptr(py_domain_sid);
1932         }
1933
1934         status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1935                                                 &gmap, &num_entries, unix_only);
1936         if (!NT_STATUS_IS_OK(status)) {
1937                 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1938                                 NT_STATUS_V(status),
1939                                 get_friendly_nt_error_msg(status));
1940                 talloc_free(frame);
1941                 return NULL;
1942         }
1943
1944         py_gmap_list = PyList_New(0);
1945         if (py_gmap_list == NULL) {
1946                 PyErr_NoMemory();
1947                 talloc_free(frame);
1948                 return NULL;
1949         }
1950
1951         for(i=0; i<num_entries; i++) {
1952                 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1953                 if (py_group_map) {
1954                         group_map = pytalloc_get_ptr(py_group_map);
1955                         *group_map = *gmap[i];
1956                         talloc_steal(group_map, gmap[i]->nt_name);
1957                         talloc_steal(group_map, gmap[i]->comment);
1958
1959                         PyList_Append(py_gmap_list, py_group_map);
1960                 }
1961         }
1962
1963         talloc_free(gmap);
1964
1965         talloc_free(frame);
1966         return py_gmap_list;
1967 }
1968
1969
1970 static PyObject *py_pdb_enum_group_members(PyObject *self, PyObject *args)
1971 {
1972         TALLOC_CTX *frame = talloc_stackframe();
1973         NTSTATUS status;
1974         struct pdb_methods *methods;
1975         PyObject *py_group_sid;
1976         struct dom_sid *group_sid;
1977         uint32_t *member_rids;
1978         size_t num_members;
1979         PyObject *py_sid_list;
1980         struct dom_sid *domain_sid, *member_sid;
1981         int i;
1982
1983         if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1984                 talloc_free(frame);
1985                 return NULL;
1986         }
1987
1988         methods = pytalloc_get_ptr(self);
1989
1990         group_sid = pytalloc_get_ptr(py_group_sid);
1991
1992         status = methods->enum_group_members(methods, frame, group_sid,
1993                                                 &member_rids, &num_members);
1994         if (!NT_STATUS_IS_OK(status)) {
1995                 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1996                                 NT_STATUS_V(status),
1997                                 get_friendly_nt_error_msg(status));
1998                 talloc_free(frame);
1999                 return NULL;
2000         }
2001
2002         py_sid_list = PyList_New(0);
2003         if (py_sid_list == NULL) {
2004                 PyErr_NoMemory();
2005                 talloc_free(frame);
2006                 return NULL;
2007         }
2008
2009         domain_sid = get_global_sam_sid();
2010
2011         for(i=0; i<num_members; i++) {
2012                 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2013                 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
2014         }
2015
2016         talloc_free(frame);
2017         return py_sid_list;
2018 }
2019
2020
2021 static PyObject *py_pdb_enum_group_memberships(PyObject *self, PyObject *args)
2022 {
2023         TALLOC_CTX *frame = talloc_stackframe();
2024         NTSTATUS status;
2025         struct pdb_methods *methods;
2026         int i;
2027
2028         struct samu *sam_acct;
2029         PyObject *py_sam_acct;
2030         PyObject *py_sid_list;
2031         struct dom_sid *user_group_sids = NULL;
2032         gid_t *user_group_ids = NULL;
2033         uint32_t num_groups = 0;
2034
2035         if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2036                 talloc_free(frame);
2037                 return NULL;
2038         }
2039
2040         methods = pytalloc_get_ptr(self);
2041
2042         sam_acct = pytalloc_get_ptr(py_sam_acct);
2043
2044         status = methods->enum_group_memberships(methods, frame, sam_acct,
2045                                                  &user_group_sids, &user_group_ids, &num_groups);
2046         if (!NT_STATUS_IS_OK(status)) {
2047                 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2048                                 NT_STATUS_V(status),
2049                                 get_friendly_nt_error_msg(status));
2050                 talloc_free(frame);
2051                 return NULL;
2052         }
2053
2054         py_sid_list = PyList_New(0);
2055         if (py_sid_list == NULL) {
2056                 PyErr_NoMemory();
2057                 talloc_free(frame);
2058                 return NULL;
2059         }
2060
2061         for(i=0; i<num_groups; i++) {
2062                 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
2063         }
2064
2065         talloc_free(frame);
2066         return py_sid_list;
2067 }
2068
2069
2070 static PyObject *py_pdb_add_groupmem(PyObject *self, PyObject *args)
2071 {
2072         TALLOC_CTX *frame = talloc_stackframe();
2073         NTSTATUS status;
2074         struct pdb_methods *methods;
2075         uint32_t group_rid, member_rid;
2076
2077         if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2078                 talloc_free(frame);
2079                 return NULL;
2080         }
2081
2082         methods = pytalloc_get_ptr(self);
2083
2084         status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2085         if (!NT_STATUS_IS_OK(status)) {
2086                 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2087                                 NT_STATUS_V(status),
2088                                 get_friendly_nt_error_msg(status));
2089                 talloc_free(frame);
2090                 return NULL;
2091         }
2092
2093         talloc_free(frame);
2094         Py_RETURN_NONE;
2095 }
2096
2097
2098 static PyObject *py_pdb_del_groupmem(PyObject *self, PyObject *args)
2099 {
2100         TALLOC_CTX *frame = talloc_stackframe();
2101         NTSTATUS status;
2102         struct pdb_methods *methods;
2103         uint32_t group_rid, member_rid;
2104
2105         if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2106                 talloc_free(frame);
2107                 return NULL;
2108         }
2109
2110         methods = pytalloc_get_ptr(self);
2111
2112         status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2113         if (!NT_STATUS_IS_OK(status)) {
2114                 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2115                                 NT_STATUS_V(status),
2116                                 get_friendly_nt_error_msg(status));
2117                 talloc_free(frame);
2118                 return NULL;
2119         }
2120
2121         talloc_free(frame);
2122         Py_RETURN_NONE;
2123 }
2124
2125
2126 static PyObject *py_pdb_create_alias(PyObject *self, PyObject *args)
2127 {
2128         TALLOC_CTX *frame = talloc_stackframe();
2129         NTSTATUS status;
2130         struct pdb_methods *methods;
2131         const char *alias_name;
2132         uint32_t rid;
2133
2134         if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2135                 talloc_free(frame);
2136                 return NULL;
2137         }
2138
2139         methods = pytalloc_get_ptr(self);
2140
2141         status = methods->create_alias(methods, alias_name, &rid);
2142         if (!NT_STATUS_IS_OK(status)) {
2143                 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2144                                 alias_name,
2145                                 NT_STATUS_V(status),
2146                                 get_friendly_nt_error_msg(status));
2147                 talloc_free(frame);
2148                 return NULL;
2149         }
2150
2151         talloc_free(frame);
2152         return PyInt_FromLong(rid);
2153 }
2154
2155
2156 static PyObject *py_pdb_delete_alias(PyObject *self, PyObject *args)
2157 {
2158         TALLOC_CTX *frame = talloc_stackframe();
2159         NTSTATUS status;
2160         struct pdb_methods *methods;
2161         PyObject *py_alias_sid;
2162         struct dom_sid *alias_sid;
2163
2164         if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2165                 talloc_free(frame);
2166                 return NULL;
2167         }
2168
2169         methods = pytalloc_get_ptr(self);
2170
2171         alias_sid = pytalloc_get_ptr(py_alias_sid);
2172
2173         status = methods->delete_alias(methods, alias_sid);
2174         if (!NT_STATUS_IS_OK(status)) {
2175                 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2176                                 NT_STATUS_V(status),
2177                                 get_friendly_nt_error_msg(status));
2178                 talloc_free(frame);
2179                 return NULL;
2180         }
2181
2182         talloc_free(frame);
2183         Py_RETURN_NONE;
2184 }
2185
2186
2187 static PyObject *py_pdb_get_aliasinfo(PyObject *self, PyObject *args)
2188 {
2189         TALLOC_CTX *frame = talloc_stackframe();
2190         NTSTATUS status;
2191         struct pdb_methods *methods;
2192         PyObject *py_alias_sid;
2193         struct dom_sid *alias_sid;
2194         struct acct_info *alias_info;
2195         PyObject *py_alias_info;
2196
2197         if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2198                 talloc_free(frame);
2199                 return NULL;
2200         }
2201
2202         methods = pytalloc_get_ptr(self);
2203
2204         alias_sid = pytalloc_get_ptr(py_alias_sid);
2205
2206         alias_info = talloc_zero(frame, struct acct_info);
2207         if (!alias_info) {
2208                 PyErr_NoMemory();
2209                 talloc_free(frame);
2210                 return NULL;
2211         }
2212
2213         status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2214         if (!NT_STATUS_IS_OK(status)) {
2215                 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2216                                 NT_STATUS_V(status),
2217                                 get_friendly_nt_error_msg(status));
2218                 talloc_free(frame);
2219                 return NULL;
2220         }
2221
2222         py_alias_info = PyDict_New();
2223         if (py_alias_info == NULL) {
2224                 PyErr_NoMemory();
2225                 talloc_free(frame);
2226                 return NULL;
2227         }
2228
2229         PyDict_SetItemString(py_alias_info, "acct_name",
2230                              PyString_FromString(alias_info->acct_name));
2231         PyDict_SetItemString(py_alias_info, "acct_desc",
2232                              PyString_FromString(alias_info->acct_desc));
2233         PyDict_SetItemString(py_alias_info, "rid",
2234                              PyInt_FromLong(alias_info->rid));
2235
2236         talloc_free(frame);
2237         return py_alias_info;
2238 }
2239
2240
2241 static PyObject *py_pdb_set_aliasinfo(PyObject *self, PyObject *args)
2242 {
2243         TALLOC_CTX *frame = talloc_stackframe();
2244         NTSTATUS status;
2245         struct pdb_methods *methods;
2246         PyObject *py_alias_sid, *py_alias_info;
2247         struct dom_sid *alias_sid;
2248         struct acct_info alias_info;
2249
2250         if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2251                                 &py_alias_info)) {
2252                 talloc_free(frame);
2253                 return NULL;
2254         }
2255
2256         methods = pytalloc_get_ptr(self);
2257
2258         alias_sid = pytalloc_get_ptr(py_alias_sid);
2259
2260         alias_info.acct_name = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2261         if (alias_info.acct_name == NULL) {
2262                 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2263                 talloc_free(frame);
2264                 return NULL;
2265         }
2266         alias_info.acct_desc = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2267         if (alias_info.acct_desc == NULL) {
2268                 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2269                 talloc_free(frame);
2270                 return NULL;
2271         }
2272
2273         status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2274         if (!NT_STATUS_IS_OK(status)) {
2275                 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2276                                 NT_STATUS_V(status),
2277                                 get_friendly_nt_error_msg(status));
2278                 talloc_free(frame);
2279                 return NULL;
2280         }
2281
2282         talloc_free(frame);
2283         Py_RETURN_NONE;
2284 }
2285
2286
2287 static PyObject *py_pdb_add_aliasmem(PyObject *self, PyObject *args)
2288 {
2289         TALLOC_CTX *frame = talloc_stackframe();
2290         NTSTATUS status;
2291         struct pdb_methods *methods;
2292         PyObject *py_alias_sid, *py_member_sid;
2293         struct dom_sid *alias_sid, *member_sid;
2294
2295         if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2296                                         dom_sid_Type, &py_member_sid)) {
2297                 talloc_free(frame);
2298                 return NULL;
2299         }
2300
2301         methods = pytalloc_get_ptr(self);
2302
2303         alias_sid = pytalloc_get_ptr(py_alias_sid);
2304         member_sid = pytalloc_get_ptr(py_member_sid);
2305
2306         status = methods->add_aliasmem(methods, alias_sid, member_sid);
2307         if (!NT_STATUS_IS_OK(status)) {
2308                 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2309                                 NT_STATUS_V(status),
2310                                 get_friendly_nt_error_msg(status));
2311                 talloc_free(frame);
2312                 return NULL;
2313         }
2314
2315         talloc_free(frame);
2316         Py_RETURN_NONE;
2317 }
2318
2319
2320 static PyObject *py_pdb_del_aliasmem(PyObject *self, PyObject *args)
2321 {
2322         TALLOC_CTX *frame = talloc_stackframe();
2323         NTSTATUS status;
2324         struct pdb_methods *methods;
2325         PyObject *py_alias_sid, *py_member_sid;
2326         const struct dom_sid *alias_sid, *member_sid;
2327
2328         if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2329                                         dom_sid_Type, &py_member_sid)) {
2330                 talloc_free(frame);
2331                 return NULL;
2332         }
2333
2334         methods = pytalloc_get_ptr(self);
2335
2336         alias_sid = pytalloc_get_ptr(py_alias_sid);
2337         member_sid = pytalloc_get_ptr(py_member_sid);
2338
2339         status = methods->del_aliasmem(methods, alias_sid, member_sid);
2340         if (!NT_STATUS_IS_OK(status)) {
2341                 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2342                                 NT_STATUS_V(status),
2343                                 get_friendly_nt_error_msg(status));
2344                 talloc_free(frame);
2345                 return NULL;
2346         }
2347
2348         talloc_free(frame);
2349         Py_RETURN_NONE;
2350 }
2351
2352
2353 static PyObject *py_pdb_enum_aliasmem(PyObject *self, PyObject *args)
2354 {
2355         TALLOC_CTX *frame = talloc_stackframe();
2356         NTSTATUS status;
2357         struct pdb_methods *methods;
2358         PyObject *py_alias_sid;
2359         struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2360         PyObject *py_member_list, *py_member_sid;
2361         size_t num_members;
2362         int i;
2363
2364         if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2365                 talloc_free(frame);
2366                 return NULL;
2367         }
2368
2369         methods = pytalloc_get_ptr(self);
2370
2371         alias_sid = pytalloc_get_ptr(py_alias_sid);
2372
2373         status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2374         if (!NT_STATUS_IS_OK(status)) {
2375                 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2376                                 NT_STATUS_V(status),
2377                                 get_friendly_nt_error_msg(status));
2378                 talloc_free(frame);
2379                 return NULL;
2380         }
2381
2382         py_member_list = PyList_New(0);
2383         if (py_member_list == NULL) {
2384                 PyErr_NoMemory();
2385                 talloc_free(frame);
2386                 return NULL;
2387         }
2388
2389         for(i=0; i<num_members; i++) {
2390                 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2391                 if (py_member_sid == NULL) {
2392                         PyErr_NoMemory();
2393                         talloc_free(frame);
2394                         return NULL;
2395                 }
2396                 tmp_sid = pytalloc_get_ptr(py_member_sid);
2397                 *tmp_sid = member_sid[i];
2398                 PyList_Append(py_member_list, py_member_sid);
2399         }
2400
2401         talloc_free(frame);
2402         return py_member_list;
2403 }
2404
2405
2406 static PyObject *py_pdb_get_account_policy(PyObject *self, PyObject *unused)
2407 {
2408         TALLOC_CTX *frame = talloc_stackframe();
2409         NTSTATUS status;
2410         struct pdb_methods *methods;
2411         PyObject *py_acct_policy;
2412         uint32_t value;
2413         const char **names;
2414         int count, i;
2415         enum pdb_policy_type type;
2416
2417         methods = pytalloc_get_ptr(self);
2418
2419         py_acct_policy = PyDict_New();
2420         if (py_acct_policy == NULL) {
2421                 PyErr_NoMemory();
2422                 talloc_free(frame);
2423                 return NULL;
2424         }
2425
2426         account_policy_names_list(frame, &names, &count);
2427         for (i=0; i<count; i++) {
2428                 type = account_policy_name_to_typenum(names[i]);
2429                 status = methods->get_account_policy(methods, type, &value);
2430                 if (NT_STATUS_IS_OK(status)) {
2431                         PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2432                 }
2433         }
2434
2435         talloc_free(frame);
2436         return py_acct_policy;
2437 }
2438
2439
2440 static PyObject *py_pdb_set_account_policy(PyObject *self, PyObject *args)
2441 {
2442         TALLOC_CTX *frame = talloc_stackframe();
2443         NTSTATUS status;
2444         struct pdb_methods *methods;
2445         PyObject *py_acct_policy, *py_value;
2446         const char **names;
2447         int count, i;
2448         enum pdb_policy_type type;
2449
2450         if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2451                 talloc_free(frame);
2452                 return NULL;
2453         }
2454
2455         methods = pytalloc_get_ptr(self);
2456
2457         account_policy_names_list(frame, &names, &count);
2458         for (i=0; i<count; i++) {
2459                 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2460                         type = account_policy_name_to_typenum(names[i]);
2461                         status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2462                         if (!NT_STATUS_IS_OK(status)) {
2463                                 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2464                                                 names[i],
2465                                                 NT_STATUS_V(status),
2466                                                 get_friendly_nt_error_msg(status));
2467                         }
2468                 }
2469         }
2470
2471         talloc_free(frame);
2472         Py_RETURN_NONE;
2473 }
2474
2475 static PyObject *py_pdb_search_users(PyObject *self, PyObject *args)
2476 {
2477         TALLOC_CTX *frame = talloc_stackframe();
2478         struct pdb_methods *methods;
2479         unsigned int acct_flags;
2480         struct pdb_search *search;
2481         struct samr_displayentry *entry;
2482         PyObject *py_userlist, *py_dict;
2483
2484         if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2485                 talloc_free(frame);
2486                 return NULL;
2487         }
2488
2489         methods = pytalloc_get_ptr(self);
2490
2491         search = talloc_zero(frame, struct pdb_search);
2492         if (search == NULL) {
2493                 PyErr_NoMemory();
2494                 talloc_free(frame);
2495                 return NULL;
2496         }
2497
2498         if (!methods->search_users(methods, search, acct_flags)) {
2499                 PyErr_Format(py_pdb_error, "Unable to search users");
2500                 talloc_free(frame);
2501                 return NULL;
2502         }
2503
2504         entry = talloc_zero(frame, struct samr_displayentry);
2505         if (entry == NULL) {
2506                 PyErr_NoMemory();
2507                 talloc_free(frame);
2508                 return NULL;
2509         }
2510
2511         py_userlist = PyList_New(0);
2512         if (py_userlist == NULL) {
2513                 PyErr_NoMemory();
2514                 talloc_free(frame);
2515                 return NULL;
2516         }
2517
2518         while (search->next_entry(search, entry)) {
2519                 py_dict = PyDict_New();
2520                 if (py_dict == NULL) {
2521                         PyErr_NoMemory();
2522                 } else {
2523                         PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2524                         PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2525                         PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2526                         PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2527                         PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2528                         PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2529                         PyList_Append(py_userlist, py_dict);
2530                 }
2531         }
2532         search->search_end(search);
2533
2534         talloc_free(frame);
2535         return py_userlist;
2536 }
2537
2538
2539 static PyObject *py_pdb_search_groups(PyObject *self, PyObject *unused)
2540 {
2541         TALLOC_CTX *frame = talloc_stackframe();
2542         struct pdb_methods *methods;
2543         struct pdb_search *search;
2544         struct samr_displayentry *entry;
2545         PyObject *py_grouplist, *py_dict;
2546
2547         methods = pytalloc_get_ptr(self);
2548
2549         search = talloc_zero(frame, struct pdb_search);
2550         if (search == NULL) {
2551                 PyErr_NoMemory();
2552                 talloc_free(frame);
2553                 return NULL;
2554         }
2555
2556         if (!methods->search_groups(methods, search)) {
2557                 PyErr_Format(py_pdb_error, "Unable to search groups");
2558                 talloc_free(frame);
2559                 return NULL;
2560         }
2561
2562         entry = talloc_zero(frame, struct samr_displayentry);
2563         if (entry == NULL) {
2564                 PyErr_NoMemory();
2565                 talloc_free(frame);
2566                 return NULL;
2567         }
2568
2569         py_grouplist = PyList_New(0);
2570         if (py_grouplist == NULL) {
2571                 PyErr_NoMemory();
2572                 talloc_free(frame);
2573                 return NULL;
2574         }
2575
2576         while (search->next_entry(search, entry)) {
2577                 py_dict = PyDict_New();
2578                 if (py_dict == NULL) {
2579                         PyErr_NoMemory();
2580                 } else {
2581                         PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2582                         PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2583                         PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2584                         PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2585                         PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2586                         PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2587                         PyList_Append(py_grouplist, py_dict);
2588                 }
2589         }
2590         search->search_end(search);
2591
2592         talloc_free(frame);
2593         return py_grouplist;
2594 }
2595
2596
2597 static PyObject *py_pdb_search_aliases(PyObject *self, PyObject *args)
2598 {
2599         TALLOC_CTX *frame = talloc_stackframe();
2600         struct pdb_methods *methods;
2601         struct pdb_search *search;
2602         struct samr_displayentry *entry;
2603         PyObject *py_aliaslist, *py_dict;
2604         PyObject *py_domain_sid;
2605         struct dom_sid *domain_sid = NULL;
2606
2607         py_domain_sid = Py_None;
2608         Py_INCREF(Py_None);
2609
2610         if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2611                 talloc_free(frame);
2612                 return NULL;
2613         }
2614
2615         methods = pytalloc_get_ptr(self);
2616
2617         if (py_domain_sid != Py_None) {
2618                 domain_sid = pytalloc_get_ptr(py_domain_sid);
2619         }
2620
2621         search = talloc_zero(frame, struct pdb_search);
2622         if (search == NULL) {
2623                 PyErr_NoMemory();
2624                 talloc_free(frame);
2625                 return NULL;
2626         }
2627
2628         if (!methods->search_aliases(methods, search, domain_sid)) {
2629                 PyErr_Format(py_pdb_error, "Unable to search aliases");
2630                 talloc_free(frame);
2631                 return NULL;
2632         }
2633
2634         entry = talloc_zero(frame, struct samr_displayentry);
2635         if (entry == NULL) {
2636                 PyErr_NoMemory();
2637                 talloc_free(frame);
2638                 return NULL;
2639         }
2640
2641         py_aliaslist = PyList_New(0);
2642         if (py_aliaslist == NULL) {
2643                 PyErr_NoMemory();
2644                 talloc_free(frame);
2645                 return NULL;
2646         }
2647
2648         while (search->next_entry(search, entry)) {
2649                 py_dict = PyDict_New();
2650                 if (py_dict == NULL) {
2651                         PyErr_NoMemory();
2652                 } else {
2653                         PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2654                         PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2655                         PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2656                         PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2657                         PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2658                         PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2659                         PyList_Append(py_aliaslist, py_dict);
2660                 }
2661         }
2662         search->search_end(search);
2663
2664         talloc_free(frame);
2665         return py_aliaslist;
2666 }
2667
2668
2669 static PyObject *py_pdb_uid_to_sid(PyObject *self, PyObject *args)
2670 {
2671         TALLOC_CTX *frame = talloc_stackframe();
2672         struct pdb_methods *methods;
2673         struct unixid id;
2674         unsigned int uid;
2675         struct dom_sid user_sid, *copy_user_sid;
2676         PyObject *py_user_sid;
2677
2678         if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2679                 talloc_free(frame);
2680                 return NULL;
2681         }
2682
2683         methods = pytalloc_get_ptr(self);
2684
2685         id.id = uid;
2686         id.type = ID_TYPE_UID;
2687
2688         if (!methods->id_to_sid(methods, &id, &user_sid)) {
2689                 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2690                 talloc_free(frame);
2691                 return NULL;
2692         }
2693
2694         copy_user_sid = dom_sid_dup(frame, &user_sid);
2695         if (copy_user_sid == NULL) {
2696                 PyErr_NoMemory();
2697                 talloc_free(frame);
2698                 return NULL;
2699         }
2700
2701         py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2702
2703         talloc_free(frame);
2704         return py_user_sid;
2705 }
2706
2707
2708 static PyObject *py_pdb_gid_to_sid(PyObject *self, PyObject *args)
2709 {
2710         TALLOC_CTX *frame = talloc_stackframe();
2711         struct pdb_methods *methods;
2712         struct unixid id;
2713         unsigned int gid;
2714         struct dom_sid group_sid, *copy_group_sid;
2715         PyObject *py_group_sid;
2716
2717         if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2718                 talloc_free(frame);
2719                 return NULL;
2720         }
2721
2722         id.id = gid;
2723         id.type = ID_TYPE_GID;
2724
2725         methods = pytalloc_get_ptr(self);
2726
2727         if (!methods->id_to_sid(methods, &id, &group_sid)) {
2728                 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2729                 talloc_free(frame);
2730                 return NULL;
2731         }
2732
2733         copy_group_sid = dom_sid_dup(frame, &group_sid);
2734         if (copy_group_sid == NULL) {
2735                 PyErr_NoMemory();
2736                 talloc_free(frame);
2737                 return NULL;
2738         }
2739
2740         py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2741
2742         talloc_free(frame);
2743         return py_group_sid;
2744 }
2745
2746
2747 static PyObject *py_pdb_sid_to_id(PyObject *self, PyObject *args)
2748 {
2749         TALLOC_CTX *frame = talloc_stackframe();
2750         struct pdb_methods *methods;
2751         PyObject *py_sid;
2752         struct dom_sid *sid;
2753         struct unixid id;
2754
2755         if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2756                 talloc_free(frame);
2757                 return NULL;
2758         }
2759
2760         methods = pytalloc_get_ptr(self);
2761
2762         sid = pytalloc_get_ptr(py_sid);
2763
2764         if (!methods->sid_to_id(methods, sid, &id)) {
2765                 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2766                 talloc_free(frame);
2767                 return NULL;
2768         }
2769
2770         talloc_free(frame);
2771         return Py_BuildValue("(II)", id.id, id.type);
2772 }
2773
2774
2775 static PyObject *py_pdb_new_rid(PyObject *self, PyObject *unused)
2776 {
2777         TALLOC_CTX *frame = talloc_stackframe();
2778         struct pdb_methods *methods;
2779         uint32_t rid;
2780
2781         methods = pytalloc_get_ptr(self);
2782
2783         if (!methods->new_rid(methods, &rid)) {
2784                 PyErr_Format(py_pdb_error, "Unable to get new rid");
2785                 talloc_free(frame);
2786                 return NULL;
2787         }
2788
2789         talloc_free(frame);
2790         return PyInt_FromLong(rid);
2791 }
2792
2793
2794 static PyObject *py_pdb_get_trusteddom_pw(PyObject *self, PyObject *args)
2795 {
2796         TALLOC_CTX *frame = talloc_stackframe();
2797         struct pdb_methods *methods;
2798         const char *domain;
2799         char *pwd;
2800         struct dom_sid sid, *copy_sid;
2801         PyObject *py_sid;
2802         time_t last_set_time;
2803         PyObject *py_value;
2804
2805         if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2806                 talloc_free(frame);
2807                 return NULL;
2808         }
2809
2810         methods = pytalloc_get_ptr(self);
2811
2812         if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2813                 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2814                 talloc_free(frame);
2815                 return NULL;
2816         }
2817
2818         copy_sid = dom_sid_dup(frame, &sid);
2819         if (copy_sid == NULL) {
2820                 PyErr_NoMemory();
2821                 talloc_free(frame);
2822                 return NULL;
2823         }
2824
2825         py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2826         if (py_sid == NULL) {
2827                 PyErr_NoMemory();
2828                 talloc_free(frame);
2829                 return NULL;
2830         }
2831
2832         py_value = PyDict_New();
2833         if (py_value == NULL) {
2834                 PyErr_NoMemory();
2835                 talloc_free(frame);
2836                 return NULL;
2837         }
2838
2839         PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2840         PyDict_SetItemString(py_value, "sid", py_sid);
2841         PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2842
2843         talloc_free(frame);
2844         return py_value;
2845 }
2846
2847
2848 static PyObject *py_pdb_set_trusteddom_pw(PyObject *self, PyObject *args)
2849 {
2850         TALLOC_CTX *frame = talloc_stackframe();
2851         struct pdb_methods *methods;
2852         const char *domain;
2853         const char *pwd;
2854         const struct dom_sid *domain_sid;
2855         PyObject *py_domain_sid;
2856
2857         if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2858                                         dom_sid_Type, &py_domain_sid)) {
2859                 talloc_free(frame);
2860                 return NULL;
2861         }
2862
2863         methods = pytalloc_get_ptr(self);
2864
2865         domain_sid = pytalloc_get_ptr(py_domain_sid);
2866
2867         if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2868                 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2869                 talloc_free(frame);
2870                 return NULL;
2871         }
2872
2873         talloc_free(frame);
2874         Py_RETURN_NONE;
2875 }
2876
2877
2878 static PyObject *py_pdb_del_trusteddom_pw(PyObject *self, PyObject *args)
2879 {
2880         TALLOC_CTX *frame = talloc_stackframe();
2881         struct pdb_methods *methods;
2882         const char *domain;
2883
2884         if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2885                 talloc_free(frame);
2886                 return NULL;
2887         }
2888
2889         methods = pytalloc_get_ptr(self);
2890
2891         if (!methods->del_trusteddom_pw(methods, domain)) {
2892                 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2893                 talloc_free(frame);
2894                 return NULL;
2895         }
2896
2897         talloc_free(frame);
2898         Py_RETURN_NONE;
2899 }
2900
2901
2902 static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
2903 {
2904         TALLOC_CTX *frame = talloc_stackframe();
2905         NTSTATUS status;
2906         struct pdb_methods *methods;
2907         uint32_t num_domains;
2908         struct trustdom_info **domains;
2909         PyObject *py_domain_list, *py_dict;
2910         int i;
2911
2912         methods = pytalloc_get_ptr(self);
2913
2914         status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
2915         if (!NT_STATUS_IS_OK(status)) {
2916                 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2917                                 NT_STATUS_V(status),
2918                                 get_friendly_nt_error_msg(status));
2919                 talloc_free(frame);
2920                 return NULL;
2921         }
2922
2923         py_domain_list = PyList_New(0);
2924         if (py_domain_list == NULL) {
2925                 PyErr_NoMemory();
2926                 talloc_free(frame);
2927                 return NULL;
2928         }
2929
2930         for(i=0; i<num_domains; i++) {
2931                 py_dict = PyDict_New();
2932                 if (py_dict) {
2933                         PyDict_SetItemString(py_dict, "name",
2934                                         PyString_FromString(domains[i]->name));
2935                         PyDict_SetItemString(py_dict, "sid",
2936                                         pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2937                 }
2938
2939                 PyList_Append(py_domain_list, py_dict);
2940         }
2941
2942         talloc_free(frame);
2943         return py_domain_list;
2944 }
2945
2946
2947 static PyObject *py_pdb_get_trusted_domain(PyObject *self, PyObject *args)
2948 {
2949         TALLOC_CTX *frame = talloc_stackframe();
2950         NTSTATUS status;
2951         struct pdb_methods *methods;
2952         const char *domain;
2953         struct pdb_trusted_domain *td;
2954         PyObject *py_domain_info;
2955
2956         if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2957                 talloc_free(frame);
2958                 return NULL;
2959         }
2960
2961         methods = pytalloc_get_ptr(self);
2962
2963         status = methods->get_trusted_domain(methods, frame, domain, &td);
2964         if (!NT_STATUS_IS_OK(status)) {
2965                 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2966                                 NT_STATUS_V(status),
2967                                 get_friendly_nt_error_msg(status));
2968                 talloc_free(frame);
2969                 return NULL;
2970         }
2971
2972         py_domain_info = PyDict_New();
2973         if (py_domain_info == NULL) {
2974                 PyErr_NoMemory();
2975                 talloc_free(frame);
2976                 return NULL;
2977         }
2978
2979         PyDict_SetItemString(py_domain_info, "domain_name",
2980                         PyString_FromString(td->domain_name));
2981         PyDict_SetItemString(py_domain_info, "netbios_name",
2982                         PyString_FromString(td->netbios_name));
2983         PyDict_SetItemString(py_domain_info, "security_identifier",
2984                         pytalloc_steal(dom_sid_Type, &td->security_identifier));
2985         PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2986                         PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2987                                                 td->trust_auth_incoming.length));
2988         PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2989                         PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2990                                                 td->trust_auth_outgoing.length));
2991         PyDict_SetItemString(py_domain_info, "trust_direction",
2992                         PyInt_FromLong(td->trust_direction));
2993         PyDict_SetItemString(py_domain_info, "trust_type",
2994                         PyInt_FromLong(td->trust_type));
2995         PyDict_SetItemString(py_domain_info, "trust_attributes",
2996                         PyInt_FromLong(td->trust_attributes));
2997         PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
2998                         PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
2999                                                 td->trust_forest_trust_info.length));
3000
3001         talloc_free(frame);
3002         return py_domain_info;
3003 }
3004
3005
3006 static PyObject *py_pdb_get_trusted_domain_by_sid(PyObject *self, PyObject *args)
3007 {
3008         TALLOC_CTX *frame = talloc_stackframe();
3009         NTSTATUS status;
3010         struct pdb_methods *methods;
3011         PyObject *py_domain_sid;
3012         struct dom_sid *domain_sid;
3013         struct pdb_trusted_domain *td;
3014         PyObject *py_domain_info;
3015
3016         if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3017                 talloc_free(frame);
3018                 return NULL;
3019         }
3020
3021         methods = pytalloc_get_ptr(self);
3022
3023         domain_sid = pytalloc_get_ptr(py_domain_sid);
3024
3025         status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3026         if (!NT_STATUS_IS_OK(status)) {
3027                 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3028                                 NT_STATUS_V(status),
3029                                 get_friendly_nt_error_msg(status));
3030                 talloc_free(frame);
3031                 return NULL;
3032         }
3033
3034         py_domain_info = PyDict_New();
3035         if (py_domain_info == NULL) {
3036                 PyErr_NoMemory();
3037                 talloc_free(frame);
3038                 return NULL;
3039         }
3040
3041         PyDict_SetItemString(py_domain_info, "domain_name",
3042                         PyString_FromString(td->domain_name));
3043         PyDict_SetItemString(py_domain_info, "netbios_name",
3044                         PyString_FromString(td->netbios_name));
3045         PyDict_SetItemString(py_domain_info, "security_identifier",
3046                         pytalloc_steal(dom_sid_Type, &td->security_identifier));
3047         PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3048                         PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3049                                                 td->trust_auth_incoming.length));
3050         PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3051                         PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3052                                                 td->trust_auth_outgoing.length));
3053         PyDict_SetItemString(py_domain_info, "trust_direction",
3054                         PyInt_FromLong(td->trust_direction));
3055         PyDict_SetItemString(py_domain_info, "trust_type",
3056                         PyInt_FromLong(td->trust_type));
3057         PyDict_SetItemString(py_domain_info, "trust_attributes",
3058                         PyInt_FromLong(td->trust_attributes));
3059         PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3060                         PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3061                                                 td->trust_forest_trust_info.length));
3062
3063         talloc_free(frame);
3064         return py_domain_info;
3065 }
3066
3067
3068 static PyObject *py_pdb_set_trusted_domain(PyObject *self, PyObject *args)
3069 {
3070         TALLOC_CTX *frame = talloc_stackframe();
3071         NTSTATUS status;
3072         struct pdb_methods *methods;
3073         const char *domain;
3074         PyObject *py_td_info;
3075         struct pdb_trusted_domain td_info;
3076         PyObject *py_tmp;
3077         Py_ssize_t len;
3078
3079         if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3080                 talloc_free(frame);
3081                 return NULL;
3082         }
3083
3084         py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3085         td_info.domain_name = PyString_AsString(py_tmp);
3086
3087         py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3088         td_info.netbios_name = PyString_AsString(py_tmp);
3089
3090         py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3091         td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3092
3093         py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3094         PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3095         td_info.trust_auth_incoming.length = len;
3096
3097         py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3098         PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3099         td_info.trust_auth_outgoing.length = len;
3100
3101         py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3102         td_info.trust_direction = PyInt_AsLong(py_tmp);
3103
3104         py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3105         td_info.trust_type = PyInt_AsLong(py_tmp);
3106
3107         py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3108         td_info.trust_attributes = PyInt_AsLong(py_tmp);
3109
3110         py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3111         PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3112         td_info.trust_forest_trust_info.length = len;
3113
3114         methods = pytalloc_get_ptr(self);
3115
3116         status = methods->set_trusted_domain(methods, domain, &td_info);
3117         if (!NT_STATUS_IS_OK(status)) {
3118                 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3119                                 NT_STATUS_V(status),
3120                                 get_friendly_nt_error_msg(status));
3121                 talloc_free(frame);
3122                 return NULL;
3123         }
3124
3125         talloc_free(frame);
3126         Py_RETURN_NONE;
3127 }
3128
3129
3130 static PyObject *py_pdb_del_trusted_domain(PyObject *self, PyObject *args)
3131 {
3132         TALLOC_CTX *frame = talloc_stackframe();
3133         NTSTATUS status;
3134         struct pdb_methods *methods;
3135         const char *domain;
3136
3137         if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3138                 talloc_free(frame);
3139                 return NULL;
3140         }
3141
3142         methods = pytalloc_get_ptr(self);
3143
3144         status = methods->del_trusted_domain(methods, domain);
3145         if (!NT_STATUS_IS_OK(status)) {
3146                 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3147                                 NT_STATUS_V(status),
3148                                 get_friendly_nt_error_msg(status));
3149                 talloc_free(frame);
3150                 return NULL;
3151         }
3152
3153         talloc_free(frame);
3154         Py_RETURN_NONE;
3155 }
3156
3157
3158 static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
3159 {
3160         TALLOC_CTX *frame = talloc_stackframe();
3161         NTSTATUS status;
3162         struct pdb_methods *methods;
3163         uint32_t num_domains;
3164         struct pdb_trusted_domain **td_info, *td;
3165         PyObject *py_td_info, *py_domain_info;
3166         int i;
3167
3168         methods = pytalloc_get_ptr(self);
3169
3170         status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3171         if (!NT_STATUS_IS_OK(status)) {
3172                 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3173                                 NT_STATUS_V(status),
3174                                 get_friendly_nt_error_msg(status));
3175                 talloc_free(frame);
3176                 return NULL;
3177         }
3178
3179         py_td_info = PyList_New(0);
3180         if (py_td_info == NULL) {
3181                 PyErr_NoMemory();
3182                 talloc_free(frame);
3183                 return NULL;
3184         }
3185
3186         for (i=0; i<num_domains; i++) {
3187
3188                 py_domain_info = PyDict_New();
3189                 if (py_domain_info == NULL) {
3190                         PyErr_NoMemory();
3191                         Py_DECREF(py_td_info);
3192                         talloc_free(frame);
3193                         return NULL;
3194                 }
3195
3196                 td = td_info[i];
3197
3198                 PyDict_SetItemString(py_domain_info, "domain_name",
3199                                 PyString_FromString(td->domain_name));
3200                 PyDict_SetItemString(py_domain_info, "netbios_name",
3201                                 PyString_FromString(td->netbios_name));
3202                 PyDict_SetItemString(py_domain_info, "security_identifier",
3203                                 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3204                 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3205                                 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3206                                                         td->trust_auth_incoming.length));
3207                 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3208                                 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3209                                                         td->trust_auth_outgoing.length));
3210                 PyDict_SetItemString(py_domain_info, "trust_direction",
3211                                 PyInt_FromLong(td->trust_direction));
3212                 PyDict_SetItemString(py_domain_info, "trust_type",
3213                                 PyInt_FromLong(td->trust_type));
3214                 PyDict_SetItemString(py_domain_info, "trust_attributes",
3215                                 PyInt_FromLong(td->trust_attributes));
3216                 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3217                                 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3218                                                         td->trust_forest_trust_info.length));
3219                 PyList_Append(py_td_info, py_domain_info);
3220         }
3221
3222         talloc_free(frame);
3223         return py_td_info;
3224 }
3225
3226
3227 static PyObject *py_pdb_get_secret(PyObject *self, PyObject *args)
3228 {
3229         TALLOC_CTX *frame = talloc_stackframe();
3230         NTSTATUS status;
3231         struct pdb_methods *methods;
3232         const char *secret_name;
3233         DATA_BLOB secret_current, secret_old;
3234         NTTIME secret_current_lastchange, secret_old_lastchange;
3235         PyObject *py_sd;
3236         struct security_descriptor *sd;
3237         PyObject *py_secret;
3238
3239         if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3240                 talloc_free(frame);
3241                 return NULL;
3242         }
3243
3244         methods = pytalloc_get_ptr(self);
3245
3246         py_sd = pytalloc_new(struct security_descriptor, security_Type);
3247         if (py_sd == NULL) {
3248                 PyErr_NoMemory();
3249                 talloc_free(frame);
3250                 return NULL;
3251         }
3252         sd = pytalloc_get_ptr(py_sd);
3253
3254         status = methods->get_secret(methods, frame, secret_name,
3255                                         &secret_current,
3256                                         &secret_current_lastchange,
3257                                         &secret_old,
3258                                         &secret_old_lastchange,
3259                                         &sd);
3260         if (!NT_STATUS_IS_OK(status)) {
3261                 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3262                                 secret_name,
3263                                 NT_STATUS_V(status),
3264                                 get_friendly_nt_error_msg(status));
3265                 talloc_free(frame);
3266                 return NULL;
3267         }
3268
3269         py_secret = PyDict_New();
3270         if (py_secret == NULL) {
3271                 PyErr_NoMemory();
3272                 Py_DECREF(py_sd);
3273                 talloc_free(frame);
3274                 return NULL;
3275         }
3276
3277         PyDict_SetItemString(py_secret, "secret_current",
3278                         PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3279         PyDict_SetItemString(py_secret, "secret_current_lastchange",
3280                         PyLong_FromUnsignedLongLong(secret_current_lastchange));
3281         PyDict_SetItemString(py_secret, "secret_old",
3282                         PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3283         PyDict_SetItemString(py_secret, "secret_old_lastchange",
3284                         PyLong_FromUnsignedLongLong(secret_old_lastchange));
3285         PyDict_SetItemString(py_secret, "sd", py_sd);
3286
3287         talloc_free(frame);
3288         return py_secret;
3289 }
3290
3291
3292 static PyObject *py_pdb_set_secret(PyObject *self, PyObject *args)
3293 {
3294         TALLOC_CTX *frame = talloc_stackframe();
3295         NTSTATUS status;
3296         struct pdb_methods *methods;
3297         const char *secret_name;
3298         PyObject *py_secret;
3299         PyObject *py_secret_cur, *py_secret_old, *py_sd;
3300         DATA_BLOB secret_current, secret_old;
3301         struct security_descriptor *sd;
3302         Py_ssize_t len;
3303
3304         if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3305                 talloc_free(frame);
3306                 return NULL;
3307         }
3308
3309         py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3310         py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3311         py_sd = PyDict_GetItemString(py_secret, "sd");
3312
3313         PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3314         PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3315         PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3316
3317         methods = pytalloc_get_ptr(self);
3318
3319         PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3320         secret_current.length = len;
3321         PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3322         secret_current.length = len;
3323         sd = pytalloc_get_ptr(py_sd);
3324
3325         status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3326         if (!NT_STATUS_IS_OK(status)) {
3327                 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3328                                 secret_name,
3329                                 NT_STATUS_V(status),
3330                                 get_friendly_nt_error_msg(status));
3331                 talloc_free(frame);
3332                 return NULL;
3333         }
3334
3335         talloc_free(frame);
3336         Py_RETURN_NONE;
3337 }
3338
3339
3340 static PyObject *py_pdb_delete_secret(PyObject *self, PyObject *args)
3341 {
3342         TALLOC_CTX *frame = talloc_stackframe();
3343         NTSTATUS status;
3344         struct pdb_methods *methods;
3345         const char *secret_name;
3346
3347         if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3348                 talloc_free(frame);
3349                 return NULL;
3350         }
3351
3352         methods = pytalloc_get_ptr(self);
3353
3354         status = methods->delete_secret(methods, secret_name);
3355         if (!NT_STATUS_IS_OK(status)) {
3356                 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3357                                 secret_name,
3358                                 NT_STATUS_V(status),
3359                                 get_friendly_nt_error_msg(status));
3360                 talloc_free(frame);
3361                 return NULL;
3362         }
3363
3364         talloc_free(frame);
3365         Py_RETURN_NONE;
3366 }
3367
3368 static PyMethodDef py_pdb_methods[] = {
3369         { "domain_info", py_pdb_domain_info, METH_NOARGS,
3370                 "domain_info() -> str\n\n \
3371                 Get domain information for the database." },
3372         { "getsampwnam", py_pdb_getsampwnam, METH_VARARGS,
3373                 "getsampwnam(username) -> samu object\n\n \
3374                 Get user information by name." },
3375         { "getsampwsid", py_pdb_getsampwsid, METH_VARARGS,
3376                 "getsampwsid(user_sid) -> samu object\n\n \
3377                 Get user information by sid (dcerpc.security.dom_sid object)." },
3378         { "create_user", py_pdb_create_user, METH_VARARGS,
3379                 "create_user(username, acct_flags) -> rid\n\n \
3380                 Create user. acct_flags are samr account control flags." },
3381         { "delete_user", py_pdb_delete_user, METH_VARARGS,
3382                 "delete_user(samu object) -> None\n\n \
3383                 Delete user." },
3384         { "add_sam_account", py_pdb_add_sam_account, METH_VARARGS,
3385                 "add_sam_account(samu object) -> None\n\n \
3386                 Add SAM account." },
3387         { "update_sam_account", py_pdb_update_sam_account, METH_VARARGS,
3388                 "update_sam_account(samu object) -> None\n\n \
3389                 Update SAM account." },
3390         { "delete_sam_account", py_pdb_delete_sam_account, METH_VARARGS,
3391                 "delete_sam_account(samu object) -> None\n\n \
3392                 Delete SAM account." },
3393         { "rename_sam_account", py_pdb_rename_sam_account, METH_VARARGS,
3394                 "rename_sam_account(samu object1, new_username) -> None\n\n \
3395                 Rename SAM account." },
3396         /* update_login_attempts */
3397         { "getgrsid", py_pdb_getgrsid, METH_VARARGS,
3398                 "getgrsid(group_sid) -> groupmap object\n\n \
3399                 Get group information by sid (dcerpc.security.dom_sid object)." },
3400         { "getgrgid", py_pdb_getgrgid, METH_VARARGS,
3401                 "getgrsid(gid) -> groupmap object\n\n \
3402                 Get group information by gid." },
3403         { "getgrnam", py_pdb_getgrnam, METH_VARARGS,
3404                 "getgrsid(groupname) -> groupmap object\n\n \
3405                 Get group information by name." },
3406         { "create_dom_group", py_pdb_create_dom_group, METH_VARARGS,
3407                 "create_dom_group(groupname) -> group_rid\n\n \
3408                 Create new domain group by name." },
3409         { "delete_dom_group", py_pdb_delete_dom_group, METH_VARARGS,
3410                 "delete_dom_group(group_rid) -> None\n\n \
3411                 Delete domain group identified by rid" },
3412         { "add_group_mapping_entry", py_pdb_add_group_mapping_entry, METH_VARARGS,
3413                 "add_group_mapping_entry(groupmap) -> None\n \
3414                 Add group mapping entry for groupmap object." },
3415         { "update_group_mapping_entry", py_pdb_update_group_mapping_entry, METH_VARARGS,
3416                 "update_group_mapping_entry(groupmap) -> None\n\n \
3417                 Update group mapping entry for groupmap object." },
3418         { "delete_group_mapping_entry", py_pdb_delete_group_mapping_entry, METH_VARARGS,
3419                 "delete_group_mapping_entry(groupmap) -> None\n\n \
3420                 Delete group mapping entry for groupmap object." },
3421         { "enum_group_mapping", py_pdb_enum_group_mapping, METH_VARARGS,
3422                 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3423                 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3424         { "enum_group_members", py_pdb_enum_group_members, METH_VARARGS,
3425                 "enum_group_members(group_sid) -> List\n\n \
3426                 Return list of users (dom_sid object) in group." },
3427         { "enum_group_memberships", py_pdb_enum_group_memberships, METH_VARARGS,
3428                 "enum_group_memberships(samu object) -> List\n\n \
3429                 Return list of groups (dom_sid object) this user is part of." },
3430         /* set_unix_primary_group */
3431         { "add_groupmem", py_pdb_add_groupmem, METH_VARARGS,
3432                 "add_groupmem(group_rid, member_rid) -> None\n\n \
3433                 Add user to group." },
3434         { "del_groupmem", py_pdb_del_groupmem, METH_VARARGS,
3435                 "del_groupmem(group_rid, member_rid) -> None\n\n \
3436                 Remove user from from group." },
3437         { "create_alias", py_pdb_create_alias, METH_VARARGS,
3438                 "create_alias(alias_name) -> alias_rid\n\n \
3439                 Create alias entry." },
3440         { "delete_alias", py_pdb_delete_alias, METH_VARARGS,
3441                 "delete_alias(alias_sid) -> None\n\n \
3442                 Delete alias entry." },
3443         { "get_aliasinfo", py_pdb_get_aliasinfo, METH_VARARGS,
3444                 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3445                 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3446         { "set_aliasinfo", py_pdb_set_aliasinfo, METH_VARARGS,
3447                 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3448                 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3449         { "add_aliasmem", py_pdb_add_aliasmem, METH_VARARGS,
3450                 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3451                 Add user to alias entry." },
3452         { "del_aliasmem", py_pdb_del_aliasmem, METH_VARARGS,
3453                 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3454                 Remove a user from alias entry." },
3455         { "enum_aliasmem", py_pdb_enum_aliasmem, METH_VARARGS,
3456                 "enum_aliasmem(alias_sid) -> List\n\n \
3457                 Return a list of members (dom_sid object) for alias entry." },
3458         /* enum_alias_memberships */
3459         /* lookup_rids */
3460         /* lookup_names */
3461         { "get_account_policy", py_pdb_get_account_policy, METH_NOARGS,
3462                 "get_account_policy() -> Mapping\n\n \
3463                 Get account policy information as a dictionary." },
3464         { "set_account_policy", py_pdb_set_account_policy, METH_VARARGS,
3465                 "get_account_policy(Mapping) -> None\n\n \
3466                 Set account policy settings from a dicionary." },
3467         /* get_seq_num */
3468         { "search_users", py_pdb_search_users, METH_VARARGS,
3469                 "search_users(acct_flags) -> List\n\n \
3470                 Search users. acct_flags are samr account control flags.\n \
3471                 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3472         { "search_groups", py_pdb_search_groups, METH_NOARGS,
3473                 "search_groups() -> List\n\n \
3474                 Search unix only groups. \n \
3475                 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3476         { "search_aliases", py_pdb_search_aliases, METH_VARARGS,
3477                 "search_aliases([domain_sid]) -> List\n\n \
3478                 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3479                 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3480         { "uid_to_sid", py_pdb_uid_to_sid, METH_VARARGS,
3481                 "uid_to_sid(uid) -> sid\n\n \
3482                 Return sid for given user id." },
3483         { "gid_to_sid", py_pdb_gid_to_sid, METH_VARARGS,
3484                 "gid_to_sid(gid) -> sid\n\n \
3485                 Return sid for given group id." },
3486         { "sid_to_id", py_pdb_sid_to_id, METH_VARARGS,
3487                 "sid_to_id(sid) -> Tuple\n\n \
3488                 Return id and type for given sid." },
3489         /* capabilities */
3490         { "new_rid", py_pdb_new_rid, METH_NOARGS,
3491                 "new_rid() -> rid\n\n \
3492                 Get a new rid." },
3493         { "get_trusteddom_pw", py_pdb_get_trusteddom_pw, METH_VARARGS,
3494                 "get_trusteddom_pw(domain) -> Mapping\n\n \
3495                 Get trusted domain password, sid and last set time in a dictionary." },
3496         { "set_trusteddom_pw", py_pdb_set_trusteddom_pw, METH_VARARGS,
3497                 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3498                 Set trusted domain password." },
3499         { "del_trusteddom_pw", py_pdb_del_trusteddom_pw, METH_VARARGS,
3500                 "del_trusteddom_pw(domain) -> None\n\n \
3501                 Delete trusted domain password." },
3502         { "enum_trusteddoms", py_pdb_enum_trusteddoms, METH_NOARGS,
3503                 "enum_trusteddoms() -> List\n\n \
3504                 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3505         { "get_trusted_domain", py_pdb_get_trusted_domain, METH_VARARGS,
3506                 "get_trusted_domain(domain) -> Mapping\n\n \
3507                 Get trusted domain information by name. Information is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info." },
3508         { "get_trusted_domain_by_sid", py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3509                 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3510                 Get trusted domain information by sid. Information is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info" },
3511         { "set_trusted_domain", py_pdb_set_trusted_domain, METH_VARARGS,
3512                 "set_trusted_domain(domain, Mapping) -> None\n\n \
3513                 Set trusted domain information for domain. Mapping is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info." },
3514         { "del_trusted_domain", py_pdb_del_trusted_domain, METH_VARARGS,
3515                 "del_trusted_domain(domain) -> None\n\n \
3516                 Delete trusted domain." },
3517         { "enum_trusted_domains", py_pdb_enum_trusted_domains, METH_VARARGS,
3518                 "enum_trusted_domains() -> List\n\n \
3519                 Get list of trusted domains. Each entry is a dictionary with keys - domain_name, netbios_name, security_identifier, trust_auth_incoming, trust_auth_outgoing, trust_direction, trust_type, trust_attributes, trust_forest_trust_info." },
3520         { "get_secret", py_pdb_get_secret, METH_VARARGS,
3521                 "get_secret(secret_name) -> Mapping\n\n \
3522                 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3523         { "set_secret", py_pdb_set_secret, METH_VARARGS,
3524                 "set_secret(secret_name, Mapping) -> None\n\n \
3525                 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3526         { "delete_secret", py_pdb_delete_secret, METH_VARARGS,
3527                 "delete_secret(secret_name) -> None\n\n \
3528                 Delete secret information for secret_name." },
3529         { NULL },
3530 };
3531
3532
3533 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3534 {
3535         TALLOC_CTX *frame = talloc_stackframe();
3536         const char *url = NULL;
3537         PyObject *pypdb;
3538         NTSTATUS status;
3539         struct pdb_methods *methods;
3540
3541         if (!PyArg_ParseTuple(args, "s", &url)) {
3542                 talloc_free(frame);
3543                 return NULL;
3544         }
3545
3546         /* Initalize list of methods */
3547         status = make_pdb_method_name(&methods, url);
3548         if (!NT_STATUS_IS_OK(status)) {
3549                 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3550                                 url,
3551                                 NT_STATUS_V(status),
3552                                 get_friendly_nt_error_msg(status));
3553                 talloc_free(frame);
3554                 return NULL;
3555         }
3556
3557         if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3558                 PyErr_NoMemory();
3559                 talloc_free(frame);
3560                 return NULL;
3561         }
3562
3563         talloc_free(frame);
3564         return pypdb;
3565 }
3566
3567
3568 static PyTypeObject PyPDB = {
3569         .tp_name = "passdb.PDB",
3570         .tp_new = py_pdb_new,
3571         .tp_flags = Py_TPFLAGS_DEFAULT,
3572         .tp_methods = py_pdb_methods,
3573         .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3574 };
3575
3576
3577 /*
3578  * Return a list of passdb backends
3579  */
3580 static PyObject *py_passdb_backends(PyObject *self, PyObject *unused)
3581 {
3582         TALLOC_CTX *frame = talloc_stackframe();
3583         PyObject *py_blist;
3584         const struct pdb_init_function_entry *entry;
3585
3586         entry = pdb_get_backends();
3587         if(! entry) {
3588                 Py_RETURN_NONE;
3589         }
3590
3591         if((py_blist = PyList_New(0)) == NULL) {
3592                 PyErr_NoMemory();
3593                 talloc_free(frame);
3594                 return NULL;
3595         }
3596
3597         while(entry) {
3598                 PyList_Append(py_blist, PyString_FromString(entry->name));
3599                 entry = entry->next;
3600         }
3601
3602         talloc_free(frame);
3603         return py_blist;
3604 }
3605
3606
3607 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3608 {
3609         TALLOC_CTX *frame = talloc_stackframe();
3610         const char *smb_config;
3611
3612         if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3613                 talloc_free(frame);
3614                 return NULL;
3615         }
3616
3617         /* Load smbconf parameters */
3618         if (!lp_load_global(smb_config)) {
3619                 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3620                 talloc_free(frame);
3621                 return NULL;
3622         }
3623
3624         talloc_free(frame);
3625         Py_RETURN_NONE;
3626 }
3627
3628
3629 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3630 {
3631         TALLOC_CTX *frame = talloc_stackframe();
3632         const char *private_dir;
3633
3634         if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3635                 talloc_free(frame);
3636                 return NULL;
3637         }
3638
3639         /* Initialize secrets database */
3640         if (!secrets_init_path(private_dir)) {
3641                 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3642                                 private_dir);
3643                 talloc_free(frame);
3644                 return NULL;
3645         }
3646
3647         talloc_free(frame);
3648         Py_RETURN_NONE;
3649 }
3650
3651 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3652 {
3653         TALLOC_CTX *frame = talloc_stackframe();
3654
3655         /* Initialize secrets database */
3656         if (!initialize_password_db(true, NULL)) {
3657                 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3658                 talloc_free(frame);
3659                 return NULL;
3660         }
3661
3662         talloc_free(frame);
3663         Py_RETURN_NONE;
3664 }
3665
3666 static PyObject *py_get_global_sam_sid(PyObject *self, PyObject *unused)
3667 {
3668         TALLOC_CTX *frame = talloc_stackframe();
3669         struct dom_sid *domain_sid, *domain_sid_copy;
3670         PyObject *py_dom_sid;
3671
3672         domain_sid = get_global_sam_sid();
3673
3674         domain_sid_copy = dom_sid_dup(frame, domain_sid);
3675         if (domain_sid_copy == NULL) {
3676                 PyErr_NoMemory();
3677                 talloc_free(frame);
3678                 return NULL;
3679         }
3680
3681         py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3682
3683         talloc_free(frame);
3684         return py_dom_sid;
3685 }
3686
3687
3688 static PyMethodDef py_passdb_methods[] = {
3689         { "get_backends", py_passdb_backends, METH_NOARGS,
3690                 "get_backends() -> list\n\n \
3691                 Get a list of password database backends supported." },
3692         { "set_smb_config", py_set_smb_config, METH_VARARGS,
3693                 "set_smb_config(path) -> None\n\n \
3694                 Set path to smb.conf file to load configuration parameters." },
3695         { "set_secrets_dir", py_set_secrets_dir, METH_VARARGS,
3696                 "set_secrets_dir(private_dir) -> None\n\n \
3697                 Set path to private directory to load secrets database from non-default location." },
3698         { "get_global_sam_sid", py_get_global_sam_sid, METH_NOARGS,
3699                 "get_global_sam_sid() -> dom_sid\n\n \
3700                 Return domain SID." },
3701         { "reload_static_pdb", py_reload_static_pdb, METH_NOARGS,
3702                 "reload_static_pdb() -> None\n\n \
3703                 Re-initalise the static pdb used internally.  Needed if 'passdb backend' is changed." },
3704         { NULL },
3705 };
3706
3707 void initpassdb(void)
3708 {
3709         TALLOC_CTX *frame = talloc_stackframe();
3710         PyObject *m, *mod;
3711         char exception_name[] = "passdb.error";
3712
3713         if (pytalloc_BaseObject_PyType_Ready(&PyPDB) < 0) {
3714                 talloc_free(frame);
3715                 return;
3716         }
3717
3718         if (pytalloc_BaseObject_PyType_Ready(&PySamu) < 0) {
3719                 talloc_free(frame);
3720                 return;
3721         }
3722
3723         if (pytalloc_BaseObject_PyType_Ready(&PyGroupmap) < 0) {
3724                 talloc_free(frame);
3725                 return;
3726         }
3727
3728         m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3729         if (m == NULL) {
3730             talloc_free(frame);
3731             return;
3732         }
3733
3734         /* Create new exception for passdb module */
3735         py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3736         Py_INCREF(py_pdb_error);
3737         PyModule_AddObject(m, "error", py_pdb_error);
3738
3739         Py_INCREF(&PyPDB);
3740         PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3741
3742         Py_INCREF(&PySamu);
3743         PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3744
3745         Py_INCREF(&PyGroupmap);
3746         PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3747
3748         /* Import dom_sid type from dcerpc.security */
3749         mod = PyImport_ImportModule("samba.dcerpc.security");
3750         if (mod == NULL) {
3751                 talloc_free(frame);
3752                 return;
3753         }
3754
3755         dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3756         if (dom_sid_Type == NULL) {
3757                 talloc_free(frame);
3758                 return;
3759         }
3760
3761         /* Import security_descriptor type from dcerpc.security */
3762         security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3763         Py_DECREF(mod);
3764         if (security_Type == NULL) {
3765                 talloc_free(frame);
3766                 return;
3767         }
3768
3769         /* Import GUID type from dcerpc.misc */
3770         mod = PyImport_ImportModule("samba.dcerpc.misc");
3771         if (mod == NULL) {
3772                 talloc_free(frame);
3773                 return;
3774         }
3775
3776         guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3777         Py_DECREF(mod);
3778         if (guid_Type == NULL) {
3779                 talloc_free(frame);
3780                 return;
3781         }
3782         talloc_free(frame);
3783 }