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/>.
20 #include "pycredentials.h"
21 #include "param/param.h"
22 #include "lib/cmdline/credentials.h"
23 #include "librpc/gen_ndr/samr.h" /* for struct samr_Password */
24 #include "libcli/util/pyerrors.h"
25 #include "param/pyparam.h"
27 struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj)
29 if (py_obj == Py_None) {
30 return cli_credentials_init_anon(NULL);
33 /* FIXME: Check type? */
34 return PyCredentials_AsCliCredentials(py_obj);
37 static PyObject *PyString_FromStringOrNULL(const char *str)
41 return PyString_FromString(str);
44 static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
46 return py_talloc_import(type, cli_credentials_init(NULL));
49 static PyObject *py_creds_get_username(py_talloc_Object *self)
51 return PyString_FromStringOrNULL(cli_credentials_get_username(self->ptr));
54 static PyObject *py_creds_set_username(py_talloc_Object *self, PyObject *args)
57 enum credentials_obtained obt = CRED_SPECIFIED;
58 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
61 return PyBool_FromLong(cli_credentials_set_username(self->ptr, newval, obt));
64 static PyObject *py_creds_get_password(py_talloc_Object *self)
66 return PyString_FromStringOrNULL(cli_credentials_get_password(self->ptr));
70 static PyObject *py_creds_set_password(py_talloc_Object *self, PyObject *args)
73 enum credentials_obtained obt = CRED_SPECIFIED;
74 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
77 return PyBool_FromLong(cli_credentials_set_password(self->ptr, newval, obt));
80 static PyObject *py_creds_get_domain(py_talloc_Object *self)
82 return PyString_FromStringOrNULL(cli_credentials_get_domain(self->ptr));
85 static PyObject *py_creds_set_domain(py_talloc_Object *self, PyObject *args)
88 enum credentials_obtained obt = CRED_SPECIFIED;
89 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
92 return PyBool_FromLong(cli_credentials_set_domain(self->ptr, newval, obt));
95 static PyObject *py_creds_get_realm(py_talloc_Object *self)
97 return PyString_FromStringOrNULL(cli_credentials_get_realm(self->ptr));
100 static PyObject *py_creds_set_realm(py_talloc_Object *self, PyObject *args)
103 enum credentials_obtained obt = CRED_SPECIFIED;
104 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
107 return PyBool_FromLong(cli_credentials_set_realm(self->ptr, newval, obt));
110 static PyObject *py_creds_get_bind_dn(py_talloc_Object *self)
112 return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(self->ptr));
115 static PyObject *py_creds_set_bind_dn(py_talloc_Object *self, PyObject *args)
118 if (!PyArg_ParseTuple(args, "s", &newval))
121 return PyBool_FromLong(cli_credentials_set_bind_dn(self->ptr, newval));
124 static PyObject *py_creds_get_workstation(py_talloc_Object *self)
126 return PyString_FromStringOrNULL(cli_credentials_get_workstation(self->ptr));
129 static PyObject *py_creds_set_workstation(py_talloc_Object *self, PyObject *args)
132 enum credentials_obtained obt = CRED_SPECIFIED;
133 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
136 return PyBool_FromLong(cli_credentials_set_workstation(self->ptr, newval, obt));
139 static PyObject *py_creds_is_anonymous(py_talloc_Object *self)
141 return PyBool_FromLong(cli_credentials_is_anonymous(self->ptr));
144 static PyObject *py_creds_set_anonymous(py_talloc_Object *self)
146 cli_credentials_set_anonymous(self->ptr);
150 static PyObject *py_creds_authentication_requested(py_talloc_Object *self)
152 return PyBool_FromLong(cli_credentials_authentication_requested(self->ptr));
155 static PyObject *py_creds_wrong_password(py_talloc_Object *self)
157 return PyBool_FromLong(cli_credentials_wrong_password(self->ptr));
160 static PyObject *py_creds_set_cmdline_callbacks(py_talloc_Object *self)
162 return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(self->ptr));
165 static PyObject *py_creds_parse_string(py_talloc_Object *self, PyObject *args)
168 enum credentials_obtained obt = CRED_SPECIFIED;
169 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
172 cli_credentials_parse_string(self->ptr, newval, obt);
176 static PyObject *py_creds_get_nt_hash(py_talloc_Object *self)
178 const struct samr_Password *ntpw = cli_credentials_get_nt_hash(self->ptr, self->ptr);
180 return PyString_FromStringAndSize((char *)ntpw->hash, 16);
183 static PyObject *py_creds_set_kerberos_state(py_talloc_Object *self, PyObject *args)
186 if (!PyArg_ParseTuple(args, "i", &state))
189 cli_credentials_set_kerberos_state(self->ptr, state);
193 static PyObject *py_creds_guess(py_talloc_Object *self, PyObject *args)
195 PyObject *py_lp_ctx = Py_None;
196 struct loadparm_context *lp_ctx;
197 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
200 lp_ctx = lp_from_py_object(py_lp_ctx);
204 cli_credentials_guess(self->ptr, lp_ctx);
209 static PyObject *py_creds_set_machine_account(py_talloc_Object *self, PyObject *args)
211 PyObject *py_lp_ctx = Py_None;
212 struct loadparm_context *lp_ctx;
214 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
217 lp_ctx = lp_from_py_object(py_lp_ctx);
221 status = cli_credentials_set_machine_account(self->ptr, lp_ctx);
222 PyErr_NTSTATUS_IS_ERR_RAISE(status);
227 static PyMethodDef py_creds_methods[] = {
228 { "get_username", (PyCFunction)py_creds_get_username, METH_NOARGS,
229 "S.get_username() -> username\nObtain username." },
230 { "set_username", (PyCFunction)py_creds_set_username, METH_VARARGS,
231 "S.set_username(name, obtained=CRED_SPECIFIED) -> None\n"
232 "Change username." },
233 { "get_password", (PyCFunction)py_creds_get_password, METH_NOARGS,
234 "S.get_password() -> password\n"
235 "Obtain password." },
236 { "set_password", (PyCFunction)py_creds_set_password, METH_VARARGS,
237 "S.set_password(password, obtained=CRED_SPECIFIED) -> None\n"
238 "Change password." },
239 { "get_domain", (PyCFunction)py_creds_get_domain, METH_NOARGS,
240 "S.get_domain() -> domain\n"
241 "Obtain domain name." },
242 { "set_domain", (PyCFunction)py_creds_set_domain, METH_VARARGS,
243 "S.set_domain(domain, obtained=CRED_SPECIFIED) -> None\n"
244 "Change domain name." },
245 { "get_realm", (PyCFunction)py_creds_get_realm, METH_NOARGS,
246 "S.get_realm() -> realm\n"
247 "Obtain realm name." },
248 { "set_realm", (PyCFunction)py_creds_set_realm, METH_VARARGS,
249 "S.set_realm(realm, obtained=CRED_SPECIFIED) -> None\n"
250 "Change realm name." },
251 { "get_bind_dn", (PyCFunction)py_creds_get_bind_dn, METH_NOARGS,
252 "S.get_bind_dn() -> bind dn\n"
254 { "set_bind_dn", (PyCFunction)py_creds_set_bind_dn, METH_VARARGS,
255 "S.set_bind_dn(bind_dn) -> None\n"
257 { "is_anonymous", (PyCFunction)py_creds_is_anonymous, METH_NOARGS,
259 { "set_anonymous", (PyCFunction)py_creds_set_anonymous, METH_NOARGS,
260 "S.set_anonymous() -> None\n"
261 "Use anonymous credentials." },
262 { "get_workstation", (PyCFunction)py_creds_get_workstation, METH_NOARGS,
264 { "set_workstation", (PyCFunction)py_creds_set_workstation, METH_VARARGS,
266 { "authentication_requested", (PyCFunction)py_creds_authentication_requested, METH_NOARGS,
268 { "wrong_password", (PyCFunction)py_creds_wrong_password, METH_NOARGS,
269 "S.wrong_password() -> bool\n"
270 "Indicate the returned password was incorrect." },
271 { "set_cmdline_callbacks", (PyCFunction)py_creds_set_cmdline_callbacks, METH_NOARGS,
272 "S.set_cmdline_callbacks() -> bool\n"
273 "Use command-line to obtain credentials not explicitly set." },
274 { "parse_string", (PyCFunction)py_creds_parse_string, METH_VARARGS,
275 "S.parse_string(text, obtained=CRED_SPECIFIED) -> None\n"
276 "Parse credentials string." },
277 { "get_nt_hash", (PyCFunction)py_creds_get_nt_hash, METH_NOARGS,
279 { "set_kerberos_state", (PyCFunction)py_creds_set_kerberos_state, METH_VARARGS,
281 { "guess", (PyCFunction)py_creds_guess, METH_VARARGS, NULL },
282 { "set_machine_account", (PyCFunction)py_creds_set_machine_account, METH_VARARGS, NULL },
286 PyTypeObject PyCredentials = {
287 .tp_name = "Credentials",
288 .tp_basicsize = sizeof(py_talloc_Object),
289 .tp_dealloc = py_talloc_dealloc,
290 .tp_new = py_creds_new,
291 .tp_flags = Py_TPFLAGS_DEFAULT,
292 .tp_methods = py_creds_methods,
295 void initcredentials(void)
299 if (PyType_Ready(&PyCredentials) < 0)
302 m = Py_InitModule3("credentials", NULL, "Credentials management.");
306 PyModule_AddObject(m, "AUTO_USE_KERBEROS", PyInt_FromLong(CRED_AUTO_USE_KERBEROS));
307 PyModule_AddObject(m, "DONT_USE_KERBEROS", PyInt_FromLong(CRED_DONT_USE_KERBEROS));
308 PyModule_AddObject(m, "MUST_USE_KERBEROS", PyInt_FromLong(CRED_MUST_USE_KERBEROS));
310 Py_INCREF(&PyCredentials);
311 PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);