bug-guix
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#39542: Adding openvpn client configurations to guix system


From: david larsson
Subject: bug#39542: Adding openvpn client configurations to guix system
Date: Sat, 01 Aug 2020 14:58:26 +0000

On 2020-08-01 13:44, david larsson wrote:
On 2020-02-10 15:57, Damien Cassou wrote:
Julien Lepiller <julien@lepiller.eu> writes:
We already have an openvpn-client-service-type and an
openvpn-server-service-type. It's not linked to network manager
though, I have no idea what it expects there. What do you need
exactly?

It seems to me that gnu/services/vpn.scm defines
openvpn-server-service-type that triggers the generation of a shepherd
service.

At the office we use 3 different VPNs that we activate on demand (test, acceptance and production). If we follow the vpn.scm way, it seems that this would require 3 shepherd services but I guess it's not possible to
instantiate the openvpn-client-service-type more than once. This seems
to be a dead end to me.

Hi Damien,

I think I have a solution for you, where you can start 3 different
vpn's with herd start vpn1-client, herd start vpn2-client etc.

Below is an ovpn-service.scm module, modeled after vpn.scm. which you
can include with (use-modules (ovpn-service)) in your config.scm, by
saving it in the same dir as config.scm. This is tested and works.

Now, I think you can modify all occurences of the word "ovpn", to, say
vpn1, vpn2, and vpn3, and save 3 different files, and then use
(use-modules (vpn1-service) (vpn2-service) (vpn3-service)) etc. in the
config.scm. An example configuration in the config.scm OS-services
section would be:

                  (ovpn-client-service
                   #:config
                   (let ([ base-dir
"/home/myuser/src/my-guixsd-config/etc_openvpn/"]
                         )
                     (ovpn-client-configuration
                      ;; client
                      (dev 'tun)
                      ;; remote-random
                      (proto 'udp)
                      ;; mute-replay-warnings
                      ;; replay-window 256

;; remote-cert-tls server lines is generated somehow
                      ;; remote-cert-tls server

                      ;; cipher aes-256-cbc
;; ncp-ciphers AES-256-GCM:AES-256-CBC:AES-128-GCM
                      ;; pull
                      ;; nobind
                      (bind? #f)
                      ;; reneg-sec 432000
                      ;; resolv-retry infinite
                      (resolv-retry? #t)
                      ;; compress lzo
                      (comp-lzo? #t)
                      ;; verb 3
                      (verbosity 3)
                      ;; persist-key
                      (persist-key? #t)
                      ;; persist-tun
                      (persist-tun? #t)
                      ;; auth-user-pass /etc/openvpn/credentials
(auth-user-pass (string-append base-dir "credentials"))
                      ;; ca /etc/openvpn/ovpn-ca.crt
                      (ca (string-append base-dir "ovpn-ca.crt"))
                      ;; tls-auth /etc/openvpn/ovpn-tls.key 1
(tls-auth (string-append base-dir "ovpn-tls.key"))

                      ;; log /tmp/openvpn.log
                      ;; script-security 2
                      ;; resolv-conf scripts not needed for guix
                      ;; up /etc/openvpn/update-resolv-conf
                      ;; down /etc/openvpn/update-resolv-conf

                      (fast-io? #t)
                      (remote
                       (list
                        ;; Resolves to multiple vpn servers in location
                 instead       (ovpn-remote-configuration
                         (name "whatever.ovpn.com")
                         (port 1196))
                        (ovpn-remote-configuration
                         (name "whatever.ovpn.com")
                         (port 1197))
                        (ovpn-remote-configuration
                         (name "whatever.ovpn.com")
                         (port 1196))
                        (ovpn-remote-configuration
                         (name "whatever.ovpn.com")
                         (port 1197))
                        )))))

Please let me know if this works for you!

Best regards,
David Larsson

and forgot the module, here:

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017 Julien Lepiller <julien@lepiller.eu>
;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (ovpn-service)
  #:use-module (gnu services)
  #:use-module (gnu services configuration)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system shadow)
  #:use-module (gnu packages admin)
  #:use-module (gnu packages vpn)
  #:use-module (guix packages)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:export (ovpn-client-service
            ovpn-server-service
            ovpn-client-service-type
            ovpn-server-service-type
            ovpn-client-configuration
            ovpn-server-configuration
            ovpn-remote-configuration
            ovpn-ccd-configuration
            generate-ovpn-client-documentation
            generate-ovpn-server-documentation))

;;;
;;; Ovpn.
;;;

(define (uglify-field-name name)
  (match name
    ('verbosity "verb")
    (_ (let ((str (symbol->string name)))
         (if (string-suffix? "?" str)
             (substring str 0 (1- (string-length str)))
             str)))))

(define (serialize-field field-name val)
  (if (eq? field-name 'pid-file)
      (format #t "")
      (format #t "~a ~a\n" (uglify-field-name field-name) val)))
(define serialize-string serialize-field)
(define-maybe string)
(define (serialize-boolean field-name val)
  (if val
      (serialize-field field-name "")
      (format #t "")))

(define (ip-mask? val)
  (and (string? val)
(if (string-match "^([0-9]+\\.){3}[0-9]+ ([0-9]+\\.){3}[0-9]+$" val)
           (let ((numbers (string-tokenize val char-set:digit)))
             (all-lte numbers (list 255 255 255 255 255 255 255 255)))
           #f)))
(define serialize-ip-mask serialize-string)

(define-syntax define-enumerated-field-type
  (lambda (x)
    (define (id-append ctx . parts)
(datum->syntax ctx (apply symbol-append (map syntax->datum parts))))
    (syntax-case x ()
      ((_ name (option ...))
       #`(begin
           (define (#,(id-append #'name #'name #'?) x)
             (memq x '(option ...)))
(define (#,(id-append #'name #'serialize- #'name) field-name val)
             (serialize-field field-name val)))))))

(define-enumerated-field-type proto
  (udp tcp udp6 tcp6))
(define-enumerated-field-type dev
  (tun tap))

(define key-usage? boolean?)
(define (serialize-key-usage field-name value)
  (if value
      (format #t "remote-cert-tls server\n")
      #f))

(define bind? boolean?)
(define (serialize-bind field-name value)
  (if value
      #f
      (format #t "nobind\n")))

(define resolv-retry? boolean?)
(define (serialize-resolv-retry field-name value)
  (if value
      (format #t "resolv-retry infinite\n")
      #f))

(define (serialize-tls-auth role location)
  (if location
      (serialize-field 'tls-auth
                       (string-append location " " (match role
                                                     ('server "0")
                                                     ('client "1"))))
      #f))
(define (tls-auth? val)
  (or (eq? val #f)
      (string? val)))
(define (serialize-tls-auth-server field-name val)
  (serialize-tls-auth 'server val))
(define (serialize-tls-auth-client field-name val)
  (serialize-tls-auth 'client val))
(define tls-auth-server? tls-auth?)
(define tls-auth-client? tls-auth?)

(define (serialize-number field-name val)
  (serialize-field field-name (number->string val)))

(define (all-lte left right)
  (if (eq? left '())
      (eq? right '())
      (and (<= (string->number (car left)) (car right))
           (all-lte (cdr left) (cdr right)))))

(define (cidr4? val)
  (if (string? val)
      (if (string-match "^([0-9]+\\.){3}[0-9]+/[0-9]+$" val)
          (let ((numbers (string-tokenize val char-set:digit)))
            (all-lte numbers (list 255 255 255 255 32)))
          #f)
      (eq? val #f)))

(define (cidr6? val)
  (if (string? val)
      (string-match "^([0-9a-f]{0,4}:){0,8}/[0-9]{1,3}$" val)
      (eq? val #f)))

(define (serialize-cidr4 field-name val)
  (if (eq? val #f) #f (serialize-field field-name val)))

(define (serialize-cidr6 field-name val)
  (if (eq? val #f) #f (serialize-field field-name val)))

(define (ip? val)
  (if (string? val)
      (if (string-match "^([0-9]+\\.){3}[0-9]+$" val)
          (let ((numbers (string-tokenize val char-set:digit)))
            (all-lte numbers (list 255 255 255 255)))
          #f)
      (eq? val #f)))
(define (serialize-ip field-name val)
  (if (eq? val #f) #f (serialize-field field-name val)))

(define (keepalive? val)
  (and (list? val)
       (and (number? (car val))
            (number? (car (cdr val))))))
(define (serialize-keepalive field-name val)
  (format #t "~a ~a ~a\n" (uglify-field-name field-name)
          (number->string (car val)) (number->string (car (cdr val)))))

(define gateway? boolean?)
(define (serialize-gateway field-name val)
  (and val
       (format #t "push \"redirect-gateway\"\n")))


(define-configuration ovpn-remote-configuration
  (name
   (string "my-server")
   "Server name.")
  (port
   (number 1194)
   "Port number the server listens to."))

(define-configuration ovpn-ccd-configuration
  (name
   (string "client")
   "Client name.")
  (iroute
   (ip-mask #f)
   "Client own network")
  (ifconfig-push
   (ip-mask #f)
   "Client VPN IP."))

(define (ovpn-remote-list? val)
  (and (list? val)
       (or (eq? val '())
           (and (ovpn-remote-configuration? (car val))
                (ovpn-remote-list? (cdr val))))))
(define (serialize-ovpn-remote-list field-name val)
  (for-each (lambda (remote)
(format #t "remote ~a ~a\n" (ovpn-remote-configuration-name remote) (number->string (ovpn-remote-configuration-port remote))))
            val))

(define (ovpn-ccd-list? val)
  (and (list? val)
       (or (eq? val '())
           (and (ovpn-ccd-configuration? (car val))
                (ovpn-ccd-list? (cdr val))))))
(define (serialize-ovpn-ccd-list field-name val)
  #f)

(define (create-ccd-directory val)
"Create a ccd directory containing files for the ccd configuration option of Ovpn. Each file in this directory represents particular settings for a
client.  Each file is named after the name of the client."
  (let ((files (map (lambda (ccd)
                      (list (ovpn-ccd-configuration-name ccd)
                            (with-output-to-string
                              (lambda ()
                                (serialize-configuration
                                 ccd ovpn-ccd-configuration-fields)))))
                    val)))
    (computed-file "ccd"
                   (with-imported-modules '((guix build utils))
                     #~(begin
                         (use-modules (guix build utils))
                         (use-modules (ice-9 match))
                         (mkdir-p #$output)
                         (for-each
                          (lambda (ccd)
                            (match ccd
                              ((name config-string)
                               (call-with-output-file
                                   (string-append #$output "/" name)
(lambda (port) (display config-string port))))))
                          '#$files))))))

(define-syntax define-split-configuration
  (lambda (x)
    (syntax-case x ()
((_ name1 name2 (common-option ...) (first-option ...) (second-option ...))
       #`(begin
           (define-configuration #,#'name1
             common-option ...
             first-option ...)
           (define-configuration #,#'name2
             common-option ...
             second-option ...))))))

(define-split-configuration ovpn-client-configuration
  ovpn-server-configuration
  ((ovpn
    (package openvpn)
    "The OpenVPN package.")

   (pid-file
    (string "/var/run/openvpn/openvpn.pid")
    "The OpenVPN pid file.")

   (proto
    (proto 'udp)
"The protocol (UDP or TCP) used to open a channel between clients and
servers.")

   (dev
    (dev 'tun)
    "The device type used to represent the VPN connection.")

   (ca
    (maybe-string "/etc/openvpn/ca.crt")
    "The certificate authority to check connections against.")

   (cert
    ;;(maybe-string "/etc/openvpn/client.crt")
    (maybe-string 'disabled)
"The certificate of the machine the daemon is running on. It should be signed
by the authority given in @code{ca}.")

   (key
    ;;(string "/etc/openvpn/client.key")
    (maybe-string 'disabled)
"The key of the machine the daemon is running on. It must be the key whose
certificate is @code{cert}.")

   (comp-lzo?
    (boolean #t)
    "Whether to use the lzo compression algorithm.")

   (persist-key?
    (boolean #t)
    "Don't re-read key files across SIGUSR1 or --ping-restart.")

   (persist-tun?
    (boolean #t)
    "Don't close and reopen TUN/TAP device or run up/down scripts across
SIGUSR1 or --ping-restart restarts.")

   (fast-io?
     (boolean #f)
"(Experimental) Optimize TUN/TAP/UDP I/O writes by avoiding a call to
poll/epoll/select prior to the write operation.")

   (verbosity
    (number 3)
    "Verbosity level."))
  ;; client-specific configuration
  ((tls-auth
    (tls-auth-client #f)
"Add an additional layer of HMAC authentication on top of the TLS control
channel to protect against DoS attacks.")

   (auth-user-pass
     (maybe-string 'disabled)
"Authenticate with server using username/password. The option is a file containing username/password on 2 lines. Do not use a file-like object as it
would be added to the store and readable by any user.")

   (verify-key-usage?
    (key-usage #t)
"Whether to check the server certificate has server usage extension.")

   (bind?
    (bind #f)
    "Bind to a specific local port number.")

   (resolv-retry?
    (resolv-retry #t)
    "Retry resolving server address.")

   (remote-random?
    (boolean #f)
    "Select remotes randomly")
   (mute-replay-warnings?
    (boolean #f)
    "Mute replay warnings")
   (replay-window?
    (number 256)
    "Dunno")

   (remote
    (ovpn-remote-list '())
    "A list of remote servers to connect to."))
  ;; server-specific configuration
  ((tls-auth
    (tls-auth-server #f)
"Add an additional layer of HMAC authentication on top of the TLS control
channel to protect against DoS attacks.")

   (port
    (number 1194)
    "Specifies the port number on which the server listens.")

   (server
    (ip-mask "10.8.0.0 255.255.255.0")
    "An ip and mask specifying the subnet inside the virtual network.")

   (server-ipv6
    (cidr6 #f)
"A CIDR notation specifying the IPv6 subnet inside the virtual network.")

   (dh
    (string "/etc/openvpn/dh2048.pem")
    "The Diffie-Hellman parameters file.")

   (ifconfig-pool-persist
    (string "/etc/openvpn/ipp.txt")
    "The file that records client IPs.")

   (redirect-gateway?
    (gateway #f)
    "When true, the server will act as a gateway for its clients.")

   (client-to-client?
    (boolean #f)
"When true, clients are allowed to talk to each other inside the VPN.")

   (keepalive
    (keepalive '(10 120))
"Causes ping-like messages to be sent back and forth over the link so that each side knows when the other side has gone down. @code{keepalive} requires a pair. The first element is the period of the ping sending, and the second
element is the timeout before considering the other side down.")

   (max-clients
    (number 100)
    "The maximum number of clients.")

   (status
    (string "/var/run/openvpn/status")
"The status file. This file shows a small report on current connection. It
is truncated and rewritten every minute.")

   (client-config-dir
    (ovpn-ccd-list '())
    "The list of configuration for some clients.")))

(define (ovpn-config-file role config)
  (let ((config-str
         (with-output-to-string
           (lambda ()
             (serialize-configuration config
                                      (match role
                                        ('server
ovpn-server-configuration-fields)
                                        ('client
ovpn-client-configuration-fields))))))
        (ccd-dir (match role
                   ('server (create-ccd-directory
(ovpn-server-configuration-client-config-dir
                              config)))
                   ('client #f))))
    (computed-file "openvpn.conf"
                   #~(begin
                       (use-modules (ice-9 match))
                       (call-with-output-file #$output
                         (lambda (port)
                           (match '#$role
                             ('server (display "" port))
                             ('client (display "client\n" port)))
                           (display #$config-str port)
                           (match '#$role
                             ('server (display
(string-append "client-config-dir " #$ccd-dir "\n") port))
                             ('client (display "" port)))))))))

(define (ovpn-shepherd-service role)
  (lambda (config)
    (let* ((config-file (ovpn-config-file role config))
           (pid-file ((match role
                        ('server ovpn-server-configuration-pid-file)
                        ('client ovpn-client-configuration-pid-file))
                      config))
           (ovpn ((match role
                       ('server ovpn-server-configuration-ovpn)
                       ('client ovpn-client-configuration-ovpn))
                     config))
           (log-file (match role
                       ('server "/var/log/openvpn-server.log")
                       ('client "/var/log/openvpn-client.log"))))
      (list (shepherd-service
             (documentation (string-append "Run the OpenVPN "
                                           (match role
                                             ('server "server")
                                             ('client "client"))
                                           " daemon."))
             (provision (match role
                          ('server '(ovpn-server))
                          ('client '(ovpn-client))))
             (requirement '(networking))
             (start #~(make-forkexec-constructor
                       (list (string-append #$ovpn "/sbin/openvpn")
"--writepid" #$pid-file "--config" #$config-file
                             "--daemon")
                       #:pid-file #$pid-file))
             (stop #~(make-kill-destructor)))))))

(define %ovpn-accounts
  (list (user-group (name "openvpn") (system? #t))
        (user-account
         (name "openvpn")
         (group "openvpn")
         (system? #t)
         (comment "Openvpn daemon user")
         (home-directory "/var/empty")
         (shell (file-append shadow "/sbin/nologin")))))

(define %ovpn-activation
  #~(begin
      (use-modules (guix build utils))
      (mkdir-p "/var/run/openvpn")))

(define ovpn-server-service-type
  (service-type (name 'ovpn-server)
                (extensions
                 (list (service-extension shepherd-root-service-type
(ovpn-shepherd-service 'server))
                       (service-extension account-service-type
                                          (const %ovpn-accounts))
                       (service-extension activation-service-type
                                          (const %ovpn-activation))))))

(define ovpn-client-service-type
  (service-type (name 'ovpn-client)
                (extensions
                 (list (service-extension shepherd-root-service-type
(ovpn-shepherd-service 'client))
                       (service-extension account-service-type
                                          (const %ovpn-accounts))
                       (service-extension activation-service-type
                                          (const %ovpn-activation))))))

(define* (ovpn-client-service #:key (config (ovpn-client-configuration)))
  (validate-configuration config ovpn-client-configuration-fields)
  (service ovpn-client-service-type config))

(define* (ovpn-server-service #:key (config (ovpn-server-configuration)))
  (validate-configuration config ovpn-server-configuration-fields)
  (service ovpn-server-service-type config))

(define (generate-ovpn-server-documentation)
  (generate-documentation
   `((ovpn-server-configuration
      ,ovpn-server-configuration-fields
      (ccd ovpn-ccd-configuration))
     (ovpn-ccd-configuration ,ovpn-ccd-configuration-fields))
   'ovpn-server-configuration))

(define (generate-ovpn-client-documentation)
  (generate-documentation
   `((ovpn-client-configuration
      ,ovpn-client-configuration-fields
      (remote ovpn-remote-configuration))
     (ovpn-remote-configuration ,ovpn-remote-configuration-fields))
   'ovpn-client-configuration))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]