Added copyright.
[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 "includes.h"
22 #include "Python.h"
23
24 #include "python/py_lsa.h"
25
26 PyObject *new_lsa_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
27                                     POLICY_HND *pol)
28 {
29         lsa_policy_hnd_object *o;
30
31         o = PyObject_New(lsa_policy_hnd_object, &lsa_policy_hnd_type);
32
33         o->cli = cli;
34         o->mem_ctx = mem_ctx;
35         memcpy(&o->pol, pol, sizeof(POLICY_HND));
36
37         return (PyObject*)o;
38 }
39
40 /* 
41  * Exceptions raised by this module 
42  */
43
44 PyObject *lsa_error;            /* This indicates a non-RPC related error
45                                    such as name lookup failure */
46
47 PyObject *lsa_ntstatus;         /* This exception is raised when a RPC call
48                                    returns a status code other than
49                                    NT_STATUS_OK */
50
51 /*
52  * Open/close lsa handles
53  */
54
55 static PyObject *lsa_open_policy(PyObject *self, PyObject *args, 
56                                 PyObject *kw) 
57 {
58         static char *kwlist[] = { "servername", "creds", "access", NULL };
59         char *server_name;
60         PyObject *creds = NULL, *result;
61         uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
62         struct cli_state *cli;
63         NTSTATUS ntstatus;
64         TALLOC_CTX *mem_ctx;
65         POLICY_HND hnd;
66
67         if (!PyArg_ParseTupleAndKeywords(
68                 args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type,
69                 &creds, &desired_access))
70                 return NULL;
71
72         if (!(cli = open_pipe_creds(server_name, creds, cli_lsa_initialise,
73                                     NULL))) {
74                 fprintf(stderr, "could not initialise cli state\n");
75                 return NULL;
76         }
77
78         if (!(mem_ctx = talloc_init())) {
79                 fprintf(stderr, "unable to initialise talloc context\n");
80                 return NULL;
81         }
82
83         ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
84                                        SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);
85
86         if (!NT_STATUS_IS_OK(ntstatus)) {
87                 cli_shutdown(cli);
88                 SAFE_FREE(cli);
89                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
90                 return NULL;
91         }
92
93         result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);
94
95         return result;
96 }
97
98 static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw) 
99 {
100         PyObject *po;
101         lsa_policy_hnd_object *hnd;
102         NTSTATUS result;
103
104         /* Parse parameters */
105
106         if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
107                 return NULL;
108
109         hnd = (lsa_policy_hnd_object *)po;
110
111         /* Call rpc function */
112
113         result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);
114
115         /* Cleanup samba stuff */
116
117         cli_shutdown(hnd->cli);
118         talloc_destroy(hnd->mem_ctx);
119
120         /* Return value */
121
122         Py_INCREF(Py_None);
123         return Py_None; 
124 }
125
126 static PyObject *lsa_lookup_names(PyObject *self, PyObject *args)
127 {
128         PyObject *py_names, *result;
129         NTSTATUS ntstatus;
130         lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
131         int num_names, i;
132         const char **names;
133         DOM_SID *sids;
134         uint32 *name_types;
135
136         if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &py_names))
137                 return NULL;
138
139         /* Convert dictionary to char ** array */
140
141         num_names = PyList_Size(py_names);
142         names = (const char **)talloc(
143                 hnd->mem_ctx, num_names * sizeof(char *));
144
145         for (i = 0; i < num_names; i++) {
146                 PyObject *obj = PyList_GetItem(py_names, i);
147
148                 names[i] = talloc_strdup(hnd->mem_ctx, PyString_AsString(obj));
149         }
150
151         ntstatus = cli_lsa_lookup_names(hnd->cli, hnd->mem_ctx, &hnd->pol,
152                                         num_names, names, &sids, &name_types);
153
154         if (!NT_STATUS_IS_OK(ntstatus) && NT_STATUS_V(ntstatus) != 0x107) {
155                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
156                 return NULL;
157         }
158
159         result = PyList_New(num_names);
160
161         for (i = 0; i < num_names; i++) {
162                 PyObject *sid_obj, *obj;
163
164                 py_from_SID(&sid_obj, &sids[i]);
165
166                 obj = Py_BuildValue("(Oi)", sid_obj, name_types[i]);
167
168                 PyList_SetItem(result, i, obj);
169         }
170         
171         return result;
172 }
173
174 static PyObject *lsa_lookup_sids(PyObject *self, PyObject *args, 
175                                  PyObject *kw) 
176 {
177         PyObject *py_sids, *result;
178         NTSTATUS ntstatus;
179         int num_sids, i;
180         char **domains, **names;
181         uint32 *types;
182         lsa_policy_hnd_object *hnd = (lsa_policy_hnd_object *)self;
183         DOM_SID *sids;
184
185         if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &py_sids))
186                 return NULL;
187
188         /* Convert dictionary to char ** array */
189
190         num_sids = PyList_Size(py_sids);
191         sids = (DOM_SID *)talloc(hnd->mem_ctx, num_sids * sizeof(DOM_SID));
192
193         memset(sids, 0, num_sids * sizeof(DOM_SID));
194
195         for (i = 0; i < num_sids; i++) {
196                 PyObject *obj = PyList_GetItem(py_sids, i);
197
198                 string_to_sid(&sids[i], PyString_AsString(obj));
199         }
200
201         ntstatus = cli_lsa_lookup_sids(hnd->cli, hnd->mem_ctx, &hnd->pol,
202                                        num_sids, sids, &domains, &names, 
203                                        &types);
204
205         if (!NT_STATUS_IS_OK(ntstatus)) {
206                 PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
207                 return NULL;
208         }
209
210         result = PyList_New(num_sids);
211
212         for (i = 0; i < num_sids; i++) {
213                 PyObject *name_obj, *obj;
214
215                 obj = Py_BuildValue("{sssssi}", "username", names[i],
216                                     "domain", domains[i], "name_type", 
217                                     types[i]);
218
219                 PyList_SetItem(result, i, obj);
220         }
221         
222         return result;
223 }
224
225 /*
226  * Method dispatch tables
227  */
228
229 static PyMethodDef lsa_hnd_methods[] = {
230
231         { "lookup_sids", lsa_lookup_sids, METH_VARARGS | METH_KEYWORDS,
232           "Convert sids to names." },
233
234         { "lookup_names", lsa_lookup_names, METH_VARARGS | METH_KEYWORDS,
235           "Convert names to sids." },
236
237         { NULL }
238 };
239
240 static void py_lsa_policy_hnd_dealloc(PyObject* self)
241 {
242         PyObject_Del(self);
243 }
244
245 static PyObject *py_lsa_policy_hnd_getattr(PyObject *self, char *attrname)
246 {
247         return Py_FindMethod(lsa_hnd_methods, self, attrname);
248 }
249
250 PyTypeObject lsa_policy_hnd_type = {
251         PyObject_HEAD_INIT(NULL)
252         0,
253         "LSA Policy Handle",
254         sizeof(lsa_policy_hnd_object),
255         0,
256         py_lsa_policy_hnd_dealloc, /*tp_dealloc*/
257         0,          /*tp_print*/
258         py_lsa_policy_hnd_getattr,          /*tp_getattr*/
259         0,          /*tp_setattr*/
260         0,          /*tp_compare*/
261         0,          /*tp_repr*/
262         0,          /*tp_as_number*/
263         0,          /*tp_as_sequence*/
264         0,          /*tp_as_mapping*/
265         0,          /*tp_hash */
266 };
267
268 static PyMethodDef lsa_methods[] = {
269
270         /* Open/close lsa handles */
271         
272         { "open_policy", lsa_open_policy, METH_VARARGS | METH_KEYWORDS, 
273           "Open a policy handle" },
274         
275         { "close", lsa_close, METH_VARARGS, "Close a policy handle" },
276
277         { NULL }
278 };
279
280 /*
281  * Module initialisation 
282 */
283
284 void initlsa(void)
285 {
286         PyObject *module, *dict;
287
288         /* Initialise module */
289
290         module = Py_InitModule("lsa", lsa_methods);
291         dict = PyModule_GetDict(module);
292
293         lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
294         PyDict_SetItemString(dict, "error", lsa_error);
295
296         lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
297         PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);
298
299         /* Initialise policy handle object */
300
301         lsa_policy_hnd_type.ob_type = &PyType_Type;
302
303         /* Initialise constants */
304
305 //      const_init(dict);
306
307         /* Do samba initialisation */
308
309         py_samba_init();
310
311         setup_logging("lsa", True);
312         DEBUGLEVEL = 10;
313 }