Description: Provide an RFC1035 compliant version of the hostname
 mod_proxy: Provide an RFC1035 compliant version of the hostname in the
 proxy_worker_shared structure. PR62085
 .
 Tone down the message that worker hostname is too long noting it only
 affects legacy modules not yet using hostname_ex.
 .
 Set the notice when hostname is too long for legacy proxy modules to info level.
 .
 Submitted by: minfrin
 Reviewed by: minfrin, jim, ylavic
 .
 Modified in Ubuntu to not change MODULE_MAGIC_NUMBER_MINOR in include/ap_mmn.h
Origin: upstream, http://svn.apache.org/viewvc?view=revision&revision=1824504
Bug: https://bz.apache.org/bugzilla/show_bug.cgi?id=62085
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/apache2/+bug/1750356
Last-Update: 2018-06-27
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- 2.4.x/include/ap_mmn.h	2018/02/16 14:58:32	1824503
+++ 2.4.x/include/ap_mmn.h	2018/02/16 15:04:41	1824504
@@ -496,6 +496,7 @@
  *                          to ap_[r]getline()
  * 20120211.68 (2.4.26-dev) Add ap_get_basic_auth_components() and deprecate
  *                          ap_get_basic_auth_pw()
+ * 20120211.75 (2.4.30-dev) Add hostname_ex to proxy_worker_shared
  */
 
 #define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */
