1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Julien Lepiller <julien@lepiller.eu>
3 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
4 ;;; Copyright © 2020 Pierre Langlois <pierre.langlois@gmx.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu services dns)
22 #:use-module (gnu services)
23 #:use-module (gnu services configuration)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu system shadow)
26 #:use-module (gnu packages admin)
27 #:use-module (gnu packages dns)
28 #:use-module (guix packages)
29 #:use-module (guix records)
30 #:use-module (guix gexp)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 match)
36 #:use-module (ice-9 regex)
37 #:export (knot-service-type
38 knot-acl-configuration
39 knot-key-configuration
40 knot-keystore-configuration
41 knot-zone-configuration
42 knot-remote-configuration
43 knot-policy-configuration
49 knot-resolver-service-type
50 knot-resolver-configuration
56 ddclient-configuration))
62 (define-record-type* <knot-key-configuration>
63 knot-key-configuration make-knot-key-configuration
64 knot-key-configuration?
65 (id knot-key-configuration-id
67 (algorithm knot-key-configuration-algorithm
68 (default #f)); one of #f, or an algorithm name
69 (secret knot-key-configuration-secret
72 (define-record-type* <knot-acl-configuration>
73 knot-acl-configuration make-knot-acl-configuration
74 knot-acl-configuration?
75 (id knot-acl-configuration-id
77 (address knot-acl-configuration-address
79 (key knot-acl-configuration-key
81 (action knot-acl-configuration-action
83 (deny? knot-acl-configuration-deny?
86 (define-record-type* <zone-entry>
87 zone-entry make-zone-entry
93 (class zone-entry-class
100 (define-record-type* <zone-file>
101 zone-file make-zone-file
103 (entries zone-file-entries
105 (origin zone-file-origin
110 (default "hostmaster"))
111 (serial zone-file-serial
113 (refresh zone-file-refresh
114 (default (* 2 24 3600)))
115 (retry zone-file-retry
117 (expiry zone-file-expiry
118 (default (* 2 7 24 3600)))
121 (define-record-type* <knot-keystore-configuration>
122 knot-keystore-configuration make-knot-keystore-configuration
123 knot-keystore-configuration?
124 (id knot-keystore-configuration-id
126 (backend knot-keystore-configuration-backend
128 (config knot-keystore-configuration-config
129 (default "/var/lib/knot/keys/keys")))
131 (define-record-type* <knot-policy-configuration>
132 knot-policy-configuration make-knot-policy-configuration
133 knot-policy-configuration?
134 (id knot-policy-configuration-id
136 (keystore knot-policy-configuration-keystore
138 (manual? knot-policy-configuration-manual?
140 (single-type-signing? knot-policy-configuration-single-type-signing?
142 (algorithm knot-policy-configuration-algorithm
143 (default "ecdsap256sha256"))
144 (ksk-size knot-policy-configuration-ksk-size
146 (zsk-size knot-policy-configuration-zsk-size
148 (dnskey-ttl knot-policy-configuration-dnskey-ttl
150 (zsk-lifetime knot-policy-configuration-zsk-lifetime
151 (default (* 30 24 3600)))
152 (propagation-delay knot-policy-configuration-propagation-delay
153 (default (* 24 3600)))
154 (rrsig-lifetime knot-policy-configuration-rrsig-lifetime
155 (default (* 14 24 3600)))
156 (rrsig-refresh knot-policy-configuration-rrsig-refresh
157 (default (* 7 24 3600)))
158 (nsec3? knot-policy-configuration-nsec3?
160 (nsec3-iterations knot-policy-configuration-nsec3-iterations
162 (nsec3-salt-length knot-policy-configuration-nsec3-salt-length
164 (nsec3-salt-lifetime knot-policy-configuration-nsec3-salt-lifetime
165 (default (* 30 24 3600))))
167 (define-record-type* <knot-zone-configuration>
168 knot-zone-configuration make-knot-zone-configuration
169 knot-zone-configuration?
170 (domain knot-zone-configuration-domain
172 (file knot-zone-configuration-file
173 (default "")) ; the file where this zone is saved.
174 (zone knot-zone-configuration-zone
175 (default (zone-file))) ; initial content of the zone file
176 (master knot-zone-configuration-master
178 (ddns-master knot-zone-configuration-ddns-master
180 (notify knot-zone-configuration-notify
182 (acl knot-zone-configuration-acl
184 (semantic-checks? knot-zone-configuration-semantic-checks?
186 (disable-any? knot-zone-configuration-disable-any?
188 (zonefile-sync knot-zone-configuration-zonefile-sync
190 (zonefile-load knot-zone-configuration-zonefile-load
192 (journal-content knot-zone-configuration-journal-content
194 (max-journal-usage knot-zone-configuration-max-journal-usage
196 (max-journal-depth knot-zone-configuration-max-journal-depth
198 (max-zone-size knot-zone-configuration-max-zone-size
200 (dnssec-policy knot-zone-configuration-dnssec-policy
202 (serial-policy knot-zone-configuration-serial-policy
203 (default 'increment)))
205 (define-record-type* <knot-remote-configuration>
206 knot-remote-configuration make-knot-remote-configuration
207 knot-remote-configuration?
208 (id knot-remote-configuration-id
210 (address knot-remote-configuration-address
212 (via knot-remote-configuration-via
214 (key knot-remote-configuration-key
217 (define-record-type* <knot-configuration>
218 knot-configuration make-knot-configuration
220 (knot knot-configuration-knot
222 (run-directory knot-configuration-run-directory
223 (default "/var/run/knot"))
224 (includes knot-configuration-includes
226 (listen-v4 knot-configuration-listen-v4
228 (listen-v6 knot-configuration-listen-v6
230 (listen-port knot-configuration-listen-port
232 (keys knot-configuration-keys
234 (keystores knot-configuration-keystores
236 (acls knot-configuration-acls
238 (remotes knot-configuration-remotes
240 (policies knot-configuration-policies
242 (zones knot-configuration-zones
245 (define-syntax define-zone-entries
247 ((_ id (name ttl class type data) ...)
248 (define id (list (make-zone-entry name ttl class type data) ...)))))
250 (define (error-out msg)
251 (raise (condition (&message (message msg)))))
253 (define (verify-knot-key-configuration key)
254 (unless (knot-key-configuration? key)
255 (error-out "keys must be a list of only knot-key-configuration."))
256 (let ((id (knot-key-configuration-id key)))
257 (unless (and (string? id) (not (equal? id "")))
258 (error-out "key id must be a non empty string.")))
259 (unless (memq '(#f hmac-md5 hmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512)
260 (knot-key-configuration-algorithm key))
261 (error-out "algorithm must be one of: #f, 'hmac-md5, 'hmac-sha1,
262 'hmac-sha224, 'hmac-sha256, 'hmac-sha384 or 'hmac-sha512")))
264 (define (verify-knot-keystore-configuration keystore)
265 (unless (knot-keystore-configuration? keystore)
266 (error-out "keystores must be a list of only knot-keystore-configuration."))
267 (let ((id (knot-keystore-configuration-id keystore)))
268 (unless (and (string? id) (not (equal? id "")))
269 (error-out "keystore id must be a non empty string.")))
270 (unless (memq '(pem pkcs11)
271 (knot-keystore-configuration-backend keystore))
272 (error-out "backend must be one of: 'pem or 'pkcs11")))
274 (define (verify-knot-policy-configuration policy)
275 (unless (knot-policy-configuration? policy)
276 (error-out "policies must be a list of only knot-policy-configuration."))
277 (let ((id (knot-policy-configuration-id policy)))
278 (unless (and (string? id) (not (equal? id "")))
279 (error-out "policy id must be a non empty string."))))
281 (define (verify-knot-acl-configuration acl)
282 (unless (knot-acl-configuration? acl)
283 (error-out "acls must be a list of only knot-acl-configuration."))
284 (let ((id (knot-acl-configuration-id acl))
285 (address (knot-acl-configuration-address acl))
286 (key (knot-acl-configuration-key acl))
287 (action (knot-acl-configuration-action acl)))
288 (unless (and (string? id) (not (equal? id "")))
289 (error-out "acl id must be a non empty string."))
290 (unless (and (list? address)
291 (fold (lambda (x1 x2) (and (string? x1) (string? x2))) "" address))
292 (error-out "acl address must be a list of strings.")))
293 (unless (boolean? (knot-acl-configuration-deny? acl))
294 (error-out "deny? must be #t or #f.")))
296 (define (verify-knot-zone-configuration zone)
297 (unless (knot-zone-configuration? zone)
298 (error-out "zones must be a list of only knot-zone-configuration."))
299 (let ((domain (knot-zone-configuration-domain zone)))
300 (unless (and (string? domain) (not (equal? domain "")))
301 (error-out "zone domain must be a non empty string."))))
303 (define (verify-knot-remote-configuration remote)
304 (unless (knot-remote-configuration? remote)
305 (error-out "remotes must be a list of only knot-remote-configuration."))
306 (let ((id (knot-remote-configuration-id remote)))
307 (unless (and (string? id) (not (equal? id "")))
308 (error-out "remote id must be a non empty string."))))
310 (define (verify-knot-configuration config)
311 (unless (package? (knot-configuration-knot config))
312 (error-out "knot configuration field must be a package."))
313 (unless (string? (knot-configuration-run-directory config))
314 (error-out "run-directory must be a string."))
315 (unless (list? (knot-configuration-includes config))
316 (error-out "includes must be a list of strings or file-like objects."))
317 (unless (list? (knot-configuration-keys config))
318 (error-out "keys must be a list of knot-key-configuration."))
319 (for-each (lambda (key) (verify-knot-key-configuration key))
320 (knot-configuration-keys config))
321 (unless (list? (knot-configuration-keystores config))
322 (error-out "keystores must be a list of knot-keystore-configuration."))
323 (for-each (lambda (keystore) (verify-knot-keystore-configuration keystore))
324 (knot-configuration-keystores config))
325 (unless (list? (knot-configuration-acls config))
326 (error-out "acls must be a list of knot-acl-configuration."))
327 (for-each (lambda (acl) (verify-knot-acl-configuration acl))
328 (knot-configuration-acls config))
329 (unless (list? (knot-configuration-zones config))
330 (error-out "zones must be a list of knot-zone-configuration."))
331 (for-each (lambda (zone) (verify-knot-zone-configuration zone))
332 (knot-configuration-zones config))
333 (unless (list? (knot-configuration-policies config))
334 (error-out "policies must be a list of knot-policy-configuration."))
335 (for-each (lambda (policy) (verify-knot-policy-configuration policy))
336 (knot-configuration-policies config))
337 (unless (list? (knot-configuration-remotes config))
338 (error-out "remotes must be a list of knot-remote-configuration."))
339 (for-each (lambda (remote) (verify-knot-remote-configuration remote))
340 (knot-configuration-remotes config))
343 (define (format-string-list l)
344 "Formats a list of string in YAML"
347 (let ((l (reverse l)))
350 (fold (lambda (x1 x2)
351 (string-append (if (symbol? x1) (symbol->string x1) x1) ", "
352 (if (symbol? x2) (symbol->string x2) x2)))
353 (if (symbol? (car l)) (symbol->string (car l)) (car l)) (cdr l))
356 (define (knot-acl-config acls)
357 (with-output-to-string
361 (let ((id (knot-acl-configuration-id acl-config))
362 (address (knot-acl-configuration-address acl-config))
363 (key (knot-acl-configuration-key acl-config))
364 (action (knot-acl-configuration-action acl-config))
365 (deny? (knot-acl-configuration-deny? acl-config)))
366 (format #t " - id: ~a\n" id)
367 (unless (eq? address '())
368 (format #t " address: ~a\n" (format-string-list address)))
369 (unless (eq? key '())
370 (format #t " key: ~a\n" (format-string-list key)))
371 (unless (eq? action '())
372 (format #t " action: ~a\n" (format-string-list action)))
373 (format #t " deny: ~a\n" (if deny? "on" "off"))))
376 (define (knot-key-config keys)
377 (with-output-to-string
381 (let ((id (knot-key-configuration-id key-config))
382 (algorithm (knot-key-configuration-algorithm key-config))
383 (secret (knot-key-configuration-secret key-config)))
384 (format #t " - id: ~a\n" id)
386 (format #t " algorithm: ~a\n" (symbol->string algorithm)))
387 (format #t " secret: ~a\n" secret)))
390 (define (knot-keystore-config keystores)
391 (with-output-to-string
394 (lambda (keystore-config)
395 (let ((id (knot-keystore-configuration-id keystore-config))
396 (backend (knot-keystore-configuration-backend keystore-config))
397 (config (knot-keystore-configuration-config keystore-config)))
398 (format #t " - id: ~a\n" id)
399 (format #t " backend: ~a\n" (symbol->string backend))
400 (format #t " config: \"~a\"\n" config)))
403 (define (knot-policy-config policies)
404 (with-output-to-string
407 (lambda (policy-config)
408 (let ((id (knot-policy-configuration-id policy-config))
409 (keystore (knot-policy-configuration-keystore policy-config))
410 (manual? (knot-policy-configuration-manual? policy-config))
411 (single-type-signing? (knot-policy-configuration-single-type-signing?
413 (algorithm (knot-policy-configuration-algorithm policy-config))
414 (ksk-size (knot-policy-configuration-ksk-size policy-config))
415 (zsk-size (knot-policy-configuration-zsk-size policy-config))
416 (dnskey-ttl (knot-policy-configuration-dnskey-ttl policy-config))
417 (zsk-lifetime (knot-policy-configuration-zsk-lifetime policy-config))
418 (propagation-delay (knot-policy-configuration-propagation-delay
420 (rrsig-lifetime (knot-policy-configuration-rrsig-lifetime
422 (nsec3? (knot-policy-configuration-nsec3? policy-config))
423 (nsec3-iterations (knot-policy-configuration-nsec3-iterations
425 (nsec3-salt-length (knot-policy-configuration-nsec3-salt-length
427 (nsec3-salt-lifetime (knot-policy-configuration-nsec3-salt-lifetime
429 (format #t " - id: ~a\n" id)
430 (format #t " keystore: ~a\n" keystore)
431 (format #t " manual: ~a\n" (if manual? "on" "off"))
432 (format #t " single-type-signing: ~a\n" (if single-type-signing?
434 (format #t " algorithm: ~a\n" algorithm)
435 (format #t " ksk-size: ~a\n" (number->string ksk-size))
436 (format #t " zsk-size: ~a\n" (number->string zsk-size))
437 (unless (eq? dnskey-ttl 'default)
438 (format #t " dnskey-ttl: ~a\n" dnskey-ttl))
439 (format #t " zsk-lifetime: ~a\n" zsk-lifetime)
440 (format #t " propagation-delay: ~a\n" propagation-delay)
441 (format #t " rrsig-lifetime: ~a\n" rrsig-lifetime)
442 (format #t " nsec3: ~a\n" (if nsec3? "on" "off"))
443 (format #t " nsec3-iterations: ~a\n"
444 (number->string nsec3-iterations))
445 (format #t " nsec3-salt-length: ~a\n"
446 (number->string nsec3-salt-length))
447 (format #t " nsec3-salt-lifetime: ~a\n" nsec3-salt-lifetime)))
450 (define (knot-remote-config remotes)
451 (with-output-to-string
454 (lambda (remote-config)
455 (let ((id (knot-remote-configuration-id remote-config))
456 (address (knot-remote-configuration-address remote-config))
457 (via (knot-remote-configuration-via remote-config))
458 (key (knot-remote-configuration-key remote-config)))
459 (format #t " - id: ~a\n" id)
460 (unless (eq? address '())
461 (format #t " address: ~a\n" (format-string-list address)))
462 (unless (eq? via '())
463 (format #t " via: ~a\n" (format-string-list via)))
465 (format #t " key: ~a\n" key))))
468 (define (serialize-zone-entries entries)
469 (with-output-to-string
473 (let ((name (zone-entry-name entry))
474 (ttl (zone-entry-ttl entry))
475 (class (zone-entry-class entry))
476 (type (zone-entry-type entry))
477 (data (zone-entry-data entry)))
478 (format #t "~a ~a ~a ~a ~a\n" name ttl class type data)))
481 (define (serialize-zone-file zone domain)
482 (computed-file (string-append domain ".zone")
484 (call-with-output-file #$output
486 (format port "$ORIGIN ~a.\n"
487 #$(zone-file-origin zone))
488 (format port "@ IN SOA ~a ~a (~a ~a ~a ~a ~a)\n"
489 #$(zone-file-ns zone)
490 #$(zone-file-mail zone)
491 #$(zone-file-serial zone)
492 #$(zone-file-refresh zone)
493 #$(zone-file-retry zone)
494 #$(zone-file-expiry zone)
495 #$(zone-file-nx zone))
497 #$(serialize-zone-entries (zone-file-entries zone))))))))
499 (define (knot-zone-config zone)
500 (let ((content (knot-zone-configuration-zone zone)))
501 #~(with-output-to-string
503 (let ((domain #$(knot-zone-configuration-domain zone))
504 (file #$(knot-zone-configuration-file zone))
505 (master (list #$@(knot-zone-configuration-master zone)))
506 (ddns-master #$(knot-zone-configuration-ddns-master zone))
507 (notify (list #$@(knot-zone-configuration-notify zone)))
508 (acl (list #$@(knot-zone-configuration-acl zone)))
509 (semantic-checks? #$(knot-zone-configuration-semantic-checks? zone))
510 (disable-any? #$(knot-zone-configuration-disable-any? zone))
511 (zonefile-sync #$(knot-zone-configuration-zonefile-sync zone))
512 (zonefile-load '#$(knot-zone-configuration-zonefile-load zone))
513 (journal-content #$(knot-zone-configuration-journal-content zone))
514 (max-journal-usage #$(knot-zone-configuration-max-journal-usage zone))
515 (max-journal-depth #$(knot-zone-configuration-max-journal-depth zone))
516 (max-zone-size #$(knot-zone-configuration-max-zone-size zone))
517 (dnssec-policy #$(knot-zone-configuration-dnssec-policy zone))
518 (serial-policy '#$(knot-zone-configuration-serial-policy zone)))
519 (format #t " - domain: ~a\n" domain)
521 ;; This server is a master
523 (format #t " file: ~a\n"
524 #$(serialize-zone-file content
525 (knot-zone-configuration-domain zone)))
526 (format #t " file: ~a\n" file))
527 ;; This server is a slave (has masters)
529 (format #t " master: ~a\n"
530 #$(format-string-list
531 (knot-zone-configuration-master zone)))
532 (if ddns-master (format #t " ddns-master ~a\n" ddns-master))))
533 (unless (eq? notify '())
534 (format #t " notify: ~a\n"
535 #$(format-string-list
536 (knot-zone-configuration-notify zone))))
537 (unless (eq? acl '())
538 (format #t " acl: ~a\n"
539 #$(format-string-list
540 (knot-zone-configuration-acl zone))))
541 (format #t " semantic-checks: ~a\n" (if semantic-checks? "on" "off"))
542 (format #t " disable-any: ~a\n" (if disable-any? "on" "off"))
544 (format #t " zonefile-sync: ~a\n" zonefile-sync))
546 (format #t " zonefile-load: ~a\n"
547 (symbol->string zonefile-load)))
549 (format #t " journal-content: ~a\n"
550 (symbol->string journal-content)))
551 (if max-journal-usage
552 (format #t " max-journal-usage: ~a\n" max-journal-usage))
553 (if max-journal-depth
554 (format #t " max-journal-depth: ~a\n" max-journal-depth))
556 (format #t " max-zone-size: ~a\n" max-zone-size))
559 (format #t " dnssec-signing: on\n")
560 (format #t " dnssec-policy: ~a\n" dnssec-policy)))
561 (format #t " serial-policy: ~a\n"
562 (symbol->string serial-policy)))))))
564 (define (knot-config-file config)
565 (verify-knot-configuration config)
566 (computed-file "knot.conf"
568 (call-with-output-file #$output
570 (for-each (lambda (inc)
571 (format port "include: ~a\n" inc))
572 '#$(knot-configuration-includes config))
573 (format port "server:\n")
574 (format port " rundir: ~a\n" #$(knot-configuration-run-directory config))
575 (format port " user: knot\n")
576 (format port " listen: ~a@~a\n"
577 #$(knot-configuration-listen-v4 config)
578 #$(knot-configuration-listen-port config))
579 (format port " listen: ~a@~a\n"
580 #$(knot-configuration-listen-v6 config)
581 #$(knot-configuration-listen-port config))
582 (format port "\nkey:\n")
583 (format port #$(knot-key-config (knot-configuration-keys config)))
584 (format port "\nkeystore:\n")
585 (format port #$(knot-keystore-config (knot-configuration-keystores config)))
586 (format port "\nacl:\n")
587 (format port #$(knot-acl-config (knot-configuration-acls config)))
588 (format port "\nremote:\n")
589 (format port #$(knot-remote-config (knot-configuration-remotes config)))
590 (format port "\npolicy:\n")
591 (format port #$(knot-policy-config (knot-configuration-policies config)))
592 (unless #$(eq? (knot-configuration-zones config) '())
593 (format port "\nzone:\n")
596 (list #$@(map knot-zone-config
597 (knot-configuration-zones config)))))))))))
599 (define %knot-accounts
600 (list (user-group (name "knot") (system? #t))
605 (comment "knot dns server user")
606 (home-directory "/var/empty")
607 (shell (file-append shadow "/sbin/nologin")))))
609 (define (knot-activation config)
611 (use-modules (guix build utils))
612 (define (mkdir-p/perms directory owner perms)
614 (chown directory (passwd:uid owner) (passwd:gid owner))
615 (chmod directory perms))
616 (mkdir-p/perms #$(knot-configuration-run-directory config)
617 (getpwnam "knot") #o755)
618 (mkdir-p/perms "/var/lib/knot" (getpwnam "knot") #o755)
619 (mkdir-p/perms "/var/lib/knot/keys" (getpwnam "knot") #o755)
620 (mkdir-p/perms "/var/lib/knot/keys/keys" (getpwnam "knot") #o755)))
622 (define (knot-shepherd-service config)
623 (let* ((config-file (knot-config-file config))
624 (knot (knot-configuration-knot config)))
625 (list (shepherd-service
626 (documentation "Run the Knot DNS daemon.")
627 (provision '(knot dns))
628 (requirement '(networking))
629 (start #~(make-forkexec-constructor
630 (list (string-append #$knot "/sbin/knotd")
631 "-c" #$config-file)))
632 (stop #~(make-kill-destructor))))))
634 (define knot-service-type
635 (service-type (name 'knot)
637 (list (service-extension shepherd-root-service-type
638 knot-shepherd-service)
639 (service-extension activation-service-type
641 (service-extension account-service-type
642 (const %knot-accounts))))
644 "Run @uref{https://www.knot-dns.cz/, Knot}, an authoritative
645 name server for the @acronym{DNS, Domain Name System}.")))
652 (define-record-type* <knot-resolver-configuration>
653 knot-resolver-configuration
654 make-knot-resolver-configuration
655 knot-resolver-configuration?
656 (package knot-resolver-configuration-package
657 (default knot-resolver))
658 (kresd-config-file knot-resolver-kresd-config-file
659 (default %kresd.conf))
660 (garbage-collection-interval knot-resolver-garbage-collection-interval
664 (plain-file "kresd.conf" "-- -*- mode: lua -*-
665 trust_anchors.add_file('/var/cache/knot-resolver/root.keys')
666 net = { '127.0.0.1', '::1' }
667 user('knot-resolver', 'knot-resolver')
668 modules = { 'hints > iterate', 'stats', 'predict' }
669 cache.size = 100 * MB
672 (define %knot-resolver-accounts
674 (name "knot-resolver")
677 (name "knot-resolver")
678 (group "knot-resolver")
680 (home-directory "/var/cache/knot-resolver")
681 (shell (file-append shadow "/sbin/nologin")))))
683 (define (knot-resolver-activation config)
685 (use-modules (guix build utils))
686 (let ((rundir "/var/cache/knot-resolver")
687 (owner (getpwnam "knot-resolver")))
689 (chown rundir (passwd:uid owner) (passwd:gid owner)))))
691 (define knot-resolver-shepherd-services
693 (($ <knot-resolver-configuration> package
695 garbage-collection-interval)
699 (requirement '(networking))
700 (documentation "Run the Knot Resolver daemon.")
701 (start #~(make-forkexec-constructor
702 '(#$(file-append package "/sbin/kresd")
703 "-c" #$kresd-config-file "-n"
704 "/var/cache/knot-resolver")))
705 (stop #~(make-kill-destructor)))
707 (provision '(kres-cache-gc))
708 (requirement '(user-processes))
709 (documentation "Run the Knot Resolver Garbage Collector daemon.")
710 (start #~(make-forkexec-constructor
711 '(#$(file-append package "/sbin/kres-cache-gc")
712 "-d" #$(number->string garbage-collection-interval)
713 "-c" "/var/cache/knot-resolver")
714 #:user "knot-resolver"
715 #:group "knot-resolver"))
716 (stop #~(make-kill-destructor)))))))
718 (define knot-resolver-service-type
720 (name 'knot-resolver)
722 (list (service-extension shepherd-root-service-type
723 knot-resolver-shepherd-services)
724 (service-extension activation-service-type
725 knot-resolver-activation)
726 (service-extension account-service-type
727 (const %knot-resolver-accounts))))
728 (default-value (knot-resolver-configuration))
729 (description "Run the Knot DNS Resolver.")))
736 (define-record-type* <dnsmasq-configuration>
737 dnsmasq-configuration make-dnsmasq-configuration
738 dnsmasq-configuration?
739 (package dnsmasq-configuration-package
740 (default dnsmasq)) ;package
741 (no-hosts? dnsmasq-configuration-no-hosts?
742 (default #f)) ;boolean
743 (port dnsmasq-configuration-port
744 (default 53)) ;integer
745 (local-service? dnsmasq-configuration-local-service?
746 (default #t)) ;boolean
747 (listen-addresses dnsmasq-configuration-listen-address
748 (default '())) ;list of string
749 (resolv-file dnsmasq-configuration-resolv-file
750 (default "/etc/resolv.conf")) ;string
751 (no-resolv? dnsmasq-configuration-no-resolv?
752 (default #f)) ;boolean
753 (servers dnsmasq-configuration-servers
754 (default '())) ;list of string
755 (addresses dnsmasq-configuration-addresses
756 (default '())) ;list of string
757 (cache-size dnsmasq-configuration-cache-size
758 (default 150)) ;integer
759 (negative-cache? dnsmasq-configuration-negative-cache?
760 (default #t)) ;boolean
761 (tftp-enable? dnsmasq-configuration-tftp-enable?
762 (default #f)) ;boolean
763 (tftp-no-fail? dnsmasq-configuration-tftp-no-fail?
764 (default #f)) ;boolean
765 (tftp-single-port? dnsmasq-configuration-tftp-single-port?
766 (default #f)) ;boolean
767 (tftp-secure? dnsmasq-tftp-secure?
768 (default #f)) ;boolean
769 (tftp-max dnsmasq-tftp-max
770 (default #f)) ;integer
771 (tftp-mtu dnsmasq-tftp-mtu
772 (default #f)) ;integer
773 (tftp-no-blocksize? dnsmasq-tftp-no-blocksize?
774 (default #f)) ;boolean
775 (tftp-lowercase? dnsmasq-tftp-lowercase?
776 (default #f)) ;boolean
777 (tftp-port-range dnsmasq-tftp-port-range
778 (default #f)) ;string
779 (tftp-root dnsmasq-tftp-root
780 (default "/var/empty,lo")) ;string
781 (tftp-unique-root dnsmasq-tftp-unique-root
782 (default #f))) ;"" or "ip" or "mac"
784 (define dnsmasq-shepherd-service
786 (($ <dnsmasq-configuration> package
788 port local-service? listen-addresses
789 resolv-file no-resolv? servers
790 addresses cache-size negative-cache?
791 tftp-enable? tftp-no-fail?
792 tftp-single-port? tftp-secure?
793 tftp-max tftp-mtu tftp-no-blocksize?
794 tftp-lowercase? tftp-port-range
795 tftp-root tftp-unique-root)
797 (provision '(dnsmasq))
798 (requirement '(networking))
799 (documentation "Run the dnsmasq DNS server.")
800 (start #~(make-forkexec-constructor
801 '(#$(file-append package "/sbin/dnsmasq")
802 "--keep-in-foreground"
803 "--pid-file=/run/dnsmasq.pid"
807 #$(format #f "--port=~a" port)
808 #$@(if local-service?
811 #$@(map (cut format #f "--listen-address=~a" <>)
813 #$(format #f "--resolv-file=~a" resolv-file)
817 #$@(map (cut format #f "--server=~a" <>)
819 #$@(map (cut format #f "--address=~a" <>)
821 #$(format #f "--cache-size=~a" cache-size)
822 #$@(if negative-cache?
831 #$@(if tftp-single-port?
832 '("--tftp-single-port")
838 (list (format #f "--tftp-max=~a" tftp-max))
841 (list (format #f "--tftp-mtu=~a" tftp-mtu))
843 #$@(if tftp-no-blocksize?
844 '("--tftp-no-blocksize")
846 #$@(if tftp-lowercase?
847 '("--tftp-lowercase")
849 #$@(if tftp-port-range
850 (list (format #f "--tftp-port-range=~a"
854 (list (format #f "--tftp-root=~a" tftp-root))
856 #$@(if tftp-unique-root
858 (if (> (length tftp-unique-root) 0)
859 (format #f "--tftp-unique-root=~a" tftp-unique-root)
860 (format #f "--tftp-unique-root")))
862 #:pid-file "/run/dnsmasq.pid"))
863 (stop #~(make-kill-destructor))))))
865 (define dnsmasq-service-type
869 (list (service-extension shepherd-root-service-type
870 (compose list dnsmasq-shepherd-service))))
871 (default-value (dnsmasq-configuration))
872 (description "Run the dnsmasq DNS server.")))
879 (define (uglify-field-name field-name)
880 (string-delete #\? (symbol->string field-name)))
882 (define (serialize-field field-name val)
883 (when (not (member field-name '(group secret-file user)))
884 (format #t "~a=~a\n" (uglify-field-name field-name) val)))
886 (define (serialize-boolean field-name val)
887 (serialize-field field-name (if val "yes" "no")))
889 (define (serialize-integer field-name val)
890 (serialize-field field-name (number->string val)))
892 (define (serialize-string field-name val)
893 (if (and (string? val) (string=? val ""))
895 (serialize-field field-name val)))
897 (define (serialize-list field-name val)
898 (if (null? val) "" (serialize-field field-name (string-join val))))
900 (define (serialize-extra-options extra-options)
901 (string-join extra-options "\n" 'suffix))
903 (define-configuration ddclient-configuration
906 "The ddclient package.")
909 "The period after which ddclient will retry to check IP and domain name.")
912 "Use syslog for the output.")
918 "Mail failed update to user.")
920 (string "/var/run/ddclient/ddclient.pid")
921 "The ddclient PID file.")
924 "Enable SSL support.")
927 "Specifies the user name or ID that is used when running ddclient
931 "Group of the user who will run the ddclient program.")
933 (string "/etc/ddclient/secrets.conf")
934 "Secret file which will be appended to @file{ddclient.conf} file. This
935 file contains credentials for use by ddclient. You are expected to create it
939 "Extra options will be appended to @file{ddclient.conf} file."))
941 (define (ddclient-account config)
942 "Return the user accounts and user groups for CONFIG."
943 (let ((ddclient-user (ddclient-configuration-user config))
944 (ddclient-group (ddclient-configuration-group config)))
946 (name ddclient-group)
951 (group ddclient-group)
952 (comment "ddclientd privilege separation user")
953 (home-directory (string-append "/var/run/" ddclient-user))))))
955 (define (ddclient-activation config)
956 "Return the activation GEXP for CONFIG."
957 (with-imported-modules '((guix build utils)
960 (use-modules (guix build utils)
963 (passwd:uid (getpw #$(ddclient-configuration-user config))))
965 (passwd:gid (getpw #$(ddclient-configuration-group config))))
966 (ddclient-secret-file
967 #$(ddclient-configuration-secret-file config)))
968 ;; 'ddclient' complains about ddclient.conf file permissions, which
969 ;; rules out /gnu/store. Thus we copy the ddclient.conf to /etc.
970 (for-each (lambda (dir)
973 (chown dir ddclient-user ddclient-group))
974 '("/var/cache/ddclient" "/var/run/ddclient"
976 (with-output-to-file "/etc/ddclient/ddclient.conf"
980 "# Generated by 'ddclient-service'.\n\n"
981 #$(with-output-to-string
983 (serialize-configuration config
984 ddclient-configuration-fields)))
985 (if (string-null? ddclient-secret-file)
987 (format #f "\n\n# Appended from '~a'.\n\n~a"
989 (with-input-from-file ddclient-secret-file
991 (chmod "/etc/ddclient/ddclient.conf" #o600)
992 (chown "/etc/ddclient/ddclient.conf"
993 ddclient-user ddclient-group)))))
995 (define (ddclient-shepherd-service config)
996 "Return a <shepherd-service> for ddclient with CONFIG."
997 (let ((ddclient (ddclient-configuration-ddclient config))
998 (ddclient-pid (ddclient-configuration-pid config))
999 (ddclient-user (ddclient-configuration-user config))
1000 (ddclient-group (ddclient-configuration-group config)))
1001 (list (shepherd-service
1002 (provision '(ddclient))
1003 (documentation "Run ddclient daemon.")
1004 (start #~(make-forkexec-constructor
1005 (list #$(file-append ddclient "/bin/ddclient")
1007 "-file" "/etc/ddclient/ddclient.conf")
1008 #:pid-file #$ddclient-pid
1009 #:environment-variables
1010 (list "SSL_CERT_DIR=/run/current-system/profile\
1012 "SSL_CERT_FILE=/run/current-system/profile\
1013 /etc/ssl/certs/ca-certificates.crt")
1014 #:user #$ddclient-user
1015 #:group #$ddclient-group))
1016 (stop #~(make-kill-destructor))))))
1018 (define ddclient-service-type
1022 (list (service-extension account-service-type
1024 (service-extension shepherd-root-service-type
1025 ddclient-shepherd-service)
1026 (service-extension activation-service-type
1027 ddclient-activation)))
1028 (default-value (ddclient-configuration))
1029 (description "Configure address updating utility for dynamic DNS services,
1032 (define (generate-ddclient-documentation)
1033 (generate-documentation
1034 `((ddclient-configuration ,ddclient-configuration-fields))
1035 'ddclient-configuration))