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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
/*
* Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Portions Copyright (c) 2009 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "krb5_locl.h"
typedef struct krb5_dcache{
krb5_ccache fcache;
char *dir;
char *name;
} krb5_dcache;
#define DCACHE(X) ((krb5_dcache*)(X)->data.data)
#define D2FCACHE(X) ((X)->fcache)
static krb5_error_code KRB5_CALLCONV dcc_close(krb5_context, krb5_ccache);
static krb5_error_code KRB5_CALLCONV dcc_get_default_name(krb5_context, char **);
static char *
primary_create(krb5_dcache *dc)
{
char *primary = NULL;
asprintf(&primary, "%s/primary", dc->dir);
if (primary == NULL)
return NULL;
return primary;
}
static int
is_filename_cacheish(const char *name)
{
return strncmp(name, "tkt", 3) == 0;
}
static krb5_error_code
set_default_cache(krb5_context context, krb5_dcache *dc, const char *residual)
{
char *path = NULL, *primary = NULL;
krb5_error_code ret;
struct iovec iov[2];
size_t len;
int fd = -1;
if (!is_filename_cacheish(residual)) {
krb5_set_error_message(context, KRB5_CC_FORMAT,
"name %s is not a cache (doesn't start with tkt)", residual);
return KRB5_CC_FORMAT;
}
asprintf(&path, "%s/primary-XXXXXX", dc->dir);
if (path == NULL)
return krb5_enomem(context);
fd = mkstemp(path);
if (fd < 0) {
ret = errno;
goto out;
}
rk_cloexec(fd);
#ifndef _WIN32
if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) {
ret = errno;
goto out;
}
#endif
len = strlen(residual);
iov[0].iov_base = rk_UNCONST(residual);
iov[0].iov_len = len;
iov[1].iov_base = "\n";
iov[1].iov_len = 1;
if (writev(fd, iov, sizeof(iov)/sizeof(iov[0])) != len + 1) {
ret = errno;
goto out;
}
primary = primary_create(dc);
if (primary == NULL) {
ret = krb5_enomem(context);
goto out;
}
if (rename(path, primary) < 0) {
ret = errno;
goto out;
}
close(fd);
fd = -1;
ret = 0;
out:
if (fd >= 0) {
(void)unlink(path);
close(fd);
}
if (path)
free(path);
if (primary)
free(primary);
return ret;
}
static krb5_error_code
get_default_cache(krb5_context context, krb5_dcache *dc, char **residual)
{
krb5_error_code ret;
char buf[MAXPATHLEN];
char *primary;
FILE *f;
*residual = NULL;
primary = primary_create(dc);
if (primary == NULL)
return krb5_enomem(context);
f = fopen(primary, "r");
if (f == NULL) {
if (errno == ENOENT) {
free(primary);
*residual = strdup("tkt");
if (*residual == NULL)
return krb5_enomem(context);
return 0;
}
ret = errno;
krb5_set_error_message(context, ret, "failed to open %s", primary);
free(primary);
return ret;
}
if (fgets(buf, sizeof(buf), f) == NULL) {
ret = ferror(f);
fclose(f);
krb5_set_error_message(context, ret, "read file %s", primary);
free(primary);
return ret;
}
fclose(f);
buf[strcspn(buf, "\r\n")] = '\0';
if (!is_filename_cacheish(buf)) {
krb5_set_error_message(context, KRB5_CC_FORMAT,
"name in %s is not a cache (doesn't start with tkt)", primary);
free(primary);
return KRB5_CC_FORMAT;
}
free(primary);
*residual = strdup(buf);
if (*residual == NULL)
return krb5_enomem(context);
return 0;
}
static const char* KRB5_CALLCONV
dcc_get_name(krb5_context context,
krb5_ccache id)
{
krb5_dcache *dc = DCACHE(id);
return dc->name;
}
static krb5_error_code
verify_directory(krb5_context context, const char *path)
{
struct stat sb;
if (stat(path, &sb) != 0) {
if (errno == ENOENT) {
/* XXX should use mkdirx_np() */
if (rk_mkdir(path, S_IRWXU) == 0)
return 0;
krb5_set_error_message(context, ENOENT,
N_("DIR directory %s doesn't exists", ""), path);
return ENOENT;
} else {
int ret = errno;
krb5_set_error_message(context, ret,
N_("DIR directory %s is bad: %s", ""), path, strerror(ret));
return errno;
}
}
if (!S_ISDIR(sb.st_mode)) {
krb5_set_error_message(context, KRB5_CC_BADNAME,
N_("DIR directory %s is not a directory", ""), path);
return KRB5_CC_BADNAME;
}
return 0;
}
static void
dcc_release(krb5_context context, krb5_dcache *dc)
{
if (dc->fcache)
krb5_cc_close(context, dc->fcache);
if (dc->dir)
free(dc->dir);
if (dc->name)
free(dc->name);
memset(dc, 0, sizeof(*dc));
free(dc);
}
static krb5_error_code KRB5_CALLCONV
dcc_resolve(krb5_context context, krb5_ccache *id, const char *res)
{
char *filename = NULL;
krb5_error_code ret;
krb5_dcache *dc;
const char *p;
p = res;
do {
p = strstr(p, "..");
if (p && (p == res || ISPATHSEP(p[-1])) && (ISPATHSEP(p[2]) || p[2] == '\0')) {
krb5_set_error_message(context, KRB5_CC_FORMAT,
N_("Path contains a .. component", ""));
return KRB5_CC_FORMAT;
}
if (p)
p += 3;
} while (p);
dc = calloc(1, sizeof(*dc));
if (dc == NULL) {
krb5_set_error_message(context, KRB5_CC_NOMEM,
N_("malloc: out of memory", ""));
return KRB5_CC_NOMEM;
}
/* check for explicit component */
if (res[0] == ':') {
char *q;
dc->dir = strdup(&res[1]);
#ifdef _WIN32
q = strrchr(dc->dir, '\\');
if (q == NULL)
#endif
q = strrchr(dc->dir, '/');
if (q) {
*q++ = '\0';
} else {
krb5_set_error_message(context, KRB5_CC_FORMAT, N_("Cache not an absolute path: %s", ""), dc->dir);
dcc_release(context, dc);
return KRB5_CC_FORMAT;
}
if (!is_filename_cacheish(q)) {
krb5_set_error_message(context, KRB5_CC_FORMAT,
N_("Name %s is not a cache (doesn't start with tkt)", ""), q);
dcc_release(context, dc);
return KRB5_CC_FORMAT;
}
ret = verify_directory(context, dc->dir);
if (ret) {
dcc_release(context, dc);
return ret;
}
dc->name = strdup(res);
if (dc->name == NULL) {
dcc_release(context, dc);
return krb5_enomem(context);
}
} else {
char *residual;
size_t len;
dc->dir = strdup(res);
if (dc->dir == NULL) {
dcc_release(context, dc);
return krb5_enomem(context);
}
len = strlen(dc->dir);
if (ISPATHSEP(dc->dir[len - 1]))
dc->dir[len - 1] = '\0';
ret = verify_directory(context, dc->dir);
if (ret) {
dcc_release(context, dc);
return ret;
}
ret = get_default_cache(context, dc, &residual);
if (ret) {
dcc_release(context, dc);
return ret;
}
asprintf(&dc->name, ":%s/%s", dc->dir, residual);
free(residual);
if (dc->name == NULL) {
dcc_release(context, dc);
return krb5_enomem(context);
}
}
asprintf(&filename, "FILE%s", dc->name);
if (filename == NULL) {
dcc_release(context, dc);
return krb5_enomem(context);
}
ret = krb5_cc_resolve(context, filename, &dc->fcache);
free(filename);
if (ret) {
dcc_release(context, dc);
return ret;
}
(*id)->data.data = dc;
(*id)->data.length = sizeof(*dc);
return 0;
}
static char *
copy_default_dcc_cache(krb5_context context)
{
const char *defname;
krb5_error_code ret;
char *name = NULL;
size_t len;
len = strlen(krb5_dcc_ops.prefix);
defname = krb5_cc_default_name(context);
if (defname == NULL ||
strncmp(defname, krb5_dcc_ops.prefix, len) != 0 ||
defname[len] != ':')
{
ret = dcc_get_default_name(context, &name);
if (ret)
return NULL;
return name;
} else {
return strdup(&defname[len + 1]);
}
}
static krb5_error_code KRB5_CALLCONV
dcc_gen_new(krb5_context context, krb5_ccache *id)
{
krb5_error_code ret;
char *name = NULL;
krb5_dcache *dc;
int fd;
size_t len;
name = copy_default_dcc_cache(context);
if (name == NULL) {
krb5_set_error_message(context, KRB5_CC_FORMAT,
N_("Can't generate DIR caches unless its the default type", ""));
return KRB5_CC_FORMAT;
}
len = strlen(krb5_dcc_ops.prefix);
if (strncmp(name, krb5_dcc_ops.prefix, len) == 0 && name[len] == ':')
++len;
else
len = 0;
ret = dcc_resolve(context, id, name + len);
free(name);
name = NULL;
if (ret)
return ret;
dc = DCACHE((*id));
asprintf(&name, ":%s/tktXXXXXX", dc->dir);
if (name == NULL) {
dcc_close(context, *id);
return krb5_enomem(context);
}
fd = mkstemp(&name[1]);
if (fd < 0) {
dcc_close(context, *id);
return krb5_enomem(context);
}
close(fd);
free(dc->name);
dc->name = name;
return 0;
}
static krb5_error_code KRB5_CALLCONV
dcc_initialize(krb5_context context,
krb5_ccache id,
krb5_principal primary_principal)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_initialize(context, D2FCACHE(dc), primary_principal);
}
static krb5_error_code KRB5_CALLCONV
dcc_close(krb5_context context,
krb5_ccache id)
{
dcc_release(context, DCACHE(id));
return 0;
}
static krb5_error_code KRB5_CALLCONV
dcc_destroy(krb5_context context,
krb5_ccache id)
{
krb5_dcache *dc = DCACHE(id);
krb5_ccache fcache = D2FCACHE(dc);
dc->fcache = NULL;
return krb5_cc_destroy(context, fcache);
}
static krb5_error_code KRB5_CALLCONV
dcc_store_cred(krb5_context context,
krb5_ccache id,
krb5_creds *creds)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_store_cred(context, D2FCACHE(dc), creds);
}
static krb5_error_code KRB5_CALLCONV
dcc_get_principal(krb5_context context,
krb5_ccache id,
krb5_principal *principal)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_get_principal(context, D2FCACHE(dc), principal);
}
static krb5_error_code KRB5_CALLCONV
dcc_get_first (krb5_context context,
krb5_ccache id,
krb5_cc_cursor *cursor)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_start_seq_get(context, D2FCACHE(dc), cursor);
}
static krb5_error_code KRB5_CALLCONV
dcc_get_next (krb5_context context,
krb5_ccache id,
krb5_cc_cursor *cursor,
krb5_creds *creds)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_next_cred(context, D2FCACHE(dc), cursor, creds);
}
static krb5_error_code KRB5_CALLCONV
dcc_end_get (krb5_context context,
krb5_ccache id,
krb5_cc_cursor *cursor)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_end_seq_get(context, D2FCACHE(dc), cursor);
}
static krb5_error_code KRB5_CALLCONV
dcc_remove_cred(krb5_context context,
krb5_ccache id,
krb5_flags which,
krb5_creds *cred)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_remove_cred(context, D2FCACHE(dc), which, cred);
}
static krb5_error_code KRB5_CALLCONV
dcc_set_flags(krb5_context context,
krb5_ccache id,
krb5_flags flags)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_set_flags(context, D2FCACHE(dc), flags);
}
static int KRB5_CALLCONV
dcc_get_version(krb5_context context,
krb5_ccache id)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_get_version(context, D2FCACHE(dc));
}
struct dcache_iter {
int first;
krb5_dcache *dc;
};
static krb5_error_code KRB5_CALLCONV
dcc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
{
struct dcache_iter *iter;
krb5_error_code ret;
char *name;
*cursor = NULL;
iter = calloc(1, sizeof(*iter));
if (iter == NULL)
return krb5_enomem(context);
iter->first = 1;
name = copy_default_dcc_cache(context);
if (name == NULL) {
free(iter);
krb5_set_error_message(context, KRB5_CC_FORMAT,
N_("Can't generate DIR caches unless its the default type", ""));
return KRB5_CC_FORMAT;
}
ret = dcc_resolve(context, NULL, name);
free(name);
if (ret) {
free(iter);
return ret;
}
/* XXX We need to opendir() here */
*cursor = iter;
return 0;
}
static krb5_error_code KRB5_CALLCONV
dcc_get_cache_next(krb5_context context, krb5_cc_cursor cursor, krb5_ccache *id)
{
struct dcache_iter *iter = cursor;
if (iter == NULL)
return krb5_einval(context, 2);
if (!iter->first) {
krb5_clear_error_message(context);
return KRB5_CC_END;
}
/* XXX We need to readdir() here */
iter->first = 0;
return KRB5_CC_END;
}
static krb5_error_code KRB5_CALLCONV
dcc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
{
struct dcache_iter *iter = cursor;
if (iter == NULL)
return krb5_einval(context, 2);
/* XXX We need to closedir() here */
if (iter->dc)
dcc_release(context, iter->dc);
free(iter);
return 0;
}
static krb5_error_code KRB5_CALLCONV
dcc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
{
krb5_dcache *dcfrom = DCACHE(from);
krb5_dcache *dcto = DCACHE(to);
return krb5_cc_move(context, D2FCACHE(dcfrom), D2FCACHE(dcto));
}
static krb5_error_code KRB5_CALLCONV
dcc_get_default_name(krb5_context context, char **str)
{
return _krb5_expand_default_cc_name(context,
KRB5_DEFAULT_CCNAME_DIR,
str);
}
static krb5_error_code KRB5_CALLCONV
dcc_set_default(krb5_context context, krb5_ccache id)
{
krb5_dcache *dc = DCACHE(id);
const char *name;
name = krb5_cc_get_name(context, D2FCACHE(dc));
if (name == NULL)
return ENOENT;
return set_default_cache(context, dc, name);
}
static krb5_error_code KRB5_CALLCONV
dcc_lastchange(krb5_context context, krb5_ccache id, krb5_timestamp *mtime)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_last_change_time(context, D2FCACHE(dc), mtime);
}
static krb5_error_code KRB5_CALLCONV
dcc_set_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat kdc_offset)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_set_kdc_offset(context, D2FCACHE(dc), kdc_offset);
}
static krb5_error_code KRB5_CALLCONV
dcc_get_kdc_offset(krb5_context context, krb5_ccache id, krb5_deltat *kdc_offset)
{
krb5_dcache *dc = DCACHE(id);
return krb5_cc_get_kdc_offset(context, D2FCACHE(dc), kdc_offset);
}
/**
* Variable containing the DIR based credential cache implemention.
*
* @ingroup krb5_ccache
*/
KRB5_LIB_VARIABLE const krb5_cc_ops krb5_dcc_ops = {
KRB5_CC_OPS_VERSION,
"DIR",
dcc_get_name,
dcc_resolve,
dcc_gen_new,
dcc_initialize,
dcc_destroy,
dcc_close,
dcc_store_cred,
NULL, /* dcc_retrieve */
dcc_get_principal,
dcc_get_first,
dcc_get_next,
dcc_end_get,
dcc_remove_cred,
dcc_set_flags,
dcc_get_version,
dcc_get_cache_first,
dcc_get_cache_next,
dcc_end_cache_get,
dcc_move,
dcc_get_default_name,
dcc_set_default,
dcc_lastchange,
dcc_set_kdc_offset,
dcc_get_kdc_offset
};
|