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