]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/ipp.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / scheduler / ipp.c
index cc6e70eabb8ffc312da9329af43ac3eec60e7211..caa1cc49ee259749d794bf53a2c99c17d92e7992 100644 (file)
@@ -7,11 +7,7 @@
  * This file contains Kerberos support code, copyright 2006 by
  * Jelmer Vernooij.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -124,8 +120,7 @@ static void send_ipp_status(cupsd_client_t *con, ipp_status_t status,
 static void    set_default(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    set_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    set_printer_defaults(cupsd_client_t *con,
-                                    cupsd_printer_t *printer);
+static int     set_printer_defaults(cupsd_client_t *con, cupsd_printer_t *printer);
 static void    start_printer(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    stop_printer(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    url_encode_attr(ipp_attribute_t *attr, char *buffer, size_t bufsize);
@@ -153,9 +148,30 @@ cupsdProcessIPPRequest(
   int                  sub_id;         /* Subscription ID */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
-                  con, con->number, con->request->request.op.operation_id);
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest(%p[%d]): operation_id=%04x(%s)", con, con->number, con->request->request.op.operation_id, ippOpString(con->request->request.op.operation_id));
+
+  if (LogLevel >= CUPSD_LOG_DEBUG2)
+  {
+    for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(con->request); attr; attr = ippNextAttribute(con->request))
+    {
+      const char  *name;                /* Attribute name */
+      char        value[1024];          /* Attribute value */
+
+      if (group != ippGetGroupTag(attr))
+      {
+        group = ippGetGroupTag(attr);
+        if (group != IPP_TAG_ZERO)
+          cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s", ippTagString(group));
+      }
+
+      if ((name = ippGetName(attr)) == NULL)
+        continue;
+
+      ippAttributeString(attr, value, sizeof(value));
+
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s %s%s '%s'", name, ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)), value);
+    }
+  }
 
  /*
   * First build an empty response message for this request...
@@ -932,6 +948,8 @@ add_class(cupsd_client_t  *con,             /* I - Client connection */
 
     pclass = cupsdAddClass(resource + 9);
     modify = 0;
+
+    pclass->printer_id = NextPrinterId ++;
   }
   else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con,
                                       NULL)) != HTTP_OK)
@@ -987,6 +1005,9 @@ add_class(cupsd_client_t  *con,            /* I - Client connection */
       */
 
       send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
+      if (!modify)
+       cupsdDeletePrinter(pclass, 0);
+
       return;
     }
 
@@ -1009,6 +1030,9 @@ add_class(cupsd_client_t  *con,           /* I - Client connection */
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Attempt to set %s printer-state to bad value %d."),
                       pclass->name, attr->values[0].integer);
+      if (!modify)
+       cupsdDeletePrinter(pclass, 0);
+
       return;
     }
 
@@ -1065,12 +1089,18 @@ add_class(cupsd_client_t  *con,         /* I - Client connection */
 
        send_ipp_status(con, IPP_NOT_FOUND,
                        _("The printer or class does not exist."));
+       if (!modify)
+         cupsdDeletePrinter(pclass, 0);
+
        return;
       }
       else if (dtype & CUPS_PRINTER_CLASS)
       {
         send_ipp_status(con, IPP_BAD_REQUEST,
                        _("Nested classes are not allowed."));
+       if (!modify)
+         cupsdDeletePrinter(pclass, 0);
+
         return;
       }
 
@@ -1082,7 +1112,13 @@ add_class(cupsd_client_t  *con,          /* I - Client connection */
     }
   }
 
-  set_printer_defaults(con, pclass);
+  if (!set_printer_defaults(con, pclass))
+  {
+    if (!modify)
+      cupsdDeletePrinter(pclass, 0);
+
+    return;
+  }
 
   if ((attr = ippFindAttribute(con->request, "auth-info-required",
                                IPP_TAG_KEYWORD)) != NULL)
@@ -1559,6 +1595,16 @@ add_job(cupsd_client_t  *con,            /* I - Client connection */
     return (NULL);
   }
 
