1 /* 2 * Copyright (c) 1997-2000 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "krb5_locl.h" 35 36 RCSID("$Id: log.c,v 1.26 2001/05/14 06:14:49 assar Exp $"); 37 38 struct facility { 39 int min; 40 int max; 41 krb5_log_log_func_t log; 42 krb5_log_close_func_t close; 43 void *data; 44 }; 45 46 static struct facility* 47 log_realloc(krb5_log_facility *f) 48 { 49 struct facility *fp; 50 f->len++; 51 fp = realloc(f->val, f->len * sizeof(*f->val)); 52 if(fp == NULL) 53 return NULL; 54 f->val = fp; 55 fp += f->len - 1; 56 return fp; 57 } 58 59 struct s2i { 60 char *s; 61 int val; 62 }; 63 64 #define L(X) { #X, LOG_ ## X } 65 66 static struct s2i syslogvals[] = { 67 L(EMERG), 68 L(ALERT), 69 L(CRIT), 70 L(ERR), 71 L(WARNING), 72 L(NOTICE), 73 L(INFO), 74 L(DEBUG), 75 76 L(AUTH), 77 #ifdef LOG_AUTHPRIV 78 L(AUTHPRIV), 79 #endif 80 #ifdef LOG_CRON 81 L(CRON), 82 #endif 83 L(DAEMON), 84 #ifdef LOG_FTP 85 L(FTP), 86 #endif 87 L(KERN), 88 L(LPR), 89 L(MAIL), 90 #ifdef LOG_NEWS 91 L(NEWS), 92 #endif 93 L(SYSLOG), 94 L(USER), 95 #ifdef LOG_UUCP 96 L(UUCP), 97 #endif 98 L(LOCAL0), 99 L(LOCAL1), 100 L(LOCAL2), 101 L(LOCAL3), 102 L(LOCAL4), 103 L(LOCAL5), 104 L(LOCAL6), 105 L(LOCAL7), 106 { NULL, -1 } 107 }; 108 109 static int 110 find_value(const char *s, struct s2i *table) 111 { 112 while(table->s && strcasecmp(table->s, s)) 113 table++; 114 return table->val; 115 } 116 117 krb5_error_code 118 krb5_initlog(krb5_context context, 119 const char *program, 120 krb5_log_facility **fac) 121 { 122 krb5_log_facility *f = calloc(1, sizeof(*f)); 123 if(f == NULL) { 124 krb5_set_error_string (context, "malloc: out of memory"); 125 return ENOMEM; 126 } 127 f->program = strdup(program); 128 if(f->program == NULL){ 129 free(f); 130 krb5_set_error_string (context, "malloc: out of memory"); 131 return ENOMEM; 132 } 133 *fac = f; 134 return 0; 135 } 136 137 krb5_error_code 138 krb5_addlog_func(krb5_context context, 139 krb5_log_facility *fac, 140 int min, 141 int max, 142 krb5_log_log_func_t log, 143 krb5_log_close_func_t close, 144 void *data) 145 { 146 struct facility *fp = log_realloc(fac); 147 if(fp == NULL) { 148 krb5_set_error_string (context, "malloc: out of memory"); 149 return ENOMEM; 150 } 151 fp->min = min; 152 fp->max = max; 153 fp->log = log; 154 fp->close = close; 155 fp->data = data; 156 return 0; 157 } 158 159 160 struct syslog_data{ 161 int priority; 162 }; 163 164 static void 165 log_syslog(const char *time, 166 const char *msg, 167 void *data) 168 169 { 170 struct syslog_data *s = data; 171 syslog(s->priority, "%s", msg); 172 } 173 174 static void 175 close_syslog(void *data) 176 { 177 free(data); 178 closelog(); 179 } 180 181 static krb5_error_code 182 open_syslog(krb5_context context, 183 krb5_log_facility *facility, int min, int max, 184 const char *sev, const char *fac) 185 { 186 struct syslog_data *sd = malloc(sizeof(*sd)); 187 int i; 188 189 if(sd == NULL) { 190 krb5_set_error_string (context, "malloc: out of memory"); 191 return ENOMEM; 192 } 193 i = find_value(sev, syslogvals); 194 if(i == -1) 195 i = LOG_ERR; 196 sd->priority = i; 197 i = find_value(fac, syslogvals); 198 if(i == -1) 199 i = LOG_AUTH; 200 sd->priority |= i; 201 roken_openlog(facility->program, LOG_PID | LOG_NDELAY, i); 202 return krb5_addlog_func(context, facility, min, max, 203 log_syslog, close_syslog, sd); 204 } 205 206 struct file_data{ 207 char *filename; 208 char *mode; 209 FILE *fd; 210 int keep_open; 211 }; 212 213 static void 214 log_file(const char *time, 215 const char *msg, 216 void *data) 217 { 218 struct file_data *f = data; 219 if(f->keep_open == 0) 220 f->fd = fopen(f->filename, f->mode); 221 if(f->fd == NULL) 222 return; 223 fprintf(f->fd, "%s %s\n", time, msg); 224 if(f->keep_open == 0) 225 fclose(f->fd); 226 } 227 228 static void 229 close_file(void *data) 230 { 231 struct file_data *f = data; 232 if(f->keep_open && f->filename) 233 fclose(f->fd); 234 free(data); 235 } 236 237 static krb5_error_code 238 open_file(krb5_context context, krb5_log_facility *fac, int min, int max, 239 char *filename, char *mode, FILE *f, int keep_open) 240 { 241 struct file_data *fd = malloc(sizeof(*fd)); 242 if(fd == NULL) { 243 krb5_set_error_string (context, "malloc: out of memory"); 244 return ENOMEM; 245 } 246 fd->filename = filename; 247 fd->mode = mode; 248 fd->fd = f; 249 fd->keep_open = keep_open; 250 251 return krb5_addlog_func(context, fac, min, max, log_file, close_file, fd); 252 } 253 254 255 256 krb5_error_code 257 krb5_addlog_dest(krb5_context context, krb5_log_facility *f, const char *orig) 258 { 259 krb5_error_code ret = 0; 260 int min = 0, max = -1, n; 261 char c; 262 const char *p = orig; 263 264 n = sscanf(p, "%d%c%d/", &min, &c, &max); 265 if(n == 2){ 266 if(c == '/') { 267 if(min < 0){ 268 max = -min; 269 min = 0; 270 }else{ 271 max = min; 272 } 273 } 274 } 275 if(n){ 276 p = strchr(p, '/'); 277 if(p == NULL) { 278 krb5_set_error_string (context, "failed to parse \"%s\"", orig); 279 return HEIM_ERR_LOG_PARSE; 280 } 281 p++; 282 } 283 if(strcmp(p, "STDERR") == 0){ 284 ret = open_file(context, f, min, max, NULL, NULL, stderr, 1); 285 }else if(strcmp(p, "CONSOLE") == 0){ 286 ret = open_file(context, f, min, max, "/dev/console", "w", NULL, 0); 287 }else if(strncmp(p, "FILE:", 4) == 0 && (p[4] == ':' || p[4] == '=')){ 288 char *fn; 289 FILE *file = NULL; 290 int keep_open = 0; 291 fn = strdup(p + 5); 292 if(fn == NULL) { 293 krb5_set_error_string (context, "malloc: out of memory"); 294 return ENOMEM; 295 } 296 if(p[4] == '='){ 297 int i = open(fn, O_WRONLY | O_CREAT | 298 O_TRUNC | O_APPEND, 0666); 299 if(i < 0) { 300 ret = errno; 301 krb5_set_error_string (context, "open(%s): %s", fn, 302 strerror(ret)); 303 return ret; 304 } 305 file = fdopen(i, "a"); 306 if(file == NULL){ 307 ret = errno; 308 close(i); 309 krb5_set_error_string (context, "fdopen(%s): %s", fn, 310 strerror(ret)); 311 return ret; 312 } 313 keep_open = 1; 314 } 315 ret = open_file(context, f, min, max, fn, "a", file, keep_open); 316 }else if(strncmp(p, "DEVICE=", 6) == 0){ 317 ret = open_file(context, f, min, max, strdup(p + 7), "w", NULL, 0); 318 }else if(strncmp(p, "SYSLOG", 6) == 0){ 319 char *severity; 320 char *facility; 321 severity = strchr(p, ':'); 322 if(severity == NULL) 323 severity = "ERR"; 324 facility = strchr(severity, ':'); 325 if(facility == NULL) 326 facility = "AUTH"; 327 ret = open_syslog(context, f, min, max, severity, facility); 328 }else{ 329 krb5_set_error_string (context, "unknown log type: %s", p); 330 ret = HEIM_ERR_LOG_PARSE; /* XXX */ 331 } 332 return ret; 333 } 334 335 336 krb5_error_code 337 krb5_openlog(krb5_context context, 338 const char *program, 339 krb5_log_facility **fac) 340 { 341 krb5_error_code ret; 342 char **p, **q; 343 344 ret = krb5_initlog(context, program, fac); 345 if(ret) 346 return ret; 347 348 p = krb5_config_get_strings(context, NULL, "logging", program, NULL); 349 if(p == NULL) 350 p = krb5_config_get_strings(context, NULL, "logging", "default", NULL); 351 if(p){ 352 for(q = p; *q; q++) 353 ret = krb5_addlog_dest(context, *fac, *q); 354 krb5_config_free_strings(p); 355 }else 356 ret = krb5_addlog_dest(context, *fac, "SYSLOG"); 357 return 0; 358 } 359 360 krb5_error_code 361 krb5_closelog(krb5_context context, 362 krb5_log_facility *fac) 363 { 364 int i; 365 for(i = 0; i < fac->len; i++) 366 (*fac->val[i].close)(&fac->val[i].data); 367 return 0; 368 } 369 370 #undef __attribute__ 371 #define __attribute__(X) 372 373 krb5_error_code 374 krb5_vlog_msg(krb5_context context, 375 krb5_log_facility *fac, 376 char **reply, 377 int level, 378 const char *fmt, 379 va_list ap) 380 __attribute__((format (printf, 5, 0))) 381 { 382 char *msg; 383 const char *actual; 384 char buf[64]; 385 time_t t; 386 int i; 387 388 vasprintf(&msg, fmt, ap); 389 if (msg != NULL) 390 actual = msg; 391 else 392 actual = fmt; 393 t = time(NULL); 394 krb5_format_time(context, t, buf, sizeof(buf), TRUE); 395 for(i = 0; i < fac->len; i++) 396 if(fac->val[i].min <= level && 397 (fac->val[i].max < 0 || fac->val[i].max >= level)) 398 (*fac->val[i].log)(buf, actual, fac->val[i].data); 399 *reply = msg; 400 return 0; 401 } 402 403 krb5_error_code 404 krb5_vlog(krb5_context context, 405 krb5_log_facility *fac, 406 int level, 407 const char *fmt, 408 va_list ap) 409 __attribute__((format (printf, 4, 0))) 410 { 411 char *msg; 412 krb5_error_code ret; 413 414 ret = krb5_vlog_msg(context, fac, &msg, level, fmt, ap); 415 free(msg); 416 return ret; 417 } 418 419 krb5_error_code 420 krb5_log_msg(krb5_context context, 421 krb5_log_facility *fac, 422 int level, 423 char **reply, 424 const char *fmt, 425 ...) 426 __attribute__((format (printf, 5, 6))) 427 { 428 va_list ap; 429 krb5_error_code ret; 430 431 va_start(ap, fmt); 432 ret = krb5_vlog_msg(context, fac, reply, level, fmt, ap); 433 va_end(ap); 434 return ret; 435 } 436 437 438 krb5_error_code 439 krb5_log(krb5_context context, 440 krb5_log_facility *fac, 441 int level, 442 const char *fmt, 443 ...) 444 __attribute__((format (printf, 4, 5))) 445 { 446 va_list ap; 447 krb5_error_code ret; 448 449 va_start(ap, fmt); 450 ret = krb5_vlog(context, fac, level, fmt, ap); 451 va_end(ap); 452 return ret; 453 } 454 455