]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/ipp.c
Merge pull request #5621 from zdohnal/cgigetarray-sigsegv
[thirdparty/cups.git] / scheduler / ipp.c
index bbd5058598e39ed8498d22ad999ab27eda561733..2fe3bf25cc3ac6d384663a95788ae9bf6f18deb2 100644 (file)
@@ -1,19 +1,14 @@
 /*
- * "$Id$"
- *
  * IPP routines for the CUPS scheduler.
  *
- * Copyright 2007-2016 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright © 2007-2019 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
  *
  * This file contains Kerberos support code, copyright 2006 by
  * Jelmer Vernooij.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
 #include <cups/ppd-private.h>
 
 #ifdef __APPLE__
-/*#  include <ApplicationServices/ApplicationServices.h>
-extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
-#  include <CoreFoundation/CoreFoundation.h>*/
 #  ifdef HAVE_MEMBERSHIP_H
 #    include <membership.h>
 #  endif /* HAVE_MEMBERSHIP_H */
-#  ifdef HAVE_MEMBERSHIPPRIV_H
-#    include <membershipPriv.h>
-#  else
 extern int mbr_user_name_to_uuid(const char* name, uuid_t uu);
 extern int mbr_group_name_to_uuid(const char* name, uuid_t uu);
 extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
-#  endif /* HAVE_MEMBERSHIPPRIV_H */
 #endif /* __APPLE__ */
 
 
@@ -120,14 +108,11 @@ static void       save_auth_info(cupsd_client_t *con, cupsd_job_t *job,
 static void    send_document(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    send_http_error(cupsd_client_t *con, http_status_t status,
                                cupsd_printer_t *printer);
-static void    send_ipp_status(cupsd_client_t *con, ipp_status_t status,
-                               const char *message, ...)
-               __attribute__((__format__(__printf__, 3, 4)));
+static void    send_ipp_status(cupsd_client_t *con, ipp_status_t status, const char *message, ...) _CUPS_FORMAT(3, 4);
 static void    set_default(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    set_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    set_printer_defaults(cupsd_client_t *con,
-                                    cupsd_printer_t *printer);
+static int     set_printer_defaults(cupsd_client_t *con, cupsd_printer_t *printer);
 static void    start_printer(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    stop_printer(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    url_encode_attr(ipp_attribute_t *attr, char *buffer, size_t bufsize);
@@ -153,11 +138,33 @@ cupsdProcessIPPRequest(
   ipp_attribute_t      *uri = NULL;    /* Printer or job URI attribute */
   ipp_attribute_t      *username;      /* requesting-user-name attr */
   int                  sub_id;         /* Subscription ID */
+  int                  valid = 1;      /* Valid request? */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
-                  con, con->number, con->request->request.op.operation_id);
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest(%p[%d]): operation_id=%04x(%s)", con, con->number, con->request->request.op.operation_id, ippOpString(con->request->request.op.operation_id));
+
+  if (LogLevel >= CUPSD_LOG_DEBUG2)
+  {
+    for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(con->request); attr; attr = ippNextAttribute(con->request))
+    {
+      const char  *name;                /* Attribute name */
+      char        value[1024];          /* Attribute value */
+
+      if (group != ippGetGroupTag(attr))
+      {
+        group = ippGetGroupTag(attr);
+        if (group != IPP_TAG_ZERO)
+          cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s", ippTagString(group));
+      }
+
+      if ((name = ippGetName(attr)) == NULL)
+        continue;
+
+      ippAttributeString(attr, value, sizeof(value));
+
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s %s%s '%s'", name, ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)), value);
+    }
+  }
 
  /*
   * First build an empty response message for this request...
@@ -165,34 +172,23 @@ cupsdProcessIPPRequest(
 
   con->response = ippNew();
 
-  con->response->request.status.version[0] =
-      con->request->request.op.version[0];
-  con->response->request.status.version[1] =
-      con->request->request.op.version[1];
-  con->response->request.status.request_id =
-      con->request->request.op.request_id;
+  con->response->request.status.version[0] = con->request->request.op.version[0];
+  con->response->request.status.version[1] = con->request->request.op.version[1];
+  con->response->request.status.request_id = con->request->request.op.request_id;
 
  /*
   * Then validate the request header and required attributes...
   */
 
-  if (con->request->request.any.version[0] != 1 &&
-      con->request->request.any.version[0] != 2)
+  if (con->request->request.any.version[0] != 1 && con->request->request.any.version[0] != 2)
   {
    /*
     * Return an error, since we only support IPP 1.x and 2.x.
     */
 
-    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                  "%04X %s Bad request version number %d.%d",
-                 IPP_VERSION_NOT_SUPPORTED, con->http->hostname,
-                  con->request->request.any.version[0],
-                 con->request->request.any.version[1]);
+    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Bad request version number %d.%d.", IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, con->http->hostname, con->request->request.any.version[0], con->request->request.any.version[1]);
 
-    send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED,
-                    _("Bad request version number %d.%d."),
-                   con->request->request.any.version[0],
-                   con->request->request.any.version[1]);
+    send_ipp_status(con, IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, _("Bad request version number %d.%d."), con->request->request.any.version[0], con->request->request.any.version[1]);
   }
   else if (con->request->request.any.request_id < 1)
   {
@@ -200,21 +196,15 @@ cupsdProcessIPPRequest(
     * Return an error, since request IDs must be between 1 and 2^31-1
     */
 
-    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                  "%04X %s Bad request ID %d",
-                 IPP_BAD_REQUEST, con->http->hostname,
-                  con->request->request.any.request_id);
+    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Bad request ID %d.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname, con->request->request.any.request_id);
 
-    send_ipp_status(con, IPP_BAD_REQUEST, _("Bad request ID %d."),
-                   con->request->request.any.request_id);
+    send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Bad request ID %d."), con->request->request.any.request_id);
   }
   else if (!con->request->attrs)
   {
-    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                  "%04X %s No attributes in request",
-                 IPP_BAD_REQUEST, con->http->hostname);
+    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s No attributes in request.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname);
 
-    send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request."));
+    send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("No attributes in request."));
   }
   else
   {
@@ -232,13 +222,9 @@ cupsdProcessIPPRequest(
        * Out of order; return an error...
        */
 
-       cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                      "%04X %s Attribute groups are out of order",
-                     IPP_BAD_REQUEST, con->http->hostname);
+       cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Attribute groups are out of order", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname);
 
-       send_ipp_status(con, IPP_BAD_REQUEST,
-                       _("Attribute groups are out of order (%x < %x)."),
-                       attr->group_tag, group);
+       send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute groups are out of order (%x < %x)."), attr->group_tag, group);
        break;
       }
       else
@@ -255,9 +241,7 @@ cupsdProcessIPPRequest(
       */
 
       attr = con->request->attrs;
-      if (attr && attr->name &&
-          !strcmp(attr->name, "attributes-charset") &&
-         (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
+      if (attr && attr->name && !strcmp(attr->name, "attributes-charset") && (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET && attr->group_tag == IPP_TAG_OPERATION)
        charset = attr;
       else
        charset = NULL;
@@ -265,9 +249,7 @@ cupsdProcessIPPRequest(
       if (attr)
         attr = attr->next;
 
-      if (attr && attr->name &&
-          !strcmp(attr->name, "attributes-natural-language") &&
-         (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
+      if (attr && attr->name && !strcmp(attr->name, "attributes-natural-language") && (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE && attr->group_tag == IPP_TAG_OPERATION)
       {
        language = attr;
 
@@ -285,36 +267,26 @@ cupsdProcessIPPRequest(
       else
        language = NULL;
 
-      if ((attr = ippFindAttribute(con->request, "printer-uri",
-                                   IPP_TAG_URI)) != NULL)
+      if ((attr = ippFindAttribute(con->request, "printer-uri", IPP_TAG_URI)) != NULL && attr->group_tag == IPP_TAG_OPERATION)
        uri = attr;
-      else if ((attr = ippFindAttribute(con->request, "job-uri",
-                                        IPP_TAG_URI)) != NULL)
+      else if ((attr = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI)) != NULL && attr->group_tag == IPP_TAG_OPERATION)
        uri = attr;
-      else if (con->request->request.op.operation_id == CUPS_GET_PPD)
-        uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
+      else if (con->request->request.op.operation_id == CUPS_GET_PPD && (attr = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME)) != NULL && attr->group_tag == IPP_TAG_OPERATION)
+        uri = attr;
       else
        uri = NULL;
 
       if (charset)
-       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
-                    "attributes-charset", NULL,
-                    charset->values[0].string.text);
+       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, charset->values[0].string.text);
       else
-       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
-                    "attributes-charset", NULL, "utf-8");
+       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, "utf-8");
 
       if (language)
-       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
-                     "attributes-natural-language", NULL,
-                    language->values[0].string.text);
+       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, language->values[0].string.text);
       else
-       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
-                     "attributes-natural-language", NULL, DefaultLanguage);
+       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, DefaultLanguage);
 
