From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id UCWpBEKDJV+3fgAA0tVLHw (envelope-from ) for ; Sat, 01 Aug 2020 14:59:14 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id cGSFAEKDJV+9OwAAbx9fmQ (envelope-from ) for ; Sat, 01 Aug 2020 14:59:14 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 8D556940981 for ; Sat, 1 Aug 2020 14:59:11 +0000 (UTC) Received: from localhost ([::1]:49346 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k1syc-0002DK-10 for larch@yhetil.org; Sat, 01 Aug 2020 10:59:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57916) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1syU-0002Ca-0L for bug-guix@gnu.org; Sat, 01 Aug 2020 10:59:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:57562) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k1syT-0007Mo-Nb for bug-guix@gnu.org; Sat, 01 Aug 2020 10:59:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1k1syT-0003lQ-KR for bug-guix@gnu.org; Sat, 01 Aug 2020 10:59:01 -0400 X-Loop: help-debbugs@gnu.org Subject: bug#39542: Adding openvpn client configurations to guix system Resent-From: david larsson Original-Sender: "Debbugs-submit" Resent-CC: bug-guix@gnu.org Resent-Date: Sat, 01 Aug 2020 14:59:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 39542 X-GNU-PR-Package: guix X-GNU-PR-Keywords: To: Damien Cassou Received: via spool by 39542-submit@debbugs.gnu.org id=B39542.159629392214444 (code B ref 39542); Sat, 01 Aug 2020 14:59:01 +0000 Received: (at 39542) by debbugs.gnu.org; 1 Aug 2020 14:58:42 +0000 Received: from localhost ([127.0.0.1]:40875 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k1sy9-0003kt-V1 for submit@debbugs.gnu.org; Sat, 01 Aug 2020 10:58:42 -0400 Received: from server0.selfhosted.xyz ([217.64.149.7]:46488) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k1sy6-0003kh-I3 for 39542@debbugs.gnu.org; Sat, 01 Aug 2020 10:58:40 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selfhosted.xyz; s=dkim; t=1596293912; bh=gWBhxPgv3qj0P2th0wRmT4+8500gn/HqACZRmN8gfQA=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=x6gWzA9uSsgo1NHuyG9yOmMB2cC+qJbmnMRFl3CQzWSVLZlF6KAE6XcMRvcl4f3Tc OPXT6N1S2cydySFQrrsTBgfA+qYgbCPR7SAmTnF+P4J5Smx7j+DEgGtC8o2x5kVEIh K3Ia1dYfgVPIVCTZ3RlUzuodBnlubLlwcJ/r0cJzu/0wtzUSVkvpDO8apRl4LWTuKV 6iONfFc3BasMuRhnapkYhy7GhPJywDWg9SteCWsIs2ljMz4xQSVuc/aZuObQrH53dz 48z5vaWf5nQIEgxk1Sv9j+h0Susw9SnXFEO09cd8ecZRyLDOc53UpxyuM20fQE2Ph2 PfApUF7Dpa+8g== X-Fuglu-Suspect: a408be8dc8714216a9f4de3526fc6957 X-Fuglu-Spamstatus: NO Received: from [127.0.0.1] (localhost [127.0.0.1]) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Sat, 01 Aug 2020 14:58:26 +0000 From: david larsson In-Reply-To: References: <87blq63hyj.fsf@cassou.me> <1BD29F0F-1268-4526-828D-C2AC25CFE2E5@lepiller.eu> <877e0u306c.fsf@cassou.me> Message-ID: X-Sender: david.larsson@selfhosted.xyz X-Spam-Score: -1.8 (-) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -1.8 (-) X-BeenThere: bug-guix@gnu.org List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-Guix , 39542@debbugs.gnu.org Errors-To: bug-guix-bounces+larch=yhetil.org@gnu.org Sender: "bug-Guix" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (rsa verify failed) header.d=selfhosted.xyz header.s=dkim header.b=x6gWzA9u; dmarc=fail reason="SPF not aligned (relaxed)" header.from=selfhosted.xyz (policy=none); spf=pass (aspmx1.migadu.com: domain of bug-guix-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=bug-guix-bounces@gnu.org X-Spam-Score: 0.09 X-TUID: kXCoSjK5Dt4D On 2020-08-01 13:44, david larsson wrote: > On 2020-02-10 15:57, Damien Cassou wrote: >> Julien Lepiller 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 ;;; Copyright © 2017 Clément Lassieur ;;; Copyright © 2017 Mathieu Othacehe ;;; ;;; 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 . (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))