HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/journal-remote/journal-gatewayd.c

44,049 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 417951ff218fb335361b3de0a7d95fc55e63172f

Lines 1001–1311 of 1,311

                m->previous_boot_id = boot.id;
                m->boot_index -= 1;
        }
 
        if (fseeko(m->tmp, pos, SEEK_SET) < 0) {
                log_error_errno(errno, "Failed to seek to position: %m");
                return MHD_CONTENT_READER_END_WITH_ERROR;
        }
 
        size_t n = MIN(m->size - pos, max);
 
        errno = 0;
        size_t k = fread(buf, 1, n, m->tmp);
        if (k != n) {
                log_error("Failed to read from file: %s", STRERROR_OR_EOF(errno));
                return MHD_CONTENT_READER_END_WITH_ERROR;
        }
 
        return (ssize_t) k;
}
 
static int request_handler_boots(
                struct MHD_Connection *connection,
                void *connection_cls) {
 
        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
        RequestMeta *m = ASSERT_PTR(connection_cls);
        int r;
 
        assert(connection);
 
        r = open_journal(m);
        if (r < 0)
                return mhd_respondf(connection, r, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to open journal: %m");
 
        m->previous_boot_id = SD_ID128_NULL;
        m->boot_index = 0;
        r = sd_journal_seek_tail(m->journal); /* seek to newest */
        if (r < 0)
                return mhd_respondf(connection, r, MHD_HTTP_INTERNAL_SERVER_ERROR, "Failed to seek in journal: %m");
 
        response = sym_MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 4*1024, request_reader_boots, m, NULL);
        if (!response)
                return respond_oom(connection);
 
        if (sym_MHD_add_response_header(response, "Content-Type", "application/json-seq") == MHD_NO)
                return respond_oom(connection);
 
        return sym_MHD_queue_response(connection, MHD_HTTP_OK, response);
}
 
static mhd_result request_handler(
                void *cls,
                struct MHD_Connection *connection,
                const char *url,
                const char *method,
                const char *version,
                const char *upload_data,
                size_t *upload_data_size,
                void **connection_cls) {
        int r, code;
 
        assert(connection);
        assert(connection_cls);
        assert(url);
        assert(method);
 
        if (!streq(method, "GET"))
                return mhd_respond(connection, MHD_HTTP_NOT_ACCEPTABLE, "Unsupported method.");
 
        if (!*connection_cls) {
                if (!request_meta(connection_cls))
                        return respond_oom(connection);
                return MHD_YES;
        }
 
        if (arg_trust_pem) {
                r = check_permissions(connection, &code, NULL);
                if (r < 0)
                        return code;
        }
 
        if (streq(url, "/"))
                return request_handler_redirect(connection, "/browse");
 
        if (streq(url, "/entries"))
                return request_handler_entries(connection, *connection_cls);
 
        if (startswith(url, "/fields/"))
                return request_handler_fields(connection, url + 8, *connection_cls);
 
        if (streq(url, "/browse"))
                return request_handler_file(connection, DOCUMENT_ROOT "/browse.html", "text/html");
 
        if (streq(url, "/machine"))
                return request_handler_machine(connection, *connection_cls);
 
        if (streq(url, "/boots"))
                return request_handler_boots(connection, *connection_cls);
 
        return mhd_respond(connection, MHD_HTTP_NOT_FOUND, "Not found.");
}
 
static int parse_argv(int argc, char *argv[]) {
        assert(argc >= 0);
        assert(argv);
 
        OptionParser opts = { argc, argv };
        int r;
 
        FOREACH_OPTION_OR_RETURN(c, &opts)
                switch (c) {
 
                OPTION_COMMON_HELP:
                        return command_print_help();
 
                OPTION_COMMON_VERSION:
                        return version();
 
                OPTION_LONG("cert", "CERT.PEM", "Server certificate in PEM format"):
                        if (arg_cert_pem)
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Certificate file specified twice");
                        r = read_full_file_full(
                                        AT_FDCWD, opts.arg, UINT64_MAX, SIZE_MAX,
                                        READ_FULL_FILE_CONNECT_SOCKET,
                                        NULL,
                                        &arg_cert_pem, NULL);
                        if (r < 0)
                                return log_error_errno(r, "Failed to read certificate file: %m");
                        assert(arg_cert_pem);
                        break;
 
                OPTION_LONG("key", "KEY.PEM", "Server key in PEM format"):
                        if (arg_key_pem)
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Key file specified twice");
                        r = read_full_file_full(
                                        AT_FDCWD, opts.arg, UINT64_MAX, SIZE_MAX,
                                        READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
                                        NULL,
                                        &arg_key_pem, NULL);
                        if (r < 0)
                                return log_error_errno(r, "Failed to read key file: %m");
                        assert(arg_key_pem);
                        break;
 
                OPTION_LONG("trust", "CERT.PEM", "Certificate authority certificate in PEM format"):
#if HAVE_GNUTLS
                        if (arg_trust_pem)
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "CA certificate file specified twice");
                        r = read_full_file_full(
                                        AT_FDCWD, opts.arg, UINT64_MAX, SIZE_MAX,
                                        READ_FULL_FILE_CONNECT_SOCKET,
                                        NULL,
                                        &arg_trust_pem, NULL);
                        if (r < 0)
                                return log_error_errno(r, "Failed to read CA certificate file: %m");
                        assert(arg_trust_pem);
                        break;
#else
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "Option --trust= is not available.");
#endif
 
                OPTION_LONG("system", NULL, "Serve system journal"):
                        arg_journal_type |= SD_JOURNAL_SYSTEM;
                        break;
 
                OPTION_LONG("user", NULL, "Serve the user journal for the current user"):
                        arg_journal_type |= SD_JOURNAL_CURRENT_USER;
                        break;
 
                OPTION('m', "merge", NULL, "Serve all available journals"):
                        arg_merge = true;
                        break;
 
                OPTION('D', "directory", "PATH", "Serve journal files in directory"):
                        r = free_and_strdup_warn(&arg_directory, opts.arg);
                        if (r < 0)
                                return r;
                        break;
 
                OPTION_LONG("file", "PATH", "Serve this journal file"):
                        r = glob_extend(&arg_file, opts.arg, GLOB_NOCHECK);
                        if (r < 0)
                                return log_error_errno(r, "Failed to add paths: %m");
                        break;
 
                OPTION_COMMON_INTROSPECT_CLI:
                        return introspect_cli(SD_JSON_FORMAT_OFF);
                }
 
        if (option_parser_get_n_args(&opts) > 0)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "This program does not take arguments.");
 
        if (!!arg_key_pem != !!arg_cert_pem)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Certificate and key files must be specified together");
 
        if (arg_trust_pem && !arg_key_pem)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "CA certificate can only be used with certificate file");
 
        return 1;
}
 
