log.c
1 /* 2 * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include "krb5_locl.h" 37 #include <vis.h> 38 39 struct facility { 40 int min; 41 int max; 42 krb5_log_log_func_t log_func; 43 krb5_log_close_func_t close_func; 44 void *data; 45 }; 46 47 static struct facility* 48 log_realloc(krb5_log_facility *f) 49 { 50 struct facility *fp; 51 fp = realloc(f->val, (f->len + 1) * sizeof(*f->val)); 52 if(fp == NULL) 53 return NULL; 54 f->len++; 55 f->val = fp; 56 fp += f->len - 1; 57 return fp; 58 } 59 60 struct s2i { 61 const char *s; 62 int val; 63 }; 64 65 #define L(X) { #X, LOG_ ## X } 66 67 static struct s2i syslogvals[] = { 68 L(EMERG), 69 L(ALERT), 70 L(CRIT), 71 L(ERR), 72 L(WARNING), 73 L(NOTICE), 74 L(INFO), 75 L(DEBUG), 76 77 L(AUTH), 78 #ifdef LOG_AUTHPRIV 79 L(AUTHPRIV), 80 #endif 81 #ifdef LOG_CRON 82 L(CRON), 83 #endif 84 L(DAEMON), 85 #ifdef LOG_FTP 86 L(FTP), 87 #endif 88 L(KERN), 89 L(LPR), 90 L(MAIL), 91 #ifdef LOG_NEWS 92 L(NEWS), 93 #endif 94 L(SYSLOG), 95 L(USER), 96 #ifdef LOG_UUCP 97 L(UUCP), 98 #endif 99 L(LOCAL0), 100 L(LOCAL1), 101 L(LOCAL2), 102 L(LOCAL3), 103 L(LOCAL4), 104 L(LOCAL5), 105 L(LOCAL6), 106 L(LOCAL7), 107 { NULL, -1 } 108 }; 109 110 #undef L 111 112 static int 113 find_value(const char *s, struct s2i *table) 114 { 115 while(table->s && strcasecmp(table->s, s)) 116 table++; 117 return table->val; 118 } 119 120 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 121 krb5_initlog(krb5_context context, 122 const char *program, 123 krb5_log_facility **fac) 124 { 125 krb5_log_facility *f = calloc(1, sizeof(*f)); 126 if(f == NULL) { 127 krb5_set_error_message(context, ENOMEM, 128 N_("malloc: out of memory", "")); 129 return ENOMEM; 130 } 131 f->program = strdup(program); 132 if(f->program == NULL){ 133 free(f); 134 krb5_set_error_message(context, ENOMEM, 135 N_("malloc: out of memory", "")); 136 return ENOMEM; 137 } 138 *fac = f; 139 return 0; 140 } 141 142 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 143 krb5_addlog_func(krb5_context context, 144 krb5_log_facility *fac, 145 int min, 146 int max, 147 krb5_log_log_func_t log_func, 148 krb5_log_close_func_t close_func, 149 void *data) 150 { 151 struct facility *fp = log_realloc(fac); 152 if(fp == NULL) { 153 krb5_set_error_message(context, ENOMEM, 154 N_("malloc: out of memory", "")); 155 return ENOMEM; 156 } 157 fp->min = min; 158 fp->max = max; 159 fp->log_func = log_func; 160 fp->close_func = close_func; 161 fp->data = data; 162 return 0; 163 } 164 165 166 struct _heimdal_syslog_data{ 167 int priority; 168 }; 169 170 static void KRB5_CALLCONV 171 log_syslog(const char *timestr, 172 const char *msg, 173 void *data) 174 175 { 176 struct _heimdal_syslog_data *s = data; 177 syslog(s->priority, "%s", msg); 178 } 179 180 static void KRB5_CALLCONV 181 close_syslog(void *data) 182 { 183 free(data); 184 closelog(); 185 } 186 187 static krb5_error_code 188 open_syslog(krb5_context context, 189 krb5_log_facility *facility, int min, int max, 190 const char *sev, const char *fac) 191 { 192 struct _heimdal_syslog_data *sd = malloc(sizeof(*sd)); 193 int i; 194 195 if(sd == NULL) { 196 krb5_set_error_message(context, ENOMEM, 197 N_("malloc: out of memory", "")); 198 return ENOMEM; 199 } 200 i = find_value(sev, syslogvals); 201 if(i == -1) 202 i = LOG_ERR; 203 sd->priority = i; 204 i = find_value(fac, syslogvals); 205 if(i == -1) 206 i = LOG_AUTH; 207 sd->priority |= i; 208 roken_openlog(facility->program, LOG_PID | LOG_NDELAY, i); 209 return krb5_addlog_func(context, facility, min, max, 210 log_syslog, close_syslog, sd); 211 } 212 213 #ifdef __APPLE__ 214 215 #include <asl.h> 216 217 #define L(X) { #X, ASL_LEVEL_ ## X } 218 219 static struct s2i aslvals[] = { 220 L(EMERG), 221 L(ALERT), 222 L(CRIT), 223 L(ERR), 224 L(WARNING), 225 L(NOTICE), 226 L(INFO), 227 L(DEBUG), 228 { NULL, -1 } 229 }; 230 231 #undef L 232 233 struct _heimdal_asl_data{ 234 aslclient client; 235 aslmsg msg; 236 int level; 237 }; 238 239 static void KRB5_CALLCONV 240 log_asl(const char *timestr, 241 const char *msg, 242 void *data) 243 { 244 struct _heimdal_asl_data *s = data; 245 asl_log(s->client, s->msg, s->level, "%s", msg); 246 } 247 248 static void KRB5_CALLCONV 249 close_asl(void *data) 250 { 251 struct _heimdal_asl_data *s = data; 252 asl_free(s->msg); 253 asl_close(s->client); 254 free(s); 255 } 256 257 static krb5_error_code 258 open_asl(krb5_context context, 259 krb5_log_facility *facility, int min, int max, 260 const char *sev, const char *fac) 261 { 262 struct _heimdal_asl_data *sd = malloc(sizeof(*sd)); 263 int i; 264 265 if(sd == NULL) { 266 krb5_set_error_message(context, ENOMEM, 267 N_("malloc: out of memory", "")); 268 return ENOMEM; 269 } 270 i = find_value(sev, aslvals); 271 if(i == -1) 272 i = ASL_LEVEL_ERR; 273 sd->level = i; 274 275 sd->client = asl_open(getprogname(), fac, 0); 276 277 sd->msg = asl_new(ASL_TYPE_MSG); 278 asl_set(sd->msg, "org.h5l.asl", "krb5"); 279 280 return krb5_addlog_func(context, facility, min, max, 281 log_asl, close_asl, sd); 282 } 283 284 #include <os/log.h> 285 286 static void KRB5_CALLCONV 287 log_oslog(const char *timestr, 288 const char *msg, 289 void *data) 290 { 291 os_log_t t = data; 292 os_log(t, "%{public}s", msg); 293 } 294 295 static void KRB5_CALLCONV 296 log_oslog_debug(const char *timestr, 297 const char *msg, 298 void *data) 299 { 300 os_log_t t = data; 301 os_log_debug(t, "%{public}s", msg); 302 } 303 304 static void KRB5_CALLCONV 305 close_oslog(void *data) 306 { 307 os_release((os_log_t)data); 308 } 309 310 static krb5_error_code 311 open_oslog(krb5_context context, 312 krb5_log_facility *facility, int min, int max, 313 const char *type, const char *fac) 314 { 315 os_log_t t = os_log_create("com.apple.Heimdal", fac); 316 krb5_log_log_func_t log = log_oslog; 317 if (strcmp(type, "debug") == 0) 318 log = log_oslog_debug; 319 return krb5_addlog_func(context, facility, min, max, 320 log, close_oslog, t); 321 } 322 323 324 #endif /* __APPLE__ */ 325 326 327 struct file_data{ 328 const char *filename; 329 const char *mode; 330 FILE *fd; 331 int keep_open; 332 int freefilename; 333 }; 334 335 static void KRB5_CALLCONV 336 log_file(const char *timestr, 337 const char *msg, 338 void *data) 339 { 340 struct file_data *f = data; 341 char *msgclean; 342 size_t len = strlen(msg); 343 FILE *fd = f->fd; 344 345 if(f->keep_open == 0) 346 fd = fopen(f->filename, f->mode); 347 if(fd == NULL) 348 return; 349 /* make sure the log doesn't contain special chars */ 350 msgclean = malloc((len + 1) * 4); 351 if (msgclean == NULL) 352 goto out; 353 strvisx(msgclean, rk_UNCONST(msg), len, VIS_OCTAL); 354 fprintf(fd, "%s %s\n", timestr, msgclean); 355 free(msgclean); 356 out: 357 if(f->keep_open == 0) 358 fclose(fd); 359 } 360 361 static void KRB5_CALLCONV 362 close_file(void *data) 363 { 364 struct file_data *f = data; 365 if(f->keep_open && f->filename) 366 fclose(f->fd); 367 if (f->filename && f->freefilename) 368 free((char *)f->filename); 369 free(data); 370 } 371 372 static krb5_error_code 373 open_file(krb5_context context, krb5_log_facility *fac, int min, int max, 374 const char *filename, const char *mode, FILE *f, int keep_open, 375 int freefilename) 376 { 377 struct file_data *fd = malloc(sizeof(*fd)); 378 if(fd == NULL) { 379 krb5_set_error_message(context, ENOMEM, 380 N_("malloc: out of memory", "")); 381 if (freefilename && filename) 382 free((char *)filename); 383 return ENOMEM; 384 } 385 fd->filename = filename; 386 fd->mode = mode; 387 fd->fd = f; 388 fd->keep_open = keep_open; 389 fd->freefilename = freefilename; 390 391 return krb5_addlog_func(context, fac, min, max, log_file, close_file, fd); 392 } 393 394 395 396 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 397 krb5_addlog_dest(krb5_context context, krb5_log_facility *f, const char *orig) 398 { 399 krb5_error_code ret = 0; 400 int min = 0, max = -1, n; 401 char c; 402 const char *p = orig; 403 404 n = sscanf(p, "%d%c%d/", &min, &c, &max); 405 if(n == 2){ 406 if(c == '/') { 407 if(min < 0){ 408 max = -min; 409 min = 0; 410 }else{ 411 max = min; 412 } 413 } 414 } 415 if(n){ 416 p = strchr(p, '/'); 417 if(p == NULL) { 418 krb5_set_error_message(context, HEIM_ERR_LOG_PARSE, 419 N_("failed to parse \"%s\"", ""), orig); 420 return HEIM_ERR_LOG_PARSE; 421 } 422 p++; 423 } 424 if(strcmp(p, "STDERR") == 0){ 425 ret = open_file(context, f, min, max, NULL, NULL, stderr, 1, 0); 426 }else if(strcmp(p, "CONSOLE") == 0){ 427 ret = open_file(context, f, min, max, "/dev/console", "w", NULL, 0, 0); 428 }else if(strncmp(p, "FILE", 4) == 0 && (p[4] == ':' || p[4] == '=')){ 429 char *fn; 430 FILE *file = NULL; 431 int keep_open = 0; 432 fn = strdup(p + 5); 433 if(fn == NULL) { 434 krb5_set_error_message(context, ENOMEM, 435 N_("malloc: out of memory", "")); 436 return ENOMEM; 437 } 438 if(p[4] == '='){ 439 int i = open(fn, O_WRONLY | O_CREAT | 440 O_TRUNC | O_APPEND, 0666); 441 if(i < 0) { 442 ret = errno; 443 krb5_set_error_message(context, ret, 444 N_("open(%s) logile: %s", ""), fn, 445 strerror(ret)); 446 free(fn); 447 return ret; 448 } 449 rk_cloexec(i); 450 file = fdopen(i, "a"); 451 if(file == NULL){ 452 ret = errno; 453 close(i); 454 krb5_set_error_message(context, ret, 455 N_("fdopen(%s) logfile: %s", ""), 456 fn, strerror(ret)); 457 free(fn); 458 return ret; 459 } 460 keep_open = 1; 461 } 462 ret = open_file(context, f, min, max, fn, "a", file, keep_open, 1); 463 }else if(strncmp(p, "DEVICE", 6) == 0 && (p[6] == ':' || p[6] == '=')){ 464 ret = open_file(context, f, min, max, strdup(p + 7), "w", NULL, 0, 1); 465 }else if(strncmp(p, "SYSLOG", 6) == 0 && (p[6] == '\0' || p[6] == ':')){ 466 char severity[128] = ""; 467 char facility[128] = ""; 468 p += 6; 469 if(*p != '\0') 470 p++; 471 if(strsep_copy(&p, ":", severity, sizeof(severity)) != -1) 472 strsep_copy(&p, ":", facility, sizeof(facility)); 473 if(*severity == '\0') 474 strlcpy(severity, "ERR", sizeof(severity)); 475 if(*facility == '\0') 476 strlcpy(facility, "AUTH", sizeof(facility)); 477 ret = open_syslog(context, f, min, max, severity, facility); 478 }else if(strncmp(p, "ASL", 3) == 0 && (p[3] == '\0' || p[3] == ':')){ 479 #ifdef __APPLE__ 480 char severity[128] = ""; 481 char facility[128] = ""; 482 p += 3; 483 if(*p != '\0') 484 p++; 485 if(strsep_copy(&p, ":", severity, sizeof(severity)) != -1) 486 strsep_copy(&p, ":", facility, sizeof(facility)); 487 if(*severity == '\0') 488 strlcpy(severity, "ERR", sizeof(severity)); 489 if(*facility == '\0') 490 strlcpy(facility, "AUTH", sizeof(facility)); 491 ret = open_asl(context, f, min, max, severity, facility); 492 #else 493 ret = HEIM_ERR_LOG_PARSE; 494 krb5_set_error_message (context, ret, 495 N_("asl is not supported on this platform", ""), p); 496 #endif /* __APPLE__ */ 497 }else if(strncmp(p, "OSLOG", 5) == 0 && (p[5] == '\0' || p[5] == ':')){ 498 #ifdef __APPLE__ 499 char type[128] = ""; 500 char facility[128] = ""; 501 p += 5; 502 if(*p != '\0') 503 p++; 504 if(strsep_copy(&p, ":", type, sizeof(type)) != -1) 505 strsep_copy(&p, ":", facility, sizeof(facility)); 506 if(*type == '\0') 507 strlcpy(type, "normal", sizeof(type)); 508 if(*facility == '\0') 509 strlcpy(facility, "general", sizeof(facility)); 510 ret = open_oslog(context, f, min, max, type, facility); 511 #else 512 ret = HEIM_ERR_LOG_PARSE; 513 krb5_set_error_message (context, ret, 514 N_("OSLOG is not supported on this platform", ""), p); 515 #endif /* __APPLE__ */ 516 }else{ 517 ret = HEIM_ERR_LOG_PARSE; /* XXX */ 518 krb5_set_error_message (context, ret, 519 N_("unknown log type: %s", ""), p); 520 } 521 return ret; 522 } 523 524 525 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 526 krb5_openlog(krb5_context context, 527 const char *program, 528 krb5_log_facility **fac) 529 { 530 krb5_error_code ret; 531 char **p, **q; 532 533 ret = krb5_initlog(context, program, fac); 534 if(ret) 535 return ret; 536 537 p = krb5_config_get_strings(context, NULL, "logging", program, NULL); 538 if(p == NULL) 539 p = krb5_config_get_strings(context, NULL, "logging", "default", NULL); 540 if(p){ 541 for(q = p; *q && ret == 0; q++) 542 ret = krb5_addlog_dest(context, *fac, *q); 543 krb5_config_free_strings(p); 544 }else 545 ret = krb5_addlog_dest(context, *fac, "SYSLOG"); 546 return ret; 547 } 548 549 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 550 krb5_closelog(krb5_context context, 551 krb5_log_facility *fac) 552 { 553 int i; 554 for(i = 0; i < fac->len; i++) 555 (*fac->val[i].close_func)(fac->val[i].data); 556 free(fac->val); 557 free(fac->program); 558 fac->val = NULL; 559 fac->len = 0; 560 fac->program = NULL; 561 free(fac); 562 return 0; 563 } 564 565 #undef __attribute__ 566 #define __attribute__(X) 567 568 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 569 krb5_vlog_msg(krb5_context context, 570 krb5_log_facility *fac, 571 char **reply, 572 int level, 573 const char *fmt, 574 va_list ap) 575 HEIMDAL_PRINTF_ATTRIBUTE((printf, 5, 0)) 576 { 577 578 char *msg = NULL; 579 const char *actual = NULL; 580 char buf[64]; 581 time_t t = 0; 582 int i; 583 584 for(i = 0; fac && i < fac->len; i++) 585 if(fac->val[i].min <= level && 586 (fac->val[i].max < 0 || fac->val[i].max >= level)) { 587 if(t == 0) { 588 t = time(NULL); 589 krb5_format_time(context, t, buf, sizeof(buf), TRUE); 590 } 591 if(actual == NULL) { 592 int ret = vasprintf(&msg, fmt, ap); 593 if(ret < 0 || msg == NULL) 594 actual = fmt; 595 else 596 actual = msg; 597 } 598 (*fac->val[i].log_func)(buf, actual, fac->val[i].data); 599 } 600 if(reply == NULL) 601 free(msg); 602 else 603 *reply = msg; 604 return 0; 605 } 606 607 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 608 krb5_vlog(krb5_context context, 609 krb5_log_facility *fac, 610 int level, 611 const char *fmt, 612 va_list ap) 613 HEIMDAL_PRINTF_ATTRIBUTE((printf, 4, 0)) 614 { 615 return krb5_vlog_msg(context, fac, NULL, level, fmt, ap); 616 } 617 618 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 619 krb5_log_msg(krb5_context context, 620 krb5_log_facility *fac, 621 int level, 622 char **reply, 623 const char *fmt, 624 ...) 625 HEIMDAL_PRINTF_ATTRIBUTE((printf, 5, 6)) 626 { 627 va_list ap; 628 krb5_error_code ret; 629 630 va_start(ap, fmt); 631 ret = krb5_vlog_msg(context, fac, reply, level, fmt, ap); 632 va_end(ap); 633 return ret; 634 } 635 636 637 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 638 krb5_log(krb5_context context, 639 krb5_log_facility *fac, 640 int level, 641 const char *fmt, 642 ...) 643 HEIMDAL_PRINTF_ATTRIBUTE((printf, 4, 5)) 644 { 645 va_list ap; 646 krb5_error_code ret; 647 648 va_start(ap, fmt); 649 ret = krb5_vlog(context, fac, level, fmt, ap); 650 va_end(ap); 651 return ret; 652 } 653 654 void KRB5_LIB_FUNCTION 655 _krb5_debugx(krb5_context context, 656 int level, 657 const char *fmt, 658 ...) 659 HEIMDAL_PRINTF_ATTRIBUTE((printf, 3, 4)) 660 { 661 va_list ap; 662 663 if (context == NULL || context->debug_dest == NULL) 664 return; 665 666 va_start(ap, fmt); 667 krb5_vlog(context, context->debug_dest, level, fmt, ap); 668 va_end(ap); 669 } 670 671 void KRB5_LIB_FUNCTION 672 _krb5_debug(krb5_context context, 673 int level, 674 krb5_error_code ret, 675 const char *fmt, 676 ...) 677 HEIMDAL_PRINTF_ATTRIBUTE((printf, 4, 5)) 678 { 679 va_list ap; 680 char *str = NULL; 681 const char *e; 682 683 if (context == NULL || context->debug_dest == NULL) 684 return; 685 686 va_start(ap, fmt); 687 vasprintf(&str, fmt, ap); 688 va_end(ap); 689 if (str == NULL) 690 return; 691 e = krb5_get_error_message(context, ret); 692 krb5_log(context, context->debug_dest, level, "%s: %s", str, e ? e : "<unknown error>"); 693 krb5_free_error_message(context, e); 694 free(str); 695 } 696 697 698 krb5_boolean KRB5_LIB_FUNCTION 699 _krb5_have_debug(krb5_context context, int level) 700 { 701 if (context == NULL || context->debug_dest == NULL) 702 return 0 ; 703 return 1; 704 }