-      if (charset &&
-          _cups_strcasecmp(charset->values[0].string.text, "us-ascii") &&
-          _cups_strcasecmp(charset->values[0].string.text, "utf-8"))
+      if (charset && _cups_strcasecmp(charset->values[0].string.text, "us-ascii") && _cups_strcasecmp(charset->values[0].string.text, "utf-8"))
       {
        /*
         * Bad character set...
@@ -322,13 +294,8 @@ cupsdProcessIPPRequest(
 
         cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"",
                        charset->values[0].string.text);
-       cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                     "%04X %s Unsupported attributes-charset value \"%s\"",
-                     IPP_CHARSET, con->http->hostname,
-                     charset->values[0].string.text);
-       send_ipp_status(con, IPP_BAD_REQUEST,
-                       _("Unsupported character set \"%s\"."),
-                       charset->values[0].string.text);
+       cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Unsupported attributes-charset value \"%s\".", IPP_STATUS_ERROR_CHARSET, con->http->hostname, charset->values[0].string.text);
+       send_ipp_status(con, IPP_STATUS_ERROR_CHARSET, _("Unsupported character set \"%s\"."), charset->values[0].string.text);
       }
       else if (!charset || !language ||
               (!uri &&
@@ -346,33 +313,24 @@ cupsdProcessIPPRequest(
 
         if (!charset)
        {
-         cupsdLogMessage(CUPSD_LOG_ERROR,
-                         "Missing attributes-charset attribute");
+         cupsdLogMessage(CUPSD_LOG_ERROR, "Missing attributes-charset attribute.");
 
-         cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                       "%04X %s Missing attributes-charset attribute",
-                       IPP_BAD_REQUEST, con->http->hostname);
+         cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Missing attributes-charset attribute.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname);
         }
 
         if (!language)
        {
          cupsdLogMessage(CUPSD_LOG_ERROR,
-                         "Missing attributes-natural-language attribute");
+                         "Missing attributes-natural-language attribute.");
 
-         cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                       "%04X %s Missing attributes-natural-language attribute",
-                       IPP_BAD_REQUEST, con->http->hostname);
+         cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Missing attributes-natural-language attribute.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname);
         }
 
         if (!uri)
        {
-         cupsdLogMessage(CUPSD_LOG_ERROR,
-                         "Missing printer-uri, job-uri, or ppd-name "
-                         "attribute");
+         cupsdLogMessage(CUPSD_LOG_ERROR, "Missing printer-uri, job-uri, or ppd-name attribute.");
 
-         cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                       "%04X %s Missing printer-uri, job-uri, or ppd-name "
-                       "attribute", IPP_BAD_REQUEST, con->http->hostname);
+         cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Missing printer-uri, job-uri, or ppd-name attribute.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname);
         }
 
        cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
@@ -391,20 +349,55 @@ cupsdProcessIPPRequest(
       else
       {
        /*
-       * OK, all the checks pass so far; make sure requesting-user-name is
-       * not "root" from a remote host...
+       * OK, all the checks pass so far; validate "requesting-user-name"
+       * attribute value...
        */
 
-        if ((username = ippFindAttribute(con->request, "requesting-user-name",
-                                        IPP_TAG_NAME)) != NULL)
-       {
-        /*
-         * Check for root user...
-         */
-
-         if (!strcmp(username->values[0].string.text, "root") &&
-             _cups_strcasecmp(con->http->hostname, "localhost") &&
-             strcmp(con->username, "root"))
+        if ((username = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_ZERO)) != NULL)
+        {
+         /*
+          * Validate "requesting-user-name"...
+          */
+
+          if (username->group_tag != IPP_TAG_OPERATION && StrictConformance)
+          {
+           cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s \"requesting-user-name\" attribute in wrong group.", IPP_STATUS_ERROR_BAD_REQUEST, con->http->hostname);
+           send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("\"requesting-user-name\" attribute in wrong group."));
+           valid = 0;
+          }
+          else if (username->value_tag != IPP_TAG_NAME && username->value_tag != IPP_TAG_NAMELANG)
+          {
+           cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s \"requesting-user-name\" attribute with wrong syntax.", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, con->http->hostname);
+           send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("\"requesting-user-name\" attribute with wrong syntax."));
+           if ((attr = ippCopyAttribute(con->response, username, 0)) != NULL)
+             attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
+           valid = 0;
+          }
+          else if (!ippValidateAttribute(username))
+          {
+           cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s \"requesting-user-name\" attribute with bad value.", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, con->http->hostname);
+
+            if (StrictConformance)
+            {
+             /*
+              * Throw an error...
+              */
+
+             send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("\"requesting-user-name\" attribute with wrong syntax."));
+              if ((attr = ippCopyAttribute(con->response, username, 0)) != NULL)
+                attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
+             valid = 0;
+           }
+           else
+           {
+            /*
+             * Map bad "requesting-user-name" to 'anonymous'...
+             */
+
+              ippSetString(con->request, &username, 0, "anonymous");
+           }
+          }
+          else if (!strcmp(username->values[0].string.text, "root") && _cups_strcasecmp(con->http->hostname, "localhost") && strcmp(con->username, "root"))
          {
           /*
            * Remote unauthenticated user masquerading as local root...
@@ -414,215 +407,207 @@ cupsdProcessIPPRequest(
          }
        }
 
-        if ((attr = ippFindAttribute(con->request, "notify-subscription-id",
-                                    IPP_TAG_INTEGER)) != NULL)
+        if ((attr = ippFindAttribute(con->request, "notify-subscription-id", IPP_TAG_INTEGER)) != NULL)
          sub_id = attr->values[0].integer;
        else
          sub_id = 0;
 
-       /*
-        * Then try processing the operation...
-       */
-
-        if (uri)
-         cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s",
-                         ippOpString(con->request->request.op.operation_id),
-                         uri->values[0].string.text);
-        else
-         cupsdLogMessage(CUPSD_LOG_DEBUG, "%s",
-                         ippOpString(con->request->request.op.operation_id));
-
-       switch (con->request->request.op.operation_id)
-       {
-         case IPP_OP_PRINT_JOB :
-              print_job(con, uri);
-              break;
-
-         case IPP_OP_VALIDATE_JOB :
-              validate_job(con, uri);
-              break;
-
-         case IPP_OP_CREATE_JOB :
-              create_job(con, uri);
-              break;
-
-         case IPP_OP_SEND_DOCUMENT :
-              send_document(con, uri);
-              break;
-
-         case IPP_OP_CANCEL_JOB :
-              cancel_job(con, uri);
-              break;
-
-         case IPP_OP_GET_JOB_ATTRIBUTES :
-              get_job_attrs(con, uri);
-              break;
-
-         case IPP_OP_GET_JOBS :
-              get_jobs(con, uri);
-              break;
-
-         case IPP_OP_GET_PRINTER_ATTRIBUTES :
-              get_printer_attrs(con, uri);
-              break;
-
-         case IPP_OP_GET_PRINTER_SUPPORTED_VALUES :
-              get_printer_supported(con, uri);
-              break;
-
-         case IPP_OP_HOLD_JOB :
-              hold_job(con, uri);
-              break;
-
-         case IPP_OP_RELEASE_JOB :
-              release_job(con, uri);
-              break;
-
-         case IPP_OP_RESTART_JOB :
-              restart_job(con, uri);
-              break;
-
-         case IPP_OP_PAUSE_PRINTER :
-              stop_printer(con, uri);
-             break;
-
-         case IPP_OP_RESUME_PRINTER :
-              start_printer(con, uri);
-             break;
-
-         case IPP_OP_PURGE_JOBS :
-         case IPP_OP_CANCEL_JOBS :
-         case IPP_OP_CANCEL_MY_JOBS :
-              cancel_all_jobs(con, uri);
-              break;
-
-         case IPP_OP_SET_JOB_ATTRIBUTES :
-              set_job_attrs(con, uri);
-              break;
-
-         case IPP_OP_SET_PRINTER_ATTRIBUTES :
-              set_printer_attrs(con, uri);
-              break;
-
-         case IPP_OP_HOLD_NEW_JOBS :
-              hold_new_jobs(con, uri);
-              break;
-
-         case IPP_OP_RELEASE_HELD_NEW_JOBS :
-              release_held_new_jobs(con, uri);
-              break;
-
-         case IPP_OP_CLOSE_JOB :
-              close_job(con, uri);
-              break;
-
-         case IPP_OP_CUPS_GET_DEFAULT :
-              get_default(con);
-              break;
-
-         case IPP_OP_CUPS_GET_PRINTERS :
-              get_printers(con, 0);
-              break;
-
-         case IPP_OP_CUPS_GET_CLASSES :
-              get_printers(con, CUPS_PRINTER_CLASS);
-              break;
-
-         case IPP_OP_CUPS_ADD_MODIFY_PRINTER :
-              add_printer(con, uri);
-              break;
-
-         case IPP_OP_CUPS_DELETE_PRINTER :
-              delete_printer(con, uri);
-              break;
-
-         case IPP_OP_CUPS_ADD_MODIFY_CLASS :
-              add_class(con, uri);
-              break;
-
-         case IPP_OP_CUPS_DELETE_CLASS :
-              delete_printer(con, uri);
-              break;
+        if (valid)
+        {
+        /*
+         * Try processing the operation...
+         */
 
-         case IPP_OP_CUPS_ACCEPT_JOBS :
-         case IPP_OP_ENABLE_PRINTER :
-              accept_jobs(con, uri);
-              break;
+         if (uri)
+           cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s", ippOpString(con->request->request.op.operation_id), uri->values[0].string.text);
+         else
+           cupsdLogMessage(CUPSD_LOG_DEBUG, "%s", ippOpString(con->request->request.op.operation_id));
 
-         case IPP_OP_CUPS_REJECT_JOBS :
-         case IPP_OP_DISABLE_PRINTER :
-              reject_jobs(con, uri);
-              break;
+         switch (con->request->request.op.operation_id)
+         {
+           case IPP_OP_PRINT_JOB :
+               print_job(con, uri);
+               break;
+
+           case IPP_OP_VALIDATE_JOB :
+               validate_job(con, uri);
+               break;
+
+           case IPP_OP_CREATE_JOB :
+               create_job(con, uri);
+               break;
+
+           case IPP_OP_SEND_DOCUMENT :
+               send_document(con, uri);
+               break;
+
+           case IPP_OP_CANCEL_JOB :
+               cancel_job(con, uri);
+               break;
+
+           case IPP_OP_GET_JOB_ATTRIBUTES :
+               get_job_attrs(con, uri);
+               break;
+
+           case IPP_OP_GET_JOBS :
+               get_jobs(con, uri);
+               break;
+
+           case IPP_OP_GET_PRINTER_ATTRIBUTES :
+               get_printer_attrs(con, uri);
+               break;
+
+           case IPP_OP_GET_PRINTER_SUPPORTED_VALUES :
+               get_printer_supported(con, uri);
+               break;
+
+           case IPP_OP_HOLD_JOB :
+               hold_job(con, uri);
+               break;
+
+           case IPP_OP_RELEASE_JOB :
+               release_job(con, uri);
+               break;
+
+           case IPP_OP_RESTART_JOB :
+               restart_job(con, uri);
+               break;
+
+           case IPP_OP_PAUSE_PRINTER :
+               stop_printer(con, uri);
+               break;
+
+           case IPP_OP_RESUME_PRINTER :
+               start_printer(con, uri);
+               break;
+
+           case IPP_OP_PURGE_JOBS :
+           case IPP_OP_CANCEL_JOBS :
+           case IPP_OP_CANCEL_MY_JOBS :
+               cancel_all_jobs(con, uri);
+               break;
+
+           case IPP_OP_SET_JOB_ATTRIBUTES :
+               set_job_attrs(con, uri);
+               break;
+
+           case IPP_OP_SET_PRINTER_ATTRIBUTES :
+               set_printer_attrs(con, uri);
+               break;
+
+           case IPP_OP_HOLD_NEW_JOBS :
+               hold_new_jobs(con, uri);
+               break;
+
+           case IPP_OP_RELEASE_HELD_NEW_JOBS :
+               release_held_new_jobs(con, uri);
+               break;
+
+           case IPP_OP_CLOSE_JOB :
+               close_job(con, uri);
+               break;
+
+           case IPP_OP_CUPS_GET_DEFAULT :
+               get_default(con);
+               break;
+
+           case IPP_OP_CUPS_GET_PRINTERS :
+               get_printers(con, 0);
+               break;
+
+           case IPP_OP_CUPS_GET_CLASSES :
+               get_printers(con, CUPS_PRINTER_CLASS);
+               break;
+
+           case IPP_OP_CUPS_ADD_MODIFY_PRINTER :
+               add_printer(con, uri);
+               break;
+
+           case IPP_OP_CUPS_DELETE_PRINTER :
+               delete_printer(con, uri);
+               break;
+
+           case IPP_OP_CUPS_ADD_MODIFY_CLASS :
+               add_class(con, uri);
+               break;
+
+           case IPP_OP_CUPS_DELETE_CLASS :
+               delete_printer(con, uri);
+               break;
+
+           case IPP_OP_CUPS_ACCEPT_JOBS :
+           case IPP_OP_ENABLE_PRINTER :
+               accept_jobs(con, uri);
+               break;
+
+           case IPP_OP_CUPS_REJECT_JOBS :
+           case IPP_OP_DISABLE_PRINTER :
+               reject_jobs(con, uri);
+               break;
 
-         case IPP_OP_CUPS_SET_DEFAULT :
-              set_default(con, uri);
-              break;
+           case IPP_OP_CUPS_SET_DEFAULT :
+               set_default(con, uri);
+               break;
 
-         case IPP_OP_CUPS_GET_DEVICES :
-              get_devices(con);
-              break;
+           case IPP_OP_CUPS_GET_DEVICES :
+               get_devices(con);
+               break;
 
-          case IPP_OP_CUPS_GET_DOCUMENT :
-             get_document(con, uri);
-             break;
+           case IPP_OP_CUPS_GET_DOCUMENT :
+               get_document(con, uri);
+               break;
 
-         case IPP_OP_CUPS_GET_PPD :
-              get_ppd(con, uri);
-              break;
+           case IPP_OP_CUPS_GET_PPD :
+               get_ppd(con, uri);
+               break;
 
-         case IPP_OP_CUPS_GET_PPDS :
-              get_ppds(con);
-              break;
+           case IPP_OP_CUPS_GET_PPDS :
+               get_ppds(con);
+               break;
 
-         case IPP_OP_CUPS_MOVE_JOB :
-              move_job(con, uri);
-              break;
+           case IPP_OP_CUPS_MOVE_JOB :
+               move_job(con, uri);
+               break;
 
-         case IPP_OP_CUPS_AUTHENTICATE_JOB :
-              authenticate_job(con, uri);
-              break;
+           case IPP_OP_CUPS_AUTHENTICATE_JOB :
+               authenticate_job(con, uri);
+               break;
 
-          case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS :
-         case IPP_OP_CREATE_JOB_SUBSCRIPTIONS :
-             create_subscriptions(con, uri);
-             break;
+           case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS :
+           case IPP_OP_CREATE_JOB_SUBSCRIPTIONS :
+               create_subscriptions(con, uri);
+               break;
+
+           case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES :
+               get_subscription_attrs(con, sub_id);
+               break;
+
+           case IPP_OP_GET_SUBSCRIPTIONS :
+               get_subscriptions(con, uri);
+               break;
 
-          case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES :
-             get_subscription_attrs(con, sub_id);
-             break;
+           case IPP_OP_RENEW_SUBSCRIPTION :
+               renew_subscription(con, sub_id);
+               break;
 
-         case IPP_OP_GET_SUBSCRIPTIONS :
-             get_subscriptions(con, uri);
-             break;
+           case IPP_OP_CANCEL_SUBSCRIPTION :
+               cancel_subscription(con, sub_id);
+               break;
 
-         case IPP_OP_RENEW_SUBSCRIPTION :
-             renew_subscription(con, sub_id);
-             break;
+           case IPP_OP_GET_NOTIFICATIONS :
+               get_notifications(con);
+               break;
 
-         case IPP_OP_CANCEL_SUBSCRIPTION :
-             cancel_subscription(con, sub_id);
-             break;
+           case IPP_OP_CUPS_CREATE_LOCAL_PRINTER :
+               create_local_printer(con);
+               break;
 
-          case IPP_OP_GET_NOTIFICATIONS :
-             get_notifications(con);
-             break;
+           default :
+               cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL, "%04X %s Operation %04X (%s) not supported.", IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, con->http->hostname, con->request->request.op.operation_id, ippOpString(con->request->request.op.operation_id));
 
-         case IPP_OP_CUPS_CREATE_LOCAL_PRINTER :
-             create_local_printer(con);
-             break;
-
-         default :
-             cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                           "%04X %s Operation %04X (%s) not supported",
-                           IPP_OPERATION_NOT_SUPPORTED, con->http->hostname,
-                           con->request->request.op.operation_id,
-                           ippOpString(con->request->request.op.operation_id));
-
-              send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED,
-                             _("%s not supported."),
-                             ippOpString(
-                                 con->request->request.op.operation_id));
-             break;
+               send_ipp_status(con, IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, _("%s not supported."), ippOpString(con->request->request.op.operation_id));
+               break;
+         }
        }
       }
     }
