1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#[derive(Debug, PartialEq)]
pub(crate) struct VisudoOptions {
pub(crate) file: Option<String>,
pub(crate) owner: bool,
pub(crate) perms: bool,
pub(crate) action: VisudoAction,
}
impl Default for VisudoOptions {
fn default() -> Self {
Self {
file: None,
owner: false,
perms: false,
action: VisudoAction::Run,
}
}
}
#[derive(Debug, PartialEq)]
pub(crate) enum VisudoAction {
Help,
Version,
Check,
Run,
}
type OptionSetter = fn(&mut VisudoOptions, Option<String>) -> Result<(), String>;
struct VisudoOption {
short: char,
long: &'static str,
takes_argument: bool,
set: OptionSetter,
}
impl VisudoOptions {
const VISUDO_OPTIONS: &'static [VisudoOption] = &[
VisudoOption {
short: 'c',
long: "check",
takes_argument: false,
set: |options, _| {
options.action = VisudoAction::Check;
Ok(())
},
},
VisudoOption {
short: 'f',
long: "file",
takes_argument: true,
set: |options, argument| {
options.file = Some(argument.ok_or("option requires an argument -- 'f'")?);
Ok(())
},
},
VisudoOption {
short: 'h',
long: "help",
takes_argument: false,
set: |options, _| {
options.action = VisudoAction::Help;
Ok(())
},
},
VisudoOption {
short: 'I',
long: "no-includes",
takes_argument: false,
set: |_, _| Ok(()),
/* ignored for compatibility sake */
},
VisudoOption {
short: 'q',
long: "quiet",
takes_argument: false,
set: |_, _| Ok(()),
/* ignored for compatibility sake */
},
VisudoOption {
short: 's',
long: "strict",
takes_argument: false,
set: |_, _| Ok(()),
/* ignored for compatibility sake */
},
VisudoOption {
short: 'V',
long: "version",
takes_argument: false,
set: |options, _| {
options.action = VisudoAction::Version;
Ok(())
},
},
VisudoOption {
short: 'O',
long: "owner",
takes_argument: false,
set: |options, _| {
options.owner = true;
Ok(())
},
},
VisudoOption {
short: 'P',
long: "perms",
takes_argument: false,
set: |options, _| {
options.perms = true;
Ok(())
},
},
];
pub(crate) fn from_env() -> Result<VisudoOptions, String> {
let args = std::env::args().collect();
Self::parse_arguments(args)
}
/// parse su arguments into VisudoOptions struct
pub(crate) fn parse_arguments(arguments: Vec<String>) -> Result<VisudoOptions, String> {
let mut options: VisudoOptions = VisudoOptions::default();
let mut arg_iter = arguments.into_iter().skip(1);
while let Some(arg) = arg_iter.next() {
// if the argument starts with -- it must be a full length option name
if arg.starts_with("--") {
// parse assignments like '--file=/etc/sudoers'
if let Some((key, value)) = arg.split_once('=') {
// lookup the option by name
if let Some(option) = Self::VISUDO_OPTIONS.iter().find(|o| o.long == &key[2..])
{
// the value is already present, when the option does not take any arguments this results in an error
if option.takes_argument {
(option.set)(&mut options, Some(value.to_string()))?;
} else {
Err(format!("'--{}' does not take any arguments", option.long))?;
}
} else {
Err(format!("unrecognized option '{arg}'"))?;
}
// lookup the option
} else if let Some(option) =
Self::VISUDO_OPTIONS.iter().find(|o| o.long == &arg[2..])
{
// try to parse an argument when the option needs an argument
if option.takes_argument {
let next_arg = arg_iter.next();
(option.set)(&mut options, next_arg)?;
} else {
(option.set)(&mut options, None)?;
}
} else {
Err(format!("unrecognized option '{arg}'"))?;
}
} else if arg.starts_with('-') && arg != "-" {
// flags can be grouped, so we loop over the characters
for (n, char) in arg.trim_start_matches('-').chars().enumerate() {
// lookup the option
if let Some(option) = Self::VISUDO_OPTIONS.iter().find(|o| o.short == char) {
// try to parse an argument when one is necessary, either the rest of the current flag group or the next argument
if option.takes_argument {
let rest = arg[(n + 2)..].trim().to_string();
let next_arg = if rest.is_empty() {
arg_iter.next()
} else {
Some(rest)
};
(option.set)(&mut options, next_arg)?;
// stop looping over flags if the current flag takes an argument
break;
} else {
// parse flag without argument
(option.set)(&mut options, None)?;
}
} else {
Err(format!("unrecognized option '{char}'"))?;
}
}
} else {
// If the arg doesn't start with a `-` it must be a file argument. However `-f`
// must take precedence
if options.file.is_none() {
options.file = Some(arg);
}
}
}
Ok(options)
}
}