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
;
13 use hyper
::http
::request
::Parts
;
14 use hyper
::{Body, Request, Response, StatusCode}
;
15 use serde_json
::{json, Value}
;
17 use tokio
::time
::Instant
;
18 use url
::form_urlencoded
;
20 use proxmox
::http_err
;
21 use proxmox
::api
::{ApiHandler, ApiMethod, HttpError}
;
22 use proxmox
::api
::{RpcEnvironment, RpcEnvironmentType, check_api_permission}
;
23 use proxmox
::api
::schema
::{ObjectSchema, parse_simple_value, verify_json_object, parse_parameter_strings}
;
25 use super::environment
::RestEnvironment
;
26 use super::formatter
::*;
29 use crate::auth_helpers
::*;
30 use crate::api2
::types
::Userid
;
32 use crate::tools
::ticket
::Ticket
;
33 use crate::config
::cached_user_info
::CachedUserInfo
;
35 extern "C" { fn tzset(); }
37 pub struct RestServer
{
38 pub api_config
: Arc
<ApiConfig
>,
43 pub fn new(api_config
: ApiConfig
) -> Self {
44 Self { api_config: Arc::new(api_config) }
48 impl tower_service
::Service
<&tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>> for RestServer
{
49 type Response
= ApiService
;
51 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
53 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
57 fn call(&mut self, ctx
: &tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>) -> Self::Future
{
58 match ctx
.get_ref().peer_addr() {
60 future
::err(format_err
!("unable to get peer address - {}", err
)).boxed()
63 future
::ok(ApiService { peer, api_config: self.api_config.clone() }
).boxed()
69 impl tower_service
::Service
<&tokio
::net
::TcpStream
> for RestServer
{
70 type Response
= ApiService
;
72 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<ApiService
, Error
>> + Send
>>;
74 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
78 fn call(&mut self, ctx
: &tokio
::net
::TcpStream
) -> Self::Future
{
79 match ctx
.peer_addr() {
81 future
::err(format_err
!("unable to get peer address - {}", err
)).boxed()
84 future
::ok(ApiService { peer, api_config: self.api_config.clone() }
).boxed()
90 pub struct ApiService
{
91 pub peer
: std
::net
::SocketAddr
,
92 pub api_config
: Arc
<ApiConfig
>,
96 peer
: &std
::net
::SocketAddr
,
97 method
: hyper
::Method
,
99 resp
: &Response
<Body
>,
102 if resp
.extensions().get
::<NoLogExtension
>().is_some() { return; }
;
104 let status
= resp
.status();
106 if !(status
.is_success() || status
.is_informational()) {
107 let reason
= status
.canonical_reason().unwrap_or("unknown reason");
109 let mut message
= "request failed";
110 if let Some(data
) = resp
.extensions().get
::<ErrorMessageExtension
>() {
114 log
::error
!("{} {}: {} {}: [client {}] {}", method
.as_str(), path
, status
.as_str(), reason
, peer
, message
);
118 impl tower_service
::Service
<Request
<Body
>> for ApiService
{
119 type Response
= Response
<Body
>;
121 type Future
= Pin
<Box
<dyn Future
<Output
= Result
<Response
<Body
>, Self::Error
>> + Send
>>;
123 fn poll_ready(&mut self, _cx
: &mut Context
) -> Poll
<Result
<(), Self::Error
>> {
127 fn call(&mut self, req
: Request
<Body
>) -> Self::Future
{
128 let path
= req
.uri().path().to_owned();
129 let method
= req
.method().clone();
131 let peer
= self.peer
;
132 handle_request(self.api_config
.clone(), req
)
133 .map(move |result
| match result
{
135 log_response(&peer
, method
, &path
, &res
);
136 Ok
::<_
, Self::Error
>(res
)
139 if let Some(apierr
) = err
.downcast_ref
::<HttpError
>() {
140 let mut resp
= Response
::new(Body
::from(apierr
.message
.clone()));
141 *resp
.status_mut() = apierr
.code
;
142 log_response(&peer
, method
, &path
, &resp
);
145 let mut resp
= Response
::new(Body
::from(err
.to_string()));
146 *resp
.status_mut() = StatusCode
::BAD_REQUEST
;
147 log_response(&peer
, method
, &path
, &resp
);
156 fn parse_query_parameters
<S
: '
static + BuildHasher
+ Send
>(
157 param_schema
: &ObjectSchema
,
158 form
: &str, // x-www-form-urlencoded body data
160 uri_param
: &HashMap
<String
, String
, S
>,
161 ) -> Result
<Value
, Error
> {
163 let mut param_list
: Vec
<(String
, String
)> = vec
![];
165 if !form
.is_empty() {
166 for (k
, v
) in form_urlencoded
::parse(form
.as_bytes()).into_owned() {
167 param_list
.push((k
, v
));
171 if let Some(query_str
) = parts
.uri
.query() {
172 for (k
, v
) in form_urlencoded
::parse(query_str
.as_bytes()).into_owned() {
173 if k
== "_dc" { continue; }
// skip extjs "disable cache" parameter
174 param_list
.push((k
, v
));
178 for (k
, v
) in uri_param
{
179 param_list
.push((k
.clone(), v
.clone()));
182 let params
= parse_parameter_strings(¶m_list
, param_schema
, true)?
;
187 async
fn get_request_parameters
<S
: '
static + BuildHasher
+ Send
>(
188 param_schema
: &ObjectSchema
,
191 uri_param
: HashMap
<String
, String
, S
>,
192 ) -> Result
<Value
, Error
> {
194 let mut is_json
= false;
196 if let Some(value
) = parts
.headers
.get(header
::CONTENT_TYPE
) {
197 match value
.to_str().map(|v
| v
.split('
;'
).next()) {
198 Ok(Some("application/x-www-form-urlencoded")) => {
201 Ok(Some("application/json")) => {
204 _
=> bail
!("unsupported content type {:?}", value
.to_str()),
209 .map_err(|err
| http_err
!(BAD_REQUEST
, "Promlems reading request body: {}", err
))
210 .try_fold(Vec
::new(), |mut acc
, chunk
| async
move {
211 if acc
.len() + chunk
.len() < 64*1024 { //fimxe: max request body size?
212 acc
.extend_from_slice(&*chunk
);
215 Err(http_err
!(BAD_REQUEST
, "Request body too large"))
219 let utf8_data
= std
::str::from_utf8(&body
)
220 .map_err(|err
| format_err
!("Request body not uft8: {}", err
))?
;
223 let mut params
: Value
= serde_json
::from_str(utf8_data
)?
;
224 for (k
, v
) in uri_param
{
225 if let Some((_optional
, prop_schema
)) = param_schema
.lookup(&k
) {
226 params
[&k
] = parse_simple_value(&v
, prop_schema
)?
;
229 verify_json_object(¶ms
, param_schema
)?
;
232 parse_query_parameters(param_schema
, utf8_data
, &parts
, &uri_param
)
236 struct NoLogExtension();
238 async
fn proxy_protected_request(
239 info
: &'
static ApiMethod
,
242 ) -> Result
<Response
<Body
>, Error
> {
244 let mut uri_parts
= parts
.uri
.clone().into_parts();
246 uri_parts
.scheme
= Some(http
::uri
::Scheme
::HTTP
);
247 uri_parts
.authority
= Some(http
::uri
::Authority
::from_static("127.0.0.1:82"));
248 let new_uri
= http
::Uri
::from_parts(uri_parts
).unwrap();
252 let request
= Request
::from_parts(parts
, req_body
);
254 let reload_timezone
= info
.reload_timezone
;
256 let resp
= hyper
::client
::Client
::new()
258 .map_err(Error
::from
)
260 resp
.extensions_mut().insert(NoLogExtension());
265 if reload_timezone { unsafe { tzset(); }
}
270 pub async
fn handle_api_request
<Env
: RpcEnvironment
, S
: '
static + BuildHasher
+ Send
>(
272 info
: &'
static ApiMethod
,
273 formatter
: &'
static OutputFormatter
,
276 uri_param
: HashMap
<String
, String
, S
>,
277 ) -> Result
<Response
<Body
>, Error
> {
279 let delay_unauth_time
= std
::time
::Instant
::now() + std
::time
::Duration
::from_millis(3000);
281 let result
= match info
.handler
{
282 ApiHandler
::AsyncHttp(handler
) => {
283 let params
= parse_query_parameters(info
.parameters
, "", &parts
, &uri_param
)?
;
284 (handler
)(parts
, req_body
, params
, info
, Box
::new(rpcenv
)).await
286 ApiHandler
::Sync(handler
) => {
287 let params
= get_request_parameters(info
.parameters
, parts
, req_body
, uri_param
).await?
;
288 (handler
)(params
, info
, &mut rpcenv
)
289 .map(|data
| (formatter
.format_data
)(data
, &rpcenv
))
291 ApiHandler
::Async(handler
) => {
292 let params
= get_request_parameters(info
.parameters
, parts
, req_body
, uri_param
).await?
;
293 (handler
)(params
, info
, &mut rpcenv
)
295 .map(|data
| (formatter
.format_data
)(data
, &rpcenv
))
299 let resp
= match result
{
302 if let Some(httperr
) = err
.downcast_ref
::<HttpError
>() {
303 if httperr
.code
== StatusCode
::UNAUTHORIZED
{
304 tokio
::time
::delay_until(Instant
::from_std(delay_unauth_time
)).await
;
307 (formatter
.format_error
)(err
)
311 if info
.reload_timezone { unsafe { tzset(); }
}
317 userid
: Option
<Userid
>,
318 token
: Option
<String
>,
319 language
: Option
<String
>,
320 api
: &Arc
<ApiConfig
>,
322 ) -> Response
<Body
> {
324 let nodename
= proxmox
::tools
::nodename();
325 let userid
= userid
.as_ref().map(|u
| u
.as_str()).unwrap_or("");
327 let token
= token
.unwrap_or_else(|| String
::from(""));
329 let mut debug
= false;
330 let mut template_file
= "index";
332 if let Some(query_str
) = parts
.uri
.query() {
333 for (k
, v
) in form_urlencoded
::parse(query_str
.as_bytes()).into_owned() {
334 if k
== "debug" && v
!= "0" && v
!= "false" {
336 } else if k
== "console" {
337 template_file
= "console";
342 let mut lang
= String
::from("");
343 if let Some(language
) = language
{
344 if Path
::new(&format
!("/usr/share/pbs-i18n/pbs-lang-{}.js", language
)).exists() {
350 "NodeName": nodename
,
352 "CSRFPreventionToken": token
,
357 let mut ct
= "text/html";
359 let index
= match api
.render_template(template_file
, &data
) {
363 format
!("Error rendering template: {}", err
)
368 .status(StatusCode
::OK
)
369 .header(header
::CONTENT_TYPE
, ct
)
374 fn extension_to_content_type(filename
: &Path
) -> (&'
static str, bool
) {
376 if let Some(ext
) = filename
.extension().and_then(|osstr
| osstr
.to_str()) {
378 "css" => ("text/css", false),
379 "html" => ("text/html", false),
380 "js" => ("application/javascript", false),
381 "json" => ("application/json", false),
382 "map" => ("application/json", false),
383 "png" => ("image/png", true),
384 "ico" => ("image/x-icon", true),
385 "gif" => ("image/gif", true),
386 "svg" => ("image/svg+xml", false),
387 "jar" => ("application/java-archive", true),
388 "woff" => ("application/font-woff", true),
389 "woff2" => ("application/font-woff2", true),
390 "ttf" => ("application/font-snft", true),
391 "pdf" => ("application/pdf", true),
392 "epub" => ("application/epub+zip", true),
393 "mp3" => ("audio/mpeg", true),
394 "oga" => ("audio/ogg", true),
395 "tgz" => ("application/x-compressed-tar", true),
396 _
=> ("application/octet-stream", false),
400 ("application/octet-stream", false)
403 async
fn simple_static_file_download(filename
: PathBuf
) -> Result
<Response
<Body
>, Error
> {
405 let (content_type
, _nocomp
) = extension_to_content_type(&filename
);
407 use tokio
::io
::AsyncReadExt
;
409 let mut file
= File
::open(filename
)
411 .map_err(|err
| http_err
!(BAD_REQUEST
, "File open failed: {}", err
))?
;
413 let mut data
: Vec
<u8> = Vec
::new();
414 file
.read_to_end(&mut data
)
416 .map_err(|err
| http_err
!(BAD_REQUEST
, "File read failed: {}", err
))?
;
418 let mut response
= Response
::new(data
.into());
419 response
.headers_mut().insert(
420 header
::CONTENT_TYPE
,
421 header
::HeaderValue
::from_static(content_type
));
425 async
fn chuncked_static_file_download(filename
: PathBuf
) -> Result
<Response
<Body
>, Error
> {
426 let (content_type
, _nocomp
) = extension_to_content_type(&filename
);
428 let file
= File
::open(filename
)
430 .map_err(|err
| http_err
!(BAD_REQUEST
, "File open failed: {}", err
))?
;
432 let payload
= tokio_util
::codec
::FramedRead
::new(file
, tokio_util
::codec
::BytesCodec
::new())
433 .map_ok(|bytes
| hyper
::body
::Bytes
::from(bytes
.freeze()));
434 let body
= Body
::wrap_stream(payload
);
436 // fixme: set other headers ?
437 Ok(Response
::builder()
438 .status(StatusCode
::OK
)
439 .header(header
::CONTENT_TYPE
, content_type
)
445 async
fn handle_static_file_download(filename
: PathBuf
) -> Result
<Response
<Body
>, Error
> {
447 let metadata
= tokio
::fs
::metadata(filename
.clone())
448 .map_err(|err
| http_err
!(BAD_REQUEST
, "File access problems: {}", err
))
451 if metadata
.len() < 1024*32 {
452 simple_static_file_download(filename
).await
454 chuncked_static_file_download(filename
).await
458 fn extract_auth_data(headers
: &http
::HeaderMap
) -> (Option
<String
>, Option
<String
>, Option
<String
>) {
460 let mut ticket
= None
;
461 let mut language
= None
;
462 if let Some(raw_cookie
) = headers
.get("COOKIE") {
463 if let Ok(cookie
) = raw_cookie
.to_str() {
464 ticket
= tools
::extract_cookie(cookie
, "PBSAuthCookie");
465 language
= tools
::extract_cookie(cookie
, "PBSLangCookie");
469 let token
= match headers
.get("CSRFPreventionToken").map(|v
| v
.to_str()) {
470 Some(Ok(v
)) => Some(v
.to_owned()),
474 (ticket
, token
, language
)
478 method
: &hyper
::Method
,
479 ticket
: &Option
<String
>,
480 token
: &Option
<String
>,
481 user_info
: &CachedUserInfo
,
482 ) -> Result
<Userid
, Error
> {
483 let ticket_lifetime
= tools
::ticket
::TICKET_LIFETIME
;
485 let ticket
= ticket
.as_ref().map(String
::as_str
);
486 let userid
: Userid
= Ticket
::parse(&ticket
.ok_or_else(|| format_err
!("missing ticket"))?
)?
487 .verify_with_time_frame(public_auth_key(), "PBS", None
, -300..ticket_lifetime
)?
;
489 if !user_info
.is_active_user(&userid
) {
490 bail
!("user account disabled or expired.");
493 if method
!= hyper
::Method
::GET
{
494 if let Some(token
) = token
{
495 println
!("CSRF prevention token: {:?}", token
);
496 verify_csrf_prevention_token(csrf_secret(), &userid
, &token
, -300, ticket_lifetime
)?
;
498 bail
!("missing CSRF prevention token");
505 pub async
fn handle_request(api
: Arc
<ApiConfig
>, req
: Request
<Body
>) -> Result
<Response
<Body
>, Error
> {
507 let (parts
, body
) = req
.into_parts();
509 let method
= parts
.method
.clone();
510 let (path
, components
) = tools
::normalize_uri_path(parts
.uri
.path())?
;
512 let comp_len
= components
.len();
514 //println!("REQUEST {} {}", method, path);
515 //println!("COMPO {:?}", components);
517 let env_type
= api
.env_type();
518 let mut rpcenv
= RestEnvironment
::new(env_type
);
520 let user_info
= CachedUserInfo
::new()?
;
522 let delay_unauth_time
= std
::time
::Instant
::now() + std
::time
::Duration
::from_millis(3000);
523 let access_forbidden_time
= std
::time
::Instant
::now() + std
::time
::Duration
::from_millis(500);
525 if comp_len
>= 1 && components
[0] == "api2" {
529 let format
= components
[1];
531 let formatter
= match format
{
532 "json" => &JSON_FORMATTER
,
533 "extjs" => &EXTJS_FORMATTER
,
534 _
=> bail
!("Unsupported output format '{}'.", format
),
537 let mut uri_param
= HashMap
::new();
539 if comp_len
== 4 && components
[2] == "access" && (
540 (components
[3] == "ticket" && method
== hyper
::Method
::POST
) ||
541 (components
[3] == "domains" && method
== hyper
::Method
::GET
)
543 // explicitly allow those calls without auth
545 let (ticket
, token
, _
) = extract_auth_data(&parts
.headers
);
546 match check_auth(&method
, &ticket
, &token
, &user_info
) {
547 Ok(userid
) => rpcenv
.set_user(Some(userid
.to_string())),
549 // always delay unauthorized calls by 3 seconds (from start of request)
550 let err
= http_err
!(UNAUTHORIZED
, "authentication failed - {}", err
);
551 tokio
::time
::delay_until(Instant
::from_std(delay_unauth_time
)).await
;
552 return Ok((formatter
.format_error
)(err
));
557 match api
.find_method(&components
[2..], method
, &mut uri_param
) {
559 let err
= http_err
!(NOT_FOUND
, "Path '{}' not found.", path
);
560 return Ok((formatter
.format_error
)(err
));
562 Some(api_method
) => {
563 let user
= rpcenv
.get_user();
564 if !check_api_permission(api_method
.access
.permission
, user
.as_deref(), &uri_param
, user_info
.as_ref()) {
565 let err
= http_err
!(FORBIDDEN
, "permission check failed");
566 tokio
::time
::delay_until(Instant
::from_std(access_forbidden_time
)).await
;
567 return Ok((formatter
.format_error
)(err
));
570 let result
= if api_method
.protected
&& env_type
== RpcEnvironmentType
::PUBLIC
{
571 proxy_protected_request(api_method
, parts
, body
).await
573 handle_api_request(rpcenv
, api_method
, formatter
, parts
, body
, uri_param
).await
576 if let Err(err
) = result
{
577 return Ok((formatter
.format_error
)(err
));
585 // not Auth required for accessing files!
587 if method
!= hyper
::Method
::GET
{
588 bail
!("Unsupported HTTP method {}", method
);
592 let (ticket
, token
, language
) = extract_auth_data(&parts
.headers
);
594 match check_auth(&method
, &ticket
, &token
, &user_info
) {
596 let new_token
= assemble_csrf_prevention_token(csrf_secret(), &userid
);
597 return Ok(get_index(Some(userid
), Some(new_token
), language
, &api
, parts
));
600 tokio
::time
::delay_until(Instant
::from_std(delay_unauth_time
)).await
;
601 return Ok(get_index(None
, None
, language
, &api
, parts
));
605 return Ok(get_index(None
, None
, language
, &api
, parts
));
608 let filename
= api
.find_alias(&components
);
609 return handle_static_file_download(filename
).await
;
613 Err(http_err
!(NOT_FOUND
, "Path '{}' not found.", path
))