Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 678 lines (572 sloc) 17.78 kb
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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include "common-internal.h"

#include <signal.h>
#include <sys/stat.h>

#include "init.h"
#include "server.h"
#include "socket.h"
#include "spawner.h"
#include "config_reader.h"
#include "server-protected.h"
#include "util.h"

#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif

#ifdef HAVE_GETOPT_LONG
# include <getopt.h>
#else
# include "getopt/getopt.h"
#endif

#define APP_NAME \
"Cherokee Web Server: Admin"

#define APP_COPY_NOTICE \
"Written by Alvaro Lopez Ortega <alvaro@alobbs.com>\n\n" \
"Copyright (C) 2001-2011 Alvaro Lopez Ortega.\n" \
"This is free software; see the source for copying conditions. There is NO\n" \
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"

#define ALPHA_NUM "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
#define PASSWORD_LEN 16
#define DEFAULT_PORT 9090
#define TIMEOUT "25"
#define THREAD_MAX_AUTO 4
#define DEFAULT_DOCUMENTROOT CHEROKEE_DATADIR "/admin"
#define DEFAULT_CONFIG_FILE CHEROKEE_CONFDIR "/cherokee.conf"
#define DEFAULT_UNIX_SOCKET TMPDIR "/cherokee-admin-scgi.socket"
#define DEFAULT_BIND "127.0.0.1"
#define RULE_PRE "vserver!1!rule!"

static int port = DEFAULT_PORT;
static char *document_root = NULL;
static char *config_file = NULL;
static char *bind_to = NULL;
static int debug = 0;
static int unsecure = 0;
static int iocache = 1;
static int scgi_port = 4000;
static int thread_num = -1;
static cherokee_server_t *srv = NULL;
static cherokee_buffer_t password = CHEROKEE_BUF_INIT;


static ret_t
find_empty_port (int starting, int *port)
{
ret_t ret;
cherokee_socket_t s;
int p = starting;
cherokee_buffer_t bind_ = CHEROKEE_BUF_INIT;

cherokee_buffer_add_str (&bind_, "127.0.0.1");
cherokee_socket_init (&s);

while (true) {
cherokee_socket_create_fd (&s, AF_INET);
ret = cherokee_socket_bind (&s, p, &bind_);
cherokee_socket_close (&s);

if (ret == ret_ok)
break;

p += 1;
if (unlikely (p > 0xFFFF)) {
goto error;
}
}

cherokee_socket_mrproper (&s);
cherokee_buffer_mrproper (&bind_);

*port = p;
return ret_ok;

error:
cherokee_socket_close (&s);
cherokee_socket_mrproper (&s);
cherokee_buffer_mrproper (&bind_);
return ret_error;
}

static ret_t
generate_password (cherokee_buffer_t *buf)
{
cuint_t i;
cuint_t n;

for (i=0; i<PASSWORD_LEN; i++) {
n = cherokee_random()%(sizeof(ALPHA_NUM)-1);
cherokee_buffer_add_char (buf, ALPHA_NUM[n]);
}

return ret_ok;
}

static ret_t
remove_old_socket (const char *path)
{
int re;
struct stat info;

/* It might not exist
*/
re = cherokee_stat (path, &info);
if (re != 0) {
return ret_ok;
}

/* If exist, it must be a socket
*/
if (! S_ISSOCK(info.st_mode)) {
PRINT_MSG ("ERROR: Something happens; '%s' isn't a socket.\n", path);
return ret_error;
}

/* Remove it
*/
re = cherokee_unlink (path);
if (re != 0) {
PRINT_MSG ("ERROR: Couldn't remove unix socket '%s'.\n", path);
return ret_error;
}

return ret_ok;
}


static void
print_connection_info (void)
{
printf ("\n");

if (! cherokee_buffer_is_empty (&password)) {
printf ("Login:\n"
" User: admin\n"
" One-time Password: %s\n\n", password.buf);
}

printf ("Web Interface:\n"
" URL: http://%s:%d/\n\n",
(bind_to) ? bind_to : "localhost", port);

fflush(stdout);
}


