Make whoami use the logged in user's username
Diff
pisshoff-server/src/command.rs | 20 ++++++++++++++++----
pisshoff-server/src/command/uname.rs | 6 ++++--
pisshoff-server/src/server.rs | 12 ++++++++++--
3 files changed, 30 insertions(+), 8 deletions(-)
@@ -1,10 +1,16 @@
pub mod uname;
use crate::server::Connection;
use itertools::{Either, Itertools};
use std::{f32, str::FromStr, time::Duration};
use thrussh::{server::Session, ChannelId};
pub async fn run_command(args: &[String], channel: ChannelId, session: &mut Session) {
pub async fn run_command(
args: &[String],
channel: ChannelId,
session: &mut Session,
conn: &mut Connection,
) {
let Some(command) = args.get(0) else {
return;
};
@@ -17,12 +23,18 @@ pub async fn run_command(args: &[String], channel: ChannelId, session: &mut Sess
);
}
"whoami" => {
session.data(channel, "root\n".to_string().into());
session.data(channel, format!("{}\n", conn.username()).into());
}
"pwd" => {
session.data(channel, "/root\n".to_string().into());
let username = conn.username();
let pwd = if conn.username() == "root" {
"/root\n".to_string()
} else {
format!("/home/{username}\n")
};
session.data(channel, pwd.into());
}
"ls" => {
@@ -76,9 +76,11 @@ pub fn execute(params: &[String]) -> String {
"uname: invalid option -- '{s}'\nTry 'uname --help' for more information.\n"
);
}
Arg::Long(s) => return format!(
Arg::Long(s) => {
return format!(
"uname: unrecognized option '--{s}'\nTry 'uname --help' for more information.\n"
),
)
}
};
}
@@ -73,6 +73,7 @@ impl thrussh::server::Server for Server {
peer_address: peer_addr,
..AuditLog::default()
},
username: None,
}
}
}
@@ -81,10 +82,17 @@ pub struct Connection {
span: Span,
server: Server,
audit_log: AuditLog,
username: Option<String>,
}
impl Connection {
pub fn username(&self) -> &str {
self.username.as_deref().unwrap_or("root")
}
fn try_login(&mut self, user: &str, password: &str) -> bool {
self.username = Some(user.to_string());
let res = if self
.server
.state
@@ -295,7 +303,7 @@ impl thrussh::server::Handler for Connection {
async move {
if let Some(args) = data {
run_command(&args, channel, &mut session).await;
run_command(&args, channel, &mut session, &mut self).await;
self.audit_log
.push_action(AuditLogAction::ExecCommand(ExecCommandEvent {
args: Box::from(args),
@@ -447,7 +455,7 @@ impl thrussh::server::Handler for Connection {
async move {
if let Some(args) = data {
run_command(&args, channel, &mut session).await;
run_command(&args, channel, &mut session, &mut self).await;
self.audit_log
.push_action(AuditLogAction::ExecCommand(ExecCommandEvent {
args: Box::from(args),