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