~kennylevinsen/greetd-devel

agretty: allow restricting a username v2 APPLIED

Hugo Osvaldo Barrera: 1
 agretty: allow restricting a username

 2 files changed, 27 insertions(+), 10 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~kennylevinsen/greetd-devel/patches/58091/mbox | git am -3
Learn more about email & git

[PATCH v2] agretty: allow restricting a username Export this patch

---
v2:
* use if/else instead of match; it ends up being easier to read.
* use println!( instead of print!("…\n"
* use inline format strings for new code
 agreety/src/main.rs | 32 +++++++++++++++++++++++---------
 man/agreety-1.scd   |  5 ++++-
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/agreety/src/main.rs b/agreety/src/main.rs
index 5ab294d..65896a2 100644
--- a/agreety/src/main.rs
+++ b/agreety/src/main.rs
@@ -57,15 +57,24 @@ enum LoginResult {
    Failure,
}

fn login(node: &str, cmd: &mut Option<String>) -> Result<LoginResult, Box<dyn std::error::Error>> {
    let username = loop {
        let username = prompt_stderr(&format!("{} login: ", node))?;
        if let Some(u) = username.strip_prefix('!') {
            *cmd = Some(u.to_string());
            eprintln!("Login command changed to: {u}");
            continue;
fn login(
    node: &str,
    cmd: &mut Option<String>,
    user: Option<&str>,
) -> Result<LoginResult, Box<dyn std::error::Error>> {
    let username = if let Some(u) = user {
        println!("{node} login: {u}");
        u.to_string()
    } else {
        loop {
            let username = prompt_stderr(&format!("{node} login: "))?;
            if let Some(u) = username.strip_prefix('!') {
                *cmd = Some(u.to_string());
                eprintln!("Login command changed to: {u}");
                continue;
            }
            break username;
        }
        break username;
    };

    let mut stream = UnixStream::connect(env::var("GREETD_SOCK")?)?;
@@ -143,6 +152,7 @@ fn main() {
        "maximum number of accepted failures",
        "FAILURES",
    );
    opts.optopt("u", "user", "restrict login to a specific user", "USER");
    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(f) => {
@@ -164,6 +174,10 @@ fn main() {
            std::process::exit(1)
        }
    };
    let user: Option<String> = match matches.opt_get("user") {
        Ok(s) => s,
        Err(_) => None,
    };

    if let Ok(issue) = get_issue() {
        print!("{}", issue);
@@ -171,7 +185,7 @@ fn main() {

    let uts = uname().unwrap();
    for _ in 0..max_failures {
        match login(uts.nodename().to_str().unwrap(), &mut cmd) {
        match login(uts.nodename().to_str().unwrap(), &mut cmd, user.as_deref()) {
            Ok(LoginResult::Success) => break,
            Ok(LoginResult::Failure) => eprintln!("Login incorrect\n"),
            Err(e) => {
diff --git a/man/agreety-1.scd b/man/agreety-1.scd
index 5043c00..fa1259c 100644
--- a/man/agreety-1.scd
+++ b/man/agreety-1.scd
@@ -21,6 +21,9 @@ agreety - A text-based greeter for greetd
	Specifies the maximum number of login failures to accept before terminating.
	Defaults to 5.

*-u, --user <user>*
	Restrict login to *user*.

# DESCRIPTION

agreety is a very simple text-based greeter, with an appearance similar to
@@ -34,4 +37,4 @@ Maintained by Kenny Levinsen <contact@kl.wtf>. For more information about
greetd development, see https://git.sr.ht/~kennylevinsen/greetd.

# SEE ALSO
*greetd*(1) *greetd*(5)
\ No newline at end of file
*greetd*(1) *greetd*(5)
-- 
2.48.1
Applied, thanks!

(Sorry for the delay)