1 # Some tests to ensure doas is working properly.
2 import ./make-test-python.nix (
5 meta.maintainers = with lib.maintainers; [ cole-h ];
10 users.groups = { foobar = {}; barfoo = {}; baz = { gid = 1337; }; };
12 test0 = { isNormalUser = true; extraGroups = [ "wheel" ]; };
13 test1 = { isNormalUser = true; };
14 test2 = { isNormalUser = true; extraGroups = [ "foobar" ]; };
15 test3 = { isNormalUser = true; extraGroups = [ "barfoo" ]; };
16 test4 = { isNormalUser = true; extraGroups = [ "baz" ]; };
17 test5 = { isNormalUser = true; };
18 test6 = { isNormalUser = true; };
19 test7 = { isNormalUser = true; };
24 wheelNeedsPassword = false;
27 { users = [ "test1" ]; groups = [ "foobar" ]; }
28 { users = [ "test2" ]; noPass = true; setEnv = [ "CORRECT" "HORSE=BATTERY" ]; }
29 { groups = [ "barfoo" 1337 ]; noPass = true; }
30 { users = [ "test5" ]; noPass = true; keepEnv = true; runAs = "test1"; }
31 { users = [ "test6" ]; noPass = true; keepEnv = true; setEnv = [ "-STAPLE" ]; }
32 { users = [ "test7" ]; noPass = true; setEnv = [ "-SSH_AUTH_SOCK" ]; }
38 with subtest("users in wheel group should have passwordless doas"):
39 machine.succeed('su - test0 -c "doas -u root true"')
41 with subtest("test1 user should not be able to use doas without password"):
42 machine.fail('su - test1 -c "doas -n -u root true"')
44 with subtest("test2 user should be able to keep some env"):
45 if "CORRECT=1" not in machine.succeed('su - test2 -c "CORRECT=1 doas env"'):
46 raise Exception("failed to keep CORRECT")
48 if "HORSE=BATTERY" not in machine.succeed('su - test2 -c "doas env"'):
49 raise Exception("failed to setenv HORSE=BATTERY")
51 with subtest("users in group 'barfoo' shouldn't require password"):
52 machine.succeed("doas -u test3 doas -n -u root true")
54 with subtest("users in group 'baz' (GID 1337) shouldn't require password"):
55 machine.succeed("doas -u test4 doas -n -u root echo true")
57 with subtest("test5 user should be able to run commands under test1"):
58 machine.succeed("doas -u test5 doas -n -u test1 true")
60 with subtest("test5 user should not be able to run commands under root"):
61 machine.fail("doas -u test5 doas -n -u root true")
63 with subtest("test6 user should be able to keepenv"):
64 envs = ["BATTERY=HORSE", "CORRECT=false"]
65 out = machine.succeed(
66 'su - test6 -c "BATTERY=HORSE CORRECT=false STAPLE=Tr0ub4dor doas env"'
69 if not all(env in out for env in envs):
70 raise Exception("failed to keep BATTERY or CORRECT")
71 if "STAPLE=Tr0ub4dor" in out:
72 raise Exception("failed to exclude STAPLE")
74 with subtest("test7 should not have access to SSH_AUTH_SOCK"):
75 if "SSH_AUTH_SOCK=HOLEY" in machine.succeed(
76 'su - test7 -c "SSH_AUTH_SOCK=HOLEY doas env"'
78 raise Exception("failed to exclude SSH_AUTH_SOCK")
80 # Test that the doas setuid wrapper precedes the unwrapped version in PATH after
82 # The PATH set by doas is defined in
83 # ../../pkgs/tools/security/doas/0001-add-NixOS-specific-dirs-to-safe-PATH.patch
84 with subtest("recursive calls to doas from subprocesses should succeed"):
85 machine.succeed('doas -u test0 sh -c "doas -u test0 true"')
87 with subtest("test0 should inherit TERMINFO_DIRS from the user environment"):
88 dirs = machine.succeed(
89 "su - test0 -c 'doas -u root $SHELL -c \"echo \$TERMINFO_DIRS\"'"
92 if not "test0" in dirs:
93 raise Exception(f"user profile TERMINFO_DIRS is not preserved: {dirs}")