2 * Copyright (c) 2014, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include <tid_internal.h>
40 #define LOG_MAX_MESSAGE_SIZE 65536
41 #define LOG_FACILITY LOG_LOCAL5
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
50 static int log_opened = 0;
52 /* We'll be noisy until overriden */
53 static int log_threshold = LOG_DEBUG;
54 static int console_threshold = LOG_DEBUG;
56 static void vfire_log(const int sev, const int facility, const char *fmt, va_list ap) {
58 /* if we want to use ap twice, we need to duplicate it before the first use */
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)) {
65 vfprintf(stderr, fmt, ap);
66 fprintf(stderr, "\n");
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)) {
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);
76 syslog(LOG_MAKEPRI(facility, sev), "%s", buf);
84 static void fire_log(const int sev, const int facility, const char *fmt, ...) {
88 vfire_log(sev, facility, fmt, ap);
92 static char *audit_fmt(const char *key, char *value) {
96 /* Rewrite any NULL's to "nones" */
97 char *val = NULL == value ? "none" : value;
99 size_t len = strlen(key)
101 + strlen(LOG_FIELD_SEP)
105 char *buf = malloc(len);
107 snprintf(buf, len, "%s%s%s%s", LOG_FIELD_SEP, key, LOG_KV_SEP, val);
113 tr_debug("audit_fmt: Message dropped, null pointer passed.");
118 static void free_array(const int count, char *array[]) {
122 for(i = 0; i < count; i++) {
127 static char *join_audit_msg(const int count, char *array[]) {
130 int len = 1; /* start at one to account for terminator */
132 /* evaluate length of concatenated string */
133 for(i = 0; i < count; i++) {
135 if ((len + strlen(array[i]) + LOG_OVERHEAD) <= LOG_MAX_MESSAGE_SIZE) {
137 len += strlen(array[i]);
141 int remain = len - 1;
142 char *buf = (char *) calloc(len, sizeof(char));
144 /* join fields up to count */
145 for(i = 0; i < count; i++) {
147 if ((strlen(buf) + strlen(array[i]) + LOG_OVERHEAD + 1) <= LOG_MAX_MESSAGE_SIZE) {
149 strncat(buf, array[i], remain);
150 remain -= strlen(array[i]);
154 tr_debug("join_audit_msg: Attribute dropped, too long.");
161 int str2sev(const char* sev) {
163 if (strcmp(sev, "debug")) {
167 else if (strcmp(sev, "info")) {
171 else if (strcmp(sev, "notice")) {
175 else if (strcmp(sev, "warning")) {
179 else if (strcmp(sev, "err")) {
183 else if (strcmp(sev, "crit")) {
187 else if (strcmp(sev, "alert")) {
191 else if (strcmp(sev, "emerg")) {
196 tr_debug("str2sev: invalid severity specified: %s, logging everything", sev);
201 void tr_log_threshold(const int sev) {
207 void tr_console_threshold(const int sev) {
209 console_threshold = sev;
217 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_FACILITY);
222 void tr_log_close() {
228 void tr_log(const int sev, const char *fmt, ...) {
237 vfire_log(sev, LOG_FACILITY, fmt, ap);
243 tr_debug("tr_log: Message dropped, null pointer passed.");
247 /* Convinience Functions */
249 void tr_audit_resp(TID_RESP *resp) {
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)
262 char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
263 free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
265 fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
271 tr_debug("tr_audit_resp: Message dropped, null pointer passed.");
275 void tr_audit_req(TID_REQ *req) {
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),
286 char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
287 free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
289 fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
295 tr_debug("tr_audit_req: Message dropped, null pointer passed.");