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