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