]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/admin.c
Merge pull request #5621 from zdohnal/cgigetarray-sigsegv
[thirdparty/cups.git] / cgi-bin / admin.c
index a3d4d7f3b9263d285edfd66502cf6d4ccf196cd5..f087809f7cf1e3d6298cf84daed90fb844869f0e 100644 (file)
@@ -1,41 +1,11 @@
 /*
- * "$Id: admin.c 5360 2006-03-30 17:02:17Z mike $"
+ * Administration CGI for CUPS.
  *
- *   Administration CGI for the Common UNIX Printing System (CUPS).
+ * Copyright © 2007-2019 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products.
  *
- *   Copyright 1997-2006 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
- *
- * Contents:
- *
- *   main()                    - Main entry for CGI.
- *   do_am_class()             - Add or modify a class.
- *   do_am_printer()           - Add or modify a printer.
- *   do_config_printer()       - Configure the default options for a printer.
- *   do_config_server()        - Configure server settings.
- *   do_delete_class()         - Delete a class...
- *   do_delete_printer()       - Delete a printer...
- *   do_export()               - Export printers to Samba...
- *   do_menu()                 - Show the main menu...
- *   do_printer_op()           - Do a printer operation.
- *   do_set_allowed_users()    - Set the allowed/denied users for a queue.
- *   do_set_sharing()          - Set printer-is-shared value...
- *   match_string()            - Return the number of matching characters.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
  */
 
 #include "cgi-private.h"
+#include <cups/http-private.h>
+#include <cups/ppd-private.h>
 #include <cups/adminutil.h>
-#include <cups/file.h>
+#include <cups/ppd.h>
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/wait.h>
+#include <limits.h>
+
+
+/*
+ * Local globals...
+ */
+
+static int     current_device = 0;     /* Current device shown */
 
 
 /*
  * Local functions...
  */
 
+static void    choose_device_cb(const char *device_class, const char *device_id, const char *device_info, const char *device_make_and_model, const char *device_uri, const char *device_location, const char *title);
 static void    do_am_class(http_t *http, int modify);
 static void    do_am_printer(http_t *http, int modify);
-static void    do_config_printer(http_t *http);
 static void    do_config_server(http_t *http);
 static void    do_delete_class(http_t *http);
 static void    do_delete_printer(http_t *http);
-static void    do_export(http_t *http);
+static void    do_list_printers(http_t *http);
 static void    do_menu(http_t *http);
-static void    do_printer_op(http_t *http,
-                             ipp_op_t op, const char *title);
 static void    do_set_allowed_users(http_t *http);
+static void    do_set_default(http_t *http);
+static void    do_set_options(http_t *http, int is_class);
 static void    do_set_sharing(http_t *http);
-static int     match_string(const char *a, const char *b);
+static char    *get_option_value(ppd_file_t *ppd, const char *name,
+                                 char *buffer, size_t bufsize);
+static double  get_points(double number, const char *uval);
+static char    *get_printer_ppd(const char *uri, char *buffer, size_t bufsize);
 
 
 /*
@@ -75,8 +58,7 @@ static int    match_string(const char *a, const char *b);
  */
 
 int                                    /* O - Exit status */
-main(int  argc,                                /* I - Number of command-line arguments */
-     char *argv[])                     /* I - Command-line arguments */
+main(void)
 {
   http_t       *http;                  /* Connection to the server */
   const char   *op;                    /* Operation name */
@@ -93,7 +75,8 @@ main(int  argc,                               /* I - Number of command-line arguments */
   if (!http)
   {
     perror("ERROR: Unable to connect to cupsd");
-    fprintf(stderr, "DEBUG: cupsServer()=\"%s\"\n", cupsServer());
+    fprintf(stderr, "DEBUG: cupsServer()=\"%s\"\n",
+            cupsServer() ? cupsServer() : "(null)");
     fprintf(stderr, "DEBUG: ippPort()=%d\n", ippPort());
     fprintf(stderr, "DEBUG: cupsEncryption()=%d\n", cupsEncryption());
     exit(1);
@@ -106,12 +89,13 @@ main(int  argc,                            /* I - Number of command-line arguments */
   */
 
   cgiSetVariable("SECTION", "admin");
+  cgiSetVariable("REFRESH_PAGE", "");
 
  /*
   * See if we have form data...
   */
 
-  if (!cgiInitialize())
+  if (!cgiInitialize() || !cgiGetVariable("OP"))
   {
    /*
     * Nope, send the administration menu...
@@ -121,7 +105,7 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
     do_menu(http);
   }
-  else if ((op = cgiGetVariable("OP")) != NULL)
+  else if ((op = cgiGetVariable("OP")) != NULL && cgiIsPOST())
   {
    /*
     * Do the operation...
@@ -129,44 +113,38 @@ main(int  argc,                           /* I - Number of command-line arguments */
 
     fprintf(stderr, "DEBUG: op=\"%s\"...\n", op);
 
-    if (!strcmp(op, "redirect"))
+    if (!*op)
     {
-      const char *url;                 /* Redirection URL... */
-      char     prefix[1024];           /* URL prefix */
-
-
-      if (getenv("HTTPS"))
-        snprintf(prefix, sizeof(prefix), "https://%s:%s",
-                getenv("SERVER_NAME"), getenv("SERVER_PORT"));
+      const char *printer = getenv("PRINTER_NAME"),
+                                       /* Printer or class name */
+               *server_port = getenv("SERVER_PORT");
+                                       /* Port number string */
+      int      port = atoi(server_port ? server_port : "0");
+                                       /* Port number */
+      char     uri[1024];              /* URL */
+
+      if (printer)
+        httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri),
+                        getenv("HTTPS") ? "https" : "http", NULL,
+                        getenv("SERVER_NAME"), port, "/%s/%s",
+                        cgiGetVariable("IS_CLASS") ? "classes" : "printers",
+                        printer);
       else
-        snprintf(prefix, sizeof(prefix), "http://%s:%s",
-                getenv("SERVER_NAME"), getenv("SERVER_PORT"));
+        httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri),
+                       getenv("HTTPS") ? "https" : "http", NULL,
+                       getenv("SERVER_NAME"), port, "/admin");
 
-      if ((url = cgiGetVariable("URL")) != NULL)
-        printf("Location: %s%s\n\n", prefix, url);
-      else
-        printf("Location: %s/admin\n\n", prefix);
-    }
-    else if (!strcmp(op, "start-printer"))
-      do_printer_op(http, IPP_RESUME_PRINTER, cgiText(_("Start Printer")));
-    else if (!strcmp(op, "stop-printer"))
-      do_printer_op(http, IPP_PAUSE_PRINTER, cgiText(_("Stop Printer")));
-    else if (!strcmp(op, "start-class"))
-      do_printer_op(http, IPP_RESUME_PRINTER, cgiText(_("Start Class")));
-    else if (!strcmp(op, "stop-class"))
-      do_printer_op(http, IPP_PAUSE_PRINTER, cgiText(_("Stop Class")));
-    else if (!strcmp(op, "accept-jobs"))
-      do_printer_op(http, CUPS_ACCEPT_JOBS, cgiText(_("Accept Jobs")));
-    else if (!strcmp(op, "reject-jobs"))
-      do_printer_op(http, CUPS_REJECT_JOBS, cgiText(_("Reject Jobs")));
-    else if (!strcmp(op, "purge-jobs"))
-      do_printer_op(http, IPP_PURGE_JOBS, cgiText(_("Purge Jobs")));
+      printf("Location: %s\n\n", uri);
+    }
     else if (!strcmp(op, "set-allowed-users"))
       do_set_allowed_users(http);
     else if (!strcmp(op, "set-as-default"))
-      do_printer_op(http, CUPS_SET_DEFAULT, cgiText(_("Set As Default")));
+      do_set_default(http);
     else if (!strcmp(op, "set-sharing"))
       do_set_sharing(http);
+    else if (!strcmp(op, "find-new-printers") ||
+             !strcmp(op, "list-available-printers"))
+      do_list_printers(http);
     else if (!strcmp(op, "add-class"))
       do_am_class(http, 0);
     else if (!strcmp(op, "add-printer"))
@@ -179,16 +157,16 @@ main(int  argc,                           /* I - Number of command-line arguments */
       do_delete_class(http);
     else if (!strcmp(op, "delete-printer"))
       do_delete_printer(http);
+    else if (!strcmp(op, "set-class-options"))
+      do_set_options(http, 1);
     else if (!strcmp(op, "set-printer-options"))
-      do_config_printer(http);
+      do_set_options(http, 0);
     else if (!strcmp(op, "config-server"))
       do_config_server(http);
-    else if (!strcmp(op, "export-samba"))
-      do_export(http);
     else
     {
      /*
-      * Bad operation code...  Display an error...
+      * Bad operation code - display an error...
       */
 
       cgiStartHTML(cgiText(_("Administration")));
@@ -196,10 +174,73 @@ main(int  argc,                           /* I - Number of command-line arguments */
       cgiEndHTML();
     }
   }
+  else if (op && !strcmp(op, "redirect"))
+  {
+    const char *url;                   /* Redirection URL... */
+    char       prefix[1024];           /* URL prefix */
+
+
+    if (getenv("HTTPS"))
+      snprintf(prefix, sizeof(prefix), "https://%s:%s",
+              getenv("SERVER_NAME"), getenv("SERVER_PORT"));
+    else
+      snprintf(prefix, sizeof(prefix), "http://%s:%s",
+              getenv("SERVER_NAME"), getenv("SERVER_PORT"));
+
+    fprintf(stderr, "DEBUG: redirecting with prefix %s!\n", prefix);
+
+    if ((url = cgiGetVariable("URL")) != NULL)
+    {
+      char     encoded[1024],          /* Encoded URL string */
+               *ptr;                   /* Pointer into encoded string */
+
+
+      ptr = encoded;
+      if (*url != '/')
+        *ptr++ = '/';
+
+      for (; *url && ptr < (encoded + sizeof(encoded) - 4); url ++)
+      {
+        if (strchr("%@&+ <>#=", *url) || *url < ' ' || *url & 128)
+       {
+        /*
+         * Percent-encode this character; safe because we have at least 4
+         * bytes left in the array...
+         */
+
+         sprintf(ptr, "%%%02X", *url & 255);
+         ptr += 3;
+       }
+       else
+         *ptr++ = *url;
+      }
+
+      *ptr = '\0';
+
+      if (*url)
+      {
+       /*
+        * URL was too long, just redirect to the admin page...
+       */
+
+       printf("Location: %s/admin\n\n", prefix);
+      }
+      else
+      {
+       /*
+        * URL is OK, redirect there...
+       */
+
+        printf("Location: %s%s\n\n", prefix, encoded);
+      }
+    }
+    else
+      printf("Location: %s/admin\n\n", prefix);
+  }
   else
   {
    /*
-    * Form data but no operation code...  Display an error...
+    * Form data but no operation code - display an error...
     */
 
     cgiStartHTML(cgiText(_("Administration")));
@@ -221,6 +262,50 @@ main(int  argc,                            /* I - Number of command-line arguments */
 }
 
 
+/*
+ * 'choose_device_cb()' - Add a device to the device selection page.
+ */
+
+static void
+choose_device_cb(
+    const char *device_class,          /* I - Class */
+    const char *device_id,             /* I - 1284 device ID */
+    const char *device_info,           /* I - Description */
+    const char *device_make_and_model, /* I - Make and model */
+    const char *device_uri,            /* I - Device URI */
+    const char *device_location,       /* I - Location */
+    const char *title)                 /* I - Page title */
+{
+ /*
+  * For modern browsers, start a multi-part page so we can show that something
+  * is happening.  Non-modern browsers just get everything at the end...
+  */
+
+  if (current_device == 0 && cgiSupportsMultipart())
+  {
+    cgiStartMultipart();
+    cgiStartHTML(title);
+    cgiCopyTemplateLang("choose-device.tmpl");
+    cgiEndHTML();
+    fflush(stdout);
+  }
+
+
+ /*
+  * Add the device to the array...
+  */
+
+  cgiSetArray("device_class", current_device, device_class);
+  cgiSetArray("device_id", current_device, device_id);
+  cgiSetArray("device_info", current_device, device_info);
+  cgiSetArray("device_make_and_model", current_device, device_make_and_model);
+  cgiSetArray("device_uri", current_device, device_uri);
+  cgiSetArray("device_location", current_device, device_location);
+
+  current_device ++;
+}
+
+
 /*
  * 'do_am_class()' - Add or modify a class.
  */
@@ -237,6 +322,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
   ipp_attribute_t *attr;               /* member-uris attribute */
   char         uri[HTTP_MAX_URI];      /* Device or printer URI */
   const char   *name,                  /* Pointer to class name */
+               *op,                    /* Operation name */
                *ptr;                   /* Pointer to CGI variable */
   const char   *title;                 /* Title of page */
   static const char * const pattrs[] = /* Requested printer attributes */
@@ -248,6 +334,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
 
 
   title = cgiText(modify ? _("Modify Class") : _("Add Class"));
+  op    = cgiGetVariable("OP");
   name  = cgiGetVariable("PRINTER_NAME");
 
   if (cgiGetVariable("PRINTER_LOCATION") == NULL)
@@ -258,18 +345,25 @@ do_am_class(http_t *http,         /* I - HTTP connection */
     *
     *    attributes-charset
     *    attributes-natural-language
-    *    printer-uri
     */
 
     request = ippNewRequest(CUPS_GET_PRINTERS);
 
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-                 NULL, "ipp://localhost/printers");
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type",
+                 CUPS_PRINTER_LOCAL);
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask",
+                 CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
 
    /*
     * Do the request and get back a response...
     */
 
+    cgiClearVariables();
+    if (op)
+      cgiSetVariable("OP", op);
+    if (name)
+      cgiSetVariable("PRINTER_NAME", name);
+
     if ((response = cupsDoRequest(http, request, "/")) != NULL)
     {
      /*
@@ -282,7 +376,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
        if (attr->name && !strcmp(attr->name, "printer-uri-supported"))
        {
          if ((ptr = strrchr(attr->values[0].string.text, '/')) != NULL &&
-             (!name || strcasecmp(name, ptr + 1)))
+             (!name || _cups_strcasecmp(name, ptr + 1)))
          {
           /*
            * Don't show the current class...
@@ -298,7 +392,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
           attr = attr->next)
        if (attr->name && !strcmp(attr->name, "printer-name"))
        {
-         if (!name || strcasecmp(name, attr->values[0].string.text))
+         if (!name || _cups_strcasecmp(name, attr->values[0].string.text))
          {
           /*
            * Don't show the current class...
@@ -359,7 +453,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
          {
            for (j = 0; j < num_printers; j ++)
            {
-             if (!strcasecmp(attr->values[i].string.text,
+             if (!_cups_strcasecmp(attr->values[i].string.text,
                              cgiGetArray("MEMBER_NAMES", j)))
              {
                cgiSetArray("MEMBER_SELECTED", j, "SELECTED");
@@ -402,6 +496,15 @@ do_am_class(http_t *http,          /* I - HTTP connection */
     return;
   }
 
+  if (!name)
+  {
+    cgiStartHTML(title);
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
+    cgiCopyTemplateLang("error.tmpl");
+    cgiEndHTML();
+    return;
+  }
+
   for (ptr = name; *ptr; ptr ++)
     if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
       break;
@@ -436,8 +539,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
   request = ippNewRequest(CUPS_ADD_CLASS);
 
   httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/classes/%s",
-                  cgiGetVariable("PRINTER_NAME"));
+                   "localhost", 0, "/classes/%s", name);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                NULL, uri);
 
@@ -457,7 +559,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
     attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris",
                          num_printers, NULL, NULL);
     for (i = 0; i < num_printers; i ++)
-      attr->values[i].string.text = strdup(cgiGetArray("MEMBER_URIS", i));
+      ippSetString(request, &attr, i, cgiGetArray("MEMBER_URIS", i));
   }
 
  /*
@@ -466,11 +568,16 @@ do_am_class(http_t *http,         /* I - HTTP connection */
 
   ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-  if (cupsLastError() > IPP_OK_CONFLICT)
