]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/adminutil.c
Merge changes from CUPS 1.5svn-r9641
[thirdparty/cups.git] / cups / adminutil.c
index 710780029b99a7c9b5e404510f369a840dc0949b..99f8476c31696210fd0f43fda64af410346b2873 100644 (file)
@@ -1,56 +1,44 @@
 /*
- * "$Id: adminutil.c 5878 2006-08-24 15:55:42Z mike $"
+ * "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $"
  *
- *   Administration utility API definitions for the Common UNIX Printing
- *   System (CUPS).
+ *   Administration utility API definitions for CUPS.
  *
- *   MANY OF THE FUNCTIONS IN THIS HEADER ARE PRIVATE AND SUBJECT TO
- *   CHANGE AT ANY TIME.  USE AT YOUR OWN RISK.
- *
- *   Copyright 2001-2006 by Easy Software Products.
+ *   Copyright 2007-2011 by Apple Inc.
+ *   Copyright 2001-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products 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 missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   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/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
  * Contents:
  *
- *   cupsAdminCreateWindowsPPD()   - Create the Windows PPD file for a printer.
- *   cupsAdminExportSamba()        - Export a printer to Samba.
- *   _cupsAdminGetServerSettings() - Get settings from the server.
- *   _cupsAdminSetServerSettings() - Set settings on the server.
- *   do_samba_command()            - Do a SAMBA command.
- *   get_cupsd_conf()              - Get the current cupsd.conf file.
- *   invalidate_cupsd_cache()      - Invalidate the cached cupsd.conf settings.
- *   write_option()                - Write a CUPS option to a PPD file.
+ *   cupsAdminCreateWindowsPPD()  - Create the Windows PPD file for a printer.
+ *   cupsAdminExportSamba()       - Export a printer to Samba.
+ *   cupsAdminGetServerSettings() - Get settings from the server.
+ *   cupsAdminSetServerSettings() - Set settings on the server.
+ *   do_samba_command()           - Do a SAMBA command.
+ *   get_cupsd_conf()             - Get the current cupsd.conf file.
+ *   invalidate_cupsd_cache()     - Invalidate the cached cupsd.conf settings.
+ *   write_option()               - Write a CUPS option to a PPD file.
  */
 
 /*
  * Include necessary headers...
  */
 
+#include "cups-private.h"
 #include "adminutil.h"
-#include "globals.h"
-#include "debug.h"
-#include <errno.h>
 #include <fcntl.h>
-#include <unistd.h>
 #include <sys/stat.h>
-#include <sys/wait.h>
+#ifdef WIN32
+#else
+#  include <unistd.h>
+#  include <sys/wait.h>
+#endif /* WIN32 */
 
 
 /*
@@ -76,11 +64,13 @@ static void         write_option(cups_file_t *dstfp, int order,
 
 /*
  * 'cupsAdminCreateWindowsPPD()' - Create the Windows PPD file for a printer.
+ *
+ * @since CUPS 1.2/Mac OS X 10.5@
  */
 
 char *                                 /* O - PPD file or NULL */
 cupsAdminCreateWindowsPPD(
-    http_t     *http,                  /* I - Connection to server */
+    http_t     *http,                  /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     const char *dest,                  /* I - Printer or class */
     char       *buffer,                        /* I - Filename buffer */
     int        bufsize)                        /* I - Size of filename buffer */
@@ -100,6 +90,7 @@ cupsAdminCreateWindowsPPD(
                        option[41],     /* Option */
                        choice[41];     /* Choice */
   int                  jcloption,      /* In a JCL option? */
+                       jclorder,       /* Next JCL order dependency */
                        linenum;        /* Current line number */
   time_t               curtime;        /* Current time */
   struct tm            *curdate;       /* Current date */
@@ -121,6 +112,9 @@ cupsAdminCreateWindowsPPD(
   if (buffer)
     *buffer = '\0';
 
+  if (!http)
+    http = _cupsConnect();
+
   if (!http || !dest || !buffer || bufsize < 2)
     return (NULL);
 
@@ -197,6 +191,7 @@ cupsAdminCreateWindowsPPD(
   */
 
   jcloption = 0;
+  jclorder  = 0;
   linenum   = 0;
   language  = cupsLangDefault();
 
@@ -235,17 +230,30 @@ cupsAdminCreateWindowsPPD(
       jcloption = 0;
       cupsFilePrintf(dstfp, "%s\n", line);
     }
+    else if (jcloption && !strncmp(line, "*OrderDependency:", 17))
+    {
+      for (ptr = line + 17; _cups_isspace(*ptr); ptr ++);
+
+      ptr = strchr(ptr, ' ');
+
+      if (ptr)
+      {
+       cupsFilePrintf(dstfp, "*OrderDependency: %d%s\n", jclorder, ptr);
+       jclorder ++;
+      }
+      else
+        cupsFilePrintf(dstfp, "%s\n", line);
+    }
     else if (jcloption &&
              strncmp(line, "*End", 4) &&
-             strncmp(line, "*Default", 8) &&
-             strncmp(line, "*OrderDependency", 16))
+             strncmp(line, "*Default", 8))
     {
       if ((ptr = strchr(line, ':')) == NULL)
       {
         snprintf(line, sizeof(line),
-                 _cupsLangString(language, _("Missing value on line %d!\n")),
-                 linenum);
-        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+                _cupsLangString(language, _("Missing value on line %d.")),
+                linenum);
+        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0);
 
         cupsFileClose(srcfp);
         cupsFileClose(dstfp);
@@ -262,9 +270,9 @@ cupsAdminCreateWindowsPPD(
       {
         snprintf(line, sizeof(line),
                 _cupsLangString(language,
-                                _("Missing double quote on line %d!\n")),
+                                _("Missing double quote on line %d.")),
                 linenum);
-        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0);
 
         cupsFileClose(srcfp);
         cupsFileClose(dstfp);
@@ -277,13 +285,13 @@ cupsAdminCreateWindowsPPD(
        return (NULL);
       }
 
-      if (sscanf(line, "*%40s%*[ \t]%40[^/]", option, choice) != 2)
+      if (sscanf(line, "*%40s%*[ \t]%40[^:/]", option, choice) != 2)
       {
         snprintf(line, sizeof(line),
                 _cupsLangString(language,
-                                _("Bad option + choice on line %d!\n")),
+                                _("Bad option + choice on line %d.")),
                 linenum);
-        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+        _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line, 0);
 
         cupsFileClose(srcfp);
         cupsFileClose(dstfp);
@@ -324,6 +332,18 @@ cupsAdminCreateWindowsPPD(
   cupsFileClose(srcfp);
   unlink(src);
 
+  if (linenum == 0)
+  {
+    _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, _("Empty PPD file."), 1);
+
+    cupsFileClose(dstfp);
+    unlink(buffer);
+
+    *buffer = '\0';
+
+    return (NULL);
+  }
+
  /*
   * Now add the CUPS-specific attributes and options...
   */
@@ -340,24 +360,24 @@ cupsAdminCreateWindowsPPD(
                                   IPP_TAG_ZERO)) != NULL &&
       (suppattr = ippFindAttribute(response, "job-hold-until-supported",
                                    IPP_TAG_ZERO)) != NULL)
