a9339c
From d82c40a2377b487ef83aa1fb907ec275a1b3e86e Mon Sep 17 00:00:00 2001
a9339c
From: Michal Sekletar <msekleta@redhat.com>
a9339c
Date: Wed, 30 May 2018 16:27:22 +0200
a9339c
Subject: [PATCH] journal: forward messages from /dev/log unmodified to
a9339c
 syslog.socket
a9339c
a9339c
(cherry picked from commit bb3ff70a86faff85fe482995c8ba5332b1a34f76)
a9339c
a9339c
Resolves: #1409659
a9339c
---
a9339c
 src/journal/journald-server.c |  2 +-
de8967
 src/journal/journald-syslog.c | 39 ++++++++++++++++++++++-------------
a9339c
 src/journal/journald-syslog.h |  2 +-
a9339c
 3 files changed, 27 insertions(+), 16 deletions(-)
a9339c
a9339c
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
a9339c
index 7c69061f4..7e67e055e 100644
a9339c
--- a/src/journal/journald-server.c
a9339c
+++ b/src/journal/journald-server.c
a9339c
@@ -1294,7 +1294,7 @@ int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void
a9339c
 
a9339c
         if (fd == s->syslog_fd) {
a9339c
                 if (n > 0 && n_fds == 0)
a9339c
-                        server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
a9339c
+                        server_process_syslog_message(s, s->buffer, n, ucred, tv, label, label_len);
a9339c
                 else if (n_fds > 0)
a9339c
                         log_warning("Got file descriptors via syslog socket. Ignoring.");
a9339c
 
a9339c
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
a9339c
index b499a0d38..01d2bf69f 100644
a9339c
--- a/src/journal/journald-syslog.c
a9339c
+++ b/src/journal/journald-syslog.c
a9339c
@@ -109,7 +109,7 @@ static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned
a9339c
                 log_debug_errno(errno, "Failed to forward syslog message: %m");
a9339c
 }
a9339c
 
a9339c
-static void forward_syslog_raw(Server *s, int priority, const char *buffer, const struct ucred *ucred, const struct timeval *tv) {
a9339c
+static void forward_syslog_raw(Server *s, int priority, const char *buffer, size_t buffer_len, const struct ucred *ucred, const struct timeval *tv) {
a9339c
         struct iovec iovec;
a9339c
 
a9339c
         assert(s);
a9339c
@@ -118,7 +118,9 @@ static void forward_syslog_raw(Server *s, int priority, const char *buffer, cons
a9339c
         if (LOG_PRI(priority) > s->max_level_syslog)
a9339c
                 return;
a9339c
 
a9339c
-        IOVEC_SET_STRING(iovec, buffer);
a9339c
+        iovec.iov_base = (char *) buffer;
a9339c
+        iovec.iov_len = buffer_len;
a9339c
+
a9339c
         forward_syslog_iovec(s, &iovec, 1, ucred, tv);
a9339c
 }
a9339c
 
a9339c
@@ -311,40 +313,49 @@ static void syslog_skip_date(char **buf) {
a9339c
 void server_process_syslog_message(
a9339c
         Server *s,
a9339c
         const char *buf,
a9339c
+        size_t buf_len,
a9339c
         const struct ucred *ucred,
a9339c
         const struct timeval *tv,
a9339c
         const char *label,
a9339c
         size_t label_len) {
a9339c
 
a9339c
         char syslog_priority[sizeof("PRIORITY=") + DECIMAL_STR_MAX(int)],
a9339c
-             syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
a9339c
+             syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)], *msg;
a9339c
         const char *message = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
a9339c
         struct iovec iovec[N_IOVEC_META_FIELDS + 6];
a9339c
-        unsigned n = 0;
a9339c
+        unsigned n = 0, i;
a9339c
         int priority = LOG_USER | LOG_INFO;
a9339c
         _cleanup_free_ char *identifier = NULL, *pid = NULL;
a9339c
-        const char *orig;
a9339c
 
a9339c
         assert(s);
a9339c
         assert(buf);
a9339c
 
a9339c
-        orig = buf;
a9339c
-        syslog_parse_priority(&buf, &priority, true);
a9339c
+        /* We are creating copy of the message because we want to forward original message verbatim to the legacy
a9339c
+           syslog implementation */
a9339c
+        for (i = buf_len; i > 0; i--)
a9339c
+                if (!strchr(WHITESPACE, buf[i-1]))
a9339c
+                        break;
a9339c
+
a9339c
+        msg = newa(char, i + 1);
a9339c
+        *((char *) mempcpy(msg, buf, i)) = 0;
a9339c
+        msg += strspn(msg, WHITESPACE);
a9339c
+
a9339c
+        syslog_parse_priority((const char **)&msg, &priority, true);
a9339c
 
a9339c
         if (s->forward_to_syslog)
a9339c
-                forward_syslog_raw(s, priority, orig, ucred, tv);
a9339c
+                forward_syslog_raw(s, priority, buf, buf_len, ucred, tv);
a9339c
 
a9339c
-        syslog_skip_date((char**) &buf;;
a9339c
-        syslog_parse_identifier(&buf, &identifier, &pid;;
a9339c
+        syslog_skip_date(&msg;;
a9339c
+        syslog_parse_identifier((const char**)&msg, &identifier, &pid;;
a9339c
 
a9339c
         if (s->forward_to_kmsg)
a9339c
-                server_forward_kmsg(s, priority, identifier, buf, ucred);
a9339c
+                server_forward_kmsg(s, priority, identifier, msg, ucred);
a9339c
 
a9339c
         if (s->forward_to_console)
a9339c
-                server_forward_console(s, priority, identifier, buf, ucred);
a9339c
+                server_forward_console(s, priority, identifier, msg, ucred);
a9339c
 
a9339c
         if (s->forward_to_wall)
a9339c
-                server_forward_wall(s, priority, identifier, buf, ucred);
a9339c
+                server_forward_wall(s, priority, identifier, msg, ucred);
a9339c
 
a9339c
         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
a9339c
 
a9339c
@@ -368,7 +379,7 @@ void server_process_syslog_message(
a9339c
                         IOVEC_SET_STRING(iovec[n++], syslog_pid);
a9339c
         }
a9339c
 
a9339c
-        message = strjoina("MESSAGE=", buf);
a9339c
+        message = strjoina("MESSAGE=", msg);
a9339c
         if (message)
a9339c
                 IOVEC_SET_STRING(iovec[n++], message);
a9339c
 
a9339c
diff --git a/src/journal/journald-syslog.h b/src/journal/journald-syslog.h
a9339c
index 3774ebdf0..e593be99a 100644
a9339c
--- a/src/journal/journald-syslog.h
a9339c
+++ b/src/journal/journald-syslog.h
a9339c
@@ -29,7 +29,7 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid);
a9339c
 
a9339c
 void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, const struct ucred *ucred, const struct timeval *tv);
a9339c
 
a9339c
-void server_process_syslog_message(Server *s, const char *buf, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len);
a9339c
+void server_process_syslog_message(Server *s, const char *buf, size_t buf_len, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len);
a9339c
 int server_open_syslog_socket(Server *s);
a9339c
 
a9339c
 void server_maybe_warn_forward_syslog_missed(Server *s);