]> git.proxmox.com Git - proxmox-backup.git/blob - src/server/rest.rs
api2: update for latest proxmox-api changes
[proxmox-backup.git] / src / server / rest.rs
1 use std::collections::HashMap;
2 use std::future::Future;
3 use std::hash::BuildHasher;
4 use std::path::{Path, PathBuf};
5 use std::pin::Pin;
6 use std::sync::Arc;
7 use std::task::{Context, Poll};
8
9 use failure::*;
10 use futures::future::{self, FutureExt, TryFutureExt};
11 use futures::stream::TryStreamExt;
12 use hyper::header;
13 use hyper::http::request::Parts;
14 use hyper::{Body, Request, Response, StatusCode};
15 use serde_json::{json, Value};
16 use tokio::fs::File;
17 use tokio::time::Instant;
18 use url::form_urlencoded;
19
20 use proxmox::api::http_err;
21 use proxmox::api::{ApiHandler, ApiMethod, HttpError};
22 use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
23 use proxmox::api::schema::{ObjectSchema, parse_simple_value, verify_json_object, parse_parameter_strings};
24
25 use super::environment::RestEnvironment;
26 use super::formatter::*;
27 use super::ApiConfig;
28
29 use crate::auth_helpers::*;
30 use crate::tools;
31
32 extern "C" { fn tzset(); }
33
34 pub struct RestServer {
35 pub api_config: Arc<ApiConfig>,
36 }
37
38 impl RestServer {
39
40 pub fn new(api_config: ApiConfig) -> Self {
41 Self { api_config: Arc::new(api_config) }
42 }
43 }
44
45 impl tower_service::Service<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer {
46 type Response = ApiService;
47 type Error = Error;
48 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
49
50 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
51 Poll::Ready(Ok(()))
52 }
53
54 fn call(&mut self, ctx: &tokio_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
55 match ctx.get_ref().peer_addr() {
56 Err(err) => {
57 future::err(format_err!("unable to get peer address - {}", err)).boxed()
58 }
59 Ok(peer) => {
60 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
61 }
62 }
63 }
64 }
65
66 impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
67 type Response = ApiService;
68 type Error = Error;
69 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
70
71 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
72 Poll::Ready(Ok(()))
73 }
74
75 fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
76 match ctx.peer_addr() {
77 Err(err) => {
78 future::err(format_err!("unable to get peer address - {}", err)).boxed()
79 }
80 Ok(peer) => {
81 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
82 }
83 }
84 }
85 }
86
87 pub struct ApiService {
88 pub peer: std::net::SocketAddr,
89 pub api_config: Arc<ApiConfig>,
90 }
91
92 fn log_response(
93 peer: &std::net::SocketAddr,
94 method: hyper::Method,
95 path: &str,
96 resp: &Response<Body>,
97 ) {
98
99 if resp.extensions().get::<NoLogExtension>().is_some() { return; };
100
101 let status = resp.status();
102
103 if !(status.is_success() || status.is_informational()) {
104 let reason = status.canonical_reason().unwrap_or("unknown reason");
105
106 let mut message = "request failed";
107 if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
108 message = &data.0;
109 }
110
111 log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
112 }
113 }
114
115 impl tower_service::Service<Request<Body>> for ApiService {
116 type Response = Response<Body>;
117 type Error = Error;
118 type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
119
120 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
121 Poll::Ready(Ok(()))
122 }
123
124 fn call(&mut self, req: Request<Body>) -> Self::Future {
125 let path = req.uri().path().to_owned();
126 let method = req.method().clone();
127
128 let peer = self.peer;
129 handle_request(self.api_config.clone(), req)
130 .map(move |result| match result {
131 Ok(res) => {
132 log_response(&peer, method, &path, &res);
133 Ok::<_, Self::Error>(res)
134 }
135 Err(err) => {
136 if let Some(apierr) = err.downcast_ref::<HttpError>() {
137 let mut resp = Response::new(Body::from(apierr.message.clone()));
138 *resp.status_mut() = apierr.code;
139 log_response(&peer, method, &path, &resp);
140 Ok(resp)
141 } else {
142 let mut resp = Response::new(Body::from(err.to_string()));
143 *resp.status_mut() = StatusCode::BAD_REQUEST;
144 log_response(&peer, method, &path, &resp);
145 Ok(resp)
146 }
147 }
148 })
149 .boxed()
150 }
151 }
152
153 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
154 param_schema: &ObjectSchema,
155 form: &str, // x-www-form-urlencoded body data
156 parts: &Parts,
157 uri_param: &HashMap<String, String, S>,
158 ) -> Result<Value, Error> {
159
160 let mut param_list: Vec<(String, String)> = vec![];
161
162 if !form.is_empty() {
163 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
164 param_list.push((k, v));
165 }
166 }
167
168 if let Some(query_str) = parts.uri.query() {
169 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
170 if k == "_dc" { continue; } // skip extjs "disable cache" parameter
171 param_list.push((k, v));
172 }
173 }
174
175 for (k, v) in uri_param {
176 param_list.push((k.clone(), v.clone()));
177 }
178
179 let params = parse_parameter_strings(&param_list, param_schema, true)?;
180
181 Ok(params)
182 }
183
184 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
185 param_schema: &ObjectSchema,
186 parts: Parts,
187 req_body: Body,
188 uri_param: HashMap<String, String, S>,
189 ) -> Result<Value, Error> {
190
191 let mut is_json = false;
192
193 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
194 match value.to_str().map(|v| v.split(';').next()) {
195 Ok(Some("application/x-www-form-urlencoded")) => {
196 is_json = false;
197 }
198 Ok(Some("application/json")) => {
199 is_json = true;
200 }
201 _ => bail!("unsupported content type {:?}", value.to_str()),
202 }
203 }
204
205 let body = req_body
206 .map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err)))
207 .try_fold(Vec::new(), |mut acc, chunk| async move {
208 if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
209 acc.extend_from_slice(&*chunk);
210 Ok(acc)
211 } else {
212 Err(http_err!(BAD_REQUEST, "Request body too large".to_string()))
213 }
214 }).await?;
215
216 let utf8_data = std::str::from_utf8(&body)
217 .map_err(|err| format_err!("Request body not uft8: {}", err))?;
218
219 if is_json {
220 let mut params: Value = serde_json::from_str(utf8_data)?;
221 for (k, v) in uri_param {
222 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
223 params[&k] = parse_simple_value(&v, prop_schema)?;
224 }
225 }
226 verify_json_object(&params, param_schema)?;
227 return Ok(params);
228 } else {
229 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
230 }
231 }
232
233 struct NoLogExtension();
234
235 async fn proxy_protected_request(
236 info: &'static ApiMethod,
237 mut parts: Parts,
238 req_body: Body,
239 ) -> Result<Response<Body>, Error> {
240
241 let mut uri_parts = parts.uri.clone().into_parts();
242
243 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
244 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
245 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
246
247 parts.uri = new_uri;
248
249 let request = Request::from_parts(parts, req_body);
250
251 let reload_timezone = info.reload_timezone;
252
253 let resp = hyper::client::Client::new()
254 .request(request)
255 .map_err(Error::from)
256 .map_ok(|mut resp| {
257 resp.extensions_mut().insert(NoLogExtension());
258 resp
259 })
260 .await?;
261
262 if reload_timezone { unsafe { tzset(); } }
263
264 Ok(resp)
265 }
266
267 pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
268 mut rpcenv: Env,
269 info: &'static ApiMethod,
270 formatter: &'static OutputFormatter,
271 parts: Parts,
272 req_body: Body,
273 uri_param: HashMap<String, String, S>,
274 ) -> Result<Response<Body>, Error> {
275
276 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
277
278 let result = match info.handler {
279 ApiHandler::AsyncHttp(handler) => {
280 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
281 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
282 }
283 ApiHandler::Sync(handler) => {
284 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
285 (handler)(params, info, &mut rpcenv)
286 .map(|data| (formatter.format_data)(data, &rpcenv))
287 }
288 ApiHandler::Async(handler) => {
289 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
290 (handler)(params, info, &mut rpcenv)
291 .await
292 .map(|data| (formatter.format_data)(data, &rpcenv))
293 }
294 };
295
296 let resp = match result {
297 Ok(resp) => resp,
298 Err(err) => {
299 if let Some(httperr) = err.downcast_ref::<HttpError>() {
300 if httperr.code == StatusCode::UNAUTHORIZED {
301 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
302 }
303 }
304 (formatter.format_error)(err)
305 }
306 };
307
308 if info.reload_timezone { unsafe { tzset(); } }
309
310 Ok(resp)
311 }
312
313 fn get_index(username: Option<String>, token: Option<String>) -> Response<Body> {
314
315 let nodename = proxmox::tools::nodename();
316 let username = username.unwrap_or_else(|| String::from(""));
317
318 let token = token.unwrap_or_else(|| String::from(""));
319
320 let setup = json!({
321 "Setup": { "auth_cookie_name": "PBSAuthCookie" },
322 "NodeName": nodename,
323 "UserName": username,
324 "CSRFPreventionToken": token,
325 });
326
327 let index = format!(r###"
328 <!DOCTYPE html>
329 <html>
330 <head>
331 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
332 <meta http-equiv="X-UA-Compatible" content="IE=edge">
333 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
334 <title>Proxmox Backup Server</title>
335 <link rel="icon" sizes="128x128" href="/images/logo-128.png" />
336 <link rel="apple-touch-icon" sizes="128x128" href="/pve2/images/logo-128.png" />
337 <link rel="stylesheet" type="text/css" href="/extjs/theme-crisp/resources/theme-crisp-all.css" />
338 <link rel="stylesheet" type="text/css" href="/extjs/crisp/resources/charts-all.css" />
339 <link rel="stylesheet" type="text/css" href="/fontawesome/css/font-awesome.css" />
340 <script type='text/javascript'> function gettext(buf) {{ return buf; }} </script>
341 <script type="text/javascript" src="/extjs/ext-all-debug.js"></script>
342 <script type="text/javascript" src="/extjs/charts-debug.js"></script>
343 <script type="text/javascript">
344 Proxmox = {};
345 </script>
346 <script type="text/javascript" src="/widgettoolkit/proxmoxlib.js"></script>
347 <script type="text/javascript" src="/extjs/locale/locale-en.js"></script>
348 <script type="text/javascript">
349 Ext.History.fieldid = 'x-history-field';
350 </script>
351 <script type="text/javascript" src="/js/proxmox-backup-gui.js"></script>
352 </head>
353 <body>
354 <!-- Fields required for history management -->
355 <form id="history-form" class="x-hidden">
356 <input type="hidden" id="x-history-field"/>
357 </form>
358 </body>
359 </html>
360 "###, setup.to_string());
361
362 Response::builder()
363 .status(StatusCode::OK)
364 .header(header::CONTENT_TYPE, "text/html")
365 .body(index.into())
366 .unwrap()
367 }
368
369 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
370
371 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
372 return match ext {
373 "css" => ("text/css", false),
374 "html" => ("text/html", false),
375 "js" => ("application/javascript", false),
376 "json" => ("application/json", false),
377 "map" => ("application/json", false),
378 "png" => ("image/png", true),
379 "ico" => ("image/x-icon", true),
380 "gif" => ("image/gif", true),
381 "svg" => ("image/svg+xml", false),
382 "jar" => ("application/java-archive", true),
383 "woff" => ("application/font-woff", true),
384 "woff2" => ("application/font-woff2", true),
385 "ttf" => ("application/font-snft", true),
386 "pdf" => ("application/pdf", true),
387 "epub" => ("application/epub+zip", true),
388 "mp3" => ("audio/mpeg", true),
389 "oga" => ("audio/ogg", true),
390 "tgz" => ("application/x-compressed-tar", true),
391 _ => ("application/octet-stream", false),
392 };
393 }
394
395 ("application/octet-stream", false)
396 }
397
398 async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
399
400 let (content_type, _nocomp) = extension_to_content_type(&filename);
401
402 use tokio::io::AsyncReadExt;
403
404 let mut file = File::open(filename)
405 .await
406 .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
407
408 let mut data: Vec<u8> = Vec::new();
409 file.read_to_end(&mut data)
410 .await
411 .map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))?;
412
413 let mut response = Response::new(data.into());
414 response.headers_mut().insert(
415 header::CONTENT_TYPE,
416 header::HeaderValue::from_static(content_type));
417 Ok(response)
418 }
419
420 async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
421 let (content_type, _nocomp) = extension_to_content_type(&filename);
422
423 let file = File::open(filename)
424 .await
425 .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
426
427 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
428 .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
429 let body = Body::wrap_stream(payload);
430
431 // fixme: set other headers ?
432 Ok(Response::builder()
433 .status(StatusCode::OK)
434 .header(header::CONTENT_TYPE, content_type)
435 .body(body)
436 .unwrap()
437 )
438 }
439
440 async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
441
442 tokio::fs::metadata(filename.clone())
443 .map_err(|err| http_err!(BAD_REQUEST, format!("File access problems: {}", err)))
444 .and_then(|metadata| async move {
445 if metadata.len() < 1024*32 {
446 simple_static_file_download(filename).await
447 } else {
448 chuncked_static_file_download(filename).await
449 }
450 })
451 .await
452 }
453
454 fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>) {
455
456 let mut ticket = None;
457 if let Some(raw_cookie) = headers.get("COOKIE") {
458 if let Ok(cookie) = raw_cookie.to_str() {
459 ticket = tools::extract_auth_cookie(cookie, "PBSAuthCookie");
460 }
461 }
462
463 let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
464 Some(Ok(v)) => Some(v.to_owned()),
465 _ => None,
466 };
467
468 (ticket, token)
469 }
470
471 fn check_auth(method: &hyper::Method, ticket: &Option<String>, token: &Option<String>) -> Result<String, Error> {
472
473 let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
474
475 let username = match ticket {
476 Some(ticket) => match tools::ticket::verify_rsa_ticket(public_auth_key(), "PBS", &ticket, None, -300, ticket_lifetime) {
477 Ok((_age, Some(username))) => username.to_owned(),
478 Ok((_, None)) => bail!("ticket without username."),
479 Err(err) => return Err(err),
480 }
481 None => bail!("missing ticket"),
482 };
483
484 if method != hyper::Method::GET {
485 if let Some(token) = token {
486 println!("CSRF prevention token: {:?}", token);
487 verify_csrf_prevention_token(csrf_secret(), &username, &token, -300, ticket_lifetime)?;
488 } else {
489 bail!("missing CSRF prevention token");
490 }
491 }
492
493 Ok(username)
494 }
495
496 pub async fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> Result<Response<Body>, Error> {
497
498 let (parts, body) = req.into_parts();
499
500 let method = parts.method.clone();
501 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
502
503 let comp_len = components.len();
504
505 println!("REQUEST {} {}", method, path);
506 println!("COMPO {:?}", components);
507
508 let env_type = api.env_type();
509 let mut rpcenv = RestEnvironment::new(env_type);
510
511 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
512
513 if comp_len >= 1 && components[0] == "api2" {
514
515 if comp_len >= 2 {
516
517 let format = components[1];
518
519 let formatter = match format {
520 "json" => &JSON_FORMATTER,
521 "extjs" => &EXTJS_FORMATTER,
522 _ => bail!("Unsupported output format '{}'.", format),
523 };
524
525 let mut uri_param = HashMap::new();
526
527 if comp_len == 4 && components[2] == "access" && components[3] == "ticket" {
528 // explicitly allow those calls without auth
529 } else {
530 let (ticket, token) = extract_auth_data(&parts.headers);
531 match check_auth(&method, &ticket, &token) {
532 Ok(username) => {
533
534 // fixme: check permissions
535
536 rpcenv.set_user(Some(username));
537 }
538 Err(err) => {
539 // always delay unauthorized calls by 3 seconds (from start of request)
540 let err = http_err!(UNAUTHORIZED, format!("permission check failed - {}", err));
541 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
542 return Ok((formatter.format_error)(err));
543 }
544 }
545 }
546
547 match api.find_method(&components[2..], method, &mut uri_param) {
548 None => {
549 let err = http_err!(NOT_FOUND, "Path not found.".to_string());
550 return Ok((formatter.format_error)(err));
551 }
552 Some(api_method) => {
553 if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
554 return proxy_protected_request(api_method, parts, body).await;
555 } else {
556 return handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await;
557 }
558 }
559 }
560 }
561 } else {
562 // not Auth required for accessing files!
563
564 if method != hyper::Method::GET {
565 bail!("Unsupported HTTP method {}", method);
566 }
567
568 if comp_len == 0 {
569 let (ticket, token) = extract_auth_data(&parts.headers);
570 if ticket != None {
571 match check_auth(&method, &ticket, &token) {
572 Ok(username) => {
573 let new_token = assemble_csrf_prevention_token(csrf_secret(), &username);
574 return Ok(get_index(Some(username), Some(new_token)));
575 }
576 _ => {
577 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
578 return Ok(get_index(None, None));
579 }
580 }
581 } else {
582 return Ok(get_index(None, None));
583 }
584 } else {
585 let filename = api.find_alias(&components);
586 return handle_static_file_download(filename).await;
587 }
588 }
589
590 Err(http_err!(NOT_FOUND, "Path not found.".to_string()))
591 }