]> gitweb @ CieloNegro.org - blackboard-dns.git/commitdiff
client makereq
authorPHO <pho@cielonegro.org>
Fri, 5 Jun 2009 07:14:45 +0000 (16:14 +0900)
committerPHO <pho@cielonegro.org>
Fri, 5 Jun 2009 07:14:45 +0000 (16:14 +0900)
ClientMain.hs [new file with mode: 0644]
GNUmakefile
ServerMain.hs
System/Posix/Uname.hs [new file with mode: 0644]
blackboard-dns.cabal

diff --git a/ClientMain.hs b/ClientMain.hs
new file mode 100644 (file)
index 0000000..b3090b3
--- /dev/null
@@ -0,0 +1,81 @@
+module Main where
+
+import           Control.Monad
+import           Control.Monad.Trans
+import           OpenSSL
+import           OpenSSL.PEM
+import           OpenSSL.RSA
+import           OpenSSL.X509.Request
+import           System.Console.Haskeline
+import           System.Console.ParseArgs
+import           System.IO
+import           System.Posix.Files
+import           System.Posix.Uname
+
+
+data Options
+    = Help
+    | Command
+    deriving (Show, Eq, Ord)
+
+arginfo :: [Arg Options]
+arginfo = [ Arg {
+              argIndex = Help
+            , argAbbr  = Just 'h'
+            , argName  = Nothing
+            , argData  = Nothing
+            , argDesc  = "print this help."
+            }
+          , Arg {
+              argIndex = Command
+            , argAbbr  = Nothing
+            , argName  = Nothing
+            , argData  = argDataDefaulted "COMMAND" ArgtypeString "update"
+            , argDesc  = "command to the client (default: \"update\")."
+            }
+          ]
+
+main :: IO ()
+main = withOpenSSL $
+       do m <- parseArgsIO ArgsComplete arginfo
+          when (gotArg m Help)
+                   $ usageError m ""
+
+          case getRequiredArg m Command of
+            "makereq"
+                -> makeReq
+            _   -> usageError m ""
+
+
+makeReq :: IO ()
+makeReq
+    = do fqdn <- runInputT defaultSettings $
+                 do defaultFqdn <- liftM uNodeName $ liftIO uname
+                    fqdn        <- getInputLine ("What's your FQDN? (default: " ++ defaultFqdn ++ "): ")
+                    case fqdn of
+                      Just s@(_:_) -> return s
+                      _            -> return defaultFqdn
+
+         putStrLn "Generating keypair..."
+         key <- generateRSAKey 1024 3 Nothing
+
+         let pubFile = fqdn ++ ".pub"
+         withFile pubFile WriteMode $ \ h ->
+             writePublicKey key >>= hPutStr h
+         putStrLn ("Wrote " ++ pubFile)
+
+         let keyFile = fqdn ++ ".key"
+         withFile keyFile WriteMode $ \ h ->
+             writePKCS8PrivateKey key Nothing >>= hPutStr h
+         setFileMode keyFile (ownerReadMode `unionFileModes` ownerWriteMode)
+         putStrLn ("Wrote " ++ keyFile ++ " (with no encryption)")
+
+         req <- newX509Req
+         setVersion     req 2
+         setSubjectName req [("CN", fqdn)]
+         setPublicKey   req key
+         let reqFile = fqdn ++ ".req"
+         withFile reqFile WriteMode $ \ h ->
+             writeX509Req req ReqNewFormat >>= hPutStr h
+         putStrLn ("Wrote " ++ reqFile)
+         putStrLn ("Now send " ++ reqFile ++ " to the server operator and tell him/her to sign it.")
\ No newline at end of file
index 2bb908fb3253dea6cae7c46043e6993d350d349d..b3350fe21e340b44af0743cfe87d92d9104e170c 100644 (file)
@@ -1,4 +1,5 @@
-RUN_COMMAND = ./dist/build/bbdns-server/bbdns-server
+#RUN_COMMAND = ./dist/build/bbdns-server/bbdns-server
+RUN_COMMAND = ./dist/build/bbdns-client/bbdns-client makereq
 
 include cabal-package.mk
 
index 833256f23961a03fa4ee73774b6cc911e25e087a..5f0db3001bc24fdb162e4e260ed755ccf0bb896f 100644 (file)
@@ -69,9 +69,9 @@ arginfo = [ Arg {
 main :: IO ()
 main = do m <- parseArgsIO ArgsComplete arginfo
           when (gotArg m Help)
-                   $ usageError m "" `seq` return ()
+                   $ usageError m ""
 
-          usageError m "" `seq` return ()
+          usageError m ""
 
 {-
 main = do sqliteConnect "ddns.db" $ \ db ->
diff --git a/System/Posix/Uname.hs b/System/Posix/Uname.hs
new file mode 100644 (file)
index 0000000..1a57ed1
--- /dev/null
@@ -0,0 +1,29 @@
+module System.Posix.Uname
+    ( Uname(..)
+    , uname
+    )
+    where
+
+import qualified Bindings.Uname as U
+import Foreign.C
+import Foreign.Marshal
+
+data Uname
+    = Uname {
+        uSysName  :: String
+      , uNodeName :: String
+      , uRelease  :: String
+      , uVersion  :: String
+      , uMachine  :: String
+      }
+    deriving (Show, Eq, Ord)
+
+uname :: IO Uname
+uname = alloca $ \ ptr ->
+        do throwErrnoIfMinus1_ "uname" (U.uname ptr)
+           sys <- peekCString $ U.sysname  ptr
+           nod <- peekCString $ U.nodename ptr
+           rel <- peekCString $ U.release  ptr
+           ver <- peekCString $ U.version  ptr
+           mac <- peekCString $ U.machine  ptr
+           return $ Uname sys nod rel ver mac
index b66aa2209d62983638ac6046035af95ce76a23d5..5e955b34b81ae056f9c517edf9a3e76a38ed04d1 100644 (file)
@@ -32,3 +32,17 @@ Executable bbdns-server
 
     GHC-Options:
         -Wall
+
+Executable bbdns-client
+    Build-Depends:
+        HsOpenSSL, base, bindings-uname, haskeline, mtl, parseargs,
+        unix
+
+    Other-Modules:
+        System.Posix.Uname
+
+    Main-Is:
+        ClientMain.hs
+
+    GHC-Options:
+        -Wall