2018-12-06 22:03:38 -05:00
|
|
|
{-
|
|
|
|
|
|
|
|
passman
|
|
|
|
Copyright (C) 2018 Jonathan Lamothe
|
|
|
|
<jlamothe1980@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2018-12-06 14:31:36 -05:00
|
|
|
|
2018-12-06 22:03:38 -05:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this program. If not, see
|
|
|
|
<https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
-}
|
2018-12-07 21:07:42 -05:00
|
|
|
|
2018-12-09 00:35:27 -05:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2018-12-06 22:03:38 -05:00
|
|
|
module Main where
|
2018-12-06 14:31:36 -05:00
|
|
|
|
2018-12-23 16:24:08 -05:00
|
|
|
import Control.Applicative ((<|>))
|
|
|
|
import Control.Lens (makeLenses, over, set, view, (^.))
|
2018-12-09 00:35:27 -05:00
|
|
|
import qualified Control.Monad.Trans.State as S
|
2018-12-23 16:24:08 -05:00
|
|
|
import Control.Monad (join, when)
|
2018-12-09 00:35:27 -05:00
|
|
|
import Control.Monad.Trans.Class (lift)
|
2018-12-17 00:04:43 -05:00
|
|
|
import Data.Foldable (mapM_)
|
2018-12-09 00:35:27 -05:00
|
|
|
import Data.Maybe (fromJust)
|
2018-12-18 15:00:18 -05:00
|
|
|
import System.Console.HCL
|
|
|
|
( Request (..)
|
|
|
|
, prompt
|
2018-12-23 16:24:08 -05:00
|
|
|
, reqAgree
|
2018-12-18 15:00:18 -05:00
|
|
|
, reqDefault
|
|
|
|
, reqFail
|
2018-12-23 16:24:08 -05:00
|
|
|
, reqIf
|
|
|
|
, reqInt
|
2018-12-18 15:00:18 -05:00
|
|
|
, reqIO
|
|
|
|
, reqMenu
|
|
|
|
, reqPassword
|
2018-12-23 16:24:08 -05:00
|
|
|
, reqResp
|
2018-12-18 15:00:18 -05:00
|
|
|
, required
|
|
|
|
, runRequest
|
|
|
|
)
|
2018-12-23 16:24:08 -05:00
|
|
|
import System.Random (RandomGen (..), StdGen, getStdGen)
|
2018-12-18 15:00:18 -05:00
|
|
|
|
|
|
|
import Password
|
2018-12-09 00:35:27 -05:00
|
|
|
|
|
|
|
data Status = Status
|
2018-12-18 15:00:18 -05:00
|
|
|
{ _gen :: StdGen
|
|
|
|
, _masterPass :: String
|
|
|
|
, _database :: PWDatabase
|
2018-12-09 00:35:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''Status
|
2018-12-07 21:07:42 -05:00
|
|
|
|
2018-12-23 16:24:08 -05:00
|
|
|
instance RandomGen Status where
|
|
|
|
next s = (x, s') where
|
|
|
|
(x, g') = next g
|
|
|
|
s' = set gen g' s
|
|
|
|
g = s^.gen
|
|
|
|
split s = (s1, s2) where
|
|
|
|
s1 = set gen g1 s
|
|
|
|
s2 = set gen g2 s
|
|
|
|
(g1, g2) = split g
|
|
|
|
g = s^.gen
|
|
|
|
|
2018-12-06 14:31:36 -05:00
|
|
|
main :: IO ()
|
2018-12-18 15:00:18 -05:00
|
|
|
main = runRequest setup >>= mapM_ (S.evalStateT mainMenu)
|
2018-12-07 21:07:42 -05:00
|
|
|
|
2018-12-18 15:00:18 -05:00
|
|
|
setup :: Request Status
|
|
|
|
setup = do
|
|
|
|
g <- reqIO getStdGen
|
|
|
|
mp <- getMasterPass
|
|
|
|
return $ Status g mp newPWDatabase
|
2018-12-09 00:35:27 -05:00
|
|
|
|
2018-12-18 15:00:18 -05:00
|
|
|
getMasterPass :: Request String
|
2018-12-07 21:07:42 -05:00
|
|
|
getMasterPass = do
|
2018-12-18 15:00:18 -05:00
|
|
|
p1 <- required $ prompt "master password: " reqPassword
|
|
|
|
p2 <- required $ prompt "confirm master password: " reqPassword
|
2018-12-07 21:07:42 -05:00
|
|
|
if p1 /= p2
|
|
|
|
then do
|
2018-12-18 15:00:18 -05:00
|
|
|
reqIO $ putStrLn "passwords do not match"
|
|
|
|
reqFail
|
2018-12-07 21:07:42 -05:00
|
|
|
else return p1
|
2018-12-06 22:03:38 -05:00
|
|
|
|
2018-12-09 00:35:27 -05:00
|
|
|
mainMenu :: S.StateT Status IO ()
|
2018-12-17 00:04:43 -05:00
|
|
|
mainMenu =
|
2018-12-09 00:35:27 -05:00
|
|
|
menu "Main Menu"
|
2018-12-23 16:24:08 -05:00
|
|
|
[ ( "add a password", addPassword )
|
2018-12-24 14:02:28 -05:00
|
|
|
, ( "view/edit a password", viewEditPass )
|
2018-12-23 16:24:08 -05:00
|
|
|
, ( "change master password", changeMasterPass )
|
2018-12-09 14:31:58 -05:00
|
|
|
, ( "lock session", lockSession )
|
2018-12-09 00:35:27 -05:00
|
|
|
, ( "quit", quit )
|
|
|
|
]
|
|
|
|
|
2018-12-23 16:24:08 -05:00
|
|
|
addPassword :: S.StateT Status IO ()
|
|
|
|
addPassword = do
|
|
|
|
svc <- req $ prompt "service name: " reqResp
|
|
|
|
db <- S.gets $ view database
|
|
|
|
if pwHasService svc db
|
|
|
|
then req (confirm "service exists - overwrite?") >>= flip when (addPassword' svc)
|
|
|
|
else addPassword' svc
|
|
|
|
mainMenu
|
|
|
|
|
|
|
|
addPassword' :: String -> S.StateT Status IO ()
|
|
|
|
addPassword' x = do
|
|
|
|
d <- buildData
|
|
|
|
S.modify $ over database $ pwSetService x d
|
|
|
|
showPass x
|
|
|
|
|
2018-12-24 14:02:28 -05:00
|
|
|
viewEditPass :: S.StateT Status IO ()
|
|
|
|
viewEditPass = menu "View/Edit Password"
|
|
|
|
[ ( "search servives", searchServ )
|
|
|
|
, ( "list services", listServ )
|
|
|
|
, ( "cancel", mainMenu )
|
|
|
|
]
|
|
|
|
|
|
|
|
searchServ :: S.StateT Status IO ()
|
|
|
|
searchServ = do
|
|
|
|
svc <- req $ prompt "service name: " reqResp
|
|
|
|
db <- S.gets $ view database
|
|
|
|
selectServ $ pwSearch svc db
|
|
|
|
|
|
|
|
listServ :: S.StateT Status IO ()
|
|
|
|
listServ = S.gets (view database) >>= selectServ . pwSearch ""
|
|
|
|
|
|
|
|
selectServ :: [String] -> S.StateT Status IO ()
|
|
|
|
selectServ xs = menu "Select Service" $
|
|
|
|
("cancel", mainMenu) :
|
|
|
|
map (\x -> (x, viewEdit x)) xs
|
|
|
|
|
|
|
|
viewEdit :: String -> S.StateT Status IO ()
|
|
|
|
viewEdit x = menu x
|
|
|
|
[ ( "show password", showPass x >> viewEdit x )
|
|
|
|
, ( "cancel", mainMenu )
|
|
|
|
]
|
|
|
|
|
2018-12-09 00:35:27 -05:00
|
|
|
changeMasterPass :: S.StateT Status IO ()
|
|
|
|
changeMasterPass = do
|
|
|
|
oldP <- S.gets $ view masterPass
|
2018-12-18 15:00:18 -05:00
|
|
|
newP <- req $ reqDefault getMasterPass oldP
|
2018-12-09 00:35:27 -05:00
|
|
|
S.modify $ set masterPass newP
|
|
|
|
mainMenu
|
|
|
|
|
2018-12-09 14:31:58 -05:00
|
|
|
lockSession :: S.StateT Status IO ()
|
|
|
|
lockSession = do
|
|
|
|
lift $ putStrLn "\nsession locked"
|
|
|
|
pass <- S.gets $ view masterPass
|
2018-12-18 15:00:18 -05:00
|
|
|
mx <- lift $ runRequest $ prompt "password: " reqPassword
|
2018-12-09 14:31:58 -05:00
|
|
|
case mx of
|
|
|
|
Nothing -> lockSession
|
|
|
|
Just x -> if x == pass
|
|
|
|
then mainMenu
|
|
|
|
else lockSession
|
|
|
|
|
2018-12-09 00:35:27 -05:00
|
|
|
quit :: S.StateT Status IO ()
|
|
|
|
quit = return ()
|
|
|
|
|
2018-12-23 16:24:08 -05:00
|
|
|
showPass :: String -> S.StateT Status IO ()
|
|
|
|
showPass x = do
|
2018-12-24 12:56:03 -05:00
|
|
|
lift $ putStrLn ""
|
2018-12-23 16:24:08 -05:00
|
|
|
db <- S.gets $ view database
|
|
|
|
case pwGetService x db of
|
|
|
|
Nothing -> lift $ putStrLn "service not found"
|
|
|
|
Just d -> do
|
|
|
|
pw <- S.gets $ view masterPass
|
|
|
|
lift $ putStrLn $ case pwGenerate pw d of
|
|
|
|
Nothing -> "invalid password data"
|
|
|
|
Just pw -> "password for " ++ x ++ ": " ++ pw
|
|
|
|
|
|
|
|
buildData :: S.StateT Status IO PWData
|
|
|
|
buildData = do
|
|
|
|
d <- S.StateT $ return . newPWData
|
|
|
|
req $ reqIf (confirm "would you like to change the default policy?")
|
|
|
|
(do
|
|
|
|
let p = d^.pwPolicy
|
|
|
|
p <- editPolicy p <|> do
|
|
|
|
reqIO $ putStrLn "invalid password policy - using default"
|
|
|
|
return p
|
|
|
|
return $ set pwPolicy p d)
|
|
|
|
(return d)
|
|
|
|
|
2018-12-24 02:32:22 -05:00
|
|
|
-- TODO: refactor this monstrosity
|
2018-12-23 16:24:08 -05:00
|
|
|
editPolicy :: PWPolicy -> Request PWPolicy
|
|
|
|
editPolicy p = if validatePWPolicy p
|
|
|
|
then do
|
|
|
|
p <- edit "length" (p^.pwLength) pwLength p
|
|
|
|
p <- edit "min upper case" (p^.pwUpper) pwUpper p
|
|
|
|
p <- edit "min lower case" (p^.pwLower) pwLower p
|
|
|
|
p <- edit "min digits" (p^.pwDigits) pwDigits p
|
|
|
|
p <- special p
|
|
|
|
if validatePWPolicy p
|
|
|
|
then return p
|
|
|
|
else reqFail
|
|
|
|
else reqFail
|
|
|
|
where
|
|
|
|
edit l v t p = reqIf
|
|
|
|
(confirm $ l ++ " is " ++ show v ++ "\nchange?")
|
|
|
|
(do
|
2018-12-24 13:10:54 -05:00
|
|
|
v <- required $ prompt ("new " ++ l ++ ": ") reqInt
|
2018-12-23 16:24:08 -05:00
|
|
|
return $ set t v p)
|
|
|
|
(return p)
|
|
|
|
special p = do
|
|
|
|
reqIO $ putStrLn $ "special chars are currently " ++
|
|
|
|
(case p^.pwSpecial of
|
|
|
|
Nothing -> "not "
|
|
|
|
Just _ -> "") ++ "allowed"
|
|
|
|
reqIf (confirm "allow special chars?")
|
|
|
|
(case p^.pwSpecial of
|
|
|
|
Nothing -> do
|
|
|
|
x <- required $ prompt "min special chars: " reqInt
|
|
|
|
return $ set pwSpecial (Just x) p
|
|
|
|
Just x -> edit "min special chars" x (pwSpecial.traverse) p)
|
|
|
|
(return $ set pwSpecial Nothing p)
|
|
|
|
|
2018-12-09 11:23:50 -05:00
|
|
|
menu
|
|
|
|
:: String
|
|
|
|
-> [(String, S.StateT Status IO a)]
|
|
|
|
-> S.StateT Status IO a
|
2018-12-18 15:00:18 -05:00
|
|
|
menu title = reqState . prompt ("\n*** " ++ title ++ " ***") .
|
|
|
|
reqMenu . map menuItem
|
2018-12-09 00:35:27 -05:00
|
|
|
|
2018-12-18 15:00:18 -05:00
|
|
|
menuItem :: (String, a) -> (String, Request a)
|
2018-12-09 00:35:27 -05:00
|
|
|
menuItem (str, x) = (str, return x)
|
|
|
|
|
2018-12-18 15:00:18 -05:00
|
|
|
reqState :: Request (S.StateT s IO a) -> S.StateT s IO a
|
2018-12-09 00:35:27 -05:00
|
|
|
reqState = join . req
|
|
|
|
|
2018-12-18 15:00:18 -05:00
|
|
|
req :: Request a -> S.StateT s IO a
|
|
|
|
req = lift . fmap fromJust . runRequest . required
|
2018-12-09 00:35:27 -05:00
|
|
|
|
2018-12-23 16:24:08 -05:00
|
|
|
tryReq :: Request a -> S.StateT s IO (Maybe a)
|
|
|
|
tryReq = lift . runRequest
|
|
|
|
|
|
|
|
confirm :: String -> Request Bool
|
|
|
|
confirm x = required $ prompt (x ++ " (y/n): ") $ reqAgree Nothing reqResp
|
|
|
|
|
2018-12-06 22:03:38 -05:00
|
|
|
--jl
|