system: Add boot file systems to /etc/fstab.
[jackhill/guix/guix.git] / gnu / services / dns.scm
CommitLineData
ba69e8f7
JL
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Julien Lepiller <julien@lepiller.eu>
8490a834 3;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
ba69e8f7
JL
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu services dns)
21 #:use-module (gnu services)
22 #:use-module (gnu services configuration)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu packages admin)
26 #:use-module (gnu packages dns)
27 #:use-module (guix packages)
28 #:use-module (guix records)
29 #:use-module (guix gexp)
30 #:use-module (srfi srfi-1)
97f6e913 31 #:use-module (srfi srfi-26)
ba69e8f7
JL
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:export (knot-service-type
37 knot-acl-configuration
38 knot-key-configuration
39 knot-keystore-configuration
40 knot-zone-configuration
41 knot-remote-configuration
42 knot-policy-configuration
43 knot-configuration
44 define-zone-entries
45 zone-file
97f6e913
SB
46 zone-entry
47
48 dnsmasq-service-type
8490a834
OP
49 dnsmasq-configuration
50
51 ddclient-service-type
52 ddclient-configuration))
ba69e8f7
JL
53
54;;;
55;;; Knot DNS.
56;;;
57
58(define-record-type* <knot-key-configuration>
59 knot-key-configuration make-knot-key-configuration
60 knot-key-configuration?
61 (id knot-key-configuration-id
62 (default ""))
63 (algorithm knot-key-configuration-algorithm
64 (default #f)); one of #f, or an algorithm name
65 (secret knot-key-configuration-secret
66 (default "")))
67
68(define-record-type* <knot-acl-configuration>
69 knot-acl-configuration make-knot-acl-configuration
70 knot-acl-configuration?
71 (id knot-acl-configuration-id
72 (default ""))
73 (address knot-acl-configuration-address
74 (default '()))
75 (key knot-acl-configuration-key
76 (default '()))
77 (action knot-acl-configuration-action
78 (default '()))
79 (deny? knot-acl-configuration-deny?
80 (default #f)))
81
82(define-record-type* <zone-entry>
83 zone-entry make-zone-entry
84 zone-entry?
85 (name zone-entry-name
86 (default "@"))
87 (ttl zone-entry-ttl
88 (default ""))
89 (class zone-entry-class
90 (default "IN"))
91 (type zone-entry-type
92 (default "A"))
93 (data zone-entry-data
94 (default "")))
95
96(define-record-type* <zone-file>
97 zone-file make-zone-file
98 zone-file?
99 (entries zone-file-entries
100 (default '()))
101 (origin zone-file-origin
102 (default ""))
103 (ns zone-file-ns
104 (default "ns"))
105 (mail zone-file-mail
106 (default "hostmaster"))
107 (serial zone-file-serial
108 (default 1))
109 (refresh zone-file-refresh
f3853a25 110 (default (* 2 24 3600)))
ba69e8f7 111 (retry zone-file-retry
f3853a25 112 (default (* 15 60)))
ba69e8f7 113 (expiry zone-file-expiry
f3853a25 114 (default (* 2 7 24 3600)))
ba69e8f7 115 (nx zone-file-nx
f3853a25 116 (default 3600)))
ba69e8f7
JL
117(define-record-type* <knot-keystore-configuration>
118 knot-keystore-configuration make-knot-keystore-configuration
119 knot-keystore-configuration?
120 (id knot-keystore-configuration-id
121 (default ""))
122 (backend knot-keystore-configuration-backend
123 (default 'pem))
124 (config knot-keystore-configuration-config
125 (default "/var/lib/knot/keys/keys")))
126
127(define-record-type* <knot-policy-configuration>
128 knot-policy-configuration make-knot-policy-configuration
129 knot-policy-configuration?
130 (id knot-policy-configuration-id
131 (default ""))
132 (keystore knot-policy-configuration-keystore
133 (default "default"))
134 (manual? knot-policy-configuration-manual?
135 (default #f))
136 (single-type-signing? knot-policy-configuration-single-type-signing?
137 (default #f))
138 (algorithm knot-policy-configuration-algorithm
139 (default "ecdsap256sha256"))
140 (ksk-size knot-policy-configuration-ksk-size
141 (default 256))
142 (zsk-size knot-policy-configuration-zsk-size
143 (default 256))
144 (dnskey-ttl knot-policy-configuration-dnskey-ttl
145 (default 'default))
146 (zsk-lifetime knot-policy-configuration-zsk-lifetime
f3853a25 147 (default (* 30 24 3600)))
ba69e8f7 148 (propagation-delay knot-policy-configuration-propagation-delay
f3853a25 149 (default (* 24 3600)))
ba69e8f7 150 (rrsig-lifetime knot-policy-configuration-rrsig-lifetime
f3853a25 151 (default (* 14 24 3600)))
ba69e8f7 152 (rrsig-refresh knot-policy-configuration-rrsig-refresh
f3853a25 153 (default (* 7 24 3600)))
ba69e8f7
JL
154 (nsec3? knot-policy-configuration-nsec3?
155 (default #f))
156 (nsec3-iterations knot-policy-configuration-nsec3-iterations
157 (default 5))
158 (nsec3-salt-length knot-policy-configuration-nsec3-salt-length
159 (default 8))
160 (nsec3-salt-lifetime knot-policy-configuration-nsec3-salt-lifetime
f3853a25 161 (default (* 30 24 3600))))
ba69e8f7
JL
162
163(define-record-type* <knot-zone-configuration>
164 knot-zone-configuration make-knot-zone-configuration
165 knot-zone-configuration?
166 (domain knot-zone-configuration-domain
167 (default ""))
168 (file knot-zone-configuration-file
169 (default "")) ; the file where this zone is saved.
170 (zone knot-zone-configuration-zone
171 (default (zone-file))) ; initial content of the zone file
172 (master knot-zone-configuration-master
173 (default '()))
174 (ddns-master knot-zone-configuration-ddns-master
175 (default #f))
176 (notify knot-zone-configuration-notify
177 (default '()))
178 (acl knot-zone-configuration-acl
179 (default '()))
180 (semantic-checks? knot-zone-configuration-semantic-checks?
181 (default #f))
182 (disable-any? knot-zone-configuration-disable-any?
183 (default #f))
184 (zonefile-sync knot-zone-configuration-zonefile-sync
185 (default 0))
186 (dnssec-policy knot-zone-configuration-dnssec-policy
187 (default #f))
188 (serial-policy knot-zone-configuration-serial-policy
189 (default 'increment)))
190
191(define-record-type* <knot-remote-configuration>
192 knot-remote-configuration make-knot-remote-configuration
193 knot-remote-configuration?
194 (id knot-remote-configuration-id
195 (default ""))
196 (address knot-remote-configuration-address
197 (default '()))
198 (via knot-remote-configuration-via
199 (default '()))
200 (key knot-remote-configuration-key
201 (default #f)))
202
203(define-record-type* <knot-configuration>
204 knot-configuration make-knot-configuration
205 knot-configuration?
206 (knot knot-configuration-knot
207 (default knot))
208 (run-directory knot-configuration-run-directory
209 (default "/var/run/knot"))
92eb600f
JL
210 (includes knot-configuration-includes
211 (default '()))
ba69e8f7
JL
212 (listen-v4 knot-configuration-listen-v4
213 (default "0.0.0.0"))
214 (listen-v6 knot-configuration-listen-v6
215 (default "::"))
216 (listen-port knot-configuration-listen-port
217 (default 53))
218 (keys knot-configuration-keys
219 (default '()))
220 (keystores knot-configuration-keystores
221 (default '()))
222 (acls knot-configuration-acls
223 (default '()))
224 (remotes knot-configuration-remotes
225 (default '()))
226 (policies knot-configuration-policies
227 (default '()))
228 (zones knot-configuration-zones
229 (default '())))
230
231(define-syntax define-zone-entries
232 (syntax-rules ()
233 ((_ id (name ttl class type data) ...)
234 (define id (list (make-zone-entry name ttl class type data) ...)))))
235
236(define (error-out msg)
237 (raise (condition (&message (message msg)))))
238
239(define (verify-knot-key-configuration key)
240 (unless (knot-key-configuration? key)
241 (error-out "keys must be a list of only knot-key-configuration."))
242 (let ((id (knot-key-configuration-id key)))
243 (unless (and (string? id) (not (equal? id "")))
244 (error-out "key id must be a non empty string.")))
245 (unless (memq '(#f hmac-md5 hmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512)
246 (knot-key-configuration-algorithm key))
247 (error-out "algorithm must be one of: #f, 'hmac-md5, 'hmac-sha1,
248'hmac-sha224, 'hmac-sha256, 'hmac-sha384 or 'hmac-sha512")))
249
250(define (verify-knot-keystore-configuration keystore)
251 (unless (knot-keystore-configuration? keystore)
252 (error-out "keystores must be a list of only knot-keystore-configuration."))
253 (let ((id (knot-keystore-configuration-id keystore)))
254 (unless (and (string? id) (not (equal? id "")))
255 (error-out "keystore id must be a non empty string.")))
256 (unless (memq '(pem pkcs11)
257 (knot-keystore-configuration-backend keystore))
258 (error-out "backend must be one of: 'pem or 'pkcs11")))
259
260(define (verify-knot-policy-configuration policy)
f3853a25 261 (unless (knot-policy-configuration? policy)
ba69e8f7
JL
262 (error-out "policies must be a list of only knot-policy-configuration."))
263 (let ((id (knot-policy-configuration-id policy)))
264 (unless (and (string? id) (not (equal? id "")))
265 (error-out "policy id must be a non empty string."))))
266
267(define (verify-knot-acl-configuration acl)
268 (unless (knot-acl-configuration? acl)
269 (error-out "acls must be a list of only knot-acl-configuration."))
270 (let ((id (knot-acl-configuration-id acl))
271 (address (knot-acl-configuration-address acl))
272 (key (knot-acl-configuration-key acl))
273 (action (knot-acl-configuration-action acl)))
274 (unless (and (string? id) (not (equal? id "")))
275 (error-out "acl id must be a non empty string."))
276 (unless (and (list? address)
277 (fold (lambda (x1 x2) (and (string? x1) (string? x2))) "" address))
278 (error-out "acl address must be a list of strings.")))
279 (unless (boolean? (knot-acl-configuration-deny? acl))
280 (error-out "deny? must be #t or #f.")))
281
282(define (verify-knot-zone-configuration zone)
283 (unless (knot-zone-configuration? zone)
284 (error-out "zones must be a list of only knot-zone-configuration."))
285 (let ((domain (knot-zone-configuration-domain zone)))
286 (unless (and (string? domain) (not (equal? domain "")))
287 (error-out "zone domain must be a non empty string."))))
288
289(define (verify-knot-remote-configuration remote)
290 (unless (knot-remote-configuration? remote)
291 (error-out "remotes must be a list of only knot-remote-configuration."))
292 (let ((id (knot-remote-configuration-id remote)))
293 (unless (and (string? id) (not (equal? id "")))
294 (error-out "remote id must be a non empty string."))))
295
296(define (verify-knot-configuration config)
297 (unless (package? (knot-configuration-knot config))
298 (error-out "knot configuration field must be a package."))
299 (unless (string? (knot-configuration-run-directory config))
300 (error-out "run-directory must be a string."))
92eb600f
JL
301 (unless (list? (knot-configuration-includes config))
302 (error-out "includes must be a list of strings or file-like objects."))
ba69e8f7
JL
303 (unless (list? (knot-configuration-keys config))
304 (error-out "keys must be a list of knot-key-configuration."))
305 (for-each (lambda (key) (verify-knot-key-configuration key))
306 (knot-configuration-keys config))
307 (unless (list? (knot-configuration-keystores config))
308 (error-out "keystores must be a list of knot-keystore-configuration."))
309 (for-each (lambda (keystore) (verify-knot-keystore-configuration keystore))
310 (knot-configuration-keystores config))
311 (unless (list? (knot-configuration-acls config))
312 (error-out "acls must be a list of knot-acl-configuration."))
313 (for-each (lambda (acl) (verify-knot-acl-configuration acl))
314 (knot-configuration-acls config))
315 (unless (list? (knot-configuration-zones config))
316 (error-out "zones must be a list of knot-zone-configuration."))
317 (for-each (lambda (zone) (verify-knot-zone-configuration zone))
318 (knot-configuration-zones config))
319 (unless (list? (knot-configuration-policies config))
320 (error-out "policies must be a list of knot-policy-configuration."))
321 (for-each (lambda (policy) (verify-knot-policy-configuration policy))
322 (knot-configuration-policies config))
323 (unless (list? (knot-configuration-remotes config))
324 (error-out "remotes must be a list of knot-remote-configuration."))
325 (for-each (lambda (remote) (verify-knot-remote-configuration remote))
326 (knot-configuration-remotes config))
327 #t)
328
329(define (format-string-list l)
330 "Formats a list of string in YAML"
331 (if (eq? l '())
332 ""
333 (let ((l (reverse l)))
334 (string-append
335 "["
336 (fold (lambda (x1 x2)
337 (string-append (if (symbol? x1) (symbol->string x1) x1) ", "
338 (if (symbol? x2) (symbol->string x2) x2)))
e379d1b5 339 (if (symbol? (car l)) (symbol->string (car l)) (car l)) (cdr l))
ba69e8f7
JL
340 "]"))))
341
342(define (knot-acl-config acls)
343 (with-output-to-string
344 (lambda ()
345 (for-each
346 (lambda (acl-config)
347 (let ((id (knot-acl-configuration-id acl-config))
348 (address (knot-acl-configuration-address acl-config))
349 (key (knot-acl-configuration-key acl-config))
350 (action (knot-acl-configuration-action acl-config))
351 (deny? (knot-acl-configuration-deny? acl-config)))
352 (format #t " - id: ~a\n" id)
353 (unless (eq? address '())
354 (format #t " address: ~a\n" (format-string-list address)))
355 (unless (eq? key '())
356 (format #t " key: ~a\n" (format-string-list key)))
357 (unless (eq? action '())
358 (format #t " action: ~a\n" (format-string-list action)))
359 (format #t " deny: ~a\n" (if deny? "on" "off"))))
360 acls))))
361
362(define (knot-key-config keys)
363 (with-output-to-string
364 (lambda ()
365 (for-each
366 (lambda (key-config)
367 (let ((id (knot-key-configuration-id key-config))
368 (algorithm (knot-key-configuration-algorithm key-config))
369 (secret (knot-key-configuration-secret key-config)))
370 (format #t " - id: ~a\n" id)
371 (if algorithm
372 (format #t " algorithm: ~a\n" (symbol->string algorithm)))
373 (format #t " secret: ~a\n" secret)))
374 keys))))
375
376(define (knot-keystore-config keystores)
377 (with-output-to-string
378 (lambda ()
379 (for-each
380 (lambda (keystore-config)
381 (let ((id (knot-keystore-configuration-id keystore-config))
382 (backend (knot-keystore-configuration-backend keystore-config))
383 (config (knot-keystore-configuration-config keystore-config)))
384 (format #t " - id: ~a\n" id)
385 (format #t " backend: ~a\n" (symbol->string backend))
386 (format #t " config: \"~a\"\n" config)))
387 keystores))))
388
389(define (knot-policy-config policies)
390 (with-output-to-string
391 (lambda ()
392 (for-each
393 (lambda (policy-config)
394 (let ((id (knot-policy-configuration-id policy-config))
395 (keystore (knot-policy-configuration-keystore policy-config))
396 (manual? (knot-policy-configuration-manual? policy-config))
397 (single-type-signing? (knot-policy-configuration-single-type-signing?
398 policy-config))
399 (algorithm (knot-policy-configuration-algorithm policy-config))
400 (ksk-size (knot-policy-configuration-ksk-size policy-config))
401 (zsk-size (knot-policy-configuration-zsk-size policy-config))
402 (dnskey-ttl (knot-policy-configuration-dnskey-ttl policy-config))
403 (zsk-lifetime (knot-policy-configuration-zsk-lifetime policy-config))
404 (propagation-delay (knot-policy-configuration-propagation-delay
405 policy-config))
406 (rrsig-lifetime (knot-policy-configuration-rrsig-lifetime
407 policy-config))
408 (nsec3? (knot-policy-configuration-nsec3? policy-config))
409 (nsec3-iterations (knot-policy-configuration-nsec3-iterations
410 policy-config))
411 (nsec3-salt-length (knot-policy-configuration-nsec3-salt-length
412 policy-config))
413 (nsec3-salt-lifetime (knot-policy-configuration-nsec3-salt-lifetime
414 policy-config)))
415 (format #t " - id: ~a\n" id)
416 (format #t " keystore: ~a\n" keystore)
417 (format #t " manual: ~a\n" (if manual? "on" "off"))
418 (format #t " single-type-signing: ~a\n" (if single-type-signing?
419 "on" "off"))
420 (format #t " algorithm: ~a\n" algorithm)
421 (format #t " ksk-size: ~a\n" (number->string ksk-size))
422 (format #t " zsk-size: ~a\n" (number->string zsk-size))
423 (unless (eq? dnskey-ttl 'default)
424 (format #t " dnskey-ttl: ~a\n" dnskey-ttl))
425 (format #t " zsk-lifetime: ~a\n" zsk-lifetime)
426 (format #t " propagation-delay: ~a\n" propagation-delay)
427 (format #t " rrsig-lifetime: ~a\n" rrsig-lifetime)
428 (format #t " nsec3: ~a\n" (if nsec3? "on" "off"))
429 (format #t " nsec3-iterations: ~a\n"
430 (number->string nsec3-iterations))
431 (format #t " nsec3-salt-length: ~a\n"
432 (number->string nsec3-salt-length))
433 (format #t " nsec3-salt-lifetime: ~a\n" nsec3-salt-lifetime)))
434 policies))))
435
436(define (knot-remote-config remotes)
437 (with-output-to-string
438 (lambda ()
439 (for-each
440 (lambda (remote-config)
441 (let ((id (knot-remote-configuration-id remote-config))
442 (address (knot-remote-configuration-address remote-config))
443 (via (knot-remote-configuration-via remote-config))
444 (key (knot-remote-configuration-key remote-config)))
445 (format #t " - id: ~a\n" id)
446 (unless (eq? address '())
447 (format #t " address: ~a\n" (format-string-list address)))
448 (unless (eq? via '())
449 (format #t " via: ~a\n" (format-string-list via)))
450 (if key
451 (format #t " key: ~a\n" key))))
452 remotes))))
453
454(define (serialize-zone-entries entries)
455 (with-output-to-string
456 (lambda ()
457 (for-each
458 (lambda (entry)
459 (let ((name (zone-entry-name entry))
460 (ttl (zone-entry-ttl entry))
461 (class (zone-entry-class entry))
462 (type (zone-entry-type entry))
463 (data (zone-entry-data entry)))
464 (format #t "~a ~a ~a ~a ~a\n" name ttl class type data)))
465 entries))))
466
467(define (serialize-zone-file zone domain)
468 (computed-file (string-append domain ".zone")
469 #~(begin
470 (call-with-output-file #$output
471 (lambda (port)
472 (format port "$ORIGIN ~a.\n"
473 #$(zone-file-origin zone))
474 (format port "@ IN SOA ~a ~a (~a ~a ~a ~a ~a)\n"
475 #$(zone-file-ns zone)
476 #$(zone-file-mail zone)
477 #$(zone-file-serial zone)
478 #$(zone-file-refresh zone)
479 #$(zone-file-retry zone)
480 #$(zone-file-expiry zone)
481 #$(zone-file-nx zone))
482 (format port "~a\n"
483 #$(serialize-zone-entries (zone-file-entries zone))))))))
484
485(define (knot-zone-config zone)
486 (let ((content (knot-zone-configuration-zone zone)))
487 #~(with-output-to-string
488 (lambda ()
489 (let ((domain #$(knot-zone-configuration-domain zone))
490 (file #$(knot-zone-configuration-file zone))
491 (master (list #$@(knot-zone-configuration-master zone)))
492 (ddns-master #$(knot-zone-configuration-ddns-master zone))
493 (notify (list #$@(knot-zone-configuration-notify zone)))
494 (acl (list #$@(knot-zone-configuration-acl zone)))
495 (semantic-checks? #$(knot-zone-configuration-semantic-checks? zone))
496 (disable-any? #$(knot-zone-configuration-disable-any? zone))
497 (dnssec-policy #$(knot-zone-configuration-dnssec-policy zone))
498 (serial-policy '#$(knot-zone-configuration-serial-policy zone)))
499 (format #t " - domain: ~a\n" domain)
500 (if (eq? master '())
501 ;; This server is a master
502 (if (equal? file "")
503 (format #t " file: ~a\n"
504 #$(serialize-zone-file content
505 (knot-zone-configuration-domain zone)))
506 (format #t " file: ~a\n" file))
507 ;; This server is a slave (has masters)
508 (begin
509 (format #t " master: ~a\n"
510 #$(format-string-list
511 (knot-zone-configuration-master zone)))
512 (if ddns-master (format #t " ddns-master ~a\n" ddns-master))))
513 (unless (eq? notify '())
514 (format #t " notify: ~a\n"
515 #$(format-string-list
516 (knot-zone-configuration-notify zone))))
517 (unless (eq? acl '())
518 (format #t " acl: ~a\n"
519 #$(format-string-list
520 (knot-zone-configuration-acl zone))))
521 (format #t " semantic-checks: ~a\n" (if semantic-checks? "on" "off"))
522 (format #t " disable-any: ~a\n" (if disable-any? "on" "off"))
523 (if dnssec-policy
524 (begin
525 (format #t " dnssec-signing: on\n")
526 (format #t " dnssec-policy: ~a\n" dnssec-policy)))
527 (format #t " serial-policy: ~a\n"
528 (symbol->string serial-policy)))))))
529
530(define (knot-config-file config)
531 (verify-knot-configuration config)
532 (computed-file "knot.conf"
533 #~(begin
534 (call-with-output-file #$output
535 (lambda (port)
92eb600f
JL
536 (if (knot-configuration-includes config)
537 (for-each (lambda (inc)
538 (format port "include: ~a\n" inc))
539 (knot-configuration-includes config)))
ba69e8f7
JL
540 (format port "server:\n")
541 (format port " rundir: ~a\n" #$(knot-configuration-run-directory config))
542 (format port " user: knot\n")
543 (format port " listen: ~a@~a\n"
544 #$(knot-configuration-listen-v4 config)
545 #$(knot-configuration-listen-port config))
546 (format port " listen: ~a@~a\n"
547 #$(knot-configuration-listen-v6 config)
548 #$(knot-configuration-listen-port config))
549 (format port "\nkey:\n")
550 (format port #$(knot-key-config (knot-configuration-keys config)))
551 (format port "\nkeystore:\n")
552 (format port #$(knot-keystore-config (knot-configuration-keystores config)))
553 (format port "\nacl:\n")
554 (format port #$(knot-acl-config (knot-configuration-acls config)))
555 (format port "\nremote:\n")
556 (format port #$(knot-remote-config (knot-configuration-remotes config)))
557 (format port "\npolicy:\n")
558 (format port #$(knot-policy-config (knot-configuration-policies config)))
559 (unless #$(eq? (knot-configuration-zones config) '())
560 (format port "\nzone:\n")
561 (format port "~a\n"
562 (string-concatenate
563 (list #$@(map knot-zone-config
564 (knot-configuration-zones config)))))))))))
565
566(define %knot-accounts
567 (list (user-group (name "knot") (system? #t))
568 (user-account
569 (name "knot")
570 (group "knot")
571 (system? #t)
572 (comment "knot dns server user")
573 (home-directory "/var/empty")
574 (shell (file-append shadow "/sbin/nologin")))))
575
576(define (knot-activation config)
577 #~(begin
578 (use-modules (guix build utils))
579 (define (mkdir-p/perms directory owner perms)
580 (mkdir-p directory)
581 (chown directory (passwd:uid owner) (passwd:gid owner))
582 (chmod directory perms))
583 (mkdir-p/perms #$(knot-configuration-run-directory config)
584 (getpwnam "knot") #o755)
585 (mkdir-p/perms "/var/lib/knot" (getpwnam "knot") #o755)
586 (mkdir-p/perms "/var/lib/knot/keys" (getpwnam "knot") #o755)
587 (mkdir-p/perms "/var/lib/knot/keys/keys" (getpwnam "knot") #o755)))
588
589(define (knot-shepherd-service config)
590 (let* ((config-file (knot-config-file config))
591 (knot (knot-configuration-knot config)))
592 (list (shepherd-service
593 (documentation "Run the Knot DNS daemon.")
594 (provision '(knot dns))
595 (requirement '(networking))
596 (start #~(make-forkexec-constructor
597 (list (string-append #$knot "/sbin/knotd")
598 "-c" #$config-file)))
599 (stop #~(make-kill-destructor))))))
600
601(define knot-service-type
602 (service-type (name 'knot)
603 (extensions
604 (list (service-extension shepherd-root-service-type
605 knot-shepherd-service)
606 (service-extension activation-service-type
607 knot-activation)
608 (service-extension account-service-type
609 (const %knot-accounts))))))
97f6e913
SB
610
611\f
612;;;
613;;; Dnsmasq.
614;;;
615
616(define-record-type* <dnsmasq-configuration>
617 dnsmasq-configuration make-dnsmasq-configuration
618 dnsmasq-configuration?
619 (package dnsmasq-configuration-package
620 (default dnsmasq)) ;package
621 (no-hosts? dnsmasq-configuration-no-hosts?
622 (default #f)) ;boolean
623 (port dnsmasq-configuration-port
624 (default 53)) ;integer
625 (local-service? dnsmasq-configuration-local-service?
626 (default #t)) ;boolean
627 (listen-addresses dnsmasq-configuration-listen-address
628 (default '())) ;list of string
629 (resolv-file dnsmasq-configuration-resolv-file
630 (default "/etc/resolv.conf")) ;string
631 (no-resolv? dnsmasq-configuration-no-resolv?
632 (default #f)) ;boolean
633 (servers dnsmasq-configuration-servers
634 (default '())) ;list of string
635 (cache-size dnsmasq-configuration-cache-size
636 (default 150)) ;integer
c061eb58
SB
637 (negative-cache? dnsmasq-configuration-negative-cache?
638 (default #t))) ;boolean
97f6e913
SB
639
640(define dnsmasq-shepherd-service
641 (match-lambda
642 (($ <dnsmasq-configuration> package
643 no-hosts?
644 port local-service? listen-addresses
645 resolv-file no-resolv? servers
c061eb58 646 cache-size negative-cache?)
97f6e913
SB
647 (shepherd-service
648 (provision '(dnsmasq))
649 (requirement '(networking))
650 (documentation "Run the dnsmasq DNS server.")
651 (start #~(make-forkexec-constructor
652 '(#$(file-append package "/sbin/dnsmasq")
653 "--keep-in-foreground"
654 "--pid-file=/run/dnsmasq.pid"
655 #$@(if no-hosts?
656 '("--no-hosts")
657 '())
658 #$(format #f "--port=~a" port)
659 #$@(if local-service?
660 '("--local-service")
661 '())
662 #$@(map (cut format #f "--listen-address=~a" <>)
663 listen-addresses)
664 #$(format #f "--resolv-file=~a" resolv-file)
665 #$@(if no-resolv?
666 '("--no-resolv")
667 '())
668 #$@(map (cut format #f "--server=~a" <>)
669 servers)
670 #$(format #f "--cache-size=~a" cache-size)
c061eb58
SB
671 #$@(if negative-cache?
672 '()
673 '("--no-negcache")))
97f6e913
SB
674 #:pid-file "/run/dnsmasq.pid"))
675 (stop #~(make-kill-destructor))))))
676
677(define dnsmasq-service-type
678 (service-type
679 (name 'dnsmasq)
680 (extensions
681 (list (service-extension shepherd-root-service-type
0d4c2d35
SB
682 (compose list dnsmasq-shepherd-service))))
683 (default-value (dnsmasq-configuration))
684 (description "Run the dnsmasq DNS server.")))
8490a834
OP
685
686\f
687;;;
688;;; ddclient
689;;;
690
691(define (uglify-field-name field-name)
692 (string-delete #\? (symbol->string field-name)))
693
694(define (serialize-field field-name val)
9325533b
OP
695 (when (not (member field-name '(group secret-file user)))
696 (format #t "~a=~a\n" (uglify-field-name field-name) val)))
8490a834
OP
697
698(define (serialize-boolean field-name val)
699 (serialize-field field-name (if val "yes" "no")))
700
701(define (serialize-integer field-name val)
702 (serialize-field field-name (number->string val)))
703
704(define (serialize-string field-name val)
705 (if (and (string? val) (string=? val ""))
706 ""
707 (serialize-field field-name val)))
708
709(define (serialize-list field-name val)
710 (if (null? val) "" (serialize-field field-name (string-join val))))
711
712(define (serialize-extra-options extra-options)
713 (string-join extra-options "\n" 'suffix))
714
715(define-configuration ddclient-configuration
716 (ddclient
717 (package ddclient)
718 "The ddclient package.")
719 (daemon
720 (integer 300)
721 "The period after which ddclient will retry to check IP and domain name.")
722 (syslog
723 (boolean #t)
724 "Use syslog for the output.")
725 (mail
726 (string "root")
727 "Mail to user.")
728 (mail-failure
729 (string "root")
730 "Mail failed update to user.")
731 (pid
732 (string "/var/run/ddclient/ddclient.pid")
733 "The ddclient PID file.")
734 (ssl
735 (boolean #t)
736 "Enable SSL support.")
737 (user
738 (string "ddclient")
739 "Specifies the user name or ID that is used when running ddclient
740program.")
741 (group
742 (string "ddclient")
743 "Group of the user who will run the ddclient program.")
744 (secret-file
745 (string "/etc/ddclient/secrets.conf")
746 "Secret file which will be appended to @file{ddclient.conf} file. This
747file contains credentials for use by ddclient. You are expected to create it
748manually.")
749 (extra-options
750 (list '())
751 "Extra options will be appended to @file{ddclient.conf} file."))
752
753(define (ddclient-account config)
754 "Return the user accounts and user groups for CONFIG."
755 (let ((ddclient-user (ddclient-configuration-user config))
756 (ddclient-group (ddclient-configuration-group config)))
757 (list (user-group
758 (name ddclient-group)
759 (system? #t))
760 (user-account
761 (name ddclient-user)
762 (system? #t)
763 (group ddclient-group)
764 (comment "ddclientd privilege separation user")
765 (home-directory (string-append "/var/run/" ddclient-user))))))
766
767(define (ddclient-activation config)
768 "Return the activation GEXP for CONFIG."
769 (with-imported-modules '((guix build utils)
770 (ice-9 rdelim))
771 #~(begin
772 (use-modules (guix build utils)
773 (ice-9 rdelim))
774 (let ((ddclient-user
7f860a8b 775 (passwd:uid (getpw #$(ddclient-configuration-user config))))
8490a834 776 (ddclient-group
7f860a8b 777 (passwd:gid (getpw #$(ddclient-configuration-group config))))
8490a834
OP
778 (ddclient-secret-file
779 #$(ddclient-configuration-secret-file config)))
780 ;; 'ddclient' complains about ddclient.conf file permissions, which
781 ;; rules out /gnu/store. Thus we copy the ddclient.conf to /etc.
782 (for-each (lambda (dir)
783 (mkdir-p dir)
784 (chmod dir #o700)
785 (chown dir ddclient-user ddclient-group))
786 '("/var/cache/ddclient" "/var/run/ddclient"
787 "/etc/ddclient"))
788 (with-output-to-file "/etc/ddclient/ddclient.conf"
789 (lambda ()
790 (display
791 (string-append
792 "# Generated by 'ddclient-service'.\n\n"
793 #$(with-output-to-string
794 (lambda ()
795 (serialize-configuration config
796 ddclient-configuration-fields)))
797 (if (string-null? ddclient-secret-file)
798 ""
799 (format #f "\n\n# Appended from '~a'.\n\n~a"
800 ddclient-secret-file
801 (with-input-from-file ddclient-secret-file
802 read-string)))))))
803 (chmod "/etc/ddclient/ddclient.conf" #o600)
804 (chown "/etc/ddclient/ddclient.conf"
805 ddclient-user ddclient-group)))))
806
807(define (ddclient-shepherd-service config)
808 "Return a <shepherd-service> for ddclient with CONFIG."
809 (let ((ddclient (ddclient-configuration-ddclient config))
810 (ddclient-pid (ddclient-configuration-pid config))
811 (ddclient-user (ddclient-configuration-user config))
812 (ddclient-group (ddclient-configuration-group config)))
813 (list (shepherd-service
814 (provision '(ddclient))
815 (documentation "Run ddclient daemon.")
816 (start #~(make-forkexec-constructor
817 (list #$(file-append ddclient "/bin/ddclient")
818 "-foreground"
819 "-file" "/etc/ddclient/ddclient.conf")
820 #:pid-file #$ddclient-pid
821 #:environment-variables
822 (list "SSL_CERT_DIR=/run/current-system/profile\
823/etc/ssl/certs"
824 "SSL_CERT_FILE=/run/current-system/profile\
825/etc/ssl/certs/ca-certificates.crt")
826 #:user #$ddclient-user
827 #:group #$ddclient-group))
828 (stop #~(make-kill-destructor))))))
829
830(define ddclient-service-type
831 (service-type
832 (name 'ddclient)
833 (extensions
834 (list (service-extension account-service-type
835 ddclient-account)
836 (service-extension shepherd-root-service-type
837 ddclient-shepherd-service)
838 (service-extension activation-service-type
839 ddclient-activation)))
840 (default-value (ddclient-configuration))
841 (description "Configure address updating utility for dynamic DNS services,
842ddclient.")))
843
844(define (generate-ddclient-documentation)
845 (generate-documentation
846 `((ddclient-configuration ,ddclient-configuration-fields))
847 'ddclient-configuration))