2015-09-17 18:16:49 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_worker_process.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <unistd.h>
|
2015-09-17 18:16:49 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
|
|
|
#include <sys/resource.h>
|
2016-11-30 14:59:02 +01:00
|
|
|
#include <sys/wait.h>
|
2015-09-17 18:16:49 +02:00
|
|
|
#include <grp.h>
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
|
|
|
#include <ev.h>
|
|
|
|
|
2016-12-10 13:41:03 +01:00
|
|
|
#include <ares.h>
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_connection_handler.h"
|
|
|
|
#include "shrpx_log_config.h"
|
|
|
|
#include "shrpx_worker.h"
|
|
|
|
#include "shrpx_accept_handler.h"
|
|
|
|
#include "shrpx_http2_upstream.h"
|
|
|
|
#include "shrpx_http2_session.h"
|
|
|
|
#include "shrpx_memcached_dispatcher.h"
|
|
|
|
#include "shrpx_memcached_request.h"
|
|
|
|
#include "shrpx_process.h"
|
2017-04-01 08:07:32 +02:00
|
|
|
#include "shrpx_tls.h"
|
2017-02-16 14:46:22 +01:00
|
|
|
#include "shrpx_log.h"
|
2015-09-17 18:16:49 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "app_helper.h"
|
|
|
|
#include "template.h"
|
2016-10-09 12:14:49 +02:00
|
|
|
#include "xsi_strerror.h"
|
2015-09-17 18:16:49 +02:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
2015-09-23 12:45:53 +02:00
|
|
|
void drop_privileges(
|
|
|
|
#ifdef HAVE_NEVERBLEED
|
|
|
|
neverbleed_t *nb
|
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
) {
|
2016-10-09 12:14:49 +02:00
|
|
|
std::array<char, STRERROR_BUFSIZE> errbuf;
|
2016-10-08 04:34:23 +02:00
|
|
|
auto config = get_config();
|
|
|
|
|
|
|
|
if (getuid() == 0 && config->uid != 0) {
|
2017-02-10 09:42:26 +01:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
|
|
|
if (nb) {
|
|
|
|
neverbleed_setuidgid(nb, config->user.c_str(), 1);
|
|
|
|
}
|
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2016-10-08 04:34:23 +02:00
|
|
|
if (initgroups(config->user.c_str(), config->gid) != 0) {
|
2015-09-17 18:16:49 +02:00
|
|
|
auto error = errno;
|
|
|
|
LOG(FATAL) << "Could not change supplementary groups: "
|
2016-10-09 12:14:49 +02:00
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2015-09-17 18:16:49 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-10-08 04:34:23 +02:00
|
|
|
if (setgid(config->gid) != 0) {
|
2015-09-17 18:16:49 +02:00
|
|
|
auto error = errno;
|
2016-10-09 12:14:49 +02:00
|
|
|
LOG(FATAL) << "Could not change gid: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2015-09-17 18:16:49 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-10-08 04:34:23 +02:00
|
|
|
if (setuid(config->uid) != 0) {
|
2015-09-17 18:16:49 +02:00
|
|
|
auto error = errno;
|
2016-10-09 12:14:49 +02:00
|
|
|
LOG(FATAL) << "Could not change uid: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2015-09-17 18:16:49 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (setuid(0) != -1) {
|
|
|
|
LOG(FATAL) << "Still have root privileges?";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void graceful_shutdown(ConnectionHandler *conn_handler) {
|
|
|
|
if (conn_handler->get_graceful_shutdown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(NOTICE) << "Graceful shutdown signal received";
|
|
|
|
|
|
|
|
conn_handler->set_graceful_shutdown(true);
|
|
|
|
|
2018-02-26 14:40:24 +01:00
|
|
|
// TODO What happens for the connections not established in the
|
|
|
|
// kernel?
|
2015-09-17 18:16:49 +02:00
|
|
|
conn_handler->accept_pending_connection();
|
2018-02-26 14:40:24 +01:00
|
|
|
conn_handler->delete_acceptor();
|
2015-09-17 18:16:49 +02:00
|
|
|
|
|
|
|
conn_handler->graceful_shutdown_worker();
|
|
|
|
|
2017-02-21 14:19:34 +01:00
|
|
|
auto single_worker = conn_handler->get_single_worker();
|
|
|
|
if (single_worker) {
|
2021-09-05 11:01:27 +02:00
|
|
|
auto worker_stat = single_worker->get_worker_stat();
|
|
|
|
if (worker_stat->num_connections == 0 &&
|
|
|
|
worker_stat->num_close_waits == 0) {
|
2015-09-28 17:40:33 +02:00
|
|
|
ev_break(conn_handler->get_loop());
|
|
|
|
}
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void reopen_log(ConnectionHandler *conn_handler) {
|
|
|
|
LOG(NOTICE) << "Reopening log files: worker process (thread main)";
|
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
auto config = get_config();
|
|
|
|
auto &loggingconf = config->logging;
|
|
|
|
|
|
|
|
(void)reopen_log_files(loggingconf);
|
|
|
|
redirect_stderr_to_errorlog(loggingconf);
|
2015-09-17 18:16:49 +02:00
|
|
|
|
2017-02-21 14:19:34 +01:00
|
|
|
conn_handler->worker_reopen_log_files();
|
2015-09-17 18:16:49 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void ipc_readcb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
auto conn_handler = static_cast<ConnectionHandler *>(w->data);
|
|
|
|
std::array<uint8_t, 1024> buf;
|
|
|
|
ssize_t nread;
|
|
|
|
while ((nread = read(w->fd, buf.data(), buf.size())) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (nread == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(ERROR) << "Failed to read data from ipc channel: errno=" << error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread == 0) {
|
|
|
|
// IPC socket closed. Perform immediate shutdown.
|
|
|
|
LOG(FATAL) << "IPC socket is closed. Perform immediate shutdown.";
|
2017-01-22 16:32:51 +01:00
|
|
|
nghttp2_Exit(EXIT_FAILURE);
|
2015-09-17 18:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (ssize_t i = 0; i < nread; ++i) {
|
|
|
|
switch (buf[i]) {
|
|
|
|
case SHRPX_IPC_GRACEFUL_SHUTDOWN:
|
|
|
|
graceful_shutdown(conn_handler);
|
|
|
|
break;
|
|
|
|
case SHRPX_IPC_REOPEN_LOG:
|
|
|
|
reopen_log(conn_handler);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
namespace {
|
|
|
|
void quic_ipc_readcb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
auto conn_handler = static_cast<ConnectionHandler *>(w->data);
|
|
|
|
|
|
|
|
if (conn_handler->quic_ipc_read() != 0) {
|
|
|
|
LOG(ERROR) << "Failed to read data from QUIC IPC channel";
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#endif // ENABLE_HTTP3
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
namespace {
|
|
|
|
int generate_ticket_key(TicketKey &ticket_key) {
|
2016-01-18 06:21:09 +01:00
|
|
|
ticket_key.cipher = get_config()->tls.ticket.cipher;
|
2015-09-17 18:16:49 +02:00
|
|
|
ticket_key.hmac = EVP_sha256();
|
|
|
|
ticket_key.hmac_keylen = EVP_MD_size(ticket_key.hmac);
|
|
|
|
|
|
|
|
assert(static_cast<size_t>(EVP_CIPHER_key_length(ticket_key.cipher)) <=
|
|
|
|
ticket_key.data.enc_key.size());
|
|
|
|
assert(ticket_key.hmac_keylen <= ticket_key.data.hmac_key.size());
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "enc_keylen=" << EVP_CIPHER_key_length(ticket_key.cipher)
|
|
|
|
<< ", hmac_keylen=" << ticket_key.hmac_keylen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RAND_bytes(reinterpret_cast<unsigned char *>(&ticket_key.data),
|
|
|
|
sizeof(ticket_key.data)) == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void renew_ticket_key_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto conn_handler = static_cast<ConnectionHandler *>(w->data);
|
|
|
|
const auto &old_ticket_keys = conn_handler->get_ticket_keys();
|
|
|
|
|
|
|
|
auto ticket_keys = std::make_shared<TicketKeys>();
|
|
|
|
LOG(NOTICE) << "Renew new ticket keys";
|
|
|
|
|
|
|
|
// If old_ticket_keys is not empty, it should contain at least 2
|
|
|
|
// keys: one for encryption, and last one for the next encryption
|
|
|
|
// key but decryption only. The keys in between are old keys and
|
|
|
|
// decryption only. The next key is provided to ensure to mitigate
|
|
|
|
// possible problem when one worker encrypt new key, but one worker,
|
|
|
|
// which did not take the that key yet, and cannot decrypt it.
|
|
|
|
//
|
|
|
|
// We keep keys for get_config()->tls_session_timeout seconds. The
|
|
|
|
// default is 12 hours. Thus the maximum ticket vector size is 12.
|
|
|
|
if (old_ticket_keys) {
|
|
|
|
auto &old_keys = old_ticket_keys->keys;
|
|
|
|
auto &new_keys = ticket_keys->keys;
|
|
|
|
|
|
|
|
assert(!old_keys.empty());
|
|
|
|
|
|
|
|
auto max_tickets =
|
|
|
|
static_cast<size_t>(std::chrono::duration_cast<std::chrono::hours>(
|
2016-10-15 11:36:04 +02:00
|
|
|
get_config()->tls.session_timeout)
|
|
|
|
.count());
|
2015-09-17 18:16:49 +02:00
|
|
|
|
|
|
|
new_keys.resize(std::min(max_tickets, old_keys.size() + 1));
|
|
|
|
std::copy_n(std::begin(old_keys), new_keys.size() - 1,
|
|
|
|
std::begin(new_keys) + 1);
|
|
|
|
} else {
|
|
|
|
ticket_keys->keys.resize(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &new_key = ticket_keys->keys[0];
|
|
|
|
|
|
|
|
if (generate_ticket_key(new_key) != 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "failed to generate ticket key";
|
|
|
|
}
|
|
|
|
conn_handler->set_ticket_keys(nullptr);
|
|
|
|
conn_handler->set_ticket_keys_to_worker(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "ticket keys generation done";
|
|
|
|
assert(ticket_keys->keys.size() >= 1);
|
|
|
|
LOG(INFO) << 0 << " enc+dec: "
|
|
|
|
<< util::format_hex(ticket_keys->keys[0].data.name);
|
|
|
|
for (size_t i = 1; i < ticket_keys->keys.size(); ++i) {
|
|
|
|
auto &key = ticket_keys->keys[i];
|
|
|
|
LOG(INFO) << i << " dec: " << util::format_hex(key.data.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_handler->set_ticket_keys(ticket_keys);
|
|
|
|
conn_handler->set_ticket_keys_to_worker(ticket_keys);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void memcached_get_ticket_key_cb(struct ev_loop *loop, ev_timer *w,
|
|
|
|
int revents) {
|
|
|
|
auto conn_handler = static_cast<ConnectionHandler *>(w->data);
|
|
|
|
auto dispatcher = conn_handler->get_tls_ticket_key_memcached_dispatcher();
|
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
auto req = std::make_unique<MemcachedRequest>();
|
2015-09-17 18:16:49 +02:00
|
|
|
req->key = "nghttpx:tls-ticket-key";
|
2018-10-17 07:02:57 +02:00
|
|
|
req->op = MemcachedOp::GET;
|
2017-04-12 15:09:44 +02:00
|
|
|
req->cb = [conn_handler, w](MemcachedRequest *req, MemcachedResult res) {
|
2015-09-17 18:16:49 +02:00
|
|
|
switch (res.status_code) {
|
2018-10-17 07:11:36 +02:00
|
|
|
case MemcachedStatusCode::NO_ERROR:
|
2015-09-17 18:16:49 +02:00
|
|
|
break;
|
2018-10-17 07:11:36 +02:00
|
|
|
case MemcachedStatusCode::EXT_NETWORK_ERROR:
|
2015-09-17 18:16:49 +02:00
|
|
|
conn_handler->on_tls_ticket_key_network_error(w);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
conn_handler->on_tls_ticket_key_not_found(w);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |version (4bytes)|len (2bytes)|key (variable length)|...
|
|
|
|
// (len, key) pairs are repeated as necessary.
|
|
|
|
|
|
|
|
auto &value = res.value;
|
|
|
|
if (value.size() < 4) {
|
|
|
|
LOG(WARN) << "Memcached: tls ticket key value is too small: got "
|
|
|
|
<< value.size();
|
|
|
|
conn_handler->on_tls_ticket_key_not_found(w);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto p = value.data();
|
|
|
|
auto version = util::get_uint32(p);
|
|
|
|
// Currently supported version is 1.
|
|
|
|
if (version != 1) {
|
|
|
|
LOG(WARN) << "Memcached: tls ticket key version: want 1, got " << version;
|
|
|
|
conn_handler->on_tls_ticket_key_not_found(w);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto end = p + value.size();
|
|
|
|
p += 4;
|
|
|
|
|
2016-01-18 06:21:09 +01:00
|
|
|
auto &ticketconf = get_config()->tls.ticket;
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
size_t expectedlen;
|
|
|
|
size_t enc_keylen;
|
|
|
|
size_t hmac_keylen;
|
2016-01-18 06:21:09 +01:00
|
|
|
if (ticketconf.cipher == EVP_aes_128_cbc()) {
|
2015-09-17 18:16:49 +02:00
|
|
|
expectedlen = 48;
|
|
|
|
enc_keylen = 16;
|
|
|
|
hmac_keylen = 16;
|
2016-01-18 06:21:09 +01:00
|
|
|
} else if (ticketconf.cipher == EVP_aes_256_cbc()) {
|
2015-09-17 18:16:49 +02:00
|
|
|
expectedlen = 80;
|
|
|
|
enc_keylen = 32;
|
|
|
|
hmac_keylen = 32;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ticket_keys = std::make_shared<TicketKeys>();
|
|
|
|
|
|
|
|
for (; p != end;) {
|
|
|
|
if (end - p < 2) {
|
|
|
|
LOG(WARN) << "Memcached: tls ticket key data is too small";
|
|
|
|
conn_handler->on_tls_ticket_key_not_found(w);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto len = util::get_uint16(p);
|
|
|
|
p += 2;
|
|
|
|
if (len != expectedlen) {
|
|
|
|
LOG(WARN) << "Memcached: wrong tls ticket key size: want "
|
|
|
|
<< expectedlen << ", got " << len;
|
|
|
|
conn_handler->on_tls_ticket_key_not_found(w);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p + len > end) {
|
|
|
|
LOG(WARN) << "Memcached: too short tls ticket key payload: want " << len
|
|
|
|
<< ", got " << (end - p);
|
|
|
|
conn_handler->on_tls_ticket_key_not_found(w);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto key = TicketKey();
|
2016-01-18 06:21:09 +01:00
|
|
|
key.cipher = ticketconf.cipher;
|
2015-09-17 18:16:49 +02:00
|
|
|
key.hmac = EVP_sha256();
|
|
|
|
key.hmac_keylen = hmac_keylen;
|
|
|
|
|
2016-05-15 14:05:20 +02:00
|
|
|
std::copy_n(p, key.data.name.size(), std::begin(key.data.name));
|
2015-09-17 18:16:49 +02:00
|
|
|
p += key.data.name.size();
|
|
|
|
|
2016-05-15 14:05:20 +02:00
|
|
|
std::copy_n(p, enc_keylen, std::begin(key.data.enc_key));
|
2015-09-17 18:16:49 +02:00
|
|
|
p += enc_keylen;
|
|
|
|
|
2016-05-15 14:05:20 +02:00
|
|
|
std::copy_n(p, hmac_keylen, std::begin(key.data.hmac_key));
|
2015-09-17 18:16:49 +02:00
|
|
|
p += hmac_keylen;
|
|
|
|
|
|
|
|
ticket_keys->keys.push_back(std::move(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_handler->on_tls_ticket_key_get_success(ticket_keys, w);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Memcached: tls ticket key get request sent";
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatcher->add_request(std::move(req));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
|
|
|
namespace {
|
|
|
|
void nb_child_cb(struct ev_loop *loop, ev_child *w, int revents) {
|
|
|
|
log_chld(w->rpid, w->rstatus, "neverbleed process");
|
|
|
|
|
|
|
|
ev_child_stop(loop, w);
|
|
|
|
|
|
|
|
LOG(FATAL) << "neverbleed process exitted; aborting now";
|
|
|
|
|
2017-01-22 16:32:51 +01:00
|
|
|
nghttp2_Exit(EXIT_FAILURE);
|
2015-09-23 12:45:53 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
int worker_process_event_loop(WorkerProcessConfig *wpconf) {
|
2016-12-10 13:41:03 +01:00
|
|
|
int rv;
|
2016-10-09 12:14:49 +02:00
|
|
|
std::array<char, STRERROR_BUFSIZE> errbuf;
|
2016-10-23 12:17:17 +02:00
|
|
|
(void)errbuf;
|
2016-10-09 12:14:49 +02:00
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
auto config = get_config();
|
|
|
|
|
|
|
|
if (reopen_log_files(config->logging) != 0) {
|
2015-09-17 18:16:49 +02:00
|
|
|
LOG(FATAL) << "Failed to open log file";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-10 13:41:03 +01:00
|
|
|
rv = ares_library_init(ARES_LIB_INIT_ALL);
|
|
|
|
if (rv != 0) {
|
|
|
|
LOG(FATAL) << "ares_library_init failed: " << ares_strerror(rv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
auto loop = EV_DEFAULT;
|
|
|
|
|
2017-03-26 14:14:34 +02:00
|
|
|
auto gen = util::make_mt19937();
|
2016-07-08 16:41:53 +02:00
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2018-08-24 15:15:43 +02:00
|
|
|
std::array<char, NEVERBLEED_ERRBUF_SIZE> nb_errbuf;
|
2018-10-15 16:02:44 +02:00
|
|
|
auto nb = std::make_unique<neverbleed_t>();
|
2018-08-24 15:15:43 +02:00
|
|
|
if (neverbleed_init(nb.get(), nb_errbuf.data()) != 0) {
|
|
|
|
LOG(FATAL) << "neverbleed_init failed: " << nb_errbuf.data();
|
|
|
|
return -1;
|
2015-09-23 12:45:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
LOG(NOTICE) << "neverbleed process [" << nb->daemon_pid << "] spawned";
|
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
ev_child nb_childev;
|
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
ev_child_init(&nb_childev, nb_child_cb, nb->daemon_pid, 0);
|
|
|
|
nb_childev.data = nullptr;
|
|
|
|
ev_child_start(loop, &nb_childev);
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2021-08-24 15:15:41 +02:00
|
|
|
auto conn_handler = std::make_unique<ConnectionHandler>(loop, gen);
|
|
|
|
|
|
|
|
#ifdef HAVE_NEVERBLEED
|
|
|
|
conn_handler->set_neverbleed(nb.get());
|
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
conn_handler->set_quic_ipc_fd(wpconf->quic_ipc_fd);
|
|
|
|
conn_handler->set_quic_lingering_worker_processes(
|
|
|
|
wpconf->quic_lingering_worker_processes);
|
|
|
|
#endif // ENABLE_HTTP3
|
|
|
|
|
2021-08-24 15:15:41 +02:00
|
|
|
for (auto &addr : config->conn.listener.addrs) {
|
|
|
|
conn_handler->add_acceptor(
|
|
|
|
std::make_unique<AcceptHandler>(&addr, conn_handler.get()));
|
|
|
|
}
|
|
|
|
|
2016-02-13 10:17:11 +01:00
|
|
|
MemchunkPool mcpool;
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
ev_timer renew_ticket_key_timer;
|
2017-04-01 08:07:32 +02:00
|
|
|
if (tls::upstream_tls_enabled(config->conn)) {
|
2016-10-08 04:34:23 +02:00
|
|
|
auto &ticketconf = config->tls.ticket;
|
2016-02-13 10:17:11 +01:00
|
|
|
auto &memcachedconf = ticketconf.memcached;
|
2016-01-18 06:21:09 +01:00
|
|
|
|
2016-02-14 12:59:10 +01:00
|
|
|
if (!memcachedconf.host.empty()) {
|
2016-02-13 10:17:11 +01:00
|
|
|
SSL_CTX *ssl_ctx = nullptr;
|
|
|
|
|
|
|
|
if (memcachedconf.tls) {
|
2018-08-24 15:15:43 +02:00
|
|
|
ssl_ctx = conn_handler->create_tls_ticket_key_memcached_ssl_ctx();
|
2016-02-13 10:17:11 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
conn_handler->set_tls_ticket_key_memcached_dispatcher(
|
2018-10-15 16:02:44 +02:00
|
|
|
std::make_unique<MemcachedDispatcher>(
|
2016-02-13 10:17:11 +01:00
|
|
|
&ticketconf.memcached.addr, loop, ssl_ctx,
|
2016-07-08 16:41:53 +02:00
|
|
|
StringRef{memcachedconf.host}, &mcpool, gen));
|
2015-09-17 18:16:49 +02:00
|
|
|
|
|
|
|
ev_timer_init(&renew_ticket_key_timer, memcached_get_ticket_key_cb, 0.,
|
|
|
|
0.);
|
2018-08-24 15:15:43 +02:00
|
|
|
renew_ticket_key_timer.data = conn_handler.get();
|
2015-09-17 18:16:49 +02:00
|
|
|
// Get first ticket keys.
|
|
|
|
memcached_get_ticket_key_cb(loop, &renew_ticket_key_timer, 0);
|
|
|
|
} else {
|
|
|
|
bool auto_tls_ticket_key = true;
|
2016-01-18 06:21:09 +01:00
|
|
|
if (!ticketconf.files.empty()) {
|
|
|
|
if (!ticketconf.cipher_given) {
|
2015-09-17 18:16:49 +02:00
|
|
|
LOG(WARN)
|
|
|
|
<< "It is strongly recommended to specify "
|
|
|
|
"--tls-ticket-key-cipher=aes-128-cbc (or "
|
|
|
|
"tls-ticket-key-cipher=aes-128-cbc in configuration file) "
|
|
|
|
"when --tls-ticket-key-file is used for the smooth "
|
|
|
|
"transition when the default value of --tls-ticket-key-cipher "
|
|
|
|
"becomes aes-256-cbc";
|
|
|
|
}
|
|
|
|
auto ticket_keys = read_tls_ticket_key_file(
|
2016-01-18 06:21:09 +01:00
|
|
|
ticketconf.files, ticketconf.cipher, EVP_sha256());
|
2015-09-17 18:16:49 +02:00
|
|
|
if (!ticket_keys) {
|
|
|
|
LOG(WARN) << "Use internal session ticket key generator";
|
|
|
|
} else {
|
2018-08-24 15:15:43 +02:00
|
|
|
conn_handler->set_ticket_keys(std::move(ticket_keys));
|
2015-09-17 18:16:49 +02:00
|
|
|
auto_tls_ticket_key = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto_tls_ticket_key) {
|
|
|
|
// Generate new ticket key every 1hr.
|
|
|
|
ev_timer_init(&renew_ticket_key_timer, renew_ticket_key_cb, 0., 1_h);
|
2018-08-24 15:15:43 +02:00
|
|
|
renew_ticket_key_timer.data = conn_handler.get();
|
2015-09-17 18:16:49 +02:00
|
|
|
ev_timer_again(loop, &renew_ticket_key_timer);
|
|
|
|
|
|
|
|
// Generate first session ticket key before running workers.
|
|
|
|
renew_ticket_key_cb(loop, &renew_ticket_key_timer, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:11:19 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-09-22 11:24:50 +02:00
|
|
|
auto &quicconf = config->quic;
|
|
|
|
|
|
|
|
std::shared_ptr<QUICKeyingMaterials> qkms;
|
|
|
|
|
|
|
|
if (!quicconf.upstream.secret_file.empty()) {
|
|
|
|
qkms = read_quic_secret_file(quicconf.upstream.secret_file);
|
|
|
|
if (!qkms) {
|
|
|
|
LOG(WARN) << "Use QUIC keying materials generated internally";
|
|
|
|
}
|
2021-08-26 10:11:19 +02:00
|
|
|
}
|
2021-08-29 03:18:59 +02:00
|
|
|
|
2021-09-22 11:24:50 +02:00
|
|
|
if (!qkms) {
|
|
|
|
qkms = std::make_shared<QUICKeyingMaterials>();
|
|
|
|
qkms->keying_materials.resize(1);
|
|
|
|
|
|
|
|
auto &qkm = qkms->keying_materials.front();
|
|
|
|
|
|
|
|
if (RAND_bytes(qkm.reserved.data(), qkm.reserved.size()) != 1) {
|
|
|
|
LOG(ERROR) << "Failed to generate QUIC secret reserved data";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RAND_bytes(qkm.secret.data(), qkm.secret.size()) != 1) {
|
|
|
|
LOG(ERROR) << "Failed to generate QUIC secret";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RAND_bytes(qkm.salt.data(), qkm.salt.size()) != 1) {
|
|
|
|
LOG(ERROR) << "Failed to generate QUIC salt";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &qkm : qkms->keying_materials) {
|
|
|
|
if (generate_quic_connection_id_encryption_key(
|
|
|
|
qkm.cid_encryption_key.data(), qkm.cid_encryption_key.size(),
|
|
|
|
qkm.secret.data(), qkm.secret.size(), qkm.salt.data(),
|
|
|
|
qkm.salt.size()) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to generate QUIC Connection ID encryption key";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_handler->set_quic_keying_materials(std::move(qkms));
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
conn_handler->set_cid_prefixes(wpconf->cid_prefixes);
|
|
|
|
conn_handler->set_quic_lingering_worker_processes(
|
|
|
|
wpconf->quic_lingering_worker_processes);
|
2021-08-26 10:11:19 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
|
|
|
|
2017-02-21 14:19:34 +01:00
|
|
|
if (config->single_thread) {
|
2018-08-24 15:15:43 +02:00
|
|
|
rv = conn_handler->create_single_worker();
|
2015-09-24 16:33:28 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-17 18:16:49 +02:00
|
|
|
} else {
|
2015-09-24 16:33:28 +02:00
|
|
|
#ifndef NOTHREADS
|
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGCHLD);
|
|
|
|
|
|
|
|
rv = pthread_sigmask(SIG_BLOCK, &set, nullptr);
|
|
|
|
if (rv != 0) {
|
2016-10-09 12:14:49 +02:00
|
|
|
LOG(ERROR) << "Blocking SIGCHLD failed: "
|
|
|
|
<< xsi_strerror(rv, errbuf.data(), errbuf.size());
|
2015-09-24 16:33:28 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif // !NOTHREADS
|
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
rv = conn_handler->create_worker_thread(config->num_worker);
|
2015-09-24 16:33:28 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-17 18:16:49 +02:00
|
|
|
|
2015-09-24 16:33:28 +02:00
|
|
|
#ifndef NOTHREADS
|
|
|
|
rv = pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
|
|
|
|
if (rv != 0) {
|
2016-10-09 12:14:49 +02:00
|
|
|
LOG(ERROR) << "Unblocking SIGCHLD failed: "
|
|
|
|
<< xsi_strerror(rv, errbuf.data(), errbuf.size());
|
2015-09-24 16:33:28 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif // !NOTHREADS
|
2015-09-17 18:16:49 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:23:03 +02:00
|
|
|
#if defined(ENABLE_HTTP3) && defined(HAVE_LIBBPF)
|
2021-10-04 15:39:31 +02:00
|
|
|
conn_handler->unload_bpf_objects();
|
2021-10-04 16:23:03 +02:00
|
|
|
#endif // defined(ENABLE_HTTP3) && defined(HAVE_LIBBPF)
|
2021-10-04 15:39:31 +02:00
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
drop_privileges(
|
|
|
|
#ifdef HAVE_NEVERBLEED
|
2018-08-24 15:15:43 +02:00
|
|
|
nb.get()
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2017-11-23 06:19:12 +01:00
|
|
|
);
|
2015-09-17 18:16:49 +02:00
|
|
|
|
|
|
|
ev_io ipcev;
|
|
|
|
ev_io_init(&ipcev, ipc_readcb, wpconf->ipc_fd, EV_READ);
|
2018-08-24 15:15:43 +02:00
|
|
|
ipcev.data = conn_handler.get();
|
2015-09-17 18:16:49 +02:00
|
|
|
ev_io_start(loop, &ipcev);
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
ev_io quic_ipcev;
|
|
|
|
ev_io_init(&quic_ipcev, quic_ipc_readcb, wpconf->quic_ipc_fd, EV_READ);
|
|
|
|
quic_ipcev.data = conn_handler.get();
|
|
|
|
ev_io_start(loop, &quic_ipcev);
|
|
|
|
#endif // ENABLE_HTTP3
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
if (tls::upstream_tls_enabled(config->conn) && !config->tls.ocsp.disabled) {
|
2017-05-18 14:11:10 +02:00
|
|
|
if (config->tls.ocsp.startup) {
|
2018-08-24 15:15:43 +02:00
|
|
|
conn_handler->set_enable_acceptor_on_ocsp_completion(true);
|
|
|
|
conn_handler->disable_acceptor();
|
2017-05-18 14:11:10 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
conn_handler->proceed_next_cert_ocsp();
|
2015-09-17 18:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Entering event loop";
|
|
|
|
}
|
|
|
|
|
|
|
|
ev_run(loop, 0);
|
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
conn_handler->cancel_ocsp_update();
|
|
|
|
|
|
|
|
// Destroy SSL_CTX held in conn_handler before killing neverbleed
|
|
|
|
// daemon. Otherwise priv_rsa_finish yields "write error" and
|
|
|
|
// worker process aborts.
|
|
|
|
conn_handler.reset();
|
2015-09-17 18:16:49 +02:00
|
|
|
|
2016-11-30 14:59:02 +01:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2018-08-24 15:15:43 +02:00
|
|
|
assert(nb->daemon_pid > 0);
|
2016-11-30 14:59:02 +01:00
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
rv = kill(nb->daemon_pid, SIGTERM);
|
|
|
|
if (rv != 0) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(ERROR) << "Could not send signal to neverbleed daemon: errno=" << error;
|
|
|
|
}
|
2016-11-30 14:59:02 +01:00
|
|
|
|
2018-08-24 15:15:43 +02:00
|
|
|
while ((rv = waitpid(nb->daemon_pid, nullptr, 0)) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (rv == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(ERROR) << "Error occurred while we were waiting for the completion "
|
|
|
|
"of neverbleed process: errno="
|
|
|
|
<< error;
|
2016-11-30 14:59:02 +01:00
|
|
|
}
|
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2016-12-10 13:41:03 +01:00
|
|
|
ares_library_cleanup();
|
|
|
|
|
2015-09-17 18:16:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|