]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Re-enable name resolution of client addresses.
authormsweet <msweet@a1ca3aef-8c08-0410-bb20-df032aa958be>
Fri, 4 Oct 2013 15:54:22 +0000 (15:54 +0000)
committermsweet <msweet@a1ca3aef-8c08-0410-bb20-df032aa958be>
Fri, 4 Oct 2013 15:54:22 +0000 (15:54 +0000)
git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@11325 a1ca3aef-8c08-0410-bb20-df032aa958be

cups/http-addr.c
cups/http.c
cups/http.h
scheduler/client.c

index d9ab970991e0dd23f61b8daf8e5c7a181d16f8e9..ad74f083fcbc21809e8073b0fc620b24f9d7c413 100644 (file)
@@ -696,7 +696,8 @@ httpGetHostByName(const char *name) /* I - Hostname or IP address */
  * 'httpGetHostname()' - Get the FQDN for the connection or local system.
  *
  * When "http" points to a connected socket, return the hostname or
  * 'httpGetHostname()' - Get the FQDN for the connection or local system.
  *
  * When "http" points to a connected socket, return the hostname or
- * address that was used in the call to httpConnect() or httpConnectEncrypt().
+ * address that was used in the call to httpConnect() or httpConnectEncrypt(),
+ * or the address of the client for the connection from httpAcceptConnection().
  * Otherwise, return the FQDN for the local system using both gethostname()
  * and gethostbyname() to get the local hostname with domain.
  *
  * Otherwise, return the FQDN for the local system using both gethostname()
  * and gethostbyname() to get the local hostname with domain.
  *
@@ -791,6 +792,47 @@ httpGetHostname(http_t *http,              /* I - HTTP connection or NULL */
 }
 
 
 }
 
 
+/*
+ * 'httpResolveHostname()' - Resolve the hostname of the HTTP connection
+ *                           address.
+ *
+ * @since CUPS 2.0@
+ */
+
+const char *                           /* O - Resolved hostname or @code NULL@ */
+httpResolveHostname(http_t *http,      /* I - HTTP connection */
+                    char   *buffer,    /* I - Hostname buffer */
+                    size_t bufsize)    /* I - Size of buffer */
+{
+  if (!http)
+    return (NULL);
+
+  if (isdigit(http->hostname[0] & 255) || http->hostname[0] == '[')
+  {
+    char       temp[1024];             /* Temporary string */
+
+    if (httpAddrLookup(http->hostaddr, temp, sizeof(temp)))
+      strlcpy(http->hostname, temp, sizeof(http->hostname));
+    else
+      return (NULL);
+  }
+
+  if (buffer)
+  {
+    if (http->hostname[0] == '/')
+      strlcpy(buffer, "localhost", bufsize);
+    else
+      strlcpy(buffer, http->hostname, bufsize);
+
+    return (buffer);
+  }
+  else if (http->hostname[0] == '/')
+    return ("localhost");
+  else
+    return (http->hostname);
+}
+
+
 /*
  * End of "$Id$".
  */
 /*
  * End of "$Id$".
  */
