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>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu services vpn)
22 #:use-module (gnu services)
23 #:use-module (gnu services configuration)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu system shadow)
26 #:use-module (gnu packages admin)
27 #:use-module (gnu packages vpn)
28 #:use-module (guix packages)
29 #:use-module (guix records)
30 #:use-module (guix gexp)
31 #:use-module (srfi srfi-1)
32 #:use-module (ice-9 match)
33 #:use-module (ice-9 regex)
34 #:export (openvpn-client-service
35 openvpn-server-service
36 openvpn-client-service-type
37 openvpn-server-service-type
38 openvpn-client-configuration
39 openvpn-server-configuration
40 openvpn-remote-configuration
41 openvpn-ccd-configuration
42 generate-openvpn-client-documentation
43 generate-openvpn-server-documentation))
49 (define (uglify-field-name name)
52 (_ (let ((str (symbol->string name)))
53 (if (string-suffix? "?" str)
54 (substring str 0 (1- (string-length str)))
57 (define (serialize-field field-name val)
58 (if (eq? field-name 'pid-file)
60 (format #t "~a ~a\n" (uglify-field-name field-name) val)))
61 (define serialize-string serialize-field)
62 (define (serialize-boolean field-name val)
64 (serialize-field field-name val)
67 (define (ip-mask? val)
69 (if (string-match "^([0-9]+\\.){3}[0-9]+ ([0-9]+\\.){3}[0-9]+$" val)
70 (let ((numbers (string-tokenize val char-set:digit)))
71 (all-lte numbers (list 255 255 255 255 255 255 255 255)))
73 (define serialize-ip-mask serialize-string)
75 (define-syntax define-enumerated-field-type
77 (define (id-append ctx . parts)
78 (datum->syntax ctx (apply symbol-append (map syntax->datum parts))))
80 ((_ name (option ...))
82 (define (#,(id-append #'name #'name #'?) x)
83 (memq x '(option ...)))
84 (define (#,(id-append #'name #'serialize- #'name) field-name val)
85 (serialize-field field-name val)))))))
87 (define-enumerated-field-type proto
89 (define-enumerated-field-type dev
92 (define key-usage? boolean?)
93 (define (serialize-key-usage field-name value)
95 (format #t "remote-cert-tls server\n")
98 (define bind? boolean?)
99 (define (serialize-bind field-name value)
102 (format #t "nobind\n")))
104 (define resolv-retry? boolean?)
105 (define (serialize-resolv-retry field-name value)
107 (format #t "resolv-retry infinite\n")
110 (define (serialize-tls-auth role location)
112 (serialize-field 'tls-auth
113 (string-append location " " (match role
117 (define (tls-auth? val)
120 (define (serialize-tls-auth-server field-name val)
121 (serialize-tls-auth 'server val))
122 (define (serialize-tls-auth-client field-name val)
123 (serialize-tls-auth 'client val))
124 (define tls-auth-server? tls-auth?)
125 (define tls-auth-client? tls-auth?)
127 (define (serialize-number field-name val)
128 (serialize-field field-name (number->string val)))
130 (define (all-lte left right)
133 (and (<= (string->number (car left)) (car right))
134 (all-lte (cdr left) (cdr right)))))
138 (if (string-match "^([0-9]+\\.){3}[0-9]+/[0-9]+$" val)
139 (let ((numbers (string-tokenize val char-set:digit)))
140 (all-lte numbers (list 255 255 255 255 32)))
146 (string-match "^([0-9a-f]{0,4}:){0,8}/[0-9]{1,3}$" val)
149 (define (serialize-cidr4 field-name val)
150 (if (eq? val #f) #f (serialize-field field-name val)))
152 (define (serialize-cidr6 field-name val)
153 (if (eq? val #f) #f (serialize-field field-name val)))
157 (if (string-match "^([0-9]+\\.){3}[0-9]+$" val)
158 (let ((numbers (string-tokenize val char-set:digit)))
159 (all-lte numbers (list 255 255 255 255)))
162 (define (serialize-ip field-name val)
163 (if (eq? val #f) #f (serialize-field field-name val)))
165 (define (keepalive? val)
167 (and (number? (car val))
168 (number? (car (cdr val))))))
169 (define (serialize-keepalive field-name val)
170 (format #t "~a ~a ~a\n" (uglify-field-name field-name)
171 (number->string (car val)) (number->string (car (cdr val)))))
173 (define gateway? boolean?)
174 (define (serialize-gateway field-name val)
176 (format #t "push \"redirect-gateway\"\n")))
179 (define-configuration openvpn-remote-configuration
185 "Port number the server listens to."))
187 (define-configuration openvpn-ccd-configuration
193 "Client own network")
198 (define (openvpn-remote-list? val)
201 (and (openvpn-remote-configuration? (car val))
202 (openvpn-remote-list? (cdr val))))))
203 (define (serialize-openvpn-remote-list field-name val)
204 (for-each (lambda (remote)
205 (format #t "remote ~a ~a\n" (openvpn-remote-configuration-name remote)
206 (number->string (openvpn-remote-configuration-port remote))))
209 (define (openvpn-ccd-list? val)
212 (and (openvpn-ccd-configuration? (car val))
213 (openvpn-ccd-list? (cdr val))))))
214 (define (serialize-openvpn-ccd-list field-name val)
217 (define (create-ccd-directory val)
218 "Create a ccd directory containing files for the ccd configuration option
219 of OpenVPN. Each file in this directory represents particular settings for a
220 client. Each file is named after the name of the client."
221 (let ((files (map (lambda (ccd)
222 (list (openvpn-ccd-configuration-name ccd)
223 (with-output-to-string
225 (serialize-configuration
226 ccd openvpn-ccd-configuration-fields)))))
229 (with-imported-modules '((guix build utils))
231 (use-modules (guix build utils))
232 (use-modules (ice-9 match))
237 ((name config-string)
238 (call-with-output-file
239 (string-append #$output "/" name)
240 (lambda (port) (display config-string port))))))
243 (define-syntax define-split-configuration
246 ((_ name1 name2 (common-option ...) (first-option ...) (second-option ...))
248 (define-configuration #,#'name1
251 (define-configuration #,#'name2
253 second-option ...))))))
255 (define-split-configuration openvpn-client-configuration
256 openvpn-server-configuration
259 "The OpenVPN package.")
262 (string "/var/run/openvpn/openvpn.pid")
263 "The OpenVPN pid file.")
267 "The protocol (UDP or TCP) used to open a channel between clients and
272 "The device type used to represent the VPN connection.")
275 (string "/etc/openvpn/ca.crt")
276 "The certificate authority to check connections against.")
279 (string "/etc/openvpn/client.crt")
280 "The certificate of the machine the daemon is running on. It should be signed
281 by the authority given in @code{ca}.")
284 (string "/etc/openvpn/client.key")
285 "The key of the machine the daemon is running on. It must be the key whose
286 certificate is @code{cert}.")
290 "Whether to use the lzo compression algorithm.")
294 "Don't re-read key files across SIGUSR1 or --ping-restart.")
298 "Don't close and reopen TUN/TAP device or run up/down scripts across
299 SIGUSR1 or --ping-restart restarts.")
304 ;; client-specific configuration
307 "Add an additional layer of HMAC authentication on top of the TLS control
308 channel to protect against DoS attacks.")
312 "Whether to check the server certificate has server usage extension.")
316 "Bind to a specific local port number.")
320 "Retry resolving server address.")
323 (openvpn-remote-list '())
324 "A list of remote servers to connect to."))
325 ;; server-specific configuration
328 "Add an additional layer of HMAC authentication on top of the TLS control
329 channel to protect against DoS attacks.")
333 "Specifies the port number on which the server listens.")
336 (ip-mask "10.8.0.0 255.255.255.0")
337 "An ip and mask specifying the subnet inside the virtual network.")
341 "A CIDR notation specifying the IPv6 subnet inside the virtual network.")
344 (string "/etc/openvpn/dh2048.pem")
345 "The Diffie-Hellman parameters file.")
347 (ifconfig-pool-persist
348 (string "/etc/openvpn/ipp.txt")
349 "The file that records client IPs.")
353 "When true, the server will act as a gateway for its clients.")
357 "When true, clients are allowed to talk to each other inside the VPN.")
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.")
368 "The maximum number of clients.")
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.")
376 (openvpn-ccd-list '())
377 "The list of configuration for some clients.")))
379 (define (openvpn-config-file role config)
381 (with-output-to-string
383 (serialize-configuration config
386 openvpn-server-configuration-fields)
388 openvpn-client-configuration-fields))))))
390 ('server (create-ccd-directory
391 (openvpn-server-configuration-client-config-dir
394 (computed-file "openvpn.conf"
396 (use-modules (ice-9 match))
397 (call-with-output-file #$output
400 ('server (display "" port))
401 ('client (display "client\n" port)))
402 (display #$config-str port)
405 (string-append "client-config-dir "
406 #$ccd-dir "\n") port))
407 ('client (display "" port)))))))))
409 (define (openvpn-shepherd-service role)
411 (let* ((config-file (openvpn-config-file role config))
412 (pid-file ((match role
413 ('server openvpn-server-configuration-pid-file)
414 ('client openvpn-client-configuration-pid-file))
416 (openvpn ((match role
417 ('server openvpn-server-configuration-openvpn)
418 ('client openvpn-client-configuration-openvpn))
420 (log-file (match role
421 ('server "/var/log/openvpn-server.log")
422 ('client "/var/log/openvpn-client.log"))))
423 (list (shepherd-service
424 (documentation (string-append "Run the OpenVPN "
429 (provision (match role
430 ('server '(vpn-server))
431 ('client '(vpn-client))))
432 (requirement '(networking))
433 (start #~(make-forkexec-constructor
434 (list (string-append #$openvpn "/sbin/openvpn")
435 "--writepid" #$pid-file "--config" #$config-file
437 #:pid-file #$pid-file))
438 (stop #~(make-kill-destructor)))))))
440 (define %openvpn-accounts
441 (list (user-group (name "openvpn") (system? #t))
446 (comment "Openvpn daemon user")
447 (home-directory "/var/empty")
448 (shell (file-append shadow "/sbin/nologin")))))
450 (define %openvpn-activation
452 (use-modules (guix build utils))
453 (mkdir-p "/var/run/openvpn")))
455 (define openvpn-server-service-type
456 (service-type (name 'openvpn-server)
458 (list (service-extension shepherd-root-service-type
459 (openvpn-shepherd-service 'server))
460 (service-extension account-service-type
461 (const %openvpn-accounts))
462 (service-extension activation-service-type
463 (const %openvpn-activation))))))
465 (define openvpn-client-service-type
466 (service-type (name 'openvpn-client)
468 (list (service-extension shepherd-root-service-type
469 (openvpn-shepherd-service 'client))
470 (service-extension account-service-type
471 (const %openvpn-accounts))
472 (service-extension activation-service-type
473 (const %openvpn-activation))))))
475 (define* (openvpn-client-service #:key (config (openvpn-client-configuration)))
476 (validate-configuration config openvpn-client-configuration-fields)
477 (service openvpn-client-service-type config))
479 (define* (openvpn-server-service #:key (config (openvpn-server-configuration)))
480 (validate-configuration config openvpn-server-configuration-fields)
481 (service openvpn-server-service-type config))
483 (define (generate-openvpn-server-documentation)
484 (generate-documentation
485 `((openvpn-server-configuration
486 ,openvpn-server-configuration-fields
487 (ccd openvpn-ccd-configuration))
488 (openvpn-ccd-configuration ,openvpn-ccd-configuration-fields))
489 'openvpn-server-configuration))
491 (define (generate-openvpn-client-documentation)
492 (generate-documentation
493 `((openvpn-client-configuration
494 ,openvpn-client-configuration-fields
495 (remote openvpn-remote-configuration))
496 (openvpn-remote-configuration ,openvpn-remote-configuration-fields))
497 'openvpn-client-configuration))