summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2016-05-02 00:21:02 +0000
committerjoe <joe@jerkface.net>2016-05-02 00:21:02 +0000
commitb04dd1bba833852e22844696ecad365a11248e98 (patch)
tree344581d239dbb97da1f033a350f37693915e64e0
parentc070e563c3c1a04dc722c495acea5f9739c3334b (diff)
implemented cokiki hosts command to merge cache hosts with etc hosts.
-rw-r--r--cokiki.hs14
1 files changed, 14 insertions, 0 deletions
diff --git a/cokiki.hs b/cokiki.hs
index 292b0b7..8e6ec35 100644
--- a/cokiki.hs
+++ b/cokiki.hs
@@ -19,6 +19,7 @@ import System.Exit
19import System.IO 19import System.IO
20import System.Posix.User 20import System.Posix.User
21import CommandLine 21import CommandLine
22import qualified Hosts
22 23
23usage = unlines 24usage = unlines
24 [ "cokiki <command> [--chroot=ROOTDIR]" 25 [ "cokiki <command> [--chroot=ROOTDIR]"
@@ -52,6 +53,9 @@ usage = unlines
52 , "" 53 , ""
53 , " tor Modify /etc/tor/torrc to configure a tor hidden" 54 , " tor Modify /etc/tor/torrc to configure a tor hidden"
54 , " service for email (smtp), ssh, and http ports." 55 , " service for email (smtp), ssh, and http ports."
56 , ""
57 , " hosts Merge hostnames from /var/cache/kiki/config/hosts"
58 , " into system file /etc/hosts."
55 ] 59 ]
56 60
57main = do 61main = do
@@ -62,6 +66,7 @@ main = do
62 ["ssh-server"] -> Just $ sshServer uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir 66 ["ssh-server"] -> Just $ sshServer uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir
63 ["strongswan"] -> Just $ strongswan uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir 67 ["strongswan"] -> Just $ strongswan uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir
64 ["tor"] -> Just $ configureTor uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir 68 ["tor"] -> Just $ configureTor uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir
69 ["hosts"] -> Just $ configureHosts uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir
65 _ -> Nothing 70 _ -> Nothing
66 spec = uncurry fancy Kiki.kikiOptions "" 71 spec = uncurry fancy Kiki.kikiOptions ""
67 errorQuit msg = do 72 errorQuit msg = do
@@ -77,6 +82,7 @@ main = do
77maybeReadFile :: FilePath -> IO (Maybe L.ByteString) 82maybeReadFile :: FilePath -> IO (Maybe L.ByteString)
78maybeReadFile path = do 83maybeReadFile path = do
79 doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path) 84 doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path)
85
80myWriteFile f bs = do 86myWriteFile f bs = do
81 createDirectoryIfMissing True (takeDirectory f) 87 createDirectoryIfMissing True (takeDirectory f)
82 hPutStrLn stderr $ "Writing "++f 88 hPutStrLn stderr $ "Writing "++f
@@ -212,6 +218,14 @@ configureTor uid root cmn = whenRoot uid root cmn $ do
212 Kiki.importAndRefresh root cmn 218 Kiki.importAndRefresh root cmn
213 return () 219 return ()
214 220
221configureHosts uid root cmn = whenRoot uid root cmn $ do
222 Kiki.importAndRefresh root cmn
223 hosts <- Hosts.decode . fromMaybe "" <$> maybeReadFile (root "/etc/hosts")
224 kikihosts <- Hosts.decode . fromMaybe "" <$> maybeReadFile (root "/var/cache/kiki/config/hosts")
225 let hosts' = hosts `Hosts.plus` kikihosts
226 case Hosts.diff hosts hosts' of
227 [] -> hPutStrLn stderr "kiki hosts are already merged."
228 _ -> myWriteFile (root "/etc/hosts") $ Hosts.encode hosts'
215 229
216parseSshConfig :: L.ByteString -> [[L.ByteString]] 230parseSshConfig :: L.ByteString -> [[L.ByteString]]
217parseSshConfig bs = map tokenize $ L8.lines bs 231parseSshConfig bs = map tokenize $ L8.lines bs