+  if (cupsLastError() == IPP_NOT_AUTHORIZED)
+  {
+    puts("Status: 401\n");
+    exit(0);
+  }
+  else if (cupsLastError() > IPP_OK_CONFLICT)
   {
     cgiStartHTML(title);
-    cgiShowIPPError(modify ? _("Unable to modify class:") :
-                             _("Unable to add class:"));
+    cgiShowIPPError(modify ? _("Unable to modify class") :
+                             _("Unable to add class"));
   }
   else
   {
@@ -481,7 +588,7 @@ do_am_class(http_t *http,           /* I - HTTP connection */
     char       refresh[1024];          /* Refresh URL */
 
     cgiFormEncode(uri, name, sizeof(uri));
-    snprintf(refresh, sizeof(refresh), "5;/admin/?OP=redirect&URL=/classes/%s",
+    snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=/classes/%s",
              uri);
     cgiSetVariable("refresh_page", refresh);
 
@@ -506,16 +613,15 @@ do_am_printer(http_t *http,               /* I - HTTP connection */
              int    modify)            /* I - Modify the printer? */
 {
   int          i;                      /* Looping var */
-  int          element;                /* Element number */
-  ipp_attribute_t *attr,               /* Current attribute */
-               *last;                  /* Last attribute */
+  ipp_attribute_t *attr;               /* Current attribute */
   ipp_t                *request,               /* IPP request */
                *response,              /* IPP response */
                *oldinfo;               /* Old printer information */
   const cgi_file_t *file;              /* Uploaded file, if any */
   const char   *var;                   /* CGI variable */
   char         uri[HTTP_MAX_URI],      /* Device or printer URI */
-               *uriptr;                /* Pointer into URI */
+               *uriptr,                /* Pointer into URI */
+               evefile[1024] = "";     /* IPP Everywhere PPD file */
   int          maxrate;                /* Maximum baud rate */
   char         baudrate[255];          /* Baud rate string */
   const char   *name,                  /* Pointer to class name */
@@ -536,8 +642,9 @@ do_am_printer(http_t *http,         /* I - HTTP connection */
                };
 
 
+  ptr = cgiGetVariable("DEVICE_URI");
   fprintf(stderr, "DEBUG: do_am_printer: DEVICE_URI=\"%s\"\n",
-          cgiGetVariable("DEVICE_URI"));
+          ptr ? ptr : "(null)");
 
   title = cgiText(modify ? _("Modify Printer") : _("Add Printer"));
 
@@ -569,121 +676,158 @@ do_am_printer(http_t *http,             /* I - HTTP connection */
   else
     oldinfo = NULL;
 
-  if ((name = cgiGetVariable("PRINTER_NAME")) == NULL ||
-      cgiGetVariable("PRINTER_LOCATION") == NULL)
-  {
-    cgiStartHTML(title);
+  file = cgiGetFile();
 
-    if (modify)
-    {
-     /*
-      * Update the location and description of an existing printer...
-      */
+  if (file)
+  {
+    fprintf(stderr, "DEBUG: file->tempfile=%s\n", file->tempfile);
+    fprintf(stderr, "DEBUG: file->name=%s\n", file->name);
+    fprintf(stderr, "DEBUG: file->filename=%s\n", file->filename);
+    fprintf(stderr, "DEBUG: file->mimetype=%s\n", file->mimetype);
+  }
 
-      if (oldinfo)
-       cgiSetIPPVars(oldinfo, NULL, NULL, NULL, 0);
+  if ((name = cgiGetVariable("PRINTER_NAME")) != NULL)
+  {
+    for (ptr = name; *ptr; ptr ++)
+      if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '\\' || *ptr == '?' || *ptr == '\'' || *ptr == '\"' || *ptr == '#')
+       break;
 
-      cgiCopyTemplateLang("modify-printer.tmpl");
+    if (*ptr || ptr == name || strlen(name) > 127)
+    {
+      cgiSetVariable("ERROR",
+                    cgiText(_("The printer name may only contain up to 127 printable characters and may not contain spaces, slashes (/ \\), quotes (' \"), question mark (?), or the pound sign (#).")));
+      cgiStartHTML(title);
+      cgiCopyTemplateLang("error.tmpl");
+      cgiEndHTML();
+      return;
     }
-    else
+  }
+
+  if ((var = cgiGetVariable("DEVICE_URI")) != NULL)
+  {
+    if ((uriptr = strrchr(var, '|')) != NULL)
     {
      /*
-      * Get the name, location, and description for a new printer...
+      * Extract make and make/model from device URI string...
       */
 
-      cgiCopyTemplateLang("add-printer.tmpl");
-    }
+      char     make[1024],             /* Make string */
+               *makeptr;               /* Pointer into make */
 
-    cgiEndHTML();
 
-    if (oldinfo)
-      ippDelete(oldinfo);
+      *uriptr++ = '\0';
 
-    return;
-  }
+      strlcpy(make, uriptr, sizeof(make));
 
-  for (ptr = name; *ptr; ptr ++)
-    if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
-      break;
+      if ((makeptr = strchr(make, ' ')) != NULL)
+        *makeptr = '\0';
+      else if ((makeptr = strchr(make, '-')) != NULL)
+        *makeptr = '\0';
+      else if (!_cups_strncasecmp(make, "laserjet", 8) ||
+               !_cups_strncasecmp(make, "deskjet", 7) ||
+               !_cups_strncasecmp(make, "designjet", 9))
+        strlcpy(make, "HP", sizeof(make));
+      else if (!_cups_strncasecmp(make, "phaser", 6))
+        strlcpy(make, "Xerox", sizeof(make));
+      else if (!_cups_strncasecmp(make, "stylus", 6))
+        strlcpy(make, "Epson", sizeof(make));
+      else
+        strlcpy(make, "Generic", sizeof(make));
 
-  if (*ptr || ptr == name || strlen(name) > 127)
-  {
-    cgiSetVariable("ERROR",
-                   cgiText(_("The printer name may only contain up to "
-                            "127 printable characters and may not "
-                            "contain spaces, slashes (/), or the "
-                            "pound sign (#).")));
-    cgiStartHTML(title);
-    cgiCopyTemplateLang("error.tmpl");
-    cgiEndHTML();
-    return;
-  }
+      if (!cgiGetVariable("CURRENT_MAKE"))
+        cgiSetVariable("CURRENT_MAKE", make);
 
-  file = cgiGetFile();
+      if (!cgiGetVariable("CURRENT_MAKE_AND_MODEL"))
+        cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr);
 
-  if (file)
-  {
-    fprintf(stderr, "DEBUG: file->tempfile=%s\n", file->tempfile);
-    fprintf(stderr, "DEBUG: file->name=%s\n", file->name);
-    fprintf(stderr, "DEBUG: file->filename=%s\n", file->filename);
-    fprintf(stderr, "DEBUG: file->mimetype=%s\n", file->mimetype);
+      if (!modify)
+      {
+        char   template[128],          /* Template name */
+               *tptr;                  /* Pointer into template name */
+
+       cgiSetVariable("PRINTER_INFO", uriptr);
+
+       for (tptr = template;
+            tptr < (template + sizeof(template) - 1) && *uriptr;
+            uriptr ++)
+         if (isalnum(*uriptr & 255) || *uriptr == '_' || *uriptr == '-' ||
+             *uriptr == '.')
+           *tptr++ = *uriptr;
+         else if ((*uriptr == ' ' || *uriptr == '/') && tptr > template &&
+                  tptr[-1] != '_')
+           *tptr++ = '_';
+         else if (*uriptr == '?' || *uriptr == '(')
+           break;
+
+        *tptr = '\0';
+
+        cgiSetVariable("TEMPLATE_NAME", template);
+      }
+    }
   }
 
-  if ((var = cgiGetVariable("DEVICE_URI")) == NULL)
+  if (!var)
   {
    /*
-    * Build a CUPS_GET_DEVICES request, which requires the following
-    * attributes:
-    *
-    *    attributes-charset
-    *    attributes-natural-language
-    *    printer-uri
+    * Look for devices so the user can pick something...
     */
 
-    fputs("DEBUG: Getting list of devices...\n", stderr);
-
-    request = ippNewRequest(CUPS_GET_DEVICES);
+    if ((attr = ippFindAttribute(oldinfo, "device-uri", IPP_TAG_URI)) != NULL)
+    {
+      strlcpy(uri, attr->values[0].string.text, sizeof(uri));
+      if ((uriptr = strchr(uri, ':')) != NULL && strncmp(uriptr, "://", 3) == 0)
+        *uriptr = '\0';
 
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-                 NULL, "ipp://localhost/printers/");
+      cgiSetVariable("CURRENT_DEVICE_URI", attr->values[0].string.text);
+      cgiSetVariable("CURRENT_DEVICE_SCHEME", uri);
+    }
 
    /*
-    * Do the request and get back a response...
+    * Scan for devices for up to 30 seconds...
     */
 
-    fprintf(stderr, "DEBUG: http=%p (%s)\n", http, http->hostname);
+    fputs("DEBUG: Getting list of devices...\n", stderr);
 
-    if ((response = cupsDoRequest(http, request, "/")) != NULL)
+    current_device = 0;
+    if (cupsGetDevices(http, 5, CUPS_INCLUDE_ALL, CUPS_EXCLUDE_NONE,
+                       (cups_device_cb_t)choose_device_cb,
+                      (void *)title) == IPP_OK)
     {
       fputs("DEBUG: Got device list!\n", stderr);
 
-      cgiSetIPPVars(response, NULL, NULL, NULL, 0);
-      ippDelete(response);
+      if (cgiSupportsMultipart())
+        cgiStartMultipart();
+
+      cgiSetVariable("CUPS_GET_DEVICES_DONE", "1");
+      cgiStartHTML(title);
+      cgiCopyTemplateLang("choose-device.tmpl");
+      cgiEndHTML();
+
+      if (cgiSupportsMultipart())
+        cgiEndMultipart();
     }
     else
+    {
       fprintf(stderr,
               "ERROR: CUPS-Get-Devices request failed with status %x: %s\n",
              cupsLastError(), cupsLastErrorString());
-
-   /*
-    * Let the user choose...
-    */
-
-    if ((attr = ippFindAttribute(oldinfo, "device-uri", IPP_TAG_URI)) != NULL)
-    {
-      strlcpy(uri, attr->values[0].string.text, sizeof(uri));
-      if ((uriptr = strchr(uri, ':')) != NULL && strncmp(uriptr, "://", 3) == 0)
-        *uriptr = '\0';
-
-      cgiSetVariable("CURRENT_DEVICE_URI", attr->values[0].string.text);
-      cgiSetVariable("CURRENT_DEVICE_SCHEME", uri);
+      if (cupsLastError() == IPP_NOT_AUTHORIZED)
+      {
+       puts("Status: 401\n");
+       exit(0);
+      }
+      else
+      {
+       cgiStartHTML(title);
+       cgiShowIPPError(modify ? _("Unable to modify printer") :
+                                _("Unable to add printer"));
+       cgiEndHTML();
+        return;
+      }
     }
-
-    cgiStartHTML(title);
-    cgiCopyTemplateLang("choose-device.tmpl");
-    cgiEndHTML();
   }
-  else if (strchr(var, '/') == NULL)
+  else if (!strchr(var, '/') ||
+           (!strncmp(var, "lpd://", 6) && !strchr(var + 6, '/')))
   {
     if ((attr = ippFindAttribute(oldinfo, "device-uri", IPP_TAG_URI)) != NULL)
     {
@@ -692,7 +836,7 @@ do_am_printer(http_t *http,         /* I - HTTP connection */
       */
 
       if (strncmp(attr->values[0].string.text, var, strlen(var)) == 0)
-       cgiSetVariable("DEVICE_URI", attr->values[0].string.text);
+       cgiSetVariable("CURRENT_DEVICE_URI", attr->values[0].string.text);
     }
 
    /*
@@ -728,9 +872,63 @@ do_am_printer(http_t *http,                /* I - HTTP connection */
     cgiCopyTemplateLang("choose-serial.tmpl");
     cgiEndHTML();
   }
-  else if (!file && (var = cgiGetVariable("PPD_NAME")) == NULL)
+  else if (!name || !cgiGetVariable("PRINTER_LOCATION"))
   {
+    cgiStartHTML(title);
+
     if (modify)
+    {
+     /*
+      * Update the location and description of an existing printer...
+      */
+
+      if (oldinfo)
+      {
+        if ((attr = ippFindAttribute(oldinfo, "printer-info",
+                                    IPP_TAG_TEXT)) != NULL)
+          cgiSetVariable("PRINTER_INFO", attr->values[0].string.text);
+
+        if ((attr = ippFindAttribute(oldinfo, "printer-location",
+                                    IPP_TAG_TEXT)) != NULL)
+          cgiSetVariable("PRINTER_LOCATION", attr->values[0].string.text);
+
+       if ((attr = ippFindAttribute(oldinfo, "printer-is-shared",
+                                    IPP_TAG_BOOLEAN)) != NULL)
+         cgiSetVariable("PRINTER_IS_SHARED",
+                        attr->values[0].boolean ? "1" : "0");
+      }
+
+      cgiCopyTemplateLang("modify-printer.tmpl");
+    }
+    else
+    {
+     /*
+      * Get the name, location, and description for a new printer...
+      */
+
+#ifdef __APPLE__
+      if (!strncmp(var, "usb:", 4))
+        cgiSetVariable("printer_is_shared", "1");
+      else
+#endif /* __APPLE__ */
+        cgiSetVariable("printer_is_shared", "0");
+
+      cgiCopyTemplateLang("add-printer.tmpl");
+    }
+
+    cgiEndHTML();
+
+    if (oldinfo)
+      ippDelete(oldinfo);
+
+    return;
+  }
+  else if (!file &&
+           (!cgiGetVariable("PPD_NAME") || cgiGetVariable("SELECT_MAKE")))
+  {
+    int ipp_everywhere = !strncmp(var, "ipp://", 6) || !strncmp(var, "ipps://", 7) || (!strncmp(var, "dnssd://", 8) && (strstr(var, "_ipp._tcp") || strstr(var, "_ipps._tcp")));
+
+    if (modify && !cgiGetVariable("SELECT_MAKE"))
     {
      /*
       * Get the PPD file...
@@ -740,7 +938,7 @@ do_am_printer(http_t *http,         /* I - HTTP connection */
       char             filename[1024]; /* PPD filename */
       ppd_file_t       *ppd;           /* PPD information */
       char             buffer[1024];   /* Buffer */
-      int              bytes;          /* Number of bytes */
+      ssize_t          bytes;          /* Number of bytes */
       http_status_t    get_status;     /* Status of GET */
 
 
@@ -754,13 +952,15 @@ do_am_printer(http_t *http,               /* I - HTTP connection */
 
       if (get_status != HTTP_OK)
       {
+        httpFlush(http);
+
         fprintf(stderr, "ERROR: Unable to get PPD file %s: %d - %s\n",
                uri, get_status, httpStatus(get_status));
       }
       else if ((fd = cupsTempFd(filename, sizeof(filename))) >= 0)
       {
        while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
-          write(fd, buffer, bytes);
+          write(fd, buffer, (size_t)bytes);
 
        close(fd);
 
@@ -777,8 +977,10 @@ do_am_printer(http_t *http,                /* I - HTTP connection */
        }
        else
        {
+         int linenum;                  /* Line number */
+
          fprintf(stderr, "ERROR: Unable to open PPD file %s: %s\n",
-                 filename, ppdErrorString(ppdLastError(&bytes)));
+                 filename, ppdErrorString(ppdLastError(&linenum)));
        }
       }
       else
@@ -790,39 +992,6 @@ do_am_printer(http_t *http,                /* I - HTTP connection */
                strerror(errno));
       }
     }
-    else if ((uriptr = strrchr(cgiGetVariable("DEVICE_URI"), '|')) != NULL)
-    {
-     /*
-      * Extract make and make/model from device URI string...
-      */
-
-      char     make[1024],             /* Make string */
-               *makeptr;               /* Pointer into make */
-
-
-      *uriptr++ = '\0';
-
-      strlcpy(make, uriptr, sizeof(make));
-
-      if ((makeptr = strchr(make, ' ')) != NULL)
-        *makeptr = '\0';
-      else if ((makeptr = strchr(make, '-')) != NULL)
-        *makeptr = '\0';
-      else if (!strncasecmp(make, "laserjet", 8) ||
-               !strncasecmp(make, "deskjet", 7) ||
-               !strncasecmp(make, "designjet", 9))
-        strcpy(make, "HP");
-      else if (!strncasecmp(make, "phaser", 6))
-        strcpy(make, "Xerox");
-      else if (!strncasecmp(make, "stylus", 6))
-        strcpy(make, "Epson");
-      else
-        strcpy(make, "Generic");
-
-      cgiSetVariable("CURRENT_MAKE", make);
-      cgiSetVariable("PPD_MAKE", make);
-      cgiSetVariable("CURRENT_MAKE_AND_MODEL", uriptr);
-    }
 
    /*
     * Build a CUPS_GET_PPDS request, which requires the following
@@ -838,9 +1007,20 @@ do_am_printer(http_t *http,               /* I - HTTP connection */
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                  NULL, "ipp://localhost/printers/");
 
-    if ((var = cgiGetVariable("PPD_MAKE")) != NULL)
+    if ((var = cgiGetVariable("PPD_MAKE")) == NULL)
+      var = cgiGetVariable("CURRENT_MAKE");
+    if (var && !cgiGetVariable("SELECT_MAKE"))
+    {
+      const char *make_model;          /* Make and model */
+
+
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
                    "ppd-make", NULL, var);
+
+      if ((make_model = cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL)
+       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
+                    "ppd-make-and-model", NULL, make_model);
+    }
     else
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
                    "requested-attributes", NULL, "ppd-make");
@@ -855,27 +1035,31 @@ do_am_printer(http_t *http,              /* I - HTTP connection */
       * Got the list of PPDs, see if the user has selected a make...
       */
 
-      cgiSetIPPVars(response, NULL, NULL, NULL, 0);
-
-      if (var == NULL)
+      if (cgiSetIPPVars(response, NULL, NULL, NULL, 0) == 0 && !modify)
       {
        /*
-       * Let the user choose a make...
+        * No PPD files with this make, try again with all makes...
        */
 
-        for (element = 0, attr = response->attrs, last = NULL;
-            attr != NULL;
-            attr = attr->next)
-         if (attr->name && strcmp(attr->name, "ppd-make") == 0)
-           if (last == NULL ||
-               strcasecmp(last->values[0].string.text,
-                          attr->values[0].string.text) != 0)
-           {
-             cgiSetArray("PPD_MAKE", element, attr->values[0].string.text);
-             element ++;
-             last = attr;
-           }
+        ippDelete(response);
+
+       request = ippNewRequest(CUPS_GET_PPDS);
 
+       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+                     NULL, "ipp://localhost/printers/");
+
+       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+                     "requested-attributes", NULL, "ppd-make");
+
+       if ((response = cupsDoRequest(http, request, "/")) != NULL)
+          cgiSetIPPVars(response, NULL, NULL, NULL, 0);
+
+        cgiStartHTML(title);
+       cgiCopyTemplateLang("choose-make.tmpl");
+        cgiEndHTML();
+      }
+      else if (!var || cgiGetVariable("SELECT_MAKE"))
+      {
         cgiStartHTML(title);
        cgiCopyTemplateLang("choose-make.tmpl");
         cgiEndHTML();
@@ -886,58 +1070,21 @@ do_am_printer(http_t *http,              /* I - HTTP connection */
        * Let the user choose a model...
        */
 
-        const char     *make_model;    /* Current make/model string */
-
-
-        if ((make_model = cgiGetVariable("CURRENT_MAKE_AND_MODEL")) != NULL)
-       {
-        /*
-         * Scan for "close" matches...
-         */
-
-          int          match,          /* Current match */
-                       best_match,     /* Best match so far */
-                       count;          /* Number of drivers */
-         const char    *best,          /* Best matching string */
-                       *current;       /* Current string */
-
-
-          count = cgiGetSize("PPD_MAKE_AND_MODEL");
-
-         for (i = 0, best_match = 0, best = NULL; i < count; i ++)
-         {
-           current = cgiGetArray("PPD_MAKE_AND_MODEL", i);
-           match   = match_string(make_model, current);
-
-           if (match > best_match)
-           {
-             best_match = match;
-             best       = current;
-           }
-         }
-
-          if (best_match > strlen(var))
-         {
-          /*
-           * Found a match longer than the make...
-           */
-
-            cgiSetVariable("CURRENT_MAKE_AND_MODEL", best);
-         }
-       }
-
         cgiStartHTML(title);
+       if (!cgiGetVariable("PPD_MAKE"))
+         cgiSetVariable("PPD_MAKE", cgiGetVariable("CURRENT_MAKE"));
+        if (ipp_everywhere)
+         cgiSetVariable("SHOW_IPP_EVERYWHERE", "1");
        cgiCopyTemplateLang("choose-model.tmpl");
         cgiEndHTML();
       }
 
-      
       ippDelete(response);
     }
     else
     {
       cgiStartHTML(title);
-      cgiShowIPPError(_("Unable to get list of printer drivers:"));
+      cgiShowIPPError(_("Unable to get list of printer drivers"));
       cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
     }
