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