From 89cd1ec7c87a96315b9c3e767e853c2ba6f1f642 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 10 Jul 2019 06:00:40 -0400 Subject: pass FileWriter through to write* --- lib/Kiki.hs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/Kiki.hs b/lib/Kiki.hs index 3a1028b..e782d8a 100644 --- a/lib/Kiki.hs +++ b/lib/Kiki.hs @@ -330,17 +330,17 @@ importAndRefresh root cmn cipher = do -- ipsec.conf snippet configuring the peer and referencing the installed cert -- file. installIpsecPeerCertificate - :: (FilePath -> FilePath) + :: FileWriter -> (L.ByteString, SockAddr, KeyData) -> IO Char8.ByteString -installIpsecPeerCertificate mkpath (contactname,addr,kd) = +installIpsecPeerCertificate fw@(FileWriter mkpath _) (contactname,addr,kd) = Char8.concat <$> do forM (take 1 ipsecs) $ \k -> do flip (either warn') (pemFromPacket k :: Either String String) $ \pem -> do case sshs of (sshkey:_) -> do theirHostname <- sshKeyToHostname sshkey - write (mkpath $ ipsecCertPath theirHostname) pem + write fw (mkpath $ ipsecCertPath theirHostname) pem return $ strongswanPeerConfiguration addr contactname theirHostname _ -> error "fuck." where @@ -367,21 +367,21 @@ ipsecKeyPath = ipsecPath "private" ipsecCertPath :: Char8.ByteString -> FilePath ipsecCertPath = ipsecPath "certs" -write' :: (FilePath -> t -> IO b) -> FilePath -> t -> IO b -write' wr f bs = do +write' :: FileWriter -> (FilePath -> t -> IO b) -> FilePath -> t -> IO b +write' fw wr f bs = do createDirectoryIfMissing True $ takeDirectory f wr f bs -write :: FilePath -> String -> IO () -write = write' writeFile +write :: FileWriter -> FilePath -> String -> IO () +write fw = write' fw writeFile -writeL :: FilePath -> Char8.ByteString -> IO () -writeL = write' L.writeFile +writeL :: FileWriter -> FilePath -> Char8.ByteString -> IO () +writeL fw = write' fw L.writeFile -writeL077 :: FilePath -> Char8.ByteString -> IO FileMode -writeL077 f bs = do +writeL077 :: FileWriter -> FilePath -> Char8.ByteString -> IO FileMode +writeL077 fw f bs = do old_umask <- setFileCreationMask 0o077 - writeL f bs + writeL fw f bs setFileCreationMask old_umask data FileWriter = FileWriter { @@ -452,10 +452,10 @@ getssh (contactname,_addr,kd) = do Char8.unlines taggedblobs -installIpsecConf :: (FilePath -> FilePath) -> SockAddr -> Char8.ByteString -> [(Char8.ByteString, SockAddr, KeyData)] -> IO () -installIpsecConf mkpath wkaddr (certBasename) cs = do - snippets <- mapM (installIpsecPeerCertificate mkpath) cs - writeL (mkpath "ipsec.conf") . Char8.unlines +installIpsecConf :: FileWriter -> SockAddr -> Char8.ByteString -> [(Char8.ByteString, SockAddr, KeyData)] -> IO () +installIpsecConf fw@(FileWriter mkpath _) wkaddr (certBasename) cs = do + snippets <- mapM (installIpsecPeerCertificate fw) cs + writeL fw (mkpath "ipsec.conf") . Char8.unlines $ [ "conn %default" , " ikelifetime=60m" , " keylife=20m" @@ -525,12 +525,12 @@ rethrowKikiErrors BadPassphrase = rethrowKikiErrors rt = unconditionally $ return rt writePublicKeyFiles :: KeyRingRuntime -> FileWriter -> String -> Char8.ByteString -> SockAddr -> IO () -writePublicKeyFiles rt (FileWriter mkpath commit) grip oname wkaddr = do +writePublicKeyFiles rt fw@(FileWriter mkpath commit) grip oname wkaddr = do -- Finally, export public keys if they do not exist. - either warn (write $ mkpath "root/.ssh/id_rsa.pub") $ show_ssh' "ssh-client" grip (rtKeyDB rt) - either warn (write $ mkpath "ssh_host_rsa_key.pub") $ show_ssh' "ssh-server" grip (rtKeyDB rt) - either warn (write $ mkpath $ ipsecCertPath oname) $ show_pem' "ipsec" grip (rtKeyDB rt) pemFromPacket + either warn (write fw $ mkpath "root/.ssh/id_rsa.pub") $ show_ssh' "ssh-client" grip (rtKeyDB rt) + either warn (write fw $ mkpath "ssh_host_rsa_key.pub") $ show_ssh' "ssh-server" grip (rtKeyDB rt) + either warn (write fw $ mkpath $ ipsecCertPath oname) $ show_pem' "ipsec" grip (rtKeyDB rt) pemFromPacket let onionkeys = mapMaybe namedContact $ Map.elems $ rtKeyDB rt cs = filter (\(_,_,kd) -> notme kd) onionkeys @@ -545,9 +545,9 @@ writePublicKeyFiles rt (FileWriter mkpath commit) grip oname wkaddr = do known_hosts = L.concat $ map getssh onionkeys - writeL (mkpath "ssh_known_hosts") known_hosts + writeL fw (mkpath "ssh_known_hosts") known_hosts - installIpsecConf mkpath wkaddr (Char8.pack $ takeBaseName $ ipsecCertPath oname) cs + installIpsecConf fw wkaddr (Char8.pack $ takeBaseName $ ipsecCertPath oname) cs commit sshKeyToHostname :: Packet -> IO Char8.ByteString -- cgit v1.2.3