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