Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iGWJh-0000jz-VH for mharc-grub-devel@gnu.org; Fri, 04 Oct 2019 18:44:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48826) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGWJe-0000hV-R3 for grub-devel@gnu.org; Fri, 04 Oct 2019 18:44:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iGWJd-0004kk-DT for grub-devel@gnu.org; Fri, 04 Oct 2019 18:44:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38010) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iGWJd-0004kV-5l for grub-devel@gnu.org; Fri, 04 Oct 2019 18:44:49 -0400 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 224702D6A04 for ; Fri, 4 Oct 2019 22:44:48 +0000 (UTC) Received: by mail-wr1-f70.google.com with SMTP id b6so3395282wrw.2 for ; Fri, 04 Oct 2019 15:44:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iMCVPF+EHjYHljAnIues1ODR52S90i/mGNoxYId/KB8=; b=SEhQ/7z90DHnRS2w1v66++rLzCiWSPgT3En4YxY4Ha9QSAxT5AIRxao33oyBCDAVn7 dn0INgIeIwqTzr8LBRrZ0tY2mHqSZz15Op+lgYVXVYiphv71us/mrJqdVmqjuV5Urdte we25R1JCAEszEtCvkZO74smoRyoHIaHmLLwp8JknPrXd+ZMfbjmKxxEl+Rndf0d0jga4 /pdFhgjgn7Qshjival556CVrHO+OfNo3173wB19GobI9DWuizrDp9uvXwzPZi9u2hFAP uhzz8GufGhU22v87FWBvsE5fX3Dd0Z7c/Rb7LEAlstskm7qwyN8oeub2U1x7hWvl/UGn AEOA== X-Gm-Message-State: APjAAAWmdnMn/rgjLQbppMOtVoTg5nDgXLH6V7Ums//lHgCR2miL1PY9 LzRjVTikbwyYw1vodYGhb3y7WPWrTkOLeNmIm6Zcb8qgjgWeRO35LUT3GEGFH6bJ8Oqq+V6y2ue +EiKeR8yTzAs= X-Received: by 2002:adf:ce86:: with SMTP id r6mr13492026wrn.57.1570229086638; Fri, 04 Oct 2019 15:44:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqwsAa9ackchSntWXVCUfcsUr0VB2fSoQvyo1X6kigbCk17d4WEP9JHBRGdIEYcv+g1uyI8hgA== X-Received: by 2002:adf:ce86:: with SMTP id r6mr13492018wrn.57.1570229086410; Fri, 04 Oct 2019 15:44:46 -0700 (PDT) Received: from minerva.home ([90.168.169.92]) by smtp.gmail.com with ESMTPSA id a204sm13033256wmh.21.2019.10.04.15.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2019 15:44:45 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Vladimir Serbinenko , Daniel Kiper , Paulo Flabiano Smorigo , Javier Martinez Canillas Subject: [PATCH 3/3] Search for specific config files for netboot Date: Sat, 5 Oct 2019 00:44:27 +0200 Message-Id: <20191004224427.22480-4-javierm@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191004224427.22480-1-javierm@redhat.com> References: <20191004224427.22480-1-javierm@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Oct 2019 22:44:52 -0000 From: Paulo Flabiano Smorigo This patch implements a search for a specific configuration when the conf= ig file is on a remoteserver. It uses the following order: 1) DHCP client UUID option. 2) MAC address (in lower case hexadecimal with dash separators); 3) IP (in upper case hexadecimal) or IPv6; 4) The original grub.cfg file. This procedure is similar to what is used by pxelinux and yaboot: http://www.syslinux.org/wiki/index.php/PXELINUX#config Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=3D873406 Signed-off-by: Paulo Flabiano Smorigo Signed-off-by: Javier Martinez Canillas --- grub-core/net/net.c | 117 ++++++++++++++++++++++++++++++++++++++++ grub-core/normal/main.c | 17 ++++-- include/grub/net.h | 3 ++ 3 files changed, 133 insertions(+), 4 deletions(-) diff --git a/grub-core/net/net.c b/grub-core/net/net.c index d5d726a315e..6492bb74274 100644 --- a/grub-core/net/net.c +++ b/grub-core/net/net.c @@ -1735,6 +1735,123 @@ grub_net_restore_hw (void) return GRUB_ERR_NONE; } =20 +grub_err_t +grub_net_search_configfile (char *config) +{ + grub_size_t config_len; + char *suffix; + + auto int search_through (grub_size_t num_tries, grub_size_t slice_size= ); + int search_through (grub_size_t num_tries, grub_size_t slice_size) + { + while (num_tries-- > 0) + { + grub_dprintf ("net", "attempt to fetch config %s\n", config); + + grub_file_t file; + file =3D grub_file_open (config, GRUB_FILE_TYPE_CONFIG); + + if (file) + { + grub_file_close (file); + return 0; + } + else + { + if (grub_errno =3D=3D GRUB_ERR_IO) + grub_errno =3D GRUB_ERR_NONE; + } + + if (grub_strlen (suffix) < slice_size) + break; + + config[grub_strlen (config) - slice_size] =3D '\0'; + } + + return 1; + } + + config_len =3D grub_strlen (config); + config[config_len] =3D '-'; + suffix =3D config + config_len + 1; + + struct grub_net_network_level_interface *inf; + FOR_NET_NETWORK_LEVEL_INTERFACES (inf) + { + /* By the Client UUID. */ + + char client_uuid_var[sizeof ("net_") + grub_strlen (inf->name) + + sizeof ("_clientuuid") + 1]; + grub_snprintf (client_uuid_var, sizeof (client_uuid_var), + "net_%s_clientuuid", inf->name); + + const char *client_uuid; + client_uuid =3D grub_env_get (client_uuid_var); + + if (client_uuid) + { + grub_strcpy (suffix, client_uuid); + if (search_through (1, 0) =3D=3D 0) return GRUB_ERR_NONE; + } + + /* By the MAC address. */ + + /* Add ethernet type */ + grub_strcpy (suffix, "01-"); + + grub_net_hwaddr_to_str (&inf->hwaddress, suffix + 3); + + char *ptr; + for (ptr =3D suffix; *ptr; ptr++) + if (*ptr =3D=3D ':') + *ptr =3D '-'; + + if (search_through (1, 0) =3D=3D 0) return GRUB_ERR_NONE; + + /* By IP address */ + + switch ((&inf->address)->type) + { + case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4: + { + grub_uint32_t n =3D grub_be_to_cpu32 ((&inf->address)->ipv4)= ; + grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%02X%02X%= 02X%02X", \ + ((n >> 24) & 0xff), ((n >> 16) & 0xff), = \ + ((n >> 8) & 0xff), ((n >> 0) & 0xff)); + + if (search_through (8, 1) =3D=3D 0) return GRUB_ERR_NONE; + break; + } + case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6: + { + char buf[GRUB_NET_MAX_STR_ADDR_LEN]; + struct grub_net_network_level_address base; + base.type =3D GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6; + grub_memcpy (&base.ipv6, ((&inf->address)->ipv6), 16); + grub_net_addr_to_str (&base, buf); + + for (ptr =3D buf; *ptr; ptr++) + if (*ptr =3D=3D ':') + *ptr =3D '-'; + + grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%s", buf)= ; + if (search_through (1, 0) =3D=3D 0) return GRUB_ERR_NONE; + break; + } + case GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV: + return grub_error (GRUB_ERR_BUG, "shouldn't reach here"); + default: + return grub_error (GRUB_ERR_BUG, + "unsupported address type %d", (&inf->addre= ss)->type); + } + } + + /* Remove the remaining minus sign at the end. */ + config[config_len] =3D '\0'; + + return GRUB_ERR_NONE; +} + static struct grub_preboot *fini_hnd; =20 static grub_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delrou= te; diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c index 1b03dfd57b9..62ad546f4f7 100644 --- a/grub-core/normal/main.c +++ b/grub-core/normal/main.c @@ -323,10 +323,19 @@ grub_cmd_normal (struct grub_command *cmd __attribu= te__ ((unused)), =20 prefix =3D grub_env_get ("prefix"); if (prefix) - { - config =3D grub_xasprintf ("%s/grub.cfg", prefix); - if (! config) - goto quit; + { + grub_size_t config_len; + config_len =3D grub_strlen (prefix) + + sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXX= XXXXX"); + config =3D grub_malloc (config_len); + + if (! config) + goto quit; + + grub_snprintf (config, config_len, "%s/grub.cfg", prefix); + + if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) =3D= =3D 0) + grub_net_search_configfile (config); =20 grub_enter_normal_mode (config); grub_free (config); diff --git a/include/grub/net.h b/include/grub/net.h index 556c54e579f..ff6d347f7da 100644 --- a/include/grub/net.h +++ b/include/grub/net.h @@ -578,4 +578,7 @@ extern char *grub_net_default_server; =20 #define VLANTAG_IDENTIFIER 0x8100 =20 +grub_err_t +grub_net_search_configfile (char *config); + #endif /* ! GRUB_NET_HEADER */ --=20 2.21.0