| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | // id_un is the running user as coreutils' id names it: by the real uid, the |
| 6 | // way the escalation tools name their caller |
| 7 | fn id_un() string { |
| 8 | res := os.exec(['id', '-un']) |
| 9 | assert res.exit_code == 0, 'id -un failed: ${res.output}' |
| 10 | return res.output.trim_space() |
| 11 | } |
| 12 | |
| 13 | fn test_current_user_is_the_real_uid() { |
| 14 | // getlogin(3) would answer the session's login, which differs after an |
| 15 | // su and is absent without a login session; the real uid always names |
| 16 | // the user running vpac |
| 17 | assert current_user()! == id_un() |
| 18 | } |
| 19 | |
| 20 | fn test_invoker_user_unescalated_is_the_running_user() { |
| 21 | if os.geteuid() == 0 { |
| 22 | // root reads the invoker from the escalation instead |
| 23 | return |
| 24 | } |
| 25 | assert invoker_user()! == id_un() |
| 26 | } |
| 27 | |
| 28 | fn test_invoker_of_accepts_a_user_of_the_password_database() { |
| 29 | me := current_user()! |
| 30 | if os.getuid() == 0 { |
| 31 | // a root test run has no unprivileged user of its own to accept |
| 32 | return |
| 33 | } |
| 34 | assert invoker_of(me)! == me |
| 35 | } |
| 36 | |
| 37 | fn test_invoker_of_rejects_root_by_uid() { |
| 38 | // root is turned away through its uid, the same check that turns away |
| 39 | // any alias of uid 0: the name 'root' is not special-cased |
| 40 | invoker_of('root') or { |
| 41 | assert err.msg().contains('not as root') |
| 42 | assert err.msg().contains('uid 0') |
| 43 | return |
| 44 | } |
| 45 | assert false, 'root accepted as the invoker' |
| 46 | } |
| 47 | |
| 48 | fn test_invoker_of_rejects_an_unknown_user() { |
| 49 | name := 'vpac-no-such-user-${os.getpid()}' |
| 50 | invoker_of(name) or { |
| 51 | assert err.msg().contains('not in the password database') |
| 52 | assert err.msg().contains(name) |
| 53 | return |
| 54 | } |
| 55 | assert false, 'an unknown user accepted as the invoker' |
| 56 | } |
| 57 | |
| 58 | fn test_invoker_of_rejects_an_empty_name() { |
| 59 | invoker_of('') or { |
| 60 | assert err.msg().contains('run vpac as your user') |
| 61 | return |
| 62 | } |
| 63 | assert false, 'an empty invoker accepted' |
| 64 | } |
| 65 | |
| 66 | fn test_user_uid() { |
| 67 | assert user_uid('root')? == 0 |
| 68 | assert user_uid(current_user()!)? == u32(os.getuid()) |
| 69 | if _ := user_uid('vpac-no-such-user-${os.getpid()}') { |
| 70 | assert false, 'an unknown user has a uid' |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn test_user_home_matches_the_password_database() { |
| 75 | // getent passwd reads through nss, as getpwnam does: field 6 is the home |
| 76 | me := current_user()! |
| 77 | res := os.exec(['getent', 'passwd', me]) |
| 78 | if res.exit_code != 0 { |
| 79 | // no getent on this system: nothing to compare against |
| 80 | return |
| 81 | } |
| 82 | fields := res.output.trim_space().split(':') |
| 83 | assert fields.len >= 6 |
| 84 | assert user_home(me)! == fields[5] |
| 85 | if _ := user_home('vpac-no-such-user-${os.getpid()}') { |
| 86 | assert false, 'an unknown user has a home' |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn test_shell_words_reads_back_as_the_words() { |
| 91 | // what su -c gives root's shell: the words come back one for one, with |
| 92 | // nothing in them expanded or run |
| 93 | words := ['env', 'VPAC_INVOKER=me', '/usr/bin/vpac', 'i', "it's", '$(id)', '`id`', 'a b', 'x;y', |
| 94 | '--eval=(define x "q")', ''] |
| 95 | line := os.exec(['sh', '-c', |
| 96 | 'set -- ' + shell_words(words) + '; for w in "$@"; do printf "[%s]" "$w"; done']) |
| 97 | assert line.exit_code == 0, line.output |
| 98 | assert line.output == words.map('[${it}]').join('') |
| 99 | } |
| 100 | |
| 101 | // the provider's own arguments come first; su takes the run as one quoted |
| 102 | // shell line, pkexec is kept in the working dir, and a command vpac does |
| 103 | // not know is handed the words as sudo is |
| 104 | fn test_root_argv() { |
| 105 | envs := ['VPAC_INVOKER=me'] |
| 106 | args := ['i', 'a b'] |
| 107 | run := ['env', 'VPAC_INVOKER=me', '/usr/bin/vpac', 'i', 'a b'] |
| 108 | assert root_argv(['sudo'], envs, '/usr/bin/vpac', args) == run |
| 109 | mut custom := ['-A', '--'] |
| 110 | custom << run |
| 111 | assert root_argv(['/opt/bin/myroot', '-A', '--'], envs, '/usr/bin/vpac', args) == custom |
| 112 | mut pk := ['--keep-cwd'] |
| 113 | pk << run |
| 114 | assert root_argv(['pkexec'], envs, '/usr/bin/vpac', args) == pk |
| 115 | su := root_argv(['/usr/bin/su', '-l'], envs, '/usr/bin/vpac', args) |
| 116 | assert su[..2] == ['-l', '-c'] |
| 117 | assert su.len == 3 |
| 118 | assert su[2].contains("'a b'"), su[2] |
| 119 | } |
| 120 | |