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