vpn.scm 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  4. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  5. ;;; Copyright © 2021 Guillaume Le Vaillant <glv@posteo.net>
  6. ;;; Copyright © 2021 Solene Rapenne <solene@perso.pw>
  7. ;;; Copyright © 2021 Domagoj Stolfa <ds815@gmx.com>
  8. ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  9. ;;; Copyright © 2021 Raghav Gururajan <rg@raghavgururajan.name>
  10. ;;; Copyright © 2021 jgart <jgart@dismail.de>
  11. ;;; Copyright © 2021 Nathan Dehnel <ncdehnel@gmail.com>
  12. ;;;
  13. ;;; This file is part of GNU Guix.
  14. ;;;
  15. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  16. ;;; under the terms of the GNU General Public License as published by
  17. ;;; the Free Software Foundation; either version 3 of the License, or (at
  18. ;;; your option) any later version.
  19. ;;;
  20. ;;; GNU Guix is distributed in the hope that it will be useful, but
  21. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;;; GNU General Public License for more details.
  24. ;;;
  25. ;;; You should have received a copy of the GNU General Public License
  26. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  27. (define-module (gnu services vpn)
  28. #:use-module (gnu services)
  29. #:use-module (gnu services configuration)
  30. #:use-module (gnu services dbus)
  31. #:use-module (gnu services shepherd)
  32. #:use-module (gnu system shadow)
  33. #:use-module (gnu packages admin)
  34. #:use-module (gnu packages vpn)
  35. #:use-module (guix packages)
  36. #:use-module (guix records)
  37. #:use-module (guix gexp)
  38. #:use-module (guix i18n)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (ice-9 match)
  41. #:use-module (ice-9 regex)
  42. #:export (openvpn-client-service
  43. openvpn-server-service
  44. openvpn-client-service-type
  45. openvpn-server-service-type
  46. openvpn-client-configuration
  47. openvpn-server-configuration
  48. openvpn-remote-configuration
  49. openvpn-ccd-configuration
  50. generate-openvpn-client-documentation
  51. generate-openvpn-server-documentation
  52. strongswan-configuration
  53. strongswan-service-type
  54. wireguard-peer
  55. wireguard-peer?
  56. wireguard-peer-name
  57. wireguard-peer-endpoint
  58. wireguard-peer-allowed-ips
  59. wireguard-peer-public-key
  60. wireguard-peer-keep-alive
  61. wireguard-configuration
  62. wireguard-configuration?
  63. wireguard-configuration-wireguard
  64. wireguard-configuration-interface
  65. wireguard-configuration-addresses
  66. wireguard-configuration-port
  67. wireguard-configuration-dns
  68. wireguard-configuration-private-key
  69. wireguard-configuration-peers
  70. wireguard-service-type))
  71. ;;;
  72. ;;; Bitmask.
  73. ;;;
  74. (define-public bitmask-service-type
  75. (service-type
  76. (name 'bitmask)
  77. (description "Setup the @uref{https://bitmask.net, Bitmask} VPN application.")
  78. (default-value bitmask)
  79. (extensions
  80. (list
  81. ;; Add bitmask to the system profile.
  82. (service-extension profile-service-type list)
  83. ;; Configure polkit policy of bitmask.
  84. (service-extension polkit-service-type list)))))
  85. ;;;
  86. ;;; OpenVPN.
  87. ;;;
  88. (define (uglify-field-name name)
  89. (match name
  90. ('verbosity "verb")
  91. (_ (let ((str (symbol->string name)))
  92. (if (string-suffix? "?" str)
  93. (substring str 0 (1- (string-length str)))
  94. str)))))
  95. (define (serialize-field field-name val)
  96. (if (eq? field-name 'pid-file)
  97. (format #t "")
  98. (format #t "~a ~a\n" (uglify-field-name field-name) val)))
  99. (define serialize-string serialize-field)
  100. (define-maybe string)
  101. (define (serialize-boolean field-name val)
  102. (if val
  103. (serialize-field field-name "")
  104. (format #t "")))
  105. (define (ip-mask? val)
  106. (and (string? val)
  107. (if (string-match "^([0-9]+\\.){3}[0-9]+ ([0-9]+\\.){3}[0-9]+$" val)
  108. (let ((numbers (string-tokenize val char-set:digit)))
  109. (all-lte numbers (list 255 255 255 255 255 255 255 255)))
  110. #f)))
  111. (define serialize-ip-mask serialize-string)
  112. (define-syntax define-enumerated-field-type
  113. (lambda (x)
  114. (define (id-append ctx . parts)
  115. (datum->syntax ctx (apply symbol-append (map syntax->datum parts))))
  116. (syntax-case x ()
  117. ((_ name (option ...))
  118. #`(begin
  119. (define (#,(id-append #'name #'name #'?) x)
  120. (memq x '(option ...)))
  121. (define (#,(id-append #'name #'serialize- #'name) field-name val)
  122. (serialize-field field-name val)))))))
  123. (define-enumerated-field-type proto
  124. (udp tcp udp6 tcp6))
  125. (define-enumerated-field-type dev
  126. (tun tap))
  127. (define key-usage? boolean?)
  128. (define (serialize-key-usage field-name value)
  129. (if value
  130. (format #t "remote-cert-tls server\n")
  131. #f))
  132. (define bind? boolean?)
  133. (define (serialize-bind field-name value)
  134. (if value
  135. #f
  136. (format #t "nobind\n")))
  137. (define resolv-retry? boolean?)
  138. (define (serialize-resolv-retry field-name value)
  139. (if value
  140. (format #t "resolv-retry infinite\n")
  141. #f))
  142. (define (serialize-tls-auth role location)
  143. (if location
  144. (serialize-field 'tls-auth
  145. (string-append location " " (match role
  146. ('server "0")
  147. ('client "1"))))
  148. #f))
  149. (define (tls-auth? val)
  150. (or (eq? val #f)
  151. (string? val)))
  152. (define (serialize-tls-auth-server field-name val)
  153. (serialize-tls-auth 'server val))
  154. (define (serialize-tls-auth-client field-name val)
  155. (serialize-tls-auth 'client val))
  156. (define tls-auth-server? tls-auth?)
  157. (define tls-auth-client? tls-auth?)
  158. (define (serialize-number field-name val)
  159. (serialize-field field-name (number->string val)))
  160. (define (all-lte left right)
  161. (if (eq? left '())
  162. (eq? right '())
  163. (and (<= (string->number (car left)) (car right))
  164. (all-lte (cdr left) (cdr right)))))
  165. (define (cidr4? val)
  166. (if (string? val)
  167. (if (string-match "^([0-9]+\\.){3}[0-9]+/[0-9]+$" val)
  168. (let ((numbers (string-tokenize val char-set:digit)))
  169. (all-lte numbers (list 255 255 255 255 32)))
  170. #f)
  171. (eq? val #f)))
  172. (define (cidr6? val)
  173. (if (string? val)
  174. (string-match "^([0-9a-f]{0,4}:){0,8}/[0-9]{1,3}$" val)
  175. (eq? val #f)))
  176. (define (serialize-cidr4 field-name val)
  177. (if (eq? val #f) #f (serialize-field field-name val)))
  178. (define (serialize-cidr6 field-name val)
  179. (if (eq? val #f) #f (serialize-field field-name val)))
  180. (define (ip? val)
  181. (if (string? val)
  182. (if (string-match "^([0-9]+\\.){3}[0-9]+$" val)
  183. (let ((numbers (string-tokenize val char-set:digit)))
  184. (all-lte numbers (list 255 255 255 255)))
  185. #f)
  186. (eq? val #f)))
  187. (define (serialize-ip field-name val)
  188. (if (eq? val #f) #f (serialize-field field-name val)))
  189. (define (keepalive? val)
  190. (and (list? val)
  191. (and (number? (car val))
  192. (number? (car (cdr val))))))
  193. (define (serialize-keepalive field-name val)
  194. (format #t "~a ~a ~a\n" (uglify-field-name field-name)
  195. (number->string (car val)) (number->string (car (cdr val)))))
  196. (define gateway? boolean?)
  197. (define (serialize-gateway field-name val)
  198. (and val
  199. (format #t "push \"redirect-gateway\"\n")))
  200. (define-configuration openvpn-remote-configuration
  201. (name
  202. (string "my-server")
  203. "Server name.")
  204. (port
  205. (number 1194)
  206. "Port number the server listens to."))
  207. (define-configuration openvpn-ccd-configuration
  208. (name
  209. (string "client")
  210. "Client name.")
  211. (iroute
  212. (ip-mask #f)
  213. "Client own network")
  214. (ifconfig-push
  215. (ip-mask #f)
  216. "Client VPN IP."))
  217. (define (openvpn-remote-list? val)
  218. (and (list? val)
  219. (or (eq? val '())
  220. (and (openvpn-remote-configuration? (car val))
  221. (openvpn-remote-list? (cdr val))))))
  222. (define (serialize-openvpn-remote-list field-name val)
  223. (for-each (lambda (remote)
  224. (format #t "remote ~a ~a\n" (openvpn-remote-configuration-name remote)
  225. (number->string (openvpn-remote-configuration-port remote))))
  226. val))
  227. (define (openvpn-ccd-list? val)
  228. (and (list? val)
  229. (or (eq? val '())
  230. (and (openvpn-ccd-configuration? (car val))
  231. (openvpn-ccd-list? (cdr val))))))
  232. (define (serialize-openvpn-ccd-list field-name val)
  233. #f)
  234. (define (create-ccd-directory val)
  235. "Create a ccd directory containing files for the ccd configuration option
  236. of OpenVPN. Each file in this directory represents particular settings for a
  237. client. Each file is named after the name of the client."
  238. (let ((files (map (lambda (ccd)
  239. (list (openvpn-ccd-configuration-name ccd)
  240. (with-output-to-string
  241. (lambda ()
  242. (serialize-configuration
  243. ccd openvpn-ccd-configuration-fields)))))
  244. val)))
  245. (computed-file "ccd"
  246. (with-imported-modules '((guix build utils))
  247. #~(begin
  248. (use-modules (guix build utils))
  249. (use-modules (ice-9 match))
  250. (mkdir-p #$output)
  251. (for-each
  252. (lambda (ccd)
  253. (match ccd
  254. ((name config-string)
  255. (call-with-output-file
  256. (string-append #$output "/" name)
  257. (lambda (port) (display config-string port))))))
  258. '#$files))))))
  259. (define-syntax define-split-configuration
  260. (lambda (x)
  261. (syntax-case x ()
  262. ((_ name1 name2 (common-option ...) (first-option ...) (second-option ...))
  263. #`(begin
  264. (define-configuration #,#'name1
  265. common-option ...
  266. first-option ...)
  267. (define-configuration #,#'name2
  268. common-option ...
  269. second-option ...))))))
  270. (define-split-configuration openvpn-client-configuration
  271. openvpn-server-configuration
  272. ((openvpn
  273. (file-like openvpn)
  274. "The OpenVPN package.")
  275. (pid-file
  276. (string "/var/run/openvpn/openvpn.pid")
  277. "The OpenVPN pid file.")
  278. (proto
  279. (proto 'udp)
  280. "The protocol (UDP or TCP) used to open a channel between clients and
  281. servers.")
  282. (dev
  283. (dev 'tun)
  284. "The device type used to represent the VPN connection.")
  285. (ca
  286. (maybe-string "/etc/openvpn/ca.crt")
  287. "The certificate authority to check connections against.")
  288. (cert
  289. (maybe-string "/etc/openvpn/client.crt")
  290. "The certificate of the machine the daemon is running on. It should be signed
  291. by the authority given in @code{ca}.")
  292. (key
  293. (maybe-string "/etc/openvpn/client.key")
  294. "The key of the machine the daemon is running on. It must be the key whose
  295. certificate is @code{cert}.")
  296. (comp-lzo?
  297. (boolean #t)
  298. "Whether to use the lzo compression algorithm.")
  299. (persist-key?
  300. (boolean #t)
  301. "Don't re-read key files across SIGUSR1 or --ping-restart.")
  302. (persist-tun?
  303. (boolean #t)
  304. "Don't close and reopen TUN/TAP device or run up/down scripts across
  305. SIGUSR1 or --ping-restart restarts.")
  306. (fast-io?
  307. (boolean #f)
  308. "(Experimental) Optimize TUN/TAP/UDP I/O writes by avoiding a call to
  309. poll/epoll/select prior to the write operation.")
  310. (verbosity
  311. (number 3)
  312. "Verbosity level."))
  313. ;; client-specific configuration
  314. ((tls-auth
  315. (tls-auth-client #f)
  316. "Add an additional layer of HMAC authentication on top of the TLS control
  317. channel to protect against DoS attacks.")
  318. (auth-user-pass
  319. (maybe-string 'disabled)
  320. "Authenticate with server using username/password. The option is a file
  321. containing username/password on 2 lines. Do not use a file-like object as it
  322. would be added to the store and readable by any user.")
  323. (verify-key-usage?
  324. (key-usage #t)
  325. "Whether to check the server certificate has server usage extension.")
  326. (bind?
  327. (bind #f)
  328. "Bind to a specific local port number.")
  329. (resolv-retry?
  330. (resolv-retry #t)
  331. "Retry resolving server address.")
  332. (remote
  333. (openvpn-remote-list '())
  334. "A list of remote servers to connect to."))
  335. ;; server-specific configuration
  336. ((tls-auth
  337. (tls-auth-server #f)
  338. "Add an additional layer of HMAC authentication on top of the TLS control
  339. channel to protect against DoS attacks.")
  340. (port
  341. (number 1194)
  342. "Specifies the port number on which the server listens.")
  343. (server
  344. (ip-mask "10.8.0.0 255.255.255.0")
  345. "An ip and mask specifying the subnet inside the virtual network.")
  346. (server-ipv6
  347. (cidr6 #f)
  348. "A CIDR notation specifying the IPv6 subnet inside the virtual network.")
  349. (dh
  350. (string "/etc/openvpn/dh2048.pem")
  351. "The Diffie-Hellman parameters file.")
  352. (ifconfig-pool-persist
  353. (string "/etc/openvpn/ipp.txt")
  354. "The file that records client IPs.")
  355. (redirect-gateway?
  356. (gateway #f)
  357. "When true, the server will act as a gateway for its clients.")
  358. (client-to-client?
  359. (boolean #f)
  360. "When true, clients are allowed to talk to each other inside the VPN.")
  361. (keepalive
  362. (keepalive '(10 120))
  363. "Causes ping-like messages to be sent back and forth over the link so that
  364. each side knows when the other side has gone down. @code{keepalive} requires
  365. a pair. The first element is the period of the ping sending, and the second
  366. element is the timeout before considering the other side down.")
  367. (max-clients
  368. (number 100)
  369. "The maximum number of clients.")
  370. (status
  371. (string "/var/run/openvpn/status")
  372. "The status file. This file shows a small report on current connection. It
  373. is truncated and rewritten every minute.")
  374. (client-config-dir
  375. (openvpn-ccd-list '())
  376. "The list of configuration for some clients.")))
  377. (define (openvpn-config-file role config)
  378. (let ((config-str
  379. (with-output-to-string
  380. (lambda ()
  381. (serialize-configuration config
  382. (match role
  383. ('server
  384. openvpn-server-configuration-fields)
  385. ('client
  386. openvpn-client-configuration-fields))))))
  387. (ccd-dir (match role
  388. ('server (create-ccd-directory
  389. (openvpn-server-configuration-client-config-dir
  390. config)))
  391. ('client #f))))
  392. (computed-file "openvpn.conf"
  393. #~(begin
  394. (use-modules (ice-9 match))
  395. (call-with-output-file #$output
  396. (lambda (port)
  397. (match '#$role
  398. ('server (display "" port))
  399. ('client (display "client\n" port)))
  400. (display #$config-str port)
  401. (match '#$role
  402. ('server (display
  403. (string-append "client-config-dir "
  404. #$ccd-dir "\n") port))
  405. ('client (display "" port)))))))))
  406. (define (openvpn-shepherd-service role)
  407. (lambda (config)
  408. (let* ((config-file (openvpn-config-file role config))
  409. (pid-file ((match role
  410. ('server openvpn-server-configuration-pid-file)
  411. ('client openvpn-client-configuration-pid-file))
  412. config))
  413. (openvpn ((match role
  414. ('server openvpn-server-configuration-openvpn)
  415. ('client openvpn-client-configuration-openvpn))
  416. config))
  417. (log-file (match role
  418. ('server "/var/log/openvpn-server.log")
  419. ('client "/var/log/openvpn-client.log"))))
  420. (list (shepherd-service
  421. (documentation (string-append "Run the OpenVPN "
  422. (match role
  423. ('server "server")
  424. ('client "client"))
  425. " daemon."))
  426. (provision (match role
  427. ('server '(vpn-server))
  428. ('client '(vpn-client))))
  429. (requirement '(networking))
  430. (start #~(make-forkexec-constructor
  431. (list (string-append #$openvpn "/sbin/openvpn")
  432. "--writepid" #$pid-file "--config" #$config-file
  433. "--daemon")
  434. #:pid-file #$pid-file))
  435. (stop #~(make-kill-destructor)))))))
  436. (define %openvpn-accounts
  437. (list (user-group (name "openvpn") (system? #t))
  438. (user-account
  439. (name "openvpn")
  440. (group "openvpn")
  441. (system? #t)
  442. (comment "Openvpn daemon user")
  443. (home-directory "/var/empty")
  444. (shell (file-append shadow "/sbin/nologin")))))
  445. (define %openvpn-activation
  446. #~(begin
  447. (use-modules (guix build utils))
  448. (mkdir-p "/var/run/openvpn")))
  449. (define openvpn-server-service-type
  450. (service-type (name 'openvpn-server)
  451. (extensions
  452. (list (service-extension shepherd-root-service-type
  453. (openvpn-shepherd-service 'server))
  454. (service-extension account-service-type
  455. (const %openvpn-accounts))
  456. (service-extension activation-service-type
  457. (const %openvpn-activation))))))
  458. (define openvpn-client-service-type
  459. (service-type (name 'openvpn-client)
  460. (extensions
  461. (list (service-extension shepherd-root-service-type
  462. (openvpn-shepherd-service 'client))
  463. (service-extension account-service-type
  464. (const %openvpn-accounts))
  465. (service-extension activation-service-type
  466. (const %openvpn-activation))))))
  467. (define* (openvpn-client-service #:key (config (openvpn-client-configuration)))
  468. (validate-configuration config openvpn-client-configuration-fields)
  469. (service openvpn-client-service-type config))
  470. (define* (openvpn-server-service #:key (config (openvpn-server-configuration)))
  471. (validate-configuration config openvpn-server-configuration-fields)
  472. (service openvpn-server-service-type config))
  473. (define (generate-openvpn-server-documentation)
  474. (generate-documentation
  475. `((openvpn-server-configuration
  476. ,openvpn-server-configuration-fields
  477. (ccd openvpn-ccd-configuration))
  478. (openvpn-ccd-configuration ,openvpn-ccd-configuration-fields))
  479. 'openvpn-server-configuration))
  480. (define (generate-openvpn-client-documentation)
  481. (generate-documentation
  482. `((openvpn-client-configuration
  483. ,openvpn-client-configuration-fields
  484. (remote openvpn-remote-configuration))
  485. (openvpn-remote-configuration ,openvpn-remote-configuration-fields))
  486. 'openvpn-client-configuration))
  487. ;;;
  488. ;;; Strongswan.
  489. ;;;
  490. (define-record-type* <strongswan-configuration>
  491. strongswan-configuration make-strongswan-configuration
  492. strongswan-configuration?
  493. (strongswan strongswan-configuration-strongswan ;file-like
  494. (default strongswan))
  495. (ipsec-conf strongswan-configuration-ipsec-conf ;string|#f
  496. (default #f))
  497. (ipsec-secrets strongswan-configuration-ipsec-secrets ;string|#f
  498. (default #f)))
  499. ;; In the future, it might be worth implementing a record type to configure
  500. ;; all of the plugins, but for *most* basic use cases, simply creating the
  501. ;; files will be sufficient. Same is true of charon-plugins.
  502. (define strongswand-configuration-files
  503. (list "charon" "charon-logging" "pki" "pool" "scepclient"
  504. "swanctl" "tnc"))
  505. ;; Plugins to load. All of these plugins end up as configuration files in
  506. ;; strongswan.d/charon/.
  507. (define charon-plugins
  508. (list "aes" "aesni" "attr" "attr-sql" "chapoly" "cmac" "constraints"
  509. "counters" "curl" "curve25519" "dhcp" "dnskey" "drbg" "eap-aka-3gpp"
  510. "eap-aka" "eap-dynamic" "eap-identity" "eap-md5" "eap-mschapv2"
  511. "eap-peap" "eap-radius" "eap-simaka-pseudonym" "eap-simaka-reauth"
  512. "eap-simaka-sql" "eap-sim" "eap-sim-file" "eap-tls" "eap-tnc"
  513. "eap-ttls" "ext-auth" "farp" "fips-prf" "gmp" "ha" "hmac"
  514. "kernel-netlink" "led" "md4" "md5" "mgf1" "nonce" "openssl" "pem"
  515. "pgp" "pkcs12" "pkcs1" "pkcs7" "pkcs8" "pubkey" "random" "rc2"
  516. "resolve" "revocation" "sha1" "sha2" "socket-default" "soup" "sql"
  517. "sqlite" "sshkey" "tnc-tnccs" "vici" "x509" "xauth-eap" "xauth-generic"
  518. "xauth-noauth" "xauth-pam" "xcbc"))
  519. (define (strongswan-configuration-file config)
  520. (match-record config <strongswan-configuration>
  521. (strongswan ipsec-conf ipsec-secrets)
  522. (if (eq? (string? ipsec-conf) (string? ipsec-secrets))
  523. (let* ((strongswan-dir
  524. (computed-file
  525. "strongswan.d"
  526. #~(begin
  527. (mkdir #$output)
  528. ;; Create all of the configuration files strongswan.d/.
  529. (map (lambda (conf-file)
  530. (let* ((filename (string-append
  531. #$output "/"
  532. conf-file ".conf")))
  533. (call-with-output-file filename
  534. (lambda (port)
  535. (display
  536. "# Created by 'strongswan-service'\n"
  537. port)))))
  538. (list #$@strongswand-configuration-files))
  539. (mkdir (string-append #$output "/charon"))
  540. ;; Create all of the plugin configuration files.
  541. (map (lambda (plugin)
  542. (let* ((filename (string-append
  543. #$output "/charon/"
  544. plugin ".conf")))
  545. (call-with-output-file filename
  546. (lambda (port)
  547. (format port "~a {
  548. load = yes
  549. }"
  550. plugin)))))
  551. (list #$@charon-plugins))))))
  552. ;; Generate our strongswan.conf to reflect the user configuration.
  553. (computed-file
  554. "strongswan.conf"
  555. #~(begin
  556. (call-with-output-file #$output
  557. (lambda (port)
  558. (display "# Generated by 'strongswan-service'.\n" port)
  559. (format port "charon {
  560. load_modular = yes
  561. plugins {
  562. include ~a/charon/*.conf"
  563. #$strongswan-dir)
  564. (if #$ipsec-conf
  565. (format port "
  566. stroke {
  567. load = yes
  568. secrets_file = ~a
  569. }
  570. }
  571. }
  572. starter {
  573. config_file = ~a
  574. }
  575. include ~a/*.conf"
  576. #$ipsec-secrets
  577. #$ipsec-conf
  578. #$strongswan-dir)
  579. (format port "
  580. }
  581. }
  582. include ~a/*.conf"
  583. #$strongswan-dir)))))))
  584. (throw 'error
  585. (G_ "strongSwan ipsec-conf and ipsec-secrets must both be (un)set")))))
  586. (define (strongswan-shepherd-service config)
  587. (let* ((ipsec (file-append strongswan "/sbin/ipsec"))
  588. (strongswan-conf-path (strongswan-configuration-file config)))
  589. (list (shepherd-service
  590. (requirement '(networking))
  591. (provision '(ipsec))
  592. (start #~(make-forkexec-constructor
  593. (list #$ipsec "start" "--nofork")
  594. #:environment-variables
  595. (list (string-append "STRONGSWAN_CONF="
  596. #$strongswan-conf-path))))
  597. (stop #~(make-kill-destructor))
  598. (documentation
  599. "strongSwan's charon IKE keying daemon for IPsec VPN.")))))
  600. (define strongswan-service-type
  601. (service-type
  602. (name 'strongswan)
  603. (extensions
  604. (list (service-extension shepherd-root-service-type
  605. strongswan-shepherd-service)))
  606. (default-value (strongswan-configuration))
  607. (description
  608. "Connect to an IPsec @acronym{VPN, Virtual Private Network} with
  609. strongSwan.")))
  610. ;;;
  611. ;;; Wireguard.
  612. ;;;
  613. (define-record-type* <wireguard-peer>
  614. wireguard-peer make-wireguard-peer
  615. wireguard-peer?
  616. (name wireguard-peer-name)
  617. (endpoint wireguard-peer-endpoint
  618. (default #f)) ;string
  619. (public-key wireguard-peer-public-key) ;string
  620. (allowed-ips wireguard-peer-allowed-ips) ;list of strings
  621. (keep-alive wireguard-peer-keep-alive
  622. (default #f))) ;integer
  623. (define-record-type* <wireguard-configuration>
  624. wireguard-configuration make-wireguard-configuration
  625. wireguard-configuration?
  626. (wireguard wireguard-configuration-wireguard ;file-like
  627. (default wireguard-tools))
  628. (interface wireguard-configuration-interface ;string
  629. (default "wg0"))
  630. (addresses wireguard-configuration-addresses ;string
  631. (default '("10.0.0.1/32")))
  632. (port wireguard-configuration-port ;integer
  633. (default 51820))
  634. (private-key wireguard-configuration-private-key ;string
  635. (default "/etc/wireguard/private.key"))
  636. (peers wireguard-configuration-peers ;list of <wiregard-peer>
  637. (default '()))
  638. (dns wireguard-configuration-dns ;list of strings
  639. (default #f)))
  640. (define (wireguard-configuration-file config)
  641. (define (peer->config peer)
  642. (let ((name (wireguard-peer-name peer))
  643. (public-key (wireguard-peer-public-key peer))
  644. (endpoint (wireguard-peer-endpoint peer))
  645. (allowed-ips (wireguard-peer-allowed-ips peer))
  646. (keep-alive (wireguard-peer-keep-alive peer)))
  647. (format #f "[Peer] #~a
  648. PublicKey = ~a
  649. AllowedIPs = ~a
  650. ~a~a"
  651. name
  652. public-key
  653. (string-join allowed-ips ",")
  654. (if endpoint
  655. (format #f "Endpoint = ~a\n" endpoint)
  656. "")
  657. (if keep-alive
  658. (format #f "PersistentKeepalive = ~a\n" keep-alive)
  659. "\n"))))
  660. (match-record config <wireguard-configuration>
  661. (wireguard interface addresses port private-key peers dns)
  662. (let* ((config-file (string-append interface ".conf"))
  663. (peers (map peer->config peers))
  664. (config
  665. (computed-file
  666. "wireguard-config"
  667. #~(begin
  668. (mkdir #$output)
  669. (chdir #$output)
  670. (call-with-output-file #$config-file
  671. (lambda (port)
  672. (let ((format (@ (ice-9 format) format)))
  673. (format port "[Interface]
  674. Address = ~a
  675. PostUp = ~a set %i private-key ~a
  676. ~a
  677. ~a
  678. ~{~a~^~%~}"
  679. #$(string-join addresses ",")
  680. #$(file-append wireguard "/bin/wg")
  681. #$private-key
  682. #$(if port
  683. (format #f "ListenPort = ~a" port)
  684. "")
  685. #$(if dns
  686. (format #f "DNS = ~a"
  687. (string-join dns ","))
  688. "")
  689. (list #$@peers)))))))))
  690. (file-append config "/" config-file))))
  691. (define (wireguard-activation config)
  692. (match-record config <wireguard-configuration>
  693. (private-key)
  694. #~(begin
  695. (use-modules (guix build utils)
  696. (ice-9 popen)
  697. (ice-9 rdelim))
  698. (mkdir-p (dirname #$private-key))
  699. (unless (file-exists? #$private-key)
  700. (let* ((pipe
  701. (open-input-pipe (string-append
  702. #$(file-append wireguard-tools "/bin/wg")
  703. " genkey")))
  704. (key (read-line pipe)))
  705. (call-with-output-file #$private-key
  706. (lambda (port)
  707. (display key port)))
  708. (chmod #$private-key #o400)
  709. (close-pipe pipe))))))
  710. (define (wireguard-shepherd-service config)
  711. (match-record config <wireguard-configuration>
  712. (wireguard interface)
  713. (let ((wg-quick (file-append wireguard "/bin/wg-quick"))
  714. (config (wireguard-configuration-file config)))
  715. (list (shepherd-service
  716. (requirement '(networking))
  717. (provision (list
  718. (symbol-append 'wireguard-
  719. (string->symbol interface))))
  720. (start #~(lambda _
  721. (invoke #$wg-quick "up" #$config)))
  722. (stop #~(lambda _
  723. (invoke #$wg-quick "down" #$config)))
  724. (documentation "Run the Wireguard VPN tunnel"))))))
  725. (define wireguard-service-type
  726. (service-type
  727. (name 'wireguard)
  728. (extensions
  729. (list (service-extension shepherd-root-service-type
  730. wireguard-shepherd-service)
  731. (service-extension activation-service-type
  732. wireguard-activation)))))