s4:heimdal: import lorikeet-heimdal-200906080040 (commit 904d0124b46eed7a8ad6e5b73e89...
[amitay/samba.git] / source4 / heimdal / lib / krb5 / log.c
1 /*
2  * Copyright (c) 1997-2006 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #include "krb5_locl.h"
35
36 struct facility {
37     int min;
38     int max;
39     krb5_log_log_func_t log_func;
40     krb5_log_close_func_t close_func;
41     void *data;
42 };
43
44 static struct facility*
45 log_realloc(krb5_log_facility *f)
46 {
47     struct facility *fp;
48     fp = realloc(f->val, (f->len + 1) * sizeof(*f->val));
49     if(fp == NULL)
50         return NULL;
51     f->len++;
52     f->val = fp;
53     fp += f->len - 1;
54     return fp;
55 }
56
57 struct s2i {
58     const char *s;
59     int val;
60 };
61
62 #define L(X) { #X, LOG_ ## X }
63
64 static struct s2i syslogvals[] = {
65     L(EMERG),
66     L(ALERT),
67     L(CRIT),
68     L(ERR),
69     L(WARNING),
70     L(NOTICE),
71     L(INFO),
72     L(DEBUG),
73
74     L(AUTH),
75 #ifdef LOG_AUTHPRIV
76     L(AUTHPRIV),
77 #endif
78 #ifdef LOG_CRON
79     L(CRON),
80 #endif
81     L(DAEMON),
82 #ifdef LOG_FTP
83     L(FTP),
84 #endif
85     L(KERN),
86     L(LPR),
87     L(MAIL),
88 #ifdef LOG_NEWS
89     L(NEWS),
90 #endif
91     L(SYSLOG),
92     L(USER),
93 #ifdef LOG_UUCP
94     L(UUCP),
95 #endif
96     L(LOCAL0),
97     L(LOCAL1),
98     L(LOCAL2),
99     L(LOCAL3),
100     L(LOCAL4),
101     L(LOCAL5),
102     L(LOCAL6),
103     L(LOCAL7),
104     { NULL, -1 }
105 };
106
107 static int
108 find_value(const char *s, struct s2i *table)
109 {
110     while(table->s && strcasecmp(table->s, s))
111         table++;
112     return table->val;
113 }
114
115 krb5_error_code KRB5_LIB_FUNCTION
116 krb5_initlog(krb5_context context,
117              const char *program,
118              krb5_log_facility **fac)
119 {
120     krb5_log_facility *f = calloc(1, sizeof(*f));
121     if(f == NULL) {
122         krb5_set_error_message(context, ENOMEM,
123                                N_("malloc: out of memory", ""));
124         return ENOMEM;
125     }
126     f->program = strdup(program);
127     if(f->program == NULL){
128         free(f);
129         krb5_set_error_message(context, ENOMEM,
130                                N_("malloc: out of memory", ""));
131         return ENOMEM;
132     }
133     *fac = f;
134     return 0;
135 }
136
137 krb5_error_code KRB5_LIB_FUNCTION
138 krb5_addlog_func(krb5_context context,
139                  krb5_log_facility *fac,
140                  int min,
141                  int max,
142                  krb5_log_log_func_t log_func,
143                  krb5_log_close_func_t close_func,
144                  void *data)
145 {
146     struct facility *fp = log_realloc(fac);
147     if(fp == NULL) {
148         krb5_set_error_message(context, ENOMEM,
149                                N_("malloc: out of memory", ""));
150         return ENOMEM;
151     }
152     fp->min = min;
153     fp->max = max;
154     fp->log_func = log_func;
155     fp->close_func = close_func;
156     fp->data = data;
157     return 0;
158 }
159
160
161 struct _heimdal_syslog_data{
162     int priority;
163 };
164
165 static void
166 log_syslog(const char *timestr,
167            const char *msg,
168            void *data)
169
170 {
171     struct _heimdal_syslog_data *s = data;
172     syslog(s->priority, "%s", msg);
173 }
174
175 static void
176 close_syslog(void *data)
177 {
178     free(data);
179     closelog();
180 }
181
182 static krb5_error_code
183 open_syslog(krb5_context context,
184             krb5_log_facility *facility, int min, int max,
185             const char *sev, const char *fac)
186 {
187     struct _heimdal_syslog_data *sd = malloc(sizeof(*sd));
188     int i;
189
190     if(sd == NULL) {
191         krb5_set_error_message(context, ENOMEM,
192                                N_("malloc: out of memory", ""));
193         return ENOMEM;
194     }
195     i = find_value(sev, syslogvals);
196     if(i == -1)
197         i = LOG_ERR;
198     sd->priority = i;
199     i = find_value(fac, syslogvals);
200     if(i == -1)
201         i = LOG_AUTH;
202     sd->priority |= i;
203     roken_openlog(facility->program, LOG_PID | LOG_NDELAY, i);
204     return krb5_addlog_func(context, facility, min, max,
205                             log_syslog, close_syslog, sd);
206 }
207
208 struct file_data{
209     const char *filename;
210     const char *mode;
211     FILE *fd;
212     int keep_open;
213 };
214
215 static void
216 log_file(const char *timestr,
217          const char *msg,
218          void *data)
219 {
220     struct file_data *f = data;
221     if(f->keep_open == 0)
222         f->fd = fopen(f->filename, f->mode);
223     if(f->fd == NULL)
224         return;
225     fprintf(f->fd, "%s %s\n", timestr, msg);
226     if(f->keep_open == 0) {
227         fclose(f->fd);
228         f->fd = NULL;
229     }
230 }
231
232 static void
233 close_file(void *data)
234 {
235     struct file_data *f = data;
236     if(f->keep_open && f->filename)
237         fclose(f->fd);
238     free(data);
239 }
240
241 static krb5_error_code
242 open_file(krb5_context context, krb5_log_facility *fac, int min, int max,
243           const char *filename, const char *mode, FILE *f, int keep_open)
244 {
245     struct file_data *fd = malloc(sizeof(*fd));
246     if(fd == NULL) {
247         krb5_set_error_message(context, ENOMEM,
248                                N_("malloc: out of memory", ""));
249         return ENOMEM;
250     }
251     fd->filename = filename;
252     fd->mode = mode;
253     fd->fd = f;
254     fd->keep_open = keep_open;
255
256     return krb5_addlog_func(context, fac, min, max, log_file, close_file, fd);
257 }
258
259
260
261 krb5_error_code KRB5_LIB_FUNCTION
262 krb5_addlog_dest(krb5_context context, krb5_log_facility *f, const char *orig)
263 {
264     krb5_error_code ret = 0;
265     int min = 0, max = -1, n;
266     char c;
267     const char *p = orig;
268
269     n = sscanf(p, "%d%c%d/", &min, &c, &max);
270     if(n == 2){
271         if(c == '/') {
272             if(min < 0){
273                 max = -min;
274                 min = 0;
275             }else{
276                 max = min;
277             }
278         }
279     }
280     if(n){
281         p = strchr(p, '/');
282         if(p == NULL) {
283             krb5_set_error_message(context, HEIM_ERR_LOG_PARSE,
284                                    N_("failed to parse \"%s\"", ""), orig);
285             return HEIM_ERR_LOG_PARSE;
286         }
287         p++;
288     }
289     if(strcmp(p, "STDERR") == 0){
290         ret = open_file(context, f, min, max, NULL, NULL, stderr, 1);
291     }else if(strcmp(p, "CONSOLE") == 0){
292         ret = open_file(context, f, min, max, "/dev/console", "w", NULL, 0);
293     }else if(strncmp(p, "FILE", 4) == 0 && (p[4] == ':' || p[4] == '=')){
294         char *fn;
295         FILE *file = NULL;
296         int keep_open = 0;
297         fn = strdup(p + 5);
298         if(fn == NULL) {
299             krb5_set_error_message(context, ENOMEM,
300                                    N_("malloc: out of memory", ""));
301             return ENOMEM;
302         }
303         if(p[4] == '='){
304             int i = open(fn, O_WRONLY | O_CREAT |
305                          O_TRUNC | O_APPEND, 0666);
306             if(i < 0) {
307                 ret = errno;
308                 krb5_set_error_message(context, ret,
309                                        N_("open(%s) logile: %s", ""), fn,
310                                        strerror(ret));
311                 free(fn);
312                 return ret;
313             }
314             rk_cloexec(i);
315             file = fdopen(i, "a");
316             if(file == NULL){
317                 ret = errno;
318                 close(i);
319                 krb5_set_error_message(context, ret,
320                                        N_("fdopen(%s) logfile: %s", ""),
321                                        fn, strerror(ret));
322                 free(fn);
323                 return ret;
324             }
325             keep_open = 1;
326         }
327         ret = open_file(context, f, min, max, fn, "a", file, keep_open);
328     }else if(strncmp(p, "DEVICE", 6) == 0 && (p[6] == ':' || p[6] == '=')){
329         ret = open_file(context, f, min, max, strdup(p + 7), "w", NULL, 0);
330     }else if(strncmp(p, "SYSLOG", 6) == 0 && (p[6] == '\0' || p[6] == ':')){
331         char severity[128] = "";
332         char facility[128] = "";
333         p += 6;
334         if(*p != '\0')
335             p++;
336         if(strsep_copy(&p, ":", severity, sizeof(severity)) != -1)
337             strsep_copy(&p, ":", facility, sizeof(facility));
338         if(*severity == '\0')
339             strlcpy(severity, "ERR", sizeof(severity));
340         if(*facility == '\0')
341             strlcpy(facility, "AUTH", sizeof(facility));
342         ret = open_syslog(context, f, min, max, severity, facility);
343     }else{
344         ret = HEIM_ERR_LOG_PARSE; /* XXX */
345         krb5_set_error_message (context, ret,
346                                 N_("unknown log type: %s", ""), p);
347     }
348     return ret;
349 }
350
351
352 krb5_error_code KRB5_LIB_FUNCTION
353 krb5_openlog(krb5_context context,
354              const char *program,
355              krb5_log_facility **fac)
356 {
357     krb5_error_code ret;
358     char **p, **q;
359
360     ret = krb5_initlog(context, program, fac);
361     if(ret)
362         return ret;
363
364     p = krb5_config_get_strings(context, NULL, "logging", program, NULL);
365     if(p == NULL)
366         p = krb5_config_get_strings(context, NULL, "logging", "default", NULL);
367     if(p){
368         for(q = p; *q && ret == 0; q++)
369             ret = krb5_addlog_dest(context, *fac, *q);
370         krb5_config_free_strings(p);
371     }else
372         ret = krb5_addlog_dest(context, *fac, "SYSLOG");
373     return ret;
374 }
375
376 krb5_error_code KRB5_LIB_FUNCTION
377 krb5_closelog(krb5_context context,
378               krb5_log_facility *fac)
379 {
380     int i;
381     for(i = 0; i < fac->len; i++)
382         (*fac->val[i].close_func)(fac->val[i].data);
383     free(fac->val);
384     free(fac->program);
385     fac->val = NULL;
386     fac->len = 0;
387     fac->program = NULL;
388     free(fac);
389     return 0;
390 }
391
392 #undef __attribute__
393 #define __attribute__(X)
394
395 krb5_error_code KRB5_LIB_FUNCTION
396 krb5_vlog_msg(krb5_context context,
397               krb5_log_facility *fac,
398               char **reply,
399               int level,
400               const char *fmt,
401               va_list ap)
402      __attribute__((format (printf, 5, 0)))
403 {
404
405     char *msg = NULL;
406     const char *actual = NULL;
407     char buf[64];
408     time_t t = 0;
409     int i;
410
411     for(i = 0; fac && i < fac->len; i++)
412         if(fac->val[i].min <= level &&
413            (fac->val[i].max < 0 || fac->val[i].max >= level)) {
414             if(t == 0) {
415                 t = time(NULL);
416                 krb5_format_time(context, t, buf, sizeof(buf), TRUE);
417             }
418             if(actual == NULL) {
419                 vasprintf(&msg, fmt, ap);
420                 if(msg == NULL)
421                     actual = fmt;
422                 else
423                     actual = msg;
424             }
425             (*fac->val[i].log_func)(buf, actual, fac->val[i].data);
426         }
427     if(reply == NULL)
428         free(msg);
429     else
430         *reply = msg;
431     return 0;
432 }
433
434 krb5_error_code KRB5_LIB_FUNCTION
435 krb5_vlog(krb5_context context,
436           krb5_log_facility *fac,
437           int level,
438           const char *fmt,
439           va_list ap)
440      __attribute__((format (printf, 4, 0)))
441 {
442     return krb5_vlog_msg(context, fac, NULL, level, fmt, ap);
443 }
444
445 krb5_error_code KRB5_LIB_FUNCTION
446 krb5_log_msg(krb5_context context,
447              krb5_log_facility *fac,
448              int level,
449              char **reply,
450              const char *fmt,
451              ...)
452      __attribute__((format (printf, 5, 6)))
453 {
454     va_list ap;
455     krb5_error_code ret;
456
457     va_start(ap, fmt);
458     ret = krb5_vlog_msg(context, fac, reply, level, fmt, ap);
459     va_end(ap);
460     return ret;
461 }
462
463
464 krb5_error_code KRB5_LIB_FUNCTION
465 krb5_log(krb5_context context,
466          krb5_log_facility *fac,
467          int level,
468          const char *fmt,
469          ...)
470      __attribute__((format (printf, 4, 5)))
471 {
472     va_list ap;
473     krb5_error_code ret;
474
475     va_start(ap, fmt);
476     ret = krb5_vlog(context, fac, level, fmt, ap);
477     va_end(ap);
478     return ret;
479 }
480