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