git.iko.soy/typeable/nixpkgs
on master help sign in
pam_oath: require OATH and pam_unix credentials to be valid
commit96d767de621242a5df6de5db82a9b088d24ef606 authorGraham Christensen <graham@grahamc.com> dateSun 12 Feb 2017, 18:17 (10 years ago) parent59e77da
3 files changed+129−2
nixos/modules/security/pam.nix+2−2 nixos/release.nix+1 nixos/tests/pam-oath-login.nix+126
nixos/modules/security/pam.nix+2−2
@@ -253,6 +253,8 @@ let
253253 "auth sufficient ${pkgs.pam_u2f}/lib/security/pam_u2f.so"}
254254 ${optionalString cfg.usbAuth
255255 "auth sufficient ${pkgs.pam_usb}/lib/security/pam_usb.so"}
256+ ${let oath = config.security.pam.oath; in optionalString cfg.oathAuth
257+ "auth requisite ${pkgs.oathToolkit}/lib/security/pam_oath.so window=${toString oath.window} usersfile=${toString oath.usersFile} digits=${toString oath.digits}"}
256258 '' +
257259 # Modules in this block require having the password set in PAM_AUTHTOK.
258260 # pam_unix is marked as 'sufficient' on NixOS which means nothing will run
@@ -271,8 +273,6 @@ let
271273 "auth sufficient pam_unix.so ${optionalString cfg.allowNullPassword "nullok"} likeauth try_first_pass"}
272274 ${optionalString cfg.otpwAuth
273275 "auth sufficient ${pkgs.otpw}/lib/security/pam_otpw.so"}
274− ${let oath = config.security.pam.oath; in optionalString cfg.oathAuth
275− "auth sufficient ${pkgs.oathToolkit}/lib/security/pam_oath.so window=${toString oath.window} usersfile=${toString oath.usersFile} digits=${toString oath.digits}"}
276276 ${optionalString use_ldap
277277 "auth sufficient ${pam_ldap}/lib/security/pam_ldap.so use_first_pass"}
278278 ${optionalString config.services.sssd.enable
nixos/release.nix+1
@@ -283,6 +283,7 @@ in rec {
283283 tests.leaps = callTest tests/leaps.nix { };
284284 tests.nsd = callTest tests/nsd.nix {};
285285 tests.openssh = callTest tests/openssh.nix {};
286+ tests.pam-oath-login = callTest tests/pam-oath-login.nix {};
286287 #tests.panamax = hydraJob (import tests/panamax.nix { system = "x86_64-linux"; });
287288 tests.peerflix = callTest tests/peerflix.nix {};
288289 tests.postgresql = callTest tests/postgresql.nix {};
nixos/tests/pam-oath-login.nixnew file+126
@@ -0,0 +1,126 @@
1+import ./make-test.nix ({ pkgs, latestKernel ? false, ... }:
2+
3+let
4+ oathSnakeoilSecret = "cdd4083ef8ff1fa9178c6d46bfb1a3";
5+
6+ # With HOTP mode the password is calculated based on a counter of
7+ # how many passwords have been made. In this env, we'll always be on
8+ # the 0th counter, so the password is static.
9+ #
10+ # Generated in nix-shell -p oathToolkit
11+ # via: oathtool -v -d6 -w10 cdd4083ef8ff1fa9178c6d46bfb1a3
12+ # and picking a the first 4:
13+ oathSnakeOilPassword1 = "143349";
14+ oathSnakeOilPassword2 = "801753";
15+ oathSnakeOilPassword3 = "019933";
16+ oathSnakeOilPassword4 = "403895";
17+
18+ alicePassword = "foobar";
19+ # Generated via: mkpasswd -m sha-512 and passing in "foobar"
20+ hashedAlicePassword = "$6$MsMrE1q.1HrCgTS$Vq2e/uILzYjSN836TobAyN9xh9oi7EmCmucnZID25qgPoibkw8qTCugiAPnn4eCGvn1A.7oEBFJaaGUaJsQQY.";
21+
22+in
23+{
24+ name = "pam-oath-login";
25+
26+ machine =
27+ { config, pkgs, lib, ... }:
28+ {
29+ security.pam.oath = {
30+ enable = true;
31+ };
32+
33+ users.extraUsers.alice = {
34+ isNormalUser = true;
35+ name = "alice";
36+ uid = 1000;
37+ hashedPassword = hashedAlicePassword;
38+ extraGroups = [ "wheel" ];
39+ createHome = true;
40+ home = "/home/alice";
41+ };
42+
43+
44+ systemd.services.setupOathSnakeoilFile = {
45+ wantedBy = [ "default.target" ];
46+ before = [ "default.target" ];
47+ unitConfig = {
48+ type = "oneshot";
49+ RemainAfterExit = true;
50+ };
51+ script = ''
52+ touch /etc/users.oath
53+ chmod 600 /etc/users.oath
54+ chown root /etc/users.oath
55+ echo "HOTP/E/6 alice - ${oathSnakeoilSecret}" > /etc/users.oath
56+ '';
57+ };
58+ };
59+
60+ testScript =
61+ ''
62+ $machine->waitForUnit('multi-user.target');
63+ $machine->waitUntilSucceeds("pgrep -f 'agetty.*tty1'");
64+ $machine->screenshot("postboot");
65+
66+
67+ subtest "Invalid password", sub {
68+ $machine->fail("pgrep -f 'agetty.*tty2'");
69+ $machine->sendKeys("alt-f2");
70+ $machine->waitUntilSucceeds("[ \$(fgconsole) = 2 ]");
71+ $machine->waitForUnit('getty@tty2.service');
72+ $machine->waitUntilSucceeds("pgrep -f 'agetty.*tty2'");
73+
74+ $machine->waitUntilTTYMatches(2, "login: ");
75+ $machine->sendChars("alice\n");
76+ $machine->waitUntilTTYMatches(2, "login: alice");
77+ $machine->waitUntilSucceeds("pgrep login");
78+
79+ $machine->waitUntilTTYMatches(2, "One-time password");
80+ $machine->sendChars("${oathSnakeOilPassword1}\n");
81+ $machine->waitUntilTTYMatches(2, "Password: ");
82+ $machine->sendChars("blorg\n");
83+ $machine->waitUntilTTYMatches(2, "Login incorrect");
84+ };
85+
86+ subtest "Invalid oath token", sub {
87+ $machine->fail("pgrep -f 'agetty.*tty3'");
88+ $machine->sendKeys("alt-f3");
89+ $machine->waitUntilSucceeds("[ \$(fgconsole) = 3 ]");
90+ $machine->waitForUnit('getty@tty3.service');
91+ $machine->waitUntilSucceeds("pgrep -f 'agetty.*tty3'");
92+
93+ $machine->waitUntilTTYMatches(3, "login: ");
94+ $machine->sendChars("alice\n");
95+ $machine->waitUntilTTYMatches(3, "login: alice");
96+ $machine->waitUntilSucceeds("pgrep login");
97+ $machine->waitUntilTTYMatches(3, "One-time password");
98+ $machine->sendChars("000000\n");
99+ $machine->waitUntilTTYMatches(3, "Login incorrect");
100+ $machine->waitUntilTTYMatches(3, "login:");
101+ };
102+
103+ subtest "Happy path (both passwords are mandatory to get us in)", sub {
104+ $machine->fail("pgrep -f 'agetty.*tty4'");
105+ $machine->sendKeys("alt-f4");
106+ $machine->waitUntilSucceeds("[ \$(fgconsole) = 4 ]");
107+ $machine->waitForUnit('getty@tty4.service');
108+ $machine->waitUntilSucceeds("pgrep -f 'agetty.*tty4'");
109+
110+ $machine->waitUntilTTYMatches(4, "login: ");
111+ $machine->sendChars("alice\n");
112+ $machine->waitUntilTTYMatches(4, "login: alice");
113+ $machine->waitUntilSucceeds("pgrep login");
114+ $machine->waitUntilTTYMatches(4, "One-time password");
115+ $machine->sendChars("${oathSnakeOilPassword2}\n");
116+ $machine->waitUntilTTYMatches(4, "Password: ");
117+ $machine->sendChars("${alicePassword}\n");
118+
119+ $machine->waitUntilSucceeds("pgrep -u alice bash");
120+ $machine->sendChars("touch done4\n");
121+ $machine->waitForFile("/home/alice/done4");
122+ };
123+
124+ '';
125+
126+})