Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iuDmH-0007EB-S2 for mharc-grub-devel@gnu.org; Wed, 22 Jan 2020 06:02:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33955) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuDmE-0007AK-4I for grub-devel@gnu.org; Wed, 22 Jan 2020 06:02:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iuDmC-0001Kl-9s for grub-devel@gnu.org; Wed, 22 Jan 2020 06:02:25 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:54202 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iuDmC-0001KW-5D for grub-devel@gnu.org; Wed, 22 Jan 2020 06:02:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579690943; h=from:from: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: in-reply-to:in-reply-to:references:references; bh=A3t8bCGLiFuNXzs46kdVoEBPmUVK00wp5zu9knJwzZw=; b=CczvOYqxBtcAXVDJu0FQFh0v/CmK+9Il++C7r0ukDUck25TbZXsldAWbWIz6ca0k0u3omI Q5MLb874TucYNoDiWW8n6/XtXvE3mLpmg4UziUn6y72/zwFJC2dptbldhMFKEdI0qtZC5Y r54RkZHCVkmL6a/Inhbsv61uZ2Ir1C8= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-85-eqS9eldUPbWCkVSi_y32mQ-1; Wed, 22 Jan 2020 06:02:20 -0500 Received: by mail-wm1-f71.google.com with SMTP id t16so1891878wmt.4 for ; Wed, 22 Jan 2020 03:02:19 -0800 (PST) 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=4y5PZvA1TiZ/hqtN8VVctAHcoPc/3ngLWU4c+Z4URQw=; b=Qb/+ByY8VhkQWYfrhxcXZtugnjjiF64xV+Tw8thrJK/0w85sPp+g/xNpPNRcjW5XL3 DKXdS3W/SryH9FzWfpMMq22SzeDfIKuaZauUImHtck7N+L13uRM3ZqRm88HfbLVxPt6y RuWHHByKpLcNGx1Sy5rVkIvRZEfAQmvNw9jzkcxL1EUhK3SEC0/DQ4Axa/AKzi8g6Jky jljDROo5a2STe+ahUvjSEwJWoHTEl4X87gYjE24CrYigDz8Bxjdg9AQsWVs/TuPwFt8i xlNZEg7TqipyhhO2VyyvvmZrTzD49fioR177XGtJPCz6F+SWo1GQuTpGIf0gol35d59r EXUg== X-Gm-Message-State: APjAAAU4MiyOgMtGksokuYAhPWRMfPDNkA9+SEonqFHfBnIgyV9sHWNL Rf05LIvt5cGp+sYDsomU6nYgs7aXN7l1fbox7X/QmrHEhIVamXRhXpKCoa5Okx9du3v28ZRJpPv cEmB2HNNjks4= X-Received: by 2002:adf:cd92:: with SMTP id q18mr10335372wrj.261.1579690938418; Wed, 22 Jan 2020 03:02:18 -0800 (PST) X-Google-Smtp-Source: APXvYqyyS2R0viS3nF+cAl2JOhy7jUlkHgd4H0ntY/3IH846xt1x8T+Rcyzni17zkM6Lf05N1GrAmQ== X-Received: by 2002:adf:cd92:: with SMTP id q18mr10335336wrj.261.1579690938109; Wed, 22 Jan 2020 03:02:18 -0800 (PST) Received: from minerva.redhat.com ([2a01:c50f:ab80:6700:527f:85b3:5615:ff0b]) by smtp.gmail.com with ESMTPSA id c2sm56546692wrp.46.2020.01.22.03.02.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 03:02:17 -0800 (PST) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Daniel Kiper , Paulo Flabiano Smorigo , Vladimir Serbinenko , Javier Martinez Canillas Subject: [PATCH v6 4/5] normal/main: Search for specific config files for netboot Date: Wed, 22 Jan 2020 12:01:55 +0100 Message-Id: <20200122110157.1062269-5-javierm@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200122110157.1062269-1-javierm@redhat.com> References: <20200122110157.1062269-1-javierm@redhat.com> MIME-Version: 1.0 X-MC-Unique: eqS9eldUPbWCkVSi_y32mQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII 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: 207.211.31.120 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: Wed, 22 Jan 2020 11:02:28 -0000 From: Paulo Flabiano Smorigo This patch implements a search for a specific configuration when the config 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 It is enabled by default but can be disabled by setting the environment variable 'feature_net_search_cfg' to 'n' in an embedded configuration. Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=3D873406 Signed-off-by: Paulo Flabiano Smorigo Signed-off-by: Javier Martinez Canillas --- Changes in v6: - Allow the feature to be disabled by setting feature_net_search_cfg=3Dn. - Remove Reviewed-by tag from Daniel Kiper since the patch #4 changed. Changes in v5: - Don't mix varible declarations and code. - Don't use variable length arrays. - Fix coding style issues. - Fix function prototype. - Include missing header. Changes in v4: None Changes in v3: - Add Reviewed-by tag from Daniel Kiper. Changes in v2: None grub-core/net/net.c | 131 ++++++++++++++++++++++++++++++++++++++++ grub-core/normal/main.c | 26 ++++++-- include/grub/net.h | 2 + 3 files changed, 155 insertions(+), 4 deletions(-) diff --git a/grub-core/net/net.c b/grub-core/net/net.c index d5d726a315e..f82ac15b4ec 100644 --- a/grub-core/net/net.c +++ b/grub-core/net/net.c @@ -1735,6 +1735,137 @@ grub_net_restore_hw (void) return GRUB_ERR_NONE; } =20 +grub_err_t +grub_net_search_config_file (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_file_t file; + + grub_dprintf ("net", "attempt to fetch config %s\n", config); + + 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 *ptr; + int client_uuid_len; + char *client_uuid_var; + const char *client_uuid; + + client_uuid_len =3D sizeof ("net_") + grub_strlen (inf->name) + + sizeof ("_clientuuid") + 1; + + client_uuid_var =3D grub_zalloc (client_uuid_len); + if (!client_uuid_var) + return grub_errno; + + grub_snprintf (client_uuid_var, client_uuid_len, + "net_%s_clientuuid", inf->name); + + client_uuid =3D grub_env_get (client_uuid_var); + grub_free (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); + + 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%02= X%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->address= )->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_delroute= ; diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c index 1b03dfd57b9..c4ebe9e22ad 100644 --- a/grub-core/normal/main.c +++ b/grub-core/normal/main.c @@ -18,6 +18,7 @@ */ =20 #include +#include #include #include #include @@ -323,10 +324,27 @@ grub_cmd_normal (struct grub_command *cmd __attribute= __ ((unused)), =20 prefix =3D grub_env_get ("prefix"); if (prefix) -=09{ -=09 config =3D grub_xasprintf ("%s/grub.cfg", prefix); -=09 if (! config) -=09 goto quit; + { + grub_size_t config_len; + int disable_net_search =3D 0; + const char *net_search_cfg; + + config_len =3D grub_strlen (prefix) + + sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX= XXXX"); + config =3D grub_malloc (config_len); + + if (!config) + goto quit; + + grub_snprintf (config, config_len, "%s/grub.cfg", prefix); + + net_search_cfg =3D grub_env_get ("feature_net_search_cfg"); + if (net_search_cfg && net_search_cfg[0] =3D=3D 'n') + disable_net_search =3D 1; + + if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) =3D= =3D 0 && + !disable_net_search) + grub_net_search_config_file (config); =20 =09 grub_enter_normal_mode (config); =09 grub_free (config); diff --git a/include/grub/net.h b/include/grub/net.h index c00b0ab117c..7ae4b6bd805 100644 --- a/include/grub/net.h +++ b/include/grub/net.h @@ -569,6 +569,8 @@ grub_net_add_dns_server (const struct grub_net_network_= level_address *s); void grub_net_remove_dns_server (const struct grub_net_network_level_address *s= ); =20 +grub_err_t +grub_net_search_config_file (char *config); =20 extern char *grub_net_default_server; =20 --=20 2.24.1