Debian Codegraph @

src/journal/journald-syslog.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "sd-event.h"
7 #include "sd-messages.h"
8
9 #include "alloc-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "format-util.h"
13 #include "iovec-util.h"
14 #include "journal-internal.h"
15 #include "journald-client.h"
16 #include "journald-console.h"
17 #include "journald-context.h"
18 #include "journald-kmsg.h"
19 #include "journald-manager.h"
20 #include "journald-syslog.h"
21 #include "journald-wall.h"
22 #include "log.h"
23 #include "log-ratelimit.h"
24 #include "parse-util.h"
25 #include "process-util.h"
26 #include "selinux-util.h"
27 #include "socket-util.h"
28 #include "stdio-util.h"
29 #include "string-util.h"
30 #include "syslog-util.h"
31 #include "time-util.h"
32
33 /* Warn once every 30s if we missed syslog message */
34 #define WARN_FORWARD_SYSLOG_MISSED_USEC (30 * USEC_PER_SEC)
35
36 static void forward_syslog_iovec(
37 Manager *m,
38 const struct iovec *iovec,
39 unsigned n_iovec,
40 const struct ucred *ucred,
41 const struct timeval *tv) {
42
43 union sockaddr_union sa;
44
45 struct msghdr msghdr = {
46 .msg_iov = (struct iovec *) iovec,
47 .msg_iovlen = n_iovec,
48 };
49 struct cmsghdr *cmsg;
50 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
51 const char *j;
52 int r;
53
54 assert(m);
55 assert(iovec);
56 assert(n_iovec > 0);
57
58 j = strjoina(m->runtime_directory, "/syslog");
59 r = sockaddr_un_set_path(&sa.un, j);
60 if (r < 0) {
61 log_debug_errno(r, "Forwarding socket path %s too long for AF_UNIX, not forwarding: %m", j);
62 return;
63 }
64
65 msghdr.msg_name = &sa.sa;
66 msghdr.msg_namelen = r;
67
68 if (ucred) {
69 zero(control);
70 msghdr.msg_control = &control;
71 msghdr.msg_controllen = sizeof(control);
72
73 cmsg = CMSG_FIRSTHDR(&msghdr);
74 cmsg->cmsg_level = SOL_SOCKET;
75 cmsg->cmsg_type = SCM_CREDENTIALS;
76 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
77 memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
78 msghdr.msg_controllen = cmsg->cmsg_len;
79 }
80
81 /* Forward the syslog message we received via /dev/log to /run/systemd/syslog. Unfortunately we
82 * currently can't set the SO_TIMESTAMP auxiliary data, and hence we don't. */
83
84 if (sendmsg(m->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
85 return;
86
87 /* The socket is full? I guess the syslog implementation is
88 * too slow, and we shouldn't wait for that... */
89 if (errno == EAGAIN) {
90 m->n_forward_syslog_missed++;
91 return;
92 }
93
94 if (ucred && IN_SET(errno, ESRCH, EPERM)) {
95 struct ucred u;
96
97 /* Hmm, presumably the sender process vanished
98 * by now, or we don't have CAP_SYS_AMDIN, so
99 * let's fix it as good as we can, and retry */
100
101 u = *ucred;
102 u.pid = getpid_cached();
103 memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
104
105 if (sendmsg(m->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
106 return;
107
108 if (errno == EAGAIN) {
109 m->n_forward_syslog_missed++;
110 return;
111 }
112 }
113
114 if (errno != ENOENT)
115 log_debug_errno(errno, "Failed to forward syslog message: %m");
116 }
117
118 static void forward_syslog_raw(
119 Manager *m,
120 int priority,
121 const char *buffer,
122 size_t buffer_len,
123 const struct ucred *ucred,
124 const struct timeval *tv) {
125
126 struct iovec iovec;
127
128 assert(m);
129 assert(buffer);
130
131 if (LOG_PRI(priority) > m->config.max_level_syslog)
132 return;
133
134 iovec = IOVEC_MAKE((char *) buffer, buffer_len);
135 forward_syslog_iovec(m, &iovec, 1, ucred, tv);
136 }
137
138 void manager_forward_syslog(
139 Manager *m,
140 int priority,
141 const char *identifier,
142 const char *message,
143 const struct ucred *ucred,
144 const struct timeval *tv) {
145
146 struct iovec iovec[5];
147 char header_priority[DECIMAL_STR_MAX(priority) + 3], header_time[64],
148 header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t) + 1];
149 int n = 0;
150 struct tm tm;
151 _cleanup_free_ char *ident_buf = NULL;
152
153 assert(m);
154 assert(priority >= 0);
155 assert(priority <= 999);
156 assert(message);
157
158 if (LOG_PRI(priority) > m->config.max_level_syslog)
159 return;
160
161 /* First: priority field */
162 xsprintf(header_priority, "<%i>", priority);
163 iovec[n++] = IOVEC_MAKE_STRING(header_priority);
164
165 /* Second: timestamp */
166 if (localtime_or_gmtime_usec(tv ? tv->tv_sec * USEC_PER_SEC : now(CLOCK_REALTIME), /* utc= */ false, &tm) < 0)
167 return;
168 if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0)
169 return;
170 iovec[n++] = IOVEC_MAKE_STRING(header_time);
171
172 /* Third: identifier and PID */
173 if (ucred) {
174 if (!identifier) {
175 (void) pid_get_comm(ucred->pid, &ident_buf);
176 identifier = ident_buf;
177 }
178
179 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
180
181 if (identifier)
182 iovec[n++] = IOVEC_MAKE_STRING(identifier);
183
184 iovec[n++] = IOVEC_MAKE_STRING(header_pid);
185 } else if (identifier) {
186 iovec[n++] = IOVEC_MAKE_STRING(identifier);
187 iovec[n++] = IOVEC_MAKE_STRING(": ");
188 }
189
190 /* Fourth: message */
191 iovec[n++] = IOVEC_MAKE_STRING(message);
192
193 forward_syslog_iovec(m, iovec, n, ucred, tv);
194 }
195
196 int syslog_fixup_facility(int priority) {
197
198 if (LOG_FAC(priority) == 0)
199 return LOG_PRI(priority) | LOG_USER;
200
201 return priority;
202 }
203
204 size_t syslog_parse_identifier(const char **buf, char **ret_identifier, pid_t *ret_pid) {
205 const char *p;
206 size_t l, e;
207 pid_t pid = 0;
208
209 assert(buf);
210 assert(ret_identifier);
211 assert(ret_pid);
212
213 p = *buf;
214
215 p += strspn(p, WHITESPACE);
216 l = strcspn(p, WHITESPACE);
217
218 if (l <= 0 ||
219 p[l-1] != ':') {
220 *ret_identifier = NULL;
221 *ret_pid = 0;
222 return 0;
223 }
224
225 e = l;
226 l--;
227
228 if (l > 0 && p[l-1] == ']') {
229 size_t k = l-1;
230
231 for (;;) {
232
233 if (p[k] == '[') {
234 _cleanup_free_ char *t = strndup(p+k+1, l-k-2);
235 if (t)
236 (void) parse_pid(t, &pid);
237
238 l = k;
239 break;
240 }
241
242 if (k == 0)
243 break;
244
245 k--;
246 }
247 }
248
249 /* The syslog identifier should be short enough in most cases and NAME_MAX should be enough. Let's
250 * refuse ridiculously long identifier string as "no identifier string found", because if it is
251 * longer than some threshold then it is quite likely some misformatted data, and not a valid syslog
252 * message. Note. NAME_MAX is counted *without* the trailing NUL. */
253 _cleanup_free_ char *identifier = NULL;
254 if (l <= NAME_MAX)
255 identifier = strndup(p, l); /* ignore OOM here. */
256
257 /* Single space is used as separator */
258 if (p[e] != '\0' && strchr(WHITESPACE, p[e]))
259 e++;
260
261 l = (p - *buf) + e;
262 *buf = p + e;
263 *ret_identifier = TAKE_PTR(identifier);
264 *ret_pid = pid;
265 return l;
266 }
267
268 static size_t syslog_skip_timestamp(const char **buf) {
269 enum {
270 LETTER,
271 SPACE,
272 NUMBER,
273 SPACE_OR_NUMBER,
274 COLON
275 } sequence[] = {
276 LETTER, LETTER, LETTER,
277 SPACE,
278 SPACE_OR_NUMBER, NUMBER,
279 SPACE,
280 SPACE_OR_NUMBER, NUMBER,
281 COLON,
282 SPACE_OR_NUMBER, NUMBER,
283 COLON,
284 SPACE_OR_NUMBER, NUMBER,
285 SPACE
286 };
287
288 const char *p;
289 size_t i;
290
291 assert(buf);
292 assert(*buf);
293
294 for (i = 0, p = *buf; i < ELEMENTSOF(sequence); i++, p++) {
295 if (!*p)
296 return 0;
297
298 switch (sequence[i]) {
299
300 case SPACE:
301 if (*p != ' ')
302 return 0;
303 break;
304
305 case SPACE_OR_NUMBER:
306 if (*p == ' ')
307 break;
308
309 _fallthrough_;
310 case NUMBER:
311 if (!ascii_isdigit(*p))
312 return 0;
313
314 break;
315
316 case LETTER:
317 if (!ascii_isalpha(*p))
318 return 0;
319
320 break;
321
322 case COLON:
323 if (*p != ':')
324 return 0;
325 break;
326 }
327 }
328
329 assert(p >= *buf);
330 size_t n = p - *buf;
331 assert(n <= ELEMENTSOF(sequence));
332
333 *buf = p;
334 return n;
335 }
336
337 void manager_process_syslog_message(
338 Manager *m,
339 const char *buf,
340 size_t raw_len,
341 const struct ucred *ucred,
342 const struct timeval *tv,
343 const char *label) {
344
345 char *t, syslog_priority[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int)],
346 syslog_facility[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
347 const char *msg, *syslog_ts, *a;
348 _cleanup_free_ char *dummy = NULL, *msg_msg = NULL, *msg_raw = NULL;
349 int priority = LOG_USER | LOG_INFO, r;
350 ClientContext *context = NULL;
351 struct iovec *iovec;
352 size_t n = 0, mm, i, leading_ws, syslog_ts_len;
353 bool store_raw;
354
355 assert(m);
356 assert(buf);
357 /* The message cannot be empty. */
358 assert(raw_len > 0);
359 /* The buffer NUL-terminated and can be used a string. raw_len is the length
360 * without the terminating NUL byte, the buffer is actually one bigger. */
361 assert(buf[raw_len] == '\0');
362
363 if (ucred && pid_is_valid(ucred->pid)) {
364 r = client_context_get(m, ucred->pid, ucred, label, /* unit_id= */ NULL, &context);
365 if (r < 0)
366 log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
367 "Failed to retrieve credentials for PID " PID_FMT ", ignoring: %m",
368 ucred->pid);
369 }
370
371 /* We are creating a copy of the message because we want to forward the original message
372 verbatim to the legacy syslog implementation */
373 for (i = raw_len; i > 0; i--)
374 if (!strchr(WHITESPACE, buf[i-1]))
375 break;
376
377 leading_ws = strspn(buf, WHITESPACE);
378
379 if (i == 0)
380 /* The message contains only whitespaces */
381 msg = buf + raw_len;
382 else if (i == raw_len)
383 /* Nice! No need to strip anything on the end, let's optimize this a bit */
384 msg = buf + leading_ws;
385 else {
386 msg = dummy = new(char, i - leading_ws + 1);
387 if (!dummy) {
388 log_oom();
389 return;
390 }
391
392 memcpy(dummy, buf + leading_ws, i - leading_ws);
393 dummy[i - leading_ws] = 0;
394 }
395
396 /* We will add the SYSLOG_RAW= field when we stripped anything
397 * _or_ if the input message contained NUL bytes. */
398 store_raw = msg != buf || strlen(msg) != raw_len;
399
400 syslog_parse_priority(&msg, &priority, true);
401
402 if (!client_context_test_priority(context, priority))
403 return;
404
405 syslog_ts = msg;
406 syslog_ts_len = syslog_skip_timestamp(&msg);
407 if (syslog_ts_len == 0)
408 /* We failed to parse the full timestamp, store the raw message too */
409 store_raw = true;
410
411 _cleanup_free_ char *identifier = NULL;
412 pid_t pid;
413 syslog_parse_identifier(&msg, &identifier, &pid);
414
415 if (client_context_check_keep_log(context, msg, strlen(msg)) <= 0)
416 return;
417
418 if (m->config.forward_to_syslog)
419 forward_syslog_raw(m, priority, buf, raw_len, ucred, tv);
420
421 if (m->config.forward_to_kmsg)
422 manager_forward_kmsg(m, priority, identifier, msg, ucred);
423
424 if (m->config.forward_to_console)
425 manager_forward_console(m, priority, identifier, msg, ucred);
426
427 if (m->config.forward_to_wall)
428 manager_forward_wall(m, priority, identifier, msg, ucred);
429
430 mm = N_IOVEC_META_FIELDS + 8 + client_context_extra_fields_n_iovec(context);
431 iovec = newa(struct iovec, mm);
432
433 iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=syslog");
434
435 xsprintf(syslog_priority, "PRIORITY=%i", LOG_PRI(priority));
436 iovec[n++] = IOVEC_MAKE_STRING(syslog_priority);
437
438 if (LOG_FAC(priority) != 0) {
439 xsprintf(syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority));
440 iovec[n++] = IOVEC_MAKE_STRING(syslog_facility);
441 }
442
443 if (identifier) {
444 a = strjoina("SYSLOG_IDENTIFIER=", identifier);
445 iovec[n++] = IOVEC_MAKE_STRING(a);
446 }
447
448 char syslog_pid[STRLEN("SYSLOG_PID=") + DECIMAL_STR_MAX(pid_t)];
449 if (pid_is_valid(pid)) {
450 xsprintf(syslog_pid, "SYSLOG_PID="PID_FMT, pid);
451 iovec[n++] = IOVEC_MAKE_STRING(syslog_pid);
452 }
453
454 if (syslog_ts_len > 0) {
455 const size_t hlen = STRLEN("SYSLOG_TIMESTAMP=");
456
457 t = newa(char, hlen + syslog_ts_len);
458 memcpy(t, "SYSLOG_TIMESTAMP=", hlen);
459 memcpy(t + hlen, syslog_ts, syslog_ts_len);
460
461 iovec[n++] = IOVEC_MAKE(t, hlen + syslog_ts_len);
462 }
463
464 msg_msg = strjoin("MESSAGE=", msg);
465 if (!msg_msg) {
466 log_oom();
467 return;
468 }
469 iovec[n++] = IOVEC_MAKE_STRING(msg_msg);
470
471 if (store_raw) {
472 const size_t hlen = STRLEN("SYSLOG_RAW=");
473
474 msg_raw = new(char, hlen + raw_len);
475 if (!msg_raw) {
476 log_oom();
477 return;
478 }
479
480 memcpy(msg_raw, "SYSLOG_RAW=", hlen);
481 memcpy(msg_raw + hlen, buf, raw_len);
482
483 iovec[n++] = IOVEC_MAKE(msg_raw, hlen + raw_len);
484 }
485
486 manager_dispatch_message(m, iovec, n, mm, context, tv, priority, 0);
487 }
488
489 int manager_open_syslog_socket(Manager *m, const char *syslog_socket) {
490 int r;
491
492 assert(m);
493 assert(syslog_socket);
494
495 if (m->syslog_fd < 0) {
496 union sockaddr_union sa;
497 socklen_t sa_len;
498
499 r = sockaddr_un_set_path(&sa.un, syslog_socket);
500 if (r < 0)
501 return log_error_errno(r, "Unable to use namespace path %s for AF_UNIX socket: %m", syslog_socket);
502 sa_len = r;
503
504 m->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
505 if (m->syslog_fd < 0)
506 return log_error_errno(errno, "socket() failed: %m");
507
508 (void) sockaddr_un_unlink(&sa.un);
509
510 r = bind(m->syslog_fd, &sa.sa, sa_len);
511 if (r < 0)
512 return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
513
514 (void) chmod(sa.un.sun_path, 0666);
515 } else
516 (void) fd_nonblock(m->syslog_fd, true);
517
518 r = setsockopt_int(m->syslog_fd, SOL_SOCKET, SO_PASSCRED, true);
519 if (r < 0)
520 return log_error_errno(r, "Failed to enable SO_PASSCRED: %m");
521
522 r = setsockopt_int(m->syslog_fd, SOL_SOCKET, SO_PASSRIGHTS, false);
523 if (r < 0)
524 log_debug_errno(r, "Failed to turn off SO_PASSRIGHTS, ignoring: %m");
525
526 if (mac_selinux_use()) {
527 r = setsockopt_int(m->syslog_fd, SOL_SOCKET, SO_PASSSEC, true);
528 if (r < 0)
529 log_full_errno(ERRNO_IS_NEG_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r,
530 "Failed to enable SO_PASSSEC, ignoring: %m");
531 }
532
533 r = setsockopt_int(m->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, true);
534 if (r < 0)
535 return log_error_errno(r, "Failed to enable SO_TIMESTAMP: %m");
536
537 r = sd_event_add_io(m->event, &m->syslog_event_source, m->syslog_fd, EPOLLIN, manager_process_datagram, m);
538 if (r < 0)
539 return log_error_errno(r, "Failed to add syslog sevrer fd to event loop: %m");
540
541 r = sd_event_source_set_priority(m->syslog_event_source, SD_EVENT_PRIORITY_NORMAL+5);
542 if (r < 0)
543 return log_error_errno(r, "Failed to adjust syslog event source priority: %m");
544
545 return 0;
546 }
547
548 void manager_maybe_warn_forward_syslog_missed(Manager *m) {
549 usec_t n;
550
551 assert(m);
552
553 if (m->n_forward_syslog_missed <= 0)
554 return;
555
556 n = now(CLOCK_MONOTONIC);
557 if (m->last_warn_forward_syslog_missed + WARN_FORWARD_SYSLOG_MISSED_USEC > n)
558 return;
559
560 manager_driver_message(m, 0,
561 LOG_MESSAGE_ID(SD_MESSAGE_FORWARD_SYSLOG_MISSED_STR),
562 LOG_MESSAGE("Forwarding to syslog missed %u messages.",
563 m->n_forward_syslog_missed));
564
565 m->n_forward_syslog_missed = 0;
566 m->last_warn_forward_syslog_missed = n;
567 }