]> git.proxmox.com Git - proxmox-backup.git/blob - src/server/rest.rs
change index to templates using handlebars
[proxmox-backup.git] / src / server / rest.rs
1 use std::collections::HashMap;
2 use std::future::Future;
3 use std::hash::BuildHasher;
4 use std::path::{Path, PathBuf};
5 use std::pin::Pin;
6 use std::sync::Arc;
7 use std::task::{Context, Poll};
8
9 use anyhow::{bail, format_err, Error};
10 use futures::future::{self, FutureExt, TryFutureExt};
11 use futures::stream::TryStreamExt;
12 use hyper::header;
13 use hyper::http::request::Parts;
14 use hyper::{Body, Request, Response, StatusCode};
15 use serde_json::{json, Value};
16 use tokio::fs::File;
17 use tokio::time::Instant;
18 use url::form_urlencoded;
19 use handlebars::Handlebars;
20
21 use proxmox::http_err;
22 use proxmox::api::{ApiHandler, ApiMethod, HttpError};
23 use proxmox::api::{RpcEnvironment, RpcEnvironmentType, check_api_permission};
24 use proxmox::api::schema::{ObjectSchema, parse_simple_value, verify_json_object, parse_parameter_strings};
25
26 use super::environment::RestEnvironment;
27 use super::formatter::*;
28 use super::ApiConfig;
29
30 use crate::auth_helpers::*;
31 use crate::tools;
32 use crate::config::cached_user_info::CachedUserInfo;
33
34 extern "C" { fn tzset(); }
35
36 pub struct RestServer {
37 pub api_config: Arc<ApiConfig>,
38 }
39
40 impl RestServer {
41
42 pub fn new(api_config: ApiConfig) -> Self {
43 Self { api_config: Arc::new(api_config) }
44 }
45 }
46
47 impl tower_service::Service<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer {
48 type Response = ApiService;
49 type Error = Error;
50 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
51
52 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
53 Poll::Ready(Ok(()))
54 }
55
56 fn call(&mut self, ctx: &tokio_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
57 match ctx.get_ref().peer_addr() {
58 Err(err) => {
59 future::err(format_err!("unable to get peer address - {}", err)).boxed()
60 }
61 Ok(peer) => {
62 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
63 }
64 }
65 }
66 }
67
68 impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
69 type Response = ApiService;
70 type Error = Error;
71 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
72
73 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
74 Poll::Ready(Ok(()))
75 }
76
77 fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
78 match ctx.peer_addr() {
79 Err(err) => {
80 future::err(format_err!("unable to get peer address - {}", err)).boxed()
81 }
82 Ok(peer) => {
83 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
84 }
85 }
86 }
87 }
88
89 pub struct ApiService {
90 pub peer: std::net::SocketAddr,
91 pub api_config: Arc<ApiConfig>,
92 }
93
94 fn log_response(
95 peer: &std::net::SocketAddr,
96 method: hyper::Method,
97 path: &str,
98 resp: &Response<Body>,
99 ) {
100
101 if resp.extensions().get::<NoLogExtension>().is_some() { return; };
102
103 let status = resp.status();
104
105 if !(status.is_success() || status.is_informational()) {
106 let reason = status.canonical_reason().unwrap_or("unknown reason");
107
108 let mut message = "request failed";
109 if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
110 message = &data.0;
111 }
112
113 log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
114 }
115 }
116
117 impl tower_service::Service<Request<Body>> for ApiService {
118 type Response = Response<Body>;
119 type Error = Error;
120 type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
121
122 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
123 Poll::Ready(Ok(()))
124 }
125
126 fn call(&mut self, req: Request<Body>) -> Self::Future {
127 let path = req.uri().path().to_owned();
128 let method = req.method().clone();
129
130 let peer = self.peer;
131 handle_request(self.api_config.clone(), req)
132 .map(move |result| match result {
133 Ok(res) => {
134 log_response(&peer, method, &path, &res);
135 Ok::<_, Self::Error>(res)
136 }
137 Err(err) => {
138 if let Some(apierr) = err.downcast_ref::<HttpError>() {
139 let mut resp = Response::new(Body::from(apierr.message.clone()));
140 *resp.status_mut() = apierr.code;
141 log_response(&peer, method, &path, &resp);
142 Ok(resp)
143 } else {
144 let mut resp = Response::new(Body::from(err.to_string()));
145 *resp.status_mut() = StatusCode::BAD_REQUEST;
146 log_response(&peer, method, &path, &resp);
147 Ok(resp)
148 }
149 }
150 })
151 .boxed()
152 }
153 }
154
155 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
156 param_schema: &ObjectSchema,
157 form: &str, // x-www-form-urlencoded body data
158 parts: &Parts,
159 uri_param: &HashMap<String, String, S>,
160 ) -> Result<Value, Error> {
161
162 let mut param_list: Vec<(String, String)> = vec![];
163
164 if !form.is_empty() {
165 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
166 param_list.push((k, v));
167 }
168 }
169
170 if let Some(query_str) = parts.uri.query() {
171 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
172 if k == "_dc" { continue; } // skip extjs "disable cache" parameter
173 param_list.push((k, v));
174 }
175 }
176
177 for (k, v) in uri_param {
178 param_list.push((k.clone(), v.clone()));
179 }
180
181 let params = parse_parameter_strings(&param_list, param_schema, true)?;
182
183 Ok(params)
184 }
185
186 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
187 param_schema: &ObjectSchema,
188 parts: Parts,
189 req_body: Body,
190 uri_param: HashMap<String, String, S>,
191 ) -> Result<Value, Error> {
192
193 let mut is_json = false;
194
195 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
196 match value.to_str().map(|v| v.split(';').next()) {
197 Ok(Some("application/x-www-form-urlencoded")) => {
198 is_json = false;
199 }
200 Ok(Some("application/json")) => {
201 is_json = true;
202 }
203 _ => bail!("unsupported content type {:?}", value.to_str()),
204 }
205 }
206
207 let body = req_body
208 .map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err)))
209 .try_fold(Vec::new(), |mut acc, chunk| async move {
210 if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
211 acc.extend_from_slice(&*chunk);
212 Ok(acc)
213 } else {
214 Err(http_err!(BAD_REQUEST, "Request body too large".to_string()))
215 }
216 }).await?;
217
218 let utf8_data = std::str::from_utf8(&body)
219 .map_err(|err| format_err!("Request body not uft8: {}", err))?;
220
221 if is_json {
222 let mut params: Value = serde_json::from_str(utf8_data)?;
223 for (k, v) in uri_param {
224 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
225 params[&k] = parse_simple_value(&v, prop_schema)?;
226 }
227 }
228 verify_json_object(&params, param_schema)?;
229 return Ok(params);
230 } else {
231 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
232 }
233 }
234
235 struct NoLogExtension();
236
237 async fn proxy_protected_request(
238 info: &'static ApiMethod,
239 mut parts: Parts,
240 req_body: Body,
241 ) -> Result<Response<Body>, Error> {
242
243 let mut uri_parts = parts.uri.clone().into_parts();
244
245 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
246 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
247 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
248
249 parts.uri = new_uri;
250
251 let request = Request::from_parts(parts, req_body);
252
253 let reload_timezone = info.reload_timezone;
254
255 let resp = hyper::client::Client::new()
256 .request(request)
257 .map_err(Error::from)
258 .map_ok(|mut resp| {
259 resp.extensions_mut().insert(NoLogExtension());
260 resp
261 })
262 .await?;
263
264 if reload_timezone { unsafe { tzset(); } }
265
266 Ok(resp)
267 }
268
269 pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
270 mut rpcenv: Env,
271 info: &'static ApiMethod,
272 formatter: &'static OutputFormatter,
273 parts: Parts,
274 req_body: Body,
275 uri_param: HashMap<String, String, S>,
276 ) -> Result<Response<Body>, Error> {
277
278 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
279
280 let result = match info.handler {
281 ApiHandler::AsyncHttp(handler) => {
282 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
283 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
284 }
285 ApiHandler::Sync(handler) => {
286 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
287 (handler)(params, info, &mut rpcenv)
288 .map(|data| (formatter.format_data)(data, &rpcenv))
289 }
290 ApiHandler::Async(handler) => {
291 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
292 (handler)(params, info, &mut rpcenv)
293 .await
294 .map(|data| (formatter.format_data)(data, &rpcenv))
295 }
296 };
297
298 let resp = match result {
299 Ok(resp) => resp,
300 Err(err) => {
301 if let Some(httperr) = err.downcast_ref::<HttpError>() {
302 if httperr.code == StatusCode::UNAUTHORIZED {
303 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
304 }
305 }
306 (formatter.format_error)(err)
307 }
308 };
309
310 if info.reload_timezone { unsafe { tzset(); } }
311
312 Ok(resp)
313 }
314
315 fn get_index(username: Option<String>, token: Option<String>, template: &Handlebars, parts: Parts) -> Response<Body> {
316
317 let nodename = proxmox::tools::nodename();
318 let username = username.unwrap_or_else(|| String::from(""));
319
320 let token = token.unwrap_or_else(|| String::from(""));
321
322 let mut debug = false;
323
324 if let Some(query_str) = parts.uri.query() {
325 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
326 if k == "debug" && v == "1" || v == "true" {
327 debug = true;
328 }
329 }
330 }
331
332 let data = json!({
333 "NodeName": nodename,
334 "UserName": username,
335 "CSRFPreventionToken": token,
336 "debug": debug,
337 });
338
339 let mut ct = "text/html";
340
341 let index = match template.render("index", &data) {
342 Ok(index) => index,
343 Err(err) => {
344 ct = "text/plain";
345 format!("Error rendering template: {}", err.desc)
346 },
347 };
348
349 Response::builder()
350 .status(StatusCode::OK)
351 .header(header::CONTENT_TYPE, ct)
352 .body(index.into())
353 .unwrap()
354 }
355
356 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
357
358 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
359 return match ext {
360 "css" => ("text/css", false),
361 "html" => ("text/html", false),
362 "js" => ("application/javascript", false),
363 "json" => ("application/json", false),
364 "map" => ("application/json", false),
365 "png" => ("image/png", true),
366 "ico" => ("image/x-icon", true),
367 "gif" => ("image/gif", true),
368 "svg" => ("image/svg+xml", false),
369 "jar" => ("application/java-archive", true),
370 "woff" => ("application/font-woff", true),
371 "woff2" => ("application/font-woff2", true),
372 "ttf" => ("application/font-snft", true),
373 "pdf" => ("application/pdf", true),
374 "epub" => ("application/epub+zip", true),
375 "mp3" => ("audio/mpeg", true),
376 "oga" => ("audio/ogg", true),
377 "tgz" => ("application/x-compressed-tar", true),
378 _ => ("application/octet-stream", false),
379 };
380 }
381
382 ("application/octet-stream", false)
383 }
384
385 async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
386
387 let (content_type, _nocomp) = extension_to_content_type(&filename);
388
389 use tokio::io::AsyncReadExt;
390
391 let mut file = File::open(filename)
392 .await
393 .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
394
395 let mut data: Vec<u8> = Vec::new();
396 file.read_to_end(&mut data)
397 .await
398 .map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))?;
399
400 let mut response = Response::new(data.into());
401 response.headers_mut().insert(
402 header::CONTENT_TYPE,
403 header::HeaderValue::from_static(content_type));
404 Ok(response)
405 }
406
407 async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
408 let (content_type, _nocomp) = extension_to_content_type(&filename);
409
410 let file = File::open(filename)
411 .await
412 .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
413
414 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
415 .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
416 let body = Body::wrap_stream(payload);
417
418 // fixme: set other headers ?
419 Ok(Response::builder()
420 .status(StatusCode::OK)
421 .header(header::CONTENT_TYPE, content_type)
422 .body(body)
423 .unwrap()
424 )
425 }
426
427 async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
428
429 let metadata = tokio::fs::metadata(filename.clone())
430 .map_err(|err| http_err!(BAD_REQUEST, format!("File access problems: {}", err)))
431 .await?;
432
433 if metadata.len() < 1024*32 {
434 simple_static_file_download(filename).await
435 } else {
436 chuncked_static_file_download(filename).await
437 }
438 }
439
440 fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>) {
441
442 let mut ticket = None;
443 if let Some(raw_cookie) = headers.get("COOKIE") {
444 if let Ok(cookie) = raw_cookie.to_str() {
445 ticket = tools::extract_auth_cookie(cookie, "PBSAuthCookie");
446 }
447 }
448
449 let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
450 Some(Ok(v)) => Some(v.to_owned()),
451 _ => None,
452 };
453
454 (ticket, token)
455 }
456
457 fn check_auth(
458 method: &hyper::Method,
459 ticket: &Option<String>,
460 token: &Option<String>,
461 user_info: &CachedUserInfo,
462 ) -> Result<String, Error> {
463
464 let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
465
466 let username = match ticket {
467 Some(ticket) => match tools::ticket::verify_rsa_ticket(public_auth_key(), "PBS", &ticket, None, -300, ticket_lifetime) {
468 Ok((_age, Some(username))) => username.to_owned(),
469 Ok((_, None)) => bail!("ticket without username."),
470 Err(err) => return Err(err),
471 }
472 None => bail!("missing ticket"),
473 };
474
475 if !user_info.is_active_user(&username) {
476 bail!("user account disabled or expired.");
477 }
478
479 if method != hyper::Method::GET {
480 if let Some(token) = token {
481 println!("CSRF prevention token: {:?}", token);
482 verify_csrf_prevention_token(csrf_secret(), &username, &token, -300, ticket_lifetime)?;
483 } else {
484 bail!("missing CSRF prevention token");
485 }
486 }
487
488 Ok(username)
489 }
490
491 pub async fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> Result<Response<Body>, Error> {
492
493 let (parts, body) = req.into_parts();
494
495 let method = parts.method.clone();
496 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
497
498 let comp_len = components.len();
499
500 println!("REQUEST {} {}", method, path);
501 println!("COMPO {:?}", components);
502
503 let env_type = api.env_type();
504 let mut rpcenv = RestEnvironment::new(env_type);
505
506 let user_info = CachedUserInfo::new()?;
507
508 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
509 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
510
511 if comp_len >= 1 && components[0] == "api2" {
512
513 if comp_len >= 2 {
514
515 let format = components[1];
516
517 let formatter = match format {
518 "json" => &JSON_FORMATTER,
519 "extjs" => &EXTJS_FORMATTER,
520 _ => bail!("Unsupported output format '{}'.", format),
521 };
522
523 let mut uri_param = HashMap::new();
524
525 if comp_len == 4 && components[2] == "access" && (
526 (components[3] == "ticket" && method == hyper::Method::POST) ||
527 (components[3] == "domains" && method == hyper::Method::GET)
528 ) {
529 // explicitly allow those calls without auth
530 } else {
531 let (ticket, token) = extract_auth_data(&parts.headers);
532 match check_auth(&method, &ticket, &token, &user_info) {
533 Ok(username) => rpcenv.set_user(Some(username)),
534 Err(err) => {
535 // always delay unauthorized calls by 3 seconds (from start of request)
536 let err = http_err!(UNAUTHORIZED, format!("authentication failed - {}", err));
537 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
538 return Ok((formatter.format_error)(err));
539 }
540 }
541 }
542
543 match api.find_method(&components[2..], method, &mut uri_param) {
544 None => {
545 let err = http_err!(NOT_FOUND, "Path not found.".to_string());
546 return Ok((formatter.format_error)(err));
547 }
548 Some(api_method) => {
549 let user = rpcenv.get_user();
550 if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) {
551 let err = http_err!(FORBIDDEN, format!("permission check failed"));
552 tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
553 return Ok((formatter.format_error)(err));
554 }
555
556 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
557 proxy_protected_request(api_method, parts, body).await
558 } else {
559 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
560 };
561
562 if let Err(err) = result {
563 return Ok((formatter.format_error)(err));
564 }
565 return result;
566 }
567 }
568
569 }
570 } else {
571 // not Auth required for accessing files!
572
573 if method != hyper::Method::GET {
574 bail!("Unsupported HTTP method {}", method);
575 }
576
577 if comp_len == 0 {
578 let (ticket, token) = extract_auth_data(&parts.headers);
579 if ticket != None {
580 match check_auth(&method, &ticket, &token, &user_info) {
581 Ok(username) => {
582 let new_token = assemble_csrf_prevention_token(csrf_secret(), &username);
583 return Ok(get_index(Some(username), Some(new_token), &api.templates, parts));
584 }
585 _ => {
586 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
587 return Ok(get_index(None, None, &api.templates, parts));
588 }
589 }
590 } else {
591 return Ok(get_index(None, None, &api.templates, parts));
592 }
593 } else {
594 let filename = api.find_alias(&components);
595 return handle_static_file_download(filename).await;
596 }
597 }
598
599 Err(http_err!(NOT_FOUND, "Path not found.".to_string()))
600 }