2 * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 #include "krb5_locl.h"
42 krb5_log_log_func_t log_func
;
43 krb5_log_close_func_t close_func
;
47 static struct facility
*
48 log_realloc(krb5_log_facility
*f
)
51 fp
= realloc(f
->val
, (f
->len
+ 1) * sizeof(*f
->val
));
65 #define L(X) { #X, LOG_ ## X }
67 static struct s2i syslogvals
[] = {
111 find_value(const char *s
, struct s2i
*table
)
113 while(table
->s
&& strcasecmp(table
->s
, s
))
118 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
119 krb5_initlog(krb5_context context
,
121 krb5_log_facility
**fac
)
123 krb5_log_facility
*f
= calloc(1, sizeof(*f
));
125 return krb5_enomem(context
);
126 f
->program
= strdup(program
);
127 if(f
->program
== NULL
){
129 return krb5_enomem(context
);
135 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
136 krb5_addlog_func(krb5_context context
,
137 krb5_log_facility
*fac
,
140 krb5_log_log_func_t log_func
,
141 krb5_log_close_func_t close_func
,
144 struct facility
*fp
= log_realloc(fac
);
146 return krb5_enomem(context
);
149 fp
->log_func
= log_func
;
150 fp
->close_func
= close_func
;
156 struct _heimdal_syslog_data
{
160 static void KRB5_CALLCONV
161 log_syslog(const char *timestr
,
166 struct _heimdal_syslog_data
*s
= data
;
167 syslog(s
->priority
, "%s", msg
);
170 static void KRB5_CALLCONV
171 close_syslog(void *data
)
177 static krb5_error_code
178 open_syslog(krb5_context context
,
179 krb5_log_facility
*facility
, int min
, int max
,
180 const char *sev
, const char *fac
)
182 struct _heimdal_syslog_data
*sd
= malloc(sizeof(*sd
));
186 return krb5_enomem(context
);
187 i
= find_value(sev
, syslogvals
);
191 i
= find_value(fac
, syslogvals
);
195 roken_openlog(facility
->program
, LOG_PID
| LOG_NDELAY
, i
);
196 return krb5_addlog_func(context
, facility
, min
, max
,
197 log_syslog
, close_syslog
, sd
);
201 const char *filename
;
208 static void KRB5_CALLCONV
209 log_file(const char *timestr
,
213 struct file_data
*f
= data
;
215 size_t len
= strlen(msg
);
216 if(f
->keep_open
== 0)
217 f
->fd
= fopen(f
->filename
, f
->mode
);
220 /* make sure the log doesn't contain special chars */
221 msgclean
= malloc((len
+ 1) * 4);
222 if (msgclean
== NULL
)
224 strvisx(msgclean
, rk_UNCONST(msg
), len
, VIS_OCTAL
);
225 fprintf(f
->fd
, "%s %s\n", timestr
, msgclean
);
228 if(f
->keep_open
== 0) {
234 static void KRB5_CALLCONV
235 close_file(void *data
)
237 struct file_data
*f
= data
;
238 if(f
->keep_open
&& f
->filename
)
240 if (f
->filename
&& f
->freefilename
)
241 free((char *)f
->filename
);
245 static krb5_error_code
246 open_file(krb5_context context
, krb5_log_facility
*fac
, int min
, int max
,
247 const char *filename
, const char *mode
, FILE *f
, int keep_open
,
250 struct file_data
*fd
= malloc(sizeof(*fd
));
252 if (freefilename
&& filename
)
253 free((char *)filename
);
254 return krb5_enomem(context
);
256 fd
->filename
= filename
;
259 fd
->keep_open
= keep_open
;
260 fd
->freefilename
= freefilename
;
262 return krb5_addlog_func(context
, fac
, min
, max
, log_file
, close_file
, fd
);
267 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
268 krb5_addlog_dest(krb5_context context
, krb5_log_facility
*f
, const char *orig
)
270 krb5_error_code ret
= 0;
271 int min
= 0, max
= -1, n
;
273 const char *p
= orig
;
278 n
= sscanf(p
, "%d%c%d/", &min
, &c
, &max
);
291 q
= strrchr(p
, '\\');
298 krb5_set_error_message(context
, HEIM_ERR_LOG_PARSE
,
299 N_("failed to parse \"%s\"", ""), orig
);
300 return HEIM_ERR_LOG_PARSE
;
304 if(strcmp(p
, "STDERR") == 0){
305 ret
= open_file(context
, f
, min
, max
, NULL
, NULL
, stderr
, 1, 0);
306 }else if(strcmp(p
, "CONSOLE") == 0){
307 ret
= open_file(context
, f
, min
, max
, "/dev/console", "w", NULL
, 0, 0);
308 }else if(strncmp(p
, "FILE", 4) == 0 && (p
[4] == ':' || p
[4] == '=')){
314 return krb5_enomem(context
);
316 int i
= open(fn
, O_WRONLY
| O_CREAT
|
317 O_TRUNC
| O_APPEND
, 0666);
320 krb5_set_error_message(context
, ret
,
321 N_("open(%s) logfile: %s", ""), fn
,
327 file
= fdopen(i
, "a");
331 krb5_set_error_message(context
, ret
,
332 N_("fdopen(%s) logfile: %s", ""),
339 ret
= open_file(context
, f
, min
, max
, fn
, "a", file
, keep_open
, 1);
340 }else if(strncmp(p
, "DEVICE", 6) == 0 && (p
[6] == ':' || p
[6] == '=')){
341 ret
= open_file(context
, f
, min
, max
, strdup(p
+ 7), "w", NULL
, 0, 1);
342 }else if(strncmp(p
, "SYSLOG", 6) == 0 && (p
[6] == '\0' || p
[6] == ':')){
343 char severity
[128] = "";
344 char facility
[128] = "";
348 if(strsep_copy(&p
, ":", severity
, sizeof(severity
)) != -1)
349 strsep_copy(&p
, ":", facility
, sizeof(facility
));
350 if(*severity
== '\0')
351 strlcpy(severity
, "ERR", sizeof(severity
));
352 if(*facility
== '\0')
353 strlcpy(facility
, "AUTH", sizeof(facility
));
354 ret
= open_syslog(context
, f
, min
, max
, severity
, facility
);
356 ret
= HEIM_ERR_LOG_PARSE
; /* XXX */
357 krb5_set_error_message (context
, ret
,
358 N_("unknown log type: %s", ""), p
);
364 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
365 krb5_openlog(krb5_context context
,
367 krb5_log_facility
**fac
)
372 ret
= krb5_initlog(context
, program
, fac
);
376 p
= krb5_config_get_strings(context
, NULL
, "logging", program
, NULL
);
378 p
= krb5_config_get_strings(context
, NULL
, "logging", "default", NULL
);
380 for(q
= p
; *q
&& ret
== 0; q
++)
381 ret
= krb5_addlog_dest(context
, *fac
, *q
);
382 krb5_config_free_strings(p
);
384 ret
= krb5_addlog_dest(context
, *fac
, "SYSLOG");
388 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
389 krb5_closelog(krb5_context context
,
390 krb5_log_facility
*fac
)
393 for(i
= 0; i
< fac
->len
; i
++)
394 (*fac
->val
[i
].close_func
)(fac
->val
[i
].data
);
405 #define __attribute__(X)
407 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
408 krb5_vlog_msg(krb5_context context
,
409 krb5_log_facility
*fac
,
414 __attribute__((format (printf
, 5, 0)))
418 const char *actual
= NULL
;
423 for(i
= 0; fac
&& i
< fac
->len
; i
++)
424 if(fac
->val
[i
].min
<= level
&&
425 (fac
->val
[i
].max
< 0 || fac
->val
[i
].max
>= level
)) {
428 krb5_format_time(context
, t
, buf
, sizeof(buf
), TRUE
);
431 int ret
= vasprintf(&msg
, fmt
, ap
);
432 if(ret
< 0 || msg
== NULL
)
437 (*fac
->val
[i
].log_func
)(buf
, actual
, fac
->val
[i
].data
);
446 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
447 krb5_vlog(krb5_context context
,
448 krb5_log_facility
*fac
,
452 __attribute__((format (printf
, 4, 0)))
454 return krb5_vlog_msg(context
, fac
, NULL
, level
, fmt
, ap
);
457 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
458 krb5_log_msg(krb5_context context
,
459 krb5_log_facility
*fac
,
464 __attribute__((format (printf
, 5, 6)))
470 ret
= krb5_vlog_msg(context
, fac
, reply
, level
, fmt
, ap
);
476 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
477 krb5_log(krb5_context context
,
478 krb5_log_facility
*fac
,
482 __attribute__((format (printf
, 4, 5)))
488 ret
= krb5_vlog(context
, fac
, level
, fmt
, ap
);
493 void KRB5_LIB_FUNCTION
494 _krb5_debug(krb5_context context
,
498 __attribute__((format (printf
, 3, 4)))
502 if (context
== NULL
|| context
->debug_dest
== NULL
)
506 krb5_vlog(context
, context
->debug_dest
, level
, fmt
, ap
);
510 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
511 _krb5_have_debug(krb5_context context
, int level
)
513 if (context
== NULL
|| context
->debug_dest
== NULL
)