From: Toni Uhlig <matzeton@googlemail.com>
Date: Thu, 18 Jan 2018 19:46:58 +0100
Subject: Add support for used defined base URI

	* fixed two possible memory leaks in htc
	* fixed compiler warning in hts for uid

Applied-Upstream: commit:f440dcb3c341d22428898952c343ad9fa6e9e7f5
---
 common.h |  1 +
 htc.c    | 21 +++++++++++++++++++--
 hts.c    |  2 +-
 http.c   |  2 +-
 http.h   |  1 +
 tunnel.c | 28 ++++++++++++++++++++++++++++
 tunnel.h |  6 ++++++
 7 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/common.h b/common.h
index 745bcfd..5c2d376 100644
--- a/common.h
+++ b/common.h
@@ -27,6 +27,7 @@ Copyright (C) 1999 Lars Brinkhoff.  See COPYING for terms and conditions.
 #define DEFAULT_CONTENT_LENGTH (100 * 1024) /* bytes */
 #define DEFAULT_KEEP_ALIVE 5 /* seconds */
 #define DEFAULT_MAX_CONNECTION_AGE 300 /* seconds */
+#define DEFAULT_BASE_URI "/index.html?crap="
 #define BUG_REPORT_EMAIL "bug-httptunnel@gnu.org"
 
 #ifndef TRUE
diff --git a/htc.c b/htc.c
index 2fdd301..97d9b04 100644
--- a/htc.c
+++ b/htc.c
@@ -41,6 +41,7 @@ typedef struct
   int max_connection_age;
   char *proxy_authorization;
   char *user_agent;
+  const char *base_uri;
 } Arguments;
 
 #define NO_PROXY_BUFFER 0
@@ -84,13 +85,15 @@ usage (FILE *f, const char *me)
 "  -T, --timeout TIME             timeout, in milliseconds, before sending\n"
 "                                 padding to a buffering proxy\n"
 "  -U, --user-agent STRING        specify User-Agent value in HTTP requests\n"
+"  -R, --base-uri STRING          specify a URI value for all HTTP requests\n"
+"                                 (default is \"%s\")\n"
 "  -V, --version                  output version information and exit\n"
 "  -w, --no-daemon                don't fork into the background\n"
 "\n"
 "Report bugs to %s.\n",
 	   me, DEFAULT_HOST_PORT, DEFAULT_KEEP_ALIVE,
 	   DEFAULT_MAX_CONNECTION_AGE, DEFAULT_PROXY_PORT,
-	   BUG_REPORT_EMAIL);
+	   DEFAULT_BASE_URI, BUG_REPORT_EMAIL);
 }
 
 static int
@@ -132,6 +135,7 @@ parse_arguments (int argc, char **argv, Arguments *arg)
   arg->max_connection_age = DEFAULT_CONNECTION_MAX_TIME;
   arg->proxy_authorization = NULL;
   arg->user_agent = NULL;
+  arg->base_uri = DEFAULT_BASE_URI;
 
   for (;;)
     {
@@ -151,6 +155,7 @@ parse_arguments (int argc, char **argv, Arguments *arg)
 	{ "timeout", required_argument, 0, 'T' },
 	{ "keep-alive", required_argument, 0, 'k' },
 	{ "user-agent", required_argument, 0, 'U' },
+	{ "base-uri", required_argument, 0, 'R' },
 	{ "forward-port", required_argument, 0, 'F' },
 	{ "content-length", required_argument, 0, 'c' },
 	{ "strict-content-length", no_argument, 0, 'S' },
@@ -161,7 +166,7 @@ parse_arguments (int argc, char **argv, Arguments *arg)
 	{ 0, 0, 0, 0 }
       };
 
-      static const char *short_options = "A:B:c:d:F:hk:M:P:sST:U:Vwz:"
+      static const char *short_options = "A:B:c:d:F:hk:M:P:sST:U:R:Vwz:"
 #ifdef DEBUG_MODE
 	"D:l:"
 #endif
@@ -257,6 +262,10 @@ parse_arguments (int argc, char **argv, Arguments *arg)
 	  arg->user_agent = optarg;
 	  break;
 
+	case 'R':
+	  arg->base_uri = optarg;
+	  break;
+
 	case 'V':
 	  printf ("htc (%s) %s\n", PACKAGE, VERSION);
 	  exit (0);
@@ -435,6 +444,7 @@ main (int argc, char **argv)
   log_notice ("  proxy_authorization = %s",
 	      arg.proxy_authorization ? arg.proxy_authorization : "(null)");
   log_notice ("  user_agent = %s", arg.user_agent ? arg.user_agent : "(null)");
