Skip to content

Commit f66c643

Browse files
authored
Merge pull request #377 from dezgeg/add_tool
nologin: Add tool
2 parents a04b7a3 + fd69cc2 commit f66c643

8 files changed

Lines changed: 166 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ feat_common_core = [
3939
"mcookie",
4040
"mesg",
4141
"mountpoint",
42+
"nologin",
4243
"renice",
4344
"rev",
4445
"setsid",
@@ -102,6 +103,7 @@ lsmem = { optional = true, version = "0.0.1", package = "uu_lsmem", path = "src/
102103
mcookie = { optional = true, version = "0.0.1", package = "uu_mcookie", path = "src/uu/mcookie" }
103104
mesg = { optional = true, version = "0.0.1", package = "uu_mesg", path = "src/uu/mesg" }
104105
mountpoint = { optional = true, version = "0.0.1", package = "uu_mountpoint", path = "src/uu/mountpoint" }
106+
nologin = { optional = true, version = "0.0.1", package = "uu_nologin", path = "src/uu/nologin" }
105107
renice = { optional = true, version = "0.0.1", package = "uu_renice", path = "src/uu/renice" }
106108
rev = { optional = true, version = "0.0.1", package = "uu_rev", path = "src/uu/rev" }
107109
setsid = { optional = true, version = "0.0.1", package = "uu_setsid", path ="src/uu/setsid" }

src/uu/nologin/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "uu_nologin"
3+
version = "0.0.1"
4+
edition = "2021"
5+
6+
[lib]
7+
path = "src/nologin.rs"
8+
9+
[[bin]]
10+
name = "nologin"
11+
path = "src/main.rs"
12+
13+
[dependencies]
14+
uucore = { workspace = true }
15+
clap = { workspace = true }

src/uu/nologin/nologin.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# nologin
2+
3+
```
4+
nologin [options]
5+
```
6+
7+
Politely refuse a login.

src/uu/nologin/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uucore::bin!(uu_nologin);

src/uu/nologin/src/nologin.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// This file is part of the uutils util-linux package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use clap::{crate_version, Arg, ArgAction, Command};
7+
use std::fs;
8+
use uucore::{error::UResult, format_usage, help_about, help_usage};
9+
10+
const ABOUT: &str = help_about!("nologin.md");
11+
const USAGE: &str = help_usage!("nologin.md");
12+
13+
#[uucore::main]
14+
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
15+
let _matches = uu_app().try_get_matches_from(args)?;
16+
17+
// Try to read custom message from /etc/nologin.txt
18+
let message = match fs::read_to_string("/etc/nologin.txt") {
19+
Ok(content) => content.trim().to_string(),
20+
Err(_) => "This account is currently not available.".to_string(),
21+
};
22+
23+
println!("{}", message);
24+
std::process::exit(1);
25+
}
26+
27+
pub fn uu_app() -> Command {
28+
Command::new(uucore::util_name())
29+
.version(crate_version!())
30+
.about(ABOUT)
31+
.override_usage(format_usage(USAGE))
32+
.infer_long_args(true)
33+
.arg(
34+
Arg::new("command")
35+
.short('c')
36+
.long("command")
37+
.value_name("command")
38+
.help("does nothing (for compatibility)"),
39+
)
40+
.arg(
41+
Arg::new("init-file")
42+
.long("init-file")
43+
.value_name("file")
44+
.help("does nothing (for compatibility)"),
45+
)
46+
.arg(
47+
Arg::new("interactive")
48+
.short('i')
49+
.long("interactive")
50+
.help("does nothing (for compatibility)")
51+
.action(ArgAction::SetTrue),
52+
)
53+
.arg(
54+
Arg::new("login")
55+
.short('l')
56+
.long("login")
57+
.help("does nothing (for compatibility)")
58+
.action(ArgAction::SetTrue),
59+
)
60+
.arg(
61+
Arg::new("noprofile")
62+
.long("noprofile")
63+
.help("does nothing (for compatibility)")
64+
.action(ArgAction::SetTrue),
65+
)
66+
.arg(
67+
Arg::new("norc")
68+
.long("norc")
69+
.help("does nothing (for compatibility)")
70+
.action(ArgAction::SetTrue),
71+
)
72+
.arg(
73+
Arg::new("posix")
74+
.long("posix")
75+
.help("does nothing (for compatibility)")
76+
.action(ArgAction::SetTrue),
77+
)
78+
.arg(
79+
Arg::new("rcfile")
80+
.long("rcfile")
81+
.value_name("file")
82+
.help("does nothing (for compatibility)"),
83+
)
84+
.arg(
85+
Arg::new("restricted")
86+
.short('r')
87+
.long("restricted")
88+
.help("does nothing (for compatibility)")
89+
.action(ArgAction::SetTrue),
90+
)
91+
}

tests/by-util/test_nologin.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of the uutils util-linux package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use uutests::{new_ucmd, util::TestScenario, util_name};
7+
8+
#[test]
9+
fn test_invalid_arg() {
10+
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
11+
}
12+
13+
#[test]
14+
fn test_nologin_args() {
15+
let args_to_try: &[&[&str]] = &[
16+
&[],
17+
&["-c", "command"],
18+
&["--init-file", "file"],
19+
&["-i"],
20+
&["--interactive"],
21+
&["-l"],
22+
&["--login"],
23+
&["--noprofile"],
24+
&["--norc"],
25+
&["--posix"],
26+
&["--rcfile", "file"],
27+
&["--restricted"],
28+
&["-r"],
29+
];
30+
for args in args_to_try {
31+
new_ucmd!()
32+
.args(args)
33+
.fails()
34+
.code_is(1)
35+
.stdout_contains("This account is currently not available.");
36+
}
37+
}

tests/tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ mod test_mesg;
3535
#[path = "by-util/test_mountpoint.rs"]
3636
mod test_mountpoint;
3737

38+
#[cfg(feature = "nologin")]
39+
#[path = "by-util/test_nologin.rs"]
40+
mod test_nologin;
41+
3842
#[cfg(feature = "blockdev")]
3943
#[path = "by-util/test_blockdev.rs"]
4044
mod test_blockdev;

0 commit comments

Comments
 (0)