summaryrefslogtreecommitdiff
path: root/KnownHosts.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-08-04 19:30:31 -0400
committerjoe <joe@jerkface.net>2014-08-04 19:30:31 -0400
commit7c4f8244594d14f3564b820f1ada264099840941 (patch)
treed3e9c57a26fe9ff7ddcbd8135775b13fa427fe18 /KnownHosts.hs
parentf75dc8570a347896093a88e482780d6969488cbf (diff)
Started api for editing ssh_known_hosts
Diffstat (limited to 'KnownHosts.hs')
-rw-r--r--KnownHosts.hs52
1 files changed, 52 insertions, 0 deletions
diff --git a/KnownHosts.hs b/KnownHosts.hs
new file mode 100644
index 0000000..a8f4fe3
--- /dev/null
+++ b/KnownHosts.hs
@@ -0,0 +1,52 @@
1{-# LANGUAGE OverloadedStrings #-}
2module KnownHosts where
3
4import qualified Data.ByteString.Lazy.Char8 as L
5import Data.Char
6import Data.List
7import qualified SSHKey as SSH
8
9data KnownHostsEntry = KnownHostsEntry
10 { eLineno :: Int
11 , eSp :: [L.ByteString]
12 , eComment :: L.ByteString
13 , eKeyBlob :: L.ByteString
14 , eKeyHeader :: L.ByteString
15 , eHosts :: [L.ByteString]
16 }
17 deriving (Eq,Show)
18
19serializeLine :: KnownHostsEntry -> L.ByteString
20serializeLine (KnownHostsEntry { eSp = sp
21 , eComment = comment
22 , eKeyHeader = hdr
23 , eKeyBlob = key
24 , eHosts = hosts
25 })
26 = L.concat $ zipWith L.append [host,hdr,key] (take 3 sp) ++ [comment]
27 where
28 host = L.intercalate "," hosts
29
30
31parseLine :: Int -> L.ByteString -> KnownHostsEntry
32parseLine num str = KnownHostsEntry
33 { eLineno = num
34 , eSp = sp
35 , eComment = comment
36 , eKeyHeader = hdr
37 , eKeyBlob = key
38 , eHosts = hosts
39 }
40 where
41 gs = L.groupBy (\a b -> isSpace a==isSpace b) str
42 (sp,ns) = partition (\x -> isSpace (L.head x)) gs
43 hosts = do
44 h <- take 1 ns
45 let hs = L.groupBy (\_ b -> b/=',') h
46 hs' = map L.tail $ drop 1 hs
47 h0 <- take 1 hs
48 h0 : hs'
49 hdr = L.concat $ take 1 (drop 1 ns)
50 key = L.concat $ take 1 (drop 2 ns)
51 comment = L.concat $ zipWith L.append (drop 3 ns) (drop 3 sp ++ repeat "")
52