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