summaryrefslogtreecommitdiff
path: root/guix/forge/acme.scm
blob: 690f1e4a76105aea79b18bce663f31efb45419da (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
;;; guix-forge --- Guix software forge meta-service
;;; Copyright © 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of guix-forge.
;;;
;;; guix-forge is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; guix-forge is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with guix-forge.  If not, see
;;; <https://www.gnu.org/licenses/>.

(define-module (forge acme)
  #:use-module ((gnu packages admin) #:select (shadow))
  #:use-module ((gnu packages autotools) #:select (autoconf automake))
  #:use-module ((gnu packages certs) #:select (nss-certs))
  #:use-module ((gnu packages curl) #:select (curl))
  #:use-module ((gnu packages documentation) #:select (asciidoc))
  #:use-module ((gnu packages pkg-config) #:select (pkg-config))
  #:use-module ((gnu packages tls) #:select (gnutls))
  #:use-module (gnu services)
  #:use-module (gnu services mcron)
  #:use-module (gnu system shadow)
  #:use-module (guix build-system gnu)
  #:use-module (guix diagnostics)
  #:use-module (guix gexp)
  #:use-module (guix git-download)
  #:use-module (guix i18n)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix profiles)
  #:use-module (guix records)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:export (acme-service-type
            <acme-configuration>
            acme-configuration
            acme-configuration?
            acme-configuration-uacme
            acme-configuration-email
            acme-configuration-acme-url
            acme-configuration-state-directory
            acme-configuration-http-01-challenge-directory
            acme-configuration-http-01-authorization-hook
            acme-configuration-http-01-cleanup-hook
            acme-configuration-key
            acme-configuration-certificates
            %letsencrypt-production-url
            %letsencrypt-staging-url
            acme-http-01-webroot-authorization-hook
            acme-http-01-webroot-cleanup-hook
            <acme-rsa-key>
            acme-rsa-key
            acme-rsa-key?
            acme-rsa-key-length
            <acme-ecdsa-key>
            acme-ecdsa-key
            acme-ecdsa-key?
            acme-ecdsa-key-length
            <acme-certificate>
            acme-certificate
            acme-certificate?
            acme-certificate-domains
            acme-certificate-deploy-hook))

