1 package PMG
::HTTPServer
;
9 use PVE
::APIServer
::AnyEvent
;
10 use PVE
::Exception
qw(raise_param_exc);
11 use PMG
::RESTEnvironment
;
19 use base
('PVE::APIServer::AnyEvent');
21 use HTTP
::Status
qw(:constants);
25 my ($this, %args) = @_;
27 my $class = ref($this) || $this;
29 my $self = $class->SUPER::new
(%args);
31 $self->{rpcenv
} = PMG
::RESTEnvironment-
>init(
32 $self->{trusted_env
} ?
'priv' : 'pub', atfork
=> sub { $self->atfork_handler() });
38 sub generate_csrf_prevention_token
{
41 return PMG
::Ticket
::assemble_csrf_prevention_token
($username);
45 my ($self, $method, $rel_uri, $ticket, $token, $peer_host) = @_;
47 my $rpcenv = $self->{rpcenv
};
49 # set environment variables
50 $rpcenv->set_user(undef);
51 $rpcenv->set_role(undef);
52 $rpcenv->set_language('C');
53 $rpcenv->set_client_ip($peer_host);
55 $rpcenv->init_request();
59 # explicitly allow some calls without auth
60 if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
61 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
69 die "No ticket\n" if !$ticket;
71 if ($ticket =~ m/^PMGQUAR:/) {
72 ($username, $age) = PMG
::Ticket
::verify_quarantine_ticket
($ticket);
73 $rpcenv->set_user($username);
74 $rpcenv->set_role('quser');
76 ($username, $age) = PMG
::Ticket
::verify_ticket
($ticket);
77 my $role = PMG
::AccessControl
::check_user_enabled
($self->{usercfg
}, $username);
78 $rpcenv->set_user($username);
79 $rpcenv->set_role($role);
82 $rpcenv->set_ticket($ticket);
85 PMG
::Ticket
::verify_csrf_prevention_token
($username, $token)
86 if ($euid != 0) && ($method ne 'GET');
99 my ($self, $clientip, $method, $rel_uri, $auth, $params, $format) = @_;
101 my $rpcenv = $self->{rpcenv
};
102 $rpcenv->set_format($format);
105 status
=> HTTP_NOT_IMPLEMENTED
,
106 message
=> "Method '$method $rel_uri' not implemented",
109 my ($handler, $info);
113 ($handler, $info) = PMG
::API2-
>find_handler($method, $rel_uri, $uri_param);
114 return if !$handler || !$info;
116 foreach my $p (keys %{$params}) {
117 if (defined($uri_param->{$p})) {
118 raise_param_exc
({$p => "duplicate parameter (already defined in URI)"});
120 $uri_param->{$p} = $params->{$p};
123 # check access permissions
124 $rpcenv->check_api2_permissions($info->{permissions
}, $uri_param);
126 if (my $pn = $info->{proxyto
}) {
129 if ($pn eq 'master') {
130 $node = PMG
::Cluster
::get_master_node
();
132 $node = $uri_param->{$pn};
133 raise_param_exc
({$pn => "proxy parameter '$pn' does not exists"}) if !$node;
136 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
137 die "unable to proxy file uploads" if $auth->{isUpload
};
138 my $remip = $self->remote_node_ip($node);
139 $resp = { proxy
=> $remip, proxynode
=> $node, proxy_params
=> $params };
145 if ($info->{protected
} && ($euid != 0)) {
146 $resp = { proxy
=> 'localhost' , proxy_params
=> $params };
150 if (my $pn = $info->{proxyto
}) {
151 if ($pn eq 'master') {
152 $rpcenv->check_node_is_master();
157 my $result = $handler->handle($info, $uri_param);
160 info
=> $info, # useful to format output
164 if ($info->{download
}) {
165 my $type = $info->{returns
}->{type
};
166 if ($type eq 'string' || $type eq 'object') {
167 $resp->{download
} = $result;
169 die "API calls which trigger downloads need to have return type 'string' or 'object' - internal error"
173 $resp->{data
} = $result;
176 if (my $count = $rpcenv->get_result_attrib('total')) {
177 $resp->{total
} = $count;
180 if (my $diff = $rpcenv->get_result_attrib('changes')) {
181 $resp->{changes
} = $diff;
186 $rpcenv->set_user(undef); # clear after request
187 $rpcenv->set_role(undef); # clear after request
188 $rpcenv->set_format(undef); # clear after request
191 $resp = { info
=> $info };
192 if (ref($err) eq "PVE::Exception") {
193 $resp->{status
} = $err->{code
} || HTTP_INTERNAL_SERVER_ERROR
;
194 $resp->{errors
} = $err->{errors
} if $err->{errors
};
195 $resp->{message
} = $err->{msg
};
197 $resp->{status
} = HTTP_INTERNAL_SERVER_ERROR
;
198 $resp->{message
} = $err;
205 sub check_cert_fingerprint
{
206 my ($self, $cert) = @_;
208 return PMG
::Cluster
::check_cert_fingerprint
($cert);
211 sub initialize_cert_cache
{
212 my ($self, $node) = @_;
214 PMG
::Cluster
::initialize_cert_cache
($node);
218 my ($self, $node) = @_;
220 my $remip = PMG
::Cluster
::remote_node_ip
($node);
222 die "unable to get remote IP address for node '$node'\n" if !$remip;