1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "conf-parser.h"
17 #include "format-util.h"
18 #include "glob-util.h"
19 #include "journal-upload.h"
22 #include "parse-util.h"
23 #include "pretty-print.h"
24 #include "process-util.h"
25 #include "rlimit-util.h"
27 #include "signal-util.h"
28 #include "string-util.h"
30 #include "tmpfile-util.h"
33 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
34 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
35 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
36 #define DEFAULT_PORT 19532
38 static const char* arg_url
= NULL
;
39 static const char *arg_key
= NULL
;
40 static const char *arg_cert
= NULL
;
41 static const char *arg_trust
= NULL
;
42 static const char *arg_directory
= NULL
;
43 static char **arg_file
= NULL
;
44 static const char *arg_cursor
= NULL
;
45 static bool arg_after_cursor
= false;
46 static int arg_journal_type
= 0;
47 static const char *arg_machine
= NULL
;
48 static bool arg_merge
= false;
49 static int arg_follow
= -1;
50 static const char *arg_save_state
= NULL
;
52 static void close_fd_input(Uploader
*u
);
54 #define SERVER_ANSWER_KEEP 2048
56 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
58 #define easy_setopt(curl, opt, value, level, cmd) \
60 code = curl_easy_setopt(curl, opt, value); \
63 "curl_easy_setopt " #opt " failed: %s", \
64 curl_easy_strerror(code)); \
69 static size_t output_callback(char *buf
,
77 log_debug("The server answers (%zu bytes): %.*s",
78 size
*nmemb
, (int)(size
*nmemb
), buf
);
80 if (nmemb
&& !u
->answer
) {
81 u
->answer
= strndup(buf
, size
*nmemb
);
83 log_warning("Failed to store server answer (%zu bytes): out of memory", size
*nmemb
);
89 static int check_cursor_updating(Uploader
*u
) {
90 _cleanup_free_
char *temp_path
= NULL
;
91 _cleanup_fclose_
FILE *f
= NULL
;
97 r
= mkdir_parents(u
->state_file
, 0755);
99 return log_error_errno(r
, "Cannot create parent directory of state file %s: %m",
102 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
104 return log_error_errno(r
, "Cannot save state to %s: %m",
111 static int update_cursor_state(Uploader
*u
) {
112 _cleanup_free_
char *temp_path
= NULL
;
113 _cleanup_fclose_
FILE *f
= NULL
;
116 if (!u
->state_file
|| !u
->last_cursor
)
119 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
124 "# This is private data. Do not parse.\n"
128 r
= fflush_and_check(f
);
132 if (rename(temp_path
, u
->state_file
) < 0) {
141 (void) unlink(temp_path
);
143 (void) unlink(u
->state_file
);
145 return log_error_errno(r
, "Failed to save state %s: %m", u
->state_file
);
148 static int load_cursor_state(Uploader
*u
) {
154 r
= parse_env_file(NULL
, u
->state_file
, "LAST_CURSOR", &u
->last_cursor
);
156 log_debug("State file %s is not present.", u
->state_file
);
158 return log_error_errno(r
, "Failed to read state file %s: %m",
161 log_debug("Last cursor was %s", u
->last_cursor
);
166 int start_upload(Uploader
*u
,
167 size_t (*input_callback
)(void *ptr
,
175 assert(input_callback
);
178 struct curl_slist
*h
;
180 h
= curl_slist_append(NULL
, "Content-Type: application/vnd.fdo.journal");
184 h
= curl_slist_append(h
, "Transfer-Encoding: chunked");
186 curl_slist_free_all(h
);
190 h
= curl_slist_append(h
, "Accept: text/plain");
192 curl_slist_free_all(h
);
202 curl
= curl_easy_init();
204 return log_error_errno(SYNTHETIC_ERRNO(ENOSR
),
205 "Call to curl_easy_init failed.");
207 /* tell it to POST to the URL */
208 easy_setopt(curl
, CURLOPT_POST
, 1L,
209 LOG_ERR
, return -EXFULL
);
211 easy_setopt(curl
, CURLOPT_ERRORBUFFER
, u
->error
,
212 LOG_ERR
, return -EXFULL
);
214 /* set where to write to */
215 easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, output_callback
,
216 LOG_ERR
, return -EXFULL
);
218 easy_setopt(curl
, CURLOPT_WRITEDATA
, data
,
219 LOG_ERR
, return -EXFULL
);
221 /* set where to read from */
222 easy_setopt(curl
, CURLOPT_READFUNCTION
, input_callback
,
223 LOG_ERR
, return -EXFULL
);
225 easy_setopt(curl
, CURLOPT_READDATA
, data
,
226 LOG_ERR
, return -EXFULL
);
228 /* use our special own mime type and chunked transfer */
229 easy_setopt(curl
, CURLOPT_HTTPHEADER
, u
->header
,
230 LOG_ERR
, return -EXFULL
);
233 /* enable verbose for easier tracing */
234 easy_setopt(curl
, CURLOPT_VERBOSE
, 1L, LOG_WARNING
, );
236 easy_setopt(curl
, CURLOPT_USERAGENT
,
237 "systemd-journal-upload " PACKAGE_STRING
,
240 if (arg_key
|| startswith(u
->url
, "https://")) {
241 easy_setopt(curl
, CURLOPT_SSLKEY
, arg_key
?: PRIV_KEY_FILE
,
242 LOG_ERR
, return -EXFULL
);
243 easy_setopt(curl
, CURLOPT_SSLCERT
, arg_cert
?: CERT_FILE
,
244 LOG_ERR
, return -EXFULL
);
247 if (streq_ptr(arg_trust
, "all"))
248 easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0,
249 LOG_ERR
, return -EUCLEAN
);
250 else if (arg_trust
|| startswith(u
->url
, "https://"))
251 easy_setopt(curl
, CURLOPT_CAINFO
, arg_trust
?: TRUST_FILE
,
252 LOG_ERR
, return -EXFULL
);
254 if (arg_key
|| arg_trust
)
255 easy_setopt(curl
, CURLOPT_SSLVERSION
, CURL_SSLVERSION_TLSv1
,
260 /* truncate the potential old error message */
267 /* upload to this place */
268 code
= curl_easy_setopt(u
->easy
, CURLOPT_URL
, u
->url
);
270 return log_error_errno(SYNTHETIC_ERRNO(EXFULL
),
271 "curl_easy_setopt CURLOPT_URL failed: %s",
272 curl_easy_strerror(code
));
279 static size_t fd_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
284 assert(nmemb
< SSIZE_MAX
/ size
);
289 assert(!size_multiply_overflow(size
, nmemb
));
291 n
= read(u
->input
, buf
, size
* nmemb
);
292 log_debug("%s: allowed %zu, read %zd", __func__
, size
*nmemb
, n
);
296 u
->uploading
= false;
298 log_error_errno(errno
, "Aborting transfer after read error on input: %m.");
299 return CURL_READFUNC_ABORT
;
302 log_debug("Reached EOF");
307 static void close_fd_input(Uploader
*u
) {
310 u
->input
= safe_close(u
->input
);
314 static int dispatch_fd_input(sd_event_source
*event
,
323 if (revents
& EPOLLHUP
) {
324 log_debug("Received HUP");
329 if (!(revents
& EPOLLIN
)) {
330 log_warning("Unexpected poll event %"PRIu32
".", revents
);
335 log_warning("dispatch_fd_input called when uploading, ignoring.");
339 return start_upload(u
, fd_input_callback
, u
);
342 static int open_file_for_upload(Uploader
*u
, const char *filename
) {
345 if (streq(filename
, "-"))
348 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
350 return log_error_errno(errno
, "Failed to open %s: %m", filename
);
356 r
= sd_event_add_io(u
->events
, &u
->input_event
,
357 fd
, EPOLLIN
, dispatch_fd_input
, u
);
359 if (r
!= -EPERM
|| arg_follow
> 0)
360 return log_error_errno(r
, "Failed to register input event: %m");
362 /* Normal files should just be consumed without polling. */
363 r
= start_upload(u
, fd_input_callback
, u
);
370 static int dispatch_sigterm(sd_event_source
*event
,
371 const struct signalfd_siginfo
*si
,
373 Uploader
*u
= userdata
;
377 log_received_signal(LOG_INFO
, si
);
380 close_journal_input(u
);
382 sd_event_exit(u
->events
, 0);
386 static int setup_signals(Uploader
*u
) {
391 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, -1) >= 0);
393 r
= sd_event_add_signal(u
->events
, &u
->sigterm_event
, SIGTERM
, dispatch_sigterm
, u
);
397 r
= sd_event_add_signal(u
->events
, &u
->sigint_event
, SIGINT
, dispatch_sigterm
, u
);
404 static int setup_uploader(Uploader
*u
, const char *url
, const char *state_file
) {
406 const char *host
, *proto
= "";
415 host
= STARTSWITH_SET(url
, "http://", "https://");
421 if (strchr(host
, ':'))
422 u
->url
= strjoin(proto
, url
, "/upload");
429 while (x
> 0 && t
[x
- 1] == '/')
432 u
->url
= strjoin(proto
, t
, ":" STRINGIFY(DEFAULT_PORT
), "/upload");
437 u
->state_file
= state_file
;
439 r
= sd_event_default(&u
->events
);
441 return log_error_errno(r
, "sd_event_default failed: %m");
443 r
= setup_signals(u
);
445 return log_error_errno(r
, "Failed to set up signals: %m");
447 (void) sd_watchdog_enabled(false, &u
->watchdog_usec
);
449 return load_cursor_state(u
);
452 static void destroy_uploader(Uploader
*u
) {
455 curl_easy_cleanup(u
->easy
);
456 curl_slist_free_all(u
->header
);
459 free(u
->last_cursor
);
460 free(u
->current_cursor
);
464 u
->input_event
= sd_event_source_unref(u
->input_event
);
467 close_journal_input(u
);
469 sd_event_source_unref(u
->sigterm_event
);
470 sd_event_source_unref(u
->sigint_event
);
471 sd_event_unref(u
->events
);
474 static int perform_upload(Uploader
*u
) {
480 u
->watchdog_timestamp
= now(CLOCK_MONOTONIC
);
481 code
= curl_easy_perform(u
->easy
);
484 log_error("Upload to %s failed: %.*s",
485 u
->url
, (int) sizeof(u
->error
), u
->error
);
487 log_error("Upload to %s failed: %s",
488 u
->url
, curl_easy_strerror(code
));
492 code
= curl_easy_getinfo(u
->easy
, CURLINFO_RESPONSE_CODE
, &status
);
494 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
495 "Failed to retrieve response code: %s",
496 curl_easy_strerror(code
));
499 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
500 "Upload to %s failed with code %ld: %s",
501 u
->url
, status
, strna(u
->answer
));
502 else if (status
< 200)
503 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
504 "Upload to %s finished with unexpected code %ld: %s",
505 u
->url
, status
, strna(u
->answer
));
507 log_debug("Upload finished successfully with code %ld: %s",
508 status
, strna(u
->answer
));
510 free_and_replace(u
->last_cursor
, u
->current_cursor
);
512 return update_cursor_state(u
);
515 static int parse_config(void) {
516 const ConfigTableItem items
[] = {
517 { "Upload", "URL", config_parse_string
, 0, &arg_url
},
518 { "Upload", "ServerKeyFile", config_parse_path
, 0, &arg_key
},
519 { "Upload", "ServerCertificateFile", config_parse_path
, 0, &arg_cert
},
520 { "Upload", "TrustedCertificateFile", config_parse_path
, 0, &arg_trust
},
523 return config_parse_many_nulstr(PKGSYSCONFDIR
"/journal-upload.conf",
524 CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
525 "Upload\0", config_item_table_lookup
, items
,
526 CONFIG_PARSE_WARN
, NULL
);
529 static int help(void) {
530 _cleanup_free_
char *link
= NULL
;
533 r
= terminal_urlify_man("systemd-journal-upload.service", "8", &link
);
537 printf("%s -u URL {FILE|-}...\n\n"
538 "Upload journal events to a remote server.\n\n"
539 " -h --help Show this help\n"
540 " --version Show package version\n"
541 " -u --url=URL Upload to this address (default port "
542 STRINGIFY(DEFAULT_PORT
) ")\n"
543 " --key=FILENAME Specify key in PEM format (default:\n"
544 " \"" PRIV_KEY_FILE
"\")\n"
545 " --cert=FILENAME Specify certificate in PEM format (default:\n"
546 " \"" CERT_FILE
"\")\n"
547 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
548 " \"" TRUST_FILE
"\")\n"
549 " --system Use the system journal\n"
550 " --user Use the user journal for the current user\n"
551 " -m --merge Use all available journals\n"
552 " -M --machine=CONTAINER Operate on local container\n"
553 " -D --directory=PATH Use journal files from directory\n"
554 " --file=PATH Use this journal file\n"
555 " --cursor=CURSOR Start at the specified cursor\n"
556 " --after-cursor=CURSOR Start after the specified cursor\n"
557 " --follow[=BOOL] Do [not] wait for input\n"
558 " --save-state[=FILE] Save uploaded cursors (default \n"
560 "\nSee the %s for details.\n"
561 , program_invocation_short_name
568 static int parse_argv(int argc
, char *argv
[]) {
583 static const struct option options
[] = {
584 { "help", no_argument
, NULL
, 'h' },
585 { "version", no_argument
, NULL
, ARG_VERSION
},
586 { "url", required_argument
, NULL
, 'u' },
587 { "key", required_argument
, NULL
, ARG_KEY
},
588 { "cert", required_argument
, NULL
, ARG_CERT
},
589 { "trust", required_argument
, NULL
, ARG_TRUST
},
590 { "system", no_argument
, NULL
, ARG_SYSTEM
},
591 { "user", no_argument
, NULL
, ARG_USER
},
592 { "merge", no_argument
, NULL
, 'm' },
593 { "machine", required_argument
, NULL
, 'M' },
594 { "directory", required_argument
, NULL
, 'D' },
595 { "file", required_argument
, NULL
, ARG_FILE
},
596 { "cursor", required_argument
, NULL
, ARG_CURSOR
},
597 { "after-cursor", required_argument
, NULL
, ARG_AFTER_CURSOR
},
598 { "follow", optional_argument
, NULL
, ARG_FOLLOW
},
599 { "save-state", optional_argument
, NULL
, ARG_SAVE_STATE
},
610 while ((c
= getopt_long(argc
, argv
, "hu:mM:D:", options
, NULL
)) >= 0)
620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
621 "cannot use more than one --url");
628 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
629 "cannot use more than one --key");
636 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
637 "cannot use more than one --cert");
644 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
645 "cannot use more than one --trust");
651 arg_journal_type
|= SD_JOURNAL_SYSTEM
;
655 arg_journal_type
|= SD_JOURNAL_CURRENT_USER
;
664 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
665 "cannot use more than one --machine/-M");
667 arg_machine
= optarg
;
672 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
673 "cannot use more than one --directory/-D");
675 arg_directory
= optarg
;
679 r
= glob_extend(&arg_file
, optarg
);
681 return log_error_errno(r
, "Failed to add paths: %m");
686 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
687 "cannot use more than one --cursor/--after-cursor");
692 case ARG_AFTER_CURSOR
:
694 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
695 "cannot use more than one --cursor/--after-cursor");
698 arg_after_cursor
= true;
703 r
= parse_boolean(optarg
);
705 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
706 "Failed to parse --follow= parameter.");
715 arg_save_state
= optarg
?: STATE_FILE
;
719 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
720 "Unknown option %s.",
724 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
725 "Missing argument to %s.",
729 assert_not_reached("Unhandled option code.");
733 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
734 "Required --url/-u option missing.");
736 if (!!arg_key
!= !!arg_cert
)
737 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
738 "Options --key and --cert must be used together.");
740 if (optind
< argc
&& (arg_directory
|| arg_file
|| arg_machine
|| arg_journal_type
))
741 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
742 "Input arguments make no sense with journal input.");
747 static int open_journal(sd_journal
**j
) {
751 r
= sd_journal_open_directory(j
, arg_directory
, arg_journal_type
);
753 r
= sd_journal_open_files(j
, (const char**) arg_file
, 0);
754 else if (arg_machine
)
755 r
= sd_journal_open_container(j
, arg_machine
, 0);
757 r
= sd_journal_open(j
, !arg_merge
*SD_JOURNAL_LOCAL_ONLY
+ arg_journal_type
);
759 log_error_errno(r
, "Failed to open %s: %m",
760 arg_directory
? arg_directory
: arg_file
? "files" : "journal");
764 int main(int argc
, char **argv
) {
769 log_show_color(true);
770 log_parse_environment();
772 /* The journal merging logic potentially needs a lot of fds. */
773 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
779 r
= parse_argv(argc
, argv
);
785 r
= setup_uploader(&u
, arg_url
, arg_save_state
);
789 sd_event_set_watchdog(u
.events
, true);
791 r
= check_cursor_updating(&u
);
795 log_debug("%s running as pid "PID_FMT
,
796 program_invocation_short_name
, getpid_cached());
798 use_journal
= optind
>= argc
;
801 r
= open_journal(&j
);
804 r
= open_journal_for_upload(&u
, j
,
805 arg_cursor
?: u
.last_cursor
,
806 arg_cursor
? arg_after_cursor
: true,
814 "STATUS=Processing input...");
817 r
= sd_event_get_state(u
.events
);
820 if (r
== SD_EVENT_FINISHED
)
827 r
= check_journal_input(&u
);
828 } else if (u
.input
< 0 && !use_journal
) {
832 log_debug("Using %s as input.", argv
[optind
]);
833 r
= open_file_for_upload(&u
, argv
[optind
++]);
839 r
= perform_upload(&u
);
844 r
= sd_event_run(u
.events
, u
.timeout
);
846 log_error_errno(r
, "Failed to run event loop: %m");
854 "STATUS=Shutting down...");
856 destroy_uploader(&u
);
859 return r
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;