2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "auth/credentials/pycredentials.h"
25 #include "libcli/security/security.h"
26 #include "lib/events/events.h"
27 #include "param/param.h"
29 /* FIXME: This prototype should be in param/pyparam.h */
30 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx);
32 static struct libnet_context *py_net_ctx(PyObject *obj, struct tevent_context *ev, struct cli_credentials *creds)
35 struct libnet_context *libnet;
36 libnet = libnet_context_init(ev, py_default_loadparm_context(NULL));
44 static PyObject *py_net_join(PyObject *cls, PyObject *args, PyObject *kwargs)
50 struct tevent_context *ev;
51 struct libnet_context *libnet_ctx;
52 struct cli_credentials *creds;
54 const char *kwnames[] = { "domain_name", "netbios_name", "join_type", "level", "credentials", NULL };
56 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssiiO:Join", discard_const_p(char *, kwnames),
57 &r.in.domain_name, &r.in.netbios_name,
58 &r.in.join_type, &r.in.level, &py_creds))
61 /* FIXME: we really need to get a context from the caller or we may end
62 * up with 2 event contexts */
63 ev = s4_event_context_init(NULL);
64 mem_ctx = talloc_new(ev);
66 creds = cli_credentials_from_py_object(py_creds);
68 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
73 libnet_ctx = py_net_ctx(cls, ev, creds);
74 if (libnet_ctx == NULL) {
75 PyErr_SetString(PyExc_RuntimeError, "Unable to initialize libnet");
80 status = libnet_Join(libnet_ctx, mem_ctx, &r);
81 if (NT_STATUS_IS_ERR(status)) {
82 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
87 result = Py_BuildValue("sss", r.out.join_password,
88 dom_sid_string(mem_ctx, r.out.domain_sid),
96 static const char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
97 "Join the domain with the specified name.";
99 static PyObject *py_net_set_password(PyObject *cls, PyObject *args, PyObject *kwargs)
101 union libnet_SetPassword r;
105 struct tevent_context *ev;
106 struct libnet_context *libnet_ctx;
107 struct cli_credentials *creds;
108 const char *kwnames[] = { "account_name", "domain_name", "newpassword", "credentials", NULL };
110 r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
112 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssO:set_password", discard_const_p(char *, kwnames),
113 &r.generic.in.account_name, &r.generic.in.domain_name,
114 &r.generic.in.newpassword, &py_creds)) {
118 /* FIXME: we really need to get a context from the caller or we may end
119 * up with 2 event contexts */
120 ev = s4_event_context_init(NULL);
121 mem_ctx = talloc_new(ev);
123 creds = cli_credentials_from_py_object(py_creds);
125 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
129 libnet_ctx = py_net_ctx(cls, ev, creds);
131 status = libnet_SetPassword(libnet_ctx, mem_ctx, &r);
132 if (NT_STATUS_IS_ERR(status)) {
133 PyErr_SetString(PyExc_RuntimeError, r.generic.out.error_string);
134 talloc_free(mem_ctx);
138 talloc_free(mem_ctx);
143 static const char py_net_set_password_doc[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
144 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
145 "Sample usage is:\n" \
146 "creds = samba.credentials.Credentials()\n" \
147 "creds.set_username('admin_user')\n" \
148 "creds.set_domain('domain_name')\n" \
149 "creds.set_password('pass')\n\n" \
150 "net.set_password(account_name=<account_name>,\n" \
151 " domain_name=creds.get_domain(),\n" \
152 " newpassword=new_pass,\n" \
153 " credentials=creds)\n";
156 static PyObject *py_net_export_keytab(PyObject *cls, PyObject *args, PyObject *kwargs)
158 struct libnet_export_keytab r;
159 struct tevent_context *ev;
161 const char *kwnames[] = { "keytab", "creds", NULL };
162 struct libnet_context *libnet_ctx;
164 struct cli_credentials *creds;
167 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO:export_keytab", discard_const_p(char *, kwnames),
168 &r.in.keytab_name, &py_creds)) {
172 creds = cli_credentials_from_py_object(py_creds);
174 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
178 /* FIXME: we really need to get a context from the caller or we may end
179 * up with 2 event contexts */
180 ev = s4_event_context_init(NULL);
181 mem_ctx = talloc_new(ev);
183 libnet_ctx = py_net_ctx(cls, ev, creds);
185 status = libnet_export_keytab(libnet_ctx, mem_ctx, &r);
186 if (NT_STATUS_IS_ERR(status)) {
187 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
188 talloc_free(mem_ctx);
192 talloc_free(mem_ctx);
197 static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
198 "Export the DC keytab to a keytab file.";
200 static struct PyMethodDef net_methods[] = {
201 {"join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc},
202 {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc},
203 {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
209 Py_InitModule3("net", net_methods, NULL);