]> git.proxmox.com Git - pve-manager.git/blob - bin/pveproxy
pveproxy: set method to tlsv1 - required for IE
[pve-manager.git] / bin / pveproxy
1 #!/usr/bin/perl -T
2
3 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
4
5 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
6
7 use strict;
8 use warnings;
9
10 use PVE::SafeSyslog;
11 use PVE::Daemon;
12 use HTTP::Response;
13 use Encode;
14 use URI;
15 use URI::QueryParam;
16 use File::Find;
17 use Data::Dumper;
18 use PVE::API2Tools;
19 use PVE::API2;
20 use PVE::API2::Formatter::Standard;
21 use PVE::API2::Formatter::HTML;
22 use PVE::HTTPServer;
23
24 use PVE::ExtJSIndex;
25 use PVE::NoVncIndex;
26 use PVE::TouchIndex;
27
28 use base qw(PVE::Daemon);
29
30 $SIG{'__WARN__'} = sub {
31 my $err = $@;
32 my $t = $_[0];
33 chomp $t;
34 print STDERR "$t\n";
35 syslog('warning', "%s", $t);
36 $@ = $err;
37 };
38
39 my $cmdline = [$0, @ARGV];
40
41 my %daemon_options = (
42 max_workers => 3,
43 restart_on_error => 5,
44 stop_wait_time => 15,
45 leave_children_open_on_reload => 1,
46 setuid => 'www-data',
47 setgid => 'www-data',
48 pidfile => '/var/run/pveproxy/pveproxy.pid',
49 );
50
51 my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options);
52
53 sub add_dirs {
54 my ($result_hash, $alias, $subdir) = @_;
55
56 $result_hash->{$alias} = $subdir;
57
58 my $wanted = sub {
59 my $dir = $File::Find::dir;
60 if ($dir =~m!^$subdir(.*)$!) {
61 my $name = "$alias$1/";
62 $result_hash->{$name} = "$dir/";
63 }
64 };
65
66 find({wanted => $wanted, follow => 0, no_chdir => 1}, $subdir);
67 }
68
69 sub init {
70 my ($self) = @_;
71
72 # we use same ALLOW/DENY/POLICY as pveproxy
73 my $proxyconf = PVE::API2Tools::read_proxy_config();
74
75 my $accept_lock_fn = "/var/lock/pveproxy.lck";
76
77 my $lockfh = IO::File->new(">>${accept_lock_fn}") ||
78 die "unable to open lock file '${accept_lock_fn}' - $!\n";
79
80 my $socket = $self->create_reusable_socket(8006);
81
82 my $dirs = {};
83
84 add_dirs($dirs, '/pve2/locale/', '/usr/share/pve-manager/locale/');
85 add_dirs($dirs, '/pve2/touch/', '/usr/share/pve-manager/touch/');
86 add_dirs($dirs, '/pve2/ext4/', '/usr/share/pve-manager/ext4/');
87 add_dirs($dirs, '/pve2/images/' => '/usr/share/pve-manager/images/');
88 add_dirs($dirs, '/pve2/css/' => '/usr/share/pve-manager/css/');
89 add_dirs($dirs, '/pve2/js/' => '/usr/share/pve-manager/js/');
90 add_dirs($dirs, '/vncterm/' => '/usr/share/vncterm/');
91 add_dirs($dirs, '/novnc/' => '/usr/share/novnc-pve/');
92
93 $self->{server_config} = {
94 base_handler_class => 'PVE::API2',
95 keep_alive => 100,
96 max_conn => 500,
97 max_requests => 1000,
98 lockfile => $accept_lock_fn,
99 socket => $socket,
100 lockfh => $lockfh,
101 debug => $self->{debug},
102 trusted_env => 0, # not trusted, anyone can connect
103 logfile => '/var/log/pveproxy/access.log',
104 allow_from => $proxyconf->{ALLOW_FROM},
105 deny_from => $proxyconf->{DENY_FROM},
106 policy => $proxyconf->{POLICY},
107 ssl => {
108 # Note: older versions are considered insecure, for example
109 # search for "Poodle"-Attac
110 method => 'tlsv1',
111 sslv2 => 0,
112 sslv3 => 0,
113 cipher_list => $proxyconf->{CIPHERS} || 'HIGH:MEDIUM:!aNULL:!MD5',
114 key_file => '/etc/pve/local/pve-ssl.key',
115 cert_file => '/etc/pve/local/pve-ssl.pem',
116 },
117 # Note: there is no authentication for those pages and dirs!
118 pages => {
119 '/' => \&get_index,
120 # avoid authentication when accessing favicon
121 '/favicon.ico' => {
122 file => '/usr/share/pve-manager/images/favicon.ico',
123 },
124 },
125 dirs => $dirs,
126 };
127 }
128
129 sub run {
130 my ($self) = @_;
131
132 my $server = PVE::HTTPServer->new(%{$self->{server_config}});
133 $server->run();
134 }
135
136 $daemon->register_start_command();
137 $daemon->register_restart_command(1);
138 $daemon->register_stop_command();
139 $daemon->register_status_command();
140
141 my $cmddef = {
142 start => [ __PACKAGE__, 'start', []],
143 restart => [ __PACKAGE__, 'restart', []],
144 stop => [ __PACKAGE__, 'stop', []],
145 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
146 };
147
148 my $cmd = shift;
149
150 PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
151
152 exit (0);
153
154 sub is_phone {
155 my ($ua) = @_;
156
157 return 0 if !$ua;
158
159 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
160
161 if ($ua =~ m/Mobile(\/|\s)/) {
162 return 1 if $ua =~ m/(BlackBerry|BB)/;
163 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
164 }
165
166 return 0;
167 }
168
169 # NOTE: Requests to those pages are not authenticated
170 # so we must be very careful here
171
172 sub get_index {
173 my ($server, $r, $args) = @_;
174
175 my $lang = 'en';
176 my $username;
177 my $token = 'null';
178
179 if (my $cookie = $r->header('Cookie')) {
180 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
181 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
182 $lang = $newlang;
183 }
184 }
185 my $ticket = PVE::REST::extract_auth_cookie($cookie);
186 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
187 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
188 }
189 }
190
191 $username = '' if !$username;
192
193 my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0;
194
195 if (defined($args->{mobile})) {
196 $mobile = $args->{mobile} ? 1 : 0;
197 }
198
199 my $page;
200
201 if (defined($args->{console}) && $args->{novnc}) {
202 $page = PVE::NoVncIndex::get_index($lang, $username, $token, $args->{console});
203 } else {
204 if ($mobile) {
205 $page = PVE::TouchIndex::get_index($lang, $username, $token, $args->{console});
206 } else {
207 $page = PVE::ExtJSIndex::get_index($lang, $username, $token, $args->{console});
208 }
209 }
210
211 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
212 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
213
214 return $resp;
215 }
216
217 __END__
218
219 =head1 NAME
220
221 pveproxy - the PVE API proxy server
222
223 =head1 SYNOPSIS
224
225 =include synopsis
226
227 =head1 DESCRIPTION
228
229 This is the REST API proxy server, listening on port 8006. This is usually
230 started as service using:
231
232 # service pveproxy start
233
234 =head1 Host based access control
235
236 It is possible to configure apache2 like access control lists. Values are read
237 from file /etc/default/pveproxy. For example:
238
239 ALLOW_FROM="10.0.0.1-10.0.0.5,192.168.0.0/22"
240 DENY_FROM="all"
241 POLICY="allow"
242
243 IP addresses can be specified using any syntax understoop by Net::IP. The
244 name 'all' is an alias for '0/0'.
245
246 The default policy is 'allow'.
247
248 Match | POLICY=deny | POLICY=allow
249 ---------------------------|-------------|------------
250 Match Allow only | allow | allow
251 Match Deny only | deny | deny
252 No match | deny | allow
253 Match Both Allow & Deny | deny | allow
254
255 =head1 SSL Cipher Suite
256
257 You can define the chiper list in /etc/default/pveproxy, for example
258
259 CIPHERS="HIGH:MEDIUM:!aNULL:!MD5"
260
261 Above is the default. See the ciphers(1) man page from the openssl
262 package for list of all available options.
263
264 =head1 FILES
265
266 /etc/default/pveproxy
267
268 =include pve_copyright