index fc67ba890e75325223dff387c90c21d49edd3e6c..bacf8c7ad856b40984b5ee8f4dbe64426852a880 100644 (file)
@@ -54,7 +54,6 @@ static http_t         *http_create(const char *host, int port,
 static void            http_debug_hex(const char *prefix, const char *buffer,
                                       int bytes);
 #endif /* DEBUG */
 static void            http_debug_hex(const char *prefix, const char *buffer,
                                       int bytes);
 #endif /* DEBUG */
-static http_field_t    http_field(const char *name);
 static ssize_t         http_read(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read_buffered(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read_chunk(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read_buffered(http_t *http, char *buffer, size_t length);
 static ssize_t         http_read_chunk(http_t *http, char *buffer, size_t length);
@@ -66,16 +65,11 @@ static ssize_t              http_write_chunk(http_t *http, const char *buffer,
                                         size_t length);
 #ifdef HAVE_SSL
 static int             http_read_ssl(http_t *http, char *buf, int len);
                                         size_t length);
 #ifdef HAVE_SSL
 static int             http_read_ssl(http_t *http, char *buf, int len);
-#  ifdef HAVE_CDSASSL
 static int             http_set_credentials(http_t *http);
 static int             http_set_credentials(http_t *http);
-#  endif /* HAVE_CDSASSL */
 #endif /* HAVE_SSL */
 static off_t           http_set_length(http_t *http);
 static void            http_set_timeout(int fd, double timeout);
 static void            http_set_wait(http_t *http);
 #endif /* HAVE_SSL */
 static off_t           http_set_length(http_t *http);
 static void            http_set_timeout(int fd, double timeout);
 static void            http_set_wait(http_t *http);
-#ifdef DEBUG
-static const char      *http_state_string(http_state_t state);
-#endif /* DEBUG */
 #ifdef HAVE_SSL
 static int             http_setup_ssl(http_t *http);
 static void            http_shutdown_ssl(http_t *http);
 #ifdef HAVE_SSL
 static int             http_setup_ssl(http_t *http);
 static void            http_shutdown_ssl(http_t *http);
@@ -740,6 +734,25 @@ httpError(http_t *http)                    /* I - HTTP connection */
 }
 
 
 }
 
 
+/*
+ * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
+ *                      name.
+ */
+
+http_field_t                           /* O - Field index */
+httpFieldValue(const char *name)       /* I - String name */
+{
+  int  i;                              /* Looping var */
+
+
+  for (i = 0; i < HTTP_FIELD_MAX; i ++)
+    if (!_cups_strcasecmp(name, http_fields[i]))
+      return ((http_field_t)i);
+
+  return (HTTP_FIELD_UNKNOWN);
+}
+
+
 /*
  * 'httpFlush()' - Flush data from a HTTP connection.
  */
 /*
  * 'httpFlush()' - Flush data from a HTTP connection.
  */
@@ -753,7 +766,7 @@ httpFlush(http_t *http)                     /* I - HTTP connection */
 
 
   DEBUG_printf(("httpFlush(http=%p), state=%s", http,
 
 
   DEBUG_printf(("httpFlush(http=%p), state=%s", http,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
  /*
   * Nothing to do if we are in the "waiting" state...
 
  /*
   * Nothing to do if we are in the "waiting" state...
@@ -1209,7 +1222,7 @@ httpGetLength2(http_t *http)              /* I - HTTP connection */
 
 
   DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http,
 
 
   DEBUG_printf(("2httpGetLength2(http=%p), state=%s", http,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
   if (!http)
     return (-1);
 
   if (!http)
     return (-1);
@@ -1894,7 +1907,7 @@ httpPeek(http_t *http,                    /* I - HTTP connection */
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("1httpPeek: 0-length chunk, set state to %s.",
-                  http_state_string(http->state)));
+                  httpStateString(http->state)));
 
    /*
     * Prevent future reads for this request...
 
    /*
     * Prevent future reads for this request...
@@ -2363,7 +2376,7 @@ httpRead2(http_t *http,                   /* I - HTTP connection */
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("1httpRead2: End of content, set state to %s.",
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("1httpRead2: End of content, set state to %s.",
-                 http_state_string(http->state)));
+                 httpStateString(http->state)));
   }
 
   return (bytes);
   }
 
   return (bytes);
@@ -2513,7 +2526,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   else if (http->state != HTTP_STATE_WAITING)
   {
     DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
   else if (http->state != HTTP_STATE_WAITING)
   {
     DEBUG_printf(("1httpReadRequest: Bad state %s, returning HTTP_STATE_ERROR.",
-                  http_state_string(http->state)));
+                  httpStateString(http->state)));
     return (HTTP_STATE_ERROR);
   }
 
     return (HTTP_STATE_ERROR);
   }
 
