1 use std
::collections
::HashMap
;
2 use std
::future
::Future
;
3 use std
::hash
::BuildHasher
;
4 use std
::path
::{Path, PathBuf}
;
6 use std
::sync
::{Arc, Mutex}
;
7 use std
::task
::{Context, Poll}
;
9 use anyhow
::{bail, format_err, Error}
;
10 use futures
::future
::{self, FutureExt, TryFutureExt}
;
11 use futures
::stream
::TryStreamExt
;
12 use hyper
::body
::HttpBody
;
13 use hyper
::header
::{self, HeaderMap}
;
14 use hyper
::http
::request
::Parts
;
15 use hyper
::{Body, Request, Response, StatusCode}
;
16 use lazy_static
::lazy_static
;
18 use serde_json
::Value
;
20 use tokio
::time
::Instant
;
21 use url
::form_urlencoded
;
22 use tower_service
::Service
;
25 check_api_permission
, ApiHandler
, ApiMethod
, HttpError
, Permission
, RpcEnvironment
,
26 RpcEnvironmentType
, UserInformation
,
28 use proxmox_router
::http_err
;
29 use proxmox_schema
::{ObjectSchemaType, ParameterSchema}
;
31 use proxmox_http
::client
::RateLimitedStream
;
33 use proxmox_async
::compression
::{DeflateEncoder, Level}
;
34 use proxmox_async
::stream
::AsyncReaderStream
;
37 ApiConfig
, FileLogger
, AuthError
, RestEnvironment
, CompressionMethod
,
38 normalize_uri_path
, formatter
::*,
45 struct AuthStringExtension(String
);
47 struct EmptyUserInformation {}
49 impl UserInformation
for EmptyUserInformation
{
50 fn is_superuser(&self, _userid
: &str) -> bool { false }
51 fn is_group_member(&self, _userid
: &str, _group
: &str) -> bool { false }
52 fn lookup_privs(&self, _userid
: &str, _path
: &[&str]) -> u64 { 0 }
55 /// REST server implementation (configured with [ApiConfig])
57 /// This struct implements the [Service] trait in order to use it with
58 /// [hyper::server::Builder::serve].
59 pub struct RestServer
{
60 api_config
: Arc
<ApiConfig
>,
63 const MAX_URI_QUERY_LENGTH
: usize = 3072;
64 const CHUNK_SIZE_LIMIT
: u64 = 32 * 1024;
67 /// Creates a new instance.
68 pub fn new(api_config
: ApiConfig
) -> Self {
70 api_config
: Arc
::new(api_config
),
75 impl Service
<&Pin
<Box
<tokio_openssl
::SslStream
<RateLimitedStream
<tokio
::net
::TcpStream
>>>>>
78 type Response
= ApiService
;
80 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
82 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
88 ctx
: &Pin
<Box
<tokio_openssl
::SslStream
<RateLimitedStream
<tokio
::net
::TcpStream
>>>>,
90 match ctx
.get_ref().peer_addr() {
91 Err(err
) => future
::err(format_err
!("unable to get peer address - {}", err
)).boxed(),
92 Ok(peer
) => future
::ok(ApiService
{
94 api_config
: self.api_config
.clone(),
101 impl Service
<&Pin
<Box
<tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>>>>
104 type Response
= ApiService
;
106 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
108 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
114 ctx
: &Pin
<Box
<tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>>>,
116 match ctx
.get_ref().peer_addr() {
117 Err(err
) => future
::err(format_err
!("unable to get peer address - {}", err
)).boxed(),
118 Ok(peer
) => future
::ok(ApiService
{
120 api_config
: self.api_config
.clone(),
127 impl Service
<&hyper
::server
::conn
::AddrStream
> for RestServer
{
128 type Response
= ApiService
;
130 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
132 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
136 fn call(&mut self, ctx
: &hyper
::server
::conn
::AddrStream
) -> Self::Future
{
137 let peer
= ctx
.remote_addr();
138 future
::ok(ApiService
{
140 api_config
: self.api_config
.clone(),
146 impl Service
<&tokio
::net
::UnixStream
> for RestServer
{
147 type Response
= ApiService
;
149 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
151 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
155 fn call(&mut self, _ctx
: &tokio
::net
::UnixStream
) -> Self::Future
{
156 // TODO: Find a way to actually represent the vsock peer in the ApiService struct - for now
157 // it doesn't really matter, so just use a fake IP address
158 let fake_peer
= "0.0.0.0:807".parse().unwrap();
159 future
::ok(ApiService
{
161 api_config
: self.api_config
.clone(),
167 // Helper [Service] containing the peer Address
169 // The lower level connection [Service] implementation on
170 // [RestServer] extracts the peer address and return an [ApiService].
172 // Rust wants this type 'pub' here (else we get 'private type `ApiService`
173 // in public interface'). The type is still private because the crate does
175 pub struct ApiService
{
176 pub peer
: std
::net
::SocketAddr
,
177 pub api_config
: Arc
<ApiConfig
>,
181 logfile
: Option
<&Arc
<Mutex
<FileLogger
>>>,
182 peer
: &std
::net
::SocketAddr
,
183 method
: hyper
::Method
,
185 resp
: &Response
<Body
>,
186 user_agent
: Option
<String
>,
188 if resp
.extensions().get
::<NoLogExtension
>().is_some() {
192 // we also log URL-to-long requests, so avoid message bigger than PIPE_BUF (4k on Linux)
193 // to profit from atomicty guarantees for O_APPEND opened logfiles
194 let path
= &path_query
[..MAX_URI_QUERY_LENGTH
.min(path_query
.len())];
196 let status
= resp
.status();
197 if !(status
.is_success() || status
.is_informational()) {
198 let reason
= status
.canonical_reason().unwrap_or("unknown reason");
200 let message
= match resp
.extensions().get
::<ErrorMessageExtension
>() {
201 Some(data
) => &data
.0,
202 None
=> "request failed",
206 "{} {}: {} {}: [client {}] {}",
215 if let Some(logfile
) = logfile
{
216 let auth_id
= match resp
.extensions().get
::<AuthStringExtension
>() {
217 Some(AuthStringExtension(auth_id
)) => auth_id
.clone(),
218 None
=> "-".to_string(),
220 let now
= proxmox_time
::epoch_i64();
221 // time format which apache/nginx use (by default), copied from pve-http-server
222 let datetime
= proxmox_time
::strftime_local("%d/%m/%Y:%H:%M:%S %z", now
)
223 .unwrap_or_else(|_
| "-".to_string());
225 logfile
.lock().unwrap().log(format
!(
226 "{} - {} [{}] \"{} {}\" {} {} {}",
233 resp
.body().size_hint().lower(),
234 user_agent
.unwrap_or_else(|| "-".to_string()),
239 fn get_proxied_peer(headers
: &HeaderMap
) -> Option
<std
::net
::SocketAddr
> {
241 static ref RE
: Regex
= Regex
::new(r
#"for="([^"]+)""#).unwrap();
243 let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
244 let capture = RE.captures(forwarded)?;
245 let rhost = capture.get(1)?.as_str();
250 fn get_user_agent(headers: &HeaderMap) -> Option<String> {
251 let agent = headers.get(header::USER_AGENT)?.to_str();
254 let mut s = s.to_owned();
261 impl Service<Request<Body>> for ApiService {
262 type Response = Response<Body>;
264 #[allow(clippy::type_complexity)]
265 type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
267 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
271 fn call(&mut self, req: Request<Body>) -> Self::Future {
272 let path = req.uri().path_and_query().unwrap().as_str().to_owned();
273 let method = req.method().clone();
274 let user_agent = get_user_agent(req.headers());
276 let config = Arc::clone(&self.api_config);
277 let peer = match get_proxied_peer(req.headers()) {
278 Some(proxied_peer) => proxied_peer,
282 let response = match handle_request(Arc::clone(&config), req, &peer).await {
283 Ok(response) => response,
285 let (err, code) = match err.downcast_ref::<HttpError>() {
286 Some(apierr) => (apierr.message.clone(), apierr.code),
287 _ => (err.to_string(), StatusCode::BAD_REQUEST),
291 .extension(ErrorMessageExtension(err.to_string()))
295 let logger = config.get_access_log();
296 log_response(logger, &peer, method, &path, &response, user_agent);
303 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
304 param_schema: ParameterSchema,
305 form: &str, // x-www-form-urlencoded body data
307 uri_param: &HashMap<String, String, S>,
308 ) -> Result<Value, Error> {
309 let mut param_list: Vec<(String, String)> = vec![];
311 if !form.is_empty() {
312 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
313 param_list.push((k, v));
317 if let Some(query_str) = parts.uri.query() {
318 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
321 } // skip extjs "disable cache" parameter
322 param_list.push((k, v));
326 for (k, v) in uri_param {
327 param_list.push((k.clone(), v.clone()));
330 let params = param_schema.parse_parameter_strings(¶m_list, true)?;
335 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
336 param_schema: ParameterSchema,
339 uri_param: HashMap<String, String, S>,
340 ) -> Result<Value, Error> {
341 let mut is_json = false;
343 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
344 match value.to_str().map(|v| v.split(';').next()) {
345 Ok(Some("application/x-www-form-urlencoded")) => {
348 Ok(Some("application/json")) => {
351 _ => bail!("unsupported content type {:?}", value.to_str()),
355 let body = TryStreamExt::map_err(req_body, |err| {
356 http_err!(BAD_REQUEST, "Problems reading request body: {}", err)
358 .try_fold(Vec::new(), |mut acc, chunk| async move {
359 // FIXME: max request body size?
360 if acc.len() + chunk.len() < 64 * 1024 {
361 acc.extend_from_slice(&*chunk);
364 Err(http_err!(BAD_REQUEST, "Request body too large"))
370 std::str::from_utf8(&body).map_err(|err| format_err!("Request body not uft8: {}", err))?;
373 let mut params: Value = serde_json::from_str(utf8_data)?;
374 for (k, v) in uri_param {
375 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
376 params[&k] = prop_schema.parse_simple_value(&v)?;
379 param_schema.verify_json(¶ms)?;
382 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
386 struct NoLogExtension();
388 async fn proxy_protected_request(
389 info: &'static ApiMethod,
392 peer: &std::net::SocketAddr,
393 ) -> Result<Response<Body>, Error> {
394 let mut uri_parts = parts.uri.clone().into_parts();
396 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
397 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
398 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
402 let mut request = Request::from_parts(parts, req_body);
403 request.headers_mut().insert(
405 format!("for=\"{}\";", peer).parse().unwrap(),
408 let reload_timezone = info.reload_timezone;
410 let resp = hyper::client::Client::new()
412 .map_err(Error::from)
414 resp.extensions_mut().insert(NoLogExtension());
428 pub(crate) async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
430 info: &'static ApiMethod,
431 formatter: &'static dyn OutputFormatter,
434 uri_param: HashMap<String, String, S>,
435 ) -> Result<Response<Body>, Error> {
436 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
437 let compression = extract_compression_method(&parts.headers);
439 let result = match info.handler {
440 ApiHandler::AsyncHttp(handler) => {
441 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
442 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
444 ApiHandler::Sync(handler) => {
446 get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
447 (handler)(params, info, &mut rpcenv).map(|data| formatter.format_data(data, &rpcenv))
449 ApiHandler::Async(handler) => {
451 get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
452 (handler)(params, info, &mut rpcenv)
454 .map(|data| formatter.format_data(data, &rpcenv))
458 let mut resp = match result {
461 if let Some(httperr) = err.downcast_ref::<HttpError>() {
462 if httperr.code == StatusCode::UNAUTHORIZED {
463 tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
466 formatter.format_error(err)
470 let resp = match compression {
471 Some(CompressionMethod::Deflate) => {
472 resp.headers_mut().insert(
473 header::CONTENT_ENCODING,
474 CompressionMethod::Deflate.content_encoding(),
477 Body::wrap_stream(DeflateEncoder::with_quality(
478 TryStreamExt::map_err(body, |err| {
479 proxmox_sys::io_format_err!("error during compression: {}", err)
488 if info.reload_timezone {
498 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
499 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
501 "css" => ("text/css", false),
502 "html" => ("text/html", false),
503 "js" => ("application/javascript", false),
504 "json" => ("application/json", false),
505 "map" => ("application/json", false),
506 "png" => ("image/png", true),
507 "ico" => ("image/x-icon", true),
508 "gif" => ("image/gif", true),
509 "svg" => ("image/svg+xml", false),
510 "jar" => ("application/java-archive", true),
511 "woff" => ("application/font-woff", true),
512 "woff2" => ("application/font-woff2", true),
513 "ttf" => ("application/font-snft", true),
514 "pdf" => ("application/pdf", true),
515 "epub" => ("application/epub+zip", true),
516 "mp3" => ("audio/mpeg", true),
517 "oga" => ("audio/ogg", true),
518 "tgz" => ("application/x-compressed-tar", true),
519 _ => ("application/octet-stream", false),
523 ("application/octet-stream", false)
526 async fn simple_static_file_download(
528 content_type: &'static str,
529 compression: Option<CompressionMethod>,
530 ) -> Result<Response<Body>, Error> {
531 use tokio::io::AsyncReadExt;
533 let mut file = File::open(filename)
535 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
537 let mut data: Vec<u8> = Vec::new();
539 let mut response = match compression {
540 Some(CompressionMethod::Deflate) => {
541 let mut enc = DeflateEncoder::with_quality(data, Level::Default);
542 enc.compress_vec(&mut file, CHUNK_SIZE_LIMIT as usize)
544 let mut response = Response::new(enc.into_inner().into());
545 response.headers_mut().insert(
546 header::CONTENT_ENCODING,
547 CompressionMethod::Deflate.content_encoding(),
552 file.read_to_end(&mut data)
554 .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
555 Response::new(data.into())
559 response.headers_mut().insert(
560 header::CONTENT_TYPE,
561 header::HeaderValue::from_static(content_type),
567 async fn chuncked_static_file_download(
569 content_type: &'static str,
570 compression: Option<CompressionMethod>,
571 ) -> Result<Response<Body>, Error> {
572 let mut resp = Response::builder()
573 .status(StatusCode::OK)
574 .header(header::CONTENT_TYPE, content_type);
576 let file = File::open(filename)
578 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
580 let body = match compression {
581 Some(CompressionMethod::Deflate) => {
583 header::CONTENT_ENCODING,
584 CompressionMethod::Deflate.content_encoding(),
586 Body::wrap_stream(DeflateEncoder::with_quality(
587 AsyncReaderStream::new(file),
591 None => Body::wrap_stream(AsyncReaderStream::new(file)),
594 Ok(resp.body(body).unwrap())
597 async fn handle_static_file_download(
599 compression: Option<CompressionMethod>,
600 ) -> Result<Response<Body>, Error> {
601 let metadata = tokio::fs::metadata(filename.clone())
602 .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
605 let (content_type, nocomp) = extension_to_content_type(&filename);
606 let compression = if nocomp { None } else { compression };
608 if metadata.len() < CHUNK_SIZE_LIMIT {
609 simple_static_file_download(filename, content_type, compression).await
611 chuncked_static_file_download(filename, content_type, compression).await
615 // FIXME: support handling multiple compression methods
616 fn extract_compression_method(headers: &http::HeaderMap) -> Option<CompressionMethod> {
617 if let Some(Ok(encodings)) = headers.get(header::ACCEPT_ENCODING).map(|v| v.to_str()) {
618 for encoding in encodings.split(&[',', ' '][..]) {
619 if let Ok(method) = encoding.parse() {
627 async fn handle_request(
630 peer: &std::net::SocketAddr,
631 ) -> Result<Response<Body>, Error> {
632 let (parts, body) = req.into_parts();
633 let method = parts.method.clone();
634 let (path, components) = normalize_uri_path(parts.uri.path())?;
636 let comp_len = components.len();
638 let query = parts.uri.query().unwrap_or_default();
639 if path.len() + query.len() > MAX_URI_QUERY_LENGTH {
640 return Ok(Response::builder()
641 .status(StatusCode::URI_TOO_LONG)
646 let env_type = api.env_type();
647 let mut rpcenv = RestEnvironment::new(env_type, Arc::clone(&api));
649 rpcenv.set_client_ip(Some(*peer));
651 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
652 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
654 if comp_len >= 1 && components[0] == "api2" {
656 let format = components[1];
658 let formatter: &dyn OutputFormatter = match format {
659 "json" => JSON_FORMATTER,
660 "extjs" => EXTJS_FORMATTER,
661 _ => bail!("Unsupported output format '{}'.", format),
664 let mut uri_param = HashMap::new();
665 let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
667 let mut auth_required = true;
668 if let Some(api_method) = api_method {
669 if let Permission::World = *api_method.access.permission {
670 auth_required = false; // no auth for endpoints with World permission
674 let mut user_info: Box<dyn UserInformation + Send + Sync> = Box::new(EmptyUserInformation {});
677 match api.check_auth(&parts.headers, &method).await {
678 Ok((authid, info)) => {
679 rpcenv.set_auth_id(Some(authid));
683 let err = match auth_err {
684 AuthError::Generic(err) => err,
685 AuthError::NoData => {
686 format_err!("no authentication credentials provided.")
689 // fixme: log Username??
690 rpcenv.log_failed_auth(None, &err.to_string());
692 // always delay unauthorized calls by 3 seconds (from start of request)
693 let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
694 tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
695 return Ok(formatter.format_error(err));
702 let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
703 return Ok(formatter.format_error(err));
705 Some(api_method) => {
706 let auth_id = rpcenv.get_auth_id();
707 let user_info = user_info;
709 if !check_api_permission(
710 api_method.access.permission,
715 let err = http_err!(FORBIDDEN, "permission check failed");
716 tokio::time::sleep_until(Instant::from_std(access_forbidden_time)).await;
717 return Ok(formatter.format_error(err));
720 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
721 proxy_protected_request(api_method, parts, body, peer).await
723 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param)
727 let mut response = match result {
729 Err(err) => formatter.format_error(err),
732 if let Some(auth_id) = auth_id {
733 response.extensions_mut().insert(AuthStringExtension(auth_id));
741 // not Auth required for accessing files!
743 if method != hyper::Method::GET {
744 bail!("Unsupported HTTP method {}", method);
748 match api.check_auth(&parts.headers, &method).await {
749 Ok((auth_id, _user_info)) => {
750 rpcenv.set_auth_id(Some(auth_id));
751 return Ok(api.get_index(rpcenv, parts).await);
753 Err(AuthError::Generic(_)) => {
754 tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
756 Err(AuthError::NoData) => {}
758 return Ok(api.get_index(rpcenv, parts).await);
760 let filename = api.find_alias(&components);
761 let compression = extract_compression_method(&parts.headers);
762 return handle_static_file_download(filename, compression).await;
766 Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))