nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0109-Prepend-prefix-when-HTTP-path-is-relative.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Stephen Benjamin <stephen@redhat.com>
5593c8
Date: Thu, 16 Aug 2018 16:58:51 -0400
5593c8
Subject: [PATCH] Prepend prefix when HTTP path is relative
5593c8
5593c8
This sets a couple of variables.  With the url http://www.example.com/foo/bar :
5593c8
http_path: /foo/bar
5593c8
http_url: http://www.example.com/foo/bar
5593c8
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
---
5593c8
 grub-core/kern/main.c    | 10 +++++-
5593c8
 grub-core/net/efi/http.c | 82 ++++++++++++++++++++++++++++++++++++------------
5593c8
 2 files changed, 71 insertions(+), 21 deletions(-)
5593c8
5593c8
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
1c6ba0
index 48058d983c..4ec3f5e4d3 100644
5593c8
--- a/grub-core/kern/main.c
5593c8
+++ b/grub-core/kern/main.c
5593c8
@@ -131,11 +131,19 @@ grub_set_prefix_and_root (void)
5593c8
   if (fwdevice && fwpath)
5593c8
     {
5593c8
       char *fw_path;
5593c8
+      char separator[3] = ")";
5593c8
 
5593c8
-      fw_path = grub_xasprintf ("(%s)/%s", fwdevice, fwpath);
5593c8
+      grub_dprintf ("fw_path", "\n");
5593c8
+      grub_dprintf ("fw_path", "fwdevice:\"%s\" fwpath:\"%s\"\n", fwdevice, fwpath);
5593c8
+
5593c8
+      if (!grub_strncmp(fwdevice, "http", 4) && fwpath[0] != '/')
5593c8
+	grub_strcpy(separator, ")/");
5593c8
+
5593c8
+      fw_path = grub_xasprintf ("(%s%s%s", fwdevice, separator, fwpath);
5593c8
       if (fw_path)
5593c8
 	{
5593c8
 	  grub_env_set ("fw_path", fw_path);
5593c8
+	  grub_dprintf ("fw_path", "fw_path:\"%s\"\n", fw_path);
5593c8
 	  grub_free (fw_path);
5593c8
 	}
5593c8
     }
5593c8
diff --git a/grub-core/net/efi/http.c b/grub-core/net/efi/http.c
1c6ba0
index 243acbaa35..de351b2cd0 100644
5593c8
--- a/grub-core/net/efi/http.c
5593c8
+++ b/grub-core/net/efi/http.c
5593c8
@@ -9,10 +9,52 @@
5593c8
 static void
5593c8
 http_configure (struct grub_efi_net_device *dev, int prefer_ip6)
5593c8
 {
5593c8
+  grub_efi_ipv6_address_t address;
5593c8
   grub_efi_http_config_data_t http_config;
5593c8
   grub_efi_httpv4_access_point_t httpv4_node;
5593c8
   grub_efi_httpv6_access_point_t httpv6_node;
5593c8
   grub_efi_status_t status;
5593c8
+  int https;
5593c8
+  char *http_url;
5593c8
+  const char *rest, *http_server, *http_path = NULL;
5593c8
+
5593c8
+  http_server = grub_env_get ("root");
5593c8
+  https = (grub_strncmp (http_server, "https", 5) == 0) ? 1 : 0;
5593c8
+
5593c8
+  /* extract http server + port */
5593c8
+  if (http_server)
5593c8
+    {
5593c8
+      http_server = grub_strchr (http_server, ',');
5593c8
+      if (http_server)
5593c8
+	http_server++;
5593c8
+    }
5593c8
+
5593c8
+  /* fw_path is like (http,192.168.1.1:8000)/httpboot, extract path part */
5593c8
+  http_path = grub_env_get ("fw_path");
5593c8
+  if (http_path)
5593c8
+    {
5593c8
+      http_path = grub_strchr (http_path, ')');
5593c8
+      if (http_path)
5593c8
+	{
5593c8
+	  http_path++;
5593c8
+	  grub_env_unset ("http_path");
5593c8
+	  grub_env_set ("http_path", http_path);
5593c8
+	}
5593c8
+    }
5593c8
+
5593c8
+  if (http_server && http_path)
5593c8
+    {
5593c8
+      if (grub_efi_string_to_ip6_address (http_server, &address, &rest) && *rest == 0)
5593c8
+	http_url = grub_xasprintf ("%s://[%s]%s", https ? "https" : "http", http_server, http_path);
5593c8
+      else
5593c8
+	http_url = grub_xasprintf ("%s://%s%s", https ? "https" : "http", http_server, http_path);
5593c8
+      if (http_url)
5593c8
+	{
5593c8
+	  grub_env_unset ("http_url");
5593c8
+	  grub_env_set ("http_url", http_url);
5593c8
+	  grub_free (http_url);
5593c8
+	}
5593c8
+    }
5593c8
 
5593c8
   grub_efi_http_t *http = dev->http;
5593c8
 
5593c8
@@ -352,32 +394,32 @@ grub_efihttp_open (struct grub_efi_net_device *dev,
5593c8
   grub_err_t err;
5593c8
   grub_off_t size;
5593c8
   char *buf;
5593c8
-  char *root_url;
5593c8
-  grub_efi_ipv6_address_t address;
5593c8
-  const char *rest;
5593c8
+  char *file_name = NULL;
5593c8
+  const char *http_path;
5593c8
 
5593c8
-  if (grub_efi_string_to_ip6_address (file->device->net->server, &address, &rest) && *rest == 0)
5593c8
-    root_url = grub_xasprintf ("%s://[%s]", type ? "https" : "http", file->device->net->server);
5593c8
-  else
5593c8
-    root_url = grub_xasprintf ("%s://%s", type ? "https" : "http", file->device->net->server);
5593c8
-  if (root_url)
5593c8
-    {
5593c8
-      grub_env_unset ("root_url");
5593c8
-      grub_env_set ("root_url", root_url);
5593c8
-      grub_free (root_url);
5593c8
-    }
5593c8
-  else
5593c8
-    {
5593c8
+  /* If path is relative, prepend http_path */
5593c8
+  http_path = grub_env_get ("http_path");
5593c8
+  if (http_path && file->device->net->name[0] != '/') {
5593c8
+    file_name = grub_xasprintf ("%s/%s", http_path, file->device->net->name);
5593c8
+    if (!file_name)
5593c8
       return grub_errno;
5593c8
-    }
5593c8
+  }
5593c8
 
5593c8
-  err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 1, 0);
5593c8
+  err = efihttp_request (dev->http, file->device->net->server,
5593c8
+			 file_name ? file_name : file->device->net->name, type, 1, 0);
5593c8
   if (err != GRUB_ERR_NONE)
5593c8
-    return err;
5593c8
+    {
5593c8
+      grub_free (file_name);
5593c8
+      return err;
5593c8
+    }
5593c8
 
5593c8
-  err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 0, &size);
5593c8
+  err = efihttp_request (dev->http, file->device->net->server,
5593c8
+			 file_name ? file_name : file->device->net->name, type, 0, &size);
5593c8
+  grub_free (file_name);
5593c8
   if (err != GRUB_ERR_NONE)
5593c8
-    return err;
5593c8
+    {
5593c8
+      return err;
5593c8
+    }
5593c8
 
5593c8
   buf = grub_malloc (size);
5593c8
   efihttp_read (dev, buf, size);