]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal-remote/journal-remote-main.c
various: use new config loader instead of config_parse_config_file()
[thirdparty/systemd.git] / src / journal-remote / journal-remote-main.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c064d8db
ZJS
2
3#include <getopt.h>
4#include <unistd.h>
5
6#include "sd-daemon.h"
7
d6b4d1c7 8#include "build.h"
c064d8db 9#include "conf-parser.h"
28db6fbf 10#include "constants.h"
d8dd35fd 11#include "daemon-util.h"
c064d8db
ZJS
12#include "fd-util.h"
13#include "fileio.h"
14#include "journal-remote-write.h"
15#include "journal-remote.h"
d8dd35fd 16#include "main-func.h"
f362fe73 17#include "memory-util.h"
9c7f2201 18#include "parse-argument.h"
d7085bcc 19#include "parse-helpers.h"
294bf0c3 20#include "pretty-print.h"
c064d8db 21#include "process-util.h"
1abaf488 22#include "rlimit-util.h"
955fc5d8 23#include "sigbus.h"
c064d8db 24#include "signal-util.h"
5c3fa98d 25#include "socket-netlink.h"
c064d8db
ZJS
26#include "socket-util.h"
27#include "stat-util.h"
28#include "string-table.h"
29#include "strv.h"
30
31#define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
32#define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
33#define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
34
d8dd35fd
YW
35static const char* arg_url = NULL;
36static const char* arg_getter = NULL;
37static const char* arg_listen_raw = NULL;
38static const char* arg_listen_http = NULL;
39static const char* arg_listen_https = NULL;
40static char** arg_files = NULL; /* Do not free this. */
9c7f2201
ZJS
41static bool arg_compress = true;
42static bool arg_seal = false;
c064d8db
ZJS
43static int http_socket = -1, https_socket = -1;
44static char** arg_gnutls_log = NULL;
45
46static JournalWriteSplitMode arg_split_mode = _JOURNAL_WRITE_SPLIT_INVALID;
7276d98c 47static char *arg_output = NULL;
c064d8db
ZJS
48
49static char *arg_key = NULL;
50static char *arg_cert = NULL;
51static char *arg_trust = NULL;
f7adeaeb 52#if HAVE_GNUTLS
c064d8db 53static bool arg_trust_all = false;
f7adeaeb
YW
54#else
55static bool arg_trust_all = true;
56#endif
c064d8db 57
f12b399d 58static uint64_t arg_max_use = UINT64_MAX;
59static uint64_t arg_max_size = UINT64_MAX;
60static uint64_t arg_n_max_files = UINT64_MAX;
61static uint64_t arg_keep_free = UINT64_MAX;
62
d8dd35fd
YW
63STATIC_DESTRUCTOR_REGISTER(arg_gnutls_log, strv_freep);
64STATIC_DESTRUCTOR_REGISTER(arg_key, freep);
65STATIC_DESTRUCTOR_REGISTER(arg_cert, freep);
66STATIC_DESTRUCTOR_REGISTER(arg_trust, freep);
7276d98c 67STATIC_DESTRUCTOR_REGISTER(arg_output, freep);
d8dd35fd 68
c064d8db
ZJS
69static const char* const journal_write_split_mode_table[_JOURNAL_WRITE_SPLIT_MAX] = {
70 [JOURNAL_WRITE_SPLIT_NONE] = "none",
71 [JOURNAL_WRITE_SPLIT_HOST] = "host",
72};
73
74DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode, JournalWriteSplitMode);
75static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode,
76 journal_write_split_mode,
77 JournalWriteSplitMode,
78 "Failed to parse split mode setting");
79
80/**********************************************************************
81 **********************************************************************
82 **********************************************************************/
83
84static int spawn_child(const char* child, char** argv) {
85 pid_t child_pid;
86 int fd[2], r;
87
88 if (pipe(fd) < 0)
89 return log_error_errno(errno, "Failed to create pager pipe: %m");
90
0c2aedb4
YW
91 r = safe_fork_full("(remote)",
92 (int[]) {STDIN_FILENO, fd[1], STDERR_FILENO },
93 NULL, 0,
e9ccae31 94 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE, &child_pid);
c064d8db
ZJS
95 if (r < 0) {
96 safe_close_pair(fd);
97 return r;
98 }
99
100 /* In the child */
101 if (r == 0) {
c064d8db
ZJS
102 execvp(child, argv);
103 log_error_errno(errno, "Failed to exec child %s: %m", child);
104 _exit(EXIT_FAILURE);
105 }
106
107 safe_close(fd[1]);
108
109 r = fd_nonblock(fd[0], true);
110 if (r < 0)
111 log_warning_errno(errno, "Failed to set child pipe to non-blocking: %m");
112
113 return fd[0];
114}
115
116static int spawn_curl(const char* url) {
117 char **argv = STRV_MAKE("curl",
118 "-HAccept: application/vnd.fdo.journal",
119 "--silent",
120 "--show-error",
121 url);
122 int r;
123
124 r = spawn_child("curl", argv);
125 if (r < 0)
126 log_error_errno(r, "Failed to spawn curl: %m");
127 return r;
128}
129
130static int spawn_getter(const char *getter) {
131 int r;
132 _cleanup_strv_free_ char **words = NULL;
133
134 assert(getter);
90e30d76 135 r = strv_split_full(&words, getter, WHITESPACE, EXTRACT_UNQUOTE);
c064d8db
ZJS
136 if (r < 0)
137 return log_error_errno(r, "Failed to split getter option: %m");
138
139 r = spawn_child(words[0], words);
140 if (r < 0)
141 log_error_errno(r, "Failed to spawn getter %s: %m", getter);
142
143 return r;
144}
145
146/**********************************************************************
147 **********************************************************************
148 **********************************************************************/
149
150static int null_timer_event_handler(sd_event_source *s,
151 uint64_t usec,
152 void *userdata);
153static int dispatch_http_event(sd_event_source *event,
154 int fd,
155 uint32_t revents,
156 void *userdata);
157
158static int request_meta(void **connection_cls, int fd, char *hostname) {
159 RemoteSource *source;
160 Writer *writer;
161 int r;
162
163 assert(connection_cls);
164 if (*connection_cls)
165 return 0;
166
167 r = journal_remote_get_writer(journal_remote_server_global, hostname, &writer);
168 if (r < 0)
169 return log_warning_errno(r, "Failed to get writer for source %s: %m",
170 hostname);
171
172 source = source_new(fd, true, hostname, writer);
173 if (!source) {
174 writer_unref(writer);
175 return log_oom();
176 }
177
178 log_debug("Added RemoteSource as connection metadata %p", source);
179
180 *connection_cls = source;
181 return 0;
182}
183
184static void request_meta_free(void *cls,
185 struct MHD_Connection *connection,
186 void **connection_cls,
187 enum MHD_RequestTerminationCode toe) {
188 RemoteSource *s;
189
190 assert(connection_cls);
191 s = *connection_cls;
192
193 if (s) {
194 log_debug("Cleaning up connection metadata %p", s);
195 source_free(s);
196 *connection_cls = NULL;
197 }
198}
199
200static int process_http_upload(
201 struct MHD_Connection *connection,
202 const char *upload_data,
203 size_t *upload_data_size,
204 RemoteSource *source) {
205
206 bool finished = false;
207 size_t remaining;
208 int r;
209
210 assert(source);
211
212 log_trace("%s: connection %p, %zu bytes",
213 __func__, connection, *upload_data_size);
214
215 if (*upload_data_size) {
216 log_trace("Received %zu bytes", *upload_data_size);
217
218 r = journal_importer_push_data(&source->importer,
219 upload_data, *upload_data_size);
220 if (r < 0)
221 return mhd_respond_oom(connection);
222
223 *upload_data_size = 0;
224 } else
225 finished = true;
226
227 for (;;) {
49615dbd 228 r = process_source(source, journal_remote_server_global->file_flags);
c064d8db
ZJS
229 if (r == -EAGAIN)
230 break;
7fdb237f 231 if (r < 0) {
ef4d6abe
ZJS
232 if (r == -ENOBUFS)
233 log_warning_errno(r, "Entry is above the maximum of %u, aborting connection %p.",
7fdb237f 234 DATA_SIZE_MAX, connection);
ef4d6abe
ZJS
235 else if (r == -E2BIG)
236 log_warning_errno(r, "Entry with more fields than the maximum of %u, aborting connection %p.",
237 ENTRY_FIELD_COUNT_MAX, connection);
c064d8db 238 else
7fdb237f
ZJS
239 log_warning_errno(r, "Failed to process data, aborting connection %p: %m",
240 connection);
241 return MHD_NO;
c064d8db
ZJS
242 }
243 }
244
245 if (!finished)
246 return MHD_YES;
247
248 /* The upload is finished */
249
250 remaining = journal_importer_bytes_remaining(&source->importer);
251 if (remaining > 0) {
252 log_warning("Premature EOF byte. %zu bytes lost.", remaining);
253 return mhd_respondf(connection,
254 0, MHD_HTTP_EXPECTATION_FAILED,
255 "Premature EOF. %zu bytes of trailing data not processed.",
256 remaining);
257 }
258
259 return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.");
260};
261
d17eabb1 262static mhd_result request_handler(
c064d8db
ZJS
263 void *cls,
264 struct MHD_Connection *connection,
265 const char *url,
266 const char *method,
267 const char *version,
268 const char *upload_data,
269 size_t *upload_data_size,
270 void **connection_cls) {
271
272 const char *header;
273 int r, code, fd;
274 _cleanup_free_ char *hostname = NULL;
a289dfd6 275 bool chunked = false;
c064d8db
ZJS
276
277 assert(connection);
278 assert(connection_cls);
279 assert(url);
280 assert(method);
281
282 log_trace("Handling a connection %s %s %s", method, url, version);
283
284 if (*connection_cls)
285 return process_http_upload(connection,
286 upload_data, upload_data_size,
287 *connection_cls);
288
289 if (!streq(method, "POST"))
290 return mhd_respond(connection, MHD_HTTP_NOT_ACCEPTABLE, "Unsupported method.");
291
292 if (!streq(url, "/upload"))
293 return mhd_respond(connection, MHD_HTTP_NOT_FOUND, "Not found.");
294
7fdb237f 295 header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Content-Type");
c064d8db
ZJS
296 if (!header || !streq(header, "application/vnd.fdo.journal"))
297 return mhd_respond(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
298 "Content-Type: application/vnd.fdo.journal is required.");
299
a289dfd6
YW
300 header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Transfer-Encoding");
301 if (header) {
302 if (!strcaseeq(header, "chunked"))
303 return mhd_respondf(connection, 0, MHD_HTTP_BAD_REQUEST,
304 "Unsupported Transfer-Encoding type: %s", header);
305
306 chunked = true;
307 }
308
7fdb237f 309 header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Content-Length");
a289dfd6 310 if (header) {
ce2f4379
ZJS
311 size_t len;
312
a289dfd6
YW
313 if (chunked)
314 return mhd_respond(connection, MHD_HTTP_BAD_REQUEST,
1f8ef095 315 "Content-Length not allowed when Transfer-Encoding type is 'chunked'");
a289dfd6
YW
316
317 r = safe_atozu(header, &len);
318 if (r < 0)
319 return mhd_respondf(connection, r, MHD_HTTP_LENGTH_REQUIRED,
320 "Content-Length: %s cannot be parsed: %m", header);
321
322 if (len > ENTRY_SIZE_MAX)
323 /* When serialized, an entry of maximum size might be slightly larger,
324 * so this does not correspond exactly to the limit in journald. Oh well.
325 */
30df858f 326 return mhd_respondf(connection, 0, MHD_HTTP_CONTENT_TOO_LARGE,
a289dfd6
YW
327 "Payload larger than maximum size of %u bytes", ENTRY_SIZE_MAX);
328 }
7fdb237f 329
c064d8db
ZJS
330 {
331 const union MHD_ConnectionInfo *ci;
332
333 ci = MHD_get_connection_info(connection,
334 MHD_CONNECTION_INFO_CONNECTION_FD);
335 if (!ci) {
336 log_error("MHD_get_connection_info failed: cannot get remote fd");
337 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
338 "Cannot check remote address.");
339 }
340
341 fd = ci->connect_fd;
342 assert(fd >= 0);
343 }
344
345 if (journal_remote_server_global->check_trust) {
346 r = check_permissions(connection, &code, &hostname);
347 if (r < 0)
348 return code;
349 } else {
350 r = getpeername_pretty(fd, false, &hostname);
351 if (r < 0)
352 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
353 "Cannot check remote hostname.");
354 }
355
356 assert(hostname);
357
358 r = request_meta(connection_cls, fd, hostname);
359 if (r == -ENOMEM)
360 return respond_oom(connection);
361 else if (r < 0)
362 return mhd_respondf(connection, r, MHD_HTTP_INTERNAL_SERVER_ERROR, "%m");
363
364 hostname = NULL;
365 return MHD_YES;
366}
367
368static int setup_microhttpd_server(RemoteServer *s,
369 int fd,
370 const char *key,
371 const char *cert,
372 const char *trust) {
373 struct MHD_OptionItem opts[] = {
c064d8db 374 { MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) microhttpd_logger},
756ef1fa 375 { MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) request_meta_free},
c064d8db 376 { MHD_OPTION_LISTEN_SOCKET, fd},
5572e9d8 377 { MHD_OPTION_CONNECTION_MEMORY_LIMIT, JOURNAL_SERVER_MEMORY_MAX},
c064d8db
ZJS
378 { MHD_OPTION_END},
379 { MHD_OPTION_END},
380 { MHD_OPTION_END},
381 { MHD_OPTION_END},
382 { MHD_OPTION_END}};
383 int opts_pos = 4;
384 int flags =
385 MHD_USE_DEBUG |
386 MHD_USE_DUAL_STACK |
387 MHD_USE_EPOLL |
388 MHD_USE_ITC;
389
3c67c8bd 390 _cleanup_(MHDDaemonWrapper_freep) MHDDaemonWrapper *d = NULL;
c064d8db
ZJS
391 const union MHD_DaemonInfo *info;
392 int r, epoll_fd;
c064d8db
ZJS
393
394 assert(fd >= 0);
395
396 r = fd_nonblock(fd, true);
397 if (r < 0)
398 return log_error_errno(r, "Failed to make fd:%d nonblocking: %m", fd);
399
400/* MHD_OPTION_STRICT_FOR_CLIENT is introduced in microhttpd 0.9.54,
401 * and MHD_USE_PEDANTIC_CHECKS will be deprecated in future.
402 * If MHD_USE_PEDANTIC_CHECKS is '#define'd, then it is deprecated
403 * and we should use MHD_OPTION_STRICT_FOR_CLIENT. On the other hand,
404 * if MHD_USE_PEDANTIC_CHECKS is not '#define'd, then it is not
405 * deprecated yet and there exists an enum element with the same name.
406 * So we can safely use it. */
407#ifdef MHD_USE_PEDANTIC_CHECKS
408 opts[opts_pos++] = (struct MHD_OptionItem)
409 {MHD_OPTION_STRICT_FOR_CLIENT, 1};
410#else
411 flags |= MHD_USE_PEDANTIC_CHECKS;
412#endif
413
414 if (key) {
415 assert(cert);
416
417 opts[opts_pos++] = (struct MHD_OptionItem)
418 {MHD_OPTION_HTTPS_MEM_KEY, 0, (char*) key};
419 opts[opts_pos++] = (struct MHD_OptionItem)
420 {MHD_OPTION_HTTPS_MEM_CERT, 0, (char*) cert};
421
422 flags |= MHD_USE_TLS;
423
424 if (trust)
425 opts[opts_pos++] = (struct MHD_OptionItem)
426 {MHD_OPTION_HTTPS_MEM_TRUST, 0, (char*) trust};
427 }
428
429 d = new(MHDDaemonWrapper, 1);
430 if (!d)
431 return log_oom();
432
433 d->fd = (uint64_t) fd;
434
435 d->daemon = MHD_start_daemon(flags, 0,
436 NULL, NULL,
437 request_handler, NULL,
438 MHD_OPTION_ARRAY, opts,
439 MHD_OPTION_END);
3c67c8bd
FS
440 if (!d->daemon)
441 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to start μhttp daemon");
c064d8db
ZJS
442
443 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
444 key ? "HTTPS" : "HTTP", fd, d);
445
446 info = MHD_get_daemon_info(d->daemon, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
3c67c8bd
FS
447 if (!info)
448 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "μhttp returned NULL daemon info");
c064d8db
ZJS
449
450 epoll_fd = info->listen_fd;
3c67c8bd
FS
451 if (epoll_fd < 0)
452 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), "μhttp epoll fd is invalid");
c064d8db
ZJS
453
454 r = sd_event_add_io(s->events, &d->io_event,
455 epoll_fd, EPOLLIN,
456 dispatch_http_event, d);
3c67c8bd
FS
457 if (r < 0)
458 return log_error_errno(r, "Failed to add event callback: %m");
c064d8db
ZJS
459
460 r = sd_event_source_set_description(d->io_event, "io_event");
3c67c8bd
FS
461 if (r < 0)
462 return log_error_errno(r, "Failed to set source name: %m");
c064d8db
ZJS
463
464 r = sd_event_add_time(s->events, &d->timer_event,
f5fbe71d 465 CLOCK_MONOTONIC, UINT64_MAX, 0,
c064d8db 466 null_timer_event_handler, d);
3c67c8bd
FS
467 if (r < 0)
468 return log_error_errno(r, "Failed to add timer_event: %m");
c064d8db
ZJS
469
470 r = sd_event_source_set_description(d->timer_event, "timer_event");
3c67c8bd
FS
471 if (r < 0)
472 return log_error_errno(r, "Failed to set source name: %m");
c064d8db 473
4bc25168 474 r = hashmap_ensure_put(&s->daemons, &uint64_hash_ops, &d->fd, d);
3c67c8bd
FS
475 if (r == -ENOMEM)
476 return log_oom();
477 if (r < 0)
478 return log_error_errno(r, "Failed to add daemon to hashmap: %m");
c064d8db 479
3c67c8bd 480 TAKE_PTR(d);
c064d8db
ZJS
481 s->active++;
482 return 0;
c064d8db
ZJS
483}
484
485static int setup_microhttpd_socket(RemoteServer *s,
486 const char *address,
487 const char *key,
488 const char *cert,
489 const char *trust) {
490 int fd;
491
492 fd = make_socket_fd(LOG_DEBUG, address, SOCK_STREAM, SOCK_CLOEXEC);
493 if (fd < 0)
494 return fd;
495
496 return setup_microhttpd_server(s, fd, key, cert, trust);
497}
498
499static int null_timer_event_handler(sd_event_source *timer_event,
500 uint64_t usec,
501 void *userdata) {
502 return dispatch_http_event(timer_event, 0, 0, userdata);
503}
504
505static int dispatch_http_event(sd_event_source *event,
506 int fd,
507 uint32_t revents,
508 void *userdata) {
99534007 509 MHDDaemonWrapper *d = ASSERT_PTR(userdata);
c064d8db 510 int r;
8a8e84d2 511 MHD_UNSIGNED_LONG_LONG timeout = ULLONG_MAX;
c064d8db 512
c064d8db 513 r = MHD_run(d->daemon);
baaa35ad
ZJS
514 if (r == MHD_NO)
515 // FIXME: unregister daemon
516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
517 "MHD_run failed!");
c064d8db 518 if (MHD_get_timeout(d->daemon, &timeout) == MHD_NO)
8a8e84d2 519 timeout = ULLONG_MAX;
c064d8db
ZJS
520
521 r = sd_event_source_set_time(d->timer_event, timeout);
522 if (r < 0) {
523 log_warning_errno(r, "Unable to set event loop timeout: %m, this may result in indefinite blocking!");
524 return 1;
525 }
526
527 r = sd_event_source_set_enabled(d->timer_event, SD_EVENT_ON);
528 if (r < 0)
529 log_warning_errno(r, "Unable to enable timer_event: %m, this may result in indefinite blocking!");
530
531 return 1; /* work to do */
532}
533
534/**********************************************************************
535 **********************************************************************
536 **********************************************************************/
537
538static int setup_signals(RemoteServer *s) {
539 int r;
540
541 assert(s);
542
543 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, -1) >= 0);
544
545 r = sd_event_add_signal(s->events, &s->sigterm_event, SIGTERM, NULL, s);
546 if (r < 0)
547 return r;
548
549 r = sd_event_add_signal(s->events, &s->sigint_event, SIGINT, NULL, s);
550 if (r < 0)
551 return r;
552
553 return 0;
554}
555
556static int setup_raw_socket(RemoteServer *s, const char *address) {
557 int fd;
558
559 fd = make_socket_fd(LOG_INFO, address, SOCK_STREAM, SOCK_CLOEXEC);
560 if (fd < 0)
561 return fd;
562
563 return journal_remote_add_raw_socket(s, fd);
564}
565
566static int create_remoteserver(
567 RemoteServer *s,
568 const char* key,
569 const char* cert,
570 const char* trust) {
571
572 int r, n, fd;
c064d8db 573
49615dbd
LP
574 r = journal_remote_server_init(
575 s,
576 arg_output,
577 arg_split_mode,
578 (arg_compress ? JOURNAL_COMPRESS : 0) |
579 (arg_seal ? JOURNAL_SEAL : 0));
c064d8db
ZJS
580 if (r < 0)
581 return r;
582
83a43339
LP
583 r = setup_signals(s);
584 if (r < 0)
585 return log_error_errno(r, "Failed to set up signals: %m");
c064d8db
ZJS
586
587 n = sd_listen_fds(true);
588 if (n < 0)
589 return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
590 else
591 log_debug("Received %d descriptors", n);
592
baaa35ad
ZJS
593 if (MAX(http_socket, https_socket) >= SD_LISTEN_FDS_START + n)
594 return log_error_errno(SYNTHETIC_ERRNO(EBADFD),
595 "Received fewer sockets than expected");
c064d8db
ZJS
596
597 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
598 if (sd_is_socket(fd, AF_UNSPEC, 0, true)) {
599 log_debug("Received a listening socket (fd:%d)", fd);
600
601 if (fd == http_socket)
602 r = setup_microhttpd_server(s, fd, NULL, NULL, NULL);
603 else if (fd == https_socket)
604 r = setup_microhttpd_server(s, fd, key, cert, trust);
605 else
606 r = journal_remote_add_raw_socket(s, fd);
607 } else if (sd_is_socket(fd, AF_UNSPEC, 0, false)) {
608 char *hostname;
609
610 r = getpeername_pretty(fd, false, &hostname);
611 if (r < 0)
612 return log_error_errno(r, "Failed to retrieve remote name: %m");
613
614 log_debug("Received a connection socket (fd:%d) from %s", fd, hostname);
615
616 r = journal_remote_add_source(s, fd, hostname, true);
baaa35ad
ZJS
617 } else
618 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
619 "Unknown socket passed on fd:%d", fd);
c064d8db
ZJS
620
621 if (r < 0)
baaa35ad 622 return log_error_errno(r, "Failed to register socket (fd:%d): %m", fd);
c064d8db
ZJS
623 }
624
625 if (arg_getter) {
626 log_info("Spawning getter %s...", arg_getter);
627 fd = spawn_getter(arg_getter);
628 if (fd < 0)
629 return fd;
630
631 r = journal_remote_add_source(s, fd, (char*) arg_output, false);
632 if (r < 0)
633 return r;
634 }
635
636 if (arg_url) {
d8dd35fd 637 const char *url, *hostname;
c064d8db
ZJS
638
639 if (!strstr(arg_url, "/entries")) {
640 if (endswith(arg_url, "/"))
641 url = strjoina(arg_url, "entries");
642 else
643 url = strjoina(arg_url, "/entries");
15d121b0 644 } else
2f82562b 645 url = strdupa_safe(arg_url);
c064d8db
ZJS
646
647 log_info("Spawning curl %s...", url);
648 fd = spawn_curl(url);
649 if (fd < 0)
650 return fd;
651
49fe5c09
LP
652 hostname = STARTSWITH_SET(arg_url, "https://", "http://");
653 if (!hostname)
654 hostname = arg_url;
c064d8db 655
2f82562b 656 hostname = strndupa_safe(hostname, strcspn(hostname, "/:"));
c064d8db 657
d8dd35fd 658 r = journal_remote_add_source(s, fd, (char *) hostname, false);
c064d8db
ZJS
659 if (r < 0)
660 return r;
661 }
662
663 if (arg_listen_raw) {
664 log_debug("Listening on a socket...");
665 r = setup_raw_socket(s, arg_listen_raw);
666 if (r < 0)
667 return r;
668 }
669
670 if (arg_listen_http) {
671 r = setup_microhttpd_socket(s, arg_listen_http, NULL, NULL, NULL);
672 if (r < 0)
673 return r;
674 }
675
676 if (arg_listen_https) {
677 r = setup_microhttpd_socket(s, arg_listen_https, key, cert, trust);
678 if (r < 0)
679 return r;
680 }
681
682 STRV_FOREACH(file, arg_files) {
683 const char *output_name;
684
685 if (streq(*file, "-")) {
686 log_debug("Using standard input as source.");
687
688 fd = STDIN_FILENO;
689 output_name = "stdin";
690 } else {
691 log_debug("Reading file %s...", *file);
692
693 fd = open(*file, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
694 if (fd < 0)
695 return log_error_errno(errno, "Failed to open %s: %m", *file);
696 output_name = *file;
697 }
698
699 r = journal_remote_add_source(s, fd, (char*) output_name, false);
700 if (r < 0)
701 return r;
702 }
703
baaa35ad
ZJS
704 if (s->active == 0)
705 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
706 "Zero sources specified");
c064d8db
ZJS
707
708 if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE) {
709 /* In this case we know what the writer will be
710 called, so we can create it and verify that we can
711 create output as expected. */
712 r = journal_remote_get_writer(s, NULL, &s->_single_writer);
713 if (r < 0)
8ee2fd90 714 return log_warning_errno(r, "Failed to get writer: %m");
c064d8db
ZJS
715 }
716
717 return 0;
718}
719
720static int negative_fd(const char *spec) {
721 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
722
723 int fd, r;
724
725 r = safe_atoi(spec, &fd);
726 if (r < 0)
727 return r;
728
729 if (fd > 0)
730 return -EINVAL;
731 else
732 return -fd;
733}
734
735static int parse_config(void) {
736 const ConfigTableItem items[] = {
f12b399d 737 { "Remote", "Seal", config_parse_bool, 0, &arg_seal },
738 { "Remote", "SplitMode", config_parse_write_split_mode, 0, &arg_split_mode },
739 { "Remote", "ServerKeyFile", config_parse_path, 0, &arg_key },
740 { "Remote", "ServerCertificateFile", config_parse_path, 0, &arg_cert },
d7085bcc 741 { "Remote", "TrustedCertificateFile", config_parse_path_or_ignore, 0, &arg_trust },
f12b399d 742 { "Remote", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use },
743 { "Remote", "MaxFileSize", config_parse_iec_uint64, 0, &arg_max_size },
744 { "Remote", "MaxFiles", config_parse_uint64, 0, &arg_n_max_files },
745 { "Remote", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free },
37ec0fdd
LP
746 {}
747 };
c064d8db 748
6378f257
ZJS
749 return config_parse_standard_file_with_dropins(
750 "systemd/journal-remote.conf",
751 "Remote\0",
752 config_item_table_lookup, items,
753 CONFIG_PARSE_WARN,
754 /* userdata= */ NULL);
c064d8db
ZJS
755}
756
37ec0fdd
LP
757static int help(void) {
758 _cleanup_free_ char *link = NULL;
759 int r;
760
761 r = terminal_urlify_man("systemd-journal-remote.service", "8", &link);
762 if (r < 0)
763 return log_oom();
764
c064d8db
ZJS
765 printf("%s [OPTIONS...] {FILE|-}...\n\n"
766 "Write external journal events to journal file(s).\n\n"
767 " -h --help Show this help\n"
768 " --version Show package version\n"
769 " --url=URL Read events from systemd-journal-gatewayd at URL\n"
770 " --getter=COMMAND Read events from the output of COMMAND\n"
771 " --listen-raw=ADDR Listen for connections at ADDR\n"
772 " --listen-http=ADDR Listen for HTTP connections at ADDR\n"
773 " --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
774 " -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
82b2281d 775 " --compress[=BOOL] Use compression in the output journal (default: yes)\n"
c064d8db
ZJS
776 " --seal[=BOOL] Use event sealing (default: no)\n"
777 " --key=FILENAME SSL key in PEM format (default:\n"
778 " \"" PRIV_KEY_FILE "\")\n"
779 " --cert=FILENAME SSL certificate in PEM format (default:\n"
780 " \"" CERT_FILE "\")\n"
781 " --trust=FILENAME|all SSL CA certificate or disable checking (default:\n"
782 " \"" TRUST_FILE "\")\n"
783 " --gnutls-log=CATEGORY...\n"
784 " Specify a list of gnutls logging categories\n"
785 " --split-mode=none|host How many output files to create\n"
37ec0fdd 786 "\nNote: file descriptors from sd_listen_fds() will be consumed, too.\n"
bc556335
DDM
787 "\nSee the %s for details.\n",
788 program_invocation_short_name,
789 link);
37ec0fdd
LP
790
791 return 0;
c064d8db
ZJS
792}
793
794static int parse_argv(int argc, char *argv[]) {
795 enum {
796 ARG_VERSION = 0x100,
797 ARG_URL,
798 ARG_LISTEN_RAW,
799 ARG_LISTEN_HTTP,
800 ARG_LISTEN_HTTPS,
801 ARG_GETTER,
802 ARG_SPLIT_MODE,
803 ARG_COMPRESS,
804 ARG_SEAL,
805 ARG_KEY,
806 ARG_CERT,
807 ARG_TRUST,
808 ARG_GNUTLS_LOG,
809 };
810
811 static const struct option options[] = {
812 { "help", no_argument, NULL, 'h' },
813 { "version", no_argument, NULL, ARG_VERSION },
814 { "url", required_argument, NULL, ARG_URL },
815 { "getter", required_argument, NULL, ARG_GETTER },
816 { "listen-raw", required_argument, NULL, ARG_LISTEN_RAW },
817 { "listen-http", required_argument, NULL, ARG_LISTEN_HTTP },
818 { "listen-https", required_argument, NULL, ARG_LISTEN_HTTPS },
819 { "output", required_argument, NULL, 'o' },
820 { "split-mode", required_argument, NULL, ARG_SPLIT_MODE },
821 { "compress", optional_argument, NULL, ARG_COMPRESS },
822 { "seal", optional_argument, NULL, ARG_SEAL },
823 { "key", required_argument, NULL, ARG_KEY },
824 { "cert", required_argument, NULL, ARG_CERT },
825 { "trust", required_argument, NULL, ARG_TRUST },
826 { "gnutls-log", required_argument, NULL, ARG_GNUTLS_LOG },
827 {}
828 };
829
830 int c, r;
831 bool type_a, type_b;
832
833 assert(argc >= 0);
834 assert(argv);
835
836 while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
79893116 837 switch (c) {
37ec0fdd 838
c064d8db 839 case 'h':
37ec0fdd 840 return help();
c064d8db
ZJS
841
842 case ARG_VERSION:
843 return version();
844
845 case ARG_URL:
baaa35ad
ZJS
846 if (arg_url)
847 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 848 "Cannot currently set more than one --url=");
c064d8db
ZJS
849
850 arg_url = optarg;
851 break;
852
853 case ARG_GETTER:
baaa35ad
ZJS
854 if (arg_getter)
855 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 856 "Cannot currently use --getter= more than once");
c064d8db
ZJS
857
858 arg_getter = optarg;
859 break;
860
861 case ARG_LISTEN_RAW:
baaa35ad
ZJS
862 if (arg_listen_raw)
863 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 864 "Cannot currently use --listen-raw= more than once");
c064d8db
ZJS
865
866 arg_listen_raw = optarg;
867 break;
868
869 case ARG_LISTEN_HTTP:
baaa35ad
ZJS
870 if (arg_listen_http || http_socket >= 0)
871 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 872 "Cannot currently use --listen-http= more than once");
c064d8db
ZJS
873
874 r = negative_fd(optarg);
875 if (r >= 0)
876 http_socket = r;
877 else
878 arg_listen_http = optarg;
879 break;
880
881 case ARG_LISTEN_HTTPS:
baaa35ad
ZJS
882 if (arg_listen_https || https_socket >= 0)
883 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 884 "Cannot currently use --listen-https= more than once");
c064d8db
ZJS
885
886 r = negative_fd(optarg);
887 if (r >= 0)
888 https_socket = r;
889 else
890 arg_listen_https = optarg;
891
892 break;
893
894 case ARG_KEY:
baaa35ad
ZJS
895 if (arg_key)
896 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
897 "Key file specified twice");
c064d8db
ZJS
898
899 arg_key = strdup(optarg);
900 if (!arg_key)
901 return log_oom();
902
903 break;
904
905 case ARG_CERT:
baaa35ad
ZJS
906 if (arg_cert)
907 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
908 "Certificate file specified twice");
c064d8db
ZJS
909
910 arg_cert = strdup(optarg);
911 if (!arg_cert)
912 return log_oom();
913
914 break;
915
916 case ARG_TRUST:
f7adeaeb 917#if HAVE_GNUTLS
d7085bcc 918 if (arg_trust)
baaa35ad 919 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 920 "Cannot use --trust more= than once");
c064d8db 921
d7085bcc
FS
922 arg_trust = strdup(optarg);
923 if (!arg_trust)
924 return log_oom();
c064d8db 925#else
f7adeaeb 926 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 927 "Option --trust= is not available.");
c064d8db 928#endif
c064d8db
ZJS
929 break;
930
931 case 'o':
baaa35ad
ZJS
932 if (arg_output)
933 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 934 "Cannot use --output=/-o more than once");
c064d8db 935
7276d98c
YW
936 r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_output);
937 if (r < 0)
938 return r;
c064d8db
ZJS
939 break;
940
941 case ARG_SPLIT_MODE:
942 arg_split_mode = journal_write_split_mode_from_string(optarg);
baaa35ad 943 if (arg_split_mode == _JOURNAL_WRITE_SPLIT_INVALID)
7211c853 944 return log_error_errno(arg_split_mode, "Invalid split mode: %s", optarg);
c064d8db
ZJS
945 break;
946
947 case ARG_COMPRESS:
9c7f2201
ZJS
948 r = parse_boolean_argument("--compress", optarg, &arg_compress);
949 if (r < 0)
950 return r;
c064d8db
ZJS
951 break;
952
953 case ARG_SEAL:
9c7f2201
ZJS
954 r = parse_boolean_argument("--seal", optarg, &arg_seal);
955 if (r < 0)
956 return r;
c064d8db
ZJS
957 break;
958
b01031e3 959 case ARG_GNUTLS_LOG:
c064d8db 960#if HAVE_GNUTLS
b01031e3 961 for (const char* p = optarg;;) {
c064d8db
ZJS
962 _cleanup_free_ char *word = NULL;
963
964 r = extract_first_word(&p, &word, ",", 0);
965 if (r < 0)
966 return log_error_errno(r, "Failed to parse --gnutls-log= argument: %m");
c064d8db
ZJS
967 if (r == 0)
968 break;
969
970 if (strv_push(&arg_gnutls_log, word) < 0)
971 return log_oom();
972
973 word = NULL;
974 }
975 break;
976#else
baaa35ad 977 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 978 "Option --gnutls-log= is not available.");
c064d8db 979#endif
c064d8db
ZJS
980
981 case '?':
982 return -EINVAL;
983
984 default:
04499a70 985 assert_not_reached();
c064d8db
ZJS
986 }
987
988 if (optind < argc)
989 arg_files = argv + optind;
990
991 type_a = arg_getter || !strv_isempty(arg_files);
992 type_b = arg_url
993 || arg_listen_raw
994 || arg_listen_http || arg_listen_https
995 || sd_listen_fds(false) > 0;
baaa35ad
ZJS
996 if (type_a && type_b)
997 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e
FS
998 "Cannot use file input or --getter= with "
999 "--listen-...= or socket activation.");
c064d8db 1000 if (type_a) {
baaa35ad
ZJS
1001 if (!arg_output)
1002 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 1003 "Option --output= must be specified with file input or --getter=.");
c064d8db 1004
baaa35ad
ZJS
1005 if (!IN_SET(arg_split_mode, JOURNAL_WRITE_SPLIT_NONE, _JOURNAL_WRITE_SPLIT_INVALID))
1006 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1007 "For active sources, only --split-mode=none is allowed.");
c064d8db
ZJS
1008
1009 arg_split_mode = JOURNAL_WRITE_SPLIT_NONE;
1010 }
1011
1012 if (arg_split_mode == _JOURNAL_WRITE_SPLIT_INVALID)
1013 arg_split_mode = JOURNAL_WRITE_SPLIT_HOST;
1014
1015 if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE && arg_output) {
baaa35ad
ZJS
1016 if (is_dir(arg_output, true) > 0)
1017 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1018 "For SplitMode=none, output must be a file.");
1019 if (!endswith(arg_output, ".journal"))
1020 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1021 "For SplitMode=none, output file name must end with .journal.");
c064d8db
ZJS
1022 }
1023
1024 if (arg_split_mode == JOURNAL_WRITE_SPLIT_HOST
baaa35ad
ZJS
1025 && arg_output && is_dir(arg_output, true) <= 0)
1026 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1027 "For SplitMode=host, output must be a directory.");
c064d8db 1028
d7085bcc
FS
1029 if (STRPTR_IN_SET(arg_trust, "-", "all")) {
1030 arg_trust_all = true;
1031 arg_trust = mfree(arg_trust);
1032 }
1033
c064d8db
ZJS
1034 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1035 journal_write_split_mode_to_string(arg_split_mode),
1036 strna(arg_key),
1037 strna(arg_cert),
1038 strna(arg_trust));
1039
1040 return 1 /* work to do */;
1041}
1042
1043static int load_certificates(char **key, char **cert, char **trust) {
1044 int r;
1045
e5de42e6 1046 r = read_full_file_full(
986311c2 1047 AT_FDCWD, arg_key ?: PRIV_KEY_FILE, UINT64_MAX, SIZE_MAX,
e5de42e6
LP
1048 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
1049 NULL,
1050 key, NULL);
c064d8db
ZJS
1051 if (r < 0)
1052 return log_error_errno(r, "Failed to read key from file '%s': %m",
1053 arg_key ?: PRIV_KEY_FILE);
1054
986311c2
LP
1055 r = read_full_file_full(
1056 AT_FDCWD, arg_cert ?: CERT_FILE, UINT64_MAX, SIZE_MAX,
1057 READ_FULL_FILE_CONNECT_SOCKET,
1058 NULL,
1059 cert, NULL);
c064d8db
ZJS
1060 if (r < 0)
1061 return log_error_errno(r, "Failed to read certificate from file '%s': %m",
1062 arg_cert ?: CERT_FILE);
1063
1064 if (arg_trust_all)
1065 log_info("Certificate checking disabled.");
1066 else {
986311c2
LP
1067 r = read_full_file_full(
1068 AT_FDCWD, arg_trust ?: TRUST_FILE, UINT64_MAX, SIZE_MAX,
1069 READ_FULL_FILE_CONNECT_SOCKET,
1070 NULL,
1071 trust, NULL);
c064d8db
ZJS
1072 if (r < 0)
1073 return log_error_errno(r, "Failed to read CA certificate file '%s': %m",
1074 arg_trust ?: TRUST_FILE);
1075 }
1076
baaa35ad
ZJS
1077 if ((arg_listen_raw || arg_listen_http) && *trust)
1078 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2530af2e 1079 "Option --trust= makes all non-HTTPS connections untrusted.");
c064d8db
ZJS
1080
1081 return 0;
1082}
1083
d8dd35fd 1084static int run(int argc, char **argv) {
94952201 1085 _cleanup_(journal_remote_server_destroy) RemoteServer s = {};
d7ac0952 1086 _unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
f362fe73
LP
1087 _cleanup_(erase_and_freep) char *key = NULL;
1088 _cleanup_free_ char *cert = NULL, *trust = NULL;
d8dd35fd 1089 int r;
c064d8db
ZJS
1090
1091 log_show_color(true);
74189020 1092 log_parse_environment();
c064d8db 1093
1abaf488
LP
1094 /* The journal merging logic potentially needs a lot of fds. */
1095 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1096
955fc5d8
YW
1097 sigbus_install();
1098
c064d8db
ZJS
1099 r = parse_config();
1100 if (r < 0)
d8dd35fd 1101 return r;
c064d8db
ZJS
1102
1103 r = parse_argv(argc, argv);
1104 if (r <= 0)
d8dd35fd 1105 return r;
c064d8db
ZJS
1106
1107 if (arg_listen_http || arg_listen_https) {
1108 r = setup_gnutls_logger(arg_gnutls_log);
1109 if (r < 0)
d8dd35fd 1110 return r;
c064d8db
ZJS
1111 }
1112
d7ef030b 1113 if (arg_listen_https || https_socket >= 0) {
d8dd35fd
YW
1114 r = load_certificates(&key, &cert, &trust);
1115 if (r < 0)
1116 return r;
1117
d7ef030b
MG
1118 s.check_trust = !arg_trust_all;
1119 }
c064d8db 1120
f12b399d 1121 journal_reset_metrics(&s.metrics);
1122 s.metrics.max_use = arg_max_use;
1123 s.metrics.max_size = arg_max_size;
a4f82d2c 1124 s.metrics.keep_free = arg_keep_free;
f12b399d 1125 s.metrics.n_max_files = arg_n_max_files;
1126
d8dd35fd
YW
1127 r = create_remoteserver(&s, key, cert, trust);
1128 if (r < 0)
1129 return r;
c064d8db
ZJS
1130
1131 r = sd_event_set_watchdog(s.events, true);
1132 if (r < 0)
d8dd35fd
YW
1133 return log_error_errno(r, "Failed to enable watchdog: %m");
1134
1135 log_debug("Watchdog is %sd.", enable_disable(r > 0));
c064d8db
ZJS
1136
1137 log_debug("%s running as pid "PID_FMT,
1138 program_invocation_short_name, getpid_cached());
d8dd35fd
YW
1139
1140 notify_message = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
c064d8db
ZJS
1141
1142 while (s.active) {
1143 r = sd_event_get_state(s.events);
1144 if (r < 0)
d8dd35fd 1145 return r;
c064d8db
ZJS
1146 if (r == SD_EVENT_FINISHED)
1147 break;
1148
1149 r = sd_event_run(s.events, -1);
d8dd35fd
YW
1150 if (r < 0)
1151 return log_error_errno(r, "Failed to run event loop: %m");
c064d8db
ZJS
1152 }
1153
d8dd35fd
YW
1154 notify_message = NULL;
1155 (void) sd_notifyf(false,
1156 "STOPPING=1\n"
1157 "STATUS=Shutting down after writing %" PRIu64 " entries...", s.event_count);
c064d8db 1158
d8dd35fd 1159 log_info("Finishing after writing %" PRIu64 " entries", s.event_count);
c064d8db 1160
d8dd35fd 1161 return 0;
c064d8db 1162}
d8dd35fd
YW
1163
1164DEFINE_MAIN_FUNCTION(run);