1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "conf-parser.h"
9 #include "daemon-util.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"
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"
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
;
43 static JournalWriteSplitMode arg_split_mode
= _JOURNAL_WRITE_SPLIT_INVALID
;
44 static const char* arg_output
= NULL
;
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;
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
);
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",
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");
67 /**********************************************************************
68 **********************************************************************
69 **********************************************************************/
71 static int spawn_child(const char* child
, char** argv
) {
76 return log_error_errno(errno
, "Failed to create pager pipe: %m");
78 r
= safe_fork("(remote)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &child_pid
);
86 fd
[0] = safe_close(fd
[0]);
88 r
= rearrange_stdio(STDIN_FILENO
, TAKE_FD(fd
[1]), STDERR_FILENO
);
90 log_error_errno(r
, "Failed to dup pipe to stdout: %m");
94 (void) rlimit_nofile_safe();
97 log_error_errno(errno
, "Failed to exec child %s: %m", child
);
103 r
= fd_nonblock(fd
[0], true);
105 log_warning_errno(errno
, "Failed to set child pipe to non-blocking: %m");
110 static int spawn_curl(const char* url
) {
111 char **argv
= STRV_MAKE("curl",
112 "-HAccept: application/vnd.fdo.journal",
118 r
= spawn_child("curl", argv
);
120 log_error_errno(r
, "Failed to spawn curl: %m");
124 static int spawn_getter(const char *getter
) {
126 _cleanup_strv_free_
char **words
= NULL
;
129 r
= strv_split_full(&words
, getter
, WHITESPACE
, EXTRACT_UNQUOTE
);
131 return log_error_errno(r
, "Failed to split getter option: %m");
133 r
= spawn_child(words
[0], words
);
135 log_error_errno(r
, "Failed to spawn getter %s: %m", getter
);
140 /**********************************************************************
141 **********************************************************************
142 **********************************************************************/
144 static int null_timer_event_handler(sd_event_source
*s
,
147 static int dispatch_http_event(sd_event_source
*event
,
152 static int request_meta(void **connection_cls
, int fd
, char *hostname
) {
153 RemoteSource
*source
;
157 assert(connection_cls
);
161 r
= journal_remote_get_writer(journal_remote_server_global
, hostname
, &writer
);
163 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
166 source
= source_new(fd
, true, hostname
, writer
);
168 writer_unref(writer
);
172 log_debug("Added RemoteSource as connection metadata %p", source
);
174 *connection_cls
= source
;
178 static void request_meta_free(void *cls
,
179 struct MHD_Connection
*connection
,
180 void **connection_cls
,
181 enum MHD_RequestTerminationCode toe
) {
184 assert(connection_cls
);
188 log_debug("Cleaning up connection metadata %p", s
);
190 *connection_cls
= NULL
;
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
) {
200 bool finished
= false;
206 log_trace("%s: connection %p, %zu bytes",
207 __func__
, connection
, *upload_data_size
);
209 if (*upload_data_size
) {
210 log_trace("Received %zu bytes", *upload_data_size
);
212 r
= journal_importer_push_data(&source
->importer
,
213 upload_data
, *upload_data_size
);
215 return mhd_respond_oom(connection
);
217 *upload_data_size
= 0;
222 r
= process_source(source
,
223 journal_remote_server_global
->compress
,
224 journal_remote_server_global
->seal
);
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
);
235 log_warning_errno(r
, "Failed to process data, aborting connection %p: %m",
244 /* The upload is finished */
246 remaining
= journal_importer_bytes_remaining(&source
->importer
);
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.",
255 return mhd_respond(connection
, MHD_HTTP_ACCEPTED
, "OK.");
258 static mhd_result
request_handler(
260 struct MHD_Connection
*connection
,
264 const char *upload_data
,
265 size_t *upload_data_size
,
266 void **connection_cls
) {
270 _cleanup_free_
char *hostname
= NULL
;
271 bool chunked
= false;
274 assert(connection_cls
);
278 log_trace("Handling a connection %s %s %s", method
, url
, version
);
281 return process_http_upload(connection
,
282 upload_data
, upload_data_size
,
285 if (!streq(method
, "POST"))
286 return mhd_respond(connection
, MHD_HTTP_NOT_ACCEPTABLE
, "Unsupported method.");
288 if (!streq(url
, "/upload"))
289 return mhd_respond(connection
, MHD_HTTP_NOT_FOUND
, "Not found.");
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.");
296 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Transfer-Encoding");
298 if (!strcaseeq(header
, "chunked"))
299 return mhd_respondf(connection
, 0, MHD_HTTP_BAD_REQUEST
,
300 "Unsupported Transfer-Encoding type: %s", header
);
305 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Length");
310 return mhd_respond(connection
, MHD_HTTP_BAD_REQUEST
,
311 "Content-Length must not specified when Transfer-Encoding type is 'chuncked'");
313 r
= safe_atozu(header
, &len
);
315 return mhd_respondf(connection
, r
, MHD_HTTP_LENGTH_REQUIRED
,
316 "Content-Length: %s cannot be parsed: %m", header
);
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.
322 return mhd_respondf(connection
, 0, MHD_HTTP_CONTENT_TOO_LARGE
,
323 "Payload larger than maximum size of %u bytes", ENTRY_SIZE_MAX
);
327 const union MHD_ConnectionInfo
*ci
;
329 ci
= MHD_get_connection_info(connection
,
330 MHD_CONNECTION_INFO_CONNECTION_FD
);
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.");
341 if (journal_remote_server_global
->check_trust
) {
342 r
= check_permissions(connection
, &code
, &hostname
);
346 r
= getpeername_pretty(fd
, false, &hostname
);
348 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
349 "Cannot check remote hostname.");
354 r
= request_meta(connection_cls
, fd
, hostname
);
356 return respond_oom(connection
);
358 return mhd_respondf(connection
, r
, MHD_HTTP_INTERNAL_SERVER_ERROR
, "%m");
364 static int setup_microhttpd_server(RemoteServer
*s
,
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},
386 const union MHD_DaemonInfo
*info
;
392 r
= fd_nonblock(fd
, true);
394 return log_error_errno(r
, "Failed to make fd:%d nonblocking: %m", fd
);
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};
407 flags
|= MHD_USE_PEDANTIC_CHECKS
;
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
};
418 flags
|= MHD_USE_TLS
;
421 opts
[opts_pos
++] = (struct MHD_OptionItem
)
422 {MHD_OPTION_HTTPS_MEM_TRUST
, 0, (char*) trust
};
425 d
= new(MHDDaemonWrapper
, 1);
429 d
->fd
= (uint64_t) fd
;
431 d
->daemon
= MHD_start_daemon(flags
, 0,
433 request_handler
, NULL
,
434 MHD_OPTION_ARRAY
, opts
,
437 log_error("Failed to start µhttp daemon");
442 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
443 key
? "HTTPS" : "HTTP", fd
, d
);
445 info
= MHD_get_daemon_info(d
->daemon
, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY
);
447 log_error("µhttp returned NULL daemon info");
452 epoll_fd
= info
->listen_fd
;
454 log_error("µhttp epoll fd is invalid");
459 r
= sd_event_add_io(s
->events
, &d
->io_event
,
461 dispatch_http_event
, d
);
463 log_error_errno(r
, "Failed to add event callback: %m");
467 r
= sd_event_source_set_description(d
->io_event
, "io_event");
469 log_error_errno(r
, "Failed to set source name: %m");
473 r
= sd_event_add_time(s
->events
, &d
->timer_event
,
474 CLOCK_MONOTONIC
, UINT64_MAX
, 0,
475 null_timer_event_handler
, d
);
477 log_error_errno(r
, "Failed to add timer_event: %m");
481 r
= sd_event_source_set_description(d
->timer_event
, "timer_event");
483 log_error_errno(r
, "Failed to set source name: %m");
487 r
= hashmap_ensure_put(&s
->daemons
, &uint64_hash_ops
, &d
->fd
, d
);
493 log_error_errno(r
, "Failed to add daemon to hashmap: %m");
501 MHD_stop_daemon(d
->daemon
);
507 static int setup_microhttpd_socket(RemoteServer
*s
,
514 fd
= make_socket_fd(LOG_DEBUG
, address
, SOCK_STREAM
, SOCK_CLOEXEC
);
518 return setup_microhttpd_server(s
, fd
, key
, cert
, trust
);
521 static int null_timer_event_handler(sd_event_source
*timer_event
,
524 return dispatch_http_event(timer_event
, 0, 0, userdata
);
527 static int dispatch_http_event(sd_event_source
*event
,
531 MHDDaemonWrapper
*d
= userdata
;
533 MHD_UNSIGNED_LONG_LONG timeout
= ULLONG_MAX
;
537 r
= MHD_run(d
->daemon
);
539 // FIXME: unregister daemon
540 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
542 if (MHD_get_timeout(d
->daemon
, &timeout
) == MHD_NO
)
543 timeout
= ULLONG_MAX
;
545 r
= sd_event_source_set_time(d
->timer_event
, timeout
);
547 log_warning_errno(r
, "Unable to set event loop timeout: %m, this may result in indefinite blocking!");
551 r
= sd_event_source_set_enabled(d
->timer_event
, SD_EVENT_ON
);
553 log_warning_errno(r
, "Unable to enable timer_event: %m, this may result in indefinite blocking!");
555 return 1; /* work to do */
558 /**********************************************************************
559 **********************************************************************
560 **********************************************************************/
562 static int setup_signals(RemoteServer
*s
) {
567 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, -1) >= 0);
569 r
= sd_event_add_signal(s
->events
, &s
->sigterm_event
, SIGTERM
, NULL
, s
);
573 r
= sd_event_add_signal(s
->events
, &s
->sigint_event
, SIGINT
, NULL
, s
);
580 static int setup_raw_socket(RemoteServer
*s
, const char *address
) {
583 fd
= make_socket_fd(LOG_INFO
, address
, SOCK_STREAM
, SOCK_CLOEXEC
);
587 return journal_remote_add_raw_socket(s
, fd
);
590 static int create_remoteserver(
598 r
= journal_remote_server_init(s
, arg_output
, arg_split_mode
, arg_compress
, arg_seal
);
602 r
= setup_signals(s
);
604 return log_error_errno(r
, "Failed to set up signals: %m");
606 n
= sd_listen_fds(true);
608 return log_error_errno(n
, "Failed to read listening file descriptors from environment: %m");
610 log_debug("Received %d descriptors", n
);
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");
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
);
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
);
625 r
= journal_remote_add_raw_socket(s
, fd
);
626 } else if (sd_is_socket(fd
, AF_UNSPEC
, 0, false)) {
629 r
= getpeername_pretty(fd
, false, &hostname
);
631 return log_error_errno(r
, "Failed to retrieve remote name: %m");
633 log_debug("Received a connection socket (fd:%d) from %s", fd
, hostname
);
635 r
= journal_remote_add_source(s
, fd
, hostname
, true);
637 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
638 "Unknown socket passed on fd:%d", fd
);
641 return log_error_errno(r
, "Failed to register socket (fd:%d): %m", fd
);
645 log_info("Spawning getter %s...", arg_getter
);
646 fd
= spawn_getter(arg_getter
);
650 r
= journal_remote_add_source(s
, fd
, (char*) arg_output
, false);
656 const char *url
, *hostname
;
658 if (!strstr(arg_url
, "/entries")) {
659 if (endswith(arg_url
, "/"))
660 url
= strjoina(arg_url
, "entries");
662 url
= strjoina(arg_url
, "/entries");
664 url
= strdupa_safe(arg_url
);
666 log_info("Spawning curl %s...", url
);
667 fd
= spawn_curl(url
);
671 hostname
= STARTSWITH_SET(arg_url
, "https://", "http://");
675 hostname
= strndupa_safe(hostname
, strcspn(hostname
, "/:"));
677 r
= journal_remote_add_source(s
, fd
, (char *) hostname
, false);
682 if (arg_listen_raw
) {
683 log_debug("Listening on a socket...");
684 r
= setup_raw_socket(s
, arg_listen_raw
);
689 if (arg_listen_http
) {
690 r
= setup_microhttpd_socket(s
, arg_listen_http
, NULL
, NULL
, NULL
);
695 if (arg_listen_https
) {
696 r
= setup_microhttpd_socket(s
, arg_listen_https
, key
, cert
, trust
);
701 STRV_FOREACH(file
, arg_files
) {
702 const char *output_name
;
704 if (streq(*file
, "-")) {
705 log_debug("Using standard input as source.");
708 output_name
= "stdin";
710 log_debug("Reading file %s...", *file
);
712 fd
= open(*file
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
714 return log_error_errno(errno
, "Failed to open %s: %m", *file
);
718 r
= journal_remote_add_source(s
, fd
, (char*) output_name
, false);
724 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
725 "Zero sources specified");
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
);
739 static int negative_fd(const char *spec
) {
740 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
744 r
= safe_atoi(spec
, &fd
);
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
},
764 return config_parse_many_nulstr(
765 PKGSYSCONFDIR
"/journal-remote.conf",
766 CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"),
768 config_item_table_lookup
, items
,
774 static int help(void) {
775 _cleanup_free_
char *link
= NULL
;
778 r
= terminal_urlify_man("systemd-journal-remote.service", "8", &link
);
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
,
811 static int parse_argv(int argc
, char *argv
[]) {
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
},
853 while ((c
= getopt_long(argc
, argv
, "ho:", options
, NULL
)) >= 0)
864 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
865 "cannot currently set more than one --url");
872 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
873 "cannot currently use --getter more than once");
880 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
881 "cannot currently use --listen-raw more than once");
883 arg_listen_raw
= optarg
;
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");
891 r
= negative_fd(optarg
);
895 arg_listen_http
= optarg
;
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");
903 r
= negative_fd(optarg
);
907 arg_listen_https
= optarg
;
913 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
914 "Key file specified twice");
916 arg_key
= strdup(optarg
);
924 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
925 "Certificate file specified twice");
927 arg_cert
= strdup(optarg
);
934 if (arg_trust
|| arg_trust_all
)
935 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
936 "Confusing trusted CA configuration");
938 if (streq(optarg
, "all"))
939 arg_trust_all
= true;
942 arg_trust
= strdup(optarg
);
946 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
947 "Option --trust is not available.");
955 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
956 "cannot use --output/-o more than once");
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
);
968 r
= parse_boolean_argument("--compress", optarg
, &arg_compress
);
974 r
= parse_boolean_argument("--seal", optarg
, &arg_seal
);
981 for (const char* p
= optarg
;;) {
982 _cleanup_free_
char *word
= NULL
;
984 r
= extract_first_word(&p
, &word
, ",", 0);
986 return log_error_errno(r
, "Failed to parse --gnutls-log= argument: %m");
990 if (strv_push(&arg_gnutls_log
, word
) < 0)
997 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
998 "Option --gnutls-log is not available.");
1005 assert_not_reached();
1009 arg_files
= argv
+ optind
;
1011 type_a
= arg_getter
|| !strv_isempty(arg_files
);
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.");
1022 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1023 "Option --output must be specified with file input or --getter.");
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.");
1029 arg_split_mode
= JOURNAL_WRITE_SPLIT_NONE
;
1032 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
1033 arg_split_mode
= JOURNAL_WRITE_SPLIT_HOST
;
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.");
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.");
1049 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1050 journal_write_split_mode_to_string(arg_split_mode
),
1055 return 1 /* work to do */;
1058 static int load_certificates(char **key
, char **cert
, char **trust
) {
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
,
1067 return log_error_errno(r
, "Failed to read key from file '%s': %m",
1068 arg_key
?: PRIV_KEY_FILE
);
1070 r
= read_full_file_full(
1071 AT_FDCWD
, arg_cert
?: CERT_FILE
, UINT64_MAX
, SIZE_MAX
,
1072 READ_FULL_FILE_CONNECT_SOCKET
,
1076 return log_error_errno(r
, "Failed to read certificate from file '%s': %m",
1077 arg_cert
?: CERT_FILE
);
1080 log_info("Certificate checking disabled.");
1082 r
= read_full_file_full(
1083 AT_FDCWD
, arg_trust
?: TRUST_FILE
, UINT64_MAX
, SIZE_MAX
,
1084 READ_FULL_FILE_CONNECT_SOCKET
,
1088 return log_error_errno(r
, "Failed to read CA certificate file '%s': %m",
1089 arg_trust
?: TRUST_FILE
);
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.");
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
;
1106 log_show_color(true);
1107 log_parse_environment();
1109 /* The journal merging logic potentially needs a lot of fds. */
1110 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1116 r
= parse_argv(argc
, argv
);
1120 if (arg_listen_http
|| arg_listen_https
) {
1121 r
= setup_gnutls_logger(arg_gnutls_log
);
1126 if (arg_listen_https
|| https_socket
>= 0) {
1127 r
= load_certificates(&key
, &cert
, &trust
);
1131 s
.check_trust
= !arg_trust_all
;
1134 r
= create_remoteserver(&s
, key
, cert
, trust
);
1138 r
= sd_event_set_watchdog(s
.events
, true);
1140 return log_error_errno(r
, "Failed to enable watchdog: %m");
1142 log_debug("Watchdog is %sd.", enable_disable(r
> 0));
1144 log_debug("%s running as pid "PID_FMT
,
1145 program_invocation_short_name
, getpid_cached());
1147 notify_message
= notify_start(NOTIFY_READY
, NOTIFY_STOPPING
);
1150 r
= sd_event_get_state(s
.events
);
1153 if (r
== SD_EVENT_FINISHED
)
1156 r
= sd_event_run(s
.events
, -1);
1158 return log_error_errno(r
, "Failed to run event loop: %m");
1161 notify_message
= NULL
;
1162 (void) sd_notifyf(false,
1164 "STATUS=Shutting down after writing %" PRIu64
" entries...", s
.event_count
);
1166 log_info("Finishing after writing %" PRIu64
" entries", s
.event_count
);
1171 DEFINE_MAIN_FUNCTION(run
);