]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/dirsvc.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / scheduler / dirsvc.c
index 4e417b0b29f5bf17253d44ba457ca25bdefbd0a7..518418a32e677fe2bff40bfe2786f25996c1671b 100644 (file)
@@ -1,16 +1,10 @@
 /*
- * "$Id$"
- *
  * Directory services routines for the CUPS scheduler.
  *
- * Copyright 2007-2014 by Apple Inc.
+ * Copyright 2007-2017 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.
  */
 
 /*
@@ -348,14 +342,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 */
 
 
  /*
@@ -384,20 +381,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;
 
@@ -672,13 +695,16 @@ dnssdDeregisterInstance(
   DNSServiceRefDeallocate(*srv);
 
 #  else /* HAVE_AVAHI */
-  if (!from_callback)
-    avahi_threaded_poll_lock(DNSSDMaster);
+  if (*srv)
+  {
+    if (!from_callback)
+      avahi_threaded_poll_lock(DNSSDMaster);
 
-  avahi_entry_group_free(*srv);
+    avahi_entry_group_free(*srv);
 
-  if (!from_callback)
-    avahi_threaded_poll_unlock(DNSSDMaster);
+    if (!from_callback)
+      avahi_threaded_poll_unlock(DNSSDMaster);
+  }
 #  endif /* HAVE_DNSSD */
 
   *srv = NULL;
@@ -1045,7 +1071,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);
@@ -1060,7 +1086,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.",
@@ -1296,13 +1322,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);
@@ -1419,23 +1452,24 @@ 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);
+         cupsdSetString(&DNSSDHostName, nameBuffer);
+       }
 
-      CFRelease(nameRef);
+       CFRelease(nameRef);
+      }
     }
 
     if (!DNSSDHostName)
@@ -1480,26 +1514,33 @@ 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);
+    }
   }
   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);
+    }
   }
 
  /*
@@ -1813,8 +1854,3 @@ update_smb(int onoff)                     /* I - 1 = turn on, 0 = turn off */
   else
     cupsdLogMessage(CUPSD_LOG_INFO, "Unknown SMBConfigFile scheme!");
 }
-
-
-/*
- * End of "$Id$".
- */