blob: 5ea7ba9520cbbfb8cba34238138224b4d260686f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
{- Copyright 2016 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
{-# LANGUAGE DeriveGeneric, BangPatterns #-}
module BackupLog where
import Types
import Types.Server
import Types.Cost
import Utility.UserInfo
import GHC.Generics
import Data.Time.Clock.POSIX
import Data.Aeson
import Data.Maybe
import System.FilePath
import System.Directory
import System.Posix.Files
import qualified Data.ByteString.Lazy as B
data BackupLog = BackupLog
{ logDate :: POSIXTime
, logEvent :: BackupEvent
}
deriving (Show, Generic)
instance ToJSON BackupLog
instance FromJSON BackupLog
-- | Log of a backup.
--
-- If an attacker cracks the user's system and finds this stored
-- on it, it should not help them recover keys from keysafe.
--
-- That's why the Name used is not included; as knowing the name lets
-- an attacker download shards and start password cracking.
--
-- Including the password entropy does let an attacker avoid trying
-- weak passwords and go right to passwords that are strong enough, but
-- this should only half the password crack time at worst.
data BackupEvent
= BackupSkipped SecretKeySource
| BackupMade
{ backupServers :: [ServerName]
, backupSecretKeySource :: SecretKeySource
, backupPasswordEntropy :: Int
}
deriving (Show, Generic)
matchesSecretKeySource :: SecretKeySource -> BackupLog -> Bool
matchesSecretKeySource a (BackupLog _ (BackupSkipped b)) = a == b
matchesSecretKeySource a (BackupLog _ (BackupMade { backupSecretKeySource = b })) = a == b
instance ToJSON BackupEvent
instance FromJSON BackupEvent
mkBackupLog :: BackupEvent -> IO BackupLog
mkBackupLog evt = BackupLog
<$> getPOSIXTime
<*> pure evt
backupMade :: [Server] -> SecretKeySource -> Entropy UnknownPassword -> BackupEvent
backupMade servers sks (Entropy n) = BackupMade
{ backupServers = map serverName servers
, backupSecretKeySource = sks
, backupPasswordEntropy = n
}
backupLogFile :: IO FilePath
backupLogFile = do
home <- myHomeDir
return $ home </> ".keysafe/backup.log"
readBackupLogs :: IO [BackupLog]
readBackupLogs = do
f <- backupLogFile
e <- doesFileExist f
if e
then fromMaybe [] . decode <$> B.readFile f
else return []
storeBackupLog :: BackupLog -> IO ()
storeBackupLog r = do
!rs <- readBackupLogs
f <- backupLogFile
let d = takeDirectory f
createDirectoryIfMissing True d
setFileMode d $
ownerReadMode
`unionFileModes` ownerWriteMode
`unionFileModes` ownerExecuteMode
setPermissions d
$ setOwnerReadable True
$ setOwnerWritable True
$ setOwnerExecutable True emptyPermissions
B.writeFile f $ encode (r:rs)
|