module System.Posix.User (
getRealUserID,
getRealGroupID,
getEffectiveUserID,
getEffectiveGroupID,
getGroups,
getLoginName,
getEffectiveUserName,
GroupEntry(..),
getGroupEntryForID,
getGroupEntryForName,
getAllGroupEntries,
UserEntry(..),
getUserEntryForID,
getUserEntryForName,
getAllUserEntries,
setUserID,
setGroupID,
) where
import System.Posix.Types
import Foreign
import Foreign.C
import System.Posix.Internals ( CGroup, CPasswd )
import Control.Concurrent.MVar ( newMVar, withMVar )
import Control.Exception
import Control.Monad
import System.IO.Error
getRealUserID :: IO UserID
getRealUserID = c_getuid
foreign import ccall unsafe "getuid"
c_getuid :: IO CUid
getRealGroupID :: IO GroupID
getRealGroupID = c_getgid
foreign import ccall unsafe "getgid"
c_getgid :: IO CGid
getEffectiveUserID :: IO UserID
getEffectiveUserID = c_geteuid
foreign import ccall unsafe "geteuid"
c_geteuid :: IO CUid
getEffectiveGroupID :: IO GroupID
getEffectiveGroupID = c_getegid
foreign import ccall unsafe "getegid"
c_getegid :: IO CGid
getGroups :: IO [GroupID]
getGroups = do
ngroups <- c_getgroups 0 nullPtr
allocaArray (fromIntegral ngroups) $ \arr -> do
throwErrnoIfMinus1_ "getGroups" (c_getgroups ngroups arr)
groups <- peekArray (fromIntegral ngroups) arr
return groups
foreign import ccall unsafe "getgroups"
c_getgroups :: CInt -> Ptr CGid -> IO CInt
getLoginName :: IO String
getLoginName = do
str <- throwErrnoIfNull "getLoginName" c_getlogin
peekCString str
foreign import ccall unsafe "getlogin"
c_getlogin :: IO CString
setUserID :: UserID -> IO ()
setUserID uid = throwErrnoIfMinus1_ "setUserID" (c_setuid uid)
foreign import ccall unsafe "setuid"
c_setuid :: CUid -> IO CInt
setGroupID :: GroupID -> IO ()
setGroupID gid = throwErrnoIfMinus1_ "setGroupID" (c_setgid gid)
foreign import ccall unsafe "setgid"
c_setgid :: CGid -> IO CInt
getEffectiveUserName :: IO String
getEffectiveUserName = do
euid <- getEffectiveUserID
pw <- getUserEntryForID euid
return (userName pw)
data GroupEntry =
GroupEntry {
groupName :: String,
groupPassword :: String,
groupID :: GroupID,
groupMembers :: [String]
} deriving (Show, Read, Eq)
getGroupEntryForID :: GroupID -> IO GroupEntry
getGroupEntryForID gid = do
allocaBytes (32) $ \pgr ->
allocaBytes grBufSize $ \pbuf ->
alloca $ \ ppgr -> do
throwErrorIfNonZero_ "getGroupEntryForID" $
c_getgrgid_r gid pgr pbuf (fromIntegral grBufSize) ppgr
throwErrnoIfNull "getGroupEntryForID" $
peekElemOff ppgr 0
unpackGroupEntry pgr
foreign import ccall unsafe "getgrgid_r"
c_getgrgid_r :: CGid -> Ptr CGroup -> CString
-> CSize -> Ptr (Ptr CGroup) -> IO CInt
getGroupEntryForName :: String -> IO GroupEntry
getGroupEntryForName name = do
allocaBytes (32) $ \pgr ->
allocaBytes grBufSize $ \pbuf ->
alloca $ \ ppgr ->
withCString name $ \ pstr -> do
throwErrorIfNonZero_ "getGroupEntryForName" $
c_getgrnam_r pstr pgr pbuf (fromIntegral grBufSize) ppgr
r <- peekElemOff ppgr 0
when (r == nullPtr) $
ioError $ flip ioeSetErrorString "no group name"
$ mkIOError doesNotExistErrorType
"getGroupEntryForName"
Nothing
(Just name)
unpackGroupEntry pgr
foreign import ccall unsafe "getgrnam_r"
c_getgrnam_r :: CString -> Ptr CGroup -> CString
-> CSize -> Ptr (Ptr CGroup) -> IO CInt
getAllGroupEntries :: IO [GroupEntry]
getAllGroupEntries =
withMVar lock $ \_ -> worker []
where worker accum =
do resetErrno
ppw <- throwErrnoIfNullAndError "getAllGroupEntries" $
c_getgrent
if ppw == nullPtr
then return (reverse accum)
else do thisentry <- unpackGroupEntry ppw
worker (thisentry : accum)
foreign import ccall unsafe "getgrent"
c_getgrent :: IO (Ptr CGroup)
grBufSize :: Int
grBufSize = sysconfWithDefault 2048 (69)
unpackGroupEntry :: Ptr CGroup -> IO GroupEntry
unpackGroupEntry ptr = do
name <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) ptr >>= peekCString
passwd <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) ptr >>= peekCString
gid <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) ptr
mem <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) ptr
members <- peekArray0 nullPtr mem >>= mapM peekCString
return (GroupEntry name passwd gid members)
data UserEntry =
UserEntry {
userName :: String,
userPassword :: String,
userID :: UserID,
userGroupID :: GroupID,
userGecos :: String,
homeDirectory :: String,
userShell :: String
} deriving (Show, Read, Eq)
lock = unsafePerformIO $ newMVar ()
getUserEntryForID :: UserID -> IO UserEntry
getUserEntryForID uid = do
allocaBytes (48) $ \ppw ->
allocaBytes pwBufSize $ \pbuf ->
alloca $ \ pppw -> do
throwErrorIfNonZero_ "getUserEntryForID" $
c_getpwuid_r uid ppw pbuf (fromIntegral pwBufSize) pppw
throwErrnoIfNull "getUserEntryForID" $
peekElemOff pppw 0
unpackUserEntry ppw
foreign import ccall unsafe "getpwuid_r"
c_getpwuid_r :: CUid -> Ptr CPasswd ->
CString -> CSize -> Ptr (Ptr CPasswd) -> IO CInt
getUserEntryForName :: String -> IO UserEntry
getUserEntryForName name = do
allocaBytes (48) $ \ppw ->
allocaBytes pwBufSize $ \pbuf ->
alloca $ \ pppw ->
withCString name $ \ pstr -> do
throwErrorIfNonZero_ "getUserEntryForName" $
c_getpwnam_r pstr ppw pbuf (fromIntegral pwBufSize) pppw
r <- peekElemOff pppw 0
when (r == nullPtr) $
ioError $ flip ioeSetErrorString "no user name"
$ mkIOError doesNotExistErrorType
"getUserEntryForName"
Nothing
(Just name)
unpackUserEntry ppw
foreign import ccall unsafe "getpwnam_r"
c_getpwnam_r :: CString -> Ptr CPasswd
-> CString -> CSize -> Ptr (Ptr CPasswd) -> IO CInt
getAllUserEntries :: IO [UserEntry]
getAllUserEntries =
withMVar lock $ \_ -> bracket_ c_setpwent c_endpwent $ worker []
where worker accum =
do resetErrno
ppw <- throwErrnoIfNullAndError "getAllUserEntries" $
c_getpwent
if ppw == nullPtr
then return (reverse accum)
else do thisentry <- unpackUserEntry ppw
worker (thisentry : accum)
foreign import ccall unsafe "getpwent"
c_getpwent :: IO (Ptr CPasswd)
foreign import ccall unsafe "setpwent"
c_setpwent :: IO ()
foreign import ccall unsafe "endpwent"
c_endpwent :: IO ()
pwBufSize :: Int
pwBufSize = sysconfWithDefault 1024 (70)
foreign import ccall unsafe "sysconf"
c_sysconf :: CInt -> IO CLong
sysconfWithDefault :: Int -> CInt -> Int
sysconfWithDefault def sc =
unsafePerformIO $ do v <- fmap fromIntegral $ c_sysconf sc
return $ if v == (1) then def else v
unpackUserEntry :: Ptr CPasswd -> IO UserEntry
unpackUserEntry ptr = do
name <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) ptr >>= peekCString
passwd <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) ptr >>= peekCString
uid <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) ptr
gid <- ((\hsc_ptr -> peekByteOff hsc_ptr 20)) ptr
gecos <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) ptr >>= peekCString
dir <- ((\hsc_ptr -> peekByteOff hsc_ptr 32)) ptr >>= peekCString
shell <- ((\hsc_ptr -> peekByteOff hsc_ptr 40)) ptr >>= peekCString
return (UserEntry name passwd uid gid gecos dir shell)
throwErrorIfNonZero_ :: String -> IO CInt -> IO ()
throwErrorIfNonZero_ loc act = do
rc <- act
if (rc == 0)
then return ()
else ioError (errnoToIOError loc (Errno (fromIntegral rc)) Nothing Nothing)
throwErrnoIfNullAndError :: String -> IO (Ptr a) -> IO (Ptr a)
throwErrnoIfNullAndError loc act = do
rc <- act
errno <- getErrno
if rc == nullPtr && errno /= eOK
then throwErrno loc
else return rc