s4-python: we need to include Python.h first
[samba.git] / source4 / libnet / py_net.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <Python.h>
21 #include "includes.h"
22 #include "libnet.h"
23 #include "auth/credentials/pycredentials.h"
24 #include "libcli/security/security.h"
25 #include "lib/events/events.h"
26 #include "param/param.h"
27
28 /* FIXME: This prototype should be in param/pyparam.h */
29 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx);
30
31 static struct libnet_context *py_net_ctx(PyObject *obj, struct tevent_context *ev, struct cli_credentials *creds)
32 {
33 /* FIXME: Use obj */
34         struct libnet_context *libnet;
35         libnet = libnet_context_init(ev, py_default_loadparm_context(NULL));
36         if (!libnet) {
37                 return NULL;
38         }
39         libnet->cred = creds;
40         return libnet;
41 }
42
43 static PyObject *py_net_join(PyObject *cls, PyObject *args, PyObject *kwargs)
44 {
45         struct libnet_Join r;
46         NTSTATUS status;
47         PyObject *result;
48         TALLOC_CTX *mem_ctx;
49         struct tevent_context *ev;
50         struct libnet_context *libnet_ctx;
51         struct cli_credentials *creds;
52         PyObject *py_creds;     
53         const char *kwnames[] = { "domain_name", "netbios_name", "join_type", "level", "credentials", NULL };
54
55         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssiiO:Join", discard_const_p(char *, kwnames), 
56                                          &r.in.domain_name, &r.in.netbios_name, 
57                                          &r.in.join_type, &r.in.level, &py_creds))
58                 return NULL;
59
60         /* FIXME: we really need to get a context from the caller or we may end
61          * up with 2 event contexts */
62         ev = s4_event_context_init(NULL);
63         mem_ctx = talloc_new(ev);
64
65         creds = cli_credentials_from_py_object(py_creds);
66         if (creds == NULL) {
67                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
68                 return NULL;
69         }
70
71         libnet_ctx = py_net_ctx(cls, ev, creds);
72
73         status = libnet_Join(libnet_ctx, mem_ctx, &r);
74         if (NT_STATUS_IS_ERR(status)) {
75                 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
76                 talloc_free(mem_ctx);
77                 return NULL;
78         }
79
80         result = Py_BuildValue("sss", r.out.join_password, 
81                                dom_sid_string(mem_ctx, r.out.domain_sid),
82                                r.out.domain_name);
83
84         talloc_free(mem_ctx);
85
86         if (result == NULL)
87                 return NULL;
88
89         return result;
90 }
91
92 static char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
93 "Join the domain with the specified name.";
94
95 static struct PyMethodDef net_methods[] = {
96         {"Join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc},
97         {NULL }
98 };
99
100 void initnet(void)
101 {
102         Py_InitModule("net", net_methods);
103 }