]> git.proxmox.com Git - proxmox-backup.git/blob - src/server/rest.rs
server/rest: implement request access log
[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, Mutex};
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::{self, HeaderMap};
13 use hyper::body::HttpBody;
14 use hyper::http::request::Parts;
15 use hyper::{Body, Request, Response, StatusCode};
16 use lazy_static::lazy_static;
17 use serde_json::{json, Value};
18 use tokio::fs::File;
19 use tokio::time::Instant;
20 use url::form_urlencoded;
21 use regex::Regex;
22
23 use proxmox::http_err;
24 use proxmox::api::{
25 ApiHandler,
26 ApiMethod,
27 HttpError,
28 Permission,
29 RpcEnvironment,
30 RpcEnvironmentType,
31 check_api_permission,
32 };
33 use proxmox::api::schema::{
34 ObjectSchema,
35 parse_parameter_strings,
36 parse_simple_value,
37 verify_json_object,
38 };
39
40 use super::environment::RestEnvironment;
41 use super::formatter::*;
42 use super::ApiConfig;
43
44 use crate::auth_helpers::*;
45 use crate::api2::types::Userid;
46 use crate::tools;
47 use crate::tools::FileLogger;
48 use crate::tools::ticket::Ticket;
49 use crate::config::cached_user_info::CachedUserInfo;
50
51 extern "C" { fn tzset(); }
52
53 pub struct RestServer {
54 pub api_config: Arc<ApiConfig>,
55 }
56
57 const MAX_URI_QUERY_LENGTH: usize = 3072;
58
59 impl RestServer {
60
61 pub fn new(api_config: ApiConfig) -> Self {
62 Self { api_config: Arc::new(api_config) }
63 }
64 }
65
66 impl tower_service::Service<&tokio_openssl::SslStream<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_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
76 match ctx.get_ref().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 impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
88 type Response = ApiService;
89 type Error = Error;
90 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
91
92 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
93 Poll::Ready(Ok(()))
94 }
95
96 fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
97 match ctx.peer_addr() {
98 Err(err) => {
99 future::err(format_err!("unable to get peer address - {}", err)).boxed()
100 }
101 Ok(peer) => {
102 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
103 }
104 }
105 }
106 }
107
108 pub struct ApiService {
109 pub peer: std::net::SocketAddr,
110 pub api_config: Arc<ApiConfig>,
111 }
112
113 fn log_response(
114 logfile: Option<&Mutex<FileLogger>>,
115 peer: &std::net::SocketAddr,
116 method: hyper::Method,
117 path_query: &str,
118 resp: &Response<Body>,
119 ) {
120
121 if resp.extensions().get::<NoLogExtension>().is_some() { return; };
122
123 // we also log URL-to-long requests, so avoid message bigger than PIPE_BUF (4k on Linux)
124 // to profit from atomicty guarantees for O_APPEND opened logfiles
125 let path = &path_query[..MAX_URI_QUERY_LENGTH.min(path_query.len())];
126
127 let status = resp.status();
128
129 if !(status.is_success() || status.is_informational()) {
130 let reason = status.canonical_reason().unwrap_or("unknown reason");
131
132 let mut message = "request failed";
133 if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
134 message = &data.0;
135 }
136
137 log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
138 }
139 if let Some(logfile) = logfile {
140 let user = match resp.extensions().get::<Userid>() {
141 Some(userid) => userid.as_str(),
142 None => "-",
143 };
144 let now = proxmox::tools::time::epoch_i64();
145 // time format which apache/nginx use (by default), copied from pve-http-server
146 let datetime = proxmox::tools::time::strftime_local("%d/%m/%Y:%H:%M:%S %z", now)
147 .unwrap_or("-".into());
148
149 logfile
150 .lock()
151 .unwrap()
152 .log(format!(
153 "{} - {} [{}] \"{} {}\" {} {}",
154 peer.ip(),
155 user,
156 datetime,
157 method.as_str(),
158 path,
159 status.as_str(),
160 resp.body().size_hint().lower(),
161 ));
162 }
163 }
164
165 fn get_proxied_peer(headers: &HeaderMap) -> Option<std::net::SocketAddr> {
166 lazy_static! {
167 static ref RE: Regex = Regex::new(r#"for="([^"]+)""#).unwrap();
168 }
169 let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
170 let capture = RE.captures(&forwarded)?;
171 let rhost = capture.get(1)?.as_str();
172
173 rhost.parse().ok()
174 }
175
176 impl tower_service::Service<Request<Body>> for ApiService {
177 type Response = Response<Body>;
178 type Error = Error;
179 type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
180
181 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
182 Poll::Ready(Ok(()))
183 }
184
185 fn call(&mut self, req: Request<Body>) -> Self::Future {
186 let path = req.uri().path_and_query().unwrap().as_str().to_owned();
187 let method = req.method().clone();
188
189 let config = Arc::clone(&self.api_config);
190 let peer = match get_proxied_peer(req.headers()) {
191 Some(proxied_peer) => proxied_peer,
192 None => self.peer,
193 };
194 async move {
195 let response = match handle_request(Arc::clone(&config), req, &peer).await {
196 Ok(response) => response,
197 Err(err) => {
198 let (err, code) = match err.downcast_ref::<HttpError>() {
199 Some(apierr) => (apierr.message.clone(), apierr.code),
200 _ => (err.to_string(), StatusCode::BAD_REQUEST),
201 };
202 Response::builder().status(code).body(err.into())?
203 }
204 };
205 let logger = config.get_file_log();
206 log_response(logger, &peer, method, &path, &response);
207 Ok(response)
208 }
209 .boxed()
210 }
211 }
212
213 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
214 param_schema: &ObjectSchema,
215 form: &str, // x-www-form-urlencoded body data
216 parts: &Parts,
217 uri_param: &HashMap<String, String, S>,
218 ) -> Result<Value, Error> {
219
220 let mut param_list: Vec<(String, String)> = vec![];
221
222 if !form.is_empty() {
223 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
224 param_list.push((k, v));
225 }
226 }
227
228 if let Some(query_str) = parts.uri.query() {
229 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
230 if k == "_dc" { continue; } // skip extjs "disable cache" parameter
231 param_list.push((k, v));
232 }
233 }
234
235 for (k, v) in uri_param {
236 param_list.push((k.clone(), v.clone()));
237 }
238
239 let params = parse_parameter_strings(&param_list, param_schema, true)?;
240
241 Ok(params)
242 }
243
244 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
245 param_schema: &ObjectSchema,
246 parts: Parts,
247 req_body: Body,
248 uri_param: HashMap<String, String, S>,
249 ) -> Result<Value, Error> {
250
251 let mut is_json = false;
252
253 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
254 match value.to_str().map(|v| v.split(';').next()) {
255 Ok(Some("application/x-www-form-urlencoded")) => {
256 is_json = false;
257 }
258 Ok(Some("application/json")) => {
259 is_json = true;
260 }
261 _ => bail!("unsupported content type {:?}", value.to_str()),
262 }
263 }
264
265 let body = req_body
266 .map_err(|err| http_err!(BAD_REQUEST, "Promlems reading request body: {}", err))
267 .try_fold(Vec::new(), |mut acc, chunk| async move {
268 if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
269 acc.extend_from_slice(&*chunk);
270 Ok(acc)
271 } else {
272 Err(http_err!(BAD_REQUEST, "Request body too large"))
273 }
274 }).await?;
275
276 let utf8_data = std::str::from_utf8(&body)
277 .map_err(|err| format_err!("Request body not uft8: {}", err))?;
278
279 if is_json {
280 let mut params: Value = serde_json::from_str(utf8_data)?;
281 for (k, v) in uri_param {
282 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
283 params[&k] = parse_simple_value(&v, prop_schema)?;
284 }
285 }
286 verify_json_object(&params, param_schema)?;
287 return Ok(params);
288 } else {
289 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
290 }
291 }
292
293 struct NoLogExtension();
294
295 async fn proxy_protected_request(
296 info: &'static ApiMethod,
297 mut parts: Parts,
298 req_body: Body,
299 peer: &std::net::SocketAddr,
300 ) -> Result<Response<Body>, Error> {
301
302 let mut uri_parts = parts.uri.clone().into_parts();
303
304 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
305 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
306 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
307
308 parts.uri = new_uri;
309
310 let mut request = Request::from_parts(parts, req_body);
311 request
312 .headers_mut()
313 .insert(header::FORWARDED, format!("for=\"{}\";", peer).parse().unwrap());
314
315 let reload_timezone = info.reload_timezone;
316
317 let resp = hyper::client::Client::new()
318 .request(request)
319 .map_err(Error::from)
320 .map_ok(|mut resp| {
321 resp.extensions_mut().insert(NoLogExtension());
322 resp
323 })
324 .await?;
325
326 if reload_timezone { unsafe { tzset(); } }
327
328 Ok(resp)
329 }
330
331 pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
332 mut rpcenv: Env,
333 info: &'static ApiMethod,
334 formatter: &'static OutputFormatter,
335 parts: Parts,
336 req_body: Body,
337 uri_param: HashMap<String, String, S>,
338 ) -> Result<Response<Body>, Error> {
339
340 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
341
342 let result = match info.handler {
343 ApiHandler::AsyncHttp(handler) => {
344 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
345 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
346 }
347 ApiHandler::Sync(handler) => {
348 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
349 (handler)(params, info, &mut rpcenv)
350 .map(|data| (formatter.format_data)(data, &rpcenv))
351 }
352 ApiHandler::Async(handler) => {
353 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
354 (handler)(params, info, &mut rpcenv)
355 .await
356 .map(|data| (formatter.format_data)(data, &rpcenv))
357 }
358 };
359
360 let resp = match result {
361 Ok(resp) => resp,
362 Err(err) => {
363 if let Some(httperr) = err.downcast_ref::<HttpError>() {
364 if httperr.code == StatusCode::UNAUTHORIZED {
365 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
366 }
367 }
368 (formatter.format_error)(err)
369 }
370 };
371
372 if info.reload_timezone { unsafe { tzset(); } }
373
374 Ok(resp)
375 }
376
377 fn get_index(
378 userid: Option<Userid>,
379 csrf_token: Option<String>,
380 language: Option<String>,
381 api: &Arc<ApiConfig>,
382 parts: Parts,
383 ) -> Response<Body> {
384
385 let nodename = proxmox::tools::nodename();
386 let user = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
387
388 let csrf_token = csrf_token.unwrap_or_else(|| String::from(""));
389
390 let mut debug = false;
391 let mut template_file = "index";
392
393 if let Some(query_str) = parts.uri.query() {
394 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
395 if k == "debug" && v != "0" && v != "false" {
396 debug = true;
397 } else if k == "console" {
398 template_file = "console";
399 }
400 }
401 }
402
403 let mut lang = String::from("");
404 if let Some(language) = language {
405 if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
406 lang = language;
407 }
408 }
409
410 let data = json!({
411 "NodeName": nodename,
412 "UserName": user,
413 "CSRFPreventionToken": csrf_token,
414 "language": lang,
415 "debug": debug,
416 });
417
418 let (ct, index) = match api.render_template(template_file, &data) {
419 Ok(index) => ("text/html", index),
420 Err(err) => {
421 ("text/plain", format!("Error rendering template: {}", err))
422 }
423 };
424
425 let mut resp = Response::builder()
426 .status(StatusCode::OK)
427 .header(header::CONTENT_TYPE, ct)
428 .body(index.into())
429 .unwrap();
430
431 if let Some(userid) = userid {
432 resp.extensions_mut().insert(userid);
433 }
434
435 resp
436 }
437
438 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
439
440 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
441 return match ext {
442 "css" => ("text/css", false),
443 "html" => ("text/html", false),
444 "js" => ("application/javascript", false),
445 "json" => ("application/json", false),
446 "map" => ("application/json", false),
447 "png" => ("image/png", true),
448 "ico" => ("image/x-icon", true),
449 "gif" => ("image/gif", true),
450 "svg" => ("image/svg+xml", false),
451 "jar" => ("application/java-archive", true),
452 "woff" => ("application/font-woff", true),
453 "woff2" => ("application/font-woff2", true),
454 "ttf" => ("application/font-snft", true),
455 "pdf" => ("application/pdf", true),
456 "epub" => ("application/epub+zip", true),
457 "mp3" => ("audio/mpeg", true),
458 "oga" => ("audio/ogg", true),
459 "tgz" => ("application/x-compressed-tar", true),
460 _ => ("application/octet-stream", false),
461 };
462 }
463
464 ("application/octet-stream", false)
465 }
466
467 async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
468
469 let (content_type, _nocomp) = extension_to_content_type(&filename);
470
471 use tokio::io::AsyncReadExt;
472
473 let mut file = File::open(filename)
474 .await
475 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
476
477 let mut data: Vec<u8> = Vec::new();
478 file.read_to_end(&mut data)
479 .await
480 .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
481
482 let mut response = Response::new(data.into());
483 response.headers_mut().insert(
484 header::CONTENT_TYPE,
485 header::HeaderValue::from_static(content_type));
486 Ok(response)
487 }
488
489 async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
490 let (content_type, _nocomp) = extension_to_content_type(&filename);
491
492 let file = File::open(filename)
493 .await
494 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
495
496 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
497 .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
498 let body = Body::wrap_stream(payload);
499
500 // fixme: set other headers ?
501 Ok(Response::builder()
502 .status(StatusCode::OK)
503 .header(header::CONTENT_TYPE, content_type)
504 .body(body)
505 .unwrap()
506 )
507 }
508
509 async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
510
511 let metadata = tokio::fs::metadata(filename.clone())
512 .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
513 .await?;
514
515 if metadata.len() < 1024*32 {
516 simple_static_file_download(filename).await
517 } else {
518 chuncked_static_file_download(filename).await
519 }
520 }
521
522 fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>, Option<String>) {
523
524 let mut ticket = None;
525 let mut language = None;
526 if let Some(raw_cookie) = headers.get("COOKIE") {
527 if let Ok(cookie) = raw_cookie.to_str() {
528 ticket = tools::extract_cookie(cookie, "PBSAuthCookie");
529 language = tools::extract_cookie(cookie, "PBSLangCookie");
530 }
531 }
532
533 let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
534 Some(Ok(v)) => Some(v.to_owned()),
535 _ => None,
536 };
537
538 (ticket, token, language)
539 }
540
541 fn check_auth(
542 method: &hyper::Method,
543 ticket: &Option<String>,
544 token: &Option<String>,
545 user_info: &CachedUserInfo,
546 ) -> Result<Userid, Error> {
547 let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
548
549 let ticket = ticket.as_ref().map(String::as_str);
550 let userid: Userid = Ticket::parse(&ticket.ok_or_else(|| format_err!("missing ticket"))?)?
551 .verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?;
552
553 if !user_info.is_active_user(&userid) {
554 bail!("user account disabled or expired.");
555 }
556
557 if method != hyper::Method::GET {
558 if let Some(token) = token {
559 verify_csrf_prevention_token(csrf_secret(), &userid, &token, -300, ticket_lifetime)?;
560 } else {
561 bail!("missing CSRF prevention token");
562 }
563 }
564
565 Ok(userid)
566 }
567
568 async fn handle_request(
569 api: Arc<ApiConfig>,
570 req: Request<Body>,
571 peer: &std::net::SocketAddr,
572 ) -> Result<Response<Body>, Error> {
573
574 let (parts, body) = req.into_parts();
575 let method = parts.method.clone();
576 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
577
578 let comp_len = components.len();
579
580 let query = parts.uri.query().unwrap_or_default();
581 if path.len() + query.len() > MAX_URI_QUERY_LENGTH {
582 return Ok(Response::builder()
583 .status(StatusCode::URI_TOO_LONG)
584 .body("".into())
585 .unwrap());
586 }
587
588 let env_type = api.env_type();
589 let mut rpcenv = RestEnvironment::new(env_type);
590
591 rpcenv.set_client_ip(Some(*peer));
592
593 let user_info = CachedUserInfo::new()?;
594
595 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
596 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
597
598 if comp_len >= 1 && components[0] == "api2" {
599
600 if comp_len >= 2 {
601
602 let format = components[1];
603
604 let formatter = match format {
605 "json" => &JSON_FORMATTER,
606 "extjs" => &EXTJS_FORMATTER,
607 _ => bail!("Unsupported output format '{}'.", format),
608 };
609
610 let mut uri_param = HashMap::new();
611 let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
612
613 let mut auth_required = true;
614 if let Some(api_method) = api_method {
615 if let Permission::World = *api_method.access.permission {
616 auth_required = false; // no auth for endpoints with World permission
617 }
618 }
619
620 if auth_required {
621 let (ticket, token, _) = extract_auth_data(&parts.headers);
622 match check_auth(&method, &ticket, &token, &user_info) {
623 Ok(userid) => rpcenv.set_user(Some(userid.to_string())),
624 Err(err) => {
625 // always delay unauthorized calls by 3 seconds (from start of request)
626 let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
627 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
628 return Ok((formatter.format_error)(err));
629 }
630 }
631 }
632
633 match api_method {
634 None => {
635 let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
636 return Ok((formatter.format_error)(err));
637 }
638 Some(api_method) => {
639 let user = rpcenv.get_user();
640 if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) {
641 let err = http_err!(FORBIDDEN, "permission check failed");
642 tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
643 return Ok((formatter.format_error)(err));
644 }
645
646 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
647 proxy_protected_request(api_method, parts, body, peer).await
648 } else {
649 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
650 };
651
652 let mut response = match result {
653 Ok(resp) => resp,
654 Err(err) => (formatter.format_error)(err),
655 };
656
657 if let Some(user) = user {
658 let userid: Userid = user.parse()?;
659 response.extensions_mut().insert(userid);
660 }
661
662 return Ok(response);
663 }
664 }
665
666 }
667 } else {
668 // not Auth required for accessing files!
669
670 if method != hyper::Method::GET {
671 bail!("Unsupported HTTP method {}", method);
672 }
673
674 if comp_len == 0 {
675 let (ticket, token, language) = extract_auth_data(&parts.headers);
676 if ticket != None {
677 match check_auth(&method, &ticket, &token, &user_info) {
678 Ok(userid) => {
679 let new_token = assemble_csrf_prevention_token(csrf_secret(), &userid);
680 return Ok(get_index(Some(userid), Some(new_token), language, &api, parts));
681 }
682 _ => {
683 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
684 return Ok(get_index(None, None, language, &api, parts));
685 }
686 }
687 } else {
688 return Ok(get_index(None, None, language, &api, parts));
689 }
690 } else {
691 let filename = api.find_alias(&components);
692 return handle_static_file_download(filename).await;
693 }
694 }
695
696 Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
697 }