--- 2.4.x/modules/proxy/balancers/mod_lbmethod_heartbeat.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/balancers/mod_lbmethod_heartbeat.c	2018/02/16 15:04:41	1824504
@@ -300,7 +300,7 @@
 
     for (i = 0; i < balancer->workers->nelts; i++) {
         worker = &APR_ARRAY_IDX(balancer->workers, i, proxy_worker *);
-        server = apr_hash_get(servers, (*worker)->s->hostname, APR_HASH_KEY_STRING);
+        server = apr_hash_get(servers, (*worker)->s->hostname_ex, APR_HASH_KEY_STRING);
 
         if (!server) {
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(01214)
--- 2.4.x/modules/proxy/mod_proxy.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/mod_proxy.c	2018/02/16 15:04:41	1824504
@@ -2838,7 +2838,7 @@
             char fbuf[50];
             if (!(flags & AP_STATUS_SHORT)) {
                 ap_rvputs(r, "<tr>\n<td>", (*worker)->s->scheme, "</td>", NULL);
-                ap_rvputs(r, "<td>", (*worker)->s->hostname, "</td><td>", NULL);
+                ap_rvputs(r, "<td>", (*worker)->s->hostname_ex, "</td><td>", NULL);
                 ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, *worker), NULL);
                 ap_rvputs(r, "</td><td>", (*worker)->s->route, NULL);
                 ap_rvputs(r, "</td><td>", (*worker)->s->redirect, NULL);
@@ -2930,7 +2930,8 @@
             ap_proxy_define_worker(p, &forward, NULL, NULL, "http://www.apache.org", 0);
             conf->forward = forward;
             PROXY_STRNCPY(conf->forward->s->name,     "proxy:forward");
-            PROXY_STRNCPY(conf->forward->s->hostname, "*");
+            PROXY_STRNCPY(conf->forward->s->hostname, "*"); /* for compatibility */
+            PROXY_STRNCPY(conf->forward->s->hostname_ex, "*");
             PROXY_STRNCPY(conf->forward->s->scheme,   "*");
             conf->forward->hash.def = conf->forward->s->hash.def =
                 ap_proxy_hashfunc(conf->forward->s->name, PROXY_HASHFUNC_DEFAULT);
@@ -2947,7 +2948,8 @@
         if (!reverse) {
             ap_proxy_define_worker(p, &reverse, NULL, NULL, "http://www.apache.org", 0);
             PROXY_STRNCPY(reverse->s->name,     "proxy:reverse");
-            PROXY_STRNCPY(reverse->s->hostname, "*");
+            PROXY_STRNCPY(reverse->s->hostname, "*"); /* for compatibility */
+            PROXY_STRNCPY(reverse->s->hostname_ex, "*");
             PROXY_STRNCPY(reverse->s->scheme,   "*");
             reverse->hash.def = reverse->s->hash.def =
                 ap_proxy_hashfunc(reverse->s->name, PROXY_HASHFUNC_DEFAULT);
--- 2.4.x/modules/proxy/mod_proxy.h	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/mod_proxy.h	2018/02/16 15:04:41	1824504
@@ -354,6 +354,8 @@
 #define PROXY_BALANCER_MAX_HOSTNAME_SIZE PROXY_WORKER_MAX_HOSTNAME_SIZE
 #define PROXY_BALANCER_MAX_STICKY_SIZE  64
 
+#define PROXY_RFC1035_HOSTNAME_SIZE	256
+
 /* RFC-1035 mentions limits of 255 for host-names and 253 for domain-names,
  * dotted together(?) this would fit the below size (+ trailing NUL).
  */
@@ -384,7 +386,7 @@
 typedef struct {
     char      name[PROXY_WORKER_MAX_NAME_SIZE];
     char      scheme[PROXY_WORKER_MAX_SCHEME_SIZE];   /* scheme to use ajp|http|https */
-    char      hostname[PROXY_WORKER_MAX_HOSTNAME_SIZE];  /* remote backend address */
+    char      hostname[PROXY_WORKER_MAX_HOSTNAME_SIZE];  /* remote backend address (deprecated, use hostname_ex below) */
     char      route[PROXY_WORKER_MAX_ROUTE_SIZE];     /* balancing route */
     char      redirect[PROXY_WORKER_MAX_ROUTE_SIZE];  /* temporary balancing redirection route */
     char      flusher[PROXY_WORKER_MAX_SCHEME_SIZE];  /* flush provider used by mod_proxy_fdpass */
@@ -444,6 +446,7 @@
     hcmethod_t      method;     /* method to use for health check */
     apr_interval_time_t interval;
     char      upgrade[PROXY_WORKER_MAX_SCHEME_SIZE];/* upgrade protocol used by mod_proxy_wstunnel */
+    char      hostname_ex[PROXY_RFC1035_HOSTNAME_SIZE];  /* RFC1035 compliant version of the remote backend address */
 } proxy_worker_shared;
 
 #define ALIGNED_PROXY_WORKER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_worker_shared)))
--- 2.4.x/modules/proxy/mod_proxy_ajp.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/mod_proxy_ajp.c	2018/02/16 15:04:41	1824504
@@ -213,7 +213,7 @@
         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00868)
                       "request failed to %pI (%s)",
                       conn->worker->cp->addr,
-                      conn->worker->s->hostname);
+                      conn->worker->s->hostname_ex);
         if (status == AJP_EOVERFLOW)
             return HTTP_BAD_REQUEST;
         else if  (status == AJP_EBAD_METHOD) {
@@ -297,7 +297,7 @@
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00876)
                               "send failed to %pI (%s)",
                               conn->worker->cp->addr,
