|
| 1 | +{-# LANGUAGE NoImplicitPrelude #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE QuasiQuotes #-} |
| 4 | +{-# LANGUAGE RecordWildCards #-} |
| 5 | + |
| 6 | +module Main where |
| 7 | + |
| 8 | +import Control.Applicative ((<*>)) |
| 9 | +import Control.Exception (Exception, throwIO) |
| 10 | +import Control.Monad (forever, mapM_, return) |
| 11 | +import Data.ByteString (ByteString, concat) |
| 12 | +import Data.Eq (Eq) |
| 13 | +import Data.Function (($)) |
| 14 | +import Data.Functor ((<$>)) |
| 15 | +import Data.List (head, map, (++)) |
| 16 | +import Data.Maybe (Maybe (Just, Nothing)) |
| 17 | +import Data.Text (Text, intercalate, strip) |
| 18 | +import Data.Text.Encoding (decodeUtf8, encodeUtf8) |
| 19 | +import Data.Typeable (Typeable) |
| 20 | +import Database.SQLite.Simple.FromRow (FromRow (fromRow)) |
| 21 | +import Database.SQLite.Simple.ToRow (ToRow (toRow)) |
| 22 | +import Database.SQLite.Simple.Types |
| 23 | +import GHC.Integer (Integer) |
| 24 | +import System.IO (IO, print, putStrLn) |
| 25 | +import Text.RawString.QQ (r) |
| 26 | +import Text.Show (Show, show) |
| 27 | + |
| 28 | +import Database.SQLite.Simple as DB |
| 29 | +import Network.Socket as Sock |
| 30 | +import Network.Socket.ByteString as SockBS |
| 31 | + |
| 32 | +type UserName = Text |
| 33 | +type Shell = Text |
| 34 | +type HomeDirectory = Text |
| 35 | +type RealName = Text |
| 36 | +type Phone = Text |
| 37 | + |
| 38 | +type UserRow = ( Null |
| 39 | + , UserName |
| 40 | + , Shell |
| 41 | + , HomeDirectory |
| 42 | + , RealName |
| 43 | + , Phone |
| 44 | + ) |
| 45 | + |
| 46 | +data User = User |
| 47 | + { userId :: Integer |
| 48 | + , userName :: UserName |
| 49 | + , shell :: Shell |
| 50 | + , homeDirectory :: HomeDirectory |
| 51 | + , realName :: RealName |
| 52 | + , phone :: Phone |
| 53 | + } deriving (Eq, Show) |
| 54 | + |
| 55 | +instance FromRow User where |
| 56 | + fromRow = User <$> field |
| 57 | + <*> field |
| 58 | + <*> field |
| 59 | + <*> field |
| 60 | + <*> field |
| 61 | + <*> field |
| 62 | + |
| 63 | +instance ToRow User where |
| 64 | + toRow User{..} = toRow ( userId |
| 65 | + , userName |
| 66 | + , shell |
| 67 | + , homeDirectory |
| 68 | + , realName |
| 69 | + , phone |
| 70 | + ) |
| 71 | + |
| 72 | +data DuplicateDataEx = DuplicateDataEx |
| 73 | + deriving (Eq, Show, Typeable) |
| 74 | + |
| 75 | +instance Exception DuplicateDataEx |
| 76 | + |
| 77 | +createUsersTable :: Query |
| 78 | +createUsersTable = [r| |
| 79 | + CREATE TABLE IF NOT EXISTS users |
| 80 | + ( id INTEGER PRIMARY KEY AUTOINCREMENT |
| 81 | + , username TEXT UNIQUE |
| 82 | + , shell TEXT |
| 83 | + , homedirectory TEXT |
| 84 | + , realname TEXT |
| 85 | + , phone TEXT |
| 86 | + ) |
| 87 | +|] |
| 88 | + |
| 89 | +insertUserQuery :: Query |
| 90 | +insertUserQuery = "INSERT INTO users VALUES (?, ?, ?, ?, ?, ?)" |
| 91 | + |
| 92 | +allUsersQuery :: Query |
| 93 | +allUsersQuery = "SELECT * FROM users" |
| 94 | + |
| 95 | +getUserByUserNameQuery :: Query |
| 96 | +getUserByUserNameQuery = "SELECT * FROM users WHERE username = ?" |
| 97 | + |
| 98 | +getUser :: Connection -> UserName -> IO (Maybe User) |
| 99 | +getUser conn un = do |
| 100 | + results <- query conn getUserByUserNameQuery (Only un) |
| 101 | + case results of |
| 102 | + [] -> return Nothing |
| 103 | + [user] -> return $ Just user |
| 104 | + _ -> throwIO DuplicateDataEx |
| 105 | + |
| 106 | +createDatabase :: IO () |
| 107 | +createDatabase = do |
| 108 | + conn <- open "fingerd.db" |
| 109 | + execute_ conn createUsersTable |
| 110 | + execute conn insertUserQuery userRow |
| 111 | + rows <- query_ conn allUsersQuery |
| 112 | + mapM_ print (rows :: [User]) |
| 113 | + DB.close conn |
| 114 | + where userRow :: UserRow |
| 115 | + userRow = ( Null |
| 116 | + , "macalimlim" |
| 117 | + , "/run/current-system/sw/bin/zsh" |
| 118 | + , "/home/macalimlim" |
| 119 | + , "Michael Angelo Calimlim" |
| 120 | + , "1234567890" |
| 121 | + ) |
| 122 | + |
| 123 | +returnUsers :: Connection -> Socket -> IO () |
| 124 | +returnUsers conn sock = do |
| 125 | + rows <- query_ conn allUsersQuery |
| 126 | + let userNames = map userName rows |
| 127 | + newlineSeparated = intercalate "\n" userNames |
| 128 | + sendAll sock $ encodeUtf8 newlineSeparated |
| 129 | + |
| 130 | +formatUser :: User -> ByteString |
| 131 | +formatUser User{..} = concat [ "Login: ", encodeUtf8 userName, "\t\t\t\t" |
| 132 | + , "Name: ", encodeUtf8 realName, "\n" |
| 133 | + , "Directory: ", encodeUtf8 homeDirectory, "\t\t\t" |
| 134 | + , "Shell: ", encodeUtf8 shell, "\n" |
| 135 | + ] |
| 136 | + |
| 137 | +returnUser :: Connection -> Socket -> UserName -> IO () |
| 138 | +returnUser conn sock un = do |
| 139 | + maybeUser <- getUser conn $ strip un |
| 140 | + case maybeUser of |
| 141 | + Nothing -> putStrLn ("Cannot find user: " ++ show un) |
| 142 | + Just user -> sendAll sock $ formatUser user |
| 143 | + |
| 144 | +handleQuery :: Connection -> Socket -> IO () |
| 145 | +handleQuery conn sock = do |
| 146 | + msg <- SockBS.recv sock 1024 |
| 147 | + case msg of |
| 148 | + "\r\n" -> returnUsers conn sock |
| 149 | + name -> returnUser conn sock $ decodeUtf8 name |
| 150 | + |
| 151 | +handleQueries :: Connection -> Socket -> IO () |
| 152 | +handleQueries conn sock = forever $ do |
| 153 | + (s, _) <- accept sock |
| 154 | + putStrLn "Got connection, handling query" |
| 155 | + handleQuery conn s |
| 156 | + Sock.close s |
| 157 | + |
| 158 | +main :: IO () |
| 159 | +main = withSocketsDo $ do |
| 160 | + addrInfos <- getAddrInfo (Just (defaultHints {addrFlags = [AI_PASSIVE]})) Nothing (Just "79") |
| 161 | + let serverAddr = head addrInfos |
| 162 | + sock <- socket (addrFamily serverAddr) Stream defaultProtocol |
| 163 | + Sock.bind sock (addrAddress serverAddr) |
| 164 | + listen sock 1 |
| 165 | + conn <- open "fingerd.db" |
| 166 | + handleQueries conn sock |
| 167 | + DB.close conn |
| 168 | + Sock.close sock |
| 169 | + |
| 170 | +{- |
| 171 | +
|
| 172 | +Chapter Exercises |
| 173 | +
|
| 174 | +1. Try using the sqlite3 command line interface to add a new user |
| 175 | +or modify an existing user in finger.db. |
| 176 | +
|
| 177 | +2. Write an executable separate of fingerd and debug which allows |
| 178 | +you to add new users to the database. |
| 179 | +
|
| 180 | +3. Add the ability to modify an existing user in the database. |
| 181 | +
|
| 182 | +4. Bound on a different port, try creating a “control socket” that |
| 183 | +permits inserting new data into the database while the server is |
| 184 | +running. This will probably require, at minimum, learning how |
| 185 | +to use forkIO and the basics of concurrency in Haskell among |
| 186 | +other things. Design the format for representing the user rows |
| 187 | +passed over the TCP socket yourself. For bonus points, write |
| 188 | +your own client executable that takes the arguments from the |
| 189 | +command line as well. |
| 190 | +
|
| 191 | +5. Celebrate completing this massive book. |
| 192 | +
|
| 193 | +-} |
0 commit comments