static ret_t
config_server (cherokee_server_t *srv)
{
ret_t ret;
cherokee_config_node_t conf;
cuint_t nthreads;
cherokee_buffer_t buf = CHEROKEE_BUF_INIT;
cherokee_buffer_t rrd_dir = CHEROKEE_BUF_INIT;
cherokee_buffer_t rrd_bin = CHEROKEE_BUF_INIT;
cherokee_buffer_t fake;

/* Generate the password
*/
if (unsecure == 0) {
ret = generate_password (&password);
if (ret != ret_ok)
return ret;
}

/* Configure the embedded server
*/
if (scgi_port > 0) {
ret = find_empty_port (scgi_port, &scgi_port);
} else {
ret = remove_old_socket (DEFAULT_UNIX_SOCKET);
}
if (ret != ret_ok) {
return ret_error;
}

cherokee_buffer_add_va (&buf, "server!bind!1!port = %d\n", port);
cherokee_buffer_add_str (&buf, "server!ipv6 = 1\n");
cherokee_buffer_add_str (&buf, "server!max_connection_reuse = 0\n");
cherokee_buffer_add_va (&buf, "server!iocache = %d\n", iocache);

if (bind_to) {
cherokee_buffer_add_va (&buf, "server!bind!1!interface = %s\n", bind_to);
}

if (thread_num != -1) {
/* Manually set
*/
cherokee_buffer_add_va (&buf, "server!thread_number = %d\n", thread_num);
} else {
/* Automatically set
*/
nthreads = MIN (cherokee_cpu_number, THREAD_MAX_AUTO);
cherokee_buffer_add_va (&buf, "server!thread_number = %d\n", nthreads);
}

cherokee_buffer_add_str (&buf, "vserver!1!nick = default\n");
cherokee_buffer_add_va (&buf, "vserver!1!document_root = %s\n", document_root);

if (scgi_port <= 0) {
cherokee_buffer_add_va (&buf,
"source!1!nick = app-logic\n"
"source!1!type = interpreter\n"
"source!1!timeout = " TIMEOUT "\n"
"source!1!host = %s\n"
"source!1!interpreter = %s/server.py %s %s %s\n"
"source!1!env_inherited = 1\n",
DEFAULT_UNIX_SOCKET, document_root,
DEFAULT_UNIX_SOCKET, config_file,
(debug) ? "-x" : "");

} else {
cherokee_buffer_add_va (&buf,
"source!1!nick = app-logic\n"
"source!1!type = interpreter\n"
"source!1!timeout = " TIMEOUT "\n"
"source!1!host = localhost:%d\n"
"source!1!interpreter = %s/server.py %d %s %s\n"
"source!1!env_inherited = 1\n",
scgi_port, document_root,
scgi_port, config_file,
(debug) ? "-x" : "");
}

if (debug) {
cherokee_buffer_add_str (&buf, "source!1!debug = 1\n");
}

cherokee_buffer_add_str (&buf,
RULE_PRE "1!match = default\n"
RULE_PRE "1!handler = scgi\n"
RULE_PRE "1!timeout = " TIMEOUT "\n"
RULE_PRE "1!handler!balancer = round_robin\n"
RULE_PRE "1!handler!balancer!source!1 = 1\n");

cherokee_buffer_add_str (&buf, RULE_PRE "1!handler!env!CTK_COOKIE = ");
generate_password (&buf);
cherokee_buffer_add_char (&buf, '\n');

cherokee_buffer_add_str (&buf, RULE_PRE "1!handler!env!CTK_SUBMITTER_SECRET = ");
generate_password (&buf);
cherokee_buffer_add_char (&buf, '\n');

if (! debug) {
cherokee_buffer_add_str (&buf, RULE_PRE "1!encoder!gzip = 1\n");
}

if ((unsecure == 0) &&
(!cherokee_buffer_is_empty (&password)))
{
cherokee_buffer_add_va (&buf,
RULE_PRE "1!auth = authlist\n"
RULE_PRE "1!auth!methods = digest\n"
RULE_PRE "1!auth!realm = Cherokee-admin\n"
RULE_PRE "1!auth!list!1!user = admin\n"
RULE_PRE "1!auth!list!1!password = %s\n",
password.buf);
}

cherokee_buffer_add_str (&buf,
RULE_PRE "2!match = directory\n"
RULE_PRE "2!match!directory = /about\n"
RULE_PRE "2!handler = server_info\n");

cherokee_buffer_add_str (&buf,
RULE_PRE "3!match = directory\n"
RULE_PRE "3!match!directory = /static\n"
RULE_PRE "3!handler = file\n"
RULE_PRE "3!expiration = time\n"
RULE_PRE "3!expiration!time = 30d\n");

cherokee_buffer_add_va (&buf,
RULE_PRE "4!match = request\n"
RULE_PRE "4!match!request = ^/favicon.ico$\n"
RULE_PRE "4!document_root = %s/static/images\n"
RULE_PRE "4!handler = file\n"
RULE_PRE "4!expiration = time\n"
RULE_PRE "4!expiration!time = 30d\n",
document_root);

cherokee_buffer_add_va (&buf,
RULE_PRE "5!match = directory\n"
RULE_PRE "5!match!directory = /icons_local\n"
RULE_PRE "5!handler = file\n"
RULE_PRE "5!document_root = %s\n"
RULE_PRE "5!expiration = time\n"
RULE_PRE "5!expiration!time = 30d\n",
CHEROKEE_ICONSDIR);

cherokee_buffer_add_va (&buf,
RULE_PRE "6!match = directory\n"
RULE_PRE "6!match!directory = /CTK\n"
RULE_PRE "6!handler = file\n"
RULE_PRE "6!document_root = %s/CTK/static\n"
RULE_PRE "6!expiration = time\n"
RULE_PRE "6!expiration!time = 30d\n",
document_root);

/* Embedded help
*/
cherokee_buffer_add_va (&buf,
RULE_PRE "7!match = and\n"
RULE_PRE "7!match!left = directory\n"
RULE_PRE "7!match!left!directory = /help\n"
RULE_PRE "7!match!right = not\n"
RULE_PRE "7!match!right!right = extensions\n"
RULE_PRE "7!match!right!right!extensions = html\n"
RULE_PRE "7!handler = file\n");

cherokee_buffer_add_va (&buf,
RULE_PRE "8!match = fullpath\n"
RULE_PRE "8!match!fullpath!1 = /static/help_404.html\n"
RULE_PRE "8!handler = file\n"
RULE_PRE "8!document_root = %s\n", document_root);

cherokee_buffer_add_va (&buf,
RULE_PRE "9!match = and\n"
RULE_PRE "9!match!left = directory\n"
RULE_PRE "9!match!left!directory = /help\n"
RULE_PRE "9!match!right = not\n"
RULE_PRE "9!match!right!right = exists\n"
RULE_PRE "9!match!right!right!match_any = 1\n"
RULE_PRE "9!handler = redir\n"
RULE_PRE "9!handler!rewrite!1!show = 1\n"
RULE_PRE "9!handler!rewrite!1!substring = /static/help_404.html\n");

cherokee_buffer_add_va (&buf,
RULE_PRE "10!match = directory\n"
RULE_PRE "10!match!directory = /help\n"
RULE_PRE "10!match!final = 0\n"
RULE_PRE "10!document_root = %s\n", CHEROKEE_DOCDIR);

/* GZip
*/
if (! debug) {
cherokee_buffer_add_va (&buf,
RULE_PRE "15!match = extensions\n"
RULE_PRE "15!match!extensions = css,js,html\n"
RULE_PRE "15!match!final = 0\n"
RULE_PRE "15!encoder!gzip = 1\n");
}

/* RRDtool graphs
*/
cherokee_config_node_init (&conf);
cherokee_buffer_fake (&fake, config_file, strlen(config_file));

ret = cherokee_config_reader_parse (&conf, &fake);
if (ret == ret_ok) {
cherokee_config_node_copy (&conf, "server!collector!rrdtool_path", &rrd_bin);
cherokee_config_node_copy (&conf, "server!collector!database_dir", &rrd_dir);
}

if (! cherokee_buffer_is_empty (&rrd_bin)) {
cherokee_buffer_add_va (&buf,
RULE_PRE "20!handler!rrdtool_path = %s\n", rrd_bin.buf);
}

if (! cherokee_buffer_is_empty (&rrd_dir)) {
cherokee_buffer_add_va (&buf,
RULE_PRE "20!handler!database_dir = %s\n", rrd_dir.buf);
}

cherokee_buffer_add_str (&buf,
RULE_PRE "20!match = directory\n"
RULE_PRE "20!match!directory = /graphs\n"
RULE_PRE "20!handler = render_rrd\n"
RULE_PRE "20!expiration = epoch\n"
RULE_PRE "20!expiration!caching = no-cache\n"
RULE_PRE "20!expiration!caching!no-store = 1\n");

cherokee_buffer_add_str (&buf, RULE_PRE "20!document_root = ");
cherokee_buffer_add_buffer (&buf, &cherokee_tmp_dir);
cherokee_buffer_add_va (&buf, "/rrd-cache\n");

/* MIME types
*/
cherokee_buffer_add_str (&buf,
"mime!text/javascript!extensions = js\n"
"mime!text/css!extensions = css\n"
"mime!image/png!extensions = png\n"
"mime!image/jpeg!extensions = jpeg,jpg\n"
"mime!image/svg+xml!extensions = svg,svgz\n"
"mime!image/gif!extensions = gif\n");

ret = cherokee_server_read_config_string (srv, &buf);
if (ret != ret_ok) {
PRINT_ERROR_S ("Could not initialize the server\n");
return ret;
}

cherokee_config_node_mrproper (&conf);

cherokee_buffer_mrproper (&rrd_bin);
cherokee_buffer_mrproper (&rrd_dir);
cherokee_buffer_mrproper (&buf);

return ret_ok;
}


