]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/dirsvc.c
Fix regression in fix for rdar://40436080.
[thirdparty/cups.git] / scheduler / dirsvc.c
index 495bf1b7f51f9e8f32d6befb2577f330518d01ad..ddd3701e07c29de2402d5f89cd702b815aef2a86 100644 (file)
@@ -1,16 +1,11 @@
 /*
- * "$Id$"
- *
  * Directory services routines for the CUPS scheduler.
  *
- * Copyright 2007-2014 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright © 2007-2018 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
@@ -237,11 +232,13 @@ cupsdStartBrowsing(void)
   if (BrowseLocalProtocols & BROWSE_SMB)
     update_smb(1);
 
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
  /*
   * Register the individual printers
   */
 
   dnssdRegisterAllPrinters(0);
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
 }
 
 
@@ -255,6 +252,7 @@ cupsdStopBrowsing(void)
   if (!Browsing || !BrowseLocalProtocols)
     return;
 
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
  /*
   * De-register the individual printers
   */
@@ -265,7 +263,6 @@ cupsdStopBrowsing(void)
   * Shut down browsing sockets...
   */
 
-#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDMaster)
     dnssdStop();
 #endif /* HAVE_DNSSD || HAVE_AVAHI */
@@ -346,14 +343,17 @@ dnssdBuildTxtRecord(
 {
   int          i,                      /* Looping var */
                count;                  /* Count of key/value pairs */
-  char         admin_hostname[256],    /* .local hostname for admin page */
+  char         admin_hostname[256],    /* Hostname for admin page */
                adminurl_str[256],      /* URL for the admin page */
                type_str[32],           /* Type to string buffer */
                state_str[32],          /* State to string buffer */
                rp_str[1024],           /* Queue name string buffer */
                air_str[1024],          /* auth-info-required string buffer */
-               *keyvalue[32][2];       /* Table of key/value pairs */
+               *keyvalue[32][2],       /* Table of key/value pairs */
+                *ptr;                   /* Pointer in string */
   cupsd_txt_t  txt;                    /* TXT record */
+  cupsd_listener_t *lis;                /* Current listener */
+  const char    *admin_scheme = "http"; /* Admin page URL scheme */
 
 
  /*
@@ -382,20 +382,46 @@ dnssdBuildTxtRecord(
     keyvalue[count  ][0] = "ty";
     keyvalue[count++][1] = p->make_model ? p->make_model : "Unknown";
 
-    if (strstr(DNSSDHostName, ".local"))
-      strlcpy(admin_hostname, DNSSDHostName, sizeof(admin_hostname));
+   /*
+    * Get the hostname for the admin page...
+    */
+
+    if (strchr(DNSSDHostName, '.'))
+    {
+     /*
+      * Use the provided hostname, but make sure it ends with a period...
+      */
+
+      if ((ptr = DNSSDHostName + strlen(DNSSDHostName) - 1) >= DNSSDHostName && *ptr == '.')
+        strlcpy(admin_hostname, DNSSDHostName, sizeof(admin_hostname));
+      else
+        snprintf(admin_hostname, sizeof(admin_hostname), "%s.", DNSSDHostName);
+    }
     else
-      snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.",
-               DNSSDHostName);
-    httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
+    {
+     /*
+      * Unqualified hostname gets ".local." added to it...
+      */
+
+      snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.", DNSSDHostName);
+    }
+
+   /*
+    * Get the URL scheme for the admin page...
+    */
+
 #  ifdef HAVE_SSL
-                    "https",
-#  else
-                    "http",
+    for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); lis; lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
+    {
+      if (lis->encryption != HTTP_ENCRYPTION_NEVER)
+      {
+        admin_scheme = "https";
+        break;
+      }
+    }
 #  endif /* HAVE_SSL */
