vpn.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 'disabled)
  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. (define openvpn-client-service-type
  461. (service-type (name 'openvpn-client)
  462. (extensions
  463. (list (service-extension shepherd-root-service-type
  464. (openvpn-shepherd-service 'client))
  465. (service-extension account-service-type
  466. (const %openvpn-accounts))
  467. (service-extension activation-service-type
  468. (const %openvpn-activation))))))
  469. (define* (openvpn-client-service #:key (config (openvpn-client-configuration)))
  470. (validate-configuration config openvpn-client-configuration-fields)
  471. (service openvpn-client-service-type config))
  472. (define* (openvpn-server-service #:key (config (openvpn-server-configuration)))
  473. (validate-configuration config openvpn-server-configuration-fields)
  474. (service openvpn-server-service-type config))
  475. (define (generate-openvpn-server-documentation)
  476. (generate-documentation
  477. `((openvpn-server-configuration
  478. ,openvpn-server-configuration-fields
  479. (ccd openvpn-ccd-configuration))
  480. (openvpn-ccd-configuration ,openvpn-ccd-configuration-fields))
  481. 'openvpn-server-configuration))
  482. (define (generate-openvpn-client-documentation)
  483. (generate-documentation
  484. `((openvpn-client-configuration
  485. ,openvpn-client-configuration-fields
  486. (remote openvpn-remote-configuration))
  487. (openvpn-remote-configuration ,openvpn-remote-configuration-fields))
  488. 'openvpn-client-configuration))
  489. ;;;
  490. ;;; Strongswan.
  491. ;;;
  492. (define-record-type* <strongswan-configuration>
  493. strongswan-configuration make-strongswan-configuration
  494. strongswan-configuration?
  495. (strongswan strongswan-configuration-strongswan ;file-like
  496. (default strongswan))
  497. (ipsec-conf strongswan-configuration-ipsec-conf ;string|#f
  498. (default #f))
  499. (ipsec-secrets strongswan-configuration-ipsec-secrets ;string|#f
  500. (default #f)))
  501. ;; In the future, it might be worth implementing a record type to configure
  502. ;; all of the plugins, but for *most* basic use cases, simply creating the
  503. ;; files will be sufficient. Same is true of charon-plugins.
  504. (define strongswand-configuration-files
  505. (list "charon" "charon-logging" "pki" "pool" "scepclient"
  506. "swanctl" "tnc"))
  507. ;; Plugins to load. All of these plugins end up as configuration files in
  508. ;; strongswan.d/charon/.
  509. (define charon-plugins
  510. (list "aes" "aesni" "attr" "attr-sql" "chapoly" "cmac" "constraints"
  511. "counters" "curl" "curve25519" "dhcp" "dnskey" "drbg" "eap-aka-3gpp"
  512. "eap-aka" "eap-dynamic" "eap-identity" "eap-md5" "eap-mschapv2"
  513. "eap-peap" "eap-radius" "eap-simaka-pseudonym" "eap-simaka-reauth"
  514. "eap-simaka-sql" "eap-sim" "eap-sim-file" "eap-tls" "eap-tnc"
  515. "eap-ttls" "ext-auth" "farp" "fips-prf" "gmp" "ha" "hmac"
  516. "kernel-netlink" "led" "md4" "md5" "mgf1" "nonce" "openssl" "pem"
  517. "pgp" "pkcs12" "pkcs1" "pkcs7" "pkcs8" "pubkey" "random" "rc2"
  518. "resolve" "revocation" "sha1" "sha2" "socket-default" "soup" "sql"
  519. "sqlite" "sshkey" "tnc-tnccs" "vici" "x509" "xauth-eap" "xauth-generic"
  520. "xauth-noauth" "xauth-pam" "xcbc"))
  521. (define (strongswan-configuration-file config)
  522. (match-record config <strongswan-configuration>
  523. (strongswan ipsec-conf ipsec-secrets)
  524. (if (eq? (string? ipsec-conf) (string? ipsec-secrets))
  525. (let* ((strongswan-dir
  526. (computed-file
  527. "strongswan.d"
  528. #~(begin
  529. (mkdir #$output)
  530. ;; Create all of the configuration files strongswan.d/.
  531. (map (lambda (conf-file)
  532. (let* ((filename (string-append
  533. #$output "/"
  534. conf-file ".conf")))
  535. (call-with-output-file filename
  536. (lambda (port)
  537. (display
  538. "# Created by 'strongswan-service'\n"
  539. port)))))
  540. (list #$@strongswand-configuration-files))
  541. (mkdir (string-append #$output "/charon"))
  542. ;; Create all of the plugin configuration files.
  543. (map (lambda (plugin)
  544. (let* ((filename (string-append
  545. #$output "/charon/"
  546. plugin ".conf")))
  547. (call-with-output-file filename
  548. (lambda (port)
  549. (format port "~a {
  550. load = yes
  551. }"
  552. plugin)))))
  553. (list #$@charon-plugins))))))
  554. ;; Generate our strongswan.conf to reflect the user configuration.
  555. (computed-file
  556. "strongswan.conf"
  557. #~(begin
  558. (call-with-output-file #$output
  559. (lambda (port)
  560. (display "# Generated by 'strongswan-service'.\n" port)
  561. (format port "charon {
  562. load_modular = yes
  563. plugins {
  564. include ~a/charon/*.conf"
  565. #$strongswan-dir)
  566. (if #$ipsec-conf
  567. (format port "
  568. stroke {
  569. load = yes
  570. secrets_file = ~a
  571. }
  572. }
  573. }
  574. starter {
  575. config_file = ~a
  576. }
  577. include ~a/*.conf"
  578. #$ipsec-secrets
  579. #$ipsec-conf
  580. #$strongswan-dir)
  581. (format port "
  582. }
  583. }
  584. include ~a/*.conf"
  585. #$strongswan-dir)))))))
  586. (throw 'error
  587. (G_ "strongSwan ipsec-conf and ipsec-secrets must both be (un)set")))))
  588. (define (strongswan-shepherd-service config)
  589. (let* ((ipsec (file-append strongswan "/sbin/ipsec"))
  590. (strongswan-conf-path (strongswan-configuration-file config)))
  591. (list (shepherd-service
  592. (requirement '(networking))
  593. (provision '(ipsec))
  594. (start #~(make-forkexec-constructor
  595. (list #$ipsec "start" "--nofork")
  596. #:environment-variables
  597. (list (string-append "STRONGSWAN_CONF="
  598. #$strongswan-conf-path))))
  599. (stop #~(make-kill-destructor))
  600. (documentation
  601. "strongSwan's charon IKE keying daemon for IPsec VPN.")))))
  602. (define strongswan-service-type
  603. (service-type
  604. (name 'strongswan)
  605. (extensions
  606. (list (service-extension shepherd-root-service-type
  607. strongswan-shepherd-service)))
  608. (default-value (strongswan-configuration))
  609. (description
  610. "Connect to an IPsec @acronym{VPN, Virtual Private Network} with
  611. strongSwan.")))
  612. ;;;
  613. ;;; Wireguard.
  614. ;;;
  615. (define-record-type* <wireguard-peer>
  616. wireguard-peer make-wireguard-peer
  617. wireguard-peer?
  618. (name wireguard-peer-name)
  619. (endpoint wireguard-peer-endpoint
  620. (default #f)) ;string
  621. (public-key wireguard-peer-public-key) ;string
  622. (allowed-ips wireguard-peer-allowed-ips) ;list of strings
  623. (keep-alive wireguard-peer-keep-alive
  624. (default #f))) ;integer
  625. (define-record-type* <wireguard-configuration>
  626. wireguard-configuration make-wireguard-configuration
  627. wireguard-configuration?
  628. (wireguard wireguard-configuration-wireguard ;file-like
  629. (default wireguard-tools))
  630. (interface wireguard-configuration-interface ;string
  631. (default "wg0"))
  632. (addresses wireguard-configuration-addresses ;string
  633. (default '("10.0.0.1/32")))
  634. (port wireguard-configuration-port ;integer
  635. (default 51820))
  636. (private-key wireguard-configuration-private-key ;string
  637. (default "/etc/wireguard/private.key"))
  638. (peers wireguard-configuration-peers ;list of <wiregard-peer>
  639. (default '()))
  640. (dns wireguard-configuration-dns ;list of strings
  641. (default #f)))
  642. (define (wireguard-configuration-file config)
  643. (define (peer->config peer)
  644. (let ((name (wireguard-peer-name peer))
  645. (public-key (wireguard-peer-public-key peer))
  646. (endpoint (wireguard-peer-endpoint peer))
  647. (allowed-ips (wireguard-peer-allowed-ips peer))
  648. (keep-alive (wireguard-peer-keep-alive peer)))
  649. (format #f "[Peer] #~a
  650. PublicKey = ~a
  651. AllowedIPs = ~a
  652. ~a~a"
  653. name
  654. public-key
  655. (string-join allowed-ips ",")
  656. (if endpoint
  657. (format #f "Endpoint = ~a\n" endpoint)
  658. "")
  659. (if keep-alive
  660. (format #f "PersistentKeepalive = ~a\n" keep-alive)
  661. "\n"))))
  662. (match-record config <wireguard-configuration>
  663. (wireguard interface addresses port private-key peers dns)
  664. (let* ((config-file (string-append interface ".conf"))
  665. (peers (map peer->config peers))
  666. (config
  667. (computed-file
  668. "wireguard-config"
  669. #~(begin
  670. (mkdir #$output)
  671. (chdir #$output)
  672. (call-with-output-file #$config-file
  673. (lambda (port)
  674. (let ((format (@ (ice-9 format) format)))
  675. (format port "[Interface]
  676. Address = ~a
  677. PostUp = ~a set %i private-key ~a
  678. ~a
  679. ~a
  680. ~{~a~^~%~}"
  681. #$(string-join addresses ",")
  682. #$(file-append wireguard "/bin/wg")
  683. #$private-key
  684. #$(if port
  685. (format #f "ListenPort = ~a" port)
  686. "")
  687. #$(if dns
  688. (format #f "DNS = ~a"
  689. (string-join dns ","))
  690. "")
  691. (list #$@peers)))))))))
  692. (file-append config "/" config-file))))
  693. (define (wireguard-activation config)
  694. (match-record config <wireguard-configuration>
  695. (private-key)
  696. #~(begin
  697. (use-modules (guix build utils)
  698. (ice-9 popen)
  699. (ice-9 rdelim))
  700. (mkdir-p (dirname #$private-key))
  701. (unless (file-exists? #$private-key)
  702. (let* ((pipe
  703. (open-input-pipe (string-append
  704. #$(file-append wireguard-tools "/bin/wg")
  705. " genkey")))
  706. (key (read-line pipe)))
  707. (call-with-output-file #$private-key
  708. (lambda (port)
  709. (display key port)))
  710. (chmod #$private-key #o400)
  711. (close-pipe pipe))))))
  712. (define (wireguard-shepherd-service config)
  713. (match-record config <wireguard-configuration>
  714. (wireguard interface)
  715. (let ((wg-quick (file-append wireguard "/bin/wg-quick"))
  716. (config (wireguard-configuration-file config)))
  717. (list (shepherd-service
  718. (requirement '(networking))
  719. (provision (list
  720. (symbol-append 'wireguard-
  721. (string->symbol interface))))
  722. (start #~(lambda _
  723. (invoke #$wg-quick "up" #$config)))
  724. (stop #~(lambda _
  725. (invoke #$wg-quick "down" #$config)))
  726. (documentation "Run the Wireguard VPN tunnel"))))))
  727. (define wireguard-service-type
  728. (service-type
  729. (name 'wireguard)
  730. (extensions
  731. (list (service-extension shepherd-root-service-type
  732. wireguard-shepherd-service)
  733. (service-extension activation-service-type
  734. wireguard-activation)))))