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 static PyObject *PyString_FromStringOrNULL(const char *str)
32 return PyString_FromString(str);
35 static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
37 py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
42 ret->talloc_ctx = talloc_new(NULL);
43 if (ret->talloc_ctx == NULL) {
47 ret->ptr = cli_credentials_init(ret->talloc_ctx);
48 return (PyObject *)ret;
51 static PyObject *py_creds_get_username(py_talloc_Object *self)
53 return PyString_FromStringOrNULL(cli_credentials_get_username(PyCredentials_AsCliCredentials(self)));
56 static PyObject *py_creds_set_username(py_talloc_Object *self, PyObject *args)
59 enum credentials_obtained obt = CRED_SPECIFIED;
60 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
63 return PyBool_FromLong(cli_credentials_set_username(PyCredentials_AsCliCredentials(self), newval, obt));
66 static PyObject *py_creds_get_password(py_talloc_Object *self)
68 return PyString_FromStringOrNULL(cli_credentials_get_password(PyCredentials_AsCliCredentials(self)));
72 static PyObject *py_creds_set_password(py_talloc_Object *self, PyObject *args)
75 enum credentials_obtained obt = CRED_SPECIFIED;
76 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
79 return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
82 static PyObject *py_creds_get_domain(py_talloc_Object *self)
84 return PyString_FromStringOrNULL(cli_credentials_get_domain(PyCredentials_AsCliCredentials(self)));
87 static PyObject *py_creds_set_domain(py_talloc_Object *self, PyObject *args)
90 enum credentials_obtained obt = CRED_SPECIFIED;
91 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
94 return PyBool_FromLong(cli_credentials_set_domain(PyCredentials_AsCliCredentials(self), newval, obt));
97 static PyObject *py_creds_get_realm(py_talloc_Object *self)
99 return PyString_FromStringOrNULL(cli_credentials_get_realm(PyCredentials_AsCliCredentials(self)));
102 static PyObject *py_creds_set_realm(py_talloc_Object *self, PyObject *args)
105 enum credentials_obtained obt = CRED_SPECIFIED;
106 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
109 return PyBool_FromLong(cli_credentials_set_realm(PyCredentials_AsCliCredentials(self), newval, obt));
112 static PyObject *py_creds_get_bind_dn(py_talloc_Object *self)
114 return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(PyCredentials_AsCliCredentials(self)));
117 static PyObject *py_creds_set_bind_dn(py_talloc_Object *self, PyObject *args)
120 if (!PyArg_ParseTuple(args, "s", &newval))
123 return PyBool_FromLong(cli_credentials_set_bind_dn(PyCredentials_AsCliCredentials(self), newval));
126 static PyObject *py_creds_get_workstation(py_talloc_Object *self)
128 return PyString_FromStringOrNULL(cli_credentials_get_workstation(PyCredentials_AsCliCredentials(self)));
131 static PyObject *py_creds_set_workstation(py_talloc_Object *self, PyObject *args)
134 enum credentials_obtained obt = CRED_SPECIFIED;
135 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
138 return PyBool_FromLong(cli_credentials_set_workstation(PyCredentials_AsCliCredentials(self), newval, obt));
141 static PyObject *py_creds_is_anonymous(py_talloc_Object *self)
143 return PyBool_FromLong(cli_credentials_is_anonymous(PyCredentials_AsCliCredentials(self)));
146 static PyObject *py_creds_set_anonymous(py_talloc_Object *self)
148 cli_credentials_set_anonymous(PyCredentials_AsCliCredentials(self));
152 static PyObject *py_creds_authentication_requested(py_talloc_Object *self)
154 return PyBool_FromLong(cli_credentials_authentication_requested(PyCredentials_AsCliCredentials(self)));
157 static PyObject *py_creds_wrong_password(py_talloc_Object *self)
159 return PyBool_FromLong(cli_credentials_wrong_password(PyCredentials_AsCliCredentials(self)));
162 static PyObject *py_creds_set_cmdline_callbacks(py_talloc_Object *self)
164 return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(PyCredentials_AsCliCredentials(self)));
167 static PyObject *py_creds_parse_string(py_talloc_Object *self, PyObject *args)
170 enum credentials_obtained obt = CRED_SPECIFIED;
171 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
174 cli_credentials_parse_string(PyCredentials_AsCliCredentials(self), newval, obt);
178 static PyObject *py_creds_get_nt_hash(py_talloc_Object *self)
180 const struct samr_Password *ntpw = cli_credentials_get_nt_hash(PyCredentials_AsCliCredentials(self), self->ptr);
182 return PyString_FromStringAndSize(discard_const_p(char, ntpw->hash), 16);
185 static PyObject *py_creds_set_kerberos_state(py_talloc_Object *self, PyObject *args)
188 if (!PyArg_ParseTuple(args, "i", &state))
191 cli_credentials_set_kerberos_state(PyCredentials_AsCliCredentials(self), state);
195 static PyObject *py_creds_guess(py_talloc_Object *self, PyObject *args)
197 PyObject *py_lp_ctx = Py_None;
198 struct loadparm_context *lp_ctx;
199 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
202 lp_ctx = lp_from_py_object(py_lp_ctx);
206 cli_credentials_guess(PyCredentials_AsCliCredentials(self), lp_ctx);
211 static PyObject *py_creds_set_machine_account(py_talloc_Object *self, PyObject *args)
213 PyObject *py_lp_ctx = Py_None;
214 struct loadparm_context *lp_ctx;
216 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
219 lp_ctx = lp_from_py_object(py_lp_ctx);
223 status = cli_credentials_set_machine_account(PyCredentials_AsCliCredentials(self), lp_ctx);
224 PyErr_NTSTATUS_IS_ERR_RAISE(status);
229 static PyMethodDef py_creds_methods[] = {
230 { "get_username", (PyCFunction)py_creds_get_username, METH_NOARGS,
231 "S.get_username() -> username\nObtain username." },
232 { "set_username", (PyCFunction)py_creds_set_username, METH_VARARGS,
233 "S.set_username(name, obtained=CRED_SPECIFIED) -> None\n"
234 "Change username." },
235 { "get_password", (PyCFunction)py_creds_get_password, METH_NOARGS,
236 "S.get_password() -> password\n"
237 "Obtain password." },
238 { "set_password", (PyCFunction)py_creds_set_password, METH_VARARGS,
239 "S.set_password(password, obtained=CRED_SPECIFIED) -> None\n"
240 "Change password." },
241 { "get_domain", (PyCFunction)py_creds_get_domain, METH_NOARGS,
242 "S.get_domain() -> domain\n"
243 "Obtain domain name." },
244 { "set_domain", (PyCFunction)py_creds_set_domain, METH_VARARGS,
245 "S.set_domain(domain, obtained=CRED_SPECIFIED) -> None\n"
246 "Change domain name." },
247 { "get_realm", (PyCFunction)py_creds_get_realm, METH_NOARGS,
248 "S.get_realm() -> realm\n"
249 "Obtain realm name." },
250 { "set_realm", (PyCFunction)py_creds_set_realm, METH_VARARGS,
251 "S.set_realm(realm, obtained=CRED_SPECIFIED) -> None\n"
252 "Change realm name." },
253 { "get_bind_dn", (PyCFunction)py_creds_get_bind_dn, METH_NOARGS,
254 "S.get_bind_dn() -> bind dn\n"
256 { "set_bind_dn", (PyCFunction)py_creds_set_bind_dn, METH_VARARGS,
257 "S.set_bind_dn(bind_dn) -> None\n"
259 { "is_anonymous", (PyCFunction)py_creds_is_anonymous, METH_NOARGS,
261 { "set_anonymous", (PyCFunction)py_creds_set_anonymous, METH_NOARGS,
262 "S.set_anonymous() -> None\n"
263 "Use anonymous credentials." },
264 { "get_workstation", (PyCFunction)py_creds_get_workstation, METH_NOARGS,
266 { "set_workstation", (PyCFunction)py_creds_set_workstation, METH_VARARGS,
268 { "authentication_requested", (PyCFunction)py_creds_authentication_requested, METH_NOARGS,
270 { "wrong_password", (PyCFunction)py_creds_wrong_password, METH_NOARGS,
271 "S.wrong_password() -> bool\n"
272 "Indicate the returned password was incorrect." },
273 { "set_cmdline_callbacks", (PyCFunction)py_creds_set_cmdline_callbacks, METH_NOARGS,
274 "S.set_cmdline_callbacks() -> bool\n"
275 "Use command-line to obtain credentials not explicitly set." },
276 { "parse_string", (PyCFunction)py_creds_parse_string, METH_VARARGS,
277 "S.parse_string(text, obtained=CRED_SPECIFIED) -> None\n"
278 "Parse credentials string." },
279 { "get_nt_hash", (PyCFunction)py_creds_get_nt_hash, METH_NOARGS,
281 { "set_kerberos_state", (PyCFunction)py_creds_set_kerberos_state, METH_VARARGS,
283 { "guess", (PyCFunction)py_creds_guess, METH_VARARGS, NULL },
284 { "set_machine_account", (PyCFunction)py_creds_set_machine_account, METH_VARARGS, NULL },
288 PyTypeObject PyCredentials = {
289 .tp_name = "Credentials",
290 .tp_basicsize = sizeof(py_talloc_Object),
291 .tp_dealloc = py_talloc_dealloc,
292 .tp_new = py_creds_new,
293 .tp_flags = Py_TPFLAGS_DEFAULT,
294 .tp_methods = py_creds_methods,
297 void initcredentials(void)
301 if (PyType_Ready(&PyCredentials) < 0)
304 m = Py_InitModule3("credentials", NULL, "Credentials management.");
308 PyModule_AddObject(m, "AUTO_USE_KERBEROS", PyInt_FromLong(CRED_AUTO_USE_KERBEROS));
309 PyModule_AddObject(m, "DONT_USE_KERBEROS", PyInt_FromLong(CRED_DONT_USE_KERBEROS));
310 PyModule_AddObject(m, "MUST_USE_KERBEROS", PyInt_FromLong(CRED_MUST_USE_KERBEROS));
312 Py_INCREF(&PyCredentials);
313 PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);