@@ -956,6 +1103,7 @@ do_am_printer(http_t *http,                /* I - HTTP connection */
     *    ppd-name
     *    device-uri
     *    printer-is-accepting-jobs
+    *    printer-is-shared
     *    printer-state
     */
 
@@ -967,16 +1115,22 @@ do_am_printer(http_t *http,              /* I - HTTP connection */
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                  NULL, uri);
 
+    if (!file)
+    {
+      var = cgiGetVariable("PPD_NAME");
+      if (!strcmp(var, "everywhere"))
+        get_printer_ppd(cgiGetVariable("DEVICE_URI"), evefile, sizeof(evefile));
+      else if (strcmp(var, "__no_change__"))
+       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name",
+                    NULL, var);
+    }
+
     ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
                  NULL, cgiGetVariable("PRINTER_LOCATION"));
 
     ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
                  NULL, cgiGetVariable("PRINTER_INFO"));
 
-    if (!file)
-      ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "ppd-name",
-                   NULL, cgiGetVariable("PPD_NAME"));
-
     strlcpy(uri, cgiGetVariable("DEVICE_URI"), sizeof(uri));
 
    /*
@@ -995,7 +1149,7 @@ do_am_printer(http_t *http,                /* I - HTTP connection */
       if ((uriptr = strchr(uri, '?')) == NULL)
         uriptr = uri + strlen(uri);
 
-      snprintf(uriptr, sizeof(uri) - (uriptr - uri),
+      snprintf(uriptr, sizeof(uri) - (size_t)(uriptr - uri),
                "?baud=%s+bits=%s+parity=%s+flow=%s",
                cgiGetVariable("BAUDRATE"), cgiGetVariable("BITS"),
               cgiGetVariable("PARITY"), cgiGetVariable("FLOW"));
@@ -1006,6 +1160,10 @@ do_am_printer(http_t *http,              /* I - HTTP connection */
 
     ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
 
+    var = cgiGetVariable("printer_is_shared");
+    ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-shared",
+                  var && (!strcmp(var, "1") || !strcmp(var, "on")));
+
     ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
                   IPP_PRINTER_IDLE);
 
@@ -1015,19 +1173,29 @@ do_am_printer(http_t *http,             /* I - HTTP connection */
 
     if (file)
       ippDelete(cupsDoFileRequest(http, request, "/admin/", file->tempfile));
+    else if (evefile[0])
+    {
+      ippDelete(cupsDoFileRequest(http, request, "/admin/", evefile));
+      unlink(evefile);
+    }
     else
       ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    if (cupsLastError() > IPP_OK_CONFLICT)
+    if (cupsLastError() == IPP_NOT_AUTHORIZED)
+    {
+      puts("Status: 401\n");
+      exit(0);
+    }
+    else if (cupsLastError() > IPP_OK_CONFLICT)
     {
       cgiStartHTML(title);
-      cgiShowIPPError(modify ? _("Unable to modify printer:") :
-                               _("Unable to add printer:"));
+      cgiShowIPPError(modify ? _("Unable to modify printer") :
+                               _("Unable to add printer"));
     }
-    else
+    else if (modify)
     {
      /*
-      * Redirect successful updates back to the printer or set-options pages...
+      * Redirect successful updates back to the printer page...
       */
 
       char     refresh[1024];          /* Refresh URL */
@@ -1035,21 +1203,24 @@ do_am_printer(http_t *http,             /* I - HTTP connection */
 
       cgiFormEncode(uri, name, sizeof(uri));
 
-      if (modify)
-       snprintf(refresh, sizeof(refresh),
-                "5;/admin/?OP=redirect&URL=/printers/%s", uri);
-      else
-       snprintf(refresh, sizeof(refresh),
-                "5;/admin/?OP=set-printer-options&PRINTER_NAME=%s", uri);
+      snprintf(refresh, sizeof(refresh),
+              "5;/admin/?OP=redirect&URL=/printers/%s", uri);
 
       cgiSetVariable("refresh_page", refresh);
 
       cgiStartHTML(title);
 
-      if (modify)
-        cgiCopyTemplateLang("printer-modified.tmpl");
-      else
-        cgiCopyTemplateLang("printer-added.tmpl");
+      cgiCopyTemplateLang("printer-modified.tmpl");
+    }
+    else
+    {
+     /*
+      * Set the printer options...
+      */
+
+      cgiSetVariable("OP", "set-printer-options");
+      do_set_options(http, 0);
+      return;
     }
 
     cgiEndHTML();
@@ -1061,602 +1232,318 @@ do_am_printer(http_t *http,           /* I - HTTP connection */
 
 
 /*
- * 'do_config_printer()' - Configure the default options for a printer.
+ * 'do_config_server()' - Configure server settings.
  */
 
 static void
-do_config_printer(http_t *http)                /* I - HTTP connection */
+do_config_server(http_t *http)         /* I - HTTP connection */
 {
-  int          i, j, k, m;             /* Looping vars */
-  int          have_options;           /* Have options? */
-  ipp_t                *request,               /* IPP request */
-               *response;              /* IPP response */
-  ipp_attribute_t *attr;               /* IPP attribute */
-  char         uri[HTTP_MAX_URI];      /* Job URI */
-  const char   *var;                   /* Variable value */
-  const char   *printer;               /* Printer printer name */
-  const char   *filename;              /* PPD filename */
-  char         tempfile[1024];         /* Temporary filename */
-  cups_file_t  *in,                    /* Input file */
-               *out;                   /* Output file */
-  char         line[1024];             /* Line from PPD file */
-  char         keyword[1024],          /* Keyword from Default line */
-               *keyptr;                /* Pointer into keyword... */
-  ppd_file_t   *ppd;                   /* PPD file */
-  ppd_group_t  *group;                 /* Option group */
-  ppd_option_t *option;                /* Option */
-  ppd_attr_t   *protocol;              /* cupsProtocol attribute */
-  const char   *title;                 /* Page title */
-
-
-  title = cgiText(_("Set Printer Options"));
-
-  fprintf(stderr, "DEBUG: do_config_printer(http=%p)\n", http);
-
- /*
-  * Get the printer name...
-  */
-
-  if ((printer = cgiGetVariable("PRINTER_NAME")) != NULL)
-    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                     "localhost", 0, "/printers/%s", printer);
-  else
+  if (cgiGetVariable("CHANGESETTINGS"))
   {
-    cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
-    cgiStartHTML(title);
-    cgiCopyTemplateLang("error.tmpl");
-    cgiEndHTML();
-    return;
-  }
-
-  fprintf(stderr, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer, uri);
+   /*
+    * Save basic setting changes...
+    */
 
- /*
-  * Get the PPD file...
-  */
+    int                        num_settings;   /* Number of server settings */
+    cups_option_t      *settings;      /* Server settings */
+    int                        advanced,       /* Advanced settings shown? */
+                       changed;        /* Have settings changed? */
+    const char         *debug_logging, /* DEBUG_LOGGING value */
+                       *preserve_jobs = NULL,
+                                       /* PRESERVE_JOBS value */
+                       *remote_admin,  /* REMOTE_ADMIN value */
+                       *remote_any,    /* REMOTE_ANY value */
+                       *share_printers,/* SHARE_PRINTERS value */
+                       *user_cancel_any,
+                                       /* USER_CANCEL_ANY value */
+                       *browse_web_if = NULL,
+                                       /* BrowseWebIF value */
+                       *preserve_job_history = NULL,
+                                       /* PreserveJobHistory value */
+                       *preserve_job_files = NULL,
+                                       /* PreserveJobFiles value */
+                       *max_clients = NULL,
+                                       /* MaxClients value */
+                       *max_jobs = NULL,
+                                       /* MaxJobs value */
+                       *max_log_size = NULL;
+                                       /* MaxLogSize value */
+    const char         *current_browse_web_if,
+                                       /* BrowseWebIF value */
+                       *current_preserve_job_history,
+                                       /* PreserveJobHistory value */
+                       *current_preserve_job_files,
+                                       /* PreserveJobFiles value */
+                       *current_max_clients,
+                                       /* MaxClients value */
+                       *current_max_jobs,
+                                       /* MaxJobs value */
+                       *current_max_log_size;
+                                       /* MaxLogSize value */
+#ifdef HAVE_GSSAPI
+    char               default_auth_type[255];
+                                       /* DefaultAuthType value */
+    const char         *val;           /* Setting value */
+#endif /* HAVE_GSSAPI */
 
-  if ((filename = cupsGetPPD2(http, printer)) == NULL)
-  {
-    fputs("DEBUG: No PPD file!?!\n", stderr);
 
-    cgiStartHTML(title);
-    cgiShowIPPError(_("Unable to get PPD file!"));
-    cgiEndHTML();
-    return;
-  }
+   /*
+    * Get the checkbox values from the form...
+    */
 
-  fprintf(stderr, "DEBUG: Got PPD file: \"%s\"\n", filename);
+    debug_logging        = cgiGetVariable("DEBUG_LOGGING") ? "1" : "0";
+    remote_admin         = cgiGetVariable("REMOTE_ADMIN") ? "1" : "0";
+    remote_any           = cgiGetVariable("REMOTE_ANY") ? "1" : "0";
+    share_printers       = cgiGetVariable("SHARE_PRINTERS") ? "1" : "0";
+    user_cancel_any      = cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0";
 
-  if ((ppd = ppdOpenFile(filename)) == NULL)
-  {
-    cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i)));
-    cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file:")));
-    cgiStartHTML(title);
-    cgiCopyTemplateLang("error.tmpl");
-    cgiEndHTML();
-    return;
-  }
+    advanced = cgiGetVariable("ADVANCEDSETTINGS") != NULL;
+    if (advanced)
+    {
+     /*
+      * Get advanced settings...
+      */
 
-  if (cgiGetVariable("job_sheets_start") != NULL ||
-      cgiGetVariable("job_sheets_end") != NULL)
-    have_options = 1;
-  else
-    have_options = 0;
+      browse_web_if        = cgiGetVariable("BROWSE_WEB_IF") ? "Yes" : "No";
+      max_clients          = cgiGetVariable("MAX_CLIENTS");
+      max_log_size         = cgiGetVariable("MAX_LOG_SIZE");
+      preserve_jobs        = cgiGetVariable("PRESERVE_JOBS");
 
-  ppdMarkDefaults(ppd);
+      if (preserve_jobs)
+      {
+        max_jobs             = cgiGetVariable("MAX_JOBS");
+       preserve_job_history = cgiGetVariable("PRESERVE_JOB_HISTORY");
+       preserve_job_files   = cgiGetVariable("PRESERVE_JOB_FILES");
 
-  DEBUG_printf(("<P>ppd->num_groups = %d\n"
-                "<UL>\n", ppd->num_groups));
+       if (!max_jobs || atoi(max_jobs) < 0)
+         max_jobs = "500";
 
-  for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
-  {
-    DEBUG_printf(("<LI>%s<UL>\n", group->text));
+       if (!preserve_job_history)
+         preserve_job_history = "On";
 
-    for (j = group->num_options, option = group->options;
-         j > 0;
-        j --, option ++)
-      if ((var = cgiGetVariable(option->keyword)) != NULL)
-      {
-        DEBUG_printf(("<LI>%s = \"%s\"</LI>\n", option->keyword, var));
-        have_options = 1;
-       ppdMarkOption(ppd, option->keyword, var);
+       if (!preserve_job_files)
+         preserve_job_files = "1d";
       }
-#ifdef DEBUG
       else
-        printf("<LI>%s not defined!</LI>\n", option->keyword);
-#endif /* DEBUG */
+      {
+        max_jobs             = "0";
+        preserve_job_history = "No";
+        preserve_job_files   = "No";
+      }
 
-    DEBUG_puts("</UL></LI>");
-  }
+      if (!max_clients || atoi(max_clients) <= 0)
+       max_clients = "100";
 
-  DEBUG_printf(("</UL>\n"
-                "<P>ppdConflicts(ppd) = %d\n", ppdConflicts(ppd)));
+      if (!max_log_size || atoi(max_log_size) <= 0.0)
+       max_log_size = "1m";
+    }
 
-  if (!have_options || ppdConflicts(ppd))
-  {
    /*
-    * Show the options to the user...
+    * Get the current server settings...
     */
 
-    fputs("DEBUG: Showing options...\n", stderr);
-
-    ppdLocalize(ppd);
-
-    cgiStartHTML("Set Printer Options");
-    cgiCopyTemplateLang("set-printer-options-header.tmpl");
-
-    if (ppdConflicts(ppd))
+    if (!cupsAdminGetServerSettings(http, &num_settings, &settings))
     {
-      for (i = ppd->num_groups, k = 0, group = ppd->groups;
-           i > 0;
-          i --, group ++)
-       for (j = group->num_options, option = group->options;
-            j > 0;
-            j --, option ++)
-          if (option->conflicted)
-         {
-           cgiSetArray("ckeyword", k, option->keyword);
-           cgiSetArray("ckeytext", k, option->text);
-           k ++;
-         }
-
-      cgiCopyTemplateLang("option-conflict.tmpl");
+      cgiStartHTML(cgiText(_("Change Settings")));
+      cgiSetVariable("MESSAGE",
+                     cgiText(_("Unable to change server settings")));
+      cgiSetVariable("ERROR", cupsLastErrorString());
+      cgiCopyTemplateLang("error.tmpl");
+      cgiEndHTML();
+      return;
     }
 
-    for (i = ppd->num_groups, group = ppd->groups;
-        i > 0;
-        i --, group ++)
-    {
-      if (!strcmp(group->name, "InstallableOptions"))
-       cgiSetVariable("GROUP", cgiText(_("Options Installed")));
-      else
-       cgiSetVariable("GROUP", group->text);
-
-      cgiCopyTemplateLang("option-header.tmpl");
-      
-      for (j = group->num_options, option = group->options;
-           j > 0;
-          j --, option ++)
-      {
-        if (!strcmp(option->keyword, "PageRegion"))
-         continue;
-
-        cgiSetVariable("KEYWORD", option->keyword);
-        cgiSetVariable("KEYTEXT", option->text);
-           
-       if (option->conflicted)
-         cgiSetVariable("CONFLICTED", "1");
-       else
-         cgiSetVariable("CONFLICTED", "0");
-
-       cgiSetSize("CHOICES", 0);
-       cgiSetSize("TEXT", 0);
-       for (k = 0, m = 0; k < option->num_choices; k ++)
-       {
-        /*
-         * Hide custom option values...
-         */
+#ifdef HAVE_GSSAPI
+   /*
+    * Get authentication settings...
+    */
 
-         if (!strcmp(option->choices[k].choice, "Custom"))
-           continue;
+    if (cgiGetVariable("KERBEROS"))
+      strlcpy(default_auth_type, "Negotiate", sizeof(default_auth_type));
+    else
+    {
+      val = cupsGetOption("DefaultAuthType", num_settings, settings);
 
-         cgiSetArray("CHOICES", m, option->choices[k].choice);
-         cgiSetArray("TEXT", m, option->choices[k].text);
+      if (!val || !_cups_strcasecmp(val, "Negotiate"))
+        strlcpy(default_auth_type, "Basic", sizeof(default_auth_type));
+      else
+        strlcpy(default_auth_type, val, sizeof(default_auth_type));
+    }
 
-          m ++;
+    fprintf(stderr, "DEBUG: DefaultAuthType %s\n", default_auth_type);
+#endif /* HAVE_GSSAPI */
 
-          if (option->choices[k].marked)
-           cgiSetVariable("DEFCHOICE", option->choices[k].choice);
-       }
+    if ((current_browse_web_if = cupsGetOption("BrowseWebIF", num_settings,
+                                               settings)) == NULL)
+      current_browse_web_if = "No";
 
-        switch (option->ui)
-       {
-         case PPD_UI_BOOLEAN :
-              cgiCopyTemplateLang("option-boolean.tmpl");
-              break;
-         case PPD_UI_PICKONE :
-              cgiCopyTemplateLang("option-pickone.tmpl");
-              break;
-         case PPD_UI_PICKMANY :
-              cgiCopyTemplateLang("option-pickmany.tmpl");
-              break;
-       }
-      }
+    if ((current_preserve_job_history = cupsGetOption("PreserveJobHistory",
+                                                      num_settings,
+                                                     settings)) == NULL)
+      current_preserve_job_history = "Yes";
 
-      cgiCopyTemplateLang("option-trailer.tmpl");
-    }
+    if ((current_preserve_job_files = cupsGetOption("PreserveJobFiles",
+                                                    num_settings,
+                                                   settings)) == NULL)
+      current_preserve_job_files = "1d";
 
-   /*
-    * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
-    * following attributes:
-    *
-    *    attributes-charset
-    *    attributes-natural-language
-    *    printer-uri
-    */
+    if ((current_max_clients = cupsGetOption("MaxClients", num_settings,
+                                             settings)) == NULL)
+      current_max_clients = "100";
 
-    request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+    if ((current_max_jobs = cupsGetOption("MaxJobs", num_settings,
+                                          settings)) == NULL)
+      current_max_jobs = "500";
 
-    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                     "localhost", 0, "/printers/%s", printer);
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-                 NULL, uri);
+    if ((current_max_log_size = cupsGetOption("MaxLogSize", num_settings,
+                                              settings)) == NULL)
+      current_max_log_size = "1m";
 
    /*
-    * Do the request and get back a response...
+    * See if the settings have changed...
     */
 
