vpn.scm 29 KB

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