vpn.scm 30 KB

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