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