diff options
-rw-r--r-- | Data/BitSyntax.hs | 2 | ||||
-rw-r--r-- | Presence/FGConsole.hs | 1 | ||||
-rw-r--r-- | Presence/LocalPeerCred.hs | 95 | ||||
-rw-r--r-- | Presence/Server.hs | 15 | ||||
-rw-r--r-- | Presence/UTmp.hs | 2 | ||||
-rw-r--r-- | Presence/XMPPServer.hs | 67 | ||||
-rw-r--r-- | Presence/main.hs | 15 |
7 files changed, 157 insertions, 40 deletions
diff --git a/Data/BitSyntax.hs b/Data/BitSyntax.hs index aebdd522..d8110c2a 100644 --- a/Data/BitSyntax.hs +++ b/Data/BitSyntax.hs | |||
@@ -66,7 +66,7 @@ import Language.Haskell.TH.Syntax | |||
66 | import qualified Data.ByteString as BS | 66 | import qualified Data.ByteString as BS |
67 | import Data.Char (ord) | 67 | import Data.Char (ord) |
68 | import Control.Monad | 68 | import Control.Monad |
69 | import Test.QuickCheck (Arbitrary(), arbitrary, Gen()) | 69 | -- import Test.QuickCheck (Arbitrary(), arbitrary, Gen()) |
70 | 70 | ||
71 | import Foreign | 71 | import Foreign |
72 | 72 | ||
diff --git a/Presence/FGConsole.hs b/Presence/FGConsole.hs index fc1ece65..93b9a590 100644 --- a/Presence/FGConsole.hs +++ b/Presence/FGConsole.hs | |||
@@ -14,7 +14,6 @@ import Control.Monad | |||
14 | import Foreign.C.Error | 14 | import Foreign.C.Error |
15 | import Foreign.C | 15 | import Foreign.C |
16 | 16 | ||
17 | import Todo | ||
18 | import Debug.Trace | 17 | import Debug.Trace |
19 | import System.Posix.Signals | 18 | import System.Posix.Signals |
20 | 19 | ||
diff --git a/Presence/LocalPeerCred.hs b/Presence/LocalPeerCred.hs new file mode 100644 index 00000000..c58fe288 --- /dev/null +++ b/Presence/LocalPeerCred.hs | |||
@@ -0,0 +1,95 @@ | |||
1 | module LocalPeerCred where | ||
2 | |||
3 | import Data.ByteString.Lazy.Char8 as L hiding (map,putStrLn,tail,splitAt,tails,filter) | ||
4 | import qualified Data.ByteString.Lazy.Char8 as L (splitAt) | ||
5 | import qualified Data.ByteString.Lazy as W8 | ||
6 | import Data.List (tails) | ||
7 | import System.IO ( withFile, IOMode(..)) | ||
8 | import Data.Maybe | ||
9 | import Data.Binary | ||
10 | import Data.Bits | ||
11 | import Network.Socket | ||
12 | import System.Posix.Types | ||
13 | import Debug.Trace | ||
14 | -- import System.Environment (getArgs) | ||
15 | |||
16 | xs ?? n | n < 0 = Nothing | ||
17 | [] ?? _ = Nothing | ||
18 | (x:_) ?? 0 = Just x | ||
19 | (_:xs) ?? n = xs ?? (n-1) | ||
20 | |||
21 | parseHex bs = L.concat . parseHex' $ bs | ||
22 | where | ||
23 | parseHex' bs = | ||
24 | let (dnib,ts) = L.splitAt 2 bs | ||
25 | parseNibble x = W8.pack $ group2 toW8 (W8.unpack $ W8.map hexDigit x) | ||
26 | hexDigit d = d - (if d>0x39 then 0x37 else 0x30) | ||
27 | group2 f (x:y:ys) = f x y : group2 f ys | ||
28 | group2 _ _ = [] | ||
29 | toW8 a b = shift a 4 .|. b | ||
30 | in parseNibble dnib : | ||
31 | if L.null ts | ||
32 | then [] | ||
33 | else parseHex' ts | ||
34 | |||
35 | getLocalPeerCred' (SockAddrInet portn host) = do | ||
36 | let port = fromEnum portn | ||
37 | trace ("tcp4 "++show(port,host)) $ withFile "/proc/net/tcp" ReadMode (parseProcNet port host) | ||
38 | |||
39 | getLocalPeerCred' (SockAddrInet6 portn flow host scope) = do | ||
40 | let port = fromEnum portn | ||
41 | trace "tcp6" $ withFile "/proc/net/tcp6" ReadMode (parseProcNet port host) | ||
42 | |||
43 | getLocalPeerCred' addr@(SockAddrUnix _) = | ||
44 | -- TODO: parse /proc/net/unix | ||
45 | -- see also: Network.Socket.getPeerCred | ||
46 | return Nothing | ||
47 | |||
48 | getLocalPeerCred sock = do | ||
49 | addr <- getPeerName sock | ||
50 | muid <- getLocalPeerCred' addr | ||
51 | case muid of | ||
52 | Just uid -> return uid | ||
53 | Nothing -> return undefined -- trace "proc failed." $ fmap (CUid . fromIntegral . sndOf3) (getPeerCred sock) | ||
54 | where sndOf3 (pid,uid,gid) = uid | ||
55 | |||
56 | from16 :: Word16 -> Int | ||
57 | from16 = fromEnum | ||
58 | |||
59 | as16 :: Word16 -> Word16 | ||
60 | as16 = id | ||
61 | |||
62 | parseProcNet port host h = do | ||
63 | tcp <- hGetContents h | ||
64 | let u = do | ||
65 | ls <- listToMaybe . tail . tails . L.lines $ tcp | ||
66 | let ws = map L.words ls | ||
67 | let rs = ( catMaybes . flip map ws $ \xs -> do | ||
68 | let ys = snd (Prelude.splitAt 1 xs) | ||
69 | localaddr <- listToMaybe ys | ||
70 | let zs = L.splitWith (==':') localaddr | ||
71 | addr <- fmap parseHex $ listToMaybe zs | ||
72 | port <- fmap (fromIntegral . as16 . decode . parseHex) $ listToMaybe $ snd (Prelude.splitAt 1 zs) | ||
73 | let ys' = snd (Prelude.splitAt 5 (tail ys)) | ||
74 | uid <- listToMaybe ys' | ||
75 | let peer = (port,decode addr) | ||
76 | user = toEnum (read (unpack uid) ::Int) ::UserID -- CUid . fromIntegral $ (read (unpack uid)::Int) | ||
77 | return $ trace ("peer:"++show(peer,user)) (peer,user) | ||
78 | ) | ||
79 | fmap snd . listToMaybe $ filter ((===(port,host)).fst) rs | ||
80 | trace ("found:"++show u) $ u `seq` return u | ||
81 | where | ||
82 | a === b = let r= a==b in trace ("Comparing "++show (a,b)++"-->"++show r) r | ||
83 | |||
84 | {- | ||
85 | main = do | ||
86 | args <- getArgs | ||
87 | let addr = fromJust $ do | ||
88 | port <- args ?? 0 | ||
89 | host <- args ?? 1 | ||
90 | return $ SockAddrInet (toEnum . fromIntegral . readInt $ port) (toEnum (read host::Int)) | ||
91 | readInt x = read x :: Int | ||
92 | |||
93 | r <- getLocalPeerCred' addr | ||
94 | putStrLn $ "r = " ++ show r | ||
95 | -} | ||
diff --git a/Presence/Server.hs b/Presence/Server.hs index feefea2b..adc3de84 100644 --- a/Presence/Server.hs +++ b/Presence/Server.hs | |||
@@ -4,18 +4,14 @@ | |||
4 | module Server where | 4 | module Server where |
5 | 5 | ||
6 | import Network.Socket | 6 | import Network.Socket |
7 | import qualified Data.ByteString as S (ByteString) | ||
8 | import Data.ByteString.Lazy.Char8 as L | 7 | import Data.ByteString.Lazy.Char8 as L |
9 | ( ByteString | 8 | ( fromChunks |
10 | , hPutStrLn | ||
11 | , fromChunks | ||
12 | , putStrLn ) | 9 | , putStrLn ) |
13 | import Data.ByteString.Char8 | 10 | import Data.ByteString.Char8 |
14 | ( hGetNonBlocking | 11 | ( hGetNonBlocking |
15 | ) | 12 | ) |
16 | import System.IO | 13 | import System.IO |
17 | ( Handle | 14 | ( IOMode(..) |
18 | , IOMode(..) | ||
19 | , hSetBuffering | 15 | , hSetBuffering |
20 | , BufferMode(..) | 16 | , BufferMode(..) |
21 | , hWaitForInput | 17 | , hWaitForInput |
@@ -23,8 +19,6 @@ import System.IO | |||
23 | , hIsEOF | 19 | , hIsEOF |
24 | ) | 20 | ) |
25 | import Control.Monad | 21 | import Control.Monad |
26 | import Control.Monad.Fix (fix) | ||
27 | import Todo | ||
28 | import Control.Concurrent (forkIO) | 22 | import Control.Concurrent (forkIO) |
29 | import Control.Exception (handle,SomeException(..)) | 23 | import Control.Exception (handle,SomeException(..)) |
30 | import Data.HList | 24 | import Data.HList |
@@ -54,8 +48,11 @@ doServer port g startCon = runServer2 port (runConn2 g) | |||
54 | runConn2 g st (sock,_) = do | 48 | runConn2 g st (sock,_) = do |
55 | h <- socketToHandle sock ReadWriteMode | 49 | h <- socketToHandle sock ReadWriteMode |
56 | hSetBuffering h NoBuffering | 50 | hSetBuffering h NoBuffering |
57 | st'' <- startCon (h .*. st) | 51 | st'' <- startCon sock (h .*. st) |
58 | let doException (SomeException e) = Prelude.putStrLn ("\n\nexception: " ++ show e ++ "\n\n") | 52 | let doException (SomeException e) = Prelude.putStrLn ("\n\nexception: " ++ show e ++ "\n\n") |
53 | handle doException $ do | ||
54 | -- PEER CRED: (0,4294967295,4294967295) | ||
55 | -- PEER NAME: 127.0.0.1:37253 | ||
59 | handle doException $ fix $ \loop -> do | 56 | handle doException $ fix $ \loop -> do |
60 | let continue () = hIsEOF h >>= flip when loop . not | 57 | let continue () = hIsEOF h >>= flip when loop . not |
61 | packet <- getPacket h | 58 | packet <- getPacket h |
diff --git a/Presence/UTmp.hs b/Presence/UTmp.hs index c94dcef2..c2549a88 100644 --- a/Presence/UTmp.hs +++ b/Presence/UTmp.hs | |||
@@ -6,7 +6,6 @@ import qualified Data.ByteString as S | |||
6 | import qualified Data.ByteString.Char8 as C | 6 | import qualified Data.ByteString.Char8 as C |
7 | import Data.BitSyntax | 7 | import Data.BitSyntax |
8 | import Data.Functor.Identity | 8 | import Data.Functor.Identity |
9 | import Foreign.C.String | ||
10 | import Data.Maybe | 9 | import Data.Maybe |
11 | import System.Posix.Signals | 10 | import System.Posix.Signals |
12 | import System.Posix.Types | 11 | import System.Posix.Types |
@@ -17,7 +16,6 @@ import Data.Int | |||
17 | import Control.Monad.Error.Class | 16 | import Control.Monad.Error.Class |
18 | import System.IO.Error | 17 | import System.IO.Error |
19 | 18 | ||
20 | import Todo | ||
21 | 19 | ||
22 | utmp_file = "/var/run/utmp" | 20 | utmp_file = "/var/run/utmp" |
23 | 21 | ||
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 591acad6..05b12b73 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -1,6 +1,7 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | {-# LANGUAGE ScopedTypeVariables #-} | 2 | {-# LANGUAGE ScopedTypeVariables #-} |
3 | {-# LANGUAGE ViewPatterns #-} | 3 | {-# LANGUAGE ViewPatterns #-} |
4 | {-# LANGUAGE TupleSections #-} | ||
4 | module XMPPServer ( listenForXmppClients ) where | 5 | module XMPPServer ( listenForXmppClients ) where |
5 | 6 | ||
6 | import Data.HList.TypeEqGeneric1() | 7 | import Data.HList.TypeEqGeneric1() |
@@ -9,27 +10,25 @@ import ByteStringOperators | |||
9 | 10 | ||
10 | import Server | 11 | import Server |
11 | import Data.ByteString.Lazy.Char8 as L | 12 | import Data.ByteString.Lazy.Char8 as L |
12 | ( ByteString | 13 | ( hPutStrLn |
13 | , hPutStrLn | ||
14 | , unlines | 14 | , unlines |
15 | , pack | 15 | , pack |
16 | , unpack | 16 | , unpack ) |
17 | , init ) | ||
18 | import qualified Data.ByteString.Lazy.Char8 as L | 17 | import qualified Data.ByteString.Lazy.Char8 as L |
19 | ( putStrLn ) | 18 | ( putStrLn ) |
20 | import System.IO | 19 | import System.IO |
21 | ( Handle | 20 | ( Handle |
22 | ) | 21 | ) |
23 | import Control.Concurrent (forkIO) | ||
24 | import Control.Concurrent.Chan | ||
25 | import Data.HList | 22 | import Data.HList |
26 | import AdaptServer | 23 | import AdaptServer |
27 | import Text.XML.HaXml.Lex (xmlLex) | 24 | import Text.XML.HaXml.Lex (xmlLex) |
28 | import Text.XML.HaXml.Parse (xmlParseWith,element,doctypedecl,processinginstruction,elemOpenTag,elemCloseTag) | 25 | import Text.XML.HaXml.Parse (xmlParseWith,element,{-doctypedecl,-}processinginstruction,elemOpenTag,elemCloseTag) |
29 | import Text.XML.HaXml.Types as Hax (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) | 26 | import Text.XML.HaXml.Types as Hax (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) |
30 | import qualified Text.XML.HaXml.Types as Hax (Element) | ||
31 | import Data.Maybe | 27 | import Data.Maybe |
32 | import Debug.Trace | 28 | import Debug.Trace |
29 | import Control.Arrow | ||
30 | import LocalPeerCred | ||
31 | import Network.Socket | ||
33 | 32 | ||
34 | 33 | ||
35 | 34 | ||
@@ -51,17 +50,21 @@ greet host = L.unlines | |||
51 | , "</stream:features>" | 50 | , "</stream:features>" |
52 | ] | 51 | ] |
53 | 52 | ||
54 | startCon st = do | 53 | startCon sock st = do |
55 | let h = hOccursFst st :: Handle | 54 | let h = hOccursFst st :: Handle |
55 | cred <- getLocalPeerCred sock | ||
56 | Prelude.putStrLn $ "PEER CRED: "++show cred | ||
57 | pname <- getPeerName sock | ||
58 | Prelude.putStrLn $ "PEER NAME: "++show pname | ||
56 | return (ConnectionFinalizer (return ()) .*. st) | 59 | return (ConnectionFinalizer (return ()) .*. st) |
57 | 60 | ||
58 | iq_query_unavailable host id mjid xmlns = L.unlines $ | 61 | iq_query_unavailable host id mjid xmlns kind = L.unlines $ |
59 | [ "<iq type='error'" | 62 | [ "<iq type='error'" |
60 | , " from='" <++> host <++> "'" | 63 | , " from='" <++> host <++> "'" |
61 | , case mjid of Just jid -> " to='" <++> jid <++> "'" | 64 | , case mjid of Just jid -> " to='" <++> jid <++> "'" |
62 | Nothing -> "" | 65 | Nothing -> "" |
63 | , " id='" <++> id <++> "'>" | 66 | , " id='" <++> id <++> "'>" |
64 | , " <query xmlns='" <++> xmlns <++> "'/>" | 67 | , " <" <++> kind <++> " xmlns='" <++> xmlns <++> "'/>" |
65 | , " <error type='cancel'>" | 68 | , " <error type='cancel'>" |
66 | , " <service-unavailable" | 69 | , " <service-unavailable" |
67 | , " xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" | 70 | , " xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" |
@@ -71,6 +74,7 @@ iq_query_unavailable host id mjid xmlns = L.unlines $ | |||
71 | 74 | ||
72 | tagattrs tag content = Prelude.concatMap (\(CElem (Elem _ a _) _)->a) | 75 | tagattrs tag content = Prelude.concatMap (\(CElem (Elem _ a _) _)->a) |
73 | $ Prelude.filter (bindElem tag) content | 76 | $ Prelude.filter (bindElem tag) content |
77 | anytagattrs content = Prelude.concatMap (\(CElem (Elem n a _) _)->map (second (n,)) a) content | ||
74 | 78 | ||
75 | bindElem tag (CElem (Elem (N n) _ _) _) | n==tag = True | 79 | bindElem tag (CElem (Elem (N n) _ _) _) | n==tag = True |
76 | bindElem _ _ = False | 80 | bindElem _ _ = False |
@@ -132,13 +136,37 @@ iqresponse host (Elem _ attrs content) = do | |||
132 | Just (iqresult host id Nothing)) | 136 | Just (iqresult host id Nothing)) |
133 | 137 | ||
134 | "get" -> trace ("iq-get "++show (attrs,content)) $ do | 138 | "get" -> trace ("iq-get "++show (attrs,content)) $ do |
135 | xmlns <- fmap pack $ | 139 | (tag,as) <- lookup (N "xmlns") (anytagattrs content) |
136 | lookup (N "xmlns") (tagattrs "query" content) | 140 | xmlns <- fmap pack $ listToMaybe . astring $ as |
137 | >>= listToMaybe . astring | 141 | let servicekind = case tag of { (N s) -> pack s ; _ -> "query" } |
138 | Just (iq_query_unavailable host id Nothing xmlns) | 142 | case xmlns of |
143 | "urn:xmpp:ping" -> do | ||
144 | let to = case fmap pack (lookup (N "from") attrs >>= unattr) of | ||
145 | Just jid -> "to='" <++> jid <++> "' " | ||
146 | Nothing -> "" | ||
147 | Just $ "<iq from='" <++> host <++> "' " <++> to <++> "id='" <++> id <++> "' type='result'/>" | ||
148 | |||
149 | _ -> Just (iq_query_unavailable host id Nothing xmlns servicekind) | ||
139 | _ -> Nothing | 150 | _ -> Nothing |
140 | 151 | ||
141 | 152 | -- <presence> | |
153 | -- <priority>1</priority> | ||
154 | -- <c xmlns='http://jabber.org/protocol/caps' | ||
155 | -- node='http://pidgin.im/' | ||
156 | -- hash='sha-1' ver='lV6i//bt2U8Rm0REcX8h4F3Nk3M=' | ||
157 | -- ext='voice-v1 camera-v1 video-v1'/> | ||
158 | -- <x xmlns='vcard-temp:x:update'/> | ||
159 | -- </presence> | ||
160 | |||
161 | presence_response host (Elem _ attrs content) = do | ||
162 | -- let id = fmap pack (lookup (N "id") attrs >>= unattr) | ||
163 | typ <- fmap pack (lookup (N "type") attrs >>= unattr) | ||
164 | case typ of | ||
165 | "subscribe" -> do | ||
166 | -- <presence to='guest@localhost' type='subscribe'/> | ||
167 | to <- fmap pack (lookup (N "to") attrs >>= unattr) | ||
168 | Just $ "<presence to='" <++> to <++> "' type='subscribed'/>" | ||
169 | _ -> Nothing | ||
142 | 170 | ||
143 | doCon st elem cont = do | 171 | doCon st elem cont = do |
144 | let h = hOccursFst st :: Handle | 172 | let h = hOccursFst st :: Handle |
@@ -152,8 +180,13 @@ doCon st elem cont = do | |||
152 | hsend (greet host) | 180 | hsend (greet host) |
153 | Element e@(Elem (N "iq") _ _) -> | 181 | Element e@(Elem (N "iq") _ _) -> |
154 | case iqresponse host e of | 182 | case iqresponse host e of |
155 | Nothing -> trace "no respones" $ return () | 183 | Nothing -> trace "IGNORE: no response to <iq>" $ return () |
156 | Just r -> hsend r | 184 | Just r -> hsend r |
185 | Element e@(Elem (N "presence") _ _) -> | ||
186 | case presence_response host e of | ||
187 | Nothing -> trace "IGNORE: no response to <presence>" $ return () | ||
188 | Just r -> hsend r | ||
189 | |||
157 | _ -> return () -- putStrLn $ "unhandled: "++show v | 190 | _ -> return () -- putStrLn $ "unhandled: "++show v |
158 | 191 | ||
159 | putStrLn (show elem) | 192 | putStrLn (show elem) |
diff --git a/Presence/main.hs b/Presence/main.hs index b0f73a9e..7df81903 100644 --- a/Presence/main.hs +++ b/Presence/main.hs | |||
@@ -1,12 +1,9 @@ | |||
1 | {-# LANGUAGE CPP #-} | 1 | {-# LANGUAGE CPP #-} |
2 | 2 | ||
3 | import System.Directory | 3 | import System.Directory |
4 | import System.IO | ||
5 | import Control.Monad | 4 | import Control.Monad |
6 | import System.Posix.Signals | 5 | import System.Posix.Signals |
7 | import System.Posix.Types | 6 | import System.Posix.Types |
8 | import Control.Monad.Error.Class | ||
9 | import Control.Exception (throw) | ||
10 | import System.Posix.Process | 7 | import System.Posix.Process |
11 | import Data.Maybe | 8 | import Data.Maybe |
12 | 9 | ||
@@ -18,8 +15,7 @@ import FGConsole | |||
18 | import XMPPServer | 15 | import XMPPServer |
19 | import Data.HList | 16 | import Data.HList |
20 | import Network.Socket (sClose) | 17 | import Network.Socket (sClose) |
21 | import Control.Concurrent (threadDelay) | 18 | import Control.Exception |
22 | import Control.Exception (handle,SomeException(..)) | ||
23 | 19 | ||
24 | jid user host rsrc = user ++ "@" ++ host ++ "/" ++ rsrc | 20 | jid user host rsrc = user ++ "@" ++ host ++ "/" ++ rsrc |
25 | 21 | ||
@@ -81,22 +77,21 @@ sendUSR1 pid = do | |||
81 | 77 | ||
82 | getStartupAction [] = throw (userError "pid file?") >> return (Right "") | 78 | getStartupAction [] = throw (userError "pid file?") >> return (Right "") |
83 | getStartupAction (p:ps) = do | 79 | getStartupAction (p:ps) = do |
84 | catch | 80 | handle onEr $ |
85 | ( do | 81 | ( do |
86 | pid <- fmap CPid (readFile p >>= readIO) | 82 | pid <- fmap CPid (readFile p >>= readIO) |
87 | -- signal pid | 83 | -- signal pid |
88 | return (Left pid) ) | 84 | return (Left pid) ) |
89 | onEr | ||
90 | where | 85 | where |
91 | onEr e = do | 86 | onEr (SomeException _) = do |
92 | pid <- getProcessID | 87 | pid <- getProcessID |
93 | putStrLn $ "starting pid = "++show pid | 88 | putStrLn $ "starting pid = "++show pid |
94 | catch (do | 89 | handle (\(SomeException _) -> getStartupAction ps) |
90 | (do | ||
95 | writeFile p (show pid) | 91 | writeFile p (show pid) |
96 | putStrLn $ "writing "++show p | 92 | putStrLn $ "writing "++show p |
97 | -- start daemon | 93 | -- start daemon |
98 | return (Right p) ) | 94 | return (Right p) ) |
99 | (\_ -> getStartupAction ps) | ||
100 | 95 | ||
101 | runOnce ps run notify = getStartupAction ps >>= doit | 96 | runOnce ps run notify = getStartupAction ps >>= doit |
102 | where | 97 | where |