@@ -634,16 +619,7 @@ cupsdProcessIPPRequest(
     * Sending data from the scheduler...
     */
 
-    cupsdLogMessage(con->response->request.status.status_code
-                        >= IPP_BAD_REQUEST &&
-                    con->response->request.status.status_code
-                       != IPP_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
-                    "[Client %d] Returning IPP %s for %s (%s) from %s",
-                   con->number,
-                   ippErrorString(con->response->request.status.status_code),
-                   ippOpString(con->request->request.op.operation_id),
-                   uri ? uri->values[0].string.text : "no URI",
-                   con->http->hostname);
+    cupsdLogClient(con, con->response->request.status.status_code >= IPP_STATUS_ERROR_BAD_REQUEST && con->response->request.status.status_code != IPP_STATUS_ERROR_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG, "Returning IPP %s for %s (%s) from %s.",  ippErrorString(con->response->request.status.status_code), ippOpString(con->request->request.op.operation_id), uri ? uri->values[0].string.text : "no URI", con->http->hostname);
 
     httpClearFields(con->http);
 
@@ -658,10 +634,7 @@ cupsdProcessIPPRequest(
 
     if (con->http->version == HTTP_1_1)
     {
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                     "[Client %d] Transfer-Encoding: chunked",
-                     con->number);
-
+      cupsdLogClient(con, CUPSD_LOG_DEBUG, "Transfer-Encoding: chunked");
       cupsdSetLength(con->http, 0);
     }
     else
@@ -680,9 +653,7 @@ cupsdProcessIPPRequest(
          length += (size_t)fileinfo.st_size;
       }
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                     "[Client %d] Content-Length: " CUPS_LLFMT,
-                     con->number, CUPS_LLCAST length);
+      cupsdLogClient(con, CUPSD_LOG_DEBUG, "Content-Length: " CUPS_LLFMT, CUPS_LLCAST length);
       httpSetLength(con->http, length);
     }
 
