🏡 index : ~doyle/pisshoff.git

author Jordan Doyle <jordan@doyle.la> 2023-06-28 22:23:37.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-06-28 22:23:37.0 +00:00:00
commit
176bed4177d1fc3b80ef93abdf2ee486b75fec7c [patch]
tree
01a22901663c28f7bdb88f6b8a41221563038d3c
parent
8147cd7eaed4d557c78302d8590e1cc3812076d8
download
176bed4177d1fc3b80ef93abdf2ee486b75fec7c.tar.gz

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(-)

diff --git a/pisshoff-server/src/command.rs b/pisshoff-server/src/command.rs
index 0df5a50..1f82200 100644
--- a/pisshoff-server/src/command.rs
+++ b/pisshoff-server/src/command.rs
@@ -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" => {
            // TODO: grab "logged in" user
            session.data(channel, "root\n".to_string().into());
            session.data(channel, format!("{}\n", conn.username()).into());
        }
        "pwd" => {
            // TODO: mock FHS
            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" => {
            // pretend /root is empty until we mock the FHS
diff --git a/pisshoff-server/src/command/uname.rs b/pisshoff-server/src/command/uname.rs
index 3563de8..8d2db25 100644
--- a/pisshoff-server/src/command/uname.rs
+++ b/pisshoff-server/src/command/uname.rs
@@ -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"
            ),
            )
            }
        };
    }

diff --git a/pisshoff-server/src/server.rs b/pisshoff-server/src/server.rs
index 5e21685..ef29239 100644
--- a/pisshoff-server/src/server.rs
+++ b/pisshoff-server/src/server.rs
@@ -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),