1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Zbigniew Jędrzejewski-Szmek
12 #include "sd-daemon.h"
14 #include "alloc-util.h"
15 #include "conf-parser.h"
19 #include "format-util.h"
20 #include "glob-util.h"
21 #include "journal-upload.h"
24 #include "parse-util.h"
25 #include "process-util.h"
27 #include "signal-util.h"
28 #include "string-util.h"
31 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
32 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
33 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
34 #define DEFAULT_PORT 19532
36 static const char* arg_url
= NULL
;
37 static const char *arg_key
= NULL
;
38 static const char *arg_cert
= NULL
;
39 static const char *arg_trust
= NULL
;
40 static const char *arg_directory
= NULL
;
41 static char **arg_file
= NULL
;
42 static const char *arg_cursor
= NULL
;
43 static bool arg_after_cursor
= false;
44 static int arg_journal_type
= 0;
45 static const char *arg_machine
= NULL
;
46 static bool arg_merge
= false;
47 static int arg_follow
= -1;
48 static const char *arg_save_state
= NULL
;
50 static void close_fd_input(Uploader
*u
);
52 #define SERVER_ANSWER_KEEP 2048
54 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
56 #define easy_setopt(curl, opt, value, level, cmd) \
58 code = curl_easy_setopt(curl, opt, value); \
61 "curl_easy_setopt " #opt " failed: %s", \
62 curl_easy_strerror(code)); \
67 static size_t output_callback(char *buf
,
75 log_debug("The server answers (%zu bytes): %.*s",
76 size
*nmemb
, (int)(size
*nmemb
), buf
);
78 if (nmemb
&& !u
->answer
) {
79 u
->answer
= strndup(buf
, size
*nmemb
);
81 log_warning_errno(ENOMEM
, "Failed to store server answer (%zu bytes): %m",
88 static int check_cursor_updating(Uploader
*u
) {
89 _cleanup_free_
char *temp_path
= NULL
;
90 _cleanup_fclose_
FILE *f
= NULL
;
96 r
= mkdir_parents(u
->state_file
, 0755);
98 return log_error_errno(r
, "Cannot create parent directory of state file %s: %m",
101 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
103 return log_error_errno(r
, "Cannot save state to %s: %m",
110 static int update_cursor_state(Uploader
*u
) {
111 _cleanup_free_
char *temp_path
= NULL
;
112 _cleanup_fclose_
FILE *f
= NULL
;
115 if (!u
->state_file
|| !u
->last_cursor
)
118 r
= fopen_temporary(u
->state_file
, &f
, &temp_path
);
123 "# This is private data. Do not parse.\n"
127 r
= fflush_and_check(f
);
131 if (rename(temp_path
, u
->state_file
) < 0) {
140 (void) unlink(temp_path
);
142 (void) unlink(u
->state_file
);
144 return log_error_errno(r
, "Failed to save state %s: %m", u
->state_file
);
147 static int load_cursor_state(Uploader
*u
) {
153 r
= parse_env_file(NULL
, u
->state_file
, NEWLINE
,
154 "LAST_CURSOR", &u
->last_cursor
,
158 log_debug("State file %s is not present.", u
->state_file
);
160 return log_error_errno(r
, "Failed to read state file %s: %m",
163 log_debug("Last cursor was %s", u
->last_cursor
);
168 int start_upload(Uploader
*u
,
169 size_t (*input_callback
)(void *ptr
,
177 assert(input_callback
);
180 struct curl_slist
*h
;
182 h
= curl_slist_append(NULL
, "Content-Type: application/vnd.fdo.journal");
186 h
= curl_slist_append(h
, "Transfer-Encoding: chunked");
188 curl_slist_free_all(h
);
192 h
= curl_slist_append(h
, "Accept: text/plain");
194 curl_slist_free_all(h
);
204 curl
= curl_easy_init();
206 log_error("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 " PACKAGE_STRING
,
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 log_error("curl_easy_setopt CURLOPT_URL failed: %s",
274 curl_easy_strerror(code
));
283 static size_t fd_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
289 assert(nmemb
<= SSIZE_MAX
/ size
);
294 r
= read(u
->input
, buf
, size
* nmemb
);
295 log_debug("%s: allowed %zu, read %zd", __func__
, size
*nmemb
, r
);
300 u
->uploading
= false;
302 log_debug("Reached EOF");
306 log_error_errno(errno
, "Aborting transfer after read error on input: %m.");
307 return CURL_READFUNC_ABORT
;
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
= "";
415 memzero(u
, sizeof(Uploader
));
418 if (!(host
= startswith(url
, "http://")) && !(host
= startswith(url
, "https://"))) {
423 if (strchr(host
, ':'))
424 u
->url
= strjoin(proto
, url
, "/upload");
431 while (x
> 0 && t
[x
- 1] == '/')
434 u
->url
= strjoin(proto
, t
, ":" STRINGIFY(DEFAULT_PORT
), "/upload");
439 u
->state_file
= state_file
;
441 r
= sd_event_default(&u
->events
);
443 return log_error_errno(r
, "sd_event_default failed: %m");
445 r
= setup_signals(u
);
447 return log_error_errno(r
, "Failed to set up signals: %m");
449 (void) sd_watchdog_enabled(false, &u
->watchdog_usec
);
451 return load_cursor_state(u
);
454 static void destroy_uploader(Uploader
*u
) {
457 curl_easy_cleanup(u
->easy
);
458 curl_slist_free_all(u
->header
);
461 free(u
->last_cursor
);
462 free(u
->current_cursor
);
466 u
->input_event
= sd_event_source_unref(u
->input_event
);
469 close_journal_input(u
);
471 sd_event_source_unref(u
->sigterm_event
);
472 sd_event_source_unref(u
->sigint_event
);
473 sd_event_unref(u
->events
);
476 static int perform_upload(Uploader
*u
) {
482 u
->watchdog_timestamp
= now(CLOCK_MONOTONIC
);
483 code
= curl_easy_perform(u
->easy
);
486 log_error("Upload to %s failed: %.*s",
487 u
->url
, (int) sizeof(u
->error
), u
->error
);
489 log_error("Upload to %s failed: %s",
490 u
->url
, curl_easy_strerror(code
));
494 code
= curl_easy_getinfo(u
->easy
, CURLINFO_RESPONSE_CODE
, &status
);
496 log_error("Failed to retrieve response code: %s",
497 curl_easy_strerror(code
));
502 log_error("Upload to %s failed with code %ld: %s",
503 u
->url
, status
, strna(u
->answer
));
505 } else if (status
< 200) {
506 log_error("Upload to %s finished with unexpected code %ld: %s",
507 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 void help(void) {
533 printf("%s -u URL {FILE|-}...\n\n"
534 "Upload journal events to a remote server.\n\n"
535 " -h --help Show this help\n"
536 " --version Show package version\n"
537 " -u --url=URL Upload to this address (default port "
538 STRINGIFY(DEFAULT_PORT
) ")\n"
539 " --key=FILENAME Specify key in PEM format (default:\n"
540 " \"" PRIV_KEY_FILE
"\")\n"
541 " --cert=FILENAME Specify certificate in PEM format (default:\n"
542 " \"" CERT_FILE
"\")\n"
543 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
544 " \"" TRUST_FILE
"\")\n"
545 " --system Use the system journal\n"
546 " --user Use the user journal for the current user\n"
547 " -m --merge Use all available journals\n"
548 " -M --machine=CONTAINER Operate on local container\n"
549 " -D --directory=PATH Use journal files from directory\n"
550 " --file=PATH Use this journal file\n"
551 " --cursor=CURSOR Start at the specified cursor\n"
552 " --after-cursor=CURSOR Start after the specified cursor\n"
553 " --follow[=BOOL] Do [not] wait for input\n"
554 " --save-state[=FILE] Save uploaded cursors (default \n"
556 , program_invocation_short_name
);
559 static int parse_argv(int argc
, char *argv
[]) {
574 static const struct option options
[] = {
575 { "help", no_argument
, NULL
, 'h' },
576 { "version", no_argument
, NULL
, ARG_VERSION
},
577 { "url", required_argument
, NULL
, 'u' },
578 { "key", required_argument
, NULL
, ARG_KEY
},
579 { "cert", required_argument
, NULL
, ARG_CERT
},
580 { "trust", required_argument
, NULL
, ARG_TRUST
},
581 { "system", no_argument
, NULL
, ARG_SYSTEM
},
582 { "user", no_argument
, NULL
, ARG_USER
},
583 { "merge", no_argument
, NULL
, 'm' },
584 { "machine", required_argument
, NULL
, 'M' },
585 { "directory", required_argument
, NULL
, 'D' },
586 { "file", required_argument
, NULL
, ARG_FILE
},
587 { "cursor", required_argument
, NULL
, ARG_CURSOR
},
588 { "after-cursor", required_argument
, NULL
, ARG_AFTER_CURSOR
},
589 { "follow", optional_argument
, NULL
, ARG_FOLLOW
},
590 { "save-state", optional_argument
, NULL
, ARG_SAVE_STATE
},
601 while ((c
= getopt_long(argc
, argv
, "hu:mM:D:", options
, NULL
)) >= 0)
612 log_error("cannot use more than one --url");
621 log_error("cannot use more than one --key");
630 log_error("cannot use more than one --cert");
639 log_error("cannot use more than one --trust");
647 arg_journal_type
|= SD_JOURNAL_SYSTEM
;
651 arg_journal_type
|= SD_JOURNAL_CURRENT_USER
;
660 log_error("cannot use more than one --machine/-M");
664 arg_machine
= optarg
;
669 log_error("cannot use more than one --directory/-D");
673 arg_directory
= optarg
;
677 r
= glob_extend(&arg_file
, optarg
);
679 return log_error_errno(r
, "Failed to add paths: %m");
684 log_error("cannot use more than one --cursor/--after-cursor");
691 case ARG_AFTER_CURSOR
:
693 log_error("cannot use more than one --cursor/--after-cursor");
698 arg_after_cursor
= true;
703 r
= parse_boolean(optarg
);
705 log_error("Failed to parse --follow= parameter.");
716 arg_save_state
= optarg
?: STATE_FILE
;
720 log_error("Unknown option %s.", argv
[optind
-1]);
724 log_error("Missing argument to %s.", argv
[optind
-1]);
728 assert_not_reached("Unhandled option code.");
732 log_error("Required --url/-u option missing.");
736 if (!!arg_key
!= !!arg_cert
) {
737 log_error("Options --key and --cert must be used together.");
741 if (optind
< argc
&& (arg_directory
|| arg_file
|| arg_machine
|| arg_journal_type
)) {
742 log_error("Input arguments make no sense with journal input.");
749 static int open_journal(sd_journal
**j
) {
753 r
= sd_journal_open_directory(j
, arg_directory
, arg_journal_type
);
755 r
= sd_journal_open_files(j
, (const char**) arg_file
, 0);
756 else if (arg_machine
)
757 r
= sd_journal_open_container(j
, arg_machine
, 0);
759 r
= sd_journal_open(j
, !arg_merge
*SD_JOURNAL_LOCAL_ONLY
+ arg_journal_type
);
761 log_error_errno(r
, "Failed to open %s: %m",
762 arg_directory
? arg_directory
: arg_file
? "files" : "journal");
766 int main(int argc
, char **argv
) {
771 log_show_color(true);
772 log_parse_environment();
778 r
= parse_argv(argc
, argv
);
784 r
= setup_uploader(&u
, arg_url
, arg_save_state
);
788 sd_event_set_watchdog(u
.events
, true);
790 r
= check_cursor_updating(&u
);
794 log_debug("%s running as pid "PID_FMT
,
795 program_invocation_short_name
, getpid_cached());
797 use_journal
= optind
>= argc
;
800 r
= open_journal(&j
);
803 r
= open_journal_for_upload(&u
, j
,
804 arg_cursor
?: u
.last_cursor
,
805 arg_cursor
? arg_after_cursor
: true,
813 "STATUS=Processing input...");
816 r
= sd_event_get_state(u
.events
);
819 if (r
== SD_EVENT_FINISHED
)
826 r
= check_journal_input(&u
);
827 } else if (u
.input
< 0 && !use_journal
) {
831 log_debug("Using %s as input.", argv
[optind
]);
832 r
= open_file_for_upload(&u
, argv
[optind
++]);
838 r
= perform_upload(&u
);
843 r
= sd_event_run(u
.events
, u
.timeout
);
845 log_error_errno(r
, "Failed to run event loop: %m");
853 "STATUS=Shutting down...");
855 destroy_uploader(&u
);
858 return r
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;