@@ -692,8 +663,7 @@ cupsdProcessIPPRequest(
       * Tell the caller the response header was sent successfully...
       */
 
-      cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient,
-                    (cupsd_selfunc_t)cupsdWriteClient, con);
+      cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient, (cupsd_selfunc_t)cupsdWriteClient, con);
 
       return (1);
     }
@@ -934,6 +904,8 @@ add_class(cupsd_client_t  *con,             /* I - Client connection */
 
     pclass = cupsdAddClass(resource + 9);
     modify = 0;
+
+    pclass->printer_id = NextPrinterId ++;
   }
   else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con,
                                       NULL)) != HTTP_OK)
@@ -989,6 +961,9 @@ add_class(cupsd_client_t  *con,             /* I - Client connection */
       */
 
       send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
+      if (!modify)
+       cupsdDeletePrinter(pclass, 0);
+
       return;
     }
 
@@ -1011,6 +986,9 @@ add_class(cupsd_client_t  *con,            /* I - Client connection */
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Attempt to set %s printer-state to bad value %d."),
                       pclass->name, attr->values[0].integer);
+      if (!modify)
+       cupsdDeletePrinter(pclass, 0);
+
       return;
     }
 
@@ -1067,12 +1045,18 @@ add_class(cupsd_client_t  *con,         /* I - Client connection */
 
        send_ipp_status(con, IPP_NOT_FOUND,
                        _("The printer or class does not exist."));
+       if (!modify)
+         cupsdDeletePrinter(pclass, 0);
+
        return;
       }
       else if (dtype & CUPS_PRINTER_CLASS)
       {
         send_ipp_status(con, IPP_BAD_REQUEST,
                        _("Nested classes are not allowed."));
+       if (!modify)
+         cupsdDeletePrinter(pclass, 0);
+
         return;
       }
 
@@ -1084,7 +1068,13 @@ add_class(cupsd_client_t  *con,          /* I - Client connection */
     }
   }
 
-  set_printer_defaults(con, pclass);
+  if (!set_printer_defaults(con, pclass))
+  {
+    if (!modify)
+      cupsdDeletePrinter(pclass, 0);
+
+    return;
+  }
 
   if ((attr = ippFindAttribute(con->request, "auth-info-required",
                                IPP_TAG_KEYWORD)) != NULL)
@@ -1550,17 +1540,34 @@ add_job(cupsd_client_t  *con,           /* I - Client connection */
                     _("Bad job-name value: Wrong type or count."));
     if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
       attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
-    return (NULL);
+
+    if (StrictConformance)
+      return (NULL);
+
+    /* Don't use invalid attribute */
+    ippDeleteAttribute(con->request, attr);
+
+    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, "Untitled");
   }
   else if (!ippValidateAttribute(attr))
   {
     send_ipp_status(con, IPP_ATTRIBUTES, _("Bad job-name value: %s"),
                     cupsLastErrorString());
+
     if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
       attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
-    return (NULL);
+
+    if (StrictConformance)
+      return (NULL);
+
+    /* Don't use invalid attribute */
+    ippDeleteAttribute(con->request, attr);
+
+    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, "Untitled");
   }
 
+  attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
+
   if ((job = cupsdAddJob(priority, printer->name)) == NULL)
   {
     send_ipp_status(con, IPP_INTERNAL_ERROR,
@@ -1579,8 +1586,6 @@ add_job(cupsd_client_t  *con,             /* I - Client connection */
   add_job_uuid(job);
   apply_printer_defaults(printer, job);
 
-  attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
-
   if (con->username[0])
   {
     cupsdSetString(&job->username, con->username);
@@ -1696,7 +1701,24 @@ add_job(cupsd_client_t  *con,            /* I - Client connection */
     attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
                         "job-hold-until", NULL, val);
   }
-  if (attr && strcmp(attr->values[0].string.text, "no-hold"))
+
+  if (printer->holding_new_jobs)
+  {
+   /*
+    * Hold all new jobs on this printer...
+    */
+
+    if (attr && strcmp(attr->values[0].string.text, "no-hold"))
+      cupsdSetJobHoldUntil(job, ippGetString(attr, 0, NULL), 0);
+    else
+      cupsdSetJobHoldUntil(job, "indefinite", 0);
+
+    job->state->values[0].integer = IPP_JOB_HELD;
+    job->state_value              = IPP_JOB_HELD;
+
+    ippSetString(job->attrs, &job->reasons, 0, "job-held-on-create");
+  }
+  else if (attr && strcmp(attr->values[0].string.text, "no-hold"))
   {
    /*
     * Hold job until specified time...
@@ -1880,18 +1902,14 @@ add_job(cupsd_client_t  *con,           /* I - Client connection */
   * Fill in the response info...
   */
 
-  httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
-                   con->clientname, con->clientport, "/jobs/%d", job->id);
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
-               job_uri);
+  httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, con->clientname, con->clientport, "/jobs/%d", job->id);
+  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, job_uri);
 
   ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
-                job->state_value);
+  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", (int)job->state_value);
   ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_TEXT, "job-state-message", NULL, "");
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
-               NULL, job->reasons->values[0].string.text);
+  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, job->reasons->values[0].string.text);
 
   con->response->request.status.status_code = IPP_OK;
 
@@ -1983,7 +2001,7 @@ add_job_subscriptions(
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
         int    port;                   /* Port portion of URI */
-
+        struct stat info;              /* File information */
 
         recipient = attr->values[0].string.text;
 
@@ -1999,9 +2017,8 @@ add_job_subscriptions(
          return;
        }
 
-        snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
-                scheme);
-        if (access(notifier, X_OK))
+        snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin, scheme);
+        if (access(notifier, X_OK) || stat(notifier, &info) || !S_ISREG(info.st_mode))
        {
           send_ipp_status(con, IPP_NOT_POSSIBLE,
                          _("notify-recipient-uri URI \"%s\" uses unknown "
@@ -2280,6 +2297,8 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
 
     printer = cupsdAddPrinter(resource + 10);
     modify  = 0;
+
+    printer->printer_id = NextPrinterId ++;
   }
   else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
                                       NULL)) != HTTP_OK)
