diff options
Diffstat (limited to 'KnownHosts.hs')
-rw-r--r-- | KnownHosts.hs | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/KnownHosts.hs b/KnownHosts.hs index a8f4fe3..e04ad90 100644 --- a/KnownHosts.hs +++ b/KnownHosts.hs | |||
@@ -11,6 +11,7 @@ data KnownHostsEntry = KnownHostsEntry | |||
11 | , eSp :: [L.ByteString] | 11 | , eSp :: [L.ByteString] |
12 | , eComment :: L.ByteString | 12 | , eComment :: L.ByteString |
13 | , eKeyBlob :: L.ByteString | 13 | , eKeyBlob :: L.ByteString |
14 | , eKey :: Maybe SSH.Key | ||
14 | , eKeyHeader :: L.ByteString | 15 | , eKeyHeader :: L.ByteString |
15 | , eHosts :: [L.ByteString] | 16 | , eHosts :: [L.ByteString] |
16 | } | 17 | } |
@@ -20,12 +21,15 @@ serializeLine :: KnownHostsEntry -> L.ByteString | |||
20 | serializeLine (KnownHostsEntry { eSp = sp | 21 | serializeLine (KnownHostsEntry { eSp = sp |
21 | , eComment = comment | 22 | , eComment = comment |
22 | , eKeyHeader = hdr | 23 | , eKeyHeader = hdr |
23 | , eKeyBlob = key | 24 | , eKeyBlob = blob |
25 | , eKey = _ | ||
24 | , eHosts = hosts | 26 | , eHosts = hosts |
25 | }) | 27 | }) |
26 | = L.concat $ zipWith L.append [host,hdr,key] (take 3 sp) ++ [comment] | 28 | = L.concat $ zipWith L.append [host,hdr,blob] (take 3 sp) ++ [comment] |
29 | -- = L.concat $ zipWith L.append [host,hdr,keyblob] (take 3 sp) ++ [comment] | ||
27 | where | 30 | where |
28 | host = L.intercalate "," hosts | 31 | host = L.intercalate "," hosts |
32 | -- keyblob = L.dropWhile isSpace $ L.dropWhile (not . isSpace) $ SSH.keyblob key | ||
29 | 33 | ||
30 | 34 | ||
31 | parseLine :: Int -> L.ByteString -> KnownHostsEntry | 35 | parseLine :: Int -> L.ByteString -> KnownHostsEntry |
@@ -34,7 +38,8 @@ parseLine num str = KnownHostsEntry | |||
34 | , eSp = sp | 38 | , eSp = sp |
35 | , eComment = comment | 39 | , eComment = comment |
36 | , eKeyHeader = hdr | 40 | , eKeyHeader = hdr |
37 | , eKeyBlob = key | 41 | , eKeyBlob = blob |
42 | , eKey = key | ||
38 | , eHosts = hosts | 43 | , eHosts = hosts |
39 | } | 44 | } |
40 | where | 45 | where |
@@ -47,6 +52,23 @@ parseLine num str = KnownHostsEntry | |||
47 | h0 <- take 1 hs | 52 | h0 <- take 1 hs |
48 | h0 : hs' | 53 | h0 : hs' |
49 | hdr = L.concat $ take 1 (drop 1 ns) | 54 | hdr = L.concat $ take 1 (drop 1 ns) |
50 | key = L.concat $ take 1 (drop 2 ns) | 55 | blob = L.concat $ take 1 (drop 2 ns) |
56 | key = SSH.blobkey $ L.concat $ hdr:" ":blob:[] | ||
51 | comment = L.concat $ zipWith L.append (drop 3 ns) (drop 3 sp ++ repeat "") | 57 | comment = L.concat $ zipWith L.append (drop 3 ns) (drop 3 sp ++ repeat "") |
52 | 58 | ||
59 | data KnownHosts = KnownHosts | ||
60 | { khcount :: Int | ||
61 | , khentries :: [KnownHostsEntry] | ||
62 | } | ||
63 | |||
64 | decode :: L.ByteString -> KnownHosts | ||
65 | decode input = renum $ foldr grok (KnownHosts 0 []) ls | ||
66 | where | ||
67 | ls = L.lines input | ||
68 | grok x (KnownHosts cnt es) = | ||
69 | KnownHosts (cnt+1) (parseLine 0 x:es) | ||
70 | renum kh = kh { khentries = zipWith setnum [1..] (khentries kh) } | ||
71 | where setnum n e = e { eLineno=n } | ||
72 | |||
73 | encode :: KnownHosts -> L.ByteString | ||
74 | encode kh = L.unlines $ map serializeLine (khentries kh) | ||