also avoid crashing in most circumstances if unable to determine the username

Mostly the username is only used for the git committer or other display
purposes, and we can just fall back to a dummy value in these cases.

The only remaining place where an error is thrown is when starting local
pairing, which needs the username to be known.
This commit is contained in:
Joey Hess 2016-06-08 15:04:15 -04:00
parent a3ff99ea3b
commit 8e4cbefbc6
Failed to extract signature
6 changed files with 20 additions and 22 deletions

View file

@ -33,7 +33,7 @@ checkEnvironment = do
checkEnvironmentIO :: IO () checkEnvironmentIO :: IO ()
checkEnvironmentIO = whenM (isNothing <$> myUserGecos) $ do checkEnvironmentIO = whenM (isNothing <$> myUserGecos) $ do
username <- myUserName username <- either (const "unknown") id <$> myUserName
ensureEnv "GIT_AUTHOR_NAME" username ensureEnv "GIT_AUTHOR_NAME" username
ensureEnv "GIT_COMMITTER_NAME" username ensureEnv "GIT_COMMITTER_NAME" username
where where
@ -52,7 +52,7 @@ ensureCommit :: Annex a -> Annex a
ensureCommit a = either retry return =<< tryNonAsync a ensureCommit a = either retry return =<< tryNonAsync a
where where
retry _ = do retry _ = do
name <- liftIO myUserName name <- liftIO $ either (const "unknown") id <$> myUserName
setConfig (ConfigKey "user.name") name setConfig (ConfigKey "user.name") name
setConfig (ConfigKey "user.email") name setConfig (ConfigKey "user.email") name
a a

View file

@ -52,13 +52,11 @@ genDescription (Just d) = return d
genDescription Nothing = do genDescription Nothing = do
reldir <- liftIO . relHome =<< liftIO . absPath =<< fromRepo Git.repoPath reldir <- liftIO . relHome =<< liftIO . absPath =<< fromRepo Git.repoPath
hostname <- fromMaybe "" <$> liftIO getHostname hostname <- fromMaybe "" <$> liftIO getHostname
#ifndef mingw32_HOST_OS
let at = if null hostname then "" else "@" let at = if null hostname then "" else "@"
username <- liftIO myUserName v <- liftIO myUserName
return $ concat [username, at, hostname, ":", reldir] return $ concat $ case v of
#else Right username -> [username, at, hostname, ":", reldir]
return $ concat [hostname, ":", reldir] Left _ -> [hostname, ":", reldir]
#endif
initialize :: Maybe String -> Maybe Version -> Annex () initialize :: Maybe String -> Maybe Version -> Annex ()
initialize mdescription mversion = do initialize mdescription mversion = do

View file