-    write_option(dstfp, 10, "cupsJobHoldUntil", "Hold Until", "job-hold-until",
-                 suppattr, defattr, 0, 1);
+    write_option(dstfp, jclorder ++, "cupsJobHoldUntil", "Hold Until",
+                 "job-hold-until", suppattr, defattr, 0, 1);
 
   if ((defattr = ippFindAttribute(response, "job-priority-default",
                                   IPP_TAG_INTEGER)) != NULL &&
       (suppattr = ippFindAttribute(response, "job-priority-supported",
                                    IPP_TAG_RANGE)) != NULL)
-    write_option(dstfp, 11, "cupsJobPriority", "Priority", "job-priority",
-                 suppattr, defattr, 0, 1);
+    write_option(dstfp, jclorder ++, "cupsJobPriority", "Priority",
+                 "job-priority", suppattr, defattr, 0, 1);
 
   if ((defattr = ippFindAttribute(response, "job-sheets-default",
                                   IPP_TAG_ZERO)) != NULL &&
       (suppattr = ippFindAttribute(response, "job-sheets-supported",
                                    IPP_TAG_ZERO)) != NULL)
   {
-    write_option(dstfp, 20, "cupsJobSheetsStart", "Start Banner",
+    write_option(dstfp, jclorder ++, "cupsJobSheetsStart", "Start Banner",
                  "job-sheets", suppattr, defattr, 0, 2);
-    write_option(dstfp, 21, "cupsJobSheetsEnd", "End Banner",
+    write_option(dstfp, jclorder, "cupsJobSheetsEnd", "End Banner",
                  "job-sheets", suppattr, defattr, 1, 2);
   }
 
@@ -372,6 +392,8 @@ cupsAdminCreateWindowsPPD(
 
 /*
  * 'cupsAdminExportSamba()' - Export a printer to Samba.
+ *
+ * @since CUPS 1.2/Mac OS X 10.5@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
@@ -402,7 +424,7 @@ cupsAdminExportSamba(
 
   if (!dest || !ppd || !samba_server || !samba_user || !samba_password)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, NULL);
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
     return (0);
   }
 
@@ -412,7 +434,7 @@ cupsAdminExportSamba(
 
   if ((fp = cupsTempFile2(authfile, sizeof(authfile))) == NULL)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+    _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
     return (0);
   }
 
@@ -463,12 +485,12 @@ cupsAdminExportSamba(
       snprintf(message, sizeof(message),
                _cupsLangString(language,
                               _("Unable to copy Windows 2000 printer "
-                                "driver files (%d)!")), status);
+                                "driver files (%d).")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
-       _cupsLangPrintf(logfile, "%s\n", message);
+       _cupsLangPuts(logfile, message);
 
       unlink(authfile);
 
@@ -498,12 +520,12 @@ cupsAdminExportSamba(
        snprintf(message, sizeof(message),
                 _cupsLangString(language,
                                 _("Unable to copy CUPS printer driver "
-                                  "files (%d)!")), status);
+                                  "files (%d).")), status);
 
-       _cupsSetError(IPP_INTERNAL_ERROR, message);
+       _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
        if (logfile)
-         _cupsLangPrintf(logfile, "%s\n", message);
+         _cupsLangPuts(logfile, message);
 
         unlink(authfile);
 
@@ -541,12 +563,12 @@ cupsAdminExportSamba(
       snprintf(message, sizeof(message),
                _cupsLangString(language,
                               _("Unable to install Windows 2000 printer "
-                                "driver files (%d)!")), status);
+                                "driver files (%d).")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
-       _cupsLangPrintf(logfile, "%s\n", message);
+       _cupsLangPuts(logfile, message);
 
       unlink(authfile);
 
@@ -554,6 +576,10 @@ cupsAdminExportSamba(
     }
   }
 
+ /*
+  * See if we have the Win9x PS driver...
+  */
+
   snprintf(file, sizeof(file), "%s/drivers/ADOBEPS4.DRV", cg->cups_datadir);
   if (!access(file, 0))
   {
@@ -582,12 +608,12 @@ cupsAdminExportSamba(
       snprintf(message, sizeof(message),
                _cupsLangString(language,
                               _("Unable to copy Windows 9x printer "
-                                "driver files (%d)!")), status);
+                                "driver files (%d).")), status);
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
-       _cupsLangPrintf(logfile, "%s\n", message);
+       _cupsLangPuts(logfile, message);
 
       unlink(authfile);
 
@@ -611,12 +637,142 @@ cupsAdminExportSamba(
       snprintf(message, sizeof(message),
                _cupsLangString(language,
                               _("Unable to install Windows 9x printer "
-                                "driver files (%d)!")), status);
+                                "driver files (%d).")), status);
+
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
+
+      if (logfile)
+       _cupsLangPuts(logfile, message);
+
+      unlink(authfile);
+
+      return (0);
+    }
+  }
+
+ /*
+  * See if we have the 64-bit Windows PS driver...
+  *
+  * Files:
+  *
+  *     x64/ps5ui.dll
+  *     x64/pscript.hlp
+  *     x64/pscript.ntf
+  *     x64/pscript5.dll
+  */
+
+  snprintf(file, sizeof(file), "%s/drivers/x64/pscript5.dll", cg->cups_datadir);
+  if (!access(file, 0))
+  {
+    have_drivers |= 4;
+
+   /*
+    * 64-bit Windows driver is installed; do the smbclient commands needed
+    * to copy the Win64 drivers over...
+    */
+
+    snprintf(address, sizeof(address), "//%s/print$", samba_server);
+
+    snprintf(subcmd, sizeof(subcmd),
+             "mkdir x64;"
+            "put %s x64/%s.ppd;"
+            "put %s/drivers/x64/ps5ui.dll x64/ps5ui.dll;"
+            "put %s/drivers/x64/pscript.hlp x64/pscript.hlp;"
+            "put %s/drivers/x64/pscript.ntf x64/pscript.ntf;"
+            "put %s/drivers/x64/pscript5.dll x64/pscript5.dll",
+            ppd, dest, cg->cups_datadir, cg->cups_datadir,
+            cg->cups_datadir, cg->cups_datadir);
+
+    if ((status = do_samba_command("smbclient", address, subcmd,
+                                   authfile, logfile)) != 0)
+    {
+      snprintf(message, sizeof(message),
+               _cupsLangString(language,
+                              _("Unable to copy 64-bit Windows printer "
+                                "driver files (%d).")), status);
+
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
+
+      if (logfile)
+       _cupsLangPuts(logfile, message);
+
+      unlink(authfile);
+
+      return (0);
+    }
+
+   /*
+    * See if we also have the CUPS driver files; if so, use them!
+    */
+
+    snprintf(file, sizeof(file), "%s/drivers/x64/cupsps6.dll", cg->cups_datadir);
+    if (!access(file, 0))
+    {
+     /*
+      * Copy the CUPS driver files over...
+      */
+
+      snprintf(subcmd, sizeof(subcmd),
+               "put %s/drivers/x64/cups6.ini x64/cups6.ini;"
+               "put %s/drivers/x64/cupsps6.dll x64/cupsps6.dll;"
+              "put %s/drivers/x64/cupsui6.dll x64/cupsui6.dll",
+              cg->cups_datadir, cg->cups_datadir, cg->cups_datadir);
+
+      if ((status = do_samba_command("smbclient", address, subcmd,
+                                     authfile, logfile)) != 0)
+      {
+       snprintf(message, sizeof(message),
+                _cupsLangString(language,
+                                _("Unable to copy 64-bit CUPS printer driver "
+                                  "files (%d).")), status);
+
+       _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
+
+       if (logfile)
+         _cupsLangPuts(logfile, message);
+
+        unlink(authfile);
+
+       return (0);
+      }
+      
+     /*
+      * Do the rpcclient command needed for the CUPS drivers...
+      */
+
+      snprintf(subcmd, sizeof(subcmd),
+               "adddriver \"Windows x64\" \"%s:"
+              "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
+              "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf,"
+              "cups6.ini,cupsps6.dll,cupsui6.dll\"",
+              dest, dest, dest);
+    }
+    else
+    {
+     /*
+      * Don't have the CUPS drivers, so just use the standard Windows
+      * drivers...
+      */
 
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      snprintf(subcmd, sizeof(subcmd),
+               "adddriver \"Windows x64\" \"%s:"
+              "pscript5.dll:%s.ppd:ps5ui.dll:pscript.hlp:NULL:RAW:"
+              "pscript5.dll,%s.ppd,ps5ui.dll,pscript.hlp,pscript.ntf\"",
+              dest, dest, dest);
+    }
+
+    if ((status = do_samba_command("rpcclient", samba_server, subcmd,
+                                   authfile, logfile)) != 0)
+    {
+      snprintf(message, sizeof(message),
+               _cupsLangString(language,
+                              _("Unable to install Windows 2000 printer "
+                                "driver files (%d).")), status);
+
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       if (logfile)
-       _cupsLangPrintf(logfile, "%s\n", message);
+       _cupsLangPuts(logfile, message);
 
       unlink(authfile);
 
@@ -629,21 +785,27 @@ cupsAdminExportSamba(
     if (!have_drivers)
       strlcpy(message,
               _cupsLangString(language,
-                             _("No Windows printer drivers are installed!")),
+                             _("No Windows printer drivers are installed.")),
               sizeof(message));
     else
       strlcpy(message,
               _cupsLangString(language,
                              _("Warning, no Windows 2000 printer drivers "
-                               "are installed!")),
+                               "are installed.")),
               sizeof(message));
 
