2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
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"
28 #include "param/pyparam.h"
32 struct libnet_context *libnet_ctx;
34 struct tevent_context *ev;
37 static PyObject *py_net_join(py_net_Object *self, PyObject *args, PyObject *kwargs)
44 const char *kwnames[] = { "domain_name", "netbios_name", "join_type", "level", "credentials", NULL };
46 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssiiO:Join", discard_const_p(char *, kwnames),
47 &r.in.domain_name, &r.in.netbios_name,
48 &r.in.join_type, &r.in.level, &py_creds))
51 mem_ctx = talloc_new(self->mem_ctx);
53 status = libnet_Join(self->libnet_ctx, mem_ctx, &r);
54 if (NT_STATUS_IS_ERR(status)) {
55 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
60 result = Py_BuildValue("sss", r.out.join_password,
61 dom_sid_string(mem_ctx, r.out.domain_sid),
69 static const char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
70 "Join the domain with the specified name.";
72 static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
74 union libnet_SetPassword r;
78 struct tevent_context *ev;
79 const char *kwnames[] = { "account_name", "domain_name", "newpassword", "credentials", NULL };
81 r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
83 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssO:set_password", discard_const_p(char *, kwnames),
84 &r.generic.in.account_name, &r.generic.in.domain_name,
85 &r.generic.in.newpassword, &py_creds)) {
89 /* FIXME: we really need to get a context from the caller or we may end
90 * up with 2 event contexts */
91 ev = s4_event_context_init(NULL);
92 mem_ctx = talloc_new(ev);
94 status = libnet_SetPassword(self->libnet_ctx, mem_ctx, &r);
95 if (NT_STATUS_IS_ERR(status)) {
96 PyErr_SetString(PyExc_RuntimeError, r.generic.out.error_string);
101 talloc_free(mem_ctx);
106 static const char py_net_set_password_doc[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
107 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
108 "Sample usage is:\n" \
109 "net.set_password(account_name=<account_name>,\n" \
110 " domain_name=domain_name,\n" \
111 " newpassword=new_pass)\n";
114 static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObject *kwargs)
116 struct libnet_export_keytab r;
118 const char *kwnames[] = { "keytab", NULL };
121 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:export_keytab", discard_const_p(char *, kwnames),
122 &r.in.keytab_name)) {
126 mem_ctx = talloc_new(self->mem_ctx);
128 status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
129 if (NT_STATUS_IS_ERR(status)) {
130 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
131 talloc_free(mem_ctx);
135 talloc_free(mem_ctx);
140 static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
141 "Export the DC keytab to a keytab file.";
143 static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwargs)
145 const char *kwnames[] = { "server_name", NULL };
146 union libnet_RemoteTOD r;
153 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s",
154 discard_const_p(char *, kwnames), &r.generic.in.server_name))
157 r.generic.level = LIBNET_REMOTE_TOD_GENERIC;
159 mem_ctx = talloc_new(NULL);
160 if (mem_ctx == NULL) {
165 status = libnet_RemoteTOD(self->libnet_ctx, mem_ctx, &r);
166 if (!NT_STATUS_IS_OK(status)) {
167 PyErr_SetString(PyExc_RuntimeError, r.generic.out.error_string);
168 talloc_free(mem_ctx);
172 ZERO_STRUCT(timestr);
173 tm = localtime(&r.generic.out.time);
174 strftime(timestr, sizeof(timestr)-1, "%c %Z",tm);
176 ret = PyString_FromString(timestr);
178 talloc_free(mem_ctx);
183 static const char py_net_time_doc[] = "time(server_name) -> timestr\n"
184 "Retrieve the remote time on a server";
186 static PyObject *py_net_user_create(py_net_Object *self, PyObject *args, PyObject *kwargs)
188 const char *kwnames[] = { "username", NULL };
191 struct libnet_CreateUser r;
193 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames),
197 r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
199 mem_ctx = talloc_new(NULL);
200 if (mem_ctx == NULL) {
205 status = libnet_CreateUser(self->libnet_ctx, mem_ctx, &r);
206 if (!NT_STATUS_IS_OK(status)) {
207 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
208 talloc_free(mem_ctx);
212 talloc_free(mem_ctx);
217 static const char py_net_create_user_doc[] = "create_user(username)\n"
218 "Create a new user.";
220 static PyObject *py_net_user_delete(py_net_Object *self, PyObject *args, PyObject *kwargs)
222 const char *kwnames[] = { "username", NULL };
225 struct libnet_DeleteUser r;
227 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames),
231 r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
233 mem_ctx = talloc_new(NULL);
234 if (mem_ctx == NULL) {
239 status = libnet_DeleteUser(self->libnet_ctx, mem_ctx, &r);
240 if (!NT_STATUS_IS_OK(status)) {
241 PyErr_SetString(PyExc_RuntimeError, r.out.error_string);
242 talloc_free(mem_ctx);
246 talloc_free(mem_ctx);
251 static const char py_net_delete_user_doc[] = "delete_user(username)\n"
254 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
256 PyObject *mod_security, *dom_sid_Type;
258 mod_security = PyImport_ImportModule("samba.dcerpc.security");
259 if (mod_security == NULL)
262 dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
263 if (dom_sid_Type == NULL)
266 return py_talloc_reference((PyTypeObject *)dom_sid_Type, sid);
269 static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *kwargs)
271 const char *kwnames[] = { "domain", "target_dir", NULL };
275 struct libnet_Vampire r;
277 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|s", discard_const_p(char *, kwnames),
278 &r.in.domain_name, &r.in.targetdir)) {
282 r.in.netbios_name = lp_netbios_name(self->libnet_ctx->lp_ctx);
283 r.out.error_string = NULL;
285 mem_ctx = talloc_new(NULL);
286 if (mem_ctx == NULL) {
291 status = libnet_Vampire(self->libnet_ctx, mem_ctx, &r);
293 if (!NT_STATUS_IS_OK(status)) {
294 PyErr_SetString(PyExc_RuntimeError,
295 r.out.error_string ? r.out.error_string : nt_errstr(status));
296 talloc_free(mem_ctx);
300 ret = Py_BuildValue("(sO)", r.out.domain_name, py_dom_sid_FromSid(r.out.domain_sid));
302 talloc_free(mem_ctx);
307 static const char py_net_vampire_doc[] = "vampire(domain, target_dir=None)\n"
310 static PyMethodDef net_obj_methods[] = {
311 {"join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc},
312 {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc},
313 {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
314 {"time", (PyCFunction)py_net_time, METH_VARARGS|METH_KEYWORDS, py_net_time_doc},
315 {"create_user", (PyCFunction)py_net_user_create, METH_VARARGS|METH_KEYWORDS, py_net_create_user_doc},
316 {"delete_user", (PyCFunction)py_net_user_delete, METH_VARARGS|METH_KEYWORDS, py_net_delete_user_doc},
317 {"vampire", (PyCFunction)py_net_vampire, METH_VARARGS|METH_KEYWORDS, py_net_vampire_doc},
321 static void py_net_dealloc(py_net_Object *self)
323 talloc_free(self->mem_ctx);
326 static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
328 PyObject *py_creds, *py_lp = Py_None;
329 const char *kwnames[] = { "creds", "lp", NULL };
331 struct loadparm_context *lp;
333 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O",
334 discard_const_p(char *, kwnames), &py_creds, &py_lp))
337 ret = PyObject_New(py_net_Object, type);
342 /* FIXME: we really need to get a context from the caller or we may end
343 * up with 2 event contexts */
344 ret->ev = s4_event_context_init(NULL);
345 ret->mem_ctx = talloc_new(ret->ev);
347 lp = lp_from_py_object(ret->mem_ctx, py_lp);
353 ret->libnet_ctx = libnet_context_init(ret->ev, lp);
354 if (ret->libnet_ctx == NULL) {
355 PyErr_SetString(PyExc_RuntimeError, "Unable to initialize net");
360 ret->libnet_ctx->cred = cli_credentials_from_py_object(py_creds);
361 if (ret->libnet_ctx->cred == NULL) {
362 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
367 return (PyObject *)ret;
371 PyTypeObject py_net_Type = {
372 PyObject_HEAD_INIT(NULL) 0,
373 .tp_name = "net.Net",
374 .tp_basicsize = sizeof(py_net_Object),
375 .tp_dealloc = (destructor)py_net_dealloc,
376 .tp_methods = net_obj_methods,
377 .tp_new = net_obj_new,
384 if (PyType_Ready(&py_net_Type) < 0)
387 m = Py_InitModule3("net", NULL, NULL);
391 Py_INCREF(&py_net_Type);
392 PyModule_AddObject(m, "Net", (PyObject *)&py_net_Type);