@@ -2329,22 +2348,6 @@ add_printer(cupsd_client_t  *con,        /* I - Client connection */
     http_uri_status_t  uri_status;     /* URI separation status */
     char               old_device_uri[1024];
                                        /* Old device URI */
-    static const char * const uri_status_strings[] =
-    {
-      "URI too large.",
-      "Bad arguments to function.",
-      "Bad resource path.",
-      "Bad port number.",
-      "Bad hostname/address.",
-      "Bad username/password.",
-      "Bad URI scheme.",
-      "Bad URI.",
-      "OK",
-      "Missing URI scheme.",
-      "Unknown URI scheme",
-      "Missing resource path."
-    };
-
 
     need_restart_job = 1;
 
@@ -2355,14 +2358,15 @@ add_printer(cupsd_client_t  *con,       /* I - Client connection */
                                 host, sizeof(host), &port,
                                 resource, sizeof(resource));
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                   "%s device-uri: %s", printer->name,
-                   uri_status_strings[uri_status - HTTP_URI_STATUS_OVERFLOW]);
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "%s device-uri: %s", printer->name, httpURIStatusString(uri_status));
 
     if (uri_status < HTTP_URI_OK)
     {
       send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"."),
                      attr->values[0].string.text);
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2383,6 +2387,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
                          "To enable, see the FileDevice directive in "
                          "\"%s/cups-files.conf\"."),
                        ServerRoot);
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
     }
@@ -2401,6 +2408,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
 
        send_ipp_status(con, IPP_NOT_POSSIBLE,
                         _("Bad device-uri scheme \"%s\"."), scheme);
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
     }
@@ -2445,6 +2455,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     {
       send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"."),
                      attr->values[0].string.text);
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2484,6 +2497,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Cannot share a remote Kerberized printer."));
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2494,6 +2510,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       */
 
       send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2517,6 +2536,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     {
       send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d."),
                       attr->values[0].integer);
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2553,6 +2575,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
                      attr->num_values,
                      (int)(sizeof(printer->reasons) /
                            sizeof(printer->reasons[0])));
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2565,8 +2590,7 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (!strcmp(attr->values[i].string.text, "none"))
         continue;
 
-      printer->reasons[printer->num_reasons] =
-          _cupsStrRetain(attr->values[i].string.text);
+      printer->reasons[printer->num_reasons] = _cupsStrAlloc(attr->values[i].string.text);
       printer->num_reasons ++;
 
       if (!strcmp(attr->values[i].string.text, "paused") &&
@@ -2586,7 +2610,13 @@ add_printer(cupsd_client_t  *con,        /* I - Client connection */
                   "Printer \"%s\" state changed.", printer->name);
   }
 
-  set_printer_defaults(con, printer);
+  if (!set_printer_defaults(con, printer))
+  {
+    if (!modify)
+      cupsdDeletePrinter(printer, 0);
+
+    return;
+  }
 
   if ((attr = ippFindAttribute(con->request, "auth-info-required",
                                IPP_TAG_KEYWORD)) != NULL)
@@ -2627,6 +2657,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (strncmp(line, "*PPD-Adobe", 10))
       {
        send_ipp_status(con, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED, _("Bad PPD file."));
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
 
@@ -2641,6 +2674,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (copy_file(srcfile, dstfile, ConfigFilePerm))
       {
        send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file - %s"), strerror(errno));
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
 
@@ -2667,6 +2703,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     else if (strstr(ppd_name, "../"))
     {
       send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
+      if (!modify)
+       cupsdDeletePrinter(printer, 0);
+
       return;
     }
     else
@@ -2680,6 +2719,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (copy_model(con, ppd_name, dstfile))
       {
         send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file."));
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
 
@@ -2696,8 +2738,7 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
 
     char cache_name[1024];             /* Cache filename for printer attrs */
 
-    snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir,
-             printer->name);
+    snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir, printer->name);
     unlink(cache_name);
 
     cupsdSetPrinterReasons(printer, "none");
@@ -2757,9 +2798,15 @@ add_printer(cupsd_client_t  *con,        /* I - Client connection */
   * Update the printer attributes and return...
   */
 
-  cupsdSetPrinterAttrs(printer);
   if (!printer->temporary)
+  {
+    if (!printer->printer_id)
+      printer->printer_id = NextPrinterId ++;
+
     cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
+  }
+
+  cupsdSetPrinterAttrs(printer);
 
   if (need_restart_job && printer->job)
   {
@@ -2858,6 +2905,8 @@ apply_printer_defaults(
                *option;                /* Current option */
 
 
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "Applying default options...");
+
  /*
   * Collect all of the default options and add the missing ones to the
   * job object...
@@ -2869,8 +2918,12 @@ apply_printer_defaults(
        i --, option ++)
     if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
     {
-      num_options = cupsAddOption(option->name, option->value, num_options,
-                                  &options);
+      if (!strcmp(option->name, "print-quality") && ippFindAttribute(job->attrs, "cupsPrintQuality", IPP_TAG_NAME))
+        continue;                     /* Don't override cupsPrintQuality */
+
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "Adding default %s=%s", option->name, option->value);
+
+      num_options = cupsAddOption(option->name, option->value, num_options, &options);
     }
 
  /*
@@ -3226,7 +3279,7 @@ cancel_all_jobs(cupsd_client_t  *con,     /* I - Client connection */
       * Cancel all jobs on all printers...
       */
 
-      cupsdCancelJobs(NULL, username, purge);
+      cupsdCancelJobs(NULL, username, purge != CUPSD_JOB_DEFAULT);
 
       cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
                      purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
@@ -3285,7 +3338,7 @@ cancel_all_jobs(cupsd_client_t  *con,     /* I - Client connection */
       * Cancel all of the jobs on the named printer...
       */
 
-      cupsdCancelJobs(printer->name, username, purge);
+      cupsdCancelJobs(printer->name, username, purge != CUPSD_JOB_DEFAULT);
 
       cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
                      printer->name,
@@ -3941,8 +3994,7 @@ close_job(cupsd_client_t  *con,           /* I - Client connection */
 
   ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
-                job->state_value);
+  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", (int)job->state_value);
 
   con->response->request.status.status_code = IPP_OK;
 
@@ -4748,10 +4800,10 @@ copy_job_attrs(cupsd_client_t *con,     /* I - Client connection */
       ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", job->koctets);
 
     if (job->name && (!ra || cupsArrayFind(ra, "job-name")))
-      ippAddString(con->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_NAME), "job-name", NULL, job->name);
+      ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, job->name);
 
     if (job->username && (!ra || cupsArrayFind(ra, "job-originating-user-name")))
-      ippAddString(con->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_NAME), "job-originating-user-name", NULL, job->username);
+      ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "job-originating-user-name", NULL, job->username);
 
     if (!ra || cupsArrayFind(ra, "job-state"))
       ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", (int)job->state_value);
@@ -4793,12 +4845,11 @@ copy_printer_attrs(
     cupsd_printer_t *printer,          /* I - Printer */
     cups_array_t    *ra)               /* I - Requested attributes array */
 {
-  char                 printer_uri[HTTP_MAX_URI];
-                                       /* Printer URI */
-  char                 printer_icons[HTTP_MAX_URI];
-                                       /* Printer icons */
-  time_t               curtime;        /* Current time */
-  int                  i;              /* Looping var */
+  char         uri[HTTP_MAX_URI];      /* URI value */
+  time_t       curtime;                /* Current time */
+  int          i;                      /* Looping var */
+  int          is_encrypted = httpIsEncrypted(con->http);
+                                       /* Is the connection encrypted? */
 
 
  /*
@@ -4806,110 +4857,88 @@ copy_printer_attrs(
   * and document-format attributes that may be provided by the client.
   */
 
+  _cupsRWLockRead(&printer->lock);
+
   curtime = time(NULL);
 
   if (!ra || cupsArrayFind(ra, "marker-change-time"))
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                  "marker-change-time", printer->marker_time);
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "marker-change-time", printer->marker_time);
 
