1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
13 #include "conf-parser.h"
14 #include "daemon-util.h"
19 #include "format-util.h"
20 #include "glob-util.h"
21 #include "journal-upload.h"
23 #include "main-func.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "pretty-print.h"
28 #include "process-util.h"
29 #include "rlimit-util.h"
31 #include "signal-util.h"
32 #include "string-util.h"
34 #include "tmpfile-util.h"
37 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
38 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
39 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
40 #define DEFAULT_PORT 19532
42 static const char* arg_url
= NULL
;
43 static const char *arg_key
= NULL
;
44 static const char *arg_cert
= NULL
;
45 static const char *arg_trust
= NULL
;
46 static const char *arg_directory
= NULL
;
47 static char **arg_file
= NULL
;
48 static const char *arg_cursor
= NULL
;
49 static bool arg_after_cursor
= false;
50 static int arg_journal_type
= 0;
51 static const char *arg_machine
= NULL
;
52 static bool arg_merge
= false;
53 static int arg_follow
= -1;
54 static const char *arg_save_state
= NULL
;
56 static void close_fd_input(Uploader
*u
);
58 #define SERVER_ANSWER_KEEP 2048
60 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
62 #define easy_setopt(curl, opt, value, level, cmd) \
64 code = curl_easy_setopt(curl, opt, value); \
67 "curl_easy_setopt " #opt " failed: %s", \
68 curl_easy_strerror(code)); \
73 static size_t output_callback(char *buf
,
81 log_debug("The server answers (%zu bytes): %.*s",
82 size
*nmemb
, (int)(size
*nmemb
), buf
);
84 if (nmemb
&& !u
->answer
) {
85 u
->answer
= strndup(buf
, size
*nmemb
);
87 log_warning("Failed to store server answer (%zu bytes): out of memory", size
*nmemb
);
93 static int check_cursor_updating(Uploader
*u
) {
94 _cleanup_free_
char *temp_path
= NULL
;
95 _cleanup_fclose_
FILE *f
= NULL
;
101 r
= mkdir_parents(u
->state_file
, 0755);
103 return log_error_errno(r
, "Cannot create parent directory of state file %s: %m",
106 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
108 return log_error_errno(r
, "Cannot save state to %s: %m",
110 (void) unlink(temp_path
);
115 static int update_cursor_state(Uploader
*u
) {
116 _cleanup_free_
char *temp_path
= NULL
;
117 _cleanup_fclose_
FILE *f
= NULL
;
120 if (!u
->state_file
|| !u
->last_cursor
)
123 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
128 "# This is private data. Do not parse.\n"
132 r
= fflush_and_check(f
);
136 if (rename(temp_path
, u
->state_file
) < 0) {
145 (void) unlink(temp_path
);
147 (void) unlink(u
->state_file
);
149 return log_error_errno(r
, "Failed to save state %s: %m", u
->state_file
);
152 static int load_cursor_state(Uploader
*u
) {
158 r
= parse_env_file(NULL
, u
->state_file
, "LAST_CURSOR", &u
->last_cursor
);
160 log_debug("State file %s is not present.", u
->state_file
);
162 return log_error_errno(r
, "Failed to read state file %s: %m",
165 log_debug("Last cursor was %s", u
->last_cursor
);
170 int start_upload(Uploader
*u
,
171 size_t (*input_callback
)(void *ptr
,
179 assert(input_callback
);
182 struct curl_slist
*h
;
184 h
= curl_slist_append(NULL
, "Content-Type: application/vnd.fdo.journal");
188 h
= curl_slist_append(h
, "Transfer-Encoding: chunked");
190 curl_slist_free_all(h
);
194 h
= curl_slist_append(h
, "Accept: text/plain");
196 curl_slist_free_all(h
);
206 curl
= curl_easy_init();
208 return log_error_errno(SYNTHETIC_ERRNO(ENOSR
),
209 "Call to curl_easy_init failed.");
211 /* tell it to POST to the URL */
212 easy_setopt(curl
, CURLOPT_POST
, 1L,
213 LOG_ERR
, return -EXFULL
);
215 easy_setopt(curl
, CURLOPT_ERRORBUFFER
, u
->error
,
216 LOG_ERR
, return -EXFULL
);
218 /* set where to write to */
219 easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, output_callback
,
220 LOG_ERR
, return -EXFULL
);
222 easy_setopt(curl
, CURLOPT_WRITEDATA
, data
,
223 LOG_ERR
, return -EXFULL
);
225 /* set where to read from */
226 easy_setopt(curl
, CURLOPT_READFUNCTION
, input_callback
,
227 LOG_ERR
, return -EXFULL
);
229 easy_setopt(curl
, CURLOPT_READDATA
, data
,
230 LOG_ERR
, return -EXFULL
);
232 /* use our special own mime type and chunked transfer */
233 easy_setopt(curl
, CURLOPT_HTTPHEADER
, u
->header
,
234 LOG_ERR
, return -EXFULL
);
237 /* enable verbose for easier tracing */
238 easy_setopt(curl
, CURLOPT_VERBOSE
, 1L, LOG_WARNING
, );
240 easy_setopt(curl
, CURLOPT_USERAGENT
,
241 "systemd-journal-upload " GIT_VERSION
,
244 if (!streq_ptr(arg_key
, "-") && (arg_key
|| startswith(u
->url
, "https://"))) {
245 easy_setopt(curl
, CURLOPT_SSLKEY
, arg_key
?: PRIV_KEY_FILE
,
246 LOG_ERR
, return -EXFULL
);
247 easy_setopt(curl
, CURLOPT_SSLCERT
, arg_cert
?: CERT_FILE
,
248 LOG_ERR
, return -EXFULL
);
251 if (STRPTR_IN_SET(arg_trust
, "-", "all"))
252 easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0,
253 LOG_ERR
, return -EUCLEAN
);
254 else if (arg_trust
|| startswith(u
->url
, "https://"))
255 easy_setopt(curl
, CURLOPT_CAINFO
, arg_trust
?: TRUST_FILE
,
256 LOG_ERR
, return -EXFULL
);
258 if (arg_key
|| arg_trust
)
259 easy_setopt(curl
, CURLOPT_SSLVERSION
, CURL_SSLVERSION_TLSv1
,
264 /* truncate the potential old error message */
271 /* upload to this place */
272 code
= curl_easy_setopt(u
->easy
, CURLOPT_URL
, u
->url
);
274 return log_error_errno(SYNTHETIC_ERRNO(EXFULL
),
275 "curl_easy_setopt CURLOPT_URL failed: %s",
276 curl_easy_strerror(code
));
283 static size_t fd_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
288 assert(nmemb
< SSIZE_MAX
/ size
);
293 assert(!size_multiply_overflow(size
, nmemb
));
295 n
= read(u
->input
, buf
, size
* nmemb
);
296 log_debug("%s: allowed %zu, read %zd", __func__
, size
*nmemb
, n
);
300 u
->uploading
= false;
302 log_error_errno(errno
, "Aborting transfer after read error on input: %m.");
303 return CURL_READFUNC_ABORT
;
306 log_debug("Reached EOF");
311 static void close_fd_input(Uploader
*u
) {
314 u
->input
= safe_close(u
->input
);
318 static int dispatch_fd_input(sd_event_source
*event
,
327 if (revents
& EPOLLHUP
) {
328 log_debug("Received HUP");
333 if (!(revents
& EPOLLIN
)) {
334 log_warning("Unexpected poll event %"PRIu32
".", revents
);
339 log_warning("dispatch_fd_input called when uploading, ignoring.");
343 return start_upload(u
, fd_input_callback
, u
);
346 static int open_file_for_upload(Uploader
*u
, const char *filename
) {
349 if (streq(filename
, "-"))
352 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
354 return log_error_errno(errno
, "Failed to open %s: %m", filename
);
360 r
= sd_event_add_io(u
->events
, &u
->input_event
,
361 fd
, EPOLLIN
, dispatch_fd_input
, u
);
363 if (r
!= -EPERM
|| arg_follow
> 0)
364 return log_error_errno(r
, "Failed to register input event: %m");
366 /* Normal files should just be consumed without polling. */
367 r
= start_upload(u
, fd_input_callback
, u
);
374 static int dispatch_sigterm(sd_event_source
*event
,
375 const struct signalfd_siginfo
*si
,
377 Uploader
*u
= userdata
;
381 log_received_signal(LOG_INFO
, si
);
384 close_journal_input(u
);
386 sd_event_exit(u
->events
, 0);
390 static int setup_signals(Uploader
*u
) {
395 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, -1) >= 0);
397 r
= sd_event_add_signal(u
->events
, &u
->sigterm_event
, SIGTERM
, dispatch_sigterm
, u
);
401 r
= sd_event_add_signal(u
->events
, &u
->sigint_event
, SIGINT
, dispatch_sigterm
, u
);
408 static int setup_uploader(Uploader
*u
, const char *url
, const char *state_file
) {
410 const char *host
, *proto
= "";
419 host
= STARTSWITH_SET(url
, "http://", "https://");
425 if (strchr(host
, ':'))
426 u
->url
= strjoin(proto
, url
, "/upload");
433 while (x
> 0 && t
[x
- 1] == '/')
436 u
->url
= strjoin(proto
, t
, ":" STRINGIFY(DEFAULT_PORT
), "/upload");
441 u
->state_file
= state_file
;
443 r
= sd_event_default(&u
->events
);
445 return log_error_errno(r
, "sd_event_default failed: %m");
447 r
= setup_signals(u
);
449 return log_error_errno(r
, "Failed to set up signals: %m");
451 (void) sd_watchdog_enabled(false, &u
->watchdog_usec
);
453 return load_cursor_state(u
);
456 static void destroy_uploader(Uploader
*u
) {
459 curl_easy_cleanup(u
->easy
);
460 curl_slist_free_all(u
->header
);
463 free(u
->last_cursor
);
464 free(u
->current_cursor
);
468 u
->input_event
= sd_event_source_unref(u
->input_event
);
471 close_journal_input(u
);
473 sd_event_source_unref(u
->sigterm_event
);
474 sd_event_source_unref(u
->sigint_event
);
475 sd_event_unref(u
->events
);
478 static int perform_upload(Uploader
*u
) {
484 u
->watchdog_timestamp
= now(CLOCK_MONOTONIC
);
485 code
= curl_easy_perform(u
->easy
);
488 log_error("Upload to %s failed: %.*s",
489 u
->url
, (int) sizeof(u
->error
), u
->error
);
491 log_error("Upload to %s failed: %s",
492 u
->url
, curl_easy_strerror(code
));
496 code
= curl_easy_getinfo(u
->easy
, CURLINFO_RESPONSE_CODE
, &status
);
498 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
499 "Failed to retrieve response code: %s",
500 curl_easy_strerror(code
));
503 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
504 "Upload to %s failed with code %ld: %s",
505 u
->url
, status
, strna(u
->answer
));
506 else if (status
< 200)
507 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
508 "Upload to %s finished with unexpected code %ld: %s",
509 u
->url
, status
, strna(u
->answer
));
511 log_debug("Upload finished successfully with code %ld: %s",
512 status
, strna(u
->answer
));
514 free_and_replace(u
->last_cursor
, u
->current_cursor
);
516 return update_cursor_state(u
);
519 static int config_parse_path_or_ignore(
521 const char *filename
,
524 unsigned section_line
,
531 _cleanup_free_
char *n
= NULL
;
551 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
553 return fatal
? -ENOEXEC
: 0;
556 return free_and_replace(*s
, n
);
559 static int parse_config(void) {
560 const ConfigTableItem items
[] = {
561 { "Upload", "URL", config_parse_string
, 0, &arg_url
},
562 { "Upload", "ServerKeyFile", config_parse_path_or_ignore
, 0, &arg_key
},
563 { "Upload", "ServerCertificateFile", config_parse_path_or_ignore
, 0, &arg_cert
},
564 { "Upload", "TrustedCertificateFile", config_parse_path_or_ignore
, 0, &arg_trust
},
568 return config_parse_many_nulstr(
569 PKGSYSCONFDIR
"/journal-upload.conf",
570 CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
572 config_item_table_lookup
, items
,
578 static int help(void) {
579 _cleanup_free_
char *link
= NULL
;
582 r
= terminal_urlify_man("systemd-journal-upload.service", "8", &link
);
586 printf("%s -u URL {FILE|-}...\n\n"
587 "Upload journal events to a remote server.\n\n"
588 " -h --help Show this help\n"
589 " --version Show package version\n"
590 " -u --url=URL Upload to this address (default port "
591 STRINGIFY(DEFAULT_PORT
) ")\n"
592 " --key=FILENAME Specify key in PEM format (default:\n"
593 " \"" PRIV_KEY_FILE
"\")\n"
594 " --cert=FILENAME Specify certificate in PEM format (default:\n"
595 " \"" CERT_FILE
"\")\n"
596 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
597 " \"" TRUST_FILE
"\")\n"
598 " --system Use the system journal\n"
599 " --user Use the user journal for the current user\n"
600 " -m --merge Use all available journals\n"
601 " -M --machine=CONTAINER Operate on local container\n"
602 " -D --directory=PATH Use journal files from directory\n"
603 " --file=PATH Use this journal file\n"
604 " --cursor=CURSOR Start at the specified cursor\n"
605 " --after-cursor=CURSOR Start after the specified cursor\n"
606 " --follow[=BOOL] Do [not] wait for input\n"
607 " --save-state[=FILE] Save uploaded cursors (default \n"
609 "\nSee the %s for details.\n"
610 , program_invocation_short_name
617 static int parse_argv(int argc
, char *argv
[]) {
632 static const struct option options
[] = {
633 { "help", no_argument
, NULL
, 'h' },
634 { "version", no_argument
, NULL
, ARG_VERSION
},
635 { "url", required_argument
, NULL
, 'u' },
636 { "key", required_argument
, NULL
, ARG_KEY
},
637 { "cert", required_argument
, NULL
, ARG_CERT
},
638 { "trust", required_argument
, NULL
, ARG_TRUST
},
639 { "system", no_argument
, NULL
, ARG_SYSTEM
},
640 { "user", no_argument
, NULL
, ARG_USER
},
641 { "merge", no_argument
, NULL
, 'm' },
642 { "machine", required_argument
, NULL
, 'M' },
643 { "directory", required_argument
, NULL
, 'D' },
644 { "file", required_argument
, NULL
, ARG_FILE
},
645 { "cursor", required_argument
, NULL
, ARG_CURSOR
},
646 { "after-cursor", required_argument
, NULL
, ARG_AFTER_CURSOR
},
647 { "follow", optional_argument
, NULL
, ARG_FOLLOW
},
648 { "save-state", optional_argument
, NULL
, ARG_SAVE_STATE
},
659 while ((c
= getopt_long(argc
, argv
, "hu:mM:D:", options
, NULL
)) >= 0)
669 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
670 "cannot use more than one --url");
677 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
678 "cannot use more than one --key");
685 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
686 "cannot use more than one --cert");
693 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
694 "cannot use more than one --trust");
700 arg_journal_type
|= SD_JOURNAL_SYSTEM
;
704 arg_journal_type
|= SD_JOURNAL_CURRENT_USER
;
713 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
714 "cannot use more than one --machine/-M");
716 arg_machine
= optarg
;
721 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
722 "cannot use more than one --directory/-D");
724 arg_directory
= optarg
;
728 r
= glob_extend(&arg_file
, optarg
, GLOB_NOCHECK
);
730 return log_error_errno(r
, "Failed to add paths: %m");
735 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
736 "cannot use more than one --cursor/--after-cursor");
741 case ARG_AFTER_CURSOR
:
743 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
744 "cannot use more than one --cursor/--after-cursor");
747 arg_after_cursor
= true;
752 r
= parse_boolean(optarg
);
754 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
755 "Failed to parse --follow= parameter.");
764 arg_save_state
= optarg
?: STATE_FILE
;
768 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
769 "Unknown option %s.",
773 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
774 "Missing argument to %s.",
778 assert_not_reached("Unhandled option code.");
782 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
783 "Required --url/-u option missing.");
785 if (!!arg_key
!= !!arg_cert
)
786 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
787 "Options --key and --cert must be used together.");
789 if (optind
< argc
&& (arg_directory
|| arg_file
|| arg_machine
|| arg_journal_type
))
790 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
791 "Input arguments make no sense with journal input.");
796 static int open_journal(sd_journal
**j
) {
800 r
= sd_journal_open_directory(j
, arg_directory
, arg_journal_type
);
802 r
= sd_journal_open_files(j
, (const char**) arg_file
, 0);
803 else if (arg_machine
) {
804 #pragma GCC diagnostic push
805 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
806 /* FIXME: replace with D-Bus call OpenMachineRootDirectory() so that things also work with raw disk images */
807 r
= sd_journal_open_container(j
, arg_machine
, 0);
808 #pragma GCC diagnostic pop
810 r
= sd_journal_open(j
, !arg_merge
*SD_JOURNAL_LOCAL_ONLY
+ arg_journal_type
);
812 log_error_errno(r
, "Failed to open %s: %m",
813 arg_directory
? arg_directory
: arg_file
? "files" : "journal");
817 static int run(int argc
, char **argv
) {
818 _cleanup_(destroy_uploader
) Uploader u
= {};
819 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
823 log_show_color(true);
824 log_parse_environment_cli();
826 /* The journal merging logic potentially needs a lot of fds. */
827 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
833 r
= parse_argv(argc
, argv
);
839 r
= setup_uploader(&u
, arg_url
, arg_save_state
);
843 sd_event_set_watchdog(u
.events
, true);
845 r
= check_cursor_updating(&u
);
849 log_debug("%s running as pid "PID_FMT
,
850 program_invocation_short_name
, getpid_cached());
852 use_journal
= optind
>= argc
;
855 r
= open_journal(&j
);
858 r
= open_journal_for_upload(&u
, j
,
859 arg_cursor
?: u
.last_cursor
,
860 arg_cursor
? arg_after_cursor
: true,
866 notify_message
= notify_start("READY=1\n"
867 "STATUS=Processing input...",
871 r
= sd_event_get_state(u
.events
);
874 if (r
== SD_EVENT_FINISHED
)
881 r
= check_journal_input(&u
);
882 } else if (u
.input
< 0 && !use_journal
) {
886 log_debug("Using %s as input.", argv
[optind
]);
887 r
= open_file_for_upload(&u
, argv
[optind
++]);
893 r
= perform_upload(&u
);
898 r
= sd_event_run(u
.events
, u
.timeout
);
900 return log_error_errno(r
, "Failed to run event loop: %m");
904 DEFINE_MAIN_FUNCTION(run
);