rofi-extras/app/pinentry-rofi.hs

107 lines
3.2 KiB
Haskell
Raw Permalink Normal View History

{-# LANGUAGE OverloadedStrings #-}
2021-06-25 23:42:29 -04:00
--------------------------------------------------------------------------------
-- | rofi-pinentry - a simply pinentry proxy for bitwarden
--
-- Rather than prompt the user like all the other pinentry programs, call the
-- bitwarden deamon and prompt for a password there
module Main where
import Bitwarden.Internal
2021-06-25 23:57:37 -04:00
import Data.List
import Data.Yaml
2021-06-25 23:57:37 -04:00
import System.Directory
import System.Environment
2021-06-25 23:42:29 -04:00
import System.Exit
import System.FilePath.Posix
2021-06-26 16:09:49 -04:00
import System.IO
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
putStrLn "OK Pleased to meet you"
pinentryLoop =<< readPinConf
newtype PinConf = PinConf { pcBwName :: String } deriving (Eq, Show)
instance FromJSON PinConf where
parseJSON (Object o) = PinConf <$> o .:? "bitwarden-name" .!= "gnupg"
parseJSON _ = fail "pinentry yaml parse error"
readPinConf :: IO PinConf
readPinConf = do
c <- decodeFileEither =<< pinConfDir
case c of
Left e -> print 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
processLine p . words =<< getLine
pinentryLoop p
2021-06-25 23:42:29 -04:00
processLine :: PinConf -> [String] -> IO ()
processLine _ [] = noop
processLine _ ["BYE"] = exitSuccess
processLine p ["GETPIN"] = getPin p
2021-06-25 23:42:29 -04:00
2021-06-26 16:09:49 -04:00
processLine _ ["GETINFO", o] = processGetInfo o
2021-06-25 23:42:29 -04:00
-- TODO this might be important
2021-06-26 16:09:49 -04:00
processLine _ ["OPTION", o] = processOption o
2021-06-25 23:42:29 -04:00
2021-06-26 16:09:49 -04:00
-- these should all do nothing
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
processLine _ ["CONFIRM"] = noop
processLine _ ["CONFIRM", "--one-button", _] = noop
2021-06-25 23:42:29 -04:00
processLine _ ss = unknownCommand $ unwords ss
2021-06-25 23:42:29 -04:00
unknownCommand :: String -> IO ()
unknownCommand c = putStrLn $ "ERR 275 Unknown command " ++ c
getPin :: PinConf -> IO ()
getPin p = do
2021-06-25 23:57:37 -04:00
its <- getItems
let w = (password . login) =<< find (\i -> pcBwName p == name i) its
maybe err send w
2021-06-25 23:57:37 -04:00
where
err = 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
processGetInfo :: String -> IO ()
processGetInfo "pid" = send . show =<< getProcessID
processGetInfo "version" = noop
processGetInfo "flavor" = noop
processGetInfo "ttyinfo" = noop
processGetInfo _ = putStrLn "ERR 83886360 IPC parameter error <rofi>"
2021-06-25 23:42:29 -04:00
processOption :: String -> IO ()
2021-06-26 16:09:49 -04:00
processOption _ = noop
send :: String -> IO ()
send s = putStrLn ("D " ++ s) >> ok
2021-06-25 23:42:29 -04:00
noop :: IO ()
noop = ok
ok :: IO ()
ok = putStrLn "OK"