-  if (printer->num_printers > 0 &&
-      (!ra || cupsArrayFind(ra, "member-uris")))
+  if (printer->num_printers > 0 && (!ra || cupsArrayFind(ra, "member-uris")))
   {
     ipp_attribute_t    *member_uris;   /* member-uris attribute */
     cupsd_printer_t    *p2;            /* Printer in class */
     ipp_attribute_t    *p2_uri;        /* printer-uri-supported for class printer */
 
 
-    if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER,
-                                     IPP_TAG_URI, "member-uris",
-                                    printer->num_printers, NULL,
-                                    NULL)) != NULL)
+    if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris", printer->num_printers, NULL, NULL)) != NULL)
     {
       for (i = 0; i < printer->num_printers; i ++)
       {
         p2 = printer->printers[i];
 
-        if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported",
-                                      IPP_TAG_URI)) != NULL)
-          member_uris->values[i].string.text =
-             _cupsStrRetain(p2_uri->values[0].string.text);
+        if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported", IPP_TAG_URI)) != NULL)
+        {
+          member_uris->values[i].string.text = _cupsStrAlloc(p2_uri->values[0].string.text);
+        }
         else
        {
-         httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri,
-                          sizeof(printer_uri), "ipp", NULL, con->clientname,
-                          con->clientport,
-                          (p2->type & CUPS_PRINTER_CLASS) ?
-                              "/classes/%s" : "/printers/%s", p2->name);
-         member_uris->values[i].string.text = _cupsStrAlloc(printer_uri);
+         httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "ipps" : "ipp", NULL, con->clientname, con->clientport, (p2->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : "/printers/%s", p2->name);
+         member_uris->values[i].string.text = _cupsStrAlloc(uri);
         }
       }
     }
   }
 
   if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
-                 "printer-alert", NULL, printer->alert);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING, "printer-alert", NULL, printer->alert);
 
-  if (printer->alert_description &&
-      (!ra || cupsArrayFind(ra, "printer-alert-description")))
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
-                 "printer-alert-description", NULL,
-                printer->alert_description);
+  if (printer->alert_description && (!ra || cupsArrayFind(ra, "printer-alert-description")))
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-alert-description", NULL, printer->alert_description);
 
   if (!ra || cupsArrayFind(ra, "printer-config-change-date-time"))
     ippAddDate(con->response, IPP_TAG_PRINTER, "printer-config-change-date-time", ippTimeToDate(printer->config_time));
 
   if (!ra || cupsArrayFind(ra, "printer-config-change-time"))
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                  "printer-config-change-time", printer->config_time);
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "printer-config-change-time", printer->config_time);
 
   if (!ra || cupsArrayFind(ra, "printer-current-time"))
-    ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
-               ippTimeToDate(curtime));
+    ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time", ippTimeToDate(curtime));
 
 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
   {
     if (printer->reg_name)
-      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                   "printer-dns-sd-name", NULL, printer->reg_name);
+      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-dns-sd-name", NULL, printer->reg_name);
     else
-      ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
-                   "printer-dns-sd-name", 0);
+      ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE, "printer-dns-sd-name", 0);
   }
 #endif /* HAVE_DNSSD || HAVE_AVAHI */
 
   if (!ra || cupsArrayFind(ra, "printer-error-policy"))
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                "printer-error-policy", NULL, printer->error_policy);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-error-policy", NULL, printer->error_policy);
 
   if (!ra || cupsArrayFind(ra, "printer-error-policy-supported"))
   {
     static const char * const errors[] =/* printer-error-policy-supported values */
-                 {
-                   "abort-job",
-                   "retry-current-job",
-                   "retry-job",
-                   "stop-printer"
-                 };
+    {
+      "abort-job",
+      "retry-current-job",
+      "retry-job",
+      "stop-printer"
+    };
 
     if (printer->type & CUPS_PRINTER_CLASS)
-      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
-                   "printer-error-policy-supported", NULL, "retry-current-job");
+      ippAddString(con->response, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_NAME), "printer-error-policy-supported", NULL, "retry-current-job");
     else
-      ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
-                   "printer-error-policy-supported",
-                   sizeof(errors) / sizeof(errors[0]), NULL, errors);
+      ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_CONST_TAG(IPP_TAG_NAME), "printer-error-policy-supported", sizeof(errors) / sizeof(errors[0]), NULL, errors);
   }
 
   if (!ra || cupsArrayFind(ra, "printer-icons"))
   {
-    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_icons, sizeof(printer_icons),
-                     "http", NULL, con->clientname, con->clientport,
-                    "/icons/%s.png", printer->name);
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons",
-                 NULL, printer_icons);
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", printer_icons);
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "https" : "http", NULL, con->clientname, con->clientport, "/icons/%s.png", printer->name);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons", NULL, uri);
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", uri);
   }
 
   if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
@@ -4923,36 +4952,35 @@ copy_printer_attrs(
 
   if (!ra || cupsArrayFind(ra, "printer-more-info"))
   {
-    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
-                     "http", NULL, con->clientname, con->clientport,
-                    (printer->type & CUPS_PRINTER_CLASS) ?
-                        "/classes/%s" : "/printers/%s", printer->name);
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
-                "printer-more-info", NULL, printer_uri);
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "https" : "http", NULL, con->clientname, con->clientport, (printer->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : "/printers/%s", printer->name);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info", NULL, uri);
   }
 
   if (!ra || cupsArrayFind(ra, "printer-op-policy"))
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                "printer-op-policy", NULL, printer->op_policy);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-op-policy", NULL, printer->op_policy);
 
   if (!ra || cupsArrayFind(ra, "printer-state"))
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
-                  printer->state);
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", (int)printer->state);
 
   if (!ra || cupsArrayFind(ra, "printer-state-change-date-time"))
     ippAddDate(con->response, IPP_TAG_PRINTER, "printer-state-change-date-time", ippTimeToDate(printer->state_time));
 
   if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                  "printer-state-change-time", printer->state_time);
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "printer-state-change-time", printer->state_time);
 
   if (!ra || cupsArrayFind(ra, "printer-state-message"))
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
-                "printer-state-message", NULL, printer->state_message);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message", NULL, printer->state_message);
 
   if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
     add_printer_state_reasons(con, printer);
 
+  if (!ra || cupsArrayFind(ra, "printer-strings-uri"))
+  {
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "https" : "http", NULL, con->clientname, con->clientport, "/strings/%s.strings", printer->name);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-strings-uri", NULL, uri);
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-strings-uri=\"%s\"", uri);
+  }
+
   if (!ra || cupsArrayFind(ra, "printer-type"))
   {
     cups_ptype_t type;                 /* printer-type value */
@@ -4976,19 +5004,13 @@ copy_printer_attrs(
   }
 
   if (!ra || cupsArrayFind(ra, "printer-up-time"))
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                  "printer-up-time", curtime);
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "printer-up-time", curtime);
 
   if (!ra || cupsArrayFind(ra, "printer-uri-supported"))
   {
-    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
-                     "ipp", NULL, con->clientname, con->clientport,
-                    (printer->type & CUPS_PRINTER_CLASS) ?
-                        "/classes/%s" : "/printers/%s", printer->name);
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
-                "printer-uri-supported", NULL, printer_uri);
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
-                    printer_uri);
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), is_encrypted ? "ipps" : "ipp", NULL, con->clientname, con->clientport, (printer->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : "/printers/%s", printer->name);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported", NULL, uri);
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", uri);
   }
 
   if (!ra || cupsArrayFind(ra, "queued-job-count"))
@@ -4998,6 +5020,8 @@ copy_printer_attrs(
   if (printer->ppd_attrs)
     copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0, NULL);
   copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY, NULL);
+
+  _cupsRWUnlock(&printer->lock);
 }
 
 
@@ -5042,9 +5066,7 @@ copy_subscription_attrs(
        * Simple event list...
        */
 
-       ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
-                    (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
-                    "notify-events", NULL, name);
+       ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_CONST_TAG(IPP_TAG_KEYWORD), "notify-events", NULL, name);
       }
       else
       {
@@ -5056,15 +5078,12 @@ copy_subscription_attrs(
          if (sub->mask & mask)
            count ++;
 
-       attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
-                            (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
-                            "notify-events", count, NULL, NULL);
+       attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION, IPP_CONST_TAG(IPP_TAG_KEYWORD), "notify-events", count, NULL, NULL);
 
        for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
          if (sub->mask & mask)
          {
-           attr->values[count].string.text =
-               (char *)cupsdEventName((cupsd_eventmask_t)mask);
+           attr->values[count].string.text = (char *)cupsdEventName((cupsd_eventmask_t)mask);
 
            count ++;
          }
@@ -5223,14 +5242,25 @@ create_local_bg_thread(
   ipp_t                *request,               /* Request to printer */
                *response;              /* Response from printer */
   ipp_attribute_t *attr;               /* Attribute in response */
+  ipp_status_t status;                 /* Status code */
+  static const char * const pattrs[] = /* Printer attributes we need */
+  {
+    "all",
+    "media-col-database"
+  };
 
 
  /*
   * Try connecting to the printer...
   */
 
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Generating PPD file from \"%s\"...", printer->name, printer->device_uri);
+
   if (httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Bad device URI \"%s\".", printer->name, printer->device_uri);
     return (NULL);
+  }
 
   if (!strcmp(scheme, "ipps") || port == 443)
     encryption = HTTP_ENCRYPTION_ALWAYS;
