summaryrefslogtreecommitdiff
path: root/cokiki.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2016-04-26 20:07:11 -0400
committerjoe <joe@jerkface.net>2016-04-26 20:07:11 -0400
commitd256e09e8b111ac463ee60af50aacc25d0d25823 (patch)
tree180ad1f9af4e09e5a075ec54cfc68fd87fd76135 /cokiki.hs
parentdc9e52b3b15cbd82b1f0fb5352d0541d249b9008 (diff)
ssh-server configuration
Diffstat (limited to 'cokiki.hs')
-rw-r--r--cokiki.hs20
1 files changed, 16 insertions, 4 deletions
diff --git a/cokiki.hs b/cokiki.hs
index fb0523c..68923a7 100644
--- a/cokiki.hs
+++ b/cokiki.hs
@@ -4,6 +4,7 @@ import Control.Applicative
4import Control.DeepSeq 4import Control.DeepSeq
5import Data.Bool 5import Data.Bool
6import Data.Char 6import Data.Char
7import Data.List
7import Data.Maybe 8import Data.Maybe
8import qualified Data.ByteString.Lazy.Char8 as L 9import qualified Data.ByteString.Lazy.Char8 as L
9import qualified Data.ByteString.Lazy.Char8 as L8 10import qualified Data.ByteString.Lazy.Char8 as L8
@@ -64,15 +65,15 @@ main = do
64maybeReadFile :: FilePath -> IO (Maybe L.ByteString) 65maybeReadFile :: FilePath -> IO (Maybe L.ByteString)
65maybeReadFile path = do 66maybeReadFile path = do
66 doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path) 67 doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path)
68myWriteFile f bs = do
69 hPutStrLn stderr $ "Writing "++f
70 -- L8.putStr bs
71 L8.writeFile f bs
67 72
68sshClient uid root cmn = do 73sshClient uid root cmn = do
69 -- /etc/ssh/ssh_config <-- 'GlobalKnownHostsFile /var/cache/kiki/ssh_known_hosts' 74 -- /etc/ssh/ssh_config <-- 'GlobalKnownHostsFile /var/cache/kiki/ssh_known_hosts'
70 sshconfig <- parseSshConfig . fromMaybe "" <$> maybeReadFile (root "/etc/ssh/ssh_config") 75 sshconfig <- parseSshConfig . fromMaybe "" <$> maybeReadFile (root "/etc/ssh/ssh_config")
71 let (ps,qs) = sshSplitAtDirective "GlobalKnownHostsFile" sshconfig 76 let (ps,qs) = sshSplitAtDirective "GlobalKnownHostsFile" sshconfig
72 myWriteFile f bs = do
73 hPutStrLn stderr $ "Writing "++f
74 -- L8.putStr bs
75 L8.writeFile f bs
76 sshconfig' <- 77 sshconfig' <-
77 case qs of 78 case qs of
78 d:ds | elem "/var/cache/kiki/ssh_known_hosts" d 79 d:ds | elem "/var/cache/kiki/ssh_known_hosts" d
@@ -83,6 +84,7 @@ sshClient uid root cmn = do
83 stmt = take 1 d ++ hs 84 stmt = take 1 d ++ hs
84 return $ Just (ps ++ stmt : ds) 85 return $ Just (ps ++ stmt : ds)
85 [] -> do -- Unconfigured add fresh directive. 86 [] -> do -- Unconfigured add fresh directive.
87 hPutStrLn stderr "adding GlobalKnownHostsFile directive"
86 let stmt = L8.unwords ["GlobalKnownHostsFile" 88 let stmt = L8.unwords ["GlobalKnownHostsFile"
87 , "/var/cache/kiki/ssh_known_hosts" 89 , "/var/cache/kiki/ssh_known_hosts"
88 , "/etc/ssh/ssh_known_hosts" 90 , "/etc/ssh/ssh_known_hosts"
@@ -98,6 +100,15 @@ sshClient uid root cmn = do
98 Kiki.importAndRefresh root cmn 100 Kiki.importAndRefresh root cmn
99 101
100sshServer root cmn = do 102sshServer root cmn = do
103 sshconfig <- parseSshConfig . fromMaybe "" <$> maybeReadFile (root "/etc/ssh/sshd_config")
104 let p:gs = groupBy (\_ d -> not $ sshIsDirective "HostKey" d) $ ["#"]:sshconfig
105 got = filter (\(d:ds) -> elem "/var/cache/kiki/ssh_host_rsa_key" d) gs
106 case got of
107 _:_ -> do hPutStrLn stderr "ssh-server already configured."
108 [] -> do let sshconfig' = drop 1 $ sshconfig ++ [stmt]
109 stmt = ["HostKey", " ", "/var/cache/kiki/ssh_host_rsa_key"]
110 hPutStrLn stderr "adding HostKey directive"
111 myWriteFile (root "/etc/ssh/sshd_config") $ unparseSshConfig sshconfig'
101 -- /etc/ssh/sshd_config <-- 'HostKey /var/cache/kiki/ssh_host_ecdsa_key' etc. 112 -- /etc/ssh/sshd_config <-- 'HostKey /var/cache/kiki/ssh_host_ecdsa_key' etc.
102 Kiki.importAndRefresh root cmn 113 Kiki.importAndRefresh root cmn
103 114
@@ -111,6 +122,7 @@ strongswan root cmn = do
111 -- /var/cache/kiki/ipsec.d/private <-- contains private key 122 -- /var/cache/kiki/ipsec.d/private <-- contains private key
112 123
113 124
125parseSshConfig :: L.ByteString -> [[L.ByteString]]
114parseSshConfig bs = map tokenize $ L8.lines bs 126parseSshConfig bs = map tokenize $ L8.lines bs
115 where 127 where
116 tokenize l = L8.groupBy tokrel l' ++ [comment] 128 tokenize l = L8.groupBy tokrel l' ++ [comment]