summaryrefslogtreecommitdiffhomepage
path: root/Gpg/Keyring.hs
blob: a0fa242b064aca2fdce49db1b13a00de2b837c62 (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
{- Copyright 2017 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

-- | Gpg keyrings for debug-me

module Gpg.Keyring where

import Gpg
import qualified Gpg.Wot

import System.FilePath
import Data.Char
import System.Directory
import Data.Time.Clock
import Data.Time.Format
import System.Process
import System.Exit

keyringDir :: FilePath
keyringDir = "/usr/share/debug-me/keyring"

data Keyring = Keyring FilePath UTCTime

keyringToDeveloperDesc :: Maybe (Gpg.Wot.WotStats) -> Keyring -> String
keyringToDeveloperDesc mws (Keyring f mtime) =
	name ++ " is " ++ desc ++ " \t(as of " ++ showtime mtime ++ ")"
  where
	name = maybe "This person" Gpg.Wot.wotStatName mws
	desc = map sanitize $ dropExtension $ takeFileName f
	sanitize '_' = ' '
	sanitize c
		| isAlphaNum c || c `elem` "-+" = c
		| otherwise = '?'
	showtime = formatTime defaultTimeLocale "%c"

findKeyringsContaining :: GpgKeyId -> IO [Keyring]
findKeyringsContaining k = 
	go [] . map (keyringDir </>) =<< getDirectoryContents keyringDir
  where
	go c [] = return c
	go c (f:fs) = do
		isfile <- doesFileExist f
		if isfile && takeExtension f == ".gpg"
			then do
				inkeyring <- isInKeyring k f
				if inkeyring
					then do
						mtime <- getModificationTime f
						let keyring = Keyring f mtime
						go (keyring : c) fs
					else go c fs
			else go c fs

-- | Check if the gpg key is included in the keyring file.
--
-- Similar to gpgv, this does not check if the key is revoked or expired,
-- only if it's included in the keyring.
isInKeyring :: GpgKeyId -> FilePath -> IO Bool
isInKeyring (GpgKeyId k) f = do
	-- gpg assumes non-absolute keyring files are relative to ~/.gnupg/
	absf <- makeAbsolute f
	let p = proc "gpg"
		-- Avoid reading any keyrings except the specified one.
		[ "--no-options"
		, "--no-default-keyring"
		, "--no-auto-check-trustdb"
		, "--keyring", absf
		, "--list-key", k
		]
	(exitcode, _, _) <- readCreateProcessWithExitCode p ""
	return (exitcode == ExitSuccess)