]> git.proxmox.com Git - proxmox-backup.git/blame - src/server/rest.rs
move src/server/formatter.rs to proxmox-rest-server crate
[proxmox-backup.git] / src / server / rest.rs
CommitLineData
91e45873 1use std::collections::HashMap;
db0cb9ce 2use std::future::Future;
62ee2eb4 3use std::hash::BuildHasher;
826bb982 4use std::path::{Path, PathBuf};
91e45873 5use std::pin::Pin;
8e7e2223 6use std::sync::{Arc, Mutex};
91e45873 7use std::task::{Context, Poll};
9bc17e8d 8
f7d4e4b5 9use anyhow::{bail, format_err, Error};
ad51d02a 10use futures::future::{self, FutureExt, TryFutureExt};
91e45873 11use futures::stream::TryStreamExt;
8e7e2223 12use hyper::body::HttpBody;
d43c407a 13use hyper::header::{self, HeaderMap};
91e45873 14use hyper::http::request::Parts;
91e45873 15use hyper::{Body, Request, Response, StatusCode};
29633e2f 16use lazy_static::lazy_static;
d43c407a 17use regex::Regex;
f4c514c1 18use serde_json::{json, Value};
9bc17e8d 19use tokio::fs::File;
db0cb9ce 20use tokio::time::Instant;
91e45873 21use url::form_urlencoded;
9bc17e8d 22
4e6dc587 23use proxmox::api::schema::{
d43c407a 24 parse_parameter_strings, parse_simple_value, verify_json_object, ObjectSchemaType,
29a59b38 25 ParameterSchema,
4e6dc587 26};
d43c407a
TL
27use proxmox::api::{
28 check_api_permission, ApiHandler, ApiMethod, HttpError, Permission, RpcEnvironment,
29 RpcEnvironmentType,
30};
31use proxmox::http_err;
fd6d2438 32use proxmox::tools::fs::CreateOptions;
a2479cfa 33
2b7f8dd5 34use pbs_tools::compression::{DeflateEncoder, Level};
fc5870be 35use pbs_tools::stream::AsyncReaderStream;
6227654a 36use pbs_api_types::{Authid, Userid};
d4d49f73 37use proxmox_rest_server::{ApiConfig, FileLogger, FileLogOptions, AuthError, RestEnvironment};
1b552c10 38use proxmox_rest_server::formatter::*;
e57e1cd8 39
d43c407a 40use crate::auth_helpers::*;
ba3d7e19 41use pbs_config::CachedUserInfo;
91e45873 42use crate::tools;
2b7f8dd5 43use crate::tools::compression::CompressionMethod;
9bc17e8d 44
d43c407a
TL
45extern "C" {
46 fn tzset();
47}
4b2cdeb9 48
f0b10921
DM
49pub struct RestServer {
50 pub api_config: Arc<ApiConfig>,
51}
52
4703ba81 53const MAX_URI_QUERY_LENGTH: usize = 3072;
59477ad2 54const CHUNK_SIZE_LIMIT: u64 = 32 * 1024;
4703ba81 55
f0b10921 56impl RestServer {
f0b10921 57 pub fn new(api_config: ApiConfig) -> Self {
d43c407a
TL
58 Self {
59 api_config: Arc::new(api_config),
60 }
f0b10921
DM
61 }
62}
63
d43c407a
TL
64impl tower_service::Service<&Pin<Box<tokio_openssl::SslStream<tokio::net::TcpStream>>>>
65 for RestServer
66{
91e45873 67 type Response = ApiService;
7fb4f564 68 type Error = Error;
91e45873
WB
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
d43c407a
TL
75 fn call(
76 &mut self,
77 ctx: &Pin<Box<tokio_openssl::SslStream<tokio::net::TcpStream>>>,
78 ) -> Self::Future {
91e45873 79 match ctx.get_ref().peer_addr() {
d43c407a
TL
80 Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(),
81 Ok(peer) => future::ok(ApiService {
82 peer,
83 api_config: self.api_config.clone(),
84 })
85 .boxed(),
80af0467 86 }
f0b10921
DM
87 }
88}
89
91e45873
WB
90impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
91 type Response = ApiService;
7fb4f564 92 type Error = Error;
91e45873
WB
93 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
94
95 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
96 Poll::Ready(Ok(()))
97 }
98
99 fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
80af0467 100 match ctx.peer_addr() {
d43c407a
TL
101 Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(),
102 Ok(peer) => future::ok(ApiService {
103 peer,
104 api_config: self.api_config.clone(),
105 })
106 .boxed(),
80af0467 107 }
7fb4f564
DM
108 }
109}
110
b57c0dbe
SR
111impl tower_service::Service<&tokio::net::UnixStream> for RestServer {
112 type Response = ApiService;
113 type Error = Error;
114 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
115
116 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
117 Poll::Ready(Ok(()))
118 }
119
120 fn call(&mut self, _ctx: &tokio::net::UnixStream) -> Self::Future {
121 // TODO: Find a way to actually represent the vsock peer in the ApiService struct - for now
122 // it doesn't really matter, so just use a fake IP address
123 let fake_peer = "0.0.0.0:807".parse().unwrap();
124 future::ok(ApiService {
125 peer: fake_peer,
d43c407a
TL
126 api_config: self.api_config.clone(),
127 })
128 .boxed()
b57c0dbe
SR
129 }
130}
131
f0b10921 132pub struct ApiService {
7fb4f564 133 pub peer: std::net::SocketAddr,
f0b10921
DM
134 pub api_config: Arc<ApiConfig>,
135}
136
7fb4f564 137fn log_response(
fe4cc5b1 138 logfile: Option<&Arc<Mutex<FileLogger>>>,
7fb4f564
DM
139 peer: &std::net::SocketAddr,
140 method: hyper::Method,
400c568f 141 path_query: &str,
7fb4f564 142 resp: &Response<Body>,
86f3c236 143 user_agent: Option<String>,
7fb4f564 144) {
d43c407a
TL
145 if resp.extensions().get::<NoLogExtension>().is_some() {
146 return;
147 };
7e03988c 148
4703ba81
TL
149 // we also log URL-to-long requests, so avoid message bigger than PIPE_BUF (4k on Linux)
150 // to profit from atomicty guarantees for O_APPEND opened logfiles
400c568f 151 let path = &path_query[..MAX_URI_QUERY_LENGTH.min(path_query.len())];
4703ba81 152
d4736445 153 let status = resp.status();
1133fe9a 154 if !(status.is_success() || status.is_informational()) {
d4736445 155 let reason = status.canonical_reason().unwrap_or("unknown reason");
44c00c0d 156
6b5013ed
TL
157 let message = match resp.extensions().get::<ErrorMessageExtension>() {
158 Some(data) => &data.0,
159 None => "request failed",
160 };
d4736445 161
d43c407a
TL
162 log::error!(
163 "{} {}: {} {}: [client {}] {}",
164 method.as_str(),
165 path,
166 status.as_str(),
167 reason,
168 peer,
169 message
170 );
78a1fa67 171 }
8e7e2223 172 if let Some(logfile) = logfile {
e6dc35ac
FG
173 let auth_id = match resp.extensions().get::<Authid>() {
174 Some(auth_id) => auth_id.to_string(),
175 None => "-".to_string(),
8e7e2223
TL
176 };
177 let now = proxmox::tools::time::epoch_i64();
178 // time format which apache/nginx use (by default), copied from pve-http-server
179 let datetime = proxmox::tools::time::strftime_local("%d/%m/%Y:%H:%M:%S %z", now)
e062ebbc 180 .unwrap_or_else(|_| "-".to_string());
8e7e2223 181
d43c407a
TL
182 logfile.lock().unwrap().log(format!(
183 "{} - {} [{}] \"{} {}\" {} {} {}",
184 peer.ip(),
185 auth_id,
186 datetime,
187 method.as_str(),
188 path,
189 status.as_str(),
190 resp.body().size_hint().lower(),
191 user_agent.unwrap_or_else(|| "-".to_string()),
192 ));
8e7e2223 193 }
78a1fa67 194}
4fdf13f9 195pub fn auth_logger() -> Result<FileLogger, Error> {
fd6d2438
DM
196 let backup_user = pbs_config::backup_user()?;
197
198 let file_opts = CreateOptions::new()
199 .owner(backup_user.uid)
200 .group(backup_user.gid);
201
202 let logger_options = FileLogOptions {
4fdf13f9
TL
203 append: true,
204 prefix_time: true,
fd6d2438 205 file_opts,
4fdf13f9
TL
206 ..Default::default()
207 };
af06decd 208 FileLogger::new(pbs_buildcfg::API_AUTH_LOG_FN, logger_options)
4fdf13f9 209}
f0b10921 210
29633e2f
TL
211fn get_proxied_peer(headers: &HeaderMap) -> Option<std::net::SocketAddr> {
212 lazy_static! {
213 static ref RE: Regex = Regex::new(r#"for="([^"]+)""#).unwrap();
214 }
215 let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
216 let capture = RE.captures(&forwarded)?;
217 let rhost = capture.get(1)?.as_str();
218
219 rhost.parse().ok()
220}
221
86f3c236
TL
222fn get_user_agent(headers: &HeaderMap) -> Option<String> {
223 let agent = headers.get(header::USER_AGENT)?.to_str();
d43c407a
TL
224 agent
225 .map(|s| {
226 let mut s = s.to_owned();
227 s.truncate(128);
228 s
229 })
230 .ok()
86f3c236
TL
231}
232
91e45873
WB
233impl tower_service::Service<Request<Body>> for ApiService {
234 type Response = Response<Body>;
7fb4f564 235 type Error = Error;
12e874ce
FG
236 #[allow(clippy::type_complexity)]
237 type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
91e45873
WB
238
239 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
240 Poll::Ready(Ok(()))
241 }
f0b10921 242
91e45873 243 fn call(&mut self, req: Request<Body>) -> Self::Future {
400c568f 244 let path = req.uri().path_and_query().unwrap().as_str().to_owned();
d4736445 245 let method = req.method().clone();
86f3c236 246 let user_agent = get_user_agent(req.headers());
d4736445 247
07995a3c 248 let config = Arc::clone(&self.api_config);
29633e2f
TL
249 let peer = match get_proxied_peer(req.headers()) {
250 Some(proxied_peer) => proxied_peer,
251 None => self.peer,
252 };
07995a3c 253 async move {
8e7e2223 254 let response = match handle_request(Arc::clone(&config), req, &peer).await {
b947b1e7 255 Ok(response) => response,
f0b10921 256 Err(err) => {
b947b1e7
TL
257 let (err, code) = match err.downcast_ref::<HttpError>() {
258 Some(apierr) => (apierr.message.clone(), apierr.code),
259 _ => (err.to_string(), StatusCode::BAD_REQUEST),
260 };
f4d371d2
TL
261 Response::builder()
262 .status(code)
263 .extension(ErrorMessageExtension(err.to_string()))
264 .body(err.into())?
f0b10921 265 }
b947b1e7 266 };
8e7e2223 267 let logger = config.get_file_log();
86f3c236 268 log_response(logger, &peer, method, &path, &response, user_agent);
b947b1e7 269 Ok(response)
07995a3c
TL
270 }
271 .boxed()
f0b10921
DM
272 }
273}
274
70fbac84 275fn parse_query_parameters<S: 'static + BuildHasher + Send>(
b2362a12 276 param_schema: ParameterSchema,
70fbac84
DM
277 form: &str, // x-www-form-urlencoded body data
278 parts: &Parts,
279 uri_param: &HashMap<String, String, S>,
280) -> Result<Value, Error> {
70fbac84
DM
281 let mut param_list: Vec<(String, String)> = vec![];
282
283 if !form.is_empty() {
284 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
285 param_list.push((k, v));
286 }
287 }
288
289 if let Some(query_str) = parts.uri.query() {
290 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
d43c407a
TL
291 if k == "_dc" {
292 continue;
293 } // skip extjs "disable cache" parameter
70fbac84
DM
294 param_list.push((k, v));
295 }
296 }
297
298 for (k, v) in uri_param {
299 param_list.push((k.clone(), v.clone()));
300 }
301
302 let params = parse_parameter_strings(&param_list, param_schema, true)?;
303
304 Ok(params)
305}
306
2bbd835b 307async fn get_request_parameters<S: 'static + BuildHasher + Send>(
b2362a12 308 param_schema: ParameterSchema,
9bc17e8d
DM
309 parts: Parts,
310 req_body: Body,
62ee2eb4 311 uri_param: HashMap<String, String, S>,
ad51d02a 312) -> Result<Value, Error> {
0ffbccce
DM
313 let mut is_json = false;
314
315 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
8346f0d5
DM
316 match value.to_str().map(|v| v.split(';').next()) {
317 Ok(Some("application/x-www-form-urlencoded")) => {
318 is_json = false;
319 }
320 Ok(Some("application/json")) => {
321 is_json = true;
322 }
ad51d02a 323 _ => bail!("unsupported content type {:?}", value.to_str()),
0ffbccce
DM
324 }
325 }
326
eeff085d
TL
327 let body = TryStreamExt::map_err(req_body, |err| {
328 http_err!(BAD_REQUEST, "Problems reading request body: {}", err)
329 })
330 .try_fold(Vec::new(), |mut acc, chunk| async move {
331 // FIXME: max request body size?
332 if acc.len() + chunk.len() < 64 * 1024 {
333 acc.extend_from_slice(&*chunk);
334 Ok(acc)
335 } else {
336 Err(http_err!(BAD_REQUEST, "Request body too large"))
337 }
338 })
339 .await?;
9bc17e8d 340
d43c407a
TL
341 let utf8_data =
342 std::str::from_utf8(&body).map_err(|err| format_err!("Request body not uft8: {}", err))?;
0ffbccce 343
ad51d02a 344 if is_json {
70fbac84 345 let mut params: Value = serde_json::from_str(utf8_data)?;
ad51d02a 346 for (k, v) in uri_param {
75a5a689 347 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
ad51d02a 348 params[&k] = parse_simple_value(&v, prop_schema)?;
9bc17e8d 349 }
ad51d02a 350 }
b2362a12 351 verify_json_object(&params, &param_schema)?;
ad51d02a 352 return Ok(params);
70fbac84
DM
353 } else {
354 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
ad51d02a 355 }
9bc17e8d
DM
356}
357
7171b3e0
DM
358struct NoLogExtension();
359
ad51d02a 360async fn proxy_protected_request(
4b2cdeb9 361 info: &'static ApiMethod,
a3da38dd 362 mut parts: Parts,
f1204833 363 req_body: Body,
29633e2f 364 peer: &std::net::SocketAddr,
ad51d02a 365) -> Result<Response<Body>, Error> {
a3da38dd
DM
366 let mut uri_parts = parts.uri.clone().into_parts();
367
368 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
369 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
370 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
371
372 parts.uri = new_uri;
373
29633e2f 374 let mut request = Request::from_parts(parts, req_body);
d43c407a
TL
375 request.headers_mut().insert(
376 header::FORWARDED,
377 format!("for=\"{}\";", peer).parse().unwrap(),
378 );
a3da38dd 379
ad51d02a
DM
380 let reload_timezone = info.reload_timezone;
381
a3da38dd
DM
382 let resp = hyper::client::Client::new()
383 .request(request)
fc7f0352 384 .map_err(Error::from)
91e45873 385 .map_ok(|mut resp| {
1cb99c23 386 resp.extensions_mut().insert(NoLogExtension());
7e03988c 387 resp
ad51d02a
DM
388 })
389 .await?;
a3da38dd 390
d43c407a
TL
391 if reload_timezone {
392 unsafe {
393 tzset();
394 }
395 }
1cb99c23 396
ad51d02a 397 Ok(resp)
f1204833
DM
398}
399
70fbac84 400pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
f757b30e 401 mut rpcenv: Env,
279ecfdf 402 info: &'static ApiMethod,
1571873d 403 formatter: &'static OutputFormatter,
9bc17e8d
DM
404 parts: Parts,
405 req_body: Body,
62ee2eb4 406 uri_param: HashMap<String, String, S>,
ad51d02a 407) -> Result<Response<Body>, Error> {
a154a8e8 408 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
2f29f1c7 409 let compression = extract_compression_method(&parts.headers);
a154a8e8 410
70fbac84 411 let result = match info.handler {
329d40b5 412 ApiHandler::AsyncHttp(handler) => {
70fbac84
DM
413 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
414 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
415 }
416 ApiHandler::Sync(handler) => {
d43c407a
TL
417 let params =
418 get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
419 (handler)(params, info, &mut rpcenv).map(|data| (formatter.format_data)(data, &rpcenv))
70fbac84 420 }
bb084b9c 421 ApiHandler::Async(handler) => {
d43c407a
TL
422 let params =
423 get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
bb084b9c
DM
424 (handler)(params, info, &mut rpcenv)
425 .await
426 .map(|data| (formatter.format_data)(data, &rpcenv))
427 }
70fbac84 428 };
a154a8e8 429
2f29f1c7 430 let mut resp = match result {
70fbac84 431 Ok(resp) => resp,
ad51d02a
DM
432 Err(err) => {
433 if let Some(httperr) = err.downcast_ref::<HttpError>() {
434 if httperr.code == StatusCode::UNAUTHORIZED {
0a8d773a 435 tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
ad51d02a 436 }
4b2cdeb9 437 }
ad51d02a
DM
438 (formatter.format_error)(err)
439 }
440 };
4b2cdeb9 441
2f29f1c7
DC
442 let resp = match compression {
443 Some(CompressionMethod::Deflate) => {
444 resp.headers_mut().insert(
445 header::CONTENT_ENCODING,
446 CompressionMethod::Deflate.content_encoding(),
447 );
448 resp.map(|body| {
449 Body::wrap_stream(DeflateEncoder::with_quality(
f2f43e19 450 TryStreamExt::map_err(body, |err| {
2f29f1c7
DC
451 proxmox::io_format_err!("error during compression: {}", err)
452 }),
b84e8aae 453 Level::Default,
2f29f1c7
DC
454 ))
455 })
456 }
457 None => resp,
458 };
459
d43c407a
TL
460 if info.reload_timezone {
461 unsafe {
462 tzset();
463 }
464 }
ad51d02a 465
ad51d02a 466 Ok(resp)
7e21da6e
DM
467}
468
abd4c4cb
TL
469fn get_index(
470 userid: Option<Userid>,
6c5bdef5 471 csrf_token: Option<String>,
abd4c4cb
TL
472 language: Option<String>,
473 api: &Arc<ApiConfig>,
474 parts: Parts,
d43c407a 475) -> Response<Body> {
f69adc81 476 let nodename = proxmox::tools::nodename();
6c5bdef5 477 let user = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
7f168523 478
6c5bdef5 479 let csrf_token = csrf_token.unwrap_or_else(|| String::from(""));
f4c514c1 480
f9e3b110 481 let mut debug = false;
01ca99da 482 let mut template_file = "index";
f9e3b110
DC
483
484 if let Some(query_str) = parts.uri.query() {
485 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
3f683799 486 if k == "debug" && v != "0" && v != "false" {
f9e3b110 487 debug = true;
01ca99da
DC
488 } else if k == "console" {
489 template_file = "console";
f9e3b110
DC
490 }
491 }
492 }
493
abd4c4cb
TL
494 let mut lang = String::from("");
495 if let Some(language) = language {
496 if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
497 lang = language;
498 }
499 }
500
f9e3b110 501 let data = json!({
f4c514c1 502 "NodeName": nodename,
6c5bdef5
TL
503 "UserName": user,
504 "CSRFPreventionToken": csrf_token,
abd4c4cb 505 "language": lang,
f9e3b110 506 "debug": debug,
f4c514c1
DM
507 });
508
adfcfb67
TL
509 let (ct, index) = match api.render_template(template_file, &data) {
510 Ok(index) => ("text/html", index),
d43c407a 511 Err(err) => ("text/plain", format!("Error rendering template: {}", err)),
f9e3b110 512 };
f4c514c1 513
8e7e2223 514 let mut resp = Response::builder()
d15009c0 515 .status(StatusCode::OK)
f9e3b110 516 .header(header::CONTENT_TYPE, ct)
d15009c0 517 .body(index.into())
8e7e2223
TL
518 .unwrap();
519
520 if let Some(userid) = userid {
e6dc35ac 521 resp.extensions_mut().insert(Authid::from((userid, None)));
8e7e2223
TL
522 }
523
524 resp
f4c514c1
DM
525}
526
826bb982 527fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
826bb982
DM
528 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
529 return match ext {
530 "css" => ("text/css", false),
531 "html" => ("text/html", false),
532 "js" => ("application/javascript", false),
533 "json" => ("application/json", false),
534 "map" => ("application/json", false),
535 "png" => ("image/png", true),
536 "ico" => ("image/x-icon", true),
537 "gif" => ("image/gif", true),
538 "svg" => ("image/svg+xml", false),
539 "jar" => ("application/java-archive", true),
540 "woff" => ("application/font-woff", true),
541 "woff2" => ("application/font-woff2", true),
542 "ttf" => ("application/font-snft", true),
543 "pdf" => ("application/pdf", true),
544 "epub" => ("application/epub+zip", true),
545 "mp3" => ("audio/mpeg", true),
546 "oga" => ("audio/ogg", true),
547 "tgz" => ("application/x-compressed-tar", true),
548 _ => ("application/octet-stream", false),
549 };
550 }
551
552 ("application/octet-stream", false)
553}
554
59477ad2
DC
555async fn simple_static_file_download(
556 filename: PathBuf,
557 content_type: &'static str,
558 compression: Option<CompressionMethod>,
559) -> Result<Response<Body>, Error> {
91e45873 560 use tokio::io::AsyncReadExt;
9bc17e8d 561
91e45873
WB
562 let mut file = File::open(filename)
563 .await
8aa67ee7 564 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
9bc17e8d 565
91e45873 566 let mut data: Vec<u8> = Vec::new();
91e45873 567
59477ad2
DC
568 let mut response = match compression {
569 Some(CompressionMethod::Deflate) => {
b84e8aae 570 let mut enc = DeflateEncoder::with_quality(data, Level::Default);
2d485333
TL
571 enc.compress_vec(&mut file, CHUNK_SIZE_LIMIT as usize)
572 .await?;
59477ad2
DC
573 let mut response = Response::new(enc.into_inner().into());
574 response.headers_mut().insert(
575 header::CONTENT_ENCODING,
576 CompressionMethod::Deflate.content_encoding(),
577 );
578 response
579 }
580 None => {
581 file.read_to_end(&mut data)
582 .await
583 .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
584 Response::new(data.into())
585 }
586 };
587
91e45873
WB
588 response.headers_mut().insert(
589 header::CONTENT_TYPE,
d43c407a
TL
590 header::HeaderValue::from_static(content_type),
591 );
59477ad2 592
91e45873
WB
593 Ok(response)
594}
595
59477ad2
DC
596async fn chuncked_static_file_download(
597 filename: PathBuf,
598 content_type: &'static str,
599 compression: Option<CompressionMethod>,
600) -> Result<Response<Body>, Error> {
601 let mut resp = Response::builder()
602 .status(StatusCode::OK)
603 .header(header::CONTENT_TYPE, content_type);
826bb982 604
91e45873
WB
605 let file = File::open(filename)
606 .await
8aa67ee7 607 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
91e45873 608
59477ad2
DC
609 let body = match compression {
610 Some(CompressionMethod::Deflate) => {
611 resp = resp.header(
612 header::CONTENT_ENCODING,
613 CompressionMethod::Deflate.content_encoding(),
614 );
615 Body::wrap_stream(DeflateEncoder::with_quality(
616 AsyncReaderStream::new(file),
b84e8aae 617 Level::Default,
59477ad2
DC
618 ))
619 }
620 None => Body::wrap_stream(AsyncReaderStream::new(file)),
621 };
91e45873 622
59477ad2 623 Ok(resp.body(body).unwrap())
9bc17e8d
DM
624}
625
59477ad2
DC
626async fn handle_static_file_download(
627 filename: PathBuf,
628 compression: Option<CompressionMethod>,
629) -> Result<Response<Body>, Error> {
9c18e935 630 let metadata = tokio::fs::metadata(filename.clone())
8aa67ee7 631 .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
9c18e935
TL
632 .await?;
633
59477ad2
DC
634 let (content_type, nocomp) = extension_to_content_type(&filename);
635 let compression = if nocomp { None } else { compression };
636
637 if metadata.len() < CHUNK_SIZE_LIMIT {
638 simple_static_file_download(filename, content_type, compression).await
9c18e935 639 } else {
59477ad2 640 chuncked_static_file_download(filename, content_type, compression).await
9c18e935 641 }
9bc17e8d
DM
642}
643
c30816c1 644fn extract_lang_header(headers: &http::HeaderMap) -> Option<String> {
c47609fe
TL
645 if let Some(Ok(cookie)) = headers.get("COOKIE").map(|v| v.to_str()) {
646 return tools::extract_cookie(cookie, "PBSLangCookie");
c30816c1 647 }
c30816c1
FG
648 None
649}
650
4d84e869
DC
651// FIXME: support handling multiple compression methods
652fn extract_compression_method(headers: &http::HeaderMap) -> Option<CompressionMethod> {
c47609fe
TL
653 if let Some(Ok(encodings)) = headers.get(header::ACCEPT_ENCODING).map(|v| v.to_str()) {
654 for encoding in encodings.split(&[',', ' '][..]) {
655 if let Ok(method) = encoding.parse() {
656 return Some(method);
4d84e869
DC
657 }
658 }
659 }
4d84e869
DC
660 None
661}
662
29633e2f
TL
663async fn handle_request(
664 api: Arc<ApiConfig>,
665 req: Request<Body>,
666 peer: &std::net::SocketAddr,
667) -> Result<Response<Body>, Error> {
141de837 668 let (parts, body) = req.into_parts();
141de837 669 let method = parts.method.clone();
217c22c7 670 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
141de837 671
9bc17e8d
DM
672 let comp_len = components.len();
673
4703ba81
TL
674 let query = parts.uri.query().unwrap_or_default();
675 if path.len() + query.len() > MAX_URI_QUERY_LENGTH {
676 return Ok(Response::builder()
677 .status(StatusCode::URI_TOO_LONG)
678 .body("".into())
679 .unwrap());
680 }
681
f1204833
DM
682 let env_type = api.env_type();
683 let mut rpcenv = RestEnvironment::new(env_type);
e82dad97 684
29633e2f
TL
685 rpcenv.set_client_ip(Some(*peer));
686
26858dba 687 let auth = &api.api_auth;
4b40148c 688
b9903d63 689 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
9989d2c4 690 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
b9903d63 691
576e3bf2 692 if comp_len >= 1 && components[0] == "api2" {
9bc17e8d
DM
693 if comp_len >= 2 {
694 let format = components[1];
ad51d02a 695
1571873d
DM
696 let formatter = match format {
697 "json" => &JSON_FORMATTER,
698 "extjs" => &EXTJS_FORMATTER,
d43c407a 699 _ => bail!("Unsupported output format '{}'.", format),
1571873d 700 };
9bc17e8d 701
e7ea17de 702 let mut uri_param = HashMap::new();
0ac61247 703 let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
e7ea17de 704
0ac61247
TL
705 let mut auth_required = true;
706 if let Some(api_method) = api_method {
707 if let Permission::World = *api_method.access.permission {
708 auth_required = false; // no auth for endpoints with World permission
709 }
710 }
711
712 if auth_required {
fd6d2438
DM
713 match auth.check_auth(&parts.headers, &method) {
714 Ok(authid) => rpcenv.set_auth_id(Some(authid)),
26858dba
SR
715 Err(auth_err) => {
716 let err = match auth_err {
717 AuthError::Generic(err) => err,
718 AuthError::NoData => {
719 format_err!("no authentication credentials provided.")
720 }
721 };
4fdf13f9 722 let peer = peer.ip();
d43c407a
TL
723 auth_logger()?.log(format!(
724 "authentication failure; rhost={} msg={}",
725 peer, err
726 ));
4fdf13f9 727
5ddf8cb1 728 // always delay unauthorized calls by 3 seconds (from start of request)
8aa67ee7 729 let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
0a8d773a 730 tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
ad51d02a 731 return Ok((formatter.format_error)(err));
5ddf8cb1 732 }
b9903d63
DM
733 }
734 }
d7d23785 735
0ac61247 736 match api_method {
255f378a 737 None => {
8aa67ee7 738 let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
ad51d02a 739 return Ok((formatter.format_error)(err));
49d123ee 740 }
255f378a 741 Some(api_method) => {
e6dc35ac 742 let auth_id = rpcenv.get_auth_id();
fd6d2438
DM
743 let user_info = CachedUserInfo::new()?;
744
d43c407a
TL
745 if !check_api_permission(
746 api_method.access.permission,
747 auth_id.as_deref(),
748 &uri_param,
749 user_info.as_ref(),
750 ) {
8aa67ee7 751 let err = http_err!(FORBIDDEN, "permission check failed");
0a8d773a 752 tokio::time::sleep_until(Instant::from_std(access_forbidden_time)).await;
4b40148c
DM
753 return Ok((formatter.format_error)(err));
754 }
755
4299ca72 756 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
29633e2f 757 proxy_protected_request(api_method, parts, body, peer).await
f1204833 758 } else {
d43c407a
TL
759 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param)
760 .await
4299ca72
DM
761 };
762
8e7e2223
TL
763 let mut response = match result {
764 Ok(resp) => resp,
765 Err(err) => (formatter.format_error)(err),
766 };
767
e6dc35ac
FG
768 if let Some(auth_id) = auth_id {
769 let auth_id: Authid = auth_id.parse()?;
770 response.extensions_mut().insert(auth_id);
f1204833 771 }
8e7e2223
TL
772
773 return Ok(response);
7e21da6e 774 }
9bc17e8d
DM
775 }
776 }
d43c407a 777 } else {
7f168523 778 // not Auth required for accessing files!
9bc17e8d 779
7d4ef127 780 if method != hyper::Method::GET {
ad51d02a 781 bail!("Unsupported HTTP method {}", method);
7d4ef127
DM
782 }
783
f4c514c1 784 if comp_len == 0 {
c30816c1 785 let language = extract_lang_header(&parts.headers);
fd6d2438 786 match auth.check_auth(&parts.headers, &method) {
26858dba 787 Ok(auth_id) => {
fd6d2438 788 let auth_id: Authid = auth_id.parse()?;
26858dba 789 if !auth_id.is_token() {
e6dc35ac
FG
790 let userid = auth_id.user();
791 let new_csrf_token = assemble_csrf_prevention_token(csrf_secret(), userid);
d43c407a
TL
792 return Ok(get_index(
793 Some(userid.clone()),
794 Some(new_csrf_token),
795 language,
796 &api,
797 parts,
798 ));
799 }
7f168523 800 }
26858dba
SR
801 Err(AuthError::Generic(_)) => {
802 tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
803 }
804 Err(AuthError::NoData) => {}
7f168523 805 }
26858dba 806 return Ok(get_index(None, None, language, &api, parts));
f4c514c1
DM
807 } else {
808 let filename = api.find_alias(&components);
59477ad2
DC
809 let compression = extract_compression_method(&parts.headers);
810 return handle_static_file_download(filename, compression).await;
f4c514c1 811 }
9bc17e8d
DM
812 }
813
8aa67ee7 814 Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
9bc17e8d 815}