{-# LANGUAGE CPP #-}-- | A test's connection to a daemon the suite runs, over the daemon's-- Unix socket, or on Windows its named pipe ("Nix.Daemon.Pipe"); where-- such a daemon listens; whom its socket or pipe is open to; and the-- environment a command the suite runs starts from, which names no daemon-- of the machine's.module DaemonLink ( DaemonLink (..), daemonAddress, isolatedStoreEnvironment, connectDaemon, listenerOpenToUsers, )whereimport qualified Data.ByteString as BSimport qualified Data.ByteString.Char8 as BS8import System.Environment (getEnvironment)import System.FilePath ((</>))import Test.Harness (TestResult, assertEqual)#if defined(mingw32_HOST_OS)import qualified Data.ByteString.Builder as BBimport Data.IORef (atomicModifyIORef', newIORef)import qualified Data.Text as Timport DirDacl (Dacl (..), DaclEntry (..), directoryDacl)import GHC.Clock (getMonotonicTimeNSec)import Nix.Daemon.Connection (Transport (..))import qualified Nix.Daemon.Pipe as Pipeimport Nix.Store.Access (sidText)import qualified System.Process as Proc#elseimport Control.Exception (onException)import Network.Socket (Family (AF_UNIX), SockAddr (SockAddrUnix), SocketType (Stream), close, connect, defaultProtocol, socket)import Network.Socket.ByteString (recv, sendAll)import NodeStatus (NodeStatus (..), nodeStatus)import Nix.Environment (argumentBytes)#endif-- | A connection to a daemon, as a test drives it byte by byte.data DaemonLink = DaemonLink { -- | Send bytes, all of them. linkSend :: BS.ByteString -> IO (), -- | Up to as many bytes as asked, once some have arrived, or none once -- the daemon has closed the connection. linkReceive :: Int -> IO BS.ByteString, -- | Close the connection. linkClose :: IO () }-- | Where a test's daemon listens, given the directory it keeps its-- store in, or 'Nothing' where that has no spelling as bytes: a socket in-- the directory, or on Windows a pipe no other daemon of any run has.daemonAddress :: FilePath -> IO (Maybe BS.ByteString)-- | Connect to the daemon listening at an address. A failure throws.connectDaemon :: BS.ByteString -> IO DaemonLink-- | Whether the daemon's socket is open to every user, as upstream's is,-- or on Windows its pipe has the DACL "Nix.Daemon.Pipe" gives it: full-- control to SYSTEM and Administrators, the daemon's own account the-- access to create an instance only where it does not own the pipe as-- Administrators, and Users the right to read and write but not to create-- an instance.listenerOpenToUsers :: BS.ByteString -> IO TestResult#if defined(mingw32_HOST_OS)daemonAddress _ = do pid <- Proc.getCurrentPid now <- getMonotonicTimeNSec pure (Just (BS8.pack ("\\\\.\\pipe\\nova-nix-test-" <> show pid <> "-" <> show now)))connectDaemon address = case Pipe.pipePath address of Left reason -> ioError (userError (T.unpack reason)) Right path -> Pipe.connectPipe path >>= \connected -> case connected of Left reason -> ioError (userError (T.unpack reason)) Right conn -> do let transport = Pipe.connectionTransport conn held <- newIORef BS.empty let receive count = atomicModifyIORef' held (\kept -> (BS.empty, kept)) >>= \kept -> if BS.null kept then maybe (pure BS.empty) (keep count) =<< transportReceive transport else keep count kept keep count bytes = do let (now, later) = BS.splitAt count bytes atomicModifyIORef' held (\kept -> (later <> kept, ())) pure now pure DaemonLink { linkSend = transportSend transport . BB.byteString, linkReceive = receive, linkClose = Pipe.closeConnection conn }-- Reading a pipe's DACL opens it, as a client does.listenerOpenToUsers address = do dacl <- directoryDacl (BS8.unpack address) owner <- Pipe.processPipeOwner let allowed mask sid = DaclEntry 0 0 mask (Just sid) fullControl = 0x1F01FF accountEntries = case owner of Pipe.OwnedByAdministrators -> [] Pipe.OwnedByAccount account -> [allowed Pipe.daemonPipeAccess (sidText account)] pure $ assertEqual "DACL" ( Just ( Dacl True ( [allowed fullControl "S-1-5-18", allowed fullControl "S-1-5-32-544"] <> accountEntries <> [allowed Pipe.clientPipeAccess "S-1-5-32-545"] ) ) ) dacl#elsedaemonAddress root = argumentBytes (root </> "socket")connectDaemon address = do sock <- socket AF_UNIX Stream defaultProtocol connect sock (SockAddrUnix (BS8.unpack address)) `onException` close sock pure DaemonLink {linkSend = sendAll sock, linkReceive = recv sock, linkClose = close sock}listenerOpenToUsers address = assertEqual "mode" (Just 0o666) . fmap nsPermissions <$> nodeStatus (BS8.unpack address)#endif-- | The process's environment for a command a test runs, in which nothing-- on the machine that runs the suite decides which store the command-- uses: there is no @NIX_REMOTE@; the system configuration directory-- (@NIX_CONF_DIR@) is one of the test's own, under its root, which holds-- nothing, so the machine's nix.conf and the @store@ setting it may hold-- are not read; and the daemon's address (@NIX_DAEMON_SOCKET_PATH@) is one-- under that root, or on Windows a pipe name of the run's own, at which-- nothing listens, so a store command given no @--store@ never reaches a-- daemon of the machine's. The directory and the socket are named and-- not made. A test that sets any of these itself filters it out of what-- this returns.isolatedStoreEnvironment :: FilePath -> IO [(String, String)]isolatedStoreEnvironment root = do ambient <- getEnvironment address <- daemonAddress root pure ( [("NIX_DAEMON_SOCKET_PATH", BS8.unpack bytes) | Just bytes <- [address]] ++ [("NIX_CONF_DIR", root </> "empty-system-config")] ++ filter ((`notElem` ["NIX_REMOTE", "NIX_DAEMON_SOCKET_PATH", "NIX_CONF_DIR"]) . fst) ambient )