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