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