proxmox-backup/src/server/rest.rs

624 lines
21 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2019-10-26 09:36:01 +00:00
use std::hash::BuildHasher;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use failure::*;
2019-11-22 12:02:05 +00:00
use futures::future::{self, FutureExt, TryFutureExt};
use futures::stream::TryStreamExt;
use hyper::header;
use hyper::http::request::Parts;
use hyper::rt::Future;
use hyper::{Body, Request, Response, StatusCode};
use serde_json::{json, Value};
use tokio::fs::File;
use url::form_urlencoded;
use proxmox::api::http_err;
2019-11-22 12:02:05 +00:00
use proxmox::api::{ApiHandler, ApiMethod, HttpError};
use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
use proxmox::api::schema::{ObjectSchema, parse_simple_value, verify_json_object, parse_parameter_strings};
use super::environment::RestEnvironment;
use super::formatter::*;
use super::ApiConfig;
use crate::auth_helpers::*;
use crate::tools;
2019-02-01 08:54:56 +00:00
extern "C" { fn tzset(); }
2018-11-15 09:18:01 +00:00
pub struct RestServer {
pub api_config: Arc<ApiConfig>,
}
impl RestServer {
pub fn new(api_config: ApiConfig) -> Self {
Self { api_config: Arc::new(api_config) }
}
}
impl tower_service::Service<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer {
type Response = ApiService;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, ctx: &tokio_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
match ctx.get_ref().peer_addr() {
2019-07-03 10:00:43 +00:00
Err(err) => {
future::err(format_err!("unable to get peer address - {}", err)).boxed()
2019-07-03 10:00:43 +00:00
}
Ok(peer) => {
future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
2019-07-03 10:00:43 +00:00
}
}
2018-11-15 09:18:01 +00:00
}
}
impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
type Response = ApiService;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
2019-07-03 10:00:43 +00:00
match ctx.peer_addr() {
Err(err) => {
future::err(format_err!("unable to get peer address - {}", err)).boxed()
2019-07-03 10:00:43 +00:00
}
Ok(peer) => {
future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
2019-07-03 10:00:43 +00:00
}
}
}
}
2018-11-15 09:18:01 +00:00
pub struct ApiService {
pub peer: std::net::SocketAddr,
2018-11-15 09:18:01 +00:00
pub api_config: Arc<ApiConfig>,
}
fn log_response(
peer: &std::net::SocketAddr,
method: hyper::Method,
path: &str,
resp: &Response<Body>,
) {
if resp.extensions().get::<NoLogExtension>().is_some() { return; };
let status = resp.status();
if !(status.is_success() || status.is_informational()) {
let reason = status.canonical_reason().unwrap_or("unknown reason");
let mut message = "request failed";
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
message = &data.0;
}
log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
}
}
2018-11-15 09:18:01 +00:00
impl tower_service::Service<Request<Body>> for ApiService {
type Response = Response<Body>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
2018-11-15 09:18:01 +00:00
fn call(&mut self, req: Request<Body>) -> Self::Future {
let path = req.uri().path().to_owned();
let method = req.method().clone();
2019-10-26 09:36:01 +00:00
let peer = self.peer;
2019-11-22 12:02:05 +00:00
handle_request(self.api_config.clone(), req)
.map(move |result| match result {
Ok(res) => {
log_response(&peer, method, &path, &res);
Ok::<_, Self::Error>(res)
}
2018-11-15 09:18:01 +00:00
Err(err) => {
if let Some(apierr) = err.downcast_ref::<HttpError>() {
2018-11-15 09:18:01 +00:00
let mut resp = Response::new(Body::from(apierr.message.clone()));
*resp.status_mut() = apierr.code;
log_response(&peer, method, &path, &resp);
2018-11-15 09:18:01 +00:00
Ok(resp)
} else {
let mut resp = Response::new(Body::from(err.to_string()));
*resp.status_mut() = StatusCode::BAD_REQUEST;
log_response(&peer, method, &path, &resp);
2018-11-15 09:18:01 +00:00
Ok(resp)
}
}
})
.boxed()
2018-11-15 09:18:01 +00:00
}
}
async fn get_request_parameters<S: 'static + BuildHasher + Send>(
param_schema: &ObjectSchema,
parts: Parts,
req_body: Body,
2019-10-26 09:36:01 +00:00
uri_param: HashMap<String, String, S>,
2019-11-22 12:02:05 +00:00
) -> Result<Value, Error> {
let mut is_json = false;
if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
match value.to_str().map(|v| v.split(';').next()) {
Ok(Some("application/x-www-form-urlencoded")) => {
is_json = false;
}
Ok(Some("application/json")) => {
is_json = true;
}
2019-11-22 12:02:05 +00:00
_ => bail!("unsupported content type {:?}", value.to_str()),
}
}
2019-11-22 12:02:05 +00:00
let body = req_body
2018-11-15 09:42:01 +00:00
.map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err)))
.try_fold(Vec::new(), |mut acc, chunk| async move {
if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
acc.extend_from_slice(&*chunk);
Ok(acc)
} else {
2019-10-26 09:36:01 +00:00
Err(http_err!(BAD_REQUEST, "Request body too large".to_string()))
}
2019-11-22 12:02:05 +00:00
}).await?;
2019-11-22 12:02:05 +00:00
let utf8 = std::str::from_utf8(&body)
.map_err(|err| format_err!("Request body not uft8: {}", err))?;
2019-11-22 12:02:05 +00:00
if is_json {
let mut params: Value = serde_json::from_str(utf8)?;
for (k, v) in uri_param {
if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
2019-11-22 12:02:05 +00:00
params[&k] = parse_simple_value(&v, prop_schema)?;
}
2019-11-22 12:02:05 +00:00
}
verify_json_object(&params, param_schema)?;
2019-11-22 12:02:05 +00:00
return Ok(params);
}
2019-11-22 12:02:05 +00:00
let mut param_list: Vec<(String, String)> = vec![];
2019-11-22 12:02:05 +00:00
if !utf8.is_empty() {
for (k, v) in form_urlencoded::parse(utf8.as_bytes()).into_owned() {
param_list.push((k, v));
}
}
if let Some(query_str) = parts.uri.query() {
for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
if k == "_dc" { continue; } // skip extjs "disable cache" parameter
param_list.push((k, v));
}
}
2018-11-16 08:15:33 +00:00
2019-11-22 12:02:05 +00:00
for (k, v) in uri_param {
param_list.push((k.clone(), v.clone()));
}
2018-11-16 08:15:33 +00:00
let params = parse_parameter_strings(&param_list, param_schema, true)?;
2019-11-22 12:02:05 +00:00
Ok(params)
}
struct NoLogExtension();
2019-11-22 12:02:05 +00:00
async fn proxy_protected_request(
2019-02-01 08:54:56 +00:00
info: &'static ApiMethod,
mut parts: Parts,
req_body: Body,
2019-11-22 12:02:05 +00:00
) -> Result<Response<Body>, Error> {
let mut uri_parts = parts.uri.clone().into_parts();
uri_parts.scheme = Some(http::uri::Scheme::HTTP);
uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
let new_uri = http::Uri::from_parts(uri_parts).unwrap();
parts.uri = new_uri;
let request = Request::from_parts(parts, req_body);
2019-11-22 12:02:05 +00:00
let reload_timezone = info.reload_timezone;
let resp = hyper::client::Client::new()
.request(request)
.map_err(Error::from)
.map_ok(|mut resp| {
resp.extensions_mut().insert(NoLogExtension());
resp
2019-11-22 12:02:05 +00:00
})
.await?;
2019-11-22 12:02:05 +00:00
if reload_timezone { unsafe { tzset(); } }
2019-11-22 12:02:05 +00:00
Ok(resp)
}
2019-11-22 12:02:05 +00:00
pub async fn handle_sync_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
mut rpcenv: Env,
2018-11-15 09:25:59 +00:00
info: &'static ApiMethod,
2018-12-05 11:42:25 +00:00
formatter: &'static OutputFormatter,
parts: Parts,
req_body: Body,
2019-10-26 09:36:01 +00:00
uri_param: HashMap<String, String, S>,
2019-11-22 12:02:05 +00:00
) -> Result<Response<Body>, Error> {
2019-11-21 08:36:41 +00:00
let handler = match info.handler {
2019-11-22 12:02:05 +00:00
ApiHandler::Async(_) => bail!("handle_sync_api_request: internal error (called with Async handler)"),
2019-11-21 08:36:41 +00:00
ApiHandler::Sync(handler) => handler,
};
2019-11-22 12:02:05 +00:00
let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
2019-11-22 12:02:05 +00:00
let mut delay = false;
2019-11-22 12:02:05 +00:00
let resp = match (handler)(params, info, &mut rpcenv) {
Ok(data) => (formatter.format_data)(data, &rpcenv),
Err(err) => {
if let Some(httperr) = err.downcast_ref::<HttpError>() {
if httperr.code == StatusCode::UNAUTHORIZED {
delay = true;
}
2019-02-01 08:54:56 +00:00
}
2019-11-22 12:02:05 +00:00
(formatter.format_error)(err)
}
};
2019-02-01 08:54:56 +00:00
2019-11-22 12:02:05 +00:00
if info.reload_timezone { unsafe { tzset(); } }
if delay {
tokio::timer::delay(delay_unauth_time).await;
}
2019-11-22 12:02:05 +00:00
Ok(resp)
}
2019-11-22 12:02:05 +00:00
pub async fn handle_async_api_request<Env: RpcEnvironment>(
rpcenv: Env,
2019-11-21 08:36:41 +00:00
info: &'static ApiMethod,
formatter: &'static OutputFormatter,
parts: Parts,
req_body: Body,
uri_param: HashMap<String, String>,
2019-11-22 12:02:05 +00:00
) -> Result<Response<Body>, Error> {
2019-11-21 08:36:41 +00:00
let handler = match info.handler {
2019-11-22 12:02:05 +00:00
ApiHandler::Sync(_) => bail!("handle_async_api_request: internal error (called with Sync handler)"),
2019-11-21 08:36:41 +00:00
ApiHandler::Async(handler) => handler,
};
2019-11-22 12:02:05 +00:00
// fixme: convert parameters to Json
let mut param_list: Vec<(String, String)> = vec![];
if let Some(query_str) = parts.uri.query() {
for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
if k == "_dc" { continue; } // skip extjs "disable cache" parameter
param_list.push((k, v));
}
}
for (k, v) in uri_param {
param_list.push((k.clone(), v.clone()));
}
let params = match parse_parameter_strings(&param_list, &info.parameters, true) {
Ok(v) => v,
Err(err) => {
2019-11-22 12:02:05 +00:00
return Ok((formatter.format_error)(Error::from(err)));
}
};
2019-11-22 12:02:05 +00:00
let resp = (handler)(parts, req_body, params, info, Box::new(rpcenv)).await?;
Ok(resp)
}
fn get_index(username: Option<String>, token: Option<String>) -> Response<Body> {
2019-08-03 15:06:23 +00:00
let nodename = proxmox::tools::nodename();
2019-10-26 09:36:01 +00:00
let username = username.unwrap_or_else(|| String::from(""));
2019-10-26 09:36:01 +00:00
let token = token.unwrap_or_else(|| String::from(""));
let setup = json!({
"Setup": { "auth_cookie_name": "PBSAuthCookie" },
"NodeName": nodename,
"UserName": username,
2019-01-23 11:49:10 +00:00
"CSRFPreventionToken": token,
});
let index = format!(r###"
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Proxmox Backup Server</title>
2018-12-01 14:21:25 +00:00
<link rel="icon" sizes="128x128" href="/images/logo-128.png" />
<link rel="apple-touch-icon" sizes="128x128" href="/pve2/images/logo-128.png" />
2018-12-01 14:21:25 +00:00
<link rel="stylesheet" type="text/css" href="/extjs/theme-crisp/resources/theme-crisp-all.css" />
<link rel="stylesheet" type="text/css" href="/extjs/crisp/resources/charts-all.css" />
<link rel="stylesheet" type="text/css" href="/fontawesome/css/font-awesome.css" />
<script type='text/javascript'> function gettext(buf) {{ return buf; }} </script>
2018-12-01 14:21:25 +00:00
<script type="text/javascript" src="/extjs/ext-all-debug.js"></script>
<script type="text/javascript" src="/extjs/charts-debug.js"></script>
<script type="text/javascript">
Proxmox = {};
</script>
2018-12-01 14:21:25 +00:00
<script type="text/javascript" src="/widgettoolkit/proxmoxlib.js"></script>
<script type="text/javascript" src="/extjs/locale/locale-en.js"></script>
<script type="text/javascript">
Ext.History.fieldid = 'x-history-field';
</script>
2018-12-04 16:53:10 +00:00
<script type="text/javascript" src="/js/proxmox-backup-gui.js"></script>
</head>
<body>
<!-- Fields required for history management -->
<form id="history-form" class="x-hidden">
<input type="hidden" id="x-history-field"/>
</form>
</body>
</html>
"###, setup.to_string());
Response::builder()
2019-01-23 11:49:10 +00:00
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html")
.body(index.into())
.unwrap()
}
fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
return match ext {
"css" => ("text/css", false),
"html" => ("text/html", false),
"js" => ("application/javascript", false),
"json" => ("application/json", false),
"map" => ("application/json", false),
"png" => ("image/png", true),
"ico" => ("image/x-icon", true),
"gif" => ("image/gif", true),
"svg" => ("image/svg+xml", false),
"jar" => ("application/java-archive", true),
"woff" => ("application/font-woff", true),
"woff2" => ("application/font-woff2", true),
"ttf" => ("application/font-snft", true),
"pdf" => ("application/pdf", true),
"epub" => ("application/epub+zip", true),
"mp3" => ("audio/mpeg", true),
"oga" => ("audio/ogg", true),
"tgz" => ("application/x-compressed-tar", true),
_ => ("application/octet-stream", false),
};
}
("application/octet-stream", false)
}
async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
let (content_type, _nocomp) = extension_to_content_type(&filename);
use tokio::io::AsyncReadExt;
let mut file = File::open(filename)
.await
.map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
let mut data: Vec<u8> = Vec::new();
file.read_to_end(&mut data)
.await
.map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))?;
let mut response = Response::new(data.into());
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(content_type));
Ok(response)
}
async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
let (content_type, _nocomp) = extension_to_content_type(&filename);
let file = File::open(filename)
.await
.map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
let payload = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new())
.map_ok(|bytes| hyper::Chunk::from(bytes.freeze()));
let body = Body::wrap_stream(payload);
// fixme: set other headers ?
Ok(Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, content_type)
.body(body)
.unwrap()
)
}
2019-11-22 12:02:05 +00:00
async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
2019-11-22 12:02:05 +00:00
tokio::fs::metadata(filename.clone())
2018-11-15 09:42:01 +00:00
.map_err(|err| http_err!(BAD_REQUEST, format!("File access problems: {}", err)))
.and_then(|metadata| async move {
if metadata.len() < 1024*32 {
simple_static_file_download(filename).await
} else {
chuncked_static_file_download(filename).await
}
2019-11-22 12:02:05 +00:00
})
.await
}
fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>) {
let mut ticket = None;
if let Some(raw_cookie) = headers.get("COOKIE") {
if let Ok(cookie) = raw_cookie.to_str() {
ticket = tools::extract_auth_cookie(cookie, "PBSAuthCookie");
}
}
let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
Some(Ok(v)) => Some(v.to_owned()),
_ => None,
};
(ticket, token)
}
fn check_auth(method: &hyper::Method, ticket: &Option<String>, token: &Option<String>) -> Result<String, Error> {
let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
let username = match ticket {
Some(ticket) => match tools::ticket::verify_rsa_ticket(public_auth_key(), "PBS", &ticket, None, -300, ticket_lifetime) {
Ok((_age, Some(username))) => username.to_owned(),
Ok((_, None)) => bail!("ticket without username."),
Err(err) => return Err(err),
}
None => bail!("missing ticket"),
};
if method != hyper::Method::GET {
if let Some(token) = token {
println!("CSRF prevention token: {:?}", token);
verify_csrf_prevention_token(csrf_secret(), &username, &token, -300, ticket_lifetime)?;
} else {
bail!("missing CSRF prevention token");
}
}
Ok(username)
}
2019-11-22 12:02:05 +00:00
pub async fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> Result<Response<Body>, Error> {
let (parts, body) = req.into_parts();
let method = parts.method.clone();
2019-11-22 12:02:05 +00:00
let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
let comp_len = components.len();
println!("REQUEST {} {}", method, path);
println!("COMPO {:?}", components);
let env_type = api.env_type();
let mut rpcenv = RestEnvironment::new(env_type);
2019-01-27 09:18:52 +00:00
2019-01-31 11:22:00 +00:00
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
if comp_len >= 1 && components[0] == "api2" {
if comp_len >= 2 {
2019-11-22 12:02:05 +00:00
let format = components[1];
2019-11-22 12:02:05 +00:00
2018-12-05 11:42:25 +00:00
let formatter = match format {
"json" => &JSON_FORMATTER,
"extjs" => &EXTJS_FORMATTER,
2019-11-22 12:02:05 +00:00
_ => bail!("Unsupported output format '{}'.", format),
2018-12-05 11:42:25 +00:00
};
2018-11-16 08:15:33 +00:00
let mut uri_param = HashMap::new();
2019-01-31 11:22:00 +00:00
if comp_len == 4 && components[2] == "access" && components[3] == "ticket" {
// explicitly allow those calls without auth
} else {
let (ticket, token) = extract_auth_data(&parts.headers);
match check_auth(&method, &ticket, &token) {
Ok(username) => {
// fixme: check permissions
rpcenv.set_user(Some(username));
}
Err(err) => {
// always delay unauthorized calls by 3 seconds (from start of request)
let err = http_err!(UNAUTHORIZED, format!("permission check failed - {}", err));
2019-11-22 12:02:05 +00:00
tokio::timer::delay(delay_unauth_time).await;
return Ok((formatter.format_error)(err));
}
2019-01-31 11:22:00 +00:00
}
}
match api.find_method(&components[2..], method, &mut uri_param) {
2019-11-21 08:36:41 +00:00
None => {
let err = http_err!(NOT_FOUND, "Path not found.".to_string());
2019-11-22 12:02:05 +00:00
return Ok((formatter.format_error)(err));
}
2019-11-21 08:36:41 +00:00
Some(api_method) => {
if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
2019-11-22 12:02:05 +00:00
return proxy_protected_request(api_method, parts, body).await;
} else {
2019-11-21 08:36:41 +00:00
match api_method.handler {
ApiHandler::Sync(_) => {
2019-11-22 12:02:05 +00:00
return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await;
2019-11-21 08:36:41 +00:00
}
ApiHandler::Async(_) => {
2019-11-22 12:02:05 +00:00
return handle_async_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await;
2019-11-21 08:36:41 +00:00
}
}
}
}
}
}
2019-11-22 12:02:05 +00:00
} else {
// not Auth required for accessing files!
if method != hyper::Method::GET {
2019-11-22 12:02:05 +00:00
bail!("Unsupported HTTP method {}", method);
}
if comp_len == 0 {
let (ticket, token) = extract_auth_data(&parts.headers);
if ticket != None {
match check_auth(&method, &ticket, &token) {
Ok(username) => {
let new_token = assemble_csrf_prevention_token(csrf_secret(), &username);
2019-11-22 12:02:05 +00:00
return Ok(get_index(Some(username), Some(new_token)));
}
_ => {
2019-11-22 12:02:05 +00:00
tokio::timer::delay(delay_unauth_time).await;
return Ok(get_index(None, None));
}
}
} else {
2019-11-22 12:02:05 +00:00
return Ok(get_index(None, None));
}
} else {
let filename = api.find_alias(&components);
2019-11-22 12:02:05 +00:00
return handle_static_file_download(filename).await;
}
}
2019-11-22 12:02:05 +00:00
Err(http_err!(NOT_FOUND, "Path not found.".to_string()))
}