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 "pretty-print.h"
17 #include "process-util.h"
18 #include "rlimit-util.h"
19 #include "signal-util.h"
20 #include "socket-netlink.h"
21 #include "socket-util.h"
22 #include "stat-util.h"
23 #include "string-table.h"
26 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
27 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
28 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
30 static const char* arg_url
= NULL
;
31 static const char* arg_getter
= NULL
;
32 static const char* arg_listen_raw
= NULL
;
33 static const char* arg_listen_http
= NULL
;
34 static const char* arg_listen_https
= NULL
;
35 static char** arg_files
= NULL
; /* Do not free this. */
36 static int arg_compress
= true;
37 static int arg_seal
= false;
38 static int http_socket
= -1, https_socket
= -1;
39 static char** arg_gnutls_log
= NULL
;
41 static JournalWriteSplitMode arg_split_mode
= _JOURNAL_WRITE_SPLIT_INVALID
;
42 static const char* arg_output
= NULL
;
44 static char *arg_key
= NULL
;
45 static char *arg_cert
= NULL
;
46 static char *arg_trust
= NULL
;
47 static bool arg_trust_all
= false;
49 STATIC_DESTRUCTOR_REGISTER(arg_gnutls_log
, strv_freep
);
50 STATIC_DESTRUCTOR_REGISTER(arg_key
, freep
);
51 STATIC_DESTRUCTOR_REGISTER(arg_cert
, freep
);
52 STATIC_DESTRUCTOR_REGISTER(arg_trust
, freep
);
54 static const char* const journal_write_split_mode_table
[_JOURNAL_WRITE_SPLIT_MAX
] = {
55 [JOURNAL_WRITE_SPLIT_NONE
] = "none",
56 [JOURNAL_WRITE_SPLIT_HOST
] = "host",
59 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode
, JournalWriteSplitMode
);
60 static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode
,
61 journal_write_split_mode
,
62 JournalWriteSplitMode
,
63 "Failed to parse split mode setting");
65 /**********************************************************************
66 **********************************************************************
67 **********************************************************************/
69 static int spawn_child(const char* child
, char** argv
) {
74 return log_error_errno(errno
, "Failed to create pager pipe: %m");
76 r
= safe_fork("(remote)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &child_pid
);
86 r
= rearrange_stdio(STDIN_FILENO
, fd
[1], STDERR_FILENO
);
88 log_error_errno(r
, "Failed to dup pipe to stdout: %m");
92 (void) rlimit_nofile_safe();
95 log_error_errno(errno
, "Failed to exec child %s: %m", child
);
101 r
= fd_nonblock(fd
[0], true);
103 log_warning_errno(errno
, "Failed to set child pipe to non-blocking: %m");
108 static int spawn_curl(const char* url
) {
109 char **argv
= STRV_MAKE("curl",
110 "-HAccept: application/vnd.fdo.journal",
116 r
= spawn_child("curl", argv
);
118 log_error_errno(r
, "Failed to spawn curl: %m");
122 static int spawn_getter(const char *getter
) {
124 _cleanup_strv_free_
char **words
= NULL
;
127 r
= strv_split_full(&words
, getter
, WHITESPACE
, EXTRACT_UNQUOTE
);
129 return log_error_errno(r
, "Failed to split getter option: %m");
131 r
= spawn_child(words
[0], words
);
133 log_error_errno(r
, "Failed to spawn getter %s: %m", getter
);
138 /**********************************************************************
139 **********************************************************************
140 **********************************************************************/
142 static int null_timer_event_handler(sd_event_source
*s
,
145 static int dispatch_http_event(sd_event_source
*event
,
150 static int request_meta(void **connection_cls
, int fd
, char *hostname
) {
151 RemoteSource
*source
;
155 assert(connection_cls
);
159 r
= journal_remote_get_writer(journal_remote_server_global
, hostname
, &writer
);
161 return log_warning_errno(r
, "Failed to get writer for source %s: %m",
164 source
= source_new(fd
, true, hostname
, writer
);
166 writer_unref(writer
);
170 log_debug("Added RemoteSource as connection metadata %p", source
);
172 *connection_cls
= source
;
176 static void request_meta_free(void *cls
,
177 struct MHD_Connection
*connection
,
178 void **connection_cls
,
179 enum MHD_RequestTerminationCode toe
) {
182 assert(connection_cls
);
186 log_debug("Cleaning up connection metadata %p", s
);
188 *connection_cls
= NULL
;
192 static int process_http_upload(
193 struct MHD_Connection
*connection
,
194 const char *upload_data
,
195 size_t *upload_data_size
,
196 RemoteSource
*source
) {
198 bool finished
= false;
204 log_trace("%s: connection %p, %zu bytes",
205 __func__
, connection
, *upload_data_size
);
207 if (*upload_data_size
) {
208 log_trace("Received %zu bytes", *upload_data_size
);
210 r
= journal_importer_push_data(&source
->importer
,
211 upload_data
, *upload_data_size
);
213 return mhd_respond_oom(connection
);
215 *upload_data_size
= 0;
220 r
= process_source(source
,
221 journal_remote_server_global
->compress
,
222 journal_remote_server_global
->seal
);
227 log_warning_errno(r
, "Entry is above the maximum of %u, aborting connection %p.",
228 DATA_SIZE_MAX
, connection
);
229 else if (r
== -E2BIG
)
230 log_warning_errno(r
, "Entry with more fields than the maximum of %u, aborting connection %p.",
231 ENTRY_FIELD_COUNT_MAX
, connection
);
233 log_warning_errno(r
, "Failed to process data, aborting connection %p: %m",
242 /* The upload is finished */
244 remaining
= journal_importer_bytes_remaining(&source
->importer
);
246 log_warning("Premature EOF byte. %zu bytes lost.", remaining
);
247 return mhd_respondf(connection
,
248 0, MHD_HTTP_EXPECTATION_FAILED
,
249 "Premature EOF. %zu bytes of trailing data not processed.",
253 return mhd_respond(connection
, MHD_HTTP_ACCEPTED
, "OK.");
256 static mhd_result
request_handler(
258 struct MHD_Connection
*connection
,
262 const char *upload_data
,
263 size_t *upload_data_size
,
264 void **connection_cls
) {
268 _cleanup_free_
char *hostname
= NULL
;
269 bool chunked
= false;
272 assert(connection_cls
);
276 log_trace("Handling a connection %s %s %s", method
, url
, version
);
279 return process_http_upload(connection
,
280 upload_data
, upload_data_size
,
283 if (!streq(method
, "POST"))
284 return mhd_respond(connection
, MHD_HTTP_NOT_ACCEPTABLE
, "Unsupported method.");
286 if (!streq(url
, "/upload"))
287 return mhd_respond(connection
, MHD_HTTP_NOT_FOUND
, "Not found.");
289 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Type");
290 if (!header
|| !streq(header
, "application/vnd.fdo.journal"))
291 return mhd_respond(connection
, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE
,
292 "Content-Type: application/vnd.fdo.journal is required.");
294 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Transfer-Encoding");
296 if (!strcaseeq(header
, "chunked"))
297 return mhd_respondf(connection
, 0, MHD_HTTP_BAD_REQUEST
,
298 "Unsupported Transfer-Encoding type: %s", header
);
303 header
= MHD_lookup_connection_value(connection
, MHD_HEADER_KIND
, "Content-Length");
308 return mhd_respond(connection
, MHD_HTTP_BAD_REQUEST
,
309 "Content-Length must not specified when Transfer-Encoding type is 'chuncked'");
311 r
= safe_atozu(header
, &len
);
313 return mhd_respondf(connection
, r
, MHD_HTTP_LENGTH_REQUIRED
,
314 "Content-Length: %s cannot be parsed: %m", header
);
316 if (len
> ENTRY_SIZE_MAX
)
317 /* When serialized, an entry of maximum size might be slightly larger,
318 * so this does not correspond exactly to the limit in journald. Oh well.
320 return mhd_respondf(connection
, 0, MHD_HTTP_PAYLOAD_TOO_LARGE
,
321 "Payload larger than maximum size of %u bytes", ENTRY_SIZE_MAX
);
325 const union MHD_ConnectionInfo
*ci
;
327 ci
= MHD_get_connection_info(connection
,
328 MHD_CONNECTION_INFO_CONNECTION_FD
);
330 log_error("MHD_get_connection_info failed: cannot get remote fd");
331 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
332 "Cannot check remote address.");
339 if (journal_remote_server_global
->check_trust
) {
340 r
= check_permissions(connection
, &code
, &hostname
);
344 r
= getpeername_pretty(fd
, false, &hostname
);
346 return mhd_respond(connection
, MHD_HTTP_INTERNAL_SERVER_ERROR
,
347 "Cannot check remote hostname.");
352 r
= request_meta(connection_cls
, fd
, hostname
);
354 return respond_oom(connection
);
356 return mhd_respondf(connection
, r
, MHD_HTTP_INTERNAL_SERVER_ERROR
, "%m");
362 static int setup_microhttpd_server(RemoteServer
*s
,
367 struct MHD_OptionItem opts
[] = {
368 { MHD_OPTION_NOTIFY_COMPLETED
, (intptr_t) request_meta_free
},
369 { MHD_OPTION_EXTERNAL_LOGGER
, (intptr_t) microhttpd_logger
},
370 { MHD_OPTION_LISTEN_SOCKET
, fd
},
371 { MHD_OPTION_CONNECTION_MEMORY_LIMIT
, 128*1024},
384 const union MHD_DaemonInfo
*info
;
390 r
= fd_nonblock(fd
, true);
392 return log_error_errno(r
, "Failed to make fd:%d nonblocking: %m", fd
);
394 /* MHD_OPTION_STRICT_FOR_CLIENT is introduced in microhttpd 0.9.54,
395 * and MHD_USE_PEDANTIC_CHECKS will be deprecated in future.
396 * If MHD_USE_PEDANTIC_CHECKS is '#define'd, then it is deprecated
397 * and we should use MHD_OPTION_STRICT_FOR_CLIENT. On the other hand,
398 * if MHD_USE_PEDANTIC_CHECKS is not '#define'd, then it is not
399 * deprecated yet and there exists an enum element with the same name.
400 * So we can safely use it. */
401 #ifdef MHD_USE_PEDANTIC_CHECKS
402 opts
[opts_pos
++] = (struct MHD_OptionItem
)
403 {MHD_OPTION_STRICT_FOR_CLIENT
, 1};
405 flags
|= MHD_USE_PEDANTIC_CHECKS
;
411 opts
[opts_pos
++] = (struct MHD_OptionItem
)
412 {MHD_OPTION_HTTPS_MEM_KEY
, 0, (char*) key
};
413 opts
[opts_pos
++] = (struct MHD_OptionItem
)
414 {MHD_OPTION_HTTPS_MEM_CERT
, 0, (char*) cert
};
416 flags
|= MHD_USE_TLS
;
419 opts
[opts_pos
++] = (struct MHD_OptionItem
)
420 {MHD_OPTION_HTTPS_MEM_TRUST
, 0, (char*) trust
};
423 d
= new(MHDDaemonWrapper
, 1);
427 d
->fd
= (uint64_t) fd
;
429 d
->daemon
= MHD_start_daemon(flags
, 0,
431 request_handler
, NULL
,
432 MHD_OPTION_ARRAY
, opts
,
435 log_error("Failed to start µhttp daemon");
440 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
441 key
? "HTTPS" : "HTTP", fd
, d
);
443 info
= MHD_get_daemon_info(d
->daemon
, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY
);
445 log_error("µhttp returned NULL daemon info");
450 epoll_fd
= info
->listen_fd
;
452 log_error("µhttp epoll fd is invalid");
457 r
= sd_event_add_io(s
->events
, &d
->io_event
,
459 dispatch_http_event
, d
);
461 log_error_errno(r
, "Failed to add event callback: %m");
465 r
= sd_event_source_set_description(d
->io_event
, "io_event");
467 log_error_errno(r
, "Failed to set source name: %m");
471 r
= sd_event_add_time(s
->events
, &d
->timer_event
,
472 CLOCK_MONOTONIC
, (uint64_t) -1, 0,
473 null_timer_event_handler
, d
);
475 log_error_errno(r
, "Failed to add timer_event: %m");
479 r
= sd_event_source_set_description(d
->timer_event
, "timer_event");
481 log_error_errno(r
, "Failed to set source name: %m");
485 r
= hashmap_ensure_allocated(&s
->daemons
, &uint64_hash_ops
);
491 r
= hashmap_put(s
->daemons
, &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(
599 r
= journal_remote_server_init(s
, arg_output
, arg_split_mode
, arg_compress
, arg_seal
);
603 r
= setup_signals(s
);
605 return log_error_errno(r
, "Failed to set up signals: %m");
607 n
= sd_listen_fds(true);
609 return log_error_errno(n
, "Failed to read listening file descriptors from environment: %m");
611 log_debug("Received %d descriptors", n
);
613 if (MAX(http_socket
, https_socket
) >= SD_LISTEN_FDS_START
+ n
)
614 return log_error_errno(SYNTHETIC_ERRNO(EBADFD
),
615 "Received fewer sockets than expected");
617 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
618 if (sd_is_socket(fd
, AF_UNSPEC
, 0, true)) {
619 log_debug("Received a listening socket (fd:%d)", fd
);
621 if (fd
== http_socket
)
622 r
= setup_microhttpd_server(s
, fd
, NULL
, NULL
, NULL
);
623 else if (fd
== https_socket
)
624 r
= setup_microhttpd_server(s
, fd
, key
, cert
, trust
);
626 r
= journal_remote_add_raw_socket(s
, fd
);
627 } else if (sd_is_socket(fd
, AF_UNSPEC
, 0, false)) {
630 r
= getpeername_pretty(fd
, false, &hostname
);
632 return log_error_errno(r
, "Failed to retrieve remote name: %m");
634 log_debug("Received a connection socket (fd:%d) from %s", fd
, hostname
);
636 r
= journal_remote_add_source(s
, fd
, hostname
, true);
638 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
639 "Unknown socket passed on fd:%d", fd
);
642 return log_error_errno(r
, "Failed to register socket (fd:%d): %m", fd
);
646 log_info("Spawning getter %s...", arg_getter
);
647 fd
= spawn_getter(arg_getter
);
651 r
= journal_remote_add_source(s
, fd
, (char*) arg_output
, false);
657 const char *url
, *hostname
;
659 if (!strstr(arg_url
, "/entries")) {
660 if (endswith(arg_url
, "/"))
661 url
= strjoina(arg_url
, "entries");
663 url
= strjoina(arg_url
, "/entries");
665 url
= strdupa(arg_url
);
667 log_info("Spawning curl %s...", url
);
668 fd
= spawn_curl(url
);
672 hostname
= STARTSWITH_SET(arg_url
, "https://", "http://");
676 hostname
= strndupa(hostname
, strcspn(hostname
, "/:"));
678 r
= journal_remote_add_source(s
, fd
, (char *) hostname
, false);
683 if (arg_listen_raw
) {
684 log_debug("Listening on a socket...");
685 r
= setup_raw_socket(s
, arg_listen_raw
);
690 if (arg_listen_http
) {
691 r
= setup_microhttpd_socket(s
, arg_listen_http
, NULL
, NULL
, NULL
);
696 if (arg_listen_https
) {
697 r
= setup_microhttpd_socket(s
, arg_listen_https
, key
, cert
, trust
);
702 STRV_FOREACH(file
, arg_files
) {
703 const char *output_name
;
705 if (streq(*file
, "-")) {
706 log_debug("Using standard input as source.");
709 output_name
= "stdin";
711 log_debug("Reading file %s...", *file
);
713 fd
= open(*file
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
715 return log_error_errno(errno
, "Failed to open %s: %m", *file
);
719 r
= journal_remote_add_source(s
, fd
, (char*) output_name
, false);
725 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
726 "Zero sources specified");
728 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_NONE
) {
729 /* In this case we know what the writer will be
730 called, so we can create it and verify that we can
731 create output as expected. */
732 r
= journal_remote_get_writer(s
, NULL
, &s
->_single_writer
);
740 static int negative_fd(const char *spec
) {
741 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
745 r
= safe_atoi(spec
, &fd
);
755 static int parse_config(void) {
756 const ConfigTableItem items
[] = {
757 { "Remote", "Seal", config_parse_bool
, 0, &arg_seal
},
758 { "Remote", "SplitMode", config_parse_write_split_mode
, 0, &arg_split_mode
},
759 { "Remote", "ServerKeyFile", config_parse_path
, 0, &arg_key
},
760 { "Remote", "ServerCertificateFile", config_parse_path
, 0, &arg_cert
},
761 { "Remote", "TrustedCertificateFile", config_parse_path
, 0, &arg_trust
},
765 return config_parse_many_nulstr(
766 PKGSYSCONFDIR
"/journal-remote.conf",
767 CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"),
769 config_item_table_lookup
, items
,
775 static int help(void) {
776 _cleanup_free_
char *link
= NULL
;
779 r
= terminal_urlify_man("systemd-journal-remote.service", "8", &link
);
783 printf("%s [OPTIONS...] {FILE|-}...\n\n"
784 "Write external journal events to journal file(s).\n\n"
785 " -h --help Show this help\n"
786 " --version Show package version\n"
787 " --url=URL Read events from systemd-journal-gatewayd at URL\n"
788 " --getter=COMMAND Read events from the output of COMMAND\n"
789 " --listen-raw=ADDR Listen for connections at ADDR\n"
790 " --listen-http=ADDR Listen for HTTP connections at ADDR\n"
791 " --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
792 " -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
793 " --compress[=BOOL] Use compression in the output journal (default: yes)\n"
794 " --seal[=BOOL] Use event sealing (default: no)\n"
795 " --key=FILENAME SSL key in PEM format (default:\n"
796 " \"" PRIV_KEY_FILE
"\")\n"
797 " --cert=FILENAME SSL certificate in PEM format (default:\n"
798 " \"" CERT_FILE
"\")\n"
799 " --trust=FILENAME|all SSL CA certificate or disable checking (default:\n"
800 " \"" TRUST_FILE
"\")\n"
801 " --gnutls-log=CATEGORY...\n"
802 " Specify a list of gnutls logging categories\n"
803 " --split-mode=none|host How many output files to create\n"
804 "\nNote: file descriptors from sd_listen_fds() will be consumed, too.\n"
805 "\nSee the %s for details.\n"
806 , program_invocation_short_name
813 static int parse_argv(int argc
, char *argv
[]) {
830 static const struct option options
[] = {
831 { "help", no_argument
, NULL
, 'h' },
832 { "version", no_argument
, NULL
, ARG_VERSION
},
833 { "url", required_argument
, NULL
, ARG_URL
},
834 { "getter", required_argument
, NULL
, ARG_GETTER
},
835 { "listen-raw", required_argument
, NULL
, ARG_LISTEN_RAW
},
836 { "listen-http", required_argument
, NULL
, ARG_LISTEN_HTTP
},
837 { "listen-https", required_argument
, NULL
, ARG_LISTEN_HTTPS
},
838 { "output", required_argument
, NULL
, 'o' },
839 { "split-mode", required_argument
, NULL
, ARG_SPLIT_MODE
},
840 { "compress", optional_argument
, NULL
, ARG_COMPRESS
},
841 { "seal", optional_argument
, NULL
, ARG_SEAL
},
842 { "key", required_argument
, NULL
, ARG_KEY
},
843 { "cert", required_argument
, NULL
, ARG_CERT
},
844 { "trust", required_argument
, NULL
, ARG_TRUST
},
845 { "gnutls-log", required_argument
, NULL
, ARG_GNUTLS_LOG
},
855 while ((c
= getopt_long(argc
, argv
, "ho:", options
, NULL
)) >= 0)
866 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
867 "cannot currently set more than one --url");
874 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
875 "cannot currently use --getter more than once");
882 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
883 "cannot currently use --listen-raw more than once");
885 arg_listen_raw
= optarg
;
888 case ARG_LISTEN_HTTP
:
889 if (arg_listen_http
|| http_socket
>= 0)
890 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
891 "cannot currently use --listen-http more than once");
893 r
= negative_fd(optarg
);
897 arg_listen_http
= optarg
;
900 case ARG_LISTEN_HTTPS
:
901 if (arg_listen_https
|| https_socket
>= 0)
902 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
903 "cannot currently use --listen-https more than once");
905 r
= negative_fd(optarg
);
909 arg_listen_https
= optarg
;
915 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
916 "Key file specified twice");
918 arg_key
= strdup(optarg
);
926 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
927 "Certificate file specified twice");
929 arg_cert
= strdup(optarg
);
936 if (arg_trust
|| arg_trust_all
)
937 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
938 "Confusing trusted CA configuration");
940 if (streq(optarg
, "all"))
941 arg_trust_all
= true;
944 arg_trust
= strdup(optarg
);
948 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
949 "Option --trust is not available.");
957 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
958 "cannot use --output/-o more than once");
964 arg_split_mode
= journal_write_split_mode_from_string(optarg
);
965 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
966 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
967 "Invalid split mode: %s", optarg
);
972 r
= parse_boolean(optarg
);
974 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
975 "Failed to parse --compress= parameter.");
985 r
= parse_boolean(optarg
);
987 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
988 "Failed to parse --seal= parameter.");
996 case ARG_GNUTLS_LOG
: {
998 const char* p
= optarg
;
1000 _cleanup_free_
char *word
= NULL
;
1002 r
= extract_first_word(&p
, &word
, ",", 0);
1004 return log_error_errno(r
, "Failed to parse --gnutls-log= argument: %m");
1008 if (strv_push(&arg_gnutls_log
, word
) < 0)
1015 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1016 "Option --gnutls-log is not available.");
1024 assert_not_reached("Unknown option code.");
1028 arg_files
= argv
+ optind
;
1030 type_a
= arg_getter
|| !strv_isempty(arg_files
);
1033 || arg_listen_http
|| arg_listen_https
1034 || sd_listen_fds(false) > 0;
1035 if (type_a
&& type_b
)
1036 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1037 "Cannot use file input or --getter with "
1038 "--arg-listen-... or socket activation.");
1041 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1042 "Option --output must be specified with file input or --getter.");
1044 if (!IN_SET(arg_split_mode
, JOURNAL_WRITE_SPLIT_NONE
, _JOURNAL_WRITE_SPLIT_INVALID
))
1045 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1046 "For active sources, only --split-mode=none is allowed.");
1048 arg_split_mode
= JOURNAL_WRITE_SPLIT_NONE
;
1051 if (arg_split_mode
== _JOURNAL_WRITE_SPLIT_INVALID
)
1052 arg_split_mode
= JOURNAL_WRITE_SPLIT_HOST
;
1054 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_NONE
&& arg_output
) {
1055 if (is_dir(arg_output
, true) > 0)
1056 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1057 "For SplitMode=none, output must be a file.");
1058 if (!endswith(arg_output
, ".journal"))
1059 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1060 "For SplitMode=none, output file name must end with .journal.");
1063 if (arg_split_mode
== JOURNAL_WRITE_SPLIT_HOST
1064 && arg_output
&& is_dir(arg_output
, true) <= 0)
1065 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1066 "For SplitMode=host, output must be a directory.");
1068 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1069 journal_write_split_mode_to_string(arg_split_mode
),
1074 return 1 /* work to do */;
1077 static int load_certificates(char **key
, char **cert
, char **trust
) {
1080 r
= read_full_file_full(AT_FDCWD
, arg_key
?: PRIV_KEY_FILE
, READ_FULL_FILE_CONNECT_SOCKET
, NULL
, key
, NULL
);
1082 return log_error_errno(r
, "Failed to read key from file '%s': %m",
1083 arg_key
?: PRIV_KEY_FILE
);
1085 r
= read_full_file_full(AT_FDCWD
, arg_cert
?: CERT_FILE
, READ_FULL_FILE_CONNECT_SOCKET
, NULL
, cert
, NULL
);
1087 return log_error_errno(r
, "Failed to read certificate from file '%s': %m",
1088 arg_cert
?: CERT_FILE
);
1091 log_info("Certificate checking disabled.");
1093 r
= read_full_file_full(AT_FDCWD
, arg_trust
?: TRUST_FILE
, READ_FULL_FILE_CONNECT_SOCKET
, NULL
, trust
, NULL
);
1095 return log_error_errno(r
, "Failed to read CA certificate file '%s': %m",
1096 arg_trust
?: TRUST_FILE
);
1099 if ((arg_listen_raw
|| arg_listen_http
) && *trust
)
1100 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1101 "Option --trust makes all non-HTTPS connections untrusted.");
1106 static int run(int argc
, char **argv
) {
1107 _cleanup_(journal_remote_server_destroy
) RemoteServer s
= {};
1108 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
1109 _cleanup_free_
char *key
= NULL
, *cert
= NULL
, *trust
= NULL
;
1112 log_show_color(true);
1113 log_parse_environment_cli();
1115 /* The journal merging logic potentially needs a lot of fds. */
1116 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1122 r
= parse_argv(argc
, argv
);
1126 if (arg_listen_http
|| arg_listen_https
) {
1127 r
= setup_gnutls_logger(arg_gnutls_log
);
1132 if (arg_listen_https
|| https_socket
>= 0) {
1133 r
= load_certificates(&key
, &cert
, &trust
);
1137 s
.check_trust
= !arg_trust_all
;
1140 r
= create_remoteserver(&s
, key
, cert
, trust
);
1144 r
= sd_event_set_watchdog(s
.events
, true);
1146 return log_error_errno(r
, "Failed to enable watchdog: %m");
1148 log_debug("Watchdog is %sd.", enable_disable(r
> 0));
1150 log_debug("%s running as pid "PID_FMT
,
1151 program_invocation_short_name
, getpid_cached());
1153 notify_message
= notify_start(NOTIFY_READY
, NOTIFY_STOPPING
);
1156 r
= sd_event_get_state(s
.events
);
1159 if (r
== SD_EVENT_FINISHED
)
1162 r
= sd_event_run(s
.events
, -1);
1164 return log_error_errno(r
, "Failed to run event loop: %m");
1167 notify_message
= NULL
;
1168 (void) sd_notifyf(false,
1170 "STATUS=Shutting down after writing %" PRIu64
" entries...", s
.event_count
);
1172 log_info("Finishing after writing %" PRIu64
" entries", s
.event_count
);
1177 DEFINE_MAIN_FUNCTION(run
);