Mention default 'secret' value in radsecproxy.conf.5.
[libradsec.git] / debug.c
1 /*
2  * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
3  * Copyright (C) 2010 NORDUnet A/S
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  */
9
10 #ifndef SYS_SOLARIS9
11 #include <stdint.h>
12 #endif
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <strings.h>
18 #include <time.h>
19 #include <sys/time.h>
20 #include <syslog.h>
21 #include <errno.h>
22 #include <assert.h>
23 #include "debug.h"
24 #include "util.h"
25
26 static char *debug_ident = NULL;
27 static uint8_t debug_level = DBG_INFO;
28 static char *debug_filepath = NULL;
29 static FILE *debug_file = NULL;
30 static int debug_syslogfacility = 0;
31 static uint8_t debug_timestamp = 0;
32
33 void debug_init(char *ident) {
34     debug_file = stderr;
35     setvbuf(debug_file, NULL, _IONBF, 0);
36     debug_ident = ident;
37 }
38
39 void debug_set_level(uint8_t level) {
40     switch (level) {
41     case 1:
42         debug_level = DBG_ERR;
43         return;
44     case 2:
45         debug_level = DBG_WARN;
46         return;
47     case 3:
48         debug_level = DBG_NOTICE;
49         return;
50     case 4:
51         debug_level = DBG_INFO;
52         return;
53     case 5:
54         debug_level = DBG_DBG;
55         return;
56     }
57 }
58
59 void debug_timestamp_on() {
60     debug_timestamp = 1;
61 }
62
63 uint8_t debug_get_level() {
64     return debug_level;
65 }
66
67 int debug_set_destination(char *dest) {
68     static const char *facstrings[] = { "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[] = { LOG_DAEMON, LOG_MAIL, LOG_USER, LOG_LOCAL0,
72                                    LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4,
73                                    LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
74     extern int errno;
75     int i;
76
77     if (!strncasecmp(dest, "file:///", 8)) {
78         debug_filepath = stringcopy(dest + 7, 0);
79         debug_file = fopen(debug_filepath, "a");
80         if (!debug_file) {
81             debug_file = stderr;
82             debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
83                    debug_filepath, strerror(errno));
84         }
85         setvbuf(debug_file, NULL, _IONBF, 0);
86         return 1;
87     }
88     if (!strncasecmp(dest, "x-syslog://", 11)) {
89         dest += 11;
90         if (*dest == '/')
91             dest++;
92         if (*dest) {
93             for (i = 0; facstrings[i]; i++)
94                 if (!strcasecmp(dest, facstrings[i]))
95                     break;
96             if (!facstrings[i])
97                 debugx(1, DBG_ERR, "Unknown syslog facility %s", dest);
98             debug_syslogfacility = facvals[i];
99         } else
100             debug_syslogfacility = LOG_DAEMON;
101         openlog(debug_ident, LOG_PID, debug_syslogfacility);
102         return 1;
103     }
104     debug(DBG_ERR, "Unknown log destination, exiting %s", dest);
105     exit(1);
106 }
107
108 void debug_reopen_log() {
109     extern int errno;
110
111     /* not a file, noop, return success */
112     if (!debug_filepath) {
113         debug(DBG_ERR, "skipping reopen");
114         return;
115     }
116
117     if (debug_file != stderr)
118         fclose(debug_file);
119
120     debug_file = fopen(debug_filepath, "a");
121     if (debug_file)
122         debug(DBG_ERR, "Reopened logfile %s", debug_filepath);
123     else {
124         debug_file = stderr;
125         debug(DBG_ERR, "Failed to open logfile %s, using stderr\n%s",
126               debug_filepath, strerror(errno));
127     }
128     setvbuf(debug_file, NULL, _IONBF, 0);
129 }
130
131 void debug_logit(uint8_t level, const char *format, va_list ap) {
132     struct timeval now;
133     char *timebuf;
134     int priority;
135
136     if (debug_syslogfacility) {
137         switch (level) {
138         case DBG_DBG:
139             priority = LOG_DEBUG;
140             break;
141         case DBG_INFO:
142             priority = LOG_INFO;
143             break;
144         case DBG_NOTICE:
145             priority = LOG_NOTICE;
146             break;
147         case DBG_WARN:
148             priority = LOG_WARNING;
149             break;
150         case DBG_ERR:
151             priority = LOG_ERR;
152             break;
153         default:
154             priority = LOG_DEBUG;
155         }
156         vsyslog(priority, format, ap);
157     } else {
158         if (debug_timestamp && (timebuf = malloc(256))) {
159             gettimeofday(&now, NULL);
160             ctime_r(&now.tv_sec, timebuf);
161             timebuf[strlen(timebuf) - 1] = '\0';
162             fprintf(debug_file, "%s: ", timebuf + 4);
163             free(timebuf);
164         }
165         vfprintf(debug_file, format, ap);
166         fprintf(debug_file, "\n");
167     }
168 }
169
170 void debug(uint8_t level, char *format, ...) {
171     va_list ap;
172     if (level < debug_level)
173         return;
174     va_start(ap, format);
175     debug_logit(level, format, ap);
176     va_end(ap);
177 }
178
179 void debugx(int status, uint8_t level, char *format, ...) {
180     if (level >= debug_level) {
181         va_list ap;
182         va_start(ap, format);
183         debug_logit(level, format, ap);
184         va_end(ap);
185     }
186     exit(status);
187 }
188
189 void debugerrno(int err, uint8_t level, char *format, ...) {
190     if (level >= debug_level) {
191         va_list ap;
192         size_t len = strlen(format);
193         char *tmp = malloc(len + 1024 + 2);
194         assert(tmp);
195         strcpy(tmp, format);
196         tmp[len++] = ':';
197         tmp[len++] = ' ';
198         if (strerror_r(err, tmp + len, 1024))
199             tmp = format;
200         va_start(ap, format);
201         debug_logit(level, tmp, ap);
202         va_end(ap);
203     }
204 }
205
206 void debugerrnox(int err, uint8_t level, char *format, ...) {
207     if (level >= debug_level) {
208         va_list ap;
209         va_start(ap, format);
210         debugerrno(err, level, format, ap);
211         va_end(ap);
212     }
213     exit(err);
214 }
215
216 /* Local Variables: */
217 /* c-file-style: "stroustrup" */
218 /* End: */