More cleanups.
[abartlet/samba.git/.git] / source3 / python / py_lsa.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_lsa.h"
22
23 PyObject *new_lsa_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
24                                     POLICY_HND *pol)
25 {
26         lsa_policy_hnd_object *o;
27
28         o = PyObject_New(lsa_policy_hnd_object, &lsa_policy_hnd_type);
29
30         o->cli = cli;
31         o->mem_ctx = mem_ctx;
32         memcpy(&o->pol, pol, sizeof(POLICY_HND));
33
34         return (PyObject*)o;
35 }
36
37 /* 
38  * Exceptions raised by this module 
39  */
40
41 PyObject *lsa_error;            /* This indicates a non-RPC related error
42                                    such as name lookup failure */
43
44 PyObject *lsa_ntstatus;         /* This exception is raised when a RPC call
45                                    returns a status code other than
46                                    NT_STATUS_OK */
47
48 /*
49  * Open/close lsa handles
50  */
51
52 static PyObject *lsa_open_policy(PyObject *self, PyObject *args, 
53                                 PyObject *kw) 
54 {
55         static char *kwlist[] = { "servername", "creds", "access", NULL };
56         char *server, *errstr;
57         PyObject *creds = NULL, *result;
58         uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
59         struct cli_state *cli;
60         NTSTATUS ntstatus;
61         TALLOC_CTX *mem_ctx;
62         POLICY_HND hnd;
63
64         if (!PyArg_ParseTupleAndKeywords(
65                     args, kw, "s|O!i", kwlist, &server, &PyDict_Type,
66                     &creds, &desired_access))
67                 return NULL;
68
69         if (!(cli = open_pipe_creds(
70                       server, creds, cli_lsa_initialise, &errstr))) {
71                 PyErr_SetString(lsa_error, errstr);
72                 free(errstr);
73                 return NULL;
74         }
75
76         if (!(mem_ctx = talloc_init())) {
77                 PyErr_SetString(
78                         lsa_error, "unable to initialise talloc context\n");
79                 return NULL;
80         }
81
82         ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
83                                        SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
84
85         if (!NT_STATUS_IS_OK(ntstatus)) {
86                 cli_shutdown(cli);
87                 SAFE_FREE(cli);
88                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
89                 return NULL;
90         }
91
92         result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
93
94         return result;
95 }
96
97 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw) 
98 {
99         PyObject *po;
100         lsa_policy_hnd_object *hnd;
101         NTSTATUS result;
102
103         /* Parse parameters */
104
105         if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
106                 return NULL;
107
108         hnd = (lsa_policy_hnd_object *)po;
109
110         /* Call rpc function */
111
112         result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
113
114         /* Cleanup samba stuff */
115
116         cli_shutdown(hnd->cli);
117         talloc_destroy(hnd->mem_ctx);
118
119         /* Return value */
120
121         Py_INCREF(Py_None);
122         return Py_None; 
123 }
124
125 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
126 {
127         PyObject *py_names, *result;
128         NTSTATUS ntstatus;
129         lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
130         int num_names, i;
131         const char **names;
132         DOM_SID *sids;
133         uint32 *name_types;
134
135         if (!PyArg_ParseTuple(args, "O", &py_names))
136                 return NULL;
137
138         if (!PyList_Check(py_names) && !PyString_Check(py_names)) {
139                 PyErr_SetString(PyExc_TypeError, "must be list or string");
140                 return NULL;
141         }
142
143         if (PyList_Check(py_names)) {
144
145                 /* Convert list to char ** array */
146
147                 num_names = PyList_Size(py_names);
148                 names = (const char **)talloc(
149                         hnd->mem_ctx, num_names * sizeof(char *));
150                 
151                 for (i = 0; i < num_names; i++) {
152                         PyObject *obj = PyList_GetItem(py_names, i);
153                         
154                         names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
155                 }
156
157         } else {
158
159                 /* Just a single element */
160
161                 num_names = 1;
162                 names = (const char **)talloc(hnd->mem_ctx, sizeof(char *));
163
164                 names[0] = PyString_AsString(py_names);
165         }
166
167         ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
168                                         num_names, names, &sids, &name_types);
169
170         if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
171                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
172                 return NULL;
173         }
174
175         result = PyList_New(num_names);
176
177         for (i = 0; i < num_names; i++) {
178                 PyObject *sid_obj, *obj;
179
180                 py_from_SID(&sid_obj, &sids[i]);
181
182                 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
183
184                 PyList_SetItem(result, i, obj);
185         }
186         
187         return result;
188 }
189
190 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args, 
191                                  PyObject *kw) 
192 {
193         PyObject *py_sids, *result;
194         NTSTATUS ntstatus;
195         int num_sids, i;
196         char **domains, **names;
197         uint32 *types;
198         lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
199         DOM_SID *sids;
200
201         if (!PyArg_ParseTuple(args, "O", &py_sids))
202                 return NULL;
203
204         if (!PyList_Check(py_sids) && !PyString_Check(py_sids)) {
205                 PyErr_SetString(PyExc_TypeError, "must be list or string");
206                 return NULL;
207         }
208
209         if (PyList_Check(py_sids)) {
210
211                 /* Convert dictionary to char ** array */
212                 
213                 num_sids = PyList_Size(py_sids);
214                 sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
215                 
216                 memset(sids, 0, num_sids * sizeof(DOM_SID));
217                 
218                 for (i = 0; i < num_sids; i++) {
219                         PyObject *obj = PyList_GetItem(py_sids, i);
220                         
221                         string_to_sid(&sids[i], PyString_AsString(obj));
222                 }
223
224         } else {
225
226                 /* Just a single element */
227
228                 num_sids = 1;
229                 sids = (DOM_SID *)talloc(hnd->mem_ctx, sizeof(DOM_SID));
230
231                 string_to_sid(&sids[0], PyString_AsString(py_sids));
232         }
233
234         ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
235                                        num_sids, sids, &domains, &names, 
236                                        &types);
237
238         if (!NT_STATUS_IS_OK(ntstatus)) {
239                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
240                 return NULL;
241         }
242
243         result = PyList_New(num_sids);
244
245         for (i = 0; i < num_sids; i++) {
246                 PyObject *obj;
247
248                 obj = Py_BuildValue("{sssssi}", "username", names[i],
249                                     "domain", domains[i], "name_type", 
250                                     types[i]);
251
252                 PyList_SetItem(result, i, obj);
253         }
254         
255         return result;
256 }
257
258 static PyObject *lsa_enum_trust_dom(PyObject *self, PyObject *args)
259 {
260         lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
261         NTSTATUS ntstatus;
262         uint32 enum_ctx = 0, num_domains, i;
263         char **domain_names;
264         DOM_SID *domain_sids;
265         PyObject *result;
266
267         if (!PyArg_ParseTuple(args, ""))
268                 return NULL;
269         
270         ntstatus = cli_lsa_enum_trust_dom(hnd->cli, hnd->mem_ctx,
271                                           &hnd->pol, &enum_ctx,
272                                           &num_domains, &domain_names,
273                                           &domain_sids);
274
275         if (!NT_STATUS_IS_OK(ntstatus)) {
276                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
277                 return NULL;
278         }
279
280         result = PyList_New(num_domains);
281
282         for (i = 0; i < num_domains; i++) {
283                 fstring sid_str;
284
285                 sid_to_string(sid_str, &domain_sids[i]);
286                 PyList_SetItem(
287                         result, i, 
288                         Py_BuildValue("(ss)", domain_names[i], sid_str));
289         }
290
291         return result;
292 }
293
294 /*
295  * Method dispatch tables
296  */
297
298 static PyMethodDef lsa_hnd_methods[] = {
299
300         /* SIDs<->names */
301
302         { "lookup_sids", (PyCFunction)lsa_lookup_sids, 
303           METH_VARARGS | METH_KEYWORDS,
304           "Convert sids to names." },
305
306         { "lookup_names", (PyCFunction)lsa_lookup_names, 
307           METH_VARARGS | METH_KEYWORDS,
308           "Convert names to sids." },
309
310         /* Trusted domains */
311
312         { "enum_trusted_domains", (PyCFunction)lsa_enum_trust_dom, 
313           METH_VARARGS, 
314           "Enumerate trusted domains." },
315
316         { NULL }
317 };
318
319 static void py_lsa_policy_hnd_dealloc(PyObject* self)
320 {
321         PyObject_Del(self);
322 }
323
324 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
325 {
326         return Py_FindMethod(lsa_hnd_methods, self, attrname);
327 }
328
329 PyTypeObject lsa_policy_hnd_type = {
330         PyObject_HEAD_INIT(NULL)
331         0,
332         "LSA Policy Handle",
333         sizeof(lsa_policy_hnd_object),
334         0,
335         py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
336         0,          /*tp_print*/
337         py_lsa_policy_hnd_getattr,          /*tp_getattr*/
338         0,          /*tp_setattr*/
339         0,          /*tp_compare*/
340         0,          /*tp_repr*/
341         0,          /*tp_as_number*/
342         0,          /*tp_as_sequence*/
343         0,          /*tp_as_mapping*/
344         0,          /*tp_hash */
345 };
346
347 static PyMethodDef lsa_methods[] = {
348
349         /* Open/close lsa handles */
350         
351         { "open_policy", (PyCFunction)lsa_open_policy, 
352           METH_VARARGS | METH_KEYWORDS, 
353           "Open a policy handle" },
354         
355         { "close", (PyCFunction)lsa_close, 
356           METH_VARARGS, 
357           "Close a policy handle" },
358
359         { NULL }
360 };
361
362 static struct const_vals {
363         char *name;
364         uint32 value;
365 } module_const_vals[] = {
366         { NULL }
367 };
368
369 static void const_init(PyObject *dict)
370 {
371         struct const_vals *tmp;
372         PyObject *obj;
373
374         for (tmp = module_const_vals; tmp->name; tmp++) {
375                 obj = PyInt_FromLong(tmp->value);
376                 PyDict_SetItemString(dict, tmp->name, obj);
377                 Py_DECREF(obj);
378         }
379 }
380
381 /*
382  * Module initialisation 
383  */
384
385 void initlsa(void)
386 {
387         PyObject *module, *dict;
388
389         /* Initialise module */
390
391         module = Py_InitModule("lsa", lsa_methods);
392         dict = PyModule_GetDict(module);
393
394         lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
395         PyDict_SetItemString(dict, "error", lsa_error);
396
397         lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
398         PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
399
400         /* Initialise policy handle object */
401
402         lsa_policy_hnd_type.ob_type = &PyType_Type;
403
404         /* Initialise constants */
405
406         const_init(dict);
407
408         /* Do samba initialisation */
409
410         py_samba_init();
411
412         setup_logging("lsa", True);
413         DEBUGLEVEL = 10;
414 }