dns.scm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu services dns)
  20. #:use-module (gnu services)
  21. #:use-module (gnu services configuration)
  22. #:use-module (gnu services shepherd)
  23. #:use-module (gnu system shadow)
  24. #:use-module (gnu packages admin)
  25. #:use-module (gnu packages dns)
  26. #:use-module (guix packages)
  27. #:use-module (guix records)
  28. #:use-module (guix gexp)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-26)
  31. #:use-module (srfi srfi-34)
  32. #:use-module (srfi srfi-35)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 regex)
  35. #:export (knot-service-type
  36. knot-acl-configuration
  37. knot-key-configuration
  38. knot-keystore-configuration
  39. knot-zone-configuration
  40. knot-remote-configuration
  41. knot-policy-configuration
  42. knot-configuration
  43. define-zone-entries
  44. zone-file
  45. zone-entry
  46. dnsmasq-service-type
  47. dnsmasq-configuration
  48. ddclient-service-type
  49. ddclient-configuration))
  50. ;;;
  51. ;;; Knot DNS.
  52. ;;;
  53. (define-record-type* <knot-key-configuration>
  54. knot-key-configuration make-knot-key-configuration
  55. knot-key-configuration?
  56. (id knot-key-configuration-id
  57. (default ""))
  58. (algorithm knot-key-configuration-algorithm
  59. (default #f)); one of #f, or an algorithm name
  60. (secret knot-key-configuration-secret
  61. (default "")))
  62. (define-record-type* <knot-acl-configuration>
  63. knot-acl-configuration make-knot-acl-configuration
  64. knot-acl-configuration?
  65. (id knot-acl-configuration-id
  66. (default ""))
  67. (address knot-acl-configuration-address
  68. (default '()))
  69. (key knot-acl-configuration-key
  70. (default '()))
  71. (action knot-acl-configuration-action
  72. (default '()))
  73. (deny? knot-acl-configuration-deny?
  74. (default #f)))
  75. (define-record-type* <zone-entry>
  76. zone-entry make-zone-entry
  77. zone-entry?
  78. (name zone-entry-name
  79. (default "@"))
  80. (ttl zone-entry-ttl
  81. (default ""))
  82. (class zone-entry-class
  83. (default "IN"))
  84. (type zone-entry-type
  85. (default "A"))
  86. (data zone-entry-data
  87. (default "")))
  88. (define-record-type* <zone-file>
  89. zone-file make-zone-file
  90. zone-file?
  91. (entries zone-file-entries
  92. (default '()))
  93. (origin zone-file-origin
  94. (default ""))
  95. (ns zone-file-ns
  96. (default "ns"))
  97. (mail zone-file-mail
  98. (default "hostmaster"))
  99. (serial zone-file-serial
  100. (default 1))
  101. (refresh zone-file-refresh
  102. (default (* 2 24 3600)))
  103. (retry zone-file-retry
  104. (default (* 15 60)))
  105. (expiry zone-file-expiry
  106. (default (* 2 7 24 3600)))
  107. (nx zone-file-nx
  108. (default 3600)))
  109. (define-record-type* <knot-keystore-configuration>
  110. knot-keystore-configuration make-knot-keystore-configuration
  111. knot-keystore-configuration?
  112. (id knot-keystore-configuration-id
  113. (default ""))
  114. (backend knot-keystore-configuration-backend
  115. (default 'pem))
  116. (config knot-keystore-configuration-config
  117. (default "/var/lib/knot/keys/keys")))
  118. (define-record-type* <knot-policy-configuration>
  119. knot-policy-configuration make-knot-policy-configuration
  120. knot-policy-configuration?
  121. (id knot-policy-configuration-id
  122. (default ""))
  123. (keystore knot-policy-configuration-keystore
  124. (default "default"))
  125. (manual? knot-policy-configuration-manual?
  126. (default #f))
  127. (single-type-signing? knot-policy-configuration-single-type-signing?
  128. (default #f))
  129. (algorithm knot-policy-configuration-algorithm
  130. (default "ecdsap256sha256"))
  131. (ksk-size knot-policy-configuration-ksk-size
  132. (default 256))
  133. (zsk-size knot-policy-configuration-zsk-size
  134. (default 256))
  135. (dnskey-ttl knot-policy-configuration-dnskey-ttl
  136. (default 'default))
  137. (zsk-lifetime knot-policy-configuration-zsk-lifetime
  138. (default (* 30 24 3600)))
  139. (propagation-delay knot-policy-configuration-propagation-delay
  140. (default (* 24 3600)))
  141. (rrsig-lifetime knot-policy-configuration-rrsig-lifetime
  142. (default (* 14 24 3600)))
  143. (rrsig-refresh knot-policy-configuration-rrsig-refresh
  144. (default (* 7 24 3600)))
  145. (nsec3? knot-policy-configuration-nsec3?
  146. (default #f))
  147. (nsec3-iterations knot-policy-configuration-nsec3-iterations
  148. (default 5))
  149. (nsec3-salt-length knot-policy-configuration-nsec3-salt-length
  150. (default 8))
  151. (nsec3-salt-lifetime knot-policy-configuration-nsec3-salt-lifetime
  152. (default (* 30 24 3600))))
  153. (define-record-type* <knot-zone-configuration>
  154. knot-zone-configuration make-knot-zone-configuration
  155. knot-zone-configuration?
  156. (domain knot-zone-configuration-domain
  157. (default ""))
  158. (file knot-zone-configuration-file
  159. (default "")) ; the file where this zone is saved.
  160. (zone knot-zone-configuration-zone
  161. (default (zone-file))) ; initial content of the zone file
  162. (master knot-zone-configuration-master
  163. (default '()))
  164. (ddns-master knot-zone-configuration-ddns-master
  165. (default #f))
  166. (notify knot-zone-configuration-notify
  167. (default '()))
  168. (acl knot-zone-configuration-acl
  169. (default '()))
  170. (semantic-checks? knot-zone-configuration-semantic-checks?
  171. (default #f))
  172. (disable-any? knot-zone-configuration-disable-any?
  173. (default #f))
  174. (zonefile-sync knot-zone-configuration-zonefile-sync
  175. (default 0))
  176. (dnssec-policy knot-zone-configuration-dnssec-policy
  177. (default #f))
  178. (serial-policy knot-zone-configuration-serial-policy
  179. (default 'increment)))
  180. (define-record-type* <knot-remote-configuration>
  181. knot-remote-configuration make-knot-remote-configuration
  182. knot-remote-configuration?
  183. (id knot-remote-configuration-id
  184. (default ""))
  185. (address knot-remote-configuration-address
  186. (default '()))
  187. (via knot-remote-configuration-via
  188. (default '()))
  189. (key knot-remote-configuration-key
  190. (default #f)))
  191. (define-record-type* <knot-configuration>
  192. knot-configuration make-knot-configuration
  193. knot-configuration?
  194. (knot knot-configuration-knot
  195. (default knot))
  196. (run-directory knot-configuration-run-directory
  197. (default "/var/run/knot"))
  198. (listen-v4 knot-configuration-listen-v4
  199. (default "0.0.0.0"))
  200. (listen-v6 knot-configuration-listen-v6
  201. (default "::"))
  202. (listen-port knot-configuration-listen-port
  203. (default 53))
  204. (keys knot-configuration-keys
  205. (default '()))
  206. (keystores knot-configuration-keystores
  207. (default '()))
  208. (acls knot-configuration-acls
  209. (default '()))
  210. (remotes knot-configuration-remotes
  211. (default '()))
  212. (policies knot-configuration-policies
  213. (default '()))
  214. (zones knot-configuration-zones
  215. (default '())))
  216. (define-syntax define-zone-entries
  217. (syntax-rules ()
  218. ((_ id (name ttl class type data) ...)
  219. (define id (list (make-zone-entry name ttl class type data) ...)))))
  220. (define (error-out msg)
  221. (raise (condition (&message (message msg)))))
  222. (define (verify-knot-key-configuration key)
  223. (unless (knot-key-configuration? key)
  224. (error-out "keys must be a list of only knot-key-configuration."))
  225. (let ((id (knot-key-configuration-id key)))
  226. (unless (and (string? id) (not (equal? id "")))
  227. (error-out "key id must be a non empty string.")))
  228. (unless (memq '(#f hmac-md5 hmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512)
  229. (knot-key-configuration-algorithm key))
  230. (error-out "algorithm must be one of: #f, 'hmac-md5, 'hmac-sha1,
  231. 'hmac-sha224, 'hmac-sha256, 'hmac-sha384 or 'hmac-sha512")))
  232. (define (verify-knot-keystore-configuration keystore)
  233. (unless (knot-keystore-configuration? keystore)
  234. (error-out "keystores must be a list of only knot-keystore-configuration."))
  235. (let ((id (knot-keystore-configuration-id keystore)))
  236. (unless (and (string? id) (not (equal? id "")))
  237. (error-out "keystore id must be a non empty string.")))
  238. (unless (memq '(pem pkcs11)
  239. (knot-keystore-configuration-backend keystore))
  240. (error-out "backend must be one of: 'pem or 'pkcs11")))
  241. (define (verify-knot-policy-configuration policy)
  242. (unless (knot-policy-configuration? policy)
  243. (error-out "policies must be a list of only knot-policy-configuration."))
  244. (let ((id (knot-policy-configuration-id policy)))
  245. (unless (and (string? id) (not (equal? id "")))
  246. (error-out "policy id must be a non empty string."))))
  247. (define (verify-knot-acl-configuration acl)
  248. (unless (knot-acl-configuration? acl)
  249. (error-out "acls must be a list of only knot-acl-configuration."))
  250. (let ((id (knot-acl-configuration-id acl))
  251. (address (knot-acl-configuration-address acl))
  252. (key (knot-acl-configuration-key acl))
  253. (action (knot-acl-configuration-action acl)))
  254. (unless (and (string? id) (not (equal? id "")))
  255. (error-out "acl id must be a non empty string."))
  256. (unless (and (list? address)
  257. (fold (lambda (x1 x2) (and (string? x1) (string? x2))) "" address))
  258. (error-out "acl address must be a list of strings.")))
  259. (unless (boolean? (knot-acl-configuration-deny? acl))
  260. (error-out "deny? must be #t or #f.")))
  261. (define (verify-knot-zone-configuration zone)
  262. (unless (knot-zone-configuration? zone)
  263. (error-out "zones must be a list of only knot-zone-configuration."))
  264. (let ((domain (knot-zone-configuration-domain zone)))
  265. (unless (and (string? domain) (not (equal? domain "")))
  266. (error-out "zone domain must be a non empty string."))))
  267. (define (verify-knot-remote-configuration remote)
  268. (unless (knot-remote-configuration? remote)
  269. (error-out "remotes must be a list of only knot-remote-configuration."))
  270. (let ((id (knot-remote-configuration-id remote)))
  271. (unless (and (string? id) (not (equal? id "")))
  272. (error-out "remote id must be a non empty string."))))
  273. (define (verify-knot-configuration config)
  274. (unless (package? (knot-configuration-knot config))
  275. (error-out "knot configuration field must be a package."))
  276. (unless (string? (knot-configuration-run-directory config))
  277. (error-out "run-directory must be a string."))
  278. (unless (list? (knot-configuration-keys config))
  279. (error-out "keys must be a list of knot-key-configuration."))
  280. (for-each (lambda (key) (verify-knot-key-configuration key))
  281. (knot-configuration-keys config))
  282. (unless (list? (knot-configuration-keystores config))
  283. (error-out "keystores must be a list of knot-keystore-configuration."))
  284. (for-each (lambda (keystore) (verify-knot-keystore-configuration keystore))
  285. (knot-configuration-keystores config))
  286. (unless (list? (knot-configuration-acls config))
  287. (error-out "acls must be a list of knot-acl-configuration."))
  288. (for-each (lambda (acl) (verify-knot-acl-configuration acl))
  289. (knot-configuration-acls config))
  290. (unless (list? (knot-configuration-zones config))
  291. (error-out "zones must be a list of knot-zone-configuration."))
  292. (for-each (lambda (zone) (verify-knot-zone-configuration zone))
  293. (knot-configuration-zones config))
  294. (unless (list? (knot-configuration-policies config))
  295. (error-out "policies must be a list of knot-policy-configuration."))
  296. (for-each (lambda (policy) (verify-knot-policy-configuration policy))
  297. (knot-configuration-policies config))
  298. (unless (list? (knot-configuration-remotes config))
  299. (error-out "remotes must be a list of knot-remote-configuration."))
  300. (for-each (lambda (remote) (verify-knot-remote-configuration remote))
  301. (knot-configuration-remotes config))
  302. #t)
  303. (define (format-string-list l)
  304. "Formats a list of string in YAML"
  305. (if (eq? l '())
  306. ""
  307. (let ((l (reverse l)))
  308. (string-append
  309. "["
  310. (fold (lambda (x1 x2)
  311. (string-append (if (symbol? x1) (symbol->string x1) x1) ", "
  312. (if (symbol? x2) (symbol->string x2) x2)))
  313. (car l) (cdr l))
  314. "]"))))
  315. (define (knot-acl-config acls)
  316. (with-output-to-string
  317. (lambda ()
  318. (for-each
  319. (lambda (acl-config)
  320. (let ((id (knot-acl-configuration-id acl-config))
  321. (address (knot-acl-configuration-address acl-config))
  322. (key (knot-acl-configuration-key acl-config))
  323. (action (knot-acl-configuration-action acl-config))
  324. (deny? (knot-acl-configuration-deny? acl-config)))
  325. (format #t " - id: ~a\n" id)
  326. (unless (eq? address '())
  327. (format #t " address: ~a\n" (format-string-list address)))
  328. (unless (eq? key '())
  329. (format #t " key: ~a\n" (format-string-list key)))
  330. (unless (eq? action '())
  331. (format #t " action: ~a\n" (format-string-list action)))
  332. (format #t " deny: ~a\n" (if deny? "on" "off"))))
  333. acls))))
  334. (define (knot-key-config keys)
  335. (with-output-to-string
  336. (lambda ()
  337. (for-each
  338. (lambda (key-config)
  339. (let ((id (knot-key-configuration-id key-config))
  340. (algorithm (knot-key-configuration-algorithm key-config))
  341. (secret (knot-key-configuration-secret key-config)))
  342. (format #t " - id: ~a\n" id)
  343. (if algorithm
  344. (format #t " algorithm: ~a\n" (symbol->string algorithm)))
  345. (format #t " secret: ~a\n" secret)))
  346. keys))))
  347. (define (knot-keystore-config keystores)
  348. (with-output-to-string
  349. (lambda ()
  350. (for-each
  351. (lambda (keystore-config)
  352. (let ((id (knot-keystore-configuration-id keystore-config))
  353. (backend (knot-keystore-configuration-backend keystore-config))
  354. (config (knot-keystore-configuration-config keystore-config)))
  355. (format #t " - id: ~a\n" id)
  356. (format #t " backend: ~a\n" (symbol->string backend))
  357. (format #t " config: \"~a\"\n" config)))
  358. keystores))))
  359. (define (knot-policy-config policies)
  360. (with-output-to-string
  361. (lambda ()
  362. (for-each
  363. (lambda (policy-config)
  364. (let ((id (knot-policy-configuration-id policy-config))
  365. (keystore (knot-policy-configuration-keystore policy-config))
  366. (manual? (knot-policy-configuration-manual? policy-config))
  367. (single-type-signing? (knot-policy-configuration-single-type-signing?
  368. policy-config))
  369. (algorithm (knot-policy-configuration-algorithm policy-config))
  370. (ksk-size (knot-policy-configuration-ksk-size policy-config))
  371. (zsk-size (knot-policy-configuration-zsk-size policy-config))
  372. (dnskey-ttl (knot-policy-configuration-dnskey-ttl policy-config))
  373. (zsk-lifetime (knot-policy-configuration-zsk-lifetime policy-config))
  374. (propagation-delay (knot-policy-configuration-propagation-delay
  375. policy-config))
  376. (rrsig-lifetime (knot-policy-configuration-rrsig-lifetime
  377. policy-config))
  378. (nsec3? (knot-policy-configuration-nsec3? policy-config))
  379. (nsec3-iterations (knot-policy-configuration-nsec3-iterations
  380. policy-config))
  381. (nsec3-salt-length (knot-policy-configuration-nsec3-salt-length
  382. policy-config))
  383. (nsec3-salt-lifetime (knot-policy-configuration-nsec3-salt-lifetime
  384. policy-config)))
  385. (format #t " - id: ~a\n" id)
  386. (format #t " keystore: ~a\n" keystore)
  387. (format #t " manual: ~a\n" (if manual? "on" "off"))
  388. (format #t " single-type-signing: ~a\n" (if single-type-signing?
  389. "on" "off"))
  390. (format #t " algorithm: ~a\n" algorithm)
  391. (format #t " ksk-size: ~a\n" (number->string ksk-size))
  392. (format #t " zsk-size: ~a\n" (number->string zsk-size))
  393. (unless (eq? dnskey-ttl 'default)
  394. (format #t " dnskey-ttl: ~a\n" dnskey-ttl))
  395. (format #t " zsk-lifetime: ~a\n" zsk-lifetime)
  396. (format #t " propagation-delay: ~a\n" propagation-delay)
  397. (format #t " rrsig-lifetime: ~a\n" rrsig-lifetime)
  398. (format #t " nsec3: ~a\n" (if nsec3? "on" "off"))
  399. (format #t " nsec3-iterations: ~a\n"
  400. (number->string nsec3-iterations))
  401. (format #t " nsec3-salt-length: ~a\n"
  402. (number->string nsec3-salt-length))
  403. (format #t " nsec3-salt-lifetime: ~a\n" nsec3-salt-lifetime)))
  404. policies))))
  405. (define (knot-remote-config remotes)
  406. (with-output-to-string
  407. (lambda ()
  408. (for-each
  409. (lambda (remote-config)
  410. (let ((id (knot-remote-configuration-id remote-config))
  411. (address (knot-remote-configuration-address remote-config))
  412. (via (knot-remote-configuration-via remote-config))
  413. (key (knot-remote-configuration-key remote-config)))
  414. (format #t " - id: ~a\n" id)
  415. (unless (eq? address '())
  416. (format #t " address: ~a\n" (format-string-list address)))
  417. (unless (eq? via '())
  418. (format #t " via: ~a\n" (format-string-list via)))
  419. (if key
  420. (format #t " key: ~a\n" key))))
  421. remotes))))
  422. (define (serialize-zone-entries entries)
  423. (with-output-to-string
  424. (lambda ()
  425. (for-each
  426. (lambda (entry)
  427. (let ((name (zone-entry-name entry))
  428. (ttl (zone-entry-ttl entry))
  429. (class (zone-entry-class entry))
  430. (type (zone-entry-type entry))
  431. (data (zone-entry-data entry)))
  432. (format #t "~a ~a ~a ~a ~a\n" name ttl class type data)))
  433. entries))))
  434. (define (serialize-zone-file zone domain)
  435. (computed-file (string-append domain ".zone")
  436. #~(begin
  437. (call-with-output-file #$output
  438. (lambda (port)
  439. (format port "$ORIGIN ~a.\n"
  440. #$(zone-file-origin zone))
  441. (format port "@ IN SOA ~a ~a (~a ~a ~a ~a ~a)\n"
  442. #$(zone-file-ns zone)
  443. #$(zone-file-mail zone)
  444. #$(zone-file-serial zone)
  445. #$(zone-file-refresh zone)
  446. #$(zone-file-retry zone)
  447. #$(zone-file-expiry zone)
  448. #$(zone-file-nx zone))
  449. (format port "~a\n"
  450. #$(serialize-zone-entries (zone-file-entries zone))))))))
  451. (define (knot-zone-config zone)
  452. (let ((content (knot-zone-configuration-zone zone)))
  453. #~(with-output-to-string
  454. (lambda ()
  455. (let ((domain #$(knot-zone-configuration-domain zone))
  456. (file #$(knot-zone-configuration-file zone))
  457. (master (list #$@(knot-zone-configuration-master zone)))
  458. (ddns-master #$(knot-zone-configuration-ddns-master zone))
  459. (notify (list #$@(knot-zone-configuration-notify zone)))
  460. (acl (list #$@(knot-zone-configuration-acl zone)))
  461. (semantic-checks? #$(knot-zone-configuration-semantic-checks? zone))
  462. (disable-any? #$(knot-zone-configuration-disable-any? zone))
  463. (dnssec-policy #$(knot-zone-configuration-dnssec-policy zone))
  464. (serial-policy '#$(knot-zone-configuration-serial-policy zone)))
  465. (format #t " - domain: ~a\n" domain)
  466. (if (eq? master '())
  467. ;; This server is a master
  468. (if (equal? file "")
  469. (format #t " file: ~a\n"
  470. #$(serialize-zone-file content
  471. (knot-zone-configuration-domain zone)))
  472. (format #t " file: ~a\n" file))
  473. ;; This server is a slave (has masters)
  474. (begin
  475. (format #t " master: ~a\n"
  476. #$(format-string-list
  477. (knot-zone-configuration-master zone)))
  478. (if ddns-master (format #t " ddns-master ~a\n" ddns-master))))
  479. (unless (eq? notify '())
  480. (format #t " notify: ~a\n"
  481. #$(format-string-list
  482. (knot-zone-configuration-notify zone))))
  483. (unless (eq? acl '())
  484. (format #t " acl: ~a\n"
  485. #$(format-string-list
  486. (knot-zone-configuration-acl zone))))
  487. (format #t " semantic-checks: ~a\n" (if semantic-checks? "on" "off"))
  488. (format #t " disable-any: ~a\n" (if disable-any? "on" "off"))
  489. (if dnssec-policy
  490. (begin
  491. (format #t " dnssec-signing: on\n")
  492. (format #t " dnssec-policy: ~a\n" dnssec-policy)))
  493. (format #t " serial-policy: ~a\n"
  494. (symbol->string serial-policy)))))))
  495. (define (knot-config-file config)
  496. (verify-knot-configuration config)
  497. (computed-file "knot.conf"
  498. #~(begin
  499. (call-with-output-file #$output
  500. (lambda (port)
  501. (format port "server:\n")
  502. (format port " rundir: ~a\n" #$(knot-configuration-run-directory config))
  503. (format port " user: knot\n")
  504. (format port " listen: ~a@~a\n"
  505. #$(knot-configuration-listen-v4 config)
  506. #$(knot-configuration-listen-port config))
  507. (format port " listen: ~a@~a\n"
  508. #$(knot-configuration-listen-v6 config)
  509. #$(knot-configuration-listen-port config))
  510. (format port "\nkey:\n")
  511. (format port #$(knot-key-config (knot-configuration-keys config)))
  512. (format port "\nkeystore:\n")
  513. (format port #$(knot-keystore-config (knot-configuration-keystores config)))
  514. (format port "\nacl:\n")
  515. (format port #$(knot-acl-config (knot-configuration-acls config)))
  516. (format port "\nremote:\n")
  517. (format port #$(knot-remote-config (knot-configuration-remotes config)))
  518. (format port "\npolicy:\n")
  519. (format port #$(knot-policy-config (knot-configuration-policies config)))
  520. (unless #$(eq? (knot-configuration-zones config) '())
  521. (format port "\nzone:\n")
  522. (format port "~a\n"
  523. (string-concatenate
  524. (list #$@(map knot-zone-config
  525. (knot-configuration-zones config)))))))))))
  526. (define %knot-accounts
  527. (list (user-group (name "knot") (system? #t))
  528. (user-account
  529. (name "knot")
  530. (group "knot")
  531. (system? #t)
  532. (comment "knot dns server user")
  533. (home-directory "/var/empty")
  534. (shell (file-append shadow "/sbin/nologin")))))
  535. (define (knot-activation config)
  536. #~(begin
  537. (use-modules (guix build utils))
  538. (define (mkdir-p/perms directory owner perms)
  539. (mkdir-p directory)
  540. (chown directory (passwd:uid owner) (passwd:gid owner))
  541. (chmod directory perms))
  542. (mkdir-p/perms #$(knot-configuration-run-directory config)
  543. (getpwnam "knot") #o755)
  544. (mkdir-p/perms "/var/lib/knot" (getpwnam "knot") #o755)
  545. (mkdir-p/perms "/var/lib/knot/keys" (getpwnam "knot") #o755)
  546. (mkdir-p/perms "/var/lib/knot/keys/keys" (getpwnam "knot") #o755)))
  547. (define (knot-shepherd-service config)
  548. (let* ((config-file (knot-config-file config))
  549. (knot (knot-configuration-knot config)))
  550. (list (shepherd-service
  551. (documentation "Run the Knot DNS daemon.")
  552. (provision '(knot dns))
  553. (requirement '(networking))
  554. (start #~(make-forkexec-constructor
  555. (list (string-append #$knot "/sbin/knotd")
  556. "-c" #$config-file)))
  557. (stop #~(make-kill-destructor))))))
  558. (define knot-service-type
  559. (service-type (name 'knot)
  560. (extensions
  561. (list (service-extension shepherd-root-service-type
  562. knot-shepherd-service)
  563. (service-extension activation-service-type
  564. knot-activation)
  565. (service-extension account-service-type
  566. (const %knot-accounts))))))
  567. ;;;
  568. ;;; Dnsmasq.
  569. ;;;
  570. (define-record-type* <dnsmasq-configuration>
  571. dnsmasq-configuration make-dnsmasq-configuration
  572. dnsmasq-configuration?
  573. (package dnsmasq-configuration-package
  574. (default dnsmasq)) ;package
  575. (no-hosts? dnsmasq-configuration-no-hosts?
  576. (default #f)) ;boolean
  577. (port dnsmasq-configuration-port
  578. (default 53)) ;integer
  579. (local-service? dnsmasq-configuration-local-service?
  580. (default #t)) ;boolean
  581. (listen-addresses dnsmasq-configuration-listen-address
  582. (default '())) ;list of string
  583. (resolv-file dnsmasq-configuration-resolv-file
  584. (default "/etc/resolv.conf")) ;string
  585. (no-resolv? dnsmasq-configuration-no-resolv?
  586. (default #f)) ;boolean
  587. (servers dnsmasq-configuration-servers
  588. (default '())) ;list of string
  589. (cache-size dnsmasq-configuration-cache-size
  590. (default 150)) ;integer
  591. (negative-cache? dnsmasq-configuration-negative-cache?
  592. (default #t))) ;boolean
  593. (define dnsmasq-shepherd-service
  594. (match-lambda
  595. (($ <dnsmasq-configuration> package
  596. no-hosts?
  597. port local-service? listen-addresses
  598. resolv-file no-resolv? servers
  599. cache-size negative-cache?)
  600. (shepherd-service
  601. (provision '(dnsmasq))
  602. (requirement '(networking))
  603. (documentation "Run the dnsmasq DNS server.")
  604. (start #~(make-forkexec-constructor
  605. '(#$(file-append package "/sbin/dnsmasq")
  606. "--keep-in-foreground"
  607. "--pid-file=/run/dnsmasq.pid"
  608. #$@(if no-hosts?
  609. '("--no-hosts")
  610. '())
  611. #$(format #f "--port=~a" port)
  612. #$@(if local-service?
  613. '("--local-service")
  614. '())
  615. #$@(map (cut format #f "--listen-address=~a" <>)
  616. listen-addresses)
  617. #$(format #f "--resolv-file=~a" resolv-file)
  618. #$@(if no-resolv?
  619. '("--no-resolv")
  620. '())
  621. #$@(map (cut format #f "--server=~a" <>)
  622. servers)
  623. #$(format #f "--cache-size=~a" cache-size)
  624. #$@(if negative-cache?
  625. '()
  626. '("--no-negcache")))
  627. #:pid-file "/run/dnsmasq.pid"))
  628. (stop #~(make-kill-destructor))))))
  629. (define dnsmasq-service-type
  630. (service-type
  631. (name 'dnsmasq)
  632. (extensions
  633. (list (service-extension shepherd-root-service-type
  634. (compose list dnsmasq-shepherd-service))))
  635. (default-value (dnsmasq-configuration))
  636. (description "Run the dnsmasq DNS server.")))
  637. ;;;
  638. ;;; ddclient
  639. ;;;
  640. (define (uglify-field-name field-name)
  641. (string-delete #\? (symbol->string field-name)))
  642. (define (serialize-field field-name val)
  643. (when (not (member field-name '(group secret-file user)))
  644. (format #t "~a=~a\n" (uglify-field-name field-name) val)))
  645. (define (serialize-boolean field-name val)
  646. (serialize-field field-name (if val "yes" "no")))
  647. (define (serialize-integer field-name val)
  648. (serialize-field field-name (number->string val)))
  649. (define (serialize-string field-name val)
  650. (if (and (string? val) (string=? val ""))
  651. ""
  652. (serialize-field field-name val)))
  653. (define (serialize-list field-name val)
  654. (if (null? val) "" (serialize-field field-name (string-join val))))
  655. (define (serialize-extra-options extra-options)
  656. (string-join extra-options "\n" 'suffix))
  657. (define-configuration ddclient-configuration
  658. (ddclient
  659. (package ddclient)
  660. "The ddclient package.")
  661. (daemon
  662. (integer 300)
  663. "The period after which ddclient will retry to check IP and domain name.")
  664. (syslog
  665. (boolean #t)
  666. "Use syslog for the output.")
  667. (mail
  668. (string "root")
  669. "Mail to user.")
  670. (mail-failure
  671. (string "root")
  672. "Mail failed update to user.")
  673. (pid
  674. (string "/var/run/ddclient/ddclient.pid")
  675. "The ddclient PID file.")
  676. (ssl
  677. (boolean #t)
  678. "Enable SSL support.")
  679. (user
  680. (string "ddclient")
  681. "Specifies the user name or ID that is used when running ddclient
  682. program.")
  683. (group
  684. (string "ddclient")
  685. "Group of the user who will run the ddclient program.")
  686. (secret-file
  687. (string "/etc/ddclient/secrets.conf")
  688. "Secret file which will be appended to @file{ddclient.conf} file. This
  689. file contains credentials for use by ddclient. You are expected to create it
  690. manually.")
  691. (extra-options
  692. (list '())
  693. "Extra options will be appended to @file{ddclient.conf} file."))
  694. (define (ddclient-account config)
  695. "Return the user accounts and user groups for CONFIG."
  696. (let ((ddclient-user (ddclient-configuration-user config))
  697. (ddclient-group (ddclient-configuration-group config)))
  698. (list (user-group
  699. (name ddclient-group)
  700. (system? #t))
  701. (user-account
  702. (name ddclient-user)
  703. (system? #t)
  704. (group ddclient-group)
  705. (comment "ddclientd privilege separation user")
  706. (home-directory (string-append "/var/run/" ddclient-user))))))
  707. (define (ddclient-activation config)
  708. "Return the activation GEXP for CONFIG."
  709. (with-imported-modules '((guix build utils)
  710. (ice-9 rdelim))
  711. #~(begin
  712. (use-modules (guix build utils)
  713. (ice-9 rdelim))
  714. (let ((ddclient-user
  715. (passwd:uid (getpw #$(ddclient-configuration-user config))))
  716. (ddclient-group
  717. (passwd:gid (getpw #$(ddclient-configuration-group config))))
  718. (ddclient-secret-file
  719. #$(ddclient-configuration-secret-file config)))
  720. ;; 'ddclient' complains about ddclient.conf file permissions, which
  721. ;; rules out /gnu/store. Thus we copy the ddclient.conf to /etc.
  722. (for-each (lambda (dir)
  723. (mkdir-p dir)
  724. (chmod dir #o700)
  725. (chown dir ddclient-user ddclient-group))
  726. '("/var/cache/ddclient" "/var/run/ddclient"
  727. "/etc/ddclient"))
  728. (with-output-to-file "/etc/ddclient/ddclient.conf"
  729. (lambda ()
  730. (display
  731. (string-append
  732. "# Generated by 'ddclient-service'.\n\n"
  733. #$(with-output-to-string
  734. (lambda ()
  735. (serialize-configuration config
  736. ddclient-configuration-fields)))
  737. (if (string-null? ddclient-secret-file)
  738. ""
  739. (format #f "\n\n# Appended from '~a'.\n\n~a"
  740. ddclient-secret-file
  741. (with-input-from-file ddclient-secret-file
  742. read-string)))))))
  743. (chmod "/etc/ddclient/ddclient.conf" #o600)
  744. (chown "/etc/ddclient/ddclient.conf"
  745. ddclient-user ddclient-group)))))
  746. (define (ddclient-shepherd-service config)
  747. "Return a <shepherd-service> for ddclient with CONFIG."
  748. (let ((ddclient (ddclient-configuration-ddclient config))
  749. (ddclient-pid (ddclient-configuration-pid config))
  750. (ddclient-user (ddclient-configuration-user config))
  751. (ddclient-group (ddclient-configuration-group config)))
  752. (list (shepherd-service
  753. (provision '(ddclient))
  754. (documentation "Run ddclient daemon.")
  755. (start #~(make-forkexec-constructor
  756. (list #$(file-append ddclient "/bin/ddclient")
  757. "-foreground"
  758. "-file" "/etc/ddclient/ddclient.conf")
  759. #:pid-file #$ddclient-pid
  760. #:environment-variables
  761. (list "SSL_CERT_DIR=/run/current-system/profile\
  762. /etc/ssl/certs"
  763. "SSL_CERT_FILE=/run/current-system/profile\
  764. /etc/ssl/certs/ca-certificates.crt")
  765. #:user #$ddclient-user
  766. #:group #$ddclient-group))
  767. (stop #~(make-kill-destructor))))))
  768. (define ddclient-service-type
  769. (service-type
  770. (name 'ddclient)
  771. (extensions
  772. (list (service-extension account-service-type
  773. ddclient-account)
  774. (service-extension shepherd-root-service-type
  775. ddclient-shepherd-service)
  776. (service-extension activation-service-type
  777. ddclient-activation)))
  778. (default-value (ddclient-configuration))
  779. (description "Configure address updating utility for dynamic DNS services,
  780. ddclient.")))
  781. (define (generate-ddclient-documentation)
  782. (generate-documentation
  783. `((ddclient-configuration ,ddclient-configuration-fields))
  784. 'ddclient-configuration))