-                              conn->worker->s->hostname);
+                              conn->worker->s->hostname_ex);
                 /*
                  * It is fatal when we failed to send a (part) of the request
                  * body.
@@ -337,7 +337,7 @@
         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00878)
                       "read response failed from %pI (%s)",
                       conn->worker->cp->addr,
-                      conn->worker->s->hostname);
+                      conn->worker->s->hostname_ex);
 
         /* If we had a successful cping/cpong and then a timeout
          * we assume it is a request that cause a back-end timeout,
@@ -635,7 +635,7 @@
         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00892)
                       "got response from %pI (%s)",
                       conn->worker->cp->addr,
-                      conn->worker->s->hostname);
+                      conn->worker->s->hostname_ex);
 
         if (conf->error_override && ap_is_HTTP_ERROR(r->status)) {
             /* clear r->status for override error, otherwise ErrorDocument
@@ -659,7 +659,7 @@
         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00893)
                       "dialog to %pI (%s) failed",
                       conn->worker->cp->addr,
-                      conn->worker->s->hostname);
+                      conn->worker->s->hostname_ex);
         /*
          * If we already send data, signal a broken backend connection
          * upwards in the chain.
@@ -794,7 +794,7 @@
                 backend->close = 1;
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00897)
                               "cping/cpong failed to %pI (%s)",
-                              worker->cp->addr, worker->s->hostname);
+                              worker->cp->addr, worker->s->hostname_ex);
                 status = HTTP_SERVICE_UNAVAILABLE;
                 retry++;
                 continue;
--- 2.4.x/modules/proxy/mod_proxy_balancer.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/mod_proxy_balancer.c	2018/02/16 15:04:41	1824504
@@ -452,7 +452,7 @@
             (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01165)
                          "%s: Forcing recovery for worker (%s)",
-                         balancer->s->name, (*worker)->s->hostname);
+                         balancer->s->name, (*worker)->s->hostname_ex);
         }
     }
 }
@@ -1469,7 +1469,7 @@
                           "</httpd:name>\n", NULL);
                 ap_rvputs(r, "          <httpd:scheme>", worker->s->scheme,
                           "</httpd:scheme>\n", NULL);
-                ap_rvputs(r, "          <httpd:hostname>", worker->s->hostname,
+                ap_rvputs(r, "          <httpd:hostname>", worker->s->hostname_ex,
                           "</httpd:hostname>\n", NULL);
                 ap_rprintf(r, "          <httpd:loadfactor>%.2f</httpd:loadfactor>\n",
                           (float)(worker->s->lbfactor)/100.0);
--- 2.4.x/modules/proxy/mod_proxy_hcheck.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/mod_proxy_hcheck.c	2018/02/16 15:04:41	1824504
@@ -416,7 +416,7 @@
                                "OPTIONS * HTTP/1.0\r\n"
                                "Host: %s:%d\r\n"
                                "\r\n",
-                               hc->s->hostname, (int)hc->s->port);
+                               hc->s->hostname_ex, (int)hc->s->port);
             break;
 
         case HEAD:
@@ -434,7 +434,7 @@
                                (wctx->path ? wctx->path : ""),
                                (wctx->path && *hc->s->hcuri ? "/" : "" ),
                                (*hc->s->hcuri ? hc->s->hcuri : ""),
-                               hc->s->hostname, (int)hc->s->port);
+                               hc->s->hostname_ex, (int)hc->s->port);
             break;
 
         default:
@@ -461,12 +461,13 @@
                                 : ap_proxy_port_of_scheme(worker->s->scheme));
         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ctx->s, APLOGNO(03248)
                      "Creating hc worker %pp for %s://%s:%d",
-                     worker, worker->s->scheme, worker->s->hostname,
+                     worker, worker->s->scheme, worker->s->hostname_ex,
                      (int)port);
 
         ap_proxy_define_worker(ctx->p, &hc, NULL, NULL, worker->s->name, 0);
         apr_snprintf(hc->s->name, sizeof hc->s->name, "%pp", worker);
-        PROXY_STRNCPY(hc->s->hostname, worker->s->hostname);
+        PROXY_STRNCPY(hc->s->hostname, worker->s->hostname); /* for compatibility */
+        PROXY_STRNCPY(hc->s->hostname_ex, worker->s->hostname_ex);
         PROXY_STRNCPY(hc->s->scheme,   worker->s->scheme);
         PROXY_STRNCPY(hc->s->hcuri,    worker->s->hcuri);
         PROXY_STRNCPY(hc->s->hcexpr,   worker->s->hcexpr);
@@ -498,7 +499,7 @@
                                 : ap_proxy_port_of_scheme(worker->s->scheme));
         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ctx->s, APLOGNO(03311)
                      "Updating hc worker %pp for %s://%s:%d",
-                     worker, worker->s->scheme, worker->s->hostname,
+                     worker, worker->s->scheme, worker->s->hostname_ex,
                      (int)port);
         hc->s->method = worker->s->method;
         create_hcheck_req(wctx, hc, ctx->p);
