]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ipp.c
Update ipp documentation to reflect the behavior of configuring WiFi on IPP USB printers.
[thirdparty/cups.git] / backend / ipp.c
index 778ab873cdfd62cf88c46b9ada876ceb62b8e56a..3779c88fa45ff41c59b0dcfb76c072979b25f3a9 100644 (file)
@@ -1,18 +1,12 @@
 /*
- * "$Id$"
- *
  * IPP backend for CUPS.
  *
- * Copyright 2007-2014 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products, all rights reserved.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * "LICENSE" which should have been included with this file.  If this
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Copyright © 2021 by OpenPrinting
+ * Copyright © 2007-2021 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
  *
- * This file is subject to the Apple OS-Developed Software exception.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
@@ -20,6 +14,7 @@
  */
 
 #include "backend-private.h"
+#include <cups/ppd-private.h>
 #include <cups/array-private.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #  define kPMPrintUIToolAgent  "com.apple.printuitool.agent"
 #  define kPMStartJob          100
 #  define kPMWaitForJob                101
-#  ifdef HAVE_XPC_PRIVATE_H
-#    include <xpc/private.h>
-#  else
 extern void    xpc_connection_set_target_uid(xpc_connection_t connection,
                                              uid_t uid);
-#  endif /* HAVE_XPC_PRIVATE_H */
 #endif /* HAVE_GSSAPI && HAVE_XPC */
 
 
@@ -48,6 +39,8 @@ extern void   xpc_connection_set_target_uid(xpc_connection_t connection,
 #define _CUPS_JSR_ACCOUNT_LIMIT_REACHED                0x08
 #define _CUPS_JSR_JOB_PASSWORD_WAIT            0x10
 #define _CUPS_JSR_JOB_RELEASE_WAIT             0x20
+#define _CUPS_JSR_DOCUMENT_FORMAT_ERROR                0x40
+#define _CUPS_JSR_DOCUMENT_UNPRINTABLE         0x80
 
 
 /*
@@ -64,11 +57,13 @@ typedef struct _cups_monitor_s              /**** Monitoring data ****/
                        version,        /* IPP version */
                        job_id,         /* Job ID for submitted job */
                        job_reasons,    /* Job state reasons bits */
+                       create_job,     /* Support Create-Job? */
                        get_job_attrs;  /* Support Get-Job-Attributes? */
   const char           *job_name;      /* Job name for submitted job */
   http_encryption_t    encryption;     /* Use encryption? */
   ipp_jstate_t         job_state;      /* Current job state */
   ipp_pstate_t         printer_state;  /* Current printer state */
+  int                  retryable;      /* Is this a job that should be retried? */
 } _cups_monitor_t;
 
 
@@ -107,6 +102,7 @@ static const char * const pattrs[] =        /* Printer attributes we want */
   "copies-supported",
   "cups-version",
   "document-format-supported",
+  "job-password-encryption-supported",
   "marker-colors",
   "marker-high-levels",
   "marker-levels",
@@ -121,6 +117,7 @@ static const char * const pattrs[] =        /* Printer attributes we want */
   "printer-alert",
   "printer-alert-description",
   "printer-is-accepting-jobs",
+  "printer-mandatory-job-attributes",
   "printer-state",
   "printer-state-message",
   "printer-state-reasons"
@@ -144,6 +141,8 @@ static cups_option_t        *attr_cache = NULL;
 static cups_array_t    *state_reasons; /* Array of printe-state-reasons keywords */
 static char            tmpfilename[1024] = "";
                                        /* Temporary spool file name */
+static char            mandatory_attrs[1024] = "";
+                                       /* cupsMandatory value */
 
 
 /*
@@ -156,6 +155,7 @@ static void         cancel_job(http_t *http, const char *uri, int id,
 static ipp_pstate_t    check_printer_state(http_t *http, const char *uri,
                                            const char *resource,
                                            const char *user, int version);
+static void            debug_attributes(ipp_t *ipp);
 static void            *monitor_printer(_cups_monitor_t *monitor);
 static ipp_t           *new_request(ipp_op_t op, int version, const char *uri,
                                     const char *user, const char *title,
@@ -218,7 +218,6 @@ main(int  argc,                             /* I - Number of command-line args */
                *compatfile = NULL;     /* Compatibility filename */
   off_t                compatsize = 0;         /* Size of compatibility file */
   int          port;                   /* Port number (not used) */
-  char         portname[255];          /* Port name */
   char         uri[HTTP_MAX_URI];      /* Updated URI without user/pass */
   char         print_job_name[1024];   /* Update job-name for Print-Job */
   http_status_t        http_status;            /* Status of HTTP request */
