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