module main import os // id_un is the running user as coreutils' id names it: by the real uid, the // way the escalation tools name their caller fn id_un() string { res := os.exec(['id', '-un']) assert res.exit_code == 0, 'id -un failed: ${res.output}' return res.output.trim_space() } fn test_current_user_is_the_real_uid() { // getlogin(3) would answer the session's login, which differs after an // su and is absent without a login session; the real uid always names // the user running vpac assert current_user()! == id_un() } fn test_invoker_user_unescalated_is_the_running_user() { if os.geteuid() == 0 { // root reads the invoker from the escalation instead return } assert invoker_user()! == id_un() } fn test_invoker_of_accepts_a_user_of_the_password_database() { me := current_user()! if os.getuid() == 0 { // a root test run has no unprivileged user of its own to accept return } assert invoker_of(me)! == me } fn test_invoker_of_rejects_root_by_uid() { // root is turned away through its uid, the same check that turns away // any alias of uid 0: the name 'root' is not special-cased invoker_of('root') or { assert err.msg().contains('not as root') assert err.msg().contains('uid 0') return } assert false, 'root accepted as the invoker' } fn test_invoker_of_rejects_an_unknown_user() { name := 'vpac-no-such-user-${os.getpid()}' invoker_of(name) or { assert err.msg().contains('not in the password database') assert err.msg().contains(name) return } assert false, 'an unknown user accepted as the invoker' } fn test_invoker_of_rejects_an_empty_name() { invoker_of('') or { assert err.msg().contains('run vpac as your user') return } assert false, 'an empty invoker accepted' } fn test_user_uid() { assert user_uid('root')? == 0 assert user_uid(current_user()!)? == u32(os.getuid()) if _ := user_uid('vpac-no-such-user-${os.getpid()}') { assert false, 'an unknown user has a uid' } } fn test_user_home_matches_the_password_database() { // getent passwd reads through nss, as getpwnam does: field 6 is the home me := current_user()! res := os.exec(['getent', 'passwd', me]) if res.exit_code != 0 { // no getent on this system: nothing to compare against return } fields := res.output.trim_space().split(':') assert fields.len >= 6 assert user_home(me)! == fields[5] if _ := user_home('vpac-no-such-user-${os.getpid()}') { assert false, 'an unknown user has a home' } } fn test_shell_words_reads_back_as_the_words() { // what su -c gives root's shell: the words come back one for one, with // nothing in them expanded or run words := ['env', 'VPAC_INVOKER=me', '/usr/bin/vpac', 'i', "it's", '$(id)', '`id`', 'a b', 'x;y', '--eval=(define x "q")', ''] line := os.exec(['sh', '-c', 'set -- ' + shell_words(words) + '; for w in "$@"; do printf "[%s]" "$w"; done']) assert line.exit_code == 0, line.output assert line.output == words.map('[${it}]').join('') } // the provider's own arguments come first; su takes the run as one quoted // shell line, pkexec is kept in the working dir, and a command vpac does // not know is handed the words as sudo is fn test_root_argv() { envs := ['VPAC_INVOKER=me'] args := ['i', 'a b'] run := ['env', 'VPAC_INVOKER=me', '/usr/bin/vpac', 'i', 'a b'] assert root_argv(['sudo'], envs, '/usr/bin/vpac', args) == run mut custom := ['-A', '--'] custom << run assert root_argv(['/opt/bin/myroot', '-A', '--'], envs, '/usr/bin/vpac', args) == custom mut pk := ['--keep-cwd'] pk << run assert root_argv(['pkexec'], envs, '/usr/bin/vpac', args) == pk su := root_argv(['/usr/bin/su', '-l'], envs, '/usr/bin/vpac', args) assert su[..2] == ['-l', '-c'] assert su.len == 3 assert su[2].contains("'a b'"), su[2] }