1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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-argument.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "pretty-print.h"
29 #include "process-util.h"
30 #include "rlimit-util.h"
32 #include "signal-util.h"
33 #include "string-util.h"
35 #include "tmpfile-util.h"
38 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
39 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
40 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
41 #define DEFAULT_PORT 19532
43 static const char* arg_url
= NULL
;
44 static const char *arg_key
= NULL
;
45 static const char *arg_cert
= NULL
;
46 static const char *arg_trust
= NULL
;
47 static const char *arg_directory
= NULL
;
48 static char **arg_file
= NULL
;
49 static const char *arg_cursor
= NULL
;
50 static bool arg_after_cursor
= false;
51 static int arg_journal_type
= 0;
52 static const char *arg_machine
= NULL
;
53 static bool arg_merge
= false;
54 static int arg_follow
= -1;
55 static const char *arg_save_state
= NULL
;
57 static void close_fd_input(Uploader
*u
);
59 #define SERVER_ANSWER_KEEP 2048
61 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
63 #define easy_setopt(curl, opt, value, level, cmd) \
65 code = curl_easy_setopt(curl, opt, value); \
68 "curl_easy_setopt " #opt " failed: %s", \
69 curl_easy_strerror(code)); \
74 static size_t output_callback(char *buf
,
82 log_debug("The server answers (%zu bytes): %.*s",
83 size
*nmemb
, (int)(size
*nmemb
), buf
);
85 if (nmemb
&& !u
->answer
) {
86 u
->answer
= strndup(buf
, size
*nmemb
);
88 log_warning("Failed to store server answer (%zu bytes): out of memory", size
*nmemb
);
94 static int check_cursor_updating(Uploader
*u
) {
95 _cleanup_free_
char *temp_path
= NULL
;
96 _cleanup_fclose_
FILE *f
= NULL
;
102 r
= mkdir_parents(u
->state_file
, 0755);
104 return log_error_errno(r
, "Cannot create parent directory of state file %s: %m",
107 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
109 return log_error_errno(r
, "Cannot save state to %s: %m",
111 (void) unlink(temp_path
);
116 static int update_cursor_state(Uploader
*u
) {
117 _cleanup_free_
char *temp_path
= NULL
;
118 _cleanup_fclose_
FILE *f
= NULL
;
121 if (!u
->state_file
|| !u
->last_cursor
)
124 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
129 "# This is private data. Do not parse.\n"
133 r
= fflush_and_check(f
);
137 if (rename(temp_path
, u
->state_file
) < 0) {
146 (void) unlink(temp_path
);
148 (void) unlink(u
->state_file
);
150 return log_error_errno(r
, "Failed to save state %s: %m", u
->state_file
);
153 static int load_cursor_state(Uploader
*u
) {
159 r
= parse_env_file(NULL
, u
->state_file
, "LAST_CURSOR", &u
->last_cursor
);
161 log_debug("State file %s is not present.", u
->state_file
);
163 return log_error_errno(r
, "Failed to read state file %s: %m",
166 log_debug("Last cursor was %s", u
->last_cursor
);
171 int start_upload(Uploader
*u
,
172 size_t (*input_callback
)(void *ptr
,
180 assert(input_callback
);
183 struct curl_slist
*h
;
185 h
= curl_slist_append(NULL
, "Content-Type: application/vnd.fdo.journal");
189 h
= curl_slist_append(h
, "Transfer-Encoding: chunked");
191 curl_slist_free_all(h
);
195 h
= curl_slist_append(h
, "Accept: text/plain");
197 curl_slist_free_all(h
);
207 curl
= curl_easy_init();
209 return log_error_errno(SYNTHETIC_ERRNO(ENOSR
),
210 "Call to curl_easy_init failed.");
212 /* tell it to POST to the URL */
213 easy_setopt(curl
, CURLOPT_POST
, 1L,
214 LOG_ERR
, return -EXFULL
);
216 easy_setopt(curl
, CURLOPT_ERRORBUFFER
, u
->error
,
217 LOG_ERR
, return -EXFULL
);
219 /* set where to write to */
220 easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, output_callback
,
221 LOG_ERR
, return -EXFULL
);
223 easy_setopt(curl
, CURLOPT_WRITEDATA
, data
,
224 LOG_ERR
, return -EXFULL
);
226 /* set where to read from */
227 easy_setopt(curl
, CURLOPT_READFUNCTION
, input_callback
,
228 LOG_ERR
, return -EXFULL
);
230 easy_setopt(curl
, CURLOPT_READDATA
, data
,
231 LOG_ERR
, return -EXFULL
);
233 /* use our special own mime type and chunked transfer */
234 easy_setopt(curl
, CURLOPT_HTTPHEADER
, u
->header
,
235 LOG_ERR
, return -EXFULL
);
238 /* enable verbose for easier tracing */
239 easy_setopt(curl
, CURLOPT_VERBOSE
, 1L, LOG_WARNING
, );
241 easy_setopt(curl
, CURLOPT_USERAGENT
,
242 "systemd-journal-upload " GIT_VERSION
,
245 if (!streq_ptr(arg_key
, "-") && (arg_key
|| startswith(u
->url
, "https://"))) {
246 easy_setopt(curl
, CURLOPT_SSLKEY
, arg_key
?: PRIV_KEY_FILE
,
247 LOG_ERR
, return -EXFULL
);
248 easy_setopt(curl
, CURLOPT_SSLCERT
, arg_cert
?: CERT_FILE
,
249 LOG_ERR
, return -EXFULL
);
252 if (STRPTR_IN_SET(arg_trust
, "-", "all"))
253 easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0,
254 LOG_ERR
, return -EUCLEAN
);
255 else if (arg_trust
|| startswith(u
->url
, "https://"))
256 easy_setopt(curl
, CURLOPT_CAINFO
, arg_trust
?: TRUST_FILE
,
257 LOG_ERR
, return -EXFULL
);
259 if (arg_key
|| arg_trust
)
260 easy_setopt(curl
, CURLOPT_SSLVERSION
, CURL_SSLVERSION_TLSv1
,
265 /* truncate the potential old error message */
272 /* upload to this place */
273 code
= curl_easy_setopt(u
->easy
, CURLOPT_URL
, u
->url
);
275 return log_error_errno(SYNTHETIC_ERRNO(EXFULL
),
276 "curl_easy_setopt CURLOPT_URL failed: %s",
277 curl_easy_strerror(code
));
284 static size_t fd_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
289 assert(nmemb
< SSIZE_MAX
/ size
);
294 assert(!size_multiply_overflow(size
, nmemb
));
296 n
= read(u
->input
, buf
, size
* nmemb
);
297 log_debug("%s: allowed %zu, read %zd", __func__
, size
*nmemb
, n
);
301 u
->uploading
= false;
303 log_error_errno(errno
, "Aborting transfer after read error on input: %m.");
304 return CURL_READFUNC_ABORT
;
307 log_debug("Reached EOF");
312 static void close_fd_input(Uploader
*u
) {
315 u
->input
= safe_close(u
->input
);
319 static int dispatch_fd_input(sd_event_source
*event
,
328 if (revents
& EPOLLHUP
) {
329 log_debug("Received HUP");
334 if (!(revents
& EPOLLIN
)) {
335 log_warning("Unexpected poll event %"PRIu32
".", revents
);
340 log_warning("dispatch_fd_input called when uploading, ignoring.");
344 return start_upload(u
, fd_input_callback
, u
);
347 static int open_file_for_upload(Uploader
*u
, const char *filename
) {
350 if (streq(filename
, "-"))
353 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
355 return log_error_errno(errno
, "Failed to open %s: %m", filename
);
360 if (arg_follow
!= 0) {
361 r
= sd_event_add_io(u
->events
, &u
->input_event
,
362 fd
, EPOLLIN
, dispatch_fd_input
, u
);
364 if (r
!= -EPERM
|| arg_follow
> 0)
365 return log_error_errno(r
, "Failed to register input event: %m");
367 /* Normal files should just be consumed without polling. */
368 r
= start_upload(u
, fd_input_callback
, u
);
375 static int dispatch_sigterm(sd_event_source
*event
,
376 const struct signalfd_siginfo
*si
,
378 Uploader
*u
= userdata
;
382 log_received_signal(LOG_INFO
, si
);
385 close_journal_input(u
);
387 sd_event_exit(u
->events
, 0);
391 static int setup_signals(Uploader
*u
) {
396 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, -1) >= 0);
398 r
= sd_event_add_signal(u
->events
, &u
->sigterm_event
, SIGTERM
, dispatch_sigterm
, u
);
402 r
= sd_event_add_signal(u
->events
, &u
->sigint_event
, SIGINT
, dispatch_sigterm
, u
);
409 static int setup_uploader(Uploader
*u
, const char *url
, const char *state_file
) {
411 const char *host
, *proto
= "";
420 host
= STARTSWITH_SET(url
, "http://", "https://");
426 if (strchr(host
, ':'))
427 u
->url
= strjoin(proto
, url
, "/upload");
434 while (x
> 0 && t
[x
- 1] == '/')
437 u
->url
= strjoin(proto
, t
, ":" STRINGIFY(DEFAULT_PORT
), "/upload");
442 u
->state_file
= state_file
;
444 r
= sd_event_default(&u
->events
);
446 return log_error_errno(r
, "sd_event_default failed: %m");
448 r
= setup_signals(u
);
450 return log_error_errno(r
, "Failed to set up signals: %m");
452 (void) sd_watchdog_enabled(false, &u
->watchdog_usec
);
454 return load_cursor_state(u
);
457 static void destroy_uploader(Uploader
*u
) {
460 curl_easy_cleanup(u
->easy
);
461 curl_slist_free_all(u
->header
);
464 free(u
->last_cursor
);
465 free(u
->current_cursor
);
469 u
->input_event
= sd_event_source_unref(u
->input_event
);
472 close_journal_input(u
);
474 sd_event_source_unref(u
->sigterm_event
);
475 sd_event_source_unref(u
->sigint_event
);
476 sd_event_unref(u
->events
);
479 static int perform_upload(Uploader
*u
) {
485 u
->watchdog_timestamp
= now(CLOCK_MONOTONIC
);
486 code
= curl_easy_perform(u
->easy
);
489 log_error("Upload to %s failed: %.*s",
490 u
->url
, (int) sizeof(u
->error
), u
->error
);
492 log_error("Upload to %s failed: %s",
493 u
->url
, curl_easy_strerror(code
));
497 code
= curl_easy_getinfo(u
->easy
, CURLINFO_RESPONSE_CODE
, &status
);
499 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
500 "Failed to retrieve response code: %s",
501 curl_easy_strerror(code
));
504 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
505 "Upload to %s failed with code %ld: %s",
506 u
->url
, status
, strna(u
->answer
));
507 else if (status
< 200)
508 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
509 "Upload to %s finished with unexpected code %ld: %s",
510 u
->url
, status
, strna(u
->answer
));
512 log_debug("Upload finished successfully with code %ld: %s",
513 status
, strna(u
->answer
));
515 free_and_replace(u
->last_cursor
, u
->current_cursor
);
517 return update_cursor_state(u
);
520 static int config_parse_path_or_ignore(
522 const char *filename
,
525 unsigned section_line
,
532 _cleanup_free_
char *n
= NULL
;
552 r
= path_simplify_and_warn(n
, PATH_CHECK_ABSOLUTE
| (fatal
? PATH_CHECK_FATAL
: 0), unit
, filename
, line
, lvalue
);
554 return fatal
? -ENOEXEC
: 0;
557 return free_and_replace(*s
, n
);
560 static int parse_config(void) {
561 const ConfigTableItem items
[] = {
562 { "Upload", "URL", config_parse_string
, 0, &arg_url
},
563 { "Upload", "ServerKeyFile", config_parse_path_or_ignore
, 0, &arg_key
},
564 { "Upload", "ServerCertificateFile", config_parse_path_or_ignore
, 0, &arg_cert
},
565 { "Upload", "TrustedCertificateFile", config_parse_path_or_ignore
, 0, &arg_trust
},
569 return config_parse_many_nulstr(
570 PKGSYSCONFDIR
"/journal-upload.conf",
571 CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
573 config_item_table_lookup
, items
,
579 static int help(void) {
580 _cleanup_free_
char *link
= NULL
;
583 r
= terminal_urlify_man("systemd-journal-upload.service", "8", &link
);
587 printf("%s -u URL {FILE|-}...\n\n"
588 "Upload journal events to a remote server.\n\n"
589 " -h --help Show this help\n"
590 " --version Show package version\n"
591 " -u --url=URL Upload to this address (default port "
592 STRINGIFY(DEFAULT_PORT
) ")\n"
593 " --key=FILENAME Specify key in PEM format (default:\n"
594 " \"" PRIV_KEY_FILE
"\")\n"
595 " --cert=FILENAME Specify certificate in PEM format (default:\n"
596 " \"" CERT_FILE
"\")\n"
597 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
598 " \"" TRUST_FILE
"\")\n"
599 " --system Use the system journal\n"
600 " --user Use the user journal for the current user\n"
601 " -m --merge Use all available journals\n"
602 " -M --machine=CONTAINER Operate on local container\n"
603 " -D --directory=PATH Use journal files from directory\n"
604 " --file=PATH Use this journal file\n"
605 " --cursor=CURSOR Start at the specified cursor\n"
606 " --after-cursor=CURSOR Start after the specified cursor\n"
607 " --follow[=BOOL] Do [not] wait for input\n"
608 " --save-state[=FILE] Save uploaded cursors (default \n"
610 "\nSee the %s for details.\n",
611 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;
751 r
= parse_boolean_argument("--follow", optarg
, NULL
);
758 arg_save_state
= optarg
?: STATE_FILE
;
762 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
763 "Unknown option %s.",
767 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
768 "Missing argument to %s.",
772 assert_not_reached("Unhandled option code.");
776 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
777 "Required --url/-u option missing.");
779 if (!!arg_key
!= !!arg_cert
)
780 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
781 "Options --key and --cert must be used together.");
783 if (optind
< argc
&& (arg_directory
|| arg_file
|| arg_machine
|| arg_journal_type
))
784 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
785 "Input arguments make no sense with journal input.");
790 static int open_journal(sd_journal
**j
) {
794 r
= sd_journal_open_directory(j
, arg_directory
, arg_journal_type
);
796 r
= sd_journal_open_files(j
, (const char**) arg_file
, 0);
797 else if (arg_machine
) {
798 #pragma GCC diagnostic push
799 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
800 /* FIXME: replace with D-Bus call OpenMachineRootDirectory() so that things also work with raw disk images */
801 r
= sd_journal_open_container(j
, arg_machine
, 0);
802 #pragma GCC diagnostic pop
804 r
= sd_journal_open(j
, !arg_merge
*SD_JOURNAL_LOCAL_ONLY
+ arg_journal_type
);
806 log_error_errno(r
, "Failed to open %s: %m",
807 arg_directory
? arg_directory
: arg_file
? "files" : "journal");
811 static int run(int argc
, char **argv
) {
812 _cleanup_(destroy_uploader
) Uploader u
= {};
813 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
817 log_show_color(true);
818 log_parse_environment();
820 /* The journal merging logic potentially needs a lot of fds. */
821 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
827 r
= parse_argv(argc
, argv
);
833 r
= setup_uploader(&u
, arg_url
, arg_save_state
);
837 sd_event_set_watchdog(u
.events
, true);
839 r
= check_cursor_updating(&u
);
843 log_debug("%s running as pid "PID_FMT
,
844 program_invocation_short_name
, getpid_cached());
846 use_journal
= optind
>= argc
;
849 r
= open_journal(&j
);
852 r
= open_journal_for_upload(&u
, j
,
853 arg_cursor
?: u
.last_cursor
,
854 arg_cursor
? arg_after_cursor
: true,
860 notify_message
= notify_start("READY=1\n"
861 "STATUS=Processing input...",
865 r
= sd_event_get_state(u
.events
);
868 if (r
== SD_EVENT_FINISHED
)
875 r
= check_journal_input(&u
);
876 } else if (u
.input
< 0 && !use_journal
) {
880 log_debug("Using %s as input.", argv
[optind
]);
881 r
= open_file_for_upload(&u
, argv
[optind
++]);
887 r
= perform_upload(&u
);
892 r
= sd_event_run(u
.events
, u
.timeout
);
894 return log_error_errno(r
, "Failed to run event loop: %m");
898 DEFINE_MAIN_FUNCTION(run
);