dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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