diff options
author | joe <joe@jerkface.net> | 2016-04-26 20:07:11 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2016-04-26 20:07:11 -0400 |
commit | d256e09e8b111ac463ee60af50aacc25d0d25823 (patch) | |
tree | 180ad1f9af4e09e5a075ec54cfc68fd87fd76135 /cokiki.hs | |
parent | dc9e52b3b15cbd82b1f0fb5352d0541d249b9008 (diff) |
ssh-server configuration
Diffstat (limited to 'cokiki.hs')
-rw-r--r-- | cokiki.hs | 20 |
1 files changed, 16 insertions, 4 deletions
@@ -4,6 +4,7 @@ import Control.Applicative | |||
4 | import Control.DeepSeq | 4 | import Control.DeepSeq |
5 | import Data.Bool | 5 | import Data.Bool |
6 | import Data.Char | 6 | import Data.Char |
7 | import Data.List | ||
7 | import Data.Maybe | 8 | import Data.Maybe |
8 | import qualified Data.ByteString.Lazy.Char8 as L | 9 | import qualified Data.ByteString.Lazy.Char8 as L |
9 | import qualified Data.ByteString.Lazy.Char8 as L8 | 10 | import qualified Data.ByteString.Lazy.Char8 as L8 |
@@ -64,15 +65,15 @@ main = do | |||
64 | maybeReadFile :: FilePath -> IO (Maybe L.ByteString) | 65 | maybeReadFile :: FilePath -> IO (Maybe L.ByteString) |
65 | maybeReadFile path = do | 66 | maybeReadFile path = do |
66 | doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path) | 67 | doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path) |
68 | myWriteFile f bs = do | ||
69 | hPutStrLn stderr $ "Writing "++f | ||
70 | -- L8.putStr bs | ||
71 | L8.writeFile f bs | ||
67 | 72 | ||
68 | sshClient uid root cmn = do | 73 | sshClient 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 | ||
100 | sshServer root cmn = do | 102 | sshServer 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 | ||
125 | parseSshConfig :: L.ByteString -> [[L.ByteString]] | ||
114 | parseSshConfig bs = map tokenize $ L8.lines bs | 126 | parseSshConfig 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] |