1 use std
::collections
::HashMap
;
2 use std
::future
::Future
;
3 use std
::hash
::BuildHasher
;
4 use std
::path
::{Path, PathBuf}
;
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
::header
::{self, HeaderMap}
;
13 use hyper
::http
::request
::Parts
;
14 use hyper
::{Body, Request, Response, StatusCode}
;
15 use lazy_static
::lazy_static
;
16 use serde_json
::{json, Value}
;
18 use tokio
::time
::Instant
;
19 use url
::form_urlencoded
;
22 use proxmox
::http_err
;
32 use proxmox
::api
::schema
::{
34 parse_parameter_strings
,
39 use super::environment
::RestEnvironment
;
40 use super::formatter
::*;
43 use crate::auth_helpers
::*;
44 use crate::api2
::types
::Userid
;
46 use crate::tools
::ticket
::Ticket
;
47 use crate::config
::cached_user_info
::CachedUserInfo
;
49 extern "C" { fn tzset(); }
51 pub struct RestServer
{
52 pub api_config
: Arc
<ApiConfig
>,
55 const MAX_URI_QUERY_LENGTH
: usize = 3072;
59 pub fn new(api_config
: ApiConfig
) -> Self {
60 Self { api_config: Arc::new(api_config) }
64 impl tower_service
::Service
<&tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>> for RestServer
{
65 type Response
= ApiService
;
67 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
69 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
73 fn call(&mut self, ctx
: &tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>) -> Self::Future
{
74 match ctx
.get_ref().peer_addr() {
76 future
::err(format_err
!("unable to get peer address - {}", err
)).boxed()
79 future
::ok(ApiService { peer, api_config: self.api_config.clone() }
).boxed()
85 impl tower_service
::Service
<&tokio
::net
::TcpStream
> for RestServer
{
86 type Response
= ApiService
;
88 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
90 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
94 fn call(&mut self, ctx
: &tokio
::net
::TcpStream
) -> Self::Future
{
95 match ctx
.peer_addr() {
97 future
::err(format_err
!("unable to get peer address - {}", err
)).boxed()
100 future
::ok(ApiService { peer, api_config: self.api_config.clone() }
).boxed()
106 pub struct ApiService
{
107 pub peer
: std
::net
::SocketAddr
,
108 pub api_config
: Arc
<ApiConfig
>,
112 peer
: &std
::net
::SocketAddr
,
113 method
: hyper
::Method
,
115 resp
: &Response
<Body
>,
118 if resp
.extensions().get
::<NoLogExtension
>().is_some() { return; }
;
120 // we also log URL-to-long requests, so avoid message bigger than PIPE_BUF (4k on Linux)
121 // to profit from atomicty guarantees for O_APPEND opened logfiles
122 let path
= &path_query
[..MAX_URI_QUERY_LENGTH
.min(path_query
.len())];
124 let status
= resp
.status();
126 if !(status
.is_success() || status
.is_informational()) {
127 let reason
= status
.canonical_reason().unwrap_or("unknown reason");
129 let mut message
= "request failed";
130 if let Some(data
) = resp
.extensions().get
::<ErrorMessageExtension
>() {
134 log
::error
!("{} {}: {} {}: [client {}] {}", method
.as_str(), path
, status
.as_str(), reason
, peer
, message
);
138 fn get_proxied_peer(headers
: &HeaderMap
) -> Option
<std
::net
::SocketAddr
> {
140 static ref RE
: Regex
= Regex
::new(r
#"for="([^"]+)""#).unwrap();
142 let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
143 let capture = RE.captures(&forwarded)?;
144 let rhost = capture.get(1)?.as_str();
149 impl tower_service::Service<Request<Body>> for ApiService {
150 type Response = Response<Body>;
152 type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
154 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
158 fn call(&mut self, req: Request<Body>) -> Self::Future {
159 let path = req.uri().path_and_query().unwrap().as_str().to_owned();
160 let method = req.method().clone();
162 let config = Arc::clone(&self.api_config);
163 let peer = match get_proxied_peer(req.headers()) {
164 Some(proxied_peer) => proxied_peer,
168 let response = match handle_request(config, req, &peer).await {
169 Ok(response) => response,
171 let (err, code) = match err.downcast_ref::<HttpError>() {
172 Some(apierr) => (apierr.message.clone(), apierr.code),
173 _ => (err.to_string(), StatusCode::BAD_REQUEST),
175 Response::builder().status(code).body(err.into())?
178 log_response(&peer, method, &path, &response);
185 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
186 param_schema: &ObjectSchema,
187 form: &str, // x-www-form-urlencoded body data
189 uri_param: &HashMap<String, String, S>,
190 ) -> Result<Value, Error> {
192 let mut param_list: Vec<(String, String)> = vec![];
194 if !form.is_empty() {
195 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
196 param_list.push((k, v));
200 if let Some(query_str) = parts.uri.query() {
201 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
202 if k == "_dc" { continue; } // skip extjs "disable cache" parameter
203 param_list.push((k, v));
207 for (k, v) in uri_param {
208 param_list.push((k.clone(), v.clone()));
211 let params = parse_parameter_strings(¶m_list, param_schema, true)?;
216 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
217 param_schema: &ObjectSchema,
220 uri_param: HashMap<String, String, S>,
221 ) -> Result<Value, Error> {
223 let mut is_json = false;
225 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
226 match value.to_str().map(|v| v.split(';').next()) {
227 Ok(Some("application/x-www-form-urlencoded")) => {
230 Ok(Some("application/json")) => {
233 _ => bail!("unsupported content type {:?}", value.to_str()),
238 .map_err(|err| http_err!(BAD_REQUEST, "Promlems reading request body: {}", err))
239 .try_fold(Vec::new(), |mut acc, chunk| async move {
240 if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
241 acc.extend_from_slice(&*chunk);
244 Err(http_err!(BAD_REQUEST, "Request body too large"))
248 let utf8_data = std::str::from_utf8(&body)
249 .map_err(|err| format_err!("Request body not uft8: {}", err))?;
252 let mut params: Value = serde_json::from_str(utf8_data)?;
253 for (k, v) in uri_param {
254 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
255 params[&k] = parse_simple_value(&v, prop_schema)?;
258 verify_json_object(¶ms, param_schema)?;
261 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
265 struct NoLogExtension();
267 async fn proxy_protected_request(
268 info: &'static ApiMethod,
271 peer: &std::net::SocketAddr,
272 ) -> Result<Response<Body>, Error> {
274 let mut uri_parts = parts.uri.clone().into_parts();
276 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
277 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
278 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
282 let mut request = Request::from_parts(parts, req_body);
285 .insert(header::FORWARDED, format!("for=\"{}\";", peer).parse().unwrap());
287 let reload_timezone = info.reload_timezone;
289 let resp = hyper::client::Client::new()
291 .map_err(Error::from)
293 resp.extensions_mut().insert(NoLogExtension());
298 if reload_timezone { unsafe { tzset(); } }
303 pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
305 info: &'static ApiMethod,
306 formatter: &'static OutputFormatter,
309 uri_param: HashMap<String, String, S>,
310 ) -> Result<Response<Body>, Error> {
312 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
314 let result = match info.handler {
315 ApiHandler::AsyncHttp(handler) => {
316 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
317 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
319 ApiHandler::Sync(handler) => {
320 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
321 (handler)(params, info, &mut rpcenv)
322 .map(|data| (formatter.format_data)(data, &rpcenv))
324 ApiHandler::Async(handler) => {
325 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
326 (handler)(params, info, &mut rpcenv)
328 .map(|data| (formatter.format_data)(data, &rpcenv))
332 let resp = match result {
335 if let Some(httperr) = err.downcast_ref::<HttpError>() {
336 if httperr.code == StatusCode::UNAUTHORIZED {
337 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
340 (formatter.format_error)(err)
344 if info.reload_timezone { unsafe { tzset(); } }
350 userid: Option<Userid>,
351 csrf_token: Option<String>,
352 language: Option<String>,
353 api: &Arc<ApiConfig>,
355 ) -> Response<Body> {
357 let nodename = proxmox::tools::nodename();
358 let user = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
360 let csrf_token = csrf_token.unwrap_or_else(|| String::from(""));
362 let mut debug = false;
363 let mut template_file = "index";
365 if let Some(query_str) = parts.uri.query() {
366 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
367 if k == "debug" && v != "0" && v != "false" {
369 } else if k == "console" {
370 template_file = "console";
375 let mut lang = String::from("");
376 if let Some(language) = language {
377 if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
383 "NodeName": nodename,
385 "CSRFPreventionToken": csrf_token,
390 let (ct, index) = match api.render_template(template_file, &data) {
391 Ok(index) => ("text/html", index),
393 ("text/plain", format!("Error rendering template: {}", err))
398 .status(StatusCode::OK)
399 .header(header::CONTENT_TYPE, ct)
404 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
406 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
408 "css" => ("text/css", false),
409 "html" => ("text/html", false),
410 "js" => ("application/javascript", false),
411 "json" => ("application/json", false),
412 "map" => ("application/json", false),
413 "png" => ("image/png", true),
414 "ico" => ("image/x-icon", true),
415 "gif" => ("image/gif", true),
416 "svg" => ("image/svg+xml", false),
417 "jar" => ("application/java-archive", true),
418 "woff" => ("application/font-woff", true),
419 "woff2" => ("application/font-woff2", true),
420 "ttf" => ("application/font-snft", true),
421 "pdf" => ("application/pdf", true),
422 "epub" => ("application/epub+zip", true),
423 "mp3" => ("audio/mpeg", true),
424 "oga" => ("audio/ogg", true),
425 "tgz" => ("application/x-compressed-tar", true),
426 _ => ("application/octet-stream", false),
430 ("application/octet-stream", false)
433 async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
435 let (content_type, _nocomp) = extension_to_content_type(&filename);
437 use tokio::io::AsyncReadExt;
439 let mut file = File::open(filename)
441 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
443 let mut data: Vec<u8> = Vec::new();
444 file.read_to_end(&mut data)
446 .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
448 let mut response = Response::new(data.into());
449 response.headers_mut().insert(
450 header::CONTENT_TYPE,
451 header::HeaderValue::from_static(content_type));
455 async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
456 let (content_type, _nocomp) = extension_to_content_type(&filename);
458 let file = File::open(filename)
460 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
462 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
463 .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
464 let body = Body::wrap_stream(payload);
466 // fixme: set other headers ?
467 Ok(Response::builder()
468 .status(StatusCode::OK)
469 .header(header::CONTENT_TYPE, content_type)
475 async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
477 let metadata = tokio::fs::metadata(filename.clone())
478 .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
481 if metadata.len() < 1024*32 {
482 simple_static_file_download(filename).await
484 chuncked_static_file_download(filename).await
488 fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>, Option<String>) {
490 let mut ticket = None;
491 let mut language = None;
492 if let Some(raw_cookie) = headers.get("COOKIE") {
493 if let Ok(cookie) = raw_cookie.to_str() {
494 ticket = tools::extract_cookie(cookie, "PBSAuthCookie");
495 language = tools::extract_cookie(cookie, "PBSLangCookie");
499 let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
500 Some(Ok(v)) => Some(v.to_owned()),
504 (ticket, token, language)
508 method: &hyper::Method,
509 ticket: &Option<String>,
510 token: &Option<String>,
511 user_info: &CachedUserInfo,
512 ) -> Result<Userid, Error> {
513 let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
515 let ticket = ticket.as_ref().map(String::as_str);
516 let userid: Userid = Ticket::parse(&ticket.ok_or_else(|| format_err!("missing ticket"))?)?
517 .verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?;
519 if !user_info.is_active_user(&userid) {
520 bail!("user account disabled or expired.");
523 if method != hyper::Method::GET {
524 if let Some(token) = token {
525 verify_csrf_prevention_token(csrf_secret(), &userid, &token, -300, ticket_lifetime)?;
527 bail!("missing CSRF prevention token");
534 async fn handle_request(
537 peer: &std::net::SocketAddr,
538 ) -> Result<Response<Body>, Error> {
540 let (parts, body) = req.into_parts();
541 let method = parts.method.clone();
542 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
544 let comp_len = components.len();
546 let query = parts.uri.query().unwrap_or_default();
547 if path.len() + query.len() > MAX_URI_QUERY_LENGTH {
548 return Ok(Response::builder()
549 .status(StatusCode::URI_TOO_LONG)
554 let env_type = api.env_type();
555 let mut rpcenv = RestEnvironment::new(env_type);
557 rpcenv.set_client_ip(Some(*peer));
559 let user_info = CachedUserInfo::new()?;
561 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
562 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
564 if comp_len >= 1 && components[0] == "api2" {
568 let format = components[1];
570 let formatter = match format {
571 "json" => &JSON_FORMATTER,
572 "extjs" => &EXTJS_FORMATTER,
573 _ => bail!("Unsupported output format '{}'.", format),
576 let mut uri_param = HashMap::new();
577 let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
579 let mut auth_required = true;
580 if let Some(api_method) = api_method {
581 if let Permission::World = *api_method.access.permission {
582 auth_required = false; // no auth for endpoints with World permission
587 let (ticket, token, _) = extract_auth_data(&parts.headers);
588 match check_auth(&method, &ticket, &token, &user_info) {
589 Ok(userid) => rpcenv.set_user(Some(userid.to_string())),
591 // always delay unauthorized calls by 3 seconds (from start of request)
592 let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
593 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
594 return Ok((formatter.format_error)(err));
601 let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
602 return Ok((formatter.format_error)(err));
604 Some(api_method) => {
605 let user = rpcenv.get_user();
606 if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) {
607 let err = http_err!(FORBIDDEN, "permission check failed");
608 tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
609 return Ok((formatter.format_error)(err));
612 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
613 proxy_protected_request(api_method, parts, body, peer).await
615 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
618 if let Err(err) = result {
619 return Ok((formatter.format_error)(err));
627 // not Auth required for accessing files!
629 if method != hyper::Method::GET {
630 bail!("Unsupported HTTP method {}", method);
634 let (ticket, token, language) = extract_auth_data(&parts.headers);
636 match check_auth(&method, &ticket, &token, &user_info) {
638 let new_token = assemble_csrf_prevention_token(csrf_secret(), &userid);
639 return Ok(get_index(Some(userid), Some(new_token), language, &api, parts));
642 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
643 return Ok(get_index(None, None, language, &api, parts));
647 return Ok(get_index(None, None, language, &api, parts));
650 let filename = api.find_alias(&components);
651 return handle_static_file_download(filename).await;
655 Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))