{- Copyright 2016 Joey Hess - - Licensed under the GNU AGPL version 3 or higher. -} module CmdLine where import Types import Tunables import qualified Gpg import Options.Applicative import qualified Data.ByteString.UTF8 as BU8 import System.Directory import Storage import Storage.Local import Storage.Network data CmdLine = CmdLine { mode :: Maybe Mode , secretkeysource :: Maybe SecretKeySource , storage :: Storage , gui :: Bool , testMode :: Bool , customShardParams :: Maybe ShardParams } data Mode = Backup | Restore | Benchmark deriving (Show) parse :: Parser CmdLine parse = CmdLine <$> optional (backup <|> restore <|> benchmark) <*> optional (gpgswitch <|> fileswitch) <*> localstorageflag <*> guiswitch <*> testmodeswitch <*> optional (ShardParams <$> totalobjects <*> neededobjects) where backup = flag' Backup ( long "backup" <> help "Store a secret key in keysafe." ) restore = flag' Restore ( long "restore" <> help "Retrieve a secret key from keysafe." ) benchmark = flag' Benchmark ( long "benchmark" <> help "Benchmark speed of keysafe's cryptographic primitives." ) gpgswitch = GpgKey . KeyId . BU8.fromString <$> strOption ( long "gpgkeyid" <> metavar "KEYID" <> help "Specify keyid of gpg key to back up or restore. (When this option is used to back up a key, it must also be used at restore time.)" ) fileswitch = KeyFile <$> strOption ( long "keyfile" <> metavar "FILE" <> help "Specify secret key file to back up or restore. (The same filename must be used to restore a key as was used to back it up.)" ) localstorageflag = flag networkStorage localStorage ( long "store-local" <> help "Store data locally, in ~/.keysafe/objects. (The default is to store data in the cloud.)" ) testmodeswitch = switch ( long "testmode" <> help "Avoid using expensive cryptographic operation to secure key. Use for testing only, not with real secret keys." ) guiswitch = switch ( long "gui" <> help "Use GUI interface for interaction. Default is to use readline interface when run in a terminal, and GUI otherwise." ) totalobjects = option auto ( long "totalshards" <> metavar "M" <> help ("Configure the number of shards to split encrypted secret key into. Default: " ++ show (totalObjects (shardParams defaultTunables)) ++ " (When this option is used to back up a key, it must also be provided at restore time.)") ) neededobjects = option auto ( long "neededshards" <> metavar "N" <> help ("Configure the number of shards needed to restore. Default: " ++ show (neededObjects (shardParams defaultTunables)) ++ " (When this option is used to back up a key, it must also be provided at restore time.)") ) get :: IO CmdLine get = execParser opts where opts = info (helper <*> parse) ( fullDesc <> header "keysafe - securely back up secret keys" ) -- | When a mode is not specified on the command line, -- default to backing up if a secret key exists, and otherwise restoring. selectMode :: CmdLine -> IO Mode selectMode cmdline = case mode cmdline of Just m -> return m Nothing -> case secretkeysource cmdline of Just (KeyFile f) -> present <$> doesFileExist f _ -> present . not . null <$> Gpg.listSecretKeys where present True = Backup present False = Restore customizeShardParams :: CmdLine -> Tunables -> Tunables customizeShardParams cmdline t = case customShardParams cmdline of Nothing -> t Just ps -> t { shardParams = ps }