2012-06-04 16:48:31 +02:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 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_config.h"
|
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <netdb.h>
|
2012-08-01 18:20:18 +02:00
|
|
|
#include <syslog.h>
|
2012-08-01 17:06:41 +02:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <limits>
|
|
|
|
#include <fstream>
|
|
|
|
|
2012-08-01 17:26:24 +02:00
|
|
|
#include "shrpx_log.h"
|
2012-08-01 17:06:41 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using namespace spdylay;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
const char SHRPX_OPT_PRIVATE_KEY_FILE[] = "private-key-file";
|
|
|
|
const char SHRPX_OPT_CERTIFICATE_FILE[] = "certificate-file";
|
|
|
|
|
|
|
|
const char SHRPX_OPT_BACKEND[] = "backend";
|
|
|
|
const char SHRPX_OPT_FRONTEND[] = "frontend";
|
|
|
|
const char SHRPX_OPT_WORKERS[] = "workers";
|
|
|
|
const char
|
|
|
|
SHRPX_OPT_SPDY_MAX_CONCURRENT_STREAMS[] = "spdy-max-concurrent-streams";
|
|
|
|
const char SHRPX_OPT_LOG_LEVEL[] = "log-level";
|
|
|
|
const char SHRPX_OPT_DAEMON[] = "daemon";
|
|
|
|
const char SHRPX_OPT_SPDY_PROXY[] = "spdy-proxy";
|
|
|
|
const char SHRPX_OPT_ADD_X_FORWARDED_FOR[] = "add-x-forwarded-for";
|
|
|
|
const char
|
|
|
|
SHRPX_OPT_FRONTEND_SPDY_READ_TIMEOUT[] = "frontend-spdy-read-timeout";
|
|
|
|
const char SHRPX_OPT_FRONTEND_READ_TIMEOUT[] = "frontend-read-timeout";
|
|
|
|
const char SHRPX_OPT_FRONTEND_WRITE_TIMEOUT[] = "frontend-write-timeout";
|
|
|
|
const char SHRPX_OPT_BACKEND_READ_TIMEOUT[] = "backend-read-timeout";
|
|
|
|
const char SHRPX_OPT_BACKEND_WRITE_TIMEOUT[] = "backend-write-timeout";
|
|
|
|
const char SHRPX_OPT_ACCESSLOG[] = "accesslog";
|
|
|
|
const char
|
|
|
|
SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT[] = "backend-keep-alive-timeout";
|
|
|
|
const char SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS[] = "frontend-spdy-window-bits";
|
|
|
|
const char SHRPX_OPT_PID_FILE[] = "pid-file";
|
|
|
|
const char SHRPX_OPT_USER[] = "user";
|
2012-08-01 18:20:18 +02:00
|
|
|
const char SHRPX_OPT_SYSLOG[] = "syslog";
|
|
|
|
const char SHRPX_OPT_SYSLOG_FACILITY[] = "syslog-facility";
|
2012-08-01 18:28:59 +02:00
|
|
|
const char SHRPX_OPT_BACKLOG[] = "backlog";
|
2012-08-20 14:50:03 +02:00
|
|
|
const char SHRPX_OPT_CIPHERS[] = "ciphers";
|
2012-08-01 17:06:41 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
Config::Config()
|
|
|
|
: verbose(false),
|
|
|
|
daemon(false),
|
|
|
|
host(0),
|
|
|
|
port(0),
|
|
|
|
private_key_file(0),
|
|
|
|
cert_file(0),
|
|
|
|
verify_client(false),
|
|
|
|
server_name(0),
|
|
|
|
downstream_host(0),
|
|
|
|
downstream_port(0),
|
|
|
|
downstream_hostport(0),
|
2012-06-05 18:26:04 +02:00
|
|
|
downstream_addrlen(0),
|
2012-06-06 16:58:19 +02:00
|
|
|
num_worker(0),
|
2012-07-11 09:20:16 +02:00
|
|
|
spdy_max_concurrent_streams(0),
|
2012-07-12 16:39:11 +02:00
|
|
|
spdy_proxy(false),
|
2012-07-17 18:08:05 +02:00
|
|
|
add_x_forwarded_for(false),
|
2012-07-26 16:18:37 +02:00
|
|
|
accesslog(false),
|
2012-07-31 18:51:16 +02:00
|
|
|
spdy_upstream_window_bits(0),
|
|
|
|
pid_file(0),
|
|
|
|
uid(0),
|
2012-08-01 17:06:41 +02:00
|
|
|
gid(0),
|
2012-08-01 18:20:18 +02:00
|
|
|
conf_path(0),
|
|
|
|
syslog(false),
|
|
|
|
syslog_facility(0),
|
2012-08-01 18:28:59 +02:00
|
|
|
use_syslog(false),
|
2012-08-20 14:50:03 +02:00
|
|
|
backlog(0),
|
|
|
|
ciphers(0)
|
2012-06-04 16:48:31 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Config *config = 0;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const Config* get_config()
|
|
|
|
{
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config* mod_config()
|
|
|
|
{
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_config()
|
|
|
|
{
|
|
|
|
config = new Config();
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
namespace {
|
|
|
|
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
|
|
|
|
const char *hostport)
|
|
|
|
{
|
|
|
|
// host and port in |hostport| is separated by single ','.
|
|
|
|
const char *p = strchr(hostport, ',');
|
|
|
|
if(!p) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Invalid host, port: " << hostport;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
size_t len = p-hostport;
|
|
|
|
if(hostlen < len+1) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Hostname too long: " << hostport;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(host, hostport, len);
|
|
|
|
host[len] = '\0';
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
unsigned long d = strtoul(p+1, 0, 10);
|
|
|
|
if(errno == 0 && 1 <= d && d <= std::numeric_limits<uint16_t>::max()) {
|
|
|
|
*port_ptr = d;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Port is invalid: " << p+1;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void set_config_str(char **destp, const char *val)
|
|
|
|
{
|
|
|
|
if(*destp) {
|
|
|
|
free(*destp);
|
|
|
|
}
|
|
|
|
*destp = strdup(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_config(const char *opt, const char *optarg)
|
|
|
|
{
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
uint16_t port;
|
|
|
|
if(util::strieq(opt, SHRPX_OPT_BACKEND)) {
|
|
|
|
if(split_host_port(host, sizeof(host), &port, optarg) == -1) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
set_config_str(&mod_config()->downstream_host, host);
|
|
|
|
mod_config()->downstream_port = port;
|
|
|
|
}
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_FRONTEND)) {
|
|
|
|
if(split_host_port(host, sizeof(host), &port, optarg) == -1) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
set_config_str(&mod_config()->host, host);
|
|
|
|
mod_config()->port = port;
|
|
|
|
}
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_WORKERS)) {
|
|
|
|
mod_config()->num_worker = strtol(optarg, 0, 10);
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_SPDY_MAX_CONCURRENT_STREAMS)) {
|
|
|
|
mod_config()->spdy_max_concurrent_streams = strtol(optarg, 0, 10);
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_LOG_LEVEL)) {
|
|
|
|
if(Log::set_severity_level_by_name(optarg) == -1) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Invalid severity level: " << optarg;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_DAEMON)) {
|
|
|
|
mod_config()->daemon = util::strieq(optarg, "yes");
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_SPDY_PROXY)) {
|
|
|
|
mod_config()->spdy_proxy = util::strieq(optarg, "yes");
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_ADD_X_FORWARDED_FOR)) {
|
|
|
|
mod_config()->add_x_forwarded_for = util::strieq(optarg, "yes");
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_FRONTEND_SPDY_READ_TIMEOUT)) {
|
|
|
|
timeval tv = {strtol(optarg, 0, 10), 0};
|
|
|
|
mod_config()->spdy_upstream_read_timeout = tv;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_FRONTEND_READ_TIMEOUT)) {
|
|
|
|
timeval tv = {strtol(optarg, 0, 10), 0};
|
|
|
|
mod_config()->upstream_read_timeout = tv;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_FRONTEND_WRITE_TIMEOUT)) {
|
|
|
|
timeval tv = {strtol(optarg, 0, 10), 0};
|
|
|
|
mod_config()->upstream_write_timeout = tv;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_BACKEND_READ_TIMEOUT)) {
|
|
|
|
timeval tv = {strtol(optarg, 0, 10), 0};
|
|
|
|
mod_config()->downstream_read_timeout = tv;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_BACKEND_WRITE_TIMEOUT)) {
|
|
|
|
timeval tv = {strtol(optarg, 0, 10), 0};
|
|
|
|
mod_config()->downstream_write_timeout = tv;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_ACCESSLOG)) {
|
|
|
|
mod_config()->accesslog = util::strieq(optarg, "yes");
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT)) {
|
|
|
|
timeval tv = {strtol(optarg, 0, 10), 0};
|
|
|
|
mod_config()->downstream_idle_read_timeout = tv;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS)) {
|
|
|
|
errno = 0;
|
|
|
|
unsigned long int n = strtoul(optarg, 0, 10);
|
|
|
|
if(errno == 0 && n < 31) {
|
|
|
|
mod_config()->spdy_upstream_window_bits = n;
|
|
|
|
} else {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "-w: specify the integer in the range [0, 30], inclusive";
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_PID_FILE)) {
|
|
|
|
set_config_str(&mod_config()->pid_file, optarg);
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_USER)) {
|
|
|
|
passwd *pwd = getpwnam(optarg);
|
|
|
|
if(pwd == 0) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "--user: failed to get uid from " << optarg
|
|
|
|
<< ": " << strerror(errno);
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mod_config()->uid = pwd->pw_uid;
|
|
|
|
mod_config()->gid = pwd->pw_gid;
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_PRIVATE_KEY_FILE)) {
|
|
|
|
set_config_str(&mod_config()->private_key_file, optarg);
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_CERTIFICATE_FILE)) {
|
|
|
|
set_config_str(&mod_config()->cert_file, optarg);
|
2012-08-01 18:20:18 +02:00
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_SYSLOG)) {
|
|
|
|
mod_config()->syslog = util::strieq(optarg, "yes");
|
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_SYSLOG_FACILITY)) {
|
|
|
|
int facility = int_syslog_facility(optarg);
|
|
|
|
if(facility == -1) {
|
|
|
|
LOG(ERROR) << "Unknown syslog facility: " << optarg;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mod_config()->syslog_facility = facility;
|
2012-08-01 18:28:59 +02:00
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_BACKLOG)) {
|
|
|
|
mod_config()->backlog = strtol(optarg, 0, 10);
|
2012-08-20 14:50:03 +02:00
|
|
|
} else if(util::strieq(opt, SHRPX_OPT_CIPHERS)) {
|
|
|
|
set_config_str(&mod_config()->ciphers, optarg);
|
2012-08-01 17:06:41 +02:00
|
|
|
} else if(util::strieq(opt, "conf")) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(WARNING) << "conf is ignored";
|
2012-08-01 17:06:41 +02:00
|
|
|
} else {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Unknown option: " << opt;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int load_config(const char *filename)
|
|
|
|
{
|
|
|
|
std::ifstream in(filename, std::ios::binary);
|
|
|
|
if(!in) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Could not open config file " << filename;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
std::string line;
|
|
|
|
int linenum = 0;
|
|
|
|
while(std::getline(in, line)) {
|
|
|
|
++linenum;
|
|
|
|
if(line.empty() || line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t i;
|
|
|
|
size_t size = line.size();
|
|
|
|
for(i = 0; i < size && line[i] != '='; ++i);
|
|
|
|
if(i == size) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(ERROR) << "Bad configuration format at line " << linenum;
|
2012-08-01 17:06:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
|
|
|
const char *s = line.c_str();
|
|
|
|
if(parse_config(s, s+i+1) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-01 18:20:18 +02:00
|
|
|
const char* str_syslog_facility(int facility)
|
|
|
|
{
|
|
|
|
switch(facility) {
|
|
|
|
case(LOG_AUTH):
|
|
|
|
return "auth";
|
|
|
|
case(LOG_AUTHPRIV):
|
|
|
|
return "authpriv";
|
|
|
|
case(LOG_CRON):
|
|
|
|
return "cron";
|
|
|
|
case(LOG_DAEMON):
|
|
|
|
return "daemon";
|
|
|
|
case(LOG_FTP):
|
|
|
|
return "ftp";
|
|
|
|
case(LOG_KERN):
|
|
|
|
return "kern";
|
|
|
|
case(LOG_LOCAL0):
|
|
|
|
return "local0";
|
|
|
|
case(LOG_LOCAL1):
|
|
|
|
return "local1";
|
|
|
|
case(LOG_LOCAL2):
|
|
|
|
return "local2";
|
|
|
|
case(LOG_LOCAL3):
|
|
|
|
return "local3";
|
|
|
|
case(LOG_LOCAL4):
|
|
|
|
return "local4";
|
|
|
|
case(LOG_LOCAL5):
|
|
|
|
return "local5";
|
|
|
|
case(LOG_LOCAL6):
|
|
|
|
return "local6";
|
|
|
|
case(LOG_LOCAL7):
|
|
|
|
return "local7";
|
|
|
|
case(LOG_LPR):
|
|
|
|
return "lpr";
|
|
|
|
case(LOG_MAIL):
|
|
|
|
return "mail";
|
|
|
|
case(LOG_SYSLOG):
|
|
|
|
return "syslog";
|
|
|
|
case(LOG_USER):
|
|
|
|
return "user";
|
|
|
|
case(LOG_UUCP):
|
|
|
|
return "uucp";
|
|
|
|
default:
|
|
|
|
return "(unknown)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int int_syslog_facility(const char *strfacility)
|
|
|
|
{
|
|
|
|
if(util::strieq(strfacility, "auth")) {
|
|
|
|
return LOG_AUTH;
|
|
|
|
} else if(util::strieq(strfacility, "authpriv")) {
|
|
|
|
return LOG_AUTHPRIV;
|
|
|
|
} else if(util::strieq(strfacility, "cron")) {
|
|
|
|
return LOG_CRON;
|
|
|
|
} else if(util::strieq(strfacility, "daemon")) {
|
|
|
|
return LOG_DAEMON;
|
|
|
|
} else if(util::strieq(strfacility, "ftp")) {
|
|
|
|
return LOG_FTP;
|
|
|
|
} else if(util::strieq(strfacility, "kern")) {
|
|
|
|
return LOG_KERN;
|
|
|
|
} else if(util::strieq(strfacility, "local0")) {
|
|
|
|
return LOG_LOCAL0;
|
|
|
|
} else if(util::strieq(strfacility, "local1")) {
|
|
|
|
return LOG_LOCAL1;
|
|
|
|
} else if(util::strieq(strfacility, "local2")) {
|
|
|
|
return LOG_LOCAL2;
|
|
|
|
} else if(util::strieq(strfacility, "local3")) {
|
|
|
|
return LOG_LOCAL3;
|
|
|
|
} else if(util::strieq(strfacility, "local4")) {
|
|
|
|
return LOG_LOCAL4;
|
|
|
|
} else if(util::strieq(strfacility, "local5")) {
|
|
|
|
return LOG_LOCAL5;
|
|
|
|
} else if(util::strieq(strfacility, "local6")) {
|
|
|
|
return LOG_LOCAL6;
|
|
|
|
} else if(util::strieq(strfacility, "local7")) {
|
|
|
|
return LOG_LOCAL7;
|
|
|
|
} else if(util::strieq(strfacility, "lpr")) {
|
|
|
|
return LOG_LPR;
|
|
|
|
} else if(util::strieq(strfacility, "mail")) {
|
|
|
|
return LOG_MAIL;
|
|
|
|
} else if(util::strieq(strfacility, "news")) {
|
|
|
|
return LOG_NEWS;
|
|
|
|
} else if(util::strieq(strfacility, "syslog")) {
|
|
|
|
return LOG_SYSLOG;
|
|
|
|
} else if(util::strieq(strfacility, "user")) {
|
|
|
|
return LOG_USER;
|
|
|
|
} else if(util::strieq(strfacility, "uucp")) {
|
|
|
|
return LOG_UUCP;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|