-                    NULL, admin_hostname, DNSSDPort, "/%s/%s",
-                    (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
-                    p->name);
+
+    httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str), admin_scheme,  NULL, admin_hostname, DNSSDPort, "/%s/%s", (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
     keyvalue[count  ][0] = "adminurl";
     keyvalue[count++][1] = adminurl_str;
 
@@ -669,17 +695,21 @@ dnssdDeregisterInstance(
 
   DNSServiceRefDeallocate(*srv);
 
+  *srv = NULL;
+
 #  else /* HAVE_AVAHI */
   if (!from_callback)
     avahi_threaded_poll_lock(DNSSDMaster);
 
-  avahi_entry_group_free(*srv);
+  if (*srv)
+  {
+    avahi_entry_group_free(*srv);
+    *srv = NULL;
+  }
 
   if (!from_callback)
     avahi_threaded_poll_unlock(DNSSDMaster);
 #  endif /* HAVE_DNSSD */
-
-  *srv = NULL;
 }
 
 
@@ -1043,7 +1073,7 @@ dnssdRegisterInstance(
 
   *srv  = DNSSDMaster;
   error = DNSServiceRegister(srv, kDNSServiceFlagsShareConnection,
-                            0, name, temp, NULL, NULL, htons(port),
+                            0, name, temp, NULL, DNSSDHostName, htons(port),
                             txt ? TXTRecordGetLength(txt) : 0,
                             txt ? TXTRecordGetBytesPtr(txt) : NULL,
                             dnssdRegisterCallback, p);
@@ -1058,7 +1088,7 @@ dnssdRegisterInstance(
 
   error = avahi_entry_group_add_service_strlst(*srv, AVAHI_IF_UNSPEC,
                                                AVAHI_PROTO_UNSPEC, 0, name,
-                                               type, NULL, NULL, port,
+                                               type, NULL, DNSSDHostName, port,
                                                txt ? *txt : NULL);
   if (error)
     cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD service add for \"%s\" failed.",
@@ -1294,13 +1324,20 @@ dnssdStop(void)
   DNSSDMaster = NULL;
 
 #  else /* HAVE_AVAHI */
-  avahi_threaded_poll_stop(DNSSDMaster);
+  if (DNSSDMaster)
+    avahi_threaded_poll_stop(DNSSDMaster);
 
-  avahi_client_free(DNSSDClient);
-  DNSSDClient = NULL;
+  if (DNSSDClient)
+  {
+    avahi_client_free(DNSSDClient);
+    DNSSDClient = NULL;
+  }
 
-  avahi_threaded_poll_free(DNSSDMaster);
-  DNSSDMaster = NULL;
+  if (DNSSDMaster)
+  {
+    avahi_threaded_poll_free(DNSSDMaster);
+    DNSSDMaster = NULL;
+  }
 #  endif /* HAVE_DNSSD */
 
   cupsArrayDelete(DNSSDPrinters);
@@ -1417,23 +1454,29 @@ dnssdUpdateDNSSDName(int from_callback) /* I - Called from callback? */
       cupsdSetString(&DNSSDComputerName, ServerName);
     }
 
-   /*
-    * Get the local hostname from the dynamic store...
-    */
-
-    cupsdClearString(&DNSSDHostName);
-
-    if ((nameRef = SCDynamicStoreCopyLocalHostName(sc)) != NULL)
+    if (!DNSSDHostName)
     {
-      if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
-                            kCFStringEncodingUTF8))
+     /*
+      * Get the local hostname from the dynamic store...
+      */
+
+      if ((nameRef = SCDynamicStoreCopyLocalHostName(sc)) != NULL)
       {
-        cupsdLogMessage(CUPSD_LOG_DEBUG,
-                       "Dynamic store host name is \"%s\".", nameBuffer);
-       cupsdSetString(&DNSSDHostName, nameBuffer);
-      }
+       if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
+                              kCFStringEncodingUTF8))
+       {
+         cupsdLogMessage(CUPSD_LOG_DEBUG, "Dynamic store host name is \"%s\".", nameBuffer);
 
-      CFRelease(nameRef);
+         if (strchr(nameBuffer, '.'))
+           cupsdSetString(&DNSSDHostName, nameBuffer);
+         else
+           cupsdSetStringf(&DNSSDHostName, "%s.local", nameBuffer);
+
+         cupsdLogMessage(CUPSD_LOG_INFO, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName);
+       }
+
+       CFRelease(nameRef);
+      }
     }
 
     if (!DNSSDHostName)
@@ -1442,9 +1485,10 @@ dnssdUpdateDNSSDName(int from_callback)  /* I - Called from callback? */
       * Use the ServerName instead...
       */
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "Using ServerName \"%s\" as host name.", ServerName);
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "Using ServerName \"%s\" as host name.", ServerName);
       cupsdSetString(&DNSSDHostName, ServerName);
+
+      cupsdLogMessage(CUPSD_LOG_INFO, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName);
     }
 
    /*
@@ -1478,26 +1522,37 @@ dnssdUpdateDNSSDName(int from_callback) /* I - Called from callback? */
   if (DNSSDClient)
   {
     const char *host_name = avahi_client_get_host_name(DNSSDClient);
-    const char *host_fqdn = avahi_client_get_host_name_fqdn(DNSSDClient);
 
     cupsdSetString(&DNSSDComputerName, host_name ? host_name : ServerName);
 
-    if (host_fqdn)
-      cupsdSetString(&DNSSDHostName, host_fqdn);
-    else if (strchr(ServerName, '.'))
-      cupsdSetString(&DNSSDHostName, ServerName);
-    else
-      cupsdSetStringf(&DNSSDHostName, "%s.local", ServerName);
+    if (!DNSSDHostName)
+    {
+      const char *host_fqdn = avahi_client_get_host_name_fqdn(DNSSDClient);
+
+      if (host_fqdn)
+       cupsdSetString(&DNSSDHostName, host_fqdn);
+      else if (strchr(ServerName, '.'))
+       cupsdSetString(&DNSSDHostName, ServerName);
+      else
+       cupsdSetStringf(&DNSSDHostName, "%s.local", ServerName);
+
+      cupsdLogMessage(CUPSD_LOG_INFO, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName);
+    }
   }
   else
 #  endif /* HAVE_AVAHI */
   {
     cupsdSetString(&DNSSDComputerName, ServerName);
 
-    if (strchr(ServerName, '.'))
-      cupsdSetString(&DNSSDHostName, ServerName);
-    else
-      cupsdSetStringf(&DNSSDHostName, "%s.local", ServerName);
+    if (!DNSSDHostName)
+    {
+      if (strchr(ServerName, '.'))
+       cupsdSetString(&DNSSDHostName, ServerName);
+      else
+       cupsdSetStringf(&DNSSDHostName, "%s.local", ServerName);
+
+      cupsdLogMessage(CUPSD_LOG_INFO, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName);
+    }
   }
 
  /*
@@ -1811,8 +1866,3 @@ update_smb(int onoff)                     /* I - 1 = turn on, 0 = turn off */
   else
     cupsdLogMessage(CUPSD_LOG_INFO, "Unknown SMBConfigFile scheme!");
 }
-
-
-/*
- * End of "$Id$".
- */