force valid arguments

This commit is contained in:
Tyler Murphy 2022-11-09 12:50:44 -05:00
parent 65345247fb
commit 49c1c8e424
8 changed files with 38 additions and 24 deletions

2
Cargo.lock generated
View file

@ -34,7 +34,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "crab" name = "crab"
version = "0.0.3" version = "0.0.4"
dependencies = [ dependencies = [
"exec", "exec",
"nix", "nix",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "crab" name = "crab"
version = "0.0.3" version = "0.0.4"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -1,6 +1,6 @@
pkgbase = crab pkgbase = crab
pkgdesc = A rusty permission authentication system pkgdesc = A rusty permission authentication system
pkgver = 0.0.3 pkgver = 0.0.4
pkgrel = 1 pkgrel = 1
url = https://g.tylerm.dev/tylermurphy534/crab.git url = https://g.tylerm.dev/tylermurphy534/crab.git
arch = x86_64 arch = x86_64

View file

@ -5,7 +5,7 @@
# Maintainer: Tyler Murphy <tylermurphy534@gmail.com> # Maintainer: Tyler Murphy <tylermurphy534@gmail.com>
pkgname=crab pkgname=crab
pkgver=0.0.3 pkgver=0.0.4
pkgrel=1 pkgrel=1
pkgdesc="A rusty permission authentication system" pkgdesc="A rusty permission authentication system"
arch=('x86_64' 'i686') arch=('x86_64' 'i686')

View file

@ -5,7 +5,7 @@ pub struct Flags {
pub arg_count: usize pub arg_count: usize
} }
pub fn parse(args: &[String]) -> Flags { pub fn parse(args: &[String]) -> Option<Flags> {
let mut flags = Flags { let mut flags = Flags {
help: false, help: false,
version: false, version: false,
@ -17,15 +17,21 @@ pub fn parse(args: &[String]) -> Flags {
flags.arg_count += 1; flags.arg_count += 1;
if arg.starts_with("--") { if arg.starts_with("--") {
let flag = &arg[2..]; let flag = &arg[2..];
check_flag(&flag, &mut flags); if !set_flag(&flag, &mut flags) {
eprintln!("Invalid argument: {}", arg);
return None
}
} else { } else {
let flag = &arg[1..]; let flag = &arg[1..];
for char in flag.chars() { for char in flag.chars() {
check_flag(&char.to_string(), &mut flags); if !set_flag(&char.to_string(), &mut flags) {
eprintln!("Invalid argument: {}", arg);
return None
}
} }
} }
} }
flags Some(flags)
} }
fn is_arg(arg: &str) -> bool { fn is_arg(arg: &str) -> bool {
@ -36,16 +42,18 @@ const HELP_FLAG: &str = "help h";
const VERSION_FLAG: &str = "version v"; const VERSION_FLAG: &str = "version v";
const DONT_PERSIST: &str = "d"; const DONT_PERSIST: &str = "d";
fn check_flag(arg: &str, flags: &mut Flags) { fn set_flag(arg: &str, flags: &mut Flags) -> bool {
if has_flag_set(&arg, HELP_FLAG) { if has_flag_set(&arg, HELP_FLAG) {
flags.help = true flags.help = true;
} return true
if has_flag_set(&arg, VERSION_FLAG) { } else if has_flag_set(&arg, VERSION_FLAG) {
flags.version = true flags.version = true;
} return true
if has_flag_set(&arg, DONT_PERSIST) { } else if has_flag_set(&arg, DONT_PERSIST) {
flags.dont_persist = true flags.dont_persist = true;
return true
} }
false
} }
fn has_flag_set(arg: &str, check: &str) -> bool { fn has_flag_set(arg: &str, check: &str) -> bool {
@ -55,4 +63,4 @@ fn has_flag_set(arg: &str, check: &str) -> bool {
} }
} }
return false return false
} }

View file

@ -2,10 +2,9 @@ pub fn help() {
let help = let help =
"Usage: "Usage:
crab [-d] command [args] crab [-d] command [args]
Options: Options:
-v --version Get the current version of the package -v --version Get the current version of the package
-h --help Generates the crab help message -h --help Generates the crab help message
-d If your user is set to persist, dont save persistance"; -d If your user is set to persist, dont save persistance";
println!("{}", help); println!("{}", help);
} }

View file

@ -5,7 +5,7 @@ use nix::unistd;
extern crate time; extern crate time;
// const ERROR_COMMAND: u8 = 1; const ERROR_ARGS: u8 = 1;
const ERROR_CONFIG: u8 = 2; const ERROR_CONFIG: u8 = 2;
const ERROR_NO_USER: u8 = 3; const ERROR_NO_USER: u8 = 3;
const ERROR_NOT_AUTHORIZED: u8 = 4; const ERROR_NOT_AUTHORIZED: u8 = 4;
@ -18,7 +18,13 @@ mod help;
fn main() -> ExitCode { fn main() -> ExitCode {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let flags = flags::parse(&args[1..]); let flags = match flags::parse(&args[1..]) {
Some(data) => data,
None => {
help::help();
return ExitCode::from(ERROR_ARGS);
}
};
if flags.version { if flags.version {
println!("crab version 0.0.3"); println!("crab version 0.0.3");
return ExitCode::SUCCESS; return ExitCode::SUCCESS;
@ -132,4 +138,4 @@ fn config(path: &str) -> Option<Config> {
users.push((user, persist)); users.push((user, persist));
} }
Some(Config{users}) Some(Config{users})
} }

View file

@ -83,12 +83,13 @@ fn get_terminal_config() -> Option<Value> {
} }
fn write_terminal_config(id: &i32, data: &str) -> Result<(), Box<dyn std::error::Error>> { fn write_terminal_config(id: &i32, data: &str) -> Result<(), Box<dyn std::error::Error>> {
std::fs::write(path(&id), data)?; std::fs::write(path(&id), "")?;
unistd::chown(std::path::Path::new(&path(&id)), Some(unistd::Uid::from(0)), Some(unistd::Gid::from(0)))?; unistd::chown(std::path::Path::new(&path(&id)), Some(unistd::Uid::from(0)), Some(unistd::Gid::from(0)))?;
let metadata = std::fs::metadata(path(&id))?; let metadata = std::fs::metadata(path(&id))?;
let mut perms = metadata.permissions(); let mut perms = metadata.permissions();
perms.set_mode(0o660); perms.set_mode(0o660);
fs::set_permissions(path(&id), perms)?; fs::set_permissions(path(&id), perms)?;
std::fs::write(path(&id), data)?;
Ok(()) Ok(())
} }
@ -98,4 +99,4 @@ fn now() -> u64 {
fn path(id: &i32) -> String { fn path(id: &i32) -> String {
return format!("/tmp/crab-{}", id); return format!("/tmp/crab-{}", id);
} }