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 "pretty-print.h"
27 #include "process-util.h"
28 #include "rlimit-util.h"
30 #include "signal-util.h"
31 #include "string-util.h"
33 #include "tmpfile-util.h"
36 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
37 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
38 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
39 #define DEFAULT_PORT 19532
41 static const char* arg_url
= NULL
;
42 static const char *arg_key
= NULL
;
43 static const char *arg_cert
= NULL
;
44 static const char *arg_trust
= NULL
;
45 static const char *arg_directory
= NULL
;
46 static char **arg_file
= NULL
;
47 static const char *arg_cursor
= NULL
;
48 static bool arg_after_cursor
= false;
49 static int arg_journal_type
= 0;
50 static const char *arg_machine
= NULL
;
51 static bool arg_merge
= false;
52 static int arg_follow
= -1;
53 static const char *arg_save_state
= NULL
;
55 static void close_fd_input(Uploader
*u
);
57 #define SERVER_ANSWER_KEEP 2048
59 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
61 #define easy_setopt(curl, opt, value, level, cmd) \
63 code = curl_easy_setopt(curl, opt, value); \
66 "curl_easy_setopt " #opt " failed: %s", \
67 curl_easy_strerror(code)); \
72 static size_t output_callback(char *buf
,
80 log_debug("The server answers (%zu bytes): %.*s",
81 size
*nmemb
, (int)(size
*nmemb
), buf
);
83 if (nmemb
&& !u
->answer
) {
84 u
->answer
= strndup(buf
, size
*nmemb
);
86 log_warning("Failed to store server answer (%zu bytes): out of memory", size
*nmemb
);
92 static int check_cursor_updating(Uploader
*u
) {
93 _cleanup_free_
char *temp_path
= NULL
;
94 _cleanup_fclose_
FILE *f
= NULL
;
100 r
= mkdir_parents(u
->state_file
, 0755);
102 return log_error_errno(r
, "Cannot create parent directory of state file %s: %m",
105 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
107 return log_error_errno(r
, "Cannot save state to %s: %m",
109 (void) unlink(temp_path
);
114 static int update_cursor_state(Uploader
*u
) {
115 _cleanup_free_
char *temp_path
= NULL
;
116 _cleanup_fclose_
FILE *f
= NULL
;
119 if (!u
->state_file
|| !u
->last_cursor
)
122 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
127 "# This is private data. Do not parse.\n"
131 r
= fflush_and_check(f
);
135 if (rename(temp_path
, u
->state_file
) < 0) {
144 (void) unlink(temp_path
);
146 (void) unlink(u
->state_file
);
148 return log_error_errno(r
, "Failed to save state %s: %m", u
->state_file
);
151 static int load_cursor_state(Uploader
*u
) {
157 r
= parse_env_file(NULL
, u
->state_file
, "LAST_CURSOR", &u
->last_cursor
);
159 log_debug("State file %s is not present.", u
->state_file
);
161 return log_error_errno(r
, "Failed to read state file %s: %m",
164 log_debug("Last cursor was %s", u
->last_cursor
);
169 int start_upload(Uploader
*u
,
170 size_t (*input_callback
)(void *ptr
,
178 assert(input_callback
);
181 struct curl_slist
*h
;
183 h
= curl_slist_append(NULL
, "Content-Type: application/vnd.fdo.journal");
187 h
= curl_slist_append(h
, "Transfer-Encoding: chunked");
189 curl_slist_free_all(h
);
193 h
= curl_slist_append(h
, "Accept: text/plain");
195 curl_slist_free_all(h
);
205 curl
= curl_easy_init();
207 return log_error_errno(SYNTHETIC_ERRNO(ENOSR
),
208 "Call to curl_easy_init failed.");
210 /* tell it to POST to the URL */
211 easy_setopt(curl
, CURLOPT_POST
, 1L,
212 LOG_ERR
, return -EXFULL
);
214 easy_setopt(curl
, CURLOPT_ERRORBUFFER
, u
->error
,
215 LOG_ERR
, return -EXFULL
);
217 /* set where to write to */
218 easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, output_callback
,
219 LOG_ERR
, return -EXFULL
);
221 easy_setopt(curl
, CURLOPT_WRITEDATA
, data
,
222 LOG_ERR
, return -EXFULL
);
224 /* set where to read from */
225 easy_setopt(curl
, CURLOPT_READFUNCTION
, input_callback
,
226 LOG_ERR
, return -EXFULL
);
228 easy_setopt(curl
, CURLOPT_READDATA
, data
,
229 LOG_ERR
, return -EXFULL
);
231 /* use our special own mime type and chunked transfer */
232 easy_setopt(curl
, CURLOPT_HTTPHEADER
, u
->header
,
233 LOG_ERR
, return -EXFULL
);
236 /* enable verbose for easier tracing */
237 easy_setopt(curl
, CURLOPT_VERBOSE
, 1L, LOG_WARNING
, );
239 easy_setopt(curl
, CURLOPT_USERAGENT
,
240 "systemd-journal-upload " GIT_VERSION
,
243 if (arg_key
|| startswith(u
->url
, "https://")) {
244 easy_setopt(curl
, CURLOPT_SSLKEY
, arg_key
?: PRIV_KEY_FILE
,
245 LOG_ERR
, return -EXFULL
);
246 easy_setopt(curl
, CURLOPT_SSLCERT
, arg_cert
?: CERT_FILE
,
247 LOG_ERR
, return -EXFULL
);
250 if (streq_ptr(arg_trust
, "all"))
251 easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0,
252 LOG_ERR
, return -EUCLEAN
);
253 else if (arg_trust
|| startswith(u
->url
, "https://"))
254 easy_setopt(curl
, CURLOPT_CAINFO
, arg_trust
?: TRUST_FILE
,
255 LOG_ERR
, return -EXFULL
);
257 if (arg_key
|| arg_trust
)
258 easy_setopt(curl
, CURLOPT_SSLVERSION
, CURL_SSLVERSION_TLSv1
,
263 /* truncate the potential old error message */
270 /* upload to this place */
271 code
= curl_easy_setopt(u
->easy
, CURLOPT_URL
, u
->url
);
273 return log_error_errno(SYNTHETIC_ERRNO(EXFULL
),
274 "curl_easy_setopt CURLOPT_URL failed: %s",
275 curl_easy_strerror(code
));
282 static size_t fd_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
287 assert(nmemb
< SSIZE_MAX
/ size
);
292 assert(!size_multiply_overflow(size
, nmemb
));
294 n
= read(u
->input
, buf
, size
* nmemb
);
295 log_debug("%s: allowed %zu, read %zd", __func__
, size
*nmemb
, n
);
299 u
->uploading
= false;
301 log_error_errno(errno
, "Aborting transfer after read error on input: %m.");
302 return CURL_READFUNC_ABORT
;
305 log_debug("Reached EOF");
310 static void close_fd_input(Uploader
*u
) {
313 u
->input
= safe_close(u
->input
);
317 static int dispatch_fd_input(sd_event_source
*event
,
326 if (revents
& EPOLLHUP
) {
327 log_debug("Received HUP");
332 if (!(revents
& EPOLLIN
)) {
333 log_warning("Unexpected poll event %"PRIu32
".", revents
);
338 log_warning("dispatch_fd_input called when uploading, ignoring.");
342 return start_upload(u
, fd_input_callback
, u
);
345 static int open_file_for_upload(Uploader
*u
, const char *filename
) {
348 if (streq(filename
, "-"))
351 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
353 return log_error_errno(errno
, "Failed to open %s: %m", filename
);
359 r
= sd_event_add_io(u
->events
, &u
->input_event
,
360 fd
, EPOLLIN
, dispatch_fd_input
, u
);
362 if (r
!= -EPERM
|| arg_follow
> 0)
363 return log_error_errno(r
, "Failed to register input event: %m");
365 /* Normal files should just be consumed without polling. */
366 r
= start_upload(u
, fd_input_callback
, u
);
373 static int dispatch_sigterm(sd_event_source
*event
,
374 const struct signalfd_siginfo
*si
,
376 Uploader
*u
= userdata
;
380 log_received_signal(LOG_INFO
, si
);
383 close_journal_input(u
);
385 sd_event_exit(u
->events
, 0);
389 static int setup_signals(Uploader
*u
) {
394 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, -1) >= 0);
396 r
= sd_event_add_signal(u
->events
, &u
->sigterm_event
, SIGTERM
, dispatch_sigterm
, u
);
400 r
= sd_event_add_signal(u
->events
, &u
->sigint_event
, SIGINT
, dispatch_sigterm
, u
);
407 static int setup_uploader(Uploader
*u
, const char *url
, const char *state_file
) {
409 const char *host
, *proto
= "";
418 host
= STARTSWITH_SET(url
, "http://", "https://");
424 if (strchr(host
, ':'))
425 u
->url
= strjoin(proto
, url
, "/upload");
432 while (x
> 0 && t
[x
- 1] == '/')
435 u
->url
= strjoin(proto
, t
, ":" STRINGIFY(DEFAULT_PORT
), "/upload");
440 u
->state_file
= state_file
;
442 r
= sd_event_default(&u
->events
);
444 return log_error_errno(r
, "sd_event_default failed: %m");
446 r
= setup_signals(u
);
448 return log_error_errno(r
, "Failed to set up signals: %m");
450 (void) sd_watchdog_enabled(false, &u
->watchdog_usec
);
452 return load_cursor_state(u
);
455 static void destroy_uploader(Uploader
*u
) {
458 curl_easy_cleanup(u
->easy
);
459 curl_slist_free_all(u
->header
);
462 free(u
->last_cursor
);
463 free(u
->current_cursor
);
467 u
->input_event
= sd_event_source_unref(u
->input_event
);
470 close_journal_input(u
);
472 sd_event_source_unref(u
->sigterm_event
);
473 sd_event_source_unref(u
->sigint_event
);
474 sd_event_unref(u
->events
);
477 static int perform_upload(Uploader
*u
) {
483 u
->watchdog_timestamp
= now(CLOCK_MONOTONIC
);
484 code
= curl_easy_perform(u
->easy
);
487 log_error("Upload to %s failed: %.*s",
488 u
->url
, (int) sizeof(u
->error
), u
->error
);
490 log_error("Upload to %s failed: %s",
491 u
->url
, curl_easy_strerror(code
));
495 code
= curl_easy_getinfo(u
->easy
, CURLINFO_RESPONSE_CODE
, &status
);
497 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
498 "Failed to retrieve response code: %s",
499 curl_easy_strerror(code
));
502 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
503 "Upload to %s failed with code %ld: %s",
504 u
->url
, status
, strna(u
->answer
));
505 else if (status
< 200)
506 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
507 "Upload to %s finished with unexpected code %ld: %s",
508 u
->url
, status
, strna(u
->answer
));
510 log_debug("Upload finished successfully with code %ld: %s",
511 status
, strna(u
->answer
));
513 free_and_replace(u
->last_cursor
, u
->current_cursor
);
515 return update_cursor_state(u
);
518 static int parse_config(void) {
519 const ConfigTableItem items
[] = {
520 { "Upload", "URL", config_parse_string
, 0, &arg_url
},
521 { "Upload", "ServerKeyFile", config_parse_path
, 0, &arg_key
},
522 { "Upload", "ServerCertificateFile", config_parse_path
, 0, &arg_cert
},
523 { "Upload", "TrustedCertificateFile", config_parse_path
, 0, &arg_trust
},
526 return config_parse_many_nulstr(PKGSYSCONFDIR
"/journal-upload.conf",
527 CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
528 "Upload\0", config_item_table_lookup
, items
,
529 CONFIG_PARSE_WARN
, NULL
);
532 static int help(void) {
533 _cleanup_free_
char *link
= NULL
;
536 r
= terminal_urlify_man("systemd-journal-upload.service", "8", &link
);
540 printf("%s -u URL {FILE|-}...\n\n"
541 "Upload journal events to a remote server.\n\n"
542 " -h --help Show this help\n"
543 " --version Show package version\n"
544 " -u --url=URL Upload to this address (default port "
545 STRINGIFY(DEFAULT_PORT
) ")\n"
546 " --key=FILENAME Specify key in PEM format (default:\n"
547 " \"" PRIV_KEY_FILE
"\")\n"
548 " --cert=FILENAME Specify certificate in PEM format (default:\n"
549 " \"" CERT_FILE
"\")\n"
550 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
551 " \"" TRUST_FILE
"\")\n"
552 " --system Use the system journal\n"
553 " --user Use the user journal for the current user\n"
554 " -m --merge Use all available journals\n"
555 " -M --machine=CONTAINER Operate on local container\n"
556 " -D --directory=PATH Use journal files from directory\n"
557 " --file=PATH Use this journal file\n"
558 " --cursor=CURSOR Start at the specified cursor\n"
559 " --after-cursor=CURSOR Start after the specified cursor\n"
560 " --follow[=BOOL] Do [not] wait for input\n"
561 " --save-state[=FILE] Save uploaded cursors (default \n"
563 "\nSee the %s for details.\n"
564 , program_invocation_short_name
571 static int parse_argv(int argc
, char *argv
[]) {
586 static const struct option options
[] = {
587 { "help", no_argument
, NULL
, 'h' },
588 { "version", no_argument
, NULL
, ARG_VERSION
},
589 { "url", required_argument
, NULL
, 'u' },
590 { "key", required_argument
, NULL
, ARG_KEY
},
591 { "cert", required_argument
, NULL
, ARG_CERT
},
592 { "trust", required_argument
, NULL
, ARG_TRUST
},
593 { "system", no_argument
, NULL
, ARG_SYSTEM
},
594 { "user", no_argument
, NULL
, ARG_USER
},
595 { "merge", no_argument
, NULL
, 'm' },
596 { "machine", required_argument
, NULL
, 'M' },
597 { "directory", required_argument
, NULL
, 'D' },
598 { "file", required_argument
, NULL
, ARG_FILE
},
599 { "cursor", required_argument
, NULL
, ARG_CURSOR
},
600 { "after-cursor", required_argument
, NULL
, ARG_AFTER_CURSOR
},
601 { "follow", optional_argument
, NULL
, ARG_FOLLOW
},
602 { "save-state", optional_argument
, NULL
, ARG_SAVE_STATE
},
613 while ((c
= getopt_long(argc
, argv
, "hu:mM:D:", options
, NULL
)) >= 0)
623 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
624 "cannot use more than one --url");
631 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
632 "cannot use more than one --key");
639 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
640 "cannot use more than one --cert");
647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
648 "cannot use more than one --trust");
654 arg_journal_type
|= SD_JOURNAL_SYSTEM
;
658 arg_journal_type
|= SD_JOURNAL_CURRENT_USER
;
667 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
668 "cannot use more than one --machine/-M");
670 arg_machine
= optarg
;
675 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
676 "cannot use more than one --directory/-D");
678 arg_directory
= optarg
;
682 r
= glob_extend(&arg_file
, optarg
);
684 return log_error_errno(r
, "Failed to add paths: %m");
689 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
690 "cannot use more than one --cursor/--after-cursor");
695 case ARG_AFTER_CURSOR
:
697 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
698 "cannot use more than one --cursor/--after-cursor");
701 arg_after_cursor
= true;
706 r
= parse_boolean(optarg
);
708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
709 "Failed to parse --follow= parameter.");
718 arg_save_state
= optarg
?: STATE_FILE
;
722 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
723 "Unknown option %s.",
727 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
728 "Missing argument to %s.",
732 assert_not_reached("Unhandled option code.");
736 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
737 "Required --url/-u option missing.");
739 if (!!arg_key
!= !!arg_cert
)
740 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
741 "Options --key and --cert must be used together.");
743 if (optind
< argc
&& (arg_directory
|| arg_file
|| arg_machine
|| arg_journal_type
))
744 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
745 "Input arguments make no sense with journal input.");
750 static int open_journal(sd_journal
**j
) {
754 r
= sd_journal_open_directory(j
, arg_directory
, arg_journal_type
);
756 r
= sd_journal_open_files(j
, (const char**) arg_file
, 0);
757 else if (arg_machine
)
758 r
= sd_journal_open_container(j
, arg_machine
, 0);
760 r
= sd_journal_open(j
, !arg_merge
*SD_JOURNAL_LOCAL_ONLY
+ arg_journal_type
);
762 log_error_errno(r
, "Failed to open %s: %m",
763 arg_directory
? arg_directory
: arg_file
? "files" : "journal");
767 static int run(int argc
, char **argv
) {
768 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
769 _cleanup_(destroy_uploader
) Uploader u
= {};
773 log_show_color(true);
774 log_parse_environment();
776 /* The journal merging logic potentially needs a lot of fds. */
777 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
783 r
= parse_argv(argc
, argv
);
789 r
= setup_uploader(&u
, arg_url
, arg_save_state
);
793 sd_event_set_watchdog(u
.events
, true);
795 r
= check_cursor_updating(&u
);
799 log_debug("%s running as pid "PID_FMT
,
800 program_invocation_short_name
, getpid_cached());
802 use_journal
= optind
>= argc
;
805 r
= open_journal(&j
);
808 r
= open_journal_for_upload(&u
, j
,
809 arg_cursor
?: u
.last_cursor
,
810 arg_cursor
? arg_after_cursor
: true,
816 notify_message
= notify_start("READY=1\n"
817 "STATUS=Processing input...",
821 r
= sd_event_get_state(u
.events
);
824 if (r
== SD_EVENT_FINISHED
)
831 r
= check_journal_input(&u
);
832 } else if (u
.input
< 0 && !use_journal
) {
836 log_debug("Using %s as input.", argv
[optind
]);
837 r
= open_file_for_upload(&u
, argv
[optind
++]);
843 r
= perform_upload(&u
);
848 r
= sd_event_run(u
.events
, u
.timeout
);
850 return log_error_errno(r
, "Failed to run event loop: %m");
854 DEFINE_MAIN_FUNCTION(run
);