-    _cupsSetError(IPP_INTERNAL_ERROR, message);
-    _cupsLangPrintf(logfile, "%s\n", message);
+    _cupsSetError(IPP_NOT_FOUND, message, 0);
+    _cupsLangPuts(logfile, message);
   }
 
   if (have_drivers == 0)
+  {
+    _cupsSetError(IPP_NOT_FOUND, message, 0);
+
+    unlink(authfile);
+
     return (0);
+  }
 
  /*
   * Finally, associate the drivers we just added with the queue...
@@ -656,13 +818,13 @@ cupsAdminExportSamba(
   {
     snprintf(message, sizeof(message),
              _cupsLangString(language,
-                            _("Unable to set Windows printer driver (%d)!\n")),
+                            _("Unable to set Windows printer driver (%d).")),
                             status);
 
-    _cupsSetError(IPP_INTERNAL_ERROR, message);
+    _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
     if (logfile)
-      _cupsLangPrintf(logfile, "%s\n", message);
+      _cupsLangPuts(logfile, message);
 
     unlink(authfile);
 
@@ -676,17 +838,17 @@ cupsAdminExportSamba(
 
 
 /*
- * '_cupsAdminGetServerSettings()' - Get settings from the server.
+ * 'cupsAdminGetServerSettings()' - Get settings from the server.
  *
  * The returned settings should be freed with cupsFreeOptions() when
  * you are done with them.
  *
- * @since CUPS 1.2@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
-_cupsAdminGetServerSettings(
-    http_t        *http,               /* I - Connection to server */
+cupsAdminGetServerSettings(
+    http_t        *http,               /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int           *num_settings,       /* O - Number of settings */
     cups_option_t **settings)          /* O - Settings */
 {
@@ -705,9 +867,65 @@ _cupsAdminGetServerSettings(
   * Range check input...
   */
 
+  if (!http)
+  {
+   /*
+    * See if we are connected to the same server...
+    */
+
+    if (cg->http)
+    {
+     /*
+      * Compare the connection hostname, port, and encryption settings to
+      * the cached defaults; these were initialized the first time we
+      * connected...
+      */
+
+      if (strcmp(cg->http->hostname, cg->server) ||
+          cg->ipp_port != _httpAddrPort(cg->http->hostaddr) ||
+         (cg->http->encryption != cg->encryption &&
+          cg->http->encryption == HTTP_ENCRYPT_NEVER))
+      {
+       /*
+       * Need to close the current connection because something has changed...
+       */
+
+       httpClose(cg->http);
+       cg->http = NULL;
+      }
+    }
+
+   /*
+    * (Re)connect as needed...
+    */
+
+    if (!cg->http)
+    {
+      if ((cg->http = _httpCreate(cupsServer(), ippPort(), NULL,
+                                  cupsEncryption(), AF_UNSPEC)) == NULL)
+      {
+       if (errno)
+         _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
+       else
+         _cupsSetError(IPP_SERVICE_UNAVAILABLE,
+                       _("Unable to connect to host."), 1);
+
+       if (num_settings)
+         *num_settings = 0;
+
+       if (settings)
+         *settings = NULL;
+
+       return (0);
+      }
+    }
+
+    http = cg->http;
+  }
+
   if (!http || !num_settings || !settings)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, NULL);
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
 
     if (num_settings)
       *num_settings = 0;
@@ -734,9 +952,9 @@ _cupsAdminGetServerSettings(
 
 
       snprintf(message, sizeof(message),
-               _cupsLangString(cupsLangDefault(), _("open of %s failed: %s")),
+               _cupsLangString(cupsLangDefault(), _("Open of %s failed: %s")),
                cupsdconf, strerror(errno));
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
     }
   }
   else
