summaryrefslogtreecommitdiff
path: root/KnownHosts.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-08-05 18:01:36 -0400
committerjoe <joe@jerkface.net>2014-08-05 18:01:36 -0400
commitfa98533c812cb945b7eb6818e763f12c56fdb9e3 (patch)
treea19ffe52f9239aed56b16589b584be6134a02b8a /KnownHosts.hs
parentef5631f8e90ed30fedc3e1dc2d2bb5f882fee183 (diff)
assignKey implemented for ssh_known_hosts
Diffstat (limited to 'KnownHosts.hs')
-rw-r--r--KnownHosts.hs28
1 files changed, 25 insertions, 3 deletions
diff --git a/KnownHosts.hs b/KnownHosts.hs
index e04ad90..e19e68e 100644
--- a/KnownHosts.hs
+++ b/KnownHosts.hs
@@ -1,9 +1,17 @@
1{-# LANGUAGE OverloadedStrings #-} 1{-# LANGUAGE OverloadedStrings #-}
2module KnownHosts where 2module KnownHosts
3 ( KnownHosts(..)
4 , decode
5 , encode
6 , assignKey
7 , parseLine
8 , serializeLine ) where
3 9
4import qualified Data.ByteString.Lazy.Char8 as L 10import qualified Data.ByteString.Lazy.Char8 as L
5import Data.Char 11import Data.Char
6import Data.List 12import Data.List
13import Data.Ord
14import Data.Maybe
7import qualified SSHKey as SSH 15import qualified SSHKey as SSH
8 16
9data KnownHostsEntry = KnownHostsEntry 17data KnownHostsEntry = KnownHostsEntry
@@ -61,14 +69,28 @@ data KnownHosts = KnownHosts
61 , khentries :: [KnownHostsEntry] 69 , khentries :: [KnownHostsEntry]
62 } 70 }
63 71
72renum kh = kh { khentries = zipWith setnum [1..] (khentries kh) }
73 where setnum n e = e { eLineno=n }
74
64decode :: L.ByteString -> KnownHosts 75decode :: L.ByteString -> KnownHosts
65decode input = renum $ foldr grok (KnownHosts 0 []) ls 76decode input = renum $ foldr grok (KnownHosts 0 []) ls
66 where 77 where
67 ls = L.lines input 78 ls = L.lines input
68 grok x (KnownHosts cnt es) = 79 grok x (KnownHosts cnt es) =
69 KnownHosts (cnt+1) (parseLine 0 x:es) 80 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 81
73encode :: KnownHosts -> L.ByteString 82encode :: KnownHosts -> L.ByteString
74encode kh = L.unlines $ map serializeLine (khentries kh) 83encode kh = L.unlines $ map serializeLine (khentries kh)
84
85assignKey :: SSH.Key -> L.ByteString -> KnownHosts -> KnownHosts
86assignKey key name kh = renum KnownHosts { khcount = khcount kh - length ds
87 , khentries = es }
88 where
89 (ms,ns) = partition (\e -> eKey e == Just key) $ khentries kh
90 ms' = map (\e -> e { eHosts = eHosts e `union` [name]}) ms
91 ns' = map (\e -> e { eHosts = eHosts e \\ if isJust (eKey e) then [name]
92 else []
93 })
94 ns
95 (ds,ns'') = partition (\e -> null (eHosts e)) ns'
96 es = sortBy (comparing eLineno) $ ms' ++ ns''