2020-07-21 09:10:38 +00:00
|
|
|
use std::net::TcpListener;
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
|
|
use anyhow::{bail, format_err, Error};
|
2020-07-29 11:50:27 +00:00
|
|
|
use futures::future::{FutureExt, TryFutureExt};
|
2020-07-21 09:10:38 +00:00
|
|
|
use hyper::body::Body;
|
|
|
|
use hyper::http::request::Parts;
|
|
|
|
use hyper::upgrade::Upgraded;
|
|
|
|
use nix::fcntl::{fcntl, FcntlArg, FdFlag};
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use tokio::io::{AsyncBufReadExt, BufReader};
|
|
|
|
|
2019-11-21 13:14:54 +00:00
|
|
|
use proxmox::api::router::{Router, SubdirMap};
|
2020-07-21 09:10:38 +00:00
|
|
|
use proxmox::api::{
|
|
|
|
api, schema::*, ApiHandler, ApiMethod, ApiResponseFuture, Permission, RpcEnvironment,
|
|
|
|
};
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::list_subdirs_api_method;
|
2020-07-21 09:10:38 +00:00
|
|
|
use proxmox::tools::websocket::WebSocket;
|
|
|
|
use proxmox::{identity, sortable};
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2020-07-21 09:10:38 +00:00
|
|
|
use crate::api2::types::*;
|
|
|
|
use crate::config::acl::PRIV_SYS_CONSOLE;
|
|
|
|
use crate::server::WorkerTask;
|
|
|
|
use crate::tools;
|
2020-08-12 10:05:52 +00:00
|
|
|
use crate::tools::ticket::{self, Empty, Ticket};
|
2020-07-21 09:10:38 +00:00
|
|
|
|
|
|
|
pub mod disks;
|
2019-12-22 16:35:06 +00:00
|
|
|
pub mod dns;
|
2020-07-21 09:10:38 +00:00
|
|
|
pub mod network;
|
2020-07-29 07:19:56 +00:00
|
|
|
pub mod tasks;
|
|
|
|
|
2020-07-21 09:10:38 +00:00
|
|
|
pub(crate) mod rrd;
|
2020-07-29 07:19:56 +00:00
|
|
|
|
|
|
|
mod apt;
|
|
|
|
mod journal;
|
2019-01-26 16:54:18 +00:00
|
|
|
mod services;
|
2019-12-18 16:24:04 +00:00
|
|
|
mod status;
|
2020-07-21 11:41:06 +00:00
|
|
|
mod subscription;
|
2020-07-21 09:10:38 +00:00
|
|
|
mod syslog;
|
|
|
|
mod time;
|
|
|
|
|
|
|
|
pub const SHELL_CMD_SCHEMA: Schema = StringSchema::new("The command to run.")
|
|
|
|
.format(&ApiStringFormat::Enum(&[
|
|
|
|
EnumEntry::new("login", "Login"),
|
|
|
|
EnumEntry::new("upgrade", "Upgrade"),
|
|
|
|
]))
|
|
|
|
.schema();
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
cmd: {
|
|
|
|
schema: SHELL_CMD_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: Object,
|
|
|
|
description: "Object with the user, ticket, port and upid",
|
|
|
|
properties: {
|
|
|
|
user: {
|
|
|
|
description: "",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
ticket: {
|
|
|
|
description: "",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
port: {
|
|
|
|
description: "",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
upid: {
|
|
|
|
description: "",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
description: "Restricted to users on realm 'pam'",
|
2020-07-23 10:38:10 +00:00
|
|
|
permission: &Permission::Privilege(&["system"], PRIV_SYS_CONSOLE, false),
|
2020-07-21 09:10:38 +00:00
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// Call termproxy and return shell ticket
|
|
|
|
async fn termproxy(
|
|
|
|
cmd: Option<String>,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv
|
2020-07-21 09:10:38 +00:00
|
|
|
.get_user()
|
2020-08-06 13:46:01 +00:00
|
|
|
.ok_or_else(|| format_err!("unknown user"))?
|
|
|
|
.parse()?;
|
2020-07-21 09:10:38 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
if userid.realm() != "pam" {
|
2020-07-21 09:10:38 +00:00
|
|
|
bail!("only pam users can use the console");
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:38:10 +00:00
|
|
|
let path = "/system";
|
2020-07-21 09:10:38 +00:00
|
|
|
|
|
|
|
// use port 0 and let the kernel decide which port is free
|
|
|
|
let listener = TcpListener::bind("localhost:0")?;
|
|
|
|
let port = listener.local_addr()?.port();
|
|
|
|
|
2020-08-12 10:05:52 +00:00
|
|
|
let ticket = Ticket::new(ticket::TERM_PREFIX, &Empty)?
|
|
|
|
.sign(
|
|
|
|
crate::auth_helpers::private_auth_key(),
|
|
|
|
Some(&ticket::term_aad(&userid, &path, port)),
|
|
|
|
)?;
|
2020-07-21 09:10:38 +00:00
|
|
|
|
|
|
|
let mut command = Vec::new();
|
|
|
|
match cmd.as_ref().map(|x| x.as_str()) {
|
|
|
|
Some("login") | None => {
|
|
|
|
command.push("login");
|
|
|
|
if userid == "root@pam" {
|
|
|
|
command.push("-f");
|
|
|
|
command.push("root");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some("upgrade") => {
|
2020-07-23 11:12:13 +00:00
|
|
|
if userid != "root@pam" {
|
|
|
|
bail!("only root@pam can upgrade");
|
|
|
|
}
|
|
|
|
// TODO: add nicer/safer wrapper like in PVE instead
|
|
|
|
command.push("sh");
|
|
|
|
command.push("-c");
|
|
|
|
command.push("apt full-upgrade; bash -l");
|
2020-07-21 09:10:38 +00:00
|
|
|
}
|
|
|
|
_ => bail!("invalid command"),
|
|
|
|
};
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let username = userid.name().to_owned();
|
2020-07-21 09:10:38 +00:00
|
|
|
let upid = WorkerTask::spawn(
|
|
|
|
"termproxy",
|
|
|
|
None,
|
2020-08-06 13:46:01 +00:00
|
|
|
userid,
|
2020-07-21 09:10:38 +00:00
|
|
|
false,
|
|
|
|
move |worker| async move {
|
|
|
|
// move inside the worker so that it survives and does not close the port
|
|
|
|
// remove CLOEXEC from listenere so that we can reuse it in termproxy
|
|
|
|
let fd = listener.as_raw_fd();
|
|
|
|
let mut flags = match fcntl(fd, FcntlArg::F_GETFD) {
|
|
|
|
Ok(bits) => FdFlag::from_bits_truncate(bits),
|
|
|
|
Err(err) => bail!("could not get fd: {}", err),
|
|
|
|
};
|
|
|
|
flags.remove(FdFlag::FD_CLOEXEC);
|
|
|
|
if let Err(err) = fcntl(fd, FcntlArg::F_SETFD(flags)) {
|
|
|
|
bail!("could not set fd: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut arguments: Vec<&str> = Vec::new();
|
|
|
|
let fd_string = fd.to_string();
|
|
|
|
arguments.push(&fd_string);
|
|
|
|
arguments.extend_from_slice(&[
|
|
|
|
"--path",
|
|
|
|
&path,
|
|
|
|
"--perm",
|
|
|
|
"Sys.Console",
|
|
|
|
"--authport",
|
|
|
|
"82",
|
|
|
|
"--port-as-fd",
|
|
|
|
"--",
|
|
|
|
]);
|
|
|
|
arguments.extend_from_slice(&command);
|
|
|
|
|
|
|
|
let mut cmd = tokio::process::Command::new("/usr/bin/termproxy");
|
|
|
|
|
2020-07-23 13:20:12 +00:00
|
|
|
cmd.args(&arguments)
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped());
|
2020-07-21 09:10:38 +00:00
|
|
|
|
|
|
|
let mut child = cmd.spawn().expect("error executing termproxy");
|
|
|
|
|
|
|
|
let stdout = child.stdout.take().expect("no child stdout handle");
|
|
|
|
let stderr = child.stderr.take().expect("no child stderr handle");
|
|
|
|
|
|
|
|
let worker_stdout = worker.clone();
|
|
|
|
let stdout_fut = async move {
|
|
|
|
let mut reader = BufReader::new(stdout).lines();
|
|
|
|
while let Some(line) = reader.next_line().await? {
|
|
|
|
worker_stdout.log(line);
|
|
|
|
}
|
2020-07-23 13:20:12 +00:00
|
|
|
Ok::<(), Error>(())
|
2020-07-21 09:10:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let worker_stderr = worker.clone();
|
|
|
|
let stderr_fut = async move {
|
|
|
|
let mut reader = BufReader::new(stderr).lines();
|
|
|
|
while let Some(line) = reader.next_line().await? {
|
|
|
|
worker_stderr.warn(line);
|
|
|
|
}
|
2020-07-23 13:20:12 +00:00
|
|
|
Ok::<(), Error>(())
|
2020-07-21 09:10:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-29 11:50:27 +00:00
|
|
|
let mut needs_kill = false;
|
|
|
|
let res = tokio::select!{
|
|
|
|
res = &mut child => {
|
2020-07-23 13:20:12 +00:00
|
|
|
let exit_code = res?;
|
|
|
|
if !exit_code.success() {
|
|
|
|
match exit_code.code() {
|
|
|
|
Some(code) => bail!("termproxy exited with {}", code),
|
|
|
|
None => bail!("termproxy exited by signal"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
},
|
2020-07-29 11:50:27 +00:00
|
|
|
res = stdout_fut => res,
|
|
|
|
res = stderr_fut => res,
|
|
|
|
res = worker.abort_future() => {
|
|
|
|
needs_kill = true;
|
|
|
|
res.map_err(Error::from)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if needs_kill {
|
|
|
|
if res.is_ok() {
|
|
|
|
child.kill()?;
|
|
|
|
child.await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = child.kill() {
|
|
|
|
worker.warn(format!("error killing termproxy: {}", err));
|
|
|
|
} else if let Err(err) = child.await {
|
|
|
|
worker.warn(format!("error awaiting termproxy: {}", err));
|
|
|
|
}
|
2020-07-21 09:10:38 +00:00
|
|
|
}
|
2020-07-29 11:50:27 +00:00
|
|
|
|
|
|
|
res
|
2020-07-21 09:10:38 +00:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
// FIXME: We're returning the user NAME only?
|
2020-07-21 09:10:38 +00:00
|
|
|
Ok(json!({
|
|
|
|
"user": username,
|
|
|
|
"ticket": ticket,
|
|
|
|
"port": port,
|
|
|
|
"upid": upid,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sortable]
|
|
|
|
pub const API_METHOD_WEBSOCKET: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::AsyncHttp(&upgrade_to_websocket),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Upgraded to websocket",
|
|
|
|
&sorted!([
|
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
(
|
|
|
|
"vncticket",
|
|
|
|
false,
|
|
|
|
&StringSchema::new("Terminal ticket").schema()
|
|
|
|
),
|
|
|
|
("port", false, &IntegerSchema::new("Terminal port").schema()),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.access(
|
2020-07-23 10:38:10 +00:00
|
|
|
Some("The user needs Sys.Console on /system."),
|
|
|
|
&Permission::Privilege(&["system"], PRIV_SYS_CONSOLE, false),
|
2020-07-21 09:10:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn upgrade_to_websocket(
|
|
|
|
parts: Parts,
|
|
|
|
req_body: Body,
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
|
|
|
) -> ApiResponseFuture {
|
|
|
|
async move {
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-08-12 10:05:52 +00:00
|
|
|
let ticket = tools::required_string_param(¶m, "vncticket")?;
|
2020-07-21 09:10:38 +00:00
|
|
|
let port: u16 = tools::required_integer_param(¶m, "port")? as u16;
|
|
|
|
|
|
|
|
// will be checked again by termproxy
|
2020-08-12 10:05:52 +00:00
|
|
|
Ticket::<Empty>::parse(ticket)?
|
|
|
|
.verify(
|
|
|
|
crate::auth_helpers::public_auth_key(),
|
|
|
|
ticket::TERM_PREFIX,
|
|
|
|
Some(&ticket::term_aad(&userid, "/system", port)),
|
|
|
|
)?;
|
2020-07-21 09:10:38 +00:00
|
|
|
|
|
|
|
let (ws, response) = WebSocket::new(parts.headers)?;
|
|
|
|
|
2020-07-23 13:20:13 +00:00
|
|
|
crate::server::spawn_internal_task(async move {
|
2020-07-21 09:10:38 +00:00
|
|
|
let conn: Upgraded = match req_body.on_upgrade().map_err(Error::from).await {
|
|
|
|
Ok(upgraded) => upgraded,
|
|
|
|
_ => bail!("error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let local = tokio::net::TcpStream::connect(format!("localhost:{}", port)).await?;
|
|
|
|
ws.serve_connection(conn, local).await
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const SUBDIRS: SubdirMap = &[
|
2020-07-21 11:41:07 +00:00
|
|
|
("apt", &apt::ROUTER),
|
2020-06-06 12:43:36 +00:00
|
|
|
("disks", &disks::ROUTER),
|
2019-11-21 08:36:41 +00:00
|
|
|
("dns", &dns::ROUTER),
|
2019-12-17 13:06:48 +00:00
|
|
|
("journal", &journal::ROUTER),
|
2019-11-21 08:36:41 +00:00
|
|
|
("network", &network::ROUTER),
|
2020-05-23 09:10:02 +00:00
|
|
|
("rrd", &rrd::ROUTER),
|
2019-11-21 08:36:41 +00:00
|
|
|
("services", &services::ROUTER),
|
2019-12-18 16:24:04 +00:00
|
|
|
("status", &status::ROUTER),
|
2020-07-21 11:41:06 +00:00
|
|
|
("subscription", &subscription::ROUTER),
|
2019-11-21 08:36:41 +00:00
|
|
|
("syslog", &syslog::ROUTER),
|
|
|
|
("tasks", &tasks::ROUTER),
|
2020-07-21 09:10:38 +00:00
|
|
|
("termproxy", &Router::new().post(&API_METHOD_TERMPROXY)),
|
2019-11-21 08:36:41 +00:00
|
|
|
("time", &time::ROUTER),
|
2020-07-21 09:10:38 +00:00
|
|
|
(
|
|
|
|
"vncwebsocket",
|
|
|
|
&Router::new().upgrade(&API_METHOD_WEBSOCKET),
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(SUBDIRS))
|
|
|
|
.subdirs(SUBDIRS);
|