-    if ((response = cupsDoRequest(http, request, "/")) != NULL)
+    changed = strcmp(debug_logging, cupsGetOption(CUPS_SERVER_DEBUG_LOGGING,
+                                                  num_settings, settings)) ||
+             strcmp(remote_admin, cupsGetOption(CUPS_SERVER_REMOTE_ADMIN,
+                                                num_settings, settings)) ||
+             strcmp(remote_any, cupsGetOption(CUPS_SERVER_REMOTE_ANY,
+                                              num_settings, settings)) ||
+             strcmp(share_printers, cupsGetOption(CUPS_SERVER_SHARE_PRINTERS,
+                                                  num_settings, settings)) ||
+#ifdef HAVE_GSSAPI
+             !cupsGetOption("DefaultAuthType", num_settings, settings) ||
+             strcmp(default_auth_type, cupsGetOption("DefaultAuthType",
+                                                     num_settings, settings)) ||
+#endif /* HAVE_GSSAPI */
+             strcmp(user_cancel_any, cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY,
+                                                   num_settings, settings));
+
+    if (advanced && !changed)
+      changed = _cups_strcasecmp(browse_web_if, current_browse_web_if) ||
+               _cups_strcasecmp(preserve_job_history, current_preserve_job_history) ||
+               _cups_strcasecmp(preserve_job_files, current_preserve_job_files) ||
+               _cups_strcasecmp(max_clients, current_max_clients) ||
+               _cups_strcasecmp(max_jobs, current_max_jobs) ||
+               _cups_strcasecmp(max_log_size, current_max_log_size);
+
+    if (changed)
     {
-      if ((attr = ippFindAttribute(response, "job-sheets-supported",
-                                   IPP_TAG_ZERO)) != NULL)
+     /*
+      * Settings *have* changed, so save the changes...
+      */
+
+      cupsFreeOptions(num_settings, settings);
+
+      num_settings = 0;
+      num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
+                                   debug_logging, num_settings, &settings);
+      num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
+                                   remote_admin, num_settings, &settings);
+      num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY,
+                                   remote_any, num_settings, &settings);
+      num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
+                                   share_printers, num_settings, &settings);
+      num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
+                                   user_cancel_any, num_settings, &settings);
+#ifdef HAVE_GSSAPI
+      num_settings = cupsAddOption("DefaultAuthType", default_auth_type,
+                                   num_settings, &settings);
+#endif /* HAVE_GSSAPI */
+
+      if (advanced)
       {
        /*
-       * Add the job sheets options...
+        * Add advanced settings...
        */
 
-       cgiSetVariable("GROUP", cgiText(_("Banners")));
-       cgiCopyTemplateLang("option-header.tmpl");
+       if (_cups_strcasecmp(browse_web_if, current_browse_web_if))
+         num_settings = cupsAddOption("BrowseWebIF", browse_web_if,
+                                      num_settings, &settings);
+       if (_cups_strcasecmp(preserve_job_history, current_preserve_job_history))
+         num_settings = cupsAddOption("PreserveJobHistory",
+                                      preserve_job_history, num_settings,
+                                      &settings);
+       if (_cups_strcasecmp(preserve_job_files, current_preserve_job_files))
+         num_settings = cupsAddOption("PreserveJobFiles", preserve_job_files,
+                                      num_settings, &settings);
+        if (_cups_strcasecmp(max_clients, current_max_clients))
+         num_settings = cupsAddOption("MaxClients", max_clients, num_settings,
+                                      &settings);
+        if (_cups_strcasecmp(max_jobs, current_max_jobs))
+         num_settings = cupsAddOption("MaxJobs", max_jobs, num_settings,
+                                      &settings);
+        if (_cups_strcasecmp(max_log_size, current_max_log_size))
+         num_settings = cupsAddOption("MaxLogSize", max_log_size, num_settings,
+                                      &settings);
+      }
 
-       cgiSetSize("CHOICES", attr->num_values);
-       cgiSetSize("TEXT", attr->num_values);
-       for (k = 0; k < attr->num_values; k ++)
+      if (!cupsAdminSetServerSettings(http, num_settings, settings))
+      {
+        if (cupsLastError() == IPP_NOT_AUTHORIZED)
        {
-         cgiSetArray("CHOICES", k, attr->values[k].string.text);
-         cgiSetArray("TEXT", k, attr->values[k].string.text);
+         puts("Status: 401\n");
+         exit(0);
        }
 
-        attr = ippFindAttribute(response, "job-sheets-default", IPP_TAG_ZERO);
+       cgiStartHTML(cgiText(_("Change Settings")));
+       cgiSetVariable("MESSAGE",
+                       cgiText(_("Unable to change server settings")));
+       cgiSetVariable("ERROR", cupsLastErrorString());
+       cgiCopyTemplateLang("error.tmpl");
+      }
+      else
+      {
+        if (advanced)
+         cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&"
+                                        "URL=/admin/?ADVANCEDSETTINGS=YES");
+        else
+         cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
+       cgiStartHTML(cgiText(_("Change Settings")));
+       cgiCopyTemplateLang("restart.tmpl");
+      }
+    }
+    else
+    {
+     /*
+      * No changes...
+      */
 
-        cgiSetVariable("KEYWORD", "job_sheets_start");
-       cgiSetVariable("KEYTEXT", cgiText(_("Starting Banner")));
-        cgiSetVariable("DEFCHOICE", attr == NULL ?
-                                   "" : attr->values[0].string.text);
+      cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect");
+      cgiStartHTML(cgiText(_("Change Settings")));
+      cgiCopyTemplateLang("norestart.tmpl");
+    }
 
-       cgiCopyTemplateLang("option-pickone.tmpl");
+    cupsFreeOptions(num_settings, settings);
 
-        cgiSetVariable("KEYWORD", "job_sheets_end");
-       cgiSetVariable("KEYTEXT", cgiText(_("Ending Banner")));
-        cgiSetVariable("DEFCHOICE", attr == NULL && attr->num_values > 1 ?
-                                   "" : attr->values[1].string.text);
+    cgiEndHTML();
+  }
+  else if (cgiGetVariable("SAVECHANGES") && cgiGetVariable("CUPSDCONF"))
+  {
+   /*
+    * Save hand-edited config file...
+    */
 
-       cgiCopyTemplateLang("option-pickone.tmpl");
+    http_status_t status;              /* PUT status */
+    char       tempfile[1024];         /* Temporary new cupsd.conf */
+    int                tempfd;                 /* Temporary file descriptor */
+    cups_file_t        *temp;                  /* Temporary file */
+    const char *start,                 /* Start of line */
+               *end;                   /* End of line */
 
-       cgiCopyTemplateLang("option-trailer.tmpl");
-      }
 
-      if (ippFindAttribute(response, "printer-error-policy-supported",
-                           IPP_TAG_ZERO) ||
-          ippFindAttribute(response, "printer-op-policy-supported",
-                          IPP_TAG_ZERO))
-      {
-       /*
-       * Add the error and operation policy options...
-       */
-
-       cgiSetVariable("GROUP", cgiText(_("Policies")));
-       cgiCopyTemplateLang("option-header.tmpl");
-
-       /*
-        * Error policy...
-       */
-
-        attr = ippFindAttribute(response, "printer-error-policy-supported",
-                               IPP_TAG_ZERO);
-
-        if (attr)
-       {
-         cgiSetSize("CHOICES", attr->num_values);
-         cgiSetSize("TEXT", attr->num_values);
-         for (k = 0; k < attr->num_values; k ++)
-         {
-           cgiSetArray("CHOICES", k, attr->values[k].string.text);
-           cgiSetArray("TEXT", k, attr->values[k].string.text);
-         }
-
-          attr = ippFindAttribute(response, "printer-error-policy",
-                                 IPP_TAG_ZERO);
-
-          cgiSetVariable("KEYWORD", "printer_error_policy");
-         cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
-          cgiSetVariable("DEFCHOICE", attr == NULL ?
-                                     "" : attr->values[0].string.text);
-        }
-
-       cgiCopyTemplateLang("option-pickone.tmpl");
-
-       /*
-        * Operation policy...
-       */
-
-        attr = ippFindAttribute(response, "printer-op-policy-supported",
-                               IPP_TAG_ZERO);
-
-        if (attr)
-       {
-         cgiSetSize("CHOICES", attr->num_values);
-         cgiSetSize("TEXT", attr->num_values);
-         for (k = 0; k < attr->num_values; k ++)
-         {
-           cgiSetArray("CHOICES", k, attr->values[k].string.text);
-           cgiSetArray("TEXT", k, attr->values[k].string.text);
-         }
-
-          attr = ippFindAttribute(response, "printer-op-policy", IPP_TAG_ZERO);
-
-          cgiSetVariable("KEYWORD", "printer_op_policy");
-         cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
-          cgiSetVariable("DEFCHOICE", attr == NULL ?
-                                     "" : attr->values[0].string.text);
-
-         cgiCopyTemplateLang("option-pickone.tmpl");
-        }
-
-       cgiCopyTemplateLang("option-trailer.tmpl");
-      }
-
-      ippDelete(response);
-    }
-
-   /*
-    * Binary protocol support...
-    */
-
-    if (ppd->protocols && strstr(ppd->protocols, "BCP"))
-    {
-      protocol = ppdFindAttr(ppd, "cupsProtocol", NULL);
-
-      cgiSetVariable("GROUP", cgiText(_("PS Binary Protocol")));
-      cgiCopyTemplateLang("option-header.tmpl");
-
-      cgiSetSize("CHOICES", 2);
-      cgiSetSize("TEXT", 2);
-      cgiSetArray("CHOICES", 0, "None");
-      cgiSetArray("TEXT", 0, cgiText(_("None")));
-
-      if (strstr(ppd->protocols, "TBCP"))
-      {
-       cgiSetArray("CHOICES", 1, "TBCP");
-       cgiSetArray("TEXT", 1, "TBCP");
-      }
-      else
-      {
-       cgiSetArray("CHOICES", 1, "BCP");
-       cgiSetArray("TEXT", 1, "BCP");
-      }
-
-      cgiSetVariable("KEYWORD", "protocol");
-      cgiSetVariable("KEYTEXT", cgiText(_("PS Binary Protocol")));
-      cgiSetVariable("DEFCHOICE", protocol ? protocol->value : "None");
-
-      cgiCopyTemplateLang("option-pickone.tmpl");
-
-      cgiCopyTemplateLang("option-trailer.tmpl");
-    }
-
-    cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
-    cgiEndHTML();
-  }
-  else
-  {
-   /*
-    * Set default options...
-    */
-
-    fputs("DEBUG: Setting options...\n", stderr);
-
-    out = cupsTempFile2(tempfile, sizeof(tempfile));
-    in  = cupsFileOpen(filename, "r");
-
-    if (!in || !out)
-    {
-      cgiSetVariable("ERROR", strerror(errno));
-      cgiStartHTML("Set Printer Options");
-      cgiCopyTemplateLang("error.tmpl");
-      cgiEndHTML();
-
-      if (in)
-        cupsFileClose(in);
-
-      if (out)
-      {
-        cupsFileClose(out);
-       unlink(tempfile);
-      }
-
-      unlink(filename);
-      return;
-    }
-
-    while (cupsFileGets(in, line, sizeof(line)))
-    {
-      if (!strncmp(line, "*cupsProtocol:", 14) && cgiGetVariable("protocol"))
-        continue;
-      else if (strncmp(line, "*Default", 8))
-        cupsFilePrintf(out, "%s\n", line);
-      else
-      {
-       /*
-        * Get default option name...
-       */
-
-        strlcpy(keyword, line + 8, sizeof(keyword));
-
-       for (keyptr = keyword; *keyptr; keyptr ++)
-         if (*keyptr == ':' || isspace(*keyptr & 255))
-           break;
-
-        *keyptr = '\0';
-
-        if (!strcmp(keyword, "PageRegion"))
-         var = cgiGetVariable("PageSize");
-       else
-         var = cgiGetVariable(keyword);
-
-        if (var != NULL)
-         cupsFilePrintf(out, "*Default%s: %s\n", keyword, var);
-       else
-         cupsFilePrintf(out, "%s\n", line);
-      }
-    }
-
-    if ((var = cgiGetVariable("protocol")) != NULL)
-      cupsFilePrintf(out, "*cupsProtocol: %s\n", cgiGetVariable("protocol"));
-
-    cupsFileClose(in);
-    cupsFileClose(out);
-
-   /*
-    * Build a CUPS_ADD_PRINTER request, which requires the following
-    * attributes:
-    *
-    *    attributes-charset
-    *    attributes-natural-language
-    *    printer-uri
-    *    job-sheets-default
-    *    [ppd file]
-    */
-
-    request = ippNewRequest(CUPS_ADD_PRINTER);
-
-    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                     "localhost", 0, "/printers/%s",
-                    cgiGetVariable("PRINTER_NAME"));
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-                 NULL, uri);
-
-    attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                         "job-sheets-default", 2, NULL, NULL);
-    attr->values[0].string.text = strdup(cgiGetVariable("job_sheets_start"));
-    attr->values[1].string.text = strdup(cgiGetVariable("job_sheets_end"));
-
-    if ((var = cgiGetVariable("printer_error_policy")) != NULL)
-      attr = ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                          "printer-error-policy", NULL, var);
-
-    if ((var = cgiGetVariable("printer_op_policy")) != NULL)
-      attr = ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                          "printer-op-policy", NULL, var);
-
-   /*
-    * Do the request and get back a response...
-    */
-
-    ippDelete(cupsDoFileRequest(http, request, "/admin/", tempfile));
-
-    if (cupsLastError() > IPP_OK_CONFLICT)
-    {
-      cgiStartHTML(title);
-      cgiShowIPPError(_("Unable to set options:"));
-    }
-    else
-    {
-     /*
-      * Redirect successful updates back to the printer page...
-      */
-
-      char     refresh[1024];          /* Refresh URL */
-
-
-      cgiFormEncode(uri, printer, sizeof(uri));
-      snprintf(refresh, sizeof(refresh),
-               "5;/admin/?OP=redirect&URL=/printers/%s", uri);
-      cgiSetVariable("refresh_page", refresh);
-
-      cgiStartHTML(title);
-
-      cgiCopyTemplateLang("printer-configured.tmpl");
-    }
-
-    cgiEndHTML();
-
-    unlink(tempfile);
-  }
-
-  unlink(filename);
-}
-
-
-/*
- * 'do_config_server()' - Configure server settings.
- */
-
-static void
-do_config_server(http_t *http)         /* I - HTTP connection */
-{
-  if (cgiIsPOST() && !cgiGetVariable("CUPSDCONF"))
-  {
-   /*
-    * Save basic setting changes...
-    */
-
-    int                        num_settings;   /* Number of server settings */
-    cups_option_t      *settings;      /* Server settings */
-
-
-    num_settings = 0;
-    num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING,
-                                 cgiGetVariable("DEBUG_LOGGING") ? "1" : "0",
-                                num_settings, &settings);
-    num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN,
-                                 cgiGetVariable("REMOTE_ADMIN") ? "1" : "0",
-                                num_settings, &settings);
-    num_settings = cupsAddOption(CUPS_SERVER_REMOTE_PRINTERS,
-                                 cgiGetVariable("REMOTE_PRINTERS") ? "1" : "0",
-                                num_settings, &settings);
-    num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS,
-                                 cgiGetVariable("SHARE_PRINTERS") ? "1" : "0",
-                                num_settings, &settings);
-    num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY,
-                                 cgiGetVariable("USER_CANCEL_ANY") ? "1" : "0",
-                                num_settings, &settings);
-
-
-    if (!_cupsAdminSetServerSettings(http, num_settings, settings))
-    {
-      cgiStartHTML(cgiText(_("Change Settings")));
-      cgiSetVariable("MESSAGE",
-                     cgiText(_("Unable to change server settings:")));
-      cgiSetVariable("ERROR", cupsLastErrorString());
-      cgiCopyTemplateLang("error.tmpl");
-    }
-    else
-    {
-      cgiSetVariable("refresh_page", "5;/admin/?OP=redirect");
-      cgiStartHTML(cgiText(_("Change Settings")));
-      cgiCopyTemplateLang("restart.tmpl");
-    }
-
-    cupsFreeOptions(num_settings, settings);
-
-    cgiEndHTML();
-  }
-  else if (cgiIsPOST())
-  {
-   /*
-    * Save hand-edited config file...
-    */
-
-    http_status_t status;              /* PUT status */
-    char       tempfile[1024];         /* Temporary new cupsd.conf */
-    int                tempfd;                 /* Temporary file descriptor */
-    cups_file_t        *temp;                  /* Temporary file */
-    const char *start,                 /* Start of line */
-               *end;                   /* End of line */
-
-
-   /*
-    * Create a temporary file for the new cupsd.conf file...
-    */
+   /*
+    * Create a temporary file for the new cupsd.conf file...
+    */
 
     if ((tempfd = cupsTempFd(tempfile, sizeof(tempfile))) < 0)
     {
       cgiStartHTML(cgiText(_("Edit Configuration File")));
-      cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
+      cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file")));
       cgiSetVariable("ERROR", strerror(errno));
       cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
-      
+
       perror(tempfile);
       return;
     }
@@ -1664,11 +1551,11 @@ do_config_server(http_t *http)          /* I - HTTP connection */
     if ((temp = cupsFileOpenFd(tempfd, "w")) == NULL)
     {
       cgiStartHTML(cgiText(_("Edit Configuration File")));
-      cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file:")));
+      cgiSetVariable("MESSAGE", cgiText(_("Unable to create temporary file")));
       cgiSetVariable("ERROR", strerror(errno));
       cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
-      
+
       perror(tempfile);
       close(tempfd);
       unlink(tempfile);
@@ -1686,7 +1573,7 @@ do_config_server(http_t *http)            /* I - HTTP connection */
         if ((end = strstr(start, "\n")) == NULL)
          end = start + strlen(start);
 
-      cupsFileWrite(temp, start, end - start);
+      cupsFileWrite(temp, start, (size_t)(end - start));
       cupsFilePutChar(temp, '\n');
 
       if (*end == '\r')
@@ -1705,10 +1592,16 @@ do_config_server(http_t *http)          /* I - HTTP connection */
 
     status = cupsPutFile(http, "/admin/conf/cupsd.conf", tempfile);
 
-    if (status != HTTP_CREATED)
+    if (status == HTTP_UNAUTHORIZED)
+    {
+      puts("Status: 401\n");
+      unlink(tempfile);
+      exit(0);
+    }
+    else if (status != HTTP_CREATED)
     {
       cgiSetVariable("MESSAGE",
-                     cgiText(_("Unable to upload cupsd.conf file:")));
+                     cgiText(_("Unable to upload cupsd.conf file")));
       cgiSetVariable("ERROR", httpStatus(status));
 
       cgiStartHTML(cgiText(_("Edit Configuration File")));
@@ -1716,7 +1609,7 @@ do_config_server(http_t *http)            /* I - HTTP connection */
     }
     else
     {
-      cgiSetVariable("refresh_page", "5;/admin/?OP=redirect");
+      cgiSetVariable("refresh_page", "5;URL=/admin/");
 
       cgiStartHTML(cgiText(_("Edit Configuration File")));
       cgiCopyTemplateLang("restart.tmpl");
@@ -1730,7 +1623,10 @@ do_config_server(http_t *http)           /* I - HTTP connection */
   {
     struct stat        info;                   /* cupsd.conf information */
     cups_file_t        *cupsd;                 /* cupsd.conf file */
-    char       *buffer;                /* Buffer for entire file */
+    char       *buffer,                /* Buffer for entire file */
+               *bufptr,                /* Pointer into buffer */
+               *bufend;                /* End of buffer */
+    int                ch;                     /* Character from file */
     char       filename[1024];         /* Filename */
     const char *server_root;           /* Location of config files */
 
@@ -1752,7 +1648,7 @@ do_config_server(http_t *http)            /* I - HTTP connection */
     {
       cgiStartHTML(cgiText(_("Edit Configuration File")));
       cgiSetVariable("MESSAGE",
-                     cgiText(_("Unable to access cupsd.conf file:")));
+                     cgiText(_("Unable to access cupsd.conf file")));
       cgiSetVariable("ERROR", strerror(errno));
       cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
@@ -1765,10 +1661,10 @@ do_config_server(http_t *http)          /* I - HTTP connection */
     {
       cgiStartHTML(cgiText(_("Edit Configuration File")));
       cgiSetVariable("MESSAGE",
-                     cgiText(_("Unable to access cupsd.conf file:")));
+                     cgiText(_("Unable to access cupsd.conf file")));
       cgiSetVariable("ERROR",
                      cgiText(_("Unable to edit cupsd.conf files larger than "
-                              "1MB!")));
+                              "1MB")));
       cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
 
