1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "conf-parser.h"
10 #include "constants.h"
11 #include "daemon-util.h"
14 #include "journal-remote-write.h"
15 #include "journal-remote.h"
16 #include "main-func.h"
17 #include "memory-util.h"
18 #include "parse-argument.h"
19 #include "parse-helpers.h"
20 #include "pretty-print.h"
21 #include "process-util.h"
22 #include "rlimit-util.h"
24 #include "signal-util.h"
25 #include "socket-netlink.h"
26 #include "socket-util.h"
27 #include "stat-util.h"
28 #include "string-table.h"
31 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
32 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
33 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
35 static const char* arg_url
= NULL
;
36 static const char* arg_getter
= NULL
;
37 static const char* arg_listen_raw
= NULL
;
38 static const char* arg_listen_http
= NULL
;
39 static const char* arg_listen_https
= NULL
;
40 static char** arg_files
= NULL
; /* Do not free this. */
41 static bool arg_compress
= true;
42 static bool arg_seal
= false;
43 static int http_socket
= -1, https_socket
= -1;
44 static char** arg_gnutls_log
= NULL
;
46 static JournalWriteSplitMode arg_split_mode
= _JOURNAL_WRITE_SPLIT_INVALID
;
47 static char *arg_output
= NULL
;
49 static char *arg_key
= NULL
;
50 static char *arg_cert
= NULL
;
51 static char *arg_trust
= NULL
;
53 static bool arg_trust_all
= false;
55 static bool arg_trust_all
= true;
58 static uint64_t arg_max_use
= UINT64_MAX
;
59 static uint64_t arg_max_size
= UINT64_MAX
;
60 static uint64_t arg_n_max_files
= UINT64_MAX
;
61 static uint64_t arg_keep_free
= UINT64_MAX
;
63 STATIC_DESTRUCTOR_REGISTER(arg_gnutls_log
, strv_freep
);
64 STATIC_DESTRUCTOR_REGISTER(arg_key
, freep
);
65 STATIC_DESTRUCTOR_REGISTER(arg_cert
, freep
);
66 STATIC_DESTRUCTOR_REGISTER(arg_trust
, freep
);
67 STATIC_DESTRUCTOR_REGISTER(arg_output
, freep
);
69 static const char* const journal_write_split_mode_table
[_JOURNAL_WRITE_SPLIT_MAX
] = {
70 [JOURNAL_WRITE_SPLIT_NONE
] = "none",
71 [JOURNAL_WRITE_SPLIT_HOST
] = "host",
74 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode
, JournalWriteSplitMode
);
75 static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode
,
76 journal_write_split_mode
,
77 JournalWriteSplitMode
,
78 "Failed to parse split mode setting");
80 /**********************************************************************
81 **********************************************************************
82 **********************************************************************/
84 static int spawn_child(const char* child
, char** argv
) {
89 return log_error_errno(errno
, "Failed to create pager pipe: %m");
91 r
= safe_fork_full("(remote)",
92 (int[]) {STDIN_FILENO
, fd
[1], STDERR_FILENO
},
94 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG_SIGTERM
|FORK_REARRANGE_STDIO
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
, &child_pid
);
103 log_error_errno(errno
, "Failed to exec child %s: %m", child
);
109 r
= fd_nonblock(fd
[0], true);
111 log_warning_errno(errno
, "Failed to set child pipe to non-blocking: %m");
116 static int spawn_curl(const char* url
) {
117 char **argv
= STRV_MAKE("curl",
118 "-HAccept: application/vnd.fdo.journal",
124 r
= spawn_child("curl", argv
);
126 log_error_errno(r
, "Failed to spawn curl: %m");
130 static int spawn_getter(const char *getter
) {
132 _cleanup_strv_free_
char **words
= NULL
;
135 r
= strv_split_full(&words
, getter
, WHITESPACE
, EXTRACT_UNQUOTE
);
137 return log_error_errno(r
, "Failed to split getter option: %m");
139 r
= spawn_child(words
[0], words
);
141 log_error_errno(r
, "Failed to spawn getter %s: %m", getter
);
146 /**********************************************************************
147 **********************************************************************
148 **********************************************************************/
150 static int null_timer_event_handler(sd_event_source
*s
,
153 static int dispatch_http_event(sd_event_source
*event
,
158 static int request_meta(void **connection_cls
, int fd
, char *hostname
) {
159 RemoteSource
*source
;
163 assert(connection_cls
);
167 r
= journal_remote_get_writer(journal_remote_server_global
, hostname
, &writer
);
169 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
172 source
= source_new(fd
, true, hostname
, writer
);
174 writer_unref(writer
);
178 log_debug("Added RemoteSource as connection metadata %p", source
);
180 *connection_cls
= source
;
184 static void request_meta_free(void *cls
,
185 struct MHD_Connection
*connection
,
186 void **connection_cls
,
187 enum MHD_RequestTerminationCode toe
) {
190 assert(connection_cls
);
194 log_debug("Cleaning up connection metadata %p", s
);
196 *connection_cls
= NULL
;
200 static int process_http_upload(
201 struct MHD_Connection
*connection
,
202 const char *upload_data
,
203 size_t *upload_data_size
,
204 RemoteSource
*source
) {
206 bool finished
= false;
212 log_trace("%s: connection %p, %zu bytes",
213 __func__
, connection
, *upload_data_size
);
215 if (*upload_data_size
) {
216 log_trace("Received %zu bytes", *upload_data_size
);
218 r
= journal_importer_push_data(&source
->importer
,
219 upload_data
, *upload_data_size
);
221 return mhd_respond_oom(connection
);
223 *upload_data_size
= 0;
228 r
= process_source(source
, journal_remote_server_global
->file_flags
);
233 log_warning_errno(r
, "Entry is above the maximum of %u, aborting connection %p.",
234 DATA_SIZE_MAX
, connection
);
235 else if (r
== -E2BIG
)
236 log_warning_errno(r
, "Entry with more fields than the maximum of %u, aborting connection %p.",
237 ENTRY_FIELD_COUNT_MAX
, connection
);
239 log_warning_errno(r
, "Failed to process data, aborting connection %p: %m",
248 /* The upload is finished */
250 remaining
= journal_importer_bytes_remaining(&source
->importer
);
252 log_warning("Premature EOF byte. %zu bytes lost.", remaining
);
253 return mhd_respondf(connection
,
254 0, MHD_HTTP_EXPECTATION_FAILED
,
255 "Premature EOF. %zu bytes of trailing data not processed.",
259 return mhd_respond(connection
, MHD_HTTP_ACCEPTED
, "OK.");
262 static mhd_result
request_handler(
264 struct MHD_Connection
*connection
,
268 const char *upload_data
,
269 size_t *upload_data_size
,
270 void **connection_cls
) {
274 _cleanup_free_
char *hostname
= NULL
;
275 bool chunked
= false;
278 assert(connection_cls
);
282 log_trace("Handling a connection %s %s %s", method
, url
, version
);
285 return process_http_upload(connection
,
286 upload_data
, upload_data_size
,
289 if (!streq(method
, "POST"))
290 return mhd_respond(connection
, MHD_HTTP_NOT_ACCEPTABLE
, "Unsupported method.");
292 if (!streq(url
, "/upload"))
293 return mhd_respond(connection
, MHD_HTTP_NOT_FOUND
, "Not found.");
295 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Type");
296 if (!header
|| !streq(header
, "application/vnd.fdo.journal"))
297 return mhd_respond(connection
, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE
,
298 "Content-Type: application/vnd.fdo.journal is required.");
300 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Transfer-Encoding");
302 if (!strcaseeq(header
, "chunked"))
303 return mhd_respondf(connection
, 0, MHD_HTTP_BAD_REQUEST
,
304 "Unsupported Transfer-Encoding type: %s", header
);
309 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Length");
314 return mhd_respond(connection
, MHD_HTTP_BAD_REQUEST
,
315 "Content-Length not allowed when Transfer-Encoding type is 'chunked'");
317 r
= safe_atozu(header
, &len
);
319 return mhd_respondf(connection
, r
, MHD_HTTP_LENGTH_REQUIRED
,
320 "Content-Length: %s cannot be parsed: %m", header
);
322 if (len
> ENTRY_SIZE_MAX
)
323 /* When serialized, an entry of maximum size might be slightly larger,
324 * so this does not correspond exactly to the limit in journald. Oh well.
326 return mhd_respondf(connection
, 0, MHD_HTTP_CONTENT_TOO_LARGE
,
327 "Payload larger than maximum size of %u bytes", ENTRY_SIZE_MAX
);
331 const union MHD_ConnectionInfo
*ci
;
333 ci
= MHD_get_connection_info(connection
,
334 MHD_CONNECTION_INFO_CONNECTION_FD
);
336 log_error("MHD_get_connection_info failed: cannot get remote fd");
337 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
338 "Cannot check remote address.");
345 if (journal_remote_server_global
->check_trust
) {
346 r
= check_permissions(connection
, &code
, &hostname
);
350 r
= getpeername_pretty(fd
, false, &hostname
);
352 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
353 "Cannot check remote hostname.");
358 r
= request_meta(connection_cls
, fd
, hostname
);
360 return respond_oom(connection
);
362 return mhd_respondf(connection
, r
, MHD_HTTP_INTERNAL_SERVER_ERROR
, "%m");
368 static int setup_microhttpd_server(RemoteServer
*s
,
373 struct MHD_OptionItem opts
[] = {
374 { MHD_OPTION_EXTERNAL_LOGGER
, (intptr_t) microhttpd_logger
},
375 { MHD_OPTION_NOTIFY_COMPLETED
, (intptr_t) request_meta_free
},
376 { MHD_OPTION_LISTEN_SOCKET
, fd
},
377 { MHD_OPTION_CONNECTION_MEMORY_LIMIT
, JOURNAL_SERVER_MEMORY_MAX
},
390 _cleanup_(MHDDaemonWrapper_freep
) MHDDaemonWrapper
*d
= NULL
;
391 const union MHD_DaemonInfo
*info
;
396 r
= fd_nonblock(fd
, true);
398 return log_error_errno(r
, "Failed to make fd:%d nonblocking: %m", fd
);
400 /* MHD_OPTION_STRICT_FOR_CLIENT is introduced in microhttpd 0.9.54,
401 * and MHD_USE_PEDANTIC_CHECKS will be deprecated in future.
402 * If MHD_USE_PEDANTIC_CHECKS is '#define'd, then it is deprecated
403 * and we should use MHD_OPTION_STRICT_FOR_CLIENT. On the other hand,
404 * if MHD_USE_PEDANTIC_CHECKS is not '#define'd, then it is not
405 * deprecated yet and there exists an enum element with the same name.
406 * So we can safely use it. */
407 #ifdef MHD_USE_PEDANTIC_CHECKS
408 opts
[opts_pos
++] = (struct MHD_OptionItem
)
409 {MHD_OPTION_STRICT_FOR_CLIENT
, 1};
411 flags
|= MHD_USE_PEDANTIC_CHECKS
;
417 opts
[opts_pos
++] = (struct MHD_OptionItem
)
418 {MHD_OPTION_HTTPS_MEM_KEY
, 0, (char*) key
};
419 opts
[opts_pos
++] = (struct MHD_OptionItem
)
420 {MHD_OPTION_HTTPS_MEM_CERT
, 0, (char*) cert
};
422 flags
|= MHD_USE_TLS
;
425 opts
[opts_pos
++] = (struct MHD_OptionItem
)
426 {MHD_OPTION_HTTPS_MEM_TRUST
, 0, (char*) trust
};
429 d
= new(MHDDaemonWrapper
, 1);
433 d
->fd
= (uint64_t) fd
;
435 d
->daemon
= MHD_start_daemon(flags
, 0,
437 request_handler
, NULL
,
438 MHD_OPTION_ARRAY
, opts
,
441 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to start μhttp daemon");
443 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
444 key
? "HTTPS" : "HTTP", fd
, d
);
446 info
= MHD_get_daemon_info(d
->daemon
, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY
);
448 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "μhttp returned NULL daemon info");
450 epoll_fd
= info
->listen_fd
;
452 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
), "μhttp epoll fd is invalid");
454 r
= sd_event_add_io(s
->event
, &d
->io_event
,
456 dispatch_http_event
, d
);
458 return log_error_errno(r
, "Failed to add event callback: %m");
460 r
= sd_event_source_set_description(d
->io_event
, "io_event");
462 return log_error_errno(r
, "Failed to set source name: %m");
464 r
= sd_event_add_time(s
->event
, &d
->timer_event
,
465 CLOCK_MONOTONIC
, UINT64_MAX
, 0,
466 null_timer_event_handler
, d
);
468 return log_error_errno(r
, "Failed to add timer_event: %m");
470 r
= sd_event_source_set_description(d
->timer_event
, "timer_event");
472 return log_error_errno(r
, "Failed to set source name: %m");
474 r
= hashmap_ensure_put(&s
->daemons
, &uint64_hash_ops
, &d
->fd
, d
);
478 return log_error_errno(r
, "Failed to add daemon to hashmap: %m");
485 static int setup_microhttpd_socket(RemoteServer
*s
,
492 fd
= make_socket_fd(LOG_DEBUG
, address
, SOCK_STREAM
, SOCK_CLOEXEC
);
496 return setup_microhttpd_server(s
, fd
, key
, cert
, trust
);
499 static int null_timer_event_handler(sd_event_source
*timer_event
,
502 return dispatch_http_event(timer_event
, 0, 0, userdata
);
505 static int dispatch_http_event(sd_event_source
*event
,
509 MHDDaemonWrapper
*d
= ASSERT_PTR(userdata
);
511 MHD_UNSIGNED_LONG_LONG timeout
= ULLONG_MAX
;
513 r
= MHD_run(d
->daemon
);
515 // FIXME: unregister daemon
516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
518 if (MHD_get_timeout(d
->daemon
, &timeout
) == MHD_NO
)
519 timeout
= ULLONG_MAX
;
521 r
= sd_event_source_set_time(d
->timer_event
, timeout
);
523 log_warning_errno(r
, "Unable to set event loop timeout: %m, this may result in indefinite blocking!");
527 r
= sd_event_source_set_enabled(d
->timer_event
, SD_EVENT_ON
);
529 log_warning_errno(r
, "Unable to enable timer_event: %m, this may result in indefinite blocking!");
531 return 1; /* work to do */
534 /**********************************************************************
535 **********************************************************************
536 **********************************************************************/
538 static int setup_raw_socket(RemoteServer
*s
, const char *address
) {
541 fd
= make_socket_fd(LOG_INFO
, address
, SOCK_STREAM
, SOCK_CLOEXEC
);
545 return journal_remote_add_raw_socket(s
, fd
);
548 static int create_remoteserver(
556 r
= journal_remote_server_init(
560 (arg_compress
? JOURNAL_COMPRESS
: 0) |
561 (arg_seal
? JOURNAL_SEAL
: 0));
565 r
= sd_event_set_signal_exit(s
->event
, true);
567 return log_error_errno(r
, "Failed to install SIGINT/SIGTERM handlers: %m");
569 n
= sd_listen_fds(true);
571 return log_error_errno(n
, "Failed to read listening file descriptors from environment: %m");
573 log_debug("Received %d descriptors", n
);
575 if (MAX(http_socket
, https_socket
) >= SD_LISTEN_FDS_START
+ n
)
576 return log_error_errno(SYNTHETIC_ERRNO(EBADFD
),
577 "Received fewer sockets than expected");
579 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
580 if (sd_is_socket(fd
, AF_UNSPEC
, 0, true)) {
581 log_debug("Received a listening socket (fd:%d)", fd
);
583 if (fd
== http_socket
)
584 r
= setup_microhttpd_server(s
, fd
, NULL
, NULL
, NULL
);
585 else if (fd
== https_socket
)
586 r
= setup_microhttpd_server(s
, fd
, key
, cert
, trust
);
588 r
= journal_remote_add_raw_socket(s
, fd
);
589 } else if (sd_is_socket(fd
, AF_UNSPEC
, 0, false)) {
592 r
= getpeername_pretty(fd
, false, &hostname
);
594 return log_error_errno(r
, "Failed to retrieve remote name: %m");
596 log_debug("Received a connection socket (fd:%d) from %s", fd
, hostname
);
598 r
= journal_remote_add_source(s
, fd
, hostname
, true);
600 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
601 "Unknown socket passed on fd:%d", fd
);
604 return log_error_errno(r
, "Failed to register socket (fd:%d): %m", fd
);
608 log_info("Spawning getter %s...", arg_getter
);
609 fd
= spawn_getter(arg_getter
);
613 r
= journal_remote_add_source(s
, fd
, (char*) arg_output
, false);
619 const char *url
, *hostname
;
621 if (!strstr(arg_url
, "/entries")) {
622 if (endswith(arg_url
, "/"))
623 url
= strjoina(arg_url
, "entries");
625 url
= strjoina(arg_url
, "/entries");
627 url
= strdupa_safe(arg_url
);
629 log_info("Spawning curl %s...", url
);
630 fd
= spawn_curl(url
);
634 hostname
= STARTSWITH_SET(arg_url
, "https://", "http://");
638 hostname
= strndupa_safe(hostname
, strcspn(hostname
, "/:"));
640 r
= journal_remote_add_source(s
, fd
, (char *) hostname
, false);
645 if (arg_listen_raw
) {
646 log_debug("Listening on a socket...");
647 r
= setup_raw_socket(s
, arg_listen_raw
);
652 if (arg_listen_http
) {
653 r
= setup_microhttpd_socket(s
, arg_listen_http
, NULL
, NULL
, NULL
);
658 if (arg_listen_https
) {
659 r
= setup_microhttpd_socket(s
, arg_listen_https
, key
, cert
, trust
);
664 STRV_FOREACH(file
, arg_files
) {
665 const char *output_name
;
667 if (streq(*file
, "-")) {
668 log_debug("Using standard input as source.");
671 output_name
= "stdin";
673 log_debug("Reading file %s...", *file
);
675 fd
= open(*file
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
677 return log_error_errno(errno
, "Failed to open %s: %m", *file
);
681 r
= journal_remote_add_source(s
, fd
, (char*) output_name
, false);
687 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
688 "Zero sources specified");
690 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_NONE
) {
691 /* In this case we know what the writer will be
692 called, so we can create it and verify that we can
693 create output as expected. */
694 r
= journal_remote_get_writer(s
, NULL
, &s
->_single_writer
);
696 return log_warning_errno(r
, "Failed to get writer: %m");
702 static int negative_fd(const char *spec
) {
703 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
707 r
= safe_atoi(spec
, &fd
);
717 static int parse_config(void) {
718 const ConfigTableItem items
[] = {
719 { "Remote", "Seal", config_parse_bool
, 0, &arg_seal
},
720 { "Remote", "SplitMode", config_parse_write_split_mode
, 0, &arg_split_mode
},
721 { "Remote", "ServerKeyFile", config_parse_path
, 0, &arg_key
},
722 { "Remote", "ServerCertificateFile", config_parse_path
, 0, &arg_cert
},
723 { "Remote", "TrustedCertificateFile", config_parse_path_or_ignore
, 0, &arg_trust
},
724 { "Remote", "MaxUse", config_parse_iec_uint64
, 0, &arg_max_use
},
725 { "Remote", "MaxFileSize", config_parse_iec_uint64
, 0, &arg_max_size
},
726 { "Remote", "MaxFiles", config_parse_uint64
, 0, &arg_n_max_files
},
727 { "Remote", "KeepFree", config_parse_iec_uint64
, 0, &arg_keep_free
},
731 return config_parse_standard_file_with_dropins(
732 "systemd/journal-remote.conf",
734 config_item_table_lookup
, items
,
736 /* userdata= */ NULL
);
739 static int help(void) {
740 _cleanup_free_
char *link
= NULL
;
743 r
= terminal_urlify_man("systemd-journal-remote.service", "8", &link
);
747 printf("%s [OPTIONS...] {FILE|-}...\n\n"
748 "Write external journal events to journal file(s).\n\n"
749 " -h --help Show this help\n"
750 " --version Show package version\n"
751 " --url=URL Read events from systemd-journal-gatewayd at URL\n"
752 " --getter=COMMAND Read events from the output of COMMAND\n"
753 " --listen-raw=ADDR Listen for connections at ADDR\n"
754 " --listen-http=ADDR Listen for HTTP connections at ADDR\n"
755 " --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
756 " -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
757 " --compress[=BOOL] Use compression in the output journal (default: yes)\n"
758 " --seal[=BOOL] Use event sealing (default: no)\n"
759 " --key=FILENAME SSL key in PEM format (default:\n"
760 " \"" PRIV_KEY_FILE
"\")\n"
761 " --cert=FILENAME SSL certificate in PEM format (default:\n"
762 " \"" CERT_FILE
"\")\n"
763 " --trust=FILENAME|all SSL CA certificate or disable checking (default:\n"
764 " \"" TRUST_FILE
"\")\n"
765 " --gnutls-log=CATEGORY...\n"
766 " Specify a list of gnutls logging categories\n"
767 " --split-mode=none|host How many output files to create\n"
768 "\nNote: file descriptors from sd_listen_fds() will be consumed, too.\n"
769 "\nSee the %s for details.\n",
770 program_invocation_short_name
,
776 static int parse_argv(int argc
, char *argv
[]) {
793 static const struct option options
[] = {
794 { "help", no_argument
, NULL
, 'h' },
795 { "version", no_argument
, NULL
, ARG_VERSION
},
796 { "url", required_argument
, NULL
, ARG_URL
},
797 { "getter", required_argument
, NULL
, ARG_GETTER
},
798 { "listen-raw", required_argument
, NULL
, ARG_LISTEN_RAW
},
799 { "listen-http", required_argument
, NULL
, ARG_LISTEN_HTTP
},
800 { "listen-https", required_argument
, NULL
, ARG_LISTEN_HTTPS
},
801 { "output", required_argument
, NULL
, 'o' },
802 { "split-mode", required_argument
, NULL
, ARG_SPLIT_MODE
},
803 { "compress", optional_argument
, NULL
, ARG_COMPRESS
},
804 { "seal", optional_argument
, NULL
, ARG_SEAL
},
805 { "key", required_argument
, NULL
, ARG_KEY
},
806 { "cert", required_argument
, NULL
, ARG_CERT
},
807 { "trust", required_argument
, NULL
, ARG_TRUST
},
808 { "gnutls-log", required_argument
, NULL
, ARG_GNUTLS_LOG
},
818 while ((c
= getopt_long(argc
, argv
, "ho:", options
, NULL
)) >= 0)
829 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
830 "Cannot currently set more than one --url=");
837 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
838 "Cannot currently use --getter= more than once");
845 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
846 "Cannot currently use --listen-raw= more than once");
848 arg_listen_raw
= optarg
;
851 case ARG_LISTEN_HTTP
:
852 if (arg_listen_http
|| http_socket
>= 0)
853 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
854 "Cannot currently use --listen-http= more than once");
856 r
= negative_fd(optarg
);
860 arg_listen_http
= optarg
;
863 case ARG_LISTEN_HTTPS
:
864 if (arg_listen_https
|| https_socket
>= 0)
865 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
866 "Cannot currently use --listen-https= more than once");
868 r
= negative_fd(optarg
);
872 arg_listen_https
= optarg
;
878 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
879 "Key file specified twice");
881 arg_key
= strdup(optarg
);
889 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
890 "Certificate file specified twice");
892 arg_cert
= strdup(optarg
);
901 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
902 "Cannot use --trust more= than once");
904 arg_trust
= strdup(optarg
);
908 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
909 "Option --trust= is not available.");
915 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
916 "Cannot use --output=/-o more than once");
918 r
= parse_path_argument(optarg
, /* suppress_root = */ false, &arg_output
);
924 arg_split_mode
= journal_write_split_mode_from_string(optarg
);
925 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
926 return log_error_errno(arg_split_mode
, "Invalid split mode: %s", optarg
);
930 r
= parse_boolean_argument("--compress", optarg
, &arg_compress
);
936 r
= parse_boolean_argument("--seal", optarg
, &arg_seal
);
943 for (const char* p
= optarg
;;) {
944 _cleanup_free_
char *word
= NULL
;
946 r
= extract_first_word(&p
, &word
, ",", 0);
948 return log_error_errno(r
, "Failed to parse --gnutls-log= argument: %m");
952 if (strv_push(&arg_gnutls_log
, word
) < 0)
959 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
960 "Option --gnutls-log= is not available.");
967 assert_not_reached();
971 arg_files
= argv
+ optind
;
973 type_a
= arg_getter
|| !strv_isempty(arg_files
);
976 || arg_listen_http
|| arg_listen_https
977 || sd_listen_fds(false) > 0;
978 if (type_a
&& type_b
)
979 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
980 "Cannot use file input or --getter= with "
981 "--listen-...= or socket activation.");
984 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
985 "Option --output= must be specified with file input or --getter=.");
987 if (!IN_SET(arg_split_mode
, JOURNAL_WRITE_SPLIT_NONE
, _JOURNAL_WRITE_SPLIT_INVALID
))
988 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
989 "For active sources, only --split-mode=none is allowed.");
991 arg_split_mode
= JOURNAL_WRITE_SPLIT_NONE
;
994 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
995 arg_split_mode
= JOURNAL_WRITE_SPLIT_HOST
;
997 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_NONE
&& arg_output
) {
998 if (is_dir(arg_output
, true) > 0)
999 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1000 "For SplitMode=none, output must be a file.");
1001 if (!endswith(arg_output
, ".journal"))
1002 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1003 "For SplitMode=none, output file name must end with .journal.");
1006 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_HOST
1007 && arg_output
&& is_dir(arg_output
, true) <= 0)
1008 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1009 "For SplitMode=host, output must be a directory.");
1011 if (STRPTR_IN_SET(arg_trust
, "-", "all")) {
1012 arg_trust_all
= true;
1013 arg_trust
= mfree(arg_trust
);
1016 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1017 journal_write_split_mode_to_string(arg_split_mode
),
1022 return 1 /* work to do */;
1025 static int load_certificates(char **key
, char **cert
, char **trust
) {
1028 r
= read_full_file_full(
1029 AT_FDCWD
, arg_key
?: PRIV_KEY_FILE
, UINT64_MAX
, SIZE_MAX
,
1030 READ_FULL_FILE_SECURE
|READ_FULL_FILE_WARN_WORLD_READABLE
|READ_FULL_FILE_CONNECT_SOCKET
,
1034 return log_error_errno(r
, "Failed to read key from file '%s': %m",
1035 arg_key
?: PRIV_KEY_FILE
);
1037 r
= read_full_file_full(
1038 AT_FDCWD
, arg_cert
?: CERT_FILE
, UINT64_MAX
, SIZE_MAX
,
1039 READ_FULL_FILE_CONNECT_SOCKET
,
1043 return log_error_errno(r
, "Failed to read certificate from file '%s': %m",
1044 arg_cert
?: CERT_FILE
);
1047 log_info("Certificate checking disabled.");
1049 r
= read_full_file_full(
1050 AT_FDCWD
, arg_trust
?: TRUST_FILE
, UINT64_MAX
, SIZE_MAX
,
1051 READ_FULL_FILE_CONNECT_SOCKET
,
1055 return log_error_errno(r
, "Failed to read CA certificate file '%s': %m",
1056 arg_trust
?: TRUST_FILE
);
1059 if ((arg_listen_raw
|| arg_listen_http
) && *trust
)
1060 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1061 "Option --trust= makes all non-HTTPS connections untrusted.");
1066 static int run(int argc
, char **argv
) {
1067 _cleanup_(journal_remote_server_destroy
) RemoteServer s
= {};
1068 _unused_
_cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
1069 _cleanup_(erase_and_freep
) char *key
= NULL
;
1070 _cleanup_free_
char *cert
= NULL
, *trust
= NULL
;
1075 /* The journal merging logic potentially needs a lot of fds. */
1076 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1084 r
= parse_argv(argc
, argv
);
1088 if (arg_listen_http
|| arg_listen_https
) {
1089 r
= setup_gnutls_logger(arg_gnutls_log
);
1094 if (arg_listen_https
|| https_socket
>= 0) {
1095 r
= load_certificates(&key
, &cert
, &trust
);
1099 s
.check_trust
= !arg_trust_all
;
1102 journal_reset_metrics(&s
.metrics
);
1103 s
.metrics
.max_use
= arg_max_use
;
1104 s
.metrics
.max_size
= arg_max_size
;
1105 s
.metrics
.keep_free
= arg_keep_free
;
1106 s
.metrics
.n_max_files
= arg_n_max_files
;
1108 r
= create_remoteserver(&s
, key
, cert
, trust
);
1112 r
= sd_event_set_watchdog(s
.event
, true);
1114 return log_error_errno(r
, "Failed to enable watchdog: %m");
1116 log_debug("Watchdog is %sd.", enable_disable(r
> 0));
1118 log_debug("%s running as pid "PID_FMT
,
1119 program_invocation_short_name
, getpid_cached());
1121 notify_message
= notify_start(NOTIFY_READY
, NOTIFY_STOPPING
);
1124 r
= sd_event_get_state(s
.event
);
1127 if (r
== SD_EVENT_FINISHED
)
1130 r
= sd_event_run(s
.event
, -1);
1132 return log_error_errno(r
, "Failed to run event loop: %m");
1135 notify_message
= NULL
;
1136 (void) sd_notifyf(false,
1138 "STATUS=Shutting down after writing %" PRIu64
" entries...", s
.event_count
);
1140 log_info("Finishing after writing %" PRIu64
" entries", s
.event_count
);
1145 DEFINE_MAIN_FUNCTION(run
);