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
),
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:SetPassword", 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
);
141 static const char py_net_join_doc
[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
142 "Join the domain with the specified name.";
144 static const char py_net_set_password_doc
[] = "SetPassword(account_name, domain_name, newpassword) -> True\n\n" \
145 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
146 "Sample usage is:\n" \
147 "creds = samba.credentials.Credentials()\n" \
148 "creds.set_username('admin_user')\n" \
149 "creds.set_domain('domain_name')\n" \
150 "creds.set_password('pass')\n\n" \
151 "net.SetPassword(account_name=<account_name>,\n" \
152 " domain_name=creds.get_domain(),\n" \
153 " newpassword=new_pass,\n" \
154 " credentials=creds)\n";
157 static struct PyMethodDef net_methods
[] = {
158 {"Join", (PyCFunction
)py_net_join
, METH_VARARGS
|METH_KEYWORDS
, py_net_join_doc
},
159 {"SetPassword", (PyCFunction
)py_net_set_password
, METH_VARARGS
|METH_KEYWORDS
, py_net_set_password_doc
},
165 Py_InitModule3("net", net_methods
, NULL
);