]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ipp.c
The IPP backend could get into an infinite loop for certain errors, causing a
[thirdparty/cups.git] / backend / ipp.c
index 2376da4e77424eb31575951e34effba0a8f96425..685d4d9dff602b55332bfba57262b043ed8cdfc7 100644 (file)
@@ -1,9 +1,7 @@
 /*
- * "$Id$"
- *
  * IPP backend for CUPS.
  *
- * Copyright 2007-2015 by Apple Inc.
+ * Copyright 2007-2017 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  * These coded instructions, statements, and computer programs are the
@@ -20,6 +18,7 @@
  */
 
 #include "backend-private.h"
+#include <cups/ppd-private.h>
 #include <cups/array-private.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -108,6 +107,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",
@@ -160,6 +160,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,
@@ -222,7 +223,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 */
@@ -236,9 +236,10 @@ 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? */
+  time_t       waittime;               /* Wait time for held jobs */
   _cups_monitor_t monitor;             /* Monitoring data */
   ipp_attribute_t *job_id_attr;                /* job-id attribute */
   int          job_id;                 /* job-id value */
@@ -249,6 +250,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 */
@@ -661,69 +663,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 */
-    cups_array_t       *lcreds = NULL; /* Loaded 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);
-      }
-
-      if (httpLoadCredentials(NULL, &lcreds, hostname))
-      {
-       /*
-        * 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);
-    }
-  }
-
  /*
   * See if the printer supports SNMP...
   */
@@ -854,6 +799,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."));
 
@@ -880,6 +895,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;
@@ -1093,7 +1109,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";
@@ -1125,6 +1141,8 @@ main(int  argc,                           /* I - Number of command-line args */
 
     cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT);
 
+    encryption_sup = ippFindAttribute(supported, "job-password-encryption-supported", IPP_TAG_KEYWORD);
+
     if ((format_sup = ippFindAttribute(supported, "document-format-supported",
                                       IPP_TAG_MIMETYPE)) != NULL)
     {
@@ -1309,6 +1327,41 @@ main(int  argc,                          /* I - Number of command-line args */
       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
     num_options = 0;
@@ -1317,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",
@@ -1430,7 +1469,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);
@@ -1441,6 +1480,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)
@@ -1493,6 +1533,8 @@ main(int  argc,                           /* I - Number of command-line args */
     else if (ipp_status < IPP_REDIRECTION_OTHER_SITE ||
              ipp_status == IPP_BAD_REQUEST)
       break;
+    else if (job_auth == NULL && ipp_status > IPP_BAD_REQUEST)
+      goto cleanup;
   }
 
  /*
@@ -1611,6 +1653,7 @@ 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)
     {
@@ -1745,6 +1788,9 @@ 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)
        {
@@ -1794,11 +1840,13 @@ main(int  argc,                         /* I - Number of command-line args */
           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());
+        debug_attributes(response);
+        ippDelete(response);
 
        if (cupsLastError() > IPP_OK_CONFLICT && !job_canceled)
        {
@@ -1927,7 +1975,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
     _cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
 
-    for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
+    for (delay = _cupsNextDelay(0, &prev_delay), waittime = time(NULL) + 30; !job_canceled;)
     {
      /*
       * Check for side-channel requests...
@@ -1965,6 +2013,9 @@ 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...
       */
@@ -1989,6 +2040,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
       fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
              ippErrorString(ipp_status), cupsLastErrorString());
+      debug_attributes(response);
 
       if (ipp_status <= IPP_OK_CONFLICT)
        password_tries = 0;
@@ -2038,10 +2090,11 @@ main(int  argc,                         /* I - Number of command-line args */
                    job_sheets->values[0].integer);
 
         /*
-          * Stop polling if the job is finished or pending-held...
+          * Stop polling if the job is finished or pending-held for 30 seconds...
          */
 
-          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 && time(NULL) > waittime))
          {
            ippDelete(response);
            break;
@@ -2165,7 +2218,7 @@ main(int  argc,                           /* I - Number of command-line args */
     return (CUPS_BACKEND_HOLD);
   else if (ipp_status == IPP_INTERNAL_ERROR)
     return (CUPS_BACKEND_STOP);
-  else if (ipp_status == IPP_CONFLICT)
+  else if (ipp_status == IPP_CONFLICT || 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 ||
           ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
@@ -2264,6 +2317,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);
@@ -2271,12 +2327,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...
@@ -2286,6 +2342,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.
  */
@@ -2331,10 +2432,13 @@ monitor_printer(
   while (monitor->job_state < IPP_JOB_CANCELED && !job_canceled)
   {
    /*
-    * Reconnect to the printer...
+    * Reconnect to the printer as needed...
     */
 
-    if (!httpReconnect(http))
+    if (httpGetFd(http) < 0)
+      httpReconnect(http);
+
+    if (httpGetFd(http) >= 0)
     {
      /*
       * Connected, so check on the printer state...
@@ -2353,7 +2457,7 @@ monitor_printer(
         * No job-id yet, so continue...
        */
 
-        goto monitor_disconnect;
+        goto monitor_sleep;
       }
 
      /*
@@ -2514,20 +2618,14 @@ monitor_printer(
           (monitor->job_state == IPP_JOB_CANCELED ||
           monitor->job_state == IPP_JOB_ABORTED))
        job_canceled = -1;
-
-     /*
-      * Disconnect from the printer - we'll reconnect on the next poll...
-      */
-
-      monitor_disconnect:
-
-      _httpDisconnect(http);
     }
 
    /*
     * Sleep for N seconds...
     */
 
+    monitor_sleep:
+
     sleep((unsigned)delay);
 
     delay = _cupsNextDelay(delay, &prev_delay);
@@ -2539,7 +2637,10 @@ monitor_printer(
 
   if (job_canceled > 0 && monitor->job_id > 0)
   {
-    if (!httpReconnect(http))
+    if (httpGetFd(http) < 0)
+      httpReconnect(http);
+
+    if (httpGetFd(http) >= 0)
     {
       cancel_job(http, monitor->uri, monitor->job_id, monitor->resource,
                  monitor->user, monitor->version);
@@ -2587,9 +2688,6 @@ new_request(
 {
   ipp_t                *request;               /* Request data */
   const char   *keyword;               /* PWG keyword */
-  ipp_tag_t    group;                  /* Current group */
-  ipp_attribute_t *attr;               /* Current attribute */
-  char         buffer[1024];           /* Value buffer */
 
 
  /*
@@ -2713,33 +2811,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);
 }
@@ -3290,7 +3363,7 @@ sigterm_handler(int sig)          /* I - Signal */
   if (tmpfilename[0])
     unlink(tmpfilename);
 
-  exit(1);
+  _exit(1);
 }
 
 
@@ -3500,7 +3573,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$".
- */