| 1 |
/* |
| 2 |
* Copyright (C) 2019 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <argp.h> |
| 9 |
#include <err.h> |
| 10 |
#include <fcntl.h> |
| 11 |
#include <stddef.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <unistd.h> |
| 15 |
|
| 16 |
#include <common/cockpitconf.h> |
| 17 |
#include <common/cockpitwebcertificate.h> |
| 18 |
#include "utils.h" |
| 19 |
#include "server.h" |
| 20 |
#include "connection.h" |
| 21 |
|
| 22 |
/* CLI arguments */ |
| 23 |
struct arguments { |
| 24 |
uint16_t port; |
| 25 |
bool no_tls; |
| 26 |
int idle_timeout; |
| 27 |
}; |
| 28 |
|
| 29 |
#define OPT_NO_TLS 1000 |
| 30 |
#define OPT_IDLE_TIMEOUT 1001 |
| 31 |
|
| 32 |
static int |
| 33 |
arg_parse_int (char *arg, struct argp_state *state, int min, int max, const char *error_msg) |
| 34 |
{ |
| 35 |
char *endptr = NULL; |
| 36 |
long num = strtol (arg, &endptr, 10); |
| 37 |
|
| 38 |
if (!*arg || *endptr != '\0' || num < min || num > max) |
| 39 |
argp_error (state, "%s: %s", error_msg, arg); |
| 40 |
return (int) num; |
| 41 |
} |
| 42 |
|
| 43 |
/* Parse a single option. */ |
| 44 |
static error_t |
| 45 |
parse_opt (int key, char *arg, struct argp_state *state) |
| 46 |
{ |
| 47 |
struct arguments *arguments = state->input; |
| 48 |
|
| 49 |
switch (key) |
| 50 |
{ |
| 51 |
case OPT_NO_TLS: |
| 52 |
arguments->no_tls = true; |
| 53 |
break; |
| 54 |
case 'p': |
| 55 |
arguments->port = arg_parse_int (arg, state, 1, UINT16_MAX, "Invalid port"); |
| 56 |
break; |
| 57 |
case OPT_IDLE_TIMEOUT: |
| 58 |
arguments->idle_timeout = arg_parse_int (arg, state, 0, INT_MAX, "Invalid idle timeout"); |
| 59 |
break; |
| 60 |
default: |
| 61 |
return ARGP_ERR_UNKNOWN; |
| 62 |
} |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static struct argp_option options[] = { |
| 67 |
{"no-tls", OPT_NO_TLS, 0, 0, "Don't use TLS" }, |
| 68 |
{"port", 'p', "PORT", 0, "Local port to bind to (9090 if unset)" }, |
| 69 |
{"idle-timeout", OPT_IDLE_TIMEOUT, "SECONDS", 0, "Time after which to exit if there are no connections; 0 to run forever (default: 90)" }, |
| 70 |
{ 0 } |
| 71 |
}; |
| 72 |
|
| 73 |
static const struct argp argp = { |
| 74 |
.options = options, |
| 75 |
.parser = parse_opt, |
| 76 |
.doc = "cockpit-tls -- TLS terminating proxy for cockpit-ws", |
| 77 |
}; |
| 78 |
|
| 79 |
int |
| 80 |
main (int argc, char **argv) |
| 81 |
{ |
| 82 |
struct arguments arguments; |
| 83 |
gnutls_certificate_request_t client_cert_mode = GNUTLS_CERT_IGNORE; |
| 84 |
const char *runtimedir; |
| 85 |
|
| 86 |
/* default option values */ |
| 87 |
arguments.no_tls = false; |
| 88 |
arguments.port = 9090; |
| 89 |
arguments.idle_timeout = 90; |
| 90 |
|
| 91 |
argp_parse (&argp, argc, argv, 0, 0, &arguments); |
| 92 |
|
| 93 |
runtimedir = secure_getenv ("RUNTIME_DIRECTORY"); |
| 94 |
if (!runtimedir) |
| 95 |
errx (EXIT_FAILURE, "$RUNTIME_DIRECTORY environment variable must be set to a private directory"); |
| 96 |
|
| 97 |
unsigned int max_connections = cockpit_conf_uint ("WebService", "MaxConcurrentConnections", 500, 10, UINT_MAX); |
| 98 |
|
| 99 |
server_init ("/run/cockpit/wsinstance", runtimedir, arguments.idle_timeout, arguments.port, max_connections); |
| 100 |
|
| 101 |
if (!arguments.no_tls) |
| 102 |
{ |
| 103 |
static const char cert_dir[] = "/run/cockpit/tls/server"; |
| 104 |
|
| 105 |
if (cockpit_conf_bool ("WebService", "ClientCertAuthentication", false)) |
| 106 |
client_cert_mode = GNUTLS_CERT_REQUEST; |
| 107 |
|
| 108 |
bool allow_unencrypted = cockpit_conf_bool ("WebService", "AllowUnencrypted", false); |
| 109 |
|
| 110 |
int cert_dirfd = open (cert_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 111 |
if (cert_dirfd == -1) |
| 112 |
err (EXIT_FAILURE, "open: %s", cert_dir); |
| 113 |
|
| 114 |
connection_crypto_init (cert_dirfd, allow_unencrypted, client_cert_mode); |
| 115 |
close (cert_dirfd); |
| 116 |
} |
| 117 |
|
| 118 |
server_run (); |
| 119 |
server_cleanup (); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |