🏡 index : ~doyle/pisshoff.git

author Jordan Doyle <jordan@doyle.la> 2024-02-17 14:18:16.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2024-02-17 14:18:51.0 +00:00:00
commit
00d6f919b97cf3889220ebb6ed05db373cae1189 [patch]
tree
6645f5be4f740d3b351a0b6fbefe1b5da33a296b
parent
1e652077744d298c6692b425f75c980c177cebb3
download
00d6f919b97cf3889220ebb6ed05db373cae1189.tar.gz

Format comments and imports



Diff

 Cargo.toml                                    |  1 +-
 pisshoff-server/src/audit.rs                  |  6 ++--
 pisshoff-server/src/command/echo.rs           | 12 +++++----
 pisshoff-server/src/command/exit.rs           | 11 +++++---
 pisshoff-server/src/command/pwd.rs            |  8 +++---
 pisshoff-server/src/command/scp.rs            | 25 ++++++++++--------
 pisshoff-server/src/command/uname.rs          | 10 ++++---
 pisshoff-server/src/command/whoami.rs         |  8 +++---
 pisshoff-server/src/config.rs                 |  5 +---
 pisshoff-server/src/main.rs                   |  6 ++--
 pisshoff-server/src/server.rs                 | 38 +++++++++++++---------------
 pisshoff-server/src/state.rs                  |  3 +-
 pisshoff-server/src/subsystem/mod.rs          |  6 ++--
 pisshoff-server/src/subsystem/sftp.rs         |  6 ++--
 pisshoff-server/src/subsystem/shell.rs        |  9 +++----
 pisshoff-server/src/subsystem/shell/parser.rs | 24 ++++++++++++------
 pisshoff-timescaledb-exporter/src/config.rs   |  3 +-
 pisshoff-timescaledb-exporter/src/main.rs     |  6 ++--
 pisshoff-types/src/audit.rs                   |  7 ++---
 rustfmt.toml                                  |  8 ++++++-
 20 files changed, 121 insertions(+), 81 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index a84c597..4a2f2e8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
    "pisshoff-server",
    "pisshoff-timescaledb-exporter",
diff --git a/pisshoff-server/src/audit.rs b/pisshoff-server/src/audit.rs
index 7db53ac..905158b 100644
--- a/pisshoff-server/src/audit.rs
+++ b/pisshoff-server/src/audit.rs
@@ -1,6 +1,6 @@
use crate::config::Config;
pub use pisshoff_types::audit::*;
use std::{io::ErrorKind, sync::Arc, time::Duration};

