python: Remove Python 2.4 support macros
[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_basicsize = sizeof(pytalloc_Object),
1112         .tp_getset = py_samu_getsetters,
1113         .tp_methods = NULL,
1114         .tp_new = py_samu_new,
1115         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1116         .tp_doc = "Samu() -> samu object\n",
1117 };
1118
1119
1120 static PyObject *py_groupmap_get_gid(PyObject *obj, void *closure)
1121 {
1122         TALLOC_CTX *frame = talloc_stackframe();
1123         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1124         PyObject *py_gid;
1125
1126         py_gid = Py_BuildValue("i", group_map->gid);
1127         talloc_free(frame);
1128         return py_gid;
1129 }
1130
1131 static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
1132 {
1133         TALLOC_CTX *frame = talloc_stackframe();
1134         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1135
1136         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1137         group_map->gid = PyInt_AsLong(value);
1138         talloc_free(frame);
1139         return 0;
1140 }
1141
1142 static PyObject *py_groupmap_get_sid(PyObject *obj, void *closure)
1143 {
1144         TALLOC_CTX *frame = talloc_stackframe();
1145         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1146         PyObject *py_sid;
1147         struct dom_sid *group_sid;
1148         TALLOC_CTX *mem_ctx;
1149
1150         mem_ctx = talloc_new(NULL);
1151         if (mem_ctx == NULL) {
1152                 PyErr_NoMemory();
1153                 talloc_free(frame);
1154                 return NULL;
1155         }
1156
1157         group_sid = dom_sid_dup(mem_ctx, &group_map->sid);
1158         if (group_sid == NULL) {
1159                 PyErr_NoMemory();
1160                 talloc_free(mem_ctx);
1161                 talloc_free(frame);
1162                 return NULL;
1163         }
1164
1165         py_sid = pytalloc_steal(dom_sid_Type, group_sid);
1166
1167         talloc_free(mem_ctx);
1168
1169         talloc_free(frame);
1170         return py_sid;
1171 }
1172
1173 static int py_groupmap_set_sid(PyObject *obj, PyObject *value, void *closure)
1174 {
1175         TALLOC_CTX *frame = talloc_stackframe();
1176         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1177
1178         PY_CHECK_TYPE(dom_sid_Type, value, return -1;);
1179         group_map->sid = *pytalloc_get_type(value, struct dom_sid);
1180         talloc_free(frame);
1181         return 0;
1182 }
1183
1184 static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
1185 {
1186         TALLOC_CTX *frame = talloc_stackframe();
1187         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1188         PyObject *py_sid_name_use;
1189
1190         py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
1191         talloc_free(frame);
1192         return py_sid_name_use;
1193 }
1194
1195 static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *closure)
1196 {
1197         TALLOC_CTX *frame = talloc_stackframe();
1198         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1199
1200         PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
1201         group_map->sid_name_use = PyInt_AsLong(value);
1202         talloc_free(frame);
1203         return 0;
1204 }
1205
1206 static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
1207 {
1208         TALLOC_CTX *frame = talloc_stackframe();
1209         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1210         PyObject *py_nt_name;
1211         if (group_map->nt_name == NULL) {
1212                 py_nt_name = Py_None;
1213                 Py_INCREF(py_nt_name);
1214         } else {
1215                 py_nt_name = PyString_FromString(group_map->nt_name);
1216         }
1217         talloc_free(frame);
1218         return py_nt_name;
1219 }
1220
1221 static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure)
1222 {
1223         TALLOC_CTX *frame = talloc_stackframe();
1224         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1225
1226         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1227         if (value == Py_None) {
1228                 fstrcpy(group_map->nt_name, NULL);
1229         } else {
1230                 fstrcpy(group_map->nt_name, PyString_AsString(value));
1231         }
1232         talloc_free(frame);
1233         return 0;
1234 }
1235
1236 static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
1237 {
1238         TALLOC_CTX *frame = talloc_stackframe();
1239         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1240         PyObject *py_comment;
1241         if (group_map->comment == NULL) {
1242                 py_comment = Py_None;
1243                 Py_INCREF(py_comment);
1244         } else {
1245                 py_comment = PyString_FromString(group_map->comment);
1246         }
1247         talloc_free(frame);
1248         return py_comment;
1249 }
1250
1251 static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure)
1252 {
1253         TALLOC_CTX *frame = talloc_stackframe();
1254         GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
1255
1256         PY_CHECK_TYPE(&PyString_Type, value, return -1;);
1257         if (value == Py_None) {
1258                 fstrcpy(group_map->comment, NULL);
1259         } else {
1260                 fstrcpy(group_map->comment, PyString_AsString(value));
1261         }
1262         talloc_free(frame);
1263         return 0;
1264 }
1265
1266 static PyGetSetDef py_groupmap_getsetters[] = {
1267         { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
1268         { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
1269         { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
1270         { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
1271         { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
1272         { NULL }
1273 };
1274
1275 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1276 {
1277         TALLOC_CTX *frame = talloc_stackframe();
1278         GROUP_MAP *group_map;
1279         TALLOC_CTX *mem_ctx;
1280         PyObject *py_group_map;
1281
1282         mem_ctx = talloc_new(NULL);
1283         if (mem_ctx == NULL) {
1284                 PyErr_NoMemory();
1285                 talloc_free(frame);
1286                 return NULL;
1287         }
1288
1289         group_map = talloc_zero(mem_ctx, GROUP_MAP);
1290         if (group_map == NULL) {
1291                 PyErr_NoMemory();
1292                 talloc_free(mem_ctx);
1293                 talloc_free(frame);
1294                 return NULL;
1295         }
1296
1297         py_group_map = pytalloc_steal(type, group_map);
1298         if (py_group_map == NULL) {
1299                 PyErr_NoMemory();
1300                 talloc_free(mem_ctx);
1301                 talloc_free(frame);
1302                 return NULL;
1303         }
1304
1305         talloc_free(mem_ctx);
1306
1307         talloc_free(frame);
1308         return py_group_map;
1309 }
1310
1311
1312 static PyTypeObject PyGroupmap = {
1313         .tp_name = "passdb.Groupmap",
1314         .tp_basicsize = sizeof(pytalloc_Object),
1315         .tp_getset = py_groupmap_getsetters,
1316         .tp_methods = NULL,
1317         .tp_new = py_groupmap_new,
1318         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1319         .tp_doc = "Groupmap() -> group map object\n",
1320 };
1321
1322
1323 static PyObject *py_pdb_domain_info(pytalloc_Object *self, PyObject *args)
1324 {
1325         TALLOC_CTX *frame = talloc_stackframe();
1326         struct pdb_methods *methods;
1327         struct pdb_domain_info *domain_info;
1328         PyObject *py_domain_info;
1329         struct dom_sid *sid;
1330         struct GUID *guid;
1331
1332         methods = pytalloc_get_ptr(self);
1333
1334         domain_info = methods->get_domain_info(methods, frame);
1335         if (! domain_info) {
1336                 Py_RETURN_NONE;
1337         }
1338
1339         sid = dom_sid_dup(frame, &domain_info->sid);
1340         if (sid == NULL) {
1341                 PyErr_NoMemory();
1342                 talloc_free(frame);
1343                 return NULL;
1344         }
1345
1346         guid = talloc(frame, struct GUID);
1347         if (guid == NULL) {
1348                 PyErr_NoMemory();
1349                 talloc_free(frame);
1350                 return NULL;
1351         }
1352         *guid = domain_info->guid;
1353
1354         if ((py_domain_info = PyDict_New()) == NULL) {
1355                 PyErr_NoMemory();
1356                 talloc_free(frame);
1357                 return NULL;
1358         }
1359
1360         PyDict_SetItemString(py_domain_info, "name", PyString_FromString(domain_info->name));
1361         PyDict_SetItemString(py_domain_info, "dns_domain", PyString_FromString(domain_info->dns_domain));
1362         PyDict_SetItemString(py_domain_info, "dns_forest", PyString_FromString(domain_info->dns_forest));
1363         PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
1364         PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
1365
1366         talloc_free(frame);
1367         return py_domain_info;
1368 }
1369
1370
1371 static PyObject *py_pdb_getsampwnam(pytalloc_Object *self, PyObject *args)
1372 {
1373         TALLOC_CTX *frame = talloc_stackframe();
1374         NTSTATUS status;
1375         const char *username;
1376         struct pdb_methods *methods;
1377         struct samu *sam_acct;
1378         PyObject *py_sam_acct;
1379
1380         if (!PyArg_ParseTuple(args, "s:getsampwnam", &username)) {
1381                 talloc_free(frame);
1382                 return NULL;
1383         }
1384
1385         methods = pytalloc_get_ptr(self);
1386
1387         py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1388         if (py_sam_acct == NULL) {
1389                 PyErr_NoMemory();
1390                 talloc_free(frame);
1391                 return NULL;
1392         }
1393         sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1394
1395         status = methods->getsampwnam(methods, sam_acct, username);
1396         if (!NT_STATUS_IS_OK(status)) {
1397                 PyErr_Format(py_pdb_error, "Unable to get user information for '%s', (%d,%s)",
1398                                 username,
1399                                 NT_STATUS_V(status),
1400                                 get_friendly_nt_error_msg(status));
1401                 Py_DECREF(py_sam_acct);
1402                 talloc_free(frame);
1403                 return NULL;
1404         }
1405
1406         talloc_free(frame);
1407         return py_sam_acct;
1408 }
1409
1410 static PyObject *py_pdb_getsampwsid(pytalloc_Object *self, PyObject *args)
1411 {
1412         TALLOC_CTX *frame = talloc_stackframe();
1413         NTSTATUS status;
1414         struct pdb_methods *methods;
1415         struct samu *sam_acct;
1416         PyObject *py_sam_acct;
1417         PyObject *py_user_sid;
1418
1419         if (!PyArg_ParseTuple(args, "O:getsampwsid", &py_user_sid)) {
1420                 talloc_free(frame);
1421                 return NULL;
1422         }
1423
1424         methods = pytalloc_get_ptr(self);
1425
1426         py_sam_acct = py_samu_new(&PySamu, NULL, NULL);
1427         if (py_sam_acct == NULL) {
1428                 PyErr_NoMemory();
1429                 talloc_free(frame);
1430                 return NULL;
1431         }
1432         sam_acct = (struct samu *)pytalloc_get_ptr(py_sam_acct);
1433
1434         status = methods->getsampwsid(methods, sam_acct, pytalloc_get_ptr(py_user_sid));
1435         if (!NT_STATUS_IS_OK(status)) {
1436                 PyErr_Format(py_pdb_error, "Unable to get user information from SID, (%d,%s)",
1437                                 NT_STATUS_V(status),
1438                                 get_friendly_nt_error_msg(status));
1439                 Py_DECREF(py_sam_acct);
1440                 talloc_free(frame);
1441                 return NULL;
1442         }
1443
1444         talloc_free(frame);
1445         return py_sam_acct;
1446 }
1447
1448 static PyObject *py_pdb_create_user(pytalloc_Object *self, PyObject *args)
1449 {
1450         TALLOC_CTX *frame = talloc_stackframe();
1451         NTSTATUS status;
1452         struct pdb_methods *methods;
1453         const char *username;
1454         unsigned int acct_flags;
1455         unsigned int rid;
1456
1457         if (!PyArg_ParseTuple(args, "sI:create_user", &username, &acct_flags)) {
1458                 talloc_free(frame);
1459                 return NULL;
1460         }
1461
1462         methods = pytalloc_get_ptr(self);
1463
1464         status = methods->create_user(methods, frame, username, acct_flags, &rid);
1465         if (!NT_STATUS_IS_OK(status)) {
1466                 PyErr_Format(py_pdb_error, "Unable to create user (%s), (%d,%s)",
1467                                 username,
1468                                 NT_STATUS_V(status),
1469                                 get_friendly_nt_error_msg(status));
1470                 talloc_free(frame);
1471                 return NULL;
1472         }
1473
1474         talloc_free(frame);
1475         return PyInt_FromLong(rid);
1476 }
1477
1478 static PyObject *py_pdb_delete_user(pytalloc_Object *self, PyObject *args)
1479 {
1480         TALLOC_CTX *frame = talloc_stackframe();
1481         NTSTATUS status;
1482         struct pdb_methods *methods;
1483         struct samu *sam_acct;
1484         PyObject *py_sam_acct;
1485
1486         if (!PyArg_ParseTuple(args, "O!:delete_user", &PySamu, &py_sam_acct)) {
1487                 talloc_free(frame);
1488                 return NULL;
1489         }
1490
1491         methods = pytalloc_get_ptr(self);
1492
1493         sam_acct = pytalloc_get_ptr(py_sam_acct);
1494
1495         status = methods->delete_user(methods, frame, sam_acct);
1496         if (!NT_STATUS_IS_OK(status)) {
1497                 PyErr_Format(py_pdb_error, "Unable to delete user, (%d,%s)",
1498                                 NT_STATUS_V(status),
1499                                 get_friendly_nt_error_msg(status));
1500                 talloc_free(frame);
1501                 return NULL;
1502         }
1503
1504         Py_RETURN_NONE;
1505         talloc_free(frame);
1506 }
1507
1508 static PyObject *py_pdb_add_sam_account(pytalloc_Object *self, PyObject *args)
1509 {
1510         TALLOC_CTX *frame = talloc_stackframe();
1511         NTSTATUS status;
1512         struct pdb_methods *methods;
1513         struct samu *sam_acct;
1514         PyObject *py_sam_acct;
1515
1516         if (!PyArg_ParseTuple(args, "O!:add_sam_account", &PySamu, &py_sam_acct)) {
1517                 talloc_free(frame);
1518                 return NULL;
1519         }
1520
1521         methods = pytalloc_get_ptr(self);
1522
1523         sam_acct = pytalloc_get_ptr(py_sam_acct);
1524
1525         status = methods->add_sam_account(methods, sam_acct);
1526         if (!NT_STATUS_IS_OK(status)) {
1527                 PyErr_Format(py_pdb_error, "Unable to add sam account '%s', (%d,%s)",
1528                                 sam_acct->username,
1529                                 NT_STATUS_V(status),
1530                                 get_friendly_nt_error_msg(status));
1531                 talloc_free(frame);
1532                 return NULL;
1533         }
1534
1535         Py_RETURN_NONE;
1536         talloc_free(frame);
1537 }
1538
1539 static PyObject *py_pdb_update_sam_account(pytalloc_Object *self, PyObject *args)
1540 {
1541         TALLOC_CTX *frame = talloc_stackframe();
1542         NTSTATUS status;
1543         struct pdb_methods *methods;
1544         struct samu *sam_acct;
1545         PyObject *py_sam_acct;
1546
1547         if (!PyArg_ParseTuple(args, "O!:update_sam_account", &PySamu, &py_sam_acct)) {
1548                 talloc_free(frame);
1549                 return NULL;
1550         }
1551
1552         methods = pytalloc_get_ptr(self);
1553
1554         sam_acct = pytalloc_get_ptr(py_sam_acct);
1555
1556         status = methods->update_sam_account(methods, sam_acct);
1557         if (!NT_STATUS_IS_OK(status)) {
1558                 PyErr_Format(py_pdb_error, "Unable to update sam account, (%d,%s)",
1559                                 NT_STATUS_V(status),
1560                                 get_friendly_nt_error_msg(status));
1561                 talloc_free(frame);
1562                 return NULL;
1563         }
1564
1565         Py_RETURN_NONE;
1566         talloc_free(frame);
1567 }
1568
1569 static PyObject *py_pdb_delete_sam_account(pytalloc_Object *self, PyObject *args)
1570 {
1571         TALLOC_CTX *frame = talloc_stackframe();
1572         NTSTATUS status;
1573         struct pdb_methods *methods;
1574         struct samu *sam_acct;
1575         PyObject *py_sam_acct;
1576
1577         if (!PyArg_ParseTuple(args, "O!:delete_sam_account", &PySamu, &py_sam_acct)) {
1578                 talloc_free(frame);
1579                 return NULL;
1580         }
1581
1582         methods = pytalloc_get_ptr(self);
1583
1584         sam_acct = pytalloc_get_ptr(py_sam_acct);
1585
1586         status = methods->delete_sam_account(methods, sam_acct);
1587         if (!NT_STATUS_IS_OK(status)) {
1588                 PyErr_Format(py_pdb_error, "Unable to delete sam account, (%d,%s)",
1589                                 NT_STATUS_V(status),
1590                                 get_friendly_nt_error_msg(status));
1591                 talloc_free(frame);
1592                 return NULL;
1593         }
1594
1595         Py_RETURN_NONE;
1596         talloc_free(frame);
1597 }
1598
1599 static PyObject *py_pdb_rename_sam_account(pytalloc_Object *self, PyObject *args)
1600 {
1601         TALLOC_CTX *frame = talloc_stackframe();
1602         NTSTATUS status;
1603         struct pdb_methods *methods;
1604         struct samu *sam_acct;
1605         const char *new_username;
1606         PyObject *py_sam_acct;
1607
1608         if (!PyArg_ParseTuple(args, "O!s:rename_sam_account", &PySamu, &py_sam_acct,
1609                                         &new_username)) {
1610                 talloc_free(frame);
1611                 return NULL;
1612         }
1613
1614         methods = pytalloc_get_ptr(self);
1615
1616         sam_acct = pytalloc_get_ptr(py_sam_acct);
1617
1618         status = methods->rename_sam_account(methods, sam_acct, new_username);
1619         if (!NT_STATUS_IS_OK(status)) {
1620                 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
1621                                 NT_STATUS_V(status),
1622                                 get_friendly_nt_error_msg(status));
1623                 talloc_free(frame);
1624                 return NULL;
1625         }
1626
1627         Py_RETURN_NONE;
1628         talloc_free(frame);
1629 }
1630
1631
1632 static PyObject *py_pdb_getgrsid(pytalloc_Object *self, PyObject *args)
1633 {
1634         TALLOC_CTX *frame = talloc_stackframe();
1635         NTSTATUS status;
1636         struct pdb_methods *methods;
1637         GROUP_MAP *group_map;
1638         struct dom_sid *domain_sid;
1639         PyObject *py_domain_sid, *py_group_map;
1640
1641         if (!PyArg_ParseTuple(args, "O!:getgrsid", dom_sid_Type, &py_domain_sid)) {
1642                 talloc_free(frame);
1643                 return NULL;
1644         }
1645
1646         methods = pytalloc_get_ptr(self);
1647
1648         domain_sid = pytalloc_get_ptr(py_domain_sid);
1649
1650         py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1651         if (py_group_map == NULL) {
1652                 PyErr_NoMemory();
1653                 talloc_free(frame);
1654                 return NULL;
1655         }
1656
1657         group_map = pytalloc_get_ptr(py_group_map);
1658
1659         status = methods->getgrsid(methods, group_map, *domain_sid);
1660         if (!NT_STATUS_IS_OK(status)) {
1661                 PyErr_Format(py_pdb_error, "Unable to get group information by sid, (%d,%s)",
1662                                 NT_STATUS_V(status),
1663                                 get_friendly_nt_error_msg(status));
1664                 talloc_free(frame);
1665                 return NULL;
1666         }
1667
1668         talloc_free(frame);
1669         return py_group_map;
1670 }
1671
1672
1673 static PyObject *py_pdb_getgrgid(pytalloc_Object *self, PyObject *args)
1674 {
1675         TALLOC_CTX *frame = talloc_stackframe();
1676         NTSTATUS status;
1677         struct pdb_methods *methods;
1678         GROUP_MAP *group_map;
1679         PyObject *py_group_map;
1680         unsigned int gid_value;
1681
1682         if (!PyArg_ParseTuple(args, "I:getgrgid", &gid_value)) {
1683                 talloc_free(frame);
1684                 return NULL;
1685         }
1686
1687         methods = pytalloc_get_ptr(self);
1688
1689         py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1690         if (py_group_map == NULL) {
1691                 PyErr_NoMemory();
1692                 talloc_free(frame);
1693                 return NULL;
1694         }
1695
1696         group_map = pytalloc_get_ptr(py_group_map);
1697
1698         status = methods->getgrgid(methods, group_map, gid_value);
1699         if (!NT_STATUS_IS_OK(status)) {
1700                 PyErr_Format(py_pdb_error, "Unable to get group information by gid, (%d,%s)",
1701                                 NT_STATUS_V(status),
1702                                 get_friendly_nt_error_msg(status));
1703                 talloc_free(frame);
1704                 return NULL;
1705         }
1706
1707         talloc_free(frame);
1708         return py_group_map;
1709 }
1710
1711
1712 static PyObject *py_pdb_getgrnam(pytalloc_Object *self, PyObject *args)
1713 {
1714         TALLOC_CTX *frame = talloc_stackframe();
1715         NTSTATUS status;
1716         struct pdb_methods *methods;
1717         GROUP_MAP *group_map;
1718         PyObject *py_group_map;
1719         const char *groupname;
1720
1721         if (!PyArg_ParseTuple(args, "s:getgrnam", &groupname)) {
1722                 talloc_free(frame);
1723                 return NULL;
1724         }
1725
1726         methods = pytalloc_get_ptr(self);
1727
1728         py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1729         if (py_group_map == NULL) {
1730                 PyErr_NoMemory();
1731                 talloc_free(frame);
1732                 return NULL;
1733         }
1734
1735         group_map = pytalloc_get_ptr(py_group_map);
1736
1737         status = methods->getgrnam(methods, group_map, groupname);
1738         if (!NT_STATUS_IS_OK(status)) {
1739                 PyErr_Format(py_pdb_error, "Unable to get group information by name, (%d,%s)",
1740                                 NT_STATUS_V(status),
1741                                 get_friendly_nt_error_msg(status));
1742                 talloc_free(frame);
1743                 return NULL;
1744         }
1745
1746         talloc_free(frame);
1747         return py_group_map;
1748 }
1749
1750
1751 static PyObject *py_pdb_create_dom_group(pytalloc_Object *self, PyObject *args)
1752 {
1753         TALLOC_CTX *frame = talloc_stackframe();
1754         NTSTATUS status;
1755         struct pdb_methods *methods;
1756         const char *groupname;
1757         uint32_t group_rid;
1758
1759         if (!PyArg_ParseTuple(args, "s:create_dom_group", &groupname)) {
1760                 talloc_free(frame);
1761                 return NULL;
1762         }
1763
1764         methods = pytalloc_get_ptr(self);
1765
1766         status = methods->create_dom_group(methods, frame, groupname, &group_rid);
1767         if (!NT_STATUS_IS_OK(status)) {
1768                 PyErr_Format(py_pdb_error, "Unable to create domain group (%s), (%d,%s)",
1769                                 groupname,
1770                                 NT_STATUS_V(status),
1771                                 get_friendly_nt_error_msg(status));
1772                 talloc_free(frame);
1773                 return NULL;
1774         }
1775
1776         talloc_free(frame);
1777         return PyInt_FromLong(group_rid);
1778 }
1779
1780
1781 static PyObject *py_pdb_delete_dom_group(pytalloc_Object *self, PyObject *args)
1782 {
1783         TALLOC_CTX *frame = talloc_stackframe();
1784         NTSTATUS status;
1785         struct pdb_methods *methods;
1786         unsigned int group_rid;
1787
1788         if (!PyArg_ParseTuple(args, "I:delete_dom_group", &group_rid)) {
1789                 talloc_free(frame);
1790                 return NULL;
1791         }
1792
1793         methods = pytalloc_get_ptr(self);
1794
1795         status = methods->delete_dom_group(methods, frame, group_rid);
1796         if (!NT_STATUS_IS_OK(status)) {
1797                 PyErr_Format(py_pdb_error, "Unable to delete domain group (rid=%d), (%d,%s)",
1798                                 group_rid,
1799                                 NT_STATUS_V(status),
1800                                 get_friendly_nt_error_msg(status));
1801                 talloc_free(frame);
1802                 return NULL;
1803         }
1804
1805         Py_RETURN_NONE;
1806         talloc_free(frame);
1807 }
1808
1809
1810 static PyObject *py_pdb_add_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1811 {
1812         TALLOC_CTX *frame = talloc_stackframe();
1813         NTSTATUS status;
1814         struct pdb_methods *methods;
1815         PyObject *py_group_map;
1816         GROUP_MAP *group_map;
1817
1818         if (!PyArg_ParseTuple(args, "O!:add_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1819                 talloc_free(frame);
1820                 return NULL;
1821         }
1822
1823         methods = pytalloc_get_ptr(self);
1824
1825         group_map = pytalloc_get_ptr(py_group_map);
1826
1827         status = methods->add_group_mapping_entry(methods, group_map);
1828         if (!NT_STATUS_IS_OK(status)) {
1829                 PyErr_Format(py_pdb_error, "Unable to add group mapping entry, (%d,%s)",
1830                                 NT_STATUS_V(status),
1831                                 get_friendly_nt_error_msg(status));
1832                 talloc_free(frame);
1833                 return NULL;
1834         }
1835
1836         Py_RETURN_NONE;
1837         talloc_free(frame);
1838 }
1839
1840
1841 static PyObject *py_pdb_update_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1842 {
1843         TALLOC_CTX *frame = talloc_stackframe();
1844         NTSTATUS status;
1845         struct pdb_methods *methods;
1846         PyObject *py_group_map;
1847         GROUP_MAP *group_map;
1848
1849         if (!PyArg_ParseTuple(args, "O!:update_group_mapping_entry", &PyGroupmap, &py_group_map)) {
1850                 talloc_free(frame);
1851                 return NULL;
1852         }
1853
1854         methods = pytalloc_get_ptr(self);
1855
1856         group_map = pytalloc_get_ptr(py_group_map);
1857
1858         status = methods->update_group_mapping_entry(methods, group_map);
1859         if (!NT_STATUS_IS_OK(status)) {
1860                 PyErr_Format(py_pdb_error, "Unable to update group mapping entry, (%d,%s)",
1861                                 NT_STATUS_V(status),
1862                                 get_friendly_nt_error_msg(status));
1863                 talloc_free(frame);
1864                 return NULL;
1865         }
1866
1867         Py_RETURN_NONE;
1868         talloc_free(frame);
1869 }
1870
1871
1872 static PyObject *py_pdb_delete_group_mapping_entry(pytalloc_Object *self, PyObject *args)
1873 {
1874         TALLOC_CTX *frame = talloc_stackframe();
1875         NTSTATUS status;
1876         struct pdb_methods *methods;
1877         PyObject *py_group_sid;
1878         struct dom_sid *group_sid;
1879
1880         if (!PyArg_ParseTuple(args, "O!:delete_group_mapping_entry", dom_sid_Type, &py_group_sid)) {
1881                 talloc_free(frame);
1882                 return NULL;
1883         }
1884
1885         methods = pytalloc_get_ptr(self);
1886
1887         group_sid = pytalloc_get_ptr(py_group_sid);
1888
1889         status = methods->delete_group_mapping_entry(methods, *group_sid);
1890         if (!NT_STATUS_IS_OK(status)) {
1891                 PyErr_Format(py_pdb_error, "Unable to delete group mapping entry, (%d,%s)",
1892                                 NT_STATUS_V(status),
1893                                 get_friendly_nt_error_msg(status));
1894                 talloc_free(frame);
1895                 return NULL;
1896         }
1897
1898         Py_RETURN_NONE;
1899         talloc_free(frame);
1900 }
1901
1902
1903 static PyObject *py_pdb_enum_group_mapping(pytalloc_Object *self, PyObject *args)
1904 {
1905         TALLOC_CTX *frame = talloc_stackframe();
1906         NTSTATUS status;
1907         struct pdb_methods *methods;
1908         enum lsa_SidType sid_name_use;
1909         int lsa_sidtype_value = SID_NAME_UNKNOWN;
1910         int unix_only = 0;
1911         PyObject *py_domain_sid;
1912         struct dom_sid *domain_sid = NULL;
1913         GROUP_MAP **gmap = NULL;
1914         GROUP_MAP *group_map;
1915         size_t num_entries;
1916         PyObject *py_gmap_list, *py_group_map;
1917         int i;
1918
1919         py_domain_sid = Py_None;
1920         Py_INCREF(Py_None);
1921
1922         if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
1923                                         &lsa_sidtype_value, &unix_only)) {
1924                 talloc_free(frame);
1925                 return NULL;
1926         }
1927
1928         methods = pytalloc_get_ptr(self);
1929
1930         sid_name_use = lsa_sidtype_value;
1931
1932         if (py_domain_sid != Py_None) {
1933                 domain_sid = pytalloc_get_ptr(py_domain_sid);
1934         }
1935
1936         status = methods->enum_group_mapping(methods, domain_sid, sid_name_use,
1937                                                 &gmap, &num_entries, unix_only);
1938         if (!NT_STATUS_IS_OK(status)) {
1939                 PyErr_Format(py_pdb_error, "Unable to enumerate group mappings, (%d,%s)",
1940                                 NT_STATUS_V(status),
1941                                 get_friendly_nt_error_msg(status));
1942                 talloc_free(frame);
1943                 return NULL;
1944         }
1945
1946         py_gmap_list = PyList_New(0);
1947         if (py_gmap_list == NULL) {
1948                 PyErr_NoMemory();
1949                 talloc_free(frame);
1950                 return NULL;
1951         }
1952
1953         for(i=0; i<num_entries; i++) {
1954                 py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
1955                 if (py_group_map) {
1956                         group_map = pytalloc_get_ptr(py_group_map);
1957                         *group_map = *gmap[i];
1958                         talloc_steal(group_map, gmap[i]->nt_name);
1959                         talloc_steal(group_map, gmap[i]->comment);
1960
1961                         PyList_Append(py_gmap_list, py_group_map);
1962                 }
1963         }
1964
1965         talloc_free(gmap);
1966
1967         talloc_free(frame);
1968         return py_gmap_list;
1969 }
1970
1971
1972 static PyObject *py_pdb_enum_group_members(pytalloc_Object *self, PyObject *args)
1973 {
1974         TALLOC_CTX *frame = talloc_stackframe();
1975         NTSTATUS status;
1976         struct pdb_methods *methods;
1977         PyObject *py_group_sid;
1978         struct dom_sid *group_sid;
1979         uint32_t *member_rids;
1980         size_t num_members;
1981         PyObject *py_sid_list;
1982         struct dom_sid *domain_sid, *member_sid;
1983         int i;
1984
1985         if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
1986                 talloc_free(frame);
1987                 return NULL;
1988         }
1989
1990         methods = pytalloc_get_ptr(self);
1991
1992         group_sid = pytalloc_get_ptr(py_group_sid);
1993
1994         status = methods->enum_group_members(methods, frame, group_sid,
1995                                                 &member_rids, &num_members);
1996         if (!NT_STATUS_IS_OK(status)) {
1997                 PyErr_Format(py_pdb_error, "Unable to enumerate group members, (%d,%s)",
1998                                 NT_STATUS_V(status),
1999                                 get_friendly_nt_error_msg(status));
2000                 talloc_free(frame);
2001                 return NULL;
2002         }
2003
2004         py_sid_list = PyList_New(0);
2005         if (py_sid_list == NULL) {
2006                 PyErr_NoMemory();
2007                 talloc_free(frame);
2008                 return NULL;
2009         }
2010
2011         domain_sid = get_global_sam_sid();
2012
2013         for(i=0; i<num_members; i++) {
2014                 member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
2015                 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
2016         }
2017
2018         talloc_free(frame);
2019         return py_sid_list;
2020 }
2021
2022
2023 static PyObject *py_pdb_enum_group_memberships(pytalloc_Object *self, PyObject *args)
2024 {
2025         TALLOC_CTX *frame = talloc_stackframe();
2026         NTSTATUS status;
2027         struct pdb_methods *methods;
2028         int i;
2029
2030         struct samu *sam_acct;
2031         PyObject *py_sam_acct;
2032         PyObject *py_sid_list;
2033         struct dom_sid *user_group_sids = NULL;
2034         gid_t *user_group_ids = NULL;
2035         uint32_t num_groups = 0;
2036
2037         if (!PyArg_ParseTuple(args, "O!:enum_group_memberships", &PySamu, &py_sam_acct)) {
2038                 talloc_free(frame);
2039                 return NULL;
2040         }
2041
2042         methods = pytalloc_get_ptr(self);
2043
2044         sam_acct = pytalloc_get_ptr(py_sam_acct);
2045
2046         status = methods->enum_group_memberships(methods, frame, sam_acct,
2047                                                  &user_group_sids, &user_group_ids, &num_groups);
2048         if (!NT_STATUS_IS_OK(status)) {
2049                 PyErr_Format(py_pdb_error, "Unable to enumerate group memberships, (%d,%s)",
2050                                 NT_STATUS_V(status),
2051                                 get_friendly_nt_error_msg(status));
2052                 talloc_free(frame);
2053                 return NULL;
2054         }
2055
2056         py_sid_list = PyList_New(0);
2057         if (py_sid_list == NULL) {
2058                 PyErr_NoMemory();
2059                 talloc_free(frame);
2060                 return NULL;
2061         }
2062
2063         for(i=0; i<num_groups; i++) {
2064                 PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
2065         }
2066
2067         talloc_free(frame);
2068         return py_sid_list;
2069 }
2070
2071
2072 static PyObject *py_pdb_add_groupmem(pytalloc_Object *self, PyObject *args)
2073 {
2074         TALLOC_CTX *frame = talloc_stackframe();
2075         NTSTATUS status;
2076         struct pdb_methods *methods;
2077         uint32_t group_rid, member_rid;
2078
2079         if (!PyArg_ParseTuple(args, "II:add_groupmem", &group_rid, &member_rid)) {
2080                 talloc_free(frame);
2081                 return NULL;
2082         }
2083
2084         methods = pytalloc_get_ptr(self);
2085
2086         status = methods->add_groupmem(methods, frame, group_rid, member_rid);
2087         if (!NT_STATUS_IS_OK(status)) {
2088                 PyErr_Format(py_pdb_error, "Unable to add group member, (%d,%s)",
2089                                 NT_STATUS_V(status),
2090                                 get_friendly_nt_error_msg(status));
2091                 talloc_free(frame);
2092                 return NULL;
2093         }
2094
2095         Py_RETURN_NONE;
2096         talloc_free(frame);
2097 }
2098
2099
2100 static PyObject *py_pdb_del_groupmem(pytalloc_Object *self, PyObject *args)
2101 {
2102         TALLOC_CTX *frame = talloc_stackframe();
2103         NTSTATUS status;
2104         struct pdb_methods *methods;
2105         uint32_t group_rid, member_rid;
2106
2107         if (!PyArg_ParseTuple(args, "II:del_groupmem", &group_rid, &member_rid)) {
2108                 talloc_free(frame);
2109                 return NULL;
2110         }
2111
2112         methods = pytalloc_get_ptr(self);
2113
2114         status = methods->del_groupmem(methods, frame, group_rid, member_rid);
2115         if (!NT_STATUS_IS_OK(status)) {
2116                 PyErr_Format(py_pdb_error, "Unable to rename sam account, (%d,%s)",
2117                                 NT_STATUS_V(status),
2118                                 get_friendly_nt_error_msg(status));
2119                 talloc_free(frame);
2120                 return NULL;
2121         }
2122
2123         Py_RETURN_NONE;
2124         talloc_free(frame);
2125 }
2126
2127
2128 static PyObject *py_pdb_create_alias(pytalloc_Object *self, PyObject *args)
2129 {
2130         TALLOC_CTX *frame = talloc_stackframe();
2131         NTSTATUS status;
2132         struct pdb_methods *methods;
2133         const char *alias_name;
2134         uint32_t rid;
2135
2136         if (!PyArg_ParseTuple(args, "s:create_alias", &alias_name)) {
2137                 talloc_free(frame);
2138                 return NULL;
2139         }
2140
2141         methods = pytalloc_get_ptr(self);
2142
2143         status = methods->create_alias(methods, alias_name, &rid);
2144         if (!NT_STATUS_IS_OK(status)) {
2145                 PyErr_Format(py_pdb_error, "Unable to create alias (%s), (%d,%s)",
2146                                 alias_name,
2147                                 NT_STATUS_V(status),
2148                                 get_friendly_nt_error_msg(status));
2149                 talloc_free(frame);
2150                 return NULL;
2151         }
2152
2153         talloc_free(frame);
2154         return PyInt_FromLong(rid);
2155 }
2156
2157
2158 static PyObject *py_pdb_delete_alias(pytalloc_Object *self, PyObject *args)
2159 {
2160         TALLOC_CTX *frame = talloc_stackframe();
2161         NTSTATUS status;
2162         struct pdb_methods *methods;
2163         PyObject *py_alias_sid;
2164         struct dom_sid *alias_sid;
2165
2166         if (!PyArg_ParseTuple(args, "O!:delete_alias", dom_sid_Type, &py_alias_sid)) {
2167                 talloc_free(frame);
2168                 return NULL;
2169         }
2170
2171         methods = pytalloc_get_ptr(self);
2172
2173         alias_sid = pytalloc_get_ptr(py_alias_sid);
2174
2175         status = methods->delete_alias(methods, alias_sid);
2176         if (!NT_STATUS_IS_OK(status)) {
2177                 PyErr_Format(py_pdb_error, "Unable to delete alias, (%d,%s)",
2178                                 NT_STATUS_V(status),
2179                                 get_friendly_nt_error_msg(status));
2180                 talloc_free(frame);
2181                 return NULL;
2182         }
2183
2184         Py_RETURN_NONE;
2185         talloc_free(frame);
2186 }
2187
2188
2189 static PyObject *py_pdb_get_aliasinfo(pytalloc_Object *self, PyObject *args)
2190 {
2191         TALLOC_CTX *frame = talloc_stackframe();
2192         NTSTATUS status;
2193         struct pdb_methods *methods;
2194         PyObject *py_alias_sid;
2195         struct dom_sid *alias_sid;
2196         struct acct_info *alias_info;
2197         PyObject *py_alias_info;
2198
2199         if (!PyArg_ParseTuple(args, "O!:get_aliasinfo", dom_sid_Type, &py_alias_sid)) {
2200                 talloc_free(frame);
2201                 return NULL;
2202         }
2203
2204         methods = pytalloc_get_ptr(self);
2205
2206         alias_sid = pytalloc_get_ptr(py_alias_sid);
2207
2208         alias_info = talloc_zero(frame, struct acct_info);
2209         if (!alias_info) {
2210                 PyErr_NoMemory();
2211                 talloc_free(frame);
2212                 return NULL;
2213         }
2214
2215         status = methods->get_aliasinfo(methods, alias_sid, alias_info);
2216         if (!NT_STATUS_IS_OK(status)) {
2217                 PyErr_Format(py_pdb_error, "Unable to get alias information, (%d,%s)",
2218                                 NT_STATUS_V(status),
2219                                 get_friendly_nt_error_msg(status));
2220                 talloc_free(frame);
2221                 return NULL;
2222         }
2223
2224         py_alias_info = PyDict_New();
2225         if (py_alias_info == NULL) {
2226                 PyErr_NoMemory();
2227                 talloc_free(frame);
2228                 return NULL;
2229         }
2230
2231         PyDict_SetItemString(py_alias_info, "acct_name",
2232                              PyString_FromString(alias_info->acct_name));
2233         PyDict_SetItemString(py_alias_info, "acct_desc",
2234                              PyString_FromString(alias_info->acct_desc));
2235         PyDict_SetItemString(py_alias_info, "rid",
2236                              PyInt_FromLong(alias_info->rid));
2237
2238         talloc_free(frame);
2239         return py_alias_info;
2240 }
2241
2242
2243 static PyObject *py_pdb_set_aliasinfo(pytalloc_Object *self, PyObject *args)
2244 {
2245         TALLOC_CTX *frame = talloc_stackframe();
2246         NTSTATUS status;
2247         struct pdb_methods *methods;
2248         PyObject *py_alias_sid, *py_alias_info;
2249         struct dom_sid *alias_sid;
2250         struct acct_info alias_info;
2251
2252         if (!PyArg_ParseTuple(args, "O!O:set_alias_info", dom_sid_Type, &py_alias_sid,
2253                                 &py_alias_info)) {
2254                 talloc_free(frame);
2255                 return NULL;
2256         }
2257
2258         methods = pytalloc_get_ptr(self);
2259
2260         alias_sid = pytalloc_get_ptr(py_alias_sid);
2261
2262         alias_info.acct_name = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
2263         if (alias_info.acct_name == NULL) {
2264                 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2265                 talloc_free(frame);
2266                 return NULL;
2267         }
2268         alias_info.acct_desc = talloc_strdup(frame, PyString_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
2269         if (alias_info.acct_desc == NULL) {
2270                 PyErr_Format(py_pdb_error, "Unable to allocate memory");
2271                 talloc_free(frame);
2272                 return NULL;
2273         }
2274
2275         status = methods->set_aliasinfo(methods, alias_sid, &alias_info);
2276         if (!NT_STATUS_IS_OK(status)) {
2277                 PyErr_Format(py_pdb_error, "Unable to set alias information, (%d,%s)",
2278                                 NT_STATUS_V(status),
2279                                 get_friendly_nt_error_msg(status));
2280                 talloc_free(frame);
2281                 return NULL;
2282         }
2283
2284         Py_RETURN_NONE;
2285         talloc_free(frame);
2286 }
2287
2288
2289 static PyObject *py_pdb_add_aliasmem(pytalloc_Object *self, PyObject *args)
2290 {
2291         TALLOC_CTX *frame = talloc_stackframe();
2292         NTSTATUS status;
2293         struct pdb_methods *methods;
2294         PyObject *py_alias_sid, *py_member_sid;
2295         struct dom_sid *alias_sid, *member_sid;
2296
2297         if (!PyArg_ParseTuple(args, "O!O!:add_aliasmem", dom_sid_Type, &py_alias_sid,
2298                                         dom_sid_Type, &py_member_sid)) {
2299                 talloc_free(frame);
2300                 return NULL;
2301         }
2302
2303         methods = pytalloc_get_ptr(self);
2304
2305         alias_sid = pytalloc_get_ptr(py_alias_sid);
2306         member_sid = pytalloc_get_ptr(py_member_sid);
2307
2308         status = methods->add_aliasmem(methods, alias_sid, member_sid);
2309         if (!NT_STATUS_IS_OK(status)) {
2310                 PyErr_Format(py_pdb_error, "Unable to add member to alias, (%d,%s)",
2311                                 NT_STATUS_V(status),
2312                                 get_friendly_nt_error_msg(status));
2313                 talloc_free(frame);
2314                 return NULL;
2315         }
2316
2317         Py_RETURN_NONE;
2318         talloc_free(frame);
2319 }
2320
2321
2322 static PyObject *py_pdb_del_aliasmem(pytalloc_Object *self, PyObject *args)
2323 {
2324         TALLOC_CTX *frame = talloc_stackframe();
2325         NTSTATUS status;
2326         struct pdb_methods *methods;
2327         PyObject *py_alias_sid, *py_member_sid;
2328         const struct dom_sid *alias_sid, *member_sid;
2329
2330         if (!PyArg_ParseTuple(args, "O!O!:del_aliasmem", dom_sid_Type, &py_alias_sid,
2331                                         dom_sid_Type, &py_member_sid)) {
2332                 talloc_free(frame);
2333                 return NULL;
2334         }
2335
2336         methods = pytalloc_get_ptr(self);
2337
2338         alias_sid = pytalloc_get_ptr(py_alias_sid);
2339         member_sid = pytalloc_get_ptr(py_member_sid);
2340
2341         status = methods->del_aliasmem(methods, alias_sid, member_sid);
2342         if (!NT_STATUS_IS_OK(status)) {
2343                 PyErr_Format(py_pdb_error, "Unable to delete member from alias, (%d,%s)",
2344                                 NT_STATUS_V(status),
2345                                 get_friendly_nt_error_msg(status));
2346                 talloc_free(frame);
2347                 return NULL;
2348         }
2349
2350         Py_RETURN_NONE;
2351         talloc_free(frame);
2352 }
2353
2354
2355 static PyObject *py_pdb_enum_aliasmem(pytalloc_Object *self, PyObject *args)
2356 {
2357         TALLOC_CTX *frame = talloc_stackframe();
2358         NTSTATUS status;
2359         struct pdb_methods *methods;
2360         PyObject *py_alias_sid;
2361         struct dom_sid *alias_sid, *member_sid, *tmp_sid;
2362         PyObject *py_member_list, *py_member_sid;
2363         size_t num_members;
2364         int i;
2365
2366         if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
2367                 talloc_free(frame);
2368                 return NULL;
2369         }
2370
2371         methods = pytalloc_get_ptr(self);
2372
2373         alias_sid = pytalloc_get_ptr(py_alias_sid);
2374
2375         status = methods->enum_aliasmem(methods, alias_sid, frame, &member_sid, &num_members);
2376         if (!NT_STATUS_IS_OK(status)) {
2377                 PyErr_Format(py_pdb_error, "Unable to enumerate members for alias, (%d,%s)",
2378                                 NT_STATUS_V(status),
2379                                 get_friendly_nt_error_msg(status));
2380                 talloc_free(frame);
2381                 return NULL;
2382         }
2383
2384         py_member_list = PyList_New(0);
2385         if (py_member_list == NULL) {
2386                 PyErr_NoMemory();
2387                 talloc_free(frame);
2388                 return NULL;
2389         }
2390
2391         for(i=0; i<num_members; i++) {
2392                 py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
2393                 if (py_member_sid == NULL) {
2394                         PyErr_NoMemory();
2395                         talloc_free(frame);
2396                         return NULL;
2397                 }
2398                 tmp_sid = pytalloc_get_ptr(py_member_sid);
2399                 *tmp_sid = member_sid[i];
2400                 PyList_Append(py_member_list, py_member_sid);
2401         }
2402
2403         talloc_free(frame);
2404         return py_member_list;
2405 }
2406
2407
2408 static PyObject *py_pdb_get_account_policy(pytalloc_Object *self)
2409 {
2410         TALLOC_CTX *frame = talloc_stackframe();
2411         NTSTATUS status;
2412         struct pdb_methods *methods;
2413         PyObject *py_acct_policy;
2414         uint32_t value;
2415         const char **names;
2416         int count, i;
2417         enum pdb_policy_type type;
2418
2419         methods = pytalloc_get_ptr(self);
2420
2421         py_acct_policy = PyDict_New();
2422         if (py_acct_policy == NULL) {
2423                 PyErr_NoMemory();
2424                 talloc_free(frame);
2425                 return NULL;
2426         }
2427
2428         account_policy_names_list(frame, &names, &count);
2429         for (i=0; i<count; i++) {
2430                 type = account_policy_name_to_typenum(names[i]);
2431                 status = methods->get_account_policy(methods, type, &value);
2432                 if (NT_STATUS_IS_OK(status)) {
2433                         PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
2434                 }
2435         }
2436
2437         talloc_free(frame);
2438         return py_acct_policy;
2439 }
2440
2441
2442 static PyObject *py_pdb_set_account_policy(pytalloc_Object *self, PyObject *args)
2443 {
2444         TALLOC_CTX *frame = talloc_stackframe();
2445         NTSTATUS status;
2446         struct pdb_methods *methods;
2447         PyObject *py_acct_policy, *py_value;
2448         const char **names;
2449         int count, i;
2450         enum pdb_policy_type type;
2451
2452         if (!PyArg_ParseTuple(args, "O!:set_account_policy", PyDict_Type, &py_acct_policy)) {
2453                 talloc_free(frame);
2454                 return NULL;
2455         }
2456
2457         methods = pytalloc_get_ptr(self);
2458
2459         account_policy_names_list(frame, &names, &count);
2460         for (i=0; i<count; i++) {
2461                 if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
2462                         type = account_policy_name_to_typenum(names[i]);
2463                         status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
2464                         if (!NT_STATUS_IS_OK(status)) {
2465                                 PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
2466                                                 names[i],
2467                                                 NT_STATUS_V(status),
2468                                                 get_friendly_nt_error_msg(status));
2469                         }
2470                 }
2471         }
2472
2473         Py_RETURN_NONE;
2474         talloc_free(frame);
2475 }
2476
2477 static PyObject *py_pdb_search_users(pytalloc_Object *self, PyObject *args)
2478 {
2479         TALLOC_CTX *frame = talloc_stackframe();
2480         struct pdb_methods *methods;
2481         unsigned int acct_flags;
2482         struct pdb_search *search;
2483         struct samr_displayentry *entry;
2484         PyObject *py_userlist, *py_dict;
2485
2486         if (!PyArg_ParseTuple(args, "I:search_users", &acct_flags)) {
2487                 talloc_free(frame);
2488                 return NULL;
2489         }
2490
2491         methods = pytalloc_get_ptr(self);
2492
2493         search = talloc_zero(frame, struct pdb_search);
2494         if (search == NULL) {
2495                 PyErr_NoMemory();
2496                 talloc_free(frame);
2497                 return NULL;
2498         }
2499
2500         if (!methods->search_users(methods, search, acct_flags)) {
2501                 PyErr_Format(py_pdb_error, "Unable to search users");
2502                 talloc_free(frame);
2503                 return NULL;
2504         }
2505
2506         entry = talloc_zero(frame, struct samr_displayentry);
2507         if (entry == NULL) {
2508                 PyErr_NoMemory();
2509                 talloc_free(frame);
2510                 return NULL;
2511         }
2512
2513         py_userlist = PyList_New(0);
2514         if (py_userlist == NULL) {
2515                 PyErr_NoMemory();
2516                 talloc_free(frame);
2517                 return NULL;
2518         }
2519
2520         while (search->next_entry(search, entry)) {
2521                 py_dict = PyDict_New();
2522                 if (py_dict == NULL) {
2523                         PyErr_NoMemory();
2524                 } else {
2525                         PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2526                         PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2527                         PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2528                         PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2529                         PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2530                         PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2531                         PyList_Append(py_userlist, py_dict);
2532                 }
2533         }
2534         search->search_end(search);
2535
2536         talloc_free(frame);
2537         return py_userlist;
2538 }
2539
2540
2541 static PyObject *py_pdb_search_groups(pytalloc_Object *self)
2542 {
2543         TALLOC_CTX *frame = talloc_stackframe();
2544         struct pdb_methods *methods;
2545         struct pdb_search *search;
2546         struct samr_displayentry *entry;
2547         PyObject *py_grouplist, *py_dict;
2548
2549         methods = pytalloc_get_ptr(self);
2550
2551         search = talloc_zero(frame, struct pdb_search);
2552         if (search == NULL) {
2553                 PyErr_NoMemory();
2554                 talloc_free(frame);
2555                 return NULL;
2556         }
2557
2558         if (!methods->search_groups(methods, search)) {
2559                 PyErr_Format(py_pdb_error, "Unable to search groups");
2560                 talloc_free(frame);
2561                 return NULL;
2562         }
2563
2564         entry = talloc_zero(frame, struct samr_displayentry);
2565         if (entry == NULL) {
2566                 PyErr_NoMemory();
2567                 talloc_free(frame);
2568                 return NULL;
2569         }
2570
2571         py_grouplist = PyList_New(0);
2572         if (py_grouplist == NULL) {
2573                 PyErr_NoMemory();
2574                 talloc_free(frame);
2575                 return NULL;
2576         }
2577
2578         while (search->next_entry(search, entry)) {
2579                 py_dict = PyDict_New();
2580                 if (py_dict == NULL) {
2581                         PyErr_NoMemory();
2582                 } else {
2583                         PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2584                         PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2585                         PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2586                         PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2587                         PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2588                         PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2589                         PyList_Append(py_grouplist, py_dict);
2590                 }
2591         }
2592         search->search_end(search);
2593
2594         talloc_free(frame);
2595         return py_grouplist;
2596 }
2597
2598
2599 static PyObject *py_pdb_search_aliases(pytalloc_Object *self, PyObject *args)
2600 {
2601         TALLOC_CTX *frame = talloc_stackframe();
2602         struct pdb_methods *methods;
2603         struct pdb_search *search;
2604         struct samr_displayentry *entry;
2605         PyObject *py_aliaslist, *py_dict;
2606         PyObject *py_domain_sid;
2607         struct dom_sid *domain_sid = NULL;
2608
2609         py_domain_sid = Py_None;
2610         Py_INCREF(Py_None);
2611
2612         if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
2613                 talloc_free(frame);
2614                 return NULL;
2615         }
2616
2617         methods = pytalloc_get_ptr(self);
2618
2619         if (py_domain_sid != Py_None) {
2620                 domain_sid = pytalloc_get_ptr(py_domain_sid);
2621         }
2622
2623         search = talloc_zero(frame, struct pdb_search);
2624         if (search == NULL) {
2625                 PyErr_NoMemory();
2626                 talloc_free(frame);
2627                 return NULL;
2628         }
2629
2630         if (!methods->search_aliases(methods, search, domain_sid)) {
2631                 PyErr_Format(py_pdb_error, "Unable to search aliases");
2632                 talloc_free(frame);
2633                 return NULL;
2634         }
2635
2636         entry = talloc_zero(frame, struct samr_displayentry);
2637         if (entry == NULL) {
2638                 PyErr_NoMemory();
2639                 talloc_free(frame);
2640                 return NULL;
2641         }
2642
2643         py_aliaslist = PyList_New(0);
2644         if (py_aliaslist == NULL) {
2645                 PyErr_NoMemory();
2646                 talloc_free(frame);
2647                 return NULL;
2648         }
2649
2650         while (search->next_entry(search, entry)) {
2651                 py_dict = PyDict_New();
2652                 if (py_dict == NULL) {
2653                         PyErr_NoMemory();
2654                 } else {
2655                         PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
2656                         PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
2657                         PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
2658                         PyDict_SetItemString(py_dict, "account_name", PyString_FromString(entry->account_name));
2659                         PyDict_SetItemString(py_dict, "fullname", PyString_FromString(entry->fullname));
2660                         PyDict_SetItemString(py_dict, "description", PyString_FromString(entry->description));
2661                         PyList_Append(py_aliaslist, py_dict);
2662                 }
2663         }
2664         search->search_end(search);
2665
2666         talloc_free(frame);
2667         return py_aliaslist;
2668 }
2669
2670
2671 static PyObject *py_pdb_uid_to_sid(pytalloc_Object *self, PyObject *args)
2672 {
2673         TALLOC_CTX *frame = talloc_stackframe();
2674         struct pdb_methods *methods;
2675         struct unixid id;
2676         unsigned int uid;
2677         struct dom_sid user_sid, *copy_user_sid;
2678         PyObject *py_user_sid;
2679
2680         if (!PyArg_ParseTuple(args, "I:uid_to_sid", &uid)) {
2681                 talloc_free(frame);
2682                 return NULL;
2683         }
2684
2685         methods = pytalloc_get_ptr(self);
2686
2687         id.id = uid;
2688         id.type = ID_TYPE_UID;
2689
2690         if (!methods->id_to_sid(methods, &id, &user_sid)) {
2691                 PyErr_Format(py_pdb_error, "Unable to get sid for uid=%d", uid);
2692                 talloc_free(frame);
2693                 return NULL;
2694         }
2695
2696         copy_user_sid = dom_sid_dup(frame, &user_sid);
2697         if (copy_user_sid == NULL) {
2698                 PyErr_NoMemory();
2699                 talloc_free(frame);
2700                 return NULL;
2701         }
2702
2703         py_user_sid = pytalloc_steal(dom_sid_Type, copy_user_sid);
2704
2705         talloc_free(frame);
2706         return py_user_sid;
2707 }
2708
2709
2710 static PyObject *py_pdb_gid_to_sid(pytalloc_Object *self, PyObject *args)
2711 {
2712         TALLOC_CTX *frame = talloc_stackframe();
2713         struct pdb_methods *methods;
2714         struct unixid id;
2715         unsigned int gid;
2716         struct dom_sid group_sid, *copy_group_sid;
2717         PyObject *py_group_sid;
2718
2719         if (!PyArg_ParseTuple(args, "I:gid_to_sid", &gid)) {
2720                 talloc_free(frame);
2721                 return NULL;
2722         }
2723
2724         id.id = gid;
2725         id.type = ID_TYPE_GID;
2726
2727         methods = pytalloc_get_ptr(self);
2728
2729         if (!methods->id_to_sid(methods, &id, &group_sid)) {
2730                 PyErr_Format(py_pdb_error, "Unable to get sid for gid=%d", gid);
2731                 talloc_free(frame);
2732                 return NULL;
2733         }
2734
2735         copy_group_sid = dom_sid_dup(frame, &group_sid);
2736         if (copy_group_sid == NULL) {
2737                 PyErr_NoMemory();
2738                 talloc_free(frame);
2739                 return NULL;
2740         }
2741
2742         py_group_sid = pytalloc_steal(dom_sid_Type, copy_group_sid);
2743
2744         talloc_free(frame);
2745         return py_group_sid;
2746 }
2747
2748
2749 static PyObject *py_pdb_sid_to_id(pytalloc_Object *self, PyObject *args)
2750 {
2751         TALLOC_CTX *frame = talloc_stackframe();
2752         struct pdb_methods *methods;
2753         PyObject *py_sid;
2754         struct dom_sid *sid;
2755         struct unixid id;
2756
2757         if (!PyArg_ParseTuple(args, "O!:sid_to_id", dom_sid_Type, &py_sid)) {
2758                 talloc_free(frame);
2759                 return NULL;
2760         }
2761
2762         methods = pytalloc_get_ptr(self);
2763
2764         sid = pytalloc_get_ptr(py_sid);
2765
2766         if (!methods->sid_to_id(methods, sid, &id)) {
2767                 PyErr_Format(py_pdb_error, "Unable to get id for sid");
2768                 talloc_free(frame);
2769                 return NULL;
2770         }
2771
2772         talloc_free(frame);
2773         return Py_BuildValue("(II)", id.id, id.type);
2774 }
2775
2776
2777 static PyObject *py_pdb_new_rid(pytalloc_Object *self)
2778 {
2779         TALLOC_CTX *frame = talloc_stackframe();
2780         struct pdb_methods *methods;
2781         uint32_t rid;
2782
2783         methods = pytalloc_get_ptr(self);
2784
2785         if (!methods->new_rid(methods, &rid)) {
2786                 PyErr_Format(py_pdb_error, "Unable to get new rid");
2787                 talloc_free(frame);
2788                 return NULL;
2789         }
2790
2791         talloc_free(frame);
2792         return PyInt_FromLong(rid);
2793 }
2794
2795
2796 static PyObject *py_pdb_get_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2797 {
2798         TALLOC_CTX *frame = talloc_stackframe();
2799         struct pdb_methods *methods;
2800         const char *domain;
2801         char *pwd;
2802         struct dom_sid sid, *copy_sid;
2803         PyObject *py_sid;
2804         time_t last_set_time;
2805         PyObject *py_value;
2806
2807         if (!PyArg_ParseTuple(args, "s:get_trusteddom_pw", &domain)) {
2808                 talloc_free(frame);
2809                 return NULL;
2810         }
2811
2812         methods = pytalloc_get_ptr(self);
2813
2814         if (!methods->get_trusteddom_pw(methods, domain, &pwd, &sid, &last_set_time)) {
2815                 PyErr_Format(py_pdb_error, "Unable to get trusted domain password");
2816                 talloc_free(frame);
2817                 return NULL;
2818         }
2819
2820         copy_sid = dom_sid_dup(frame, &sid);
2821         if (copy_sid == NULL) {
2822                 PyErr_NoMemory();
2823                 talloc_free(frame);
2824                 return NULL;
2825         }
2826
2827         py_sid = pytalloc_steal(dom_sid_Type, copy_sid);
2828         if (py_sid == NULL) {
2829                 PyErr_NoMemory();
2830                 talloc_free(frame);
2831                 return NULL;
2832         }
2833
2834         py_value = PyDict_New();
2835         if (py_value == NULL) {
2836                 PyErr_NoMemory();
2837                 talloc_free(frame);
2838                 return NULL;
2839         }
2840
2841         PyDict_SetItemString(py_value, "pwd", PyString_FromString(pwd));
2842         PyDict_SetItemString(py_value, "sid", py_sid);
2843         PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
2844
2845         talloc_free(frame);
2846         return py_value;
2847 }
2848
2849
2850 static PyObject *py_pdb_set_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2851 {
2852         TALLOC_CTX *frame = talloc_stackframe();
2853         struct pdb_methods *methods;
2854         const char *domain;
2855         const char *pwd;
2856         const struct dom_sid *domain_sid;
2857         PyObject *py_domain_sid;
2858
2859         if (!PyArg_ParseTuple(args, "ssO!:set_trusteddom_pw", &domain, &pwd,
2860                                         dom_sid_Type, &py_domain_sid)) {
2861                 talloc_free(frame);
2862                 return NULL;
2863         }
2864
2865         methods = pytalloc_get_ptr(self);
2866
2867         domain_sid = pytalloc_get_ptr(py_domain_sid);
2868
2869         if (!methods->set_trusteddom_pw(methods, domain, pwd, domain_sid)) {
2870                 PyErr_Format(py_pdb_error, "Unable to set trusted domain password");
2871                 talloc_free(frame);
2872                 return NULL;
2873         }
2874
2875         Py_RETURN_NONE;
2876         talloc_free(frame);
2877 }
2878
2879
2880 static PyObject *py_pdb_del_trusteddom_pw(pytalloc_Object *self, PyObject *args)
2881 {
2882         TALLOC_CTX *frame = talloc_stackframe();
2883         struct pdb_methods *methods;
2884         const char *domain;
2885
2886         if (!PyArg_ParseTuple(args, "s:del_trusteddom_pw", &domain)) {
2887                 talloc_free(frame);
2888                 return NULL;
2889         }
2890
2891         methods = pytalloc_get_ptr(self);
2892
2893         if (!methods->del_trusteddom_pw(methods, domain)) {
2894                 PyErr_Format(py_pdb_error, "Unable to delete trusted domain password");
2895                 talloc_free(frame);
2896                 return NULL;
2897         }
2898
2899         Py_RETURN_NONE;
2900         talloc_free(frame);
2901 }
2902
2903
2904 static PyObject *py_pdb_enum_trusteddoms(pytalloc_Object *self)
2905 {
2906         TALLOC_CTX *frame = talloc_stackframe();
2907         NTSTATUS status;
2908         struct pdb_methods *methods;
2909         uint32_t num_domains;
2910         struct trustdom_info **domains;
2911         PyObject *py_domain_list, *py_dict;
2912         int i;
2913
2914         methods = pytalloc_get_ptr(self);
2915
2916         status = methods->enum_trusteddoms(methods, frame, &num_domains, &domains);
2917         if (!NT_STATUS_IS_OK(status)) {
2918                 PyErr_Format(py_pdb_error, "Unable to enumerate trusted domains, (%d,%s)",
2919                                 NT_STATUS_V(status),
2920                                 get_friendly_nt_error_msg(status));
2921                 talloc_free(frame);
2922                 return NULL;
2923         }
2924
2925         py_domain_list = PyList_New(0);
2926         if (py_domain_list == NULL) {
2927                 PyErr_NoMemory();
2928                 talloc_free(frame);
2929                 return NULL;
2930         }
2931
2932         for(i=0; i<num_domains; i++) {
2933                 py_dict = PyDict_New();
2934                 if (py_dict) {
2935                         PyDict_SetItemString(py_dict, "name",
2936                                         PyString_FromString(domains[i]->name));
2937                         PyDict_SetItemString(py_dict, "sid",
2938                                         pytalloc_steal(dom_sid_Type, &domains[i]->sid));
2939                 }
2940
2941                 PyList_Append(py_domain_list, py_dict);
2942         }
2943
2944         talloc_free(frame);
2945         return py_domain_list;
2946 }
2947
2948
2949 static PyObject *py_pdb_get_trusted_domain(pytalloc_Object *self, PyObject *args)
2950 {
2951         TALLOC_CTX *frame = talloc_stackframe();
2952         NTSTATUS status;
2953         struct pdb_methods *methods;
2954         const char *domain;
2955         struct pdb_trusted_domain *td;
2956         PyObject *py_domain_info;
2957
2958         if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
2959                 talloc_free(frame);
2960                 return NULL;
2961         }
2962
2963         methods = pytalloc_get_ptr(self);
2964
2965         status = methods->get_trusted_domain(methods, frame, domain, &td);
2966         if (!NT_STATUS_IS_OK(status)) {
2967                 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
2968                                 NT_STATUS_V(status),
2969                                 get_friendly_nt_error_msg(status));
2970                 talloc_free(frame);
2971                 return NULL;
2972         }
2973
2974         py_domain_info = PyDict_New();
2975         if (py_domain_info == NULL) {
2976                 PyErr_NoMemory();
2977                 talloc_free(frame);
2978                 return NULL;
2979         }
2980
2981         PyDict_SetItemString(py_domain_info, "domain_name",
2982                         PyString_FromString(td->domain_name));
2983         PyDict_SetItemString(py_domain_info, "netbios_name",
2984                         PyString_FromString(td->netbios_name));
2985         PyDict_SetItemString(py_domain_info, "security_identifier",
2986                         pytalloc_steal(dom_sid_Type, &td->security_identifier));
2987         PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
2988                         PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
2989                                                 td->trust_auth_incoming.length));
2990         PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
2991                         PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
2992                                                 td->trust_auth_outgoing.length));
2993         PyDict_SetItemString(py_domain_info, "trust_direction",
2994                         PyInt_FromLong(td->trust_direction));
2995         PyDict_SetItemString(py_domain_info, "trust_type",
2996                         PyInt_FromLong(td->trust_type));
2997         PyDict_SetItemString(py_domain_info, "trust_attributes",
2998                         PyInt_FromLong(td->trust_attributes));
2999         PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3000                         PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3001                                                 td->trust_forest_trust_info.length));
3002
3003         talloc_free(frame);
3004         return py_domain_info;
3005 }
3006
3007
3008 static PyObject *py_pdb_get_trusted_domain_by_sid(pytalloc_Object *self, PyObject *args)
3009 {
3010         TALLOC_CTX *frame = talloc_stackframe();
3011         NTSTATUS status;
3012         struct pdb_methods *methods;
3013         PyObject *py_domain_sid;
3014         struct dom_sid *domain_sid;
3015         struct pdb_trusted_domain *td;
3016         PyObject *py_domain_info;
3017
3018         if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
3019                 talloc_free(frame);
3020                 return NULL;
3021         }
3022
3023         methods = pytalloc_get_ptr(self);
3024
3025         domain_sid = pytalloc_get_ptr(py_domain_sid);
3026
3027         status = methods->get_trusted_domain_by_sid(methods, frame, domain_sid, &td);
3028         if (!NT_STATUS_IS_OK(status)) {
3029                 PyErr_Format(py_pdb_error, "Unable to get trusted domain information, (%d,%s)",
3030                                 NT_STATUS_V(status),
3031                                 get_friendly_nt_error_msg(status));
3032                 talloc_free(frame);
3033                 return NULL;
3034         }
3035
3036         py_domain_info = PyDict_New();
3037         if (py_domain_info == NULL) {
3038                 PyErr_NoMemory();
3039                 talloc_free(frame);
3040                 return NULL;
3041         }
3042
3043         PyDict_SetItemString(py_domain_info, "domain_name",
3044                         PyString_FromString(td->domain_name));
3045         PyDict_SetItemString(py_domain_info, "netbios_name",
3046                         PyString_FromString(td->netbios_name));
3047         PyDict_SetItemString(py_domain_info, "security_identifier",
3048                         pytalloc_steal(dom_sid_Type, &td->security_identifier));
3049         PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3050                         PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3051                                                 td->trust_auth_incoming.length));
3052         PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3053                         PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3054                                                 td->trust_auth_outgoing.length));
3055         PyDict_SetItemString(py_domain_info, "trust_direction",
3056                         PyInt_FromLong(td->trust_direction));
3057         PyDict_SetItemString(py_domain_info, "trust_type",
3058                         PyInt_FromLong(td->trust_type));
3059         PyDict_SetItemString(py_domain_info, "trust_attributes",
3060                         PyInt_FromLong(td->trust_attributes));
3061         PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3062                         PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3063                                                 td->trust_forest_trust_info.length));
3064
3065         talloc_free(frame);
3066         return py_domain_info;
3067 }
3068
3069
3070 static PyObject *py_pdb_set_trusted_domain(pytalloc_Object *self, PyObject *args)
3071 {
3072         TALLOC_CTX *frame = talloc_stackframe();
3073         NTSTATUS status;
3074         struct pdb_methods *methods;
3075         const char *domain;
3076         PyObject *py_td_info;
3077         struct pdb_trusted_domain td_info;
3078         PyObject *py_tmp;
3079         Py_ssize_t len;
3080
3081         if (!PyArg_ParseTuple(args, "sO!:set_trusted_domain", &domain, &PyDict_Type, &py_td_info)) {
3082                 talloc_free(frame);
3083                 return NULL;
3084         }
3085
3086         py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
3087         td_info.domain_name = PyString_AsString(py_tmp);
3088
3089         py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
3090         td_info.netbios_name = PyString_AsString(py_tmp);
3091
3092         py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
3093         td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
3094
3095         py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_incoming");
3096         PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_incoming.data, &len);
3097         td_info.trust_auth_incoming.length = len;
3098
3099         py_tmp = PyDict_GetItemString(py_td_info, "trust_auth_outgoing");
3100         PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_auth_outgoing.data, &len);
3101         td_info.trust_auth_outgoing.length = len;
3102
3103         py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
3104         td_info.trust_direction = PyInt_AsLong(py_tmp);
3105
3106         py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
3107         td_info.trust_type = PyInt_AsLong(py_tmp);
3108
3109         py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
3110         td_info.trust_attributes = PyInt_AsLong(py_tmp);
3111
3112         py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
3113         PyString_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
3114         td_info.trust_forest_trust_info.length = len;
3115
3116         methods = pytalloc_get_ptr(self);
3117
3118         status = methods->set_trusted_domain(methods, domain, &td_info);
3119         if (!NT_STATUS_IS_OK(status)) {
3120                 PyErr_Format(py_pdb_error, "Unable to set trusted domain information, (%d,%s)",
3121                                 NT_STATUS_V(status),
3122                                 get_friendly_nt_error_msg(status));
3123                 talloc_free(frame);
3124                 return NULL;
3125         }
3126
3127         Py_RETURN_NONE;
3128         talloc_free(frame);
3129 }
3130
3131
3132 static PyObject *py_pdb_del_trusted_domain(pytalloc_Object *self, PyObject *args)
3133 {
3134         TALLOC_CTX *frame = talloc_stackframe();
3135         NTSTATUS status;
3136         struct pdb_methods *methods;
3137         const char *domain;
3138
3139         if (!PyArg_ParseTuple(args, "s:del_trusted_domain", &domain)) {
3140                 talloc_free(frame);
3141                 return NULL;
3142         }
3143
3144         methods = pytalloc_get_ptr(self);
3145
3146         status = methods->del_trusted_domain(methods, domain);
3147         if (!NT_STATUS_IS_OK(status)) {
3148                 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3149                                 NT_STATUS_V(status),
3150                                 get_friendly_nt_error_msg(status));
3151                 talloc_free(frame);
3152                 return NULL;
3153         }
3154
3155         Py_RETURN_NONE;
3156         talloc_free(frame);
3157 }
3158
3159
3160 static PyObject *py_pdb_enum_trusted_domains(pytalloc_Object *self)
3161 {
3162         TALLOC_CTX *frame = talloc_stackframe();
3163         NTSTATUS status;
3164         struct pdb_methods *methods;
3165         uint32_t num_domains;
3166         struct pdb_trusted_domain **td_info, *td;
3167         PyObject *py_td_info, *py_domain_info;
3168         int i;
3169
3170         methods = pytalloc_get_ptr(self);
3171
3172         status = methods->enum_trusted_domains(methods, frame, &num_domains, &td_info);
3173         if (!NT_STATUS_IS_OK(status)) {
3174                 PyErr_Format(py_pdb_error, "Unable to delete trusted domain, (%d,%s)",
3175                                 NT_STATUS_V(status),
3176                                 get_friendly_nt_error_msg(status));
3177                 talloc_free(frame);
3178                 return NULL;
3179         }
3180
3181         py_td_info = PyList_New(0);
3182         if (py_td_info == NULL) {
3183                 PyErr_NoMemory();
3184                 talloc_free(frame);
3185                 return NULL;
3186         }
3187
3188         for (i=0; i<num_domains; i++) {
3189
3190                 py_domain_info = PyDict_New();
3191                 if (py_domain_info == NULL) {
3192                         PyErr_NoMemory();
3193                         Py_DECREF(py_td_info);
3194                         talloc_free(frame);
3195                         return NULL;
3196                 }
3197
3198                 td = td_info[i];
3199
3200                 PyDict_SetItemString(py_domain_info, "domain_name",
3201                                 PyString_FromString(td->domain_name));
3202                 PyDict_SetItemString(py_domain_info, "netbios_name",
3203                                 PyString_FromString(td->netbios_name));
3204                 PyDict_SetItemString(py_domain_info, "security_identifier",
3205                                 pytalloc_steal(dom_sid_Type, &td->security_identifier));
3206                 PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
3207                                 PyString_FromStringAndSize((char *)td->trust_auth_incoming.data,
3208                                                         td->trust_auth_incoming.length));
3209                 PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
3210                                 PyString_FromStringAndSize((char *)td->trust_auth_outgoing.data,
3211                                                         td->trust_auth_outgoing.length));
3212                 PyDict_SetItemString(py_domain_info, "trust_direction",
3213                                 PyInt_FromLong(td->trust_direction));
3214                 PyDict_SetItemString(py_domain_info, "trust_type",
3215                                 PyInt_FromLong(td->trust_type));
3216                 PyDict_SetItemString(py_domain_info, "trust_attributes",
3217                                 PyInt_FromLong(td->trust_attributes));
3218                 PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
3219                                 PyString_FromStringAndSize((char *)td->trust_forest_trust_info.data,
3220                                                         td->trust_forest_trust_info.length));
3221                 PyList_Append(py_td_info, py_domain_info);
3222         }
3223
3224         talloc_free(frame);
3225         return py_td_info;
3226 }
3227
3228
3229 static PyObject *py_pdb_get_secret(pytalloc_Object *self, PyObject *args)
3230 {
3231         TALLOC_CTX *frame = talloc_stackframe();
3232         NTSTATUS status;
3233         struct pdb_methods *methods;
3234         const char *secret_name;
3235         DATA_BLOB secret_current, secret_old;
3236         NTTIME secret_current_lastchange, secret_old_lastchange;
3237         PyObject *py_sd;
3238         struct security_descriptor *sd;
3239         PyObject *py_secret;
3240
3241         if (!PyArg_ParseTuple(args, "s:get_secret_name", &secret_name)) {
3242                 talloc_free(frame);
3243                 return NULL;
3244         }
3245
3246         methods = pytalloc_get_ptr(self);
3247
3248         py_sd = pytalloc_new(struct security_descriptor, security_Type);
3249         if (py_sd == NULL) {
3250                 PyErr_NoMemory();
3251                 talloc_free(frame);
3252                 return NULL;
3253         }
3254         sd = pytalloc_get_ptr(py_sd);
3255
3256         status = methods->get_secret(methods, frame, secret_name,
3257                                         &secret_current,
3258                                         &secret_current_lastchange,
3259                                         &secret_old,
3260                                         &secret_old_lastchange,
3261                                         &sd);
3262         if (!NT_STATUS_IS_OK(status)) {
3263                 PyErr_Format(py_pdb_error, "Unable to get information for secret (%s), (%d,%s)",
3264                                 secret_name,
3265                                 NT_STATUS_V(status),
3266                                 get_friendly_nt_error_msg(status));
3267                 talloc_free(frame);
3268                 return NULL;
3269         }
3270
3271         py_secret = PyDict_New();
3272         if (py_secret == NULL) {
3273                 PyErr_NoMemory();
3274                 Py_DECREF(py_sd);
3275                 talloc_free(frame);
3276                 return NULL;
3277         }
3278
3279         PyDict_SetItemString(py_secret, "secret_current",
3280                         PyString_FromStringAndSize((char *)secret_current.data, secret_current.length));
3281         PyDict_SetItemString(py_secret, "secret_current_lastchange",
3282                         PyLong_FromUnsignedLongLong(secret_current_lastchange));
3283         PyDict_SetItemString(py_secret, "secret_old",
3284                         PyString_FromStringAndSize((char *)secret_old.data, secret_old.length));
3285         PyDict_SetItemString(py_secret, "secret_old_lastchange",
3286                         PyLong_FromUnsignedLongLong(secret_old_lastchange));
3287         PyDict_SetItemString(py_secret, "sd", py_sd);
3288
3289         talloc_free(frame);
3290         return py_secret;
3291 }
3292
3293
3294 static PyObject *py_pdb_set_secret(pytalloc_Object *self, PyObject *args)
3295 {
3296         TALLOC_CTX *frame = talloc_stackframe();
3297         NTSTATUS status;
3298         struct pdb_methods *methods;
3299         const char *secret_name;
3300         PyObject *py_secret;
3301         PyObject *py_secret_cur, *py_secret_old, *py_sd;
3302         DATA_BLOB secret_current, secret_old;
3303         struct security_descriptor *sd;
3304         Py_ssize_t len;
3305
3306         if (!PyArg_ParseTuple(args, "sO!:set_secret_name", &secret_name, PyDict_Type, &py_secret)) {
3307                 talloc_free(frame);
3308                 return NULL;
3309         }
3310
3311         py_secret_cur = PyDict_GetItemString(py_secret, "secret_current");
3312         py_secret_old = PyDict_GetItemString(py_secret, "secret_old");
3313         py_sd = PyDict_GetItemString(py_secret, "sd");
3314
3315         PY_CHECK_TYPE(&PyString_Type, py_secret_cur, return NULL;);
3316         PY_CHECK_TYPE(&PyString_Type, py_secret_old, return NULL;);
3317         PY_CHECK_TYPE(security_Type, py_sd, return NULL;);
3318
3319         methods = pytalloc_get_ptr(self);
3320
3321         PyString_AsStringAndSize(py_secret_cur, (char **)&secret_current.data, &len);
3322         secret_current.length = len;
3323         PyString_AsStringAndSize(py_secret_old, (char **)&secret_old.data, &len);
3324         secret_current.length = len;
3325         sd = pytalloc_get_ptr(py_sd);
3326
3327         status = methods->set_secret(methods, secret_name, &secret_current, &secret_old, sd);
3328         if (!NT_STATUS_IS_OK(status)) {
3329                 PyErr_Format(py_pdb_error, "Unable to set information for secret (%s), (%d,%s)",
3330                                 secret_name,
3331                                 NT_STATUS_V(status),
3332                                 get_friendly_nt_error_msg(status));
3333                 talloc_free(frame);
3334                 return NULL;
3335         }
3336
3337         Py_RETURN_NONE;
3338         talloc_free(frame);
3339 }
3340
3341
3342 static PyObject *py_pdb_delete_secret(pytalloc_Object *self, PyObject *args)
3343 {
3344         TALLOC_CTX *frame = talloc_stackframe();
3345         NTSTATUS status;
3346         struct pdb_methods *methods;
3347         const char *secret_name;
3348
3349         if (!PyArg_ParseTuple(args, "s:delete_secret", &secret_name)) {
3350                 talloc_free(frame);
3351                 return NULL;
3352         }
3353
3354         methods = pytalloc_get_ptr(self);
3355
3356         status = methods->delete_secret(methods, secret_name);
3357         if (!NT_STATUS_IS_OK(status)) {
3358                 PyErr_Format(py_pdb_error, "Unable to delete secret (%s), (%d,%s)",
3359                                 secret_name,
3360                                 NT_STATUS_V(status),
3361                                 get_friendly_nt_error_msg(status));
3362                 talloc_free(frame);
3363                 return NULL;
3364         }
3365
3366         Py_RETURN_NONE;
3367         talloc_free(frame);
3368 }
3369
3370 static PyMethodDef py_pdb_methods[] = {
3371         { "domain_info", (PyCFunction)py_pdb_domain_info, METH_NOARGS,
3372                 "domain_info() -> str\n\n \
3373                 Get domain information for the database." },
3374         { "getsampwnam", (PyCFunction)py_pdb_getsampwnam, METH_VARARGS,
3375                 "getsampwnam(username) -> samu object\n\n \
3376                 Get user information by name." },
3377         { "getsampwsid", (PyCFunction)py_pdb_getsampwsid, METH_VARARGS,
3378                 "getsampwsid(user_sid) -> samu object\n\n \
3379                 Get user information by sid (dcerpc.security.dom_sid object)." },
3380         { "create_user", (PyCFunction)py_pdb_create_user, METH_VARARGS,
3381                 "create_user(username, acct_flags) -> rid\n\n \
3382                 Create user. acct_flags are samr account control flags." },
3383         { "delete_user", (PyCFunction)py_pdb_delete_user, METH_VARARGS,
3384                 "delete_user(samu object) -> None\n\n \
3385                 Delete user." },
3386         { "add_sam_account", (PyCFunction)py_pdb_add_sam_account, METH_VARARGS,
3387                 "add_sam_account(samu object) -> None\n\n \
3388                 Add SAM account." },
3389         { "update_sam_account", (PyCFunction)py_pdb_update_sam_account, METH_VARARGS,
3390                 "update_sam_account(samu object) -> None\n\n \
3391                 Update SAM account." },
3392         { "delete_sam_account", (PyCFunction)py_pdb_delete_sam_account, METH_VARARGS,
3393                 "delete_sam_account(samu object) -> None\n\n \
3394                 Delete SAM account." },
3395         { "rename_sam_account", (PyCFunction)py_pdb_rename_sam_account, METH_VARARGS,
3396                 "rename_sam_account(samu object1, new_username) -> None\n\n \
3397                 Rename SAM account." },
3398         /* update_login_attempts */
3399         { "getgrsid", (PyCFunction)py_pdb_getgrsid, METH_VARARGS,
3400                 "getgrsid(group_sid) -> groupmap object\n\n \
3401                 Get group information by sid (dcerpc.security.dom_sid object)." },
3402         { "getgrgid", (PyCFunction)py_pdb_getgrgid, METH_VARARGS,
3403                 "getgrsid(gid) -> groupmap object\n\n \
3404                 Get group information by gid." },
3405         { "getgrnam", (PyCFunction)py_pdb_getgrnam, METH_VARARGS,
3406                 "getgrsid(groupname) -> groupmap object\n\n \
3407                 Get group information by name." },
3408         { "create_dom_group", (PyCFunction)py_pdb_create_dom_group, METH_VARARGS,
3409                 "create_dom_group(groupname) -> group_rid\n\n \
3410                 Create new domain group by name." },
3411         { "delete_dom_group", (PyCFunction)py_pdb_delete_dom_group, METH_VARARGS,
3412                 "delete_dom_group(group_rid) -> None\n\n \
3413                 Delete domain group identified by rid" },
3414         { "add_group_mapping_entry", (PyCFunction)py_pdb_add_group_mapping_entry, METH_VARARGS,
3415                 "add_group_mapping_entry(groupmap) -> None\n \
3416                 Add group mapping entry for groupmap object." },
3417         { "update_group_mapping_entry", (PyCFunction)py_pdb_update_group_mapping_entry, METH_VARARGS,
3418                 "update_group_mapping_entry(groupmap) -> None\n\n \
3419                 Update group mapping entry for groupmap object." },
3420         { "delete_group_mapping_entry", (PyCFunction)py_pdb_delete_group_mapping_entry, METH_VARARGS,
3421                 "delete_group_mapping_entry(groupmap) -> None\n\n \
3422                 Delete group mapping entry for groupmap object." },
3423         { "enum_group_mapping", (PyCFunction)py_pdb_enum_group_mapping, METH_VARARGS,
3424                 "enum_group_mapping([domain_sid, [type, [unix_only]]]) -> List\n\n \
3425                 Return list of group mappings as groupmap objects. Optional arguments are domain_sid object, type of group, unix only flag." },
3426         { "enum_group_members", (PyCFunction)py_pdb_enum_group_members, METH_VARARGS,
3427                 "enum_group_members(group_sid) -> List\n\n \
3428                 Return list of users (dom_sid object) in group." },
3429         { "enum_group_memberships", (PyCFunction)py_pdb_enum_group_memberships, METH_VARARGS,
3430                 "enum_group_memberships(samu object) -> List\n\n \
3431                 Return list of groups (dom_sid object) this user is part of." },
3432         /* set_unix_primary_group */
3433         { "add_groupmem", (PyCFunction)py_pdb_add_groupmem, METH_VARARGS,
3434                 "add_groupmem(group_rid, member_rid) -> None\n\n \
3435                 Add user to group." },
3436         { "del_groupmem", (PyCFunction)py_pdb_del_groupmem, METH_VARARGS,
3437                 "del_groupmem(group_rid, member_rid) -> None\n\n \
3438                 Remove user from from group." },
3439         { "create_alias", (PyCFunction)py_pdb_create_alias, METH_VARARGS,
3440                 "create_alias(alias_name) -> alias_rid\n\n \
3441                 Create alias entry." },
3442         { "delete_alias", (PyCFunction)py_pdb_delete_alias, METH_VARARGS,
3443                 "delete_alias(alias_sid) -> None\n\n \
3444                 Delete alias entry." },
3445         { "get_aliasinfo", (PyCFunction)py_pdb_get_aliasinfo, METH_VARARGS,
3446                 "get_aliasinfo(alias_sid) -> Mapping\n\n \
3447                 Get alias information as a dictionary with keys - acct_name, acct_desc, rid." },
3448         { "set_aliasinfo", (PyCFunction)py_pdb_set_aliasinfo, METH_VARARGS,
3449                 "set_alias_info(alias_sid, Mapping) -> None\n\n \
3450                 Set alias information from a dictionary with keys - acct_name, acct_desc." },
3451         { "add_aliasmem", (PyCFunction)py_pdb_add_aliasmem, METH_VARARGS,
3452                 "add_aliasmem(alias_sid, member_sid) -> None\n\n \
3453                 Add user to alias entry." },
3454         { "del_aliasmem", (PyCFunction)py_pdb_del_aliasmem, METH_VARARGS,
3455                 "del_aliasmem(alias_sid, member_sid) -> None\n\n \
3456                 Remove a user from alias entry." },
3457         { "enum_aliasmem", (PyCFunction)py_pdb_enum_aliasmem, METH_VARARGS,
3458                 "enum_aliasmem(alias_sid) -> List\n\n \
3459                 Return a list of members (dom_sid object) for alias entry." },
3460         /* enum_alias_memberships */
3461         /* lookup_rids */
3462         /* lookup_names */
3463         { "get_account_policy", (PyCFunction)py_pdb_get_account_policy, METH_NOARGS,
3464                 "get_account_policy() -> Mapping\n\n \
3465                 Get account policy information as a dictionary." },
3466         { "set_account_policy", (PyCFunction)py_pdb_set_account_policy, METH_VARARGS,
3467                 "get_account_policy(Mapping) -> None\n\n \
3468                 Set account policy settings from a dicionary." },
3469         /* get_seq_num */
3470         { "search_users", (PyCFunction)py_pdb_search_users, METH_VARARGS,
3471                 "search_users(acct_flags) -> List\n\n \
3472                 Search users. acct_flags are samr account control flags.\n \
3473                 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3474         { "search_groups", (PyCFunction)py_pdb_search_groups, METH_NOARGS,
3475                 "search_groups() -> List\n\n \
3476                 Search unix only groups. \n \
3477                 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3478         { "search_aliases", (PyCFunction)py_pdb_search_aliases, METH_VARARGS,
3479                 "search_aliases([domain_sid]) -> List\n\n \
3480                 Search aliases. domain_sid is dcerpc.security.dom_sid object.\n \
3481                 Each list entry is dictionary with keys - idx, rid, acct_flags, account_name, fullname, description." },
3482         { "uid_to_sid", (PyCFunction)py_pdb_uid_to_sid, METH_VARARGS,
3483                 "uid_to_sid(uid) -> sid\n\n \
3484                 Return sid for given user id." },
3485         { "gid_to_sid", (PyCFunction)py_pdb_gid_to_sid, METH_VARARGS,
3486                 "gid_to_sid(gid) -> sid\n\n \
3487                 Return sid for given group id." },
3488         { "sid_to_id", (PyCFunction)py_pdb_sid_to_id, METH_VARARGS,
3489                 "sid_to_id(sid) -> Tuple\n\n \
3490                 Return id and type for given sid." },
3491         /* capabilities */
3492         { "new_rid", (PyCFunction)py_pdb_new_rid, METH_NOARGS,
3493                 "new_rid() -> rid\n\n \
3494                 Get a new rid." },
3495         { "get_trusteddom_pw", (PyCFunction)py_pdb_get_trusteddom_pw, METH_VARARGS,
3496                 "get_trusteddom_pw(domain) -> Mapping\n\n \
3497                 Get trusted domain password, sid and last set time in a dictionary." },
3498         { "set_trusteddom_pw", (PyCFunction)py_pdb_set_trusteddom_pw, METH_VARARGS,
3499                 "set_trusteddom_pw(domain, pwd, sid) -> None\n\n \
3500                 Set trusted domain password." },
3501         { "del_trusteddom_pw", (PyCFunction)py_pdb_del_trusteddom_pw, METH_VARARGS,
3502                 "del_trusteddom_pw(domain) -> None\n\n \
3503                 Delete trusted domain password." },
3504         { "enum_trusteddoms", (PyCFunction)py_pdb_enum_trusteddoms, METH_NOARGS,
3505                 "enum_trusteddoms() -> List\n\n \
3506                 Get list of trusted domains. Each item is a dictionary with name and sid keys" },
3507         { "get_trusted_domain", (PyCFunction)py_pdb_get_trusted_domain, METH_VARARGS,
3508                 "get_trusted_domain(domain) -> Mapping\n\n \
3509                 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." },
3510         { "get_trusted_domain_by_sid", (PyCFunction)py_pdb_get_trusted_domain_by_sid, METH_VARARGS,
3511                 "get_trusted_domain_by_sid(domain_sid) -> Mapping\n\n \
3512                 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" },
3513         { "set_trusted_domain", (PyCFunction)py_pdb_set_trusted_domain, METH_VARARGS,
3514                 "set_trusted_domain(domain, Mapping) -> None\n\n \
3515                 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." },
3516         { "del_trusted_domain", (PyCFunction)py_pdb_del_trusted_domain, METH_VARARGS,
3517                 "del_trusted_domain(domain) -> None\n\n \
3518                 Delete trusted domain." },
3519         { "enum_trusted_domains", (PyCFunction)py_pdb_enum_trusted_domains, METH_VARARGS,
3520                 "enum_trusted_domains() -> List\n\n \
3521                 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." },
3522         { "get_secret", (PyCFunction)py_pdb_get_secret, METH_VARARGS,
3523                 "get_secret(secret_name) -> Mapping\n\n \
3524                 Get secret information for secret_name. Information is a dictionary with keys - secret_current, secret_current_lastchange, secret_old, secret_old_lastchange, sd." },
3525         { "set_secret", (PyCFunction)py_pdb_set_secret, METH_VARARGS,
3526                 "set_secret(secret_name, Mapping) -> None\n\n \
3527                 Set secret information for secret_name using dictionary with keys - secret_current, sd." },
3528         { "delete_secret", (PyCFunction)py_pdb_delete_secret, METH_VARARGS,
3529                 "delete_secret(secret_name) -> None\n\n \
3530                 Delete secret information for secret_name." },
3531         { NULL },
3532 };
3533
3534
3535 static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3536 {
3537         TALLOC_CTX *frame = talloc_stackframe();
3538         const char *url = NULL;
3539         PyObject *pypdb;
3540         NTSTATUS status;
3541         struct pdb_methods *methods;
3542
3543         if (!PyArg_ParseTuple(args, "s", &url)) {
3544                 talloc_free(frame);
3545                 return NULL;
3546         }
3547
3548         /* Initalize list of methods */
3549         status = make_pdb_method_name(&methods, url);
3550         if (!NT_STATUS_IS_OK(status)) {
3551                 PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
3552                                 url,
3553                                 NT_STATUS_V(status),
3554                                 get_friendly_nt_error_msg(status));
3555                 talloc_free(frame);
3556                 return NULL;
3557         }
3558
3559         if ((pypdb = pytalloc_steal(type, methods)) == NULL) {
3560                 PyErr_NoMemory();
3561                 talloc_free(frame);
3562                 return NULL;
3563         }
3564
3565         talloc_free(frame);
3566         return pypdb;
3567 }
3568
3569
3570 static PyTypeObject PyPDB = {
3571         .tp_name = "passdb.PDB",
3572         .tp_basicsize = sizeof(pytalloc_Object),
3573         .tp_new = py_pdb_new,
3574         .tp_flags = Py_TPFLAGS_DEFAULT,
3575         .tp_methods = py_pdb_methods,
3576         .tp_doc = "PDB(url[, read_write_flags]) -> Password DB object\n",
3577 };
3578
3579
3580 /*
3581  * Return a list of passdb backends
3582  */
3583 static PyObject *py_passdb_backends(PyObject *self)
3584 {
3585         TALLOC_CTX *frame = talloc_stackframe();
3586         PyObject *py_blist;
3587         const struct pdb_init_function_entry *entry;
3588
3589         entry = pdb_get_backends();
3590         if(! entry) {
3591                 Py_RETURN_NONE;
3592         }
3593
3594         if((py_blist = PyList_New(0)) == NULL) {
3595                 PyErr_NoMemory();
3596                 talloc_free(frame);
3597                 return NULL;
3598         }
3599
3600         while(entry) {
3601                 PyList_Append(py_blist, PyString_FromString(entry->name));
3602                 entry = entry->next;
3603         }
3604
3605         talloc_free(frame);
3606         return py_blist;
3607 }
3608
3609
3610 static PyObject *py_set_smb_config(PyObject *self, PyObject *args)
3611 {
3612         TALLOC_CTX *frame = talloc_stackframe();
3613         const char *smb_config;
3614
3615         if (!PyArg_ParseTuple(args, "s", &smb_config)) {
3616                 talloc_free(frame);
3617                 return NULL;
3618         }
3619
3620         /* Load smbconf parameters */
3621         if (!lp_load_global(smb_config)) {
3622                 PyErr_Format(py_pdb_error, "Cannot open '%s'", smb_config);
3623                 talloc_free(frame);
3624                 return NULL;
3625         }
3626
3627         Py_RETURN_NONE;
3628         talloc_free(frame);
3629 }
3630
3631
3632 static PyObject *py_set_secrets_dir(PyObject *self, PyObject *args)
3633 {
3634         TALLOC_CTX *frame = talloc_stackframe();
3635         const char *private_dir;
3636
3637         if (!PyArg_ParseTuple(args, "s", &private_dir)) {
3638                 talloc_free(frame);
3639                 return NULL;
3640         }
3641
3642         /* Initialize secrets database */
3643         if (!secrets_init_path(private_dir)) {
3644                 PyErr_Format(py_pdb_error, "Cannot open secrets file database in '%s'",
3645                                 private_dir);
3646                 talloc_free(frame);
3647                 return NULL;
3648         }
3649
3650         talloc_free(frame);
3651         Py_RETURN_NONE;
3652 }
3653
3654 static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
3655 {
3656         TALLOC_CTX *frame = talloc_stackframe();
3657
3658         /* Initialize secrets database */
3659         if (!initialize_password_db(true, NULL)) {
3660                 PyErr_Format(py_pdb_error, "Cannot re-open passdb backend %s", lp_passdb_backend());
3661                 talloc_free(frame);
3662                 return NULL;
3663         }
3664
3665         talloc_free(frame);
3666         Py_RETURN_NONE;
3667 }
3668
3669 static PyObject *py_get_global_sam_sid(PyObject *self)
3670 {
3671         TALLOC_CTX *frame = talloc_stackframe();
3672         struct dom_sid *domain_sid, *domain_sid_copy;
3673         PyObject *py_dom_sid;
3674
3675         domain_sid = get_global_sam_sid();
3676
3677         domain_sid_copy = dom_sid_dup(frame, domain_sid);
3678         if (domain_sid_copy == NULL) {
3679                 PyErr_NoMemory();
3680                 talloc_free(frame);
3681                 return NULL;
3682         }
3683
3684         py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
3685
3686         talloc_free(frame);
3687         return py_dom_sid;
3688 }
3689
3690
3691 static PyMethodDef py_passdb_methods[] = {
3692         { "get_backends", (PyCFunction)py_passdb_backends, METH_NOARGS,
3693                 "get_backends() -> list\n\n \
3694                 Get a list of password database backends supported." },
3695         { "set_smb_config", (PyCFunction)py_set_smb_config, METH_VARARGS,
3696                 "set_smb_config(path) -> None\n\n \
3697                 Set path to smb.conf file to load configuration parameters." },
3698         { "set_secrets_dir", (PyCFunction)py_set_secrets_dir, METH_VARARGS,
3699                 "set_secrets_dir(private_dir) -> None\n\n \
3700                 Set path to private directory to load secrets database from non-default location." },
3701         { "get_global_sam_sid", (PyCFunction)py_get_global_sam_sid, METH_NOARGS,
3702                 "get_global_sam_sid() -> dom_sid\n\n \
3703                 Return domain SID." },
3704         { "reload_static_pdb", (PyCFunction)py_reload_static_pdb, METH_NOARGS,
3705                 "reload_static_pdb() -> None\n\n \
3706                 Re-initalise the static pdb used internally.  Needed if 'passdb backend' is changed." },
3707         { NULL },
3708 };
3709
3710 void initpassdb(void)
3711 {
3712         TALLOC_CTX *frame = talloc_stackframe();
3713         PyObject *m, *mod;
3714         char exception_name[] = "passdb.error";
3715
3716         PyTypeObject *talloc_type = pytalloc_GetObjectType();
3717         if (talloc_type == NULL) {
3718                 talloc_free(frame);
3719                 return;
3720         }
3721
3722         PyPDB.tp_base = talloc_type;
3723         if (PyType_Ready(&PyPDB) < 0) {
3724                 talloc_free(frame);
3725                 return;
3726         }
3727
3728         PySamu.tp_base = talloc_type;
3729         if (PyType_Ready(&PySamu) < 0) {
3730                 talloc_free(frame);
3731                 return;
3732         }
3733
3734         PyGroupmap.tp_base = talloc_type;
3735         if (PyType_Ready(&PyGroupmap) < 0) {
3736                 talloc_free(frame);
3737                 return;
3738         }
3739
3740         m = Py_InitModule3("passdb", py_passdb_methods, "SAMBA Password Database");
3741         if (m == NULL) {
3742             talloc_free(frame);
3743             return;
3744         }
3745
3746         /* Create new exception for passdb module */
3747         py_pdb_error = PyErr_NewException(exception_name, NULL, NULL);
3748         Py_INCREF(py_pdb_error);
3749         PyModule_AddObject(m, "error", py_pdb_error);
3750
3751         Py_INCREF(&PyPDB);
3752         PyModule_AddObject(m, "PDB", (PyObject *)&PyPDB);
3753
3754         Py_INCREF(&PySamu);
3755         PyModule_AddObject(m, "Samu", (PyObject *)&PySamu);
3756
3757         Py_INCREF(&PyGroupmap);
3758         PyModule_AddObject(m, "Groupmap", (PyObject *)&PyGroupmap);
3759
3760         /* Import dom_sid type from dcerpc.security */
3761         mod = PyImport_ImportModule("samba.dcerpc.security");
3762         if (mod == NULL) {
3763                 talloc_free(frame);
3764                 return;
3765         }
3766
3767         dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
3768         if (dom_sid_Type == NULL) {
3769                 talloc_free(frame);
3770                 return;
3771         }
3772
3773         /* Import security_descriptor type from dcerpc.security */
3774         security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
3775         Py_DECREF(mod);
3776         if (security_Type == NULL) {
3777                 talloc_free(frame);
3778                 return;
3779         }
3780
3781         /* Import GUID type from dcerpc.misc */
3782         mod = PyImport_ImportModule("samba.dcerpc.misc");
3783         if (mod == NULL) {
3784                 talloc_free(frame);
3785                 return;
3786         }
3787
3788         guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
3789         Py_DECREF(mod);
3790         if (guid_Type == NULL) {
3791                 talloc_free(frame);
3792                 return;
3793         }
3794         talloc_free(frame);
3795 }