]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/adminutil.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / adminutil.c
index 548deafa0af8688b404e3d322dc34a79cbab4485..2bd6caf5fc389e2babe9a6a72d6dd1a4150d2759 100644 (file)
@@ -1,13 +1,10 @@
 /*
- * "$Id$"
+ * "$Id: adminutil.c 6378 2007-03-21 07:18:18Z mike $"
  *
  *   Administration utility API definitions for the Common UNIX Printing
  *   System (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 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
  *
  *   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.
+ *   cupsAdminGetServerSettings()  - Get settings from the server.
+ *   _cupsAdminGetServerSettings() - Get settings from the server (private).
+ *   cupsAdminSetServerSettings()  - Set settings on the server.
+ *   _cupsAdminSetServerSettings() - Set settings on the server (private).
  *   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.
 #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 */
 
 
 /*
  * Local functions...
  */
 
+extern int             _cupsAdminGetServerSettings(http_t *http,
+                                                   int *num_settings,
+                                                   cups_option_t **settings);
+extern int             _cupsAdminSetServerSettings(http_t *http,
+                                                   int num_settings,
+                                                   cups_option_t *settings);
 static int             do_samba_command(const char *command,
                                         const char *address,
                                         const char *subcommand,
@@ -103,7 +111,7 @@ cupsAdminCreateWindowsPPD(
                        linenum;        /* Current line number */
   time_t               curtime;        /* Current time */
   struct tm            *curdate;       /* Current date */
-  static const char    *pattrs[] =     /* Printer attributes we want */
+  static const char * const pattrs[] = /* Printer attributes we want */
                        {
                          "job-hold-until-supported",
                          "job-hold-until-default",
@@ -222,18 +230,18 @@ cupsAdminCreateWindowsPPD(
       */
 
       cupsFilePrintf(dstfp, "*%% Commented out for CUPS Windows Driver...\n"
-                            "*%%%s", line + 1);
+                            "*%%%s\n", line + 1);
       continue;
     }
     else if (!strncmp(line, "*JCLOpenUI", 10))
     {
       jcloption = 1;
-      cupsFilePuts(dstfp, line);
+      cupsFilePrintf(dstfp, "%s\n", line);
     }
     else if (!strncmp(line, "*JCLCloseUI", 11))
     {
       jcloption = 0;
-      cupsFilePuts(dstfp, line);
+      cupsFilePrintf(dstfp, "%s\n", line);
     }
     else if (jcloption &&
              strncmp(line, "*End", 4) &&
@@ -243,7 +251,7 @@ cupsAdminCreateWindowsPPD(
       if ((ptr = strchr(line, ':')) == NULL)
       {
         snprintf(line, sizeof(line),
-                 _cupsLangString(language, _("Missing value on line %d!\n")),
+                 _cupsLangString(language, _("Missing value on line %d!")),
                  linenum);
         _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
 
@@ -262,7 +270,7 @@ 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);
 
@@ -281,7 +289,7 @@ cupsAdminCreateWindowsPPD(
       {
         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);
 
@@ -312,17 +320,33 @@ cupsAdminCreateWindowsPPD(
       }
 
       snprintf(ptr + 1, sizeof(line) - (ptr - line + 1),
-               "%%cupsJobTicket: %s=%s\n\"\n*End\n", option, choice);
+               "%%cupsJobTicket: %s=%s\n\"\n*End", option, choice);
 
-      cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s", line);
+      cupsFilePrintf(dstfp, "*%% Changed for CUPS Windows Driver...\n%s\n",
+                     line);
     }
     else
-      cupsFilePuts(dstfp, line);
+      cupsFilePrintf(dstfp, "%s\n", line);
   }
 
   cupsFileClose(srcfp);
   unlink(src);
 
+  if (linenum == 0)
+  {
+    snprintf(line, sizeof(line),
+             _cupsLangString(language, _("Empty PPD file!")),
+             linenum);
+    _cupsSetError(IPP_DOCUMENT_FORMAT_ERROR, line);
+
+    cupsFileClose(dstfp);
+    unlink(buffer);
+
+    *buffer = '\0';
+
+    return (NULL);
+  }
+
  /*
   * Now add the CUPS-specific attributes and options...
   */
@@ -655,7 +679,7 @@ 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);
@@ -674,6 +698,25 @@ cupsAdminExportSamba(
 }
 
 
+/*
+ * 'cupsAdminGetServerSettings()' - Get settings from the server.
+ *
+ * The returned settings should be freed with cupsFreeOptions() when
+ * you are done with them.
+ *
+ * @since CUPS 1.3@
+ */
+
+int                                    /* O - 1 on success, 0 on failure */
+cupsAdminGetServerSettings(
+    http_t        *http,               /* I - Connection to server */
+    int           *num_settings,       /* O - Number of settings */
+    cups_option_t **settings)          /* O - Settings */
+{
+  return (_cupsAdminGetServerSettings(http, num_settings, settings));
+}
+
+
 /*
  * '_cupsAdminGetServerSettings()' - Get settings from the server.
  *
@@ -749,6 +792,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? */
@@ -771,19 +815,31 @@ _cupsAdminGetServerSettings(
       if (!value)
         continue;
 
-      if (!strcasecmp(line, "Port"))
-      {
-       remote_access = 1;
-      }
-      else if (!strcasecmp(line, "Listen"))
+      if (!strcasecmp(line, "Port") || !strcasecmp(line, "Listen"))
       {
        char    *port;                  /* Pointer to port number, if any */
 
 
        if ((port = strrchr(value, ':')) != NULL)
          *port = '\0';
+       else if (isdigit(*value & 255))
+       {
+        /*
+         * Listen on a port number implies remote access...
+         */
+
+         remote_access = 1;
+         continue;
+       }
 
-       if (strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1"))
+       if (strcasecmp(value, "localhost") && strcmp(value, "127.0.0.1")
+#ifdef AF_LOCAL
+            && *value != '/'
+#endif /* AF_LOCAL */
+#ifdef AF_INET6
+            && strcmp(value, "::1")
+#endif /* AF_INET6 */
+           )
          remote_access = 1;
       }
       else if (!strcasecmp(line, "Browsing"))
@@ -859,9 +915,19 @@ _cupsAdminGetServerSettings(
        in_location       = 0;
       }
       else if (!strcasecmp(line, "Allow") && in_admin_location &&
-               strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1"))
+               strcasecmp(value, "localhost") && strcasecmp(value, "127.0.0.1")
+#ifdef AF_LOCAL
+              && *value != '/'
+#endif /* AF_LOCAL */
+#ifdef AF_INET6
+              && strcmp(value, "::1")
+#endif /* AF_INET6 */
+              )
       {
        remote_admin = 1;
+
+       if (!strcasecmp(value, "all"))
+         remote_any = 1;
       }
       else if (line[0] != '<' && !in_location && !in_policy)
         cg->cupsd_num_settings = cupsAddOption(line, value,
@@ -882,6 +948,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",
@@ -919,6 +990,22 @@ _cupsAdminGetServerSettings(
 }
 
 
+/*
+ * 'cupsAdminSetServerSettings()' - Set settings on the server.
+ *
+ * @since CUPS 1.3@
+ */
+
+int                                    /* O - 1 on success, 0 on failure */
+cupsAdminSetServerSettings(
+    http_t        *http,               /* I - Connection to server */
+    int           num_settings,                /* I - Number of settings */
+    cups_option_t *settings)           /* I - Settings */
+{
+  return (_cupsAdminSetServerSettings(http, num_settings, settings));
+}
+
+
 /*
  * '_cupsAdminSetServerSettings()' - Set settings on the server.
  *
@@ -933,6 +1020,8 @@ _cupsAdminSetServerSettings(
 {
   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? */
@@ -952,6 +1041,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? */
@@ -963,6 +1053,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 */
@@ -995,39 +1090,144 @@ _cupsAdminSetServerSettings(
   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;
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_remote_admin = atoi(val);
+  else
+    old_remote_admin = 0;
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    remote_any = atoi(val);
+  else
+    remote_any = 0;
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_remote_printers = atoi(val);
+  else
+    old_remote_printers = 1;
+
+  if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, cupsd_num_settings,
+                           cupsd_settings)) != NULL)
+    old_share_printers = atoi(val);
+  else
+    old_share_printers = 0;
+
+  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;
+
+  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;
+
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings,
+                           settings)) != NULL)
+    remote_any = atoi(val);
 
   if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
                            settings)) != NULL)
