fix(vsftpd): set owner of the directory to the specified user and group

to ftp
This commit is contained in:
Mahdi Dibaiee 2017-04-23 15:31:39 +04:30
parent 587e54d6b6
commit cee3ff4e72

View File

@ -2,6 +2,7 @@
module Main (call, main) where module Main (call, main) where
import System.Serverman.Types import System.Serverman.Types
import System.Serverman.Utils import System.Serverman.Utils
import System.Serverman.Log
import Types import Types
import System.Directory hiding (writable) import System.Directory hiding (writable)
@ -15,6 +16,7 @@ module Main (call, main) where
import Data.List import Data.List
import Data.Either import Data.Either
import Control.Monad.State hiding (liftIO) import Control.Monad.State hiding (liftIO)
import System.Posix (setOwnerAndGroup, getFileStatus, fileOwner)
help :: App String help :: App String
help = return $ help = return $
@ -29,17 +31,17 @@ module Main (call, main) where
, ("--recreate-user", "if the specified username exists, delete and create it again, otherwise leave it intact")] , ("--recreate-user", "if the specified username exists, delete and create it again, otherwise leave it intact")]
call :: Service -> App () call :: Service -> App ()
call s@(Service { name, version, service })= do call s@Service { name, version, service }= do
(AppState { os, arguments }) <- get AppState { os, arguments } <- get
let params@(FileSharingParams { directory, port, user, pass, anonymous, anonymousWrite, writable, recreateUser }) = toFSParams arguments let params@FileSharingParams { directory, port, user, pass, anonymous, anonymousWrite, writable, recreateUser } = toFSParams arguments
let content = show params let content = show params
config = "/etc/" config = "/etc/"
original = config </> "vsftpd.conf" original = config </> "vsftpd.conf"
userList = config </> "vsftpd-serverman-user-list" userList = config </> "vsftpd-serverman-user-list"
when recreateUser $ executeRoot "userdel" [user] "" True >> return () when recreateUser $ void $ executeRoot "userdel" [user] "" True
(Right opensslResponse) <- execute "openssl" ["passwd", "-1", pass] "" True (Right opensslResponse) <- execute "openssl" ["passwd", "-1", pass] "" True
let encryptedPassword = head . lines $ opensslResponse let encryptedPassword = head . lines $ opensslResponse
@ -47,18 +49,26 @@ module Main (call, main) where
executeRoot "groupadd" ["-f", "ftp"] "" False executeRoot "groupadd" ["-f", "ftp"] "" False
executeRoot "useradd" [user, "-d", directory, "-G", "ftp", "-p", encryptedPassword] "" False executeRoot "useradd" [user, "-d", directory, "-G", "ftp", "-p", encryptedPassword] "" False
ftpId <- getGroupId (Just "ftp")
userId <- getUserId (Just user)
liftIO $ do liftIO $ do
execIfExists original $ do execIfExists original $
renameFileIfMissing original (original ++ ".backup") renameFileIfMissing original (original ++ ".backup")
writeFile original content writeFile original content
writeFile userList user writeFile userList user
createDirectoryIfMissing True directory
setOwnerAndGroup directory userId ftpId
writeFile (directory </> "serverman-sample") "Hello from Serverman!"
result <- restartService "vsftpd" result <- restartService "vsftpd"
case result of case result of
Left err -> return () Left err -> return ()
Right _ -> Right _ ->
liftIO $ putStrLn $ "restarted vsftpd" info "restarted vsftpd"
main :: IO () main :: IO ()
main = return () main = return ()