Merge pull request #21 from MggMuggins/master

Add additional flags to user and groupadd; Update Cargo files
This commit is contained in:
Jeremy Soller
2017-12-10 07:26:59 -07:00
committed by GitHub
4 changed files with 113 additions and 39 deletions
Generated
+3 -3
View File
@@ -179,7 +179,7 @@ dependencies = [
[[package]]
name = "redox_users"
version = "0.1.0"
source = "git+https://github.com/redox-os/users.git?branch=goyox86/failure#47a167cdf2aa64a857190274de7d1a03d43405b6"
source = "git+https://github.com/redox-os/users.git#90e5ffe4df14361e9032c35bd96c38be8558a822"
dependencies = [
"argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"extra 0.1.0 (git+https://github.com/redox-os/libextra.git)",
@@ -255,7 +255,7 @@ dependencies = [
"rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_users 0.1.0 (git+https://github.com/redox-os/users.git?branch=goyox86/failure)",
"redox_users 0.1.0 (git+https://github.com/redox-os/users.git)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -294,7 +294,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd"
"checksum redox_syscall 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "ab105df655884ede59d45b7070c8a65002d921461ee813a024558ca16030eea0"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum redox_users 0.1.0 (git+https://github.com/redox-os/users.git?branch=goyox86/failure)" = "<none>"
"checksum redox_users 0.1.0 (git+https://github.com/redox-os/users.git)" = "<none>"
"checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e"
"checksum scoped_threadpool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4ea459fe3ceff01e09534847c49860891d3ff1c12b4eb7731b67f2778fb60190"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
+1 -2
View File
@@ -46,6 +46,5 @@ liner = "0.1"
rand = "0.3"
redox_syscall = "0.1"
redox_termios = "0.1"
# TODO: Change this to the master branch
redox_users = { git = "https://github.com/redox-os/users.git", branch = "goyox86/failure" }
redox_users = { git = "https://github.com/redox-os/users.git" }
termion = "1.5.1"
+36 -7
View File
@@ -11,7 +11,7 @@ use std::io::Write;
use std::process::exit;
use arg_parser::ArgParser;
use redox_users::{add_group, get_unique_group_id};
use redox_users::{add_group, get_group_by_id, get_unique_group_id, UsersError};
const MAN_PAGE: &'static str = /* @MANSTART{groupadd} */ r#"
NAME
@@ -31,6 +31,11 @@ OPTIONS
even if the group already exists. A message is still
printed to stdout.
-g, --gid GID
The group id to use. This value must not be used and must
be non-negative. The default is to pick the smallest available
group id (between values defined in redox_users).
-h, --help
Display this help and exit.
@@ -45,7 +50,8 @@ fn main() {
let mut parser = ArgParser::new(1)
.add_flag(&["h", "help"])
.add_flag(&["f", "force"]);
.add_flag(&["f", "force"])
.add_opt("g", "gid");
parser.parse(env::args());
// Shows the help
@@ -62,19 +68,42 @@ fn main() {
&parser.args[0]
};
let gid = match get_unique_group_id() {
Some(gid) => gid,
None => {
eprintln!("groupadd: no available gid");
let gid = if let Some(gid) = parser.get_opt("gid") {
let gid = gid.parse::<u32>().unwrap_or_else(|err| {
eprintln!("groupadd: {}", err);
exit(1);
});
match get_group_by_id(gid as usize) {
Ok(_) => {
eprintln!("groupadd: group already exists");
exit(1);
},
Err(ref err) if err.downcast_ref::<UsersError>() == Some(&UsersError::AlreadyExists) => {
gid
},
Err(err) => {
eprintln!("groupadd: {}", err);
exit(1);
}
}
} else {
match get_unique_group_id() {
Some(gid) => gid,
None => {
eprintln!("groupadd: no available gid");
exit(1);
}
}
};
match add_group(groupname, gid, &[""]) {
Ok(_) => { },
Err(ref err) if err.downcast_ref::<UsersError>() == Some(&UsersError::AlreadyExists) && parser.found("force") => {
exit(0);
},
Err(err) => {
eprintln!("groupadd: {}", err);
exit(1)
exit(1);
}
}
}
+73 -27
View File
@@ -39,18 +39,30 @@ OPTIONS
directory. The default value is LOGIN prepended with "/home".
This flag DOES NOT create the home directory. See --create-home.
-g, --gid GID
The group id to use for the default login group. This value must
not be in use and must be non-negative. The default is to pick the
smallest available group id between values defined in redox_users.
-m, --create-home
Creates the user's home directory if it does not already exist.
This option is not enabled by default. This option must be specified
for a home directory to be created.
for a home directory to be created. If not set, the user's home dir is
set to "/".
-N, --no-user-group
Do not attempt to create the user's user group.
Do not attempt to create the user's user group. Instead, the groupid
is set to 99 (should be the "nobody" group).
-s, --shell SHELL
The path to the user's default login shell. If left blank, the
default shell is set as /bin/ion
default shell is set as "/bin/ion"
-u, --uid UID
The user id to use. This value must not be in use and must be
non-negative. The default is to pick the smallest available
user id between the defaults defined in redox_users
AUTHORS
Written by Wesley Hershberger.
@@ -66,9 +78,11 @@ fn main() {
.add_flag(&["h", "help"])
.add_opt("c", "comment")
.add_opt("d", "home-dir")
.add_opt("g", "gid")
.add_flag(&["m", "create-home"])
.add_flag(&["N", "no-user-group"])
.add_opt("s", "shell");
.add_opt("s", "shell")
.add_opt("u", "uid");
parser.parse(env::args());
if parser.found("help") {
@@ -84,21 +98,61 @@ fn main() {
&parser.args[0]
};
let uid = match get_unique_user_id() {
Some(id) => id,
None => {
eprintln!("useradd: no available uid");
exit(1);
let uid = if parser.found("uid") {
match parser.get_opt("uid") {
Some(uid) => uid.parse::<u32>().unwrap_or_else(|err| {
eprintln!("useradd: invalid uid value: {}", err);
exit(1);
}),
None => {
eprintln!("useradd: missing uid value");
exit(1);
}
}
} else {
match get_unique_user_id() {
Some(id) => id,
None => {
eprintln!("useradd: no available uid");
exit(1);
}
}
};
let gid = match get_unique_group_id() {
Some(id) => id,
None => {
eprintln!("useradd: no available gid");
exit(1);
//This is a ridiculous mess and could use reworking
let gid: u32;
if parser.found("no-user-group") {
gid = 99;
//TODO: Add this user to the "nobody" group
} else {
if parser.found("gid") {
gid = match parser.get_opt("gid") {
Some(gid) => gid.parse::<u32>().unwrap_or_else(|err| {
eprintln!("useradd: invalid argument to gid: {}", err);
exit(1);
}),
None => {
eprintln!("useradd: missing gid argument");
exit(1);
}
};
} else {
gid = match get_unique_group_id() {
Some(id) => id,
None => {
eprintln!("useradd: no available gid");
exit(1);
}
};
}
};
match add_group(login, gid, &[login]) {
Ok(_) => {},
Err(err) => {
eprintln!("useradd: error creating group {}: {}", login, err);
exit(1);
}
}
}
let username = if parser.found("comment") {
match parser.get_opt("comment") {
@@ -116,12 +170,14 @@ fn main() {
match parser.get_opt("home-dir") {
Some(dir) => dir,
None => {
eprintln!("useradd: invalid argument: -d");
eprintln!("useradd: missing directory argument");
exit(1);
}
}
} else {
} else if parser.found("create-home") {
format!("{}/{}", DEFAULT_HOME, login)
} else {
"/".to_string()
};
let shell = if parser.found("shell") {
@@ -136,16 +192,6 @@ fn main() {
DEFAULT_SHELL.to_string()
};
if !parser.found("no-user-group") {
match add_group(login, gid, &[login]) {
Ok(_) => {},
Err(err) => {
eprintln!("useradd: error creating group {}: {}", login, err);
exit(1);
}
}
}
match add_user(login, uid, gid, username.as_str(), userhome.as_str(), shell.as_str()) {
Ok(_) => {},
Err(err) => {