@@ -232,7 +231,7 @@ main(int  argc,                             /* I - Number of command-line args */
   int          delay,                  /* Delay for retries */
                prev_delay;             /* Previous delay */
   const char   *compression;           /* Compression mode */
-  int          waitjob,                /* Wait for job complete? */
+  int          waitjob,                        /* Wait for job complete? */
                waitjob_tries = 0,      /* Number of times we've waited */
                waitprinter;            /* Wait for printer ready? */
   _cups_monitor_t monitor;             /* Monitoring data */
@@ -245,6 +244,7 @@ main(int  argc,                             /* I - Number of command-line args */
 #endif /* HAVE_LIBZ */
   ipp_attribute_t *copies_sup;         /* copies-supported */
   ipp_attribute_t *cups_version;       /* cups-version */
+  ipp_attribute_t *encryption_sup;     /* job-password-encryption-supported */
   ipp_attribute_t *format_sup;         /* document-format-supported */
   ipp_attribute_t *job_auth;           /* job-authorization-uri */
   ipp_attribute_t *media_col_sup;      /* media-col-supported */
@@ -257,6 +257,7 @@ main(int  argc,                             /* I - Number of command-line args */
                get_job_attrs = 0,      /* Does printer support Get-Job-Attributes? */
                send_document = 0,      /* Does printer support Send-Document? */
                validate_job = 0,       /* Does printer support Validate-Job? */
+               validate_retried = 0,   /* Was Validate-Job request retried? */
                copies,                 /* Number of copies for job */
                copies_remaining;       /* Number of copies remaining */
   const char   *content_type,          /* CONTENT_TYPE environment variable */
@@ -351,8 +352,7 @@ main(int  argc,                             /* I - Number of command-line args */
   * that way.
   */
 
-  if (!getuid() && (value = getenv("AUTH_UID")) != NULL &&
-      !getenv("AUTH_PASSWORD"))
+  if (!getuid() && (value = getenv("AUTH_UID")) != NULL)
   {
     uid_t      uid = (uid_t)atoi(value);
                                        /* User ID */
@@ -386,7 +386,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
 #  else /* No XPC, just try to run as the user ID */
     if (uid > 0)
-      seteuid(uid);
+      setuid(uid);
 #  endif /* HAVE_XPC */
   }
 #endif /* HAVE_GSSAPI */
@@ -418,9 +418,9 @@ main(int  argc,                             /* I - Number of command-line args */
     port = IPP_PORT;                   /* Default to port 631 */
 
   if (!strcmp(scheme, "https") || !strcmp(scheme, "ipps"))
-    cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
+    cupsSetEncryption(HTTP_ENCRYPTION_ALWAYS);
   else
-    cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
+    cupsSetEncryption(HTTP_ENCRYPTION_IF_REQUESTED);
 
  /*
   * See if there are any options...
@@ -507,13 +507,13 @@ main(int  argc,                           /* I - Number of command-line args */
        */
 
         if (!_cups_strcasecmp(value, "always"))
-         cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
+         cupsSetEncryption(HTTP_ENCRYPTION_ALWAYS);
         else if (!_cups_strcasecmp(value, "required"))
-         cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
+         cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED);
         else if (!_cups_strcasecmp(value, "never"))
-         cupsSetEncryption(HTTP_ENCRYPT_NEVER);
+         cupsSetEncryption(HTTP_ENCRYPTION_NEVER);
         else if (!_cups_strcasecmp(value, "ifrequested"))
-         cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
+         cupsSetEncryption(HTTP_ENCRYPTION_IF_REQUESTED);
        else
        {
          _cupsLangPrintFilter(stderr, "ERROR",
@@ -528,8 +528,8 @@ main(int  argc,                             /* I - Number of command-line args */
          */
 
          snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") ||
-                        _cups_strcasecmp(value, "yes") ||
-                        _cups_strcasecmp(value, "true");
+                        !_cups_strcasecmp(value, "yes") ||
+                        !_cups_strcasecmp(value, "true");
       }
       else if (!_cups_strcasecmp(name, "version"))
       {
@@ -657,57 +657,12 @@ main(int  argc,                           /* I - Number of command-line args */
 
   start_time = time(NULL);
 
-  sprintf(portname, "%d", port);
-
-  update_reasons(NULL, "+connecting-to-device");
-  fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
-
-  while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
-  {
-    _cupsLangPrintFilter(stderr, "INFO",
-                         _("Unable to locate printer \"%s\"."), hostname);
-    sleep(10);
-
-    if (getenv("CLASS") != NULL)
-    {
-      update_reasons(NULL, "-connecting-to-device");
-      return (CUPS_BACKEND_STOP);
-    }
-
-    if (job_canceled)
-      return (CUPS_BACKEND_OK);
-  }
+  addrlist = backendLookup(hostname, port, &job_canceled);
 
   http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, cupsEncryption(), 1,
                       0, NULL);
   httpSetTimeout(http, 30.0, timeout_cb, NULL);
 
-  if (httpIsEncrypted(http))
-  {
-   /*
-    * Validate TLS credentials...
-    */
-
-    cups_array_t       *creds;         /* TLS credentials */
-    http_trust_t       trust;          /* Trust level */
-    static const char  *trusts[] = { NULL, "+cups-pki-invalid", "+cups-pki-changed", "+cups-pki-expired", NULL, "+cups-pki-unknown" };
-                                       /* Trust keywords */
-
-    if (!httpCopyCredentials(http, &creds))
-    {
-      trust = httpCredentialsGetTrust(creds, hostname);
-
-      update_reasons(NULL, "-cups-pki-invalid,cups-pki-changed,cups-pki-expired,cups-pki-unknown");
-      if (trusts[trust])
-      {
-        update_reasons(NULL, trusts[trust]);
-        return (CUPS_BACKEND_STOP);
-      }
-
-      httpFreeCredentials(creds);
-    }
-  }
-
  /*
   * See if the printer supports SNMP...
   */
@@ -746,11 +701,11 @@ main(int  argc,                           /* I - Number of command-line args */
     fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
     _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
 
-    if (httpReconnect(http))
+    if (httpReconnect2(http, 30000, NULL))
     {
       int error = errno;               /* Connection error */
 
-      if (http->status == HTTP_PKI_ERROR)
+      if (http->status == HTTP_STATUS_CUPS_PKI_ERROR)
        update_reasons(NULL, "+cups-certificate-error");
 
       if (job_canceled)
@@ -782,8 +737,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
       fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
 
-      if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
-          errno == EHOSTUNREACH)
+      if (errno == ECONNREFUSED || errno == EHOSTDOWN || errno == EHOSTUNREACH || errno == ETIMEDOUT || errno == ENOTCONN)
       {
         if (contimeout && (time(NULL) - start_time) > contimeout)
        {
@@ -802,13 +756,13 @@ main(int  argc,                           /* I - Number of command-line args */
              break;
 
          case EHOSTUNREACH :
+         default :
              _cupsLangPrintFilter(stderr, "WARNING",
                                   _("The printer is unreachable at this "
                                     "time."));
              break;
 
          case ECONNREFUSED :
-         default :
              _cupsLangPrintFilter(stderr, "WARNING",
                                   _("The printer is in use."));
              break;
@@ -838,6 +792,76 @@ main(int  argc,                            /* I - Number of command-line args */
   else if (!http)
     return (CUPS_BACKEND_FAILED);
 
+  if (httpIsEncrypted(http))
+  {
+   /*
+    * Validate TLS credentials...
+    */
+
+    cups_array_t       *creds;         /* TLS credentials */
+    cups_array_t       *lcreds = NULL; /* Loaded credentials */
+    http_trust_t       trust;          /* Trust level */
+    char               credinfo[1024], /* Information on credentials */
+                       lcredinfo[1024];/* Information on saved credentials */
+    static const char  * const trusts[] = { NULL, "+cups-pki-invalid", "+cups-pki-changed", "+cups-pki-expired", NULL, "+cups-pki-unknown" };
+                                       /* Trust keywords */
+    static const char  * const trust_msgs[] =
+    {
+      "Credentials are OK/trusted",
+      "Credentials are invalid",
+      "Credentials have changed",
+      "Credentials are expired",
+      "Credentials have been renewed",
+      "Credentials are unknown/new"
+    };
+
+    fputs("DEBUG: Connection is encrypted.\n", stderr);
+
+    if (!httpCopyCredentials(http, &creds))
+    {
+      trust = httpCredentialsGetTrust(creds, hostname);
+      httpCredentialsString(creds, credinfo, sizeof(credinfo));
+
+      fprintf(stderr, "DEBUG: %s (%s)\n", trust_msgs[trust], cupsLastErrorString());
+      fprintf(stderr, "DEBUG: Printer credentials: %s\n", credinfo);
+
+      if (!httpLoadCredentials(NULL, &lcreds, hostname))
+      {
+        httpCredentialsString(lcreds, lcredinfo, sizeof(lcredinfo));
+       fprintf(stderr, "DEBUG: Stored credentials: %s\n", lcredinfo);
+      }
+      else
+        fputs("DEBUG: No stored credentials.\n", stderr);
+
+      update_reasons(NULL, "-cups-pki-invalid,cups-pki-changed,cups-pki-expired,cups-pki-unknown");
+      if (trusts[trust])
+      {
+        update_reasons(NULL, trusts[trust]);
+        return (CUPS_BACKEND_STOP);
+      }
+
+      if (!lcreds)
+      {
+       /*
+        * Could not load the credentials, let's save the ones we have so we
+        * can detect changes...
+        */
+
+        httpSaveCredentials(NULL, creds, hostname);
+      }
+
+      httpFreeCredentials(lcreds);
+      httpFreeCredentials(creds);
+    }
+    else
+    {
+      fputs("DEBUG: No printer credentials.\n", stderr);
+
+      update_reasons(NULL, "cups-pki-unknown");
+      return (CUPS_BACKEND_STOP);
+    }
+  }
+
   update_reasons(NULL, "-connecting-to-device");
   _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
 
@@ -864,6 +888,7 @@ main(int  argc,                             /* I - Number of command-line args */
 #endif /* HAVE_LIBZ */
   copies_sup           = NULL;
   cups_version         = NULL;
+  encryption_sup       = NULL;
   format_sup           = NULL;
   media_col_sup        = NULL;
   supported            = NULL;
@@ -883,7 +908,7 @@ main(int  argc,                             /* I - Number of command-line args */
     * Build the IPP request...
     */
 
-    request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+    request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
     ippSetVersion(request, version / 10, version % 10);
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                 NULL, uri);
@@ -898,7 +923,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
     fputs("DEBUG: Getting supported attributes...\n", stderr);
 
-    if (http->version < HTTP_1_1)
+    if (http->version < HTTP_VERSION_1_1)
     {
       fprintf(stderr, "DEBUG: Printer responded with HTTP version %d.%d.\n",
               http->version / 100, http->version % 100);
@@ -912,15 +937,15 @@ main(int  argc,                           /* I - Number of command-line args */
     fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
             ippErrorString(ipp_status), cupsLastErrorString());
 
-    if (ipp_status <= IPP_OK_CONFLICT)
+    if (ipp_status <= IPP_STATUS_OK_CONFLICTING)
       password_tries = 0;
     else
     {
       fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
               ippErrorString(ipp_status));
 
-      if (ipp_status == IPP_PRINTER_BUSY ||
-         ipp_status == IPP_SERVICE_UNAVAILABLE)
+      if (ipp_status == IPP_STATUS_ERROR_BUSY ||
+         ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE)
       {
         if (contimeout && (time(NULL) - start_time) > contimeout)
        {
@@ -937,8 +962,8 @@ main(int  argc,                             /* I - Number of command-line args */
 
         delay = _cupsNextDelay(delay, &prev_delay);
       }
-      else if ((ipp_status == IPP_BAD_REQUEST ||
-               ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10)
+      else if ((ipp_status == IPP_STATUS_ERROR_BAD_REQUEST ||
+               ipp_status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) && version > 10)
       {
        /*
        * Switch to IPP/1.1 or IPP/1.0...
@@ -961,9 +986,9 @@ main(int  argc,                             /* I - Number of command-line args */
          version = 10;
         }
 
-       httpReconnect(http);
+       httpReconnect2(http, 30000, NULL);
       }
-      else if (ipp_status == IPP_NOT_FOUND)
+      else if (ipp_status == IPP_STATUS_ERROR_NOT_FOUND)
       {
         _cupsLangPrintFilter(stderr, "ERROR",
                             _("The printer configuration is incorrect or the "
@@ -973,8 +998,8 @@ main(int  argc,                             /* I - Number of command-line args */
 
        return (CUPS_BACKEND_STOP);
       }
-      else if (ipp_status == IPP_FORBIDDEN ||
-               ipp_status == IPP_AUTHENTICATION_CANCELED)
+      else if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
+               ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
       {
         const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
                                        /* WWW-Authenticate field value */
@@ -987,13 +1012,13 @@ main(int  argc,                          /* I - Number of command-line args */
        fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
        return (CUPS_BACKEND_AUTH_REQUIRED);
       }
-      else if (ipp_status != IPP_NOT_AUTHORIZED)
+      else if (ipp_status != IPP_STATUS_ERROR_NOT_AUTHORIZED)
       {
        _cupsLangPrintFilter(stderr, "ERROR",
                             _("Unable to get printer status."));
         sleep(10);
 
-       httpReconnect(http);
+       httpReconnect2(http, 30000, NULL);
       }
 
       ippDelete(supported);
@@ -1077,7 +1102,7 @@ main(int  argc,                           /* I - Number of command-line args */
                         "compression value \"%s\".\n", compression);
         compression = NULL;
       }
-      else if (!compression)
+      else if (!compression && (!strcmp(final_content_type, "image/pwg-raster") || !strcmp(final_content_type, "image/urf")))
       {
         if (ippContainsString(compression_sup, "gzip"))
           compression = "gzip";
@@ -1107,7 +1132,16 @@ main(int  argc,                          /* I - Number of command-line args */
        copies_sup = NULL; /* No */
     }
 
-    cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT);
+    if ((cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT)) != NULL)
+    {
+      const char *val = ippGetString(cups_version, 0, NULL);
+
+      fprintf(stderr, "DEBUG: cups-version = \"%s\"\n", val);
+      if (!strcmp(val, "cups-version"))
+        cups_version = NULL;           /* Bogus cups-version value returned by buggy printers! */
+    }
+
+    encryption_sup = ippFindAttribute(supported, "job-password-encryption-supported", IPP_TAG_KEYWORD);
 
     if ((format_sup = ippFindAttribute(supported, "document-format-supported",
                                       IPP_TAG_MIMETYPE)) != NULL)
@@ -1141,7 +1175,7 @@ main(int  argc,                           /* I - Number of command-line args */
                 ippOpString(operations_sup->values[i].integer));
 
       for (i = 0; i < operations_sup->num_values; i ++)
-        if (operations_sup->values[i].integer == IPP_PRINT_JOB)
+        if (operations_sup->values[i].integer == IPP_OP_PRINT_JOB)
          break;
 
       if (i >= operations_sup->num_values)
@@ -1149,7 +1183,7 @@ main(int  argc,                           /* I - Number of command-line args */
                             "cups-ipp-missing-print-job");
 
       for (i = 0; i < operations_sup->num_values; i ++)
-        if (operations_sup->values[i].integer == IPP_CANCEL_JOB)
+        if (operations_sup->values[i].integer == IPP_OP_CANCEL_JOB)
          break;
 
       if (i >= operations_sup->num_values)
@@ -1157,7 +1191,7 @@ main(int  argc,                           /* I - Number of command-line args */
                             "cups-ipp-missing-cancel-job");
 
       for (i = 0; i < operations_sup->num_values; i ++)
-        if (operations_sup->values[i].integer == IPP_GET_JOB_ATTRIBUTES)
+        if (operations_sup->values[i].integer == IPP_OP_GET_JOB_ATTRIBUTES)
          break;
 
       if (i >= operations_sup->num_values)
@@ -1165,7 +1199,7 @@ main(int  argc,                           /* I - Number of command-line args */
                              "cups-ipp-missing-get-job-attributes");
 
       for (i = 0; i < operations_sup->num_values; i ++)
-        if (operations_sup->values[i].integer == IPP_GET_PRINTER_ATTRIBUTES)
+        if (operations_sup->values[i].integer == IPP_OP_GET_PRINTER_ATTRIBUTES)
          break;
 
       if (i >= operations_sup->num_values)
@@ -1174,13 +1208,13 @@ main(int  argc,                         /* I - Number of command-line args */
 
       for (i = 0; i < operations_sup->num_values; i ++)
       {
-        if (operations_sup->values[i].integer == IPP_VALIDATE_JOB)
+        if (operations_sup->values[i].integer == IPP_OP_VALIDATE_JOB)
          validate_job = 1;
-        else if (operations_sup->values[i].integer == IPP_CREATE_JOB)
+        else if (operations_sup->values[i].integer == IPP_OP_CREATE_JOB)
          create_job = 1;
-        else if (operations_sup->values[i].integer == IPP_SEND_DOCUMENT)
+        else if (operations_sup->values[i].integer == IPP_OP_SEND_DOCUMENT)
          send_document = 1;
-        else if (operations_sup->values[i].integer == IPP_GET_JOB_ATTRIBUTES)
+        else if (operations_sup->values[i].integer == IPP_OP_GET_JOB_ATTRIBUTES)
          get_job_attrs = 1;
       }
 
@@ -1208,7 +1242,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
     report_printer_state(supported);
   }
-  while (!job_canceled && ipp_status > IPP_OK_CONFLICT);
+  while (!job_canceled && ipp_status > IPP_STATUS_OK_CONFLICTING);
 
   if (job_canceled)
     return (CUPS_BACKEND_OK);
@@ -1226,7 +1260,7 @@ main(int  argc,                           /* I - Number of command-line args */
                                         IPP_TAG_BOOLEAN);
 
     if (printer_state == NULL ||
-       (printer_state->values[0].integer > IPP_PRINTER_PROCESSING &&
+       (printer_state->values[0].integer > IPP_PSTATE_PROCESSING &&
         waitprinter) ||
        printer_accepting == NULL ||
        !printer_accepting->values[0].boolean)
@@ -1282,11 +1316,51 @@ main(int  argc,                         /* I - Number of command-line args */
       * Load the PPD file and generate PWG attribute mapping information...
       */
 
+      ppd_attr_t *mandatory;           /* cupsMandatory value */
+
       ppd = ppdOpenFile(getenv("PPD"));
       pc  = _ppdCacheCreateWithPPD(ppd);
 
       ppdMarkDefaults(ppd);
       cupsMarkOptions(ppd, num_options, options);
+
+      if ((mandatory = ppdFindAttr(ppd, "cupsMandatory", NULL)) != NULL)
+        strlcpy(mandatory_attrs, mandatory->value, sizeof(mandatory_attrs));
+    }
+
+   /*
+    * Validate job-password/-encryption...
+    */
+
+    if (cupsGetOption("job-password", num_options, options))
+    {
+      const char *keyword;             /* job-password-encryption value */
+      static const char * const hashes[] =
+      {                                        /* List of supported hash algorithms, in order of preference */
+        "sha-512",
+        "sha-384",
+        "sha-512_256",
+        "sha-512-224",
+        "sha-256",
+        "sha-224",
+        "sha",
+        "none"
+      };
+
+      if ((keyword = cupsGetOption("job-password-encryption", num_options, options)) == NULL || !ippContainsString(encryption_sup, keyword))
+      {
+       /*
+        * Either no job-password-encryption or the value isn't supported by
+        * the printer...
+        */
+
+        for (i = 0; i < (int)(sizeof(hashes) / sizeof(hashes[0])); i ++)
+          if (ippContainsString(encryption_sup, hashes[i]))
+            break;
+
+        if (i < (int)(sizeof(hashes) / sizeof(hashes[0])))
+          num_options = cupsAddOption("job-password-encryption", hashes[i], num_options, &options);
+      }
     }
   }
   else
@@ -1296,24 +1370,10 @@ main(int  argc,                         /* I - Number of command-line args */
 
   if (format_sup != NULL)
   {
-    for (i = 0; i < format_sup->num_values; i ++)
-      if (!_cups_strcasecmp(final_content_type,
-                            format_sup->values[i].string.text))
-      {
-        document_format = final_content_type;
-       break;
-      }
-
-    if (!document_format)
-    {
-      for (i = 0; i < format_sup->num_values; i ++)
-       if (!_cups_strcasecmp("application/octet-stream",
-                             format_sup->values[i].string.text))
-       {
-         document_format = "application/octet-stream";
-         break;
-       }
-    }
+    if (ippContainsString(format_sup, final_content_type))
+      document_format = final_content_type;
+    else if (ippContainsString(format_sup, "application/octet-stream"))
+      document_format = "application/octet-stream";
   }
 
   fprintf(stderr, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
@@ -1326,7 +1386,7 @@ main(int  argc,                           /* I - Number of command-line args */
   * (I hate compatibility hacks!)
   */
 
-  if (http->version < HTTP_1_1 && num_files == 0)
+  if (http->version < HTTP_VERSION_1_1 && num_files == 0)
   {
     if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
     {
@@ -1354,7 +1414,7 @@ main(int  argc,                           /* I - Number of command-line args */
     files      = &compatfile;
     num_files  = 1;
   }
-  else if (http->version < HTTP_1_1 && num_files == 1)
+  else if (http->version < HTTP_VERSION_1_1 && num_files == 1)
   {
     struct stat        fileinfo;               /* File information */
 
@@ -1384,10 +1444,14 @@ main(int  argc,                         /* I - Number of command-line args */
   monitor.port          = port;
   monitor.version       = version;
   monitor.job_id        = 0;
+  monitor.create_job    = create_job;
   monitor.get_job_attrs = get_job_attrs;
   monitor.encryption    = cupsEncryption();
-  monitor.job_state     = IPP_JOB_PENDING;
-  monitor.printer_state = IPP_PRINTER_IDLE;
+  monitor.job_state     = IPP_JSTATE_PENDING;
+  monitor.printer_state = IPP_PSTATE_IDLE;
+  monitor.retryable     = argc == 6 && document_format && strcmp(document_format, "image/pwg-raster") && strcmp(document_format, "image/urf");
+
+  fprintf(stderr, "DEBUG: retryable=%d\n", monitor.retryable);
 
   if (create_job)
   {
@@ -1408,7 +1472,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
   while (!job_canceled && validate_job)
   {
-    request = new_request(IPP_VALIDATE_JOB, version, uri, argv[2],
+    request = new_request(IPP_OP_VALIDATE_JOB, version, uri, argv[2],
                           monitor.job_name, num_options, options, compression,
                          copies_sup ? copies : 1, document_format, pc, ppd,
                          media_col_sup, doc_handling_sup, print_color_mode_sup);
@@ -1419,6 +1483,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
     fprintf(stderr, "DEBUG: Validate-Job: %s (%s)\n",
             ippErrorString(ipp_status), cupsLastErrorString());
+    debug_attributes(response);
 
     if ((job_auth = ippFindAttribute(response, "job-authorization-uri",
                                     IPP_TAG_URI)) != NULL)
@@ -1426,6 +1491,30 @@ main(int  argc,                          /* I - Number of command-line args */
                                   ippGetString(job_auth, 0, NULL), num_options,
                                   &options);
 
+    if (ipp_status == IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED || ipp_status == IPP_STATUS_OK_CONFLICTING)
+    {
+     /*
+      * One or more options are not supported...
+      */
+
+      ipp_attribute_t  *attr;          /* Unsupported attribute */
+
+      if ((attr = ippFindAttribute(response, "sides", IPP_TAG_ZERO)) != NULL)
+      {
+       /*
+        * The sides value is not supported, revert to one-sided as needed...
+        */
+
+        const char *sides = cupsGetOption("sides", num_options, options);
+
+        if (!sides || !strncmp(sides, "two-sided-", 10))
+        {
+          fputs("DEBUG: Unable to do two-sided printing, setting sides to 'one-sided'.\n", stderr);
+          num_options = cupsAddOption("sides", "one-sided", num_options, &options);
+        }
+      }
+    }
+
     ippDelete(response);
 
     if (job_canceled)
@@ -1445,6 +1534,7 @@ main(int  argc,                           /* I - Number of command-line args */
              ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
       goto cleanup;
     else if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
+             ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED ||
             ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
     {
       const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
@@ -1467,9 +1557,21 @@ main(int  argc,                          /* I - Number of command-line args */
                           "cups-ipp-missing-validate-job");
       break;
     }
-    else if (ipp_status < IPP_REDIRECTION_OTHER_SITE ||
-             ipp_status == IPP_BAD_REQUEST)
+    else if (ipp_status < IPP_STATUS_REDIRECTION_OTHER_SITE ||
+             ipp_status == IPP_STATUS_ERROR_BAD_REQUEST)
       break;
+    else if (job_auth == NULL && ipp_status > IPP_STATUS_ERROR_BAD_REQUEST)
+    {
+      if (!validate_retried)
+      {
+        // Retry Validate-Job operation once, to work around known printer bug...
+        validate_retried = 1;
+        sleep(10);
+        continue;
+      }
+
+      goto cleanup;
+    }
   }
 
  /*
@@ -1493,8 +1595,8 @@ main(int  argc,                           /* I - Number of command-line args */
     if (job_canceled)
       break;
 
-    request = new_request((num_files > 1 || create_job) ? IPP_CREATE_JOB :
-                                                          IPP_PRINT_JOB,
+    request = new_request((num_files > 1 || create_job) ? IPP_OP_CREATE_JOB :
+                                                          IPP_OP_PRINT_JOB,
                          version, uri, argv[2], monitor.job_name, num_options,
                          options, compression, copies_sup ? copies : 1,
                          document_format, pc, ppd, media_col_sup,
@@ -1519,7 +1621,7 @@ main(int  argc,                           /* I - Number of command-line args */
         fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr);
 
       http_status = cupsSendRequest(http, request, resource, length);
-      if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
+      if (http_status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA)
       {
        if (compression && strcmp(compression, "none"))
          httpSetField(http, HTTP_FIELD_CONTENT_ENCODING, compression);
@@ -1538,7 +1640,7 @@ main(int  argc,                           /* I - Number of command-line args */
          http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
         }
 
-        while (http_status == HTTP_CONTINUE &&
+        while (http_status == HTTP_STATUS_CONTINUE &&
                (!job_canceled || compatsize > 0))
        {
         /*
@@ -1548,6 +1650,7 @@ main(int  argc,                           /* I - Number of command-line args */
           FD_ZERO(&input);
          FD_SET(fd, &input);
          FD_SET(snmp_fd, &input);
+         FD_SET(CUPS_SC_FD, &input);
 
           while (select(fd > snmp_fd ? fd + 1 : snmp_fd + 1, &input, NULL, NULL,
                        NULL) <= 0 && !job_canceled);
@@ -1562,7 +1665,7 @@ main(int  argc,                           /* I - Number of command-line args */
              fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
 
              if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
-                     != HTTP_CONTINUE)
+                     != HTTP_STATUS_CONTINUE)
                break;
            }
            else if (bytes == 0 || (errno != EINTR && errno != EAGAIN))
@@ -1570,7 +1673,7 @@ main(int  argc,                           /* I - Number of command-line args */
          }
        }
 
-       if (http_status == HTTP_ERROR)
+       if (http_status == HTTP_STATUS_ERROR)
          fprintf(stderr, "DEBUG: Error writing document data for "
                          "Print-Job: %s\n", strerror(httpError(http)));
 
@@ -1587,8 +1690,9 @@ main(int  argc,                           /* I - Number of command-line args */
     fprintf(stderr, "DEBUG: %s: %s (%s)\n",
             (num_files > 1 || create_job) ? "Create-Job" : "Print-Job",
             ippErrorString(ipp_status), cupsLastErrorString());
+    debug_attributes(response);
 
-    if (ipp_status > IPP_OK_CONFLICT)
+    if (ipp_status > IPP_STATUS_OK_CONFLICTING)
     {
       job_id = 0;
 
@@ -1640,7 +1744,7 @@ main(int  argc,                           /* I - Number of command-line args */
          else if (www_auth[0])
            auth_info_required = "username,password";
        }
-       else if (ipp_status == IPP_REQUEST_VALUE)
+       else if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE)
        {
         /*
          * Print file is too large, abort this job...
@@ -1696,7 +1800,7 @@ main(int  argc,                           /* I - Number of command-line args */
         * Send the next file in the job...
        */
 
-       request = ippNewRequest(IPP_SEND_DOCUMENT);
+       request = ippNewRequest(IPP_OP_SEND_DOCUMENT);
        ippSetVersion(request, version / 10, version % 10);
 
        ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
@@ -1721,8 +1825,11 @@ main(int  argc,                          /* I - Number of command-line args */
                       "compression", NULL, compression);
 
        fprintf(stderr, "DEBUG: Sending file %d using chunking...\n", i + 1);
+       fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
+       debug_attributes(request);
+
        http_status = cupsSendRequest(http, request, resource, 0);
-       if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
+       if (http_status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA)
        {
          if (compression && strcmp(compression, "none"))
            httpSetField(http, HTTP_FIELD_CONTENT_ENCODING, compression);
@@ -1746,11 +1853,11 @@ main(int  argc,                         /* I - Number of command-line args */
 
        if (fd >= 0)
        {
-         while (!job_canceled && http_status == HTTP_CONTINUE &&
+         while (!job_canceled && http_status == HTTP_STATUS_CONTINUE &&
                 (bytes = read(fd, buffer, sizeof(buffer))) > 0)
          {
            if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
-                   != HTTP_CONTINUE)
+                   != HTTP_STATUS_CONTINUE)
              break;
            else
            {
@@ -1766,26 +1873,36 @@ main(int  argc,                         /* I - Number of command-line args */
            close(fd);
        }
 
-        if (http_status == HTTP_ERROR)
+        if (http_status == HTTP_STATUS_ERROR)
           fprintf(stderr, "DEBUG: Error writing document data for "
                           "Send-Document: %s\n", strerror(httpError(http)));
 
-       ippDelete(cupsGetResponse(http, resource));
+       response = cupsGetResponse(http, resource);
        ippDelete(request);
 
-       fprintf(stderr, "DEBUG: Send-Document: %s (%s)\n",
-               ippErrorString(cupsLastError()), cupsLastErrorString());
+       fprintf(stderr, "DEBUG: Send-Document: %s (%s)\n", ippErrorString(cupsLastError()), cupsLastErrorString());
+        debug_attributes(response);
 
-       if (cupsLastError() > IPP_OK_CONFLICT)
+       if (cupsLastError() > IPP_STATUS_OK_CONFLICTING && !job_canceled)
        {
+         ipp_attribute_t *reasons = ippFindAttribute(response, "job-state-reasons", IPP_TAG_KEYWORD);
+                                       /* job-state-reasons values */
+
          ipp_status = cupsLastError();
 
-         _cupsLangPrintFilter(stderr, "ERROR",
-                              _("Unable to add document to print job."));
+          if (ippContainsString(reasons, "document-format-error"))
+            ipp_status = IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR;
+          else if (ippContainsString(reasons, "document-unprintable"))
+            ipp_status = IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE;
+
+         ippDelete(response);
+         _cupsLangPrintFilter(stderr, "ERROR", _("Unable to add document to print job."));
          break;
        }
        else
        {
+         ippDelete(response);
+
          password_tries = 0;
 
          if (num_files == 0 || fd < 0)
@@ -1794,12 +1911,15 @@ main(int  argc,                         /* I - Number of command-line args */
       }
     }
 
-    if (ipp_status <= IPP_OK_CONFLICT && argc > 6)
+    if (job_canceled)
+      break;
+
+    if (ipp_status <= IPP_STATUS_OK_CONFLICTING && argc > 6)
     {
       fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
       copies_remaining --;
     }
-    else if ((ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR || ipp_status == IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE) &&
+    else if ((ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED || ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR || ipp_status == IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE) &&
              argc == 6 &&
              document_format && strcmp(document_format, "image/pwg-raster") && strcmp(document_format, "image/urf"))
     {
@@ -1813,9 +1933,9 @@ main(int  argc,                           /* I - Number of command-line args */
 
       goto cleanup;
     }
-    else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
-             ipp_status == IPP_NOT_POSSIBLE ||
-            ipp_status == IPP_PRINTER_BUSY)
+    else if (ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE ||
+             ipp_status == IPP_STATUS_ERROR_NOT_POSSIBLE ||
+            ipp_status == IPP_STATUS_ERROR_BUSY)
     {
       if (argc == 6)
       {
@@ -1831,14 +1951,14 @@ main(int  argc,                         /* I - Number of command-line args */
       }
       continue;
     }
-    else if (ipp_status == IPP_REQUEST_VALUE ||
-             ipp_status == IPP_ERROR_JOB_CANCELED ||
-             ipp_status == IPP_NOT_AUTHORIZED ||
+    else if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE ||
+             ipp_status == IPP_STATUS_ERROR_JOB_CANCELED ||
+             ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED ||
              ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
              ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
              ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
              ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED ||
-             ipp_status == IPP_INTERNAL_ERROR)
+             ipp_status == IPP_STATUS_ERROR_INTERNAL)
     {
      /*
       * Print file is too large, job was canceled, we need new
@@ -1868,7 +1988,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
       goto cleanup;
     }
-    else if (ipp_status == IPP_NOT_FOUND)
+    else if (ipp_status == IPP_STATUS_ERROR_NOT_FOUND)
     {
      /*
       * Printer does not actually implement support for Create-Job/
@@ -1882,7 +2002,7 @@ main(int  argc,                           /* I - Number of command-line args */
       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
                           "cups-ipp-missing-send-document");
 
-      ipp_status = IPP_INTERNAL_ERROR; /* Force queue to stop */
+      ipp_status = IPP_STATUS_ERROR_INTERNAL;  /* Force queue to stop */
 
       goto cleanup;
     }
@@ -1914,14 +2034,14 @@ main(int  argc,                         /* I - Number of command-line args */
 
       check_printer_state(http, uri, resource, argv[2], version);
 
-      if (cupsLastError() <= IPP_OK_CONFLICT)
+      if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
         password_tries = 0;
 
      /*
-      * Build an IPP_GET_JOB_ATTRIBUTES request...
+      * Build an IPP_OP_GET_JOB_ATTRIBUTES request...
       */
 
-      request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
+      request = ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES);
       ippSetVersion(request, version / 10, version % 10);
 
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
@@ -1938,15 +2058,18 @@ main(int  argc,                         /* I - Number of command-line args */
                     "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
                    NULL, jattrs);
 
+      fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
+      debug_attributes(request);
+
      /*
       * Do the request...
       */
 
-      httpReconnect(http);
+      httpReconnect2(http, 30000, NULL);
       response   = cupsDoRequest(http, request, resource);
       ipp_status = cupsLastError();
 
-      if (ipp_status == IPP_NOT_FOUND || ipp_status == IPP_NOT_POSSIBLE)
+      if (ipp_status == IPP_STATUS_ERROR_NOT_FOUND || ipp_status == IPP_STATUS_ERROR_NOT_POSSIBLE)
       {
        /*
         * Job has gone away and/or the server has no job history...
@@ -1956,32 +2079,33 @@ main(int  argc,                         /* I - Number of command-line args */
                             "cups-ipp-missing-job-history");
         ippDelete(response);
 
-       ipp_status = IPP_OK;
+       ipp_status = IPP_STATUS_OK;
         break;
       }
 
       fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
              ippErrorString(ipp_status), cupsLastErrorString());
+      debug_attributes(response);
 
-      if (ipp_status <= IPP_OK_CONFLICT)
+      if (ipp_status <= IPP_STATUS_OK_CONFLICTING)
        password_tries = 0;
       else
       {
-       if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
-           ipp_status != IPP_PRINTER_BUSY)
+       if (ipp_status != IPP_STATUS_ERROR_SERVICE_UNAVAILABLE &&
+           ipp_status != IPP_STATUS_ERROR_BUSY)
        {
          ippDelete(response);
-          ipp_status = IPP_OK;
+          ipp_status = IPP_STATUS_OK;
           break;
        }
-       else if (ipp_status == IPP_INTERNAL_ERROR)
+       else if (ipp_status == IPP_STATUS_ERROR_INTERNAL)
        {
          waitjob_tries ++;
 
          if (waitjob_tries > 4)
          {
            ippDelete(response);
-           ipp_status = IPP_OK;
+           ipp_status = IPP_STATUS_OK;
            break;
          }
        }
@@ -1997,18 +2121,14 @@ main(int  argc,                         /* I - Number of command-line args */
          */
 
          if (cups_version &&
-             job_state->values[0].integer >= IPP_JOB_PENDING &&
-             job_state->values[0].integer <= IPP_JOB_COMPLETED)
+             job_state->values[0].integer >= IPP_JSTATE_PENDING &&
+             job_state->values[0].integer <= IPP_JSTATE_COMPLETED)
            update_reasons(NULL,
                           remote_job_states[job_state->values[0].integer -
-                                            IPP_JOB_PENDING]);
+                                            IPP_JSTATE_PENDING]);
 
-         if ((job_sheets = ippFindAttribute(response,
-                                            "job-media-sheets-completed",
-                                            IPP_TAG_INTEGER)) == NULL)
-           job_sheets = ippFindAttribute(response,
-                                         "job-impressions-completed",
-                                         IPP_TAG_INTEGER);
+         if ((job_sheets = ippFindAttribute(response, "job-impressions-completed", IPP_TAG_INTEGER)) == NULL)
+           job_sheets = ippFindAttribute(response, "job-media-sheets-completed", IPP_TAG_INTEGER);
 
          if (job_sheets)
            fprintf(stderr, "PAGE: total %d\n",
@@ -2018,15 +2138,15 @@ main(int  argc,                         /* I - Number of command-line args */
           * Stop polling if the job is finished or pending-held...
          */
 
-          if (job_state->values[0].integer > IPP_JOB_STOPPED)
+          if (job_state->values[0].integer > IPP_JSTATE_STOPPED || job_state->values[0].integer == IPP_JSTATE_HELD)
          {
            ippDelete(response);
            break;
          }
        }
-       else if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
-                ipp_status != IPP_NOT_POSSIBLE &&
-                ipp_status != IPP_PRINTER_BUSY)
+       else if (ipp_status != IPP_STATUS_ERROR_SERVICE_UNAVAILABLE &&
+                ipp_status != IPP_STATUS_ERROR_NOT_POSSIBLE &&
+                ipp_status != IPP_STATUS_ERROR_BUSY)
        {
         /*
          * If the printer does not return a job-state attribute, it does not
@@ -2036,7 +2156,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
          update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
                               "cups-ipp-missing-job-state");
-         ipp_status = IPP_INTERNAL_ERROR;
+         ipp_status = IPP_STATUS_ERROR_INTERNAL;
          break;
        }
       }
@@ -2061,7 +2181,7 @@ main(int  argc,                           /* I - Number of command-line args */
   {
     cancel_job(http, uri, job_id, resource, argv[2], version);
 
-    if (cupsLastError() > IPP_OK_CONFLICT)
+    if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
       _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
   }
 
@@ -2071,7 +2191,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
   check_printer_state(http, uri, resource, argv[2], version);
 
-  if (cupsLastError() <= IPP_OK_CONFLICT)
+  if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
     password_tries = 0;
 
  /*
@@ -2118,9 +2238,9 @@ main(int  argc,                           /* I - Number of command-line args */
   * Return the queue status...
   */
 
-  if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
-      ipp_status == IPP_AUTHENTICATION_CANCELED ||
-      ipp_status <= IPP_OK_CONFLICT)
+  if (ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED || ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
+      ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED ||
+      ipp_status <= IPP_STATUS_OK_CONFLICTING)
     fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
 
   if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED)
@@ -2132,25 +2252,27 @@ main(int  argc,                         /* I - Number of command-line args */
   else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
     fputs("JOBSTATE: account-authorization-failed\n", stderr);
 
-  if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
-      ipp_status == IPP_AUTHENTICATION_CANCELED)
+  // job_canceled can be -1 which should not be treated as CUPS_BACKEND_OK
+  if (job_canceled > 0)
+    return (CUPS_BACKEND_OK);
+  else if (ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED || ipp_status == IPP_STATUS_ERROR_FORBIDDEN || ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
     return (CUPS_BACKEND_AUTH_REQUIRED);
   else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
           ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
           ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
           ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
     return (CUPS_BACKEND_HOLD);
-  else if (ipp_status == IPP_INTERNAL_ERROR)
+  else if (ipp_status == IPP_STATUS_ERROR_INTERNAL)
     return (CUPS_BACKEND_STOP);
-  else if (ipp_status == IPP_CONFLICT)
+  else if (ipp_status == IPP_STATUS_ERROR_CONFLICTING || ipp_status == IPP_STATUS_ERROR_REQUEST_ENTITY || ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE)
     return (CUPS_BACKEND_FAILED);
-  else if (ipp_status == IPP_REQUEST_VALUE ||
+  else if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE ||
           ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
-           ipp_status == IPP_DOCUMENT_FORMAT || job_canceled < 0)
+           ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED || job_canceled < 0)
   {
-    if (ipp_status == IPP_REQUEST_VALUE)
+    if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE)
       _cupsLangPrintFilter(stderr, "ERROR", _("Print job too large."));
-    else if (ipp_status == IPP_DOCUMENT_FORMAT)
+    else if (ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED)
       _cupsLangPrintFilter(stderr, "ERROR",
                            _("Printer cannot print supplied content."));
     else if (ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES)
@@ -2161,7 +2283,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
     return (CUPS_BACKEND_CANCEL);
   }
-  else if (ipp_status > IPP_OK_CONFLICT && ipp_status != IPP_ERROR_JOB_CANCELED)
+  else if (ipp_status > IPP_STATUS_OK_CONFLICTING && ipp_status != IPP_STATUS_ERROR_JOB_CANCELED)
     return (CUPS_BACKEND_RETRY_CURRENT);
   else
     return (CUPS_BACKEND_OK);
@@ -2185,7 +2307,7 @@ cancel_job(http_t     *http,              /* I - HTTP connection */
 
   _cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
 
-  request = ippNewRequest(IPP_CANCEL_JOB);
+  request = ippNewRequest(IPP_OP_CANCEL_JOB);
   ippSetVersion(request, version / 10, version % 10);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
@@ -2219,7 +2341,7 @@ check_printer_state(
   ipp_t                *request,               /* IPP request */
                *response;              /* IPP response */
   ipp_attribute_t *attr;               /* Attribute in response */
-  ipp_pstate_t printer_state = IPP_PRINTER_STOPPED;
+  ipp_pstate_t printer_state = IPP_PSTATE_STOPPED;
                                        /* Current printer-state */
 
 
@@ -2227,7 +2349,7 @@ check_printer_state(
   * Send a Get-Printer-Attributes request and log the results...
   */
 
-  request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+  request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
   ippSetVersion(request, version / 10, version % 10);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
@@ -2241,6 +2363,9 @@ check_printer_state(
                "requested-attributes",
                (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
 
+  fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
+  debug_attributes(request);
+
   if ((response = cupsDoRequest(http, request, resource)) != NULL)
   {
     report_printer_state(response);
@@ -2248,12 +2373,12 @@ check_printer_state(
     if ((attr = ippFindAttribute(response, "printer-state",
                                 IPP_TAG_ENUM)) != NULL)
       printer_state = (ipp_pstate_t)attr->values[0].integer;
-
-    ippDelete(response);
   }
 
   fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
          ippErrorString(cupsLastError()), cupsLastErrorString());
+  debug_attributes(response);
+  ippDelete(response);
 
  /*
   * Return the printer-state value...
@@ -2263,6 +2388,51 @@ check_printer_state(
 }
 
 
+/*
+ * 'debug_attributes()' - Print out the request or response attributes as DEBUG
+ * messages...
+ */
+
+static void
+debug_attributes(ipp_t *ipp)           /* I - Request or response message */
+{
+  ipp_tag_t    group;                  /* Current group */
+  ipp_attribute_t *attr;               /* Current attribute */
+  char         buffer[1024];           /* Value buffer */
+
+
+  for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(ipp);
+       attr;
+       attr = ippNextAttribute(ipp))
+  {
+    const char *name = ippGetName(attr);
+
+    if (!name)
+    {
+      group = IPP_TAG_ZERO;
+      continue;
+    }
+
+    if (group != ippGetGroupTag(attr))
+    {
+      group = ippGetGroupTag(attr);
+      fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(group));
+    }
+
+    if (!strcmp(name, "job-password"))
+      strlcpy(buffer, "---", sizeof(buffer));
+    else
+      ippAttributeString(attr, buffer, sizeof(buffer));
+
+    fprintf(stderr, "DEBUG: %s %s%s %s\n", name,
+            ippGetCount(attr) > 1 ? "1setOf " : "",
+            ippTagString(ippGetValueTag(attr)), buffer);
+  }
+
+  fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(IPP_TAG_END));
+}
+
+
 /*
  * 'monitor_printer()' - Monitor the printer state.
  */
@@ -2305,13 +2475,16 @@ monitor_printer(
 
   monitor->job_reasons = 0;
 
-  while (monitor->job_state < IPP_JOB_CANCELED && !job_canceled)
+  while (monitor->job_state < IPP_JSTATE_CANCELED && !job_canceled)
   {
    /*
-    * Reconnect to the printer...
+    * Reconnect to the printer as needed...
     */
 
-    if (!httpReconnect(http))
+    if (httpGetFd(http) < 0)
+      httpReconnect2(http, 30000, NULL);
+
+    if (httpGetFd(http) >= 0)
     {
      /*
       * Connected, so check on the printer state...
@@ -2321,21 +2494,30 @@ monitor_printer(
                                                    monitor->resource,
                                                   monitor->user,
                                                   monitor->version);
-      if (cupsLastError() <= IPP_OK_CONFLICT)
+      if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
         password_tries = 0;
 
+      if (monitor->job_id == 0 && monitor->create_job)
+      {
+       /*
+        * No job-id yet, so continue...
+       */
+
+        goto monitor_sleep;
+      }
+
      /*
       * Check the status of the job itself...
       */
 
       job_op  = (monitor->job_id > 0 && monitor->get_job_attrs) ?
-                    IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS;
+                    IPP_OP_GET_JOB_ATTRIBUTES : IPP_OP_GET_JOBS;
       request = ippNewRequest(job_op);
       ippSetVersion(request, monitor->version / 10, monitor->version % 10);
 
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                   NULL, monitor->uri);
-      if (job_op == IPP_GET_JOB_ATTRIBUTES)
+      if (job_op == IPP_OP_GET_JOB_ATTRIBUTES)
        ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
                      monitor->job_id);
 
@@ -2356,16 +2538,16 @@ monitor_printer(
       fprintf(stderr, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op),
              ippErrorString(cupsLastError()), cupsLastErrorString());
 
-      if (cupsLastError() <= IPP_OK_CONFLICT)
+      if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
         password_tries = 0;
 
-      if (job_op == IPP_GET_JOB_ATTRIBUTES)
+      if (job_op == IPP_OP_GET_JOB_ATTRIBUTES)
       {
        if ((attr = ippFindAttribute(response, "job-state",
                                     IPP_TAG_ENUM)) != NULL)
          monitor->job_state = (ipp_jstate_t)attr->values[0].integer;
        else
-         monitor->job_state = IPP_JOB_COMPLETED;
+         monitor->job_state = IPP_JSTATE_COMPLETED;
       }
       else if (response)
       {
@@ -2373,7 +2555,7 @@ monitor_printer(
         {
           job_id    = 0;
           job_name  = NULL;
-          job_state = IPP_JOB_PENDING;
+          job_state = IPP_JSTATE_PENDING;
           job_user  = NULL;
 
           while (attr && attr->group_tag != IPP_TAG_JOB)
@@ -2415,6 +2597,16 @@ monitor_printer(
         }
       }
 
+      fprintf(stderr, "DEBUG: (monitor) job-state = %s\n", ippEnumString("job-state", (int)monitor->job_state));
+
+      if (!job_canceled &&
+          (monitor->job_state == IPP_JSTATE_CANCELED ||
+          monitor->job_state == IPP_JSTATE_ABORTED))
+      {
+       job_canceled = -1;
+       fprintf(stderr, "DEBUG: (monitor) job_canceled = -1\n");
+      }
+
       if ((attr = ippFindAttribute(response, "job-state-reasons",
                                    IPP_TAG_KEYWORD)) != NULL)
       {
@@ -2422,20 +2614,25 @@ monitor_printer(
 
         for (i = 0; i < attr->num_values; i ++)
         {
-          if (!strcmp(attr->values[i].string.text,
-                      "account-authorization-failed"))
+          if (!strcmp(attr->values[i].string.text, "account-authorization-failed"))
             new_reasons |= _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED;
           else if (!strcmp(attr->values[i].string.text, "account-closed"))
             new_reasons |= _CUPS_JSR_ACCOUNT_CLOSED;
           else if (!strcmp(attr->values[i].string.text, "account-info-needed"))
             new_reasons |= _CUPS_JSR_ACCOUNT_INFO_NEEDED;
-          else if (!strcmp(attr->values[i].string.text,
-                           "account-limit-reached"))
+          else if (!strcmp(attr->values[i].string.text, "account-limit-reached"))
             new_reasons |= _CUPS_JSR_ACCOUNT_LIMIT_REACHED;
           else if (!strcmp(attr->values[i].string.text, "job-password-wait"))
             new_reasons |= _CUPS_JSR_JOB_PASSWORD_WAIT;
           else if (!strcmp(attr->values[i].string.text, "job-release-wait"))
             new_reasons |= _CUPS_JSR_JOB_RELEASE_WAIT;
+          else if (!strcmp(attr->values[i].string.text, "document-format-error"))
+            new_reasons |= _CUPS_JSR_DOCUMENT_FORMAT_ERROR;
+          else if (!strcmp(attr->values[i].string.text, "document-unprintable"))
+            new_reasons |= _CUPS_JSR_DOCUMENT_UNPRINTABLE;
+
+         if (!job_canceled && (!strncmp(attr->values[i].string.text, "job-canceled-", 13) || !strcmp(attr->values[i].string.text, "aborted-by-system")))
+            job_canceled = 1;
         }
 
         if (new_reasons != monitor->job_reasons)
@@ -2452,6 +2649,26 @@ monitor_printer(
            fputs("JOBSTATE: job-password-wait\n", stderr);
          else if (new_reasons & _CUPS_JSR_JOB_RELEASE_WAIT)
            fputs("JOBSTATE: job-release-wait\n", stderr);
+          else if (new_reasons & (_CUPS_JSR_DOCUMENT_FORMAT_ERROR | _CUPS_JSR_DOCUMENT_UNPRINTABLE))
+          {
+            if (monitor->retryable)
+            {
+             /*
+              * Can't print this, so retry as raster...
+              */
+
+              job_canceled = 1;
+              fputs("JOBSTATE: cups-retry-as-raster\n", stderr);
+           }
+           else if (new_reasons & _CUPS_JSR_DOCUMENT_FORMAT_ERROR)
+           {
+             fputs("JOBSTATE: document-format-error\n", stderr);
+           }
+           else
+           {
+             fputs("JOBSTATE: document-unprintable\n", stderr);
+           }
+          }
          else
            fputs("JOBSTATE: job-printing\n", stderr);
 
@@ -2461,25 +2678,20 @@ monitor_printer(
 
       ippDelete(response);
 
-      fprintf(stderr, "DEBUG: (monitor) job-state=%s\n",
-              ippEnumString("job-state", monitor->job_state));
+      fprintf(stderr, "DEBUG: (monitor) job-state = %s\n", ippEnumString("job-state", (int)monitor->job_state));
 
       if (!job_canceled &&
-          (monitor->job_state == IPP_JOB_CANCELED ||
-          monitor->job_state == IPP_JOB_ABORTED))
+          (monitor->job_state == IPP_JSTATE_CANCELED ||
+          monitor->job_state == IPP_JSTATE_ABORTED))
        job_canceled = -1;
-
-     /*
-      * Disconnect from the printer - we'll reconnect on the next poll...
-      */
-
-      _httpDisconnect(http);
     }
 
    /*
     * Sleep for N seconds...
     */
 
+    monitor_sleep:
+
     sleep((unsigned)delay);
 
     delay = _cupsNextDelay(delay, &prev_delay);
@@ -2491,13 +2703,19 @@ monitor_printer(
 
   if (job_canceled > 0 && monitor->job_id > 0)
   {
-    if (!httpReconnect(http))
+    if (httpGetFd(http) < 0)
+      httpReconnect2(http, 30000, NULL);
+
+    if (httpGetFd(http) >= 0)
     {
       cancel_job(http, monitor->uri, monitor->job_id, monitor->resource,
                  monitor->user, monitor->version);
 
-      if (cupsLastError() > IPP_OK_CONFLICT)
+      if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
+      {
+       fprintf(stderr, "DEBUG: (monitor) cancel_job() = %s\n", cupsLastErrorString());
        _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
+      }
     }
   }
 
@@ -2534,20 +2752,8 @@ new_request(
     ipp_attribute_t *print_color_mode_sup)
                                        /* I - Printer supports print-color-mode */
 {
-  int          i;                      /* Looping var */
   ipp_t                *request;               /* Request data */
   const char   *keyword;               /* PWG keyword */
-  _pwg_size_t  *size;                  /* PWG media size */
-  ipp_t                *media_col,             /* media-col value */
-               *media_size;            /* media-size value */
-  const char   *media_source,          /* media-source value */
-               *media_type,            /* media-type value */
-               *collate_str,           /* multiple-document-handling value */
-               *mandatory;             /* Mandatory attributes */
-  ipp_tag_t    group;                  /* Current group */
-  ipp_attribute_t *attr;               /* Current attribute */
-  const char   *color_attr_name;       /* Supported color attribute */
-  char         buffer[1024];           /* Value buffer */
 
 
  /*
@@ -2566,36 +2772,31 @@ new_request(
   * Add standard attributes...
   */
 
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-              NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
   fprintf(stderr, "DEBUG: printer-uri=\"%s\"\n", uri);
 
   if (user && *user)
   {
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
-                "requesting-user-name", NULL, user);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, user);
     fprintf(stderr, "DEBUG: requesting-user-name=\"%s\"\n", user);
   }
 
   if (title && *title)
   {
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
-                title);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, title);
     fprintf(stderr, "DEBUG: job-name=\"%s\"\n", title);
   }
 
-  if (format && op != IPP_CREATE_JOB)
+  if (format && op != IPP_OP_CREATE_JOB)
   {
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
-                "document-format", NULL, format);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL, format);
     fprintf(stderr, "DEBUG: document-format=\"%s\"\n", format);
   }
 
 #ifdef HAVE_LIBZ
   if (compression && op != IPP_OP_CREATE_JOB && op != IPP_OP_VALIDATE_JOB)
   {
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
-                "compression", NULL, compression);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "compression", NULL, compression);
     fprintf(stderr, "DEBUG: compression=\"%s\"\n", compression);
   }
 #endif /* HAVE_LIBZ */
@@ -2608,313 +2809,13 @@ new_request(
   {
     if (pc)
     {
-      int      num_finishings = 0,     /* Number of finishing values */
-               finishings[10];         /* Finishing enum values */
-      ppd_choice_t *choice;            /* Marked choice */
-
      /*
       * Send standard IPP attributes...
       */
 
       fputs("DEBUG: Adding standard IPP operation/job attributes.\n", stderr);
 
-      if (pc->password &&
-          (keyword = cupsGetOption("job-password", num_options,
-                                   options)) != NULL)
-      {
-        ippAddOctetString(request, IPP_TAG_OPERATION, "job-password", keyword, (int)strlen(keyword));
-
-        if ((keyword = cupsGetOption("job-password-encryption", num_options,
-                                    options)) == NULL)
-         keyword = "none";
-
-        ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
-                     "job-password-encryption", NULL, keyword);
-      }
-
-      if (pc->account_id)
-      {
-        if ((keyword = cupsGetOption("job-account-id", num_options,
-                                    options)) == NULL)
-         keyword = cupsGetOption("job-billing", num_options, options);
-
-        if (keyword)
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_NAME, "job-account-id",
-                      NULL, keyword);
-      }
-
-      if (pc->accounting_user_id)
-      {
-        if ((keyword = cupsGetOption("job-accounting-user-id", num_options,
-                                    options)) == NULL)
-         keyword = user;
-
-        if (keyword)
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_NAME,
-                      "job-accounting-user-id", NULL, keyword);
-      }
-
-      for (mandatory = (char *)cupsArrayFirst(pc->mandatory);
-           mandatory;
-           mandatory = (char *)cupsArrayNext(pc->mandatory))
-      {
-        if (strcmp(mandatory, "copies") &&
-            strcmp(mandatory, "destination-uris") &&
-            strcmp(mandatory, "finishings") &&
-            strcmp(mandatory, "job-account-id") &&
-            strcmp(mandatory, "job-accounting-user-id") &&
-            strcmp(mandatory, "job-password") &&
-            strcmp(mandatory, "job-password-encryption") &&
-            strcmp(mandatory, "media") &&
-            strncmp(mandatory, "media-col", 9) &&
-            strcmp(mandatory, "multiple-document-handling") &&
-            strcmp(mandatory, "output-bin") &&
-            strcmp(mandatory, "print-color-mode") &&
-            strcmp(mandatory, "print-quality") &&
-            strcmp(mandatory, "sides") &&
-            (keyword = cupsGetOption(mandatory, num_options, options)) != NULL)
-       {
-         _ipp_option_t *opt = _ippFindOption(mandatory);
-                                       /* Option type */
-          ipp_tag_t    value_tag = opt ? opt->value_tag : IPP_TAG_NAME;
-                                       /* Value type */
-
-          switch (value_tag)
-          {
-            case IPP_TAG_INTEGER :
-            case IPP_TAG_ENUM :
-                ippAddInteger(request, IPP_TAG_JOB, value_tag, mandatory,
-                              atoi(keyword));
-                break;
-            case IPP_TAG_BOOLEAN :
-                ippAddBoolean(request, IPP_TAG_JOB, mandatory,
-                              !_cups_strcasecmp(keyword, "true"));
-                break;
-            case IPP_TAG_RANGE :
-                {
-                  int lower, upper;    /* Range */
-
-                 if (sscanf(keyword, "%d-%d", &lower, &upper) != 2)
-                   lower = upper = atoi(keyword);
-
-                 ippAddRange(request, IPP_TAG_JOB, mandatory, lower, upper);
-                }
-                break;
-            case IPP_TAG_STRING :
-                ippAddOctetString(request, IPP_TAG_JOB, mandatory, keyword, (int)strlen(keyword));
-                break;
-            default :
-                if (!strcmp(mandatory, "print-color-mode") && !strcmp(keyword, "monochrome"))
-                {
-                  if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
-                    keyword = "auto-monochrome";
-                  else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
-                    keyword = "process-monochrome";
-                }
-
-                ippAddString(request, IPP_TAG_JOB, value_tag, mandatory,
-                             NULL, keyword);
-                break;
-         }
-       }
-      }
-
-      if ((keyword = cupsGetOption("PageSize", num_options, options)) == NULL)
-       keyword = cupsGetOption("media", num_options, options);
-
-      if ((size = _ppdCacheGetSize(pc, keyword)) != NULL)
-      {
-       /*
-        * Add a media-col value...
-       */
-
-       media_size = ippNew();
-       ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
-                     "x-dimension", size->width);
-       ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
-                     "y-dimension", size->length);
-
-       media_col = ippNew();
-       ippAddCollection(media_col, IPP_TAG_ZERO, "media-size", media_size);
-
-       media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot",
-                                                           num_options,
-                                                           options));
-       media_type   = _ppdCacheGetType(pc, cupsGetOption("MediaType",
-                                                         num_options,
-                                                         options));
-
-       for (i = 0; i < media_col_sup->num_values; i ++)
-       {
-         if (!strcmp(media_col_sup->values[i].string.text,
-                     "media-left-margin"))
-           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
-                         "media-left-margin", size->left);
-         else if (!strcmp(media_col_sup->values[i].string.text,
-                          "media-bottom-margin"))
-           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
-                         "media-bottom-margin", size->bottom);
-         else if (!strcmp(media_col_sup->values[i].string.text,
-                          "media-right-margin"))
-           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
-                         "media-right-margin", size->right);
-         else if (!strcmp(media_col_sup->values[i].string.text,
-                          "media-top-margin"))
-           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
-                         "media-top-margin", size->top);
-         else if (!strcmp(media_col_sup->values[i].string.text,
-                          "media-source") && media_source)
-           ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
-                        "media-source", NULL, media_source);
-         else if (!strcmp(media_col_sup->values[i].string.text,
-                          "media-type") && media_type)
-           ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
-                        "media-type", NULL, media_type);
-       }
-
-       ippAddCollection(request, IPP_TAG_JOB, "media-col", media_col);
-      }
-
-      if ((keyword = cupsGetOption("output-bin", num_options,
-                                  options)) == NULL)
-      {
-        if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL)
-         keyword = _ppdCacheGetBin(pc, choice->choice);
-      }
-
-      if (keyword)
-       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin",
-                    NULL, keyword);
-
-      color_attr_name = print_color_mode_sup ? "print-color-mode" : "output-mode";
-
-      if ((keyword = cupsGetOption("print-color-mode", num_options,
-                                  options)) == NULL)
-      {
-       if ((choice = ppdFindMarkedChoice(ppd, "ColorModel")) != NULL)
-       {
-         if (!_cups_strcasecmp(choice->choice, "Gray"))
-           keyword = "monochrome";
-         else
-           keyword = "color";
-       }
-      }
-
-      if (keyword && !strcmp(keyword, "monochrome"))
-      {
-       if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
-         keyword = "auto-monochrome";
-       else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
-         keyword = "process-monochrome";
-      }
-
-      if (keyword)
-       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, color_attr_name,
-                    NULL, keyword);
-
-      if ((keyword = cupsGetOption("print-quality", num_options,
-                                  options)) != NULL)
-       ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
-                     atoi(keyword));
-      else if ((choice = ppdFindMarkedChoice(ppd, "cupsPrintQuality")) != NULL)
-      {
-       if (!_cups_strcasecmp(choice->choice, "draft"))
-         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
-                       IPP_QUALITY_DRAFT);
-       else if (!_cups_strcasecmp(choice->choice, "normal"))
-         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
-                       IPP_QUALITY_NORMAL);
-       else if (!_cups_strcasecmp(choice->choice, "high"))
-         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
-                       IPP_QUALITY_HIGH);
-      }
-
-      if ((keyword = cupsGetOption("sides", num_options, options)) != NULL)
-       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
-                    NULL, keyword);
-      else if (pc->sides_option &&
-               (choice = ppdFindMarkedChoice(ppd, pc->sides_option)) != NULL)
-      {
-       if (!_cups_strcasecmp(choice->choice, pc->sides_1sided))
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
-                      NULL, "one-sided");
-       else if (!_cups_strcasecmp(choice->choice, pc->sides_2sided_long))
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
-                      NULL, "two-sided-long-edge");
-       if (!_cups_strcasecmp(choice->choice, pc->sides_2sided_short))
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
-                      NULL, "two-sided-short-edge");
-      }
-
-      if ((keyword = cupsGetOption("multiple-document-handling",
-                                  num_options, options)) != NULL)
-      {
-        if (strstr(keyword, "uncollated"))
-          keyword = "false";
-        else
-          keyword = "true";
-      }
-      else if ((keyword = cupsGetOption("collate", num_options,
-                                        options)) == NULL)
-        keyword = "true";
-
-      if (format)
-      {
-        if (!_cups_strcasecmp(format, "image/gif") ||
-           !_cups_strcasecmp(format, "image/jp2") ||
-           !_cups_strcasecmp(format, "image/jpeg") ||
-           !_cups_strcasecmp(format, "image/png") ||
-           !_cups_strcasecmp(format, "image/tiff") ||
-           !_cups_strncasecmp(format, "image/x-", 8))
-       {
-        /*
-         * Collation makes no sense for single page image formats...
-         */
-
-         keyword = "false";
-       }
-       else if (!_cups_strncasecmp(format, "image/", 6) ||
-                !_cups_strcasecmp(format, "application/vnd.cups-raster"))
-       {
-        /*
-         * Multi-page image formats will have copies applied by the upstream
-         * filters...
-         */
-
-         copies = 1;
-       }
-      }
-
-      if (doc_handling_sup)
-      {
-        if (!_cups_strcasecmp(keyword, "true"))
-         collate_str = "separate-documents-collated-copies";
-       else
-         collate_str = "separate-documents-uncollated-copies";
-
-        for (i = 0; i < doc_handling_sup->num_values; i ++)
-         if (!strcmp(doc_handling_sup->values[i].string.text, collate_str))
-         {
-           ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                        "multiple-document-handling", NULL, collate_str);
-           break;
-          }
-
-        if (i >= doc_handling_sup->num_values)
-          copies = 1;
-      }
-
-     /*
-      * Map finishing options...
-      */
-
-      num_finishings = _ppdCacheGetFinishingValues(pc, num_options, options,
-                                                   (int)(sizeof(finishings) /
-                                                         sizeof(finishings[0])),
-                                                   finishings);
-      if (num_finishings > 0)
-       ippAddIntegers(request, IPP_TAG_JOB, IPP_TAG_ENUM, "finishings",
-                      num_finishings, finishings);
+      copies = _cupsConvertOptions(request, ppd, pc, media_col_sup, doc_handling_sup, print_color_mode_sup, user, format, copies, num_options, options);
 
      /*
       * Map FaxOut options...
@@ -2926,7 +2827,7 @@ new_request(
        char    phone[1024],            /* Phone number string */
                *ptr,                   /* Pointer into string */
                tel_uri[1024];          /* tel: URI */
-        static const char * const allowed = "0123456789#*-+.()";
+        static const char * const allowed = "0123456789#*-+.()pw";
                                        /* Allowed characters */
 
         destination = ippNew();
@@ -2937,28 +2838,60 @@ new_request(
         */
 
         _httpDecodeURI(phone, keyword, sizeof(phone));
-        for (ptr = phone; *ptr;)
+        ptr = phone;
+
+        /*
+         * Weed out "Custom." in the beginning, this allows to put the
+         * "phone" option as custom string option into the PPD so that
+         * print dialogs not supporting fax display the option and
+         * allow entering the phone number. Print dialogs also send "None"
+         * if no phone number got entered, filter this, too.
+         */
+        if (!_cups_strcasecmp(phone, "None"))
+          *ptr = '\0';
+        if (!_cups_strncasecmp(phone, "Custom.", 7))
+          _cups_strcpy(ptr, ptr + 7);
+
+        for (; *ptr;)
        {
-         if (!strchr(allowed, *ptr))
+         if (*ptr == ',')
+           *ptr = 'p';
+         else if (!strchr(allowed, *ptr))
            _cups_strcpy(ptr, ptr + 1);
          else
            ptr ++;
         }
 
-        httpAssembleURI(HTTP_URI_CODING_ALL, tel_uri, sizeof(tel_uri), "tel", NULL, NULL, 0, phone);
-        ippAddString(destination, IPP_TAG_JOB, IPP_TAG_URI, "destination-uri", NULL, tel_uri);
-
-       if ((keyword = cupsGetOption("faxPrefix", num_options,
-                                    options)) != NULL && *keyword)
+        if (strlen(phone) > 0)
         {
-         char  predial[1024];          /* Pre-dial string */
+          httpAssembleURI(HTTP_URI_CODING_ALL, tel_uri, sizeof(tel_uri), "tel", NULL, NULL, 0, phone);
+          ippAddString(destination, IPP_TAG_JOB, IPP_TAG_URI, "destination-uri", NULL, tel_uri);
+          fprintf(stderr, "DEBUG: Faxing to phone %s; destination-uri: %s\n", phone, tel_uri);
 
-         _httpDecodeURI(predial, keyword, sizeof(predial));
-         ippAddString(destination, IPP_TAG_JOB, IPP_TAG_TEXT, "pre-dial-string", NULL, predial);
-       }
+          if ((keyword = cupsGetOption("faxPrefix", num_options, options)) != NULL && *keyword)
+          {
+            char       predial[1024];          /* Pre-dial string */
+
+            _httpDecodeURI(predial, keyword, sizeof(predial));
+            ptr = predial;
+            if (!_cups_strcasecmp(ptr, "None"))
+              *ptr = '\0';
+            if (!_cups_strncasecmp(ptr, "Custom.", 7))
+              ptr += 7;
+            if (strlen(ptr) > 0)
+            {
+              ippAddString(destination, IPP_TAG_JOB, IPP_TAG_TEXT, "pre-dial-string", NULL, ptr);
+              fprintf(stderr, "DEBUG: Pre-dialing %s; pre-dial-string: %s\n", ptr, ptr);
+            }
+            else
+              fprintf(stderr, "WARNING: Pre-dial number for fax not valid! Sending fax without pre-dial number.\n");
+          }
 
-        ippAddCollection(request, IPP_TAG_JOB, "destination-uris", destination);
-        ippDelete(destination);
+          ippAddCollection(request, IPP_TAG_JOB, "destination-uris", destination);
+          ippDelete(destination);
+        }
+        else
+          fprintf(stderr, "ERROR: Phone number for fax not valid! Fax cannot be sent.\n");
       }
     }
     else
@@ -2976,33 +2909,8 @@ new_request(
       ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", copies);
   }
 
-  fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10,
-          ippOpString(ippGetOperation(request)), ippGetRequestId(request));
-  for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(request);
-       attr;
-       attr = ippNextAttribute(request))
-  {
-    const char *name = ippGetName(attr);
-
-    if (!name)
-    {
-      group = IPP_TAG_ZERO;
-      continue;
-    }
-
-    if (group != ippGetGroupTag(attr))
-    {
-      group = ippGetGroupTag(attr);
-      fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(group));
-    }
-
-    ippAttributeString(attr, buffer, sizeof(buffer));
-    fprintf(stderr, "DEBUG: %s %s%s %s\n", name,
-            ippGetCount(attr) > 1 ? "1setOf " : "",
-            ippTagString(ippGetValueTag(attr)), buffer);
-  }
-
-  fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(IPP_TAG_END));
+  fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
+  debug_attributes(request);
 
   return (request);
 }
@@ -3196,6 +3104,7 @@ report_printer_state(ipp_t *ipp)  /* I - IPP response */
 {
   ipp_attribute_t      *pa,            /* printer-alert */
                        *pam,           /* printer-alert-message */
+                       *pmja,          /* printer-mandatory-job-attributes */
                        *psm,           /* printer-state-message */
                        *reasons,       /* printer-state-reasons */
                        *marker;        /* marker-* attributes */
@@ -3209,13 +3118,33 @@ report_printer_state(ipp_t *ipp)        /* I - IPP response */
   * Report alerts and messages...
   */
 
-  if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_TEXT)) != NULL)
+  if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_STRING)) != NULL)
     report_attr(pa);
 
   if ((pam = ippFindAttribute(ipp, "printer-alert-message",
                               IPP_TAG_TEXT)) != NULL)
     report_attr(pam);
 
