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