]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
create_local_printer(): Improved comparison of device URI host name create-local-printer-localhost-fix 353/head
authorTill Kamppeter <till.kamppeter@gmail.com>
Fri, 10 Jun 2022 19:19:29 +0000 (21:19 +0200)
committerZdenek Dohnal <zdohnal@redhat.com>
Thu, 16 Jun 2022 09:43:09 +0000 (11:43 +0200)
Improved the comparison of the device URI host name with the CUPS
server's host name to find out whether the temporary queue is for a
local IPP service.

- Compare case-insensitively as host names are case-insensitive

- If we have the DNS-SD host name (DNSSDHostName) of the CUPS server,
  consider names equal if they only differ by the presence or absense
  of a trailing dot ('.')

- If we only have the ServerName (for example with "Browsing = Off",
  not sharing printers), consider also a server name without ".local"
  equal to a device URI host name with ".local" as equal.

scheduler/ipp.c

index 1769ef8dcb68fa14d12f2e80df15e34ec5b22bd7..56bd8b30498fedfc70895736fbc8f008b3f3e205 100644 (file)
@@ -5547,8 +5547,9 @@ create_local_printer(
   * the hostname by localhost. This way we assure that local-only services
   * like ipp-usb or Printer Applications always work.
   *
-  * When comparing our hostname with the one in the device URI, consider
-  * names with ยจ.local(.)" suffix and names without suffix the same.
+  * When comparing our hostname with the one in the device URI,
+  * consider names with or without trailing dot ('.') the same. Also
+  * compare case-insensitively.
   */
 
 #ifdef HAVE_DNSSD
@@ -5566,23 +5567,30 @@ create_local_printer(
     int host_len,
         server_name_len;
 
+    /* Get host name of device URI */
     httpSeparateURI(HTTP_URI_CODING_ALL, ptr,
                    scheme, sizeof(scheme), userpass, sizeof(userpass), host,
                    sizeof(host), &port, resource, sizeof(resource));
 
+    /* Take trailing dot out of comparison */
     host_len = strlen(host);
-    if (strcmp(host + host_len - 6, ".local") == 0)
-      host_len -= 6;
-    if (strcmp(host + host_len - 7, ".local.") == 0)
-      host_len -= 7;
+    if (host_len > 1 && host[host_len - 1] == '.')
+      host_len --;
 
     server_name_len = strlen(nameptr);
-    if (strcmp(nameptr + server_name_len - 6, ".local") == 0)
-      server_name_len -= 6;
-    if (strcmp(nameptr + server_name_len - 7, ".local.") == 0)
-      server_name_len -= 7;
+    if (server_name_len > 1 && nameptr[server_name_len - 1] == '.')
+      server_name_len --;
+
+   /*
+    * If we have no DNSSDHostName but only a ServerName (if we are not
+    * sharing printers, Browsing = Off) the ServerName has no ".local"
+    * but the requested device URI has. Take this into account.
+    */
+
+    if (nameptr == ServerName && host_len >= 6 && (server_name_len < 6 || strcmp(nameptr + server_name_len - 6, ".local") != 0) && strcmp(host + host_len - 6, ".local") == 0)
+      host_len -= 6;
 
-    if (host_len == server_name_len && strncmp(host, nameptr, host_len) == 0)
+    if (host_len == server_name_len && strncasecmp(host, nameptr, host_len) == 0)
       ptr = "localhost";
     else
       ptr = host;