dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0199-Add-a-url-parser.patch

4fe85b
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4fe85b
From: Peter Jones <pjones@redhat.com>
4fe85b
Date: Tue, 14 Jun 2016 16:18:44 -0400
4fe85b
Subject: [PATCH] Add a url parser.
4fe85b
4fe85b
This patch adds a url parser that can parse http, https, tftp, and tftps
4fe85b
urls, and is easily extensible to handle more types.
4fe85b
4fe85b
It's a little ugly in terms of the arguments it takes.
4fe85b
4fe85b
Signed-off-by: Peter Jones <pjones@redhat.com>
4fe85b
---
4fe85b
 grub-core/Makefile.core.def |   1 +
4fe85b
 grub-core/kern/misc.c       |  13 +
4fe85b
 grub-core/net/url.c         | 856 ++++++++++++++++++++++++++++++++++++++++++++
4fe85b
 include/grub/misc.h         |  45 +++
4fe85b
 include/grub/net/url.h      |  28 ++
4fe85b
 5 files changed, 943 insertions(+)
4fe85b
 create mode 100644 grub-core/net/url.c
4fe85b
 create mode 100644 include/grub/net/url.h
4fe85b
4fe85b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
4fe85b
index 9378c732981..d73ea6f6c51 100644
4fe85b
--- a/grub-core/Makefile.core.def
4fe85b
+++ b/grub-core/Makefile.core.def
4fe85b
@@ -2103,6 +2103,7 @@ module = {
4fe85b
   common = net/ethernet.c;
4fe85b
   common = net/arp.c;
4fe85b
   common = net/netbuff.c;
4fe85b
+  common = net/url.c;
4fe85b
 };
4fe85b
 
4fe85b
 module = {
4fe85b
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
4fe85b
index a3e5056db3f..392c697db03 100644
4fe85b
--- a/grub-core/kern/misc.c
4fe85b
+++ b/grub-core/kern/misc.c
4fe85b
@@ -325,6 +325,19 @@ grub_strrchr (const char *s, int c)
4fe85b
   return p;
4fe85b
 }
4fe85b
 
4fe85b
+char *
4fe85b
+grub_strchrnul (const char *s, int c)
4fe85b
+{
4fe85b
+  do
4fe85b
+    {
4fe85b
+      if (*s == c)
4fe85b
+	break;
4fe85b
+    }
4fe85b
+  while (*s++);
4fe85b
+
4fe85b
+  return (char *) s;
4fe85b
+}
4fe85b
+
4fe85b
 int
4fe85b
 grub_strword (const char *haystack, const char *needle)
