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