Compare commits

..

No commits in common. "3dae3c410998160a5d1cf5b80a7a82eae2f9b9ef" and "c21ba2fa39f7407fdfd6de161947e0a4959d812f" have entirely different histories.

7 changed files with 226 additions and 133 deletions

177
Cargo.lock generated
View File

@ -1,9 +1,19 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "addr2line"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c0929d69e78dd9bf5408269919fcbcaeb2e35e5d43e5815517cdc6a8e11a423"
dependencies = [
"gimli",
]
[[package]] [[package]]
name = "adduser" name = "adduser"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"assert_cli",
"chrono", "chrono",
"clap", "clap",
"derive_builder", "derive_builder",
@ -13,9 +23,14 @@ dependencies = [
"regex", "regex",
"simplelog", "simplelog",
"tempfile", "tempfile",
"test_bin",
] ]
[[package]]
name = "adler"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.13" version = "0.7.13"
@ -25,6 +40,20 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "assert_cli"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a29ab7c0ed62970beb0534d637a8688842506d0ff9157de83286dacd065c8149"
dependencies = [
"colored",
"difference",
"environment",
"failure",
"failure_derive",
"serde_json",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -42,6 +71,20 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2baad346b2d4e94a24347adeee9c7a93f412ee94b9cc26e5b59dea23848e9f28"
dependencies = [
"addr2line",
"cfg-if 1.0.0",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -54,6 +97,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.19" version = "0.4.19"
@ -99,6 +148,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "colored"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.10.2" version = "0.10.2"
@ -159,6 +219,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "difference"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.8.1" version = "0.8.1"
@ -172,6 +238,34 @@ dependencies = [
"termcolor", "termcolor",
] ]
[[package]]
name = "environment"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4b14e20978669064c33b4c1e0fb4083412e40fe56cbea2eae80fd7591503ee"
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -184,11 +278,17 @@ version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 0.1.10",
"libc", "libc",
"wasi 0.9.0+wasi-snapshot-preview1", "wasi 0.9.0+wasi-snapshot-preview1",
] ]
[[package]]
name = "gimli"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.9.1" version = "0.9.1"
@ -235,6 +335,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -253,7 +359,7 @@ version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 0.1.10",
] ]
[[package]] [[package]]
@ -262,6 +368,16 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "miniz_oxide"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d"
dependencies = [
"adler",
"autocfg",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.43" version = "0.1.43"
@ -281,6 +397,12 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "object"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "2.4.0" version = "2.4.0"
@ -409,6 +531,35 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232"
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
[[package]]
name = "serde_json"
version = "1.0.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "simplelog" name = "simplelog"
version = "0.8.0" version = "0.8.0"
@ -443,13 +594,25 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "synstructure"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.1.0" version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 0.1.10",
"libc", "libc",
"rand", "rand",
"redox_syscall", "redox_syscall",
@ -466,12 +629,6 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "test_bin"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1937bab04b0bd0f2c19628d3408fb6dd78faf5aa60f4bdb03212507a9b7314ba"
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.12.1" version = "0.12.1"

View File

@ -21,11 +21,10 @@ chrono = "0.4"
env_logger = "0.8" env_logger = "0.8"
derive_builder = "0.9" derive_builder = "0.9"
clap = "3.0.0-beta" clap = "3.0.0-beta"
assert_cli = "0.6"
[dev-dependencies] [dev-dependencies]
tempfile = "3.1" tempfile = "3.1"
test_bin= "0.3"
# optimize for size at cost of compilation speed. # optimize for size at cost of compilation speed.
[profile.release] [profile.release]

View File

@ -18,40 +18,36 @@ fn main() -> Result<(), UserLibError> {
.about("the new users name") .about("the new users name")
.takes_value(true) .takes_value(true)
.required(true), .required(true),
) /*
.arg(
Arg::new("INPUT")
.about("Sets the input file to use")
.required(true)
.index(1),
) )
.arg( .arg(
Arg::new("passwd") Arg::new("v")
.short('p') .short('v')
.long("passwd") .multiple(true)
.value_name("FILE") .about("Sets the level of verbosity"),
.about("The passwd file")
.default_value("/etc/passwd")
.takes_value(true),
) )
.subcommand(
App::new("test")
.about("controls testing features")
.version("1.3")
.author("Someone E. <someone_else@other.com>")
.arg( .arg(
Arg::new("shadow") Arg::new("debug")
.short('s') .short('d')
.long("shadow") .about("print debug information verbosely"),
.value_name("FILE") ),
.about("The shadow file") )*/
.default_value("/etc/shadow")
.takes_value(true),
)
.arg(
Arg::new("group")
.short('g')
.long("group")
.value_name("FILE")
.about("The group file")
.default_value("/etc/group")
.takes_value(true),
)
.get_matches(); .get_matches();
let mf = adduser::Files { let mf = adduser::Files {
passwd: Some(PathBuf::from(matches.value_of("passwd").unwrap())), passwd: Some(PathBuf::from("./passwd")),
shadow: Some(PathBuf::from(matches.value_of("shadow").unwrap())), shadow: Some(PathBuf::from("./shadow")),
group: Some(PathBuf::from(matches.value_of("group").unwrap())), group: Some(PathBuf::from("./group")),
}; };
let mut db = adduser::UserDBLocal::load_files(mf).unwrap(); let mut db = adduser::UserDBLocal::load_files(mf).unwrap();