@@ -519,12 +520,12 @@
         *addr = worker->cp->addr;
     }
     else {
-        rv = apr_sockaddr_info_get(addr, worker->s->hostname,
+        rv = apr_sockaddr_info_get(addr, worker->s->hostname_ex,
                                    APR_UNSPEC, worker->s->port, 0, p);
         if (rv != APR_SUCCESS) {
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ctx->s, APLOGNO(03249)
                          "DNS lookup failure for: %s:%d",
-                         worker->s->hostname, (int)worker->s->port);
+                         worker->s->hostname_ex, (int)worker->s->port);
         }
     }
     return (rv == APR_SUCCESS ? OK : !OK);
@@ -579,7 +580,7 @@
     status = ap_proxy_acquire_connection(proxy_function, backend, hc, ctx->s);
     if (status == OK) {
         (*backend)->addr = hc->cp->addr;
-        (*backend)->hostname = hc->s->hostname;
+        (*backend)->hostname = hc->s->hostname_ex;
         if (strcmp(hc->s->scheme, "https") == 0) {
             if (!ap_proxy_ssl_enable(NULL)) {
                 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ctx->s, APLOGNO(03252)
--- 2.4.x/modules/proxy/mod_proxy_http.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/mod_proxy_http.c	2018/02/16 15:04:41	1824504
@@ -1973,7 +1973,7 @@
                 backend->close = 1;
                 ap_log_rerror(APLOG_MARK, APLOG_INFO, status, r, APLOGNO(01115)
                               "HTTP: 100-Continue failed to %pI (%s)",
-                              worker->cp->addr, worker->s->hostname);
+                              worker->cp->addr, worker->s->hostname_ex);
                 retry++;
                 continue;
             } else {
--- 2.4.x/modules/proxy/proxy_util.c	2018/02/16 14:58:32	1824503
+++ 2.4.x/modules/proxy/proxy_util.c	2018/02/16 15:04:41	1824504
@@ -1694,9 +1694,13 @@
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf, APLOGNO(010117)
         "Alert! worker scheme (%s) too long; truncated to: %s", uri.scheme, wshared->scheme);
     }
+    if (PROXY_STRNCPY(wshared->hostname_ex, uri.hostname) != APR_SUCCESS) {
+        return apr_psprintf(p, "worker hostname (%s) too long", uri.hostname);
+    }
     if (PROXY_STRNCPY(wshared->hostname, uri.hostname) != APR_SUCCESS) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf, APLOGNO(010118)
-        "Alert! worker hostname (%s) too long; truncated to: %s", uri.hostname, wshared->hostname);
+        ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf, APLOGNO(010118)
+        "worker hostname (%s) too long; truncated for legacy modules that do not use "
+        "proxy_worker_shared->hostname_ex: %s", uri.hostname, wshared->hostname);
     }
     wshared->flush_packets = flush_off;
     wshared->flush_wait = PROXY_FLUSH_WAIT;
@@ -1854,7 +1858,7 @@
 
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00930)
                 "initialized pool in child %" APR_PID_T_FMT " for (%s) min=%d max=%d smax=%d",
-                 getpid(), worker->s->hostname, worker->s->min,
+                 getpid(), worker->s->hostname_ex, worker->s->min,
                  worker->s->hmax, worker->s->smax);
 
             /* Set the acquire timeout */
@@ -1871,7 +1875,7 @@
 
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00931)
                  "initialized single connection worker in child %" APR_PID_T_FMT " for (%s)",
-                 getpid(), worker->s->hostname);
+                 getpid(), worker->s->hostname_ex);
         }
         apr_global_mutex_unlock(proxy_mutex);
 
@@ -1890,7 +1894,7 @@
         if (PROXY_WORKER_IS(worker, PROXY_WORKER_STOPPED)) {
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(3305)
                          "%s: Won't retry worker (%s): stopped",
-                         proxy_function, worker->s->hostname);
+                         proxy_function, worker->s->hostname_ex);
             return DECLINED;
         }
         if ((worker->s->status & PROXY_WORKER_IGNORE_ERRORS)
@@ -1899,13 +1903,13 @@
             worker->s->status &= ~PROXY_WORKER_IN_ERROR;
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00932)
                          "%s: worker for (%s) has been marked for retry",
-                         proxy_function, worker->s->hostname);
+                         proxy_function, worker->s->hostname_ex);
             return OK;
         }
         else {
             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00933)
                          "%s: too soon to retry worker for (%s)",
