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