ADD sudo to cifs

This commit is contained in:
Nathan Dwarshuis 2021-03-25 23:16:55 -04:00
parent 165410af84
commit c034952771
1 changed files with 38 additions and 9 deletions

View File

@ -313,6 +313,7 @@ data DataConfig = VeracryptConfig
{ sshfsRemote :: String { sshfsRemote :: String
} | CIFSConfig } | CIFSConfig
{ cifsRemote :: String { cifsRemote :: String
, cifsSudo :: Bool
, cifsPassword :: Maybe PasswordConfig , cifsPassword :: Maybe PasswordConfig
} deriving Show } deriving Show
@ -334,6 +335,7 @@ instance FromJSON TreeConfig where
devData <- case (devType :: String) of devData <- case (devType :: String) of
"cifs" -> CIFSConfig "cifs" -> CIFSConfig
<$> o .: "remote" <$> o .: "remote"
<*> o .:? "sudo" .!= False
<*> o .:? "password" <*> o .:? "password"
"sshfs" -> SSHFSConfig "sshfs" -> SSHFSConfig
<$> o .: "remote" <$> o .: "remote"
@ -427,13 +429,14 @@ instance Mountable DeviceConfig where
$ io $ io
$ case devData of $ case devData of
SSHFSConfig{ sshfsRemote = r } -> mountSSHFS m' r SSHFSConfig{ sshfsRemote = r } -> mountSSHFS m' r
CIFSConfig{ cifsPassword = p } -> mountCIFS m' p CIFSConfig{ cifsPassword = p, cifsSudo = s } -> mountCIFS s m' p
VeracryptConfig{ veracryptPassword = p, veracryptVolume = v } -> VeracryptConfig{ veracryptPassword = p, veracryptVolume = v } ->
mountVeracrypt m' p v mountVeracrypt m' p v
mount DeviceConfig{ deviceMount = m, deviceData = d } True = do mount DeviceConfig{ deviceMount = m, deviceData = d } True = do
m' <- getAbsMountpoint m m' <- getAbsMountpoint m
runAndRemoveDir m' $ io $ case d of runAndRemoveDir m' $ io $ case d of
CIFSConfig{ cifsSudo = s } -> runMountSudoMaybe s "umount" [m']
VeracryptConfig{} -> runVeraCrypt ["-d", m'] "" VeracryptConfig{} -> runVeraCrypt ["-d", m'] ""
_ -> runMount "umount" [m'] "" _ -> runMount "umount" [m'] ""
@ -454,11 +457,11 @@ instance Mountable DeviceConfig where
mountSSHFS :: FilePath -> String -> IO MountResult mountSSHFS :: FilePath -> String -> IO MountResult
mountSSHFS mountpoint remote = runMount "sshfs" [remote, mountpoint] "" mountSSHFS mountpoint remote = runMount "sshfs" [remote, mountpoint] ""
mountCIFS :: FilePath -> Maybe PasswordConfig -> IO MountResult mountCIFS :: Bool -> FilePath -> Maybe PasswordConfig -> IO MountResult
mountCIFS mountpoint pwdConfig = withPasswordGetter pwdConfig runPwd run mountCIFS useSudo mountpoint pwdConfig = withPasswordGetter pwdConfig runPwd run
where where
run = runMount "mount" [mountpoint] "" run = runMountSudoMaybe useSudo "mount" [mountpoint]
runPwd p = runMount' "mount" [mountpoint] "" [("PASSWD", p)] runPwd p = runMountSudoMaybe' useSudo "mount" [mountpoint] [("PASSWD", p)]
mountVeracrypt :: FilePath -> Maybe PasswordConfig -> String -> IO MountResult mountVeracrypt :: FilePath -> Maybe PasswordConfig -> String -> IO MountResult
mountVeracrypt mountpoint pwdConfig volume = mountVeracrypt mountpoint pwdConfig volume =
@ -593,7 +596,7 @@ instance Mountable MTPFS where
withTmpMountDir m $ io $ runMount "jmtpfs" [dev, m] "" withTmpMountDir m $ io $ runMount "jmtpfs" [dev, m] ""
mount MTPFS { mtpfsMountpoint = m } True = mount MTPFS { mtpfsMountpoint = m } True =
runAndRemoveDir m $ io $ runMount "nmount" [m] "" runAndRemoveDir m $ io $ runMount "umount" [m] ""
-- | return True always since the list won't even show without jmtpfs -- | return True always since the list won't even show without jmtpfs
allInstalled _ = return True allInstalled _ = return True
@ -670,6 +673,26 @@ runMount' :: String -> [String] -> String -> [(String, String)] -> IO MountResul
runMount' cmd args stdin environ = eitherToMountResult runMount' cmd args stdin environ = eitherToMountResult
<$> readCmdEither' cmd args stdin environ <$> readCmdEither' cmd args stdin environ
runMountSudoMaybe :: Bool -> String -> [String] -> IO MountResult
runMountSudoMaybe useSudo cmd args =
runMountSudoMaybe' useSudo cmd args []
runMountSudoMaybe' :: Bool -> String -> [String] -> [(String, String)] -> IO MountResult
runMountSudoMaybe' useSudo cmd args environ = maybe
(runMount' cmd args "" environ)
(\r -> runSudoMount' r cmd args environ)
=<< if useSudo then readPassword' "Sudo Password" else return Nothing
-- TODO untested
-- runSudoMount :: String -> String -> [String] -> String -> IO MountResult
-- runSudoMount rootpass cmd args stdin = runSudoMount' rootpass cmd args stdin []
runSudoMount' :: String -> String -> [String] -> [(String, String)] -> IO MountResult
runSudoMount' rootpass cmd args environ = runMount "sudo" args' rootpass
where
args' = ["-S"] ++ environ' ++ [cmd] ++ args
environ' = fmap (\(k, v) -> k ++ "=" ++ v) environ
eitherToMountResult :: Either (Int, String, String) String -> MountResult eitherToMountResult :: Either (Int, String, String) String -> MountResult
eitherToMountResult (Right _) = MountSuccess eitherToMountResult (Right _) = MountSuccess
eitherToMountResult (Left (_, _, e)) = MountError e eitherToMountResult (Left (_, _, e)) = MountError e
@ -701,12 +724,18 @@ rmDirOnMountError d f = do
unless (res == MountSuccess) $ rmDirMaybe d unless (res == MountSuccess) $ rmDirMaybe d
return res return res
-- | Run a mount command and create the mountpoint if it does not exist, and
-- remove the mountpoint if a mount error occurs
withTmpMountDir :: FilePath -> RofiMountIO MountResult -> RofiMountIO MountResult withTmpMountDir :: FilePath -> RofiMountIO MountResult -> RofiMountIO MountResult
withTmpMountDir m = rmDirOnMountError m withTmpMountDir m = rmDirOnMountError m
. bracketOnError_ (mkDirMaybe m) (rmDirMaybe m) . bracketOnError_ (mkDirMaybe m) (rmDirMaybe m)
-- | Run an unmount command and remove the mountpoint if no errors occur
runAndRemoveDir :: FilePath -> RofiMountIO MountResult -> RofiMountIO MountResult runAndRemoveDir :: FilePath -> RofiMountIO MountResult -> RofiMountIO MountResult
runAndRemoveDir m f = rmDirOnMountError m $ finally f (rmDirMaybe m) runAndRemoveDir m f = do
res <- catch f (return . MountError . (displayException :: SomeException -> String))
when (res == MountSuccess) $ rmDirMaybe m
return res
mkDirMaybe :: FilePath -> RofiMountIO () mkDirMaybe :: FilePath -> RofiMountIO ()
mkDirMaybe fp = whenInMountDir fp $ io $ createDirectoryIfMissing True fp mkDirMaybe fp = whenInMountDir fp $ io $ createDirectoryIfMissing True fp