s4-libnet: Python binding for libnet_SetPassword()
authorKamen Mazdrashki <kamen.mazdrashki@postpath.com>
Thu, 24 Dec 2009 03:43:21 +0000 (05:43 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 25 Dec 2009 13:15:42 +0000 (14:15 +0100)
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
source4/libnet/py_net.c

index 4d3e81ce26c16ca0a6d756f0575257c3e8d10f97..01afe595c4c51fac3f4d998a744c43ce6fe6be43 100644 (file)
@@ -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/>.
 */
@@ -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=<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);
 }
+