dns.scm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2020 Pierre Langlois <pierre.langlois@gmx.com>
  4. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  5. ;;; Copyright © 2022 Remco van 't Veer <remco@remworks.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; 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 (guix modules)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (srfi srfi-34)
  35. #:use-module (srfi srfi-35)
  36. #:use-module (ice-9 match)
  37. #:use-module (ice-9 regex)
  38. #:export (knot-service-type
  39. knot-acl-configuration
  40. knot-key-configuration
  41. knot-keystore-configuration
  42. knot-zone-configuration
  43. knot-remote-configuration
  44. knot-policy-configuration
  45. knot-configuration
  46. define-zone-entries
  47. zone-file
  48. zone-entry
  49. knot-resolver-service-type
  50. knot-resolver-configuration
  51. dnsmasq-service-type
  52. dnsmasq-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 (* 12 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. (zonefile-sync knot-zone-configuration-zonefile-sync
  176. (default 0))
  177. (zonefile-load knot-zone-configuration-zonefile-load
  178. (default #f))
  179. (journal-content knot-zone-configuration-journal-content
  180. (default #f))
  181. (max-journal-usage knot-zone-configuration-max-journal-usage
  182. (default #f))
  183. (max-journal-depth knot-zone-configuration-max-journal-depth
  184. (default #f))
  185. (max-zone-size knot-zone-configuration-max-zone-size
  186. (default #f))
  187. (dnssec-policy knot-zone-configuration-dnssec-policy
  188. (default #f))
  189. (serial-policy knot-zone-configuration-serial-policy
  190. (default 'increment)))
  191. (define-record-type* <knot-remote-configuration>
  192. knot-remote-configuration make-knot-remote-configuration
  193. knot-remote-configuration?
  194. (id knot-remote-configuration-id
  195. (default ""))
  196. (address knot-remote-configuration-address
  197. (default '()))
  198. (via knot-remote-configuration-via
  199. (default '()))
  200. (key knot-remote-configuration-key
  201. (default #f)))
  202. (define-record-type* <knot-configuration>
  203. knot-configuration make-knot-configuration
  204. knot-configuration?
  205. (knot knot-configuration-knot
  206. (default knot))
  207. (run-directory knot-configuration-run-directory
  208. (default "/var/run/knot"))
  209. (includes knot-configuration-includes
  210. (default '()))
  211. (listen-v4 knot-configuration-listen-v4
  212. (default "0.0.0.0"))
  213. (listen-v6 knot-configuration-listen-v6
  214. (default "::"))
  215. (listen-port knot-configuration-listen-port
  216. (default 53))
  217. (keys knot-configuration-keys
  218. (default '()))
  219. (keystores knot-configuration-keystores
  220. (default '()))
  221. (acls knot-configuration-acls
  222. (default '()))
  223. (remotes knot-configuration-remotes
  224. (default '()))
  225. (policies knot-configuration-policies
  226. (default '()))
  227. (zones knot-configuration-zones
  228. (default '())))
  229. (define-syntax define-zone-entries
  230. (syntax-rules ()
  231. ((_ id (name ttl class type data) ...)
  232. (define id (list (make-zone-entry name ttl class type data) ...)))))
  233. (define (error-out msg)
  234. (raise (condition (&message (message msg)))))
  235. (define (verify-knot-key-configuration key)
  236. (unless (knot-key-configuration? key)
  237. (error-out "keys must be a list of only knot-key-configuration."))
  238. (let ((id (knot-key-configuration-id key)))
  239. (unless (and (string? id) (not (equal? id "")))
  240. (error-out "key id must be a non empty string.")))
  241. (unless (memq (knot-key-configuration-algorithm key)
  242. '(#f hmac-md5 hmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512))
  243. (error-out "algorithm must be one of: #f, 'hmac-md5, 'hmac-sha1,
  244. 'hmac-sha224, 'hmac-sha256, 'hmac-sha384 or 'hmac-sha512")))
  245. (define (verify-knot-keystore-configuration keystore)
  246. (unless (knot-keystore-configuration? keystore)
  247. (error-out "keystores must be a list of only knot-keystore-configuration."))
  248. (let ((id (knot-keystore-configuration-id keystore)))
  249. (unless (and (string? id) (not (equal? id "")))
  250. (error-out "keystore id must be a non empty string.")))
  251. (unless (memq (knot-keystore-configuration-backend keystore)
  252. '(pem pkcs11))
  253. (error-out "backend must be one of: 'pem or 'pkcs11")))
  254. (define (verify-knot-policy-configuration policy)
  255. (unless (knot-policy-configuration? policy)
  256. (error-out "policies must be a list of only knot-policy-configuration."))
  257. (let ((id (knot-policy-configuration-id policy)))
  258. (unless (and (string? id) (not (equal? id "")))
  259. (error-out "policy id must be a non empty string."))))
  260. (define (verify-knot-acl-configuration acl)
  261. (unless (knot-acl-configuration? acl)
  262. (error-out "acls must be a list of only knot-acl-configuration."))
  263. (let ((id (knot-acl-configuration-id acl))
  264. (address (knot-acl-configuration-address acl))
  265. (key (knot-acl-configuration-key acl))
  266. (action (knot-acl-configuration-action acl)))
  267. (unless (and (string? id) (not (equal? id "")))
  268. (error-out "acl id must be a non empty string."))
  269. (unless (and (list? address)
  270. (every string? address))
  271. (error-out "acl address must be a list of strings.")))
  272. (unless (boolean? (knot-acl-configuration-deny? acl))
  273. (error-out "deny? must be #t or #f.")))
  274. (define (verify-knot-zone-configuration zone)
  275. (unless (knot-zone-configuration? zone)
  276. (error-out "zones must be a list of only knot-zone-configuration."))
  277. (let ((domain (knot-zone-configuration-domain zone)))
  278. (unless (and (string? domain) (not (equal? domain "")))
  279. (error-out "zone domain must be a non empty string."))))
  280. (define (verify-knot-remote-configuration remote)
  281. (unless (knot-remote-configuration? remote)
  282. (error-out "remotes must be a list of only knot-remote-configuration."))
  283. (let ((id (knot-remote-configuration-id remote)))
  284. (unless (and (string? id) (not (equal? id "")))
  285. (error-out "remote id must be a non empty string."))))
  286. (define (verify-knot-configuration config)
  287. (unless (file-like? (knot-configuration-knot config))
  288. (error-out "knot configuration field must be a file-like object."))
  289. (unless (string? (knot-configuration-run-directory config))
  290. (error-out "run-directory must be a string."))
  291. (unless (list? (knot-configuration-includes config))
  292. (error-out "includes must be a list of strings or file-like objects."))
  293. (unless (list? (knot-configuration-keys config))
  294. (error-out "keys must be a list of knot-key-configuration."))
  295. (for-each (lambda (key) (verify-knot-key-configuration key))
  296. (knot-configuration-keys config))
  297. (unless (list? (knot-configuration-keystores config))
  298. (error-out "keystores must be a list of knot-keystore-configuration."))
  299. (for-each (lambda (keystore) (verify-knot-keystore-configuration keystore))
  300. (knot-configuration-keystores config))
  301. (unless (list? (knot-configuration-acls config))
  302. (error-out "acls must be a list of knot-acl-configuration."))
  303. (for-each (lambda (acl) (verify-knot-acl-configuration acl))
  304. (knot-configuration-acls config))
  305. (unless (list? (knot-configuration-zones config))
  306. (error-out "zones must be a list of knot-zone-configuration."))
  307. (for-each (lambda (zone) (verify-knot-zone-configuration zone))
  308. (knot-configuration-zones config))
  309. (unless (list? (knot-configuration-policies config))
  310. (error-out "policies must be a list of knot-policy-configuration."))
  311. (for-each (lambda (policy) (verify-knot-policy-configuration policy))
  312. (knot-configuration-policies config))
  313. (unless (list? (knot-configuration-remotes config))
  314. (error-out "remotes must be a list of knot-remote-configuration."))
  315. (for-each (lambda (remote) (verify-knot-remote-configuration remote))
  316. (knot-configuration-remotes config))
  317. #t)
  318. (define (format-string-list l)
  319. "Formats a list of string in YAML"
  320. (if (eq? l '())
  321. ""
  322. (let ((l (reverse l)))
  323. (string-append
  324. "["
  325. (fold (lambda (x1 x2)
  326. (string-append (if (symbol? x1) (symbol->string x1) x1) ", "
  327. (if (symbol? x2) (symbol->string x2) x2)))
  328. (if (symbol? (car l)) (symbol->string (car l)) (car l)) (cdr l))
  329. "]"))))
  330. (define (knot-acl-config acls)
  331. (with-output-to-string
  332. (lambda ()
  333. (for-each
  334. (lambda (acl-config)
  335. (let ((id (knot-acl-configuration-id acl-config))
  336. (address (knot-acl-configuration-address acl-config))
  337. (key (knot-acl-configuration-key acl-config))
  338. (action (knot-acl-configuration-action acl-config))
  339. (deny? (knot-acl-configuration-deny? acl-config)))
  340. (format #t " - id: ~a\n" id)
  341. (unless (eq? address '())
  342. (format #t " address: ~a\n" (format-string-list address)))
  343. (unless (eq? key '())
  344. (format #t " key: ~a\n" (format-string-list key)))
  345. (unless (eq? action '())
  346. (format #t " action: ~a\n" (format-string-list action)))
  347. (format #t " deny: ~a\n" (if deny? "on" "off"))))
  348. acls))))
  349. (define (knot-key-config keys)
  350. (with-output-to-string
  351. (lambda ()
  352. (for-each
  353. (lambda (key-config)
  354. (let ((id (knot-key-configuration-id key-config))
  355. (algorithm (knot-key-configuration-algorithm key-config))
  356. (secret (knot-key-configuration-secret key-config)))
  357. (format #t " - id: ~a\n" id)
  358. (if algorithm
  359. (format #t " algorithm: ~a\n" (symbol->string algorithm)))
  360. (format #t " secret: ~a\n" secret)))
  361. keys))))
  362. (define (knot-keystore-config keystores)
  363. (with-output-to-string
  364. (lambda ()
  365. (for-each
  366. (lambda (keystore-config)
  367. (let ((id (knot-keystore-configuration-id keystore-config))
  368. (backend (knot-keystore-configuration-backend keystore-config))
  369. (config (knot-keystore-configuration-config keystore-config)))
  370. (format #t " - id: ~a\n" id)
  371. (format #t " backend: ~a\n" (symbol->string backend))
  372. (format #t " config: \"~a\"\n" config)))
  373. keystores))))
  374. (define (knot-policy-config policies)
  375. (with-output-to-string
  376. (lambda ()
  377. (for-each
  378. (lambda (policy-config)
  379. (let ((id (knot-policy-configuration-id policy-config))
  380. (keystore (knot-policy-configuration-keystore policy-config))
  381. (manual? (knot-policy-configuration-manual? policy-config))
  382. (single-type-signing? (knot-policy-configuration-single-type-signing?
  383. policy-config))
  384. (algorithm (knot-policy-configuration-algorithm policy-config))
  385. (ksk-size (knot-policy-configuration-ksk-size policy-config))
  386. (zsk-size (knot-policy-configuration-zsk-size policy-config))
  387. (dnskey-ttl (knot-policy-configuration-dnskey-ttl policy-config))
  388. (zsk-lifetime (knot-policy-configuration-zsk-lifetime policy-config))
  389. (propagation-delay (knot-policy-configuration-propagation-delay
  390. policy-config))
  391. (rrsig-lifetime (knot-policy-configuration-rrsig-lifetime
  392. policy-config))
  393. (nsec3? (knot-policy-configuration-nsec3? policy-config))
  394. (nsec3-iterations (knot-policy-configuration-nsec3-iterations
  395. policy-config))
  396. (nsec3-salt-length (knot-policy-configuration-nsec3-salt-length
  397. policy-config))
  398. (nsec3-salt-lifetime (knot-policy-configuration-nsec3-salt-lifetime
  399. policy-config)))
  400. (format #t " - id: ~a\n" id)
  401. (format #t " keystore: ~a\n" keystore)
  402. (format #t " manual: ~a\n" (if manual? "on" "off"))
  403. (format #t " single-type-signing: ~a\n" (if single-type-signing?
  404. "on" "off"))
  405. (format #t " algorithm: ~a\n" algorithm)
  406. (format #t " ksk-size: ~a\n" (number->string ksk-size))
  407. (format #t " zsk-size: ~a\n" (number->string zsk-size))
  408. (unless (eq? dnskey-ttl 'default)
  409. (format #t " dnskey-ttl: ~a\n" dnskey-ttl))
  410. (format #t " zsk-lifetime: ~a\n" zsk-lifetime)
  411. (format #t " propagation-delay: ~a\n" propagation-delay)
  412. (format #t " rrsig-lifetime: ~a\n" rrsig-lifetime)
  413. (format #t " nsec3: ~a\n" (if nsec3? "on" "off"))
  414. (format #t " nsec3-iterations: ~a\n"
  415. (number->string nsec3-iterations))
  416. (format #t " nsec3-salt-length: ~a\n"
  417. (number->string nsec3-salt-length))
  418. (format #t " nsec3-salt-lifetime: ~a\n" nsec3-salt-lifetime)))
  419. policies))))
  420. (define (knot-remote-config remotes)
  421. (with-output-to-string
  422. (lambda ()
  423. (for-each
  424. (lambda (remote-config)
  425. (let ((id (knot-remote-configuration-id remote-config))
  426. (address (knot-remote-configuration-address remote-config))
  427. (via (knot-remote-configuration-via remote-config))
  428. (key (knot-remote-configuration-key remote-config)))
  429. (format #t " - id: ~a\n" id)
  430. (unless (eq? address '())
  431. (format #t " address: ~a\n" (format-string-list address)))
  432. (unless (eq? via '())
  433. (format #t " via: ~a\n" (format-string-list via)))
  434. (if key
  435. (format #t " key: ~a\n" key))))
  436. remotes))))
  437. (define (serialize-zone-entries entries)
  438. (with-output-to-string
  439. (lambda ()
  440. (for-each
  441. (lambda (entry)
  442. (let ((name (zone-entry-name entry))
  443. (ttl (zone-entry-ttl entry))
  444. (class (zone-entry-class entry))
  445. (type (zone-entry-type entry))
  446. (data (zone-entry-data entry)))
  447. (format #t "~a ~a ~a ~a ~a\n" name ttl class type data)))
  448. entries))))
  449. (define (serialize-zone-file zone domain)
  450. (computed-file (string-append domain ".zone")
  451. #~(begin
  452. (call-with-output-file #$output
  453. (lambda (port)
  454. (format port "$ORIGIN ~a.\n"
  455. #$(zone-file-origin zone))
  456. (format port "@ IN SOA ~a ~a (~a ~a ~a ~a ~a)\n"
  457. #$(zone-file-ns zone)
  458. #$(zone-file-mail zone)
  459. #$(zone-file-serial zone)
  460. #$(zone-file-refresh zone)
  461. #$(zone-file-retry zone)
  462. #$(zone-file-expiry zone)
  463. #$(zone-file-nx zone))
  464. (format port "~a\n"
  465. #$(serialize-zone-entries (zone-file-entries zone))))))))
  466. (define (knot-zone-config zone)
  467. (let ((content (knot-zone-configuration-zone zone)))
  468. #~(with-output-to-string
  469. (lambda ()
  470. (let ((domain #$(knot-zone-configuration-domain zone))
  471. (file #$(knot-zone-configuration-file zone))
  472. (master (list #$@(knot-zone-configuration-master zone)))
  473. (ddns-master #$(knot-zone-configuration-ddns-master zone))
  474. (notify (list #$@(knot-zone-configuration-notify zone)))
  475. (acl (list #$@(knot-zone-configuration-acl zone)))
  476. (semantic-checks? #$(knot-zone-configuration-semantic-checks? zone))
  477. (zonefile-sync #$(knot-zone-configuration-zonefile-sync zone))
  478. (zonefile-load '#$(knot-zone-configuration-zonefile-load zone))
  479. (journal-content #$(knot-zone-configuration-journal-content zone))
  480. (max-journal-usage #$(knot-zone-configuration-max-journal-usage zone))
  481. (max-journal-depth #$(knot-zone-configuration-max-journal-depth zone))
  482. (max-zone-size #$(knot-zone-configuration-max-zone-size zone))
  483. (dnssec-policy #$(knot-zone-configuration-dnssec-policy zone))
  484. (serial-policy '#$(knot-zone-configuration-serial-policy zone)))
  485. (format #t " - domain: ~a\n" domain)
  486. (if (eq? master '())
  487. ;; This server is a master
  488. (if (equal? file "")
  489. (format #t " file: ~a\n"
  490. #$(serialize-zone-file content
  491. (knot-zone-configuration-domain zone)))
  492. (format #t " file: ~a\n" file))
  493. ;; This server is a slave (has masters)
  494. (begin
  495. (format #t " master: ~a\n"
  496. #$(format-string-list
  497. (knot-zone-configuration-master zone)))
  498. (if ddns-master (format #t " ddns-master ~a\n" ddns-master))))
  499. (unless (eq? notify '())
  500. (format #t " notify: ~a\n"
  501. #$(format-string-list
  502. (knot-zone-configuration-notify zone))))
  503. (unless (eq? acl '())
  504. (format #t " acl: ~a\n"
  505. #$(format-string-list
  506. (knot-zone-configuration-acl zone))))
  507. (format #t " semantic-checks: ~a\n" (if semantic-checks? "on" "off"))
  508. (if zonefile-sync
  509. (format #t " zonefile-sync: ~a\n" zonefile-sync))
  510. (if zonefile-load
  511. (format #t " zonefile-load: ~a\n"
  512. (symbol->string zonefile-load)))
  513. (if journal-content
  514. (format #t " journal-content: ~a\n"
  515. (symbol->string journal-content)))
  516. (if max-journal-usage
  517. (format #t " max-journal-usage: ~a\n" max-journal-usage))
  518. (if max-journal-depth
  519. (format #t " max-journal-depth: ~a\n" max-journal-depth))
  520. (if max-zone-size
  521. (format #t " max-zone-size: ~a\n" max-zone-size))
  522. (if dnssec-policy
  523. (begin
  524. (format #t " dnssec-signing: on\n")
  525. (format #t " dnssec-policy: ~a\n" dnssec-policy)))
  526. (format #t " serial-policy: ~a\n"
  527. (symbol->string serial-policy)))))))
  528. (define (knot-config-file config)
  529. (verify-knot-configuration config)
  530. (computed-file "knot.conf"
  531. #~(begin
  532. (call-with-output-file #$output
  533. (lambda (port)
  534. (for-each (lambda (inc)
  535. (format port "include: ~a\n" inc))
  536. '#$(knot-configuration-includes config))
  537. (format port "server:\n")
  538. (format port " rundir: ~a\n" #$(knot-configuration-run-directory config))
  539. (format port " user: knot\n")
  540. (format port " listen: ~a@~a\n"
  541. #$(knot-configuration-listen-v4 config)
  542. #$(knot-configuration-listen-port config))
  543. (format port " listen: ~a@~a\n"
  544. #$(knot-configuration-listen-v6 config)
  545. #$(knot-configuration-listen-port config))
  546. (format port "\nkey:\n")
  547. (format port #$(knot-key-config (knot-configuration-keys config)))
  548. (format port "\nkeystore:\n")
  549. (format port #$(knot-keystore-config (knot-configuration-keystores config)))
  550. (format port "\nacl:\n")
  551. (format port #$(knot-acl-config (knot-configuration-acls config)))
  552. (format port "\nremote:\n")
  553. (format port #$(knot-remote-config (knot-configuration-remotes config)))
  554. (format port "\npolicy:\n")
  555. (format port #$(knot-policy-config (knot-configuration-policies config)))
  556. (unless #$(eq? (knot-configuration-zones config) '())
  557. (format port "\nzone:\n")
  558. (format port "~a\n"
  559. (string-concatenate
  560. (list #$@(map knot-zone-config
  561. (knot-configuration-zones config)))))))))))
  562. (define %knot-accounts
  563. (list (user-group (name "knot") (system? #t))
  564. (user-account
  565. (name "knot")
  566. (group "knot")
  567. (system? #t)
  568. (comment "knot dns server user")
  569. (home-directory "/var/empty")
  570. (shell (file-append shadow "/sbin/nologin")))))
  571. (define (knot-activation config)
  572. (with-imported-modules (source-module-closure '((gnu build activation)))
  573. #~(begin
  574. (use-modules (gnu build activation))
  575. (mkdir-p/perms #$(knot-configuration-run-directory config)
  576. (getpwnam "knot") #o755)
  577. (mkdir-p/perms "/var/lib/knot" (getpwnam "knot") #o755)
  578. (mkdir-p/perms "/var/lib/knot/keys" (getpwnam "knot") #o755)
  579. (mkdir-p/perms "/var/lib/knot/keys/keys" (getpwnam "knot") #o755))))
  580. (define (knot-shepherd-service config)
  581. (let* ((config-file (knot-config-file config))
  582. (knot (knot-configuration-knot config)))
  583. (list (shepherd-service
  584. (documentation "Run the Knot DNS daemon.")
  585. (provision '(knot dns))
  586. (requirement '(networking))
  587. (actions (list (shepherd-configuration-action config-file)))
  588. (start #~(make-forkexec-constructor
  589. (list (string-append #$knot "/sbin/knotd")
  590. "-c" #$config-file)))
  591. (stop #~(make-kill-destructor))))))
  592. (define knot-service-type
  593. (service-type (name 'knot)
  594. (extensions
  595. (list (service-extension shepherd-root-service-type
  596. knot-shepherd-service)
  597. (service-extension activation-service-type
  598. knot-activation)
  599. (service-extension account-service-type
  600. (const %knot-accounts))))
  601. (description
  602. "Run @uref{https://www.knot-dns.cz/, Knot}, an authoritative
  603. name server for the @acronym{DNS, Domain Name System}.")))
  604. ;;;
  605. ;;; Knot Resolver.
  606. ;;;
  607. (define-record-type* <knot-resolver-configuration>
  608. knot-resolver-configuration
  609. make-knot-resolver-configuration
  610. knot-resolver-configuration?
  611. (package knot-resolver-configuration-package
  612. (default knot-resolver))
  613. (kresd-config-file knot-resolver-kresd-config-file
  614. (default %kresd.conf))
  615. (garbage-collection-interval knot-resolver-garbage-collection-interval
  616. (default 1000)))
  617. (define %kresd.conf
  618. (plain-file "kresd.conf" "-- -*- mode: lua -*-
  619. trust_anchors.add_file('/var/cache/knot-resolver/root.keys')
  620. net = { '127.0.0.1', '::1' }
  621. user('knot-resolver', 'knot-resolver')
  622. modules = { 'hints > iterate', 'stats', 'predict' }
  623. cache.size = 100 * MB
  624. "))
  625. (define %knot-resolver-accounts
  626. (list (user-group
  627. (name "knot-resolver")
  628. (system? #t))
  629. (user-account
  630. (name "knot-resolver")
  631. (group "knot-resolver")
  632. (system? #t)
  633. (home-directory "/var/cache/knot-resolver")
  634. (shell (file-append shadow "/sbin/nologin")))))
  635. (define (knot-resolver-activation config)
  636. #~(begin
  637. (use-modules (guix build utils))
  638. (let ((rundir "/var/cache/knot-resolver")
  639. (owner (getpwnam "knot-resolver")))
  640. (mkdir-p rundir)
  641. (chown rundir (passwd:uid owner) (passwd:gid owner)))))
  642. (define knot-resolver-shepherd-services
  643. (match-lambda
  644. (($ <knot-resolver-configuration> package
  645. kresd-config-file
  646. garbage-collection-interval)
  647. (list
  648. (shepherd-service
  649. (provision '(kresd))
  650. (requirement '(networking))
  651. (documentation "Run the Knot Resolver daemon.")
  652. (start #~(make-forkexec-constructor
  653. '(#$(file-append package "/sbin/kresd")
  654. "-c" #$kresd-config-file "-n"
  655. "/var/cache/knot-resolver")))
  656. (stop #~(make-kill-destructor)))
  657. (shepherd-service
  658. (provision '(kres-cache-gc))
  659. (requirement '(user-processes))
  660. (documentation "Run the Knot Resolver Garbage Collector daemon.")
  661. (start #~(make-forkexec-constructor
  662. '(#$(file-append package "/sbin/kres-cache-gc")
  663. "-d" #$(number->string garbage-collection-interval)
  664. "-c" "/var/cache/knot-resolver")
  665. #:user "knot-resolver"
  666. #:group "knot-resolver"))
  667. (stop #~(make-kill-destructor)))))))
  668. (define knot-resolver-service-type
  669. (service-type
  670. (name 'knot-resolver)
  671. (extensions
  672. (list (service-extension shepherd-root-service-type
  673. knot-resolver-shepherd-services)
  674. (service-extension activation-service-type
  675. knot-resolver-activation)
  676. (service-extension account-service-type
  677. (const %knot-resolver-accounts))))
  678. (default-value (knot-resolver-configuration))
  679. (description "Run the Knot DNS Resolver.")))
  680. ;;;
  681. ;;; Dnsmasq.
  682. ;;;
  683. (define-record-type* <dnsmasq-configuration>
  684. dnsmasq-configuration make-dnsmasq-configuration
  685. dnsmasq-configuration?
  686. (package dnsmasq-configuration-package
  687. (default dnsmasq)) ;file-like
  688. (no-hosts? dnsmasq-configuration-no-hosts?
  689. (default #f)) ;boolean
  690. (port dnsmasq-configuration-port
  691. (default 53)) ;integer
  692. (local-service? dnsmasq-configuration-local-service?
  693. (default #t)) ;boolean
  694. (listen-addresses dnsmasq-configuration-listen-address
  695. (default '())) ;list of string
  696. (resolv-file dnsmasq-configuration-resolv-file
  697. (default "/etc/resolv.conf")) ;string
  698. (no-resolv? dnsmasq-configuration-no-resolv?
  699. (default #f)) ;boolean
  700. (forward-private-reverse-lookup?
  701. dnsmasq-configuration-forward-private-reverse-lookup?
  702. (default #t)) ;boolean
  703. (query-servers-in-order?
  704. dnsmasq-configuration-query-servers-in-order?
  705. (default #f)) ;boolean
  706. (servers dnsmasq-configuration-servers
  707. (default '())) ;list of string
  708. (servers-file dnsmasq-configuration-servers-file
  709. (default #f)) ;string|file-like
  710. (addresses dnsmasq-configuration-addresses
  711. (default '())) ;list of string
  712. (cache-size dnsmasq-configuration-cache-size
  713. (default 150)) ;integer
  714. (negative-cache? dnsmasq-configuration-negative-cache?
  715. (default #t)) ;boolean
  716. (cpe-id dnsmasq-configuration-cpe-id
  717. (default #f)) ;string
  718. (tftp-enable? dnsmasq-configuration-tftp-enable?
  719. (default #f)) ;boolean
  720. (tftp-no-fail? dnsmasq-configuration-tftp-no-fail?
  721. (default #f)) ;boolean
  722. (tftp-single-port? dnsmasq-configuration-tftp-single-port?
  723. (default #f)) ;boolean
  724. (tftp-secure? dnsmasq-tftp-secure?
  725. (default #f)) ;boolean
  726. (tftp-max dnsmasq-tftp-max
  727. (default #f)) ;integer
  728. (tftp-mtu dnsmasq-tftp-mtu
  729. (default #f)) ;integer
  730. (tftp-no-blocksize? dnsmasq-tftp-no-blocksize?
  731. (default #f)) ;boolean
  732. (tftp-lowercase? dnsmasq-tftp-lowercase?
  733. (default #f)) ;boolean
  734. (tftp-port-range dnsmasq-tftp-port-range
  735. (default #f)) ;string
  736. (tftp-root dnsmasq-tftp-root
  737. (default "/var/empty,lo")) ;string
  738. (tftp-unique-root dnsmasq-tftp-unique-root
  739. (default #f))) ;"" or "ip" or "mac"
  740. (define (dnsmasq-shepherd-service config)
  741. (match-record config <dnsmasq-configuration>
  742. (package
  743. no-hosts?
  744. port local-service? listen-addresses
  745. resolv-file no-resolv?
  746. forward-private-reverse-lookup? query-servers-in-order?
  747. servers addresses servers-file
  748. cache-size negative-cache?
  749. cpe-id
  750. tftp-enable? tftp-no-fail?
  751. tftp-single-port? tftp-secure?
  752. tftp-max tftp-mtu tftp-no-blocksize?
  753. tftp-lowercase? tftp-port-range
  754. tftp-root tftp-unique-root)
  755. (shepherd-service
  756. (provision '(dnsmasq))
  757. (requirement '(networking))
  758. (documentation "Run the dnsmasq DNS server.")
  759. (start #~(make-forkexec-constructor
  760. (list
  761. #$(file-append package "/sbin/dnsmasq")
  762. "--keep-in-foreground"
  763. "--pid-file=/run/dnsmasq.pid"
  764. #$@(if no-hosts?
  765. '("--no-hosts")
  766. '())
  767. #$(format #f "--port=~a" port)
  768. #$@(if local-service?
  769. '("--local-service")
  770. '())
  771. #$@(map (cut format #f "--listen-address=~a" <>)
  772. listen-addresses)
  773. #$(format #f "--resolv-file=~a" resolv-file)
  774. #$@(if no-resolv?
  775. '("--no-resolv")
  776. '())
  777. #$@(if forward-private-reverse-lookup?
  778. '()
  779. '("--bogus-priv"))
  780. #$@(if query-servers-in-order?
  781. '("--strict-order")
  782. '())
  783. #$@(if servers-file
  784. (list #~(string-append "--servers-file=" #$servers-file))
  785. '())
  786. #$@(map (cut format #f "--server=~a" <>)
  787. servers)
  788. #$@(map (cut format #f "--address=~a" <>)
  789. addresses)
  790. #$(format #f "--cache-size=~a" cache-size)
  791. #$@(if negative-cache?
  792. '()
  793. '("--no-negcache"))
  794. #$@(if cpe-id
  795. (list (format #f "--add-cpe-id=~a" cpe-id))
  796. '())
  797. #$@(if tftp-enable?
  798. '("--enable-tftp")
  799. '())
  800. #$@(if tftp-no-fail?
  801. '("--tftp-no-fail")
  802. '())
  803. #$@(if tftp-single-port?
  804. '("--tftp-single-port")
  805. '())
  806. #$@(if tftp-secure?
  807. '("--tftp-secure")
  808. '())
  809. #$@(if tftp-max
  810. (list (format #f "--tftp-max=~a" tftp-max))
  811. '())
  812. #$@(if tftp-mtu
  813. (list (format #f "--tftp-mtu=~a" tftp-mtu))
  814. '())
  815. #$@(if tftp-no-blocksize?
  816. '("--tftp-no-blocksize")
  817. '())
  818. #$@(if tftp-lowercase?
  819. '("--tftp-lowercase")
  820. '())
  821. #$@(if tftp-port-range
  822. (list (format #f "--tftp-port-range=~a"
  823. tftp-port-range))
  824. '())
  825. #$@(if tftp-root
  826. (list (format #f "--tftp-root=~a" tftp-root))
  827. '())
  828. #$@(if tftp-unique-root
  829. (list
  830. (if (> (length tftp-unique-root) 0)
  831. (format #f "--tftp-unique-root=~a" tftp-unique-root)
  832. (format #f "--tftp-unique-root")))
  833. '()))
  834. #:pid-file "/run/dnsmasq.pid"))
  835. (stop #~(make-kill-destructor)))))
  836. (define (dnsmasq-activation config)
  837. #~(begin
  838. (use-modules (guix build utils))
  839. ;; create directory to store dnsmasq lease file
  840. (mkdir-p "/var/lib/misc")))
  841. (define dnsmasq-service-type
  842. (service-type
  843. (name 'dnsmasq)
  844. (extensions
  845. (list (service-extension shepherd-root-service-type
  846. (compose list dnsmasq-shepherd-service))
  847. (service-extension activation-service-type
  848. dnsmasq-activation)))
  849. (default-value (dnsmasq-configuration))
  850. (description "Run the dnsmasq DNS server.")))