+  {
     remote_admin = atoi(val);
+
+    if (remote_admin == old_remote_admin && remote_any < 0)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      remote_admin = -1;
+    }
+  }
   else
-    remote_admin = 0;
+    remote_admin = -1;
 
   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;
 
   if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
                            settings)) != NULL)
+  {
     share_printers = atoi(val);
+
+    if (share_printers == old_share_printers && remote_any < 0)
+    {
+     /*
+      * No change to this setting...
+      */
+
+      share_printers = -1;
+    }
+  }
   else
-    share_printers = 0;
+    share_printers = -1;
 
   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;
 
  /*
   * Create a temporary file for the new cupsd.conf file...
@@ -1066,48 +1266,68 @@ _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")) &&
+        (share_printers >= 0 || remote_admin >= 0))
     {
       if (!wrote_port_listen)
       {
         wrote_port_listen = 1;
 
-       if (share_printers || remote_admin)
+       if (share_printers > 0 || remote_admin > 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 127.0.0.1:%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, "BrowseOrder")) &&
+            (remote_printers >= 0 || share_printers >= 0))
     {
       if (!wrote_browsing)
       {
         wrote_browsing = 1;
 
-        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
@@ -1117,10 +1337,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
@@ -1131,7 +1351,7 @@ _cupsAdminSetServerSettings(
        }
       }
     }
-    else if (!strcasecmp(line, "LogLevel"))
+    else if (!strcasecmp(line, "LogLevel") && debug_logging >= 0)
     {
       wrote_loglevel = 1;
 
@@ -1194,23 +1414,24 @@ _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");
+         cupsFilePrintf(temp, "  Allow %s\n",
+                        remote_any > 0 ? "all" : "@LOCAL");
        else
          cupsFilePuts(temp, "  Allow localhost\n");
       }
-      else if (in_conf_location)
+      else if (in_conf_location && remote_admin >= 0)
       {
        wrote_conf_location = 1;
 
@@ -1224,28 +1445,30 @@ _cupsAdminSetServerSettings(
         cupsFilePuts(temp, "  Order allow,deny\n");
 
        if (remote_admin)
-         cupsFilePuts(temp, "  Allow @LOCAL\n");
+         cupsFilePrintf(temp, "  Allow %s\n",
+                        remote_any > 0 ? "all" : "@LOCAL");
        else
          cupsFilePuts(temp, "  Allow localhost\n");
       }
-      else if (in_root_location)
+      else if (in_root_location && (remote_admin >= 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
           cupsFilePuts(temp, "  # Restrict access to the server...\n");
 
         cupsFilePuts(temp, "  Order allow,deny\n");
 
-       if (remote_admin || share_printers)
-         cupsFilePuts(temp, "  Allow @LOCAL\n");
+       if (remote_admin > 0 || share_printers > 0)
+         cupsFilePrintf(temp, "  Allow %s\n",
+                        remote_any > 0 ? "all" : "@LOCAL");
        else
          cupsFilePuts(temp, "  Allow localhost\n");
       }
@@ -1267,7 +1490,7 @@ _cupsAdminSetServerSettings(
 
       indent += 2;
 
-      if (!strcasecmp(value, "cancel-job"))
+      if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
       {
        /*
        * Don't write anything for this limit section...
@@ -1286,7 +1509,7 @@ _cupsAdminSetServerSettings(
          if (*valptr)
            *valptr++ = '\0';
 
-          if (!strcasecmp(value, "cancel-job"))
+          if (!strcasecmp(value, "cancel-job") && user_cancel_any >= 0)
          {
           /*
            * Write everything except for this definition...
@@ -1322,7 +1545,9 @@ _cupsAdminSetServerSettings(
 
       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) ||
+              (in_root_location && share_printers >= 0)) &&
              (!strcasecmp(line, "Allow") || !strcasecmp(line, "Deny") ||
              !strcasecmp(line, "Order")))
       continue;
@@ -1346,14 +1571,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);
 
      /*
@@ -1361,7 +1585,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)
     {
@@ -1387,13 +1611,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");
@@ -1401,10 +1625,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
@@ -1414,7 +1638,7 @@ _cupsAdminSetServerSettings(
     }
   }
 
-  if (!wrote_loglevel)
+  if (!wrote_loglevel && debug_logging >= 0)
   {
     if (debug_logging)
     {
@@ -1428,9 +1652,9 @@ _cupsAdminSetServerSettings(
     }
   }
 
-  if (!wrote_port_listen)
+  if (!wrote_port_listen && (share_printers >= 0 || remote_admin >= 0))
   {
-    if (share_printers || remote_admin)
+    if (share_printers > 0 || remote_admin > 0)
     {
       cupsFilePuts(temp, "# Allow remote access\n");
       cupsFilePrintf(temp, "Port %d\n", ippPort());
@@ -1439,7 +1663,7 @@ _cupsAdminSetServerSettings(
     {
       cupsFilePuts(temp,
                    "# Only listen for connections from the local machine.\n");
-      cupsFilePrintf(temp, "Listen 127.0.0.1:%d\n", ippPort());
+      cupsFilePrintf(temp, "Listen localhost:%d\n", ippPort());
     }
 
 #ifdef CUPS_DEFAULT_DOMAINSOCKET
@@ -1448,14 +1672,14 @@ _cupsAdminSetServerSettings(
 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
   }
 
-  if (!wrote_root_location)
+  if (!wrote_root_location && (remote_admin >= 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
       cupsFilePuts(temp, "# Restrict access to the server...\n");
@@ -1463,15 +1687,15 @@ _cupsAdminSetServerSettings(
     cupsFilePuts(temp, "<Location />\n"
                        "  Order allow,deny\n");
 
-    if (remote_admin || share_printers)
-      cupsFilePuts(temp, "  Allow @LOCAL\n");
+    if (remote_admin > 0 || share_printers > 0)
+      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
     else
       cupsFilePuts(temp, "  Allow localhost\n");
 
     cupsFilePuts(temp, "</Location>\n");
   }
 
-  if (!wrote_admin_location)
+  if (!wrote_admin_location && remote_admin >= 0)
   {
     if (remote_admin)
       cupsFilePuts(temp, "# Allow remote administration...\n");
@@ -1482,14 +1706,14 @@ _cupsAdminSetServerSettings(
                        "  Order allow,deny\n");
 
     if (remote_admin)
-      cupsFilePuts(temp, "  Allow @LOCAL\n");
+      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
     else
       cupsFilePuts(temp, "  Allow localhost\n");
 
     cupsFilePuts(temp, "</Location>\n");
   }
 
-  if (!wrote_conf_location)
+  if (!wrote_conf_location && remote_admin >= 0)
   {
     if (remote_admin)
       cupsFilePuts(temp,
@@ -1503,14 +1727,14 @@ _cupsAdminSetServerSettings(
                        "  Order allow,deny\n");
 
     if (remote_admin)
-      cupsFilePuts(temp, "  Allow @LOCAL\n");
+      cupsFilePrintf(temp, "  Allow %s\n", remote_any > 0 ? "all" : "@LOCAL");
     else
       cupsFilePuts(temp, "  Allow localhost\n");
 
     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 "
@@ -1567,8 +1791,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);
@@ -1589,22 +1813,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...
     */
