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, $api_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 '/quarantine/sendlink' && ($method eq 'GET' || $method eq 'POST')) ||
62 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
70 die "API tokens not implemented\n" if $api_token;
72 die "No ticket\n" if !$ticket;
74 if ($ticket =~ m/^PMGQUAR:/) {
75 ($username, $age) = PMG
::Ticket
::verify_quarantine_ticket
($ticket);
76 $rpcenv->set_user($username);
77 $rpcenv->set_role('quser');
79 ($username, $age, my $tfa) = PMG
::Ticket
::verify_ticket
($ticket, undef, 0);
80 # TFA tickets don't return a username, and return a tfa challenge, either is enough to
82 die "No ticket\n" if !$username || $tfa;
83 my $role = PMG
::AccessControl
::check_user_enabled
($self->{usercfg
}, $username);
84 $rpcenv->set_user($username);
85 $rpcenv->set_role($role);
88 $rpcenv->set_ticket($ticket);
91 PMG
::Ticket
::verify_csrf_prevention_token
($username, $token)
92 if ($euid != 0) && ($method ne 'GET');
105 my ($self, $clientip, $method, $rel_uri, $auth, $params, $format) = @_;
107 my $rpcenv = $self->{rpcenv
};
108 $rpcenv->set_format($format);
111 status
=> HTTP_NOT_IMPLEMENTED
,
112 message
=> "Method '$method $rel_uri' not implemented",
115 my ($handler, $info);
119 ($handler, $info) = PMG
::API2-
>find_handler($method, $rel_uri, $uri_param);
120 return if !$handler || !$info;
122 foreach my $p (keys %{$params}) {
123 if (defined($uri_param->{$p})) {
124 raise_param_exc
({$p => "duplicate parameter (already defined in URI)"});
126 $uri_param->{$p} = $params->{$p};
129 # check access permissions
130 $rpcenv->check_api2_permissions($info->{permissions
}, $uri_param);
132 if (my $pn = $info->{proxyto
}) {
135 if ($pn eq 'master') {
136 $node = PMG
::Cluster
::get_master_node
();
138 $node = $uri_param->{$pn};
139 raise_param_exc
({$pn => "proxy parameter '$pn' does not exists"}) if !$node;
142 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
143 die "unable to proxy file uploads" if $auth->{isUpload
};
144 my $remip = $self->remote_node_ip($node);
145 $resp = { proxy
=> $remip, proxynode
=> $node, proxy_params
=> $params };
151 if ($info->{protected
} && ($euid != 0)) {
152 $resp = { proxy
=> 'localhost' , proxy_params
=> $params };
156 if (my $pn = $info->{proxyto
}) {
157 if ($pn eq 'master') {
158 $rpcenv->check_node_is_master();
163 my $result = $handler->handle($info, $uri_param);
166 info
=> $info, # useful to format output
170 if ($info->{download
}) {
171 my $type = $info->{returns
}->{type
};
172 if ($type eq 'string' || $type eq 'object') {
173 $resp->{download
} = $result;
175 die "API calls which trigger downloads need to have return type 'string' or 'object' - internal error"
179 $resp->{data
} = $result;
182 if (my $count = $rpcenv->get_result_attrib('total')) {
183 $resp->{total
} = $count;
186 if (my $diff = $rpcenv->get_result_attrib('changes')) {
187 $resp->{changes
} = $diff;
192 $rpcenv->set_user(undef); # clear after request
193 $rpcenv->set_role(undef); # clear after request
194 $rpcenv->set_format(undef); # clear after request
197 $resp = { info
=> $info };
198 if (ref($err) eq "PVE::Exception") {
199 $resp->{status
} = $err->{code
} || HTTP_INTERNAL_SERVER_ERROR
;
200 $resp->{errors
} = $err->{errors
} if $err->{errors
};
201 $resp->{message
} = $err->{msg
};
203 $resp->{status
} = HTTP_INTERNAL_SERVER_ERROR
;
204 $resp->{message
} = $err;
211 sub check_cert_fingerprint
{
212 my ($self, $cert) = @_;
214 return PMG
::Cluster
::check_cert_fingerprint
($cert);
217 sub initialize_cert_cache
{
218 my ($self, $node) = @_;
220 PMG
::Cluster
::initialize_cert_cache
($node);
224 my ($self, $node) = @_;
226 my $remip = PMG
::Cluster
::remote_node_ip
($node);
228 die "unable to get remote IP address for node '$node'\n" if !$remip;