samba-3.5.8 for ARM
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.5.8 / source4 / auth / credentials / pycredentials.c
blobb0433abeab150d9a7781fea32cb43e7c0117d493
1 /*
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/>.
19 #include "includes.h"
20 #include <Python.h>
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
30 #endif
32 static PyObject *PyString_FromStringOrNULL(const char *str)
34 if (str == NULL)
35 Py_RETURN_NONE;
36 return PyString_FromString(str);
39 static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
41 py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
42 if (ret == NULL) {
43 PyErr_NoMemory();
44 return NULL;
46 ret->talloc_ctx = talloc_new(NULL);
47 if (ret->talloc_ctx == NULL) {
48 PyErr_NoMemory();
49 return NULL;
51 ret->ptr = cli_credentials_init(ret->talloc_ctx);
52 return (PyObject *)ret;
55 static PyObject *py_creds_get_username(py_talloc_Object *self)
57 return PyString_FromStringOrNULL(cli_credentials_get_username(PyCredentials_AsCliCredentials(self)));
60 static PyObject *py_creds_set_username(py_talloc_Object *self, PyObject *args)
62 char *newval;
63 enum credentials_obtained obt = CRED_SPECIFIED;
64 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
65 return NULL;
67 return PyBool_FromLong(cli_credentials_set_username(PyCredentials_AsCliCredentials(self), newval, obt));
70 static PyObject *py_creds_get_password(py_talloc_Object *self)
72 return PyString_FromStringOrNULL(cli_credentials_get_password(PyCredentials_AsCliCredentials(self)));
76 static PyObject *py_creds_set_password(py_talloc_Object *self, PyObject *args)
78 char *newval;
79 enum credentials_obtained obt = CRED_SPECIFIED;
80 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
81 return NULL;
83 return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
86 static PyObject *py_creds_get_domain(py_talloc_Object *self)
88 return PyString_FromStringOrNULL(cli_credentials_get_domain(PyCredentials_AsCliCredentials(self)));
91 static PyObject *py_creds_set_domain(py_talloc_Object *self, PyObject *args)
93 char *newval;
94 enum credentials_obtained obt = CRED_SPECIFIED;
95 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
96 return NULL;
98 return PyBool_FromLong(cli_credentials_set_domain(PyCredentials_AsCliCredentials(self), newval, obt));
101 static PyObject *py_creds_get_realm(py_talloc_Object *self)
103 return PyString_FromStringOrNULL(cli_credentials_get_realm(PyCredentials_AsCliCredentials(self)));
106 static PyObject *py_creds_set_realm(py_talloc_Object *self, PyObject *args)
108 char *newval;
109 enum credentials_obtained obt = CRED_SPECIFIED;
110 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
111 return NULL;
113 return PyBool_FromLong(cli_credentials_set_realm(PyCredentials_AsCliCredentials(self), newval, obt));
116 static PyObject *py_creds_get_bind_dn(py_talloc_Object *self)
118 return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(PyCredentials_AsCliCredentials(self)));
121 static PyObject *py_creds_set_bind_dn(py_talloc_Object *self, PyObject *args)
123 char *newval;
124 if (!PyArg_ParseTuple(args, "s", &newval))
125 return NULL;
127 return PyBool_FromLong(cli_credentials_set_bind_dn(PyCredentials_AsCliCredentials(self), newval));
130 static PyObject *py_creds_get_workstation(py_talloc_Object *self)
132 return PyString_FromStringOrNULL(cli_credentials_get_workstation(PyCredentials_AsCliCredentials(self)));
135 static PyObject *py_creds_set_workstation(py_talloc_Object *self, PyObject *args)
137 char *newval;
138 enum credentials_obtained obt = CRED_SPECIFIED;
139 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
140 return NULL;
142 return PyBool_FromLong(cli_credentials_set_workstation(PyCredentials_AsCliCredentials(self), newval, obt));
145 static PyObject *py_creds_is_anonymous(py_talloc_Object *self)
147 return PyBool_FromLong(cli_credentials_is_anonymous(PyCredentials_AsCliCredentials(self)));
150 static PyObject *py_creds_set_anonymous(py_talloc_Object *self)
152 cli_credentials_set_anonymous(PyCredentials_AsCliCredentials(self));
153 Py_RETURN_NONE;
156 static PyObject *py_creds_authentication_requested(py_talloc_Object *self)
158 return PyBool_FromLong(cli_credentials_authentication_requested(PyCredentials_AsCliCredentials(self)));
161 static PyObject *py_creds_wrong_password(py_talloc_Object *self)
163 return PyBool_FromLong(cli_credentials_wrong_password(PyCredentials_AsCliCredentials(self)));
166 static PyObject *py_creds_set_cmdline_callbacks(py_talloc_Object *self)
168 return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(PyCredentials_AsCliCredentials(self)));
171 static PyObject *py_creds_parse_string(py_talloc_Object *self, PyObject *args)
173 char *newval;
174 enum credentials_obtained obt = CRED_SPECIFIED;
175 if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
176 return NULL;
178 cli_credentials_parse_string(PyCredentials_AsCliCredentials(self), newval, obt);
179 Py_RETURN_NONE;
182 static PyObject *py_creds_get_nt_hash(py_talloc_Object *self)
184 const struct samr_Password *ntpw = cli_credentials_get_nt_hash(PyCredentials_AsCliCredentials(self), self->ptr);
186 return PyString_FromStringAndSize(discard_const_p(char, ntpw->hash), 16);
189 static PyObject *py_creds_set_kerberos_state(py_talloc_Object *self, PyObject *args)
191 int state;
192 if (!PyArg_ParseTuple(args, "i", &state))
193 return NULL;
195 cli_credentials_set_kerberos_state(PyCredentials_AsCliCredentials(self), state);
196 Py_RETURN_NONE;
199 static PyObject *py_creds_guess(py_talloc_Object *self, PyObject *args)
201 PyObject *py_lp_ctx = Py_None;
202 struct loadparm_context *lp_ctx;
203 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
204 return NULL;
206 lp_ctx = lp_from_py_object(py_lp_ctx);
207 if (lp_ctx == NULL)
208 return NULL;
210 cli_credentials_guess(PyCredentials_AsCliCredentials(self), lp_ctx);
212 Py_RETURN_NONE;
215 static PyObject *py_creds_set_machine_account(py_talloc_Object *self, PyObject *args)
217 PyObject *py_lp_ctx = Py_None;
218 struct loadparm_context *lp_ctx;
219 NTSTATUS status;
220 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
221 return NULL;
223 lp_ctx = lp_from_py_object(py_lp_ctx);
224 if (lp_ctx == NULL)
225 return NULL;
227 status = cli_credentials_set_machine_account(PyCredentials_AsCliCredentials(self), lp_ctx);
228 PyErr_NTSTATUS_IS_ERR_RAISE(status);
230 Py_RETURN_NONE;
233 static PyMethodDef py_creds_methods[] = {
234 { "get_username", (PyCFunction)py_creds_get_username, METH_NOARGS,
235 "S.get_username() -> username\nObtain username." },
236 { "set_username", (PyCFunction)py_creds_set_username, METH_VARARGS,
237 "S.set_username(name, obtained=CRED_SPECIFIED) -> None\n"
238 "Change username." },
239 { "get_password", (PyCFunction)py_creds_get_password, METH_NOARGS,
240 "S.get_password() -> password\n"
241 "Obtain password." },
242 { "set_password", (PyCFunction)py_creds_set_password, METH_VARARGS,
243 "S.set_password(password, obtained=CRED_SPECIFIED) -> None\n"
244 "Change password." },
245 { "get_domain", (PyCFunction)py_creds_get_domain, METH_NOARGS,
246 "S.get_domain() -> domain\n"
247 "Obtain domain name." },
248 { "set_domain", (PyCFunction)py_creds_set_domain, METH_VARARGS,
249 "S.set_domain(domain, obtained=CRED_SPECIFIED) -> None\n"
250 "Change domain name." },
251 { "get_realm", (PyCFunction)py_creds_get_realm, METH_NOARGS,
252 "S.get_realm() -> realm\n"
253 "Obtain realm name." },
254 { "set_realm", (PyCFunction)py_creds_set_realm, METH_VARARGS,
255 "S.set_realm(realm, obtained=CRED_SPECIFIED) -> None\n"
256 "Change realm name." },
257 { "get_bind_dn", (PyCFunction)py_creds_get_bind_dn, METH_NOARGS,
258 "S.get_bind_dn() -> bind dn\n"
259 "Obtain bind DN." },
260 { "set_bind_dn", (PyCFunction)py_creds_set_bind_dn, METH_VARARGS,
261 "S.set_bind_dn(bind_dn) -> None\n"
262 "Change bind DN." },
263 { "is_anonymous", (PyCFunction)py_creds_is_anonymous, METH_NOARGS,
264 NULL },
265 { "set_anonymous", (PyCFunction)py_creds_set_anonymous, METH_NOARGS,
266 "S.set_anonymous() -> None\n"
267 "Use anonymous credentials." },
268 { "get_workstation", (PyCFunction)py_creds_get_workstation, METH_NOARGS,
269 NULL },
270 { "set_workstation", (PyCFunction)py_creds_set_workstation, METH_VARARGS,
271 NULL },
272 { "authentication_requested", (PyCFunction)py_creds_authentication_requested, METH_NOARGS,
273 NULL },
274 { "wrong_password", (PyCFunction)py_creds_wrong_password, METH_NOARGS,
275 "S.wrong_password() -> bool\n"
276 "Indicate the returned password was incorrect." },
277 { "set_cmdline_callbacks", (PyCFunction)py_creds_set_cmdline_callbacks, METH_NOARGS,
278 "S.set_cmdline_callbacks() -> bool\n"
279 "Use command-line to obtain credentials not explicitly set." },
280 { "parse_string", (PyCFunction)py_creds_parse_string, METH_VARARGS,
281 "S.parse_string(text, obtained=CRED_SPECIFIED) -> None\n"
282 "Parse credentials string." },
283 { "get_nt_hash", (PyCFunction)py_creds_get_nt_hash, METH_NOARGS,
284 NULL },
285 { "set_kerberos_state", (PyCFunction)py_creds_set_kerberos_state, METH_VARARGS,
286 NULL },
287 { "guess", (PyCFunction)py_creds_guess, METH_VARARGS, NULL },
288 { "set_machine_account", (PyCFunction)py_creds_set_machine_account, METH_VARARGS, NULL },
289 { NULL }
292 PyTypeObject PyCredentials = {
293 .tp_name = "Credentials",
294 .tp_basicsize = sizeof(py_talloc_Object),
295 .tp_dealloc = py_talloc_dealloc,
296 .tp_new = py_creds_new,
297 .tp_flags = Py_TPFLAGS_DEFAULT,
298 .tp_methods = py_creds_methods,
301 void initcredentials(void)
303 PyObject *m;
305 if (PyType_Ready(&PyCredentials) < 0)
306 return;
308 m = Py_InitModule3("credentials", NULL, "Credentials management.");
309 if (m == NULL)
310 return;
312 PyModule_AddObject(m, "AUTO_USE_KERBEROS", PyInt_FromLong(CRED_AUTO_USE_KERBEROS));
313 PyModule_AddObject(m, "DONT_USE_KERBEROS", PyInt_FromLong(CRED_DONT_USE_KERBEROS));
314 PyModule_AddObject(m, "MUST_USE_KERBEROS", PyInt_FromLong(CRED_MUST_USE_KERBEROS));
316 Py_INCREF(&PyCredentials);
317 PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);