]> git.proxmox.com Git - pve-manager.git/blob - PVE/Service/pveproxy.pm
fix #3789: pass disable TLS 1.2/1.3 options
[pve-manager.git] / PVE / Service / pveproxy.pm
1 package PVE::Service::pveproxy;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Daemon;
8 use HTTP::Response;
9 use Encode;
10 use URI;
11 use URI::QueryParam;
12 use Data::Dumper;
13 use PVE::Cluster;
14 use PVE::DataCenterConfig;
15 use PVE::APIServer::Utils;
16 use PVE::API2;
17 use PVE::APIServer::Formatter;
18 use PVE::APIServer::Formatter::Standard;
19 use PVE::APIServer::Formatter::HTML;
20 use PVE::APIServer::AnyEvent;
21 use PVE::HTTPServer;
22 use PVE::pvecfg;
23
24 use Template;
25
26 use PVE::Tools;
27
28 use base qw(PVE::Daemon);
29
30 my $cmdline = [$0, @ARGV];
31
32 my %daemon_options = (
33 max_workers => 3,
34 restart_on_error => 5,
35 stop_wait_time => 15,
36 leave_children_open_on_reload => 1,
37 setuid => 'www-data',
38 setgid => 'www-data',
39 pidfile => '/var/run/pveproxy/pveproxy.pid',
40 );
41
42 my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options);
43
44 sub add_dirs {
45 my ($result_hash, $alias, $subdir) = @_;
46
47 PVE::APIServer::AnyEvent::add_dirs($result_hash, $alias, $subdir);
48 }
49
50 my $basedirs = {
51 novnc => '/usr/share/novnc-pve',
52 extjs => '/usr/share/javascript/extjs',
53 manager => '/usr/share/pve-manager',
54 i18n => '/usr/share/pve-i18n',
55 docs => '/usr/share/pve-docs',
56 fontawesome => '/usr/share/fonts-font-awesome',
57 xtermjs => '/usr/share/pve-xtermjs',
58 widgettoolkit => '/usr/share/javascript/proxmox-widget-toolkit',
59 };
60
61 sub init {
62 my ($self) = @_;
63
64 # we use same ALLOW/DENY/POLICY as pveproxy
65 my $proxyconf = PVE::APIServer::Utils::read_proxy_config($self->{name});
66
67 my $accept_lock_fn = "/var/lock/pveproxy.lck";
68
69 my $lockfh = IO::File->new(">>${accept_lock_fn}") ||
70 die "unable to open lock file '${accept_lock_fn}' - $!\n";
71
72 my $listen_ip = $proxyconf->{LISTEN_IP};
73 my $socket = $self->create_reusable_socket(8006, $listen_ip);
74
75 my $dirs = {};
76
77 add_dirs($dirs, '/pve2/locale/', "$basedirs->{i18n}/");
78 add_dirs($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
79 add_dirs($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
80 add_dirs($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
81 add_dirs($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
82 add_dirs($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
83 add_dirs($dirs, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/");
84 add_dirs($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/");
85 add_dirs($dirs, '/pve-docs/' => "$basedirs->{docs}/");
86 add_dirs($dirs, '/pve-docs/api-viewer/extjs/' => "$basedirs->{extjs}/");
87 add_dirs($dirs, '/novnc/' => "$basedirs->{novnc}/");
88 add_dirs($dirs, '/xtermjs/' => "$basedirs->{xtermjs}/");
89 add_dirs($dirs, '/pwt/images/' => "$basedirs->{widgettoolkit}/images/");
90 add_dirs($dirs, '/pwt/css/' => "$basedirs->{widgettoolkit}/css/");
91
92 $self->{server_config} = {
93 title => 'Proxmox VE API',
94 keep_alive => 100,
95 max_conn => 500,
96 max_requests => 1000,
97 lockfile => $accept_lock_fn,
98 socket => $socket,
99 lockfh => $lockfh,
100 debug => $self->{debug},
101 trusted_env => 0, # not trusted, anyone can connect
102 logfile => '/var/log/pveproxy/access.log',
103 allow_from => $proxyconf->{ALLOW_FROM},
104 deny_from => $proxyconf->{DENY_FROM},
105 policy => $proxyconf->{POLICY},
106 ssl => {
107 cipher_list => $proxyconf->{CIPHERS},
108 ciphersuites => $proxyconf->{CIPHERSUITES},
109 key_file => '/etc/pve/local/pve-ssl.key',
110 cert_file => '/etc/pve/local/pve-ssl.pem',
111 honor_cipher_order => $proxyconf->{HONOR_CIPHER_ORDER},
112 },
113 compression => $proxyconf->{COMPRESSION},
114 # Note: there is no authentication for those pages and dirs!
115 pages => {
116 '/' => sub { get_index($self->{nodename}, @_) },
117 # avoid authentication when accessing favicon
118 '/favicon.ico' => {
119 file => "$basedirs->{manager}/images/favicon.ico",
120 },
121 '/proxmoxlib.js' => {
122 file => "$basedirs->{widgettoolkit}/proxmoxlib.js",
123 },
124 '/qrcode.min.js' => {
125 file => '/usr/share/javascript/qrcodejs/qrcode.min.js',
126 },
127 },
128 dirs => $dirs,
129 };
130
131 if (defined($proxyconf->{DHPARAMS})) {
132 $self->{server_config}->{ssl}->{dh_file} = $proxyconf->{DHPARAMS};
133 }
134 if (defined($proxyconf->{DISABLE_TLS_1_2})) {
135 $self->{server_config}->{ssl}->{tlsv1_2} = !$proxyconf->{DISABLE_TLS_1_2};
136 }
137 if (defined($proxyconf->{DISABLE_TLS_1_3})) {
138 $self->{server_config}->{ssl}->{tlsv1_3} = !$proxyconf->{DISABLE_TLS_1_3};
139 }
140 my $custom_key_path = '/etc/pve/local/pveproxy-ssl.key';
141 if (defined($proxyconf->{TLS_KEY_FILE})) {
142 $custom_key_path = $proxyconf->{TLS_KEY_FILE};
143 }
144 if (-f '/etc/pve/local/pveproxy-ssl.pem' && -f $custom_key_path) {
145 $self->{server_config}->{ssl}->{cert_file} = '/etc/pve/local/pveproxy-ssl.pem';
146 $self->{server_config}->{ssl}->{key_file} = $custom_key_path;
147 syslog('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
148 }
149 }
150
151 sub run {
152 my ($self) = @_;
153
154 my $server = PVE::HTTPServer->new(%{$self->{server_config}});
155 $server->run();
156 }
157
158 $daemon->register_start_command();
159 $daemon->register_restart_command(1);
160 $daemon->register_stop_command();
161 $daemon->register_status_command();
162
163 our $cmddef = {
164 start => [ __PACKAGE__, 'start', []],
165 restart => [ __PACKAGE__, 'restart', []],
166 stop => [ __PACKAGE__, 'stop', []],
167 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
168 };
169
170 sub is_phone {
171 my ($ua) = @_;
172
173 return 0 if !$ua;
174
175 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
176
177 if ($ua =~ m/Mobile(\/|\s)/) {
178 return 1 if $ua =~ m/(BlackBerry|BB)/;
179 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
180 }
181
182 return 0;
183 }
184
185 # NOTE: Requests to those pages are not authenticated
186 # so we must be very careful here
187
188 sub get_index {
189 my ($nodename, $server, $r, $args) = @_;
190
191 my $lang;
192 my $username;
193 my $token = 'null';
194
195 if (my $cookie = $r->header('Cookie')) {
196 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
197 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
198 $lang = $newlang;
199 }
200 }
201 my $ticket = PVE::APIServer::Formatter::extract_auth_value($cookie, $server->{cookie_name});
202 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
203 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
204 }
205 }
206
207 if (!$lang) {
208 my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
209 $lang = $dc_conf->{language} // 'en';
210 }
211
212 $username = '' if !$username;
213
214 my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0;
215
216 if (defined($args->{mobile})) {
217 $mobile = $args->{mobile} ? 1 : 0;
218 }
219
220 my $novnc = defined($args->{console}) && $args->{novnc};
221 my $xtermjs = defined($args->{console}) && $args->{xtermjs};
222
223 my $page = '';
224 my $template = Template->new({ABSOLUTE => 1});
225
226 my $langfile = 0;
227
228 if (-f "$basedirs->{i18n}/pve-lang-$lang.js") {
229 $langfile = 1;
230 }
231
232 my $version = PVE::pvecfg::version();
233
234 my $wtversionraw = PVE::Tools::file_read_firstline("$basedirs->{widgettoolkit}/proxmoxlib.js");
235 my $wtversion;
236 if ($wtversionraw =~ m|^// (.*)$|) {
237 $wtversion = $1;
238 };
239
240 my $debug = $server->{debug};
241 if (exists $args->{debug}) {
242 $debug = !defined($args->{debug}) || $args->{debug};
243 }
244
245 my $vars = {
246 lang => $lang,
247 langfile => $langfile,
248 username => $username,
249 token => $token,
250 console => $args->{console},
251 nodename => $nodename,
252 debug => $debug,
253 version => "$version",
254 wtversion => $wtversion,
255 };
256
257 # by default, load the normal index
258 my $dir = $basedirs->{manager};
259
260 if ($novnc) {
261 $dir = $basedirs->{novnc};
262 } elsif ($xtermjs) {
263 $dir = $basedirs->{xtermjs};
264 } elsif ($mobile) {
265 $dir = "$basedirs->{manager}/touch";
266 }
267
268 $template->process("$dir/index.html.tpl", $vars, \$page)
269 || die $template->error(), "\n";
270 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
271 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
272
273 return $resp;
274 }
275
276 1;