1 /* $NetBSD: log.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $ */
4 * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "krb5_locl.h"
44 krb5_log_log_func_t log_func
;
45 krb5_log_close_func_t close_func
;
49 static struct facility
*
50 log_realloc(krb5_log_facility
*f
)
53 fp
= realloc(f
->val
, (f
->len
+ 1) * sizeof(*f
->val
));
67 #define L(X) { #X, LOG_ ## X }
69 static struct s2i syslogvals
[] = {
113 find_value(const char *s
, struct s2i
*table
)
115 while(table
->s
&& strcasecmp(table
->s
, s
))
120 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
121 krb5_initlog(krb5_context context
,
123 krb5_log_facility
**fac
)
125 krb5_log_facility
*f
= calloc(1, sizeof(*f
));
127 krb5_set_error_message(context
, ENOMEM
,
128 N_("malloc: out of memory", ""));
131 f
->program
= strdup(program
);
132 if(f
->program
== NULL
){
134 krb5_set_error_message(context
, ENOMEM
,
135 N_("malloc: out of memory", ""));
142 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
143 krb5_addlog_func(krb5_context context
,
144 krb5_log_facility
*fac
,
147 krb5_log_log_func_t log_func
,
148 krb5_log_close_func_t close_func
,
151 struct facility
*fp
= log_realloc(fac
);
153 krb5_set_error_message(context
, ENOMEM
,
154 N_("malloc: out of memory", ""));
159 fp
->log_func
= log_func
;
160 fp
->close_func
= close_func
;
166 struct _heimdal_syslog_data
{
170 static void KRB5_CALLCONV
171 log_syslog(const char *timestr
,
176 struct _heimdal_syslog_data
*s
= data
;
177 syslog(s
->priority
, "%s", msg
);
180 static void KRB5_CALLCONV
181 close_syslog(void *data
)
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
)
192 struct _heimdal_syslog_data
*sd
= malloc(sizeof(*sd
));
196 krb5_set_error_message(context
, ENOMEM
,
197 N_("malloc: out of memory", ""));
200 i
= find_value(sev
, syslogvals
);
204 i
= find_value(fac
, syslogvals
);
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
);
214 const char *filename
;
220 static void KRB5_CALLCONV
221 log_file(const char *timestr
,
225 struct file_data
*f
= data
;
227 size_t len
= strlen(msg
);
228 if(f
->keep_open
== 0)
229 f
->fd
= fopen(f
->filename
, f
->mode
);
232 /* make sure the log doesn't contain special chars */
233 msgclean
= malloc((len
+ 1) * 4);
234 if (msgclean
== NULL
)
236 strvisx(msgclean
, rk_UNCONST(msg
), len
, VIS_OCTAL
);
237 fprintf(f
->fd
, "%s %s\n", timestr
, msgclean
);
240 if(f
->keep_open
== 0) {
246 static void KRB5_CALLCONV
247 close_file(void *data
)
249 struct file_data
*f
= data
;
250 if(f
->keep_open
&& f
->filename
)
255 static krb5_error_code
256 open_file(krb5_context context
, krb5_log_facility
*fac
, int min
, int max
,
257 const char *filename
, const char *mode
, FILE *f
, int keep_open
)
259 struct file_data
*fd
= malloc(sizeof(*fd
));
261 krb5_set_error_message(context
, ENOMEM
,
262 N_("malloc: out of memory", ""));
265 fd
->filename
= filename
;
268 fd
->keep_open
= keep_open
;
270 return krb5_addlog_func(context
, fac
, min
, max
, log_file
, close_file
, fd
);
275 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
276 krb5_addlog_dest(krb5_context context
, krb5_log_facility
*f
, const char *orig
)
278 krb5_error_code ret
= 0;
279 int min
= 0, max
= -1, n
;
281 const char *p
= orig
;
283 n
= sscanf(p
, "%d%c%d/", &min
, &c
, &max
);
297 krb5_set_error_message(context
, HEIM_ERR_LOG_PARSE
,
298 N_("failed to parse \"%s\"", ""), orig
);
299 return HEIM_ERR_LOG_PARSE
;
303 if(strcmp(p
, "STDERR") == 0){
304 ret
= open_file(context
, f
, min
, max
, NULL
, NULL
, stderr
, 1);
305 }else if(strcmp(p
, "CONSOLE") == 0){
306 ret
= open_file(context
, f
, min
, max
, "/dev/console", "w", NULL
, 0);
307 }else if(strncmp(p
, "FILE", 4) == 0 && (p
[4] == ':' || p
[4] == '=')){
313 krb5_set_error_message(context
, ENOMEM
,
314 N_("malloc: out of memory", ""));
318 int i
= open(fn
, O_WRONLY
| O_CREAT
|
319 O_TRUNC
| O_APPEND
, 0666);
322 krb5_set_error_message(context
, ret
,
323 N_("open(%s) logile: %s", ""), fn
,
329 file
= fdopen(i
, "a");
333 krb5_set_error_message(context
, ret
,
334 N_("fdopen(%s) logfile: %s", ""),
341 ret
= open_file(context
, f
, min
, max
, fn
, "a", file
, keep_open
);
342 }else if(strncmp(p
, "DEVICE", 6) == 0 && (p
[6] == ':' || p
[6] == '=')){
343 ret
= open_file(context
, f
, min
, max
, strdup(p
+ 7), "w", NULL
, 0);
344 }else if(strncmp(p
, "SYSLOG", 6) == 0 && (p
[6] == '\0' || p
[6] == ':')){
345 char severity
[128] = "";
346 char facility
[128] = "";
350 if(strsep_copy(&p
, ":", severity
, sizeof(severity
)) != -1)
351 strsep_copy(&p
, ":", facility
, sizeof(facility
));
352 if(*severity
== '\0')
353 strlcpy(severity
, "ERR", sizeof(severity
));
354 if(*facility
== '\0')
355 strlcpy(facility
, "AUTH", sizeof(facility
));
356 ret
= open_syslog(context
, f
, min
, max
, severity
, facility
);
358 ret
= HEIM_ERR_LOG_PARSE
; /* XXX */
359 krb5_set_error_message (context
, ret
,
360 N_("unknown log type: %s", ""), p
);
366 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
367 krb5_openlog(krb5_context context
,
369 krb5_log_facility
**fac
)
374 ret
= krb5_initlog(context
, program
, fac
);
378 p
= krb5_config_get_strings(context
, NULL
, "logging", program
, NULL
);
380 p
= krb5_config_get_strings(context
, NULL
, "logging", "default", NULL
);
382 for(q
= p
; *q
&& ret
== 0; q
++)
383 ret
= krb5_addlog_dest(context
, *fac
, *q
);
384 krb5_config_free_strings(p
);
386 ret
= krb5_addlog_dest(context
, *fac
, "SYSLOG");
390 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
391 krb5_closelog(krb5_context context
,
392 krb5_log_facility
*fac
)
395 for(i
= 0; i
< fac
->len
; i
++)
396 (*fac
->val
[i
].close_func
)(fac
->val
[i
].data
);
407 #define __attribute__(X)
409 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
410 krb5_vlog_msg(krb5_context context
,
411 krb5_log_facility
*fac
,
416 __attribute__((format (printf
, 5, 0)))
420 const char *actual
= NULL
;
425 for(i
= 0; fac
&& i
< fac
->len
; i
++)
426 if(fac
->val
[i
].min
<= level
&&
427 (fac
->val
[i
].max
< 0 || fac
->val
[i
].max
>= level
)) {
430 krb5_format_time(context
, t
, buf
, sizeof(buf
), TRUE
);
433 int ret
= vasprintf(&msg
, fmt
, ap
);
434 if(ret
< 0 || msg
== NULL
)
439 (*fac
->val
[i
].log_func
)(buf
, actual
, fac
->val
[i
].data
);
448 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
449 krb5_vlog(krb5_context context
,
450 krb5_log_facility
*fac
,
454 __attribute__((format (printf
, 4, 0)))
456 return krb5_vlog_msg(context
, fac
, NULL
, level
, fmt
, ap
);
459 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
460 krb5_log_msg(krb5_context context
,
461 krb5_log_facility
*fac
,
466 __attribute__((format (printf
, 5, 6)))
472 ret
= krb5_vlog_msg(context
, fac
, reply
, level
, fmt
, ap
);
478 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
479 krb5_log(krb5_context context
,
480 krb5_log_facility
*fac
,
484 __attribute__((format (printf
, 4, 5)))
490 ret
= krb5_vlog(context
, fac
, level
, fmt
, ap
);
495 void KRB5_LIB_FUNCTION
496 _krb5_debug(krb5_context context
,
500 __attribute__((format (printf
, 3, 4)))
504 if (context
== NULL
|| context
->debug_dest
== NULL
)
508 krb5_vlog(context
, context
->debug_dest
, level
, fmt
, ap
);
512 krb5_boolean KRB5_LIB_FUNCTION
513 _krb5_have_debug(krb5_context context
, int level
)
515 if (context
== NULL
|| context
->debug_dest
== NULL
)