+  attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
+
+  if (attr && !ippValidateAttribute(attr))
+  {
+    send_ipp_status(con, IPP_ATTRIBUTES, _("Bad requesting-user-name value: %s"), cupsLastErrorString());
+    if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
+      attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
+    return (NULL);
+  }
+
   if ((job = cupsdAddJob(priority, printer->name)) == NULL)
   {
     send_ipp_status(con, IPP_INTERNAL_ERROR,
@@ -1577,8 +1623,6 @@ add_job(cupsd_client_t  *con,             /* I - Client connection */
   add_job_uuid(job);
   apply_printer_defaults(printer, job);
 
-  attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
-
   if (con->username[0])
   {
     cupsdSetString(&job->username, con->username);
@@ -1694,7 +1738,24 @@ add_job(cupsd_client_t  *con,            /* I - Client connection */
     attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
                         "job-hold-until", NULL, val);
   }
-  if (attr && strcmp(attr->values[0].string.text, "no-hold"))
+
+  if (printer->holding_new_jobs)
+  {
+   /*
+    * Hold all new jobs on this printer...
+    */
+
+    if (attr && strcmp(attr->values[0].string.text, "no-hold"))
+      cupsdSetJobHoldUntil(job, ippGetString(attr, 0, NULL), 0);
+    else
+      cupsdSetJobHoldUntil(job, "indefinite", 0);
+
+    job->state->values[0].integer = IPP_JOB_HELD;
+    job->state_value              = IPP_JOB_HELD;
+
+    ippSetString(job->attrs, &job->reasons, 0, "job-held-on-create");
+  }
+  else if (attr && strcmp(attr->values[0].string.text, "no-hold"))
   {
    /*
     * Hold job until specified time...
@@ -2278,6 +2339,8 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
 
     printer = cupsdAddPrinter(resource + 10);
     modify  = 0;
+
+    printer->printer_id = NextPrinterId ++;
   }
   else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
                                       NULL)) != HTTP_OK)
@@ -2327,22 +2390,6 @@ add_printer(cupsd_client_t  *con,        /* I - Client connection */
     http_uri_status_t  uri_status;     /* URI separation status */
     char               old_device_uri[1024];
                                        /* Old device URI */
-    static const char * const uri_status_strings[] =
-    {
-      "URI too large.",
-      "Bad arguments to function.",
-      "Bad resource path.",
-      "Bad port number.",
-      "Bad hostname/address.",
-      "Bad username/password.",
-      "Bad URI scheme.",
-      "Bad URI.",
-      "OK",
-      "Missing URI scheme.",
-      "Unknown URI scheme",
-      "Missing resource path."
-    };
-
 
     need_restart_job = 1;
 
@@ -2353,14 +2400,15 @@ add_printer(cupsd_client_t  *con,       /* I - Client connection */
                                 host, sizeof(host), &port,
                                 resource, sizeof(resource));
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                   "%s device-uri: %s", printer->name,
-                   uri_status_strings[uri_status - HTTP_URI_STATUS_OVERFLOW]);
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "%s device-uri: %s", printer->name, httpURIStatusString(uri_status));
 
     if (uri_status < HTTP_URI_OK)
     {
       send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"."),
                      attr->values[0].string.text);
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2381,6 +2429,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
                          "To enable, see the FileDevice directive in "
                          "\"%s/cups-files.conf\"."),
                        ServerRoot);
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
     }
@@ -2399,6 +2450,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
 
        send_ipp_status(con, IPP_NOT_POSSIBLE,
                         _("Bad device-uri scheme \"%s\"."), scheme);
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
     }
@@ -2443,6 +2497,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     {
       send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"."),
                      attr->values[0].string.text);
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2482,6 +2539,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Cannot share a remote Kerberized printer."));
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2492,6 +2552,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       */
 
       send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2515,6 +2578,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     {
       send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d."),
                       attr->values[0].integer);
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2551,6 +2617,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
                      attr->num_values,
                      (int)(sizeof(printer->reasons) /
                            sizeof(printer->reasons[0])));
+      if (!modify)
+        cupsdDeletePrinter(printer, 0);
+
       return;
     }
 
