2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "krb5_locl.h"
36 RCSID("$Id: log.c,v 1.31 2002/09/05 14:59:14 joda Exp $");
41 krb5_log_log_func_t log;
42 krb5_log_close_func_t close;
46 static struct facility*
47 log_realloc(krb5_log_facility *f)
51 fp = realloc(f->val, f->len * sizeof(*f->val));
64 #define L(X) { #X, LOG_ ## X }
66 static struct s2i syslogvals[] = {
110 find_value(const char *s, struct s2i *table)
112 while(table->s && strcasecmp(table->s, s))
118 krb5_initlog(krb5_context context,
120 krb5_log_facility **fac)
122 krb5_log_facility *f = calloc(1, sizeof(*f));
124 krb5_set_error_string (context, "malloc: out of memory");
127 f->program = strdup(program);
128 if(f->program == NULL){
130 krb5_set_error_string (context, "malloc: out of memory");
138 krb5_addlog_func(krb5_context context,
139 krb5_log_facility *fac,
142 krb5_log_log_func_t log,
143 krb5_log_close_func_t close,
146 struct facility *fp = log_realloc(fac);
148 krb5_set_error_string (context, "malloc: out of memory");
160 struct _heimdal_syslog_data{
165 log_syslog(const char *time,
170 struct _heimdal_syslog_data *s = data;
171 syslog(s->priority, "%s", msg);
175 close_syslog(void *data)
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)
186 struct _heimdal_syslog_data *sd = malloc(sizeof(*sd));
190 krb5_set_error_string (context, "malloc: out of memory");
193 i = find_value(sev, syslogvals);
197 i = find_value(fac, syslogvals);
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);
207 const char *filename;
214 log_file(const char *time,
218 struct file_data *f = data;
219 if(f->keep_open == 0)
220 f->fd = fopen(f->filename, f->mode);
223 fprintf(f->fd, "%s %s\n", time, msg);
224 if(f->keep_open == 0)
229 close_file(void *data)
231 struct file_data *f = data;
232 if(f->keep_open && f->filename)
237 static krb5_error_code
238 open_file(krb5_context context, krb5_log_facility *fac, int min, int max,
239 const char *filename, const char *mode, FILE *f, int keep_open)
241 struct file_data *fd = malloc(sizeof(*fd));
243 krb5_set_error_string (context, "malloc: out of memory");
246 fd->filename = filename;
249 fd->keep_open = keep_open;
251 return krb5_addlog_func(context, fac, min, max, log_file, close_file, fd);
257 krb5_addlog_dest(krb5_context context, krb5_log_facility *f, const char *orig)
259 krb5_error_code ret = 0;
260 int min = 0, max = -1, n;
262 const char *p = orig;
264 n = sscanf(p, "%d%c%d/", &min, &c, &max);
278 krb5_set_error_string (context, "failed to parse \"%s\"", orig);
279 return HEIM_ERR_LOG_PARSE;
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] == '=')){
293 krb5_set_error_string (context, "malloc: out of memory");
297 int i = open(fn, O_WRONLY | O_CREAT |
298 O_TRUNC | O_APPEND, 0666);
301 krb5_set_error_string (context, "open(%s): %s", fn,
305 file = fdopen(i, "a");
309 krb5_set_error_string (context, "fdopen(%s): %s", fn,
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 && (p[6] == '\0' || p[6] == ':')){
319 char severity[128] = "";
320 char facility[128] = "";
324 if(strsep_copy(&p, ":", severity, sizeof(severity)) != -1)
325 strsep_copy(&p, ":", facility, sizeof(facility));
326 if(*severity == '\0')
327 strlcpy(severity, "ERR", sizeof(severity));
328 if(*facility == '\0')
329 strlcpy(facility, "AUTH", sizeof(facility));
330 ret = open_syslog(context, f, min, max, severity, facility);
332 krb5_set_error_string (context, "unknown log type: %s", p);
333 ret = HEIM_ERR_LOG_PARSE; /* XXX */
340 krb5_openlog(krb5_context context,
342 krb5_log_facility **fac)
347 ret = krb5_initlog(context, program, fac);
351 p = krb5_config_get_strings(context, NULL, "logging", program, NULL);
353 p = krb5_config_get_strings(context, NULL, "logging", "default", NULL);
356 ret = krb5_addlog_dest(context, *fac, *q);
357 krb5_config_free_strings(p);
359 ret = krb5_addlog_dest(context, *fac, "SYSLOG");
364 krb5_closelog(krb5_context context,
365 krb5_log_facility *fac)
368 for(i = 0; i < fac->len; i++)
369 (*fac->val[i].close)(fac->val[i].data);
374 #define __attribute__(X)
377 krb5_vlog_msg(krb5_context context,
378 krb5_log_facility *fac,
383 __attribute__((format (printf, 5, 0)))
387 const char *actual = NULL;
392 for(i = 0; fac && i < fac->len; i++)
393 if(fac->val[i].min <= level &&
394 (fac->val[i].max < 0 || fac->val[i].max >= level)) {
397 krb5_format_time(context, t, buf, sizeof(buf), TRUE);
400 vasprintf(&msg, fmt, ap);
406 (*fac->val[i].log)(buf, actual, fac->val[i].data);
416 krb5_vlog(krb5_context context,
417 krb5_log_facility *fac,
421 __attribute__((format (printf, 4, 0)))
423 return krb5_vlog_msg(context, fac, NULL, level, fmt, ap);
427 krb5_log_msg(krb5_context context,
428 krb5_log_facility *fac,
433 __attribute__((format (printf, 5, 6)))
439 ret = krb5_vlog_msg(context, fac, reply, level, fmt, ap);
446 krb5_log(krb5_context context,
447 krb5_log_facility *fac,
451 __attribute__((format (printf, 4, 5)))
457 ret = krb5_vlog(context, fac, level, fmt, ap);