@@ -750,6 +968,7 @@ _cupsAdminGetServerSettings(
 
     int                remote_access = 0,      /* Remote access allowed? */
                remote_admin = 0,       /* Remote administration allowed? */
+               remote_any = 0,         /* Remote access from anywhere allowed? */
                browsing = 1,           /* Browsing enabled? */
                browse_allow = 1,       /* Browse address set? */
                browse_address = 0,     /* Browse address set? */
@@ -769,10 +988,10 @@ _cupsAdminGetServerSettings(
 
     while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
     {
-      if (!value)
-        continue;
+      if (!value && strncmp(line, "</", 2))
+        value = line + strlen(line);
 
-      if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
+      if ((!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")) && value)
       {
        char    *port;                  /* Pointer to port number, if any */
 
@@ -794,7 +1013,7 @@ _cupsAdminGetServerSettings(
             && *value != '/'
 #endif /* AF_LOCAL */
 #ifdef AF_INET6
-            && strcmp(value, "::1")
+            && strcmp(value, "[::1]")
 #endif /* AF_INET6 */
            )
          remote_access = 1;
@@ -828,7 +1047,7 @@ _cupsAdminGetServerSettings(
       {
        in_policy = 0;
       }
-      else if (!strcasecmp(line, "<Limit") && in_policy)
+      else if (!strcasecmp(line, "<Limit") && in_policy && value)
       {
        /*
        * See if the policy limit is for the Cancel-Job operation...
@@ -839,7 +1058,7 @@ _cupsAdminGetServerSettings(
 
        while (*value)
        {
-         for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
+         for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
 
          if (*valptr)
            *valptr++ = '\0';
@@ -850,7 +1069,7 @@ _cupsAdminGetServerSettings(
            break;
          }
 
-          for (value = valptr; isspace(*value & 255); value ++);
+          for (value = valptr; _cups_isspace(*value); value ++);
        }
       }
       else if (!strcasecmp(line, "</Limit>"))
@@ -861,7 +1080,7 @@ _cupsAdminGetServerSettings(
       {
        cancel_policy = 0;
       }
-      else if (!strcasecmp(line, "<Location"))
+      else if (!strcasecmp(line, "<Location") && value)
       {
         in_admin_location = !strcasecmp(value, "/admin");
        in_location       = 1;
@@ -871,7 +1090,7 @@ _cupsAdminGetServerSettings(
        in_admin_location = 0;
        in_location       = 0;
       }
-      else if (!strcasecmp(line, "Allow") && in_admin_location &&
+      else if (!strcasecmp(line, "Allow") && value &&
                strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1")
 #ifdef AF_LOCAL
               && *value != '/'
@@ -881,9 +1100,18 @@ _cupsAdminGetServerSettings(
 #endif /* AF_INET6 */
               )
       {
-       remote_admin = 1;
+        if (in_admin_location)
+         remote_admin = 1;
+        else if (!strcasecmp(value, "all"))
+         remote_any = 1;
       }
-      else if (line[0] != '<' && !in_location && !in_policy)
+      else if (line[0] != '<' && !in_location && !in_policy &&
+              strcasecmp(line, "Allow") &&
+               strcasecmp(line, "AuthType") &&
+              strcasecmp(line, "Deny") &&
+              strcasecmp(line, "Order") &&
+              strcasecmp(line, "Require") &&
+              strcasecmp(line, "Satisfy"))
         cg->cupsd_num_settings = cupsAddOption(line, value,
                                               cg->cupsd_num_settings,
                                               &(cg->cupsd_settings));
@@ -902,6 +1130,11 @@ _cupsAdminGetServerSettings(
                                           cg->cupsd_num_settings,
                                           &(cg->cupsd_settings));
 
+    cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
+                                           remote_any ? "1" : "0",
+                                          cg->cupsd_num_settings,
+                                          &(cg->cupsd_settings));
+
     cg->cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
                                            (browsing && browse_allow) ?
                                               "1" : "0",
@@ -940,19 +1173,21 @@ _cupsAdminGetServerSettings(
 
 
 /*
- * '_cupsAdminSetServerSettings()' - Set settings on the server.
+ * 'cupsAdminSetServerSettings()' - Set settings on the server.
  *
- * @since CUPS 1.2@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
-_cupsAdminSetServerSettings(
-    http_t        *http,               /* I - Connection to server */
+cupsAdminSetServerSettings(
+    http_t        *http,               /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int           num_settings,                /* I - Number of settings */
     cups_option_t *settings)           /* I - Settings */
 {
   int          i;                      /* Looping var */
   http_status_t status;                        /* GET/PUT status */
+  const char   *server_port_env;       /* SERVER_PORT env var */
+  int          server_port;            /* IPP port for server */
   cups_file_t  *cupsd;                 /* cupsd.conf file */
   char         cupsdconf[1024];        /* cupsd.conf filename */
   int          remote;                 /* Remote cupsd.conf file? */
@@ -972,6 +1207,7 @@ _cupsAdminSetServerSettings(
   int          remote_printers,        /* Show remote printers */
                share_printers,         /* Share local printers */
                remote_admin,           /* Remote administration allowed? */
+               remote_any,             /* Remote access from anywhere? */
                user_cancel_any,        /* Cancel-job policy set? */
                debug_logging;          /* LogLevel debug set? */
   int          wrote_port_listen,      /* Wrote the port/listen lines? */
@@ -983,6 +1219,11 @@ _cupsAdminSetServerSettings(
                wrote_root_location;    /* Wrote the / location? */
   int          indent;                 /* Indentation */
   int          cupsd_num_settings;     /* New number of settings */
+  int          old_remote_printers,    /* Show remote printers */
+               old_share_printers,     /* Share local printers */
+               old_remote_admin,       /* Remote administration allowed? */
+               old_user_cancel_any,    /* Cancel-job policy set? */
+               old_debug_logging;      /* LogLevel debug set? */
   cups_option_t        *cupsd_settings,        /* New settings */
                *setting;               /* Current setting */
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
@@ -992,9 +1233,12 @@ _cupsAdminSetServerSettings(
   * Range check input...
   */
 
+  if (!http)
+    http = _cupsConnect();
+
   if (!http || !num_settings || !settings)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, NULL);
+    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
 
     return (0);
   }
@@ -1003,51 +1247,192 @@ _cupsAdminSetServerSettings(
   * Get the cupsd.conf file...
   */
 
-  if ((status = get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
-                               &remote)) == HTTP_OK)
+  if (get_cupsd_conf(http, cg, 0, cupsdconf, sizeof(cupsdconf),
+                     &remote) == HTTP_OK)
   {
     if ((cupsd = cupsFileOpen(cupsdconf, "r")) == NULL)
     {
-      _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+      _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
       return (0);
     }
   }
   else
     return (0);
 
+ /*
+  * Get current settings...
+  */
+
+  if (!cupsAdminGetServerSettings(http, &cupsd_num_settings,
+                                 &cupsd_settings))
+    return (0);
+
+  if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_debug_logging = atoi(val);
+  else
+    old_debug_logging = 0;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: old debug_logging=%d",
+                old_debug_logging));
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_remote_admin = atoi(val);
+  else
+    old_remote_admin = 0;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: old remote_admin=%d",
+                old_remote_admin));
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    remote_any = atoi(val);
+  else
+    remote_any = 0;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: old remote_any=%d",
+                remote_any));
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_remote_printers = atoi(val);
+  else
+    old_remote_printers = 1;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: old remote_printers=%d",
+                old_remote_printers));
+
+  if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_share_printers = atoi(val);
+  else
+    old_share_printers = 0;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: old share_printers=%d",
+                old_share_printers));
+
+  if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_user_cancel_any = atoi(val);
+  else
+    old_user_cancel_any = 0;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d",
+                old_user_cancel_any));
+
+  cupsFreeOptions(cupsd_num_settings, cupsd_settings);
+
  /*
   * Get basic settings...
   */
 
   if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
                            settings)) != NULL)
+  {
     debug_logging = atoi(val);
+
+    if (debug_logging == old_debug_logging)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      debug_logging = -1;
+    }
+  }
   else
-    debug_logging = 0;
+    debug_logging = -1;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: debug_logging=%d",
+                debug_logging));
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings,
+                           settings)) != NULL)
+    remote_any = atoi(val);
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: remote_any=%d",
+                remote_any));
 
   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
                            settings)) != NULL)
+  {
     remote_admin = atoi(val);
+
+    if (remote_admin == old_remote_admin)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      remote_admin = -1;
+    }
+  }
   else
-    remote_admin = 0;
+    remote_admin = -1;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: remote_admin=%d",
+                remote_admin));
 
   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, num_settings,
                            settings)) != NULL)
