2 use crate::api
::schema
::*;
3 use crate::api
::router
::*;
4 use crate::api
::config
::*;
5 use crate::auth_helpers
::*;
6 use super::environment
::RestEnvironment
;
7 use super::formatter
::*;
9 use std
::path
::{Path, PathBuf}
;
11 use std
::collections
::HashMap
;
14 use serde_json
::{json, Value}
;
15 use url
::form_urlencoded
;
17 use futures
::future
::{self, Either}
;
18 //use tokio::prelude::*;
19 //use tokio::timer::Delay;
21 //use bytes::{BytesMut, BufMut};
23 //use hyper::body::Payload;
24 use hyper
::http
::request
::Parts
;
25 use hyper
::{Body, Request, Response, StatusCode}
;
26 use hyper
::service
::{Service, NewService}
;
27 use hyper
::rt
::{Future, Stream}
;
30 extern "C" { fn tzset(); }
32 pub struct RestServer
{
33 pub api_config
: Arc
<ApiConfig
>,
38 pub fn new(api_config
: ApiConfig
) -> Self {
39 Self { api_config: Arc::new(api_config) }
43 impl NewService
for RestServer
47 type Error
= hyper
::Error
;
48 type InitError
= hyper
::Error
;
49 type Service
= ApiService
;
50 type Future
= Box
<Future
<Item
= Self::Service
, Error
= Self::InitError
> + Send
>;
51 fn new_service(&self) -> Self::Future
{
52 Box
::new(future
::ok(ApiService { api_config: self.api_config.clone() }
))
56 pub struct ApiService
{
57 pub api_config
: Arc
<ApiConfig
>,
61 fn log_response(path
: &str, resp
: &Response
<Body
>) {
63 if resp
.extensions().get
::<NoLogExtension
>().is_some() { return; }
;
65 let status
= resp
.status();
67 if !status
.is_success() {
68 let reason
= status
.canonical_reason().unwrap_or("unknown reason");
69 let client
= "unknown"; // fixme: howto get peer_addr ?
71 let mut message
= "request failed";
72 if let Some(data
) = resp
.extensions().get
::<ErrorMessageExtension
>() {
76 log
::error
!("{}: {} {}: [client {}] {}", path
, status
.as_str(), reason
, client
, message
);
81 impl Service
for ApiService
{
84 type Error
= hyper
::Error
;
85 type Future
= Box
<Future
<Item
= Response
<Body
>, Error
= Self::Error
> + Send
>;
87 fn call(&mut self, req
: Request
<Self::ReqBody
>) -> Self::Future
{
88 let path
= req
.uri().path().to_owned();
89 Box
::new(handle_request(self.api_config
.clone(), req
).then(move |result
| {
92 Self::log_response(&path
, &res
);
93 Ok
::<_
, hyper
::Error
>(res
)
96 if let Some(apierr
) = err
.downcast_ref
::<HttpError
>() {
97 let mut resp
= Response
::new(Body
::from(apierr
.message
.clone()));
98 *resp
.status_mut() = apierr
.code
;
99 Self::log_response(&path
, &resp
);
102 let mut resp
= Response
::new(Body
::from(err
.to_string()));
103 *resp
.status_mut() = StatusCode
::BAD_REQUEST
;
104 Self::log_response(&path
, &resp
);
113 fn get_request_parameters_async(
114 info
: &'
static ApiMethod
,
117 uri_param
: HashMap
<String
, String
>,
118 ) -> Box
<Future
<Item
= Value
, Error
= failure
::Error
> + Send
>
121 .map_err(|err
| http_err
!(BAD_REQUEST
, format
!("Promlems reading request body: {}", err
)))
122 .fold(Vec
::new(), |mut acc
, chunk
| {
123 if acc
.len() + chunk
.len() < 64*1024 { //fimxe: max request body size?
124 acc
.extend_from_slice(&*chunk
);
127 else { Err(http_err!(BAD_REQUEST, format!("Request body too large"))) }
129 .and_then(move |body
| {
131 let utf8
= std
::str::from_utf8(&body
)?
;
133 let mut param_list
: Vec
<(String
, String
)> = vec
![];
136 for (k
, v
) in form_urlencoded
::parse(utf8
.as_bytes()).into_owned() {
137 param_list
.push((k
, v
));
142 if let Some(query_str
) = parts
.uri
.query() {
143 for (k
, v
) in form_urlencoded
::parse(query_str
.as_bytes()).into_owned() {
144 if k
== "_dc" { continue; }
// skip extjs "disable cache" parameter
145 param_list
.push((k
, v
));
149 for (k
, v
) in uri_param
{
150 param_list
.push((k
.clone(), v
.clone()));
153 let params
= parse_parameter_strings(¶m_list
, &info
.parameters
, true)?
;
161 struct NoLogExtension();
163 fn proxy_protected_request(
164 info
: &'
static ApiMethod
,
170 let mut uri_parts
= parts
.uri
.clone().into_parts();
172 uri_parts
.scheme
= Some(http
::uri
::Scheme
::HTTP
);
173 uri_parts
.authority
= Some(http
::uri
::Authority
::from_static("127.0.0.1:82"));
174 let new_uri
= http
::Uri
::from_parts(uri_parts
).unwrap();
178 let request
= Request
::from_parts(parts
, req_body
);
180 let resp
= hyper
::client
::Client
::new()
182 .map_err(|e
| Error
::from(e
))
184 resp
.extensions_mut().insert(NoLogExtension
);
188 let resp
= if info
.reload_timezone
{
189 Either
::A(resp
.then(|resp
| {unsafe { tzset() }
; resp
}))
194 return Box
::new(resp
);
197 fn handle_sync_api_request(
198 mut rpcenv
: RestEnvironment
,
199 info
: &'
static ApiMethod
,
200 formatter
: &'
static OutputFormatter
,
203 uri_param
: HashMap
<String
, String
>,
206 let params
= get_request_parameters_async(info
, parts
, req_body
, uri_param
);
208 let delay_unauth_time
= std
::time
::Instant
::now() + std
::time
::Duration
::from_millis(3000);
211 .and_then(move |params
| {
212 let mut delay
= false;
213 let resp
= match (info
.handler
)(params
, info
, &mut rpcenv
) {
214 Ok(data
) => (formatter
.format_result
)(data
, &rpcenv
),
216 if let Some(httperr
) = err
.downcast_ref
::<HttpError
>() {
217 if httperr
.code
== StatusCode
::UNAUTHORIZED { delay = true; }
219 (formatter
.format_error
)(err
)
223 if info
.reload_timezone
{
228 let delayed_response
= tokio
::timer
::Delay
::new(delay_unauth_time
)
229 .map_err(|err
| http_err
!(INTERNAL_SERVER_ERROR
, format
!("tokio timer delay error: {}", err
)))
230 .and_then(|_
| Ok(resp
));
232 Either
::A(delayed_response
)
234 Either
::B(future
::ok(resp
))
241 fn handle_async_api_request(
242 mut rpcenv
: RestEnvironment
,
243 info
: &'
static ApiAsyncMethod
,
244 formatter
: &'
static OutputFormatter
,
247 uri_param
: HashMap
<String
, String
>,
250 // fixme: convert parameters to Json
251 let mut param_list
: Vec
<(String
, String
)> = vec
![];
253 if let Some(query_str
) = parts
.uri
.query() {
254 for (k
, v
) in form_urlencoded
::parse(query_str
.as_bytes()).into_owned() {
255 if k
== "_dc" { continue; }
// skip extjs "disable cache" parameter
256 param_list
.push((k
, v
));
260 for (k
, v
) in uri_param
{
261 param_list
.push((k
.clone(), v
.clone()));
264 let params
= match parse_parameter_strings(¶m_list
, &info
.parameters
, true) {
267 let resp
= (formatter
.format_error
)(Error
::from(err
));
268 return Box
::new(future
::ok(resp
));
272 match (info
.handler
)(parts
, req_body
, params
, info
, &mut rpcenv
) {
273 Ok(future
) => future
,
275 let resp
= (formatter
.format_error
)(Error
::from(err
));
276 Box
::new(future
::ok(resp
))
281 fn get_index() -> BoxFut
{
283 let nodename
= tools
::nodename();
284 let username
= ""; // fixme: implement real auth
288 "Setup": { "auth_cookie_name": "PBSAuthCookie" }
,
289 "NodeName": nodename
,
290 "UserName": username
,
291 "CSRFPreventionToken": token
,
294 let index
= format
!(r
###"
298 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
299 <meta http-equiv="X-UA-Compatible" content="IE=edge">
300 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
301 <title>Proxmox Backup Server</title>
302 <link rel="icon" sizes="128x128" href="/images/logo-128.png" />
303 <link rel="apple-touch-icon" sizes="128x128" href="/pve2/images/logo-128.png" />
304 <link rel="stylesheet" type="text/css" href="/extjs/theme-crisp/resources/theme-crisp-all.css" />
305 <link rel="stylesheet" type="text/css" href="/extjs/crisp/resources/charts-all.css" />
306 <link rel="stylesheet" type="text/css" href="/fontawesome/css/font-awesome.css" />
307 <script type='text/javascript'> function gettext(buf) {{ return buf; }} </script>
308 <script type="text/javascript" src="/extjs/ext-all-debug.js"></script>
309 <script type="text/javascript" src="/extjs/charts-debug.js"></script>
310 <script type="text/javascript">
313 <script type="text/javascript" src="/widgettoolkit/proxmoxlib.js"></script>
314 <script type="text/javascript" src="/extjs/locale/locale-en.js"></script>
315 <script type="text/javascript">
316 Ext.History.fieldid = 'x-history-field';
318 <script type="text/javascript" src="/js/proxmox-backup-gui.js"></script>
321 <!-- Fields required for history management -->
322 <form id="history-form" class="x-hidden">
323 <input type="hidden" id="x-history-field"/>
327 "###, setup.to_string());
329 let resp
= Response
::builder()
330 .status(StatusCode
::OK
)
331 .header(header
::CONTENT_TYPE
, "text/html")
335 Box
::new(future
::ok(resp
))
338 fn extension_to_content_type(filename
: &Path
) -> (&'
static str, bool
) {
340 if let Some(ext
) = filename
.extension().and_then(|osstr
| osstr
.to_str()) {
342 "css" => ("text/css", false),
343 "html" => ("text/html", false),
344 "js" => ("application/javascript", false),
345 "json" => ("application/json", false),
346 "map" => ("application/json", false),
347 "png" => ("image/png", true),
348 "ico" => ("image/x-icon", true),
349 "gif" => ("image/gif", true),
350 "svg" => ("image/svg+xml", false),
351 "jar" => ("application/java-archive", true),
352 "woff" => ("application/font-woff", true),
353 "woff2" => ("application/font-woff2", true),
354 "ttf" => ("application/font-snft", true),
355 "pdf" => ("application/pdf", true),
356 "epub" => ("application/epub+zip", true),
357 "mp3" => ("audio/mpeg", true),
358 "oga" => ("audio/ogg", true),
359 "tgz" => ("application/x-compressed-tar", true),
360 _
=> ("application/octet-stream", false),
364 ("application/octet-stream", false)
367 fn simple_static_file_download(filename
: PathBuf
) -> BoxFut
{
369 let (content_type
, _nocomp
) = extension_to_content_type(&filename
);
371 Box
::new(File
::open(filename
)
372 .map_err(|err
| http_err
!(BAD_REQUEST
, format
!("File open failed: {}", err
)))
373 .and_then(move |file
| {
374 let buf
: Vec
<u8> = Vec
::new();
375 tokio
::io
::read_to_end(file
, buf
)
376 .map_err(|err
| http_err
!(BAD_REQUEST
, format
!("File read failed: {}", err
)))
377 .and_then(move |data
| {
378 let mut response
= Response
::new(data
.1.into
());
379 response
.headers_mut().insert(
380 header
::CONTENT_TYPE
,
381 header
::HeaderValue
::from_static(content_type
));
387 fn chuncked_static_file_download(filename
: PathBuf
) -> BoxFut
{
389 let (content_type
, _nocomp
) = extension_to_content_type(&filename
);
391 Box
::new(File
::open(filename
)
392 .map_err(|err
| http_err
!(BAD_REQUEST
, format
!("File open failed: {}", err
)))
393 .and_then(move |file
| {
394 let payload
= tokio
::codec
::FramedRead
::new(file
, tokio
::codec
::BytesCodec
::new()).
396 //sigh - howto avoid copy here? or the whole map() ??
397 hyper
::Chunk
::from(bytes
.to_vec())
399 let body
= Body
::wrap_stream(payload
);
401 // fixme: set other headers ?
402 Ok(Response
::builder()
403 .status(StatusCode
::OK
)
404 .header(header
::CONTENT_TYPE
, content_type
)
410 fn handle_static_file_download(filename
: PathBuf
) -> BoxFut
{
412 let response
= tokio
::fs
::metadata(filename
.clone())
413 .map_err(|err
| http_err
!(BAD_REQUEST
, format
!("File access problems: {}", err
)))
414 .and_then(|metadata
| {
415 if metadata
.len() < 1024*32 {
416 Either
::A(simple_static_file_download(filename
))
418 Either
::B(chuncked_static_file_download(filename
))
422 return Box
::new(response
);
425 pub fn handle_request(api
: Arc
<ApiConfig
>, req
: Request
<Body
>) -> BoxFut
{
427 let (parts
, body
) = req
.into_parts();
429 let method
= parts
.method
.clone();
430 let path
= parts
.uri
.path();
433 // do not allow ".", "..", or hidden files ".XXXX"
434 // also remove empty path components
436 let items
= path
.split('
/'
);
437 let mut path
= String
::new();
438 let mut components
= vec
![];
441 if name
.is_empty() { continue; }
442 if name
.starts_with(".") {
443 return Box
::new(future
::err(http_err
!(BAD_REQUEST
, "Path contains illegal components.".to_string())));
447 components
.push(name
);
450 let comp_len
= components
.len();
452 println
!("REQUEST {} {}", method
, path
);
453 println
!("COMPO {:?}", components
);
455 let env_type
= api
.env_type();
456 let mut rpcenv
= RestEnvironment
::new(env_type
);
458 let delay_unauth_time
= std
::time
::Instant
::now() + std
::time
::Duration
::from_millis(3000);
460 if let Some(raw_cookie
) = parts
.headers
.get("COOKIE") {
461 if let Ok(cookie
) = raw_cookie
.to_str() {
462 if let Some(ticket
) = tools
::extract_auth_cookie(cookie
, "PBSAuthCookie") {
463 if let Ok((_
, Some(username
))) = tools
::ticket
::verify_rsa_ticket(
464 public_auth_key(), "PBS", &ticket
, None
, -300, 3600*2) {
465 rpcenv
.set_user(Some(username
));
472 if comp_len
>= 1 && components
[0] == "api2" {
473 println
!("GOT API REQUEST");
475 let format
= components
[1];
476 let formatter
= match format
{
477 "json" => &JSON_FORMATTER
,
478 "extjs" => &EXTJS_FORMATTER
,
480 return Box
::new(future
::err(http_err
!(BAD_REQUEST
, format
!("Unsupported output format '{}'.", format
))));
484 let mut uri_param
= HashMap
::new();
486 if comp_len
== 4 && components
[2] == "access" && components
[3] == "ticket" {
487 // explicitly allow those calls without auth
489 if let Some(_username
) = rpcenv
.get_user() {
490 // fixme: check permissions
492 // always delay unauthorized calls by 3 seconds (from start of request)
493 let resp
= (formatter
.format_error
)(http_err
!(UNAUTHORIZED
, "permission check failed.".into()));
494 let delayed_response
= tokio
::timer
::Delay
::new(delay_unauth_time
)
495 .map_err(|err
| http_err
!(INTERNAL_SERVER_ERROR
, format
!("tokio timer delay error: {}", err
)))
496 .and_then(|_
| Ok(resp
));
498 return Box
::new(delayed_response
);
502 match api
.find_method(&components
[2..], method
, &mut uri_param
) {
503 MethodDefinition
::None
=> {}
504 MethodDefinition
::Simple(api_method
) => {
505 if api_method
.protected
&& env_type
== RpcEnvironmentType
::PUBLIC
{
506 return proxy_protected_request(api_method
, parts
, body
);
508 return handle_sync_api_request(rpcenv
, api_method
, formatter
, parts
, body
, uri_param
);
511 MethodDefinition
::Async(async_method
) => {
512 return handle_async_api_request(rpcenv
, async_method
, formatter
, parts
, body
, uri_param
);
517 // not Auth for accessing files!
522 let filename
= api
.find_alias(&components
);
523 return handle_static_file_download(filename
);
527 Box
::new(future
::err(http_err
!(NOT_FOUND
, "Path not found.".to_string())))