1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
|
---|
4 |
|
---|
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.
|
---|
9 |
|
---|
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.
|
---|
14 |
|
---|
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/>.
|
---|
17 | */
|
---|
18 |
|
---|
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"
|
---|
27 |
|
---|
28 | #ifndef Py_RETURN_NONE
|
---|
29 | #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | static PyObject *PyString_FromStringOrNULL(const char *str)
|
---|
33 | {
|
---|
34 | if (str == NULL)
|
---|
35 | Py_RETURN_NONE;
|
---|
36 | return PyString_FromString(str);
|
---|
37 | }
|
---|
38 |
|
---|
39 | static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
---|
40 | {
|
---|
41 | py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
|
---|
42 | if (ret == NULL) {
|
---|
43 | PyErr_NoMemory();
|
---|
44 | return NULL;
|
---|
45 | }
|
---|
46 | ret->talloc_ctx = talloc_new(NULL);
|
---|
47 | if (ret->talloc_ctx == NULL) {
|
---|
48 | PyErr_NoMemory();
|
---|
49 | return NULL;
|
---|
50 | }
|
---|
51 | ret->ptr = cli_credentials_init(ret->talloc_ctx);
|
---|
52 | return (PyObject *)ret;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static PyObject *py_creds_get_username(py_talloc_Object *self)
|
---|
56 | {
|
---|
57 | return PyString_FromStringOrNULL(cli_credentials_get_username(PyCredentials_AsCliCredentials(self)));
|
---|
58 | }
|
---|
59 |
|
---|
60 | static PyObject *py_creds_set_username(py_talloc_Object *self, PyObject *args)
|
---|
61 | {
|
---|
62 | char *newval;
|
---|
63 | enum credentials_obtained obt = CRED_SPECIFIED;
|
---|
64 | if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
|
---|
65 | return NULL;
|
---|
66 |
|
---|
67 | return PyBool_FromLong(cli_credentials_set_username(PyCredentials_AsCliCredentials(self), newval, obt));
|
---|
68 | }
|
---|
69 |
|
---|
70 | static PyObject *py_creds_get_password(py_talloc_Object *self)
|
---|
71 | {
|
---|
72 | return PyString_FromStringOrNULL(cli_credentials_get_password(PyCredentials_AsCliCredentials(self)));
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | static PyObject *py_creds_set_password(py_talloc_Object *self, PyObject *args)
|
---|
77 | {
|
---|
78 | char *newval;
|
---|
79 | enum credentials_obtained obt = CRED_SPECIFIED;
|
---|
80 | if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
|
---|
81 | return NULL;
|
---|
82 |
|
---|
83 | return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
|
---|
84 | }
|
---|
85 |
|
---|
86 | static PyObject *py_creds_get_domain(py_talloc_Object *self)
|
---|
87 | {
|
---|
88 | return PyString_FromStringOrNULL(cli_credentials_get_domain(PyCredentials_AsCliCredentials(self)));
|
---|
89 | }
|
---|
90 |
|
---|
91 | static PyObject *py_creds_set_domain(py_talloc_Object *self, PyObject *args)
|
---|
92 | {
|
---|
93 | char *newval;
|
---|
94 | enum credentials_obtained obt = CRED_SPECIFIED;
|
---|
95 | if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
|
---|
96 | return NULL;
|
---|
97 |
|
---|
98 | return PyBool_FromLong(cli_credentials_set_domain(PyCredentials_AsCliCredentials(self), newval, obt));
|
---|
99 | }
|
---|
100 |
|
---|
101 | static PyObject *py_creds_get_realm(py_talloc_Object *self)
|
---|
102 | {
|
---|
103 | return PyString_FromStringOrNULL(cli_credentials_get_realm(PyCredentials_AsCliCredentials(self)));
|
---|
104 | }
|
---|
105 |
|
---|
106 | static PyObject *py_creds_set_realm(py_talloc_Object *self, PyObject *args)
|
---|
107 | {
|
---|
108 | char *newval;
|
---|
109 | enum credentials_obtained obt = CRED_SPECIFIED;
|
---|
110 | if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
|
---|
111 | return NULL;
|
---|
112 |
|
---|
113 | return PyBool_FromLong(cli_credentials_set_realm(PyCredentials_AsCliCredentials(self), newval, obt));
|
---|
114 | }
|
---|
115 |
|
---|
116 | static PyObject *py_creds_get_bind_dn(py_talloc_Object *self)
|
---|
117 | {
|
---|
118 | return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(PyCredentials_AsCliCredentials(self)));
|
---|
119 | }
|
---|
120 |
|
---|
121 | static PyObject *py_creds_set_bind_dn(py_talloc_Object *self, PyObject *args)
|
---|
122 | {
|
---|
123 | char *newval;
|
---|
124 | if (!PyArg_ParseTuple(args, "s", &newval))
|
---|
125 | return NULL;
|
---|
126 |
|
---|
127 | return PyBool_FromLong(cli_credentials_set_bind_dn(PyCredentials_AsCliCredentials(self), newval));
|
---|
128 | }
|
---|
129 |
|
---|
130 | static PyObject *py_creds_get_workstation(py_talloc_Object *self)
|
---|
131 | {
|
---|
132 | return PyString_FromStringOrNULL(cli_credentials_get_workstation(PyCredentials_AsCliCredentials(self)));
|
---|
133 | }
|
---|
134 |
|
---|
135 | static PyObject *py_creds_set_workstation(py_talloc_Object *self, PyObject *args)
|
---|
136 | {
|
---|
137 | char *newval;
|
---|
138 | enum credentials_obtained obt = CRED_SPECIFIED;
|
---|
139 | if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
|
---|
140 | return NULL;
|
---|
141 |
|
---|
142 | return PyBool_FromLong(cli_credentials_set_workstation(PyCredentials_AsCliCredentials(self), newval, obt));
|
---|
143 | }
|
---|
144 |
|
---|
145 | static PyObject *py_creds_is_anonymous(py_talloc_Object *self)
|
---|
146 | {
|
---|
147 | return PyBool_FromLong(cli_credentials_is_anonymous(PyCredentials_AsCliCredentials(self)));
|
---|
148 | }
|
---|
149 |
|
---|
150 | static PyObject *py_creds_set_anonymous(py_talloc_Object *self)
|
---|
151 | {
|
---|
152 | cli_credentials_set_anonymous(PyCredentials_AsCliCredentials(self));
|
---|
153 | Py_RETURN_NONE;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static PyObject *py_creds_authentication_requested(py_talloc_Object *self)
|
---|
157 | {
|
---|
158 | return PyBool_FromLong(cli_credentials_authentication_requested(PyCredentials_AsCliCredentials(self)));
|
---|
159 | }
|
---|
160 |
|
---|
161 | static PyObject *py_creds_wrong_password(py_talloc_Object *self)
|
---|
162 | {
|
---|
163 | return PyBool_FromLong(cli_credentials_wrong_password(PyCredentials_AsCliCredentials(self)));
|
---|
164 | }
|
---|
165 |
|
---|
166 | static PyObject *py_creds_set_cmdline_callbacks(py_talloc_Object *self)
|
---|
167 | {
|
---|
168 | return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(PyCredentials_AsCliCredentials(self)));
|
---|
169 | }
|
---|
170 |
|
---|
171 | static PyObject *py_creds_parse_string(py_talloc_Object *self, PyObject *args)
|
---|
172 | {
|
---|
173 | char *newval;
|
---|
174 | enum credentials_obtained obt = CRED_SPECIFIED;
|
---|
175 | if (!PyArg_ParseTuple(args, "s|i", &newval, &obt))
|
---|
176 | return NULL;
|
---|
177 |
|
---|
178 | cli_credentials_parse_string(PyCredentials_AsCliCredentials(self), newval, obt);
|
---|
179 | Py_RETURN_NONE;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static PyObject *py_creds_get_nt_hash(py_talloc_Object *self)
|
---|
183 | {
|
---|
184 | const struct samr_Password *ntpw = cli_credentials_get_nt_hash(PyCredentials_AsCliCredentials(self), self->ptr);
|
---|
185 |
|
---|
186 | return PyString_FromStringAndSize(discard_const_p(char, ntpw->hash), 16);
|
---|
187 | }
|
---|
188 |
|
---|
189 | static PyObject *py_creds_set_kerberos_state(py_talloc_Object *self, PyObject *args)
|
---|
190 | {
|
---|
191 | int state;
|
---|
192 | if (!PyArg_ParseTuple(args, "i", &state))
|
---|
193 | return NULL;
|
---|
194 |
|
---|
195 | cli_credentials_set_kerberos_state(PyCredentials_AsCliCredentials(self), state);
|
---|
196 | Py_RETURN_NONE;
|
---|
197 | }
|
---|
198 |
|
---|
199 | static PyObject *py_creds_guess(py_talloc_Object *self, PyObject *args)
|
---|
200 | {
|
---|
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;
|
---|
205 |
|
---|
206 | lp_ctx = lp_from_py_object(py_lp_ctx);
|
---|
207 | if (lp_ctx == NULL)
|
---|
208 | return NULL;
|
---|
209 |
|
---|
210 | cli_credentials_guess(PyCredentials_AsCliCredentials(self), lp_ctx);
|
---|
211 |
|
---|
212 | Py_RETURN_NONE;
|
---|
213 | }
|
---|
214 |
|
---|
215 | static PyObject *py_creds_set_machine_account(py_talloc_Object *self, PyObject *args)
|
---|
216 | {
|
---|
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;
|
---|
222 |
|
---|
223 | lp_ctx = lp_from_py_object(py_lp_ctx);
|
---|
224 | if (lp_ctx == NULL)
|
---|
225 | return NULL;
|
---|
226 |
|
---|
227 | status = cli_credentials_set_machine_account(PyCredentials_AsCliCredentials(self), lp_ctx);
|
---|
228 | PyErr_NTSTATUS_IS_ERR_RAISE(status);
|
---|
229 |
|
---|
230 | Py_RETURN_NONE;
|
---|
231 | }
|
---|
232 |
|
---|
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 }
|
---|
290 | };
|
---|
291 |
|
---|
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,
|
---|
299 | };
|
---|
300 |
|
---|
301 | void initcredentials(void)
|
---|
302 | {
|
---|
303 | PyObject *m;
|
---|
304 |
|
---|
305 | if (PyType_Ready(&PyCredentials) < 0)
|
---|
306 | return;
|
---|
307 |
|
---|
308 | m = Py_InitModule3("credentials", NULL, "Credentials management.");
|
---|
309 | if (m == NULL)
|
---|
310 | return;
|
---|
311 |
|
---|
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));
|
---|
315 |
|
---|
316 | Py_INCREF(&PyCredentials);
|
---|
317 | PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);
|
---|
318 | }
|
---|