+  {
     remote_printers = atoi(val);
+
+    if (remote_printers == old_remote_printers)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      remote_printers = -1;
+    }
+  }
   else
-    remote_printers = 1;
+    remote_printers = -1;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: remote_printers=%d",
+                remote_printers));
 
   if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
                            settings)) != NULL)
+  {
     share_printers = atoi(val);
+
+    if (share_printers == old_share_printers)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      share_printers = -1;
+    }
+  }
   else
-    share_printers = 0;
+    share_printers = -1;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: share_printers=%d",
+                share_printers));
 
   if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
                            settings)) != NULL)
+  {
     user_cancel_any = atoi(val);
+
+    if (user_cancel_any == old_user_cancel_any)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      user_cancel_any = -1;
+    }
+  }
   else
-    user_cancel_any = 0;
+    user_cancel_any = -1;
+
+  DEBUG_printf(("1cupsAdminSetServerSettings: user_cancel_any=%d",
+                user_cancel_any));
 
  /*
   * Create a temporary file for the new cupsd.conf file...
@@ -1060,7 +1445,7 @@ _cupsAdminSetServerSettings(
     if (remote)
       unlink(cupsdconf);
 
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+    _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
     return (0);
   }
 
@@ -1086,48 +1471,90 @@ _cupsAdminSetServerSettings(
   wrote_root_location  = 0;
   indent               = 0;
 
+  if ((server_port_env = getenv("SERVER_PORT")) != NULL)
+  {
+    if ((server_port = atoi(server_port_env)) <= 0)
+      server_port = ippPort();
+  }
+  else
+    server_port = ippPort();
+
+  if (server_port <= 0)
+    server_port = IPP_PORT;
+
   while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum))
   {
-    if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
+    if ((!strcasecmp(line, "Port") || !strcasecmp(line, "Listen")) &&
+        (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
     {
       if (!wrote_port_listen)
       {
         wrote_port_listen = 1;
 
-       if (share_printers || remote_admin)
+       if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
        {
          cupsFilePuts(temp, "# Allow remote access\n");
-         cupsFilePrintf(temp, "Port %d\n", ippPort());
+         cupsFilePrintf(temp, "Port %d\n", server_port);
        }
        else
        {
          cupsFilePuts(temp, "# Only listen for connections from the local "
                             "machine.\n");
-         cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
+         cupsFilePrintf(temp, "Listen localhost:%d\n", server_port);
        }
 
 #ifdef CUPS_DEFAULT_DOMAINSOCKET
-        if (!access(CUPS_DEFAULT_DOMAINSOCKET, 0))
+        if ((!value || strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)) &&
+           !access(CUPS_DEFAULT_DOMAINSOCKET, 0))
           cupsFilePuts(temp, "Listen " CUPS_DEFAULT_DOMAINSOCKET "\n");
 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
       }
+      else if (value && value[0] == '/'
+#ifdef CUPS_DEFAULT_DOMAINSOCKET
+               && strcmp(CUPS_DEFAULT_DOMAINSOCKET, value)
+#endif /* CUPS_DEFAULT_DOMAINSOCKET */
+               )
+        cupsFilePrintf(temp, "Listen %s\n", value);
     }