@@ -1789,7 +1685,7 @@ do_config_server(http_t *http)            /* I - HTTP connection */
 
       cgiStartHTML(cgiText(_("Edit Configuration File")));
       cgiSetVariable("MESSAGE",
-                     cgiText(_("Unable to access cupsd.conf file:")));
+                     cgiText(_("Unable to access cupsd.conf file")));
       cgiSetVariable("ERROR", strerror(errno));
       cgiCopyTemplateLang("error.tmpl");
       cgiEndHTML();
@@ -1802,21 +1698,65 @@ do_config_server(http_t *http)          /* I - HTTP connection */
     * Allocate memory and load the file into a string buffer...
     */
 
-    buffer = calloc(1, info.st_size + 1);
+    if ((buffer = calloc(1, (size_t)info.st_size + 1)) != NULL)
+    {
+      cupsFileRead(cupsd, buffer, (size_t)info.st_size);
+      cgiSetVariable("CUPSDCONF", buffer);
+      free(buffer);
+    }
 
-    cupsFileRead(cupsd, buffer, info.st_size);
     cupsFileClose(cupsd);
 
-    cgiSetVariable("CUPSDCONF", buffer);
-    free(buffer);
+   /*
+    * Then get the default cupsd.conf file and put that into a string as
+    * well...
+    */
+
+    strlcat(filename, ".default", sizeof(filename));
+
+    if (!stat(filename, &info) && info.st_size < (1024 * 1024) &&
+        (cupsd = cupsFileOpen(filename, "r")) != NULL)
+    {
+      if ((buffer = calloc(1, 2 * (size_t)info.st_size + 1)) != NULL)
+      {
+       bufend = buffer + 2 * info.st_size - 1;
+
+       for (bufptr = buffer;
+            bufptr < bufend && (ch = cupsFileGetChar(cupsd)) != EOF;)
+       {
+         if (ch == '\\' || ch == '\"')
+         {
+           *bufptr++ = '\\';
+           *bufptr++ = (char)ch;
+         }
+         else if (ch == '\n')
+         {
+           *bufptr++ = '\\';
+           *bufptr++ = 'n';
+         }
+         else if (ch == '\t')
+         {
+           *bufptr++ = '\\';
+           *bufptr++ = 't';
+         }
+         else if (ch >= ' ')
+           *bufptr++ = (char)ch;
+       }
+
+       *bufptr = '\0';
+
+       cgiSetVariable("CUPSDCONF_DEFAULT", buffer);
+       free(buffer);
+      }
+
+      cupsFileClose(cupsd);
+    }
 
    /*
     * Show the current config file...
     */
 
-    cgiStartHTML("Edit Configuration File");
-
-    printf("<!-- \"%s\" -->\n", filename);
+    cgiStartHTML(cgiText(_("Edit Configuration File")));
 
     cgiCopyTemplateLang("edit-config.tmpl");
 
@@ -1826,7 +1766,7 @@ do_config_server(http_t *http)            /* I - HTTP connection */
 
 
 /*
- * 'do_delete_class()' - Delete a class...
+ * 'do_delete_class()' - Delete a class.
  */
 
 static void
@@ -1837,10 +1777,13 @@ do_delete_class(http_t *http)           /* I - HTTP connection */
   const char   *pclass;                /* Printer class name */
 
 
-  cgiStartHTML(cgiText(_("Delete Class")));
+ /*
+  * Get form variables...
+  */
 
   if (cgiGetVariable("CONFIRM") == NULL)
   {
+    cgiStartHTML(cgiText(_("Delete Class")));
     cgiCopyTemplateLang("class-confirm.tmpl");
     cgiEndHTML();
     return;
@@ -1851,7 +1794,8 @@ do_delete_class(http_t *http)             /* I - HTTP connection */
                      "localhost", 0, "/classes/%s", pclass);
   else
   {
-    cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
+    cgiStartHTML(cgiText(_("Delete Class")));
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
     cgiCopyTemplateLang("error.tmpl");
     cgiEndHTML();
     return;
@@ -1877,8 +1821,28 @@ do_delete_class(http_t *http)            /* I - HTTP connection */
 
   ippDelete(cupsDoRequest(http, request, "/admin/"));
 
+ /*
+  * Show the results...
+  */
+
+  if (cupsLastError() == IPP_NOT_AUTHORIZED)
+  {
+    puts("Status: 401\n");
+    exit(0);
+  }
+  else if (cupsLastError() <= IPP_OK_CONFLICT)
+  {
+   /*
+    * Redirect successful updates back to the classes page...
+    */
+
+    cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/classes");
+  }
+
+  cgiStartHTML(cgiText(_("Delete Class")));
+
   if (cupsLastError() > IPP_OK_CONFLICT)
-    cgiShowIPPError(_("Unable to delete class:"));
+    cgiShowIPPError(_("Unable to delete class"));
   else
     cgiCopyTemplateLang("class-deleted.tmpl");
 
@@ -1887,7 +1851,7 @@ do_delete_class(http_t *http)             /* I - HTTP connection */
 
 
 /*
- * 'do_delete_printer()' - Delete a printer...
+ * 'do_delete_printer()' - Delete a printer.
  */
 
 static void
@@ -1898,10 +1862,13 @@ do_delete_printer(http_t *http)         /* I - HTTP connection */
   const char   *printer;               /* Printer printer name */
 
 
-  cgiStartHTML(cgiText(_("Delete Printer")));
+ /*
+  * Get form variables...
+  */
 
   if (cgiGetVariable("CONFIRM") == NULL)
   {
+    cgiStartHTML(cgiText(_("Delete Printer")));
     cgiCopyTemplateLang("printer-confirm.tmpl");
     cgiEndHTML();
     return;
@@ -1912,7 +1879,8 @@ do_delete_printer(http_t *http)           /* I - HTTP connection */
                      "localhost", 0, "/printers/%s", printer);
   else
   {
-    cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
+    cgiStartHTML(cgiText(_("Delete Printer")));
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
     cgiCopyTemplateLang("error.tmpl");
     cgiEndHTML();
     return;
@@ -1938,200 +1906,49 @@ do_delete_printer(http_t *http)                /* I - HTTP connection */
 
   ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-  if (cupsLastError() > IPP_OK_CONFLICT)
-    cgiShowIPPError(_("Unable to delete printer:"));
-  else
-    cgiCopyTemplateLang("printer-deleted.tmpl");
-
-  cgiEndHTML();
-}
-
-
-/*
- * 'do_export()' - Export printers to Samba...
- */
-
-static void
-do_export(http_t *http)                        /* I - HTTP connection */
-{
-  int          i, j;                   /* Looping vars */
-  ipp_t                *request,               /* IPP request */
-               *response;              /* IPP response */
-  const char   *username,              /* Samba username */
-               *password,              /* Samba password */
-               *export_all;            /* Export all printers? */
-  int          export_count,           /* Number of printers to export */
-               printer_count;          /* Number of available printers */
-  const char   *name,                  /* What name to pull */
-               *dest;                  /* Current destination */
-  char         ppd[1024];              /* PPD file */
-
-
  /*
-  * Get form data...
+  * Show the results...
   */
 
-  username     = cgiGetVariable("USERNAME");
-  password     = cgiGetVariable("PASSWORD");
-  export_all   = cgiGetVariable("EXPORT_ALL");
-  export_count = cgiGetSize("EXPORT_NAME");
-
- /*
-  * Get list of available printers...
-  */
-
-  cgiSetSize("PRINTER_NAME", 0);
-  cgiSetSize("PRINTER_EXPORT", 0);
-
-  request = ippNewRequest(CUPS_GET_PRINTERS);
-
-  ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
-                "printer-type", 0);
-
-  ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
-                "printer-type-mask", CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE |
-                                    CUPS_PRINTER_IMPLICIT);
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
-               "requested-attributes", NULL, "printer-name");
-
-  if ((response = cupsDoRequest(http, request, "/")) != NULL)
+  if (cupsLastError() == IPP_NOT_AUTHORIZED)
   {
-    cgiSetIPPVars(response, NULL, NULL, NULL, 0);
-    ippDelete(response);
-
-    if (!export_all)
-    {
-      printer_count = cgiGetSize("PRINTER_NAME");
-
-      for (i = 0; i < printer_count; i ++)
-      {
-        dest = cgiGetArray("PRINTER_NAME", i);
-
-        for (j = 0; j < export_count; j ++)
-         if (!strcasecmp(dest, cgiGetArray("EXPORT_NAME", j)))
-            break;
-
-        cgiSetArray("PRINTER_EXPORT", i, j < export_count ? "Y" : "");
-      }
-    }
+    puts("Status: 401\n");
+    exit(0);
   }
-
- /*
-  * Export or get the printers to export...
-  */
-
-  if (username && *username && password && *password &&
-      (export_all || export_count > 0))
+  else if (cupsLastError() <= IPP_OK_CONFLICT)
   {
    /*
-    * Do export...
+    * Redirect successful updates back to the printers page...
     */
 
-    fputs("DEBUG: Export printers...\n", stderr);
-
-    if (export_all)
-    {
-      name         = "PRINTER_NAME";
-      export_count = cgiGetSize("PRINTER_NAME");
-    }
-    else
-      name = "EXPORT_NAME";
-
-    for (i = 0; i < export_count; i ++)
-    {
-      dest = cgiGetArray(name, i);
-
-      if (!cupsAdminCreateWindowsPPD(http, dest, ppd, sizeof(ppd)))
-        break;
-
-      j = cupsAdminExportSamba(dest, ppd, "localhost", username, password,
-                               stderr);
-
-      unlink(ppd);
-
-      if (!j)
-        break;
-    }
-
-    if (i < export_count)
-      cgiSetVariable("ERROR", cupsLastErrorString());
-    else
-    {
-      cgiStartHTML(cgiText(_("Export Printers to Samba")));
-      cgiCopyTemplateLang("samba-exported.tmpl");
-      cgiEndHTML();
-      return;
-    }
+    cgiSetVariable("refresh_page", "5;URL=/admin/?OP=redirect&URL=/printers");
   }
-  else if (username && !*username)
-    cgiSetVariable("ERROR",
-                   cgiText(_("A Samba username is required to export "
-                            "printer drivers!")));
-  else if (username && (!password || !*password))
-    cgiSetVariable("ERROR",
-                   cgiText(_("A Samba password is required to export "
-                            "printer drivers!")));
 
- /*
-  * Show form...
-  */
+  cgiStartHTML(cgiText(_("Delete Printer")));
+
+  if (cupsLastError() > IPP_OK_CONFLICT)
+    cgiShowIPPError(_("Unable to delete printer"));
+  else
+    cgiCopyTemplateLang("printer-deleted.tmpl");
 
-  cgiStartHTML(cgiText(_("Export Printers to Samba")));
-  cgiCopyTemplateLang("samba-export.tmpl");
   cgiEndHTML();
 }
 
 
 /*
- * 'do_menu()' - Show the main menu...
+ * 'do_list_printers()' - List available printers.
  */
 
 static void
-do_menu(http_t *http)                  /* I - HTTP connection */
+do_list_printers(http_t *http)         /* I - HTTP connection */
 {
-  int          num_settings;           /* Number of server settings */
-  cups_option_t        *settings;              /* Server settings */
-  const char   *val;                   /* Setting value */
-  char         filename[1024];         /* Temporary filename */
-  const char   *datadir;               /* Location of data files */
   ipp_t                *request,               /* IPP request */
                *response;              /* IPP response */
   ipp_attribute_t *attr;               /* IPP attribute */
 
 
- /*
-  * Get the current server settings...
-  */
-
-  if (!_cupsAdminGetServerSettings(http, &num_settings, &settings))
-  {
-    cgiSetVariable("SETTINGS_MESSAGE",
-                   cgiText(_("Unable to open cupsd.conf file:")));
-    cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
-  }
-
-  if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
-                           settings)) != NULL && atoi(val))
-    cgiSetVariable("DEBUG_LOGGING", "CHECKED");
-
-  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
-                           settings)) != NULL && atoi(val))
-    cgiSetVariable("REMOTE_ADMIN", "CHECKED");
-
-  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_PRINTERS, num_settings,
-                           settings)) != NULL && atoi(val))
-    cgiSetVariable("REMOTE_PRINTERS", "CHECKED");
-
-  if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
-                           settings)) != NULL && atoi(val))
-    cgiSetVariable("SHARE_PRINTERS", "CHECKED");
-
-  if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
-                           settings)) != NULL && atoi(val))
-    cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
-
-  cupsFreeOptions(num_settings, settings);
+  cgiStartHTML(cgiText(_("List Available Printers")));
+  fflush(stdout);
 
  /*
   * Get the list of printers and their devices...
@@ -2232,7 +2049,7 @@ do_menu(http_t *http)                     /* I - HTTP connection */
        */
 
        if (device_info && device_make_and_model && device_uri &&
-           strcasecmp(device_make_and_model, "unknown") &&
+           _cups_strcasecmp(device_make_and_model, "unknown") &&
            strchr(device_uri, ':'))
        {
         /*
@@ -2246,8 +2063,8 @@ do_menu(http_t *http)                     /* I - HTTP connection */
            * Not found, so it must be a new printer...
            */
 
-            char       options[1024],  /* Form variables for this device */
-                       *options_ptr;   /* Pointer into string */
+            char       option[1024],   /* Form variables for this device */
+                       *option_ptr;    /* Pointer into string */
            const char  *ptr;           /* Pointer into device string */
 
 
@@ -2259,59 +2076,28 @@ do_menu(http_t *http)                   /* I - HTTP connection */
            * suitable name.
            */
 
-           strcpy(options, "PRINTER_NAME=");
-           options_ptr = options + strlen(options);
-
-            if (strncasecmp(device_info, "unknown", 7))
+            if (_cups_strncasecmp(device_info, "unknown", 7))
              ptr = device_info;
             else if ((ptr = strstr(device_uri, "://")) != NULL)
              ptr += 3;
            else
              ptr = device_make_and_model;
 
-           for (;
-                options_ptr < (options + sizeof(options) - 1) && *ptr;
+           for (option_ptr = option;
+                option_ptr < (option + sizeof(option) - 1) && *ptr;
                 ptr ++)
              if (isalnum(*ptr & 255) || *ptr == '_' || *ptr == '-' ||
                  *ptr == '.')
-               *options_ptr++ = *ptr;
-             else if ((*ptr == ' ' || *ptr == '/') && options_ptr[-1] != '_')
-               *options_ptr++ = '_';
+               *option_ptr++ = *ptr;
+             else if ((*ptr == ' ' || *ptr == '/') && option_ptr > option &&
+                      option_ptr[-1] != '_')
+               *option_ptr++ = '_';
              else if (*ptr == '?' || *ptr == '(')
                break;
 
-           /*
-           * Then add the make and model in the printer info, so
-           * that MacOS clients see something reasonable...
-           */
-
-            strlcpy(options_ptr, "&PRINTER_LOCATION=Local+Printer"
-                                "&PRINTER_INFO=",
-                   sizeof(options) - (options_ptr - options));
-           options_ptr += strlen(options_ptr);
-
-            cgiFormEncode(options_ptr, device_make_and_model,
-                         sizeof(options) - (options_ptr - options));
-           options_ptr += strlen(options_ptr);
-
-           /*
-           * Then copy the device URI...
-           */
-
-           strlcpy(options_ptr, "&DEVICE_URI=",
-                   sizeof(options) - (options_ptr - options));
-           options_ptr += strlen(options_ptr);
-
-            cgiFormEncode(options_ptr, device_uri,
-                         sizeof(options) - (options_ptr - options));
-           options_ptr += strlen(options_ptr);
+            *option_ptr = '\0';
 
-            if (options_ptr < (options + sizeof(options) - 1))
-           {
-             *options_ptr++ = '|';
-             cgiFormEncode(options_ptr, device_make_and_model,
-                         sizeof(options) - (options_ptr - options));
-           }
+            cgiSetArray("TEMPLATE_NAME", i, option);
 
            /*
            * Finally, set the form variables for this printer...
@@ -2319,7 +2105,6 @@ do_menu(http_t *http)                     /* I - HTTP connection */
 
            cgiSetArray("device_info", i, device_info);
            cgiSetArray("device_make_and_model", i, device_make_and_model);
-           cgiSetArray("device_options", i, options);
             cgiSetArray("device_uri", i, device_uri);
            i ++;
          }
@@ -2345,136 +2130,125 @@ do_menu(http_t *http)                 /* I - HTTP connection */
   }
 
  /*
-  * See if Samba and the Windows drivers are installed...
-  */
-
-  if ((datadir = getenv("CUPS_DATADIR")) == NULL)
-    datadir = CUPS_DATADIR;
-
-  snprintf(filename, sizeof(filename), "%s/drivers/pscript5.dll", datadir);
-  if (!access(filename, R_OK))
-  {
-   /*
-    * Found Windows 2000 driver file, see if we have smbclient and
-    * rpcclient...
-    */
-
-    if (cupsFileFind("smbclient", getenv("PATH"), 1, filename,
-                     sizeof(filename)) &&
-        cupsFileFind("rpcclient", getenv("PATH"), 1, filename,
-                    sizeof(filename)))
-      cgiSetVariable("HAVE_SAMBA", "Y");
-    else
-    {
-      if (!cupsFileFind("smbclient", getenv("PATH"), 1, filename,
-                        sizeof(filename)))
-        fputs("ERROR: smbclient not found!\n", stderr);
-
-      if (!cupsFileFind("rpcclient", getenv("PATH"), 1, filename,
-                        sizeof(filename)))
-        fputs("ERROR: rpcclient not found!\n", stderr);
-    }
-  }
-  else
-    perror(filename);
-
- /*
-  * Finally, show the main menu template...
+  * Finally, show the printer list...
   */
 
