From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id sFLjCmsCY2RudgAASxT56A (envelope-from ) for ; Tue, 16 May 2023 06:11:23 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id QE7LCmsCY2SdcAEA9RJhRA (envelope-from ) for ; Tue, 16 May 2023 06:11:23 +0200 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 A16A41C80E for ; Tue, 16 May 2023 06:11:22 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pym1l-0008KP-3w; Tue, 16 May 2023 00:11:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pym1g-0008Iz-2d for guix-patches@gnu.org; Tue, 16 May 2023 00:11:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pym1f-0002pd-4r for guix-patches@gnu.org; Tue, 16 May 2023 00:11:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pym1f-00023D-0K for guix-patches@gnu.org; Tue, 16 May 2023 00:11:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v3 1/3] services: wireguard: Implement a dynamic IP monitoring feature. References: In-Reply-To: Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 16 May 2023 04:11:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63402@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.16842102457815 (code B ref 63402); Tue, 16 May 2023 04:11:02 +0000 Received: (at 63402) by debbugs.gnu.org; 16 May 2023 04:10:45 +0000 Received: from localhost ([127.0.0.1]:44870 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pym1M-00021y-5t for submit@debbugs.gnu.org; Tue, 16 May 2023 00:10:45 -0400 Received: from mail-qv1-f41.google.com ([209.85.219.41]:57430) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pym1J-00021i-6z for 63402@debbugs.gnu.org; Tue, 16 May 2023 00:10:42 -0400 Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-61d97ab176eso63486046d6.2 for <63402@debbugs.gnu.org>; Mon, 15 May 2023 21:10:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684210235; x=1686802235; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gOYKLpYJZ8DjDgNtBykKf8sqQMfZNacugWijB7wPfDc=; b=PbVGMGb+WOt/kJP3yAUizC82AsbgrTI7P4yHw9W1evUUkkqbiObR9OHVdP93cO++CS BMmlq+5jqpe90LZcjSbd7plgfEAPBBqffTV/Hy0/S80ps+NMFjY1X5tGjx4YJWGOl7ui n2tnxrhTF5BHklRz1XSXTmJiRlYrrmvc6S5O2E6dF3e16Z2K5G9EyC8H8ixgiIFoFr0c w8+WH9Z2LZEL+6H55gLNCQz3TID4mlErjzeusZLCFNY8bykVaKNI5lvZTVk4/i3fBecB QnXecN2TWL8JMW+iIG41H2rIb1CLBX4u2gYGnHyXS7XR5Fx6RDOr1ZuWqorxjwC9XONo 5rHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684210235; x=1686802235; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gOYKLpYJZ8DjDgNtBykKf8sqQMfZNacugWijB7wPfDc=; b=UP1eJEPtDsGwHff/YitKFILnea0Jp5fymqR6nrh0ITs/uKxy77najIFe+d4YgxxzOl IIr5PrXAIuUblTEcBlB4/eaRpu+/69f8AuV8anrPlv3mhYjjL9FTIaPJCTRt1JkkdREp tap0SNI1XfWV7R+BXWOYRFGHpMnETQV6nULeaRdoSadsjK22+knXhqopR4aaaoVbbROa xf/W6FYavKgFrFJt18a/jLlFmeF0uCXyBTwFAxbWeDlZL81HEL38rH0BC3CYlvP0e7ON 2AM28sgWlBkdkUMI3JPAXOr2rvCkt9ASn8GFXaO5G0+QdkzXuTXjUMJ4Rxse3kKJlQ6R zksA== X-Gm-Message-State: AC+VfDzkCiG6E920D2QctBE97CGObjPq44WovVyx6xFQCEQnF+99HJkY ek905JHL1NoPUqeTRU7GDKf9oSd8Z9FJT0El X-Google-Smtp-Source: ACHHUZ4f0P1ktohmtev3mc6KdsCgyDGBrW9qhZj4smrQk1ejRKVuNsJ6fTmazSQ8lyanIk/5soeDfA== X-Received: by 2002:a05:6214:1cc4:b0:618:e1d9:75b8 with SMTP id g4-20020a0562141cc400b00618e1d975b8mr52530172qvd.34.1684210234926; Mon, 15 May 2023 21:10:34 -0700 (PDT) Received: from localhost.localdomain (dsl-205-236-230-106.b2b2c.ca. [205.236.230.106]) by smtp.gmail.com with ESMTPSA id f21-20020a0caa95000000b005f2dba7a5b0sm5367347qvb.132.2023.05.15.21.10.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 21:10:34 -0700 (PDT) From: Maxim Cournoyer Date: Tue, 16 May 2023 00:09:06 -0400 Message-Id: <76b34e5229e0e97068cb3bd42152f29630a8dbfc.1684210148.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1684210283; a=rsa-sha256; cv=none; b=PEOqiJFJt2bKH3hpKgETi+PqjhhfVHORMkmZLCfHdUsUfXlnIb1w59F8qCH5zJ02RosJ2o VpmzINNHkGobiRiasYCYaVQrkFAAvtoO8kGqIGbpc/dVyMT8ac+T+szQ9DZmH2QNaJrZSw 2h3DNzb+zi7bRcFCcBCHIQdTcttcHdVPbVbb/XzG5G1ay/oYhvpb4uGbET+cIAyJAmy7Hu YSZ7gDrAUKo6HYTeoOngt7peNIOfaU/b6UbKm6fX2zPSsCmA0zoc8qogfZctcvWZKUL7Ct yZ7smdW99WSAXLflPv4oTeA56gC1N8sPj5OYvmYDWE94DtQTLAcX11yzKbT2/w== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=PbVGMGb+; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1684210283; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=gOYKLpYJZ8DjDgNtBykKf8sqQMfZNacugWijB7wPfDc=; b=fCKx6imgINaDWFxCMV9bKqMfKjvgCzLei3M4cUo+hqu4tX4eOGEBeHpDgZME1PkDDPn0mq HdHj0YvvKPEqpDSKm+mgwMNJCbMVDle4lrhCday1QjssKVBIZt9/iuvCUVmoXUfwcGFNED 1AH/ER57Mp/xgtKMf//iexR4TS4b2BJFFToxxmX06sSOv3+48pDMeC8ZU/QPx2WnkMNAvV EG77nhb65qCwexLyFKD0rLp1VvdppXOUlkoLDWY8eUCxhzknbQaDGcqsWYYyMD+mUQmSK1 tQyA1rwwHdQo2Lwqrbl+owW7LkZ/q3uTRNT/aCpk+qnue7VQd0jwqBG62tcEEg== X-Migadu-Spam-Score: -2.89 X-Spam-Score: -2.89 X-Migadu-Queue-Id: A16A41C80E X-Migadu-Scanner: scn0.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=PbVGMGb+; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-TUID: EfkNnoVqaCoy * gnu/services/vpn.scm () [monitor-ips?, monitor-ips-internal]: New fields. * gnu/services/vpn.scm (define-with-source): New syntax. (wireguard-service-name, strip-port/maybe) (ipv4-address?, ipv6-address?, host-name?) (peers->endpoint-host-names) (wireguard-monitoring-jobs): New procedures. (wireguard-service-type): Register it. * tests/services/vpn.scm: New file. * Makefile.am (SCM_TESTS): Register it. * doc/guix.texi (VPN Services): Update doc. --- Makefile.am | 1 + doc/guix.texi | 18 +++++- gnu/services/vpn.scm | 123 +++++++++++++++++++++++++++++++++++++++-- tests/services/vpn.scm | 80 +++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 6 deletions(-) create mode 100644 tests/services/vpn.scm diff --git a/Makefile.am b/Makefile.am index 13718e4353..fb6e4f57cd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -553,6 +553,7 @@ SCM_TESTS = \ tests/services/lightdm.scm \ tests/services/linux.scm \ tests/services/telephony.scm \ + tests/services/vpn.scm \ tests/sets.scm \ tests/size.scm \ tests/status.scm \ diff --git a/doc/guix.texi b/doc/guix.texi index 60972f408d..4499a911d6 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -32591,9 +32591,23 @@ VPN Services @item @code{dns} (default: @code{#f}) The DNS server(s) to announce to VPN clients via DHCP. +@item @code{monitor-ips?} (default: @code{#f}) +@cindex Dynamic IP, with Wireguard +@cindex dyndns, usage with Wireguard +Whether to monitor the resolved Internet addresses (IPs) of the +endpoints of the configured peers, restarting the service when there is +a mismatch between the endpoint IPs in actual use versus those freshly +resolved from their host names. Set this to @code{#t} if one or more +endpoints use host names provided by a dynamic DNS service to keep +connections working. + +@item @code{monitor-ips-internal} (default: @code{'(next-minute (range 0 60 5))}) +The time interval at which the IP monitoring job should run, provided as +an mcron time specification (@pxref{Guile Syntax,,,mcron}). + @item @code{private-key} (default: @code{"/etc/wireguard/private.key"}) -The private key file for the interface. It is automatically generated if -the file does not exist. +The private key file for the interface. It is automatically generated +if the file does not exist. @item @code{peers} (default: @code{'()}) The authorized peers on this interface. This is a list of diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm index a884d71eb2..e21f999bc0 100644 --- a/gnu/services/vpn.scm +++ b/gnu/services/vpn.scm @@ -11,6 +11,7 @@ ;;; Copyright © 2021 Nathan Dehnel ;;; Copyright © 2022 Cameron V Chaparro ;;; Copyright © 2022 Timo Wilken +;;; Copyright © 2023 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -31,10 +32,12 @@ (define-module (gnu services vpn) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services dbus) + #:use-module (gnu services mcron) #:use-module (gnu services shepherd) #:use-module (gnu system shadow) #:use-module (gnu packages admin) #:use-module (gnu packages vpn) + #:use-module (guix modules) #:use-module (guix packages) #:use-module (guix records) #:use-module (guix gexp) @@ -73,6 +76,8 @@ (define-module (gnu services vpn) wireguard-configuration-addresses wireguard-configuration-port wireguard-configuration-dns + wireguard-configuration-monitor-ips? + wireguard-configuration-monitor-ips-interval wireguard-configuration-private-key wireguard-configuration-peers wireguard-configuration-pre-up @@ -741,6 +746,10 @@ (define-record-type* (default '())) (dns wireguard-configuration-dns ;list of strings (default #f)) + (monitor-ips? wireguard-configuration-monitor-ips? ;boolean + (default #f)) + (monitor-ips-interval wireguard-configuration-monitor-ips-interval + (default '(next-minute (range 0 60 5)))) ;string | list (pre-up wireguard-configuration-pre-up ;list of strings (default '())) (post-up wireguard-configuration-post-up ;list of strings @@ -871,6 +880,49 @@ (define (wireguard-activation config) (chmod #$private-key #o400) (close-pipe pipe)))))) +;;; XXX: Copied from (guix scripts pack), changing define to define*. +(define-syntax-rule (define-with-source (variable args ...) body body* ...) + "Bind VARIABLE to a procedure accepting ARGS defined as BODY, also setting +its source property." + (begin + (define* (variable args ...) + body body* ...) + (eval-when (load eval) + (set-procedure-property! variable 'source + '(define* (variable args ...) body body* ...))))) + +(define (wireguard-service-name interface) + "Return the WireGuard service name (a symbol) configured to use INTERFACE." + (symbol-append 'wireguard- (string->symbol interface))) + +(define-with-source (strip-port/maybe endpoint #:key ipv6?) + "Strip the colon and port, if present in ENDPOINT, a string." + (if ipv6? + (if (string-prefix? "[" endpoint) + (first (string-split (string-drop endpoint 1) #\])) ;ipv6 + endpoint) + (first (string-split endpoint #\:)))) ;ipv4 + +(define (ipv4-address? str) + "Return true if STR denotes an IPv4 address." + (false-if-exception + (->bool (inet-pton AF_INET (strip-port/maybe str))))) + +(define (ipv6-address? str) + "Return true if STR denotes an IPv6 address." + (false-if-exception + (->bool (inet-pton AF_INET6 (strip-port/maybe str #:ipv6? #t))))) + +(define (host-name? name) + "Predicate to check whether NAME is a host name, i.e. not an IP address." + (not (or (ipv6-address? name) (ipv4-address? name)))) + +(define (peers->endpoint-host-names peers) + "Return host names used as the endpoints of PEERS, if any. Any \":PORT\" +suffixes are stripped." + (map strip-port/maybe + (filter host-name? (filter-map wireguard-peer-endpoint peers)))) + (define (wireguard-shepherd-service config) (match-record config (wireguard interface) @@ -878,9 +930,7 @@ (define (wireguard-shepherd-service config) (config (wireguard-configuration-file config))) (list (shepherd-service (requirement '(networking)) - (provision (list - (symbol-append 'wireguard- - (string->symbol interface)))) + (provision (list (wireguard-service-name interface))) (start #~(lambda _ (invoke #$wg-quick "up" #$config))) (stop #~(lambda _ @@ -888,6 +938,69 @@ (define (wireguard-shepherd-service config) #f)) ;stopped! (documentation "Run the Wireguard VPN tunnel")))))) +(define (wireguard-monitoring-jobs config) + (match-record config + (interface monitor-ips? monitor-ips-interval peers) + (let ((host-names (peers->endpoint-host-names peers))) + (if monitor-ips? + (if (null? host-names) + (begin + (warn "monitor-ips? is #t but no host name to monitor") + '()) + ;; The mcron monitor job may be a string or a list; ungexp strips + ;; one quote level, which must be added back when a list is + ;; provided. + (list + #~(job + (if (string? #$monitor-ips-interval) + #$monitor-ips-interval + '#$monitor-ips-interval) + #$(program-file + (format #f "wireguard-~a-monitoring" interface) + (with-imported-modules (source-module-closure + '((gnu services herd))) + #~(begin + (use-modules (gnu services herd) + (ice-9 popen) + (ice-9 textual-ports) + (srfi srfi-1) + (srfi srfi-26)) + + (define (host-name->ip name) + "Return the IP address resolved from NAME." + (let* ((ai (car (getaddrinfo name))) + (sa (addrinfo:addr ai))) + (inet-ntop (sockaddr:fam sa) + (sockaddr:addr sa)))) + + #$(procedure-source strip-port/maybe) + + (define service-name '#$(wireguard-service-name + interface)) + + (when (start-service service-name) + (let* ((resolved-ips (map host-name->ip + '#$host-names)) + (pipe (open-pipe* + OPEN_READ + #$(file-append wireguard-tools + "/bin/wg") + "show" #$interface "endpoints")) + (lines (string-split (get-string-all pipe) + #\newline)) + (used-ips (map (compose + strip-port/maybe + last + (cut string-split <> #\tab)) + lines))) + (close-pipe pipe) + (unless (every (cut member <> used-ips) + resolved-ips) + (format #t "restarting ~a service due to \ +stale endpoint IPs~%" service-name) + (restart-service service-name)))))))))) + '())))) ;monitor-ips? is #f + (define wireguard-service-type (service-type (name 'wireguard) @@ -898,6 +1011,8 @@ (define wireguard-service-type wireguard-activation) (service-extension profile-service-type (compose list - wireguard-configuration-wireguard)))) + wireguard-configuration-wireguard)) + (service-extension mcron-service-type + wireguard-monitoring-jobs))) (description "Set up Wireguard @acronym{VPN, Virtual Private Network} tunnels."))) diff --git a/tests/services/vpn.scm b/tests/services/vpn.scm new file mode 100644 index 0000000000..9c6fa65df6 --- /dev/null +++ b/tests/services/vpn.scm @@ -0,0 +1,80 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2023 Maxim Cournoyer +;;; +;;; 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 (tests services vpn) + #:use-module (gnu packages vpn) + #:use-module (gnu services vpn) + #:use-module (guix gexp) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-64)) + +;;; Commentary: +;;; +;;; Unit tests for the (gnu services vpn) module. +;;; +;;; Code: + +;;; Access some internals for whitebox testing. +(define ipv4-address? (@@ (gnu services vpn) ipv4-address?)) +(define ipv6-address? (@@ (gnu services vpn) ipv6-address?)) +(define host-name? (@@ (gnu services vpn) host-name?)) +(define peers->endpoint-host-names + (@@ (gnu services vpn) peers->endpoint-host-names)) + +(test-begin "vpn-services") + +(test-assert "ipv4-address?" + (every ipv4-address? + (list "192.95.5.67:1234" + "10.0.0.1"))) + +(test-assert "ipv6-address?" + (every ipv6-address? + (list "[2607:5300:60:6b0::c05f:543]:2468" + "2607:5300:60:6b0::c05f:543" + "2345:0425:2CA1:0000:0000:0567:5673:23b5" + "2345:0425:2CA1::0567:5673:23b5"))) + +(define %wireguard-peers + (list (wireguard-peer + (name "dummy1") + (public-key "VlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XjoalC8=") + (endpoint "some.dynamic-dns.service:53281") + (allowed-ips '())) + (wireguard-peer + (name "dummy2") + (public-key "AlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC9=") + (endpoint "example.org") + (allowed-ips '())) + (wireguard-peer + (name "dummy3") + (public-key "BlesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC7=") + (endpoint "10.0.0.7:7777") + (allowed-ips '())) + (wireguard-peer + (name "dummy4") + (public-key "ClesLiEB5BFd//OD2ILKXviolfz+hodG6uZ+XgoalC6=") + (endpoint "[2345:0425:2CA1::0567:5673:23b5]:44444") + (allowed-ips '())))) + +(test-equal "peers->endpoint-host-names" + '("some.dynamic-dns.service" "example.org") + (peers->endpoint-host-names %wireguard-peers)) + +(test-end "vpn-services") base-commit: 242cc93438d67f5b35602d5add02e230850b0b43 -- 2.39.2