pub use pisshoff_types::audit::*;
use tokio::{
    fs::OpenOptions,
    io::{AsyncWriteExt, BufWriter},
@@ -9,6 +9,8 @@ use tokio::{
};
use tracing::{debug, info};

use crate::config::Config;

pub fn start_audit_writer(
    config: Arc<Config>,
    mut reload: watch::Receiver<()>,
diff --git a/pisshoff-server/src/command/echo.rs b/pisshoff-server/src/command/echo.rs
index 33e39f4..c21c7c5 100644
--- a/pisshoff-server/src/command/echo.rs
+++ b/pisshoff-server/src/command/echo.rs
@@ -1,10 +1,11 @@
use async_trait::async_trait;
use itertools::Itertools;
use thrussh::ChannelId;

use crate::{
    command::{Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};
use async_trait::async_trait;
use itertools::Itertools;
use thrussh::ChannelId;

#[derive(Debug, Clone)]
pub struct Echo {}
@@ -40,6 +41,9 @@ impl Command for Echo {

#[cfg(test)]
mod test {
    use mockall::predicate::always;
    use test_case::test_case;

    use crate::{
        command::{echo::Echo, Command, CommandResult},
        server::{
@@ -47,8 +51,6 @@ mod test {
            ConnectionState, MockThrusshSession,
        },
    };
    use mockall::predicate::always;
    use test_case::test_case;

    #[test_case(&[], "\n"; "no parameters")]
    #[test_case(&["hello"], "hello\n"; "single parameter")]
diff --git a/pisshoff-server/src/command/exit.rs b/pisshoff-server/src/command/exit.rs
index aa01ea0..1f5ebc2 100644
--- a/pisshoff-server/src/command/exit.rs
+++ b/pisshoff-server/src/command/exit.rs
@@ -1,10 +1,12 @@
use std::str::FromStr;

use async_trait::async_trait;
use thrussh::ChannelId;

use crate::{
    command::{Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};
use async_trait::async_trait;
use std::str::FromStr;
use thrussh::ChannelId;

#[derive(Debug, Clone)]
pub struct Exit {}
@@ -39,11 +41,12 @@ impl Command for Exit {

#[cfg(test)]
mod test {
    use test_case::test_case;

    use crate::{
        command::{exit::Exit, Command, CommandResult},
        server::{test::fake_channel_id, ConnectionState, MockThrusshSession},
    };
    use test_case::test_case;

    #[test_case(&[], 0; "no parameters")]
    #[test_case(&["3"], 3; "with parameter")]
diff --git a/pisshoff-server/src/command/pwd.rs b/pisshoff-server/src/command/pwd.rs
index 39f7586..fe94aaa 100644
--- a/pisshoff-server/src/command/pwd.rs
+++ b/pisshoff-server/src/command/pwd.rs
@@ -1,9 +1,10 @@
use async_trait::async_trait;
use thrussh::ChannelId;

use crate::{
    command::{Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};
use async_trait::async_trait;
use thrussh::ChannelId;

#[derive(Debug, Clone)]
pub struct Pwd {}
@@ -37,6 +38,8 @@ impl Command for Pwd {

#[cfg(test)]
mod test {
    use mockall::predicate::always;

    use crate::{
        command::{pwd::Pwd, Command, CommandResult},
        server::{
@@ -44,7 +47,6 @@ mod test {
            ConnectionState, MockThrusshSession,
        },
    };
    use mockall::predicate::always;

    #[tokio::test]
    async fn works() {
diff --git a/pisshoff-server/src/command/scp.rs b/pisshoff-server/src/command/scp.rs
index 7ded331..67eb7b6 100644
--- a/pisshoff-server/src/command/scp.rs
+++ b/pisshoff-server/src/command/scp.rs
@@ -1,7 +1,5 @@
use crate::{
    command::{Arg, Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};
use std::{path::PathBuf, str::FromStr};

use async_trait::async_trait;
use bytes::{Buf, BytesMut};
use nom::{
@@ -11,10 +9,14 @@ use nom::{
    IResult,
};
use pisshoff_types::audit::{AuditLogAction, WriteFileEvent};
use std::{path::PathBuf, str::FromStr};
use thrussh::ChannelId;
use tracing::warn;

use crate::{
    command::{Arg, Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};

const HELP: &str = "usage: scp [-346ABCOpqRrsTv] [-c cipher] [-D sftp_server_path] [-F ssh_config]
           [-i identity_file] [-J destination] [-l limit] [-o ssh_option]
           [-P port] [-S program] [-X sftp_option] source ... target\n";
@@ -115,8 +117,8 @@ impl Command for Scp {
                            self.pending_data
                                .advance(self.pending_data.len() - rest.len());

                            // signal to the client we received their message and we're now listening for
                            // more data
                            // signal to the client we received their message and we're now
                            // listening for more data
                            session.data(channel, SUCCESS.to_string().into());

                            state
@@ -150,8 +152,8 @@ impl Command for Scp {
                    if self.pending_data.starts_with(&[0]) {
                        self.pending_data.advance(1);

                        // signal to the client we received their message and we're now listening for
                        // more data
                        // signal to the client we received their message and we're now listening
                        // for more data
                        session.data(channel, SUCCESS.to_string().into());
                    }

@@ -281,6 +283,9 @@ impl<'a> Receive<'a> {

#[cfg(test)]
mod test {
    use insta::assert_debug_snapshot;
    use mockall::predicate::always;

    use crate::{
        command::{scp::Scp, Command},
        server::{
@@ -288,8 +293,6 @@ mod test {
            ConnectionState, MockThrusshSession,
        },
    };
    use insta::assert_debug_snapshot;
    use mockall::predicate::always;

    mod packet_parser {
        use crate::command::scp::Receive;
diff --git a/pisshoff-server/src/command/uname.rs b/pisshoff-server/src/command/uname.rs
index 25b2310..a062d2c 100644
--- a/pisshoff-server/src/command/uname.rs
+++ b/pisshoff-server/src/command/uname.rs
@@ -1,10 +1,11 @@
use async_trait::async_trait;
use bitflags::bitflags;
use thrussh::ChannelId;

use crate::{
    command::{Arg, Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};
use async_trait::async_trait;
use bitflags::bitflags;
use thrussh::ChannelId;

bitflags! {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -181,9 +182,10 @@ pub fn execute(params: &[String]) -> (String, u32) {

#[cfg(test)]
mod test {
    use crate::command::uname::execute;
    use test_case::test_case;

    use crate::command::uname::execute;

    #[test_case("", 0; "none")]
    #[test_case("-a", 0; "all")]
    #[test_case("-snrvmpio", 0; "all separate")]
diff --git a/pisshoff-server/src/command/whoami.rs b/pisshoff-server/src/command/whoami.rs
index d92110c..8d62463 100644
--- a/pisshoff-server/src/command/whoami.rs
+++ b/pisshoff-server/src/command/whoami.rs
@@ -1,9 +1,10 @@
use async_trait::async_trait;
use thrussh::ChannelId;

use crate::{
    command::{Command, CommandResult},
    server::{ConnectionState, ThrusshSession},
};
use async_trait::async_trait;
use thrussh::ChannelId;

#[derive(Debug, Clone)]
pub struct Whoami {}
@@ -33,6 +34,8 @@ impl Command for Whoami {

#[cfg(test)]
mod test {
    use mockall::predicate::always;

    use crate::{
        command::{whoami::Whoami, Command, CommandResult},
        server::{
@@ -40,7 +43,6 @@ mod test {
            ConnectionState, MockThrusshSession,
        },
    };
    use mockall::predicate::always;

    #[tokio::test]
    async fn works() {
diff --git a/pisshoff-server/src/config.rs b/pisshoff-server/src/config.rs
index 753d8eb..c7e4dff 100644
--- a/pisshoff-server/src/config.rs
+++ b/pisshoff-server/src/config.rs
@@ -1,8 +1,7 @@
use std::{io::ErrorKind, net::SocketAddr, path::PathBuf, sync::Arc};

use clap::Parser;
use serde::{de::DeserializeOwned, Deserialize};
use std::path::PathBuf;
use std::sync::Arc;
use std::{io::ErrorKind, net::SocketAddr};

/// Parser for command line arguments, these arguments can also be passed via capitalised env vars
/// of the same name.
diff --git a/pisshoff-server/src/main.rs b/pisshoff-server/src/main.rs
index ae975d3..5fafc90 100644
--- a/pisshoff-server/src/main.rs
+++ b/pisshoff-server/src/main.rs
@@ -1,11 +1,11 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]

use crate::{config::Args, server::Server};
use std::sync::Arc;

use anyhow::anyhow;
use clap::Parser;
use futures::FutureExt;
use std::sync::Arc;
use thrussh::MethodSet;
use tokio::{
    signal::unix::SignalKind,
@@ -14,6 +14,8 @@ use tokio::{
use tracing::{error, info};
use tracing_subscriber::EnvFilter;

use crate::{config::Args, server::Server};

mod audit;
mod command;
mod config;
diff --git a/pisshoff-server/src/server.rs b/pisshoff-server/src/server.rs
index a3b7d61..69f6f77 100644
--- a/pisshoff-server/src/server.rs
+++ b/pisshoff-server/src/server.rs
@@ -1,21 +1,3 @@
use crate::{
    audit::{
        AuditLog, AuditLogAction, LoginAttemptEvent, OpenDirectTcpIpEvent, OpenX11Event,
        PtyRequestEvent, X11RequestEvent,
    },
    audit::{
        SignalEvent, SubsystemRequestEvent, TcpIpForwardEvent, WindowAdjustedEvent,
        WindowChangeRequestEvent,
    },
    config::Config,
    file_system::FileSystem,
    state::State,
    subsystem::{self, shell::Shell, Subsystem as SubsystemTrait},
};
use futures::{
    future::{BoxFuture, InspectErr},
    FutureExt, TryFutureExt,
};
use std::{
    borrow::Cow,
    collections::HashMap,
@@ -25,15 +7,31 @@ use std::{
    sync::Arc,
    task::{Context, Poll},
};

use futures::{
    future::{BoxFuture, InspectErr},
    FutureExt, TryFutureExt,
};
use thrussh::{
    server::{Auth, Response, Session},
    ChannelId, CryptoVec, Pty, Sig,
};
use thrussh_keys::key::PublicKey;
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::Mutex;
use tokio::sync::{mpsc::UnboundedSender, Mutex};
use tracing::{debug, error, info, info_span, instrument::Instrumented, Instrument, Span};

use crate::{
    audit::{
        AuditLog, AuditLogAction, LoginAttemptEvent, OpenDirectTcpIpEvent, OpenX11Event,
        PtyRequestEvent, SignalEvent, SubsystemRequestEvent, TcpIpForwardEvent,
        WindowAdjustedEvent, WindowChangeRequestEvent, X11RequestEvent,
    },
    config::Config,
    file_system::FileSystem,
    state::State,
    subsystem::{self, shell::Shell, Subsystem as SubsystemTrait},
};

pub static KEYBOARD_INTERACTIVE_PROMPT: &[(Cow<'static, str>, bool)] =
    &[(Cow::Borrowed("Password: "), false)];

diff --git a/pisshoff-server/src/state.rs b/pisshoff-server/src/state.rs
index e997998..f48c541 100644
--- a/pisshoff-server/src/state.rs
+++ b/pisshoff-server/src/state.rs
@@ -1,6 +1,7 @@
use parking_lot::RwLock;
use std::{borrow::Cow, collections::HashSet};

use parking_lot::RwLock;

#[derive(Default)]
pub struct State {
    /// A list of passwords that have previously been accepted, and will forever be accepted
diff --git a/pisshoff-server/src/subsystem/mod.rs b/pisshoff-server/src/subsystem/mod.rs
index 457c92f..55770bd 100644
--- a/pisshoff-server/src/subsystem/mod.rs
+++ b/pisshoff-server/src/subsystem/mod.rs
@@ -1,7 +1,7 @@
use crate::server::ConnectionState;
use async_trait::async_trait;
use thrussh::server::Session;
use thrussh::ChannelId;
use thrussh::{server::Session, ChannelId};

use crate::server::ConnectionState;

pub mod sftp;
pub mod shell;
diff --git a/pisshoff-server/src/subsystem/sftp.rs b/pisshoff-server/src/subsystem/sftp.rs
index ff8208e..10a3b55 100644
--- a/pisshoff-server/src/subsystem/sftp.rs
+++ b/pisshoff-server/src/subsystem/sftp.rs
@@ -1,4 +1,5 @@
use crate::{server::ConnectionState, subsystem::Subsystem};
use std::{collections::HashMap, io::Write, mem::size_of, str::FromStr};

use async_trait::async_trait;
use bytes::Bytes;
use nom::{
@@ -9,12 +10,13 @@ use nom::{
    IResult,
};
use pisshoff_types::audit::{AuditLogAction, MkdirEvent, WriteFileEvent};
use std::{collections::HashMap, io::Write, mem::size_of, str::FromStr};
use strum::FromRepr;
use thrussh::{server::Session, ChannelId};
use tracing::{debug, error, trace, warn};
use uuid::Uuid;

use crate::{server::ConnectionState, subsystem::Subsystem};

// https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-13
#[derive(Default, Clone, Debug)]
pub struct Sftp {
diff --git a/pisshoff-server/src/subsystem/shell.rs b/pisshoff-server/src/subsystem/shell.rs
index a9d7047..bf090fb 100644
--- a/pisshoff-server/src/subsystem/shell.rs
+++ b/pisshoff-server/src/subsystem/shell.rs
@@ -1,5 +1,10 @@
mod parser;

use async_trait::async_trait;
use pisshoff_types::audit::{AuditLogAction, ExecCommandEvent};
use thrussh::{server::Session, ChannelId};
use tracing::info;

use crate::{
    command::{CommandResult, ConcreteCommand},
    server::{ConnectionState, EitherSession, StdoutCaptureSession},
@@ -8,10 +13,6 @@ use crate::{
        Subsystem,
    },
};
use async_trait::async_trait;
use pisshoff_types::audit::{AuditLogAction, ExecCommandEvent};
use thrussh::{server::Session, ChannelId};
use tracing::info;

pub const SHELL_PROMPT: &str = "bash-5.1$ ";

diff --git a/pisshoff-server/src/subsystem/shell/parser.rs b/pisshoff-server/src/subsystem/shell/parser.rs
index 22b0202..6ff1b69 100644
--- a/pisshoff-server/src/subsystem/shell/parser.rs
+++ b/pisshoff-server/src/subsystem/shell/parser.rs
@@ -1,4 +1,5 @@
use crate::{command::PartialCommand, subsystem::shell::IResult};
use std::{borrow::Cow, collections::HashMap};

use nom::{
    branch::alt,
    bytes::complete::{escaped_transform, is_not, tag, take, take_until, take_while1},
@@ -9,7 +10,8 @@ use nom::{
    sequence::{delimited, preceded},
    AsChar,
};
use std::{borrow::Cow, collections::HashMap};

use crate::{command::PartialCommand, subsystem::shell::IResult};

#[derive(Debug, PartialEq, Eq)]
pub enum IterState<'a> {
@@ -300,11 +302,14 @@ fn atoi(v: &[u8]) -> Option<u8> {
#[cfg(test)]
mod test {
    mod iter {
        use crate::command::PartialCommand;
        use crate::server::ConnectionState;
        use crate::subsystem::shell::parser::{tokenize, Iter, IterState};
        use std::borrow::Cow;

        use crate::{
            command::PartialCommand,
            server::ConnectionState,
            subsystem::shell::parser::{tokenize, Iter, IterState},
        };

        #[test]
        fn single_nested() {
            let (rest, s) = tokenize(b"echo $(echo hello) world!").unwrap();
@@ -377,9 +382,10 @@ mod test {
    }

    mod parse_command {
        use crate::subsystem::shell::parser::{tokenize, Expansion, ParsedPart, RedirectionTo};
        use std::borrow::Cow;

        use crate::subsystem::shell::parser::{tokenize, Expansion, ParsedPart, RedirectionTo};

        #[test]
        fn messed_up() {
            let (rest, s) = tokenize(b"echo    ${HI}'this' \"is a \\t${TEST}\"using'$(complex string)>|' $(echo parsing) for the hell of it;fin").unwrap();
@@ -450,9 +456,10 @@ mod test {
    }

    mod parse_expansion {
        use crate::subsystem::shell::parser::{parse_expansion, Expansion, ParsedPart};
        use std::borrow::Cow;

        use crate::subsystem::shell::parser::{parse_expansion, Expansion, ParsedPart};

        #[test]
        fn double_dollar() {
            let (rest, s) = parse_expansion(b"$$a").unwrap();
@@ -531,9 +538,10 @@ mod test {
    }

    mod parse_double_quoted {
        use crate::subsystem::shell::parser::{parse_double_quoted, Expansion, ParsedPart};
        use std::borrow::Cow;

        use crate::subsystem::shell::parser::{parse_double_quoted, Expansion, ParsedPart};

        #[test]
        fn with_expansion() {
            let (rest, s) = parse_double_quoted(b"\"hello world $('cat' 'test') test\"").unwrap();
diff --git a/pisshoff-timescaledb-exporter/src/config.rs b/pisshoff-timescaledb-exporter/src/config.rs
index 8cb9e34..7bb8743 100644
--- a/pisshoff-timescaledb-exporter/src/config.rs
+++ b/pisshoff-timescaledb-exporter/src/config.rs
@@ -1,6 +1,7 @@
use std::{io::ErrorKind, path::PathBuf, sync::Arc};

use clap::Parser;
use serde::{de::DeserializeOwned, Deserialize};
use std::{io::ErrorKind, path::PathBuf, sync::Arc};

/// Parser for command line arguments
#[derive(Parser)]
diff --git a/pisshoff-timescaledb-exporter/src/main.rs b/pisshoff-timescaledb-exporter/src/main.rs
index b9bf8b2..c105b5f 100644
--- a/pisshoff-timescaledb-exporter/src/main.rs
+++ b/pisshoff-timescaledb-exporter/src/main.rs
@@ -1,7 +1,8 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]

use crate::config::Args;
use std::sync::Arc;

use clap::Parser;
use deadpool_postgres::{
    tokio_postgres::{NoTls, Statement, Transaction},
@@ -9,12 +10,13 @@ use deadpool_postgres::{
};
use futures::{StreamExt, TryFutureExt};
use pisshoff_types::audit::{AuditLog, AuditLogEvent};
use std::sync::Arc;
use tokio::net::{UnixListener, UnixStream};
use tokio_util::codec::{Decoder, LinesCodec};
use tracing::{error, info};
use tracing_subscriber::EnvFilter;

use crate::config::Args;

mod config;

mod embedded {
diff --git a/pisshoff-types/src/audit.rs b/pisshoff-types/src/audit.rs
index 27f3f59..c823b09 100644
--- a/pisshoff-types/src/audit.rs
+++ b/pisshoff-types/src/audit.rs
@@ -1,11 +1,12 @@
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::{
    borrow::Cow,
    fmt::{Debug, Formatter},
    net::SocketAddr,
    time::{Duration, Instant},
};

use bytes::Bytes;
use serde::{Deserialize, Serialize};
use strum::IntoStaticStr;
use time::OffsetDateTime;
use uuid::Uuid;
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..fee9839
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1,8 @@
edition = "2021"
unstable_features = true
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
normalize_comments = true
comment_width = 100
wrap_comments = true