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