@@ -2612,7 +2625,7 @@ httpReadRequest(http_t *http,             /* I - HTTP connection */
   }
 
   DEBUG_printf(("1httpReadRequest: Set state to %s.",
   }
 
   DEBUG_printf(("1httpReadRequest: Set state to %s.",
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
   if (!strcmp(req_version, "HTTP/1.0"))
   {
 
   if (!strcmp(req_version, "HTTP/1.0"))
   {
@@ -3201,7 +3214,7 @@ _httpUpdate(http_t        *http,  /* I - HTTP connection */
 
 
   DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http, status,
 
 
   DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", http, status,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
  /*
   * Grab a single line from the connection...
 
  /*
   * Grab a single line from the connection...
@@ -3272,7 +3285,7 @@ _httpUpdate(http_t        *http,  /* I - HTTP connection */
          http->state ++;
 
          DEBUG_printf(("1_httpUpdate: Set state to %s.",
          http->state ++;
 
          DEBUG_printf(("1_httpUpdate: Set state to %s.",
-                       http_state_string(http->state)));
+                       httpStateString(http->state)));
 
       case HTTP_STATE_POST_SEND :
       case HTTP_STATE_HEAD :
 
       case HTTP_STATE_POST_SEND :
       case HTTP_STATE_HEAD :
@@ -3345,7 +3358,7 @@ _httpUpdate(http_t        *http,  /* I - HTTP connection */
 
       httpSetCookie(http, value);
     }
 
       httpSetCookie(http, value);
     }
-    else if ((field = http_field(line)) != HTTP_FIELD_UNKNOWN)
+    else if ((field = httpFieldValue(line)) != HTTP_FIELD_UNKNOWN)
       httpSetField(http, field, value);
 #ifdef DEBUG
     else
       httpSetField(http, field, value);
 #ifdef DEBUG
     else
@@ -3375,7 +3388,7 @@ httpUpdate(http_t *http)          /* I - HTTP connection */
 
 
   DEBUG_printf(("httpUpdate(http=%p), state=%s", http,
 
 
   DEBUG_printf(("httpUpdate(http=%p), state=%s", http,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
  /*
   * Flush pending data, if any...
 
  /*
   * Flush pending data, if any...
@@ -3780,7 +3793,7 @@ httpWrite2(http_t     *http,              /* I - HTTP connection */
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("2httpWrite2: Changed state to %s.",
       http->state = HTTP_STATE_STATUS;
 
     DEBUG_printf(("2httpWrite2: Changed state to %s.",
-                 http_state_string(http->state)));
+                 httpStateString(http->state)));
   }
 
   DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT ".", CUPS_LLCAST bytes));
   }
 
   DEBUG_printf(("1httpWrite2: Returning " CUPS_LLFMT ".", CUPS_LLCAST bytes));
@@ -4027,7 +4040,7 @@ httpWriteResponse(http_t        *http,    /* I - HTTP connection */
            http->state == HTTP_STATE_STATUS)
   {
     DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
            http->state == HTTP_STATE_STATUS)
   {
     DEBUG_printf(("1httpWriteResponse: Resetting state to HTTP_STATE_WAITING, "
-                  "was %s.", http_state_string(http->state)));
+                  "was %s.", httpStateString(http->state)));
     http->state = HTTP_STATE_WAITING;
   }
   else
     http->state = HTTP_STATE_WAITING;
   }
   else
@@ -4534,24 +4547,6 @@ http_debug_hex(const char *prefix,       /* I - Prefix for line */
 #endif /* DEBUG */
 
 
 #endif /* DEBUG */
 
 
-/*
- * 'http_field()' - Return the field index for a field name.
- */
-
-static http_field_t                    /* O - Field index */
-http_field(const char *name)           /* I - String name */
-{
-  int  i;                              /* Looping var */
-
-
-  for (i = 0; i < HTTP_FIELD_MAX; i ++)
-    if (_cups_strcasecmp(name, http_fields[i]) == 0)
-      return ((http_field_t)i);
-
-  return (HTTP_FIELD_UNKNOWN);
-}
-
-
 /*
  * 'http_read()' - Read a buffer from a HTTP connection.
  *
 /*
  * 'http_read()' - Read a buffer from a HTTP connection.
  *
@@ -5105,7 +5100,7 @@ http_set_length(http_t *http)             /* I - Connection */
 
 
   DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", http, http->mode,
 
 
   DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", http, http->mode,
-                http_state_string(http->state)));
+                httpStateString(http->state)));
 
   if ((remaining = httpGetLength2(http)) >= 0)
   {
 
   if ((remaining = httpGetLength2(http)) >= 0)
   {
@@ -5651,45 +5646,6 @@ http_shutdown_ssl(http_t *http)          /* I - HTTP connection */
 #endif /* HAVE_SSL */
 
 
 #endif /* HAVE_SSL */
 
 
-#ifdef DEBUG
-/*
- * 'http_state_string()' - Return the string associated with a given HTTP state.
- */
-
-static const char *                    /* O - State string */
-http_state_string(http_state_t state)  /* I - HTTP state */
-{
-  static char buffer[255];             /* Unknown value buffer */
-  static const char * const states[] = /* State strings */
-  {
-    "HTTP_STATE_ERROR",
-    "HTTP_STATE_WAITING",
-    "HTTP_STATE_OPTIONS",
-    "HTTP_STATE_GET",
-    "HTTP_STATE_GET_SEND",
-    "HTTP_STATE_HEAD",
-    "HTTP_STATE_POST",
-    "HTTP_STATE_POST_RECV",
-    "HTTP_STATE_POST_SEND",
-    "HTTP_STATE_PUT",
-    "HTTP_STATE_PUT_RECV",
-    "HTTP_STATE_DELETE",
-    "HTTP_STATE_TRACE",
-    "HTTP_STATE_CONNECT",
-    "HTTP_STATE_STATUS",
-    "HTTP_STATE_UNKNOWN_METHOD",
-    "HTTP_STATE_UNKNOWN_VERSION"
-  };
-
-  if (state >= HTTP_STATE_ERROR && state <= HTTP_STATE_UNKNOWN_VERSION)
-    return (states[state - HTTP_STATE_ERROR]);
-
-  snprintf(buffer, sizeof(buffer), "??? %d ???", (int)state);
-  return (buffer);
-}
-#endif /* DEBUG */
-
-
 #ifdef HAVE_SSL
 /*
  * 'http_upgrade()' - Force upgrade to TLS encryption.
 #ifdef HAVE_SSL
 /*
  * 'http_upgrade()' - Force upgrade to TLS encryption.
index ce2c2515201898865b7bd3b0204baeac569a2505..1395387282144698b95cc728998cb38231ccd580 100644 (file)
@@ -615,6 +615,7 @@ extern http_state_t httpWriteResponse(http_t *http,
 
 /**** New in CUPS 2.0 ****/
 extern int             httpAddrFamily(http_addr_t *addr) _CUPS_API_2_0;
 
 /**** New in CUPS 2.0 ****/
 extern int             httpAddrFamily(http_addr_t *addr) _CUPS_API_2_0;
+extern http_field_t    httpFieldValue(const char *name) _CUPS_API_2_0;
 extern time_t          httpGetActivity(http_t *http) _CUPS_API_2_0;
 extern http_addr_t     *httpGetAddress(http_t *http) _CUPS_API_2_0;
 extern http_encryption_t httpGetEncryption(http_t *http) _CUPS_API_2_0;
 extern time_t          httpGetActivity(http_t *http) _CUPS_API_2_0;
 extern http_addr_t     *httpGetAddress(http_t *http) _CUPS_API_2_0;
 extern http_encryption_t httpGetEncryption(http_t *http) _CUPS_API_2_0;
@@ -623,6 +624,7 @@ extern size_t               httpGetReady(http_t *http) _CUPS_API_2_0;
 extern size_t          httpGetRemaining(http_t *http) _CUPS_API_2_0;
 extern int             httpIsChunked(http_t *http) _CUPS_API_2_0;
 extern int             httpIsEncrypted(http_t *http) _CUPS_API_2_0;
 extern size_t          httpGetRemaining(http_t *http) _CUPS_API_2_0;
 extern int             httpIsChunked(http_t *http) _CUPS_API_2_0;
 extern int             httpIsEncrypted(http_t *http) _CUPS_API_2_0;
+extern const char      *httpResolveHostname(http_t *http, char *buffer, size_t bufsize) _CUPS_API_2_0;
 extern void            httpSetKeepAlive(http_t *http, http_keepalive_t keep_alive) _CUPS_API_2_0;
 extern void            httpShutdown(http_t *http) _CUPS_API_2_0;
 extern const char      *httpStateString(http_state_t state);
 extern void            httpSetKeepAlive(http_t *http, http_keepalive_t keep_alive) _CUPS_API_2_0;
 extern void            httpShutdown(http_t *http) _CUPS_API_2_0;
 extern const char      *httpStateString(http_state_t state);
index 0fda74791eb06541d71687f72e266bcb45e06860..bc47f9c7aa3aa73f743c58934abfc2ebc284b04d 100644 (file)
@@ -75,15 +75,12 @@ static void         write_pipe(cupsd_client_t *con);
 void
 cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
 {
 void
 cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
 {
+  const char           *hostname;      /* Hostname of client */
   char                 name[256];      /* Hostname of client */
   int                  count;          /* Count of connections on a host */
   char                 name[256];      /* Hostname of client */
   int                  count;          /* Count of connections on a host */
-//  int                        val;            /* Parameter value */
   cupsd_client_t       *con,           /* New client pointer */
                        *tempcon;       /* Temporary client pointer */
   cupsd_client_t       *con,           /* New client pointer */
                        *tempcon;       /* Temporary client pointer */
-//  http_addrlist_t    *addrlist,      /* List of adddresses for host */
-//                     *addr;          /* Current address */
   socklen_t            addrlen;        /* Length of address */
   socklen_t            addrlen;        /* Length of address */
-//  char                       *hostname;      /* Hostname for address */
   http_addr_t          temp;           /* Temporary address variable */
   static time_t                last_dos = 0;   /* Time of last DoS attack */
 #ifdef HAVE_TCPD_H
   http_addr_t          temp;           /* Temporary address variable */
   static time_t                last_dos = 0;   /* Time of last DoS attack */
 #ifdef HAVE_TCPD_H
@@ -141,10 +138,6 @@ cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
 
   con->number = ++ LastClientNumber;
   con->file   = -1;
 
   con->number = ++ LastClientNumber;
   con->file   = -1;
-//  con->http->activity   = time(NULL);
-//  httpGetAddress(con->http)   = &(con->clientaddr);
-//  con->http->wait_value = 10000;
-//  con->http->mode       = _HTTP_MODE_SERVER;
 
   if ((con->http = httpAcceptConnection(lis->fd, 0)) == NULL)
   {
 
   if ((con->http = httpAcceptConnection(lis->fd, 0)) == NULL)
   {
@@ -211,36 +204,14 @@ cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
     return;
   }
 
     return;
   }
 
-#if 0 /* Decide if we keep this, and where to store the name */
  /*
   * Get the hostname or format the IP address as needed...
   */
 
  /*
   * Get the hostname or format the IP address as needed...
   */
 
-  if (httpAddrLocalhost(httpGetAddress(con->http)))
-  {
-   /*
-    * Map accesses from the loopback interface to "localhost"...
-    */
-
-    strlcpy(httpGetHostname(con->http, NULL, 0), "localhost", sizeof(httpGetHostname(con->http, NULL, 0)));
-    hostname = httpGetHostname(con->http, NULL, 0);
-  }
+  if (HostNameLookups)
+    hostname = httpResolveHostname(con->http, NULL, 0);
   else
   else
-  {
-   /*
-    * Map accesses from the same host to the server name.
-    */
-
-    if (HostNameLookups)
-      hostname = httpAddrLookup(httpGetAddress(con->http), httpGetHostname(con->http, NULL, 0),
-                                sizeof(httpGetHostname(con->http, NULL, 0)));
-    else
-    {
-      hostname = NULL;
-      httpAddrString(httpGetAddress(con->http), httpGetHostname(con->http, NULL, 0),
-                     sizeof(httpGetHostname(con->http, NULL, 0)));
-    }
-  }
+    hostname = httpGetHostname(con->http, NULL, 0);
 
   if (hostname == NULL && HostNameLookups == 2)
   {
 
   if (hostname == NULL && HostNameLookups == 2)
   {
@@ -264,8 +235,10 @@ cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
     * Do double lookups as needed...
     */
 
     * Do double lookups as needed...
     */
 
-    if ((addrlist = httpAddrGetList(httpGetHostname(con->http, NULL, 0), AF_UNSPEC, NULL))
-            != NULL)
+    http_addrlist_t    *addrlist,      /* List of addresses */
+                       *addr;          /* Current address */
+
+    if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, NULL)) != NULL)
     {
      /*
       * See if the hostname maps to the same IP address...
     {
      /*
       * See if the hostname maps to the same IP address...
@@ -296,7 +269,6 @@ cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
       return;
     }
   }
       return;
     }
   }
-#endif /* 0 */
 
 #ifdef HAVE_TCPD_H
  /*
 
 #ifdef HAVE_TCPD_H
  /*