summaryrefslogtreecommitdiff
path: root/email-key-rotation/serialize.scm
blob: e79d9553d9e4da0d0c12eb2e1e8a29b91e4e5068 (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
(define-module (email-key-rotation serialize)
  #:use-module (email-key-rotation state)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-26)
  #:use-module (rnrs bytevectors)
  #:use-module (oop goops)
  #:export (read-state write-state)
  #:declarative? #t
  #:duplicates (merge-generics))

(define* (read-state #:optional (port (current-input-port)))
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'read-state)
	  (make-exception-with-irritants (list port))
	  (make-exception-with-message
	   "while reading the current state file:")
	  exn)))
    (lambda ()
      (match (read port)
	(('email-key-rotation-state
	  private-key-file
	  (selectors ...)
	  selector
	  current-key
	  expired-key)
	 (the-email-key-rotation-state
	  (make <email-key-rotation-state>
	    #:private-key-file private-key-file
	    #:selectors selectors
	    #:current-dkim-selector selector
	    #:current-dkim-private-key current-key
	    #:expired-dkim-private-key expired-key)))))))

(define* (write-state state #:optional (port (current-output-port)))
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'write-state)
	  (make-exception-with-irritants (list state port))
	  (make-exception-with-message
	   "cannot write the new state:")
	  exn)))
    (lambda ()
      (when (file-port? port)
	(chmod port #o600)
	(let ((state (the-email-key-rotation-state state)))
	  (write `(email-key-rotation-state
		   ,(private-key-file state)
		   (,@(selectors state))
		   ,(current-dkim-selector state)
		   ,(current-dkim-private-key state)
		   ,(expired-dkim-private-key state))
		 port))))))