c921b8ade7d4075b9ebdd134ce3f1f0c4806556b
[trust_router.git] / common / tr_debug.c
1 /*
2  * Copyright (c) 2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <syslog.h>
37 #include <tr_debug.h>
38 #include <tid_internal.h>
39
40 #define LOG_MAX_MESSAGE_SIZE 65536
41 #define LOG_FACILITY LOG_LOCAL5
42
43 #define LOG_PREFIX "F-TICKS/abfab/1.0"
44 #define LOG_OVERHEAD strlen(LOG_PREFIX)
45 #define LOG_FIELD_SEP "#"
46 #define LOG_MSG_TERMINATOR "#"
47 #define LOG_KV_SEP "="
48 #define AUDIT_FACILITY LOG_AUTHPRIV
49
50 static int log_opened = 0;
51
52 /* We'll be noisy until overriden */
53 static int log_threshold = LOG_DEBUG;
54 static int console_threshold = LOG_DEBUG;
55
56 static void vfire_log(const int sev, const int facility, const char *fmt, va_list ap) {
57
58    /* if we want to use ap twice, we need to duplicate it before the first use */
59    va_list ap_copy;
60    va_copy(ap_copy, ap);
61
62   /* write messages to stderr if they are more severe than the threshold and are not audit messages */
63   if ((sev <= console_threshold) && (facility != AUDIT_FACILITY)) {
64
65     vfprintf(stderr, fmt, ap);
66     fprintf(stderr, "\n");
67   }
68
69   /* write messages to syslog if they are more severe than the threshold or are audit messages */
70   if (sev <= log_threshold || (facility == AUDIT_FACILITY)) {
71
72     /* Make sure that the message will fit, truncate if necessary */
73     char *buf = malloc(LOG_MAX_MESSAGE_SIZE);
74     vsnprintf(buf, LOG_MAX_MESSAGE_SIZE, fmt, ap_copy);
75
76     /* syslog.h provides a macro for generating priorities, however in versions of glibc < 2.17 it is
77        broken if you use it as documented: https://sourceware.org/bugzilla/show_bug.cgi?id=14347
78        RHEL6 uses glibc 2.12, so do not use LOG_MAKEPRI until around 2020.
79     */
80     syslog((facility|sev), "%s", buf);
81
82     free(buf);
83   }
84
85   va_end(ap_copy);
86 }
87
88 static void fire_log(const int sev, const int facility, const char *fmt, ...) {
89   va_list ap;
90
91   va_start(ap, fmt);
92   vfire_log(sev, facility, fmt, ap);
93   va_end(ap);
94 }
95
96 static char *audit_fmt(const char *key, char *value) {
97
98   if (NULL != key) {
99
100     /* Rewrite any NULL's to "nones" */
101     char *val = NULL == value ? "none" : value;
102
103     size_t len = strlen(key)
104                + strlen(val)
105                + strlen(LOG_FIELD_SEP)
106                + strlen(LOG_KV_SEP)
107                + 1;
108
109     char *buf = malloc(len);
110
111     snprintf(buf, len, "%s%s%s%s", LOG_FIELD_SEP, key, LOG_KV_SEP, val);
112
113     return buf;
114   }
115   else {
116
117     tr_debug("audit_fmt: Message dropped, null pointer passed.");
118     return NULL;
119   }
120 }
121
122 static void free_array(const int count, char *array[]) {
123
124    int i;
125
126    for(i = 0; i < count; i++) {
127       free(array[i]);
128    }
129 }
130
131 static char *join_audit_msg(const int count, char *array[]) {
132
133   int i;
134   int len = 1; /* start at one to account for terminator */
135
136   /* evaluate length of concatenated string */
137   for(i = 0; i < count; i++) {
138
139     if ((len + strlen(array[i]) + LOG_OVERHEAD) <= LOG_MAX_MESSAGE_SIZE) {
140
141       len += strlen(array[i]);
142     }
143   }
144
145   int remain = len - 1;
146   char *buf = (char *) calloc(len, sizeof(char));
147
148   /* join fields up to count */
149   for(i = 0; i < count; i++) {
150
151     if ((strlen(buf) + strlen(array[i]) + LOG_OVERHEAD + 1) <= LOG_MAX_MESSAGE_SIZE) {
152
153       strncat(buf, array[i], remain);
154       remain -= strlen(array[i]);
155     }
156     else {
157
158       tr_debug("join_audit_msg: Attribute dropped, too long.");
159     }
160   }
161
162   return buf;
163 }
164
165 int str2sev(const char* sev) {
166
167   if (strcmp(sev, "debug") ==0 ) {
168
169     return LOG_DEBUG;
170   }
171   else if (strcmp(sev, "info") == 0) {
172
173     return LOG_INFO;
174   }
175   else if (strcmp(sev, "notice") == 0) {
176
177     return LOG_NOTICE;
178   }
179   else if (strcmp(sev, "warning") == 0 ) {
180
181     return LOG_WARNING;
182   }
183   else if (strcmp(sev, "err") == 0) {
184
185     return LOG_ERR;
186   }
187   else if (strcmp(sev, "crit") == 0) {
188
189     return LOG_CRIT;
190   }
191   else if (strcmp(sev, "alert") == 0) {
192
193     return LOG_ALERT;
194   }
195   else if (strcmp(sev, "emerg")  == 0) {
196
197     return LOG_EMERG;
198   }
199
200   tr_debug("str2sev: invalid severity specified: %s, logging everything", sev);
201
202   return LOG_DEBUG;
203 }
204
205 void tr_log_threshold(const int sev) {
206
207   log_threshold = sev;
208   return;
209 }
210
211 void tr_console_threshold(const int sev) {
212
213   console_threshold = sev;
214   return;
215 }
216
217 void tr_log_open() {
218
219   if (!log_opened) {
220
221     openlog(NULL, LOG_PID | LOG_NDELAY, LOG_FACILITY);
222     log_opened = 1;
223   }
224 }
225
226 void tr_log_close() {
227
228     closelog();
229     log_opened = 0;
230 }
231
232 void tr_log(const int sev, const char *fmt, ...) {
233
234   if (NULL != fmt) {
235
236     va_list ap;
237     va_start(ap, fmt);
238
239     vfire_log(sev, LOG_FACILITY, fmt, ap);
240
241     va_end(ap);
242   }
243   else {
244
245           tr_debug("tr_log: Message dropped, null pointer passed.");
246   }
247 }
248
249 /* Convinience Functions */
250
251 void tr_audit_resp(TID_RESP *resp) {
252
253   if (NULL != resp) {
254
255     char *attrs[] = { audit_fmt("result", resp->result ? "error" : "success"),
256                       audit_fmt("comm", NULL != resp->comm ? resp->comm->buf : NULL),
257                       audit_fmt("rp_realm", NULL != resp->rp_realm ? resp->rp_realm->buf : NULL),
258                       audit_fmt("realm", NULL != resp->realm ? resp->realm->buf : NULL),
259                       audit_fmt("err", NULL != resp->err_msg ? resp->err_msg->buf : NULL)
260                     };
261
262     char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
263     free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
264
265     fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
266
267     free(msg);
268   }
269   else {
270
271     tr_debug("tr_audit_resp: Message dropped, null pointer passed.");
272   }
273 }
274
275 void tr_audit_req(TID_REQ *req) {
276
277   if (NULL != req) {
278
279     char *attrs[] = { audit_fmt("comm", NULL != req->comm ? req->comm->buf : NULL),
280                       audit_fmt("rp_realm", NULL != req->rp_realm ? req->rp_realm->buf : NULL),
281                       audit_fmt("realm", NULL != req->realm ? req->realm->buf : NULL),
282                     };
283
284     char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
285     free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
286
287     fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
288
289     free(msg);
290   }
291   else {
292
293         tr_debug("tr_audit_req: Message dropped, null pointer passed.");
294   }
295 }