]> git.proxmox.com Git - pve-manager.git/blob - bin/pveproxy
pveproxy: add routing to novnc dirs
[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 use English;
10 use Getopt::Long;
11 use POSIX ":sys_wait_h";
12 use Socket;
13 use IO::Socket::INET;
14 use PVE::SafeSyslog;
15 use PVE::APIDaemon;
16 use HTTP::Response;
17 use Encode;
18 use URI;
19 use URI::QueryParam;
20 use File::Find;
21 use Data::Dumper;
22 use PVE::API2;
23 use PVE::API2::Formatter::Standard;
24 use PVE::API2::Formatter::HTML;
25
26 my $pidfile = "/var/run/pveproxy/pveproxy.pid";
27 my $lockfile = "/var/lock/pveproxy.lck";
28
29 my $opt_debug;
30
31 initlog ('pveproxy');
32
33 if (!GetOptions ('debug' => \$opt_debug)) {
34 die "usage: $0 [--debug]\n";
35 }
36
37 $SIG{'__WARN__'} = sub {
38 my $err = $@;
39 my $t = $_[0];
40 chomp $t;
41 syslog('warning', "WARNING: %s", $t);
42 $@ = $err;
43 };
44
45 $0 = "pveproxy";
46
47 # run as www-data
48 my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
49 POSIX::setgid($gid) || die "setgid $gid failed - $!\n";
50 $EGID = "$gid $gid"; # this calls setgroups
51 my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
52 POSIX::setuid($uid) || die "setuid $uid failed - $!\n";
53
54 # just to be sure
55 die "detected strange uid/gid\n" if !($UID == $uid && $EUID == $uid && $GID eq "$gid $gid" && $EGID eq "$gid $gid");
56
57 my $proxyconf = PVE::APIDaemon::read_proxy_config();
58
59 sub add_dirs {
60 my ($result_hash, $alias, $subdir) = @_;
61
62 $result_hash->{$alias} = $subdir;
63
64 my $wanted = sub {
65 my $dir = $File::Find::dir;
66 if ($dir =~m!^$subdir(.*)$!) {
67 my $name = "$alias$1/";
68 $result_hash->{$name} = "$dir/";
69 }
70 };
71
72 find({wanted => $wanted, follow => 0, no_chdir => 1}, $subdir);
73 }
74
75 my $cpid;
76 my $daemon;
77 eval {
78
79 my $dirs = {};
80
81 add_dirs($dirs, '/pve2/ext4/', '/usr/share/pve-manager/ext4/');
82 add_dirs($dirs, '/pve2/images/' => '/usr/share/pve-manager/images/');
83 add_dirs($dirs, '/pve2/css/' => '/usr/share/pve-manager/css/');
84 add_dirs($dirs, '/pve2/js/' => '/usr/share/pve-manager/js/');
85 add_dirs($dirs, '/vncterm/' => '/usr/share/vncterm/');
86 add_dirs($dirs, '/novnc/' => '/usr/share/novnc-pve/');
87
88 $daemon = PVE::APIDaemon->new(
89 base_handler_class => 'PVE::API2',
90 port => 8006,
91 keep_alive => 100,
92 max_conn => 500,
93 max_requests => 1000,
94 debug => $opt_debug,
95 allow_from => $proxyconf->{ALLOW_FROM},
96 deny_from => $proxyconf->{DENY_FROM},
97 policy => $proxyconf->{POLICY},
98 trusted_env => 0, # not trusted, anyone can connect
99 logfile => '/var/log/pveproxy/access.log',
100 lockfile => $lockfile,
101 ssl => {
102 cipher_list => $proxyconf->{CIPHERS} || 'HIGH:MEDIUM:!aNULL:!MD5',
103 key_file => '/etc/pve/local/pve-ssl.key',
104 cert_file => '/etc/pve/local/pve-ssl.pem',
105 },
106 # Note: there is no authentication for those pages and dirs!
107 pages => {
108 '/' => \&get_index,
109 # avoid authentication when accessing favicon
110 '/favicon.ico' => {
111 file => '/usr/share/pve-manager/images/favicon.ico',
112 },
113 },
114 dirs => $dirs,
115 );
116 };
117
118 my $err = $@;
119
120 if ($err) {
121 syslog ('err' , "unable to start server: $err");
122 print STDERR $err;
123 exit (-1);
124 }
125
126
127 if ($opt_debug || !($cpid = fork ())) {
128
129 $SIG{PIPE} = 'IGNORE';
130 $SIG{INT} = 'IGNORE' if !$opt_debug;
131
132 $SIG{TERM} = $SIG{QUIT} = sub {
133 syslog ('info' , "server closing");
134
135 $SIG{INT} = 'DEFAULT';
136
137 unlink "$pidfile" if !$opt_debug;
138
139 exit (0);
140 };
141
142 syslog ('info' , "starting server");
143
144 if (!$opt_debug) {
145 # redirect STDIN/STDOUT/SDTERR to /dev/null
146 open STDIN, '</dev/null' || die "can't read /dev/null [$!]";
147 open STDOUT, '>/dev/null' || die "can't write /dev/null [$!]";
148 open STDERR, '>&STDOUT' || die "can't open STDERR to STDOUT [$!]";
149 }
150
151 POSIX::setsid();
152
153 eval {
154 $daemon->start_server();
155 };
156 my $err = $@;
157
158 if ($err) {
159 syslog ('err' , "unexpected server error: $err");
160 print STDERR $err if $opt_debug;
161 exit (-1);
162 }
163
164 } else {
165
166 open (PIDFILE, ">$pidfile") ||
167 die "cant write '$pidfile' - $! :ERROR";
168 print PIDFILE "$cpid\n";
169 close (PIDFILE) ||
170 die "cant write '$pidfile' - $! :ERROR";
171 }
172
173 exit (0);
174
175 # NOTE: Requests to those pages are not authenticated
176 # so we must be very careful here
177
178 sub get_index {
179 my ($server, $r, $args) = @_;
180
181 my $lang = 'en';
182 my $username;
183 my $token = 'null';
184
185 if (my $cookie = $r->header('Cookie')) {
186 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
187 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
188 $lang = $newlang;
189 }
190 }
191 my $ticket = PVE::REST::extract_auth_cookie($cookie);
192 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
193 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
194 }
195 }
196
197 my $workspace = defined($args->{console}) ?
198 "PVE.ConsoleWorkspace" : "PVE.StdWorkspace";
199
200 $username = '' if !$username;
201
202 my $jssrc = <<_EOJS;
203 if (!PVE) PVE = {};
204 PVE.UserName = '$username';
205 PVE.CSRFPreventionToken = '$token';
206 _EOJS
207
208 my $langfile = "/usr/share/pve-manager/ext4/locale/ext-lang-${lang}.js";
209 $jssrc .= PVE::Tools::file_get_contents($langfile) if -f $langfile;
210
211 my $i18nsrc;
212 $langfile = "/usr/share/pve-manager/root/pve-lang-${lang}.js";
213 if (-f $langfile) {
214 $i18nsrc = PVE::Tools::file_get_contents($langfile);
215 } else {
216 $i18nsrc = 'function gettext(buf) { return buf; }';
217 }
218
219 $jssrc .= <<_EOJS;
220
221 // we need this (the java applet ignores the zindex)
222 Ext.useShims = true;
223
224 Ext.History.fieldid = 'x-history-field';
225
226 Ext.onReady(function() { Ext.create('$workspace');});
227
228 _EOJS
229
230 my $page = <<_EOD;
231 <html>
232 <head>
233 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
234 <meta http-equiv="X-UA-Compatible" content="IE=10">
235 <title>Proxmox Virtual Environment</title>
236
237 <link rel="stylesheet" type="text/css" href="/pve2/ext4/resources/css/ext-all.css" />
238 <link rel="stylesheet" type="text/css" href="/pve2/css/ext-pve.css" />
239
240 <script type="text/javascript">$i18nsrc</script>
241 <script type="text/javascript" src="/pve2/ext4/ext-all-debug.js"></script>
242 <script type="text/javascript" src="/pve2/ext4/pvemanagerlib.js"></script>
243 <script type="text/javascript">$jssrc</script>
244
245 </head>
246 <body>
247 <!-- Fields required for history management -->
248 <form id="history-form" class="x-hidden">
249 <input type="hidden" id="x-history-field"/>
250 </form>
251 </body>
252 </html>
253 _EOD
254
255 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
256 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
257
258 return $resp;
259 }
260
261 __END__
262
263 =head1 NAME
264
265 pveproxy - the PVE API proxy server
266
267 =head1 SYNOPSIS
268
269 pveproxy [--debug]
270
271 =head1 DESCRIPTION
272
273 This is the REST API proxy server, listening on port 8006. This is usually started
274 as service using:
275
276 # service pveproxy start
277
278 =head1 Host based access control
279
280 It is possible to configure apache2 like access control lists. Values are read
281 from file /etc/default/pveproxy. For example:
282
283 ALLOW_FROM="10.0.0.1-10.0.0.5,192.168.0.0/22"
284 DENY_FROM="all"
285 POLICY="allow"
286
287 IP addresses can be specified using any syntax understoop by Net::IP. The
288 name 'all' is an alias for '0/0'.
289
290 The default policy is 'allow'.
291
292 Match | POLICY=deny | POLICY=allow
293 ---------------------------|-------------|------------
294 Match Allow only | allow | allow
295 Match Deny only | deny | deny
296 No match | deny | allow
297 Match Both Allow & Deny | deny | allow
298
299 =head1 SSL Cipher Suite
300
301 You can define the chiper list in /etc/default/pveproxy, for example
302
303 CIPHERS="HIGH:MEDIUM:!aNULL:!MD5"
304
305 Above is the default. See the ciphers(1) man page from the openssl
306 package for list of all available options.
307
308 =head1 FILES
309
310 /etc/default/pveproxy
311
312 =head1 COPYRIGHT AND DISCLAIMER
313
314 Copyright (C) 2007-2013 Proxmox Server Solutions GmbH
315
316 This program is free software: you can redistribute it and/or modify it
317 under the terms of the GNU Affero General Public License as published
318 by the Free Software Foundation, either version 3 of the License, or
319 (at your option) any later version.
320
321 This program is distributed in the hope that it will be useful, but
322 WITHOUT ANY WARRANTY; without even the implied warranty of
323 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
324 Affero General Public License for more details.
325
326 You should have received a copy of the GNU Affero General Public
327 License along with this program. If not, see
328 <http://www.gnu.org/licenses/>.
329