REST.pm 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package PVE::REST;

use warnings;
use strict;
use Digest::SHA1 qw(sha1_base64);
use PVE::Cluster;
use PVE::SafeSyslog;
use PVE::Tools;
use PVE::API2;
use Apache2::Const;
use CGI;
use mod_perl2;
use JSON;
use Digest::SHA;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Status qw(:constants :is status_message);
use HTML::Entities;
use PVE::JSONSchema;
use PVE::AccessControl;
use PVE::RPCEnvironment;

use Data::Dumper; # fixme: remove

my $cookie_name = 'PVEAuthCookie';

my $baseuri = "/api2";

# http://perl.apache.org/docs/2.0/api/Apache2/SubProcess.html

31 32 33 34 35 36 37 38 39 40
my $debug_enabled;
sub enable_debug {
    $debug_enabled = 1;
}

sub debug_msg {
    return if !$debug_enabled;
    syslog('info', @_);
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
sub extract_auth_cookie {
    my ($cookie) = @_;

    return undef if !$cookie;

    return ($cookie =~ /(?:^|\s)$cookie_name=([^;]*)/)[0];
}

sub create_auth_cookie {
    my ($ticket) = @_;

    return "${cookie_name}=$ticket; path=/; secure;";
}

sub format_response_data {
    my($format, $res, $uri) = @_;

    my $data = $res->{data};
    my $info = $res->{info};

    my ($ct, $raw);

    if ($format eq 'json') {
	$ct = 'application/json';
	$raw = to_json($data, {utf8 => 1, allow_nonref => 1});
    } elsif ($format eq 'html') {
	$ct = 'text/html';
	$raw = "<html><body>";
	if (!is_success($res->{status})) {
	    my $msg = $res->{message} || '';
	    $raw .= "<h1>ERROR $res->{status} $msg</h1>";
	}
	my $lnk = PVE::JSONSchema::method_get_child_link($info);
	if ($lnk && $data && $data->{data} && is_success($res->{status})) {

	    my $href = $lnk->{href};
	    if ($href =~ m/^\{(\S+)\}$/) {
		my $prop = $1;
		$uri =~ s/\/+$//; # remove trailing slash
		foreach my $elem (sort {$a->{$prop} cmp $b->{$prop}} @{$data->{data}}) {
		    next if !ref($elem);

		    if (defined(my $value = $elem->{$prop})) {
			if ($value ne '') {
			    if (scalar(keys %$elem) > 1) {
				my $tv = to_json($elem, {allow_nonref => 1, canonical => 1});
				$raw .= "<a href='$uri/$value'>$value</a> <pre>$tv</pre><br>";
			    } else {
				$raw .= "<a href='$uri/$value'>$value</a><br>";
			    }
			}
		    }
		}
	    }
	} else {
	    $raw .= "<pre>";
	    $raw .= encode_entities(to_json($data, {utf8 => 1, allow_nonref => 1, pretty => 1}));
	    $raw .= "</pre>";
	}
	$raw .= "</body></html>";

    } elsif ($format eq 'png') {
	$ct = 'image/png';

	# fixme: better to revove that whole png thing ?

	my $filename;
	$raw = '';

	if ($data && ref($data) && ref($data->{data}) && 
	    $data->{data}->{filename}) {
	    $filename = $data->{data}->{filename};
	    $raw = PVE::Tools::file_get_contents($filename);
	}
	    
    } elsif ($format eq 'extjs') {
	$ct = 'application/json';
	$raw = to_json($data, {utf8 => 1, allow_nonref => 1});
    } elsif ($format eq 'htmljs') {
	# we use this for extjs file upload forms
	$ct = 'text/html';
	$raw = encode_entities(to_json($data, {utf8 => 1, allow_nonref => 1}));
    } else {
	$ct = 'text/plain';
	$raw = to_json($data, {utf8 => 1, allow_nonref => 1, pretty => 1});
    }

    return wantarray ? ($raw, $ct) : $raw;
}

sub prepare_response_data {
    my ($format, $res) = @_;

    my $success = 1;
    my $new = {
	data => $res->{data},
    };
    if (scalar(keys %{$res->{errors}})) {
	$success = 0;
	$new->{errors} = $res->{errors};
    }

    if ($format eq 'extjs' || $format eq 'htmljs') {
	# HACK: extjs wants 'success' property instead of useful HTTP status codes
	if (is_error($res->{status})) {
	    $success = 0;
	    $new->{message} = $res->{message} || status_message($res->{status});
	    $new->{status} = $res->{status} || HTTP_OK;
	    $res->{message} = undef;
	    $res->{status} = HTTP_OK;
	}
	$new->{success} = $success;
    }

    if ($success && $res->{total}) {
	$new->{total} = $res->{total};
    }

    $res->{data} = $new;
}

sub create_http_request {
    my ($uri, $method, $params) = @_;

    # NOTE: HTTP::Request::Common::PUT is crap - so we use our own code
    # borrowed from HTTP::Request::Common::POST

    if  ($method eq 'POST' || $method eq 'PUT') {

	my $req = HTTP::Request->new($method => $uri);
	$req->header('Content-Type' => 'application/x-www-form-urlencoded'); 

	# We use a temporary URI object to format
	# the application/x-www-form-urlencoded content.
	my $url = URI->new('http:');
	$url->query_form(%$params);
	my $content = $url->query;
	if (defined($content)) {
	    $req->header('Content-Length' => length($content));
	    $req->content($content);
	} else {
	    $req->header('Content-Length' => 0);
	}

	return $req;
    }

    die "unknown method '$method'"; 
}

sub proxy_handler {
    my($r, $clientip, $host, $method, $abs_uri, $ticket, $token, $params) = @_;

194
    debug_msg("proxy start $method $host:$abs_uri");
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    my $ua = LWP::UserAgent->new(
	protocols_allowed => [ 'http', 'https' ],
	timeout => 30,
	);

    $ua->default_header('cookie' => "${cookie_name}=$ticket") if $ticket;
    $ua->default_header('CSRFPreventionToken' => $token) if $token;
    $ua->default_header('PVEDisableProxy' => 'true');
    $ua->default_header('PVEClientIP' => $clientip);

    my $uri = URI->new();

    if ($host eq 'localhost') {
	$uri->scheme('http');
	$uri->host('localhost');
	$uri->port(85);
    } else {
	$uri->scheme('https');
	$uri->host($host);
	$uri->port(8006);
    }

    $uri->path($abs_uri);

    my $response;
    if ($method eq 'GET') {
	$uri->query_form($params);
	$response = $ua->request(HTTP::Request::Common::GET($uri));		    
    } elsif ($method eq 'POST' || $method eq 'PUT') {
	$response = $ua->request(create_http_request($uri, $method, $params));
    } elsif ($method eq 'DELETE') {
	$response = $ua->request(HTTP::Request::Common::DELETE($uri));
    } else {
	my $code = HTTP_NOT_IMPLEMENTED;
	$r->status_line("$code proxy method '$method' not implemented");
	return $code;
    }


    if (my $cookie = $response->header("Set-Cookie")) {
	$r->err_headers_out()->add("Set-Cookie" => $cookie);
    }

    my $ct = $response->header('Content-Type');

    my $code = $response->code;
    $r->status($code);

    if (my $message = $response->message) {
	$r->status_line("$code $message");
    }

    $r->content_type($ct) if $ct;
    my $raw = $response->decoded_content;

    # note: do not use err_headers_out(), because mod_deflate has a bug,
    # resulting in dup length (for exampe 'content-length: 89, 75')
    $r->headers_out()->add('Content-Length' , length($raw));
    $r->print($raw);

256
    debug_msg("proxy end $method $host:$abs_uri ($code)");
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

    return OK;
}

my $check_permissions = sub {
    my ($rpcenv, $perm, $username, $param) = @_;

    return 1 if !$username && $perm->{user} eq 'world';

    return 1 if $username eq 'root@pam';

    die "permission check failed (user != root)\n" if !$perm;

    return 1 if $perm->{user} && $perm->{user} eq 'all';

    return 1 if $perm->{user} && $perm->{user} eq 'arg' && 
	$username eq $param->{username};

    if ($perm->{path} && $perm->{privs}) {
	my $path = PVE::Tools::template_replace($perm->{path}, $param);
	if (!$rpcenv->check($username, $path, $perm->{privs})) {
	    my $privstr = join(',', @{$perm->{privs}});
	    die "Permission check failed ($path, $privstr)\n";
	}
	return 1;
    }

    die "Permission check failed\n";
};

sub rest_handler {
    my ($clientip, $method, $abs_uri, $rel_uri, $ticket, $token, $params) = @_;

    my $rpcenv = PVE::RPCEnvironment::get();

    eval { $rpcenv->init_request(); };
    if (my $err = $@) {
	syslog('err', $err);
	return { status => HTTP_INTERNAL_SERVER_ERROR, message => $err };
    }
 
    my $euid = $>;

    my $require_auth = 1;

    # explicitly allow some calls without auth
    if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
	($rel_uri eq '/access/ticket' && $method eq 'POST')) {
	$require_auth = 0;
    }

    my ($username, $age);

    if ($require_auth) {

	eval {
	    die "No ticket\n" if !$ticket;

	    ($username, $age) = PVE::AccessControl::verify_ticket($ticket);

	    PVE::AccessControl::verify_csrf_prevention_token($username, $token)
		if ($euid != 0) && ($method ne 'GET');
	};
	if (my $err = $@) {
	    return { 
		status => HTTP_UNAUTHORIZED, 
		message => $err,
	    };
	}
    }
    
    my $uri_param = {};
    my ($handler, $info) = PVE::API2->find_handler($method, $rel_uri, $uri_param);
    if (!$handler || !$info) {
	return {
	    status => HTTP_NOT_IMPLEMENTED,
	    message => "Method '$method $abs_uri' not implemented",
	};
    }

    delete $params->{_dc}; # remove disable cache parameter

    foreach my $p (keys %{$params}) {
	if (defined($uri_param->{$p})) {
	    return {
		status => HTTP_BAD_REQUEST,
		message => "Parameter verification failed - duplicate parameter '$p'",
	    };
	}
	$uri_param->{$p} = $params->{$p};
    }

    # check access permissions
    eval { &$check_permissions($rpcenv, $info->{permissions}, $username, $uri_param); };
    if (my $err = $@) {
	return { 
	    status => HTTP_FORBIDDEN, 
	    message => $err,
	};
    }

    if ($info->{proxyto}) {
	my $remip;
	eval {
	    my $pn = $info->{proxyto};
	    my $node = $uri_param->{$pn};
	    die "proxy parameter '$pn' does not exists" if !$node;

	    if ($node ne 'localhost' && 
		$node ne PVE::INotify::nodename()) {
		$remip = PVE::Cluster::remote_node_ip($node);
	    }
	};
	if (my $err = $@) {
	    return {
		status => HTTP_INTERNAL_SERVER_ERROR,
		message => $err,
	    };
	}
	if ($remip) {
	    return { proxy => $remip };
	}
    } 

    # fixme: not sure if we should do that here, because we can't proxy those
    # methods to other hosts?
    return { proxy => 'localhost' } if $info->{protected} && ($euid != 0);

    # set environment variables
    $rpcenv->set_language('C'); # fixme:
    $rpcenv->set_user($username);
    $rpcenv->set_client_ip($clientip);
    $rpcenv->set_result_count(undef);

    my $resp = { 
	info => $info, # useful to format output
	status => HTTP_OK,
    }; 

    eval {
	$resp->{data} = $handler->handle($info, $uri_param);

	if (my $count = $rpcenv->get_result_count()) {
	    $resp->{total} = $count;
	}
    };
    my $err = $@;
    if ($err) {
	if (ref($err) eq "PVE::Exception") {
	    $resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
	    $resp->{message} = $err->{msg} || $@;
	    $resp->{errors} = $err->{errors} if $err->{errors};
	} else {
	    $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
	    $resp->{message} = $@;
	}
    }

    $rpcenv->set_user(undef);

    if ($rel_uri eq '/access/ticket') {
	$resp->{ticket} = $resp->{data}->{ticket};
    }

    # fixme: update ticket if too old
    # $resp->{ticket} = update_ticket($ticket);

    return $resp;
}

