Refactored open_pipe_creds() function to remove unused parameter.
[samba.git] / source / python / py_samr.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "python/py_samr.h"
22
23 /* 
24  * Exceptions raised by this module 
25  */
26
27 PyObject *samr_error;           /* This indicates a non-RPC related error
28                                    such as name lookup failure */
29
30 PyObject *samr_ntstatus;        /* This exception is raised when a RPC call
31                                    returns a status code other than
32                                    NT_STATUS_OK */
33
34 /* SAMR connect handle object */
35
36 static void py_samr_connect_hnd_dealloc(PyObject* self)
37 {
38         PyObject_Del(self);
39 }
40
41 static PyMethodDef samr_connect_methods[] = {
42         { NULL }
43 };
44
45 static PyObject *py_samr_connect_hnd_getattr(PyObject *self, char *attrname)
46 {
47         return Py_FindMethod(samr_connect_methods, self, attrname);
48 }
49
50 PyTypeObject samr_connect_hnd_type = {
51         PyObject_HEAD_INIT(NULL)
52         0,
53         "SAMR Connect Handle",
54         sizeof(samr_connect_hnd_object),
55         0,
56         py_samr_connect_hnd_dealloc, /*tp_dealloc*/
57         0,          /*tp_print*/
58         py_samr_connect_hnd_getattr,          /*tp_getattr*/
59         0,          /*tp_setattr*/
60         0,          /*tp_compare*/
61         0,          /*tp_repr*/
62         0,          /*tp_as_number*/
63         0,          /*tp_as_sequence*/
64         0,          /*tp_as_mapping*/
65         0,          /*tp_hash */
66 };
67
68 PyObject *new_samr_connect_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
69                                       POLICY_HND *pol)
70 {
71         samr_connect_hnd_object *o;
72
73         o = PyObject_New(samr_connect_hnd_object, &samr_connect_hnd_type);
74
75         o->cli = cli;
76         o->mem_ctx = mem_ctx;
77         memcpy(&o->pol, pol, sizeof(POLICY_HND));
78
79         return (PyObject*)o;
80 }
81
82 /* SAMR domain handle object */
83
84 static void py_samr_domain_hnd_dealloc(PyObject* self)
85 {
86         PyObject_Del(self);
87 }
88
89 static PyMethodDef samr_domain_methods[] = {
90         { NULL }
91 };
92
93 static PyObject *py_samr_domain_hnd_getattr(PyObject *self, char *attrname)
94 {
95         return Py_FindMethod(samr_domain_methods, self, attrname);
96 }
97
98 PyTypeObject samr_domain_hnd_type = {
99         PyObject_HEAD_INIT(NULL)
100         0,
101         "SAMR Domain Handle",
102         sizeof(samr_domain_hnd_object),
103         0,
104         py_samr_domain_hnd_dealloc, /*tp_dealloc*/
105         0,          /*tp_print*/
106         py_samr_domain_hnd_getattr,          /*tp_getattr*/
107         0,          /*tp_setattr*/
108         0,          /*tp_compare*/
109         0,          /*tp_repr*/
110         0,          /*tp_as_number*/
111         0,          /*tp_as_sequence*/
112         0,          /*tp_as_mapping*/
113         0,          /*tp_hash */
114 };
115
116 PyObject *new_samr_domain_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
117                                       POLICY_HND *pol)
118 {
119         samr_domain_hnd_object *o;
120
121         o = PyObject_New(samr_domain_hnd_object, &samr_domain_hnd_type);
122
123         o->cli = cli;
124         o->mem_ctx = mem_ctx;
125         memcpy(&o->pol, pol, sizeof(POLICY_HND));
126
127         return (PyObject*)o;
128 }
129
130 /* SAMR user handle object */
131
132 static void py_samr_user_hnd_dealloc(PyObject* self)
133 {
134         PyObject_Del(self);
135 }
136
137 static PyMethodDef samr_user_methods[] = {
138         { NULL }
139 };
140
141 static PyObject *py_samr_user_hnd_getattr(PyObject *self, char *attrname)
142 {
143         return Py_FindMethod(samr_user_methods, self, attrname);
144 }
145
146 PyTypeObject samr_user_hnd_type = {
147         PyObject_HEAD_INIT(NULL)
148         0,
149         "SAMR User Handle",
150         sizeof(samr_user_hnd_object),
151         0,
152         py_samr_user_hnd_dealloc, /*tp_dealloc*/
153         0,          /*tp_print*/
154         py_samr_user_hnd_getattr,          /*tp_getattr*/
155         0,          /*tp_setattr*/
156         0,          /*tp_compare*/
157         0,          /*tp_repr*/
158         0,          /*tp_as_number*/
159         0,          /*tp_as_sequence*/
160         0,          /*tp_as_mapping*/
161         0,          /*tp_hash */
162 };
163
164 PyObject *new_samr_user_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
165                                       POLICY_HND *pol)
166 {
167         samr_user_hnd_object *o;
168
169         o = PyObject_New(samr_user_hnd_object, &samr_user_hnd_type);
170
171         o->cli = cli;
172         o->mem_ctx = mem_ctx;
173         memcpy(&o->pol, pol, sizeof(POLICY_HND));
174
175         return (PyObject*)o;
176 }
177
178 /* SAMR group handle object */
179
180 static void py_samr_group_hnd_dealloc(PyObject* self)
181 {
182         PyObject_Del(self);
183 }
184
185 static PyMethodDef samr_group_methods[] = {
186         { NULL }
187 };
188
189 static PyObject *py_samr_group_hnd_getattr(PyObject *self, char *attrname)
190 {
191         return Py_FindMethod(samr_group_methods, self, attrname);
192 }
193
194 PyTypeObject samr_group_hnd_type = {
195         PyObject_HEAD_INIT(NULL)
196         0,
197         "SAMR Group Handle",
198         sizeof(samr_group_hnd_object),
199         0,
200         py_samr_group_hnd_dealloc, /*tp_dealloc*/
201         0,          /*tp_print*/
202         py_samr_group_hnd_getattr,          /*tp_getattr*/
203         0,          /*tp_setattr*/
204         0,          /*tp_compare*/
205         0,          /*tp_repr*/
206         0,          /*tp_as_number*/
207         0,          /*tp_as_sequence*/
208         0,          /*tp_as_mapping*/
209         0,          /*tp_hash */
210 };
211
212 PyObject *new_samr_group_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
213                                       POLICY_HND *pol)
214 {
215         samr_group_hnd_object *o;
216
217         o = PyObject_New(samr_group_hnd_object, &samr_group_hnd_type);
218
219         o->cli = cli;
220         o->mem_ctx = mem_ctx;
221         memcpy(&o->pol, pol, sizeof(POLICY_HND));
222
223         return (PyObject*)o;
224 }
225
226 /* Alias handle object */
227
228 static void py_samr_alias_hnd_dealloc(PyObject* self)
229 {
230         PyObject_Del(self);
231 }
232
233 static PyMethodDef samr_alias_methods[] = {
234         { NULL }
235 };
236
237 static PyObject *py_samr_alias_hnd_getattr(PyObject *self, char *attrname)
238 {
239         return Py_FindMethod(samr_alias_methods, self, attrname);
240 }
241
242 PyTypeObject samr_alias_hnd_type = {
243         PyObject_HEAD_INIT(NULL)
244         0,
245         "SAMR Alias Handle",
246         sizeof(samr_alias_hnd_object),
247         0,
248         py_samr_alias_hnd_dealloc, /*tp_dealloc*/
249         0,          /*tp_print*/
250         py_samr_alias_hnd_getattr,          /*tp_getattr*/
251         0,          /*tp_setattr*/
252         0,          /*tp_compare*/
253         0,          /*tp_repr*/
254         0,          /*tp_as_number*/
255         0,          /*tp_as_sequence*/
256         0,          /*tp_as_mapping*/
257         0,          /*tp_hash */
258 };
259
260 PyObject *new_samr_alias_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
261                                       POLICY_HND *pol)
262 {
263         samr_alias_hnd_object *o;
264
265         o = PyObject_New(samr_alias_hnd_object, &samr_alias_hnd_type);
266
267         o->cli = cli;
268         o->mem_ctx = mem_ctx;
269         memcpy(&o->pol, pol, sizeof(POLICY_HND));
270
271         return (PyObject*)o;
272 }
273
274 static PyObject *samr_connect(PyObject *self, PyObject *args, PyObject *kw)
275 {
276         static char *kwlist[] = { "server", "creds", "access", NULL };
277         uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
278         char *server_name;
279         struct cli_state *cli;
280         POLICY_HND hnd;
281         TALLOC_CTX *mem_ctx;
282         PyObject *result = NULL, *creds = NULL;
283         NTSTATUS ntstatus;
284
285         if (!PyArg_ParseTupleAndKeywords(
286                     args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type,
287                     &creds, &desired_access)) 
288                 return NULL;
289
290         if (!(cli = open_pipe_creds(server_name, creds, cli_samr_initialise)))
291                 goto done;
292
293         if (!(mem_ctx = talloc_init())) {
294                 PyErr_SetString(samr_ntstatus,
295                                 "unable to initialise talloc context\n");
296                 goto done;
297         }
298
299         ntstatus = cli_samr_connect(cli, mem_ctx, desired_access, &hnd);
300
301         if (!NT_STATUS_IS_OK(ntstatus)) {
302                 cli_shutdown(cli);
303                 SAFE_FREE(cli);
304                 PyErr_SetObject(samr_ntstatus, py_ntstatus_tuple(ntstatus));
305                 goto done;
306         }
307
308         result = new_samr_connect_hnd_object(cli, mem_ctx, &hnd);
309
310 done:
311         return result;
312 }
313
314 /*
315  * Module initialisation 
316  */
317
318 static PyMethodDef samr_methods[] = {
319
320         /* Open/close samr connect handles */
321         
322         { "connect", (PyCFunction)samr_connect, 
323           METH_VARARGS | METH_KEYWORDS, 
324           "Open a connect handle" },
325         
326         { NULL }
327 };
328
329 static struct const_vals {
330         char *name;
331         uint32 value;
332 } module_const_vals[] = {
333         { NULL }
334 };
335
336 static void const_init(PyObject *dict)
337 {
338         struct const_vals *tmp;
339         PyObject *obj;
340
341         for (tmp = module_const_vals; tmp->name; tmp++) {
342                 obj = PyInt_FromLong(tmp->value);
343                 PyDict_SetItemString(dict, tmp->name, obj);
344                 Py_DECREF(obj);
345         }
346 }
347
348 void initsamr(void)
349 {
350         PyObject *module, *dict;
351
352         /* Initialise module */
353
354         module = Py_InitModule("samr", samr_methods);
355         dict = PyModule_GetDict(module);
356
357         samr_error = PyErr_NewException("samr.error", NULL, NULL);
358         PyDict_SetItemString(dict, "error", samr_error);
359
360         samr_ntstatus = PyErr_NewException("samr.ntstatus", NULL, NULL);
361         PyDict_SetItemString(dict, "ntstatus", samr_ntstatus);
362
363         /* Initialise policy handle object */
364
365         samr_connect_hnd_type.ob_type = &PyType_Type;
366         samr_domain_hnd_type.ob_type = &PyType_Type;
367         samr_user_hnd_type.ob_type = &PyType_Type;
368         samr_group_hnd_type.ob_type = &PyType_Type;
369         samr_alias_hnd_type.ob_type = &PyType_Type;
370
371         /* Initialise constants */
372
373         const_init(dict);
374
375         /* Do samba initialisation */
376
377         py_samba_init();
378
379         setup_logging("samr", True);
380         DEBUGLEVEL = 10;
381 }