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