Add OpenSSL linking exemption to GPL license.
[libradsec.git] / debug.c
1 /* Copyright (c) 2006-2009, Stig Venaas, UNINETT AS.
2  * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3  * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
5
6 #ifndef SYS_SOLARIS9
7 #include <stdint.h>
8 #endif
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <time.h>
15 #include <sys/time.h>
16 #include <syslog.h>
17 #include <errno.h>
18 #include <assert.h>
19 #include "debug.h"
20 #include "util.h"
21
22 static char *debug_ident = NULL;
23 static uint8_t debug_level = DBG_INFO;
24 static char *debug_filepath = NULL;
25 static FILE *debug_file = NULL;
26 static int debug_syslogfacility = 0;
27 #if defined(WANT_FTICKS)
28 static int fticks_syslogfacility = 0;
29 #endif
30 static uint8_t debug_timestamp = 0;
31
32 void debug_init(char *ident) {
33     debug_file = stderr;
34     setvbuf(debug_file, NULL, _IONBF, 0);
35     debug_ident = ident;
36 }
37
38 void debug_set_level(uint8_t level) {
39     switch (level) {
40     case 1:
41         debug_level = DBG_ERR;
42         return;
43     case 2:
44         debug_level = DBG_WARN;
45         return;
46     case 3:
47         debug_level = DBG_NOTICE;
48         return;
49     case 4:
50         debug_level = DBG_INFO;
51         return;
52     case 5:
53         debug_level = DBG_DBG;
54         return;
55     }
56 }
57
58 void debug_timestamp_on() {
59     debug_timestamp = 1;
60 }
61
62 uint8_t debug_get_level() {
63     return debug_level;
64 }
65
66 int debug_set_destination(char *dest, int log_type) {
67     static const char *facstrings[] = {
68         "LOG_DAEMON", "LOG_MAIL", "LOG_USER", "LOG_LOCAL0",
69         "LOG_LOCAL1", "LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4",
70         "LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", NULL };
71     static const int facvals[] = {
72         LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
73         LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
74         LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
75     extern int errno;
76     int i;
77
78     if (!strncasecmp(dest, "file:///", 8)) {
79         if (log_type != LOG_TYPE_FTICKS) {
80             debug_filepath = stringcopy(dest + 7, 0);
81             debug_file = fopen(debug_filepath, "a");
82             if (!debug_file) {
83                 debug_file = stderr;
84                 debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
85                        debug_filepath, strerror(errno));
86             }
87             setvbuf(debug_file, NULL, _IONBF, 0);
88         } else {
89             debug(DBG_WARN, "FTicksSyslogFacility starting with file:/// not "
90                   "permitted, assuming default F-Ticks destination");
91         }
92         return 1;
93     }
94     if (!strncasecmp(dest, "x-syslog://", 11) || log_type == LOG_TYPE_FTICKS) {
95         if (!strncasecmp(dest, "x-syslog://", 11)) {
96             dest += 11;
97             if (*dest == '/')
98                 dest++;
99         }
100         if (*dest) {
101             for (i = 0; facstrings[i]; i++)
102                 if (!strcasecmp(dest, facstrings[i]))
103                     break;
104             if (!facstrings[i])
105                 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
106             if (log_type != LOG_TYPE_FTICKS)
107                 debug_syslogfacility = facvals[i];
108 #if defined(WANT_FTICKS)
109             else if (log_type == LOG_TYPE_FTICKS)
110                 fticks_syslogfacility = facvals[i];
111 #endif
112         } else {
113             if (log_type != LOG_TYPE_FTICKS)
114                 debug_syslogfacility = LOG_DAEMON;
115 #if defined(WANT_FTICKS)
116             else if (log_type == LOG_TYPE_FTICKS)
117                 fticks_syslogfacility = 0;
118 #endif
119         }
120         openlog(debug_ident, LOG_PID, debug_syslogfacility);
121         return 1;
122     }
123     debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
124     exit(1);
125 }
126
127 void debug_reopen_log() {
128     extern int errno;
129
130     /* not a file, noop, return success */
131     if (!debug_filepath) {
132         debug(DBG_ERR, "skipping reopen");
133         return;
134     }
135
136     if (debug_file != stderr)
137         fclose(debug_file);
138
139     debug_file = fopen(debug_filepath, "a");
140     if (debug_file)
141         debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
142     else {
143         debug_file = stderr;
144         debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
145               debug_filepath, strerror(errno));
146     }
147     setvbuf(debug_file, NULL, _IONBF, 0);
148 }
149
150 void debug_logit(uint8_t level, const char *format, va_list ap) {
151     struct timeval now;
152     char *timebuf;
153     int priority;
154
155     if (debug_syslogfacility) {
156         switch (level) {
157         case DBG_DBG:
158             priority = LOG_DEBUG;
159             break;
160         case DBG_INFO:
161             priority = LOG_INFO;
162             break;
163         case DBG_NOTICE:
164             priority = LOG_NOTICE;
165             break;
166         case DBG_WARN:
167             priority = LOG_WARNING;
168             break;
169         case DBG_ERR:
170             priority = LOG_ERR;
171             break;
172         default:
173             priority = LOG_DEBUG;
174         }
175         vsyslog(priority, format, ap);
176     } else {
177         if (debug_timestamp && (timebuf = malloc(256))) {
178             gettimeofday(&now, NULL);
179             ctime_r(&now.tv_sec, timebuf);
180             timebuf[strlen(timebuf) - 1] = '\0';
181             fprintf(debug_file, "%s: ", timebuf + 4);
182             free(timebuf);
183         }
184         vfprintf(debug_file, format, ap);
185         fprintf(debug_file, "\n");
186     }
187 }
188
189 void debug(uint8_t level, char *format, ...) {
190     va_list ap;
191     if (level < debug_level)
192         return;
193     va_start(ap, format);
194     debug_logit(level, format, ap);
195     va_end(ap);
196 }
197
198 void debugx(int status, uint8_t level, char *format, ...) {
199     if (level >= debug_level) {
200         va_list ap;
201         va_start(ap, format);
202         debug_logit(level, format, ap);
203         va_end(ap);
204     }
205     exit(status);
206 }
207
208 void debugerrno(int err, uint8_t level, char *format, ...) {
209     if (level >= debug_level) {
210         va_list ap;
211         size_t len = strlen(format);
212         char *tmp = malloc(len + 1024 + 2);
213         assert(tmp);
214         strcpy(tmp, format);
215         tmp[len++] = ':';
216         tmp[len++] = ' ';
217         if (strerror_r(err, tmp + len, 1024))
218             tmp = format;
219         va_start(ap, format);
220         debug_logit(level, tmp, ap);
221         va_end(ap);
222     }
223 }
224
225 void debugerrnox(int err, uint8_t level, char *format, ...) {
226     if (level >= debug_level) {
227         va_list ap;
228         va_start(ap, format);
229         debugerrno(err, level, format, ap);
230         va_end(ap);
231     }
232     exit(err);
233 }
234
235 #if defined(WANT_FTICKS)
236 void fticks_debug(const char *format, ...) {
237     int priority;
238     va_list ap;
239     va_start(ap, format);
240     if (!debug_syslogfacility && !fticks_syslogfacility)
241         debug_logit(0xff, format, ap);
242     else {
243         priority = LOG_DEBUG | fticks_syslogfacility;
244         vsyslog(priority, format, ap);
245         va_end(ap);
246     }
247 }
248 #endif
249 /* Local Variables: */
250 /* c-file-style: "stroustrup" */
251 /* End: */