2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "krb5_locl.h"
46 krb5_log_log_func_t log
;
47 krb5_log_close_func_t close
;
51 static struct facility
*
52 log_realloc(krb5_log_facility
*f
)
56 fp
= realloc(f
->val
, f
->len
* sizeof(*f
->val
));
69 #define L(X) { #X, LOG_ ## X }
71 struct s2i syslogvals
[] = {
115 find_value(const char *s
, struct s2i
*table
)
117 while(table
->s
&& strcasecmp(table
->s
, s
))
123 krb5_initlog(krb5_context context
,
125 krb5_log_facility
**fac
)
127 krb5_log_facility
*f
= calloc(1, sizeof(*f
));
130 f
->program
= strdup(program
);
131 if(f
->program
== NULL
){
140 krb5_addlog_func(krb5_context context
,
141 krb5_log_facility
*fac
,
144 krb5_log_log_func_t log
,
145 krb5_log_close_func_t close
,
148 struct facility
*fp
= log_realloc(fac
);
165 log_syslog(const char *time
,
170 struct 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 syslog_data
*sd
= malloc(sizeof(*sd
));
191 i
= find_value(sev
, syslogvals
);
195 i
= find_value(fac
, syslogvals
);
199 roken_openlog(facility
->program
, LOG_PID
| LOG_NDELAY
, i
);
200 return krb5_addlog_func(context
, facility
, min
, max
,
201 log_syslog
, close_syslog
, sd
);
212 log_file(const char *time
,
216 struct file_data
*f
= data
;
217 if(f
->keep_open
== 0)
218 f
->fd
= fopen(f
->filename
, f
->mode
);
221 fprintf(f
->fd
, "%s %s\n", time
, msg
);
222 if(f
->keep_open
== 0)
227 close_file(void *data
)
229 struct file_data
*f
= data
;
230 if(f
->keep_open
&& f
->filename
)
235 static krb5_error_code
236 open_file(krb5_context context
, krb5_log_facility
*fac
, int min
, int max
,
237 char *filename
, char *mode
, FILE *f
, int keep_open
)
239 struct file_data
*fd
= malloc(sizeof(*fd
));
242 fd
->filename
= filename
;
245 fd
->keep_open
= keep_open
;
247 return krb5_addlog_func(context
, fac
, min
, max
, log_file
, close_file
, fd
);
253 krb5_addlog_dest(krb5_context context
, krb5_log_facility
*f
, const char *p
)
255 krb5_error_code ret
= 0;
256 int min
= 0, max
= -1, n
;
258 n
= sscanf(p
, "%d%c%d/", &min
, &c
, &max
);
270 if(p
== NULL
) return HEIM_ERR_LOG_PARSE
;
273 if(strcmp(p
, "STDERR") == 0){
274 ret
= open_file(context
, f
, min
, max
, NULL
, NULL
, stderr
, 1);
275 }else if(strcmp(p
, "CONSOLE") == 0){
276 ret
= open_file(context
, f
, min
, max
, "/dev/console", "w", NULL
, 0);
277 }else if(strncmp(p
, "FILE:", 4) == 0 && (p
[4] == ':' || p
[4] == '=')){
285 int i
= open(fn
, O_WRONLY
| O_CREAT
|
286 O_TRUNC
| O_APPEND
, 0666);
289 file
= fdopen(i
, "a");
296 ret
= open_file(context
, f
, min
, max
, fn
, "a", file
, keep_open
);
297 }else if(strncmp(p
, "DEVICE=", 6) == 0){
298 ret
= open_file(context
, f
, min
, max
, strdup(p
+ 7), "w", NULL
, 0);
299 }else if(strncmp(p
, "SYSLOG", 6) == 0){
302 severity
= strchr(p
, ':');
305 facility
= strchr(severity
, ':');
308 ret
= open_syslog(context
, f
, min
, max
, severity
, facility
);
310 ret
= HEIM_ERR_LOG_PARSE
; /* XXX */
317 krb5_openlog(krb5_context context
,
319 krb5_log_facility
**fac
)
324 ret
= krb5_initlog(context
, program
, fac
);
328 p
= krb5_config_get_strings(context
->cf
, "logging", program
, NULL
);
330 p
= krb5_config_get_strings(context
->cf
, "logging", "default", NULL
);
333 ret
= krb5_addlog_dest(context
, *fac
, *q
);
334 krb5_config_free_strings(p
);
336 ret
= krb5_addlog_dest(context
, *fac
, "SYSLOG");
341 krb5_closelog(krb5_context context
,
342 krb5_log_facility
*fac
)
345 for(i
= 0; i
< fac
->len
; i
++)
346 (*fac
->val
[i
].close
)(&fac
->val
[i
].data
);
351 krb5_vlog_msg(krb5_context context
,
352 krb5_log_facility
*fac
,
363 vasprintf(&msg
, fmt
, ap
);
365 strftime(buf
, sizeof(buf
), "%d-%b-%Y %H:%M:%S", localtime(&t
));
366 for(i
= 0; i
< fac
->len
; i
++)
367 if(fac
->val
[i
].min
<= level
&&
368 (fac
->val
[i
].max
< 0 || fac
->val
[i
].max
>= level
))
369 (*fac
->val
[i
].log
)(buf
, msg
, fac
->val
[i
].data
);
375 krb5_vlog(krb5_context context
,
376 krb5_log_facility
*fac
,
384 ret
= krb5_vlog_msg(context
, fac
, &msg
, level
, fmt
, ap
);
390 krb5_log_msg(krb5_context context
,
391 krb5_log_facility
*fac
,
401 ret
= krb5_vlog_msg(context
, fac
, reply
, level
, fmt
, ap
);
408 krb5_log(krb5_context context
,
409 krb5_log_facility
*fac
,
418 ret
= krb5_vlog(context
, fac
, level
, fmt
, ap
);