2013-09-30 13:50:42 +04:00
#!/usr/bin/perl -T
2013-04-05 18:44:21 +04:00
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
use strict;
2013-09-30 13:50:42 +04:00
use warnings;
2013-04-10 10:08:02 +04:00
use English;
2013-04-05 18:44:21 +04:00
use Getopt::Long;
use POSIX ":sys_wait_h";
use Socket;
use IO::Socket::INET;
use PVE::SafeSyslog;
use PVE::APIDaemon;
use HTTP::Response;
use Encode;
2013-04-10 08:06:49 +04:00
use URI;
use URI::QueryParam;
2013-04-05 18:44:21 +04:00
use File::Find;
use Data::Dumper;
2014-04-28 09:26:50 +04:00
use PVE::API2;
2014-05-02 13:32:00 +04:00
use PVE::API2::Formatter::Standard;
use PVE::API2::Formatter::HTML;
2013-04-05 18:44:21 +04:00
2014-08-04 10:32:21 +04:00
use PVE::ExtJSIndex;
2014-08-05 08:38:01 +04:00
use PVE::NoVncIndex;
2014-08-04 10:32:21 +04:00
2013-04-10 10:08:02 +04:00
my $pidfile = "/var/run/pveproxy/pveproxy.pid";
2013-04-05 18:44:21 +04:00
my $lockfile = "/var/lock/pveproxy.lck";
my $opt_debug;
initlog ('pveproxy');
2013-06-27 09:55:11 +04:00
if (!GetOptions ('debug' => \$opt_debug)) {
die "usage: $0 [--debug]\n";
2013-04-05 18:44:21 +04:00
}
$SIG{'__WARN__'} = sub {
my $err = $@;
my $t = $_[0];
chomp $t;
syslog('warning', "WARNING: %s", $t);
$@ = $err;
};
$0 = "pveproxy";
2013-04-10 10:08:02 +04:00
# run as www-data
my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
POSIX::setgid($gid) || die "setgid $gid failed - $!\n";
$EGID = "$gid $gid"; # this calls setgroups
my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
POSIX::setuid($uid) || die "setuid $uid failed - $!\n";
# just to be sure
die "detected strange uid/gid\n" if !($UID == $uid && $EUID == $uid && $GID eq "$gid $gid" && $EGID eq "$gid $gid");
2013-06-27 09:55:11 +04:00
my $proxyconf = PVE::APIDaemon::read_proxy_config();
2013-04-10 09:10:50 +04:00
sub add_dirs {
my ($result_hash, $alias, $subdir) = @_;
$result_hash->{$alias} = $subdir;
my $wanted = sub {
my $dir = $File::Find::dir;
if ($dir =~m!^$subdir(.*)$!) {
my $name = "$alias$1/";
$result_hash->{$name} = "$dir/";
}
};
find({wanted => $wanted, follow => 0, no_chdir => 1}, $subdir);
}
2013-04-05 18:44:21 +04:00
my $cpid;
my $daemon;
eval {
2013-04-10 09:10:50 +04:00
my $dirs = {};
2014-08-04 10:32:21 +04:00
add_dirs($dirs, '/pve2/locale/', '/usr/share/pve-manager/locale/');
2013-04-10 09:10:50 +04:00
add_dirs($dirs, '/pve2/ext4/', '/usr/share/pve-manager/ext4/');
add_dirs($dirs, '/pve2/images/' => '/usr/share/pve-manager/images/');
add_dirs($dirs, '/pve2/css/' => '/usr/share/pve-manager/css/');
2014-05-02 09:44:53 +04:00
add_dirs($dirs, '/pve2/js/' => '/usr/share/pve-manager/js/');
2013-04-10 09:10:50 +04:00
add_dirs($dirs, '/vncterm/' => '/usr/share/vncterm/');
2014-06-13 12:34:13 +04:00
add_dirs($dirs, '/novnc/' => '/usr/share/novnc-pve/');
2013-04-10 09:10:50 +04:00
2013-04-05 18:44:21 +04:00
$daemon = PVE::APIDaemon->new(
2014-04-28 09:26:50 +04:00
base_handler_class => 'PVE::API2',
2013-04-05 18:44:21 +04:00
port => 8006,
keep_alive => 100,
max_conn => 500,
max_requests => 1000,
2013-04-11 10:17:34 +04:00
debug => $opt_debug,
2013-06-27 09:55:11 +04:00
allow_from => $proxyconf->{ALLOW_FROM},
deny_from => $proxyconf->{DENY_FROM},
policy => $proxyconf->{POLICY},
2013-04-05 18:44:21 +04:00
trusted_env => 0, # not trusted, anyone can connect
2013-04-10 10:08:02 +04:00
logfile => '/var/log/pveproxy/access.log',
2013-04-05 18:44:21 +04:00
lockfile => $lockfile,
ssl => {
2013-08-01 11:36:53 +04:00
cipher_list => $proxyconf->{CIPHERS} || 'HIGH:MEDIUM:!aNULL:!MD5',
2013-04-05 18:44:21 +04:00
key_file => '/etc/pve/local/pve-ssl.key',
cert_file => '/etc/pve/local/pve-ssl.pem',
},
# Note: there is no authentication for those pages and dirs!
pages => {
'/' => \&get_index,
# avoid authentication when accessing favicon
'/favicon.ico' => {
file => '/usr/share/pve-manager/images/favicon.ico',
},
},
2013-04-10 09:10:50 +04:00
dirs => $dirs,
2013-04-05 18:44:21 +04:00
);
};
my $err = $@;
if ($err) {
syslog ('err' , "unable to start server: $err");
print STDERR $err;
exit (-1);
}
2013-04-10 10:08:02 +04:00
2013-04-05 18:44:21 +04:00
if ($opt_debug || !($cpid = fork ())) {
$SIG{PIPE} = 'IGNORE';
$SIG{INT} = 'IGNORE' if !$opt_debug;
$SIG{TERM} = $SIG{QUIT} = sub {
syslog ('info' , "server closing");
$SIG{INT} = 'DEFAULT';
2013-04-10 13:06:08 +04:00
unlink "$pidfile" if !$opt_debug;
2013-04-05 18:44:21 +04:00
exit (0);
};
syslog ('info' , "starting server");
if (!$opt_debug) {
# redirect STDIN/STDOUT/SDTERR to /dev/null
open STDIN, '</dev/null' || die "can't read /dev/null [$!]";
open STDOUT, '>/dev/null' || die "can't write /dev/null [$!]";
open STDERR, '>&STDOUT' || die "can't open STDERR to STDOUT [$!]";
}
POSIX::setsid();
eval {
$daemon->start_server();
};
my $err = $@;
if ($err) {
syslog ('err' , "unexpected server error: $err");
print STDERR $err if $opt_debug;
exit (-1);
}
} else {
open (PIDFILE, ">$pidfile") ||
die "cant write '$pidfile' - $! :ERROR";
print PIDFILE "$cpid\n";
close (PIDFILE) ||
die "cant write '$pidfile' - $! :ERROR";
}
exit (0);
# NOTE: Requests to those pages are not authenticated
# so we must be very careful here
sub get_index {
2013-04-12 14:51:28 +04:00
my ($server, $r, $args) = @_;
2013-04-05 18:44:21 +04:00
my $lang = 'en';
2013-04-10 08:06:49 +04:00
my $username;
2013-04-05 18:44:21 +04:00
my $token = 'null';
if (my $cookie = $r->header('Cookie')) {
if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
$lang = $newlang;
}
}
my $ticket = PVE::REST::extract_auth_cookie($cookie);
if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
$token = PVE::AccessControl::assemble_csrf_prevention_token($username);
}
}
2013-04-10 08:06:49 +04:00
$username = '' if !$username;
2014-08-05 08:38:01 +04:00
my $page;
if (defined($args->{console}) && $args->{novnc}) {
$page = PVE::NoVncIndex::get_index($lang, $username, $token, $args->{console});
} else {
$page = PVE::ExtJSIndex::get_index($lang, $username, $token, $args->{console});
}
2013-04-05 18:44:21 +04:00
2013-10-29 14:08:17 +04:00
my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
my $resp = HTTP::Response->new(200, "OK", $headers, $page);
2013-04-05 18:44:21 +04:00
return $resp;
}
__END__
=head1 NAME
pveproxy - the PVE API proxy server
=head1 SYNOPSIS
2013-06-27 09:55:11 +04:00
pveproxy [--debug]
2013-04-05 18:44:21 +04:00
=head1 DESCRIPTION
2013-04-23 12:30:10 +04:00
This is the REST API proxy server, listening on port 8006. This is usually started
as service using:
# service pveproxy start
=head1 Host based access control
2013-06-27 09:55:11 +04:00
It is possible to configure apache2 like access control lists. Values are read
2013-04-23 12:30:10 +04:00
from file /etc/default/pveproxy. For example:
ALLOW_FROM="10.0.0.1-10.0.0.5,192.168.0.0/22"
DENY_FROM="all"
POLICY="allow"
IP addresses can be specified using any syntax understoop by Net::IP. The
name 'all' is an alias for '0/0'.
The default policy is 'allow'.
Match | POLICY=deny | POLICY=allow
---------------------------|-------------|------------
Match Allow only | allow | allow
Match Deny only | deny | deny
No match | deny | allow
Match Both Allow & Deny | deny | allow
2013-08-01 11:36:53 +04:00
=head1 SSL Cipher Suite
You can define the chiper list in /etc/default/pveproxy, for example
CIPHERS="HIGH:MEDIUM:!aNULL:!MD5"
Above is the default. See the ciphers(1) man page from the openssl
package for list of all available options.
2013-04-23 12:30:10 +04:00
=head1 FILES
/etc/default/pveproxy
=head1 COPYRIGHT AND DISCLAIMER
Copyright (C) 2007-2013 Proxmox Server Solutions GmbH
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
2013-04-05 18:44:21 +04:00
2013-04-23 12:30:10 +04:00
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
2013-04-05 18:44:21 +04:00