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