2
Unix SMB/CIFS implementation.
3
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 3 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "pycredentials.h"
22
#include "param/param.h"
23
#include "lib/cmdline/credentials.h"
24
#include "librpc/gen_ndr/samr.h" /* for struct samr_Password */
25
#include "libcli/util/pyerrors.h"
26
#include "param/pyparam.h"
28
#ifndef Py_RETURN_NONE
29
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
32
struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj)
34
if (py_obj == Py_None) {
35
return cli_credentials_init_anon(NULL);
38
return PyCredentials_AsCliCredentials(py_obj);
41
static PyObject *PyString_FromStringOrNULL(const char *str)
45
return PyString_FromString(str);
48
static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
50
return py_talloc_import(type, cli_credentials_init(NULL));
53
static PyObject *py_creds_get_username(py_talloc_Object *self)
55
return PyString_FromStringOrNULL(cli_credentials_get_username(PyCredentials_AsCliCredentials(self)));
58
static PyObject *py_creds_set_username(py_talloc_Object *self, PyObject *args)
61
enum credentials_obtained obt = CRED_SPECIFIED;
62
if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
65
return PyBool_FromLong(cli_credentials_set_username(PyCredentials_AsCliCredentials(self), newval, obt));
68
static PyObject *py_creds_get_password(py_talloc_Object *self)
70
return PyString_FromStringOrNULL(cli_credentials_get_password(PyCredentials_AsCliCredentials(self)));
74
static PyObject *py_creds_set_password(py_talloc_Object *self, PyObject *args)
77
enum credentials_obtained obt = CRED_SPECIFIED;
78
if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
81
return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
84
static PyObject *py_creds_get_domain(py_talloc_Object *self)
86
return PyString_FromStringOrNULL(cli_credentials_get_domain(PyCredentials_AsCliCredentials(self)));
89
static PyObject *py_creds_set_domain(py_talloc_Object *self, PyObject *args)
92
enum credentials_obtained obt = CRED_SPECIFIED;
93
if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
96
return PyBool_FromLong(cli_credentials_set_domain(PyCredentials_AsCliCredentials(self), newval, obt));
99
static PyObject *py_creds_get_realm(py_talloc_Object *self)
101
return PyString_FromStringOrNULL(cli_credentials_get_realm(PyCredentials_AsCliCredentials(self)));
104
static PyObject *py_creds_set_realm(py_talloc_Object *self, PyObject *args)
107
enum credentials_obtained obt = CRED_SPECIFIED;
108
if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
111
return PyBool_FromLong(cli_credentials_set_realm(PyCredentials_AsCliCredentials(self), newval, obt));
114
static PyObject *py_creds_get_bind_dn(py_talloc_Object *self)
116
return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(PyCredentials_AsCliCredentials(self)));
119
static PyObject *py_creds_set_bind_dn(py_talloc_Object *self, PyObject *args)
122
if (!PyArg_ParseTuple(args, "s", &newval))
125
return PyBool_FromLong(cli_credentials_set_bind_dn(PyCredentials_AsCliCredentials(self), newval));
128
static PyObject *py_creds_get_workstation(py_talloc_Object *self)
130
return PyString_FromStringOrNULL(cli_credentials_get_workstation(PyCredentials_AsCliCredentials(self)));
133
static PyObject *py_creds_set_workstation(py_talloc_Object *self, PyObject *args)
136
enum credentials_obtained obt = CRED_SPECIFIED;
137
if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
140
return PyBool_FromLong(cli_credentials_set_workstation(PyCredentials_AsCliCredentials(self), newval, obt));
143
static PyObject *py_creds_is_anonymous(py_talloc_Object *self)
145
return PyBool_FromLong(cli_credentials_is_anonymous(PyCredentials_AsCliCredentials(self)));
148
static PyObject *py_creds_set_anonymous(py_talloc_Object *self)
150
cli_credentials_set_anonymous(PyCredentials_AsCliCredentials(self));
154
static PyObject *py_creds_authentication_requested(py_talloc_Object *self)
156
return PyBool_FromLong(cli_credentials_authentication_requested(PyCredentials_AsCliCredentials(self)));
159
static PyObject *py_creds_wrong_password(py_talloc_Object *self)
161
return PyBool_FromLong(cli_credentials_wrong_password(PyCredentials_AsCliCredentials(self)));
164
static PyObject *py_creds_set_cmdline_callbacks(py_talloc_Object *self)
166
return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(PyCredentials_AsCliCredentials(self)));
169
static PyObject *py_creds_parse_string(py_talloc_Object *self, PyObject *args)
172
enum credentials_obtained obt = CRED_SPECIFIED;
173
if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
176
cli_credentials_parse_string(PyCredentials_AsCliCredentials(self), newval, obt);
180
static PyObject *py_creds_get_nt_hash(py_talloc_Object *self)
182
const struct samr_Password *ntpw = cli_credentials_get_nt_hash(PyCredentials_AsCliCredentials(self), self->ptr);
184
return PyString_FromStringAndSize(discard_const_p(char, ntpw->hash), 16);
187
static PyObject *py_creds_set_kerberos_state(py_talloc_Object *self, PyObject *args)
190
if (!PyArg_ParseTuple(args, "i", &state))
193
cli_credentials_set_kerberos_state(PyCredentials_AsCliCredentials(self), state);
197
static PyObject *py_creds_guess(py_talloc_Object *self, PyObject *args)
199
PyObject *py_lp_ctx = Py_None;
200
struct loadparm_context *lp_ctx;
201
if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
204
lp_ctx = lp_from_py_object(py_lp_ctx);
208
cli_credentials_guess(PyCredentials_AsCliCredentials(self), lp_ctx);
213
static PyObject *py_creds_set_machine_account(py_talloc_Object *self, PyObject *args)
215
PyObject *py_lp_ctx = Py_None;
216
struct loadparm_context *lp_ctx;
218
if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
221
lp_ctx = lp_from_py_object(py_lp_ctx);
225
status = cli_credentials_set_machine_account(PyCredentials_AsCliCredentials(self), lp_ctx);
226
PyErr_NTSTATUS_IS_ERR_RAISE(status);
231
static PyMethodDef py_creds_methods[] = {
232
{ "get_username", (PyCFunction)py_creds_get_username, METH_NOARGS,
233
"S.get_username() -> username\nObtain username." },
234
{ "set_username", (PyCFunction)py_creds_set_username, METH_VARARGS,
235
"S.set_username(name, obtained=CRED_SPECIFIED) -> None\n"
236
"Change username." },
237
{ "get_password", (PyCFunction)py_creds_get_password, METH_NOARGS,
238
"S.get_password() -> password\n"
239
"Obtain password." },
240
{ "set_password", (PyCFunction)py_creds_set_password, METH_VARARGS,
241
"S.set_password(password, obtained=CRED_SPECIFIED) -> None\n"
242
"Change password." },
243
{ "get_domain", (PyCFunction)py_creds_get_domain, METH_NOARGS,
244
"S.get_domain() -> domain\n"
245
"Obtain domain name." },
246
{ "set_domain", (PyCFunction)py_creds_set_domain, METH_VARARGS,
247
"S.set_domain(domain, obtained=CRED_SPECIFIED) -> None\n"
248
"Change domain name." },
249
{ "get_realm", (PyCFunction)py_creds_get_realm, METH_NOARGS,
250
"S.get_realm() -> realm\n"
251
"Obtain realm name." },
252
{ "set_realm", (PyCFunction)py_creds_set_realm, METH_VARARGS,
253
"S.set_realm(realm, obtained=CRED_SPECIFIED) -> None\n"
254
"Change realm name." },
255
{ "get_bind_dn", (PyCFunction)py_creds_get_bind_dn, METH_NOARGS,
256
"S.get_bind_dn() -> bind dn\n"
258
{ "set_bind_dn", (PyCFunction)py_creds_set_bind_dn, METH_VARARGS,
259
"S.set_bind_dn(bind_dn) -> None\n"
261
{ "is_anonymous", (PyCFunction)py_creds_is_anonymous, METH_NOARGS,
263
{ "set_anonymous", (PyCFunction)py_creds_set_anonymous, METH_NOARGS,
264
"S.set_anonymous() -> None\n"
265
"Use anonymous credentials." },
266
{ "get_workstation", (PyCFunction)py_creds_get_workstation, METH_NOARGS,
268
{ "set_workstation", (PyCFunction)py_creds_set_workstation, METH_VARARGS,
270
{ "authentication_requested", (PyCFunction)py_creds_authentication_requested, METH_NOARGS,
272
{ "wrong_password", (PyCFunction)py_creds_wrong_password, METH_NOARGS,
273
"S.wrong_password() -> bool\n"
274
"Indicate the returned password was incorrect." },
275
{ "set_cmdline_callbacks", (PyCFunction)py_creds_set_cmdline_callbacks, METH_NOARGS,
276
"S.set_cmdline_callbacks() -> bool\n"
277
"Use command-line to obtain credentials not explicitly set." },
278
{ "parse_string", (PyCFunction)py_creds_parse_string, METH_VARARGS,
279
"S.parse_string(text, obtained=CRED_SPECIFIED) -> None\n"
280
"Parse credentials string." },
281
{ "get_nt_hash", (PyCFunction)py_creds_get_nt_hash, METH_NOARGS,
283
{ "set_kerberos_state", (PyCFunction)py_creds_set_kerberos_state, METH_VARARGS,
285
{ "guess", (PyCFunction)py_creds_guess, METH_VARARGS, NULL },
286
{ "set_machine_account", (PyCFunction)py_creds_set_machine_account, METH_VARARGS, NULL },
290
PyTypeObject PyCredentials = {
291
.tp_name = "Credentials",
292
.tp_basicsize = sizeof(py_talloc_Object),
293
.tp_dealloc = py_talloc_dealloc,
294
.tp_new = py_creds_new,
295
.tp_flags = Py_TPFLAGS_DEFAULT,
296
.tp_methods = py_creds_methods,
299
void initcredentials(void)
303
if (PyType_Ready(&PyCredentials) < 0)
306
m = Py_InitModule3("credentials", NULL, "Credentials management.");
310
PyModule_AddObject(m, "AUTO_USE_KERBEROS", PyInt_FromLong(CRED_AUTO_USE_KERBEROS));
311
PyModule_AddObject(m, "DONT_USE_KERBEROS", PyInt_FromLong(CRED_DONT_USE_KERBEROS));
312
PyModule_AddObject(m, "MUST_USE_KERBEROS", PyInt_FromLong(CRED_MUST_USE_KERBEROS));
314
Py_INCREF(&PyCredentials);
315
PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);