@@ -1644,6 +1901,10 @@ 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 */
 
@@ -1680,8 +1941,9 @@ do_samba_command(const char *command,     /* I - Command to run */
   {
     status = -1;
 
-    _cupsLangPrintf(stderr, _("cupsaddsmb: Unable to run \"%s\": %s\n"),
-                    command, strerror(errno));
+    if (logfile)
+      _cupsLangPrintf(logfile, _("Unable to run \"%s\": %s\n"),
+                      command, strerror(errno));
   }
   else
   {
@@ -1701,6 +1963,7 @@ do_samba_command(const char *command,     /* I - Command to run */
     return (WEXITSTATUS(status));
   else
     return (-WTERMSIG(status));
+#endif /* WIN32 */
 }
 
 
@@ -1718,7 +1981,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 */
 
@@ -1735,6 +2000,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))
   {
    /*
@@ -1761,12 +2027,13 @@ get_cupsd_conf(
       status = HTTP_OK;
   }
   else
+#endif /* !WIN32 */
   {
    /*
     * Read cupsd.conf via a HTTP GET request...
     */
 
-    if ((fd = cupsTempFd(name, sizeof(name))) < 0)
+    if ((fd = cupsTempFd(name, namesize)) < 0)
     {
       *name = '\0';
 
@@ -1927,5 +2194,5 @@ write_option(cups_file_t     *dstfp,      /* I - PPD file */
 
 
 /*
- * End of "$Id$".
+ * End of "$Id: adminutil.c 6378 2007-03-21 07:18:18Z mike $".
  */