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