2018-12-26 14:38:42 -05:00
|
|
|
{-
|
|
|
|
|
|
|
|
passman
|
2019-01-01 04:47:42 -05:00
|
|
|
Copyright (C) 2018, 2019 Jonathan Lamothe
|
2018-12-26 14:38:42 -05:00
|
|
|
<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.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
module UI (getMasterPass, mainMenu) where
|
|
|
|
|
|
|
|
import Control.Lens (over, set, view, (^.))
|
|
|
|
import Control.Monad (when)
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
|
|
|
import qualified Control.Monad.Trans.State as S
|
|
|
|
import System.Console.HCL
|
|
|
|
( Request
|
|
|
|
, prompt
|
|
|
|
, reqDefault
|
|
|
|
, reqFail
|
|
|
|
, reqIf
|
|
|
|
, reqInt
|
|
|
|
, reqIO
|
|
|
|
, reqPassword
|
|
|
|
, reqResp
|
|
|
|
, required
|
|
|
|
)
|
|
|
|
|
|
|
|
import Password
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import Util
|
|
|
|
|
|
|
|
getMasterPass :: Request String
|
|
|
|
getMasterPass = do
|
|
|
|
p1 <- required $ prompt "master password: " reqPassword
|
|
|
|
p2 <- required $ prompt "confirm master password: " reqPassword
|
|
|
|
if p1 /= p2
|
|
|
|
then do
|
2018-12-28 12:59:48 -05:00
|
|
|
reqIO $ putStrLn "The passwords you entered do not match."
|
2018-12-26 14:38:42 -05:00
|
|
|
reqFail
|
|
|
|
else return p1
|
|
|
|
|
|
|
|
mainMenu :: S.StateT Status IO ()
|
|
|
|
mainMenu =
|
|
|
|
menu "Main Menu"
|
|
|
|
[ ( "add a password", addPassword )
|
|
|
|
, ( "view/edit a password", viewEditMenu )
|
|
|
|
, ( "change master password", changeMasterPass )
|
2019-01-01 21:02:52 -05:00
|
|
|
, ( "save manually", save >> mainMenu )
|
2018-12-26 14:38:42 -05:00
|
|
|
, ( "lock session", lockSession )
|
|
|
|
, ( "quit", quit )
|
|
|
|
]
|
|
|
|
|
|
|
|
addPassword :: S.StateT Status IO ()
|
|
|
|
addPassword = do
|
|
|
|
svc <- req $ prompt "service name: " reqResp
|
2018-12-31 15:27:36 -05:00
|
|
|
ifServExists svc
|
|
|
|
(do
|
2018-12-28 12:59:48 -05:00
|
|
|
edit <- req (confirm $
|
|
|
|
"The service already exists in the database.\n" ++
|
|
|
|
"Would you like to edit it?")
|
|
|
|
if edit
|
2018-12-31 14:33:23 -05:00
|
|
|
then servMenu svc
|
2018-12-31 15:27:36 -05:00
|
|
|
else mainMenu)
|
|
|
|
(do
|
2018-12-28 12:59:48 -05:00
|
|
|
d <- buildData
|
|
|
|
setService svc d
|
|
|
|
showPass svc
|
2018-12-31 15:27:36 -05:00
|
|
|
servMenu svc)
|
2018-12-26 14:38:42 -05:00
|
|
|
|
|
|
|
viewEditMenu :: S.StateT Status IO ()
|
|
|
|
viewEditMenu = menu "View/Edit Password"
|
|
|
|
[ ( "search services", searchServ )
|
|
|
|
, ( "list services", listServ )
|
|
|
|
, ( "cancel", mainMenu )
|
|
|
|
]
|
|
|
|
|
2018-12-27 14:26:44 -05:00
|
|
|
changeMasterPass :: S.StateT Status IO ()
|
|
|
|
changeMasterPass = do
|
2019-01-01 21:15:09 -05:00
|
|
|
req (confirm $
|
|
|
|
"\nWARNING: Changing your master password will change all of your saved passwords.\n" ++
|
|
|
|
"Are you sure you would like to proceed?") >>= flip when
|
|
|
|
(do
|
|
|
|
oldP <- S.gets $ view masterPass
|
|
|
|
newP <- req $ reqDefault getMasterPass oldP
|
|
|
|
S.modify $ set masterPass newP)
|
2018-12-27 14:26:44 -05:00
|
|
|
mainMenu
|
|
|
|
|
|
|
|
lockSession :: S.StateT Status IO ()
|
|
|
|
lockSession = do
|
2018-12-28 12:59:48 -05:00
|
|
|
lift $ putStrLn "\nThe session is locked."
|
2018-12-27 14:26:44 -05:00
|
|
|
pass <- S.gets $ view masterPass
|
2018-12-28 12:59:48 -05:00
|
|
|
x <- req $ prompt "master password: " reqPassword
|
|
|
|
if x == pass
|
|
|
|
then mainMenu
|
|
|
|
else lockSession
|
2018-12-27 14:26:44 -05:00
|
|
|
|
|
|
|
quit :: S.StateT Status IO ()
|
2018-12-29 20:15:59 -05:00
|
|
|
quit = save
|
2018-12-27 14:26:44 -05:00
|
|
|
|
|
|
|
buildData :: S.StateT Status IO PWData
|
|
|
|
buildData = do
|
|
|
|
d <- run newPWData
|
2018-12-28 12:59:48 -05:00
|
|
|
req $ reqIf (confirm "Would you like to change the password policy?")
|
2018-12-27 14:26:44 -05:00
|
|
|
(do
|
|
|
|
let p = d^.pwPolicy
|
2018-12-28 12:59:48 -05:00
|
|
|
p' <- reqDefault (editPolicy p) p
|
|
|
|
return $ set pwPolicy p' d)
|
2018-12-27 14:26:44 -05:00
|
|
|
(return d)
|
|
|
|
|
2018-12-26 14:38:42 -05:00
|
|
|
searchServ :: S.StateT Status IO ()
|
|
|
|
searchServ = do
|
|
|
|
svc <- req $ prompt "service name: " reqResp
|
|
|
|
db <- S.gets $ view database
|
|
|
|
case pwSearch svc db of
|
|
|
|
[] -> do
|
2018-12-28 12:59:48 -05:00
|
|
|
lift $ putStrLn "\nThe service could not be found in the database."
|
2018-12-26 14:38:42 -05:00
|
|
|
mainMenu
|
2018-12-26 15:03:01 -05:00
|
|
|
[x] -> servMenu x
|
2018-12-26 14:38:42 -05:00
|
|
|
xs -> selectServ xs
|
|
|
|
|
|
|
|
listServ :: S.StateT Status IO ()
|
|
|
|
listServ = S.gets (view database) >>= selectServ . pwSearch ""
|
|
|
|
|
|
|
|
selectServ :: [String] -> S.StateT Status IO ()
|
|
|
|
selectServ xs = menu "Select Service" $
|
2018-12-26 15:03:01 -05:00
|
|
|
map (\x -> (x, servMenu x)) xs ++
|
2018-12-26 14:38:42 -05:00
|
|
|
[("(cancel)", mainMenu)]
|
|
|
|
|
2018-12-26 15:03:01 -05:00
|
|
|
servMenu :: String -> S.StateT Status IO ()
|
|
|
|
servMenu x = menu x
|
2018-12-31 14:29:44 -05:00
|
|
|
[ ( "show password", showPass x >> servMenu x )
|
|
|
|
, ( "edit password", editPassMenu x )
|
|
|
|
, ( "remove service", removeServ x )
|
2018-12-31 15:27:36 -05:00
|
|
|
, ( "rename service", renameServ x )
|
2018-12-31 14:29:44 -05:00
|
|
|
, ( "back", mainMenu )
|
2018-12-26 15:03:01 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
editPassMenu :: String -> S.StateT Status IO ()
|
|
|
|
editPassMenu x = menu (x ++ " : Edit Password")
|
2018-12-27 15:56:25 -05:00
|
|
|
[ ( "generate new password", changeSalt x )
|
|
|
|
, ( "edit password policy", doEditPolicy x )
|
2018-12-28 12:59:48 -05:00
|
|
|
, ( "back", servMenu x )
|
2018-12-26 14:38:42 -05:00
|
|
|
]
|
|
|
|
|
2018-12-31 14:29:44 -05:00
|
|
|
removeServ :: String -> S.StateT Status IO ()
|
|
|
|
removeServ x = do
|
|
|
|
go <- req $ confirm $
|
|
|
|
"Are you sure you want to delete the password for " ++ x ++ "?"
|
|
|
|
if go
|
|
|
|
then do
|
2018-12-31 15:27:36 -05:00
|
|
|
removeServ' x
|
2018-12-31 14:29:44 -05:00
|
|
|
mainMenu
|
|
|
|
else servMenu x
|
|
|
|
|
2018-12-31 15:27:36 -05:00
|
|
|
removeServ' :: String -> S.StateT Status IO ()
|
|
|
|
removeServ' = S.modify . over database . pwRemoveService
|
|
|
|
|
|
|
|
renameServ :: String -> S.StateT Status IO ()
|
|
|
|
renameServ x = do
|
|
|
|
y <- req $ prompt "new service name: " reqResp
|
|
|
|
if x == y
|
|
|
|
then servMenu x
|
|
|
|
else ifServExists y
|
|
|
|
(do
|
|
|
|
overwrite <- req $ confirm $
|
|
|
|
y ++ " already exists.\n" ++
|
|
|
|
"Would you like to overwrite it?"
|
|
|
|
if overwrite
|
|
|
|
then renameServ' x y
|
|
|
|
else servMenu x)
|
|
|
|
(renameServ' x y)
|
|
|
|
|
|
|
|
renameServ' :: String -> String -> S.StateT Status IO ()
|
|
|
|
renameServ' x y = withService x mainMenu $ \d -> do
|
|
|
|
removeServ' x
|
|
|
|
setService y d
|
|
|
|
servMenu y
|
|
|
|
|
2018-12-26 15:43:13 -05:00
|
|
|
changeSalt :: String -> S.StateT Status IO ()
|
2018-12-27 15:56:25 -05:00
|
|
|
changeSalt x = withService x mainMenu $ \d -> do
|
|
|
|
salt <- run newPWSalt
|
2018-12-28 12:59:48 -05:00
|
|
|
setService x $ set pwSalt salt d
|
2018-12-27 15:56:25 -05:00
|
|
|
showPass x
|
2018-12-28 12:59:48 -05:00
|
|
|
editPassMenu x
|
2018-12-27 15:56:25 -05:00
|
|
|
|
|
|
|
doEditPolicy :: String -> S.StateT Status IO ()
|
|
|
|
doEditPolicy x = withService x mainMenu $ \d -> do
|
|
|
|
let p = d^.pwPolicy
|
2018-12-28 12:59:48 -05:00
|
|
|
p' <- req $ reqDefault (editPolicy p) p
|
|
|
|
setService x $ set pwPolicy p' d
|
|
|
|
showPass x
|
2018-12-27 15:56:25 -05:00
|
|
|
editPassMenu x
|
2018-12-26 15:43:13 -05:00
|
|
|
|
2018-12-26 14:38:42 -05:00
|
|
|
showPass :: String -> S.StateT Status IO ()
|
|
|
|
showPass x = do
|
|
|
|
lift $ putStrLn ""
|
2018-12-27 15:56:25 -05:00
|
|
|
withService x
|
2018-12-28 12:59:48 -05:00
|
|
|
(lift $ putStrLn "The service could not be found in the database.") $
|
2018-12-27 15:56:25 -05:00
|
|
|
\d -> do
|
2020-02-28 01:23:44 -05:00
|
|
|
mp <- S.gets $ view masterPass
|
|
|
|
lift $ putStrLn $ case pwGenerate mp d of
|
2018-12-28 12:59:48 -05:00
|
|
|
Nothing -> "The password data were not valid."
|
2018-12-26 14:38:42 -05:00
|
|
|
Just pw -> "password for " ++ x ++ ": " ++ pw
|
|
|
|
|
|
|
|
-- TODO: refactor this monstrosity
|
|
|
|
editPolicy :: PWPolicy -> Request PWPolicy
|
2020-02-28 01:23:44 -05:00
|
|
|
editPolicy policy = do
|
|
|
|
p <-
|
|
|
|
edit "length" (policy^.pwLength) pwLength policy >>=
|
|
|
|
edit "min upper case" (policy^.pwUpper) pwUpper >>=
|
|
|
|
edit "min lower case" (policy^.pwLower) pwLower >>=
|
|
|
|
edit "min digits" (policy^.pwDigits) pwDigits >>=
|
|
|
|
special
|
2018-12-28 12:59:48 -05:00
|
|
|
if validatePWPolicy p
|
|
|
|
then return p
|
|
|
|
else do
|
|
|
|
reqIO $ putStrLn $
|
|
|
|
"\nThe password policy you entered is invalid\n." ++
|
|
|
|
"It will not be changed."
|
|
|
|
reqFail
|
2018-12-26 14:38:42 -05:00
|
|
|
where
|
|
|
|
edit l v t p = do
|
2020-02-28 01:23:44 -05:00
|
|
|
v' <- reqDefault
|
2018-12-26 14:38:42 -05:00
|
|
|
(prompt ("new " ++ l ++ " (default " ++ show v ++ "): ") reqInt) v
|
2020-02-28 01:23:44 -05:00
|
|
|
return $ set t v' p
|
2018-12-26 14:38:42 -05:00
|
|
|
special p = do
|
2018-12-28 12:59:48 -05:00
|
|
|
reqIO $ putStrLn $ "Special characters are currently " ++
|
2018-12-26 14:38:42 -05:00
|
|
|
(case p^.pwSpecial of
|
|
|
|
Nothing -> "not "
|
2018-12-28 12:59:48 -05:00
|
|
|
Just _ -> "") ++ "allowed."
|
|
|
|
reqIf (confirm "Would you like to allow special characters?")
|
2018-12-26 14:38:42 -05:00
|
|
|
(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)
|
|
|
|
|
|
|
|
--jl
|