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
::header
::{self, HeaderMap}
;
13 use hyper
::body
::HttpBody
;
14 use hyper
::http
::request
::Parts
;
15 use hyper
::{Body, Request, Response, StatusCode}
;
16 use lazy_static
::lazy_static
;
17 use serde_json
::{json, Value}
;
19 use tokio
::time
::Instant
;
20 use percent_encoding
::percent_decode_str
;
21 use url
::form_urlencoded
;
24 use proxmox
::http_err
;
34 use proxmox
::api
::schema
::{
36 parse_parameter_strings
,
41 use super::environment
::RestEnvironment
;
42 use super::formatter
::*;
45 use crate::auth_helpers
::*;
46 use crate::api2
::types
::{Authid, Userid}
;
48 use crate::tools
::FileLogger
;
49 use crate::tools
::ticket
::Ticket
;
50 use crate::config
::cached_user_info
::CachedUserInfo
;
52 extern "C" { fn tzset(); }
54 pub struct RestServer
{
55 pub api_config
: Arc
<ApiConfig
>,
58 const MAX_URI_QUERY_LENGTH
: usize = 3072;
62 pub fn new(api_config
: ApiConfig
) -> Self {
63 Self { api_config: Arc::new(api_config) }
67 impl tower_service
::Service
<&tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>> for RestServer
{
68 type Response
= ApiService
;
70 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
72 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
76 fn call(&mut self, ctx
: &tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>) -> Self::Future
{
77 match ctx
.get_ref().peer_addr() {
79 future
::err(format_err
!("unable to get peer address - {}", err
)).boxed()
82 future
::ok(ApiService { peer, api_config: self.api_config.clone() }
).boxed()
88 impl tower_service
::Service
<&tokio
::net
::TcpStream
> for RestServer
{
89 type Response
= ApiService
;
91 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
93 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
97 fn call(&mut self, ctx
: &tokio
::net
::TcpStream
) -> Self::Future
{
98 match ctx
.peer_addr() {
100 future
::err(format_err
!("unable to get peer address - {}", err
)).boxed()
103 future
::ok(ApiService { peer, api_config: self.api_config.clone() }
).boxed()
109 pub struct ApiService
{
110 pub peer
: std
::net
::SocketAddr
,
111 pub api_config
: Arc
<ApiConfig
>,
115 logfile
: Option
<&Arc
<Mutex
<FileLogger
>>>,
116 peer
: &std
::net
::SocketAddr
,
117 method
: hyper
::Method
,
119 resp
: &Response
<Body
>,
120 user_agent
: Option
<String
>,
123 if resp
.extensions().get
::<NoLogExtension
>().is_some() { return; }
;
125 // we also log URL-to-long requests, so avoid message bigger than PIPE_BUF (4k on Linux)
126 // to profit from atomicty guarantees for O_APPEND opened logfiles
127 let path
= &path_query
[..MAX_URI_QUERY_LENGTH
.min(path_query
.len())];
129 let status
= resp
.status();
131 if !(status
.is_success() || status
.is_informational()) {
132 let reason
= status
.canonical_reason().unwrap_or("unknown reason");
134 let mut message
= "request failed";
135 if let Some(data
) = resp
.extensions().get
::<ErrorMessageExtension
>() {
139 log
::error
!("{} {}: {} {}: [client {}] {}", method
.as_str(), path
, status
.as_str(), reason
, peer
, message
);
141 if let Some(logfile
) = logfile
{
142 let auth_id
= match resp
.extensions().get
::<Authid
>() {
143 Some(auth_id
) => auth_id
.to_string(),
144 None
=> "-".to_string(),
146 let now
= proxmox
::tools
::time
::epoch_i64();
147 // time format which apache/nginx use (by default), copied from pve-http-server
148 let datetime
= proxmox
::tools
::time
::strftime_local("%d/%m/%Y:%H:%M:%S %z", now
)
149 .unwrap_or("-".into());
155 "{} - {} [{}] \"{} {}\" {} {} {}",
162 resp
.body().size_hint().lower(),
163 user_agent
.unwrap_or("-".into()),
168 fn get_proxied_peer(headers
: &HeaderMap
) -> Option
<std
::net
::SocketAddr
> {
170 static ref RE
: Regex
= Regex
::new(r
#"for="([^"]+)""#).unwrap();
172 let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
173 let capture = RE.captures(&forwarded)?;
174 let rhost = capture.get(1)?.as_str();
179 fn get_user_agent(headers: &HeaderMap) -> Option<String> {
180 let agent = headers.get(header::USER_AGENT)?.to_str();
182 let mut s = s.to_owned();
188 impl tower_service::Service<Request<Body>> for ApiService {
189 type Response = Response<Body>;
191 type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
193 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
197 fn call(&mut self, req: Request<Body>) -> Self::Future {
198 let path = req.uri().path_and_query().unwrap().as_str().to_owned();
199 let method = req.method().clone();
200 let user_agent = get_user_agent(req.headers());
202 let config = Arc::clone(&self.api_config);
203 let peer = match get_proxied_peer(req.headers()) {
204 Some(proxied_peer) => proxied_peer,
208 let response = match handle_request(Arc::clone(&config), req, &peer).await {
209 Ok(response) => response,
211 let (err, code) = match err.downcast_ref::<HttpError>() {
212 Some(apierr) => (apierr.message.clone(), apierr.code),
213 _ => (err.to_string(), StatusCode::BAD_REQUEST),
215 Response::builder().status(code).body(err.into())?
218 let logger = config.get_file_log();
219 log_response(logger, &peer, method, &path, &response, user_agent);
226 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
227 param_schema: &ObjectSchema,
228 form: &str, // x-www-form-urlencoded body data
230 uri_param: &HashMap<String, String, S>,
231 ) -> Result<Value, Error> {
233 let mut param_list: Vec<(String, String)> = vec![];
235 if !form.is_empty() {
236 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
237 param_list.push((k, v));
241 if let Some(query_str) = parts.uri.query() {
242 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
243 if k == "_dc" { continue; } // skip extjs "disable cache" parameter
244 param_list.push((k, v));
248 for (k, v) in uri_param {
249 param_list.push((k.clone(), v.clone()));
252 let params = parse_parameter_strings(¶m_list, param_schema, true)?;
257 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
258 param_schema: &ObjectSchema,
261 uri_param: HashMap<String, String, S>,
262 ) -> Result<Value, Error> {
264 let mut is_json = false;
266 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
267 match value.to_str().map(|v| v.split(';').next()) {
268 Ok(Some("application/x-www-form-urlencoded")) => {
271 Ok(Some("application/json")) => {
274 _ => bail!("unsupported content type {:?}", value.to_str()),
279 .map_err(|err| http_err!(BAD_REQUEST, "Promlems reading request body: {}", err))
280 .try_fold(Vec::new(), |mut acc, chunk| async move {
281 if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
282 acc.extend_from_slice(&*chunk);
285 Err(http_err!(BAD_REQUEST, "Request body too large"))
289 let utf8_data = std::str::from_utf8(&body)
290 .map_err(|err| format_err!("Request body not uft8: {}", err))?;
293 let mut params: Value = serde_json::from_str(utf8_data)?;
294 for (k, v) in uri_param {
295 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
296 params[&k] = parse_simple_value(&v, prop_schema)?;
299 verify_json_object(¶ms, param_schema)?;
302 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
306 struct NoLogExtension();
308 async fn proxy_protected_request(
309 info: &'static ApiMethod,
312 peer: &std::net::SocketAddr,
313 ) -> Result<Response<Body>, Error> {
315 let mut uri_parts = parts.uri.clone().into_parts();
317 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
318 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
319 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
323 let mut request = Request::from_parts(parts, req_body);
326 .insert(header::FORWARDED, format!("for=\"{}\";", peer).parse().unwrap());
328 let reload_timezone = info.reload_timezone;
330 let resp = hyper::client::Client::new()
332 .map_err(Error::from)
334 resp.extensions_mut().insert(NoLogExtension());
339 if reload_timezone { unsafe { tzset(); } }
344 pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
346 info: &'static ApiMethod,
347 formatter: &'static OutputFormatter,
350 uri_param: HashMap<String, String, S>,
351 ) -> Result<Response<Body>, Error> {
353 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
355 let result = match info.handler {
356 ApiHandler::AsyncHttp(handler) => {
357 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
358 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
360 ApiHandler::Sync(handler) => {
361 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
362 (handler)(params, info, &mut rpcenv)
363 .map(|data| (formatter.format_data)(data, &rpcenv))
365 ApiHandler::Async(handler) => {
366 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
367 (handler)(params, info, &mut rpcenv)
369 .map(|data| (formatter.format_data)(data, &rpcenv))
373 let resp = match result {
376 if let Some(httperr) = err.downcast_ref::<HttpError>() {
377 if httperr.code == StatusCode::UNAUTHORIZED {
378 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
381 (formatter.format_error)(err)
385 if info.reload_timezone { unsafe { tzset(); } }
391 userid: Option<Userid>,
392 csrf_token: Option<String>,
393 language: Option<String>,
394 api: &Arc<ApiConfig>,
396 ) -> Response<Body> {
398 let nodename = proxmox::tools::nodename();
399 let user = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
401 let csrf_token = csrf_token.unwrap_or_else(|| String::from(""));
403 let mut debug = false;
404 let mut template_file = "index";
406 if let Some(query_str) = parts.uri.query() {
407 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
408 if k == "debug" && v != "0" && v != "false" {
410 } else if k == "console" {
411 template_file = "console";
416 let mut lang = String::from("");
417 if let Some(language) = language {
418 if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
424 "NodeName": nodename,
426 "CSRFPreventionToken": csrf_token,
431 let (ct, index) = match api.render_template(template_file, &data) {
432 Ok(index) => ("text/html", index),
434 ("text/plain", format!("Error rendering template: {}", err))
438 let mut resp = Response::builder()
439 .status(StatusCode::OK)
440 .header(header::CONTENT_TYPE, ct)
444 if let Some(userid) = userid {
445 resp.extensions_mut().insert(Authid::from((userid, None)));
451 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
453 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
455 "css" => ("text/css", false),
456 "html" => ("text/html", false),
457 "js" => ("application/javascript", false),
458 "json" => ("application/json", false),
459 "map" => ("application/json", false),
460 "png" => ("image/png", true),
461 "ico" => ("image/x-icon", true),
462 "gif" => ("image/gif", true),
463 "svg" => ("image/svg+xml", false),
464 "jar" => ("application/java-archive", true),
465 "woff" => ("application/font-woff", true),
466 "woff2" => ("application/font-woff2", true),
467 "ttf" => ("application/font-snft", true),
468 "pdf" => ("application/pdf", true),
469 "epub" => ("application/epub+zip", true),
470 "mp3" => ("audio/mpeg", true),
471 "oga" => ("audio/ogg", true),
472 "tgz" => ("application/x-compressed-tar", true),
473 _ => ("application/octet-stream", false),
477 ("application/octet-stream", false)
480 async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
482 let (content_type, _nocomp) = extension_to_content_type(&filename);
484 use tokio::io::AsyncReadExt;
486 let mut file = File::open(filename)
488 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
490 let mut data: Vec<u8> = Vec::new();
491 file.read_to_end(&mut data)
493 .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
495 let mut response = Response::new(data.into());
496 response.headers_mut().insert(
497 header::CONTENT_TYPE,
498 header::HeaderValue::from_static(content_type));
502 async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
503 let (content_type, _nocomp) = extension_to_content_type(&filename);
505 let file = File::open(filename)
507 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
509 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
510 .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
511 let body = Body::wrap_stream(payload);
513 // fixme: set other headers ?
514 Ok(Response::builder()
515 .status(StatusCode::OK)
516 .header(header::CONTENT_TYPE, content_type)
522 async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
524 let metadata = tokio::fs::metadata(filename.clone())
525 .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
528 if metadata.len() < 1024*32 {
529 simple_static_file_download(filename).await
531 chuncked_static_file_download(filename).await
535 fn extract_lang_header(headers: &http::HeaderMap) -> Option<String> {
536 if let Some(raw_cookie) = headers.get("COOKIE") {
537 if let Ok(cookie) = raw_cookie.to_str() {
538 return tools::extract_cookie(cookie, "PBSLangCookie");
547 csrf_token: Option<String>,
555 fn extract_auth_data(headers: &http::HeaderMap) -> Option<AuthData> {
556 if let Some(raw_cookie) = headers.get(header::COOKIE) {
557 if let Ok(cookie) = raw_cookie.to_str() {
558 if let Some(ticket) = tools::extract_cookie(cookie, "PBSAuthCookie") {
559 let csrf_token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
560 Some(Ok(v)) => Some(v.to_owned()),
563 return Some(AuthData::User(UserAuthData {
571 match headers.get(header::AUTHORIZATION).map(|v| v.to_str()) {
573 if v.starts_with("PBSAPIToken ") || v.starts_with("PBSAPIToken=") {
574 Some(AuthData::ApiToken(v["PBSAPIToken ".len()..].to_owned()))
584 method: &hyper::Method,
585 auth_data: &AuthData,
586 user_info: &CachedUserInfo,
587 ) -> Result<Authid, Error> {
589 AuthData::User(user_auth_data) => {
590 let ticket = user_auth_data.ticket.clone();
591 let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
593 let userid: Userid = Ticket::parse(&ticket)?
594 .verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?;
596 let auth_id = Authid::from(userid.clone());
597 if !user_info.is_active_auth_id(&auth_id) {
598 bail!("user account disabled or expired.");
601 if method != hyper::Method::GET {
602 if let Some(csrf_token) = &user_auth_data.csrf_token {
603 verify_csrf_prevention_token(csrf_secret(), &userid, &csrf_token, -300, ticket_lifetime)?;
605 bail!("missing CSRF prevention token");
611 AuthData::ApiToken(api_token) => {
612 let mut parts = api_token.splitn(2, ':');
613 let tokenid = parts.next()
614 .ok_or_else(|| format_err!("failed to split API token header"))?;
615 let tokenid: Authid = tokenid.parse()?;
617 let tokensecret = parts.next()
618 .ok_or_else(|| format_err!("failed to split API token header"))?;
619 let tokensecret = percent_decode_str(tokensecret)
621 .map_err(|_| format_err!("failed to decode API token header"))?;
623 crate::config::token_shadow::verify_secret(&tokenid, &tokensecret)?;
630 async fn handle_request(
633 peer: &std::net::SocketAddr,
634 ) -> Result<Response<Body>, Error> {
636 let (parts, body) = req.into_parts();
637 let method = parts.method.clone();
638 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
640 let comp_len = components.len();
642 let query = parts.uri.query().unwrap_or_default();
643 if path.len() + query.len() > MAX_URI_QUERY_LENGTH {
644 return Ok(Response::builder()
645 .status(StatusCode::URI_TOO_LONG)
650 let env_type = api.env_type();
651 let mut rpcenv = RestEnvironment::new(env_type);
653 rpcenv.set_client_ip(Some(*peer));
655 let user_info = CachedUserInfo::new()?;
657 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
658 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
660 if comp_len >= 1 && components[0] == "api2" {
664 let format = components[1];
666 let formatter = match format {
667 "json" => &JSON_FORMATTER,
668 "extjs" => &EXTJS_FORMATTER,
669 _ => bail!("Unsupported output format '{}'.", format),
672 let mut uri_param = HashMap::new();
673 let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
675 let mut auth_required = true;
676 if let Some(api_method) = api_method {
677 if let Permission::World = *api_method.access.permission {
678 auth_required = false; // no auth for endpoints with World permission
683 let auth_result = match extract_auth_data(&parts.headers) {
684 Some(auth_data) => check_auth(&method, &auth_data, &user_info),
685 None => Err(format_err!("no authentication credentials provided.")),
688 Ok(authid) => rpcenv.set_auth_id(Some(authid.to_string())),
690 // always delay unauthorized calls by 3 seconds (from start of request)
691 let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
692 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
693 return Ok((formatter.format_error)(err));
700 let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
701 return Ok((formatter.format_error)(err));
703 Some(api_method) => {
704 let auth_id = rpcenv.get_auth_id();
705 if !check_api_permission(api_method.access.permission, auth_id.as_deref(), &uri_param, user_info.as_ref()) {
706 let err = http_err!(FORBIDDEN, "permission check failed");
707 tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
708 return Ok((formatter.format_error)(err));
711 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
712 proxy_protected_request(api_method, parts, body, peer).await
714 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
717 let mut response = match result {
719 Err(err) => (formatter.format_error)(err),
722 if let Some(auth_id) = auth_id {
723 let auth_id: Authid = auth_id.parse()?;
724 response.extensions_mut().insert(auth_id);
733 // not Auth required for accessing files!
735 if method != hyper::Method::GET {
736 bail!("Unsupported HTTP method {}", method);
740 let language = extract_lang_header(&parts.headers);
741 if let Some(auth_data) = extract_auth_data(&parts.headers) {
742 match check_auth(&method, &auth_data, &user_info) {
743 Ok(auth_id) if !auth_id.is_token() => {
744 let userid = auth_id.user();
745 let new_csrf_token = assemble_csrf_prevention_token(csrf_secret(), userid);
746 return Ok(get_index(Some(userid.clone()), Some(new_csrf_token), language, &api, parts));
749 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
750 return Ok(get_index(None, None, language, &api, parts));
754 return Ok(get_index(None, None, language, &api, parts));
757 let filename = api.find_alias(&components);
758 return handle_static_file_download(filename).await;
762 Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))