sub split_abs_uri {
    my ($abs_uri) = @_;

    my ($format, $rel_uri) = $abs_uri =~ m/^\Q$baseuri\E\/+(html|json|extjs|png|htmljs)(\/.*)?$/;
    $rel_uri = '/' if !$rel_uri;
 
    return wantarray ? ($rel_uri, $format) : $rel_uri;
}

my $known_methods = {
    GET => 1,
    POST => 1,
    PUT => 1,
    DELETE => 1,
};

sub handler {
     my($r) = @_;

446
     debug_msg("perl handler called");
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

     my $method = $r->method;
     my $clientip = $r->connection->remote_ip();

     return HTTP_NOT_IMPLEMENTED
	 if !$known_methods->{$method};

     my $cgi = CGI->new ($r);

     my $params = $cgi->Vars();

     my $cookie = $r->headers_in->{Cookie};
     my $token = $r->headers_in->{CSRFPreventionToken};

     my $ticket = extract_auth_cookie($cookie);

     $r->no_cache (1);

     my $abs_uri = $r->uri;
     my ($rel_uri, $format) = split_abs_uri($abs_uri);
     return HTTP_NOT_IMPLEMENTED if !$format;

     my $res = rest_handler($clientip, $method, $abs_uri, $rel_uri, 
			    $ticket, $token, $params);

     if ($res->{proxy}) {
	 if (($res->{proxy} ne 'localhost') && $r->headers_in->{'PVEDisableProxy'}) {
	     my $code = FORBIDDEN;
	     $r->status($code);
	     $r->status_line("$code proxy loop detected - aborted ");
	     return $res->{status};	     
	 } 
	 return proxy_handler($r, $clientip, $res->{proxy}, $method, 
			      $abs_uri, $ticket, $token, $params);
     }

     prepare_response_data($format, $res);

     if ($res->{ticket}) {
	 my $cookie = create_auth_cookie($res->{ticket});
	 $r->err_headers_out()->add("Set-Cookie" => $cookie);
     }

     $r->status($res->{status} || HTTP_OK);
 
     if ($res->{message}) {
	 my ($firstline) = $res->{message} =~ m/\A(.*)$/m;
	 $r->status_line("$res->{status} $firstline");
     }

     my ($raw, $ct) = format_response_data($format, $res, $abs_uri);
     $r->content_type ($ct);

     # note: do not use err_headers_out(), because mod_deflate has a bug,
     # resulting in dup length (for exampe 'content-length: 89, 75')
     $r->headers_out()->add('Content-Length', length($raw));
     $r->print($raw);
    
505
     debug_msg("perl handler end $res->{status}");
506 507 508 509 510

     return OK;
}

1;