@@ -2584,7 +2653,13 @@ add_printer(cupsd_client_t  *con,        /* I - Client connection */
                   "Printer \"%s\" state changed.", printer->name);
   }
 
-  set_printer_defaults(con, printer);
+  if (!set_printer_defaults(con, printer))
+  {
+    if (!modify)
+      cupsdDeletePrinter(printer, 0);
+
+    return;
+  }
 
   if ((attr = ippFindAttribute(con->request, "auth-info-required",
                                IPP_TAG_KEYWORD)) != NULL)
@@ -2625,6 +2700,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (strncmp(line, "*PPD-Adobe", 10))
       {
        send_ipp_status(con, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED, _("Bad PPD file."));
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
 
@@ -2639,6 +2717,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (copy_file(srcfile, dstfile, ConfigFilePerm))
       {
        send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file - %s"), strerror(errno));
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
 
@@ -2665,6 +2746,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
     else if (strstr(ppd_name, "../"))
     {
       send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
+      if (!modify)
+       cupsdDeletePrinter(printer, 0);
+
       return;
     }
     else
@@ -2678,6 +2762,9 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
       if (copy_model(con, ppd_name, dstfile))
       {
         send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file."));
+       if (!modify)
+         cupsdDeletePrinter(printer, 0);
+
        return;
       }
 
@@ -2694,8 +2781,7 @@ add_printer(cupsd_client_t  *con, /* I - Client connection */
 
     char cache_name[1024];             /* Cache filename for printer attrs */
 
-    snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir,
-             printer->name);
+    snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir, printer->name);
     unlink(cache_name);
 
     cupsdSetPrinterReasons(printer, "none");
@@ -2755,9 +2841,15 @@ add_printer(cupsd_client_t  *con,        /* I - Client connection */
   * Update the printer attributes and return...
   */
 
-  cupsdSetPrinterAttrs(printer);
   if (!printer->temporary)
+  {
+    if (!printer->printer_id)
+      printer->printer_id = NextPrinterId ++;
+
     cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
+  }
+
+  cupsdSetPrinterAttrs(printer);
 
   if (need_restart_job && printer->job)
   {
@@ -2856,6 +2948,8 @@ apply_printer_defaults(
                *option;                /* Current option */
 
 
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "Applying default options...");
+
  /*
   * Collect all of the default options and add the missing ones to the
   * job object...
@@ -2867,8 +2961,12 @@ apply_printer_defaults(
        i --, option ++)
     if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
     {
-      num_options = cupsAddOption(option->name, option->value, num_options,
-                                  &options);
+      if (!strcmp(option->name, "print-quality") && ippFindAttribute(job->attrs, "cupsPrintQuality", IPP_TAG_NAME))
+        continue;                     /* Don't override cupsPrintQuality */
+
+      cupsdLogJob(job, CUPSD_LOG_DEBUG, "Adding default %s=%s", option->name, option->value);
+
+      num_options = cupsAddOption(option->name, option->value, num_options, &options);
     }
 
  /*
@@ -5309,7 +5407,7 @@ create_local_bg_thread(
     }
   }
   else
-    cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed.", printer->name);
+    cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed: %s", printer->name, cupsLastErrorString());
 
   return (NULL);
 }
@@ -5422,6 +5520,9 @@ create_local_printer(
     return;
   }
 
+  printer->shared    = 0;
+  printer->temporary = 1;
+
   cupsdSetDeviceURI(printer, ippGetString(device_uri, 0, NULL));
 
   if (printer_geo_location)
@@ -6399,10 +6500,9 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
   ipp_jstate_t job_state;              /* job-state value */
   int          first_job_id = 1,       /* First job ID */
                first_index = 1,        /* First index */
-               current_index = 0;      /* Current index */
-  int          limit = 0;              /* Maximum number of jobs to return */
-  int          count;                  /* Number of jobs that match */
-  int          need_load_job = 0;      /* Do we need to load the job? */
+               limit = 0,              /* Maximum number of jobs to return */
+               count,                  /* Number of jobs that match */
+               need_load_job = 0;      /* Do we need to load the job? */
   const char   *job_attr;              /* Job attribute requested */
   ipp_attribute_t *job_ids;            /* job-ids attribute */
   cupsd_job_t  *job;                   /* Current job pointer */
@@ -6710,9 +6810,12 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
   }
   else
   {
-    for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
-        (limit <= 0 || count < limit) && job;
-        job = (cupsd_job_t *)cupsArrayNext(list))
+    if (first_index > 1)
+      job = (cupsd_job_t *)cupsArrayIndex(list, first_index - 1);
+    else
+      job = (cupsd_job_t *)cupsArrayFirst(list);
+
+    for (count = 0; (limit <= 0 || count < limit) && job; job = (cupsd_job_t *)cupsArrayNext(list))
     {
      /*
       * Filter out jobs that don't match...
@@ -6744,10 +6847,6 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
       if (job->id < first_job_id)
        continue;
 
-      current_index ++;
-      if (current_index < first_index)
-        continue;
-
       if (need_load_job && !job->attrs)
       {
         cupsdLoadJob(job);
@@ -7369,6 +7468,7 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
   ipp_attribute_t *attr;               /* Current attribute */
   int          limit;                  /* Max number of printers to return */
   int          count;                  /* Number of printers that match */
+  int          printer_id;             /* Printer we are interested in */
   cupsd_printer_t *printer;            /* Current printer pointer */
   cups_ptype_t printer_type,           /* printer-type attribute */
                printer_mask;           /* printer-type-mask attribute */
@@ -7422,6 +7522,17 @@ get_printers(cupsd_client_t *con,        /* I - Client connection */
   * Support filtering...
   */
 
+  if ((attr = ippFindAttribute(con->request, "printer-id", IPP_TAG_INTEGER)) != NULL)
+  {
+    if ((printer_id = ippGetInteger(attr, 0)) <= 0)
+    {
+      send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Bad \"printer-id\" value %d."), printer_id);
+      return;
+    }
+  }
+  else
+    printer_id = 0;
+
   if ((attr = ippFindAttribute(con->request, "printer-type",
                                IPP_TAG_ENUM)) != NULL)
     printer_type = (cups_ptype_t)attr->values[0].integer;
@@ -7471,6 +7582,9 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
     if (!local && !printer->shared)
       continue;
 
+    if (printer_id && printer->printer_id != printer_id)
+      continue;
+
     if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
         (printer->type & printer_mask) == printer_type &&
        (!location ||
@@ -8888,6 +9002,8 @@ release_held_new_jobs(
                     "Printer \"%s\" now printing pending/new jobs (\"%s\").",
                     printer->name, get_username(con));
 
+  cupsdCheckJobs();
+
  /*
   * Everything was ok, so return OK status...
   */
@@ -10559,7 +10675,7 @@ set_printer_attrs(cupsd_client_t  *con, /* I - Client connection */
  * 'set_printer_defaults()' - Set printer default options from a request.
  */
 
-static void
+static int                             /* O - 1 on success, 0 on failure */
 set_printer_defaults(
     cupsd_client_t  *con,              /* I - Client connection */
     cupsd_printer_t *printer)          /* I - Printer */
@@ -10688,7 +10804,7 @@ set_printer_defaults(
        send_ipp_status(con, IPP_NOT_POSSIBLE,
                        _("Unknown printer-op-policy \"%s\"."),
                        attr->values[0].string.text);
-       return;
+       return (0);
       }
     }
     else if (!strcmp(attr->name, "printer-error-policy"))
@@ -10705,7 +10821,7 @@ set_printer_defaults(
        send_ipp_status(con, IPP_NOT_POSSIBLE,
                        _("Unknown printer-error-policy \"%s\"."),
                        attr->values[0].string.text);
-       return;
+       return (0);
       }
 
       cupsdLogMessage(CUPSD_LOG_DEBUG,
@@ -10801,6 +10917,8 @@ set_printer_defaults(
          break;
     }
   }
+
+  return (1);
 }