rofi-extras/app/pinentry-rofi.hs

99 lines
2.9 KiB
Haskell
Raw Normal View History

2021-06-25 23:42:29 -04:00
--------------------------------------------------------------------------------
2023-02-13 22:19:49 -05:00
-- rofi-pinentry - a simply pinentry proxy for bitwarden
2021-06-25 23:42:29 -04:00
--
-- Rather than prompt the user like all the other pinentry programs, call the
-- bitwarden deamon and prompt for a password there
module Main where
2023-02-13 22:19:49 -05:00
import Bitwarden.Internal
import qualified Data.Text.IO as TI
import Data.Yaml
import RIO
import qualified RIO.List as L
import qualified RIO.Text as T
import System.Directory
import System.Environment
import System.FilePath.Posix
import System.Posix.Process
2021-06-25 23:42:29 -04:00
main :: IO ()
main = do
2021-06-26 16:09:49 -04:00
hSetBuffering stdout LineBuffering
2023-02-13 22:19:49 -05:00
TI.putStrLn "OK Pleased to meet you"
pinentryLoop =<< readPinConf
2023-02-13 23:31:50 -05:00
newtype PinConf = PinConf {pcBwName :: T.Text} deriving (Eq, Show)
instance FromJSON PinConf where
parseJSON (Object o) = PinConf <$> o .:? "bitwarden-name" .!= "gnupg"
2023-02-13 22:19:49 -05:00
parseJSON _ = fail "pinentry yaml parse error"
readPinConf :: IO PinConf
readPinConf = do
c <- decodeFileEither =<< pinConfDir
case c of
2023-02-13 22:19:49 -05:00
Left e -> TI.putStrLn (T.pack $ show e) >> exitWith (ExitFailure 1)
Right r -> return r
pinConfDir :: IO FilePath
pinConfDir = maybe defHome (return . (</> confname)) =<< lookupEnv "GNUPGHOME"
where
defHome = (</> ".gnupg" </> confname) <$> getHomeDirectory
confname = "pinentry-rofi.yml"
2021-06-25 23:42:29 -04:00
pinentryLoop :: PinConf -> IO ()
pinentryLoop p = do
2023-02-13 22:19:49 -05:00
processLine p . T.words =<< TI.getLine
pinentryLoop p
2021-06-25 23:42:29 -04:00
2023-02-13 22:19:49 -05:00
processLine :: PinConf -> [T.Text] -> IO ()
processLine _ [] = noop
processLine _ ["BYE"] = exitSuccess
processLine p ["GETPIN"] = getPin p
processLine _ ["GETINFO", o] = processGetInfo o
2021-06-25 23:42:29 -04:00
-- TODO this might be important
2023-02-13 22:19:49 -05:00
processLine _ ["OPTION", o] = processOption o
2021-06-26 16:09:49 -04:00
-- these should all do nothing
2023-02-13 22:19:49 -05:00
processLine _ ("SETDESC" : _) = noop
processLine _ ("SETOK" : _) = noop
processLine _ ("SETNOTOK" : _) = noop
processLine _ ("SETCANCEL" : _) = noop
processLine _ ("SETPROMPT" : _) = noop
processLine _ ("SETERROR" : _) = noop
2021-06-25 23:42:29 -04:00
-- CONFIRM can take a flag
2023-02-13 22:19:49 -05:00
processLine _ ["CONFIRM"] = noop
processLine _ ["CONFIRM", "--one-button", _] = noop
2023-02-13 22:19:49 -05:00
processLine _ ss = unknownCommand $ T.unwords ss
2021-06-25 23:42:29 -04:00
2023-02-13 22:19:49 -05:00
unknownCommand :: T.Text -> IO ()
unknownCommand c = TI.putStrLn $ T.append "ERR 275 Unknown command " c
2021-06-25 23:42:29 -04:00
getPin :: PinConf -> IO ()
getPin p = do
2021-06-25 23:57:37 -04:00
its <- getItems
2023-02-13 23:31:50 -05:00
let w = (password . login) =<< L.find (\i -> pcBwName p == name i) its
maybe err send w
2021-06-25 23:57:37 -04:00
where
2023-02-13 22:19:49 -05:00
err = TI.putStrLn "ERR 83886179 Operation canceled <rofi>"
2021-06-26 16:09:49 -04:00
-- these are the only supported options for GETINFO; anything else is an error
2023-02-13 22:19:49 -05:00
processGetInfo :: T.Text -> IO ()
processGetInfo "pid" = send . T.pack . show =<< getProcessID
2021-06-26 16:09:49 -04:00
processGetInfo "version" = noop
2023-02-13 22:19:49 -05:00
processGetInfo "flavor" = noop
2021-06-26 16:09:49 -04:00
processGetInfo "ttyinfo" = noop
2023-02-13 22:19:49 -05:00
processGetInfo _ = TI.putStrLn "ERR 83886360 IPC parameter error <rofi>"
2021-06-25 23:42:29 -04:00
2023-02-13 22:19:49 -05:00
processOption :: T.Text -> IO ()
2021-06-26 16:09:49 -04:00
processOption _ = noop
2023-02-13 22:19:49 -05:00
send :: T.Text -> IO ()
send s = TI.putStrLn (T.append "D " s) >> ok
2021-06-25 23:42:29 -04:00
noop :: IO ()
noop = ok
ok :: IO ()
2023-02-13 22:19:49 -05:00
ok = TI.putStrLn "OK"