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