View File

@ -41,9 +41,6 @@ impl User {
} }
pub fn username(&mut self, name: String) -> &mut Self { pub fn username(&mut self, name: String) -> &mut Self {
self.username = crate::Username { username: name }; self.username = crate::Username { username: name };
if let crate::Password::Shadow(ref mut s) = self.password {
s.set_username(self.username.clone());
}
self self
} }
pub fn disable_password(&mut self) -> &mut Self { pub fn disable_password(&mut self) -> &mut Self {
@ -181,18 +178,13 @@ impl Ord for User {
impl Default for User { impl Default for User {
fn default() -> Self { fn default() -> Self {
let username = passwd_fields::Username {
username: "defaultusername".to_owned(),
};
let line = "defaultusername:!!:0:0:99999:7:::";
let password = passwd_fields::Password::Shadow(
shadow_fields::Shadow::new_from_string(line.to_owned(), u32::MAX).unwrap(),
);
Self { Self {
source: "".to_owned(), source: "".to_owned(),
pos: u32::MAX, pos: u32::MAX,
username, username: crate::Username {
password, username: "defaultusername".to_owned(),
},
password: crate::Password::Disabled,
uid: crate::Uid { uid: 1001 }, uid: crate::Uid { uid: 1001 },
gid: crate::Gid { gid: 1001 }, gid: crate::Gid { gid: 1001 },
gecos: crate::Gecos::Simple { gecos: crate::Gecos::Simple {

View File

@ -26,11 +26,6 @@ impl Shadow {
pub fn get_username(&self) -> &str { pub fn get_username(&self) -> &str {
&self.username.username &self.username.username
} }
pub fn set_username(&mut self, username: crate::Username) {
self.username = username;
}
#[must_use] #[must_use]
pub fn get_password(&self) -> &str { pub fn get_password(&self) -> &str {
&self.password.password &self.password.password

View File

@ -231,16 +231,10 @@ impl UserDBWrite for UserDBLocal {
Err("Failed to create the user. A user with the same Name already exists".into()) Err("Failed to create the user. A user with the same Name already exists".into())
} else { } else {
let opened = self.source_files.lock_all_get(); let opened = self.source_files.lock_all_get();
let (mut locked_p, mut locked_s, mut _locked_g) = let (mut locked_p, mut _locked_s, mut _locked_g) =
opened.expect("failed to lock files!"); opened.expect("failed to lock files!");
//dbg!(&locked_p); dbg!(&locked_p);
locked_p.append(format!("{}", new_user))?; locked_p.append(format!("{}", new_user))?;
if let Some(shadow) = new_user.get_shadow() {
info!("Adding shadow entry {}", shadow);
locked_s.append(format!("{}", shadow))?;
} else {
warn!("Omitting shadow entry!")
}
assert!(self assert!(self
.users .users
.insert(args.username.to_owned(), new_user) .insert(args.username.to_owned(), new_user)

View File

@ -1,5 +1,4 @@
extern crate adduser; extern crate adduser;
extern crate test_bin;
mod testfiles; mod testfiles;
#[test] #[test]
@ -31,93 +30,54 @@ fn test_create_user_function() {
.build() .build()
.unwrap(), .unwrap(),
); );
let password_file_string = fs::read_to_string(&p.path).unwrap(); let pf2 = fs::read_to_string(&p.path).unwrap();
let shadow_file_string = fs::read_to_string(&p.path).unwrap();
assert_eq!(user_res.unwrap().get_username().unwrap(), "test2"); assert_eq!(user_res.unwrap().get_username().unwrap(), "test2");
let pflines = pf.lines(); let pflines = pf.lines();
let pflines2 = password_file_string.lines(); let pflines2 = pf2.lines();
for (l1, l2) in pflines.zip(pflines2) { for (l1, l2) in pflines.zip(pflines2) {
dbg!(l1, l2); dbg!(l1, l2);
assert!(l1 == l2); assert!(l1 == l2);
} }
assert!(password_file_string assert!(pf2.lines().last().unwrap().starts_with("test2"));
.lines()
.last()
.unwrap()
.starts_with("test2"));
assert!(shadow_file_string
.lines()
.last()
.unwrap()
.starts_with("test2"));
} }
#[test] #[test]
fn test_create_user_binary() { fn test_create_user_binary() {
use testfiles::Fixture; use testfiles::Fixture;
use adduser::api::UserDBWrite;
use adduser::api::UserRead;
use std::fs; use std::fs;
let p = Fixture::copy("passwd"); let p = Fixture::copy("passwd");
let s = Fixture::copy("shadow"); let s = Fixture::copy("shadow");
let g = Fixture::copy("group"); let g = Fixture::copy("group");
//dbg!(&p, &s, &g); dbg!(&p, &s, &g);
let passwd_string = fs::read_to_string(&p.path).unwrap(); let pf = fs::read_to_string(&p.path).unwrap();
let passwd_lines = passwd_string.lines();
let shadow_string = fs::read_to_string(&s.path).unwrap();
let shadow_lines = shadow_string.lines();
let out = test_bin::get_test_bin("create_user") let mf = adduser::Files {
.args(&[ passwd: Some(p.path.clone()),
"testuser3", shadow: Some(s.path),
"-p", group: Some(g.path),
p.path.to_str().unwrap(), };
"-s",
s.path.to_str().unwrap(), let mut db = adduser::UserDBLocal::load_files(mf).unwrap();
"-g",
g.path.to_str().unwrap(), let user_res: Result<&adduser::User, adduser::UserLibError> = db.new_user(
]) adduser::api::CreateUserArgs::builder()
.output() .username("test2")
.expect("Failed to run the command"); // .delete_home(adduser::api::DeleteHome::Delete)
println!( .build()
"The output after running: {}", .unwrap(),
String::from_utf8_lossy(&out.stdout)
); );
println!( let pf2 = fs::read_to_string(&p.path).unwrap();
"The error after running: {}", assert_eq!(user_res.unwrap().get_username().unwrap(), "test2");
String::from_utf8_lossy(&out.stderr) let pflines = pf.lines();
); let pflines2 = pf2.lines();
assert_eq!(String::from_utf8_lossy(&out.stdout), ""); for (l1, l2) in pflines.zip(pflines2) {
assert_eq!(String::from_utf8_lossy(&out.stderr), ""); dbg!(l1, l2);
let passwd_string_after = fs::read_to_string(&p.path).unwrap();
let passwd_lines_after = passwd_string_after.lines();
let shadow_string_after = fs::read_to_string(&s.path).unwrap();
let shadow_lines_after = shadow_string_after.lines();
for (l1, l2) in passwd_lines.zip(passwd_lines_after) {
//dbg!(l1, l2);
assert!(l1 == l2); assert!(l1 == l2);
} }
assert_eq!( assert!(pf2.lines().last().unwrap().starts_with("test2"));
passwd_string_after
.lines()
.last()
.unwrap()
.starts_with("testuser3"),
true
);
//dbg!(&shadow_string_after);
for (l1, l2) in shadow_lines.zip(shadow_lines_after) {
//dbg!(l1, l2);
assert!(l1 == l2);
}
assert_eq!(
shadow_string_after
.lines()
.last()
.unwrap()
.starts_with("testuser3"),
true
);
} }