-  cgiStartHTML(cgiText(_("Administration")));
-
-  cgiCopyTemplateLang("admin.tmpl");
+  cgiCopyTemplateLang("list-available-printers.tmpl");
 
   cgiEndHTML();
 }
 
 
 /*
- * 'do_printer_op()' - Do a printer operation.
+ * 'do_menu()' - Show the main menu.
  */
 
 static void
-do_printer_op(http_t      *http,       /* I - HTTP connection */
-             ipp_op_t    op,           /* I - Operation to perform */
-             const char  *title)       /* I - Title of page */
+do_menu(http_t *http)                  /* I - HTTP connection */
 {
-  ipp_t                *request;               /* IPP request */
-  char         uri[HTTP_MAX_URI];      /* Printer URI */
-  const char   *printer,               /* Printer name (purge-jobs) */
-               *is_class;              /* Is a class? */
+  int          num_settings;           /* Number of server settings */
+  cups_option_t        *settings;              /* Server settings */
+  const char   *val;                   /* Setting value */
 
 
-  is_class = cgiGetVariable("IS_CLASS");
-  printer  = cgiGetVariable("PRINTER_NAME");
+ /*
+  * Get the current server settings...
+  */
 
-  if (!printer)
+  if (!cupsAdminGetServerSettings(http, &num_settings, &settings))
   {
-    cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
-    cgiStartHTML(title);
-    cgiCopyTemplateLang("error.tmpl");
-    cgiEndHTML();
-    return;
+    cgiSetVariable("SETTINGS_MESSAGE",
+                   cgiText(_("Unable to open cupsd.conf file:")));
+    cgiSetVariable("SETTINGS_ERROR", cupsLastErrorString());
   }
 
- /*
-  * Build a printer request, which requires the following
-  * attributes:
-  *
-  *    attributes-charset
-  *    attributes-natural-language
-  *    printer-uri
-  */
+  if ((val = cupsGetOption(CUPS_SERVER_DEBUG_LOGGING, num_settings,
+                           settings)) != NULL && atoi(val))
+    cgiSetVariable("DEBUG_LOGGING", "CHECKED");
 
-  request = ippNewRequest(op);
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ADMIN, num_settings,
+                           settings)) != NULL && atoi(val))
+    cgiSetVariable("REMOTE_ADMIN", "CHECKED");
 
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, is_class ? "/classes/%s" : "/printers/%s",
-                  printer);
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-               NULL, uri);
+  if ((val = cupsGetOption(CUPS_SERVER_REMOTE_ANY, num_settings,
+                           settings)) != NULL && atoi(val))
+    cgiSetVariable("REMOTE_ANY", "CHECKED");
 
- /*
-  * Do the request and get back a response...
-  */
+  if ((val = cupsGetOption(CUPS_SERVER_SHARE_PRINTERS, num_settings,
+                           settings)) != NULL && atoi(val))
+    cgiSetVariable("SHARE_PRINTERS", "CHECKED");
 
-  ippDelete(cupsDoRequest(http, request, "/admin/"));
+  if ((val = cupsGetOption(CUPS_SERVER_USER_CANCEL_ANY, num_settings,
+                           settings)) != NULL && atoi(val))
+    cgiSetVariable("USER_CANCEL_ANY", "CHECKED");
 
-  if (cupsLastError() > IPP_OK_CONFLICT)
+#ifdef HAVE_GSSAPI
+  cgiSetVariable("HAVE_GSSAPI", "1");
+
+  if ((val = cupsGetOption("DefaultAuthType", num_settings,
+                           settings)) != NULL && !_cups_strcasecmp(val, "Negotiate"))
+    cgiSetVariable("KERBEROS", "CHECKED");
+  else
+#endif /* HAVE_GSSAPI */
+  cgiSetVariable("KERBEROS", "");
+
+  if ((val = cupsGetOption("BrowseWebIF", num_settings,
+                           settings)) == NULL)
+    val = "No";
+
+  if (!_cups_strcasecmp(val, "yes") || !_cups_strcasecmp(val, "on") ||
+      !_cups_strcasecmp(val, "true"))
+    cgiSetVariable("BROWSE_WEB_IF", "CHECKED");
+
+  if ((val = cupsGetOption("PreserveJobHistory", num_settings,
+                           settings)) == NULL)
+    val = "Yes";
+
+  if (val &&
+      (!_cups_strcasecmp(val, "0") || !_cups_strcasecmp(val, "no") ||
+       !_cups_strcasecmp(val, "off") || !_cups_strcasecmp(val, "false") ||
+       !_cups_strcasecmp(val, "disabled")))
   {
-    cgiStartHTML(title);
-    cgiShowIPPError(_("Unable to change printer:"));
+    cgiSetVariable("PRESERVE_JOB_HISTORY", "0");
+    cgiSetVariable("PRESERVE_JOB_FILES", "0");
   }
   else
   {
-   /*
-    * Redirect successful updates back to the printer page...
-    */
+    cgiSetVariable("PRESERVE_JOBS", "CHECKED");
+    cgiSetVariable("PRESERVE_JOB_HISTORY", val);
 
-    char       url[1024],              /* Printer/class URL */
-               refresh[1024];          /* Refresh URL */
+    if ((val = cupsGetOption("PreserveJobFiles", num_settings,
+                            settings)) == NULL)
+      val = "1d";
 
+    cgiSetVariable("PRESERVE_JOB_FILES", val);
 
-    cgiRewriteURL(uri, url, sizeof(url), NULL);
-    cgiFormEncode(uri, url, sizeof(uri));
-    snprintf(refresh, sizeof(refresh), "5;/admin/?OP=redirect&URL=%s", uri);
-    cgiSetVariable("refresh_page", refresh);
+  }
 
-    cgiStartHTML(title);
+  if ((val = cupsGetOption("MaxClients", num_settings, settings)) == NULL)
+    val = "100";
 
-    if (op == IPP_PAUSE_PRINTER)
-      cgiCopyTemplateLang("printer-stop.tmpl");
-    else if (op == IPP_RESUME_PRINTER)
-      cgiCopyTemplateLang("printer-start.tmpl");
-    else if (op == CUPS_ACCEPT_JOBS)
-      cgiCopyTemplateLang("printer-accept.tmpl");
-    else if (op == CUPS_REJECT_JOBS)
-      cgiCopyTemplateLang("printer-reject.tmpl");
-    else if (op == IPP_PURGE_JOBS)
-      cgiCopyTemplateLang("printer-purge.tmpl");
-    else if (op == CUPS_SET_DEFAULT)
-      cgiCopyTemplateLang("printer-default.tmpl");
-  }
+  cgiSetVariable("MAX_CLIENTS", val);
+
+  if ((val = cupsGetOption("MaxJobs", num_settings, settings)) == NULL)
+    val = "500";
+
+  cgiSetVariable("MAX_JOBS", val);
+
+  if ((val = cupsGetOption("MaxLogSize", num_settings, settings)) == NULL)
+    val = "1m";
+
+  cgiSetVariable("MAX_LOG_SIZE", val);
+
+  cupsFreeOptions(num_settings, settings);
+
+ /*
+  * Finally, show the main menu template...
+  */
+
+  cgiStartHTML(cgiText(_("Administration")));
+
+  cgiCopyTemplateLang("admin.tmpl");
 
   cgiEndHTML();
 }
