1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/*
Unix SMB/CIFS implementation.
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include "includes.h"
#include "pycredentials.h"
#include "param/param.h"
#include "lib/cmdline/credentials.h"
#include "librpc/gen_ndr/samr.h" /* for struct samr_Password */
#include "libcli/util/pyerrors.h"
#include "param/pyparam.h"
#include <tevent.h>
void initcredentials(void);
static PyObject *PyString_FromStringOrNULL(const char *str)
{
if (str == NULL)
Py_RETURN_NONE;
return PyString_FromString(str);
}
static PyObject *py_creds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
pytalloc_Object *ret = (pytalloc_Object *)type->tp_alloc(type, 0);
if (ret == NULL) {
PyErr_NoMemory();
return NULL;
}
ret->talloc_ctx = talloc_new(NULL);
if (ret->talloc_ctx == NULL) {
PyErr_NoMemory();
return NULL;
}
ret->ptr = cli_credentials_init(ret->talloc_ctx);
return (PyObject *)ret;
}
static PyObject *py_creds_get_username(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_username(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_username(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
return PyBool_FromLong(cli_credentials_set_username(PyCredentials_AsCliCredentials(self), newval, obt));
}
static PyObject *py_creds_get_password(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_password(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_password(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
}
static PyObject *py_creds_get_domain(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_domain(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_domain(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
return PyBool_FromLong(cli_credentials_set_domain(PyCredentials_AsCliCredentials(self), newval, obt));
}
static PyObject *py_creds_get_realm(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_realm(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_realm(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
return PyBool_FromLong(cli_credentials_set_realm(PyCredentials_AsCliCredentials(self), newval, obt));
}
static PyObject *py_creds_get_bind_dn(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_bind_dn(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_bind_dn(pytalloc_Object *self, PyObject *args)
{
char *newval;
if (!PyArg_ParseTuple(args, "s", &newval))
return NULL;
return PyBool_FromLong(cli_credentials_set_bind_dn(PyCredentials_AsCliCredentials(self), newval));
}
static PyObject *py_creds_get_workstation(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_workstation(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_workstation(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
return PyBool_FromLong(cli_credentials_set_workstation(PyCredentials_AsCliCredentials(self), newval, obt));
}
static PyObject *py_creds_is_anonymous(pytalloc_Object *self)
{
return PyBool_FromLong(cli_credentials_is_anonymous(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_anonymous(pytalloc_Object *self)
{
cli_credentials_set_anonymous(PyCredentials_AsCliCredentials(self));
Py_RETURN_NONE;
}
static PyObject *py_creds_authentication_requested(pytalloc_Object *self)
{
return PyBool_FromLong(cli_credentials_authentication_requested(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_wrong_password(pytalloc_Object *self)
{
return PyBool_FromLong(cli_credentials_wrong_password(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_cmdline_callbacks(pytalloc_Object *self)
{
return PyBool_FromLong(cli_credentials_set_cmdline_callbacks(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_parse_string(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
return NULL;
}
obt = _obt;
cli_credentials_parse_string(PyCredentials_AsCliCredentials(self), newval, obt);
Py_RETURN_NONE;
}
static PyObject *py_creds_get_nt_hash(pytalloc_Object *self)
{
const struct samr_Password *ntpw = cli_credentials_get_nt_hash(PyCredentials_AsCliCredentials(self), self->ptr);
return PyString_FromStringAndSize(discard_const_p(char, ntpw->hash), 16);
}
static PyObject *py_creds_set_kerberos_state(pytalloc_Object *self, PyObject *args)
{
int state;
if (!PyArg_ParseTuple(args, "i", &state))
return NULL;
cli_credentials_set_kerberos_state(PyCredentials_AsCliCredentials(self), state);
Py_RETURN_NONE;
}
static PyObject *py_creds_set_krb_forwardable(pytalloc_Object *self, PyObject *args)
{
int state;
if (!PyArg_ParseTuple(args, "i", &state))
return NULL;
cli_credentials_set_krb_forwardable(PyCredentials_AsCliCredentials(self), state);
Py_RETURN_NONE;
}
static PyObject *py_creds_get_forced_sasl_mech(pytalloc_Object *self)
{
return PyString_FromStringOrNULL(cli_credentials_get_forced_sasl_mech(PyCredentials_AsCliCredentials(self)));
}
static PyObject *py_creds_set_forced_sasl_mech(pytalloc_Object *self, PyObject *args)
{
char *newval;
enum credentials_obtained obt = CRED_SPECIFIED;
int _obt = obt;
if (!PyArg_ParseTuple(args, "s", &newval)) {
return NULL;
}
obt = _obt;
cli_credentials_set_forced_sasl_mech(PyCredentials_AsCliCredentials(self), newval);
Py_RETURN_NONE;
}
static PyObject *py_creds_guess(pytalloc_Object *self, PyObject *args)
{
PyObject *py_lp_ctx = Py_None;
struct loadparm_context *lp_ctx;
TALLOC_CTX *mem_ctx;
struct cli_credentials *creds;
creds = PyCredentials_AsCliCredentials(self);
if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
return NULL;
mem_ctx = talloc_new(NULL);
if (mem_ctx == NULL) {
PyErr_NoMemory();
return NULL;
}
lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
if (lp_ctx == NULL) {
talloc_free(mem_ctx);
return NULL;
}
cli_credentials_guess(creds, lp_ctx);
talloc_free(mem_ctx);
Py_RETURN_NONE;
}
static PyObject *py_creds_set_machine_account(pytalloc_Object *self, PyObject *args)
{
PyObject *py_lp_ctx = Py_None;
struct loadparm_context *lp_ctx;
NTSTATUS status;
struct cli_credentials *creds;
TALLOC_CTX *mem_ctx;
creds = PyCredentials_AsCliCredentials(self);
if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
return NULL;
mem_ctx = talloc_new(NULL);
if (mem_ctx == NULL) {
PyErr_NoMemory();
return NULL;
}
lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
if (lp_ctx == NULL) {
talloc_free(mem_ctx);
return NULL;
}
status = cli_credentials_set_machine_account(creds, lp_ctx);
talloc_free(mem_ctx);
PyErr_NTSTATUS_IS_ERR_RAISE(status);
Py_RETURN_NONE;
}
static PyObject *PyCredentialCacheContainer_from_ccache_container(struct ccache_container *ccc)
{
PyCredentialCacheContainerObject *py_ret;
if (ccc == NULL) {
Py_RETURN_NONE;
}
py_ret = (PyCredentialCacheContainerObject *)PyCredentialCacheContainer.tp_alloc(&PyCredentialCacheContainer, 0);
if (py_ret == NULL) {
PyErr_NoMemory();
return NULL;
}
py_ret->mem_ctx = talloc_new(NULL);
py_ret->ccc = talloc_reference(py_ret->mem_ctx, ccc);
return (PyObject *)py_ret;
}
static PyObject *py_creds_get_named_ccache(pytalloc_Object *self, PyObject *args)
{
PyObject *py_lp_ctx = Py_None;
char *ccache_name;
struct loadparm_context *lp_ctx;
struct ccache_container *ccc;
struct tevent_context *event_ctx;
int ret;
const char *error_string;
struct cli_credentials *creds;
TALLOC_CTX *mem_ctx;
creds = PyCredentials_AsCliCredentials(self);
if (!PyArg_ParseTuple(args, "|Os", &py_lp_ctx, &ccache_name))
return NULL;
mem_ctx = talloc_new(NULL);
if (mem_ctx == NULL) {
PyErr_NoMemory();
return NULL;
}
lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
if (lp_ctx == NULL) {
talloc_free(mem_ctx);
return NULL;
}
event_ctx = samba_tevent_context_init(mem_ctx);
ret = cli_credentials_get_named_ccache(creds, event_ctx, lp_ctx,
ccache_name, &ccc, &error_string);
talloc_unlink(mem_ctx, lp_ctx);
if (ret == 0) {
talloc_steal(ccc, event_ctx);
talloc_free(mem_ctx);
return PyCredentialCacheContainer_from_ccache_container(ccc);
}
PyErr_SetString(PyExc_RuntimeError, error_string?error_string:"NULL");
talloc_free(mem_ctx);
return NULL;
}
static PyObject *py_creds_set_gensec_features(pytalloc_Object *self, PyObject *args)
{
unsigned int gensec_features;
if (!PyArg_ParseTuple(args, "I", &gensec_features))
return NULL;
cli_credentials_set_gensec_features(PyCredentials_AsCliCredentials(self), gensec_features);
Py_RETURN_NONE;
}
static PyObject *py_creds_get_gensec_features(pytalloc_Object *self, PyObject *args)
{
unsigned int gensec_features;
gensec_features = cli_credentials_get_gensec_features(PyCredentials_AsCliCredentials(self));
return PyInt_FromLong(gensec_features);
}
static PyMethodDef py_creds_methods[] = {
{ "get_username", (PyCFunction)py_creds_get_username, METH_NOARGS,
"S.get_username() -> username\nObtain username." },
{ "set_username", (PyCFunction)py_creds_set_username, METH_VARARGS,
"S.set_username(name, obtained=CRED_SPECIFIED) -> None\n"
"Change username." },
{ "get_password", (PyCFunction)py_creds_get_password, METH_NOARGS,
"S.get_password() -> password\n"
"Obtain password." },
{ "set_password", (PyCFunction)py_creds_set_password, METH_VARARGS,
"S.set_password(password, obtained=CRED_SPECIFIED) -> None\n"
"Change password." },
{ "get_domain", (PyCFunction)py_creds_get_domain, METH_NOARGS,
"S.get_domain() -> domain\n"
"Obtain domain name." },
{ "set_domain", (PyCFunction)py_creds_set_domain, METH_VARARGS,
"S.set_domain(domain, obtained=CRED_SPECIFIED) -> None\n"
"Change domain name." },
{ "get_realm", (PyCFunction)py_creds_get_realm, METH_NOARGS,
"S.get_realm() -> realm\n"
"Obtain realm name." },
{ "set_realm", (PyCFunction)py_creds_set_realm, METH_VARARGS,
"S.set_realm(realm, obtained=CRED_SPECIFIED) -> None\n"
"Change realm name." },
{ "get_bind_dn", (PyCFunction)py_creds_get_bind_dn, METH_NOARGS,
"S.get_bind_dn() -> bind dn\n"
"Obtain bind DN." },
{ "set_bind_dn", (PyCFunction)py_creds_set_bind_dn, METH_VARARGS,
"S.set_bind_dn(bind_dn) -> None\n"
"Change bind DN." },
{ "is_anonymous", (PyCFunction)py_creds_is_anonymous, METH_NOARGS,
NULL },
{ "set_anonymous", (PyCFunction)py_creds_set_anonymous, METH_NOARGS,
"S.set_anonymous() -> None\n"
"Use anonymous credentials." },
{ "get_workstation", (PyCFunction)py_creds_get_workstation, METH_NOARGS,
NULL },
{ "set_workstation", (PyCFunction)py_creds_set_workstation, METH_VARARGS,
NULL },
{ "authentication_requested", (PyCFunction)py_creds_authentication_requested, METH_NOARGS,
NULL },
{ "wrong_password", (PyCFunction)py_creds_wrong_password, METH_NOARGS,
"S.wrong_password() -> bool\n"
"Indicate the returned password was incorrect." },
{ "set_cmdline_callbacks", (PyCFunction)py_creds_set_cmdline_callbacks, METH_NOARGS,
"S.set_cmdline_callbacks() -> bool\n"
"Use command-line to obtain credentials not explicitly set." },
{ "parse_string", (PyCFunction)py_creds_parse_string, METH_VARARGS,
"S.parse_string(text, obtained=CRED_SPECIFIED) -> None\n"
"Parse credentials string." },
{ "get_nt_hash", (PyCFunction)py_creds_get_nt_hash, METH_NOARGS,
NULL },
{ "set_kerberos_state", (PyCFunction)py_creds_set_kerberos_state, METH_VARARGS,
NULL },
{ "set_krb_forwardable", (PyCFunction)py_creds_set_krb_forwardable, METH_VARARGS,
NULL },
{ "guess", (PyCFunction)py_creds_guess, METH_VARARGS, NULL },
{ "set_machine_account", (PyCFunction)py_creds_set_machine_account, METH_VARARGS, NULL },
{ "get_named_ccache", (PyCFunction)py_creds_get_named_ccache, METH_VARARGS, NULL },
{ "set_gensec_features", (PyCFunction)py_creds_set_gensec_features, METH_VARARGS, NULL },
{ "get_gensec_features", (PyCFunction)py_creds_get_gensec_features, METH_NOARGS, NULL },
{ "get_forced_sasl_mech", (PyCFunction)py_creds_get_forced_sasl_mech, METH_NOARGS,
"S.get_forced_sasl_mech() -> SASL mechanism\nObtain forced SASL mechanism." },
{ "set_forced_sasl_mech", (PyCFunction)py_creds_set_forced_sasl_mech, METH_VARARGS,
"S.set_forced_sasl_mech(name) -> None\n"
"Set forced SASL mechanism." },
{ NULL }
};
PyTypeObject PyCredentials = {
.tp_name = "credentials.Credentials",
.tp_basicsize = sizeof(pytalloc_Object),
.tp_new = py_creds_new,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_methods = py_creds_methods,
};
PyTypeObject PyCredentialCacheContainer = {
.tp_name = "credentials.CredentialCacheContainer",
.tp_basicsize = sizeof(pytalloc_Object),
.tp_flags = Py_TPFLAGS_DEFAULT,
};
void initcredentials(void)
{
PyObject *m;
PyTypeObject *talloc_type = pytalloc_GetObjectType();
if (talloc_type == NULL)
return;
PyCredentials.tp_base = PyCredentialCacheContainer.tp_base = talloc_type;
if (PyType_Ready(&PyCredentials) < 0)
return;
if (PyType_Ready(&PyCredentialCacheContainer) < 0)
return;
m = Py_InitModule3("credentials", NULL, "Credentials management.");
if (m == NULL)
return;
PyModule_AddObject(m, "AUTO_USE_KERBEROS", PyInt_FromLong(CRED_AUTO_USE_KERBEROS));
PyModule_AddObject(m, "DONT_USE_KERBEROS", PyInt_FromLong(CRED_DONT_USE_KERBEROS));
PyModule_AddObject(m, "MUST_USE_KERBEROS", PyInt_FromLong(CRED_MUST_USE_KERBEROS));
PyModule_AddObject(m, "AUTO_KRB_FORWARDABLE", PyInt_FromLong(CRED_AUTO_KRB_FORWARDABLE));
PyModule_AddObject(m, "NO_KRB_FORWARDABLE", PyInt_FromLong(CRED_NO_KRB_FORWARDABLE));
PyModule_AddObject(m, "FORCE_KRB_FORWARDABLE", PyInt_FromLong(CRED_FORCE_KRB_FORWARDABLE));
Py_INCREF(&PyCredentials);
PyModule_AddObject(m, "Credentials", (PyObject *)&PyCredentials);
Py_INCREF(&PyCredentialCacheContainer);
PyModule_AddObject(m, "CredentialCacheContainer", (PyObject *)&PyCredentialCacheContainer);
}
|