diff options
Diffstat (limited to 'source4/libnet')
-rw-r--r-- | source4/libnet/libnet.c | 3 | ||||
-rw-r--r-- | source4/libnet/libnet_passwd.c | 8 | ||||
-rw-r--r-- | source4/libnet/py_net.c | 78 |
3 files changed, 78 insertions, 11 deletions
diff --git a/source4/libnet/libnet.c b/source4/libnet/libnet.c index b10fb65df6..86cf80b4da 100644 --- a/source4/libnet/libnet.c +++ b/source4/libnet/libnet.c @@ -42,6 +42,9 @@ struct libnet_context *libnet_context_init(struct tevent_context *ev, ctx->event_ctx = ev; ctx->lp_ctx = lp_ctx; + /* make sure dcerpc is initialized */ + dcerpc_init(lp_ctx); + /* name resolution methods */ ctx->resolve_ctx = lp_resolve_context(lp_ctx); diff --git a/source4/libnet/libnet_passwd.c b/source4/libnet/libnet_passwd.c index cb09e3041e..8195347b83 100644 --- a/source4/libnet/libnet_passwd.c +++ b/source4/libnet/libnet_passwd.c @@ -109,11 +109,11 @@ static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CT nt_errstr(status)); r->samr.out.error_string = talloc_asprintf(mem_ctx, "samr_ChangePasswordUser3 for '%s\\%s' failed: %s", - r->samr.in.domain_name, r->samr.in.account_name, + r->samr.in.domain_name, r->samr.in.account_name, nt_errstr(status)); } goto disconnect; - } + } /* prepare samr_ChangePasswordUser2 */ encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_ASCII|STR_TERMINATE); @@ -216,7 +216,7 @@ static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CT #endif disconnect: /* close connection */ - talloc_free(c.out.dcerpc_pipe); + talloc_unlink(ctx, c.out.dcerpc_pipe); return status; } @@ -627,7 +627,7 @@ static NTSTATUS libnet_SetPassword_samr(struct libnet_context *ctx, TALLOC_CTX * disconnect: /* close connection */ - talloc_free(c.out.dcerpc_pipe); + talloc_unlink(ctx, c.out.dcerpc_pipe); return status; } diff --git a/source4/libnet/py_net.c b/source4/libnet/py_net.c index 4d3e81ce26..e5ca5e1c97 100644 --- a/source4/libnet/py_net.c +++ b/source4/libnet/py_net.c @@ -1,18 +1,19 @@ -/* +/* Unix SMB/CIFS implementation. Samba utility functions Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008 - + Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009 + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ @@ -65,10 +66,16 @@ static PyObject *py_net_join(PyObject *cls, PyObject *args, PyObject *kwargs) creds = cli_credentials_from_py_object(py_creds); if (creds == NULL) { PyErr_SetString(PyExc_TypeError, "Expected credentials object"); + talloc_free(mem_ctx); return NULL; } libnet_ctx = py_net_ctx(cls, ev, creds); + if (libnet_ctx == NULL) { + PyErr_SetString(PyExc_RuntimeError, "Unable to initialize libnet"); + talloc_free(mem_ctx); + return NULL; + } status = libnet_Join(libnet_ctx, mem_ctx, &r); if (NT_STATUS_IS_ERR(status)) { @@ -77,7 +84,7 @@ static PyObject *py_net_join(PyObject *cls, PyObject *args, PyObject *kwargs) return NULL; } - result = Py_BuildValue("sss", r.out.join_password, + result = Py_BuildValue("sss", r.out.join_password, dom_sid_string(mem_ctx, r.out.domain_sid), r.out.domain_name); @@ -89,15 +96,72 @@ static PyObject *py_net_join(PyObject *cls, PyObject *args, PyObject *kwargs) return result; } -static char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \ +static PyObject *py_net_set_password(PyObject *cls, PyObject *args, PyObject *kwargs) +{ + union libnet_SetPassword r; + NTSTATUS status; + PyObject *py_creds; + TALLOC_CTX *mem_ctx; + struct tevent_context *ev; + struct libnet_context *libnet_ctx; + struct cli_credentials *creds; + const char *kwnames[] = { "account_name", "domain_name", "newpassword", "credentials", NULL }; + + r.generic.level = LIBNET_SET_PASSWORD_GENERIC; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssO:SetPassword", discard_const_p(char *, kwnames), + &r.generic.in.account_name, &r.generic.in.domain_name, + &r.generic.in.newpassword, &py_creds)) { + return NULL; + } + + /* FIXME: we really need to get a context from the caller or we may end + * up with 2 event contexts */ + ev = s4_event_context_init(NULL); + mem_ctx = talloc_new(ev); + + creds = cli_credentials_from_py_object(py_creds); + if (creds == NULL) { + PyErr_SetString(PyExc_TypeError, "Expected credentials object"); + return NULL; + } + + libnet_ctx = py_net_ctx(cls, ev, creds); + + status = libnet_SetPassword(libnet_ctx, mem_ctx, &r); + if (NT_STATUS_IS_ERR(status)) { + PyErr_SetString(PyExc_RuntimeError, r.generic.out.error_string); + talloc_free(mem_ctx); + return NULL; + } + + Py_RETURN_NONE; +} + +static const char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \ "Join the domain with the specified name."; +static const char py_net_set_password_doc[] = "SetPassword(account_name, domain_name, newpassword) -> True\n\n" \ +"Set password for a user. You must supply credential with enough rights to do this.\n\n" \ +"Sample usage is:\n" \ +"creds = samba.credentials.Credentials()\n" \ +"creds.set_username('admin_user')\n" \ +"creds.set_domain('domain_name')\n" \ +"creds.set_password('pass')\n\n" \ +"net.SetPassword(account_name=<account_name>,\n" \ +" domain_name=creds.get_domain(),\n" \ +" newpassword=new_pass,\n" \ +" credentials=creds)\n"; + + static struct PyMethodDef net_methods[] = { {"Join", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc}, + {"SetPassword", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc}, {NULL } }; void initnet(void) { - Py_InitModule("net", net_methods); + Py_InitModule3("net", net_methods, NULL); } + |