static void
print_help (void)
{
printf (APP_NAME "\n"
"Usage: cherokee-admin [options]\n\n"
" -h, --help Print this help\n"
" -V, --version Print version and exit\n"
" -x, --debug Enables debug\n"
" -u, --unsecure Turn off the authentication\n"
" -b[<IP>], --bind[=<IP>] Bind net iface; no arg means all\n"
" -d<DIR>, --appdir=<DIR> Application directory\n"
" -p<NUM>, --port=<NUM> TCP port\n"
" -t, --internal-unix Use a Unix domain socket internally\n"
" -i, --disable-iocache Disable I/O cache: reduces mem usage\n"
                " -T<NUM>, --threads=<NUM> Threads number\n"
" -C<PATH>, --target=<PATH> Configuration file to modify\n\n"
"Report bugs to " PACKAGE_BUGREPORT "\n");
}

static void
process_parameters (int argc, char **argv)
{
ret_t ret;
int c;
cherokee_boolean_t error = false;

struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{"debug", no_argument, NULL, 'x'},
{"unsecure", no_argument, NULL, 'u'},
{"internal-tcp", no_argument, NULL, 't'},
{"disable-iocache", no_argument, NULL, 'i'},
{"bind", optional_argument, NULL, 'b'},
{"appdir", required_argument, NULL, 'd'},
{"port", required_argument, NULL, 'p'},
{"target", required_argument, NULL, 'C'},
{"threads", required_argument, NULL, 'T'},
{NULL, 0, NULL, 0}
};