(define-public uacme
  (package
    (name "uacme")
    (version "1.7.4")
    (source (origin
              ;; TODO: Unbundle libev.
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/ndilieto/uacme/")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1jhjyjnrfq07qgslmz1qpka1ahnmpya2garbxldkh2fr0bmsn26b"))))
    (build-system gnu-build-system)
    (arguments
     (list #:phases
           #~(modify-phases %standard-phases
               (add-after 'unpack 'set-version
                 (lambda _
                   (substitute* "configure.ac"
                     (("m4_esyscmd\\(\\[build-aux/git-version-gen \\.tarball-version\\]\\)")
                      #$version)))))))
    (inputs
     (list curl gnutls))
    (native-inputs
     (list asciidoc autoconf automake pkg-config))
    (home-page "https://github.com/ndilieto/uacme")
    (synopsis "Minimal ACMEv2 client written in C")
    (description "uacme is a lightweight client for the RFC8555 ACMEv2
protocol, written in plain C with minimal dependencies.  The ACMEv2
protocol allows a Certificate Authority such as Let's Encrypt and an
applicant to automate the process of verification and certificate
issuance.

uacme calls an external hook program with the tokens required for
domain authorization.  The hook program can be an executable, shell
script, perl script, python script, or any file that the operating
system can execute.

uacme is ACME challenge agnostic.  It provides the user or hook
program with all tokens and information required to complete any
challenge type but leaves the task of setting up and cleaning up the
challenge environment to the user or hook.

uacme also includes ualpn, a lightweight proxying tls-alpn-01
challenge responder compliant with RFC8737 and RFC8738.")
    (license license:gpl3+)))

(define-record-type* <acme-rsa-key>
  acme-rsa-key make-acme-rsa-key
  acme-rsa-key?
  (length acme-rsa-key-length
          (default 2048)
          (sanitize sanitize-acme-rsa-key-length)))

(define (sanitize-acme-rsa-key-length length)
  (if (and (zero? (remainder length 8))
           (>= length 2048)
           (<= length 8192))
      length
      (leave (G_ "RSA ACME key length ~a is invalid. It must be a multiple of 8 between 2048 and 8192.~%")
             length)))

(define-record-type* <acme-ecdsa-key>
  acme-ecdsa-key make-acme-ecdsa-key
  acme-ecdsa-key?
  (length acme-ecdsa-key-length
          (default 256)
          (sanitize sanitize-acme-ecdsa-key-length)))

(define (sanitize-acme-ecdsa-key-length length)
  (if (memv length (list 256 384))
      length
      (leave (G_ "ECDSA ACME key length ~a is invalid. It must either be 256 or 384.~%")
             length)))

(define (acme-key-length key)
  (cond
   ((acme-rsa-key? key)
    (acme-rsa-key-length key))
   ((acme-ecdsa-key? key)
    (acme-ecdsa-key-length key))))

(define %letsencrypt-production-url
  "https://acme-v02.api.letsencrypt.org/directory")

(define %letsencrypt-staging-url
  "https://acme-staging-v02.api.letsencrypt.org/directory")

(define-record-type* <acme-configuration>
  acme-configuration make-acme-configuration
  acme-configuration?
  this-acme-configuration
  (uacme acme-configuration-uacme
         (default uacme))
  (email acme-configuration-email
         (default #false))
  (acme-url acme-configuration-acme-url
            (default %letsencrypt-production-url))
  (state-directory acme-configuration-state-directory
                   (default "/var/lib/acme"))
  (http-01-challenge-directory acme-configuration-http-01-challenge-directory
                               (default "/var/run/acme/acme-challenge"))
  (http-01-authorization-hook acme-configuration-http-01-authorization-hook
                              (default (program-file "acme-http-01-authorization-hook"
                                                     (acme-http-01-webroot-authorization-gexp this-acme-configuration)))
                              (thunked))
  (http-01-cleanup-hook acme-configuration-http-01-cleanup-hook
                        (default (program-file "acme-http-01-cleanup-hook"
                                               (acme-http-01-webroot-cleanup-gexp this-acme-configuration)))
                        (thunked))
  (key acme-configuration-key
       (default (acme-ecdsa-key))
       (sanitize sanitize-acme-key))
  (certificates acme-configuration-certificates
                (default '())))

(define (sanitize-acme-key key)
  (if (or (acme-rsa-key? key)
          (acme-ecdsa-key? key))
      key
      (leave (G_ "Unsupported ACME key ~s~%")
             key)))

(define-record-type* <acme-certificate>
  acme-certificate make-acme-certificate?
  acme-certificate?
  (domains acme-certificate-domains)
  (deploy-hook acme-certificate-deploy-hook
               (default #false)))

(define (acme-http-01-webroot-authorization-gexp config)
  "Return a HTTP-01 authorization G-expression for the ACME service
described by @var{config}. The G-expression installs a challenge file
in the configured challenge directory."
  (match-record config <acme-configuration>
    (http-01-challenge-directory)
    #~(begin
        (use-modules (srfi srfi-26)
                     (ice-9 match))

        (match (command-line)
          ((_ identifier token auth)
           (let ((challenge-file (string-append #$http-01-challenge-directory "/" token)))
             (call-with-output-file challenge-file
               (cut display auth <>))))))))

(define (acme-http-01-webroot-cleanup-gexp config)
  "Return a HTTP-01 cleanup G-expression for the ACME service described
by @var{config}. The G-expression cleans up the challenge file
installed by the authorization hook in the configured challenge
directory."
  (match-record config <acme-configuration>
    (http-01-challenge-directory)
    #~(begin
        (use-modules (ice-9 match))

        (match (command-line)
          ((_ identifier token auth)
           (delete-file (string-append #$http-01-challenge-directory "/" token)))))))

(define (uacme-hook-gexp config)
  "Put together authorization and cleanup hooks described in
@var{config} into a hook suitable for passing to uacme."
  (match-record config <acme-configuration>
    (http-01-authorization-hook http-01-cleanup-hook)
    (with-imported-modules '((guix build utils))
      #~(begin
          (use-modules (guix build utils)
                       (ice-9 match))

          (match (command-line)
            ((_ "begin" "http-01" identifier token auth)
             (invoke #$http-01-authorization-hook
                     identifier token auth))
            ((_ (or "done" "failed") "http-01" identifier token auth)
             (invoke #$http-01-cleanup-hook
                     identifier token auth))
            ;; Decline other types of challenges.
            (_ (exit #false)))))))

(define %acme-accounts
  (list (user-account
         (name "acme")
         (group "acme")
         (system? #true)
         (comment "ACME user")
         (home-directory "/var/empty")
         (shell (file-append shadow "/sbin/nologin")))
        (user-group
         (name "acme")
         (system? #true))))

(define acme-activation
  (match-record-lambda <acme-configuration>
      (state-directory http-01-challenge-directory key certificates)
    (with-imported-modules '((guix build utils))
      #~(begin
          (use-modules (guix build utils)
                       (ice-9 match)
                       (srfi srfi-26))

          ;; Create state directory.
          (mkdir-p #$state-directory)

          ;; Create keys and self-signed certificates if none exist so
          ;; that services (such as nginx) that need the certificates
          ;; don't fail to start.
          (for-each (match-lambda
                      ((identifier certificate-template-file)
                       (let* ((key-file (string-append #$state-directory "/private/" identifier "/key.pem"))
                              (certificate-file (string-append #$state-directory "/" identifier "/cert.pem")))
                         (unless (and (file-exists? key-file)
                                      (file-exists? certificate-file))
                           (mkdir-p (dirname key-file))
                           (invoke #$(file-append gnutls "/bin/certtool")
                                   "--generate-privkey"
                                   "--key-type" #$(cond
                                                   ((acme-rsa-key? key) "rsa")
                                                   ((acme-ecdsa-key? key) "ecdsa"))
                                   "--bits" #$(number->string (acme-key-length key))
                                   "--outfile" key-file)
                           (chmod key-file #o400)
                           (mkdir-p (dirname certificate-file))
                           (invoke #$(file-append gnutls "/bin/certtool")
                                   "--generate-self-signed"
                                   "--template" certificate-template-file
                                   "--load-privkey" key-file
                                   "--outfile" certificate-file)))))
                    '#$(map (match-record-lambda <acme-certificate>
                                (domains)
                              (let ((identifier (first domains)))
                                (list identifier
                                      (plain-file "certtool-template.cfg"
                                                  (format #f
                                                          "cn = \"~a\"
expiration_days = 1
~{dns_name = \"~a\"~%~}
signing_key
encryption_key
tls_www_server
"
                                                          identifier
                                                          domains)))))
                            certificates))

          ;; Set ownership of state directory and its contents.
          (let ((user (getpw "acme")))
            (for-each (cut chown <> (passwd:uid user) (passwd:gid user))
                      (find-files #$state-directory #:directories? #t))

            ;; Create challenge directory and make it world readable,
            ;; but only writable by the acme user.
            (mkdir-p #$http-01-challenge-directory)
            (chown #$http-01-challenge-directory (passwd:uid user) (passwd:gid user))
            (chmod #$http-01-challenge-directory #o755))

          ;; Advise user about setup unless account key already
          ;; exists.
          (unless (file-exists? #$(string-append state-directory "/private/key.pem"))
            (display "
If this is the first time you are using the acme service, please
register by running `/usr/bin/acme register' and initialize your
certificates by running `/usr/bin/acme renew'

"))))))

(define* (acme-renew config #:key quiet?)
  "Return a G-expression that renews certificates described in
@var{config}. Unless @var{quiet?} is #true, be verbose."
  (match-record config <acme-configuration>
    (uacme email acme-url state-directory key certificates)
    (with-imported-modules '((guix build utils))
      #~(begin
          (use-modules (guix build utils))

          ;; Set path to TLS certificates for verification of HTTPS
          ;; servers.
          (setenv "SSL_CERT_FILE"
                  #$(file-append (profile
                                  (content (packages->manifest (list curl nss-certs))))
                                 "/etc/ssl/certs/ca-certificates.crt"))

          ;; Register ACME account if account does not already exist. We
          ;; assume the presence of an account key implies the existence
          ;; of an account.
          (unless (file-exists? #$(string-append state-directory "/private/key.pem"))
            (system* #$(file-append uacme "/bin/uacme")
                     "--verbose"
                     "--confdir" #$state-directory
                     "--acme-url" #$acme-url
                     "--yes"
                     "new"
                     #$@(if email
                            (list email)
                            (list))))

          ;; Renew configured certificates.
          (for-each (match-lambda
                      ((domains deploy-hook)
                       ;; uacme returns 0 on successful renewal, 1 when
                       ;; there is no need to renew, and 2 on other
                       ;; failures.
                       (case (status:exit-val
                              (apply system*
                                     #$(file-append uacme "/bin/uacme")
                                     "--confdir" #$state-directory
                                     "--acme-url" #$acme-url
                                     "--type" #$(cond
                                                 ((acme-rsa-key? key) "RSA")
                                                 ((acme-ecdsa-key? key) "EC"))
                                     "--bits" #$(number->string (acme-key-length key))
                                     "--hook" #$(program-file "uacme-hook"
                                                              (uacme-hook-gexp config))
                                     "issue"
                                     #$@(if quiet?
                                            (list)
                                            (list "--verbose"))
                                     domains))
                         ((0) (invoke deploy-hook))
                         ((2) (exit #false)))))
                    '#$(map (match-record-lambda <acme-certificate>
                                (domains deploy-hook)
                              (list domains
                                    deploy-hook))
                            certificates))))))

(define (acme-helper config)
  "Return a G-expression for a interactive ACME helper tool. Bake in
parameters described in @var{config}."
  (match-record config <acme-configuration>
    (uacme state-directory acme-url email)
    #~(begin
        (use-modules (ice-9 match))

        (match (command-line)
          ((command "renew")
           #$(acme-renew config))
          ((command _ ...)
           (format (current-error-port)
                   "Usage: ~a COMMAND

Valid COMMANDs are

renew: Issue or renew configured certificates

"
                   command)
           (exit #false))))))

(define (acme-helper-sudo-wrapper config)
  #~(begin
      (use-modules (ice-9 match))

      (match (command-line)
        ((_ arguments ...)
         (apply system*
                ;; We cannot refer to sudo in the store since that sudo
                ;; does not have the setuid bit set. See "(guix) Setuid
                ;; Programs".
                "/run/setuid-programs/sudo"
                "--user" "acme"
                "--group" "acme"
                #$(program-file "acme-helper" (acme-helper config))
                arguments)))))

(define (acme-special-files config)
  `(("/usr/bin/acme" ,(program-file "acme"
                                    (acme-helper-sudo-wrapper config)))))

(define (acme-cron-job config)
  ;; Attempt to renew certificates once a day, at a random minute
  ;; within the day. This helps even out the load on the ACME
  ;; server.
  #~(job '(next-minute-from (next-hour '(0))
                            (list (random (* 24 60))))
         #$(program-file "acme-renew-cron"
                         (acme-renew config #:quiet? #true))
         #:user "acme"))

(define (raise-to-top pred lst)
  "Reorder @var{lst} so that the first element that satisfies @var{pred}
is the first element. The order of the remaining elements is
unspecified. If no element matching @var{pred} is found, return
@code{#f}."
  (and (find pred lst)
       (append (find-tail pred lst)
               (take-while (negate pred) lst))))

(define acme-service-type
  (service-type
   (name 'acme)
   (description "Automatically fetch and renew ACME certificates.")
   (extensions (list (service-extension account-service-type
                                        (const %acme-accounts))
                     (service-extension activation-service-type
                                        acme-activation)
                     (service-extension special-files-service-type
                                        acme-special-files)
                     (service-extension mcron-service-type
                                        (compose list acme-cron-job))))
   (compose concatenate)
   (extend (lambda (config certificates)
             (acme-configuration
              (inherit config)
              (certificates
               ;; Append new certificates onto existing ones. When any
               ;; of the new certificates are for the same domains as
               ;; existing certificates, combine them into a single
               ;; certificate.
               (fold (lambda (certificate previous-certificates)
                       (cond
                        ((raise-to-top (lambda (this-certificate)
                                         (lset= string=?
                                                (acme-certificate-domains this-certificate)
                                                (acme-certificate-domains certificate)))
                                       previous-certificates)
                         => (match-lambda
                              ((matched-certificate other-certificates ...)
                               (cons (acme-certificate
                                      (inherit certificate)
                                      ;; Combine deploy hooks of the
                                      ;; two certificates.
                                      (deploy-hook
                                       (with-imported-modules '((guix build utils))
                                         #~(begin
                                             (use-modules (guix build utils))

                                             (invoke #$(acme-certificate-deploy-hook certificate))
                                             (invoke #$(acme-certificate-deploy-hook matched-certificate))))))
                                     other-certificates))))
                        (else
                         (cons certificate previous-certificates))))
                     (acme-configuration-certificates config)
                     certificates)))))
   (default-value (acme-configuration))))