@@ -5238,17 +5268,47 @@ create_local_bg_thread(
     encryption = HTTP_ENCRYPTION_IF_REQUESTED;
 
   if ((http = httpConnect2(host, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL)) == NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to connect to %s:%d: %s", printer->name, host, port, cupsLastErrorString());
     return (NULL);
+  }
 
  /*
   * Query the printer for its capabilities...
   */
 
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer->name, host, port);
+
   request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
+  ippSetVersion(request, 2, 0);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri);
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all");
+  ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
 
   response = cupsDoRequest(http, request, resource);
+  status   = cupsLastError();
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Get-Printer-Attributes returned %s (%s)", printer->name, ippErrorString(cupsLastError()), cupsLastErrorString());
+
+  if (status == IPP_STATUS_ERROR_BAD_REQUEST || status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)
+  {
+   /*
+    * Try request using IPP/1.1, in case we are talking to an old CUPS server or
+    * printer...
+    */
+
+    ippDelete(response);
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Re-sending Get-Printer-Attributes request using IPP/1.1...", printer->name);
+
+    request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
+    ippSetVersion(request, 1, 1);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all");
+
+    response = cupsDoRequest(http, request, resource);
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: IPP/1.1 Get-Printer-Attributes returned %s (%s)", printer->name, ippErrorString(cupsLastError()), cupsLastErrorString());
+  }
 
   // TODO: Grab printer icon file...
   httpClose(http);
@@ -5259,6 +5319,8 @@ create_local_bg_thread(
 
   if (_ppdCreateFromIPP(fromppd, sizeof(fromppd), response))
   {
+    _cupsRWLockWrite(&printer->lock);
+
     if ((!printer->info || !*(printer->info)) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
       cupsdSetString(&printer->info, ippGetString(attr, 0, NULL));
 
@@ -5268,12 +5330,18 @@ create_local_bg_thread(
     if ((!printer->geo_location || !*(printer->geo_location)) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
       cupsdSetString(&printer->geo_location, ippGetString(attr, 0, NULL));
 
+    _cupsRWUnlock(&printer->lock);
+
     if ((from = cupsFileOpen(fromppd, "r")) == NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to read generated PPD: %s", printer->name, strerror(errno));
       return (NULL);
+    }
 
     snprintf(toppd, sizeof(toppd), "%s/ppd/%s.ppd", ServerRoot, printer->name);
     if ((to = cupsdCreateConfFile(toppd, ConfigFilePerm)) == NULL)
     {
+      cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to create PPD for printer: %s", printer->name, strerror(errno));
       cupsFileClose(from);
       return (NULL);
     }
@@ -5284,12 +5352,18 @@ create_local_bg_thread(
     cupsFileClose(from);
     if (!cupsdCloseCreatedConfFile(to, toppd))
     {
-      printer->state     = IPP_PSTATE_IDLE;
-      printer->accepting = 1;
+      printer->config_time = time(NULL);
+      printer->state       = IPP_PSTATE_IDLE;
+      printer->accepting   = 1;
 
       cupsdSetPrinterAttrs(printer);
+
+      cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL, "Printer \"%s\" is now available.", printer->name);
+      cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" is now available.", printer->name);
     }
   }
+  else
+    cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed: %s", printer->name, cupsLastErrorString());
 
   return (NULL);
 }
@@ -5382,6 +5456,16 @@ create_local_printer(
   printer_info         = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT);
   printer_location     = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT);
 
+ /*
+  * See if the printer already exists...
+  */
+
+  if ((printer = cupsdFindDest(name)) != NULL)
+  {
+    send_ipp_status(con, IPP_STATUS_ERROR_NOT_POSSIBLE, _("Printer \"%s\" already exists."), name);
+    goto add_printer_attributes;
+  }
+
  /*
   * Create the printer...
   */
@@ -5392,6 +5476,9 @@ create_local_printer(
     return;
   }
 
+  printer->shared    = 0;
+  printer->temporary = 1;
+
   cupsdSetDeviceURI(printer, ippGetString(device_uri, 0, NULL));
 
   if (printer_geo_location)
@@ -5415,8 +5502,10 @@ create_local_printer(
 
   send_ipp_status(con, IPP_STATUS_OK, _("Local printer created."));
 
+  add_printer_attributes:
+
   ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting);
-  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", printer->state);
+  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", (int)printer->state);
   add_printer_state_reasons(con, printer);
 
   httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), httpIsEncrypted(con->http) ? "ipps" : "ipp", NULL, con->clientname, con->clientport, "/printers/%s", printer->name);
@@ -5653,7 +5742,7 @@ create_subscriptions(
 
         snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
                 scheme);
-        if (access(notifier, X_OK))
+        if (access(notifier, X_OK) || !strcmp(scheme, ".") || !strcmp(scheme, ".."))
        {
           send_ipp_status(con, IPP_NOT_POSSIBLE,
                          _("notify-recipient-uri URI \"%s\" uses unknown "
@@ -5743,7 +5832,26 @@ create_subscriptions(
     }
 
     if (recipient)
+    {
       cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
+
+
+      if (!strncmp(recipient, "mailto:", 7) && user_data)
+      {
+        char   temp[64];               /* Temporary string */
+
+       memcpy(temp, user_data->values[0].unknown.data, (size_t)user_data->values[0].unknown.length);
+       temp[user_data->values[0].unknown.length] = '\0';
+
+       if (httpSeparateURI(HTTP_URI_CODING_ALL, temp, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_OK)
+       {
+         send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad notify-user-data \"%s\"."), temp);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM, "notify-status-code", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES);
+         return;
+       }
+      }
+    }
+
     if (pullmethod)
       cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
     cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
@@ -6367,10 +6475,9 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
   ipp_jstate_t job_state;              /* job-state value */
   int          first_job_id = 1,       /* First job ID */
                first_index = 1,        /* First index */
-               current_index = 0;      /* Current index */
-  int          limit = 0;              /* Maximum number of jobs to return */
-  int          count;                  /* Number of jobs that match */
-  int          need_load_job = 0;      /* Do we need to load the job? */
+               limit = 0,              /* Maximum number of jobs to return */
+               count,                  /* Number of jobs that match */
+               need_load_job = 0;      /* Do we need to load the job? */
   const char   *job_attr;              /* Job attribute requested */
   ipp_attribute_t *job_ids;            /* job-ids attribute */
   cupsd_job_t  *job;                   /* Current job pointer */
@@ -6678,9 +6785,12 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
   }
   else
   {
-    for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
-        (limit <= 0 || count < limit) && job;
-        job = (cupsd_job_t *)cupsArrayNext(list))
+    if (first_index > 1)
+      job = (cupsd_job_t *)cupsArrayIndex(list, first_index - 1);
+    else
+      job = (cupsd_job_t *)cupsArrayFirst(list);
+
+    for (count = 0; (limit <= 0 || count < limit) && job; job = (cupsd_job_t *)cupsArrayNext(list))
     {
      /*
       * Filter out jobs that don't match...
@@ -6712,10 +6822,6 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
       if (job->id < first_job_id)
        continue;
 
-      current_index ++;
-      if (current_index < first_index)
-        continue;
-
       if (need_load_job && !job->attrs)
       {
         cupsdLoadJob(job);
@@ -7337,6 +7443,7 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
   ipp_attribute_t *attr;               /* Current attribute */
   int          limit;                  /* Max number of printers to return */
   int          count;                  /* Number of printers that match */
+  int          printer_id;             /* Printer we are interested in */
   cupsd_printer_t *printer;            /* Current printer pointer */
   cups_ptype_t printer_type,           /* printer-type attribute */
                printer_mask;           /* printer-type-mask attribute */
@@ -7390,6 +7497,17 @@ get_printers(cupsd_client_t *con,        /* I - Client connection */
   * Support filtering...
   */
 
+  if ((attr = ippFindAttribute(con->request, "printer-id", IPP_TAG_INTEGER)) != NULL)
+  {
+    if ((printer_id = ippGetInteger(attr, 0)) <= 0)
+    {
+      send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Bad \"printer-id\" value %d."), printer_id);
+      return;
+    }
+  }
+  else
+    printer_id = 0;
+
   if ((attr = ippFindAttribute(con->request, "printer-type",
                                IPP_TAG_ENUM)) != NULL)
     printer_type = (cups_ptype_t)attr->values[0].integer;