@@ -2512,7 +2286,7 @@ do_set_allowed_users(http_t *http)        /* I - HTTP connection */
 
   if (!printer)
   {
-    cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
     cgiStartHTML(cgiText(_("Set Allowed Users")));
     cgiCopyTemplateLang("error.tmpl");
     cgiEndHTML();
@@ -2561,8 +2335,13 @@ do_set_allowed_users(http_t *http)       /* I - HTTP connection */
 
     cgiStartHTML(cgiText(_("Set Allowed Users")));
 
-    if (cupsLastError() > IPP_OK_CONFLICT)
-      cgiShowIPPError(_("Unable to get printer attributes:"));
+    if (cupsLastError() == IPP_NOT_AUTHORIZED)
+    {
+      puts("Status: 401\n");
+      exit(0);
+    }
+    else if (cupsLastError() > IPP_OK_CONFLICT)
+      cgiShowIPPError(_("Unable to get printer attributes"));
     else
       cgiCopyTemplateLang("users.tmpl");
 
@@ -2583,6 +2362,9 @@ do_set_allowed_users(http_t *http)        /* I - HTTP connection */
       while (*ptr == ',' || isspace(*ptr & 255))
        ptr ++;
 
+      if (!*ptr)
+        break;
+
       if (*ptr == '\'' || *ptr == '\"')
       {
        /*
@@ -2648,6 +2430,9 @@ do_set_allowed_users(http_t *http)        /* I - HTTP connection */
         while (*ptr == ',' || isspace(*ptr & 255))
          ptr ++;
 
+        if (!*ptr)
+         break;
+
         if (*ptr == '\'' || *ptr == '\"')
        {
         /*
@@ -2682,7 +2467,7 @@ do_set_allowed_users(http_t *http)        /* I - HTTP connection */
         * Add the name...
        */
 
-        attr->values[i].string.text = strdup(ptr);
+        ippSetString(request, &attr, i, ptr);
 
        /*
         * Advance to the next name...
@@ -2698,10 +2483,15 @@ do_set_allowed_users(http_t *http)      /* I - HTTP connection */
 
     ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    if (cupsLastError() > IPP_OK_CONFLICT)
+    if (cupsLastError() == IPP_NOT_AUTHORIZED)
+    {
+      puts("Status: 401\n");
+      exit(0);
+    }
+    else if (cupsLastError() > IPP_OK_CONFLICT)
     {
       cgiStartHTML(cgiText(_("Set Allowed Users")));
-      cgiShowIPPError(_("Unable to change printer:"));
+      cgiShowIPPError(_("Unable to change printer"));
     }
     else
     {
@@ -2715,7 +2505,8 @@ do_set_allowed_users(http_t *http)        /* I - HTTP connection */
 
       cgiRewriteURL(uri, url, sizeof(url), NULL);
       cgiFormEncode(uri, url, sizeof(uri));
-      snprintf(refresh, sizeof(refresh), "5;/admin/?OP=redirect&URL=%s", uri);
+      snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=%s",
+               uri);
       cgiSetVariable("refresh_page", refresh);
 
       cgiStartHTML(cgiText(_("Set Allowed Users")));
@@ -2730,44 +2521,42 @@ do_set_allowed_users(http_t *http)      /* I - HTTP connection */
 
 
 /*
- * 'do_set_sharing()' - Set printer-is-shared value...
+ * 'do_set_default()' - Set the server default printer/class.
  */
 
 static void
-do_set_sharing(http_t *http)           /* I - HTTP connection */
+do_set_default(http_t *http)           /* I - HTTP connection */
 {
-  ipp_t                *request,               /* IPP request */
-               *response;              /* IPP response */
+  const char   *title;                 /* Page title */
+  ipp_t                *request;               /* IPP request */
   char         uri[HTTP_MAX_URI];      /* Printer URI */
-  const char   *printer,               /* Printer name */
-               *is_class,              /* Is a class? */
-               *shared;                /* Sharing value */
+  const char   *printer,               /* Printer name (purge-jobs) */
+               *is_class;              /* Is a class? */
 
 
   is_class = cgiGetVariable("IS_CLASS");
   printer  = cgiGetVariable("PRINTER_NAME");
-  shared   = cgiGetVariable("SHARED");
+  title    = cgiText(_("Set As Server Default"));
 
-  if (!printer || !shared)
+  if (!printer)
   {
-    cgiSetVariable("ERROR", cgiText(_("Missing form variable!")));
-    cgiStartHTML(cgiText(_("Set Publishing")));
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
+    cgiStartHTML(title);
     cgiCopyTemplateLang("error.tmpl");
     cgiEndHTML();
     return;
   }
 
  /*
-  * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
-  * following attributes:
+  * Build a printer request, which requires the following
+  * attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
-  *    printer-is-shared
   */
 
-  request = ippNewRequest(is_class ? CUPS_ADD_CLASS : CUPS_ADD_PRINTER);
+  request = ippNewRequest(CUPS_SET_DEFAULT);
 
   httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
                    "localhost", 0, is_class ? "/classes/%s" : "/printers/%s",
@@ -2775,23 +2564,853 @@ do_set_sharing(http_t *http)           /* I - HTTP connection */
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                NULL, uri);
 
-  ippAddBoolean(request, IPP_TAG_OPERATION, "printer-is-shared", atoi(shared));
-
  /*
   * Do the request and get back a response...
   */
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
-  {
-    cgiSetIPPVars(response, NULL, NULL, NULL, 0);
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    ippDelete(response);
+  if (cupsLastError() == IPP_NOT_AUTHORIZED)
+  {
+    puts("Status: 401\n");
+    exit(0);
   }
-
-  if (cupsLastError() > IPP_OK_CONFLICT)
+  else if (cupsLastError() > IPP_OK_CONFLICT)
+  {
+    cgiStartHTML(title);
+    cgiShowIPPError(_("Unable to set server default"));
+  }
+  else
+  {
+   /*
+    * Redirect successful updates back to the printer page...
+    */
+
+    char       url[1024],              /* Printer/class URL */
+               refresh[1024];          /* Refresh URL */
+
+
+    cgiRewriteURL(uri, url, sizeof(url), NULL);
+    cgiFormEncode(uri, url, sizeof(uri));
+    snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=%s", uri);
+    cgiSetVariable("refresh_page", refresh);
+
+    cgiStartHTML(title);
+    cgiCopyTemplateLang("printer-default.tmpl");
+  }
+
+  cgiEndHTML();
+}
+
+
+/*
+ * 'do_set_options()' - Configure the default options for a queue.
+ */
+
+static void
+do_set_options(http_t *http,           /* I - HTTP connection */
+               int    is_class)                /* I - Set options for class? */
+{
+  int          i, j, k, m;             /* Looping vars */
+  int          have_options;           /* Have options? */
+  ipp_t                *request,               /* IPP request */
+               *response;              /* IPP response */
+  ipp_attribute_t *attr;               /* IPP attribute */
+  char         uri[HTTP_MAX_URI];      /* Job URI */
+  const char   *var;                   /* Variable value */
+  const char   *printer;               /* Printer printer name */
+  const char   *filename;              /* PPD filename */
+  char         tempfile[1024];         /* Temporary filename */
+  cups_file_t  *in,                    /* Input file */
+               *out;                   /* Output file */
+  char         line[1024],             /* Line from PPD file */
+               value[1024],            /* Option value */
+               keyword[1024],          /* Keyword from Default line */
+               *keyptr;                /* Pointer into keyword... */
+  ppd_file_t   *ppd;                   /* PPD file */
+  ppd_group_t  *group;                 /* Option group */
+  ppd_option_t *option;                /* Option */
+  ppd_coption_t        *coption;               /* Custom option */
+  ppd_cparam_t *cparam;                /* Custom parameter */
+  ppd_attr_t   *ppdattr;               /* PPD attribute */
+  const char   *title;                 /* Page title */
+
+
+  title = cgiText(is_class ? _("Set Class Options") : _("Set Printer Options"));
+
+  fprintf(stderr, "DEBUG: do_set_options(http=%p, is_class=%d)\n", http,
+          is_class);
+
+ /*
+  * Get the printer name...
+  */
+
+  if ((printer = cgiGetVariable("PRINTER_NAME")) != NULL)
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+                     "localhost", 0, is_class ? "/classes/%s" : "/printers/%s",
+                    printer);
+  else
+  {
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
+    cgiStartHTML(title);
+    cgiCopyTemplateLang("error.tmpl");
+    cgiEndHTML();
+    return;
+  }
+
+  fprintf(stderr, "DEBUG: printer=\"%s\", uri=\"%s\"...\n", printer, uri);
+
+ /*
+  * If the user clicks on the Auto-Configure button, send an AutoConfigure
+  * command file to the printer...
+  */
+
+  if (cgiGetVariable("AUTOCONFIGURE"))
+  {
+    cgiPrintCommand(http, printer, "AutoConfigure", "Set Default Options");
+    return;
+  }
+
+ /*
+  * Get the PPD file...
+  */
+
+  if (is_class)
+    filename = NULL;
+  else
+    filename = cupsGetPPD2(http, printer);
+
+  if (filename)
+  {
+    fprintf(stderr, "DEBUG: Got PPD file: \"%s\"\n", filename);
+
+    if ((ppd = ppdOpenFile(filename)) == NULL)
+    {
+      cgiSetVariable("ERROR", ppdErrorString(ppdLastError(&i)));
+      cgiSetVariable("MESSAGE", cgiText(_("Unable to open PPD file")));
+      cgiStartHTML(title);
+      cgiCopyTemplateLang("error.tmpl");
+      cgiEndHTML();
+      return;
+    }
+  }
+  else
+  {
+    fputs("DEBUG: No PPD file\n", stderr);
+    ppd = NULL;
+  }
+
+  if (cgiGetVariable("job_sheets_start") != NULL ||
+      cgiGetVariable("job_sheets_end") != NULL)
+    have_options = 1;
+  else
+    have_options = 0;
+
+  if (ppd)
+  {
+    ppdMarkDefaults(ppd);
+
+    for (option = ppdFirstOption(ppd);
+         option;
+        option = ppdNextOption(ppd))
+    {
+      if ((var = cgiGetVariable(option->keyword)) != NULL)
+      {
+       have_options = 1;
+       ppdMarkOption(ppd, option->keyword, var);
+       fprintf(stderr, "DEBUG: Set %s to %s...\n", option->keyword, var);
+      }
+      else
+        fprintf(stderr, "DEBUG: Didn't find %s...\n", option->keyword);
+    }
+  }
+
+  if (!have_options || ppdConflicts(ppd))
+  {
+   /*
+    * Show the options to the user...
+    */
+
+    fputs("DEBUG: Showing options...\n", stderr);
+
+   /*
+    * Show auto-configure button if supported...
+    */
+
+    if (ppd)
+    {
+      if (ppd->num_filters == 0 ||
+          ((ppdattr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL &&
+           ppdattr->value && strstr(ppdattr->value, "AutoConfigure")))
+        cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
+      else
+      {
+        for (i = 0; i < ppd->num_filters; i ++)
+         if (!strncmp(ppd->filters[i], "application/vnd.cups-postscript", 31))
+         {
+           cgiSetVariable("HAVE_AUTOCONFIGURE", "YES");
+           break;
+         }
+      }
+    }
+
+   /*
+    * Get the printer attributes...
+    */
+
+    request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+                     "localhost", 0, "/printers/%s", printer);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+                 NULL, uri);
+
+    response = cupsDoRequest(http, request, "/");
+
+   /*
+    * List the groups used as "tabs"...
+    */
+
+    i = 0;
+
+    if (ppd)
+    {
+      for (group = ppd->groups;
+          i < ppd->num_groups;
+          i ++, group ++)
+      {
+        cgiSetArray("GROUP_ID", i, group->name);
+
+       if (!strcmp(group->name, "InstallableOptions"))
+         cgiSetArray("GROUP", i, cgiText(_("Options Installed")));
+       else
+         cgiSetArray("GROUP", i, group->text);
+      }
+    }
+
+    if (ippFindAttribute(response, "job-sheets-supported", IPP_TAG_ZERO))
+    {
+      cgiSetArray("GROUP_ID", i, "CUPS_BANNERS");
+      cgiSetArray("GROUP", i ++, cgiText(_("Banners")));
+    }
+
+    if (ippFindAttribute(response, "printer-error-policy-supported",
+                        IPP_TAG_ZERO) ||
+       ippFindAttribute(response, "printer-op-policy-supported",
+                        IPP_TAG_ZERO))
+    {
+      cgiSetArray("GROUP_ID", i, "CUPS_POLICIES");
+      cgiSetArray("GROUP", i ++, cgiText(_("Policies")));
+    }
+
+    if ((attr = ippFindAttribute(response, "port-monitor-supported",
+                                 IPP_TAG_NAME)) != NULL && attr->num_values > 1)
+    {
+      cgiSetArray("GROUP_ID", i, "CUPS_PORT_MONITOR");
+      cgiSetArray("GROUP", i, cgiText(_("Port Monitor")));
+    }
+
+    cgiStartHTML(cgiText(_("Set Printer Options")));
+    cgiCopyTemplateLang("set-printer-options-header.tmpl");
+
+    if (ppd)
+    {
+      ppdLocalize(ppd);
+
+      if (ppdConflicts(ppd))
+      {
+       for (i = ppd->num_groups, k = 0, group = ppd->groups;
+            i > 0;
+            i --, group ++)
+         for (j = group->num_options, option = group->options;
+              j > 0;
+              j --, option ++)
+           if (option->conflicted)
+           {
+             cgiSetArray("ckeyword", k, option->keyword);
+             cgiSetArray("ckeytext", k, option->text);
+
+             for (m = 0; m < option->num_choices; m ++)
+             {
+               if (option->choices[m].marked)
+               {
+                 cgiSetArray("cchoice", k, option->choices[m].text);
+                 break;
+               }
+              }
+
+             k ++;
+           }
+
+       cgiCopyTemplateLang("option-conflict.tmpl");
+      }
+
+      for (i = ppd->num_groups, group = ppd->groups;
+          i > 0;
+          i --, group ++)
+      {
+       for (j = group->num_options, option = group->options;
+            j > 0;
+            j --, option ++)
+       {
+         if (!strcmp(option->keyword, "PageRegion"))
+           continue;
+
+         if (option->num_choices > 1)
+           break;
+       }
+
+        if (j == 0)
+         continue;
+
+        cgiSetVariable("GROUP_ID", group->name);
+
+       if (!strcmp(group->name, "InstallableOptions"))
+         cgiSetVariable("GROUP", cgiText(_("Options Installed")));
+       else
+         cgiSetVariable("GROUP", group->text);
+
+       cgiCopyTemplateLang("option-header.tmpl");
+
+       for (j = group->num_options, option = group->options;
+            j > 0;
+            j --, option ++)
+       {
+         if (!strcmp(option->keyword, "PageRegion") || option->num_choices < 2)
+           continue;
+
+         cgiSetVariable("KEYWORD", option->keyword);
+         cgiSetVariable("KEYTEXT", option->text);
+
+         if (option->conflicted)
+           cgiSetVariable("CONFLICTED", "1");
+         else
+           cgiSetVariable("CONFLICTED", "0");
+
+         cgiSetSize("CHOICES", 0);
+         cgiSetSize("TEXT", 0);
+         for (k = 0, m = 0; k < option->num_choices; k ++)
+         {
+           cgiSetArray("CHOICES", m, option->choices[k].choice);
+           cgiSetArray("TEXT", m, option->choices[k].text);
+
+           m ++;
+
+           if (option->choices[k].marked)
+             cgiSetVariable("DEFCHOICE", option->choices[k].choice);
+         }
+
+         cgiSetSize("PARAMS", 0);
+         cgiSetSize("PARAMTEXT", 0);
+         cgiSetSize("PARAMVALUE", 0);
+         cgiSetSize("INPUTTYPE", 0);
+
+         if ((coption = ppdFindCustomOption(ppd, option->keyword)))
+         {
+            const char *units = NULL;  /* Units value, if any */
+
+           cgiSetVariable("ISCUSTOM", "1");
+
+           for (cparam = ppdFirstCustomParam(coption), m = 0;
+                cparam;
+                cparam = ppdNextCustomParam(coption), m ++)
+           {
+             if (!_cups_strcasecmp(option->keyword, "PageSize") &&
+                 _cups_strcasecmp(cparam->name, "Width") &&
+                 _cups_strcasecmp(cparam->name, "Height"))
+              {
+               m --;
+               continue;
+              }
+
+             cgiSetArray("PARAMS", m, cparam->name);
+             cgiSetArray("PARAMTEXT", m, cparam->text);
+             cgiSetArray("INPUTTYPE", m, "text");
+
+             switch (cparam->type)
+             {
+               case PPD_CUSTOM_UNKNOWN :
+                   break;
+
+               case PPD_CUSTOM_POINTS :
+                   if (!_cups_strncasecmp(option->defchoice, "Custom.", 7))
+                   {
+                     units = option->defchoice + strlen(option->defchoice) - 2;
+
+                     if (strcmp(units, "mm") && strcmp(units, "cm") &&
+                         strcmp(units, "in") && strcmp(units, "ft"))
+                     {
+                       if (units[1] == 'm')
+                         units ++;
+                       else
+                         units = "pt";
+                     }
+                   }
+                   else
+                     units = "pt";
+
+                    if (!strcmp(units, "mm"))
+                     snprintf(value, sizeof(value), "%g",
+                              cparam->current.custom_points / 72.0 * 25.4);
+                    else if (!strcmp(units, "cm"))
+                     snprintf(value, sizeof(value), "%g",
+                              cparam->current.custom_points / 72.0 * 2.54);
+                    else if (!strcmp(units, "in"))
+                     snprintf(value, sizeof(value), "%g",
+                              cparam->current.custom_points / 72.0);
+                    else if (!strcmp(units, "ft"))
+                     snprintf(value, sizeof(value), "%g",
+                              cparam->current.custom_points / 72.0 / 12.0);
+                    else if (!strcmp(units, "m"))
+                     snprintf(value, sizeof(value), "%g",
+                              cparam->current.custom_points / 72.0 * 0.0254);
+                    else
+                     snprintf(value, sizeof(value), "%g",
+                              cparam->current.custom_points);
+                   cgiSetArray("PARAMVALUE", m, value);
+                   break;
+
+               case PPD_CUSTOM_CURVE :
+               case PPD_CUSTOM_INVCURVE :
+               case PPD_CUSTOM_REAL :
+                   snprintf(value, sizeof(value), "%g",
+                            cparam->current.custom_real);
+                   cgiSetArray("PARAMVALUE", m, value);
+                   break;
+
+               case PPD_CUSTOM_INT:
+                   snprintf(value, sizeof(value), "%d",
+                            cparam->current.custom_int);
+                   cgiSetArray("PARAMVALUE", m, value);
+                   break;
+
+               case PPD_CUSTOM_PASSCODE:
+               case PPD_CUSTOM_PASSWORD:
+                   if (cparam->current.custom_password)
+                     cgiSetArray("PARAMVALUE", m,
+                                 cparam->current.custom_password);
+                   else
+                     cgiSetArray("PARAMVALUE", m, "");
+                   cgiSetArray("INPUTTYPE", m, "password");
+                   break;
+
+               case PPD_CUSTOM_STRING:
+                   if (cparam->current.custom_string)
+                     cgiSetArray("PARAMVALUE", m,
+                                 cparam->current.custom_string);
+                   else
+                     cgiSetArray("PARAMVALUE", m, "");
+                   break;
+             }
+           }
+
+            if (units)
+           {
+             cgiSetArray("PARAMS", m, "Units");
+             cgiSetArray("PARAMTEXT", m, cgiText(_("Units")));
+             cgiSetArray("PARAMVALUE", m, units);
+           }
+         }
+         else
+           cgiSetVariable("ISCUSTOM", "0");
+
+         switch (option->ui)
+         {
+           case PPD_UI_BOOLEAN :
+               cgiCopyTemplateLang("option-boolean.tmpl");
+               break;
+           case PPD_UI_PICKONE :
+               cgiCopyTemplateLang("option-pickone.tmpl");
+               break;
+           case PPD_UI_PICKMANY :
+               cgiCopyTemplateLang("option-pickmany.tmpl");
+               break;
+         }
+       }
+
+       cgiCopyTemplateLang("option-trailer.tmpl");
+      }
+    }
+
+    if ((attr = ippFindAttribute(response, "job-sheets-supported",
+                                IPP_TAG_ZERO)) != NULL)
+    {
+     /*
+      * Add the job sheets options...
+      */
+
+      cgiSetVariable("GROUP_ID", "CUPS_BANNERS");
+      cgiSetVariable("GROUP", cgiText(_("Banners")));
+      cgiCopyTemplateLang("option-header.tmpl");
+
+      cgiSetSize("CHOICES", attr->num_values);
+      cgiSetSize("TEXT", attr->num_values);
+      for (k = 0; k < attr->num_values; k ++)
+      {
+       cgiSetArray("CHOICES", k, attr->values[k].string.text);
+       cgiSetArray("TEXT", k, attr->values[k].string.text);
+      }
+
+      attr = ippFindAttribute(response, "job-sheets-default", IPP_TAG_ZERO);
+
+      cgiSetVariable("KEYWORD", "job_sheets_start");
+      cgiSetVariable("KEYTEXT",
+                     /* TRANSLATORS: Banner/cover sheet before the print job. */
+                     cgiText(_("Starting Banner")));
+      cgiSetVariable("DEFCHOICE", attr != NULL ?
+                                 attr->values[0].string.text : "");
+
+      cgiCopyTemplateLang("option-pickone.tmpl");
+
+      cgiSetVariable("KEYWORD", "job_sheets_end");
+      cgiSetVariable("KEYTEXT",
+                     /* TRANSLATORS: Banner/cover sheet after the print job. */
+                     cgiText(_("Ending Banner")));
+      cgiSetVariable("DEFCHOICE", attr != NULL && attr->num_values > 1 ?
+                                 attr->values[1].string.text : "");
+
+      cgiCopyTemplateLang("option-pickone.tmpl");
+
+      cgiCopyTemplateLang("option-trailer.tmpl");
+    }
+
+    if (ippFindAttribute(response, "printer-error-policy-supported",
+                        IPP_TAG_ZERO) ||
+       ippFindAttribute(response, "printer-op-policy-supported",
+                        IPP_TAG_ZERO))
+    {
+     /*
+      * Add the error and operation policy options...
+      */
+
+      cgiSetVariable("GROUP_ID", "CUPS_POLICIES");
+      cgiSetVariable("GROUP", cgiText(_("Policies")));
+      cgiCopyTemplateLang("option-header.tmpl");
+
+     /*
+      * Error policy...
+      */
+
+      attr = ippFindAttribute(response, "printer-error-policy-supported",
+                             IPP_TAG_ZERO);
+
+      if (attr)
+      {
+       cgiSetSize("CHOICES", attr->num_values);
+       cgiSetSize("TEXT", attr->num_values);
+       for (k = 0; k < attr->num_values; k ++)
+       {
+         cgiSetArray("CHOICES", k, attr->values[k].string.text);
+         cgiSetArray("TEXT", k, attr->values[k].string.text);
+       }
+
+       attr = ippFindAttribute(response, "printer-error-policy",
+                               IPP_TAG_ZERO);
+
+       cgiSetVariable("KEYWORD", "printer_error_policy");
+       cgiSetVariable("KEYTEXT", cgiText(_("Error Policy")));
+       cgiSetVariable("DEFCHOICE", attr == NULL ?
+                                   "" : attr->values[0].string.text);
+      }
+
+      cgiCopyTemplateLang("option-pickone.tmpl");
+
+     /*
+      * Operation policy...
+      */
+
+      attr = ippFindAttribute(response, "printer-op-policy-supported",
+                             IPP_TAG_ZERO);
+
+      if (attr)
+      {
+       cgiSetSize("CHOICES", attr->num_values);
+       cgiSetSize("TEXT", attr->num_values);
+       for (k = 0; k < attr->num_values; k ++)
+       {
+         cgiSetArray("CHOICES", k, attr->values[k].string.text);
+         cgiSetArray("TEXT", k, attr->values[k].string.text);
+       }
+
+       attr = ippFindAttribute(response, "printer-op-policy", IPP_TAG_ZERO);
+
+       cgiSetVariable("KEYWORD", "printer_op_policy");
+       cgiSetVariable("KEYTEXT", cgiText(_("Operation Policy")));
+       cgiSetVariable("DEFCHOICE", attr == NULL ?
+                                   "" : attr->values[0].string.text);
+
+       cgiCopyTemplateLang("option-pickone.tmpl");
+      }
+
+      cgiCopyTemplateLang("option-trailer.tmpl");
+    }
+
+   /*
+    * Binary protocol support...
+    */
+
+    if ((attr = ippFindAttribute(response, "port-monitor-supported",
+                                 IPP_TAG_NAME)) != NULL && attr->num_values > 1)
+    {
+      cgiSetVariable("GROUP_ID", "CUPS_PORT_MONITOR");
+      cgiSetVariable("GROUP", cgiText(_("Port Monitor")));
+
+      cgiSetSize("CHOICES", attr->num_values);
+      cgiSetSize("TEXT", attr->num_values);
+
+      for (i = 0; i < attr->num_values; i ++)
+      {
+        cgiSetArray("CHOICES", i, attr->values[i].string.text);
+        cgiSetArray("TEXT", i, attr->values[i].string.text);
+      }
+
+      attr = ippFindAttribute(response, "port-monitor", IPP_TAG_NAME);
+      cgiSetVariable("KEYWORD", "port_monitor");
+      cgiSetVariable("KEYTEXT", cgiText(_("Port Monitor")));
+      cgiSetVariable("DEFCHOICE", attr ? attr->values[0].string.text : "none");
+
+      cgiCopyTemplateLang("option-header.tmpl");
+      cgiCopyTemplateLang("option-pickone.tmpl");
+      cgiCopyTemplateLang("option-trailer.tmpl");
+    }
+
+    cgiCopyTemplateLang("set-printer-options-trailer.tmpl");
+    cgiEndHTML();
+
+    ippDelete(response);
+  }
+  else
+  {
+   /*
+    * Set default options...
+    */
+
+    fputs("DEBUG: Setting options...\n", stderr);
+
+    if (filename)
+    {
+      out = cupsTempFile2(tempfile, sizeof(tempfile));
+      in  = cupsFileOpen(filename, "r");
+
+      if (!in || !out)
+      {
+       cgiSetVariable("ERROR", strerror(errno));
+       cgiStartHTML(cgiText(_("Set Printer Options")));
+       cgiCopyTemplateLang("error.tmpl");
+       cgiEndHTML();
+
+       if (in)
+         cupsFileClose(in);
+
+       if (out)
+       {
+         cupsFileClose(out);
+         unlink(tempfile);
+       }
+
+       unlink(filename);
+       return;
+      }
+
+      while (cupsFileGets(in, line, sizeof(line)))
+      {
+       if (!strncmp(line, "*cupsProtocol:", 14))
+         continue;
+       else if (strncmp(line, "*Default", 8))
+         cupsFilePrintf(out, "%s\n", line);
+       else
+       {
+        /*
+         * Get default option name...
+         */
+
+         strlcpy(keyword, line + 8, sizeof(keyword));
+
+         for (keyptr = keyword; *keyptr; keyptr ++)
+           if (*keyptr == ':' || isspace(*keyptr & 255))
+             break;
+
+         *keyptr = '\0';
+
+         if (!strcmp(keyword, "PageRegion") ||
+             !strcmp(keyword, "PaperDimension") ||
+             !strcmp(keyword, "ImageableArea"))
+           var = get_option_value(ppd, "PageSize", value, sizeof(value));
+         else
+           var = get_option_value(ppd, keyword, value, sizeof(value));
+
+         if (!var)
+           cupsFilePrintf(out, "%s\n", line);
+         else
+           cupsFilePrintf(out, "*Default%s: %s\n", keyword, var);
+       }
+      }
+
+      cupsFileClose(in);
+      cupsFileClose(out);
+    }
+    else
+    {
+     /*
+      * Make sure temporary filename is cleared when there is no PPD...
+      */
+
+      tempfile[0] = '\0';
+    }
+
+   /*
+    * Build a CUPS_ADD_MODIFY_CLASS/PRINTER request, which requires the
+    * following attributes:
+    *
+    *    attributes-charset
+    *    attributes-natural-language
+    *    printer-uri
+    *    job-sheets-default
+    *    printer-error-policy
+    *    printer-op-policy
+    *    [ppd file]
+    */
+
+    request = ippNewRequest(is_class ? CUPS_ADD_MODIFY_CLASS :
+                                       CUPS_ADD_MODIFY_PRINTER);
+
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+                 NULL, uri);
+
+    attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                         "job-sheets-default", 2, NULL, NULL);
+    ippSetString(request, &attr, 0, cgiGetVariable("job_sheets_start"));
+    ippSetString(request, &attr, 1, cgiGetVariable("job_sheets_end"));
+
+    if ((var = cgiGetVariable("printer_error_policy")) != NULL)
+      ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                  "printer-error-policy", NULL, var);
+
+    if ((var = cgiGetVariable("printer_op_policy")) != NULL)
+      ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                  "printer-op-policy", NULL, var);
+
+    if ((var = cgiGetVariable("port_monitor")) != NULL)
+      ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                  "port-monitor", NULL, var);
+
+   /*
+    * Do the request and get back a response...
+    */
+
+    if (filename)
+      ippDelete(cupsDoFileRequest(http, request, "/admin/", tempfile));
+    else
+      ippDelete(cupsDoRequest(http, request, "/admin/"));
+
+    if (cupsLastError() == IPP_NOT_AUTHORIZED)
+    {
+      puts("Status: 401\n");
+      exit(0);
+    }
+    else if (cupsLastError() > IPP_OK_CONFLICT)
+    {
+      cgiStartHTML(title);
+      cgiShowIPPError(_("Unable to set options"));
+    }
+    else
+    {
+     /*
+      * Redirect successful updates back to the printer page...
+      */
+
+      char     refresh[1024];          /* Refresh URL */
+
+
+      cgiFormEncode(uri, printer, sizeof(uri));
+      snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=/%s/%s",
+              is_class ? "classes" : "printers", uri);
+      cgiSetVariable("refresh_page", refresh);
+
+      cgiStartHTML(title);
+
+      cgiCopyTemplateLang("printer-configured.tmpl");
+    }
+
+    cgiEndHTML();
+
+    if (filename)
+      unlink(tempfile);
+  }
+
+  if (filename)
+    unlink(filename);
+}
+
+
+/*
+ * 'do_set_sharing()' - Set printer-is-shared value.
+ */
+
+static void
+do_set_sharing(http_t *http)           /* I - HTTP connection */
+{
+  ipp_t                *request,               /* IPP request */
+               *response;              /* IPP response */
+  char         uri[HTTP_MAX_URI];      /* Printer URI */
+  const char   *printer,               /* Printer name */
+               *is_class,              /* Is a class? */
+               *shared;                /* Sharing value */
+
+
+  is_class = cgiGetVariable("IS_CLASS");
+  printer  = cgiGetVariable("PRINTER_NAME");
+  shared   = cgiGetVariable("SHARED");
+
+  if (!printer || !shared)
+  {
+    cgiSetVariable("ERROR", cgiText(_("Missing form variable")));
+    cgiStartHTML(cgiText(_("Set Publishing")));
+    cgiCopyTemplateLang("error.tmpl");
+    cgiEndHTML();
+    return;
+  }
+
+ /*
+  * Build a CUPS-Add-Printer/CUPS-Add-Class request, which requires the
+  * following attributes:
+  *
+  *    attributes-charset
+  *    attributes-natural-language
+  *    printer-uri
+  *    printer-is-shared
+  */
+
+  request = ippNewRequest(is_class ? CUPS_ADD_CLASS : CUPS_ADD_PRINTER);
+
+  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+                   "localhost", 0, is_class ? "/classes/%s" : "/printers/%s",
+                  printer);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
+               NULL, uri);
+
+  ippAddBoolean(request, IPP_TAG_OPERATION, "printer-is-shared", (char)atoi(shared));
+
+ /*
+  * Do the request and get back a response...
+  */
+
+  if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
+  {
+    cgiSetIPPVars(response, NULL, NULL, NULL, 0);
+
+    ippDelete(response);
+  }
+
+  if (cupsLastError() == IPP_NOT_AUTHORIZED)
+  {
+    puts("Status: 401\n");
+    exit(0);
+  }
+  else if (cupsLastError() > IPP_OK_CONFLICT)
   {
     cgiStartHTML(cgiText(_("Set Publishing")));
-    cgiShowIPPError(_("Unable to change printer-is-shared attribute:"));
+    cgiShowIPPError(_("Unable to change printer-is-shared attribute"));
   }
   else
   {
@@ -2805,7 +3424,7 @@ do_set_sharing(http_t *http)              /* I - HTTP connection */
 
     cgiRewriteURL(uri, url, sizeof(url), NULL);
     cgiFormEncode(uri, url, sizeof(uri));
-    snprintf(refresh, sizeof(refresh), "5;/admin/?OP=redirect&URL=%s", uri);
+    snprintf(refresh, sizeof(refresh), "5;URL=/admin/?OP=redirect&URL=%s", uri);
     cgiSetVariable("refresh_page", refresh);
 
     cgiStartHTML(cgiText(_("Set Publishing")));
@@ -2818,54 +3437,373 @@ do_set_sharing(http_t *http)           /* I - HTTP connection */
 
 
 /*
- * 'match_string()' - Return the number of matching characters.
+ * 'get_option_value()' - Return the value of an option.
+ *
+ * This function also handles generation of custom option values.
  */
 
-static int                             /* O - Number of matching characters */
-match_string(const char *a,            /* I - First string */
-             const char *b)            /* I - Second string */
+static char *                          /* O - Value string or NULL on error */
+get_option_value(
+    ppd_file_t    *ppd,                        /* I - PPD file */
+    const char    *name,               /* I - Option name */
+    char          *buffer,             /* I - String buffer */
+    size_t        bufsize)             /* I - Size of buffer */
 {
-  int  count;                          /* Number of matching characters */
+  char         *bufptr,                /* Pointer into buffer */
+               *bufend;                /* End of buffer */
+  ppd_coption_t *coption;              /* Custom option */
+  ppd_cparam_t *cparam;                /* Current custom parameter */
+  char         keyword[256];           /* Parameter name */
+  const char   *val,                   /* Parameter value */
+               *uval;                  /* Units value */
+  long         integer;                /* Integer value */
+  double       number,                 /* Number value */
+               number_points;          /* Number in points */
 
 
  /*
-  * Loop through both strings until we hit the end of either or we find
-  * a non-matching character.  For the purposes of comparison, we ignore
-  * whitespace and do a case-insensitive comparison so that we have a
-  * better chance of finding a match...
+  * See if we have a custom option choice...
   */
 
-  for (count = 0; *a && *b; a++, b++, count ++)
+  if ((val = cgiGetVariable(name)) == NULL)
   {
    /*
-    * Skip leading whitespace characters...
+    * Option not found!
     */
 
-    while (isspace(*a & 255))
-      a ++;
-
-    while (isspace(*b & 255))
-      b ++;
-
+    return (NULL);
+  }
+  else if (_cups_strcasecmp(val, "Custom") ||
+           (coption = ppdFindCustomOption(ppd, name)) == NULL)
+  {
    /*
-    * Break out if we run out of characters...
+    * Not a custom choice...
     */
 
-    if (!*a || !*b)
-      break;
+    strlcpy(buffer, val, bufsize);
+    return (buffer);
+  }
 
  /*
-    * Do a case-insensitive comparison of the next two chars...
-    */
+ /*
+  * OK, we have a custom option choice, format it...
+  */
 
-    if (tolower(*a & 255) != tolower(*b & 255))
-      break;
+  *buffer = '\0';
+
+  if (!strcmp(coption->keyword, "PageSize"))
+  {
+    const char *lval;                  /* Length string value */
+    double     width,                  /* Width value */
+               width_points,           /* Width in points */
+               length,                 /* Length value */
+               length_points;          /* Length in points */
+
+
+    val  = cgiGetVariable("PageSize.Width");
+    lval = cgiGetVariable("PageSize.Height");
+    uval = cgiGetVariable("PageSize.Units");
+
+    if (!val || !lval || !uval ||
+        (width = strtod(val, NULL)) == 0.0 ||
+        (length = strtod(lval, NULL)) == 0.0 ||
+        (strcmp(uval, "pt") && strcmp(uval, "in") && strcmp(uval, "ft") &&
+        strcmp(uval, "cm") && strcmp(uval, "mm") && strcmp(uval, "m")))
+      return (NULL);
+
+    width_points  = get_points(width, uval);
+    length_points = get_points(length, uval);
+
+    if (width_points < ppd->custom_min[0] ||
+        width_points > ppd->custom_max[0] ||
+        length_points < ppd->custom_min[1] ||
+       length_points > ppd->custom_max[1])
+      return (NULL);
+
+    snprintf(buffer, bufsize, "Custom.%gx%g%s", width, length, uval);
+  }
+  else if (cupsArrayCount(coption->params) == 1)
+  {
+    cparam = ppdFirstCustomParam(coption);
+    snprintf(keyword, sizeof(keyword), "%s.%s", coption->keyword, cparam->name);
+
+    if ((val = cgiGetVariable(keyword)) == NULL)
+      return (NULL);
+
+    switch (cparam->type)
+    {
+      case PPD_CUSTOM_UNKNOWN :
+         break;
+
+      case PPD_CUSTOM_CURVE :
+      case PPD_CUSTOM_INVCURVE :
+      case PPD_CUSTOM_REAL :
+         if ((number = strtod(val, NULL)) == 0.0 ||
+             number < cparam->minimum.custom_real ||
+             number > cparam->maximum.custom_real)
+           return (NULL);
+
+          snprintf(buffer, bufsize, "Custom.%g", number);
+          break;
+
+      case PPD_CUSTOM_INT :
+          if (!*val || (integer = strtol(val, NULL, 10)) == LONG_MIN ||
+             integer == LONG_MAX ||
+             integer < cparam->minimum.custom_int ||
+             integer > cparam->maximum.custom_int)
+            return (NULL);
+
+          snprintf(buffer, bufsize, "Custom.%ld", integer);
+          break;
+
+      case PPD_CUSTOM_POINTS :
+          snprintf(keyword, sizeof(keyword), "%s.Units", coption->keyword);
+
+         if ((number = strtod(val, NULL)) == 0.0 ||
+             (uval = cgiGetVariable(keyword)) == NULL ||
+             (strcmp(uval, "pt") && strcmp(uval, "in") && strcmp(uval, "ft") &&
+              strcmp(uval, "cm") && strcmp(uval, "mm") && strcmp(uval, "m")))
+           return (NULL);
+
+         number_points = get_points(number, uval);
+         if (number_points < cparam->minimum.custom_points ||
+             number_points > cparam->maximum.custom_points)
+           return (NULL);
+
+         snprintf(buffer, bufsize, "Custom.%g%s", number, uval);
+          break;
+
+      case PPD_CUSTOM_PASSCODE :
+          for (uval = val; *uval; uval ++)
+           if (!isdigit(*uval & 255))
+             return (NULL);
+
+      case PPD_CUSTOM_PASSWORD :
+      case PPD_CUSTOM_STRING :
+          integer = (long)strlen(val);
+         if (integer < cparam->minimum.custom_string ||
+             integer > cparam->maximum.custom_string)
+           return (NULL);
+
+          snprintf(buffer, bufsize, "Custom.%s", val);
+         break;
+    }
+  }
+  else
+  {
+    const char *prefix = "{";          /* Prefix string */
+
+
+    bufptr = buffer;
+    bufend = buffer + bufsize;
+
+    for (cparam = ppdFirstCustomParam(coption);
+        cparam;
+        cparam = ppdNextCustomParam(coption))
+    {
+      snprintf(keyword, sizeof(keyword), "%s.%s", coption->keyword,
+               cparam->name);
+
+      if ((val = cgiGetVariable(keyword)) == NULL)
+       return (NULL);
+
+      snprintf(bufptr, (size_t)(bufend - bufptr), "%s%s=", prefix, cparam->name);
+      bufptr += strlen(bufptr);
+      prefix = " ";
+
+      switch (cparam->type)
+      {
+       case PPD_CUSTOM_UNKNOWN :
+           break;
+
+       case PPD_CUSTOM_CURVE :
+       case PPD_CUSTOM_INVCURVE :
+       case PPD_CUSTOM_REAL :
+           if ((number = strtod(val, NULL)) == 0.0 ||
+               number < cparam->minimum.custom_real ||
+               number > cparam->maximum.custom_real)
+             return (NULL);
+
+           snprintf(bufptr, (size_t)(bufend - bufptr), "%g", number);
+           break;
+
+       case PPD_CUSTOM_INT :
+           if (!*val || (integer = strtol(val, NULL, 10)) == LONG_MIN ||
+               integer == LONG_MAX ||
+               integer < cparam->minimum.custom_int ||
+               integer > cparam->maximum.custom_int)
+             return (NULL);
+
+           snprintf(bufptr, (size_t)(bufend - bufptr), "%ld", integer);
+           break;
+
+       case PPD_CUSTOM_POINTS :
+           snprintf(keyword, sizeof(keyword), "%s.Units", coption->keyword);
+
+           if ((number = strtod(val, NULL)) == 0.0 ||
+               (uval = cgiGetVariable(keyword)) == NULL ||
+               (strcmp(uval, "pt") && strcmp(uval, "in") &&
+                strcmp(uval, "ft") && strcmp(uval, "cm") &&
+                strcmp(uval, "mm") && strcmp(uval, "m")))
+             return (NULL);
+
+           number_points = get_points(number, uval);
+           if (number_points < cparam->minimum.custom_points ||
+               number_points > cparam->maximum.custom_points)
+             return (NULL);
+
+           snprintf(bufptr, (size_t)(bufend - bufptr), "%g%s", number, uval);
+           break;
+
+       case PPD_CUSTOM_PASSCODE :
+           for (uval = val; *uval; uval ++)
+             if (!isdigit(*uval & 255))
+               return (NULL);
+
+       case PPD_CUSTOM_PASSWORD :
+       case PPD_CUSTOM_STRING :
+           integer = (long)strlen(val);
+           if (integer < cparam->minimum.custom_string ||
+               integer > cparam->maximum.custom_string)
+             return (NULL);
+
+           if ((bufptr + 2) > bufend)
+             return (NULL);
+
+           bufend --;
+           *bufptr++ = '\"';
+
+           while (*val && bufptr < bufend)
+           {
+             if (*val == '\\' || *val == '\"')
+             {
+               if ((bufptr + 1) >= bufend)
+                 return (NULL);
+
+               *bufptr++ = '\\';
+             }
+
+             *bufptr++ = *val++;
+           }
+
+           if (bufptr >= bufend)
+             return (NULL);
+
+           *bufptr++ = '\"';
+           *bufptr   = '\0';
+           bufend ++;
+           break;
+      }
+
+      bufptr += strlen(bufptr);
+    }
+
+    if (bufptr == buffer || (bufend - bufptr) < 2)
+      return (NULL);
+
+    memcpy(bufptr, "}", 2);
   }
 
-  return (count);
+  return (buffer);
+}
+
+
+/*
+ * 'get_points()' - Get a value in points.
+ */
+
+static double                          /* O - Number in points */
+get_points(double     number,          /* I - Original number */
+           const char *uval)           /* I - Units */
+{
+  if (!strcmp(uval, "mm"))             /* Millimeters */
+    return (number * 72.0 / 25.4);
+  else if (!strcmp(uval, "cm"))                /* Centimeters */
+    return (number * 72.0 / 2.54);
+  else if (!strcmp(uval, "in"))                /* Inches */
+    return (number * 72.0);
+  else if (!strcmp(uval, "ft"))                /* Feet */
+    return (number * 72.0 * 12.0);
+  else if (!strcmp(uval, "m"))         /* Meters */
+    return (number * 72.0 / 0.0254);
+  else                                 /* Points */
+    return (number);
 }
 
-    
+
 /*
- * End of "$Id: admin.c 5360 2006-03-30 17:02:17Z mike $".
+ * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
  */
+
+static char *                          /* O - Filename or NULL */
+get_printer_ppd(const char *uri,       /* I - Printer URI */
+                char       *buffer,    /* I - Filename buffer */
+               size_t     bufsize)     /* I - Size of filename buffer */
+{
+  http_t       *http;                  /* Connection to printer */
+  ipp_t                *request,               /* Get-Printer-Attributes request */
+               *response;              /* Get-Printer-Attributes response */
+  char         resolved[1024],         /* Resolved URI */
+               scheme[32],             /* URI scheme */
+               userpass[256],          /* Username:password */
+               host[256],              /* Hostname */
+               resource[256];          /* Resource path */
+  int          port;                   /* Port number */
+  static const char * const pattrs[] = /* Printer attributes we need */
+  {
+    "all",
+    "media-col-database"
+  };
+
+
+ /*
+  * Connect to the printer...
+  */
+
+  if (strstr(uri, "._tcp"))
+  {
+   /*
+    * Resolve URI...
+    */
+
+    if (!_httpResolveURI(uri, resolved, sizeof(resolved), _HTTP_RESOLVE_DEFAULT, NULL, NULL))
+    {
+      fprintf(stderr, "ERROR: Unable to resolve \"%s\".\n", uri);
+      return (NULL);
+    }
+
+    uri = resolved;
+  }
+
+  if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
+  {
+    fprintf(stderr, "ERROR: Bad printer URI \"%s\".\n", uri);
+    return (NULL);
+  }
+
+  http = httpConnect2(host, port, NULL, AF_UNSPEC, !strcmp(scheme, "ipps") ? HTTP_ENCRYPTION_ALWAYS : HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
+  if (!http)
+  {
+    fprintf(stderr, "ERROR: Unable to connect to \"%s:%d\": %s\n", host, port, cupsLastErrorString());
+    return (NULL);
+  }
+
+ /*
+  * Send a Get-Printer-Attributes request...
+  */
+
+  request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
+  ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes",  (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
+  response = cupsDoRequest(http, request, resource);
+
+  if (!_ppdCreateFromIPP(buffer, bufsize, response))
+    fprintf(stderr, "ERROR: Unable to create PPD file: %s\n", strerror(errno));
+
+  ippDelete(response);
+  httpClose(http);
+
+  if (buffer[0])
+    return (buffer);
+  else
+    return (NULL);
+}