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