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
570
571
572
573
|
/*
Authors:
Sumit Bose <sbose@redhat.com>
Copyright (C) 2009-2010 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 <errno.h>
#include <talloc.h>
#include "config.h"
#include "util/util.h"
#include "util/sss_krb5.h"
int sss_krb5_verify_keytab(const char *principal,
const char *realm_str,
const char *keytab_name)
{
krb5_context context = NULL;
krb5_keytab keytab = NULL;
krb5_error_code krberr;
int ret;
char *full_princ = NULL;
char *realm_name = NULL;
char *default_realm = NULL;
TALLOC_CTX *tmp_ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
krberr = krb5_init_context(&context);
if (krberr) {
DEBUG(2, ("Failed to init kerberos context\n"));
ret = EFAULT;
goto done;
}
if (keytab_name) {
krberr = krb5_kt_resolve(context, keytab_name, &keytab);
} else {
krberr = krb5_kt_default(context, &keytab);
}
if (krberr) {
DEBUG(0, ("Failed to read keytab file: %s\n",
sss_krb5_get_error_message(context, krberr)));
ret = EFAULT;
goto done;
}
if (!realm_str) {
krberr = krb5_get_default_realm(context, &default_realm);
if (krberr) {
DEBUG(2, ("Failed to get default realm name: %s\n",
sss_krb5_get_error_message(context, krberr)));
ret = EFAULT;
goto done;
}
realm_name = talloc_strdup(tmp_ctx, default_realm);
krb5_free_default_realm(context, default_realm);
if (!realm_name) {
ret = ENOMEM;
goto done;
}
} else {
realm_name = talloc_strdup(tmp_ctx, realm_str);
if (!realm_name) {
ret = ENOMEM;
goto done;
}
}
if (principal) {
if (!strchr(principal, '@')) {
full_princ = talloc_asprintf(tmp_ctx, "%s@%s",
principal, realm_name);
} else {
full_princ = talloc_strdup(tmp_ctx, principal);
}
} else {
char hostname[512];
ret = gethostname(hostname, 511);
if (ret == -1) {
ret = errno;
goto done;
}
hostname[511] = '\0';
full_princ = talloc_asprintf(tmp_ctx, "host/%s@%s",
hostname, realm_name);
}
if (!full_princ) {
ret = ENOMEM;
goto done;
}
DEBUG(4, ("Principal name is: [%s]\n", full_princ));
ret = sss_krb5_verify_keytab_ex(full_princ, keytab_name, context, keytab);
if (ret) goto done;
ret = EOK;
done:
if (keytab) krb5_kt_close(context, keytab);
if (context) krb5_free_context(context);
talloc_free(tmp_ctx);
return ret;
}
int sss_krb5_verify_keytab_ex(const char *principal, const char *keytab_name,
krb5_context context, krb5_keytab keytab)
{
bool found;
char *kt_principal;
krb5_error_code krberr;
krb5_kt_cursor cursor;
krb5_keytab_entry entry;
krberr = krb5_kt_start_seq_get(context, keytab, &cursor);
if (krberr) {
DEBUG(0, ("Cannot read keytab [%s].\n", keytab_name));
sss_log(SSS_LOG_ERR, "Error reading keytab file [%s]: [%d][%s]. "
"Unable to create GSSAPI-encrypted LDAP connection.",
keytab_name, krberr,
sss_krb5_get_error_message(context, krberr));
return EIO;
}
found = false;
while((krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
krb5_unparse_name(context, entry.principal, &kt_principal);
if (strcmp(principal, kt_principal) == 0) {
found = true;
}
free(kt_principal);
krberr = krb5_free_keytab_entry_contents(context, &entry);
if (krberr) {
/* This should never happen. The API docs for this function
* specify only success for this function
*/
DEBUG(1,("Could not free keytab entry contents\n"));
/* This is non-fatal, so we'll continue here */
}
if (found) {
break;
}
}
krberr = krb5_kt_end_seq_get(context, keytab, &cursor);
if (krberr) {
DEBUG(0, ("Could not close keytab.\n"));
sss_log(SSS_LOG_ERR, "Could not close keytab file [%s].",
keytab_name);
return EIO;
}
if (!found) {
DEBUG(0, ("Principal [%s] not found in keytab [%s]\n",
principal, keytab_name ? keytab_name : "default"));
sss_log(SSS_LOG_ERR, "Error processing keytab file [%s]: "
"Principal [%s] was not found. "
"Unable to create GSSAPI-encrypted LDAP connection.",
keytab_name, principal);
return EFAULT;
}
return EOK;
}
enum matching_mode {MODE_NORMAL, MODE_PREFIX, MODE_POSTFIX};
/**
* We only have primary and instances stored separately, we need to
* join them to one string and compare that string.
*
* @param ctx kerberos context
* @param principal principal we want to match
* @param pattern_primary primary part of the principal we want to
* perform matching against. It is possible to use * wildcard
* at the beginning or at the end of the string. If NULL, it
* will act as "*"
* @param pattern_realm realm part of the principal we want to perform
* the matching against. If NULL, it will act as "*"
*/
static bool match_principal(krb5_context ctx,
krb5_principal principal,
const char *pattern_primary,
const char *pattern_realm)
{
krb5_data *realm_data;
char *primary = NULL;
char *primary_str = NULL;
int primary_str_len = 0;
int tmp_len;
int len_diff;
int mode = MODE_NORMAL;
TALLOC_CTX *tmp_ctx;
bool ret;
realm_data = krb5_princ_realm(ctx, principal);
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
DEBUG(1, ("talloc_new failed\n"));
return false;
}
if (pattern_primary) {
tmp_len = strlen(pattern_primary);
if (pattern_primary[tmp_len] == '*') {
mode = MODE_PREFIX;
primary_str = talloc_strdup(tmp_ctx, pattern_primary);
primary_str[tmp_len] = '\0';
primary_str_len = tmp_len-1;
} else if (pattern_primary[0] == '*') {
mode = MODE_POSTFIX;
primary_str = talloc_strdup(tmp_ctx, pattern_primary+1);
primary_str_len = tmp_len-1;
}
krb5_unparse_name_flags(ctx, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM,
&primary);
len_diff = strlen(primary)-primary_str_len;
if ((mode == MODE_NORMAL &&
strcmp(primary, pattern_primary) != 0) ||
(mode == MODE_PREFIX &&
strncmp(primary, primary_str, primary_str_len) != 0) ||
(mode == MODE_POSTFIX &&
strcmp(primary+len_diff, primary_str) != 0)) {
ret = false;
goto done;
}
}
if (!pattern_realm || (realm_data->length == strlen(pattern_realm) &&
strncmp(realm_data->data, pattern_realm, realm_data->length) == 0)) {
DEBUG(7, ("Principal matched to the sample (%s@%s).\n", pattern_primary,
pattern_realm));
ret = true;
}
done:
free(primary);
talloc_free(tmp_ctx);
return ret;
}
krb5_error_code find_principal_in_keytab(krb5_context ctx,
krb5_keytab keytab,
const char *pattern_primary,
const char *pattern_realm,
krb5_principal *princ)
{
krb5_error_code kerr;
krb5_error_code kt_err;
krb5_error_code kerr_d;
krb5_kt_cursor cursor;
krb5_keytab_entry entry;
bool principal_found = false;
memset(&cursor, 0, sizeof(cursor));
kerr = krb5_kt_start_seq_get(ctx, keytab, &cursor);
if (kerr != 0) {
DEBUG(1, ("krb5_kt_start_seq_get failed.\n"));
return kerr;
}
DEBUG(9, ("Trying to find principal %s@%s in keytab.\n", pattern_primary, pattern_realm));
memset(&entry, 0, sizeof(entry));
while ((kt_err = krb5_kt_next_entry(ctx, keytab, &entry, &cursor)) == 0) {
principal_found = match_principal(ctx, entry.principal, pattern_primary, pattern_realm);
if (principal_found) {
break;
}
kerr = krb5_free_keytab_entry_contents(ctx, &entry);
if (kerr != 0) {
DEBUG(1, ("Failed to free keytab entry.\n"));
}
memset(&entry, 0, sizeof(entry));
}
/* Close the keytab here. Even though we're using cursors, the file
* handle is stored in the krb5_keytab structure, and it gets
* overwritten by other keytab calls, creating a leak. */
kerr = krb5_kt_end_seq_get(ctx, keytab, &cursor);
if (kerr != 0) {
DEBUG(1, ("krb5_kt_end_seq_get failed.\n"));
goto done;
}
if (!principal_found) {
kerr = KRB5_KT_NOTFOUND;
DEBUG(1, ("No principal matching %s@%s found in keytab.\n",
pattern_primary, pattern_realm));
goto done;
}
/* check if we got any errors from krb5_kt_next_entry */
if (kt_err != 0 && kt_err != KRB5_KT_END) {
DEBUG(1, ("Error while reading keytab.\n"));
goto done;
}
kerr = krb5_copy_principal(ctx, entry.principal, princ);
if (kerr != 0) {
DEBUG(1, ("krb5_copy_principal failed.\n"));
goto done;
}
kerr = 0;
done:
kerr_d = krb5_free_keytab_entry_contents(ctx, &entry);
if (kerr_d != 0) {
DEBUG(1, ("Failed to free keytab entry.\n"));
}
return kerr;
}
const char *KRB5_CALLCONV sss_krb5_get_error_message(krb5_context ctx,
krb5_error_code ec)
{
#ifdef HAVE_KRB5_GET_ERROR_MESSAGE
return krb5_get_error_message(ctx, ec);
#else
int ret;
char *s = NULL;
int size = sizeof("Kerberos error [XXXXXXXXXXXX]");
s = malloc(sizeof(char) * (size));
if (s == NULL) {
return NULL;
}
ret = snprintf(s, size, "Kerberos error [%12d]", ec);
if (ret < 0 || ret >= size) {
return NULL;
}
return s;
#endif
}
void KRB5_CALLCONV sss_krb5_free_error_message(krb5_context ctx, const char *s)
{
#ifdef HAVE_KRB5_GET_ERROR_MESSAGE
krb5_free_error_message(ctx, s);
#else
free(s);
#endif
return;
}
krb5_error_code KRB5_CALLCONV sss_krb5_get_init_creds_opt_alloc(
krb5_context context,
krb5_get_init_creds_opt **opt)
{
#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
return krb5_get_init_creds_opt_alloc(context, opt);
#else
*opt = calloc(1, sizeof(krb5_get_init_creds_opt));
if (*opt == NULL) {
return ENOMEM;
}
krb5_get_init_creds_opt_init(*opt);
return 0;
#endif
}
void KRB5_CALLCONV sss_krb5_get_init_creds_opt_free (krb5_context context,
krb5_get_init_creds_opt *opt)
{
#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
krb5_get_init_creds_opt_free(context, opt);
#else
free(opt);
#endif
return;
}
void KRB5_CALLCONV sss_krb5_free_unparsed_name(krb5_context context, char *name)
{
#ifdef HAVE_KRB5_FREE_UNPARSED_NAME
krb5_free_unparsed_name(context, name);
#else
if (name != NULL) {
memset(name, 0, strlen(name));
free(name);
}
#endif
}
krb5_error_code check_for_valid_tgt(const char *ccname, const char *realm,
const char *client_princ_str, bool *result)
{
krb5_context context = NULL;
krb5_ccache ccache = NULL;
krb5_error_code krberr;
TALLOC_CTX *tmp_ctx = NULL;
krb5_creds mcred;
krb5_creds cred;
char *server_name = NULL;
krb5_principal client_principal = NULL;
krb5_principal server_principal = NULL;
*result = false;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(1, ("talloc_new failed.\n"));
return ENOMEM;
}
krberr = krb5_init_context(&context);
if (krberr) {
DEBUG(1, ("Failed to init kerberos context\n"));
goto done;
}
krberr = krb5_cc_resolve(context, ccname, &ccache);
if (krberr != 0) {
DEBUG(1, ("krb5_cc_resolve failed.\n"));
goto done;
}
server_name = talloc_asprintf(tmp_ctx, "krbtgt/%s@%s", realm, realm);
if (server_name == NULL) {
DEBUG(1, ("talloc_asprintf failed.\n"));
krberr = ENOMEM;
goto done;
}
krberr = krb5_parse_name(context, server_name, &server_principal);
if (krberr != 0) {
DEBUG(1, ("krb5_parse_name failed.\n"));
goto done;
}
krberr = krb5_parse_name(context, client_princ_str, &client_principal);
if (krberr != 0) {
DEBUG(1, ("krb5_parse_name failed.\n"));
goto done;
}
memset(&mcred, 0, sizeof(mcred));
memset(&cred, 0, sizeof(mcred));
mcred.client = client_principal;
mcred.server = server_principal;
krberr = krb5_cc_retrieve_cred(context, ccache, 0, &mcred, &cred);
if (krberr != 0) {
DEBUG(1, ("krb5_cc_retrieve_cred failed.\n"));
krberr = 0;
goto done;
}
DEBUG(7, ("TGT end time [%d].\n", cred.times.endtime));
if (cred.times.endtime > time(NULL)) {
DEBUG(3, ("TGT is valid.\n"));
*result = true;
}
krb5_free_cred_contents(context, &cred);
krberr = 0;
done:
if (client_principal != NULL) {
krb5_free_principal(context, client_principal);
}
if (server_principal != NULL) {
krb5_free_principal(context, server_principal);
}
if (ccache != NULL) {
krb5_cc_close(context, ccache);
}
if (context != NULL) krb5_free_context(context);
talloc_free(tmp_ctx);
return krberr;
}
krb5_error_code KRB5_CALLCONV sss_krb5_get_init_creds_opt_set_expire_callback(
krb5_context context,
krb5_get_init_creds_opt *opt,
krb5_expire_callback_func cb,
void *data)
{
#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_EXPIRE_CALLBACK
return krb5_get_init_creds_opt_set_expire_callback(context, opt, cb, data);
#else
DEBUG(5, ("krb5_get_init_creds_opt_set_expire_callback not available.\n"));
return 0;
#endif
}
errno_t check_fast(const char *str, bool *use_fast)
{
#if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_FAST_FLAGS
if (strcasecmp(str, "never") == 0 ) {
*use_fast = false;
} else if (strcasecmp(str, "try") == 0 || strcasecmp(str, "demand") == 0) {
*use_fast = true;
} else {
sss_log(SSS_LOG_ALERT, "Unsupported value [%s] for option krb5_use_fast,"
"please use never, try, or demand.\n");
return EINVAL;
}
return EOK;
#else
sss_log(SSS_LOG_ALERT, "This build of sssd done not support FAST. "
"Please remove option krb5_use_fast.\n");
return EINVAL;
#endif
}
krb5_error_code KRB5_CALLCONV sss_krb5_get_init_creds_opt_set_fast_ccache_name(
krb5_context context,
krb5_get_init_creds_opt *opt,
const char *fast_ccache_name)
{
#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_FAST_CCACHE_NAME
return krb5_get_init_creds_opt_set_fast_ccache_name(context, opt,
fast_ccache_name);
#else
DEBUG(5, ("krb5_get_init_creds_opt_set_fast_ccache_name not available.\n"));
return 0;
#endif
}
krb5_error_code KRB5_CALLCONV sss_krb5_get_init_creds_opt_set_fast_flags(
krb5_context context,
krb5_get_init_creds_opt *opt,
krb5_flags flags)
{
#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_FAST_FLAGS
return krb5_get_init_creds_opt_set_fast_flags(context, opt, flags);
#else
DEBUG(5, ("krb5_get_init_creds_opt_set_fast_flags not available.\n"));
return 0;
#endif
}
|