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