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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/*
SSSD
Unit tests - exercise the krb5 child
Authors:
Jakub Hrozek <jhrozek@redhat.com>
Copyright (C) 2012 Red Hat
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 <stdio.h>
#include <stdlib.h>
#include <talloc.h>
#include <popt.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include "util/util.h"
#include "src/tools/tools_util.h"
/* Interfaces being tested */
#include "providers/krb5/krb5_auth.h"
#include "providers/krb5/krb5_common.h"
#include "providers/krb5/krb5_utils.h"
extern struct dp_option default_krb5_opts[];
extern struct sss_krb5_cc_be file_cc;
extern struct sss_krb5_cc_be dir_cc;
static krb5_context krb5_error_ctx;
#define KRB5_CHILD_TEST_DEBUG(level, error) KRB5_DEBUG(level, krb5_error_ctx, error)
#define CHECK_KRET(kret, err) do { \
if (kret) { \
KRB5_CHILD_TEST_DEBUG(SSSDBG_OP_FAILURE, kret); \
return err; \
} \
} while(0) \
#define CHECK_KRET_L(kret, err, label) do { \
if (kret) { \
KRB5_CHILD_TEST_DEBUG(SSSDBG_OP_FAILURE, kret); \
goto label; \
} \
} while(0) \
struct krb5_child_test_ctx {
struct tevent_context *ev;
struct krb5child_req *kr;
bool done;
errno_t child_ret;
uint8_t *buf;
ssize_t len;
struct krb5_child_response *res;
};
static errno_t
setup_krb5_child_test(TALLOC_CTX *mem_ctx, struct krb5_child_test_ctx **_ctx)
{
struct krb5_child_test_ctx *ctx;
ctx = talloc_zero(mem_ctx, struct krb5_child_test_ctx);
if (!ctx) return ENOMEM;
ctx->ev = tevent_context_init(ctx);
if (ctx->ev == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, ("Could not init tevent context"));
talloc_free(ctx);
return EFAULT;
}
*_ctx = ctx;
return EOK;
}
int re_destructor(void *memctx)
{
struct krb5_ctx *ctx = (struct krb5_ctx *) memctx;
if (ctx->illegal_path_re) {
pcre_free(ctx->illegal_path_re);
ctx->illegal_path_re = NULL;
}
return 0;
}
static struct krb5_ctx *
create_dummy_krb5_ctx(TALLOC_CTX *mem_ctx, const char *realm)
{
struct krb5_ctx *krb5_ctx;
const char *errstr;
int errval;
int errpos;
int i;
errno_t ret;
krb5_ctx = talloc_zero(mem_ctx, struct krb5_ctx);
if (!krb5_ctx) return NULL;
krb5_ctx->illegal_path_re = pcre_compile2(ILLEGAL_PATH_PATTERN, 0,
&errval, &errstr, &errpos, NULL);
if (krb5_ctx->illegal_path_re == NULL) {
DEBUG(SSSDBG_OP_FAILURE,
("Invalid Regular Expression pattern at position %d. "
"(Error: %d [%s])\n", errpos, errval, errstr));
goto fail;
}
talloc_set_destructor((TALLOC_CTX *) krb5_ctx, re_destructor);
/* Kerberos options */
krb5_ctx->opts = talloc_zero_array(krb5_ctx, struct dp_option, KRB5_OPTS);
if (!krb5_ctx->opts) goto fail;
for (i = 0; i < KRB5_OPTS; i++) {
krb5_ctx->opts[i].opt_name = default_krb5_opts[i].opt_name;
krb5_ctx->opts[i].type = default_krb5_opts[i].type;
krb5_ctx->opts[i].def_val = default_krb5_opts[i].def_val;
switch (krb5_ctx->opts[i].type) {
case DP_OPT_STRING:
ret = dp_opt_set_string(krb5_ctx->opts, i,
default_krb5_opts[i].def_val.string);
break;
case DP_OPT_BLOB:
ret = dp_opt_set_blob(krb5_ctx->opts, i,
default_krb5_opts[i].def_val.blob);
break;
case DP_OPT_NUMBER:
ret = dp_opt_set_int(krb5_ctx->opts, i,
default_krb5_opts[i].def_val.number);
break;
case DP_OPT_BOOL:
ret = dp_opt_set_bool(krb5_ctx->opts, i,
default_krb5_opts[i].def_val.boolean);
break;
}
if (ret) goto fail;
}
ret = dp_opt_set_string(krb5_ctx->opts, KRB5_REALM, realm);
if (ret) goto fail;
return krb5_ctx;
fail:
talloc_free(krb5_ctx);
return NULL;
}
static struct pam_data *
create_dummy_pam_data(TALLOC_CTX *mem_ctx, const char *user,
const char *password)
{
struct pam_data *pd;
pd = talloc_zero(mem_ctx, struct pam_data);
if (!pd) goto fail;
pd->cmd = SSS_PAM_AUTHENTICATE;
pd->user = talloc_strdup(pd, user);
if (!pd->user) goto fail;
pd->authtok = discard_const(talloc_strdup(pd, password));
if (!pd->authtok) goto fail;
pd->authtok_size = strlen(password);
pd->authtok_type = SSS_AUTHTOK_TYPE_PASSWORD;
DEBUG(SSSDBG_FUNC_DATA, ("Authtok [%s] len [%d]\n",
pd->authtok, pd->authtok_size));
return pd;
fail:
talloc_free(pd);
return NULL;
}
static struct krb5child_req *
create_dummy_req(TALLOC_CTX *mem_ctx, const char *user,
const char *password, const char *realm,
const char *ccname, const char *ccname_template,
int timeout)
{
enum sss_krb5_cc_type cc_be;
struct krb5child_req *kr;
struct passwd *pwd;
bool private = false;
errno_t ret;
const char *tmpl;
/* The top level child request */
kr = talloc_zero(mem_ctx, struct krb5child_req);
if (!kr) return NULL;
pwd = getpwnam(user);
if (!pwd) {
DEBUG(SSSDBG_FATAL_FAILURE,
("Cannot get info on user [%s]\n", user));
goto fail;
}
kr->uid = pwd->pw_uid;
kr->gid = pwd->pw_gid;
/* The Kerberos context */
kr->krb5_ctx = create_dummy_krb5_ctx(kr, realm);
/* PAM Data structure */
kr->pd = create_dummy_pam_data(kr, user, password);
ret = krb5_get_simple_upn(kr, kr->krb5_ctx, NULL, kr->pd->user, NULL,
&kr->upn);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("krb5_get_simple_upn failed.\n"));
goto fail;
}
/* Override options with what was provided by the user */
if (ccname_template) {
ret = dp_opt_set_string(kr->krb5_ctx->opts, KRB5_CCNAME_TMPL,
ccname_template);
if (ret != EOK) goto fail;
tmpl = ccname_template;
} else {
tmpl = dp_opt_get_cstring(kr->krb5_ctx->opts, KRB5_CCNAME_TMPL);
}
if (timeout) {
ret = dp_opt_set_int(kr->krb5_ctx->opts, KRB5_AUTH_TIMEOUT, timeout);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to set value for krb5_auth_timeout\n"));
goto fail;
}
}
if (!ccname) {
kr->ccname = expand_ccname_template(kr, kr,
dp_opt_get_cstring(kr->krb5_ctx->opts,
KRB5_CCNAME_TMPL),
true, true, &private);
if (!kr->ccname) goto fail;
DEBUG(SSSDBG_FUNC_DATA, ("ccname [%s] uid [%llu] gid [%llu]\n",
kr->ccname, (unsigned long long) kr->uid,
(unsigned long long) kr->gid));
} else {
kr->ccname = talloc_strdup(kr, ccname);
}
if (!kr->ccname) goto fail;
cc_be = sss_krb5_get_type(kr->ccname);
switch (cc_be) {
case SSS_KRB5_TYPE_FILE:
kr->krb5_ctx->cc_be = &file_cc;
break;
#ifdef HAVE_KRB5_DIRCACHE
case SSS_KRB5_TYPE_DIR:
kr->krb5_ctx->cc_be = &dir_cc;
break;
#endif /* HAVE_KRB5_DIRCACHE */
default:
if (tmpl[0] != '/') {
DEBUG(SSSDBG_OP_FAILURE, ("Unkown ccname database\n"));
ret = EINVAL;
goto fail;
}
DEBUG(SSSDBG_CONF_SETTINGS, ("The ccname template was "
"missing an explicit type, but looks like an absolute "
"path specifier. Assuming FILE:\n"));
kr->krb5_ctx->cc_be = &file_cc;
break;
}
DEBUG(SSSDBG_FUNC_DATA, ("ccname [%s] uid [%llu] gid [%llu]\n",
kr->ccname, kr->uid, kr->gid));
ret = kr->krb5_ctx->cc_be->create(kr->ccname,
kr->krb5_ctx->illegal_path_re,
kr->uid, kr->gid, private);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("create_ccache_dir failed.\n"));
goto fail;
}
return kr;
fail:
talloc_free(kr);
return NULL;
}
static void
child_done(struct tevent_req *req)
{
struct krb5_child_test_ctx *ctx = tevent_req_callback_data(req,
struct krb5_child_test_ctx);
errno_t ret;
ret = handle_child_recv(req, ctx, &ctx->buf, &ctx->len);
talloc_free(req);
ctx->done = true;
ctx->child_ret = ret;
}
static void
printtime(krb5_timestamp ts)
{
krb5_error_code kret;
char timestring[BUFSIZ];
char fill = '\0';
#ifdef HAVE_KRB5_TIMESTAMP_TO_SFSTRING
kret = krb5_timestamp_to_sfstring(ts, timestring, BUFSIZ, &fill);
if (kret) {
KRB5_CHILD_TEST_DEBUG(SSSDBG_OP_FAILURE, kret);
}
printf("%s", timestring);
#else
printf("%s", ctime(&ts));
#endif /* HAVE_KRB5_TIMESTAMP_TO_SFSTRING */
}
static void
print_creds(krb5_context kcontext, krb5_creds *cred, const char *defname)
{
krb5_error_code kret;
char *name = NULL;
char *sname = NULL;
kret = krb5_unparse_name(kcontext, cred->client, &name);
CHECK_KRET_L(kret, EIO, done);
kret = krb5_unparse_name(kcontext, cred->server, &sname);
CHECK_KRET_L(kret, EIO, done);
if (!cred->times.starttime) {
cred->times.starttime = cred->times.authtime;
}
printf("\t\t%s\n", sname);
printf("\t\tValid from\t"); printtime(cred->times.starttime);
printf("\n\t\tValid until\t"); printtime(cred->times.endtime);
printf("\n");
if (strcmp(name, defname)) {
printf("\t\tfor client %s", name);
}
done:
krb5_free_unparsed_name(kcontext, name);
krb5_free_unparsed_name(kcontext, sname);
}
static errno_t
print_ccache(const char *cc)
{
krb5_cc_cursor cur;
krb5_ccache cache = NULL;
krb5_error_code kret;
krb5_context kcontext = NULL;
krb5_principal_data *princ = NULL;
krb5_creds creds;
char *defname = NULL;
int i = 1;
errno_t ret = EIO;
kret = krb5_init_context(&kcontext);
CHECK_KRET_L(kret, EIO, done);
kret = krb5_cc_resolve(kcontext, cc, &cache);
CHECK_KRET_L(kret, EIO, done);
kret = krb5_cc_get_principal(kcontext, cache, &princ);
CHECK_KRET_L(kret, EIO, done);
kret = krb5_unparse_name(kcontext, princ, &defname);
CHECK_KRET_L(kret, EIO, done);
printf("\nTicket cache: %s:%s\nDefault principal: %s\n\n",
krb5_cc_get_type(kcontext, cache),
krb5_cc_get_name(kcontext, cache), defname);
kret = krb5_cc_start_seq_get(kcontext, cache, &cur);
CHECK_KRET_L(kret, EIO, done);
while (!(kret = krb5_cc_next_cred(kcontext, cache, &cur, &creds))) {
printf("Ticket #%d:\n", i);
print_creds(kcontext, &creds, defname);
krb5_free_cred_contents(kcontext, &creds);
}
kret = krb5_cc_end_seq_get(kcontext, cache, &cur);
CHECK_KRET_L(kret, EIO, done);
ret = EOK;
done:
krb5_cc_close(kcontext, cache);
krb5_free_unparsed_name(kcontext, defname);
krb5_free_principal(kcontext, princ);
krb5_free_context(kcontext);
return ret;
}
int
main(int argc, const char *argv[])
{
int opt;
errno_t ret;
struct krb5_child_test_ctx *ctx = NULL;
struct tevent_req *req;
int pc_debug = 0;
int pc_timeout = 0;
const char *pc_user = NULL;;
const char *pc_passwd = NULL;;
const char *pc_realm = NULL;;
const char *pc_ccname = NULL;;
const char *pc_ccname_tp = NULL;;
char *password = NULL;
bool rm_ccache = true;
poptContext pc;
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, 0,
"The debug level to run with", NULL },
{ "user", 'u', POPT_ARG_STRING, &pc_user, 0,
"The user to log in as", NULL },
{ "password", 'w', POPT_ARG_STRING, &pc_passwd, 0,
"The authtok to use", NULL },
{ "ask-password", 'W', POPT_ARG_NONE, NULL, 'W',
"Ask interactively for authtok", NULL },
{ "ccname", 'c', POPT_ARG_STRING, &pc_ccname, 0,
"Force usage of a certain credential cache", NULL },
{ "ccname-template", 't', POPT_ARG_STRING, &pc_ccname_tp, 0,
"Specify the credential cache template", NULL },
{ "realm", 'r', POPT_ARG_STRING, &pc_realm, 0,
"The Kerberos realm to use", NULL },
{ "keep-ccache", 'k', POPT_ARG_NONE, NULL, 'k',
"Do not delete the ccache when the tool finishes", NULL },
{ "timeout", '\0', POPT_ARG_INT, &pc_timeout, 0,
"The timeout for the child, in seconds", NULL },
POPT_TABLEEND
};
debug_prg_name = argv[0];
pc = poptGetContext(NULL, argc, argv, long_options, 0);
while ((opt = poptGetNextOpt(pc)) > 0) {
switch(opt) {
case 'W':
errno = 0;
password = getpass("Enter password:");
if (!password) {
return 1;
}
break;
case 'k':
rm_ccache = false;
break;
default:
DEBUG(SSSDBG_FATAL_FAILURE, ("Unexpected option\n"));
return 1;
}
}
DEBUG_INIT(pc_debug);
if (opt != -1) {
poptPrintUsage(pc, stderr, 0);
fprintf(stderr, "%s", poptStrerror(opt));
return 1;
}
if (!pc_user) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Please specify the user\n"));
poptPrintUsage(pc, stderr, 0);
return 1;
}
if (!pc_realm) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Please specify the realm\n"));
poptPrintUsage(pc, stderr, 0);
return 1;
}
if (!password && !pc_passwd) {
DEBUG(SSSDBG_FATAL_FAILURE,
("Password was not provided or asked for\n"));
poptPrintUsage(pc, stderr, 0);
return 1;
}
if (pc_ccname && pc_ccname_tp) {
DEBUG(SSSDBG_MINOR_FAILURE,
("Both ccname and ccname template specified, "
"will prefer ccname\n"));
}
ret = setup_krb5_child_test(NULL, &ctx);
if (ret != EOK) {
poptPrintUsage(pc, stderr, 0);
fprintf(stderr, "%s", poptStrerror(opt));
return 3;
}
ctx->kr = create_dummy_req(ctx, pc_user, password ? password : pc_passwd,
pc_realm, pc_ccname, pc_ccname_tp, pc_timeout);
if (!ctx->kr) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Cannot create Kerberos request\n"));
ret = 4;
goto done;
}
req = handle_child_send(ctx, ctx->ev, ctx->kr);
if (!req) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Cannot create child request\n"));
ret = 4;
goto done;
}
tevent_req_set_callback(req, child_done, ctx);
while (ctx->done == false) {
tevent_loop_once(ctx->ev);
}
printf("Child returned %d\n", ctx->child_ret);
ret = parse_krb5_child_response(ctx, ctx->buf, ctx->len,
ctx->kr->pd, 0, &ctx->res);
if (ret != EOK) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Could not parse child response\n"));
ret = 5;
goto done;
}
if (!ctx->res->ccname) {
fprintf(stderr, "No ccname returned\n");
ret = 6;
goto done;
}
print_ccache(ctx->res->ccname);
ret = 0;
done:
if (rm_ccache && ctx->res
&& ctx->res->ccname
&& ctx->kr) {
ctx->kr->krb5_ctx->cc_be->remove(ctx->res->ccname);
}
free(password);
talloc_free(ctx);
poptFreeContext(pc);
return ret;
}
|