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