Use json_is_true() in place of json_boolean_value() for compatibility
[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 const char *sev2str(int sev)
166 {
167   switch (sev) {
168   case LOG_DEBUG:   return "debug";
169   case LOG_INFO:    return "info";
170   case LOG_NOTICE:  return "notice";
171   case LOG_WARNING: return "warning";
172   case LOG_ERR:     return "err";
173   case LOG_CRIT:    return "crit";
174   case LOG_ALERT:   return "alert";
175   default:          return "invalid";
176   }
177 }
178
179 int str2sev(const char* sev) {
180
181   if (strcmp(sev, "debug") ==0 ) {
182
183     return LOG_DEBUG;
184   }
185   else if (strcmp(sev, "info") == 0) {
186
187     return LOG_INFO;
188   }
189   else if (strcmp(sev, "notice") == 0) {
190
191     return LOG_NOTICE;
192   }
193   else if (strcmp(sev, "warning") == 0 ) {
194
195     return LOG_WARNING;
196   }
197   else if (strcmp(sev, "err") == 0) {
198
199     return LOG_ERR;
200   }
201   else if (strcmp(sev, "crit") == 0) {
202
203     return LOG_CRIT;
204   }
205   else if (strcmp(sev, "alert") == 0) {
206
207     return LOG_ALERT;
208   }
209   else if (strcmp(sev, "emerg")  == 0) {
210
211     return LOG_EMERG;
212   }
213
214   tr_debug("str2sev: invalid severity specified: %s, logging everything", sev);
215
216   return LOG_DEBUG;
217 }
218
219 void tr_log_threshold(const int sev) {
220
221   log_threshold = sev;
222   return;
223 }
224
225 void tr_console_threshold(const int sev) {
226
227   console_threshold = sev;
228   return;
229 }
230
231 void tr_log_open() {
232
233   if (!log_opened) {
234
235     openlog(NULL, LOG_PID | LOG_NDELAY, LOG_FACILITY);
236     log_opened = 1;
237   }
238 }
239
240 void tr_log_close() {
241
242     closelog();
243     log_opened = 0;
244 }
245
246 void tr_log(const int sev, const char *fmt, ...) {
247
248   if (NULL != fmt) {
249
250     va_list ap;
251     va_start(ap, fmt);
252
253     vfire_log(sev, LOG_FACILITY, fmt, ap);
254
255     va_end(ap);
256   }
257   else {
258
259           tr_debug("tr_log: Message dropped, null pointer passed.");
260   }
261 }
262
263 /* Convinience Functions */
264
265 void tr_audit_resp(TID_RESP *resp) {
266
267   if (NULL != resp) {
268
269     char *attrs[] = { audit_fmt("result", resp->result ? "error" : "success"),
270                       audit_fmt("comm", NULL != resp->comm ? resp->comm->buf : NULL),
271                       audit_fmt("rp_realm", NULL != resp->rp_realm ? resp->rp_realm->buf : NULL),
272                       audit_fmt("realm", NULL != resp->realm ? resp->realm->buf : NULL),
273                       audit_fmt("err", NULL != resp->err_msg ? resp->err_msg->buf : NULL)
274                     };
275
276     char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
277     free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
278
279     fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
280
281     free(msg);
282   }
283   else {
284
285     tr_debug("tr_audit_resp: Message dropped, null pointer passed.");
286   }
287 }
288
289 void tr_audit_req(TID_REQ *req) {
290
291   if (NULL != req) {
292
293     char *attrs[] = { audit_fmt("comm", NULL != req->comm ? req->comm->buf : NULL),
294                       audit_fmt("rp_realm", NULL != req->rp_realm ? req->rp_realm->buf : NULL),
295                       audit_fmt("realm", NULL != req->realm ? req->realm->buf : NULL),
296                     };
297
298     char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
299     free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
300
301     fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
302
303     free(msg);
304   }
305   else {
306
307         tr_debug("tr_audit_req: Message dropped, null pointer passed.");
308   }
309 }