module X11Auth where import Control.Applicative import Data.Word(Word16) import System.Directory(getHomeDirectory) import Network.BSD(getHostName) import qualified Data.ByteString as B import IOUtil(lookupEnv) import X11Utils data Xauth = Xauth { family::Word16, address,number,name,auth_data::String } deriving Show familyIPv4 = 0::Word16 familyIPv6 = 6::Word16 familyLocal = 256::Word16 familyWild = 65535::Word16 familyNetname = 254::Word16 familyKrb5Principal = 253::Word16 familyLocalHost = 252::Word16 readAuth = runGet getAuth <$> (B.readFile =<< getAuthFilePath) getAuthFilePath = -- getEnv "XAUTHORITY" <|> (++"/.Xauthority") <$> getHomeDirectory maybe ((++"/.Xauthority") <$> getHomeDirectory) return =<< lookupEnv "XAUTHORITY" -- See the .Xauthority file format description on -- https://gitlab.freedesktop.org/xorg/lib/libxau getAuth = do eof <- atEnd if eof then return [] else (:) <$> getEntry <*> getAuth where getEntry = Xauth <$> w16be <*> str16 <*> str16 <*> str16 <*> str16 str16 = string8 . fi =<< w16be findAuth hostname nr = do let local = hostname `elem` ["","localhost"] host <- if local then getHostName else return hostname let fam = if local then familyLocal else familyNetname -- ?? auth <- readAuth return [a | a<-auth,family a==fam,number a==nr,address a==host]