From f03e88fa4bc1101ca9031d697f33e36effb669fd Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Thu, 24 Dec 2009 04:08:12 +0200 Subject: s4-net: Fix 'talloc_free with references ...' error Signed-off-by: Jelmer Vernooij --- source4/libnet/libnet_passwd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source4/libnet') 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; } -- cgit From 20c7c27322e95ab75ea711a825e593b0adac9114 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Thu, 24 Dec 2009 05:43:21 +0200 Subject: s4-libnet: Python binding for libnet_SetPassword() Signed-off-by: Jelmer Vernooij --- source4/libnet/py_net.c | 72 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) (limited to 'source4/libnet') diff --git a/source4/libnet/py_net.c b/source4/libnet/py_net.c index 4d3e81ce26..01afe595c4 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 2008 - + Copyright (C) Kamen Mazdrashki 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 . */ @@ -77,7 +78,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,11 +90,69 @@ 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; + } + + dcerpc_init(py_default_loadparm_context(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=,\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 } }; @@ -101,3 +160,4 @@ void initnet(void) { Py_InitModule("net", net_methods); } + -- cgit From ef453c63af3d29c428029aa39a5b59b0317c76ce Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 25 Dec 2009 14:48:45 +0100 Subject: py_net/libnet: Remove C++-style comments, add more error checking, move initialization of dcerpc subsystem to libnet. --- source4/libnet/libnet.c | 3 +++ source4/libnet/py_net.c | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'source4/libnet') 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/py_net.c b/source4/libnet/py_net.c index 01afe595c4..e5ca5e1c97 100644 --- a/source4/libnet/py_net.c +++ b/source4/libnet/py_net.c @@ -66,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)) { @@ -109,8 +115,8 @@ static PyObject *py_net_set_password(PyObject *cls, PyObject *args, PyObject *kw return NULL; } - // FIXME: we really need to get a context from the caller or we may end - // up with 2 event contexts + /* 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); @@ -120,8 +126,6 @@ static PyObject *py_net_set_password(PyObject *cls, PyObject *args, PyObject *kw return NULL; } - dcerpc_init(py_default_loadparm_context(NULL)); - libnet_ctx = py_net_ctx(cls, ev, creds); status = libnet_SetPassword(libnet_ctx, mem_ctx, &r); @@ -158,6 +162,6 @@ static struct PyMethodDef net_methods[] = { void initnet(void) { - Py_InitModule("net", net_methods); + Py_InitModule3("net", net_methods, NULL); } -- cgit