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