@@ -7439,6 +7557,9 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
     if (!local && !printer->shared)
       continue;
 
+    if (printer_id && printer->printer_id != printer_id)
+      continue;
+
     if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
         (printer->type & printer_mask) == printer_type &&
        (!location ||
@@ -7841,13 +7962,16 @@ hold_job(cupsd_client_t  *con,          /* I - Client connection */
   * Hold the job and return...
   */
 
-  if ((attr = ippFindAttribute(con->request, "job-hold-until",
-                              IPP_TAG_KEYWORD)) == NULL)
-    attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
-
-  if (attr)
+  if ((attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_ZERO)) != NULL)
   {
-    when = attr->values[0].string.text;
+    if ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1 || !ippValidateAttribute(attr))
+    {
+      send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value."));
+      ippCopyAttribute(con->response, attr, 0);
+      return;
+    }
+
+    when = ippGetString(attr, 0, NULL);
 
     cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
                  "Job job-hold-until value changed by user.");
@@ -8856,6 +8980,8 @@ release_held_new_jobs(
                     "Printer \"%s\" now printing pending/new jobs (\"%s\").",
                     printer->name, get_username(con));
 
+  cupsdCheckJobs();
+
  /*
   * Everything was ok, so return OK status...
   */
@@ -9816,17 +9942,13 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
   * Fill in the response info...
   */
 
-  httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
-                   con->clientname, con->clientport, "/jobs/%d", jobid);
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
-               job_uri);
+  httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL, con->clientname, con->clientport, "/jobs/%d", jobid);
+  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, job_uri);
 
   ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
-                job->state_value);
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
-               NULL, job->reasons->values[0].string.text);
+  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", (int)job->state_value);
+  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, job->reasons->values[0].string.text);
 
   con->response->request.status.status_code = IPP_OK;
 
@@ -10209,7 +10331,39 @@ set_job_attrs(cupsd_client_t  *con,    /* I - Client connection */
       continue;
     }
 
-    if (!strcmp(attr->name, "job-priority"))
+    if (!ippValidateAttribute(attr))
+    {
+      send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Bad '%s' value."), attr->name);
+      ippCopyAttribute(con->response, attr, 0);
+      return;
+    }
+
+    if (!strcmp(attr->name, "job-hold-until"))
+    {
+      const char *when = ippGetString(attr, 0, NULL);
+                                       /* job-hold-until value */
+
+      if ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1)
+      {
+       send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value."));
+       ippCopyAttribute(con->response, attr, 0);
+       return;
+      }
+
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s", when);
+      cupsdSetJobHoldUntil(job, when, 0);
+
+      if (!strcmp(when, "no-hold"))
+      {
+       cupsdReleaseJob(job);
+       check_jobs = 1;
+      }
+      else
+       cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".", username);
+
+      event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
+    }
+    else if (!strcmp(attr->name, "job-priority"))
     {
      /*
       * Change the job priority...
@@ -10275,7 +10429,7 @@ set_job_attrs(cupsd_client_t  *con,     /* I - Client connection */
 
          case IPP_JOB_PROCESSING :
          case IPP_JOB_STOPPED :
-             if (job->state_value != attr->values[0].integer)
+             if (job->state_value != (ipp_jstate_t)attr->values[0].integer)
              {
                send_ipp_status(con, IPP_NOT_POSSIBLE,
                                _("Job state cannot be changed."));
@@ -10329,28 +10483,6 @@ set_job_attrs(cupsd_client_t  *con,    /* I - Client connection */
       */
 
       ippCopyAttribute(job->attrs, attr, 0);
-
-     /*
-      * See if the job-name or job-hold-until is being changed.
-      */
-
-      if (!strcmp(attr->name, "job-hold-until"))
-      {
-        cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
-                   attr->values[0].string.text);
-        cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
-
-       if (!strcmp(attr->values[0].string.text, "no-hold"))
-       {
-         cupsdReleaseJob(job);
-          check_jobs = 1;
-       }
-       else
-         cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT,
-                          "Job held by \"%s\".", username);
-
-        event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
-      }
     }
     else if (attr->value_tag == IPP_TAG_DELETEATTR)
     {
@@ -10527,7 +10659,7 @@ set_printer_attrs(cupsd_client_t  *con, /* I - Client connection */
  * 'set_printer_defaults()' - Set printer default options from a request.
  */
 
-static void
+static int                             /* O - 1 on success, 0 on failure */
 set_printer_defaults(
     cupsd_client_t  *con,              /* I - Client connection */
     cupsd_printer_t *printer)          /* I - Printer */
@@ -10656,7 +10788,7 @@ set_printer_defaults(
        send_ipp_status(con, IPP_NOT_POSSIBLE,
                        _("Unknown printer-op-policy \"%s\"."),
                        attr->values[0].string.text);
-       return;
+       return (0);
       }
     }
     else if (!strcmp(attr->name, "printer-error-policy"))
@@ -10673,7 +10805,7 @@ set_printer_defaults(
        send_ipp_status(con, IPP_NOT_POSSIBLE,
                        _("Unknown printer-error-policy \"%s\"."),
                        attr->values[0].string.text);
-       return;
+       return (0);
       }
 
       cupsdLogMessage(CUPSD_LOG_DEBUG,
@@ -10769,6 +10901,8 @@ set_printer_defaults(
          break;
     }
   }
+
+  return (1);
 }
 
 
@@ -11166,81 +11300,35 @@ validate_job(cupsd_client_t  *con,    /* I - Client connection */
     }
   }
 
+ /*
+  * Is the job-hold-until value valid?
+  */
+
+  if ((attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_ZERO)) != NULL && ((ippGetValueTag(attr) != IPP_TAG_KEYWORD && ippGetValueTag(attr) != IPP_TAG_NAME && ippGetValueTag(attr) != IPP_TAG_NAMELANG) || ippGetCount(attr) != 1 || !ippValidateAttribute(attr)))
+  {
+    send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-hold-until' value."));
+    ippCopyAttribute(con->response, attr, 0);
+    return;
+  }
+
  /*
   * Is the job-name valid?
   */
 
   if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL)
   {
-    int bad_name = 0;                  /* Is the job-name value bad? */
-
     if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) ||
-        name->num_values != 1)
-    {
-      bad_name = 1;
-    }
-    else
-    {
-     /*
-      * Validate that job-name conforms to RFC 5198 (Network Unicode) and
-      * IPP Everywhere requirements for "name" values...
-      */
-
-      const unsigned char *nameptr;    /* Pointer into "job-name" attribute */
-
-      for (nameptr = (unsigned char *)name->values[0].string.text;
-           *nameptr;
-           nameptr ++)
-      {
-        if (*nameptr < ' ' && *nameptr != '\t')
-          break;
-        else if (*nameptr == 0x7f)
-          break;
-        else if ((*nameptr & 0xe0) == 0xc0)
-        {
-          if ((nameptr[1] & 0xc0) != 0x80)
-            break;
-
-          nameptr ++;
-        }
-        else if ((*nameptr & 0xf0) == 0xe0)
-        {
-          if ((nameptr[1] & 0xc0) != 0x80 ||
-              (nameptr[2] & 0xc0) != 0x80)
-           break;
-
-         nameptr += 2;
-       }
-        else if ((*nameptr & 0xf8) == 0xf0)
-        {
-          if ((nameptr[1] & 0xc0) != 0x80 ||
-             (nameptr[2] & 0xc0) != 0x80 ||
-             (nameptr[3] & 0xc0) != 0x80)
-           break;
-
-         nameptr += 3;
-       }
-        else if (*nameptr & 0x80)
-          break;
-      }
-
-      if (*nameptr)
-        bad_name = 1;
-    }
-
-    if (bad_name)
+        name->num_values != 1 || !ippValidateAttribute(name))
     {
       if (StrictConformance)
       {
-       send_ipp_status(con, IPP_ATTRIBUTES,
-                       _("Unsupported 'job-name' value."));
+       send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Unsupported 'job-name' value."));
        ippCopyAttribute(con->response, name, 0);
        return;
       }
       else
       {
-        cupsdLogMessage(CUPSD_LOG_WARN,
-                        "Unsupported 'job-name' value, deleting from request.");
+        cupsdLogMessage(CUPSD_LOG_WARN, "Unsupported 'job-name' value, deleting from request.");
         ippDeleteAttribute(con->request, name);
       }
     }
@@ -11366,8 +11454,3 @@ validate_user(cupsd_job_t    *job,      /* I - Job */
   return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
                            con, owner) == HTTP_OK);
 }
-
-
-/*
- * End of "$Id$".
- */