dns.scm 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  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.
  12. ;;;
  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.
  17. ;;;
  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/>.
  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
  43. knot-configuration
  44. define-zone-entries
  45. zone-file
  46. zone-entry
  47. knot-resolver-service-type
  48. knot-resolver-configuration
  49. dnsmasq-service-type
  50. dnsmasq-configuration
  51. ddclient-service-type
  52. ddclient-configuration))
  53. ;;;
  54. ;;; Knot DNS.
  55. ;;;
  56. (define-record-type* <knot-key-configuration>
  57. knot-key-configuration make-knot-key-configuration
  58. knot-key-configuration?
  59. (id knot-key-configuration-id
  60. (default ""))
  61. (algorithm knot-key-configuration-algorithm
  62. (default #f)); one of #f, or an algorithm name
  63. (secret knot-key-configuration-secret
  64. (default "")))
  65. (define-record-type* <knot-acl-configuration>
  66. knot-acl-configuration make-knot-acl-configuration
  67. knot-acl-configuration?
  68. (id knot-acl-configuration-id
  69. (default ""))
  70. (address knot-acl-configuration-address
  71. (default '()))
  72. (key knot-acl-configuration-key
  73. (default '()))
  74. (action knot-acl-configuration-action
  75. (default '()))
  76. (deny? knot-acl-configuration-deny?
  77. (default #f)))
  78. (define-record-type* <zone-entry>
  79. zone-entry make-zone-entry
  80. zone-entry?
  81. (name zone-entry-name
  82. (default "@"))
  83. (ttl zone-entry-ttl
  84. (default ""))
  85. (class zone-entry-class
  86. (default "IN"))
  87. (type zone-entry-type
  88. (default "A"))
  89. (data zone-entry-data
  90. (default "")))
  91. (define-record-type* <zone-file>
  92. zone-file make-zone-file
  93. zone-file?
  94. (entries zone-file-entries
  95. (default '()))
  96. (origin zone-file-origin
  97. (default ""))
  98. (ns zone-file-ns
  99. (default "ns"))
  100. (mail zone-file-mail
  101. (default "hostmaster"))
  102. (serial zone-file-serial
  103. (default 1))
  104. (refresh zone-file-refresh
  105. (default (* 2 24 3600)))
  106. (retry zone-file-retry
  107. (default (* 15 60)))
  108. (expiry zone-file-expiry
  109. (default (* 2 7 24 3600)))
  110. (nx zone-file-nx
  111. (default 3600)))
  112. (define-record-type* <knot-keystore-configuration>
  113. knot-keystore-configuration make-knot-keystore-configuration
  114. knot-keystore-configuration?
  115. (id knot-keystore-configuration-id
  116. (default ""))
  117. (backend knot-keystore-configuration-backend
  118. (default 'pem))
  119. (config knot-keystore-configuration-config
  120. (default "/var/lib/knot/keys/keys")))
  121. (define-record-type* <knot-policy-configuration>
  122. knot-policy-configuration make-knot-policy-configuration
  123. knot-policy-configuration?
  124. (id knot-policy-configuration-id
  125. (default ""))
  126. (keystore knot-policy-configuration-keystore
  127. (default "default"))
  128. (manual? knot-policy-configuration-manual?
  129. (default #f))
  130. (single-type-signing? knot-policy-configuration-single-type-signing?
  131. (default #f))
  132. (algorithm knot-policy-configuration-algorithm
  133. (default "ecdsap256sha256"))
  134. (ksk-size knot-policy-configuration-ksk-size
  135. (default 256))
  136. (zsk-size knot-policy-configuration-zsk-size
  137. (default 256))
  138. (dnskey-ttl knot-policy-configuration-dnskey-ttl
  139. (default 'default))
  140. (zsk-lifetime knot-policy-configuration-zsk-lifetime
  141. (default (* 30 24 3600)))
  142. (propagation-delay knot-policy-configuration-propagation-delay
  143. (default (* 24 3600)))
  144. (rrsig-lifetime knot-policy-configuration-rrsig-lifetime
  145. (default (* 14 24 3600)))
  146. (rrsig-refresh knot-policy-configuration-rrsig-refresh
  147. (default (* 7 24 3600)))
  148. (nsec3? knot-policy-configuration-nsec3?
  149. (default #f))
  150. (nsec3-iterations knot-policy-configuration-nsec3-iterations
  151. (default 5))
  152. (nsec3-salt-length knot-policy-configuration-nsec3-salt-length
  153. (default 8))
  154. (nsec3-salt-lifetime knot-policy-configuration-nsec3-salt-lifetime
  155. (default (* 30 24 3600))))
  156. (define-record-type* <knot-zone-configuration>
  157. knot-zone-configuration make-knot-zone-configuration
  158. knot-zone-configuration?
  159. (domain knot-zone-configuration-domain
  160. (default ""))
  161. (file knot-zone-configuration-file
  162. (default "")) ; the file where this zone is saved.
  163. (zone knot-zone-configuration-zone
  164. (default (zone-file))) ; initial content of the zone file
  165. (master knot-zone-configuration-master
  166. (default '()))
  167. (ddns-master knot-zone-configuration-ddns-master
  168. (default #f))
  169. (notify knot-zone-configuration-notify
  170. (default '()))
  171. (acl knot-zone-configuration-acl
  172. (default '()))
  173. (semantic-checks? knot-zone-configuration-semantic-checks?
  174. (default #f))
  175. (disable-any? knot-zone-configuration-disable-any?
  176. (default #f))
  177. (zonefile-sync knot-zone-configuration-zonefile-sync
  178. (default 0))
  179. (zonefile-load knot-zone-configuration-zonefile-load
  180. (default #f))
  181. (journal-content knot-zone-configuration-journal-content
  182. (default #f))
  183. (max-journal-usage knot-zone-configuration-max-journal-usage
  184. (default #f))
  185. (max-journal-depth knot-zone-configuration-max-journal-depth
  186. (default #f))
  187. (max-zone-size knot-zone-configuration-max-zone-size
  188. (default #f))
  189. (dnssec-policy knot-zone-configuration-dnssec-policy
  190. (default #f))
  191. (serial-policy knot-zone-configuration-serial-policy
  192. (default 'increment)))
  193. (define-record-type* <knot-remote-configuration>
  194. knot-remote-configuration make-knot-remote-configuration
  195. knot-remote-configuration?
  196. (id knot-remote-configuration-id
  197. (default ""))
  198. (address knot-remote-configuration-address
  199. (default '()))
  200. (via knot-remote-configuration-via
  201. (default '()))
  202. (key knot-remote-configuration-key
  203. (default #f)))
  204. (define-record-type* <knot-configuration>
  205. knot-configuration make-knot-configuration
  206. knot-configuration?
  207. (knot knot-configuration-knot
  208. (default knot))
  209. (run-directory knot-configuration-run-directory
  210. (default "/var/run/knot"))
  211. (includes knot-configuration-includes
  212. (default '()))
  213. (listen-v4 knot-configuration-listen-v4
  214. (default "0.0.0.0"))
  215. (listen-v6 knot-configuration-listen-v6
  216. (default "::"))
  217. (listen-port knot-configuration-listen-port
  218. (default 53))
  219. (keys knot-configuration-keys
  220. (default '()))
  221. (keystores knot-configuration-keystores
  222. (default '()))
  223. (acls knot-configuration-acls
  224. (default '()))
  225. (remotes knot-configuration-remotes
  226. (default '()))
  227. (policies knot-configuration-policies
  228. (default '()))
  229. (zones knot-configuration-zones
  230. (default '())))
  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. (define (error-out msg)
  236. (raise (condition (&message (message msg)))))
  237. (define (verify-knot-key-configuration key)
  238. (unless (knot-key-configuration? key)
  239. (error-out "keys must be a list of only knot-key-configuration."))
  240. (let ((id (knot-key-configuration-id key)))
  241. (unless (and (string? id) (not (equal? id "")))
  242. (error-out "key id must be a non empty string.")))
  243. (unless (memq '(#f hmac-md5 hmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512)
  244. (knot-key-configuration-algorithm key))
  245. (error-out "algorithm must be one of: #f, 'hmac-md5, 'hmac-sha1,
  246. 'hmac-sha224, 'hmac-sha256, 'hmac-sha384 or 'hmac-sha512")))
  247. (define (verify-knot-keystore-configuration keystore)
  248. (unless (knot-keystore-configuration? keystore)
  249. (error-out "keystores must be a list of only knot-keystore-configuration."))
  250. (let ((id (knot-keystore-configuration-id keystore)))
  251. (unless (and (string? id) (not (equal? id "")))
  252. (error-out "keystore id must be a non empty string.")))
  253. (unless (memq '(pem pkcs11)
  254. (knot-keystore-configuration-backend keystore))
  255. (error-out "backend must be one of: 'pem or 'pkcs11")))
  256. (define (verify-knot-policy-configuration policy)
  257. (unless (knot-policy-configuration? policy)
  258. (error-out "policies must be a list of only knot-policy-configuration."))
  259. (let ((id (knot-policy-configuration-id policy)))
  260. (unless (and (string? id) (not (equal? id "")))
  261. (error-out "policy id must be a non empty string."))))
  262. (define (verify-knot-acl-configuration acl)
  263. (unless (knot-acl-configuration? acl)
  264. (error-out "acls must be a list of only knot-acl-configuration."))
  265. (let ((id (knot-acl-configuration-id acl))
  266. (address (knot-acl-configuration-address acl))
  267. (key (knot-acl-configuration-key acl))
  268. (action (knot-acl-configuration-action acl)))
  269. (unless (and (string? id) (not (equal? id "")))
  270. (error-out "acl id must be a non empty string."))
  271. (unless (and (list? address)
  272. (fold (lambda (x1 x2) (and (string? x1) (string? x2))) "" address))
  273. (error-out "acl address must be a list of strings.")))
  274. (unless (boolean? (knot-acl-configuration-deny? acl))
  275. (error-out "deny? must be #t or #f.")))
  276. (define (verify-knot-zone-configuration zone)
  277. (unless (knot-zone-configuration? zone)
  278. (error-out "zones must be a list of only knot-zone-configuration."))
  279. (let ((domain (knot-zone-configuration-domain zone)))
  280. (unless (and (string? domain) (not (equal? domain "")))
  281. (error-out "zone domain must be a non empty string."))))
  282. (define (verify-knot-remote-configuration remote)
  283. (unless (knot-remote-configuration? remote)
  284. (error-out "remotes must be a list of only knot-remote-configuration."))
  285. (let ((id (knot-remote-configuration-id remote)))
  286. (unless (and (string? id) (not (equal? id "")))
  287. (error-out "remote id must be a non empty string."))))
  288. (define (verify-knot-configuration config)
  289. (unless (package? (knot-configuration-knot config))
  290. (error-out "knot configuration field must be a package."))
  291. (unless (string? (knot-configuration-run-directory config))
  292. (error-out "run-directory must be a string."))
  293. (unless (list? (knot-configuration-includes config))
  294. (error-out "includes must be a list of strings or file-like objects."))
  295. (unless (list? (knot-configuration-keys config))
  296. (error-out "keys must be a list of knot-key-configuration."))
  297. (for-each (lambda (key) (verify-knot-key-configuration key))
  298. (knot-configuration-keys config))
  299. (unless (list? (knot-configuration-keystores config))
  300. (error-out "keystores must be a list of knot-keystore-configuration."))
  301. (for-each (lambda (keystore) (verify-knot-keystore-configuration keystore))
  302. (knot-configuration-keystores config))
  303. (unless (list? (knot-configuration-acls config))
  304. (error-out "acls must be a list of knot-acl-configuration."))
  305. (for-each (lambda (acl) (verify-knot-acl-configuration acl))
  306. (knot-configuration-acls config))
  307. (unless (list? (knot-configuration-zones config))
  308. (error-out "zones must be a list of knot-zone-configuration."))
  309. (for-each (lambda (zone) (verify-knot-zone-configuration zone))
  310. (knot-configuration-zones config))
  311. (unless (list? (knot-configuration-policies config))
  312. (error-out "policies must be a list of knot-policy-configuration."))
  313. (for-each (lambda (policy) (verify-knot-policy-configuration policy))
  314. (knot-configuration-policies config))
  315. (unless (list? (knot-configuration-remotes config))
  316. (error-out "remotes must be a list of knot-remote-configuration."))
  317. (for-each (lambda (remote) (verify-knot-remote-configuration remote))
  318. (knot-configuration-remotes config))
  319. #t)
  320. (define (format-string-list l)
  321. "Formats a list of string in YAML"
  322. (if (eq? l '())
  323. ""
  324. (let ((l (reverse l)))
  325. (string-append
  326. "["
  327. (fold (lambda (x1 x2)
  328. (string-append (if (symbol? x1) (symbol->string x1) x1) ", "
  329. (if (symbol? x2) (symbol->string x2) x2)))
  330. (if (symbol? (car l)) (symbol->string (car l)) (car l)) (cdr l))
  331. "]"))))
  332. (define (knot-acl-config acls)
  333. (with-output-to-string
  334. (lambda ()
  335. (for-each
  336. (lambda (acl-config)
  337. (let ((id (knot-acl-configuration-id acl-config))
  338. (address (knot-acl-configuration-address acl-config))
  339. (key (knot-acl-configuration-key acl-config))
  340. (action (knot-acl-configuration-action acl-config))
  341. (deny? (knot-acl-configuration-deny? acl-config)))
  342. (format #t " - id: ~a\n" id)
  343. (unless (eq? address '())
  344. (format #t " address: ~a\n" (format-string-list address)))
  345. (unless (eq? key '())
  346. (format #t " key: ~a\n" (format-string-list key)))
  347. (unless (eq? action '())
  348. (format #t " action: ~a\n" (format-string-list action)))
  349. (format #t " deny: ~a\n" (if deny? "on" "off"))))
  350. acls))))
  351. (define (knot-key-config keys)
  352. (with-output-to-string
  353. (lambda ()
  354. (for-each
  355. (lambda (key-config)
  356. (let ((id (knot-key-configuration-id key-config))
  357. (algorithm (knot-key-configuration-algorithm key-config))
  358. (secret (knot-key-configuration-secret key-config)))
  359. (format #t " - id: ~a\n" id)
  360. (if algorithm
  361. (format #t " algorithm: ~a\n" (symbol->string algorithm)))
  362. (format #t " secret: ~a\n" secret)))
  363. keys))))
  364. (define (knot-keystore-config keystores)
  365. (with-output-to-string
  366. (lambda ()
  367. (for-each
  368. (lambda (keystore-config)
  369. (let ((id (knot-keystore-configuration-id keystore-config))
  370. (backend (knot-keystore-configuration-backend keystore-config))
  371. (config (knot-keystore-configuration-config keystore-config)))
  372. (format #t " - id: ~a\n" id)
  373. (format #t " backend: ~a\n" (symbol->string backend))
  374. (format #t " config: \"~a\"\n" config)))
  375. keystores))))
  376. (define (knot-policy-config policies)
  377. (with-output-to-string
  378. (lambda ()
  379. (for-each
  380. (lambda (policy-config)
  381. (let ((id (knot-policy-configuration-id policy-config))
  382. (keystore (knot-policy-configuration-keystore policy-config))
  383. (manual? (knot-policy-configuration-manual? policy-config))
  384. (single-type-signing? (knot-policy-configuration-single-type-signing?
  385. policy-config))
  386. (algorithm (knot-policy-configuration-algorithm policy-config))
  387. (ksk-size (knot-policy-configuration-ksk-size policy-config))
  388. (zsk-size (knot-policy-configuration-zsk-size policy-config))
  389. (dnskey-ttl (knot-policy-configuration-dnskey-ttl policy-config))
  390. (zsk-lifetime (knot-policy-configuration-zsk-lifetime policy-config))
  391. (propagation-delay (knot-policy-configuration-propagation-delay
  392. policy-config))
  393. (rrsig-lifetime (knot-policy-configuration-rrsig-lifetime
  394. policy-config))
  395. (nsec3? (knot-policy-configuration-nsec3? policy-config))
  396. (nsec3-iterations (knot-policy-configuration-nsec3-iterations
  397. policy-config))
  398. (nsec3-salt-length (knot-policy-configuration-nsec3-salt-length
  399. policy-config))
  400. (nsec3-salt-lifetime (knot-policy-configuration-nsec3-salt-lifetime
  401. policy-config)))
  402. (format #t " - id: ~a\n" id)
  403. (format #t " keystore: ~a\n" keystore)
  404. (format #t " manual: ~a\n" (if manual? "on" "off"))
  405. (format #t " single-type-signing: ~a\n" (if single-type-signing?
  406. "on" "off"))
  407. (format #t " algorithm: ~a\n" algorithm)
  408. (format #t " ksk-size: ~a\n" (number->string ksk-size))
  409. (format #t " zsk-size: ~a\n" (number->string zsk-size))
  410. (unless (eq? dnskey-ttl 'default)
  411. (format #t " dnskey-ttl: ~a\n" dnskey-ttl))
  412. (format #t " zsk-lifetime: ~a\n" zsk-lifetime)
  413. (format #t " propagation-delay: ~a\n" propagation-delay)
  414. (format #t " rrsig-lifetime: ~a\n" rrsig-lifetime)
  415. (format #t " nsec3: ~a\n" (if nsec3? "on" "off"))
  416. (format #t " nsec3-iterations: ~a\n"
  417. (number->string nsec3-iterations))
  418. (format #t " nsec3-salt-length: ~a\n"
  419. (number->string nsec3-salt-length))
  420. (format #t " nsec3-salt-lifetime: ~a\n" nsec3-salt-lifetime)))
  421. policies))))
  422. (define (knot-remote-config remotes)
  423. (with-output-to-string
  424. (lambda ()
  425. (for-each
  426. (lambda (remote-config)
  427. (let ((id (knot-remote-configuration-id remote-config))
  428. (address (knot-remote-configuration-address remote-config))
  429. (via (knot-remote-configuration-via remote-config))
  430. (key (knot-remote-configuration-key remote-config)))
  431. (format #t " - id: ~a\n" id)
  432. (unless (eq? address '())
  433. (format #t " address: ~a\n" (format-string-list address)))
  434. (unless (eq? via '())
  435. (format #t " via: ~a\n" (format-string-list via)))
  436. (if key
  437. (format #t " key: ~a\n" key))))
  438. remotes))))
  439. (define (serialize-zone-entries entries)
  440. (with-output-to-string
  441. (lambda ()
  442. (for-each
  443. (lambda (entry)
  444. (let ((name (zone-entry-name entry))
  445. (ttl (zone-entry-ttl entry))
  446. (class (zone-entry-class entry))
  447. (type (zone-entry-type entry))
  448. (data (zone-entry-data entry)))
  449. (format #t "~a ~a ~a ~a ~a\n" name ttl class type data)))
  450. entries))))
  451. (define (serialize-zone-file zone domain)
  452. (computed-file (string-append domain ".zone")
  453. #~(begin
  454. (call-with-output-file #$output
  455. (lambda (port)
  456. (format port "$ORIGIN ~a.\n"
  457. #$(zone-file-origin zone))
  458. (format port "@ IN SOA ~a ~a (~a ~a ~a ~a ~a)\n"
  459. #$(zone-file-ns zone)
  460. #$(zone-file-mail zone)
  461. #$(zone-file-serial zone)
  462. #$(zone-file-refresh zone)
  463. #$(zone-file-retry zone)
  464. #$(zone-file-expiry zone)
  465. #$(zone-file-nx zone))
  466. (format port "~a\n"
  467. #$(serialize-zone-entries (zone-file-entries zone))))))))
  468. (define (knot-zone-config zone)
  469. (let ((content (knot-zone-configuration-zone zone)))
  470. #~(with-output-to-string
  471. (lambda ()
  472. (let ((domain #$(knot-zone-configuration-domain zone))
  473. (file #$(knot-zone-configuration-file zone))
  474. (master (list #$@(knot-zone-configuration-master zone)))
  475. (ddns-master #$(knot-zone-configuration-ddns-master zone))
  476. (notify (list #$@(knot-zone-configuration-notify zone)))
  477. (acl (list #$@(knot-zone-configuration-acl zone)))
  478. (semantic-checks? #$(knot-zone-configuration-semantic-checks? zone))
  479. (disable-any? #$(knot-zone-configuration-disable-any? zone))
  480. (zonefile-sync #$(knot-zone-configuration-zonefile-sync zone))
  481. (zonefile-load '#$(knot-zone-configuration-zonefile-load zone))
  482. (journal-content #$(knot-zone-configuration-journal-content zone))
  483. (max-journal-usage #$(knot-zone-configuration-max-journal-usage zone))
  484. (max-journal-depth #$(knot-zone-configuration-max-journal-depth zone))
  485. (max-zone-size #$(knot-zone-configuration-max-zone-size zone))
  486. (dnssec-policy #$(knot-zone-configuration-dnssec-policy zone))
  487. (serial-policy '#$(knot-zone-configuration-serial-policy zone)))
  488. (format #t " - domain: ~a\n" domain)
  489. (if (eq? master '())
  490. ;; This server is a master
  491. (if (equal? file "")
  492. (format #t " file: ~a\n"
  493. #$(serialize-zone-file content
  494. (knot-zone-configuration-domain zone)))
  495. (format #t " file: ~a\n" file))
  496. ;; This server is a slave (has masters)
  497. (begin
  498. (format #t " master: ~a\n"
  499. #$(format-string-list
  500. (knot-zone-configuration-master zone)))
  501. (if ddns-master (format #t " ddns-master ~a\n" ddns-master))))
  502. (unless (eq? notify '())
  503. (format #t " notify: ~a\n"
  504. #$(format-string-list
  505. (knot-zone-configuration-notify zone))))
  506. (unless (eq? acl '())
  507. (format #t " acl: ~a\n"
  508. #$(format-string-list
  509. (knot-zone-configuration-acl zone))))
  510. (format #t " semantic-checks: ~a\n" (if semantic-checks? "on" "off"))
  511. (format #t " disable-any: ~a\n" (if disable-any? "on" "off"))
  512. (if zonefile-sync
  513. (format #t " zonefile-sync: ~a\n" zonefile-sync))
  514. (if zonefile-load
  515. (format #t " zonefile-load: ~a\n"
  516. (symbol->string zonefile-load)))
  517. (if journal-content
  518. (format #t " journal-content: ~a\n"
  519. (symbol->string journal-content)))
  520. (if max-journal-usage
  521. (format #t " max-journal-usage: ~a\n" max-journal-usage))
  522. (if max-journal-depth
  523. (format #t " max-journal-depth: ~a\n" max-journal-depth))
  524. (if max-zone-size
  525. (format #t " max-zone-size: ~a\n" max-zone-size))
  526. (if dnssec-policy
  527. (begin
  528. (format #t " dnssec-signing: on\n")
  529. (format #t " dnssec-policy: ~a\n" dnssec-policy)))
  530. (format #t " serial-policy: ~a\n"
  531. (symbol->string serial-policy)))))))
  532. (define (knot-config-file config)
  533. (verify-knot-configuration config)
  534. (computed-file "knot.conf"
  535. #~(begin
  536. (call-with-output-file #$output
  537. (lambda (port)
  538. (for-each (lambda (inc)
  539. (format port "include: ~a\n" inc))
  540. '#$(knot-configuration-includes config))
  541. (format port "server:\n")
  542. (format port " rundir: ~a\n" #$(knot-configuration-run-directory config))
  543. (format port " user: knot\n")
  544. (format port " listen: ~a@~a\n"
  545. #$(knot-configuration-listen-v4 config)
  546. #$(knot-configuration-listen-port config))
  547. (format port " listen: ~a@~a\n"
  548. #$(knot-configuration-listen-v6 config)
  549. #$(knot-configuration-listen-port config))
  550. (format port "\nkey:\n")
  551. (format port #$(knot-key-config (knot-configuration-keys config)))
  552. (format port "\nkeystore:\n")
  553. (format port #$(knot-keystore-config (knot-configuration-keystores config)))
  554. (format port "\nacl:\n")
  555. (format port #$(knot-acl-config (knot-configuration-acls config)))
  556. (format port "\nremote:\n")
  557. (format port #$(knot-remote-config (knot-configuration-remotes config)))
  558. (format port "\npolicy:\n")
  559. (format port #$(knot-policy-config (knot-configuration-policies config)))
  560. (unless #$(eq? (knot-configuration-zones config) '())
  561. (format port "\nzone:\n")
  562. (format port "~a\n"
  563. (string-concatenate
  564. (list #$@(map knot-zone-config
  565. (knot-configuration-zones config)))))))))))
  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. (define (knot-activation config)
  576. #~(begin
  577. (use-modules (guix build utils))
  578. (define (mkdir-p/perms directory owner perms)
  579. (mkdir-p directory)
  580. (chown directory (passwd:uid owner) (passwd:gid owner))
  581. (chmod directory perms))
  582. (mkdir-p/perms #$(knot-configuration-run-directory config)
  583. (getpwnam "knot") #o755)
  584. (mkdir-p/perms "/var/lib/knot" (getpwnam "knot") #o755)
  585. (mkdir-p/perms "/var/lib/knot/keys" (getpwnam "knot") #o755)
  586. (mkdir-p/perms "/var/lib/knot/keys/keys" (getpwnam "knot") #o755)))
  587. (define (knot-shepherd-service config)
  588. (let* ((config-file (knot-config-file config))
  589. (knot (knot-configuration-knot config)))
  590. (list (shepherd-service
  591. (documentation "Run the Knot DNS daemon.")
  592. (provision '(knot dns))
  593. (requirement '(networking))
  594. (start #~(make-forkexec-constructor
  595. (list (string-append #$knot "/sbin/knotd")
  596. "-c" #$config-file)))
  597. (stop #~(make-kill-destructor))))))
  598. (define knot-service-type
  599. (service-type (name 'knot)
  600. (extensions
  601. (list (service-extension shepherd-root-service-type
  602. knot-shepherd-service)
  603. (service-extension activation-service-type
  604. knot-activation)
  605. (service-extension account-service-type
  606. (const %knot-accounts))))
  607. (description
  608. "Run @uref{https://www.knot-dns.cz/, Knot}, an authoritative
  609. name server for the @acronym{DNS, Domain Name System}.")))
  610. ;;;
  611. ;;; Knot Resolver.
  612. ;;;
  613. (define-record-type* <knot-resolver-configuration>
  614. knot-resolver-configuration
  615. make-knot-resolver-configuration
  616. knot-resolver-configuration?
  617. (package knot-resolver-configuration-package
  618. (default knot-resolver))
  619. (kresd-config-file knot-resolver-kresd-config-file
  620. (default %kresd.conf))
  621. (garbage-collection-interval knot-resolver-garbage-collection-interval
  622. (default 1000)))
  623. (define %kresd.conf
  624. (plain-file "kresd.conf" "-- -*- mode: lua -*-
  625. trust_anchors.add_file('/var/cache/knot-resolver/root.keys')
  626. net = { '127.0.0.1', '::1' }
  627. user('knot-resolver', 'knot-resolver')
  628. modules = { 'hints > iterate', 'stats', 'predict' }
  629. cache.size = 100 * MB
  630. "))
  631. (define %knot-resolver-accounts
  632. (list (user-group
  633. (name "knot-resolver")
  634. (system? #t))
  635. (user-account
  636. (name "knot-resolver")
  637. (group "knot-resolver")
  638. (system? #t)
  639. (home-directory "/var/cache/knot-resolver")
  640. (shell (file-append shadow "/sbin/nologin")))))
  641. (define (knot-resolver-activation config)
  642. #~(begin
  643. (use-modules (guix build utils))
  644. (let ((rundir "/var/cache/knot-resolver")
  645. (owner (getpwnam "knot-resolver")))
  646. (mkdir-p rundir)
  647. (chown rundir (passwd:uid owner) (passwd:gid owner)))))
  648. (define knot-resolver-shepherd-services
  649. (match-lambda
  650. (($ <knot-resolver-configuration> package
  651. kresd-config-file
  652. garbage-collection-interval)
  653. (list
  654. (shepherd-service
  655. (provision '(kresd))
  656. (requirement '(networking))
  657. (documentation "Run the Knot Resolver daemon.")
  658. (start #~(make-forkexec-constructor
  659. '(#$(file-append package "/sbin/kresd")
  660. "-c" #$kresd-config-file "-f" "1"
  661. "/var/cache/knot-resolver")))
  662. (stop #~(make-kill-destructor)))
  663. (shepherd-service
  664. (provision '(kres-cache-gc))
  665. (requirement '(user-processes))
  666. (documentation "Run the Knot Resolver Garbage Collector daemon.")
  667. (start #~(make-forkexec-constructor
  668. '(#$(file-append package "/sbin/kres-cache-gc")
  669. "-d" #$(number->string garbage-collection-interval)
  670. "-c" "/var/cache/knot-resolver")
  671. #:user "knot-resolver"
  672. #:group "knot-resolver"))
  673. (stop #~(make-kill-destructor)))))))
  674. (define knot-resolver-service-type
  675. (service-type
  676. (name 'knot-resolver)
  677. (extensions
  678. (list (service-extension shepherd-root-service-type
  679. knot-resolver-shepherd-services)
  680. (service-extension activation-service-type
  681. knot-resolver-activation)
  682. (service-extension account-service-type
  683. (const %knot-resolver-accounts))))
  684. (default-value (knot-resolver-configuration))
  685. (description "Run the Knot DNS Resolver.")))
  686. ;;;
  687. ;;; Dnsmasq.
  688. ;;;
  689. (define-record-type* <dnsmasq-configuration>
  690. dnsmasq-configuration make-dnsmasq-configuration
  691. dnsmasq-configuration?
  692. (package dnsmasq-configuration-package
  693. (default dnsmasq)) ;package
  694. (no-hosts? dnsmasq-configuration-no-hosts?
  695. (default #f)) ;boolean
  696. (port dnsmasq-configuration-port
  697. (default 53)) ;integer
  698. (local-service? dnsmasq-configuration-local-service?
  699. (default #t)) ;boolean
  700. (listen-addresses dnsmasq-configuration-listen-address
  701. (default '())) ;list of string
  702. (resolv-file dnsmasq-configuration-resolv-file
  703. (default "/etc/resolv.conf")) ;string
  704. (no-resolv? dnsmasq-configuration-no-resolv?
  705. (default #f)) ;boolean
  706. (servers dnsmasq-configuration-servers
  707. (default '())) ;list of string
  708. (addresses dnsmasq-configuration-addresses
  709. (default '())) ;list of string
  710. (cache-size dnsmasq-configuration-cache-size
  711. (default 150)) ;integer
  712. (negative-cache? dnsmasq-configuration-negative-cache?
  713. (default #t)) ;boolean
  714. (tftp-enable? dnsmasq-configuration-tftp-enable?
  715. (default #f)) ;boolean
  716. (tftp-no-fail? dnsmasq-configuration-tftp-no-fail?
  717. (default #f)) ;boolean
  718. (tftp-single-port? dnsmasq-configuration-tftp-single-port?
  719. (default #f)) ;boolean
  720. (tftp-secure? dnsmasq-tftp-secure?
  721. (default #f)) ;boolean
  722. (tftp-max dnsmasq-tftp-max
  723. (default #f)) ;integer
  724. (tftp-mtu dnsmasq-tftp-mtu
  725. (default #f)) ;integer
  726. (tftp-no-blocksize? dnsmasq-tftp-no-blocksize?
  727. (default #f)) ;boolean
  728. (tftp-lowercase? dnsmasq-tftp-lowercase?
  729. (default #f)) ;boolean
  730. (tftp-port-range dnsmasq-tftp-port-range
  731. (default #f)) ;string
  732. (tftp-root dnsmasq-tftp-root
  733. (default "/var/empty,lo")) ;string
  734. (tftp-unique-root dnsmasq-tftp-unique-root
  735. (default #f))) ;"" or "ip" or "mac"
  736. (define dnsmasq-shepherd-service
  737. (match-lambda
  738. (($ <dnsmasq-configuration> package
  739. no-hosts?
  740. port local-service? listen-addresses
  741. resolv-file no-resolv? servers
  742. addresses cache-size negative-cache?
  743. tftp-enable? tftp-no-fail?
  744. tftp-single-port? tftp-secure?
  745. tftp-max tftp-mtu tftp-no-blocksize?
  746. tftp-lowercase? tftp-port-range
  747. tftp-root tftp-unique-root)
  748. (shepherd-service
  749. (provision '(dnsmasq))
  750. (requirement '(networking))
  751. (documentation "Run the dnsmasq DNS server.")
  752. (start #~(make-forkexec-constructor
  753. '(#$(file-append package "/sbin/dnsmasq")
  754. "--keep-in-foreground"
  755. "--pid-file=/run/dnsmasq.pid"
  756. #$@(if no-hosts?
  757. '("--no-hosts")
  758. '())
  759. #$(format #f "--port=~a" port)
  760. #$@(if local-service?
  761. '("--local-service")
  762. '())
  763. #$@(map (cut format #f "--listen-address=~a" <>)
  764. listen-addresses)
  765. #$(format #f "--resolv-file=~a" resolv-file)
  766. #$@(if no-resolv?
  767. '("--no-resolv")
  768. '())
  769. #$@(map (cut format #f "--server=~a" <>)
  770. servers)
  771. #$@(map (cut format #f "--address=~a" <>)
  772. addresses)
  773. #$(format #f "--cache-size=~a" cache-size)
  774. #$@(if negative-cache?
  775. '()
  776. '("--no-negcache"))
  777. #$@(if tftp-enable?
  778. '("--enable-tftp")
  779. '())
  780. #$@(if tftp-no-fail?
  781. '("--tftp-no-fail")
  782. '())
  783. #$@(if tftp-single-port?
  784. '("--tftp-single-port")
  785. '())
  786. #$@(if tftp-secure?
  787. '("--tftp-secure?")
  788. '())
  789. #$@(if tftp-max
  790. (list (format #f "--tftp-max=~a" tftp-max))
  791. '())
  792. #$@(if tftp-mtu
  793. (list (format #f "--tftp-mtu=~a" tftp-mtu))
  794. '())
  795. #$@(if tftp-no-blocksize?
  796. '("--tftp-no-blocksize")
  797. '())
  798. #$@(if tftp-lowercase?
  799. '("--tftp-lowercase")
  800. '())
  801. #$@(if tftp-port-range
  802. (list (format #f "--tftp-port-range=~a"
  803. tftp-port-range))
  804. '())
  805. #$@(if tftp-root
  806. (list (format #f "--tftp-root=~a" tftp-root))
  807. '())
  808. #$@(if tftp-unique-root
  809. (list
  810. (if (> (length tftp-unique-root) 0)
  811. (format #f "--tftp-unique-root=~a" tftp-unique-root)
  812. (format #f "--tftp-unique-root")))
  813. '()))
  814. #:pid-file "/run/dnsmasq.pid"))
  815. (stop #~(make-kill-destructor))))))
  816. (define dnsmasq-service-type
  817. (service-type
  818. (name 'dnsmasq)
  819. (extensions
  820. (list (service-extension shepherd-root-service-type
  821. (compose list dnsmasq-shepherd-service))))
  822. (default-value (dnsmasq-configuration))
  823. (description "Run the dnsmasq DNS server.")))
  824. ;;;
  825. ;;; ddclient
  826. ;;;
  827. (define (uglify-field-name field-name)
  828. (string-delete #\? (symbol->string field-name)))
  829. (define (serialize-field field-name val)
  830. (when (not (member field-name '(group secret-file user)))
  831. (format #t "~a=~a\n" (uglify-field-name field-name) val)))
  832. (define (serialize-boolean field-name val)
  833. (serialize-field field-name (if val "yes" "no")))
  834. (define (serialize-integer field-name val)
  835. (serialize-field field-name (number->string val)))
  836. (define (serialize-string field-name val)
  837. (if (and (string? val) (string=? val ""))
  838. ""
  839. (serialize-field field-name val)))
  840. (define (serialize-list field-name val)
  841. (if (null? val) "" (serialize-field field-name (string-join val))))
  842. (define (serialize-extra-options extra-options)
  843. (string-join extra-options "\n" 'suffix))
  844. (define-configuration ddclient-configuration
  845. (ddclient
  846. (package ddclient)
  847. "The ddclient package.")
  848. (daemon
  849. (integer 300)
  850. "The period after which ddclient will retry to check IP and domain name.")
  851. (syslog
  852. (boolean #t)
  853. "Use syslog for the output.")
  854. (mail
  855. (string "root")
  856. "Mail to user.")
  857. (mail-failure
  858. (string "root")
  859. "Mail failed update to user.")
  860. (pid
  861. (string "/var/run/ddclient/ddclient.pid")
  862. "The ddclient PID file.")
  863. (ssl
  864. (boolean #t)
  865. "Enable SSL support.")
  866. (user
  867. (string "ddclient")
  868. "Specifies the user name or ID that is used when running ddclient
  869. program.")
  870. (group
  871. (string "ddclient")
  872. "Group of the user who will run the ddclient program.")
  873. (secret-file
  874. (string "/etc/ddclient/secrets.conf")
  875. "Secret file which will be appended to @file{ddclient.conf} file. This
  876. file contains credentials for use by ddclient. You are expected to create it
  877. manually.")
  878. (extra-options
  879. (list '())
  880. "Extra options will be appended to @file{ddclient.conf} file."))
  881. (define (ddclient-account config)
  882. "Return the user accounts and user groups for CONFIG."
  883. (let ((ddclient-user (ddclient-configuration-user config))
  884. (ddclient-group (ddclient-configuration-group config)))
  885. (list (user-group
  886. (name ddclient-group)
  887. (system? #t))
  888. (user-account
  889. (name ddclient-user)
  890. (system? #t)
  891. (group ddclient-group)
  892. (comment "ddclientd privilege separation user")
  893. (home-directory (string-append "/var/run/" ddclient-user))))))
  894. (define (ddclient-activation config)
  895. "Return the activation GEXP for CONFIG."
  896. (with-imported-modules '((guix build utils)
  897. (ice-9 rdelim))
  898. #~(begin
  899. (use-modules (guix build utils)
  900. (ice-9 rdelim))
  901. (let ((ddclient-user
  902. (passwd:uid (getpw #$(ddclient-configuration-user config))))
  903. (ddclient-group
  904. (passwd:gid (getpw #$(ddclient-configuration-group config))))
  905. (ddclient-secret-file
  906. #$(ddclient-configuration-secret-file config)))
  907. ;; 'ddclient' complains about ddclient.conf file permissions, which
  908. ;; rules out /gnu/store. Thus we copy the ddclient.conf to /etc.
  909. (for-each (lambda (dir)
  910. (mkdir-p dir)
  911. (chmod dir #o700)
  912. (chown dir ddclient-user ddclient-group))
  913. '("/var/cache/ddclient" "/var/run/ddclient"
  914. "/etc/ddclient"))
  915. (with-output-to-file "/etc/ddclient/ddclient.conf"
  916. (lambda ()
  917. (display
  918. (string-append
  919. "# Generated by 'ddclient-service'.\n\n"
  920. #$(with-output-to-string
  921. (lambda ()
  922. (serialize-configuration config
  923. ddclient-configuration-fields)))
  924. (if (string-null? ddclient-secret-file)
  925. ""
  926. (format #f "\n\n# Appended from '~a'.\n\n~a"
  927. ddclient-secret-file
  928. (with-input-from-file ddclient-secret-file
  929. read-string)))))))
  930. (chmod "/etc/ddclient/ddclient.conf" #o600)
  931. (chown "/etc/ddclient/ddclient.conf"
  932. ddclient-user ddclient-group)))))
  933. (define (ddclient-shepherd-service config)
  934. "Return a <shepherd-service> for ddclient with CONFIG."
  935. (let ((ddclient (ddclient-configuration-ddclient config))
  936. (ddclient-pid (ddclient-configuration-pid config))
  937. (ddclient-user (ddclient-configuration-user config))
  938. (ddclient-group (ddclient-configuration-group config)))
  939. (list (shepherd-service
  940. (provision '(ddclient))
  941. (documentation "Run ddclient daemon.")
  942. (start #~(make-forkexec-constructor
  943. (list #$(file-append ddclient "/bin/ddclient")
  944. "-foreground"
  945. "-file" "/etc/ddclient/ddclient.conf")
  946. #:pid-file #$ddclient-pid
  947. #:environment-variables
  948. (list "SSL_CERT_DIR=/run/current-system/profile\
  949. /etc/ssl/certs"
  950. "SSL_CERT_FILE=/run/current-system/profile\
  951. /etc/ssl/certs/ca-certificates.crt")
  952. #:user #$ddclient-user
  953. #:group #$ddclient-group))
  954. (stop #~(make-kill-destructor))))))
  955. (define ddclient-service-type
  956. (service-type
  957. (name 'ddclient)
  958. (extensions
  959. (list (service-extension account-service-type
  960. ddclient-account)
  961. (service-extension shepherd-root-service-type
  962. ddclient-shepherd-service)
  963. (service-extension activation-service-type
  964. ddclient-activation)))
  965. (default-value (ddclient-configuration))
  966. (description "Configure address updating utility for dynamic DNS services,
  967. ddclient.")))
  968. (define (generate-ddclient-documentation)
  969. (generate-documentation
  970. `((ddclient-configuration ,ddclient-configuration-fields))
  971. 'ddclient-configuration))