From patchwork Sat Jun 12 17:19:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Solene Rapenne X-Patchwork-Id: 30185 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 987C827BC81; Sat, 12 Jun 2021 18:21:31 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 1C76E27BC78 for ; Sat, 12 Jun 2021 18:21:31 +0100 (BST) Received: from localhost ([::1]:53966 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ls7K5-0002Fe-K3 for patchwork@mira.cbaines.net; Sat, 12 Jun 2021 13:21:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39134) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ls7Jf-0002FF-Vj for guix-patches@gnu.org; Sat, 12 Jun 2021 13:21:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:58669) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ls7Je-0003q6-Ms for guix-patches@gnu.org; Sat, 12 Jun 2021 13:21:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ls7Je-0002JH-FU for guix-patches@gnu.org; Sat, 12 Jun 2021 13:21:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#48975] New firewall service Resent-From: Solene Rapenne Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 12 Jun 2021 17:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 48975 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: 48975@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16235184178809 (code B ref -1); Sat, 12 Jun 2021 17:21:02 +0000 Received: (at submit) by debbugs.gnu.org; 12 Jun 2021 17:20:17 +0000 Received: from localhost ([127.0.0.1]:41982 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ls7Iu-0002I1-KB for submit@debbugs.gnu.org; Sat, 12 Jun 2021 13:20:16 -0400 Received: from lists.gnu.org ([209.51.188.17]:41456) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ls7It-0002Hu-CU for submit@debbugs.gnu.org; Sat, 12 Jun 2021 13:20:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39032) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ls7It-00027k-49 for guix-patches@gnu.org; Sat, 12 Jun 2021 13:20:15 -0400 Received: from perso.pw ([163.172.223.238]:29991) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ls7Ip-0003J8-To for guix-patches@gnu.org; Sat, 12 Jun 2021 13:20:14 -0400 Received: from perso.pw (localhost [127.0.0.1]) by perso.pw (OpenSMTPD) with ESMTP id ae87e3e5 for ; Sat, 12 Jun 2021 19:20:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=perso.pw; h=date:from:to :subject:message-id:mime-version:content-type :content-transfer-encoding; s=1337; bh=BMUX1hiSWHT9VDrXpZRKW5f7U ZA=; b=kdP2oNesEKBbrp8+c0IcDoWLwyThWKh1KPL2zzDviVDXqIj4uc5JSIGvr 7RWi1I9SqeqOSGYDoGJMZ4tO9gezj0AiJ1Ok5cHAOj9Gm7WNjUBVbTdQd5jC1C9x 7QsHsT6duXQGmYkL+XfR1hYkHoFI2V2pQd4sLjF68sOjECiTDE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=perso.pw; h=date:from:to :subject:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=1337; b=MkmlECIBEuqvTThbAvN zVSCE/0P1n98AiXY2ug3U7ArR7Y7fXdqzfypmQxJAljm5spZIAOjs+TZ8FOStnRI TGP1/zorODxubwYEd4Jus8GnKYXTrUpyw/Dtb6m0oi1zX33YX/wdxEh9WIOdofco USIUQDirAPUji2JOBDSoEAlA= Received: from localhost (176-154-164-34.abo.bbox.fr [176.154.164.34]) by perso.pw (OpenSMTPD) with ESMTPSA id 556dfa08 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO) for ; Sat, 12 Jun 2021 19:20:00 +0200 (CEST) Date: Sat, 12 Jun 2021 19:19:59 +0200 Message-ID: <20210612191959.6394494e@perso.pw> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Received-SPF: pass client-ip=163.172.223.238; envelope-from=solene@perso.pw; helo=perso.pw X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" Reply-to: Solene Rapenne X-ACL-Warn: , Solene Rapenne via Guix-patches X-Patchwork-Original-From: Solene Rapenne via Guix-patches via From: Solene Rapenne X-getmail-retrieved-from-mailbox: Patches Hello, I wrote a new firewall service, I already wrote an email to guix-devel about it and I've been suggested to submit it here. The idea is to propose an easy way to manage your firewall. On a personal computer or a server with no fancy network, you certainly want to block access from the outside to all the ports except a few ones. The configuration looks like this, currently it only supports TCP and UDP ports. Maybe NAT could be added later or other feature, I'm opened to suggestions. (service firewall-service-type (firewall-configuration (udp '(53)) (tcp '(22 70 1965)))) Here is the code, I took bits from iptables as a base and then used the Tor service way to generate the configuration file. diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm index 87b3d754a3..d311f95448 100644 --- a/gnu/services/networking.scm +++ b/gnu/services/networking.scm @@ -221,7 +221,11 @@ keepalived-configuration keepalived-configuration? - keepalived-service-type)) + keepalived-service-type + + firewall-service-type + firewall-configuration + firewall-configuration?)) ;;; Commentary: ;;; @@ -2190,4 +2194,76 @@ of the IPFS peer-to-peer storage network."))) "Run @uref{https://www.keepalived.org/, Keepalived} routing software."))) + +;;; +;;; Firewall +;;; + +(define-record-type* + firewall-configuration make-firewall-configuration + firewall-configuration? + (tcp firewall-configuration-tcp + (default '())) + (udp firewall-configuration-udp + (default '()))) + +(define (firewall-configuration->file tcp udp) + "Return the iptables rules from the ports list" + (computed-file + "firewall-generated-rules" + (with-imported-modules '((guix build utils)) + #~(begin + (use-modules (guix build utils) + (ice-9 match)) + (call-with-output-file #$output + (lambda (out) + (display "\ +*filter +:INPUT DROP +:FORWARD DROP +:OUTPUT ACCEPT +-A INPUT -i lo -j ACCEPT +-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT\n" out) + + ;; tcp rules + (when (not (null? (list #$@tcp))) + (format out "\ +~{-A INPUT -p tcp --dport ~a -j ACCEPT~%~}" + (list #$@tcp))) + + ;; udp rules + (when (not (null? (list #$@udp))) + (format out "\ +~{-A INPUT -p udp --dport ~a -j ACCEPT~%~}" + (list #$@udp))) + + (display "COMMIT\n" out) + #t)))))) + +(define firewall-shepherd-service + (match-lambda + (($ tcp udp) + (let* ((iptables-restore (file-append iptables "/sbin/iptables-restore")) + (ip6tables-restore (file-append iptables "/sbin/ip6tables-restore")) + (ruleset (firewall-configuration->file tcp udp))) + (shepherd-service + (documentation "Easy firewall management") + (provision '(firewall)) + (start #~(lambda _ + (invoke #$iptables-restore #$ruleset) + (invoke #$ip6tables-restore #$ruleset))) + (stop #~(lambda _ + (invoke #$iptables-restore #$ruleset) + (invoke #$ip6tables-restore #$ruleset)))))))) + +(define firewall-service-type + (service-type + (name 'firewall) + (description + "Run @command{iptables-restore}, setting up the specified rules.") + (extensions + (list (service-extension shepherd-root-service-type + (compose list firewall-shepherd-service)))))) + + ;;; networking.scm ends here