+  if ((pmja = ippFindAttribute(ipp, "printer-mandatory-job-attributes", IPP_TAG_KEYWORD)) != NULL)
+  {
+    int        i,                              /* Looping var */
+       count = ippGetCount(pmja);      /* Number of values */
+
+    for (i = 0, valptr = value; i < count; i ++, valptr += strlen(valptr))
+    {
+      if (i)
+        snprintf(valptr, sizeof(value) - (size_t)(valptr - value), " %s", ippGetString(pmja, i, NULL));
+      else
+        strlcpy(value, ippGetString(pmja, i, NULL), sizeof(value));
+    }
+
+    if (strcmp(value, mandatory_attrs))
+    {
+      strlcpy(mandatory_attrs, value, sizeof(mandatory_attrs));
+      fprintf(stderr, "PPD: cupsMandatory=\"%s\"\n", value);
+    }
+  }
+
   if ((psm = ippFindAttribute(ipp, "printer-state-message",
                               IPP_TAG_TEXT)) != NULL)
   {
@@ -3230,11 +3159,10 @@ report_printer_state(ipp_t *ipp)        /* I - IPP response */
       if (*ptr < ' ' && *ptr > 0 && *ptr != '\t')
       {
        /*
-        * Substitute "<XX>" for the control character; sprintf is safe because
-       * we always leave 6 chars free at the end...
+        * Substitute "<XX>" for the control character...
        */
 
-        sprintf(valptr, "<%02X>", *ptr);
+        snprintf(valptr, sizeof(value) - (size_t)(valptr - value), "<%02X>", *ptr);
        valptr += 4;
       }
       else
@@ -3317,7 +3245,8 @@ run_as_user(char       *argv[],           /* I - Command-line arguments */
            const char *device_uri,     /* I - Device URI */
            int        fd)              /* I - File to print */
 {
-  const char           *auth_negotiate;/* AUTH_NEGOTIATE env var */
+  const char           *auth_negotiate,/* AUTH_NEGOTIATE env var */
+                       *content_type;  /* [FINAL_]CONTENT_TYPE env vars */
   xpc_connection_t     conn;           /* Connection to XPC service */
   xpc_object_t         request;        /* Request message dictionary */
   __block xpc_object_t response;       /* Response message dictionary */
@@ -3380,6 +3309,10 @@ run_as_user(char       *argv[],          /* I - Command-line arguments */
                             getenv("AUTH_INFO_REQUIRED"));
   if ((auth_negotiate = getenv("AUTH_NEGOTIATE")) != NULL)
     xpc_dictionary_set_string(request, "auth-negotiate", auth_negotiate);
+  if ((content_type = getenv("CONTENT_TYPE")) != NULL)
+    xpc_dictionary_set_string(request, "content-type", content_type);
+  if ((content_type = getenv("FINAL_CONTENT_TYPE")) != NULL)
+    xpc_dictionary_set_string(request, "final-content-type", content_type);
   xpc_dictionary_set_fd(request, "stdin", fd);
   xpc_dictionary_set_fd(request, "stderr", 2);
   xpc_dictionary_set_fd(request, "side-channel", CUPS_SC_FD);
@@ -3502,7 +3435,7 @@ sigterm_handler(int sig)          /* I - Signal */
 {
   (void)sig;   /* remove compiler warnings... */
 
-  write(2, "DEBUG: Got SIGTERM.\n", 20);
+  backendMessage("DEBUG: Got SIGTERM.\n");
 
 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
   if (child_pid)
@@ -3518,7 +3451,7 @@ sigterm_handler(int sig)          /* I - Signal */
     * Flag that the job should be canceled...
     */
 
-    write(2, "DEBUG: job_canceled = 1.\n", 25);
+    backendMessage("DEBUG: sigterm_handler: job_canceled = 1.\n");
 
     job_canceled = 1;
     return;
@@ -3532,7 +3465,7 @@ sigterm_handler(int sig)          /* I - Signal */
   if (tmpfilename[0])
     unlink(tmpfilename);
 
-  exit(1);
+  _exit(1);
 }
 
 
@@ -3729,6 +3662,8 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
     }
   }
 
+  cupsArrayDelete(new_reasons);
+
   _cupsMutexUnlock(&report_mutex);
 
  /*
@@ -3742,7 +3677,3 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
   else if (rem[0])
     fprintf(stderr, "%s\n", rem);
 }
-
-/*
- * End of "$Id$".
- */