changed `struct fd_set' to `fd_set'
[heimdal.git] / lib / krb5 / log.c
blob1fbeb94ecfd816be2c685fdad9165b11a195ad15
1 /*
2 * Copyright (c) 1997 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
36 * SUCH DAMAGE.
39 #include "krb5_locl.h"
41 RCSID("$Id$");
43 struct facility {
44 int min;
45 int max;
46 krb5_log_log_func_t log;
47 krb5_log_close_func_t close;
48 void *data;
51 static struct facility*
52 log_realloc(krb5_log_facility *f)
54 struct facility *fp;
55 f->len++;
56 fp = realloc(f->val, f->len * sizeof(*f->val));
57 if(fp == NULL)
58 return NULL;
59 f->val = fp;
60 fp += f->len - 1;
61 return fp;
64 struct s2i{
65 char *s;
66 int val;
69 #define L(X) { #X, LOG_ ## X }
71 struct s2i syslogvals[] = {
72 L(EMERG),
73 L(ALERT),
74 L(CRIT),
75 L(ERR),
76 L(WARNING),
77 L(NOTICE),
78 L(INFO),
79 L(DEBUG),
81 L(AUTH),
82 #ifdef LOG_AUTHPRIV
83 L(AUTHPRIV),
84 #endif
85 #ifdef LOG_CRON
86 L(CRON),
87 #endif
88 L(DAEMON),
89 #ifdef LOG_FTP
90 L(FTP),
91 #endif
92 L(KERN),
93 L(LPR),
94 L(MAIL),
95 #ifdef LOG_NEWS
96 L(NEWS),
97 #endif
98 L(SYSLOG),
99 L(USER),
100 #ifdef LOG_UUCP
101 L(UUCP),
102 #endif
103 L(LOCAL0),
104 L(LOCAL1),
105 L(LOCAL2),
106 L(LOCAL3),
107 L(LOCAL4),
108 L(LOCAL5),
109 L(LOCAL6),
110 L(LOCAL7),
111 { NULL, -1 }
114 static int
115 find_value(const char *s, struct s2i *table)
117 while(table->s && strcasecmp(table->s, s))
118 table++;
119 return table->val;
122 krb5_error_code
123 krb5_initlog(krb5_context context,
124 const char *program,
125 krb5_log_facility **fac)
127 krb5_log_facility *f = calloc(1, sizeof(*f));
128 if(f == NULL)
129 return ENOMEM;
130 f->program = strdup(program);
131 if(f->program == NULL){
132 free(f);
133 return ENOMEM;
135 *fac = f;
136 return 0;
139 krb5_error_code
140 krb5_addlog_func(krb5_context context,
141 krb5_log_facility *fac,
142 int min,
143 int max,
144 krb5_log_log_func_t log,
145 krb5_log_close_func_t close,
146 void *data)
148 struct facility *fp = log_realloc(fac);
149 if(fp == NULL)
150 return ENOMEM;
151 fp->min = min;
152 fp->max = max;
153 fp->log = log;
154 fp->close = close;
155 fp->data = data;
156 return 0;
160 struct syslog_data{
161 int priority;
164 static void
165 log_syslog(const char *time,
166 const char *msg,
167 void *data)
170 struct syslog_data *s = data;
171 syslog(s->priority, "%s", msg);
174 static void
175 close_syslog(void *data)
177 free(data);
178 closelog();
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));
187 int i;
189 if(sd == NULL)
190 return ENOMEM;
191 i = find_value(sev, syslogvals);
192 if(i == -1)
193 i = LOG_ERR;
194 sd->priority = i;
195 i = find_value(fac, syslogvals);
196 if(i == -1)
197 i = LOG_AUTH;
198 sd->priority |= i;
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);
204 struct file_data{
205 char *filename;
206 char *mode;
207 FILE *fd;
208 int keep_open;
211 static void
212 log_file(const char *time,
213 const char *msg,
214 void *data)
216 struct file_data *f = data;
217 if(f->keep_open == 0)
218 f->fd = fopen(f->filename, f->mode);
219 if(f->fd == NULL)
220 return;
221 fprintf(f->fd, "%s %s\n", time, msg);
222 if(f->keep_open == 0)
223 fclose(f->fd);
226 static void
227 close_file(void *data)
229 struct file_data *f = data;
230 if(f->keep_open && f->filename)
231 fclose(f->fd);
232 free(data);
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));
240 if(fd == NULL)
241 return ENOMEM;
242 fd->filename = filename;
243 fd->mode = mode;
244 fd->fd = f;
245 fd->keep_open = keep_open;
247 return krb5_addlog_func(context, fac, min, max, log_file, close_file, fd);
252 krb5_error_code
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;
257 char c;
258 n = sscanf(p, "%d%c%d/", &min, &c, &max);
259 if(n == 2){
260 if(c == '/')
261 if(min < 0){
262 max = -min;
263 min = 0;
264 }else{
265 max = min;
268 if(n){
269 p = strchr(p, '/');
270 if(p == NULL) return HEIM_ERR_LOG_PARSE;
271 p++;
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] == '=')){
278 char *fn;
279 FILE *file = NULL;
280 int keep_open = 0;
281 fn = strdup(p + 5);
282 if(fn == NULL)
283 return ENOMEM;
284 if(p[4] == '='){
285 int i = open(fn, O_WRONLY | O_CREAT |
286 O_TRUNC | O_APPEND, 0666);
287 if(i < 0)
288 return errno;
289 file = fdopen(i, "a");
290 if(file == NULL){
291 close(i);
292 return errno;
294 keep_open = 1;
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){
300 char *severity;
301 char *facility;
302 severity = strchr(p, ':');
303 if(severity == NULL)
304 severity = "ERR";
305 facility = strchr(severity, ':');
306 if(facility == NULL)
307 facility = "AUTH";
308 ret = open_syslog(context, f, min, max, severity, facility);
309 }else{
310 ret = HEIM_ERR_LOG_PARSE; /* XXX */
312 return ret;
316 krb5_error_code
317 krb5_openlog(krb5_context context,
318 const char *program,
319 krb5_log_facility **fac)
321 krb5_error_code ret;
322 char **p, **q;
324 ret = krb5_initlog(context, program, fac);
325 if(ret)
326 return ret;
328 p = krb5_config_get_strings(context->cf, "logging", program, NULL);
329 if(p == NULL)
330 p = krb5_config_get_strings(context->cf, "logging", "default", NULL);
331 if(p){
332 for(q = p; *q; q++)
333 ret = krb5_addlog_dest(context, *fac, *q);
334 krb5_config_free_strings(p);
335 }else
336 ret = krb5_addlog_dest(context, *fac, "SYSLOG");
337 return 0;
340 krb5_error_code
341 krb5_closelog(krb5_context context,
342 krb5_log_facility *fac)
344 int i;
345 for(i = 0; i < fac->len; i++)
346 (*fac->val[i].close)(&fac->val[i].data);
347 return 0;
350 krb5_error_code
351 krb5_vlog_msg(krb5_context context,
352 krb5_log_facility *fac,
353 char **reply,
354 int level,
355 const char *fmt,
356 va_list ap)
358 char *msg;
359 char buf[64];
360 time_t t;
361 int i;
363 vasprintf(&msg, fmt, ap);
364 t = time(NULL);
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);
370 *reply = msg;
371 return 0;
374 krb5_error_code
375 krb5_vlog(krb5_context context,
376 krb5_log_facility *fac,
377 int level,
378 const char *fmt,
379 va_list ap)
381 char *msg;
382 krb5_error_code ret;
384 ret = krb5_vlog_msg(context, fac, &msg, level, fmt, ap);
385 free(msg);
386 return ret;
389 krb5_error_code
390 krb5_log_msg(krb5_context context,
391 krb5_log_facility *fac,
392 int level,
393 char **reply,
394 const char *fmt,
395 ...)
397 va_list ap;
398 krb5_error_code ret;
400 va_start(ap, fmt);
401 ret = krb5_vlog_msg(context, fac, reply, level, fmt, ap);
402 va_end(ap);
403 return ret;
407 krb5_error_code
408 krb5_log(krb5_context context,
409 krb5_log_facility *fac,
410 int level,
411 const char *fmt,
412 ...)
414 va_list ap;
415 krb5_error_code ret;
417 va_start(ap, fmt);
418 ret = krb5_vlog(context, fac, level, fmt, ap);
419 va_end(ap);
420 return ret;