-    else if (!strcasecmp(line, "Browsing") ||
-             !strcasecmp(line, "BrowseAddress") ||
-             !strcasecmp(line, "BrowseAllow") ||
-             !strcasecmp(line, "BrowseDeny") ||
-             !strcasecmp(line, "BrowseOrder"))
+    else if ((!strcasecmp(line, "Browsing") ||
+              !strcasecmp(line, "BrowseAddress") ||
+              !strcasecmp(line, "BrowseAllow") ||
+              !strcasecmp(line, "BrowseDeny") ||
+              !strcasecmp(line, "BrowseLocalProtocols") ||
+              !strcasecmp(line, "BrowseRemoteProtocols") ||
+              !strcasecmp(line, "BrowseOrder")) &&
+            (remote_printers >= 0 || share_printers >= 0))
     {
       if (!wrote_browsing)
       {
+       int new_remote_printers = (remote_printers > 0 ||
+                                  (remote_printers == -1 &&
+                                   old_remote_printers > 0));
+       int new_share_printers = (share_printers > 0 ||
+                                 (share_printers == -1 &&
+                                  old_share_printers > 0));
+
         wrote_browsing = 1;
 
-        if (remote_printers || share_printers)
+        if (new_remote_printers || new_share_printers)
        {
-         if (remote_printers && share_printers)
+         const char *localp = cupsGetOption("BrowseLocalProtocols",
+                                            num_settings, settings);
+         const char *remotep = cupsGetOption("BrowseRemoteProtocols",
+                                             num_settings, settings);
+
+          if (!localp || !localp[0])
+           localp = cupsGetOption("BrowseLocalProtocols", cupsd_num_settings,
+                                  cupsd_settings);
+
+          if (!remotep)
+           remotep = cupsGetOption("BrowseRemoteProtocols", cupsd_num_settings,
+                                   cupsd_settings);
+
+         if (new_remote_printers && new_share_printers)
            cupsFilePuts(temp,
                         "# Enable printer sharing and shared printers.\n");
-         else if (remote_printers)
+         else if (new_remote_printers)
            cupsFilePuts(temp,
                         "# Show shared printers on the local network.\n");
          else
@@ -1137,11 +1564,37 @@ _cupsAdminSetServerSettings(
          cupsFilePuts(temp, "Browsing On\n");
          cupsFilePuts(temp, "BrowseOrder allow,deny\n");
 
-         if (remote_printers)
-           cupsFilePuts(temp, "BrowseAllow @LOCAL\n");
+         if (new_remote_printers)
+         {
+           cupsFilePuts(temp, "BrowseAllow all\n");
+
+           if (!remotep)
+             remotep = CUPS_DEFAULT_BROWSE_REMOTE_PROTOCOLS;
+
+           cupsFilePrintf(temp, "BrowseRemoteProtocols %s\n", remotep);
+          }
+         else
+           cupsFilePuts(temp, "BrowseRemoteProtocols\n");
+
+         cupsd_num_settings = cupsAddOption("BrowseRemoteProtocols", remotep,
+                                            cupsd_num_settings,
+                                            &cupsd_settings);
 
-         if (share_printers)
+         if (new_share_printers)
+         {
            cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
+
+           if (!localp)
+             localp = CUPS_DEFAULT_BROWSE_LOCAL_PROTOCOLS;
+
+           cupsFilePrintf(temp, "BrowseLocalProtocols %s\n", localp);
+         }
+         else
+           cupsFilePuts(temp, "BrowseLocalProtocols\n");
+
+         cupsd_num_settings = cupsAddOption("BrowseLocalProtocols", localp,
+                                            cupsd_num_settings,
+                                            &cupsd_settings);
         }
        else
        {
@@ -1151,7 +1604,7 @@ _cupsAdminSetServerSettings(
        }
       }
     }
-    else if (!strcasecmp(line, "LogLevel"))
+    else if (!strcasecmp(line, "LogLevel") && debug_logging >= 0)
     {
       wrote_loglevel = 1;
 
@@ -1164,7 +1617,7 @@ _cupsAdminSetServerSettings(
       else
       {
         cupsFilePuts(temp, "# Show general information in error_log.\n");
-       cupsFilePuts(temp, "LogLevel info\n");
+       cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
       }
     }
     else if (!strcasecmp(line, "<Policy"))
@@ -1187,8 +1640,8 @@ _cupsAdminSetServerSettings(
                             "cancel a job...\n"
                             "  <Limit Cancel-Job>\n"
                             "    Order deny,allow\n"
-                            "    Allow @SYSTEM\n"
-                            "    Allow @OWNER\n"
+                            "    Require user @OWNER "
+                            CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
                             "  </Limit>\n");
       }
 
@@ -1214,23 +1667,22 @@ _cupsAdminSetServerSettings(
     {
       in_location = 0;
       indent -= 2;
-      if (in_admin_location)
+      if (in_admin_location && remote_admin >= 0)
       {
        wrote_admin_location = 1;
 
        if (remote_admin)
           cupsFilePuts(temp, "  # Allow remote administration...\n");
-       else
+       else if (remote_admin == 0)
           cupsFilePuts(temp, "  # Restrict access to the admin pages...\n");
 
         cupsFilePuts(temp, "  Order allow,deny\n");
 
        if (remote_admin)
-         cupsFilePuts(temp, "  Allow @LOCAL\n");
-       else
-         cupsFilePuts(temp, "  Allow localhost\n");
+         cupsFilePrintf(temp, "  Allow %s\n",
+                        remote_any > 0 ? "all" : "@LOCAL");
       }
-      else if (in_conf_location)
+      else if (in_conf_location && remote_admin >= 0)
       {
        wrote_conf_location = 1;
 
@@ -1244,30 +1696,31 @@ _cupsAdminSetServerSettings(
         cupsFilePuts(temp, "  Order allow,deny\n");
 
        if (remote_admin)
-         cupsFilePuts(temp, "  Allow @LOCAL\n");
-       else
-         cupsFilePuts(temp, "  Allow localhost\n");
+         cupsFilePrintf(temp, "  Allow %s\n",
+                        remote_any > 0 ? "all" : "@LOCAL");
       }
-      else if (in_root_location)
+      else if (in_root_location &&
+               (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
       {
        wrote_root_location = 1;
 
-       if (remote_admin && share_printers)
+       if (remote_admin > 0 && share_printers > 0)
           cupsFilePuts(temp, "  # Allow shared printing and remote "
                             "administration...\n");
-       else if (remote_admin)
+       else if (remote_admin > 0)
           cupsFilePuts(temp, "  # Allow remote administration...\n");
-       else if (share_printers)
+       else if (share_printers > 0)
           cupsFilePuts(temp, "  # Allow shared printing...\n");
+       else if (remote_any > 0)
+          cupsFilePuts(temp, "  # Allow remote access...\n");
        else
           cupsFilePuts(temp, "  # Restrict access to the server...\n");
 
         cupsFilePuts(temp, "  Order allow,deny\n");
 
-       if (remote_admin || share_printers)
-         cupsFilePuts(temp, "  Allow @LOCAL\n");
-       else
-         cupsFilePuts(temp, "  Allow localhost\n");
+       if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
+         cupsFilePrintf(temp, "  Allow %s\n",
+                        remote_any > 0 ? "all" : "@LOCAL");
       }
 
       in_admin_location = 0;
@@ -1276,80 +1729,86 @@ _cupsAdminSetServerSettings(
 
       cupsFilePuts(temp, "</Location>\n");
     }
-    else if (!strcasecmp(line, "<Limit") && in_default_policy)
+    else if (!strcasecmp(line, "<Limit"))
     {
-     /*
-      * See if the policy limit is for the Cancel-Job operation...
-      */
-
-      char     *valptr;                /* Pointer into value */
-
-
-      indent += 2;
-
-      if (!strcasecmp(value, "cancel-job"))
+      if (in_default_policy)
       {
        /*
-       * Don't write anything for this limit section...
+       * See if the policy limit is for the Cancel-Job operation...
        */
-
-       in_cancel_job = 2;
-      }
-      else
-      {
-       cupsFilePrintf(temp, "  %s", line);
-
-       while (*value)
+  
+       char    *valptr;                /* Pointer into value */
+  
+  
+       if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
        {
-         for (valptr = value; !isspace(*valptr & 255) && *valptr; valptr ++);
-
-         if (*valptr)
-           *valptr++ = '\0';
-
-          if (!strcasecmp(value, "cancel-job"))
+        /*
+         * Don't write anything for this limit section...
+         */
+  
+         in_cancel_job = 2;
+       }
+       else
+       {
+         cupsFilePrintf(temp, "%*s%s", indent, "", line);
+  
+         while (*value)
          {
-          /*
-           * Write everything except for this definition...
-           */
-
-           in_cancel_job = 1;
+           for (valptr = value; *valptr && !_cups_isspace(*valptr); valptr ++);
+  
+           if (*valptr)
+             *valptr++ = '\0';
+  
+           if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
+           {
+            /*
+             * Write everything except for this definition...
+             */
+  
+             in_cancel_job = 1;
+           }
+           else
+             cupsFilePrintf(temp, " %s", value);
+  
+           for (value = valptr; _cups_isspace(*value); value ++);
          }
-         else
-           cupsFilePrintf(temp, " %s", value);
-
-          for (value = valptr; isspace(*value & 255); value ++);
+  
+         cupsFilePuts(temp, ">\n");
        }
-
-       cupsFilePuts(temp, ">\n");
       }
+      else
+        cupsFilePrintf(temp, "%*s%s %s>\n", indent, "", line, value);
+
+      indent += 2;
     }
     else if (!strcasecmp(line, "</Limit>") && in_cancel_job)
     {
       indent -= 2;
 
       if (in_cancel_job == 1)
-        cupsFilePuts(temp, "  </Limit>\n");
+       cupsFilePuts(temp, "  </Limit>\n");
 
       wrote_policy = 1;
 
       if (!user_cancel_any)
        cupsFilePuts(temp, "  # Only the owner or an administrator can cancel "
-                          "a job...\n"
-                          "  <Limit Cancel-Job>\n"
-                          "    Order deny,allow\n"
-                          "    Require user @OWNER @SYSTEM\n"
+                          "a job...\n"
+                          "  <Limit Cancel-Job>\n"
+                          "    Order deny,allow\n"
+                          "    Require user @OWNER "
+                          CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
                           "  </Limit>\n");
 
       in_cancel_job = 0;
     }
-    else if ((in_admin_location || in_conf_location || in_root_location) &&
+    else if ((((in_admin_location || in_conf_location || in_root_location) &&
+               (remote_admin >= 0 || remote_any > 0)) ||
+              (in_root_location && share_printers >= 0)) &&
              (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny") ||
              !strcasecmp(line, "Order")))
       continue;
     else if (in_cancel_job == 2)
       continue;
-    else if (!strcasecmp(line, "<Limit")  && value)
-      cupsFilePrintf(temp, "  %s %s>\n", line, value);
     else if (line[0] == '<')
     {
       if (value)
@@ -1366,14 +1825,13 @@ _cupsAdminSetServerSettings(
       }
     }
     else if (!in_policy && !in_location &&
-             (val = cupsGetOption(line, num_settings, settings)) != NULL &&
-             !cupsGetOption(line, cupsd_num_settings, cupsd_settings))
+             (val = cupsGetOption(line, num_settings, settings)) != NULL)
     {
      /*
-      * Add this directive to the list of directives we have written...
+      * Replace this directive's value with the new one...
       */
 
-      cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
+      cupsd_num_settings = cupsAddOption(line, val, cupsd_num_settings,
                                          &cupsd_settings);
 
      /*
@@ -1381,7 +1839,7 @@ _cupsAdminSetServerSettings(
       * only support setting root directives, not in sections...
       */
 
-      cupsFilePrintf(temp, "%s %s\n", line, value);
+      cupsFilePrintf(temp, "%s %s\n", line, val);
     }
     else if (value)
     {
@@ -1390,7 +1848,7 @@ _cupsAdminSetServerSettings(
        /*
         * Record the non-policy, non-location directives that we find
        * in the server settings, since we cache this info and record it
-       * in _cupsAdminGetServerSettings()...
+       * in cupsAdminGetServerSettings()...
        */
 
        cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings,
@@ -1407,13 +1865,13 @@ _cupsAdminSetServerSettings(
   * Write any missing info...
   */
 
-  if (!wrote_browsing)
+  if (!wrote_browsing && (remote_printers >= 0 || share_printers >= 0))
   {
-    if (remote_printers || share_printers)
+    if (remote_printers > 0 || share_printers > 0)
     {
-      if (remote_printers && share_printers)
+      if (remote_printers > 0 && share_printers > 0)
        cupsFilePuts(temp, "# Enable printer sharing and shared printers.\n");
-      else if (remote_printers)
+      else if (remote_printers > 0)
        cupsFilePuts(temp, "# Show shared printers on the local network.\n");
       else
        cupsFilePuts(temp, "# Share local printers on the local network.\n");
@@ -1421,10 +1879,10 @@ _cupsAdminSetServerSettings(
       cupsFilePuts(temp, "Browsing On\n");
       cupsFilePuts(temp, "BrowseOrder allow,deny\n");
 
-      if (remote_printers)
-       cupsFilePuts(temp, "BrowseAllow @LOCAL\n");
+      if (remote_printers > 0)
+       cupsFilePuts(temp, "BrowseAllow all\n");
 
-      if (share_printers)
+      if (share_printers > 0)
        cupsFilePuts(temp, "BrowseAddress @LOCAL\n");
     }
     else
@@ -1434,7 +1892,7 @@ _cupsAdminSetServerSettings(
     }
   }
 
-  if (!wrote_loglevel)
+  if (!wrote_loglevel && debug_logging >= 0)
   {
     if (debug_logging)
     {
@@ -1444,13 +1902,14 @@ _cupsAdminSetServerSettings(
     else
     {
       cupsFilePuts(temp, "# Show general information in error_log.\n");
-      cupsFilePuts(temp, "LogLevel info\n");
+      cupsFilePuts(temp, "LogLevel " CUPS_DEFAULT_LOG_LEVEL "\n");
     }
   }
 
-  if (!wrote_port_listen)
+  if (!wrote_port_listen &&
+      (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
   {
-    if (share_printers || remote_admin)
+    if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
     {
       cupsFilePuts(temp, "# Allow remote access\n");
       cupsFilePrintf(temp, "Port %d\n", ippPort());
@@ -1468,30 +1927,31 @@ _cupsAdminSetServerSettings(
 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
   }
 
-  if (!wrote_root_location)
+  if (!wrote_root_location &&
+      (remote_admin >= 0 || remote_any > 0 || share_printers >= 0))
   {
-    if (remote_admin && share_printers)
+    if (remote_admin > 0 && share_printers > 0)
       cupsFilePuts(temp,
                    "# Allow shared printing and remote administration...\n");
-    else if (remote_admin)
+    else if (remote_admin > 0)
       cupsFilePuts(temp, "# Allow remote administration...\n");
-    else if (share_printers)
+    else if (share_printers > 0)
       cupsFilePuts(temp, "# Allow shared printing...\n");
+    else if (remote_any > 0)
+      cupsFilePuts(temp, "# Allow remote access...\n");
     else
       cupsFilePuts(temp, "# Restrict access to the server...\n");
 
     cupsFilePuts(temp, "<Location />\n"
                        "  Order allow,deny\n");
 
-    if (remote_admin || share_printers)
-      cupsFilePuts(temp, "  Allow @LOCAL\n");
-    else
-      cupsFilePuts(temp, "  Allow localhost\n");
+    if (remote_admin > 0 || remote_any > 0 || share_printers > 0)
+      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
 
     cupsFilePuts(temp, "</Location>\n");
   }
 
-  if (!wrote_admin_location)
+  if (!wrote_admin_location && remote_admin >= 0)
   {
     if (remote_admin)
       cupsFilePuts(temp, "# Allow remote administration...\n");
@@ -1502,14 +1962,12 @@ _cupsAdminSetServerSettings(
                        "  Order allow,deny\n");
 
     if (remote_admin)
-      cupsFilePuts(temp, "  Allow @LOCAL\n");
-    else
-      cupsFilePuts(temp, "  Allow localhost\n");
+      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
 
     cupsFilePuts(temp, "</Location>\n");
   }
 
-  if (!wrote_conf_location)
+  if (!wrote_conf_location && remote_admin >= 0)
   {
     if (remote_admin)
       cupsFilePuts(temp,
@@ -1518,23 +1976,21 @@ _cupsAdminSetServerSettings(
       cupsFilePuts(temp, "# Restrict access to the configuration files...\n");
 
     cupsFilePuts(temp, "<Location /admin/conf>\n"
-                       "  AuthType Basic\n"
+                       "  AuthType Default\n"
                        "  Require user @SYSTEM\n"
                        "  Order allow,deny\n");
 
     if (remote_admin)
-      cupsFilePuts(temp, "  Allow @LOCAL\n");
-    else
-      cupsFilePuts(temp, "  Allow localhost\n");
+      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
 
     cupsFilePuts(temp, "</Location>\n");
   }
 
-  if (!wrote_policy)
+  if (!wrote_policy && user_cancel_any >= 0)
   {
     cupsFilePuts(temp, "<Policy default>\n"
                        "  # Job-related operations must be done by the owner "
-                      "or an adminstrator...\n"
+                      "or an administrator...\n"
                        "  <Limit Send-Document Send-URI Hold-Job Release-Job "
                       "Restart-Job Purge-Jobs Set-Job-Attributes "
                       "Create-Job-Subscription Renew-Subscription "
@@ -1545,7 +2001,7 @@ _cupsAdminSetServerSettings(
                        "    Order deny,allow\n"
                        "  </Limit>\n"
                        "  # All administration operations require an "
-                      "adminstrator to authenticate...\n"
+                      "administrator to authenticate...\n"
                       "  <Limit Pause-Printer Resume-Printer "
                        "Set-Printer-Attributes Enable-Printer "
                       "Disable-Printer Pause-Printer-After-Current-Job "
@@ -1556,7 +2012,7 @@ _cupsAdminSetServerSettings(
                       "CUPS-Add-Class CUPS-Delete-Class "
                       "CUPS-Accept-Jobs CUPS-Reject-Jobs "
                       "CUPS-Set-Default CUPS-Add-Device CUPS-Delete-Device>\n"
-                       "    AuthType Basic\n"
+                       "    AuthType Default\n"
                       "    Require user @SYSTEM\n"
                        "    Order deny,allow\n"
                        "</Limit>\n");
@@ -1565,8 +2021,9 @@ _cupsAdminSetServerSettings(
       cupsFilePuts(temp, "  # Only the owner or an administrator can cancel "
                          "a job...\n"
                         "  <Limit Cancel-Job>\n"
-                        "    Require user @OWNER @SYSTEM\n"
                         "    Order deny,allow\n"
+                        "    Require user @OWNER "
+                        CUPS_DEFAULT_PRINTOPERATOR_AUTH "\n"
                         "  </Limit>\n");
 
     cupsFilePuts(temp, "  <Limit All>\n"
@@ -1577,6 +2034,8 @@ _cupsAdminSetServerSettings(
 
   for (i = num_settings, setting = settings; i > 0; i --, setting ++)
     if (setting->name[0] != '_' &&
+        strcasecmp(setting->name, "Listen") &&
+       strcasecmp(setting->name, "Port") &&
         !cupsGetOption(setting->name, cupsd_num_settings, cupsd_settings))
     {
      /*
@@ -1587,8 +2046,8 @@ _cupsAdminSetServerSettings(
                                          cupsd_num_settings, &cupsd_settings);
 
      /*
-      * Write the new value in its place, without indentation since we
-      * only support setting root directives, not in sections...
+      * Write the new value, without indentation since we only support
+      * setting root directives, not in sections...
       */
 
       cupsFilePrintf(temp, "%s %s\n", setting->name, setting->value);
@@ -1609,22 +2068,55 @@ _cupsAdminSetServerSettings(
     * Updated OK, add the basic settings...
     */
 
-    cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
-                                       debug_logging ? "1" : "0",
-                                      cupsd_num_settings, &cupsd_settings);
-    cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
-                                       remote_admin ? "1" : "0",
-                                      cupsd_num_settings, &cupsd_settings);
-    cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
-                                       remote_printers ? "1" : "0",
-                                      cupsd_num_settings, &cupsd_settings);
-    cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
-                                       share_printers ? "1" : "0",
-                                      cupsd_num_settings, &cupsd_settings);
-    cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
-                                       user_cancel_any ? "1" : "0",
+    if (debug_logging >= 0)
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
+                                        debug_logging ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+    else
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
+                                        old_debug_logging ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+
+    if (remote_admin >= 0)
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
+                                        remote_admin ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+    else
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
+                                        old_remote_admin ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+
+    cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
+                                       remote_any ? "1" : "0",
                                       cupsd_num_settings, &cupsd_settings);
 
+    if (remote_printers >= 0)
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
+                                        remote_printers ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+    else
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
+                                        old_remote_printers ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+
+    if (share_printers >= 0)
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
+                                        share_printers ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+    else
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
+                                        old_share_printers ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+
+    if (user_cancel_any >= 0)
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
+                                        user_cancel_any ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+    else
+      cupsd_num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
+                                        old_user_cancel_any ? "1" : "0",
+                                        cupsd_num_settings, &cupsd_settings);
+
    /*
     * Save the new values...
     */
@@ -1664,13 +2156,17 @@ do_samba_command(const char *command,   /* I - Command to run */
                 const char *authfile,  /* I - Samba authentication file */
                 FILE *logfile)         /* I - Optional log file */
 {
+#ifdef WIN32
+  return (1);                          /* Always fail on Windows... */
+
+#else
   int          status;                 /* Status of command */
   int          pid;                    /* Process ID of child */
 
 
   if (logfile)
     _cupsLangPrintf(logfile,
-                    _("Running command: %s %s -N -A %s -c \'%s\'\n"),
+                    _("Running command: %s %s -N -A %s -c \'%s\'"),
                    command, address, authfile, subcmd);
 
   if ((pid = fork()) == 0)
@@ -1679,18 +2175,23 @@ do_samba_command(const char *command,   /* I - Command to run */
     * Child goes here, redirect stdin/out/err and execute the command...
     */
 
-    close(0);
-    open("/dev/null", O_RDONLY);
+    int fd = open("/dev/null", O_RDONLY);
 
-    close(1);
+    if (fd > 0)
+    {
+      dup2(fd, 0);
+      close(fd);
+    }
 
     if (logfile)
-      dup(fileno(logfile));
-    else
-      open("/dev/null", O_WRONLY);
+      dup2(fileno(logfile), 1);
+    else if ((fd = open("/dev/null", O_WRONLY)) > 1)
+    {
+      dup2(fd, 1);
+      close(fd);
+    }
 
-    close(2);
-    dup(1);
+    dup2(1, 2);
 
     execlp(command, command, address, "-N", "-A", authfile, "-c", subcmd,
            (char *)0);
@@ -1701,7 +2202,7 @@ do_samba_command(const char *command,     /* I - Command to run */
     status = -1;
 
     if (logfile)
-      _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s\n"),
+      _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s"),
                       command, strerror(errno));
   }
   else
@@ -1714,14 +2215,15 @@ do_samba_command(const char *command,   /* I - Command to run */
   }
 
   if (logfile)
-    _cupsLangPuts(logfile, "\n");
+    _cupsLangPuts(logfile, "");
 
-  DEBUG_printf(("status=%d\n", status));
+  DEBUG_printf(("9do_samba_command: status=%d", status));
 
   if (WIFEXITED(status))
     return (WEXITSTATUS(status));
   else
     return (-WTERMSIG(status));
+#endif /* WIN32 */
 }
 
 
@@ -1739,7 +2241,9 @@ get_cupsd_conf(
     int             *remote)           /* O - Remote file? */
 {
   int          fd;                     /* Temporary file descriptor */
+#ifndef WIN32
   struct stat  info;                   /* cupsd.conf file information */
+#endif /* WIN32 */
   http_status_t        status;                 /* Status of getting cupsd.conf */
   char         host[HTTP_MAX_HOST];    /* Hostname for connection */
 
@@ -1756,6 +2260,7 @@ get_cupsd_conf(
   snprintf(name, namesize, "%s/cupsd.conf", cg->cups_serverroot);
   *remote = 0;
 
+#ifndef WIN32
   if (!strcasecmp(host, "localhost") && !access(name, R_OK))
   {
    /*
@@ -1770,7 +2275,7 @@ get_cupsd_conf(
       snprintf(message, sizeof(message),
                _cupsLangString(cupsLangDefault(), _("stat of %s failed: %s")),
                name, strerror(errno));
-      _cupsSetError(IPP_INTERNAL_ERROR, message);
+      _cupsSetError(IPP_INTERNAL_ERROR, message, 0);
 
       *name = '\0';
 
@@ -1782,6 +2287,7 @@ get_cupsd_conf(
       status = HTTP_OK;
   }
   else
+#endif /* !WIN32 */
   {
    /*
     * Read cupsd.conf via a HTTP GET request...
@@ -1791,7 +2297,7 @@ get_cupsd_conf(
     {
       *name = '\0';
 
-      _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
+      _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
 
       invalidate_cupsd_cache(cg);
 
@@ -1948,5 +2454,5 @@ write_option(cups_file_t     *dstfp,      /* I - PPD file */
 
 
 /*
- * End of "$Id: adminutil.c 5878 2006-08-24 15:55:42Z mike $".
+ * End of "$Id: adminutil.c 7850 2008-08-20 00:07:25Z mike $".
  */