2016-10-20 18:54:38 +00:00
|
|
|
{ config, lib, pkgs, ... }@args: with import <stockholm/lib>; let
|
2016-09-09 07:43:00 +00:00
|
|
|
|
|
|
|
cfg = config.lass.usershadow;
|
|
|
|
|
|
|
|
out = {
|
|
|
|
options.lass.usershadow = api;
|
|
|
|
config = lib.mkIf cfg.enable imp;
|
|
|
|
};
|
|
|
|
|
|
|
|
api = {
|
|
|
|
enable = mkEnableOption "usershadow";
|
|
|
|
pattern = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/home/%/.shadow";
|
|
|
|
};
|
2016-10-27 11:29:03 +00:00
|
|
|
path = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
};
|
2016-09-09 07:43:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
imp = {
|
|
|
|
environment.systemPackages = [ usershadow ];
|
2016-10-27 11:29:03 +00:00
|
|
|
lass.usershadow.path = "${usershadow}";
|
2016-09-09 07:43:00 +00:00
|
|
|
security.pam.services.sshd.text = ''
|
2019-04-26 15:25:16 +00:00
|
|
|
auth required pam_exec.so expose_authtok /run/wrappers/bin/shadow_verify_pam ${cfg.pattern}
|
2019-04-25 19:22:23 +00:00
|
|
|
auth required pam_permit.so
|
2016-09-09 07:43:00 +00:00
|
|
|
account required pam_permit.so
|
|
|
|
session required pam_permit.so
|
|
|
|
'';
|
|
|
|
|
2021-12-01 17:53:18 +00:00
|
|
|
security.pam.services.dovecot2.text = ''
|
|
|
|
auth required pam_exec.so expose_authtok /run/wrappers/bin/shadow_verify_pam ${cfg.pattern}
|
|
|
|
auth required pam_permit.so
|
|
|
|
account required pam_permit.so
|
|
|
|
session required pam_permit.so
|
|
|
|
'';
|
2019-04-13 12:49:48 +00:00
|
|
|
|
|
|
|
security.wrappers.shadow_verify_pam = {
|
|
|
|
source = "${usershadow}/bin/verify_pam";
|
|
|
|
owner = "root";
|
2021-12-01 18:12:00 +00:00
|
|
|
group = "root";
|
2019-04-13 12:49:48 +00:00
|
|
|
};
|
2019-04-17 18:16:06 +00:00
|
|
|
security.wrappers.shadow_verify_arg = {
|
|
|
|
source = "${usershadow}/bin/verify_arg";
|
|
|
|
owner = "root";
|
2021-12-01 18:12:00 +00:00
|
|
|
group = "root";
|
2019-04-17 18:16:06 +00:00
|
|
|
};
|
2016-09-09 07:43:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
usershadow = let {
|
|
|
|
deps = [
|
|
|
|
"pwstore-fast"
|
|
|
|
"bytestring"
|
|
|
|
];
|
2018-06-19 19:23:35 +00:00
|
|
|
body = pkgs.writeHaskellPackage "passwords" {
|
2019-04-13 12:54:29 +00:00
|
|
|
ghc-options = [
|
|
|
|
"-rtsopts"
|
|
|
|
"-Wall"
|
|
|
|
];
|
2016-10-27 11:29:03 +00:00
|
|
|
executables.verify_pam = {
|
2016-09-09 07:43:00 +00:00
|
|
|
extra-depends = deps;
|
|
|
|
text = ''
|
|
|
|
import System.IO
|
|
|
|
import Data.Char (chr)
|
|
|
|
import System.Environment (getEnv, getArgs)
|
|
|
|
import Crypto.PasswordStore (verifyPasswordWith, pbkdf2)
|
|
|
|
import qualified Data.ByteString.Char8 as BS8
|
|
|
|
import System.Exit (exitFailure, exitSuccess)
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
user <- getEnv "PAM_USER"
|
|
|
|
shadowFilePattern <- head <$> getArgs
|
|
|
|
let shadowFile = lhs <> user <> tail rhs
|
|
|
|
(lhs, rhs) = span (/= '%') shadowFilePattern
|
|
|
|
hash <- readFile shadowFile
|
|
|
|
password <- takeWhile (/= (chr 0)) <$> hGetLine stdin
|
|
|
|
let res = verifyPasswordWith pbkdf2 (2^) (BS8.pack password) (BS8.pack hash)
|
|
|
|
if res then exitSuccess else exitFailure
|
|
|
|
'';
|
|
|
|
};
|
2016-10-27 11:29:03 +00:00
|
|
|
executables.verify_arg = {
|
|
|
|
extra-depends = deps;
|
|
|
|
text = ''
|
2016-11-24 23:07:55 +00:00
|
|
|
import System.Environment (getArgs)
|
2016-10-27 11:29:03 +00:00
|
|
|
import Crypto.PasswordStore (verifyPasswordWith, pbkdf2)
|
|
|
|
import qualified Data.ByteString.Char8 as BS8
|
|
|
|
import System.Exit (exitFailure, exitSuccess)
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
argsList <- getArgs
|
|
|
|
let shadowFilePattern = argsList !! 0
|
|
|
|
let user = argsList !! 1
|
|
|
|
let password = argsList !! 2
|
|
|
|
let shadowFile = lhs <> user <> tail rhs
|
|
|
|
(lhs, rhs) = span (/= '%') shadowFilePattern
|
|
|
|
hash <- readFile shadowFile
|
|
|
|
let res = verifyPasswordWith pbkdf2 (2^) (BS8.pack password) (BS8.pack hash)
|
|
|
|
if res then do (putStr "yes") else exitFailure
|
|
|
|
'';
|
|
|
|
};
|
2016-09-09 07:43:00 +00:00
|
|
|
executables.passwd = {
|
|
|
|
extra-depends = deps;
|
|
|
|
text = ''
|
|
|
|
import System.Environment (getEnv)
|
|
|
|
import Crypto.PasswordStore (makePasswordWith, pbkdf2)
|
|
|
|
import qualified Data.ByteString.Char8 as BS8
|
2016-11-24 23:07:55 +00:00
|
|
|
import System.IO (stdin, stdout, hSetEcho, hFlush, putStr, putStrLn)
|
|
|
|
import Control.Exception (bracket_)
|
2016-09-09 07:43:00 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
home <- getEnv "HOME"
|
2016-11-24 23:07:55 +00:00
|
|
|
mb_password <- bracket_ (hSetEcho stdin False) (hSetEcho stdin True) $ do
|
|
|
|
putStr "Enter new UNIX password: "
|
|
|
|
hFlush stdout
|
|
|
|
password <- BS8.hGetLine stdin
|
|
|
|
putStrLn ""
|
|
|
|
putStr "Retype new UNIX password: "
|
|
|
|
hFlush stdout
|
|
|
|
password2 <- BS8.hGetLine stdin
|
|
|
|
return $ if password == password2
|
|
|
|
then Just password
|
|
|
|
else Nothing
|
|
|
|
case mb_password of
|
|
|
|
Just password -> do
|
|
|
|
hash <- makePasswordWith pbkdf2 password 10
|
|
|
|
BS8.writeFile (home ++ "/.shadow") hash
|
|
|
|
putStrLn "passwd: all authentication tokens updated successfully."
|
|
|
|
Nothing -> putStrLn "Sorry, passwords do not match"
|
2016-09-09 07:43:00 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
in out
|