sudo-rs 0.2.0-dev.20230627

A memory safe implementation of sudo and su.
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::path::PathBuf;

#[derive(Debug, PartialEq)]
pub struct SuOptions {
    pub user: String,
    pub command: Option<String>,
    pub group: Option<String>,
    pub supp_group: Option<String>,
    pub pty: bool,
    pub login: bool,
    pub shell: Option<PathBuf>,
    pub whitelist_environment: Vec<String>,
    pub arguments: Vec<String>,
    pub action: SuAction,
}

impl Default for SuOptions {
    fn default() -> Self {
        Self {
            user: "root".to_owned(),
            command: None,
            group: None,
            supp_group: None,
            pty: false,
            login: false,
            shell: None,
            whitelist_environment: vec![],
            arguments: vec![],
            action: SuAction::Run,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum SuAction {
    Help,
    Version,
    Run,
}

type OptionSetter = &'static dyn Fn(&mut SuOptions, Option<String>) -> Result<(), String>;

struct SuOption {
    short: char,
    long: &'static str,
    takes_argument: bool,
    set: OptionSetter,
}

impl SuOptions {
    const SU_OPTIONS: &[SuOption] = &[
        SuOption {
            short: 'c',
            long: "command",
            takes_argument: true,
            set: &|sudo_options, argument| {
                if argument.is_some() {
                    sudo_options.command = argument;
                } else {
                    Err("no command provided")?
                }

                Ok(())
            },
        },
        SuOption {
            short: 'g',
            long: "group",
            takes_argument: true,
            set: &|sudo_options, argument| {
                if argument.is_some() {
                    sudo_options.group = argument;
                } else {
                    Err("no group provided")?
                }

                Ok(())
            },
        },
        SuOption {
            short: 'G',
            long: "supp-group",
            takes_argument: true,
            set: &|sudo_options, argument| {
                if argument.is_some() {
                    sudo_options.supp_group = argument;
                } else {
                    Err("no supplementary group provided")?
                }

                Ok(())
            },
        },
        SuOption {
            short: 'l',
            long: "login",
            takes_argument: false,
            set: &|sudo_options, _| {
                sudo_options.login = true;
                Ok(())
            },
        },
        SuOption {
            short: 'P',
            long: "pty",
            takes_argument: false,
            set: &|sudo_options, _| {
                sudo_options.pty = true;
                Ok(())
            },
        },
        SuOption {
            short: 's',
            long: "shell",
            takes_argument: true,
            set: &|sudo_options, argument| {
                if let Some(path) = argument {
                    sudo_options.shell = Some(PathBuf::from(path));
                } else {
                    Err("no shell provided")?
                }

                Ok(())
            },
        },
        SuOption {
            short: 'w',
            long: "whitelist-environment",
            takes_argument: true,
            set: &|sudo_options, argument| {
                if let Some(list) = argument {
                    sudo_options.whitelist_environment =
                        list.split(',').map(str::to_string).collect();
                } else {
                    Err("no enivronment whitelist provided")?
                }

                Ok(())
            },
        },
        SuOption {
            short: 'V',
            long: "version",
            takes_argument: false,
            set: &|sudo_options, _| {
                sudo_options.action = SuAction::Version;
                Ok(())
            },
        },
        SuOption {
            short: 'h',
            long: "help",
            takes_argument: false,
            set: &|sudo_options, _| {
                sudo_options.action = SuAction::Help;
                Ok(())
            },
        },
    ];

    pub fn from_env() -> Result<SuOptions, String> {
        let args = std::env::args().collect();

        Self::parse_arguments(args)
    }

    /// parse su arguments into SuOptions struct
    fn parse_arguments(arguments: Vec<String>) -> Result<SuOptions, String> {
        let mut options: SuOptions = SuOptions::default();
        let mut arg_iter = arguments.into_iter().skip(1);

        while let Some(arg) = arg_iter.next() {
            // - or -l or --login indicates a login shell should be started
            if arg == "-" {
                options.login = true;
            // if the argument starts with -- it must be a full length option name
            } else if arg.starts_with("--") {
                // parse assignments like '--group=ferris'
                if arg.contains('=') {
                    // convert assignment to normal tokens
                    let (key, value) = arg.split_once('=').unwrap();
                    // lookup the option by name
                    if let Some(option) = Self::SU_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::SU_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('-') {
                // flags can be grouped, so we loop over the the characters
                for (n, char) in arg.trim_start_matches('-').chars().enumerate() {
                    // lookup the option
                    if let Some(option) = Self::SU_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 {
                // when no option is provided (styarting with - or --) the next argument is interpreted as an username
                options.user = arg;
                // the rest of the arguments are passed to the shell
                options.arguments = arg_iter.collect();
                break;
            }
        }

        Ok(options)
    }
}

#[cfg(test)]
mod tests {
    use std::vec;

    use super::{SuAction, SuOptions};

    fn parse(args: &[&str]) -> SuOptions {
        let mut args = args.iter().map(|s| s.to_string()).collect::<Vec<String>>();
        args.insert(0, "/bin/su".to_string());
        SuOptions::parse_arguments(args).unwrap()
    }

    #[test]

    fn it_parses_group() {
        let expected = SuOptions {
            group: Some("ferris".to_string()),
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-g", "ferris"]));
        assert_eq!(expected, parse(&["-gferris"]));
        assert_eq!(expected, parse(&["--group", "ferris"]));
        assert_eq!(expected, parse(&["--group=ferris"]));
    }

    #[test]
    fn it_parses_shell_default() {
        let result = parse(&["--shell", "/bin/bash"]);
        assert_eq!(
            result,
            SuOptions {
                shell: Some("/bin/bash".into()),
                ..Default::default()
            }
        );
    }

    #[test]
    fn it_parses_whitelist() {
        let result = parse(&["-w", "FOO,BAR"]);
        assert_eq!(
            result,
            SuOptions {
                whitelist_environment: vec!["FOO".to_string(), "BAR".to_string()],
                ..Default::default()
            }
        );
    }

    #[test]
    fn it_parses_combined_options() {
        let expected = SuOptions {
            login: true,
            pty: true,
            ..Default::default()
        };

        assert_eq!(expected, parse(&["-Pl"]));
        assert_eq!(expected, parse(&["-lP"]));
    }

    #[test]
    fn it_parses_combined_options_and_arguments() {
        let expected = SuOptions {
            login: true,
            pty: true,
            shell: Some("/bin/bash".into()),
            ..Default::default()
        };

        assert_eq!(expected, parse(&["-Pls/bin/bash"]));
        assert_eq!(expected, parse(&["-Pls", "/bin/bash"]));
        assert_eq!(expected, parse(&["-Pl", "-s/bin/bash"]));
        assert_eq!(expected, parse(&["-lP", "-s", "/bin/bash"]));
        assert_eq!(expected, parse(&["-lP", "--shell=/bin/bash"]));
        assert_eq!(expected, parse(&["-lP", "--shell", "/bin/bash"]));
    }

    #[test]
    fn it_parses_an_user() {
        assert_eq!(
            SuOptions {
                user: "ferris".to_string(),
                pty: true,
                ..Default::default()
            },
            parse(&["-P", "ferris"])
        );

        assert_eq!(
            SuOptions {
                user: "ferris".to_string(),
                arguments: vec!["-P".to_string()],
                ..Default::default()
            },
            parse(&["ferris", "-P"])
        );
    }

    #[test]
    fn it_parses_arguments() {
        let expected = SuOptions {
            user: "ferris".to_string(),
            pty: true,
            arguments: vec!["script.sh".to_string()],
            ..Default::default()
        };

        assert_eq!(expected, parse(&["-P", "ferris", "script.sh"]));
    }

    #[test]
    fn it_parses_command() {
        let expected = SuOptions {
            command: Some("'echo hi'".to_string()),
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-c", "'echo hi'"]));
        assert_eq!(expected, parse(&["-c'echo hi'"]));
        assert_eq!(expected, parse(&["--command", "'echo hi'"]));
        assert_eq!(expected, parse(&["--command='echo hi'"]));

        let expected = SuOptions {
            command: Some("env".to_string()),
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-c", "env"]));
        assert_eq!(expected, parse(&["-cenv"]));
        assert_eq!(expected, parse(&["--command", "env"]));
        assert_eq!(expected, parse(&["--command=env"]));
    }

    #[test]
    fn it_parses_supplementary_group() {
        let expected = SuOptions {
            supp_group: Some("ferris".to_string()),
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-G", "ferris"]));
        assert_eq!(expected, parse(&["-Gferris"]));
        assert_eq!(expected, parse(&["--supp-group", "ferris"]));
        assert_eq!(expected, parse(&["--supp-group=ferris"]));
    }

    #[test]
    fn it_parses_login() {
        let expected = SuOptions {
            login: true,
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-"]));
        assert_eq!(expected, parse(&["-l"]));
        assert_eq!(expected, parse(&["--login"]));
    }

    #[test]
    fn it_parses_pty() {
        let expected = SuOptions {
            pty: true,
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-P"]));
        assert_eq!(expected, parse(&["--pty"]));
    }

    #[test]
    fn it_parses_shell() {
        let expected = SuOptions {
            shell: Some("some-shell".into()),
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-s", "some-shell"]));
        assert_eq!(expected, parse(&["-ssome-shell"]));
        assert_eq!(expected, parse(&["--shell", "some-shell"]));
        assert_eq!(expected, parse(&["--shell=some-shell"]));
    }

    #[test]
    fn it_parses_whitelist_environment() {
        let expected = SuOptions {
            whitelist_environment: vec!["FOO".to_string(), "BAR".to_string()],
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-w", "FOO,BAR"]));
        assert_eq!(expected, parse(&["-wFOO,BAR"]));
        assert_eq!(expected, parse(&["--whitelist-environment", "FOO,BAR"]));
        assert_eq!(expected, parse(&["--whitelist-environment=FOO,BAR"]));
    }

    #[test]
    fn it_parses_help() {
        let expected = SuOptions {
            action: SuAction::Help,
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-h"]));
        assert_eq!(expected, parse(&["--help"]));
    }

    #[test]
    fn it_parses_version() {
        let expected = SuOptions {
            action: SuAction::Version,
            ..Default::default()
        };
        assert_eq!(expected, parse(&["-V"]));
        assert_eq!(expected, parse(&["--version"]));
    }
}