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
>,
60 fn log_response(method
: hyper
::Method
, path
: &str, resp
: &Response
<Body
>) {
62 if resp
.extensions().get
::<NoLogExtension
>().is_some() { return; }
;
64 let status
= resp
.status();
66 if !status
.is_success() {
67 let reason
= status
.canonical_reason().unwrap_or("unknown reason");
68 let client
= "unknown"; // fixme: howto get peer_addr ?
70 let mut message
= "request failed";
71 if let Some(data
) = resp
.extensions().get
::<ErrorMessageExtension
>() {
75 log
::error
!("{} {}: {} {}: [client {}] {}", method
.as_str(), path
, status
.as_str(), reason
, client
, message
);
79 impl Service
for ApiService
{
82 type Error
= hyper
::Error
;
83 type Future
= Box
<Future
<Item
= Response
<Body
>, Error
= Self::Error
> + Send
>;
85 fn call(&mut self, req
: Request
<Self::ReqBody
>) -> Self::Future
{
86 let path
= req
.uri().path().to_owned();
87 let method
= req
.method().clone();
89 Box
::new(handle_request(self.api_config
.clone(), req
).then(move |result
| {
92 log_response(method
, &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 log_response(method
, &path
, &resp
);
102 let mut resp
= Response
::new(Body
::from(err
.to_string()));
103 *resp
.status_mut() = StatusCode
::BAD_REQUEST
;
104 log_response(method
, &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 fn extract_auth_data(headers
: &http
::HeaderMap
) -> (Option
<String
>, Option
<String
>) {
427 let mut ticket
= None
;
428 if let Some(raw_cookie
) = headers
.get("COOKIE") {
429 if let Ok(cookie
) = raw_cookie
.to_str() {
430 ticket
= tools
::extract_auth_cookie(cookie
, "PBSAuthCookie");
434 let token
= match headers
.get("CSRFPreventionToken").map(|v
| v
.to_str()) {
435 Some(Ok(v
)) => Some(v
.to_owned()),
442 fn check_auth(method
: &hyper
::Method
, ticket
: Option
<String
>, token
: Option
<String
>) -> Result
<String
, Error
> {
444 let ticket_lifetime
= 3600*2; // 2 hours
446 let username
= match ticket
{
447 Some(ticket
) => match tools
::ticket
::verify_rsa_ticket(public_auth_key(), "PBS", &ticket
, None
, -300, ticket_lifetime
) {
448 Ok((_age
, Some(username
))) => username
.to_owned(),
449 Ok((_
, None
)) => bail
!("ticket without username."),
450 Err(err
) => return Err(err
),
452 None
=> bail
!("missing ticket"),
455 if method
!= hyper
::Method
::GET
{
456 if let Some(token
) = token
{
457 println
!("CSRF prev token: {:?}", token
);
458 verify_csrf_prevention_token(csrf_secret(), &username
, &token
, -300, ticket_lifetime
)?
;
467 pub fn handle_request(api
: Arc
<ApiConfig
>, req
: Request
<Body
>) -> BoxFut
{
469 let (parts
, body
) = req
.into_parts();
471 let method
= parts
.method
.clone();
472 let path
= parts
.uri
.path();
475 // do not allow ".", "..", or hidden files ".XXXX"
476 // also remove empty path components
478 let items
= path
.split('
/'
);
479 let mut path
= String
::new();
480 let mut components
= vec
![];
483 if name
.is_empty() { continue; }
484 if name
.starts_with(".") {
485 return Box
::new(future
::err(http_err
!(BAD_REQUEST
, "Path contains illegal components.".to_string())));
489 components
.push(name
);
492 let comp_len
= components
.len();
494 println
!("REQUEST {} {}", method
, path
);
495 println
!("COMPO {:?}", components
);
497 let env_type
= api
.env_type();
498 let mut rpcenv
= RestEnvironment
::new(env_type
);
500 let delay_unauth_time
= std
::time
::Instant
::now() + std
::time
::Duration
::from_millis(3000);
502 if comp_len
>= 1 && components
[0] == "api2" {
503 println
!("GOT API REQUEST");
506 let format
= components
[1];
507 let formatter
= match format
{
508 "json" => &JSON_FORMATTER
,
509 "extjs" => &EXTJS_FORMATTER
,
511 return Box
::new(future
::err(http_err
!(BAD_REQUEST
, format
!("Unsupported output format '{}'.", format
))));
515 let mut uri_param
= HashMap
::new();
517 if comp_len
== 4 && components
[2] == "access" && components
[3] == "ticket" {
518 // explicitly allow those calls without auth
520 let (ticket
, token
) = extract_auth_data(&parts
.headers
);
521 match check_auth(&method
, ticket
, token
) {
524 // fixme: check permissions
526 rpcenv
.set_user(Some(username
));
529 // always delay unauthorized calls by 3 seconds (from start of request)
530 let err
= http_err
!(UNAUTHORIZED
, format
!("permission check failed - {}", err
));
531 let resp
= (formatter
.format_error
)(err
);
532 let delayed_response
= tokio
::timer
::Delay
::new(delay_unauth_time
)
533 .map_err(|err
| http_err
!(INTERNAL_SERVER_ERROR
, format
!("tokio timer delay error: {}", err
)))
534 .and_then(|_
| Ok(resp
));
536 return Box
::new(delayed_response
);
541 match api
.find_method(&components
[2..], method
, &mut uri_param
) {
542 MethodDefinition
::None
=> {}
543 MethodDefinition
::Simple(api_method
) => {
544 if api_method
.protected
&& env_type
== RpcEnvironmentType
::PUBLIC
{
545 return proxy_protected_request(api_method
, parts
, body
);
547 return handle_sync_api_request(rpcenv
, api_method
, formatter
, parts
, body
, uri_param
);
550 MethodDefinition
::Async(async_method
) => {
551 return handle_async_api_request(rpcenv
, async_method
, formatter
, parts
, body
, uri_param
);
556 // not Auth for accessing files!
561 let filename
= api
.find_alias(&components
);
562 return handle_static_file_download(filename
);
566 Box
::new(future
::err(http_err
!(NOT_FOUND
, "Path not found.".to_string())))