4fe85b
 {
4fe85b
diff --git a/grub-core/net/url.c b/grub-core/net/url.c
4fe85b
new file mode 100644
4fe85b
index 00000000000..537019f2c78
4fe85b
--- /dev/null
4fe85b
+++ b/grub-core/net/url.c
4fe85b
@@ -0,0 +1,856 @@
4fe85b
+/*
4fe85b
+ *  GRUB  --  GRand Unified Bootloader
4fe85b
+ *  Copyright (C) 2016  Free Software Foundation, Inc.
4fe85b
+ *
4fe85b
+ *  GRUB is free software: you can redistribute it and/or modify
4fe85b
+ *  it under the terms of the GNU General Public License as published by
4fe85b
+ *  the Free Software Foundation, either version 3 of the License, or
4fe85b
+ *  (at your option) any later version.
4fe85b
+ *
4fe85b
+ *  GRUB is distributed in the hope that it will be useful,
4fe85b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
4fe85b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4fe85b
+ *  GNU General Public License for more details.
4fe85b
+ *
4fe85b
+ *  You should have received a copy of the GNU General Public License
4fe85b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
4fe85b
+ */
4fe85b
+
4fe85b
+#ifdef URL_TEST
4fe85b
+
4fe85b
+#define _GNU_SOURCE 1
4fe85b
+
4fe85b
+#include <errno.h>
4fe85b
+#include <limits.h>
4fe85b
+#include <stdio.h>
4fe85b
+#include <stdlib.h>
4fe85b
+#include <string.h>
4fe85b
+#include <sys/types.h>
4fe85b
+#include <unistd.h>
4fe85b
+
4fe85b
+#define N_(x) x
4fe85b
+
4fe85b
+#define grub_malloc(x) malloc(x)
4fe85b
+#define grub_free(x) ({if (x) free(x);})
4fe85b
+#define grub_error(a, fmt, args...) printf(fmt "\n", ## args)
4fe85b
+#define grub_dprintf(a, fmt, args...) printf(a ": " fmt, ## args)
4fe85b
+#define grub_strlen(x) strlen(x)
4fe85b
+#define grub_strdup(x) strdup(x)
4fe85b
+#define grub_strstr(x,y) strstr(x,y)
4fe85b
+#define grub_memcpy(x,y,z) memcpy(x,y,z)
4fe85b
+#define grub_strcmp(x,y) strcmp(x,y)
4fe85b
+#define grub_strncmp(x,y,z) strncmp(x,y,z)
4fe85b
+#define grub_strcasecmp(x,y) strcasecmp(x,y)
4fe85b
+#define grub_strchrnul(x,y) strchrnul(x,y)
4fe85b
+#define grub_strchr(x,y) strchr(x,y)
4fe85b
+#define grub_strndup(x,y) strndup(x,y)
4fe85b
+#define grub_strtoul(x,y,z) strtoul(x,y,z)
4fe85b
+#define grub_memmove(x,y,z) memmove(x,y,z)
4fe85b
+#define grub_size_t size_t
4fe85b
+#define grub_errno errno
4fe85b
+
4fe85b
+#else
4fe85b
+#include <grub/types.h>
4fe85b
+#include <grub/mm.h>
4fe85b
+#include <grub/misc.h>
4fe85b
+#include <grub/net/url.h>
4fe85b
+#endif
4fe85b
+
4fe85b
+static char *
4fe85b
+translate_slashes(char *str)
4fe85b
+{
4fe85b
+  int i, j;
4fe85b
+  if (str == NULL)
4fe85b
+    return str;
4fe85b
+
4fe85b
+  for (i = 0, j = 0; str[i] != '\0'; i++, j++)
4fe85b
+    {
4fe85b
+      if (str[i] == '\\')
4fe85b
+	{
4fe85b
+	  str[j] = '/';
4fe85b
+	  if (str[i+1] == '\\')
4fe85b
+	    i++;
4fe85b
+	}
4fe85b
+    }
4fe85b
+
4fe85b
+  return str;
4fe85b
+}
4fe85b
+
4fe85b
+static inline int
4fe85b
+hex2int (char c)
4fe85b
+{
4fe85b
+  if (c >= '0' && c <= '9')
4fe85b
+    return c - '0';
4fe85b
+  c |= 0x20;
4fe85b
+  if (c >= 'a' && c <= 'f')
4fe85b
+    return c - 'a' + 10;
4fe85b
+  return -1;
4fe85b
+}
4fe85b
+
4fe85b
+static int
4fe85b
+url_unescape (char *buf, grub_size_t len)
4fe85b
+{
4fe85b
+  int c, rc;
4fe85b
+  unsigned int i;
4fe85b
+
4fe85b
+
4fe85b
+  if (len < 3)
4fe85b
+    {
4fe85b
+      for (i = 0; i < len; i++)
4fe85b
+	if (buf[i] == '%')
4fe85b
+	  return -1;
4fe85b
+      return 0;
4fe85b
+    }
4fe85b
+
4fe85b
+  for (i = 0; len > 2 && i < len - 2; i++)
4fe85b
+    {
4fe85b
+      if (buf[i] == '%')
4fe85b
+	{
4fe85b
+	  unsigned int j;
4fe85b
+	  for (j = i+1; j < i+3; j++)
4fe85b
+	    {
4fe85b
+	      if (!(buf[j] >= '0' && buf[j] <= '9') &&
4fe85b
+		  !(buf[j] >= 'a' && buf[j] <= 'f') &&
4fe85b
+		  !(buf[j] >= 'A' && buf[j] <= 'F'))
4fe85b
+		return -1;
4fe85b
+	    }
4fe85b
+	  i += 2;
4fe85b
+	}
4fe85b
+    }
4fe85b
+  if (i == len - 2)
4fe85b
+    {
4fe85b
+      if (buf[i+1] == '%' || buf[i+2] == '%')
4fe85b
+	return -1;
4fe85b
+    }
4fe85b
+  for (i = 0; i < len - 2; i++)
4fe85b
+    {
4fe85b
+      if (buf[i] == '%')
4fe85b
+	{
4fe85b
+	  rc = hex2int (buf[i+1]);
4fe85b
+	  if (rc < 0)
4fe85b
+	    return -1;
4fe85b
+	  c = (rc & 0xf) << 4;
4fe85b
+	  rc = hex2int (buf[i+2]);
4fe85b
+	  if (rc < 0)
4fe85b
+	    return -1;
4fe85b
+	  c |= (rc & 0xf);
4fe85b
+
4fe85b
+	  buf[i] = c;
4fe85b
+	  grub_memmove (buf+i+1, buf+i+3, len-(i+2));
4fe85b
+	  len -= 2;
4fe85b
+	}
4fe85b
+    }
4fe85b
+  return 0;
4fe85b
+}
4fe85b
+
4fe85b
+static int
4fe85b
+extract_http_url_info (char *url, int ssl,
4fe85b
+		       char **userinfo, char **host, int *port,
4fe85b
+		       char **file)
4fe85b
+{
4fe85b
+  char *colon, *slash, *query, *at = NULL, *separator, *auth_end;
4fe85b
+
4fe85b
+  char *userinfo_off = NULL;
4fe85b
+  char *userinfo_end;
4fe85b
+  char *host_off = NULL;
4fe85b
+  char *host_end;
4fe85b
+  char *port_off = NULL;
4fe85b
+  char *port_end;
4fe85b
+  char *file_off = NULL;
4fe85b
+
4fe85b
+  grub_size_t l;
4fe85b
+  int c;
4fe85b
+
4fe85b
+  if (!url || !userinfo || !host || !port || !file)
4fe85b
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument");
4fe85b
+
4fe85b
+  *userinfo = *host = *file = NULL;
4fe85b
+  *port = -1;
4fe85b
+
4fe85b
+  userinfo_off = url;
4fe85b
+
4fe85b
+  slash = grub_strchrnul (userinfo_off, '/');
4fe85b
+  query = grub_strchrnul (userinfo_off, '?');
4fe85b
+  auth_end = slash < query ? slash : query;
4fe85b
+  /* auth_end here is one /past/ the last character in the auth section, i.e.
4fe85b
+   * it's the : or / or NUL */
4fe85b
+
4fe85b
+  separator = grub_strchrnul (userinfo_off, '@');
4fe85b
+  if (separator > auth_end)
4fe85b
+    {
4fe85b
+      host_off = userinfo_off;
4fe85b
+      userinfo_off = NULL;
4fe85b
+      userinfo_end = NULL;
4fe85b
+    }
4fe85b
+  else
4fe85b
+    {
4fe85b
+      at = separator;
4fe85b
+      *separator = '\0';
4fe85b
+      userinfo_end = separator;
4fe85b
+      host_off = separator + 1;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (*host_off == '[')
4fe85b
+    {
4fe85b
+      separator = grub_strchrnul (host_off, ']');
4fe85b
+      if (separator >= auth_end)
4fe85b
+	goto fail;
4fe85b
+
4fe85b
+      separator += 1;
4fe85b
+      host_end = separator;
4fe85b
+    }
4fe85b
+  else
4fe85b
+    {
4fe85b
+      host_end = separator = colon = grub_strchrnul (host_off, ':');
4fe85b
+
4fe85b
+      if (colon > auth_end)
4fe85b
+	{
4fe85b
+	  separator = NULL;
4fe85b
+	  host_end = auth_end;
4fe85b
+	}
4fe85b
+    }
4fe85b
+
4fe85b
+  if (separator && separator < auth_end)
4fe85b
+    {
4fe85b
+      if (*separator == ':')
4fe85b
+	{
4fe85b
+	  port_off = separator + 1;
4fe85b
+	  port_end = auth_end;
4fe85b
+
4fe85b
+	  if (auth_end - port_end > 0)
4fe85b
+	    goto fail;
4fe85b
+	  if (port_end - port_off < 1)
4fe85b
+	    goto fail;
4fe85b
+	}
4fe85b
+      else
4fe85b
+	goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  file_off = auth_end;
4fe85b
+  if (port_off)
4fe85b
+    {
4fe85b
+      unsigned long portul;
4fe85b
+
4fe85b
+      separator = NULL;
4fe85b
+      c = *port_end;
4fe85b
+      *port_end = '\0';
4fe85b
+
4fe85b
+      portul = grub_strtoul (port_off, &separator, 10);
4fe85b
+      *port_end = c;
4fe85b
+#ifdef URL_TEST
4fe85b
+      if (portul == ULONG_MAX && errno == ERANGE)
4fe85b
+	goto fail;
4fe85b
+#else
4fe85b
+      if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
4fe85b
+	goto fail;
4fe85b
+#endif
4fe85b
+      if (portul & ~0xfffful)
4fe85b
+	goto fail;
4fe85b
+      if (separator != port_end)
4fe85b
+	goto fail;
4fe85b
+
4fe85b
+      *port = portul & 0xfffful;
4fe85b
+    }
4fe85b
+  else if (ssl)
4fe85b
+    *port = 443;
4fe85b
+  else
4fe85b
+    *port = 80;
4fe85b
+
4fe85b
+  if (userinfo_off && *userinfo_off)
4fe85b
+    {
4fe85b
+      l = userinfo_end - userinfo_off + 1;
4fe85b
+
4fe85b
+      *userinfo = grub_strndup (userinfo_off, l);
4fe85b
+      if (!*userinfo)
4fe85b
+	goto fail;
4fe85b
+      (*userinfo)[l-1]= '\0';
4fe85b
+    }
4fe85b
+
4fe85b
+  l = host_end - host_off;
4fe85b
+  c = *host_end;
4fe85b
+  *host_end = '\0';
4fe85b
+  *host = grub_strndup (host_off, l);
4fe85b
+  *host_end = c;
4fe85b
+  if (!*host)
4fe85b
+    goto fail;
4fe85b
+  (*host)[l] = '\0';
4fe85b
+
4fe85b
+  *file = grub_strdup (file_off);
4fe85b
+  if (!*file)
4fe85b
+    goto fail;
4fe85b
+
4fe85b
+  if (at)
4fe85b
+    *at = '@';
4fe85b
+  return 0;
4fe85b
+fail:
4fe85b
+  if (at)
4fe85b
+    *at = '@';
4fe85b
+  grub_free (*userinfo);
4fe85b
+  grub_free (*host);
4fe85b
+  grub_free (*file);
4fe85b
+
4fe85b
+  return -1;
4fe85b
+}
4fe85b
+
4fe85b
+static int
4fe85b
+extract_tftp_url_info (char *url, int ssl, char **host, char **file, int *port)
4fe85b
+{
4fe85b
+  char *slash, *semi;
4fe85b
+
4fe85b
+  char *host_off = url;
4fe85b
+  char *host_end;
4fe85b
+  char *file_off;
4fe85b
+
4fe85b
+  int c;
4fe85b
+
4fe85b
+  if (!url || !host || !file || !port)
4fe85b
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument");
4fe85b
+
4fe85b
+  if (ssl)
4fe85b
+    *port = 3713;
4fe85b
+  else
4fe85b
+    *port = 69;
4fe85b
+
4fe85b
+  slash = grub_strchr (url, '/');
4fe85b
+  if (!slash)
4fe85b
+    return -1;
4fe85b
+
4fe85b
+  host_end = file_off = slash;
4fe85b
+
4fe85b
+  semi = grub_strchrnul (slash, ';');
4fe85b
+  if (!grub_strncmp (semi, ";mode=", 6) && grub_strcmp (semi+6, "octet"))
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
4fe85b
+		  N_("TFTP mode `%s' is not implemented."), semi+6);
4fe85b
+      return -1;
4fe85b
+    }
4fe85b
+
4fe85b
+  /*
4fe85b
+   * Maybe somebody added a new method, I dunno.  Anyway, semi is a reserved
4fe85b
+   * character, so if it's there, it's the start of the mode block or it's
4fe85b
+   * invalid.  So set it to \0 unconditionally, not just for ;mode=octet
4fe85b
+   */
4fe85b
+  *semi = '\0';
4fe85b
+
4fe85b
+  c = *host_end;
4fe85b
+  *host_end = '\0';
4fe85b
+  *host = grub_strdup (host_off);
4fe85b
+  *host_end = c;
4fe85b
+
4fe85b
+  *file = grub_strdup (file_off);
4fe85b
+
4fe85b
+  if (!*file || !*host)
4fe85b
+    {
4fe85b
+      grub_free (*file);
4fe85b
+      grub_free (*host);
4fe85b
+      return -1;
4fe85b
+    }
4fe85b
+
4fe85b
+  return 0;
4fe85b
+}
4fe85b
+
4fe85b
+int
4fe85b
+extract_url_info (const char *urlbuf, grub_size_t buflen,
4fe85b
+		  char **scheme, char **userinfo,
4fe85b
+		  char **host, int *port, char **file)
4fe85b
+{
4fe85b
+  char *url;
4fe85b
+  char *colon;
4fe85b
+
4fe85b
+  char *scheme_off;
4fe85b
+  char *specific_off;
4fe85b
+
4fe85b
+  int rc;
4fe85b
+  int c;
4fe85b
+
4fe85b
+  int https;
4fe85b
+
4fe85b
+  if (!urlbuf || !buflen || !scheme || !userinfo || !host || !port || !file)
4fe85b
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument");
4fe85b
+
4fe85b
+  *scheme = *userinfo = *host = *file = NULL;
4fe85b
+  *port = -1;
4fe85b
+
4fe85b
+  /* make sure we have our own coherent grub_string. */
4fe85b
+  url = grub_malloc (buflen + 1);
4fe85b
+  if (!url)
4fe85b
+    return -1;
4fe85b
+
4fe85b
+  grub_memcpy (url, urlbuf, buflen);
4fe85b
+  url[buflen] = '\0';
4fe85b
+
4fe85b
+  grub_dprintf ("net", "dhcpv6 boot-file-url: `%s'\n", url);
4fe85b
+
4fe85b
+  /* get rid of any backslashes */
4fe85b
+  url = translate_slashes (url);
4fe85b
+
4fe85b
+  /* find the constituent parts */
4fe85b
+  colon = grub_strstr (url, "://");
4fe85b
+  if (!colon)
4fe85b
+    goto fail;
4fe85b
+
4fe85b
+  scheme_off = url;
4fe85b
+  c = *colon;
4fe85b
+  *colon = '\0';
4fe85b
+  specific_off = colon + 3;
4fe85b
+
4fe85b
+  https = !grub_strcasecmp (scheme_off, "https");
4fe85b
+
4fe85b
+  rc = 0;
4fe85b
+  if (!grub_strcasecmp (scheme_off, "tftp"))
4fe85b
+    {
4fe85b
+      rc = extract_tftp_url_info (specific_off, 0, host, file, port);
4fe85b
+    }
4fe85b
+#ifdef URL_TEST
4fe85b
+  else if (!grub_strcasecmp (scheme_off, "http") || https)
4fe85b
+#else
4fe85b
+  else if (!grub_strcasecmp (scheme_off, "http"))
4fe85b
+#endif
4fe85b
+    {
4fe85b
+      rc = extract_http_url_info (specific_off,
4fe85b
+				  https, userinfo, host, port, file);
4fe85b
+    }
4fe85b
+#ifdef URL_TEST
4fe85b
+  else if (!grub_strcasecmp (scheme_off, "iscsi"))
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
4fe85b
+		  N_("Unimplemented URL scheme `%s'"), scheme_off);
4fe85b
+      *colon = c;
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+  else if (!grub_strcasecmp (scheme_off, "tftps"))
4fe85b
+    {
4fe85b
+      rc = extract_tftp_url_info (specific_off, 1, host, file, port);
4fe85b
+    }
4fe85b
+#endif
4fe85b
+  else
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
4fe85b
+		  N_("Unimplemented URL scheme `%s'"), scheme_off);
4fe85b
+      *colon = c;
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (rc < 0)
4fe85b
+    {
4fe85b
+      *colon = c;
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  *scheme = grub_strdup (scheme_off);
4fe85b
+  *colon = c;
4fe85b
+  if (!*scheme)
4fe85b
+    goto fail;
4fe85b
+
4fe85b
+  if (*userinfo)
4fe85b
+    {
4fe85b
+      rc = url_unescape (*userinfo, grub_strlen (*userinfo));
4fe85b
+      if (rc < 0)
4fe85b
+	goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (*host)
4fe85b
+    {
4fe85b
+      rc = url_unescape (*host, grub_strlen (*host));
4fe85b
+      if (rc < 0)
4fe85b
+	goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (*file)
4fe85b
+    {
4fe85b
+      rc = url_unescape (*file, grub_strlen (*file));
4fe85b
+      if (rc < 0)
4fe85b
+	goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  grub_free (url);
4fe85b
+  return 0;
4fe85b
+fail:
4fe85b
+  grub_free (*scheme);
4fe85b
+  grub_free (*userinfo);
4fe85b
+  grub_free (*host);
4fe85b
+  grub_free (*file);
4fe85b
+
4fe85b
+  if (!grub_errno)
4fe85b
+    grub_error (GRUB_ERR_NET_BAD_ADDRESS, N_("Invalid boot-file-url `%s'"),
4fe85b
+		url);
4fe85b
+  grub_free (url);
4fe85b
+  return -1;
4fe85b
+}
4fe85b
+
4fe85b
+#ifdef URL_TEST
4fe85b
+
4fe85b
+struct test {
4fe85b
+    char *url;
4fe85b
+    int rc;
4fe85b
+    char *scheme;
4fe85b
+    char *userinfo;
4fe85b
+    char *host;
4fe85b
+    int port;
4fe85b
+    char *file;
4fe85b
+} tests[] = {
4fe85b
+  {.url = "http://foo.example.com/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://foo.example.com/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://[foo.example.com/",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "http://[foo.example.com/?foobar",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "http://foo.example.com:/",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "http://foo.example.com:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://foo.example.com:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://[1234::1]/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://[1234::1]/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://[1234::1]:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://[1234::1]:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@foo.example.com/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@foo.example.com/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@[foo.example.com/",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "http://foo@[foo.example.com/?foobar",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "http://foo@foo.example.com:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@foo.example.com:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@[1234::1]/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@[1234::1]/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 80,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@[1234::1]:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "http://foo@[1234::1]:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "http",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://foo.example.com/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://foo.example.com/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://[foo.example.com/",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "https://[foo.example.com/?foobar",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "https://foo.example.com:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://foo.example.com:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://[1234::1]/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://[1234::1]/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://[1234::1]:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://[1234::1]:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@foo.example.com/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@foo.example.com/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@[foo.example.com/",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "https://f%6fo@[foo.example.com/?fooba%72",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "https://foo@foo.example.com:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@foo.example.com:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@[1234::1]/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@[1234::1]/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 443,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "https://f%6fo@[12%334::1]:81/",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/",
4fe85b
+  },
4fe85b
+  {.url = "https://foo@[1234::1]:81/?foobar",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "https",
4fe85b
+   .userinfo = "foo",
4fe85b
+   .host = "[1234::1]",
4fe85b
+   .port = 81,
4fe85b
+   .file = "/?foobar",
4fe85b
+  },
4fe85b
+  {.url = "tftp://foo.e%78ample.com/foo/bar/b%61%7a",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "tftp",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 69,
4fe85b
+   .file = "/foo/bar/baz",
4fe85b
+  },
4fe85b
+  {.url = "tftp://foo.example.com/foo/bar/baz",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "tftp",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 69,
4fe85b
+   .file = "/foo/bar/baz",
4fe85b
+  },
4fe85b
+  {.url = "tftps://foo.example.com/foo/bar/baz",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "tftps",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 3713,
4fe85b
+   .file = "/foo/bar/baz",
4fe85b
+  },
4fe85b
+  {.url = "tftps://foo.example.com/foo/bar/baz;mode=netascii",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "tftps://foo.example.com/foo/bar/baz;mode=octet",
4fe85b
+   .rc = 0,
4fe85b
+   .scheme = "tftps",
4fe85b
+   .host = "foo.example.com",
4fe85b
+   .port = 3713,
4fe85b
+   .file = "/foo/bar/baz",
4fe85b
+  },
4fe85b
+  {.url = "tftps://foo.example.com/foo/bar/baz;mode=invalid",
4fe85b
+   .rc = -1,
4fe85b
+  },
4fe85b
+  {.url = "",
4fe85b
+  },
4fe85b
+};
4fe85b
+
4fe85b
+static int
4fe85b
+string_test (char *name, char *a, char *b)
4fe85b
+{
4fe85b
+  if ((a && !b) || (!a && b))
4fe85b
+    {
4fe85b
+      printf("expected %s \"%s\", got \"%s\"\n", name, a, b);
4fe85b
+      return -1;
4fe85b
+    }
4fe85b
+  if (a && b && strcmp(a, b))
4fe85b
+    {
4fe85b
+      printf("expected %s \"%s\", got \"%s\"\n", name, a, b);
4fe85b
+      return -1;
4fe85b
+    }
4fe85b
+  return 0;
4fe85b
+}
4fe85b
+
4fe85b
+int
4fe85b
+main(void)
4fe85b
+{
4fe85b
+	unsigned int i;
4fe85b
+	int rc;
4fe85b
+
4fe85b
+	for (i = 0; tests[i].url[0] != '\0'; i++)
4fe85b
+	{
4fe85b
+		char *scheme, *userinfo, *host, *file;
4fe85b
+		int port;
4fe85b
+
4fe85b
+		printf("======= url: \"%s\"\n", tests[i].url);
4fe85b
+		rc = extract_url_info(tests[i].url, strlen(tests[i].url) + 1,
4fe85b
+				      &scheme, &userinfo, &host, &port, &file;;
4fe85b
+		if (tests[i].rc != rc)
4fe85b
+		  {
4fe85b
+		    printf("  extract_url_info(...) = %d\n", rc);
4fe85b
+		    exit(1);
4fe85b
+		  }
4fe85b
+		else if (rc >= 0)
4fe85b
+		  {
4fe85b
+		    if (string_test("scheme", tests[i].scheme, scheme) < 0)
4fe85b
+		      exit(1);
4fe85b
+		    if (string_test("userinfo", tests[i].userinfo, userinfo) < 0)
4fe85b
+		      exit(1);
4fe85b
+		    if (string_test("host", tests[i].host, host) < 0)
4fe85b
+		      exit(1);
4fe85b
+		    if (port != tests[i].port)
4fe85b
+		      {
4fe85b
+			printf("  bad port \"%d\" should have been \"%d\"\n",
4fe85b
+			       port, tests[i].port);
4fe85b
+			exit(1);
4fe85b
+		      }
4fe85b
+		    if (string_test("file", tests[i].file, file) < 0)
4fe85b
+		      exit(1);
4fe85b
+		  }
4fe85b
+		free(scheme);
4fe85b
+		free(userinfo);
4fe85b
+		free(host);
4fe85b
+		free(file);
4fe85b
+	}
4fe85b
+	printf("everything worked?!?\n");
4fe85b
+}
4fe85b
+#endif
4fe85b
diff --git a/include/grub/misc.h b/include/grub/misc.h
4fe85b
index c6cd4564d86..34250291908 100644
4fe85b
--- a/include/grub/misc.h
4fe85b
+++ b/include/grub/misc.h
4fe85b
@@ -104,6 +104,7 @@ int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
4fe85b
 
4fe85b
 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
4fe85b
 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
4fe85b
+char *EXPORT_FUNC(grub_strchrnul) (const char *s, int c);
4fe85b
 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
4fe85b
 
4fe85b
 /* Copied from gnulib.
4fe85b
@@ -226,6 +227,50 @@ grub_toupper (int c)
4fe85b
   return c;
4fe85b
 }
4fe85b
 
4fe85b
+static inline char *
4fe85b
+grub_strcasestr (const char *haystack, const char *needle)
4fe85b
+{
4fe85b
+  /* Be careful not to look at the entire extent of haystack or needle
4fe85b
+     until needed.  This is useful because of these two cases:
4fe85b
+       - haystack may be very long, and a match of needle found early,
4fe85b
+       - needle may be very long, and not even a short initial segment of
4fe85b
+       needle may be found in haystack.  */
4fe85b
+  if (*needle != '\0')
4fe85b
+    {
4fe85b
+      /* Speed up the following searches of needle by caching its first
4fe85b
+	 character.  */
4fe85b
+      char b = *needle++;
4fe85b
+
4fe85b
+      for (;; haystack++)
4fe85b
+	{
4fe85b
+	  if (*haystack == '\0')
4fe85b
+	    /* No match.  */
4fe85b
+	    return 0;
4fe85b
+	  if (grub_tolower(*haystack) == grub_tolower(b))
4fe85b
+	    /* The first character matches.  */
4fe85b
+	    {
4fe85b
+	      const char *rhaystack = haystack + 1;
4fe85b
+	      const char *rneedle = needle;
4fe85b
+
4fe85b
+	      for (;; rhaystack++, rneedle++)
4fe85b
+		{
4fe85b
+		  if (*rneedle == '\0')
4fe85b
+		    /* Found a match.  */
4fe85b
+		    return (char *) haystack;
4fe85b
+		  if (*rhaystack == '\0')
4fe85b
+		    /* No match.  */
4fe85b
+		    return 0;
4fe85b
+		  if (grub_tolower(*rhaystack) != grub_tolower(*rneedle))
4fe85b
+		    /* Nothing in this round.  */
4fe85b
+		    break;
4fe85b
+		}
4fe85b
+	    }
4fe85b
+	}
4fe85b
+    }
4fe85b
+  else
4fe85b
+    return (char *) haystack;
4fe85b
+}
4fe85b
+
4fe85b
 static inline int
4fe85b
 grub_strcasecmp (const char *s1, const char *s2)
4fe85b
 {
4fe85b
diff --git a/include/grub/net/url.h b/include/grub/net/url.h
4fe85b
new file mode 100644
4fe85b
index 00000000000..a215fa27d0a
4fe85b
--- /dev/null
4fe85b
+++ b/include/grub/net/url.h
4fe85b
@@ -0,0 +1,28 @@
4fe85b
+/* url.h - prototypes for url parsing functions */
4fe85b
+/*
4fe85b
+ *  GRUB  --  GRand Unified Bootloader
4fe85b
+ *  Copyright (C) 2016  Free Software Foundation, Inc.
4fe85b
+ *
4fe85b
+ *  GRUB is free software: you can redistribute it and/or modify
4fe85b
+ *  it under the terms of the GNU General Public License as published by
4fe85b
+ *  the Free Software Foundation, either version 3 of the License, or
4fe85b
+ *  (at your option) any later version.
4fe85b
+ *
4fe85b
+ *  GRUB is distributed in the hope that it will be useful,
4fe85b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
4fe85b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4fe85b
+ *  GNU General Public License for more details.
4fe85b
+ *
4fe85b
+ *  You should have received a copy of the GNU General Public License
4fe85b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
4fe85b
+ */
4fe85b
+
4fe85b
+#ifndef GRUB_URL_HEADER
4fe85b
+#define GRUB_URL_HEADER	1
4fe85b
+
4fe85b
+int
4fe85b
+EXPORT_FUNC(extract_url_info) (const char *urlbuf, grub_size_t buflen,
4fe85b
+			       char **scheme, char **userinfo,
4fe85b
+			       char **host, int *port, char **file);
4fe85b
+
4fe85b
+#endif /* GRUB_URL_HEADER */