2019-08-26 11:33:38 +00:00
|
|
|
use std::collections::HashMap;
|
2019-12-12 14:27:07 +00:00
|
|
|
use std::future::Future;
|
2019-10-26 09:36:01 +00:00
|
|
|
use std::hash::BuildHasher;
|
2018-12-02 10:00:52 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-08-26 11:33:38 +00:00
|
|
|
use std::pin::Pin;
|
2018-11-15 07:18:48 +00:00
|
|
|
use std::sync::Arc;
|
2019-08-26 11:33:38 +00:00
|
|
|
use std::task::{Context, Poll};
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-11-22 12:02:05 +00:00
|
|
|
use futures::future::{self, FutureExt, TryFutureExt};
|
2019-08-26 11:33:38 +00:00
|
|
|
use futures::stream::TryStreamExt;
|
|
|
|
use hyper::header;
|
|
|
|
use hyper::http::request::Parts;
|
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
2018-12-01 12:37:49 +00:00
|
|
|
use serde_json::{json, Value};
|
2018-11-15 07:18:48 +00:00
|
|
|
use tokio::fs::File;
|
2019-12-12 14:27:07 +00:00
|
|
|
use tokio::time::Instant;
|
2019-08-26 11:33:38 +00:00
|
|
|
use url::form_urlencoded;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::http_err;
|
2019-11-22 12:02:05 +00:00
|
|
|
use proxmox::api::{ApiHandler, ApiMethod, HttpError};
|
2020-04-16 08:01:59 +00:00
|
|
|
use proxmox::api::{RpcEnvironment, RpcEnvironmentType, check_api_permission};
|
2019-11-22 16:22:07 +00:00
|
|
|
use proxmox::api::schema::{ObjectSchema, parse_simple_value, verify_json_object, parse_parameter_strings};
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
use super::environment::RestEnvironment;
|
|
|
|
use super::formatter::*;
|
2019-11-22 08:23:03 +00:00
|
|
|
use super::ApiConfig;
|
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
use crate::auth_helpers::*;
|
2020-08-06 13:46:01 +00:00
|
|
|
use crate::api2::types::Userid;
|
2019-08-26 11:33:38 +00:00
|
|
|
use crate::tools;
|
2020-08-12 10:05:52 +00:00
|
|
|
use crate::tools::ticket::Ticket;
|
2020-04-16 08:01:59 +00:00
|
|
|
use crate::config::cached_user_info::CachedUserInfo;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
impl tower_service::Service<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer {
|
|
|
|
type Response = ApiService;
|
2019-07-03 09:54:35 +00:00
|
|
|
type Error = Error;
|
2019-08-26 11:33:38 +00:00
|
|
|
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) => {
|
2019-08-26 11:33:38 +00:00
|
|
|
future::err(format_err!("unable to get peer address - {}", err)).boxed()
|
2019-07-03 10:00:43 +00:00
|
|
|
}
|
|
|
|
Ok(peer) => {
|
2019-08-26 11:33:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
|
|
|
|
type Response = ApiService;
|
2019-07-03 09:54:35 +00:00
|
|
|
type Error = Error;
|
2019-08-26 11:33:38 +00:00
|
|
|
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) => {
|
2019-08-26 11:33:38 +00:00
|
|
|
future::err(format_err!("unable to get peer address - {}", err)).boxed()
|
2019-07-03 10:00:43 +00:00
|
|
|
}
|
|
|
|
Ok(peer) => {
|
2019-08-26 11:33:38 +00:00
|
|
|
future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
|
2019-07-03 10:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 09:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 09:18:01 +00:00
|
|
|
pub struct ApiService {
|
2019-07-03 09:54:35 +00:00
|
|
|
pub peer: std::net::SocketAddr,
|
2018-11-15 09:18:01 +00:00
|
|
|
pub api_config: Arc<ApiConfig>,
|
|
|
|
}
|
|
|
|
|
2019-07-03 09:54:35 +00:00
|
|
|
fn log_response(
|
|
|
|
peer: &std::net::SocketAddr,
|
|
|
|
method: hyper::Method,
|
|
|
|
path: &str,
|
|
|
|
resp: &Response<Body>,
|
|
|
|
) {
|
2019-02-14 12:28:41 +00:00
|
|
|
|
2019-02-15 09:16:12 +00:00
|
|
|
if resp.extensions().get::<NoLogExtension>().is_some() { return; };
|
2019-02-14 12:28:41 +00:00
|
|
|
|
2019-02-15 09:16:12 +00:00
|
|
|
let status = resp.status();
|
2019-02-14 12:28:41 +00:00
|
|
|
|
2019-05-14 04:23:22 +00:00
|
|
|
if !(status.is_success() || status.is_informational()) {
|
2019-02-15 09:16:12 +00:00
|
|
|
let reason = status.canonical_reason().unwrap_or("unknown reason");
|
2019-02-15 08:55:12 +00:00
|
|
|
|
2019-02-15 09:16:12 +00:00
|
|
|
let mut message = "request failed";
|
|
|
|
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
|
|
|
|
message = &data.0;
|
2019-02-14 12:07:34 +00:00
|
|
|
}
|
2019-02-15 09:16:12 +00:00
|
|
|
|
2019-07-03 09:54:35 +00:00
|
|
|
log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
|
2019-02-14 12:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 09:18:01 +00:00
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
impl tower_service::Service<Request<Body>> for ApiService {
|
|
|
|
type Response = Response<Body>;
|
2019-07-03 09:54:35 +00:00
|
|
|
type Error = Error;
|
2019-08-26 11:33:38 +00:00
|
|
|
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
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
2019-02-14 12:07:34 +00:00
|
|
|
let path = req.uri().path().to_owned();
|
2019-02-15 09:16:12 +00:00
|
|
|
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)
|
2019-08-26 11:33:38 +00:00
|
|
|
.map(move |result| match result {
|
2019-02-14 12:07:34 +00:00
|
|
|
Ok(res) => {
|
2019-07-03 09:54:35 +00:00
|
|
|
log_response(&peer, method, &path, &res);
|
|
|
|
Ok::<_, Self::Error>(res)
|
2019-02-14 12:07:34 +00:00
|
|
|
}
|
2018-11-15 09:18:01 +00:00
|
|
|
Err(err) => {
|
2019-02-13 13:31:43 +00:00
|
|
|
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;
|
2019-07-03 09:54:35 +00:00
|
|
|
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;
|
2019-07-03 09:54:35 +00:00
|
|
|
log_response(&peer, method, &path, &resp);
|
2018-11-15 09:18:01 +00:00
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 11:33:38 +00:00
|
|
|
})
|
|
|
|
.boxed()
|
2018-11-15 09:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:44:14 +00:00
|
|
|
fn parse_query_parameters<S: 'static + BuildHasher + Send>(
|
|
|
|
param_schema: &ObjectSchema,
|
|
|
|
form: &str, // x-www-form-urlencoded body data
|
|
|
|
parts: &Parts,
|
|
|
|
uri_param: &HashMap<String, String, S>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let mut param_list: Vec<(String, String)> = vec![];
|
|
|
|
|
|
|
|
if !form.is_empty() {
|
|
|
|
for (k, v) in form_urlencoded::parse(form.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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k, v) in uri_param {
|
|
|
|
param_list.push((k.clone(), v.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = parse_parameter_strings(¶m_list, param_schema, true)?;
|
|
|
|
|
|
|
|
Ok(params)
|
|
|
|
}
|
|
|
|
|
2019-11-22 16:24:16 +00:00
|
|
|
async fn get_request_parameters<S: 'static + BuildHasher + Send>(
|
2019-11-22 16:22:07 +00:00
|
|
|
param_schema: &ObjectSchema,
|
2018-11-15 07:18:48 +00:00
|
|
|
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> {
|
|
|
|
|
2019-02-27 11:12:00 +00:00
|
|
|
let mut is_json = false;
|
|
|
|
|
|
|
|
if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
|
2019-03-19 11:50:15 +00:00
|
|
|
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-02-27 11:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let body = req_body
|
2020-07-29 07:38:11 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "Promlems reading request body: {}", err))
|
2019-08-26 11:33:38 +00:00
|
|
|
.try_fold(Vec::new(), |mut acc, chunk| async move {
|
2018-11-15 07:18:48 +00:00
|
|
|
if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
|
|
|
|
acc.extend_from_slice(&*chunk);
|
|
|
|
Ok(acc)
|
2019-08-26 11:33:38 +00:00
|
|
|
} else {
|
2020-07-29 07:38:11 +00:00
|
|
|
Err(http_err!(BAD_REQUEST, "Request body too large"))
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
2019-11-22 12:02:05 +00:00
|
|
|
}).await?;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-11-22 17:44:14 +00:00
|
|
|
let utf8_data = std::str::from_utf8(&body)
|
2019-11-22 12:02:05 +00:00
|
|
|
.map_err(|err| format_err!("Request body not uft8: {}", err))?;
|
2019-02-27 11:12:00 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
if is_json {
|
2019-11-22 17:44:14 +00:00
|
|
|
let mut params: Value = serde_json::from_str(utf8_data)?;
|
2019-11-22 12:02:05 +00:00
|
|
|
for (k, v) in uri_param {
|
2019-11-22 16:22:07 +00:00
|
|
|
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)?;
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
2019-11-22 12:02:05 +00:00
|
|
|
}
|
2019-11-22 16:22:07 +00:00
|
|
|
verify_json_object(¶ms, param_schema)?;
|
2019-11-22 12:02:05 +00:00
|
|
|
return Ok(params);
|
2019-11-22 17:44:14 +00:00
|
|
|
} else {
|
|
|
|
parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
|
2019-11-22 12:02:05 +00:00
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 15:04:24 +00:00
|
|
|
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,
|
2019-01-28 17:06:42 +00:00
|
|
|
mut parts: Parts,
|
2019-01-28 16:30:39 +00:00
|
|
|
req_body: Body,
|
2019-11-22 12:02:05 +00:00
|
|
|
) -> Result<Response<Body>, Error> {
|
2019-01-28 16:30:39 +00:00
|
|
|
|
2019-01-28 17:06:42 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-28 17:06:42 +00:00
|
|
|
let resp = hyper::client::Client::new()
|
|
|
|
.request(request)
|
2019-02-18 12:21:25 +00:00
|
|
|
.map_err(Error::from)
|
2019-08-26 11:33:38 +00:00
|
|
|
.map_ok(|mut resp| {
|
2019-02-18 05:54:12 +00:00
|
|
|
resp.extensions_mut().insert(NoLogExtension());
|
2019-02-14 12:28:41 +00:00
|
|
|
resp
|
2019-11-22 12:02:05 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2019-01-28 17:06:42 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
if reload_timezone { unsafe { tzset(); } }
|
2019-02-18 05:54:12 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
Ok(resp)
|
2019-01-28 16:30:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:44:14 +00:00
|
|
|
pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
|
2019-05-08 09:09:01 +00:00
|
|
|
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,
|
2018-11-15 07:18:48 +00:00
|
|
|
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-01-31 13:34:21 +00:00
|
|
|
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
|
|
|
|
|
2019-11-22 17:44:14 +00:00
|
|
|
let result = match info.handler {
|
2019-11-23 08:03:21 +00:00
|
|
|
ApiHandler::AsyncHttp(handler) => {
|
2019-11-22 17:44:14 +00:00
|
|
|
let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
|
|
|
|
(handler)(parts, req_body, params, info, Box::new(rpcenv)).await
|
|
|
|
}
|
|
|
|
ApiHandler::Sync(handler) => {
|
|
|
|
let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
|
|
|
|
(handler)(params, info, &mut rpcenv)
|
|
|
|
.map(|data| (formatter.format_data)(data, &rpcenv))
|
|
|
|
}
|
2019-12-16 08:59:45 +00:00
|
|
|
ApiHandler::Async(handler) => {
|
|
|
|
let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
|
|
|
|
(handler)(params, info, &mut rpcenv)
|
|
|
|
.await
|
|
|
|
.map(|data| (formatter.format_data)(data, &rpcenv))
|
|
|
|
}
|
2019-11-22 17:44:14 +00:00
|
|
|
};
|
2019-01-31 13:34:21 +00:00
|
|
|
|
2019-11-22 17:44:14 +00:00
|
|
|
let resp = match result {
|
|
|
|
Ok(resp) => resp,
|
2019-11-22 12:02:05 +00:00
|
|
|
Err(err) => {
|
|
|
|
if let Some(httperr) = err.downcast_ref::<HttpError>() {
|
|
|
|
if httperr.code == StatusCode::UNAUTHORIZED {
|
2019-12-12 14:27:07 +00:00
|
|
|
tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
|
2019-11-22 12:02:05 +00:00
|
|
|
}
|
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(); } }
|
|
|
|
|
|
|
|
Ok(resp)
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 12:33:05 +00:00
|
|
|
fn get_index(
|
|
|
|
userid: Option<Userid>,
|
|
|
|
token: Option<String>,
|
|
|
|
language: Option<String>,
|
|
|
|
api: &Arc<ApiConfig>,
|
|
|
|
parts: Parts,
|
|
|
|
) -> Response<Body> {
|
2018-12-01 12:37:49 +00:00
|
|
|
|
2019-08-03 15:06:23 +00:00
|
|
|
let nodename = proxmox::tools::nodename();
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
|
2019-02-17 17:50:40 +00:00
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
let token = token.unwrap_or_else(|| String::from(""));
|
2018-12-01 12:37:49 +00:00
|
|
|
|
2020-04-29 09:59:31 +00:00
|
|
|
let mut debug = false;
|
2020-07-21 09:10:39 +00:00
|
|
|
let mut template_file = "index";
|
2020-04-29 09:59:31 +00:00
|
|
|
|
|
|
|
if let Some(query_str) = parts.uri.query() {
|
|
|
|
for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
|
2020-06-25 08:45:49 +00:00
|
|
|
if k == "debug" && v != "0" && v != "false" {
|
2020-04-29 09:59:31 +00:00
|
|
|
debug = true;
|
2020-07-21 09:10:39 +00:00
|
|
|
} else if k == "console" {
|
|
|
|
template_file = "console";
|
2020-04-29 09:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:33:05 +00:00
|
|
|
let mut lang = String::from("");
|
|
|
|
if let Some(language) = language {
|
|
|
|
if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
|
|
|
|
lang = language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 09:59:31 +00:00
|
|
|
let data = json!({
|
2018-12-01 12:37:49 +00:00
|
|
|
"NodeName": nodename,
|
2020-08-06 13:46:01 +00:00
|
|
|
"UserName": userid,
|
2019-01-23 11:49:10 +00:00
|
|
|
"CSRFPreventionToken": token,
|
2020-09-07 12:33:05 +00:00
|
|
|
"language": lang,
|
2020-04-29 09:59:31 +00:00
|
|
|
"debug": debug,
|
2018-12-01 12:37:49 +00:00
|
|
|
});
|
|
|
|
|
2020-04-29 09:59:31 +00:00
|
|
|
let mut ct = "text/html";
|
|
|
|
|
2020-07-21 09:10:39 +00:00
|
|
|
let index = match api.render_template(template_file, &data) {
|
2020-04-29 09:59:31 +00:00
|
|
|
Ok(index) => index,
|
|
|
|
Err(err) => {
|
|
|
|
ct = "text/plain";
|
2020-07-21 09:10:36 +00:00
|
|
|
format!("Error rendering template: {}", err)
|
2020-07-21 09:10:39 +00:00
|
|
|
}
|
2020-04-29 09:59:31 +00:00
|
|
|
};
|
2018-12-01 12:37:49 +00:00
|
|
|
|
2019-02-17 17:50:40 +00:00
|
|
|
Response::builder()
|
2019-01-23 11:49:10 +00:00
|
|
|
.status(StatusCode::OK)
|
2020-04-29 09:59:31 +00:00
|
|
|
.header(header::CONTENT_TYPE, ct)
|
2019-01-23 11:49:10 +00:00
|
|
|
.body(index.into())
|
2019-02-17 17:50:40 +00:00
|
|
|
.unwrap()
|
2018-12-01 12:37:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 10:00:52 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2018-12-02 10:00:52 +00:00
|
|
|
let (content_type, _nocomp) = extension_to_content_type(&filename);
|
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
use tokio::io::AsyncReadExt;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
let mut file = File::open(filename)
|
|
|
|
.await
|
2020-07-29 07:38:11 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
let mut data: Vec<u8> = Vec::new();
|
|
|
|
file.read_to_end(&mut data)
|
|
|
|
.await
|
2020-07-29 07:38:11 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
|
2019-08-26 11:33:38 +00:00
|
|
|
|
|
|
|
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> {
|
2018-12-02 10:00:52 +00:00
|
|
|
let (content_type, _nocomp) = extension_to_content_type(&filename);
|
|
|
|
|
2019-08-26 11:33:38 +00:00
|
|
|
let file = File::open(filename)
|
|
|
|
.await
|
2020-07-29 07:38:11 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
|
2019-08-26 11:33:38 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
|
|
|
|
.map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
|
2019-08-26 11:33:38 +00:00
|
|
|
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()
|
|
|
|
)
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-12-17 07:56:52 +00:00
|
|
|
let metadata = tokio::fs::metadata(filename.clone())
|
2020-07-29 07:38:11 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
|
2019-12-17 07:56:52 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
if metadata.len() < 1024*32 {
|
|
|
|
simple_static_file_download(filename).await
|
|
|
|
} else {
|
|
|
|
chuncked_static_file_download(filename).await
|
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 12:33:05 +00:00
|
|
|
fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>, Option<String>) {
|
2019-02-16 14:52:55 +00:00
|
|
|
|
|
|
|
let mut ticket = None;
|
2020-09-07 12:33:05 +00:00
|
|
|
let mut language = None;
|
2019-02-16 14:52:55 +00:00
|
|
|
if let Some(raw_cookie) = headers.get("COOKIE") {
|
|
|
|
if let Ok(cookie) = raw_cookie.to_str() {
|
2020-09-07 12:30:44 +00:00
|
|
|
ticket = tools::extract_cookie(cookie, "PBSAuthCookie");
|
2020-09-07 12:33:05 +00:00
|
|
|
language = tools::extract_cookie(cookie, "PBSLangCookie");
|
2019-02-16 14:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
|
|
|
|
Some(Ok(v)) => Some(v.to_owned()),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2020-09-07 12:33:05 +00:00
|
|
|
(ticket, token, language)
|
2019-02-16 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
fn check_auth(
|
|
|
|
method: &hyper::Method,
|
|
|
|
ticket: &Option<String>,
|
|
|
|
token: &Option<String>,
|
|
|
|
user_info: &CachedUserInfo,
|
2020-08-06 13:46:01 +00:00
|
|
|
) -> Result<Userid, Error> {
|
2019-03-05 11:52:39 +00:00
|
|
|
let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
|
2019-02-16 14:52:55 +00:00
|
|
|
|
2020-08-12 10:05:52 +00:00
|
|
|
let ticket = ticket.as_ref().map(String::as_str);
|
|
|
|
let userid: Userid = Ticket::parse(&ticket.ok_or_else(|| format_err!("missing ticket"))?)?
|
|
|
|
.verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?;
|
2019-02-16 14:52:55 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
if !user_info.is_active_user(&userid) {
|
2020-04-16 08:01:59 +00:00
|
|
|
bail!("user account disabled or expired.");
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:52:55 +00:00
|
|
|
if method != hyper::Method::GET {
|
|
|
|
if let Some(token) = token {
|
2019-02-17 16:18:44 +00:00
|
|
|
println!("CSRF prevention token: {:?}", token);
|
2020-08-06 13:46:01 +00:00
|
|
|
verify_csrf_prevention_token(csrf_secret(), &userid, &token, -300, ticket_lifetime)?;
|
2019-02-16 14:52:55 +00:00
|
|
|
} else {
|
2019-02-17 16:18:44 +00:00
|
|
|
bail!("missing CSRF prevention token");
|
2019-02-16 14:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
Ok(userid)
|
2019-02-16 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
pub async fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> Result<Response<Body>, Error> {
|
2019-02-17 16:31:53 +00:00
|
|
|
|
|
|
|
let (parts, body) = req.into_parts();
|
|
|
|
|
|
|
|
let method = parts.method.clone();
|
2020-07-15 07:11:16 +00:00
|
|
|
let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
|
2019-02-17 16:31:53 +00:00
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
let comp_len = components.len();
|
|
|
|
|
2020-07-09 14:18:14 +00:00
|
|
|
//println!("REQUEST {} {}", method, path);
|
|
|
|
//println!("COMPO {:?}", components);
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-01-28 16:30:39 +00:00
|
|
|
let env_type = api.env_type();
|
|
|
|
let mut rpcenv = RestEnvironment::new(env_type);
|
2019-01-27 09:18:52 +00:00
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
|
|
|
|
2019-01-31 11:22:00 +00:00
|
|
|
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
|
2020-04-16 10:56:34 +00:00
|
|
|
let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
|
2019-01-31 11:22:00 +00:00
|
|
|
|
2019-01-22 11:10:38 +00:00
|
|
|
if comp_len >= 1 && components[0] == "api2" {
|
2019-02-16 14:52:55 +00:00
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
if comp_len >= 2 {
|
2019-11-22 12:02:05 +00:00
|
|
|
|
2018-11-15 07:18:48 +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-15 07:18:48 +00:00
|
|
|
|
2018-11-16 08:15:33 +00:00
|
|
|
let mut uri_param = HashMap::new();
|
|
|
|
|
2020-04-09 09:36:45 +00:00
|
|
|
if comp_len == 4 && components[2] == "access" && (
|
|
|
|
(components[3] == "ticket" && method == hyper::Method::POST) ||
|
|
|
|
(components[3] == "domains" && method == hyper::Method::GET)
|
|
|
|
) {
|
2019-01-31 11:22:00 +00:00
|
|
|
// explicitly allow those calls without auth
|
|
|
|
} else {
|
2020-09-07 12:33:05 +00:00
|
|
|
let (ticket, token, _) = extract_auth_data(&parts.headers);
|
2020-04-16 08:01:59 +00:00
|
|
|
match check_auth(&method, &ticket, &token, &user_info) {
|
2020-08-06 13:46:01 +00:00
|
|
|
Ok(userid) => rpcenv.set_user(Some(userid.to_string())),
|
2019-02-16 14:52:55 +00:00
|
|
|
Err(err) => {
|
|
|
|
// always delay unauthorized calls by 3 seconds (from start of request)
|
2020-07-29 07:38:11 +00:00
|
|
|
let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
|
2019-12-12 14:27:07 +00:00
|
|
|
tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
|
2019-11-22 12:02:05 +00:00
|
|
|
return Ok((formatter.format_error)(err));
|
2019-02-16 14:52:55 +00:00
|
|
|
}
|
2019-01-31 11:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-27 09:42:45 +00:00
|
|
|
|
2019-01-14 11:26:04 +00:00
|
|
|
match api.find_method(&components[2..], method, &mut uri_param) {
|
2019-11-21 08:36:41 +00:00
|
|
|
None => {
|
2020-07-29 07:38:11 +00:00
|
|
|
let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
|
2019-11-22 12:02:05 +00:00
|
|
|
return Ok((formatter.format_error)(err));
|
2019-04-01 06:04:12 +00:00
|
|
|
}
|
2019-11-21 08:36:41 +00:00
|
|
|
Some(api_method) => {
|
2020-04-16 08:01:59 +00:00
|
|
|
let user = rpcenv.get_user();
|
2020-04-18 06:49:20 +00:00
|
|
|
if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) {
|
2020-07-29 07:38:11 +00:00
|
|
|
let err = http_err!(FORBIDDEN, "permission check failed");
|
2020-04-16 10:56:34 +00:00
|
|
|
tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
|
2020-04-16 08:01:59 +00:00
|
|
|
return Ok((formatter.format_error)(err));
|
|
|
|
}
|
|
|
|
|
2020-03-26 11:54:20 +00:00
|
|
|
let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
|
|
|
|
proxy_protected_request(api_method, parts, body).await
|
2019-01-28 16:30:39 +00:00
|
|
|
} else {
|
2020-03-26 11:54:20 +00:00
|
|
|
handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
return Ok((formatter.format_error)(err));
|
2019-01-28 16:30:39 +00:00
|
|
|
}
|
2020-03-26 11:54:20 +00:00
|
|
|
return result;
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
2020-03-26 11:54:20 +00:00
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
2019-11-22 12:02:05 +00:00
|
|
|
} else {
|
2019-02-17 17:50:40 +00:00
|
|
|
// not Auth required for accessing files!
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-04-01 05:52:30 +00:00
|
|
|
if method != hyper::Method::GET {
|
2019-11-22 12:02:05 +00:00
|
|
|
bail!("Unsupported HTTP method {}", method);
|
2019-04-01 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 12:37:49 +00:00
|
|
|
if comp_len == 0 {
|
2020-09-07 12:33:05 +00:00
|
|
|
let (ticket, token, language) = extract_auth_data(&parts.headers);
|
2019-02-17 17:50:40 +00:00
|
|
|
if ticket != None {
|
2020-04-16 08:01:59 +00:00
|
|
|
match check_auth(&method, &ticket, &token, &user_info) {
|
2020-08-06 13:46:01 +00:00
|
|
|
Ok(userid) => {
|
|
|
|
let new_token = assemble_csrf_prevention_token(csrf_secret(), &userid);
|
2020-09-07 12:33:05 +00:00
|
|
|
return Ok(get_index(Some(userid), Some(new_token), language, &api, parts));
|
2019-04-01 05:52:30 +00:00
|
|
|
}
|
2019-08-26 11:33:38 +00:00
|
|
|
_ => {
|
2019-12-12 14:27:07 +00:00
|
|
|
tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
|
2020-09-07 12:33:05 +00:00
|
|
|
return Ok(get_index(None, None, language, &api, parts));
|
2019-08-26 11:33:38 +00:00
|
|
|
}
|
2019-02-17 17:50:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-07 12:33:05 +00:00
|
|
|
return Ok(get_index(None, None, language, &api, parts));
|
2019-02-17 17:50:40 +00:00
|
|
|
}
|
2018-12-01 12:37:49 +00:00
|
|
|
} else {
|
|
|
|
let filename = api.find_alias(&components);
|
2019-11-22 12:02:05 +00:00
|
|
|
return handle_static_file_download(filename).await;
|
2018-12-01 12:37:49 +00:00
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 07:38:11 +00:00
|
|
|
Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|