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