Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add POSIX setgid/setuid/sticky bit support #59

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 93 additions & 4 deletions suppaftp/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use thiserror::Error;

/// POSIX system regex to parse list output
static POSIX_LS_RE: Lazy<Regex> = lazy_regex!(
r#"^([\-ld])([\-rwxs]{9})\s+(\d+)\s+(.+)\s+(.+)\s+(\d+)\s+(.+\s+\d{1,2}\s+(?:\d{1,2}:\d{1,2}|\d{4}))\s+(.+)$"#
r#"^([\-ld])([\-rwxsStT]{9})\s+(\d+)\s+(.+)\s+(.+)\s+(\d+)\s+(.+\s+\d{1,2}\s+(?:\d{1,2}:\d{1,2}|\d{4}))\s+(.+)$"#
);
/// DOS system regex to parse list output
static DOS_LS_RE: Lazy<Regex> =
Expand Down Expand Up @@ -194,7 +194,7 @@ impl File {
match POSIX_LS_RE.captures(line) {
// String matches regex
Some(metadata) => {
trace!("Parsed POXIS line {}", line);
trace!("Parsed POSIX line {}", line);
// NOTE: metadata fmt: (regex, file_type, permissions, link_count, uid, gid, filesize, mtime, filename)
// Expected 7 + 1 (8) values: + 1 cause regex is repeated at 0
if metadata.len() < 8 {
Expand All @@ -214,7 +214,7 @@ impl File {
let mut count: u8 = 0;
for (i, c) in metadata.get(2).unwrap().as_str()[range].chars().enumerate() {
match c {
'-' => {}
'-' | 'S' | 'T' => {}
_ => {
count += match i {
0 => 4,
Expand Down Expand Up @@ -414,10 +414,11 @@ impl TryFrom<&str> for File {
type Error = ParseError;

fn try_from(line: &str) -> Result<Self, Self::Error> {
// First try to parse the line in POSIX format (vast majority case).
match Self::from_posix_line(line) {
Ok(entry) => Ok(entry),
// If POSIX parsing fails, try with DOS parser.
Err(_) => match Self::from_dos_line(line) {
// If POSIX parsing fails, try with DOS parser
Ok(entry) => Ok(entry),
Err(err) => Err(err),
},
Expand Down Expand Up @@ -586,6 +587,94 @@ mod test {
.unwrap(),
Duration::from_secs(1541376000)
);
// Setuid bit
let file: File =
File::from_str("drws------ 2 u-redacted g-redacted 3864 Feb 17 2023 sas")
.ok()
.unwrap();
assert_eq!(file.is_directory(), true);
assert_eq!(file.can_read(PosixPexQuery::Owner), true);
assert_eq!(file.can_write(PosixPexQuery::Owner), true);
assert_eq!(file.can_execute(PosixPexQuery::Owner), true);
assert_eq!(file.can_read(PosixPexQuery::Group), false);
assert_eq!(file.can_write(PosixPexQuery::Group), false);
assert_eq!(file.can_execute(PosixPexQuery::Group), false);
assert_eq!(file.can_read(PosixPexQuery::Others), false);
assert_eq!(file.can_write(PosixPexQuery::Others), false);
assert_eq!(file.can_execute(PosixPexQuery::Others), false);
let file: File =
File::from_str("drwS------ 2 u-redacted g-redacted 3864 Feb 17 2023 sas")
.ok()
.unwrap();
assert_eq!(file.is_directory(), true);
assert_eq!(file.can_read(PosixPexQuery::Owner), true);
assert_eq!(file.can_write(PosixPexQuery::Owner), true);
assert_eq!(file.can_execute(PosixPexQuery::Owner), false);
assert_eq!(file.can_read(PosixPexQuery::Group), false);
assert_eq!(file.can_write(PosixPexQuery::Group), false);
assert_eq!(file.can_execute(PosixPexQuery::Group), false);
assert_eq!(file.can_read(PosixPexQuery::Others), false);
assert_eq!(file.can_write(PosixPexQuery::Others), false);
assert_eq!(file.can_execute(PosixPexQuery::Others), false);
// Setgid bit
let file: File =
File::from_str("drwx--s--- 2 u-redacted g-redacted 3864 Feb 17 2023 sas")
.ok()
.unwrap();
assert_eq!(file.is_directory(), true);
assert_eq!(file.can_read(PosixPexQuery::Owner), true);
assert_eq!(file.can_write(PosixPexQuery::Owner), true);
assert_eq!(file.can_execute(PosixPexQuery::Owner), true);
assert_eq!(file.can_read(PosixPexQuery::Group), false);
assert_eq!(file.can_write(PosixPexQuery::Group), false);
assert_eq!(file.can_execute(PosixPexQuery::Group), true);
assert_eq!(file.can_read(PosixPexQuery::Others), false);
assert_eq!(file.can_write(PosixPexQuery::Others), false);
assert_eq!(file.can_execute(PosixPexQuery::Others), false);
let file: File =
File::from_str("drwx--S--- 2 u-redacted g-redacted 3864 Feb 17 2023 sas")
.ok()
.unwrap();
assert_eq!(file.is_directory(), true);
assert_eq!(file.can_read(PosixPexQuery::Owner), true);
assert_eq!(file.can_write(PosixPexQuery::Owner), true);
assert_eq!(file.can_execute(PosixPexQuery::Owner), true);
assert_eq!(file.can_read(PosixPexQuery::Group), false);
assert_eq!(file.can_write(PosixPexQuery::Group), false);
assert_eq!(file.can_execute(PosixPexQuery::Group), false);
assert_eq!(file.can_read(PosixPexQuery::Others), false);
assert_eq!(file.can_write(PosixPexQuery::Others), false);
assert_eq!(file.can_execute(PosixPexQuery::Others), false);
// Sticky bit
let file: File =
File::from_str("drwx-----t 2 u-redacted g-redacted 3864 Feb 17 2023 sas")
.ok()
.unwrap();
assert_eq!(file.is_directory(), true);
assert_eq!(file.can_read(PosixPexQuery::Owner), true);
assert_eq!(file.can_write(PosixPexQuery::Owner), true);
assert_eq!(file.can_execute(PosixPexQuery::Owner), true);
assert_eq!(file.can_read(PosixPexQuery::Group), false);
assert_eq!(file.can_write(PosixPexQuery::Group), false);
assert_eq!(file.can_execute(PosixPexQuery::Group), false);
assert_eq!(file.can_read(PosixPexQuery::Others), false);
assert_eq!(file.can_write(PosixPexQuery::Others), false);
assert_eq!(file.can_execute(PosixPexQuery::Others), true);
let file: File =
File::from_str("drwx--S--T 2 u-redacted g-redacted 3864 Feb 17 2023 sas")
.ok()
.unwrap();
assert_eq!(file.is_directory(), true);
assert_eq!(file.can_read(PosixPexQuery::Owner), true);
assert_eq!(file.can_write(PosixPexQuery::Owner), true);
assert_eq!(file.can_execute(PosixPexQuery::Owner), true);
assert_eq!(file.can_read(PosixPexQuery::Group), false);
assert_eq!(file.can_write(PosixPexQuery::Group), false);
assert_eq!(file.can_execute(PosixPexQuery::Group), false);
assert_eq!(file.can_read(PosixPexQuery::Others), false);
assert_eq!(file.can_write(PosixPexQuery::Others), false);
assert_eq!(file.can_execute(PosixPexQuery::Others), false);

// Error
assert_eq!(
File::from_posix_line("drwxrwxr-x 1 0 9 Nov 5 2018 docs")
Expand Down