-                         proxy_function, worker->s->hostname);
+                         proxy_function, worker->s->hostname_ex);
             return DECLINED;
         }
     }
@@ -2127,7 +2131,7 @@
         if (!PROXY_WORKER_IS_USABLE(worker)) {
             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00940)
                          "%s: disabled connection for (%s)",
-                         proxy_function, worker->s->hostname);
+                         proxy_function, worker->s->hostname_ex);
             return HTTP_SERVICE_UNAVAILABLE;
         }
     }
@@ -2150,12 +2154,12 @@
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00941)
                      "%s: failed to acquire connection for (%s)",
-                     proxy_function, worker->s->hostname);
+                     proxy_function, worker->s->hostname_ex);
         return HTTP_SERVICE_UNAVAILABLE;
     }
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00942)
                  "%s: has acquired connection for (%s)",
-                 proxy_function, worker->s->hostname);
+                 proxy_function, worker->s->hostname_ex);
 
     (*conn)->worker = worker;
     (*conn)->close  = 0;
@@ -2170,7 +2174,7 @@
 {
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00943)
                 "%s: has released connection for (%s)",
-                proxy_function, conn->worker->s->hostname);
+                proxy_function, conn->worker->s->hostname_ex);
     connection_cleanup(conn);
 
     return OK;
@@ -2766,7 +2770,7 @@
                              "%s: error creating Unix domain socket for "
                              "target %s",
                              proxy_function,
-                             worker->s->hostname);
+                             worker->s->hostname_ex);
                 break;
             }
             conn->connection = NULL;
@@ -2779,7 +2783,7 @@
                              "%s (%s) failed",
                              proxy_function,
                              conn->uds_path,
-                             worker->s->hostname);
+                             worker->s->hostname_ex);
                 break;
             }
 
@@ -2788,7 +2792,7 @@
                          "%s (%s)",
                          proxy_function,
                          conn->uds_path,
-                         worker->s->hostname);
+                         worker->s->hostname_ex);
         }
         else
 #endif
@@ -2802,7 +2806,7 @@
                              "target %s",
                              proxy_function,
                              backend_addr->family,
-                             worker->s->hostname);
+                             worker->s->hostname_ex);
                 /*
                  * this could be an IPv6 address from the DNS but the
                  * local machine won't give us an IPv6 socket; hopefully the
@@ -2852,7 +2856,7 @@
             }
             ap_log_error(APLOG_MARK, APLOG_TRACE2, 0, s,
                          "%s: fam %d socket created to connect to %s",
-                         proxy_function, backend_addr->family, worker->s->hostname);
+                         proxy_function, backend_addr->family, worker->s->hostname_ex);
 
             if (conf->source_address_set) {
                 local_addr = apr_pmemdup(conn->scpool, conf->source_address,
@@ -2877,7 +2881,7 @@
                              "%s: attempt to connect to %pI (%s) failed",
                              proxy_function,
                              backend_addr,
-                             worker->s->hostname);
+                             worker->s->hostname_ex);
                 backend_addr = backend_addr->next;
                 continue;
             }
@@ -2886,7 +2890,7 @@
                          "%s: connection established with %pI (%s)",
                          proxy_function,
                          backend_addr,
-                         worker->s->hostname);
+                         worker->s->hostname_ex);
         }
 
         /* Set a timeout on the socket */
@@ -2920,7 +2924,7 @@
                                  "via http CONNECT through %pI (%s) failed",
                                  proxy_function,
                                  forward->target_host, forward->target_port,
-                                 backend_addr, worker->s->hostname);
+                                 backend_addr, worker->s->hostname_ex);
                     backend_addr = backend_addr->next;
                     continue;
                 }
@@ -2942,7 +2946,7 @@
                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00959)
                     "ap_proxy_connect_backend disabling worker for (%s) for %"
                     APR_TIME_T_FMT "s",
-                    worker->s->hostname, apr_time_sec(worker->s->retry));
+                    worker->s->hostname_ex, apr_time_sec(worker->s->retry));
             }
         }
         else {
