1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2010 - 2015, Göteborg Bit Factory.
//
// 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.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <assert.h>
#include <Server.h>
#include <TLSServer.h>
#include <Timer.h>
#include <text.h>
// Indicates that certain signals were caught.
bool _sighup = false;
bool _sigusr1 = false;
bool _sigusr2 = false;
////////////////////////////////////////////////////////////////////////////////
static void signal_handler (int s)
{
switch (s)
{
case SIGHUP: _sighup = true; break; // Graceful stop
case SIGUSR1: _sigusr1 = true; break; // Config reload
case SIGUSR2: _sigusr2 = true; break;
}
}
////////////////////////////////////////////////////////////////////////////////
Server::Server ()
: _log (NULL)
, _log_clients (false)
, _client_address ("")
, _client_port (0)
, _host ("::")
, _port ("12345")
, _pool_size (4)
, _queue_size (10)
, _daemon (false)
, _pid_file ("")
, _request_count (0)
, _limit (0) // Unlimited
, _ca_file ("")
, _cert_file ("")
, _key_file ("")
, _crl_file ("")
{
}
////////////////////////////////////////////////////////////////////////////////
Server::~Server ()
{
}
////////////////////////////////////////////////////////////////////////////////
void Server::setPort (const std::string& port)
{
if (_log) _log->format ("Using port %s", port.c_str ());
_port = port;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setHost (const std::string& host)
{
if (_log) _log->format ("Using address %s", host.c_str ());
_host = host;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setFamily (const std::string& family)
{
if (_log) _log->format ("Using family %s", family.c_str ());
_family = family;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setQueueSize (int size)
{
if (_log) _log->format ("Queue size %d requests", size);
_queue_size = size;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setPoolSize (int size)
{
if (_log) _log->format ("Thread Pool size %d", size);
_pool_size = size;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setDaemon ()
{
if (_log) _log->write ("Will run as daemon");
_daemon = true;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setPidFile (const std::string& file)
{
if (_log) _log->write ("PID file " + file);
assert (file.length () > 0);
_pid_file = file;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setLimit (int max)
{
if (_log) _log->format ("Request size limit %d bytes", max);
assert (max >= 0);
_limit = max;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setCAFile (const std::string& file)
{
if (_log) _log->format ("CA %s", file.c_str ());
_ca_file = file;
File cert (file);
if (! cert.readable ())
throw format ("CA Certificate not readable: '{1}'", file);
}
////////////////////////////////////////////////////////////////////////////////
void Server::setCertFile (const std::string& file)
{
if (_log) _log->format ("Certificate %s", file.c_str ());
_cert_file = file;
File cert (file);
if (! cert.readable ())
throw format ("Server Certificate not readable: '{1}'", file);
}
////////////////////////////////////////////////////////////////////////////////
void Server::setKeyFile (const std::string& file)
{
if (_log) _log->format ("Private Key %s", file.c_str ());
_key_file = file;
File key (file);
if (! key.readable ())
throw format ("Server key not readable: '{1}'", file);
}
////////////////////////////////////////////////////////////////////////////////
void Server::setCRLFile (const std::string& file)
{
if (_log) _log->format ("CRL %s", file.c_str ());
_crl_file = file;
File crl (file);
if (! crl.readable ())
throw format ("CRL Certificate not readable: '{1}'", file);
}
////////////////////////////////////////////////////////////////////////////////
void Server::setLogClients (bool value)
{
if (_log) _log->format ("IP logging %s", (value ? "on" : "off"));
_log_clients = value;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setLog (Log* l)
{
_log = l;
}
////////////////////////////////////////////////////////////////////////////////
void Server::setConfig (Config* c)
{
_config = c;
}
////////////////////////////////////////////////////////////////////////////////
void Server::beginServer ()
{
if (_log) _log->write ("Server starting");
if (_daemon)
{
daemonize (); // Only the child returns.
writePidFile ();
}
signal (SIGHUP, signal_handler); // Graceful stop
signal (SIGUSR1, signal_handler); // Config reload
signal (SIGUSR2, signal_handler);
TLSServer server;
if (_config)
{
server.debug (_config->getInteger ("debug.tls"));
std::string ciphers = _config->get ("ciphers");
if (ciphers != "")
{
server.ciphers (ciphers);
if (_log) _log->format ("Using ciphers: %s", ciphers.c_str ());
}
std::string trust = _config->get ("trust");
if (trust == "allow all")
server.trust (TLSServer::allow_all);
else if (trust == "strict")
server.trust (TLSServer::strict);
else if (_log)
_log->format ("Invalid 'trust' setting value of '%s'", trust.c_str ());
}
server.init (_ca_file, // CA
_crl_file, // CRL
_cert_file, // Cert
_key_file); // Key
server.queue (_queue_size);
server.bind (_host, _port, _family);
server.listen ();
if (_log) _log->write ("Server ready");
_request_count = 0;
while (1)
{
try
{
TLSTransaction tx;
tx.trust (server.trust ());
server.accept (tx);
if (_sighup)
throw "SIGHUP shutdown.";
// Get client address and port, for logging.
if (_log_clients)
tx.getClient (_client_address, _client_port);
// Metrics.
HighResTimer timer;
timer.start ();
std::string input;
tx.recv (input);
// Handle the request.
++_request_count;
// Call the derived class handler.
std::string output;
handler (input, output);
if (output.length ())
tx.send (output);
if (_log)
{
timer.stop ();
_log->format ("[%d] Serviced in %.6fs", _request_count, timer.total ());
}
}
catch (std::string& e) { if (_log) _log->write (std::string ("Error: ") + e); }
catch (char* e) { if (_log) _log->write (std::string ("Error: ") + e); }
catch (...) { if (_log) _log->write ("Error: Unknown exception"); }
}
}
////////////////////////////////////////////////////////////////////////////////
// TODO To provide these data, a request count, a start time, and a cumulative
// utilization time must be tracked.
void Server::stats (int& requests, time_t& uptime, double& utilization)
{
requests = _request_count;
uptime = 0;
utilization = 0.0;
}
////////////////////////////////////////////////////////////////////////////////
void Server::daemonize ()
{
if (_log) _log->write ("Daemonizing");
/* TODO Load RUN_AS_USER from config.
// If run as root, switch to preferred user.
if (getuid () == 0 || geteuid () == 0 )
{
struct passwd *pw = getpwnam (RUN_AS_USER);
if (pw)
{
if (_log) _log->write ("setting user to " RUN_AS_USER);
setuid (pw->pw_uid);
}
}
*/
// Fork off the parent process
pid_t pid = fork ();
if (pid < 0)
{
exit (EXIT_FAILURE);
}
// If we got a good PID, then we can exit the parent process.
if (pid > 0)
{
exit (EXIT_SUCCESS);
}
// Change the file mode mask
umask (0);
// Create a new SID for the child process
pid_t sid = setsid ();
if (sid < 0)
{
if (_log) _log->write ("setsid failed");
exit (EXIT_FAILURE);
}
// Change the current working directory
// Why is this important? To ensure that program is independent of $CWD?
if ((chdir ("/")) < 0)
{
if (_log) _log->write ("chdir failed");
exit (EXIT_FAILURE);
}
// Redirect standard files to /dev/null.
freopen ("/dev/null", "r", stdin);
freopen ("/dev/null", "w", stdout);
freopen ("/dev/null", "w", stderr);
if (_log) _log->write ("Daemonized");
}
////////////////////////////////////////////////////////////////////////////////
void Server::writePidFile ()
{
pid_t pid = getpid ();
FILE* output = fopen (_pid_file.c_str (), "w");
if (output)
{
fprintf (output, "%d", pid);
fclose (output);
}
else
if (_log) _log->write ("Error: could not write PID to '" + _pid_file + "'.");
}
////////////////////////////////////////////////////////////////////////////////
void Server::removePidFile ()
{
assert (_pid_file.length () > 0);
unlink (_pid_file.c_str ());
}
////////////////////////////////////////////////////////////////////////////////
|