Add functions to log trust query results handled by the TID server
[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(LOG_MAKEPRI(facility, sev), "%s", buf);
77
78     free(buf);
79   }
80
81   va_end(ap_copy);
82 }
83
84 static void fire_log(const int sev, const int facility, const char *fmt, ...) {
85   va_list ap;
86
87   va_start(ap, fmt);
88   vfire_log(sev, facility, fmt, ap);
89   va_end(ap);
90 }
91
92 static char *audit_fmt(const char *key, char *value) {
93
94   if (NULL != key) {
95
96     /* Rewrite any NULL's to "nones" */
97     char *val = NULL == value ? "none" : value;
98
99     size_t len = strlen(key)
100                + strlen(val)
101                + strlen(LOG_FIELD_SEP)
102                + strlen(LOG_KV_SEP)
103                + 1;
104
105     char *buf = malloc(len);
106
107     snprintf(buf, len, "%s%s%s%s", LOG_FIELD_SEP, key, LOG_KV_SEP, val);
108
109     return buf;
110   }
111   else {
112
113     tr_debug("audit_fmt: Message dropped, null pointer passed.");
114     return NULL;
115   }
116 }
117
118 static void free_array(const int count, char *array[]) {
119
120    int i;
121
122    for(i = 0; i < count; i++) {
123       free(array[i]);
124    }
125 }
126
127 static char *join_audit_msg(const int count, char *array[]) {
128
129   int i;
130   int len = 1; /* start at one to account for terminator */
131
132   /* evaluate length of concatenated string */
133   for(i = 0; i < count; i++) {
134
135     if ((len + strlen(array[i]) + LOG_OVERHEAD) <= LOG_MAX_MESSAGE_SIZE) {
136
137       len += strlen(array[i]);
138     }
139   }
140
141   int remain = len - 1;
142   char *buf = (char *) calloc(len, sizeof(char));
143
144   /* join fields up to count */
145   for(i = 0; i < count; i++) {
146
147     if ((strlen(buf) + strlen(array[i]) + LOG_OVERHEAD + 1) <= LOG_MAX_MESSAGE_SIZE) {
148
149       strncat(buf, array[i], remain);
150       remain -= strlen(array[i]);
151     }
152     else {
153
154       tr_debug("join_audit_msg: Attribute dropped, too long.");
155     }
156   }
157
158   return buf;
159 }
160
161 int str2sev(const char* sev) {
162
163   if (strcmp(sev, "debug")) {
164
165     return LOG_DEBUG;
166   }
167   else if (strcmp(sev, "info")) {
168
169     return LOG_INFO;
170   }
171   else if (strcmp(sev, "notice")) {
172
173     return LOG_NOTICE;
174   }
175   else if (strcmp(sev, "warning")) {
176
177     return LOG_WARNING;
178   }
179   else if (strcmp(sev, "err")) {
180
181     return LOG_ERR;
182   }
183   else if (strcmp(sev, "crit")) {
184
185     return LOG_CRIT;
186   }
187   else if (strcmp(sev, "alert")) {
188
189     return LOG_ALERT;
190   }
191   else if (strcmp(sev, "emerg")) {
192
193     return LOG_EMERG;
194   }
195
196   tr_debug("str2sev: invalid severity specified: %s, logging everything", sev);
197
198   return LOG_DEBUG;
199 }
200
201 void tr_log_threshold(const int sev) {
202
203   log_threshold = sev;
204   return;
205 }
206
207 void tr_console_threshold(const int sev) {
208
209   console_threshold = sev;
210   return;
211 }
212
213 void tr_log_open() {
214
215   if (!log_opened) {
216
217     openlog(NULL, LOG_PID | LOG_NDELAY, LOG_FACILITY);
218     log_opened = 1;
219   }
220 }
221
222 void tr_log_close() {
223
224     closelog();
225     log_opened = 0;
226 }
227
228 void tr_log(const int sev, const char *fmt, ...) {
229
230   tr_log_open();
231
232   if (NULL != fmt) {
233
234     va_list ap;
235     va_start(ap, fmt);
236
237     vfire_log(sev, LOG_FACILITY, fmt, ap);
238
239     va_end(ap);
240   }
241   else {
242
243           tr_debug("tr_log: Message dropped, null pointer passed.");
244   }
245 }
246
247 /* Convinience Functions */
248
249 void tr_audit_resp(TID_RESP *resp) {
250
251   tr_log_open();
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   tr_log_open();
278
279   if (NULL != req) {
280
281     char *attrs[] = { audit_fmt("comm", NULL != req->comm ? req->comm->buf : NULL),
282                       audit_fmt("rp_realm", NULL != req->rp_realm ? req->rp_realm->buf : NULL),
283                       audit_fmt("realm", NULL != req->realm ? req->realm->buf : NULL),
284                     };
285
286     char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
287     free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
288
289     fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
290
291     free(msg);
292   }
293   else {
294
295         tr_debug("tr_audit_req: Message dropped, null pointer passed.");
296   }
297 }