From patchwork Sun Jan 17 15:08:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jouke Witteveen X-Patchwork-Id: 1857 Return-Path: Delivered-To: patchwork@archlinux.org Received: from mail.archlinux.org [95.216.189.61] by patchwork.archlinux.org with IMAP (fetchmail-6.4.15) for (single-drop); Sun, 17 Jan 2021 15:08:15 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id kD0WNd5SBGD7agUAK+/4rw (envelope-from ) for ; Sun, 17 Jan 2021 15:08:14 +0000 Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)) (No client certificate requested) by mail.archlinux.org (Postfix) with ESMTPS id 4157F378EE3; Sun, 17 Jan 2021 15:08:14 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id EA0902C5A0; Sun, 17 Jan 2021 15:08:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lists.archlinux.org; s=luna; t=1610896093; bh=j+tEgmnIWnB55xmwTSD7JfIf95+bgsD30QfA4n0bi4g=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc; b=HSQyFgHFUHL0JUgFLp8uthAlR+DIyoayEBcQh2UHlAXWbaT/gXecMJuZGZuj2s/rF WAcYps1/MRCLFqbAGGvO6mbHqM/2g5GA7UuxUK6kQ7geU6TM1hr2PkXUzVbdBsH735 zrQYNUowYWO6dl0hb9Hlt+Em8PHvEbcsP8pwKqj0v/3rbBRW7WREyUXyZu6fSNyMRY nVdgQZRZhCUkXrFWOlAbsmM6EMED2DTMX1NjSZL7tPpFs6ba+TDx+iXtxTOXF1RHjs 84m+WKz7FKhC0q6VZRmQ/5YShxVk7OWc1WLvXLy8ZFJXTOs43IBGGQftvu4AwnGtK0 VGBci+dLpxIpzUw3hKjdPn0CxGxHLMT8dFtb9+HARhsUo4dPDXZ5D/WuKiaQAy5oXq pF0iWi71IqgVIbMXX+h7RWEzXlptx/UWlr+gBaf4+LhRBYwwq4lZ+3FbIKtQpOmzGg mnindEX8VgQMkQHfZ9zdmjdlJZqN1Qm0RCZo4BfvDkox6+m/xHaAz+88oznARFp6Wt pFdzZQm5P9J1QSqmo9utSo1YQf10TQ9dj+steRp29/4ZLDzdjsPXJllcpTcDfB5NeA IBd5lKc9ttXIBq7sgmGxejHpzODdckdpZBRY53rQZj1XMF14a5pRontRuPdQX8zwdQ ZZlJMj8Pw9OpACsIluykskYA= Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 49F8E2C588 for ; Sun, 17 Jan 2021 15:08:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on luna.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=DKIM_SIGNED=0.1, DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,FREEMAIL_FROM=0.5, RCVD_IN_DNSWL_MED=-2.3,T_DMARC_POLICY_NONE=0.01, T_DMARC_SIMPLE_DKIM=0.01 autolearn=failed autolearn_force=no version=3.4.4 X-Spam-BL-Results: [127.0.9.2] Received: from mail.archlinux.org (mail.archlinux.org [IPv6:2a01:4f9:c010:3052::1]) by luna.archlinux.org (Postfix) with ESMTPS for ; Sun, 17 Jan 2021 15:08:11 +0000 (UTC) Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.archlinux.org (Postfix) with ESMTPS id 8C884378EE2 for ; Sun, 17 Jan 2021 15:08:10 +0000 (UTC) Received: by mail-ej1-x62c.google.com with SMTP id by1so13728869ejc.0 for ; Sun, 17 Jan 2021 07:08:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=EobShgdIb0vEBISpn+N/KRQLplWFulj3Guof44YsEvc=; b=M4AQpnw/fQlL2BogiDp6JcuduBsVrFyO70brgpCu+dk4pdzdTuP7ZZ35+xx3Fn3ewu oyBMLRC/wZdYCXW4yF9lECwKvv9jMIzVBrQWc9g5jz0UCfIw+hMgtGMlHNw1/rz6P+Zp kqvXhUAnPgP/It8/NErlW0ReWcmOM34k3l8ytvLAn12AQWEFUgMoOThX2Muqcf6JlBaE 6hLJphB/jERZY9IZirdw0PSpimMZzCggR3DOnsXlRlU7MpBB2bXMBbw0fNPBBBDdv7fJ U9mVbuox7SyxBri16YoeEVrGjSiQ7JyObN22RKaFOagrXVz9l6F9CmF2VX3582fZhaBh NGCA== X-Gm-Message-State: AOAM533jWs6mMogpuXq4K6lVLlMkDo+4xKokXhGELgwz/RniOOCV6BNr myPHb63TTzxp6sXxTVNXJEiyBkOwUYy3cw== X-Google-Smtp-Source: ABdhPJzhMkCUJsLxAUHKiA//ENrvhXkL/q84Le5rEbRd6F8JUkpesPBLnj6DtfhqAZAaisbBmPsx7Q== X-Received: by 2002:a17:907:a82:: with SMTP id by2mr5705519ejc.200.1610896089988; Sun, 17 Jan 2021 07:08:09 -0800 (PST) Received: from Mindship-03 (252-168-166-62.ftth.glasoperator.nl. [62.166.168.252]) by smtp.gmail.com with ESMTPSA id qk1sm3005548ejb.86.2021.01.17.07.08.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Jan 2021 07:08:09 -0800 (PST) Date: Sun, 17 Jan 2021 16:08:04 +0100 To: arch-projects@archlinux.org Message-ID: MIME-Version: 1.0 Content-Disposition: inline Subject: [arch-projects] [netctl][PATCH] Add verify subcommand to netctl X-BeenThere: arch-projects@lists.archlinux.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Arch Linux projects development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jouke Witteveen via arch-projects From: Jouke Witteveen Reply-To: Arch Linux projects development discussion Cc: Jouke Witteveen Errors-To: arch-projects-bounces@lists.archlinux.org Sender: "arch-projects" Authentication-Results: mail.archlinux.org; dkim=pass header.d=lists.archlinux.org header.s=luna header.b=HSQyFgHF; dmarc=pass (policy=none) header.from=archlinux.org; spf=pass (mail.archlinux.org: domain of arch-projects-bounces@lists.archlinux.org designates 2a01:4f8:160:3033::2 as permitted sender) smtp.mailfrom=arch-projects-bounces@lists.archlinux.org X-Rspamd-Queue-Id: 4157F378EE3 X-Spamd-Result: default: False [-0.71 / 15.00]; ARC_NA(0.00)[]; HAS_REPLYTO(0.00)[arch-projects@lists.archlinux.org]; R_DKIM_ALLOW(-0.20)[lists.archlinux.org:s=luna]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2a01:4f8:160:3033::2:c]; FORGED_SENDER_MAILLIST(0.00)[]; TAGGED_RCPT(0.00)[]; REPLYTO_ADDR_EQ_FROM(0.00)[]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; MID_RHS_NOT_FQDN(0.50)[]; DKIM_TRACE(0.00)[lists.archlinux.org:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[archlinux.org,none]; NEURAL_HAM(-0.00)[-1.000]; MAILLIST(-0.20)[mailman]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_LAST(0.00)[]; ASN(0.00)[asn:24940, ipnet:2a01:4f8::/29, country:DE]; FROM_NEQ_ENVFROM(0.00)[arch-projects@lists.archlinux.org,arch-projects-bounces@lists.archlinux.org]; RCVD_COUNT_SEVEN(0.00)[7]; FREEMAIL_CC(0.00)[gmail.com] X-Rspamd-Server: mail.archlinux.org The name of the subcommand mirrors the same subcommand of systemd-analyze. Verification before restarting would be useful. However, verification requires the profile file to be readable by the current user. The current user may be a regular user with permission to control systemd, so restarting does not require the profile file to be readable. Therefore, we do not attempt verification before restarting. --- docs/netctl.1.txt | 6 +++++- src/lib/globals | 6 ++++++ src/netctl.in | 26 ++++++++++++++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/docs/netctl.1.txt b/docs/netctl.1.txt index d6d9360..76d45b0 100644 --- a/docs/netctl.1.txt +++ b/docs/netctl.1.txt @@ -82,7 +82,11 @@ The following commands are understood: *edit [+PROFILE+]*:: Open the file of the specified profile in an editor. This does not - reenable or restart any profiles. + reenable, restart, or verify any profiles. + +*verify [+PROFILE+]*:: + Check the file of the specified profile for syntax errors. If no + errors are found, no output is produced. *wait-online [+PROFILE+]*:: Wait until the interface of the profile has a routable IP address of diff --git a/src/lib/globals b/src/lib/globals index 74ce623..32a1802 100644 --- a/src/lib/globals +++ b/src/lib/globals @@ -109,6 +109,12 @@ list_profiles() { find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name $'*\n*' -not -name '*.action' -not -name '*.conf' -not -name '*.service' -printf '%f\n' } +## Exit if a profile file is not syntactically correct +# $1: profile name +verify_profile() { + /bin/bash -n "$PROFILE_DIR/$1" || exit 1 +} + ## Sources all hooks and a profile (but no interface configuration) # $1: profile name load_profile() { diff --git a/src/netctl.in b/src/netctl.in index c1ed493..a580048 100644 --- a/src/netctl.in +++ b/src/netctl.in @@ -23,7 +23,8 @@ Commands: disable [PROFILE] Disable the systemd unit for a profile reenable [PROFILE] Reenable the systemd unit for a profile is-enabled [PROFILE] Check whether a profile is enabled - edit [PROFILE] Edit a profile + edit [PROFILE] Edit a profile file + verify [PROFILE] Check the syntax of a profile file wait-online [PROFILE] Wait for a profile to finish connecting END } @@ -71,9 +72,6 @@ stop_all() { switch_to() { cd "$PROFILE_DIR" - if [[ ! -r $1 ]]; then - exit_error "Profile '$1' does not exist or is not readable" - fi # We assume interface names are not quoted # Using read removes leading whitespace read InterfaceLine < \ @@ -129,6 +127,11 @@ unit_disable() { "@systemdsystemconfdir@/$unit.d"{/profile.conf,} } +unit_reenable() { + unit_disable "$1" + unit_enable "$1" +} + wait_online() { local profile="$1" if sd_call "is-active --quiet" "$profile"; then @@ -163,20 +166,19 @@ case $# in sd_call "$1" "$2";; switch-to) ensure_root "$(basename "$0")" + verify_profile "$2" switch_to "$2";; - enable|disable) + enable|disable|reenable) ensure_root "$(basename "$0")" + if [[ $1 != "disable" ]]; then + verify_profile "$2" + fi "unit_$1" "$2" if systemd-notify --booted; then systemctl daemon-reload fi;; - reenable) - ensure_root "$(basename "$0")" - unit_disable "$2" - unit_enable "$2" - if systemd-notify --booted; then - systemctl daemon-reload - fi;; + verify) + verify_profile "$2";; edit) exec ${EDITOR:-nano} "$PROFILE_DIR/$2";; wait-online)