dns.scm 43 KB

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