@ -17,7 +17,7 @@ import qualified Data.Map as M
newUserId :: GpgCmd -> IO UserId newUserId :: GpgCmd -> IO UserId
newUserId cmd = do newUserId cmd = do
oldkeys <- secretKeys cmd oldkeys <- secretKeys cmd
username <- myUserName username <- either (const "unknown") id <$> myUserName
let basekeyname = username ++ "'s git-annex encryption key" let basekeyname = username ++ "'s git-annex encryption key"
return $ Prelude.head $ filter (\n -> M.null $ M.filter (== n) oldkeys) return $ Prelude.head $ filter (\n -> M.null $ M.filter (== n) oldkeys)
( basekeyname ( basekeyname

View file

@ -226,7 +226,7 @@ startLocalPairing stage oncancel alert muuid displaysecret secret = do
let pubkey = either error id $ validateSshPubKey $ sshPubKey keypair let pubkey = either error id $ validateSshPubKey $ sshPubKey keypair
pairdata <- liftIO $ PairData pairdata <- liftIO $ PairData
<$> getHostname <$> getHostname
<*> myUserName <*> (either error id <$> myUserName)
<*> pure reldir <*> pure reldir
<*> pure pubkey <*> pure pubkey
<*> (maybe genUUID return muuid) <*> (maybe genUUID return muuid)
@ -291,8 +291,8 @@ promptSecret msg cont = pairPage $ do
let (username, hostname) = maybe ("", "") let (username, hostname) = maybe ("", "")
(\(_, v, a) -> (T.pack $ remoteUserName v, T.pack $ fromMaybe (showAddr a) (remoteHostName v))) (\(_, v, a) -> (T.pack $ remoteUserName v, T.pack $ fromMaybe (showAddr a) (remoteHostName v)))
(verifiableVal . fromPairMsg <$> msg) (verifiableVal . fromPairMsg <$> msg)
u <- T.pack <$> liftIO myUserName u <- liftIO myUserName
let sameusername = username == u let sameusername = Right username == (T.pack <$> u)
$(widgetFile "configurators/pairing/local/prompt") $(widgetFile "configurators/pairing/local/prompt")
{- This counts unicode characters as more than one character, {- This counts unicode characters as more than one character,

View file

@ -156,10 +156,10 @@ getAddSshR :: Handler Html
getAddSshR = postAddSshR getAddSshR = postAddSshR
postAddSshR :: Handler Html postAddSshR :: Handler Html
postAddSshR = sshConfigurator $ do postAddSshR = sshConfigurator $ do
username <- liftIO $ T.pack <$> myUserName username <- liftIO $ either (const Nothing) (Just . T.pack) <$> myUserName
((result, form), enctype) <- liftH $ ((result, form), enctype) <- liftH $
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ sshInputAForm textField $ runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ sshInputAForm textField $
SshInput Nothing (Just username) Password Nothing Nothing 22 SshInput Nothing username Password Nothing Nothing 22
case result of case result of
FormSuccess sshinput -> do FormSuccess sshinput -> do
s <- liftAssistant $ testServer sshinput s <- liftAssistant $ testServer sshinput

View file

@ -15,7 +15,7 @@ module Utility.UserInfo (
) where ) where
import Utility.Env import Utility.Env
import Utility.Exception import Utility.Data
import System.PosixCompat import System.PosixCompat
import Control.Applicative import Control.Applicative
@ -25,7 +25,7 @@ import Prelude
- -
- getpwent will fail on LDAP or NIS, so use HOME if set. -} - getpwent will fail on LDAP or NIS, so use HOME if set. -}
myHomeDir :: IO FilePath myHomeDir :: IO FilePath
myHomeDir = myVal env homeDirectory myHomeDir = either error return =<< myVal env homeDirectory
where where
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
env = ["HOME"] env = ["HOME"]
@ -34,7 +34,7 @@ myHomeDir = myVal env homeDirectory
#endif #endif
{- Current user's user name. -} {- Current user's user name. -}
myUserName :: IO String myUserName :: IO (Either String String)
myUserName = myVal env userName myUserName = myVal env userName
where where
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
@ -48,15 +48,15 @@ myUserGecos :: IO (Maybe String)
#if defined(__ANDROID__) || defined(mingw32_HOST_OS) #if defined(__ANDROID__) || defined(mingw32_HOST_OS)
myUserGecos = return Nothing myUserGecos = return Nothing
#else #else
myUserGecos = catchMaybeIO $ myVal [] userGecos myUserGecos = eitherToMaybe <$> myVal [] userGecos
#endif #endif
myVal :: [String] -> (UserEntry -> String) -> IO String myVal :: [String] -> (UserEntry -> String) -> IO (Either String String)
myVal envvars extract = go envvars myVal envvars extract = go envvars
where where
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
go [] = extract <$> (getUserEntryForID =<< getEffectiveUserID) go [] = Right . extract <$> (getUserEntryForID =<< getEffectiveUserID)
#else #else
go [] = extract <$> error ("environment not set: " ++ show envvars) go [] = return $ Left ("environment not set: " ++ show envvars)
#endif #endif
go (v:vs) = maybe (go vs) return =<< getEnv v go (v:vs) = maybe (go vs) (return . Right) =<< getEnv v