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.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.
80 syslog((facility|sev), "%s", buf);
88 static void fire_log(const int sev, const int facility, const char *fmt, ...) {
92 vfire_log(sev, facility, fmt, ap);
96 static char *audit_fmt(const char *key, char *value) {
100 /* Rewrite any NULL's to "nones" */
101 char *val = NULL == value ? "none" : value;
103 size_t len = strlen(key)
105 + strlen(LOG_FIELD_SEP)
109 char *buf = malloc(len);
111 snprintf(buf, len, "%s%s%s%s", LOG_FIELD_SEP, key, LOG_KV_SEP, val);
117 tr_debug("audit_fmt: Message dropped, null pointer passed.");
122 static void free_array(const int count, char *array[]) {
126 for(i = 0; i < count; i++) {
131 static char *join_audit_msg(const int count, char *array[]) {
134 int len = 1; /* start at one to account for terminator */
136 /* evaluate length of concatenated string */
137 for(i = 0; i < count; i++) {
139 if ((len + strlen(array[i]) + LOG_OVERHEAD) <= LOG_MAX_MESSAGE_SIZE) {
141 len += strlen(array[i]);
145 int remain = len - 1;
146 char *buf = (char *) calloc(len, sizeof(char));
148 /* join fields up to count */
149 for(i = 0; i < count; i++) {
151 if ((strlen(buf) + strlen(array[i]) + LOG_OVERHEAD + 1) <= LOG_MAX_MESSAGE_SIZE) {
153 strncat(buf, array[i], remain);
154 remain -= strlen(array[i]);
158 tr_debug("join_audit_msg: Attribute dropped, too long.");
165 int str2sev(const char* sev) {
167 if (strcmp(sev, "debug") ==0 ) {
171 else if (strcmp(sev, "info") == 0) {
175 else if (strcmp(sev, "notice") == 0) {
179 else if (strcmp(sev, "warning") == 0 ) {
183 else if (strcmp(sev, "err") == 0) {
187 else if (strcmp(sev, "crit") == 0) {
191 else if (strcmp(sev, "alert") == 0) {
195 else if (strcmp(sev, "emerg") == 0) {
200 tr_debug("str2sev: invalid severity specified: %s, logging everything", sev);
205 void tr_log_threshold(const int sev) {
211 void tr_console_threshold(const int sev) {
213 console_threshold = sev;
221 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_FACILITY);
226 void tr_log_close() {
232 void tr_log(const int sev, const char *fmt, ...) {
239 vfire_log(sev, LOG_FACILITY, fmt, ap);
245 tr_debug("tr_log: Message dropped, null pointer passed.");
249 /* Convinience Functions */
251 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) {
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),
284 char *msg = join_audit_msg(sizeof(attrs) / sizeof(attrs[0]), attrs);
285 free_array(sizeof(attrs) / sizeof(attrs[0]), attrs);
287 fire_log(LOG_INFO, AUDIT_FACILITY, "%s%s%s", LOG_PREFIX, msg, LOG_MSG_TERMINATOR);
293 tr_debug("tr_audit_req: Message dropped, null pointer passed.");