+  log_notice ("  base_uri = %s", arg.base_uri ? arg.base_uri : "(null)");
   log_notice ("  debug_level = %d", debug_level);
 
 
@@ -582,6 +592,13 @@ main (int argc, char **argv)
 		       strerror (errno));
 	}
 
+	  if (arg.base_uri != NULL)
+	{
+	  if (tunnel_setopt (tunnel, "base_uri", (void *)arg.base_uri) == -1)
+	    log_error ("tunnel_setopt base_uri error: %s",
+		       strerror (errno));
+	}
+
       if (tunnel_connect (tunnel) == -1)
 	{
 	  log_error ("couldn't open tunnel: %s", strerror (errno));
diff --git a/hts.c b/hts.c
index 1d7ef3a..b0ae68b 100644
--- a/hts.c
+++ b/hts.c
@@ -298,7 +298,7 @@ main (int argc, char **argv)
   Arguments arg;
   Tunnel *tunnel;
   FILE *pid_file;
-  uid_t uid;
+  uid_t uid = 0;
   gid_t gid;
 
   parse_arguments (argc, argv, &arg);
diff --git a/http.c b/http.c
index ec153b7..ef2833e 100644
--- a/http.c
+++ b/http.c
@@ -31,7 +31,7 @@ http_method (int fd, Http_destination *dest,
   n = 0;
   if (dest->proxy_name != NULL)
     n = sprintf (str, "http://%s:%d", dest->host_name, dest->host_port);
-  sprintf (str + n, "/index.html?crap=%ld", time (NULL));
+  sprintf (str + n, "%s%ld", dest->base_uri, time (NULL));
 
   request = http_create_request (method, str, 1, 1);
   if (request == NULL)
diff --git a/http.h b/http.h
index bb7188a..a4053f0 100644
--- a/http.h
+++ b/http.h
@@ -53,6 +53,7 @@ typedef struct
   int proxy_port;
   const char *proxy_authorization;
   const char *user_agent;
+  const char *base_uri;
 } Http_destination;
 
 extern ssize_t http_get (int fd, Http_destination *dest);
diff --git a/tunnel.c b/tunnel.c
index 370bfc0..304ba59 100644
--- a/tunnel.c
+++ b/tunnel.c
@@ -1322,6 +1322,7 @@ tunnel_new_client (const char *host, int host_port,
   tunnel->dest.proxy_port = proxy_port;
   tunnel->dest.proxy_authorization = NULL;
   tunnel->dest.user_agent = NULL;
+  tunnel->dest.base_uri = NULL;
   /* -1 to allow for TUNNEL_DISCONNECT */
   tunnel->content_length = content_length - 1;
   tunnel->buf_ptr = tunnel->buf;
@@ -1363,6 +1364,15 @@ tunnel_destroy (Tunnel *tunnel)
   if (tunnel->server_socket != -1)
     close (tunnel->server_socket);
 
+  if (tunnel->dest.proxy_authorization)
+    free ((char *)tunnel->dest.proxy_authorization);
+
+  if (tunnel->dest.user_agent)
+    free ((char *)tunnel->dest.user_agent);
+
+  if (tunnel->dest.base_uri)
+    free ((char *)tunnel->dest.base_uri);
+
   free (tunnel);
 }
 
@@ -1426,6 +1436,24 @@ tunnel_opt (Tunnel *tunnel, const char *opt, void *data, int get_flag)
 	    return -1;
 	}
     }
+  else if (strcmp (opt, "base_uri") == 0)
+    {
+      if (get_flag)
+	{
+	  if (tunnel->dest.base_uri == NULL)
+	    *(char **)data = NULL;
+	  else
+	    *(char **)data = strdup (tunnel->dest.base_uri);
+	}
+	  else
+	{
+	  if (tunnel->dest.base_uri != NULL)
+	    free ((char *)tunnel->dest.base_uri);
+	  tunnel->dest.base_uri = strdup ((char *)data);
+	  if (tunnel->dest.base_uri == NULL)
+	    return -1;
+	}
+    }
   else
     {
       errno = EINVAL;
diff --git a/tunnel.h b/tunnel.h
index 8e770f8..f9e7c4e 100644
--- a/tunnel.h
+++ b/tunnel.h
@@ -91,6 +91,12 @@ int tunnel_getopt (Tunnel *tunnel, const char *opt, void *data);
     copied into a newly malloced memory region which the caller must
     accept responsibility to manage.
 
+  * base_uri
+
+    DATA must be a pointer to a char pointer.  The char pointer
+    specifies the base URI for every tunnel requests/responses.  When
+    this option is not set, the default value is used.
+
 int tunnel_close (Tunnel *tunnel);
 
   Close the tunnel.