while ((c = getopt_long(argc, argv, "hVxutib::d:p:C:T:", long_options, NULL)) != -1) {
switch(c) {
case 'b':
free (bind_to);
if (optarg) {
bind_to = strdup(optarg);
} else if (argv[optind] && argv[optind][0] != '-') {
bind_to = strdup(argv[optind]);
optind++;
} else {
bind_to = NULL;
}
break;
case 'p':
ret = cherokee_atoi (optarg, &port);
if (ret != ret_ok) {
error = true;
}
break;
case 'T':
ret = cherokee_atoi (optarg, &thread_num);
if (ret != ret_ok) {
error = true;
}
break;
case 'd':
free (document_root);
document_root = strdup(optarg);
break;
case 'C':
free (config_file);
config_file = strdup(optarg);
break;
case 'x':
debug = 1;
iocache = 0;
break;
case 'u':
unsecure = 1;
break;
case 'i':
iocache = 0;
break;
case 't':
scgi_port = 0;
break;
case 'V':
printf (APP_NAME " " PACKAGE_VERSION "\n" APP_COPY_NOTICE);
exit(0);
case 'h':
case '?':
default:
error = true;
}

if (error) {
print_help();
exit (1);
}
}

/* Check for trailing parameters
*/
for (c = optind; c < argc; c++) {
if ((argv[c] != NULL) && (strlen(argv[c]) > 0)) {
print_help();
exit (1);
}
}
}

