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 "pretty-print.h"
18 #include "process-util.h"
19 #include "rlimit-util.h"
20 #include "signal-util.h"
21 #include "socket-netlink.h"
22 #include "socket-util.h"
23 #include "stat-util.h"
24 #include "string-table.h"
27 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
28 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
29 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
31 static const char* arg_url
= NULL
;
32 static const char* arg_getter
= NULL
;
33 static const char* arg_listen_raw
= NULL
;
34 static const char* arg_listen_http
= NULL
;
35 static const char* arg_listen_https
= NULL
;
36 static char** arg_files
= NULL
; /* Do not free this. */
37 static int arg_compress
= true;
38 static int arg_seal
= false;
39 static int http_socket
= -1, https_socket
= -1;
40 static char** arg_gnutls_log
= NULL
;
42 static JournalWriteSplitMode arg_split_mode
= _JOURNAL_WRITE_SPLIT_INVALID
;
43 static const char* arg_output
= NULL
;
45 static char *arg_key
= NULL
;
46 static char *arg_cert
= NULL
;
47 static char *arg_trust
= NULL
;
48 static bool arg_trust_all
= false;
50 STATIC_DESTRUCTOR_REGISTER(arg_gnutls_log
, strv_freep
);
51 STATIC_DESTRUCTOR_REGISTER(arg_key
, freep
);
52 STATIC_DESTRUCTOR_REGISTER(arg_cert
, freep
);
53 STATIC_DESTRUCTOR_REGISTER(arg_trust
, freep
);
55 static const char* const journal_write_split_mode_table
[_JOURNAL_WRITE_SPLIT_MAX
] = {
56 [JOURNAL_WRITE_SPLIT_NONE
] = "none",
57 [JOURNAL_WRITE_SPLIT_HOST
] = "host",
60 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode
, JournalWriteSplitMode
);
61 static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode
,
62 journal_write_split_mode
,
63 JournalWriteSplitMode
,
64 "Failed to parse split mode setting");
66 /**********************************************************************
67 **********************************************************************
68 **********************************************************************/
70 static int spawn_child(const char* child
, char** argv
) {
75 return log_error_errno(errno
, "Failed to create pager pipe: %m");
77 r
= safe_fork("(remote)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &child_pid
);
87 r
= rearrange_stdio(STDIN_FILENO
, fd
[1], STDERR_FILENO
);
89 log_error_errno(r
, "Failed to dup pipe to stdout: %m");
93 (void) rlimit_nofile_safe();
96 log_error_errno(errno
, "Failed to exec child %s: %m", child
);
102 r
= fd_nonblock(fd
[0], true);
104 log_warning_errno(errno
, "Failed to set child pipe to non-blocking: %m");
109 static int spawn_curl(const char* url
) {
110 char **argv
= STRV_MAKE("curl",
111 "-HAccept: application/vnd.fdo.journal",
117 r
= spawn_child("curl", argv
);
119 log_error_errno(r
, "Failed to spawn curl: %m");
123 static int spawn_getter(const char *getter
) {
125 _cleanup_strv_free_
char **words
= NULL
;
128 r
= strv_split_full(&words
, getter
, WHITESPACE
, EXTRACT_UNQUOTE
);
130 return log_error_errno(r
, "Failed to split getter option: %m");
132 r
= spawn_child(words
[0], words
);
134 log_error_errno(r
, "Failed to spawn getter %s: %m", getter
);
139 /**********************************************************************
140 **********************************************************************
141 **********************************************************************/
143 static int null_timer_event_handler(sd_event_source
*s
,
146 static int dispatch_http_event(sd_event_source
*event
,
151 static int request_meta(void **connection_cls
, int fd
, char *hostname
) {
152 RemoteSource
*source
;
156 assert(connection_cls
);
160 r
= journal_remote_get_writer(journal_remote_server_global
, hostname
, &writer
);
162 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
165 source
= source_new(fd
, true, hostname
, writer
);
167 writer_unref(writer
);
171 log_debug("Added RemoteSource as connection metadata %p", source
);
173 *connection_cls
= source
;
177 static void request_meta_free(void *cls
,
178 struct MHD_Connection
*connection
,
179 void **connection_cls
,
180 enum MHD_RequestTerminationCode toe
) {
183 assert(connection_cls
);
187 log_debug("Cleaning up connection metadata %p", s
);
189 *connection_cls
= NULL
;
193 static int process_http_upload(
194 struct MHD_Connection
*connection
,
195 const char *upload_data
,
196 size_t *upload_data_size
,
197 RemoteSource
*source
) {
199 bool finished
= false;
205 log_trace("%s: connection %p, %zu bytes",
206 __func__
, connection
, *upload_data_size
);
208 if (*upload_data_size
) {
209 log_trace("Received %zu bytes", *upload_data_size
);
211 r
= journal_importer_push_data(&source
->importer
,
212 upload_data
, *upload_data_size
);
214 return mhd_respond_oom(connection
);
216 *upload_data_size
= 0;
221 r
= process_source(source
,
222 journal_remote_server_global
->compress
,
223 journal_remote_server_global
->seal
);
228 log_warning_errno(r
, "Entry is above the maximum of %u, aborting connection %p.",
229 DATA_SIZE_MAX
, connection
);
230 else if (r
== -E2BIG
)
231 log_warning_errno(r
, "Entry with more fields than the maximum of %u, aborting connection %p.",
232 ENTRY_FIELD_COUNT_MAX
, connection
);
234 log_warning_errno(r
, "Failed to process data, aborting connection %p: %m",
243 /* The upload is finished */
245 remaining
= journal_importer_bytes_remaining(&source
->importer
);
247 log_warning("Premature EOF byte. %zu bytes lost.", remaining
);
248 return mhd_respondf(connection
,
249 0, MHD_HTTP_EXPECTATION_FAILED
,
250 "Premature EOF. %zu bytes of trailing data not processed.",
254 return mhd_respond(connection
, MHD_HTTP_ACCEPTED
, "OK.");
257 static mhd_result
request_handler(
259 struct MHD_Connection
*connection
,
263 const char *upload_data
,
264 size_t *upload_data_size
,
265 void **connection_cls
) {
269 _cleanup_free_
char *hostname
= NULL
;
270 bool chunked
= false;
273 assert(connection_cls
);
277 log_trace("Handling a connection %s %s %s", method
, url
, version
);
280 return process_http_upload(connection
,
281 upload_data
, upload_data_size
,
284 if (!streq(method
, "POST"))
285 return mhd_respond(connection
, MHD_HTTP_NOT_ACCEPTABLE
, "Unsupported method.");
287 if (!streq(url
, "/upload"))
288 return mhd_respond(connection
, MHD_HTTP_NOT_FOUND
, "Not found.");
290 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Type");
291 if (!header
|| !streq(header
, "application/vnd.fdo.journal"))
292 return mhd_respond(connection
, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE
,
293 "Content-Type: application/vnd.fdo.journal is required.");
295 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Transfer-Encoding");
297 if (!strcaseeq(header
, "chunked"))
298 return mhd_respondf(connection
, 0, MHD_HTTP_BAD_REQUEST
,
299 "Unsupported Transfer-Encoding type: %s", header
);
304 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Length");
309 return mhd_respond(connection
, MHD_HTTP_BAD_REQUEST
,
310 "Content-Length must not specified when Transfer-Encoding type is 'chuncked'");
312 r
= safe_atozu(header
, &len
);
314 return mhd_respondf(connection
, r
, MHD_HTTP_LENGTH_REQUIRED
,
315 "Content-Length: %s cannot be parsed: %m", header
);
317 if (len
> ENTRY_SIZE_MAX
)
318 /* When serialized, an entry of maximum size might be slightly larger,
319 * so this does not correspond exactly to the limit in journald. Oh well.
321 return mhd_respondf(connection
, 0, MHD_HTTP_PAYLOAD_TOO_LARGE
,
322 "Payload larger than maximum size of %u bytes", ENTRY_SIZE_MAX
);
326 const union MHD_ConnectionInfo
*ci
;
328 ci
= MHD_get_connection_info(connection
,
329 MHD_CONNECTION_INFO_CONNECTION_FD
);
331 log_error("MHD_get_connection_info failed: cannot get remote fd");
332 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
333 "Cannot check remote address.");
340 if (journal_remote_server_global
->check_trust
) {
341 r
= check_permissions(connection
, &code
, &hostname
);
345 r
= getpeername_pretty(fd
, false, &hostname
);
347 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
348 "Cannot check remote hostname.");
353 r
= request_meta(connection_cls
, fd
, hostname
);
355 return respond_oom(connection
);
357 return mhd_respondf(connection
, r
, MHD_HTTP_INTERNAL_SERVER_ERROR
, "%m");
363 static int setup_microhttpd_server(RemoteServer
*s
,
368 struct MHD_OptionItem opts
[] = {
369 { MHD_OPTION_NOTIFY_COMPLETED
, (intptr_t) request_meta_free
},
370 { MHD_OPTION_EXTERNAL_LOGGER
, (intptr_t) microhttpd_logger
},
371 { MHD_OPTION_LISTEN_SOCKET
, fd
},
372 { MHD_OPTION_CONNECTION_MEMORY_LIMIT
, 128*1024},
385 const union MHD_DaemonInfo
*info
;
391 r
= fd_nonblock(fd
, true);
393 return log_error_errno(r
, "Failed to make fd:%d nonblocking: %m", fd
);
395 /* MHD_OPTION_STRICT_FOR_CLIENT is introduced in microhttpd 0.9.54,
396 * and MHD_USE_PEDANTIC_CHECKS will be deprecated in future.
397 * If MHD_USE_PEDANTIC_CHECKS is '#define'd, then it is deprecated
398 * and we should use MHD_OPTION_STRICT_FOR_CLIENT. On the other hand,
399 * if MHD_USE_PEDANTIC_CHECKS is not '#define'd, then it is not
400 * deprecated yet and there exists an enum element with the same name.
401 * So we can safely use it. */
402 #ifdef MHD_USE_PEDANTIC_CHECKS
403 opts
[opts_pos
++] = (struct MHD_OptionItem
)
404 {MHD_OPTION_STRICT_FOR_CLIENT
, 1};
406 flags
|= MHD_USE_PEDANTIC_CHECKS
;
412 opts
[opts_pos
++] = (struct MHD_OptionItem
)
413 {MHD_OPTION_HTTPS_MEM_KEY
, 0, (char*) key
};
414 opts
[opts_pos
++] = (struct MHD_OptionItem
)
415 {MHD_OPTION_HTTPS_MEM_CERT
, 0, (char*) cert
};
417 flags
|= MHD_USE_TLS
;
420 opts
[opts_pos
++] = (struct MHD_OptionItem
)
421 {MHD_OPTION_HTTPS_MEM_TRUST
, 0, (char*) trust
};
424 d
= new(MHDDaemonWrapper
, 1);
428 d
->fd
= (uint64_t) fd
;
430 d
->daemon
= MHD_start_daemon(flags
, 0,
432 request_handler
, NULL
,
433 MHD_OPTION_ARRAY
, opts
,
436 log_error("Failed to start µhttp daemon");
441 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
442 key
? "HTTPS" : "HTTP", fd
, d
);
444 info
= MHD_get_daemon_info(d
->daemon
, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY
);
446 log_error("µhttp returned NULL daemon info");
451 epoll_fd
= info
->listen_fd
;
453 log_error("µhttp epoll fd is invalid");
458 r
= sd_event_add_io(s
->events
, &d
->io_event
,
460 dispatch_http_event
, d
);
462 log_error_errno(r
, "Failed to add event callback: %m");
466 r
= sd_event_source_set_description(d
->io_event
, "io_event");
468 log_error_errno(r
, "Failed to set source name: %m");
472 r
= sd_event_add_time(s
->events
, &d
->timer_event
,
473 CLOCK_MONOTONIC
, (uint64_t) -1, 0,
474 null_timer_event_handler
, d
);
476 log_error_errno(r
, "Failed to add timer_event: %m");
480 r
= sd_event_source_set_description(d
->timer_event
, "timer_event");
482 log_error_errno(r
, "Failed to set source name: %m");
486 r
= hashmap_ensure_allocated(&s
->daemons
, &uint64_hash_ops
);
492 r
= hashmap_put(s
->daemons
, &d
->fd
, d
);
494 log_error_errno(r
, "Failed to add daemon to hashmap: %m");
502 MHD_stop_daemon(d
->daemon
);
508 static int setup_microhttpd_socket(RemoteServer
*s
,
515 fd
= make_socket_fd(LOG_DEBUG
, address
, SOCK_STREAM
, SOCK_CLOEXEC
);
519 return setup_microhttpd_server(s
, fd
, key
, cert
, trust
);
522 static int null_timer_event_handler(sd_event_source
*timer_event
,
525 return dispatch_http_event(timer_event
, 0, 0, userdata
);
528 static int dispatch_http_event(sd_event_source
*event
,
532 MHDDaemonWrapper
*d
= userdata
;
534 MHD_UNSIGNED_LONG_LONG timeout
= ULLONG_MAX
;
538 r
= MHD_run(d
->daemon
);
540 // FIXME: unregister daemon
541 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
543 if (MHD_get_timeout(d
->daemon
, &timeout
) == MHD_NO
)
544 timeout
= ULLONG_MAX
;
546 r
= sd_event_source_set_time(d
->timer_event
, timeout
);
548 log_warning_errno(r
, "Unable to set event loop timeout: %m, this may result in indefinite blocking!");
552 r
= sd_event_source_set_enabled(d
->timer_event
, SD_EVENT_ON
);
554 log_warning_errno(r
, "Unable to enable timer_event: %m, this may result in indefinite blocking!");
556 return 1; /* work to do */
559 /**********************************************************************
560 **********************************************************************
561 **********************************************************************/
563 static int setup_signals(RemoteServer
*s
) {
568 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, -1) >= 0);
570 r
= sd_event_add_signal(s
->events
, &s
->sigterm_event
, SIGTERM
, NULL
, s
);
574 r
= sd_event_add_signal(s
->events
, &s
->sigint_event
, SIGINT
, NULL
, s
);
581 static int setup_raw_socket(RemoteServer
*s
, const char *address
) {
584 fd
= make_socket_fd(LOG_INFO
, address
, SOCK_STREAM
, SOCK_CLOEXEC
);
588 return journal_remote_add_raw_socket(s
, fd
);
591 static int create_remoteserver(
600 r
= journal_remote_server_init(s
, arg_output
, arg_split_mode
, arg_compress
, arg_seal
);
604 r
= setup_signals(s
);
606 return log_error_errno(r
, "Failed to set up signals: %m");
608 n
= sd_listen_fds(true);
610 return log_error_errno(n
, "Failed to read listening file descriptors from environment: %m");
612 log_debug("Received %d descriptors", n
);
614 if (MAX(http_socket
, https_socket
) >= SD_LISTEN_FDS_START
+ n
)
615 return log_error_errno(SYNTHETIC_ERRNO(EBADFD
),
616 "Received fewer sockets than expected");
618 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
619 if (sd_is_socket(fd
, AF_UNSPEC
, 0, true)) {
620 log_debug("Received a listening socket (fd:%d)", fd
);
622 if (fd
== http_socket
)
623 r
= setup_microhttpd_server(s
, fd
, NULL
, NULL
, NULL
);
624 else if (fd
== https_socket
)
625 r
= setup_microhttpd_server(s
, fd
, key
, cert
, trust
);
627 r
= journal_remote_add_raw_socket(s
, fd
);
628 } else if (sd_is_socket(fd
, AF_UNSPEC
, 0, false)) {
631 r
= getpeername_pretty(fd
, false, &hostname
);
633 return log_error_errno(r
, "Failed to retrieve remote name: %m");
635 log_debug("Received a connection socket (fd:%d) from %s", fd
, hostname
);
637 r
= journal_remote_add_source(s
, fd
, hostname
, true);
639 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
640 "Unknown socket passed on fd:%d", fd
);
643 return log_error_errno(r
, "Failed to register socket (fd:%d): %m", fd
);
647 log_info("Spawning getter %s...", arg_getter
);
648 fd
= spawn_getter(arg_getter
);
652 r
= journal_remote_add_source(s
, fd
, (char*) arg_output
, false);
658 const char *url
, *hostname
;
660 if (!strstr(arg_url
, "/entries")) {
661 if (endswith(arg_url
, "/"))
662 url
= strjoina(arg_url
, "entries");
664 url
= strjoina(arg_url
, "/entries");
666 url
= strdupa(arg_url
);
668 log_info("Spawning curl %s...", url
);
669 fd
= spawn_curl(url
);
673 hostname
= STARTSWITH_SET(arg_url
, "https://", "http://");
677 hostname
= strndupa(hostname
, strcspn(hostname
, "/:"));
679 r
= journal_remote_add_source(s
, fd
, (char *) hostname
, false);
684 if (arg_listen_raw
) {
685 log_debug("Listening on a socket...");
686 r
= setup_raw_socket(s
, arg_listen_raw
);
691 if (arg_listen_http
) {
692 r
= setup_microhttpd_socket(s
, arg_listen_http
, NULL
, NULL
, NULL
);
697 if (arg_listen_https
) {
698 r
= setup_microhttpd_socket(s
, arg_listen_https
, key
, cert
, trust
);
703 STRV_FOREACH(file
, arg_files
) {
704 const char *output_name
;
706 if (streq(*file
, "-")) {
707 log_debug("Using standard input as source.");
710 output_name
= "stdin";
712 log_debug("Reading file %s...", *file
);
714 fd
= open(*file
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
716 return log_error_errno(errno
, "Failed to open %s: %m", *file
);
720 r
= journal_remote_add_source(s
, fd
, (char*) output_name
, false);
726 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
727 "Zero sources specified");
729 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_NONE
) {
730 /* In this case we know what the writer will be
731 called, so we can create it and verify that we can
732 create output as expected. */
733 r
= journal_remote_get_writer(s
, NULL
, &s
->_single_writer
);
741 static int negative_fd(const char *spec
) {
742 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
746 r
= safe_atoi(spec
, &fd
);
756 static int parse_config(void) {
757 const ConfigTableItem items
[] = {
758 { "Remote", "Seal", config_parse_bool
, 0, &arg_seal
},
759 { "Remote", "SplitMode", config_parse_write_split_mode
, 0, &arg_split_mode
},
760 { "Remote", "ServerKeyFile", config_parse_path
, 0, &arg_key
},
761 { "Remote", "ServerCertificateFile", config_parse_path
, 0, &arg_cert
},
762 { "Remote", "TrustedCertificateFile", config_parse_path
, 0, &arg_trust
},
766 return config_parse_many_nulstr(
767 PKGSYSCONFDIR
"/journal-remote.conf",
768 CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"),
770 config_item_table_lookup
, items
,
776 static int help(void) {
777 _cleanup_free_
char *link
= NULL
;
780 r
= terminal_urlify_man("systemd-journal-remote.service", "8", &link
);
784 printf("%s [OPTIONS...] {FILE|-}...\n\n"
785 "Write external journal events to journal file(s).\n\n"
786 " -h --help Show this help\n"
787 " --version Show package version\n"
788 " --url=URL Read events from systemd-journal-gatewayd at URL\n"
789 " --getter=COMMAND Read events from the output of COMMAND\n"
790 " --listen-raw=ADDR Listen for connections at ADDR\n"
791 " --listen-http=ADDR Listen for HTTP connections at ADDR\n"
792 " --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
793 " -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
794 " --compress[=BOOL] Use compression in the output journal (default: yes)\n"
795 " --seal[=BOOL] Use event sealing (default: no)\n"
796 " --key=FILENAME SSL key in PEM format (default:\n"
797 " \"" PRIV_KEY_FILE
"\")\n"
798 " --cert=FILENAME SSL certificate in PEM format (default:\n"
799 " \"" CERT_FILE
"\")\n"
800 " --trust=FILENAME|all SSL CA certificate or disable checking (default:\n"
801 " \"" TRUST_FILE
"\")\n"
802 " --gnutls-log=CATEGORY...\n"
803 " Specify a list of gnutls logging categories\n"
804 " --split-mode=none|host How many output files to create\n"
805 "\nNote: file descriptors from sd_listen_fds() will be consumed, too.\n"
806 "\nSee the %s for details.\n"
807 , program_invocation_short_name
814 static int parse_argv(int argc
, char *argv
[]) {
831 static const struct option options
[] = {
832 { "help", no_argument
, NULL
, 'h' },
833 { "version", no_argument
, NULL
, ARG_VERSION
},
834 { "url", required_argument
, NULL
, ARG_URL
},
835 { "getter", required_argument
, NULL
, ARG_GETTER
},
836 { "listen-raw", required_argument
, NULL
, ARG_LISTEN_RAW
},
837 { "listen-http", required_argument
, NULL
, ARG_LISTEN_HTTP
},
838 { "listen-https", required_argument
, NULL
, ARG_LISTEN_HTTPS
},
839 { "output", required_argument
, NULL
, 'o' },
840 { "split-mode", required_argument
, NULL
, ARG_SPLIT_MODE
},
841 { "compress", optional_argument
, NULL
, ARG_COMPRESS
},
842 { "seal", optional_argument
, NULL
, ARG_SEAL
},
843 { "key", required_argument
, NULL
, ARG_KEY
},
844 { "cert", required_argument
, NULL
, ARG_CERT
},
845 { "trust", required_argument
, NULL
, ARG_TRUST
},
846 { "gnutls-log", required_argument
, NULL
, ARG_GNUTLS_LOG
},
856 while ((c
= getopt_long(argc
, argv
, "ho:", options
, NULL
)) >= 0)
867 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
868 "cannot currently set more than one --url");
875 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
876 "cannot currently use --getter more than once");
883 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
884 "cannot currently use --listen-raw more than once");
886 arg_listen_raw
= optarg
;
889 case ARG_LISTEN_HTTP
:
890 if (arg_listen_http
|| http_socket
>= 0)
891 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
892 "cannot currently use --listen-http more than once");
894 r
= negative_fd(optarg
);
898 arg_listen_http
= optarg
;
901 case ARG_LISTEN_HTTPS
:
902 if (arg_listen_https
|| https_socket
>= 0)
903 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
904 "cannot currently use --listen-https more than once");
906 r
= negative_fd(optarg
);
910 arg_listen_https
= optarg
;
916 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
917 "Key file specified twice");
919 arg_key
= strdup(optarg
);
927 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
928 "Certificate file specified twice");
930 arg_cert
= strdup(optarg
);
937 if (arg_trust
|| arg_trust_all
)
938 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
939 "Confusing trusted CA configuration");
941 if (streq(optarg
, "all"))
942 arg_trust_all
= true;
945 arg_trust
= strdup(optarg
);
949 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
950 "Option --trust is not available.");
958 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
959 "cannot use --output/-o more than once");
965 arg_split_mode
= journal_write_split_mode_from_string(optarg
);
966 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
967 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
968 "Invalid split mode: %s", optarg
);
973 r
= parse_boolean(optarg
);
975 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
976 "Failed to parse --compress= parameter.");
986 r
= parse_boolean(optarg
);
988 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
989 "Failed to parse --seal= parameter.");
997 case ARG_GNUTLS_LOG
: {
999 const char* p
= optarg
;
1001 _cleanup_free_
char *word
= NULL
;
1003 r
= extract_first_word(&p
, &word
, ",", 0);
1005 return log_error_errno(r
, "Failed to parse --gnutls-log= argument: %m");
1009 if (strv_push(&arg_gnutls_log
, word
) < 0)
1016 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1017 "Option --gnutls-log is not available.");
1025 assert_not_reached("Unknown option code.");
1029 arg_files
= argv
+ optind
;
1031 type_a
= arg_getter
|| !strv_isempty(arg_files
);
1034 || arg_listen_http
|| arg_listen_https
1035 || sd_listen_fds(false) > 0;
1036 if (type_a
&& type_b
)
1037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1038 "Cannot use file input or --getter with "
1039 "--arg-listen-... or socket activation.");
1042 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1043 "Option --output must be specified with file input or --getter.");
1045 if (!IN_SET(arg_split_mode
, JOURNAL_WRITE_SPLIT_NONE
, _JOURNAL_WRITE_SPLIT_INVALID
))
1046 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1047 "For active sources, only --split-mode=none is allowed.");
1049 arg_split_mode
= JOURNAL_WRITE_SPLIT_NONE
;
1052 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
1053 arg_split_mode
= JOURNAL_WRITE_SPLIT_HOST
;
1055 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_NONE
&& arg_output
) {
1056 if (is_dir(arg_output
, true) > 0)
1057 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1058 "For SplitMode=none, output must be a file.");
1059 if (!endswith(arg_output
, ".journal"))
1060 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1061 "For SplitMode=none, output file name must end with .journal.");
1064 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_HOST
1065 && arg_output
&& is_dir(arg_output
, true) <= 0)
1066 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1067 "For SplitMode=host, output must be a directory.");
1069 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1070 journal_write_split_mode_to_string(arg_split_mode
),
1075 return 1 /* work to do */;
1078 static int load_certificates(char **key
, char **cert
, char **trust
) {
1081 r
= read_full_file_full(
1082 AT_FDCWD
, arg_key
?: PRIV_KEY_FILE
, UINT64_MAX
, SIZE_MAX
,
1083 READ_FULL_FILE_SECURE
|READ_FULL_FILE_WARN_WORLD_READABLE
|READ_FULL_FILE_CONNECT_SOCKET
,
1087 return log_error_errno(r
, "Failed to read key from file '%s': %m",
1088 arg_key
?: PRIV_KEY_FILE
);
1090 r
= read_full_file_full(
1091 AT_FDCWD
, arg_cert
?: CERT_FILE
, UINT64_MAX
, SIZE_MAX
,
1092 READ_FULL_FILE_CONNECT_SOCKET
,
1096 return log_error_errno(r
, "Failed to read certificate from file '%s': %m",
1097 arg_cert
?: CERT_FILE
);
1100 log_info("Certificate checking disabled.");
1102 r
= read_full_file_full(
1103 AT_FDCWD
, arg_trust
?: TRUST_FILE
, UINT64_MAX
, SIZE_MAX
,
1104 READ_FULL_FILE_CONNECT_SOCKET
,
1108 return log_error_errno(r
, "Failed to read CA certificate file '%s': %m",
1109 arg_trust
?: TRUST_FILE
);
1112 if ((arg_listen_raw
|| arg_listen_http
) && *trust
)
1113 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1114 "Option --trust makes all non-HTTPS connections untrusted.");
1119 static int run(int argc
, char **argv
) {
1120 _cleanup_(journal_remote_server_destroy
) RemoteServer s
= {};
1121 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
1122 _cleanup_(erase_and_freep
) char *key
= NULL
;
1123 _cleanup_free_
char *cert
= NULL
, *trust
= NULL
;
1126 log_show_color(true);
1127 log_parse_environment_cli();
1129 /* The journal merging logic potentially needs a lot of fds. */
1130 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1136 r
= parse_argv(argc
, argv
);
1140 if (arg_listen_http
|| arg_listen_https
) {
1141 r
= setup_gnutls_logger(arg_gnutls_log
);
1146 if (arg_listen_https
|| https_socket
>= 0) {
1147 r
= load_certificates(&key
, &cert
, &trust
);
1151 s
.check_trust
= !arg_trust_all
;
1154 r
= create_remoteserver(&s
, key
, cert
, trust
);
1158 r
= sd_event_set_watchdog(s
.events
, true);
1160 return log_error_errno(r
, "Failed to enable watchdog: %m");
1162 log_debug("Watchdog is %sd.", enable_disable(r
> 0));
1164 log_debug("%s running as pid "PID_FMT
,
1165 program_invocation_short_name
, getpid_cached());
1167 notify_message
= notify_start(NOTIFY_READY
, NOTIFY_STOPPING
);
1170 r
= sd_event_get_state(s
.events
);
1173 if (r
== SD_EVENT_FINISHED
)
1176 r
= sd_event_run(s
.events
, -1);
1178 return log_error_errno(r
, "Failed to run event loop: %m");
1181 notify_message
= NULL
;
1182 (void) sd_notifyf(false,
1184 "STATUS=Shutting down after writing %" PRIu64
" entries...", s
.event_count
);
1186 log_info("Finishing after writing %" PRIu64
" entries", s
.event_count
);
1191 DEFINE_MAIN_FUNCTION(run
);