proto: Simply Login by removing VT field

There are currently use-cases for this functionality, so remove feature
in order to simplify the protocol.
This commit is contained in:
Kenny Levinsen
2020-01-07 01:55:33 +01:00
parent 231f11e43b
commit 252ab3738a
7 changed files with 23 additions and 50 deletions

View File

@@ -8,7 +8,7 @@ use rpassword::prompt_password_stderr;
use clap::{crate_authors, crate_version, App, Arg}; use clap::{crate_authors, crate_version, App, Arg};
use nix::sys::utsname::uname; use nix::sys::utsname::uname;
use greet_proto::{Header, Request, Response, VtSelection}; use greet_proto::{Header, Request, Response};
fn prompt_stderr(prompt: &str) -> Result<String, Box<dyn std::error::Error>> { fn prompt_stderr(prompt: &str) -> Result<String, Box<dyn std::error::Error>> {
let stdin = io::stdin(); let stdin = io::stdin();
@@ -17,7 +17,7 @@ fn prompt_stderr(prompt: &str) -> Result<String, Box<dyn std::error::Error>> {
Ok(stdin_iter.next().unwrap()?) Ok(stdin_iter.next().unwrap()?)
} }
fn login(node: &str, cmd: Option<&str>, vt: VtSelection) -> Result<(), Box<dyn std::error::Error>> { fn login(node: &str, cmd: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
let username = prompt_stderr(&format!("{} login: ", node)).unwrap(); let username = prompt_stderr(&format!("{} login: ", node)).unwrap();
let password = prompt_password_stderr("Password: ").unwrap(); let password = prompt_password_stderr("Password: ").unwrap();
let command = match cmd { let command = match cmd {
@@ -34,7 +34,6 @@ fn login(node: &str, cmd: Option<&str>, vt: VtSelection) -> Result<(), Box<dyn s
password, password,
command: vec![command], command: vec![command],
env, env,
vt,
}; };
// Write request // Write request
@@ -111,13 +110,13 @@ fn main() {
}; };
let vntr: usize = env::var("XDG_VTNR").unwrap_or("0".to_string()).parse().expect("unable to parse VTNR"); let vntr: usize = env::var("XDG_VTNR").unwrap_or("0".to_string()).parse().expect("unable to parse VTNR");
let (vt_text, target_vt) = match matches.value_of("vt") { let vt_text = match matches.value_of("vt") {
None => (format!("tty{}; next VT", vntr), VtSelection::Next), None => format!("tty{}; next VT", vntr),
Some("next") => (format!("tty{}; next VT", vntr), VtSelection::Next), Some("next") => format!("tty{}; next VT", vntr),
Some("current") => (format!("tty{}", vntr), VtSelection::Current), Some("current") => format!("tty{}", vntr),
Some(n) => match n.parse() { Some(n) => match n.parse::<usize>() {
Ok(v) if v == vntr => (format!("tty{}", v), VtSelection::Specific(v)), Ok(v) if v == vntr => format!("tty{}", v),
Ok(v) => (format!("tty{}; tty{}", vntr, v), VtSelection::Specific(v)), Ok(v) => format!("tty{}; tty{}", vntr, v),
Err(e) => { Err(e) => {
eprintln!("unable to parse VT number: {}", e); eprintln!("unable to parse VT number: {}", e);
std::process::exit(1) std::process::exit(1)
@@ -134,7 +133,7 @@ fn main() {
println!(""); println!("");
for _ in 0..max_failures { for _ in 0..max_failures {
match login(uts.nodename(), cmd, target_vt) { match login(uts.nodename(), cmd) {
Ok(()) => { Ok(()) => {
break; break;
} }

View File

@@ -60,20 +60,6 @@ impl Header {
} }
} }
#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum VtSelection {
Current,
Next,
Specific(usize),
}
impl Default for VtSelection {
fn default() -> Self {
VtSelection::Current
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Copy)] #[derive(Debug, Deserialize, Serialize, Clone, Copy)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum ShutdownAction { pub enum ShutdownAction {
@@ -91,9 +77,6 @@ pub enum Request {
password: String, password: String,
command: Vec<String>, command: Vec<String>,
env: HashMap<String, String>, env: HashMap<String, String>,
#[serde(default)]
vt: VtSelection,
}, },
Shutdown { Shutdown {
action: ShutdownAction, action: ShutdownAction,

View File

@@ -3,7 +3,7 @@ use std::env;
use std::io::{self, BufRead, Read, Write}; use std::io::{self, BufRead, Read, Write};
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use greet_proto::{Header, Request, Response, VtSelection}; use greet_proto::{Header, Request, Response};
use rpassword::prompt_password_stderr; use rpassword::prompt_password_stderr;
@@ -24,7 +24,6 @@ fn login() -> Result<(), Box<dyn std::error::Error>> {
password, password,
command: vec![command], command: vec![command],
env: HashMap::new(), env: HashMap::new(),
vt: VtSelection::Next,
}; };
// Write request // Write request

View File

@@ -103,8 +103,7 @@ impl Pollable for Client {
password, password,
command, command,
env, env,
vt, } => match ctx.login(username, password, command, env) {
} => match ctx.login(username, password, command, env, vt) {
Ok(_) => Response::Success, Ok(_) => Response::Success,
Err(e) => Response::Failure(Failure::LoginError { Err(e) => Response::Failure(Failure::LoginError {
description: format!("{}", e), description: format!("{}", e),

View File

@@ -4,8 +4,6 @@ use std::fs::read_to_string;
use clap::{crate_authors, crate_version, App, Arg}; use clap::{crate_authors, crate_version, App, Arg};
use serde::Deserialize; use serde::Deserialize;
use greet_proto::VtSelection;
fn default_vt() -> toml::Value { fn default_vt() -> toml::Value {
toml::Value::String("next".to_string()) toml::Value::String("next".to_string())
} }
@@ -25,6 +23,12 @@ pub struct Config {
pub socket_path: String, pub socket_path: String,
} }
pub enum VtSelection {
Next,
Current,
Specific(usize)
}
impl Config { impl Config {
pub fn vt(&self) -> VtSelection { pub fn vt(&self) -> VtSelection {
match &self.vt { match &self.vt {

View File

@@ -9,8 +9,7 @@ use nix::unistd::{alarm, execv, fork, ForkResult};
use crate::scrambler::Scrambler; use crate::scrambler::Scrambler;
use crate::session::{Session, SessionChild}; use crate::session::{Session, SessionChild};
use crate::terminal; use greet_proto::ShutdownAction;
use greet_proto::{ShutdownAction, VtSelection};
/// Context keeps track of running sessions and start new ones. /// Context keeps track of running sessions and start new ones.
pub struct Context<'a> { pub struct Context<'a> {
@@ -87,7 +86,6 @@ impl<'a> Context<'a> {
mut password: String, mut password: String,
cmd: Vec<String>, cmd: Vec<String>,
provided_env: HashMap<String, String>, provided_env: HashMap<String, String>,
vt: VtSelection,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if !self.greeter.is_some() { if !self.greeter.is_some() {
eprintln!("login request not valid when greeter is not active"); eprintln!("login request not valid when greeter is not active");
@@ -98,14 +96,8 @@ impl<'a> Context<'a> {
return Err(io::Error::new(io::ErrorKind::Other, "session already active").into()); return Err(io::Error::new(io::ErrorKind::Other, "session already active").into());
} }
let vt = match vt {
VtSelection::Current => self.vt,
VtSelection::Specific(vt) => vt,
VtSelection::Next => terminal::Terminal::open(0)?.vt_get_next()?,
};
let pending_session = let pending_session =
Session::new("login", "user", &username, &password, cmd, provided_env, vt)?; Session::new("login", "user", &username, &password, cmd, provided_env, self.vt)?;
password.scramble(); password.scramble();
self.pending_session = Some(pending_session); self.pending_session = Some(pending_session);

View File

@@ -4,8 +4,6 @@ use std::fs::remove_file;
use nix::poll::{poll, PollFd}; use nix::poll::{poll, PollFd};
use nix::unistd::chown; use nix::unistd::chown;
use greet_proto::VtSelection;
mod config; mod config;
mod client; mod client;
mod context; mod context;
@@ -38,9 +36,9 @@ fn main() {
let term = terminal::Terminal::open(0) let term = terminal::Terminal::open(0)
.expect("unable to open controlling terminal"); .expect("unable to open controlling terminal");
let vt = match config.vt() { let vt = match config.vt() {
VtSelection::Current => term.vt_get_current().expect("unable to get current VT"), config::VtSelection::Current => term.vt_get_current().expect("unable to get current VT"),
VtSelection::Next => term.vt_get_next().expect("unable to get next VT"), config::VtSelection::Next => term.vt_get_next().expect("unable to get next VT"),
VtSelection::Specific(v) => v config::VtSelection::Specific(v) => v
}; };
drop(term); drop(term);
@@ -73,7 +71,6 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
let mut idx_compensation: isize = 0; let mut idx_compensation: isize = 0;
for (idx, fd) in fds.iter().enumerate() { for (idx, fd) in fds.iter().enumerate() {
if let Some(revents) = fd.revents() { if let Some(revents) = fd.revents() {