static ret_t
check_for_python (void)
{
int re;
pid_t pid;
int exitcode = -1;
char const *args[] = {"env", "python", "-c", "raise SystemExit", NULL};

pid = fork();
if (pid == -1) {
return ret_error;

} else if (pid == 0) {
execv ("/usr/bin/env", (char * const *) args);

} else {
do {
re = waitpid (pid, &exitcode, 0);
} while (re == -1 && errno == EINTR);

return (WEXITSTATUS(exitcode) == 0)? ret_ok : ret_error;
}

SHOULDNT_HAPPEN;
return ret_error;
}

static void
signals_handler (int sig, siginfo_t *si, void *context)
{
ret_t ret;
int retcode;

UNUSED(context);

switch (sig) {
case SIGCHLD:
ret = cherokee_wait_pid (si->si_pid, &retcode);
break;

case SIGINT:
case SIGTERM:
if (srv->wanna_exit) {
break;
}

printf ("Cherokee-admin is exiting..\n");
cherokee_server_handle_TERM (srv);
break;

default:
PRINT_ERROR ("Unknown signal: %d\n", sig);
}
}

int
main (int argc, char **argv)
{
ret_t ret;
struct sigaction act;

/* Globals */
document_root = strdup (DEFAULT_DOCUMENTROOT);
config_file = strdup (DEFAULT_CONFIG_FILE);
bind_to = strdup (DEFAULT_BIND);

if ((!bind_to) || (!config_file) || (!document_root)) {
PRINT_MSG ("ERROR: Couldn't allocate memory.\n");
exit (EXIT_ERROR);
}

/* Python */
ret = check_for_python();
if (ret != ret_ok) {
PRINT_MSG ("ERROR: Couldn't find python.\n");
exit (EXIT_ERROR);
}

/* Signal handling */
act.sa_handler = SIG_IGN;
sigaction (SIGPIPE, &act, NULL);

memset(&act, 0, sizeof(act));
act.sa_sigaction = signals_handler;
act.sa_flags = SA_SIGINFO;
sigaction (SIGCHLD, &act, NULL);
sigaction (SIGINT, &act, NULL);
sigaction (SIGTERM, &act, NULL);

/* Initialize the embedded server */
cherokee_init();

/* Seed random numbers
*/
cherokee_random_seed();

cherokee_spawner_set_active (false);
process_parameters (argc, argv);

ret = cherokee_server_new (&srv);
if (ret != ret_ok)
exit (EXIT_ERROR);

ret = config_server (srv);
if (ret != ret_ok)
exit (EXIT_ERROR);

ret = cherokee_server_initialize (srv);
if (ret != ret_ok)
exit (EXIT_ERROR);

print_connection_info();

ret = cherokee_server_unlock_threads (srv);
if (ret != ret_ok)
exit (EXIT_ERROR);

do {
ret = cherokee_server_step (srv);
} while (ret == ret_eagain);

cherokee_server_stop (srv);
cherokee_server_free (srv);

cherokee_mrproper();
return EXIT_OK;
}
Something went wrong with that request. Please try again.