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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
31 #:use-module (srfi srfi-26)
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
52 ddclient-configuration))
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
63 (algorithm knot-key-configuration-algorithm
64 (default #f)); one of #f, or an algorithm name
65 (secret knot-key-configuration-secret
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
73 (address knot-acl-configuration-address
75 (key knot-acl-configuration-key
77 (action knot-acl-configuration-action
79 (deny? knot-acl-configuration-deny?
82 (define-record-type* <zone-entry>
83 zone-entry make-zone-entry
89 (class zone-entry-class
96 (define-record-type* <zone-file>
97 zone-file make-zone-file
99 (entries zone-file-entries
101 (origin zone-file-origin
106 (default "hostmaster"))
107 (serial zone-file-serial
109 (refresh zone-file-refresh
110 (default (* 2 24 3600)))
111 (retry zone-file-retry
113 (expiry zone-file-expiry
114 (default (* 2 7 24 3600)))
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
122 (backend knot-keystore-configuration-backend
124 (config knot-keystore-configuration-config
125 (default "/var/lib/knot/keys/keys")))
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
132 (keystore knot-policy-configuration-keystore
134 (manual? knot-policy-configuration-manual?
136 (single-type-signing? knot-policy-configuration-single-type-signing?
138 (algorithm knot-policy-configuration-algorithm
139 (default "ecdsap256sha256"))
140 (ksk-size knot-policy-configuration-ksk-size
142 (zsk-size knot-policy-configuration-zsk-size
144 (dnskey-ttl knot-policy-configuration-dnskey-ttl
146 (zsk-lifetime knot-policy-configuration-zsk-lifetime
147 (default (* 30 24 3600)))
148 (propagation-delay knot-policy-configuration-propagation-delay
149 (default (* 24 3600)))
150 (rrsig-lifetime knot-policy-configuration-rrsig-lifetime
151 (default (* 14 24 3600)))
152 (rrsig-refresh knot-policy-configuration-rrsig-refresh
153 (default (* 7 24 3600)))
154 (nsec3? knot-policy-configuration-nsec3?
156 (nsec3-iterations knot-policy-configuration-nsec3-iterations
158 (nsec3-salt-length knot-policy-configuration-nsec3-salt-length
160 (nsec3-salt-lifetime knot-policy-configuration-nsec3-salt-lifetime
161 (default (* 30 24 3600))))
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
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
174 (ddns-master knot-zone-configuration-ddns-master
176 (notify knot-zone-configuration-notify
178 (acl knot-zone-configuration-acl
180 (semantic-checks? knot-zone-configuration-semantic-checks?
182 (disable-any? knot-zone-configuration-disable-any?
184 (zonefile-sync knot-zone-configuration-zonefile-sync
186 (zonefile-load knot-zone-configuration-zonefile-load
188 (journal-content knot-zone-configuration-journal-content
190 (max-journal-usage knot-zone-configuration-max-journal-usage
192 (max-journal-depth knot-zone-configuration-max-journal-depth
194 (max-zone-size knot-zone-configuration-max-zone-size
196 (dnssec-policy knot-zone-configuration-dnssec-policy
198 (serial-policy knot-zone-configuration-serial-policy
199 (default 'increment)))
201 (define-record-type* <knot-remote-configuration>
202 knot-remote-configuration make-knot-remote-configuration
203 knot-remote-configuration?
204 (id knot-remote-configuration-id
206 (address knot-remote-configuration-address
208 (via knot-remote-configuration-via
210 (key knot-remote-configuration-key
213 (define-record-type* <knot-configuration>
214 knot-configuration make-knot-configuration
216 (knot knot-configuration-knot
218 (run-directory knot-configuration-run-directory
219 (default "/var/run/knot"))
220 (includes knot-configuration-includes
222 (listen-v4 knot-configuration-listen-v4
224 (listen-v6 knot-configuration-listen-v6
226 (listen-port knot-configuration-listen-port
228 (keys knot-configuration-keys
230 (keystores knot-configuration-keystores
232 (acls knot-configuration-acls
234 (remotes knot-configuration-remotes
236 (policies knot-configuration-policies
238 (zones knot-configuration-zones
241 (define-syntax define-zone-entries
243 ((_ id (name ttl class type data) ...)
244 (define id (list (make-zone-entry name ttl class type data) ...)))))
246 (define (error-out msg)
247 (raise (condition (&message (message msg)))))
249 (define (verify-knot-key-configuration key)
250 (unless (knot-key-configuration? key)
251 (error-out "keys must be a list of only knot-key-configuration."))
252 (let ((id (knot-key-configuration-id key)))
253 (unless (and (string? id) (not (equal? id "")))
254 (error-out "key id must be a non empty string.")))
255 (unless (memq '(#f hmac-md5 hmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512)
256 (knot-key-configuration-algorithm key))
257 (error-out "algorithm must be one of: #f, 'hmac-md5, 'hmac-sha1,
258 'hmac-sha224, 'hmac-sha256, 'hmac-sha384 or 'hmac-sha512")))
260 (define (verify-knot-keystore-configuration keystore)
261 (unless (knot-keystore-configuration? keystore)
262 (error-out "keystores must be a list of only knot-keystore-configuration."))
263 (let ((id (knot-keystore-configuration-id keystore)))
264 (unless (and (string? id) (not (equal? id "")))
265 (error-out "keystore id must be a non empty string.")))
266 (unless (memq '(pem pkcs11)
267 (knot-keystore-configuration-backend keystore))
268 (error-out "backend must be one of: 'pem or 'pkcs11")))
270 (define (verify-knot-policy-configuration policy)
271 (unless (knot-policy-configuration? policy)
272 (error-out "policies must be a list of only knot-policy-configuration."))
273 (let ((id (knot-policy-configuration-id policy)))
274 (unless (and (string? id) (not (equal? id "")))
275 (error-out "policy id must be a non empty string."))))
277 (define (verify-knot-acl-configuration acl)
278 (unless (knot-acl-configuration? acl)
279 (error-out "acls must be a list of only knot-acl-configuration."))
280 (let ((id (knot-acl-configuration-id acl))
281 (address (knot-acl-configuration-address acl))
282 (key (knot-acl-configuration-key acl))
283 (action (knot-acl-configuration-action acl)))
284 (unless (and (string? id) (not (equal? id "")))
285 (error-out "acl id must be a non empty string."))
286 (unless (and (list? address)
287 (fold (lambda (x1 x2) (and (string? x1) (string? x2))) "" address))
288 (error-out "acl address must be a list of strings.")))
289 (unless (boolean? (knot-acl-configuration-deny? acl))
290 (error-out "deny? must be #t or #f.")))
292 (define (verify-knot-zone-configuration zone)
293 (unless (knot-zone-configuration? zone)
294 (error-out "zones must be a list of only knot-zone-configuration."))
295 (let ((domain (knot-zone-configuration-domain zone)))
296 (unless (and (string? domain) (not (equal? domain "")))
297 (error-out "zone domain must be a non empty string."))))
299 (define (verify-knot-remote-configuration remote)
300 (unless (knot-remote-configuration? remote)
301 (error-out "remotes must be a list of only knot-remote-configuration."))
302 (let ((id (knot-remote-configuration-id remote)))
303 (unless (and (string? id) (not (equal? id "")))
304 (error-out "remote id must be a non empty string."))))
306 (define (verify-knot-configuration config)
307 (unless (package? (knot-configuration-knot config))
308 (error-out "knot configuration field must be a package."))
309 (unless (string? (knot-configuration-run-directory config))
310 (error-out "run-directory must be a string."))
311 (unless (list? (knot-configuration-includes config))
312 (error-out "includes must be a list of strings or file-like objects."))
313 (unless (list? (knot-configuration-keys config))
314 (error-out "keys must be a list of knot-key-configuration."))
315 (for-each (lambda (key) (verify-knot-key-configuration key))
316 (knot-configuration-keys config))
317 (unless (list? (knot-configuration-keystores config))
318 (error-out "keystores must be a list of knot-keystore-configuration."))
319 (for-each (lambda (keystore) (verify-knot-keystore-configuration keystore))
320 (knot-configuration-keystores config))
321 (unless (list? (knot-configuration-acls config))
322 (error-out "acls must be a list of knot-acl-configuration."))
323 (for-each (lambda (acl) (verify-knot-acl-configuration acl))
324 (knot-configuration-acls config))
325 (unless (list? (knot-configuration-zones config))
326 (error-out "zones must be a list of knot-zone-configuration."))
327 (for-each (lambda (zone) (verify-knot-zone-configuration zone))
328 (knot-configuration-zones config))
329 (unless (list? (knot-configuration-policies config))
330 (error-out "policies must be a list of knot-policy-configuration."))
331 (for-each (lambda (policy) (verify-knot-policy-configuration policy))
332 (knot-configuration-policies config))
333 (unless (list? (knot-configuration-remotes config))
334 (error-out "remotes must be a list of knot-remote-configuration."))
335 (for-each (lambda (remote) (verify-knot-remote-configuration remote))
336 (knot-configuration-remotes config))
339 (define (format-string-list l)
340 "Formats a list of string in YAML"
343 (let ((l (reverse l)))
346 (fold (lambda (x1 x2)
347 (string-append (if (symbol? x1) (symbol->string x1) x1) ", "
348 (if (symbol? x2) (symbol->string x2) x2)))
349 (if (symbol? (car l)) (symbol->string (car l)) (car l)) (cdr l))
352 (define (knot-acl-config acls)
353 (with-output-to-string
357 (let ((id (knot-acl-configuration-id acl-config))
358 (address (knot-acl-configuration-address acl-config))
359 (key (knot-acl-configuration-key acl-config))
360 (action (knot-acl-configuration-action acl-config))
361 (deny? (knot-acl-configuration-deny? acl-config)))
362 (format #t " - id: ~a\n" id)
363 (unless (eq? address '())
364 (format #t " address: ~a\n" (format-string-list address)))
365 (unless (eq? key '())
366 (format #t " key: ~a\n" (format-string-list key)))
367 (unless (eq? action '())
368 (format #t " action: ~a\n" (format-string-list action)))
369 (format #t " deny: ~a\n" (if deny? "on" "off"))))
372 (define (knot-key-config keys)
373 (with-output-to-string
377 (let ((id (knot-key-configuration-id key-config))
378 (algorithm (knot-key-configuration-algorithm key-config))
379 (secret (knot-key-configuration-secret key-config)))
380 (format #t " - id: ~a\n" id)
382 (format #t " algorithm: ~a\n" (symbol->string algorithm)))
383 (format #t " secret: ~a\n" secret)))
386 (define (knot-keystore-config keystores)
387 (with-output-to-string
390 (lambda (keystore-config)
391 (let ((id (knot-keystore-configuration-id keystore-config))
392 (backend (knot-keystore-configuration-backend keystore-config))
393 (config (knot-keystore-configuration-config keystore-config)))
394 (format #t " - id: ~a\n" id)
395 (format #t " backend: ~a\n" (symbol->string backend))
396 (format #t " config: \"~a\"\n" config)))
399 (define (knot-policy-config policies)
400 (with-output-to-string
403 (lambda (policy-config)
404 (let ((id (knot-policy-configuration-id policy-config))
405 (keystore (knot-policy-configuration-keystore policy-config))
406 (manual? (knot-policy-configuration-manual? policy-config))
407 (single-type-signing? (knot-policy-configuration-single-type-signing?
409 (algorithm (knot-policy-configuration-algorithm policy-config))
410 (ksk-size (knot-policy-configuration-ksk-size policy-config))
411 (zsk-size (knot-policy-configuration-zsk-size policy-config))
412 (dnskey-ttl (knot-policy-configuration-dnskey-ttl policy-config))
413 (zsk-lifetime (knot-policy-configuration-zsk-lifetime policy-config))
414 (propagation-delay (knot-policy-configuration-propagation-delay
416 (rrsig-lifetime (knot-policy-configuration-rrsig-lifetime
418 (nsec3? (knot-policy-configuration-nsec3? policy-config))
419 (nsec3-iterations (knot-policy-configuration-nsec3-iterations
421 (nsec3-salt-length (knot-policy-configuration-nsec3-salt-length
423 (nsec3-salt-lifetime (knot-policy-configuration-nsec3-salt-lifetime
425 (format #t " - id: ~a\n" id)
426 (format #t " keystore: ~a\n" keystore)
427 (format #t " manual: ~a\n" (if manual? "on" "off"))
428 (format #t " single-type-signing: ~a\n" (if single-type-signing?
430 (format #t " algorithm: ~a\n" algorithm)
431 (format #t " ksk-size: ~a\n" (number->string ksk-size))
432 (format #t " zsk-size: ~a\n" (number->string zsk-size))
433 (unless (eq? dnskey-ttl 'default)
434 (format #t " dnskey-ttl: ~a\n" dnskey-ttl))
435 (format #t " zsk-lifetime: ~a\n" zsk-lifetime)
436 (format #t " propagation-delay: ~a\n" propagation-delay)
437 (format #t " rrsig-lifetime: ~a\n" rrsig-lifetime)
438 (format #t " nsec3: ~a\n" (if nsec3? "on" "off"))
439 (format #t " nsec3-iterations: ~a\n"
440 (number->string nsec3-iterations))
441 (format #t " nsec3-salt-length: ~a\n"
442 (number->string nsec3-salt-length))
443 (format #t " nsec3-salt-lifetime: ~a\n" nsec3-salt-lifetime)))
446 (define (knot-remote-config remotes)
447 (with-output-to-string
450 (lambda (remote-config)
451 (let ((id (knot-remote-configuration-id remote-config))
452 (address (knot-remote-configuration-address remote-config))
453 (via (knot-remote-configuration-via remote-config))
454 (key (knot-remote-configuration-key remote-config)))
455 (format #t " - id: ~a\n" id)
456 (unless (eq? address '())
457 (format #t " address: ~a\n" (format-string-list address)))
458 (unless (eq? via '())
459 (format #t " via: ~a\n" (format-string-list via)))
461 (format #t " key: ~a\n" key))))
464 (define (serialize-zone-entries entries)
465 (with-output-to-string
469 (let ((name (zone-entry-name entry))
470 (ttl (zone-entry-ttl entry))
471 (class (zone-entry-class entry))
472 (type (zone-entry-type entry))
473 (data (zone-entry-data entry)))
474 (format #t "~a ~a ~a ~a ~a\n" name ttl class type data)))
477 (define (serialize-zone-file zone domain)
478 (computed-file (string-append domain ".zone")
480 (call-with-output-file #$output
482 (format port "$ORIGIN ~a.\n"
483 #$(zone-file-origin zone))
484 (format port "@ IN SOA ~a ~a (~a ~a ~a ~a ~a)\n"
485 #$(zone-file-ns zone)
486 #$(zone-file-mail zone)
487 #$(zone-file-serial zone)
488 #$(zone-file-refresh zone)
489 #$(zone-file-retry zone)
490 #$(zone-file-expiry zone)
491 #$(zone-file-nx zone))
493 #$(serialize-zone-entries (zone-file-entries zone))))))))
495 (define (knot-zone-config zone)
496 (let ((content (knot-zone-configuration-zone zone)))
497 #~(with-output-to-string
499 (let ((domain #$(knot-zone-configuration-domain zone))
500 (file #$(knot-zone-configuration-file zone))
501 (master (list #$@(knot-zone-configuration-master zone)))
502 (ddns-master #$(knot-zone-configuration-ddns-master zone))
503 (notify (list #$@(knot-zone-configuration-notify zone)))
504 (acl (list #$@(knot-zone-configuration-acl zone)))
505 (semantic-checks? #$(knot-zone-configuration-semantic-checks? zone))
506 (disable-any? #$(knot-zone-configuration-disable-any? zone))
507 (zonefile-sync #$(knot-zone-configuration-zonefile-sync zone))
508 (zonefile-load '#$(knot-zone-configuration-zonefile-load zone))
509 (journal-content #$(knot-zone-configuration-journal-content zone))
510 (max-journal-usage #$(knot-zone-configuration-max-journal-usage zone))
511 (max-journal-depth #$(knot-zone-configuration-max-journal-depth zone))
512 (max-zone-size #$(knot-zone-configuration-max-zone-size zone))
513 (dnssec-policy #$(knot-zone-configuration-dnssec-policy zone))
514 (serial-policy '#$(knot-zone-configuration-serial-policy zone)))
515 (format #t " - domain: ~a\n" domain)
517 ;; This server is a master
519 (format #t " file: ~a\n"
520 #$(serialize-zone-file content
521 (knot-zone-configuration-domain zone)))
522 (format #t " file: ~a\n" file))
523 ;; This server is a slave (has masters)
525 (format #t " master: ~a\n"
526 #$(format-string-list
527 (knot-zone-configuration-master zone)))
528 (if ddns-master (format #t " ddns-master ~a\n" ddns-master))))
529 (unless (eq? notify '())
530 (format #t " notify: ~a\n"
531 #$(format-string-list
532 (knot-zone-configuration-notify zone))))
533 (unless (eq? acl '())
534 (format #t " acl: ~a\n"
535 #$(format-string-list
536 (knot-zone-configuration-acl zone))))
537 (format #t " semantic-checks: ~a\n" (if semantic-checks? "on" "off"))
538 (format #t " disable-any: ~a\n" (if disable-any? "on" "off"))
540 (format #t " zonefile-sync: ~a\n" zonefile-sync))
542 (format #t " zonefile-load: ~a\n"
543 (symbol->string zonefile-load)))
545 (format #t " journal-content: ~a\n"
546 (symbol->string journal-content)))
547 (if max-journal-usage
548 (format #t " max-journal-usage: ~a\n" max-journal-usage))
549 (if max-journal-depth
550 (format #t " max-journal-depth: ~a\n" max-journal-depth))
552 (format #t " max-zone-size: ~a\n" max-zone-size))
555 (format #t " dnssec-signing: on\n")
556 (format #t " dnssec-policy: ~a\n" dnssec-policy)))
557 (format #t " serial-policy: ~a\n"
558 (symbol->string serial-policy)))))))
560 (define (knot-config-file config)
561 (verify-knot-configuration config)
562 (computed-file "knot.conf"
564 (call-with-output-file #$output
566 (for-each (lambda (inc)
567 (format port "include: ~a\n" inc))
568 '#$(knot-configuration-includes config))
569 (format port "server:\n")
570 (format port " rundir: ~a\n" #$(knot-configuration-run-directory config))
571 (format port " user: knot\n")
572 (format port " listen: ~a@~a\n"
573 #$(knot-configuration-listen-v4 config)
574 #$(knot-configuration-listen-port config))
575 (format port " listen: ~a@~a\n"
576 #$(knot-configuration-listen-v6 config)
577 #$(knot-configuration-listen-port config))
578 (format port "\nkey:\n")
579 (format port #$(knot-key-config (knot-configuration-keys config)))
580 (format port "\nkeystore:\n")
581 (format port #$(knot-keystore-config (knot-configuration-keystores config)))
582 (format port "\nacl:\n")
583 (format port #$(knot-acl-config (knot-configuration-acls config)))
584 (format port "\nremote:\n")
585 (format port #$(knot-remote-config (knot-configuration-remotes config)))
586 (format port "\npolicy:\n")
587 (format port #$(knot-policy-config (knot-configuration-policies config)))
588 (unless #$(eq? (knot-configuration-zones config) '())
589 (format port "\nzone:\n")
592 (list #$@(map knot-zone-config
593 (knot-configuration-zones config)))))))))))
595 (define %knot-accounts
596 (list (user-group (name "knot") (system? #t))
601 (comment "knot dns server user")
602 (home-directory "/var/empty")
603 (shell (file-append shadow "/sbin/nologin")))))
605 (define (knot-activation config)
607 (use-modules (guix build utils))
608 (define (mkdir-p/perms directory owner perms)
610 (chown directory (passwd:uid owner) (passwd:gid owner))
611 (chmod directory perms))
612 (mkdir-p/perms #$(knot-configuration-run-directory config)
613 (getpwnam "knot") #o755)
614 (mkdir-p/perms "/var/lib/knot" (getpwnam "knot") #o755)
615 (mkdir-p/perms "/var/lib/knot/keys" (getpwnam "knot") #o755)
616 (mkdir-p/perms "/var/lib/knot/keys/keys" (getpwnam "knot") #o755)))
618 (define (knot-shepherd-service config)
619 (let* ((config-file (knot-config-file config))
620 (knot (knot-configuration-knot config)))
621 (list (shepherd-service
622 (documentation "Run the Knot DNS daemon.")
623 (provision '(knot dns))
624 (requirement '(networking))
625 (start #~(make-forkexec-constructor
626 (list (string-append #$knot "/sbin/knotd")
627 "-c" #$config-file)))
628 (stop #~(make-kill-destructor))))))
630 (define knot-service-type
631 (service-type (name 'knot)
633 (list (service-extension shepherd-root-service-type
634 knot-shepherd-service)
635 (service-extension activation-service-type
637 (service-extension account-service-type
638 (const %knot-accounts))))))
645 (define-record-type* <dnsmasq-configuration>
646 dnsmasq-configuration make-dnsmasq-configuration
647 dnsmasq-configuration?
648 (package dnsmasq-configuration-package
649 (default dnsmasq)) ;package
650 (no-hosts? dnsmasq-configuration-no-hosts?
651 (default #f)) ;boolean
652 (port dnsmasq-configuration-port
653 (default 53)) ;integer
654 (local-service? dnsmasq-configuration-local-service?
655 (default #t)) ;boolean
656 (listen-addresses dnsmasq-configuration-listen-address
657 (default '())) ;list of string
658 (resolv-file dnsmasq-configuration-resolv-file
659 (default "/etc/resolv.conf")) ;string
660 (no-resolv? dnsmasq-configuration-no-resolv?
661 (default #f)) ;boolean
662 (servers dnsmasq-configuration-servers
663 (default '())) ;list of string
664 (cache-size dnsmasq-configuration-cache-size
665 (default 150)) ;integer
666 (negative-cache? dnsmasq-configuration-negative-cache?
667 (default #t))) ;boolean
669 (define dnsmasq-shepherd-service
671 (($ <dnsmasq-configuration> package
673 port local-service? listen-addresses
674 resolv-file no-resolv? servers
675 cache-size negative-cache?)
677 (provision '(dnsmasq))
678 (requirement '(networking))
679 (documentation "Run the dnsmasq DNS server.")
680 (start #~(make-forkexec-constructor
681 '(#$(file-append package "/sbin/dnsmasq")
682 "--keep-in-foreground"
683 "--pid-file=/run/dnsmasq.pid"
687 #$(format #f "--port=~a" port)
688 #$@(if local-service?
691 #$@(map (cut format #f "--listen-address=~a" <>)
693 #$(format #f "--resolv-file=~a" resolv-file)
697 #$@(map (cut format #f "--server=~a" <>)
699 #$(format #f "--cache-size=~a" cache-size)
700 #$@(if negative-cache?
703 #:pid-file "/run/dnsmasq.pid"))
704 (stop #~(make-kill-destructor))))))
706 (define dnsmasq-service-type
710 (list (service-extension shepherd-root-service-type
711 (compose list dnsmasq-shepherd-service))))
712 (default-value (dnsmasq-configuration))
713 (description "Run the dnsmasq DNS server.")))
720 (define (uglify-field-name field-name)
721 (string-delete #\? (symbol->string field-name)))
723 (define (serialize-field field-name val)
724 (when (not (member field-name '(group secret-file user)))
725 (format #t "~a=~a\n" (uglify-field-name field-name) val)))
727 (define (serialize-boolean field-name val)
728 (serialize-field field-name (if val "yes" "no")))
730 (define (serialize-integer field-name val)
731 (serialize-field field-name (number->string val)))
733 (define (serialize-string field-name val)
734 (if (and (string? val) (string=? val ""))
736 (serialize-field field-name val)))
738 (define (serialize-list field-name val)
739 (if (null? val) "" (serialize-field field-name (string-join val))))
741 (define (serialize-extra-options extra-options)
742 (string-join extra-options "\n" 'suffix))
744 (define-configuration ddclient-configuration
747 "The ddclient package.")
750 "The period after which ddclient will retry to check IP and domain name.")
753 "Use syslog for the output.")
759 "Mail failed update to user.")
761 (string "/var/run/ddclient/ddclient.pid")
762 "The ddclient PID file.")
765 "Enable SSL support.")
768 "Specifies the user name or ID that is used when running ddclient
772 "Group of the user who will run the ddclient program.")
774 (string "/etc/ddclient/secrets.conf")
775 "Secret file which will be appended to @file{ddclient.conf} file. This
776 file contains credentials for use by ddclient. You are expected to create it
780 "Extra options will be appended to @file{ddclient.conf} file."))
782 (define (ddclient-account config)
783 "Return the user accounts and user groups for CONFIG."
784 (let ((ddclient-user (ddclient-configuration-user config))
785 (ddclient-group (ddclient-configuration-group config)))
787 (name ddclient-group)
792 (group ddclient-group)
793 (comment "ddclientd privilege separation user")
794 (home-directory (string-append "/var/run/" ddclient-user))))))
796 (define (ddclient-activation config)
797 "Return the activation GEXP for CONFIG."
798 (with-imported-modules '((guix build utils)
801 (use-modules (guix build utils)
804 (passwd:uid (getpw #$(ddclient-configuration-user config))))
806 (passwd:gid (getpw #$(ddclient-configuration-group config))))
807 (ddclient-secret-file
808 #$(ddclient-configuration-secret-file config)))
809 ;; 'ddclient' complains about ddclient.conf file permissions, which
810 ;; rules out /gnu/store. Thus we copy the ddclient.conf to /etc.
811 (for-each (lambda (dir)
814 (chown dir ddclient-user ddclient-group))
815 '("/var/cache/ddclient" "/var/run/ddclient"
817 (with-output-to-file "/etc/ddclient/ddclient.conf"
821 "# Generated by 'ddclient-service'.\n\n"
822 #$(with-output-to-string
824 (serialize-configuration config
825 ddclient-configuration-fields)))
826 (if (string-null? ddclient-secret-file)
828 (format #f "\n\n# Appended from '~a'.\n\n~a"
830 (with-input-from-file ddclient-secret-file
832 (chmod "/etc/ddclient/ddclient.conf" #o600)
833 (chown "/etc/ddclient/ddclient.conf"
834 ddclient-user ddclient-group)))))
836 (define (ddclient-shepherd-service config)
837 "Return a <shepherd-service> for ddclient with CONFIG."
838 (let ((ddclient (ddclient-configuration-ddclient config))
839 (ddclient-pid (ddclient-configuration-pid config))
840 (ddclient-user (ddclient-configuration-user config))
841 (ddclient-group (ddclient-configuration-group config)))
842 (list (shepherd-service
843 (provision '(ddclient))
844 (documentation "Run ddclient daemon.")
845 (start #~(make-forkexec-constructor
846 (list #$(file-append ddclient "/bin/ddclient")
848 "-file" "/etc/ddclient/ddclient.conf")
849 #:pid-file #$ddclient-pid
850 #:environment-variables
851 (list "SSL_CERT_DIR=/run/current-system/profile\
853 "SSL_CERT_FILE=/run/current-system/profile\
854 /etc/ssl/certs/ca-certificates.crt")
855 #:user #$ddclient-user
856 #:group #$ddclient-group))
857 (stop #~(make-kill-destructor))))))
859 (define ddclient-service-type
863 (list (service-extension account-service-type
865 (service-extension shepherd-root-service-type
866 ddclient-shepherd-service)
867 (service-extension activation-service-type
868 ddclient-activation)))
869 (default-value (ddclient-configuration))
870 (description "Configure address updating utility for dynamic DNS services,
873 (define (generate-ddclient-documentation)
874 (generate-documentation
875 `((ddclient-configuration ,ddclient-configuration-fields))
876 'ddclient-configuration))