static int run(int argc, char *argv[]) {
        _cleanup_(MHD_stop_daemonp) struct MHD_Daemon *d = NULL;
        static const struct sigaction sigterm = {
                .sa_handler = nop_signal_handler,
                .sa_flags = SA_RESTART,
        };
        struct MHD_OptionItem opts[] = {
                { MHD_OPTION_EXTERNAL_LOGGER,
                  (intptr_t) microhttpd_logger, NULL },
                { MHD_OPTION_NOTIFY_COMPLETED,
                  (intptr_t) request_meta_free, NULL },
                { MHD_OPTION_END, 0, NULL },
                { MHD_OPTION_END, 0, NULL },
                { MHD_OPTION_END, 0, NULL },
                { MHD_OPTION_END, 0, NULL },
                { MHD_OPTION_END, 0, NULL },
        };
        int opts_pos = 2;
 
        /* We force MHD_USE_ITC here, in order to make sure
         * libmicrohttpd doesn't use shutdown() on our listening
         * socket, which would break socket re-activation. See
         *
         * https://lists.gnu.org/archive/html/libmicrohttpd/2015-09/msg00014.html
         * https://github.com/systemd/systemd/pull/1286
         */
 
        int flags =
                MHD_USE_DEBUG |
                MHD_USE_DUAL_STACK |
                MHD_USE_ITC |
                MHD_USE_POLL_INTERNAL_THREAD |
                MHD_USE_THREAD_PER_CONNECTION;
        int r, n;
 
        COMPRESS_JOURNAL_NOTE;
        LIBGNUTLS_NOTE(suggested);
        LIBMICROHTTPD_NOTE(required);
 
        log_setup();
 
        r = parse_argv(argc, argv);
        if (r <= 0)
                return r;
 
        r = dlopen_microhttpd(LOG_ERR);
        if (r < 0)
                return r;
 
        journal_browse_prepare();
 
        assert_se(sigaction(SIGTERM, &sigterm, NULL) >= 0);
 
        r = setup_gnutls_logger(NULL);
        if (r < 0)
                return r;
 
        n = sd_listen_fds(1);
        if (n < 0)
                return log_error_errno(n, "Failed to determine passed sockets: %m");
        if (n > 1)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Can't listen on more than one socket.");
 
        if (n == 1)
                opts[opts_pos++] = (struct MHD_OptionItem)
                        { MHD_OPTION_LISTEN_SOCKET, SD_LISTEN_FDS_START };
 
        if (arg_key_pem) {
                assert(arg_cert_pem);
                opts[opts_pos++] = (struct MHD_OptionItem)
                        { MHD_OPTION_HTTPS_MEM_KEY, 0, arg_key_pem };
                opts[opts_pos++] = (struct MHD_OptionItem)
                        { MHD_OPTION_HTTPS_MEM_CERT, 0, arg_cert_pem };
                flags |= MHD_USE_TLS;
        }
 
        if (arg_trust_pem) {
                assert(flags & MHD_USE_TLS);
                opts[opts_pos++] = (struct MHD_OptionItem)
                        { MHD_OPTION_HTTPS_MEM_TRUST, 0, arg_trust_pem };
        }
 
        d = sym_MHD_start_daemon(
                        flags,
                        /* port= */ 19531,
                        /* acp= */ NULL,
                        /* acp_cls= */ NULL,
                        request_handler,
                        /* dh_cls= */ NULL,
                        MHD_OPTION_ARRAY,
                        opts,
                        MHD_OPTION_END);
        if (!d)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to start daemon!");
 
        pause();
 
        return 0;
}
 
DEFINE_MAIN_FUNCTION(run);