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