]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote-main.c
tree-wide: use FORK_REARRANGE_STDIO and FORK_CLOSE_ALL_FDS
[thirdparty/systemd.git] / src / journal-remote / journal-remote-main.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4 #include <unistd.h>
5
6 #include "sd-daemon.h"
7
8 #include "build.h"
9 #include "conf-parser.h"
10 #include "constants.h"
11 #include "daemon-util.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "journal-remote-write.h"
15 #include "journal-remote.h"
16 #include "main-func.h"
17 #include "memory-util.h"
18 #include "parse-argument.h"
19 #include "pretty-print.h"
20 #include "process-util.h"
21 #include "rlimit-util.h"
22 #include "signal-util.h"
23 #include "socket-netlink.h"
24 #include "socket-util.h"
25 #include "stat-util.h"
26 #include "string-table.h"
27 #include "strv.h"
28
29 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
30 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
31 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
32
33 static const char* arg_url = NULL;
34 static const char* arg_getter = NULL;
35 static const char* arg_listen_raw = NULL;
36 static const char* arg_listen_http = NULL;
37 static const char* arg_listen_https = NULL;
38 static char** arg_files = NULL; /* Do not free this. */
39 static bool arg_compress = true;
40 static bool arg_seal = false;
41 static int http_socket = -1, https_socket = -1;
42 static char** arg_gnutls_log = NULL;
43
44 static JournalWriteSplitMode arg_split_mode = _JOURNAL_WRITE_SPLIT_INVALID;
45 static const char* arg_output = NULL;
46
47 static char *arg_key = NULL;
48 static char *arg_cert = NULL;
49 static char *arg_trust = NULL;
50 #if HAVE_GNUTLS
51 static bool arg_trust_all = false;
52 #else
53 static bool arg_trust_all = true;
54 #endif
55
56 static uint64_t arg_max_use = UINT64_MAX;
57 static uint64_t arg_max_size = UINT64_MAX;
58 static uint64_t arg_n_max_files = UINT64_MAX;
59 static uint64_t arg_keep_free = UINT64_MAX;
60
61 STATIC_DESTRUCTOR_REGISTER(arg_gnutls_log, strv_freep);
62 STATIC_DESTRUCTOR_REGISTER(arg_key, freep);
63 STATIC_DESTRUCTOR_REGISTER(arg_cert, freep);
64 STATIC_DESTRUCTOR_REGISTER(arg_trust, freep);
65
66 static const char* const journal_write_split_mode_table[_JOURNAL_WRITE_SPLIT_MAX] = {
67 [JOURNAL_WRITE_SPLIT_NONE] = "none",
68 [JOURNAL_WRITE_SPLIT_HOST] = "host",
69 };
70
71 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode, JournalWriteSplitMode);
72 static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode,
73 journal_write_split_mode,
74 JournalWriteSplitMode,
75 "Failed to parse split mode setting");
76
77 /**********************************************************************
78 **********************************************************************
79 **********************************************************************/
80
81 static int spawn_child(const char* child, char** argv) {
82 pid_t child_pid;
83 int fd[2], r;
84
85 if (pipe(fd) < 0)
86 return log_error_errno(errno, "Failed to create pager pipe: %m");
87
88 r = safe_fork_full("(remote)",
89 (int[]) {STDIN_FILENO, fd[1], STDERR_FILENO },
90 NULL, 0,
91 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_REARRANGE_STDIO|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE, &child_pid);
92 if (r < 0) {
93 safe_close_pair(fd);
94 return r;
95 }
96
97 /* In the child */
98 if (r == 0) {
99 execvp(child, argv);
100 log_error_errno(errno, "Failed to exec child %s: %m", child);
101 _exit(EXIT_FAILURE);
102 }
103
104 safe_close(fd[1]);
105
106 r = fd_nonblock(fd[0], true);
107 if (r < 0)
108 log_warning_errno(errno, "Failed to set child pipe to non-blocking: %m");
109
110 return fd[0];
111 }
112
113 static int spawn_curl(const char* url) {
114 char **argv = STRV_MAKE("curl",
115 "-HAccept: application/vnd.fdo.journal",
116 "--silent",
117 "--show-error",
118 url);
119 int r;
120
121 r = spawn_child("curl", argv);
122 if (r < 0)
123 log_error_errno(r, "Failed to spawn curl: %m");
124 return r;
125 }
126
127 static int spawn_getter(const char *getter) {
128 int r;
129 _cleanup_strv_free_ char **words = NULL;
130
131 assert(getter);
132 r = strv_split_full(&words, getter, WHITESPACE, EXTRACT_UNQUOTE);
133 if (r < 0)
134 return log_error_errno(r, "Failed to split getter option: %m");
135
136 r = spawn_child(words[0], words);
137 if (r < 0)
138 log_error_errno(r, "Failed to spawn getter %s: %m", getter);
139
140 return r;
141 }
142
143 /**********************************************************************
144 **********************************************************************
145 **********************************************************************/
146
147 static int null_timer_event_handler(sd_event_source *s,
148 uint64_t usec,
149 void *userdata);
150 static int dispatch_http_event(sd_event_source *event,
151 int fd,
152 uint32_t revents,
153 void *userdata);
154
155 static int request_meta(void **connection_cls, int fd, char *hostname) {
156 RemoteSource *source;
157 Writer *writer;
158 int r;
159
160 assert(connection_cls);
161 if (*connection_cls)
162 return 0;
163
164 r = journal_remote_get_writer(journal_remote_server_global, hostname, &writer);
165 if (r < 0)
166 return log_warning_errno(r, "Failed to get writer for source %s: %m",
167 hostname);
168
169 source = source_new(fd, true, hostname, writer);
170 if (!source) {
171 writer_unref(writer);
172 return log_oom();
173 }
174
175 log_debug("Added RemoteSource as connection metadata %p", source);
176
177 *connection_cls = source;
178 return 0;
179 }
180
181 static void request_meta_free(void *cls,
182 struct MHD_Connection *connection,
183 void **connection_cls,
184 enum MHD_RequestTerminationCode toe) {
185 RemoteSource *s;
186
187 assert(connection_cls);
188 s = *connection_cls;
189
190 if (s) {
191 log_debug("Cleaning up connection metadata %p", s);
192 source_free(s);
193 *connection_cls = NULL;
194 }
195 }
196
197 static int process_http_upload(
198 struct MHD_Connection *connection,
199 const char *upload_data,
200 size_t *upload_data_size,
201 RemoteSource *source) {
202
203 bool finished = false;
204 size_t remaining;
205 int r;
206
207 assert(source);
208
209 log_trace("%s: connection %p, %zu bytes",
210 __func__, connection, *upload_data_size);
211
212 if (*upload_data_size) {
213 log_trace("Received %zu bytes", *upload_data_size);
214
215 r = journal_importer_push_data(&source->importer,
216 upload_data, *upload_data_size);
217 if (r < 0)
218 return mhd_respond_oom(connection);
219
220 *upload_data_size = 0;
221 } else
222 finished = true;
223
224 for (;;) {
225 r = process_source(source, journal_remote_server_global->file_flags);
226 if (r == -EAGAIN)
227 break;
228 if (r < 0) {
229 if (r == -ENOBUFS)
230 log_warning_errno(r, "Entry is above the maximum of %u, aborting connection %p.",
231 DATA_SIZE_MAX, connection);
232 else if (r == -E2BIG)
233 log_warning_errno(r, "Entry with more fields than the maximum of %u, aborting connection %p.",
234 ENTRY_FIELD_COUNT_MAX, connection);
235 else
236 log_warning_errno(r, "Failed to process data, aborting connection %p: %m",
237 connection);
238 return MHD_NO;
239 }
240 }
241
242 if (!finished)
243 return MHD_YES;
244
245 /* The upload is finished */
246
247 remaining = journal_importer_bytes_remaining(&source->importer);
248 if (remaining > 0) {
249 log_warning("Premature EOF byte. %zu bytes lost.", remaining);
250 return mhd_respondf(connection,
251 0, MHD_HTTP_EXPECTATION_FAILED,
252 "Premature EOF. %zu bytes of trailing data not processed.",
253 remaining);
254 }
255
256 return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.");
257 };
258
259 static mhd_result request_handler(
260 void *cls,
261 struct MHD_Connection *connection,
262 const char *url,
263 const char *method,
264 const char *version,
265 const char *upload_data,
266 size_t *upload_data_size,
267 void **connection_cls) {
268
269 const char *header;
270 int r, code, fd;
271 _cleanup_free_ char *hostname = NULL;
272 bool chunked = false;
273
274 assert(connection);
275 assert(connection_cls);
276 assert(url);
277 assert(method);
278
279 log_trace("Handling a connection %s %s %s", method, url, version);
280
281 if (*connection_cls)
282 return process_http_upload(connection,
283 upload_data, upload_data_size,
284 *connection_cls);
285
286 if (!streq(method, "POST"))
287 return mhd_respond(connection, MHD_HTTP_NOT_ACCEPTABLE, "Unsupported method.");
288
289 if (!streq(url, "/upload"))
290 return mhd_respond(connection, MHD_HTTP_NOT_FOUND, "Not found.");
291
292 header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Content-Type");
293 if (!header || !streq(header, "application/vnd.fdo.journal"))
294 return mhd_respond(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
295 "Content-Type: application/vnd.fdo.journal is required.");
296
297 header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Transfer-Encoding");
298 if (header) {
299 if (!strcaseeq(header, "chunked"))
300 return mhd_respondf(connection, 0, MHD_HTTP_BAD_REQUEST,
301 "Unsupported Transfer-Encoding type: %s", header);
302
303 chunked = true;
304 }
305
306 header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Content-Length");
307 if (header) {
308 size_t len;
309
310 if (chunked)
311 return mhd_respond(connection, MHD_HTTP_BAD_REQUEST,
312 "Content-Length must not specified when Transfer-Encoding type is 'chuncked'");
313
314 r = safe_atozu(header, &len);
315 if (r < 0)
316 return mhd_respondf(connection, r, MHD_HTTP_LENGTH_REQUIRED,
317 "Content-Length: %s cannot be parsed: %m", header);
318
319 if (len > ENTRY_SIZE_MAX)
320 /* When serialized, an entry of maximum size might be slightly larger,
321 * so this does not correspond exactly to the limit in journald. Oh well.
322 */
323 return mhd_respondf(connection, 0, MHD_HTTP_CONTENT_TOO_LARGE,
324 "Payload larger than maximum size of %u bytes", ENTRY_SIZE_MAX);
325 }
326
327 {
328 const union MHD_ConnectionInfo *ci;
329
330 ci = MHD_get_connection_info(connection,
331 MHD_CONNECTION_INFO_CONNECTION_FD);
332 if (!ci) {
333 log_error("MHD_get_connection_info failed: cannot get remote fd");
334 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
335 "Cannot check remote address.");
336 }
337
338 fd = ci->connect_fd;
339 assert(fd >= 0);
340 }
341
342 if (journal_remote_server_global->check_trust) {
343 r = check_permissions(connection, &code, &hostname);
344 if (r < 0)
345 return code;
346 } else {
347 r = getpeername_pretty(fd, false, &hostname);
348 if (r < 0)
349 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
350 "Cannot check remote hostname.");
351 }
352
353 assert(hostname);
354
355 r = request_meta(connection_cls, fd, hostname);
356 if (r == -ENOMEM)
357 return respond_oom(connection);
358 else if (r < 0)
359 return mhd_respondf(connection, r, MHD_HTTP_INTERNAL_SERVER_ERROR, "%m");
360
361 hostname = NULL;
362 return MHD_YES;
363 }
364
365 static int setup_microhttpd_server(RemoteServer *s,
366 int fd,
367 const char *key,
368 const char *cert,
369 const char *trust) {
370 struct MHD_OptionItem opts[] = {
371 { MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) request_meta_free},
372 { MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) microhttpd_logger},
373 { MHD_OPTION_LISTEN_SOCKET, fd},
374 { MHD_OPTION_CONNECTION_MEMORY_LIMIT, 128*1024},
375 { MHD_OPTION_END},
376 { MHD_OPTION_END},
377 { MHD_OPTION_END},
378 { MHD_OPTION_END},
379 { MHD_OPTION_END}};
380 int opts_pos = 4;
381 int flags =
382 MHD_USE_DEBUG |
383 MHD_USE_DUAL_STACK |
384 MHD_USE_EPOLL |
385 MHD_USE_ITC;
386
387 const union MHD_DaemonInfo *info;
388 int r, epoll_fd;
389 MHDDaemonWrapper *d;
390
391 assert(fd >= 0);
392
393 r = fd_nonblock(fd, true);
394 if (r < 0)
395 return log_error_errno(r, "Failed to make fd:%d nonblocking: %m", fd);
396
397 /* MHD_OPTION_STRICT_FOR_CLIENT is introduced in microhttpd 0.9.54,
398 * and MHD_USE_PEDANTIC_CHECKS will be deprecated in future.
399 * If MHD_USE_PEDANTIC_CHECKS is '#define'd, then it is deprecated
400 * and we should use MHD_OPTION_STRICT_FOR_CLIENT. On the other hand,
401 * if MHD_USE_PEDANTIC_CHECKS is not '#define'd, then it is not
402 * deprecated yet and there exists an enum element with the same name.
403 * So we can safely use it. */
404 #ifdef MHD_USE_PEDANTIC_CHECKS
405 opts[opts_pos++] = (struct MHD_OptionItem)
406 {MHD_OPTION_STRICT_FOR_CLIENT, 1};
407 #else
408 flags |= MHD_USE_PEDANTIC_CHECKS;
409 #endif
410
411 if (key) {
412 assert(cert);
413
414 opts[opts_pos++] = (struct MHD_OptionItem)
415 {MHD_OPTION_HTTPS_MEM_KEY, 0, (char*) key};
416 opts[opts_pos++] = (struct MHD_OptionItem)
417 {MHD_OPTION_HTTPS_MEM_CERT, 0, (char*) cert};
418
419 flags |= MHD_USE_TLS;
420
421 if (trust)
422 opts[opts_pos++] = (struct MHD_OptionItem)
423 {MHD_OPTION_HTTPS_MEM_TRUST, 0, (char*) trust};
424 }
425
426 d = new(MHDDaemonWrapper, 1);
427 if (!d)
428 return log_oom();
429
430 d->fd = (uint64_t) fd;
431
432 d->daemon = MHD_start_daemon(flags, 0,
433 NULL, NULL,
434 request_handler, NULL,
435 MHD_OPTION_ARRAY, opts,
436 MHD_OPTION_END);
437 if (!d->daemon) {
438 log_error("Failed to start µhttp daemon");
439 r = -EINVAL;
440 goto error;
441 }
442
443 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
444 key ? "HTTPS" : "HTTP", fd, d);
445
446 info = MHD_get_daemon_info(d->daemon, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
447 if (!info) {
448 log_error("µhttp returned NULL daemon info");
449 r = -EOPNOTSUPP;
450 goto error;
451 }
452
453 epoll_fd = info->listen_fd;
454 if (epoll_fd < 0) {
455 log_error("µhttp epoll fd is invalid");
456 r = -EUCLEAN;
457 goto error;
458 }
459
460 r = sd_event_add_io(s->events, &d->io_event,
461 epoll_fd, EPOLLIN,
462 dispatch_http_event, d);
463 if (r < 0) {
464 log_error_errno(r, "Failed to add event callback: %m");
465 goto error;
466 }
467
468 r = sd_event_source_set_description(d->io_event, "io_event");
469 if (r < 0) {
470 log_error_errno(r, "Failed to set source name: %m");
471 goto error;
472 }
473
474 r = sd_event_add_time(s->events, &d->timer_event,
475 CLOCK_MONOTONIC, UINT64_MAX, 0,
476 null_timer_event_handler, d);
477 if (r < 0) {
478 log_error_errno(r, "Failed to add timer_event: %m");
479 goto error;
480 }
481
482 r = sd_event_source_set_description(d->timer_event, "timer_event");
483 if (r < 0) {
484 log_error_errno(r, "Failed to set source name: %m");
485 goto error;
486 }
487
488 r = hashmap_ensure_put(&s->daemons, &uint64_hash_ops, &d->fd, d);
489 if (r == -ENOMEM) {
490 log_oom();
491 goto error;
492 }
493 if (r < 0) {
494 log_error_errno(r, "Failed to add daemon to hashmap: %m");
495 goto error;
496 }
497
498 s->active++;
499 return 0;
500
501 error:
502 MHD_stop_daemon(d->daemon);
503 free(d->daemon);
504 free(d);
505 return r;
506 }
507
508 static int setup_microhttpd_socket(RemoteServer *s,
509 const char *address,
510 const char *key,
511 const char *cert,
512 const char *trust) {
513 int fd;
514
515 fd = make_socket_fd(LOG_DEBUG, address, SOCK_STREAM, SOCK_CLOEXEC);
516 if (fd < 0)
517 return fd;
518
519 return setup_microhttpd_server(s, fd, key, cert, trust);
520 }
521
522 static int null_timer_event_handler(sd_event_source *timer_event,
523 uint64_t usec,
524 void *userdata) {
525 return dispatch_http_event(timer_event, 0, 0, userdata);
526 }
527
528 static int dispatch_http_event(sd_event_source *event,
529 int fd,
530 uint32_t revents,
531 void *userdata) {
532 MHDDaemonWrapper *d = ASSERT_PTR(userdata);
533 int r;
534 MHD_UNSIGNED_LONG_LONG timeout = ULLONG_MAX;
535
536 r = MHD_run(d->daemon);
537 if (r == MHD_NO)
538 // FIXME: unregister daemon
539 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
540 "MHD_run failed!");
541 if (MHD_get_timeout(d->daemon, &timeout) == MHD_NO)
542 timeout = ULLONG_MAX;
543
544 r = sd_event_source_set_time(d->timer_event, timeout);
545 if (r < 0) {
546 log_warning_errno(r, "Unable to set event loop timeout: %m, this may result in indefinite blocking!");
547 return 1;
548 }
549
550 r = sd_event_source_set_enabled(d->timer_event, SD_EVENT_ON);
551 if (r < 0)
552 log_warning_errno(r, "Unable to enable timer_event: %m, this may result in indefinite blocking!");
553
554 return 1; /* work to do */
555 }
556
557 /**********************************************************************
558 **********************************************************************
559 **********************************************************************/
560
561 static int setup_signals(RemoteServer *s) {
562 int r;
563
564 assert(s);
565
566 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, -1) >= 0);
567
568 r = sd_event_add_signal(s->events, &s->sigterm_event, SIGTERM, NULL, s);
569 if (r < 0)
570 return r;
571
572 r = sd_event_add_signal(s->events, &s->sigint_event, SIGINT, NULL, s);
573 if (r < 0)
574 return r;
575
576 return 0;
577 }
578
579 static int setup_raw_socket(RemoteServer *s, const char *address) {
580 int fd;
581
582 fd = make_socket_fd(LOG_INFO, address, SOCK_STREAM, SOCK_CLOEXEC);
583 if (fd < 0)
584 return fd;
585
586 return journal_remote_add_raw_socket(s, fd);
587 }
588
589 static int create_remoteserver(
590 RemoteServer *s,
591 const char* key,
592 const char* cert,
593 const char* trust) {
594
595 int r, n, fd;
596
597 r = journal_remote_server_init(
598 s,
599 arg_output,
600 arg_split_mode,
601 (arg_compress ? JOURNAL_COMPRESS : 0) |
602 (arg_seal ? JOURNAL_SEAL : 0));
603 if (r < 0)
604 return r;
605
606 r = setup_signals(s);
607 if (r < 0)
608 return log_error_errno(r, "Failed to set up signals: %m");
609
610 n = sd_listen_fds(true);
611 if (n < 0)
612 return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
613 else
614 log_debug("Received %d descriptors", n);
615
616 if (MAX(http_socket, https_socket) >= SD_LISTEN_FDS_START + n)
617 return log_error_errno(SYNTHETIC_ERRNO(EBADFD),
618 "Received fewer sockets than expected");
619
620 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
621 if (sd_is_socket(fd, AF_UNSPEC, 0, true)) {
622 log_debug("Received a listening socket (fd:%d)", fd);
623
624 if (fd == http_socket)
625 r = setup_microhttpd_server(s, fd, NULL, NULL, NULL);
626 else if (fd == https_socket)
627 r = setup_microhttpd_server(s, fd, key, cert, trust);
628 else
629 r = journal_remote_add_raw_socket(s, fd);
630 } else if (sd_is_socket(fd, AF_UNSPEC, 0, false)) {
631 char *hostname;
632
633 r = getpeername_pretty(fd, false, &hostname);
634 if (r < 0)
635 return log_error_errno(r, "Failed to retrieve remote name: %m");
636
637 log_debug("Received a connection socket (fd:%d) from %s", fd, hostname);
638
639 r = journal_remote_add_source(s, fd, hostname, true);
640 } else
641 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
642 "Unknown socket passed on fd:%d", fd);
643
644 if (r < 0)
645 return log_error_errno(r, "Failed to register socket (fd:%d): %m", fd);
646 }
647
648 if (arg_getter) {
649 log_info("Spawning getter %s...", arg_getter);
650 fd = spawn_getter(arg_getter);
651 if (fd < 0)
652 return fd;
653
654 r = journal_remote_add_source(s, fd, (char*) arg_output, false);
655 if (r < 0)
656 return r;
657 }
658
659 if (arg_url) {
660 const char *url, *hostname;
661
662 if (!strstr(arg_url, "/entries")) {
663 if (endswith(arg_url, "/"))
664 url = strjoina(arg_url, "entries");
665 else
666 url = strjoina(arg_url, "/entries");
667 } else
668 url = strdupa_safe(arg_url);
669
670 log_info("Spawning curl %s...", url);
671 fd = spawn_curl(url);
672 if (fd < 0)
673 return fd;
674
675 hostname = STARTSWITH_SET(arg_url, "https://", "http://");
676 if (!hostname)
677 hostname = arg_url;
678
679 hostname = strndupa_safe(hostname, strcspn(hostname, "/:"));
680
681 r = journal_remote_add_source(s, fd, (char *) hostname, false);
682 if (r < 0)
683 return r;
684 }
685
686 if (arg_listen_raw) {
687 log_debug("Listening on a socket...");
688 r = setup_raw_socket(s, arg_listen_raw);
689 if (r < 0)
690 return r;
691 }
692
693 if (arg_listen_http) {
694 r = setup_microhttpd_socket(s, arg_listen_http, NULL, NULL, NULL);
695 if (r < 0)
696 return r;
697 }
698
699 if (arg_listen_https) {
700 r = setup_microhttpd_socket(s, arg_listen_https, key, cert, trust);
701 if (r < 0)
702 return r;
703 }
704
705 STRV_FOREACH(file, arg_files) {
706 const char *output_name;
707
708 if (streq(*file, "-")) {
709 log_debug("Using standard input as source.");
710
711 fd = STDIN_FILENO;
712 output_name = "stdin";
713 } else {
714 log_debug("Reading file %s...", *file);
715
716 fd = open(*file, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
717 if (fd < 0)
718 return log_error_errno(errno, "Failed to open %s: %m", *file);
719 output_name = *file;
720 }
721
722 r = journal_remote_add_source(s, fd, (char*) output_name, false);
723 if (r < 0)
724 return r;
725 }
726
727 if (s->active == 0)
728 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
729 "Zero sources specified");
730
731 if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE) {
732 /* In this case we know what the writer will be
733 called, so we can create it and verify that we can
734 create output as expected. */
735 r = journal_remote_get_writer(s, NULL, &s->_single_writer);
736 if (r < 0)
737 return r;
738 }
739
740 return 0;
741 }
742
743 static int negative_fd(const char *spec) {
744 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
745
746 int fd, r;
747
748 r = safe_atoi(spec, &fd);
749 if (r < 0)
750 return r;
751
752 if (fd > 0)
753 return -EINVAL;
754 else
755 return -fd;
756 }
757
758 static int parse_config(void) {
759 const ConfigTableItem items[] = {
760 { "Remote", "Seal", config_parse_bool, 0, &arg_seal },
761 { "Remote", "SplitMode", config_parse_write_split_mode, 0, &arg_split_mode },
762 { "Remote", "ServerKeyFile", config_parse_path, 0, &arg_key },
763 { "Remote", "ServerCertificateFile", config_parse_path, 0, &arg_cert },
764 { "Remote", "TrustedCertificateFile", config_parse_path, 0, &arg_trust },
765 { "Remote", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use },
766 { "Remote", "MaxFileSize", config_parse_iec_uint64, 0, &arg_max_size },
767 { "Remote", "MaxFiles", config_parse_uint64, 0, &arg_n_max_files },
768 { "Remote", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free },
769 {}
770 };
771
772 return config_parse_many_nulstr(
773 PKGSYSCONFDIR "/journal-remote.conf",
774 CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"),
775 "Remote\0",
776 config_item_table_lookup, items,
777 CONFIG_PARSE_WARN,
778 NULL,
779 NULL);
780 }
781
782 static int help(void) {
783 _cleanup_free_ char *link = NULL;
784 int r;
785
786 r = terminal_urlify_man("systemd-journal-remote.service", "8", &link);
787 if (r < 0)
788 return log_oom();
789
790 printf("%s [OPTIONS...] {FILE|-}...\n\n"
791 "Write external journal events to journal file(s).\n\n"
792 " -h --help Show this help\n"
793 " --version Show package version\n"
794 " --url=URL Read events from systemd-journal-gatewayd at URL\n"
795 " --getter=COMMAND Read events from the output of COMMAND\n"
796 " --listen-raw=ADDR Listen for connections at ADDR\n"
797 " --listen-http=ADDR Listen for HTTP connections at ADDR\n"
798 " --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
799 " -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
800 " --compress[=BOOL] Use compression in the output journal (default: yes)\n"
801 " --seal[=BOOL] Use event sealing (default: no)\n"
802 " --key=FILENAME SSL key in PEM format (default:\n"
803 " \"" PRIV_KEY_FILE "\")\n"
804 " --cert=FILENAME SSL certificate in PEM format (default:\n"
805 " \"" CERT_FILE "\")\n"
806 " --trust=FILENAME|all SSL CA certificate or disable checking (default:\n"
807 " \"" TRUST_FILE "\")\n"
808 " --gnutls-log=CATEGORY...\n"
809 " Specify a list of gnutls logging categories\n"
810 " --split-mode=none|host How many output files to create\n"
811 "\nNote: file descriptors from sd_listen_fds() will be consumed, too.\n"
812 "\nSee the %s for details.\n",
813 program_invocation_short_name,
814 link);
815
816 return 0;
817 }
818
819 static int parse_argv(int argc, char *argv[]) {
820 enum {
821 ARG_VERSION = 0x100,
822 ARG_URL,
823 ARG_LISTEN_RAW,
824 ARG_LISTEN_HTTP,
825 ARG_LISTEN_HTTPS,
826 ARG_GETTER,
827 ARG_SPLIT_MODE,
828 ARG_COMPRESS,
829 ARG_SEAL,
830 ARG_KEY,
831 ARG_CERT,
832 ARG_TRUST,
833 ARG_GNUTLS_LOG,
834 };
835
836 static const struct option options[] = {
837 { "help", no_argument, NULL, 'h' },
838 { "version", no_argument, NULL, ARG_VERSION },
839 { "url", required_argument, NULL, ARG_URL },
840 { "getter", required_argument, NULL, ARG_GETTER },
841 { "listen-raw", required_argument, NULL, ARG_LISTEN_RAW },
842 { "listen-http", required_argument, NULL, ARG_LISTEN_HTTP },
843 { "listen-https", required_argument, NULL, ARG_LISTEN_HTTPS },
844 { "output", required_argument, NULL, 'o' },
845 { "split-mode", required_argument, NULL, ARG_SPLIT_MODE },
846 { "compress", optional_argument, NULL, ARG_COMPRESS },
847 { "seal", optional_argument, NULL, ARG_SEAL },
848 { "key", required_argument, NULL, ARG_KEY },
849 { "cert", required_argument, NULL, ARG_CERT },
850 { "trust", required_argument, NULL, ARG_TRUST },
851 { "gnutls-log", required_argument, NULL, ARG_GNUTLS_LOG },
852 {}
853 };
854
855 int c, r;
856 bool type_a, type_b;
857
858 assert(argc >= 0);
859 assert(argv);
860
861 while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
862 switch (c) {
863
864 case 'h':
865 return help();
866
867 case ARG_VERSION:
868 return version();
869
870 case ARG_URL:
871 if (arg_url)
872 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
873 "cannot currently set more than one --url");
874
875 arg_url = optarg;
876 break;
877
878 case ARG_GETTER:
879 if (arg_getter)
880 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
881 "cannot currently use --getter more than once");
882
883 arg_getter = optarg;
884 break;
885
886 case ARG_LISTEN_RAW:
887 if (arg_listen_raw)
888 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
889 "cannot currently use --listen-raw more than once");
890
891 arg_listen_raw = optarg;
892 break;
893
894 case ARG_LISTEN_HTTP:
895 if (arg_listen_http || http_socket >= 0)
896 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
897 "cannot currently use --listen-http more than once");
898
899 r = negative_fd(optarg);
900 if (r >= 0)
901 http_socket = r;
902 else
903 arg_listen_http = optarg;
904 break;
905
906 case ARG_LISTEN_HTTPS:
907 if (arg_listen_https || https_socket >= 0)
908 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
909 "cannot currently use --listen-https more than once");
910
911 r = negative_fd(optarg);
912 if (r >= 0)
913 https_socket = r;
914 else
915 arg_listen_https = optarg;
916
917 break;
918
919 case ARG_KEY:
920 if (arg_key)
921 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
922 "Key file specified twice");
923
924 arg_key = strdup(optarg);
925 if (!arg_key)
926 return log_oom();
927
928 break;
929
930 case ARG_CERT:
931 if (arg_cert)
932 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
933 "Certificate file specified twice");
934
935 arg_cert = strdup(optarg);
936 if (!arg_cert)
937 return log_oom();
938
939 break;
940
941 case ARG_TRUST:
942 #if HAVE_GNUTLS
943 if (arg_trust || arg_trust_all)
944 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
945 "Confusing trusted CA configuration");
946
947 if (streq(optarg, "all"))
948 arg_trust_all = true;
949 else {
950 arg_trust = strdup(optarg);
951 if (!arg_trust)
952 return log_oom();
953 }
954 #else
955 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
956 "Option --trust is not available.");
957 #endif
958 break;
959
960 case 'o':
961 if (arg_output)
962 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
963 "cannot use --output/-o more than once");
964
965 arg_output = optarg;
966 break;
967
968 case ARG_SPLIT_MODE:
969 arg_split_mode = journal_write_split_mode_from_string(optarg);
970 if (arg_split_mode == _JOURNAL_WRITE_SPLIT_INVALID)
971 return log_error_errno(arg_split_mode, "Invalid split mode: %s", optarg);
972 break;
973
974 case ARG_COMPRESS:
975 r = parse_boolean_argument("--compress", optarg, &arg_compress);
976 if (r < 0)
977 return r;
978 break;
979
980 case ARG_SEAL:
981 r = parse_boolean_argument("--seal", optarg, &arg_seal);
982 if (r < 0)
983 return r;
984 break;
985
986 case ARG_GNUTLS_LOG:
987 #if HAVE_GNUTLS
988 for (const char* p = optarg;;) {
989 _cleanup_free_ char *word = NULL;
990
991 r = extract_first_word(&p, &word, ",", 0);
992 if (r < 0)
993 return log_error_errno(r, "Failed to parse --gnutls-log= argument: %m");
994 if (r == 0)
995 break;
996
997 if (strv_push(&arg_gnutls_log, word) < 0)
998 return log_oom();
999
1000 word = NULL;
1001 }
1002 break;
1003 #else
1004 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1005 "Option --gnutls-log is not available.");
1006 #endif
1007
1008 case '?':
1009 return -EINVAL;
1010
1011 default:
1012 assert_not_reached();
1013 }
1014
1015 if (optind < argc)
1016 arg_files = argv + optind;
1017
1018 type_a = arg_getter || !strv_isempty(arg_files);
1019 type_b = arg_url
1020 || arg_listen_raw
1021 || arg_listen_http || arg_listen_https
1022 || sd_listen_fds(false) > 0;
1023 if (type_a && type_b)
1024 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1025 "Cannot use file input or --getter with "
1026 "--arg-listen-... or socket activation.");
1027 if (type_a) {
1028 if (!arg_output)
1029 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1030 "Option --output must be specified with file input or --getter.");
1031
1032 if (!IN_SET(arg_split_mode, JOURNAL_WRITE_SPLIT_NONE, _JOURNAL_WRITE_SPLIT_INVALID))
1033 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1034 "For active sources, only --split-mode=none is allowed.");
1035
1036 arg_split_mode = JOURNAL_WRITE_SPLIT_NONE;
1037 }
1038
1039 if (arg_split_mode == _JOURNAL_WRITE_SPLIT_INVALID)
1040 arg_split_mode = JOURNAL_WRITE_SPLIT_HOST;
1041
1042 if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE && arg_output) {
1043 if (is_dir(arg_output, true) > 0)
1044 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1045 "For SplitMode=none, output must be a file.");
1046 if (!endswith(arg_output, ".journal"))
1047 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1048 "For SplitMode=none, output file name must end with .journal.");
1049 }
1050
1051 if (arg_split_mode == JOURNAL_WRITE_SPLIT_HOST
1052 && arg_output && is_dir(arg_output, true) <= 0)
1053 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1054 "For SplitMode=host, output must be a directory.");
1055
1056 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1057 journal_write_split_mode_to_string(arg_split_mode),
1058 strna(arg_key),
1059 strna(arg_cert),
1060 strna(arg_trust));
1061
1062 return 1 /* work to do */;
1063 }
1064
1065 static int load_certificates(char **key, char **cert, char **trust) {
1066 int r;
1067
1068 r = read_full_file_full(
1069 AT_FDCWD, arg_key ?: PRIV_KEY_FILE, UINT64_MAX, SIZE_MAX,
1070 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
1071 NULL,
1072 key, NULL);
1073 if (r < 0)
1074 return log_error_errno(r, "Failed to read key from file '%s': %m",
1075 arg_key ?: PRIV_KEY_FILE);
1076
1077 r = read_full_file_full(
1078 AT_FDCWD, arg_cert ?: CERT_FILE, UINT64_MAX, SIZE_MAX,
1079 READ_FULL_FILE_CONNECT_SOCKET,
1080 NULL,
1081 cert, NULL);
1082 if (r < 0)
1083 return log_error_errno(r, "Failed to read certificate from file '%s': %m",
1084 arg_cert ?: CERT_FILE);
1085
1086 if (arg_trust_all)
1087 log_info("Certificate checking disabled.");
1088 else {
1089 r = read_full_file_full(
1090 AT_FDCWD, arg_trust ?: TRUST_FILE, UINT64_MAX, SIZE_MAX,
1091 READ_FULL_FILE_CONNECT_SOCKET,
1092 NULL,
1093 trust, NULL);
1094 if (r < 0)
1095 return log_error_errno(r, "Failed to read CA certificate file '%s': %m",
1096 arg_trust ?: TRUST_FILE);
1097 }
1098
1099 if ((arg_listen_raw || arg_listen_http) && *trust)
1100 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1101 "Option --trust makes all non-HTTPS connections untrusted.");
1102
1103 return 0;
1104 }
1105
1106 static int run(int argc, char **argv) {
1107 _cleanup_(journal_remote_server_destroy) RemoteServer s = {};
1108 _unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
1109 _cleanup_(erase_and_freep) char *key = NULL;
1110 _cleanup_free_ char *cert = NULL, *trust = NULL;
1111 int r;
1112
1113 log_show_color(true);
1114 log_parse_environment();
1115
1116 /* The journal merging logic potentially needs a lot of fds. */
1117 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1118
1119 r = parse_config();
1120 if (r < 0)
1121 return r;
1122
1123 r = parse_argv(argc, argv);
1124 if (r <= 0)
1125 return r;
1126
1127 if (arg_listen_http || arg_listen_https) {
1128 r = setup_gnutls_logger(arg_gnutls_log);
1129 if (r < 0)
1130 return r;
1131 }
1132
1133 if (arg_listen_https || https_socket >= 0) {
1134 r = load_certificates(&key, &cert, &trust);
1135 if (r < 0)
1136 return r;
1137
1138 s.check_trust = !arg_trust_all;
1139 }
1140
1141 journal_reset_metrics(&s.metrics);
1142 s.metrics.max_use = arg_max_use;
1143 s.metrics.max_size = arg_max_size;
1144 s.metrics.keep_free = arg_keep_free;
1145 s.metrics.n_max_files = arg_n_max_files;
1146
1147 r = create_remoteserver(&s, key, cert, trust);
1148 if (r < 0)
1149 return r;
1150
1151 r = sd_event_set_watchdog(s.events, true);
1152 if (r < 0)
1153 return log_error_errno(r, "Failed to enable watchdog: %m");
1154
1155 log_debug("Watchdog is %sd.", enable_disable(r > 0));
1156
1157 log_debug("%s running as pid "PID_FMT,
1158 program_invocation_short_name, getpid_cached());
1159
1160 notify_message = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
1161
1162 while (s.active) {
1163 r = sd_event_get_state(s.events);
1164 if (r < 0)
1165 return r;
1166 if (r == SD_EVENT_FINISHED)
1167 break;
1168
1169 r = sd_event_run(s.events, -1);
1170 if (r < 0)
1171 return log_error_errno(r, "Failed to run event loop: %m");
1172 }
1173
1174 notify_message = NULL;
1175 (void) sd_notifyf(false,
1176 "STOPPING=1\n"
1177 "STATUS=Shutting down after writing %" PRIu64 " entries...", s.event_count);
1178
1179 log_info("Finishing after writing %" PRIu64 " entries", s.event_count);
1180
1181 return 0;
1182 }
1183
1184 DEFINE_MAIN_FUNCTION(run);