]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/ipp.c
Merge changes from CUPS 1.4svn-r8606.
[thirdparty/cups.git] / scheduler / ipp.c
index d69dec1e8669f8fe8db298370224019e1913ca95..637d760b4fdf7120c6b68a88a485cbad2cfe0d7c 100644 (file)
@@ -1,44 +1,49 @@
 /*
- * "$Id: ipp.c 4906 2006-01-10 20:53:28Z mike $"
+ * "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $"
  *
  *   IPP routines for the Common UNIX Printing System (CUPS) scheduler.
  *
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ *   Copyright 2007-2009 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
+ *   This file contains Kerberos support code, copyright 2006 by
+ *   Jelmer Vernooij.
  *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   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/".
  *
  * Contents:
  *
- *   cupsdProcessIPPRequest()    - Process an incoming IPP request...
+ *   cupsdProcessIPPRequest()    - Process an incoming IPP request.
+ *   cupsdTimeoutJob()           - Timeout a job waiting on job files.
  *   accept_jobs()               - Accept print jobs to a printer.
  *   add_class()                 - Add a class to the system.
  *   add_file()                  - Add a file to a job.
+ *   add_job()                   - Add a job to a print queue.
  *   add_job_state_reasons()     - Add the "job-state-reasons" attribute based
  *                                 upon the job and printer state...
- *   add_job_subscriptions()     - Add any subcriptions for a job.
+ *   add_job_subscriptions()     - Add any subscriptions for a job.
+ *   add_job_uuid()              - Add job-uuid attribute to a job.
  *   add_printer()               - Add a printer to the system.
  *   add_printer_state_reasons() - Add the "printer-state-reasons" attribute
  *                                 based upon the printer state...
- *   add_queued_job_count()      - Add the "queued-job-count" attribute for
+ *   add_queued_job_count()      - Add the "queued-job-count" attribute for the
+ *                                 specified printer or class.
+ *   apple_init_profile()        - Initialize a color profile.
+ *   apple_register_profiles()   - Register color profiles for a printer.
+ *   apple_unregister_profiles() - Remove color profiles for the specified
+ *                                 printer.
+ *   apply_printer_defaults()    - Apply printer default options to a job.
  *   authenticate_job()          - Set job authentication info.
  *   cancel_all_jobs()           - Cancel all print jobs.
  *   cancel_job()                - Cancel a print job.
  *   cancel_subscription()       - Cancel a subscription.
  *   check_quotas()              - Check quotas for a printer and user.
+ *   check_rss_recipient()       - Check that we do not have a duplicate RSS
+ *                                 feed URI.
  *   copy_attribute()            - Copy a single attribute.
  *   copy_attrs()                - Copy attributes from one request to another.
  *   copy_banner()               - Copy a banner file to the requests directory
@@ -46,6 +51,8 @@
  *   copy_file()                 - Copy a PPD file or interface script...
  *   copy_model()                - Copy a PPD model file, substituting default
  *                                 values as needed...
+ *   copy_job_attrs()            - Copy job attributes.
+ *   copy_printer_attrs()        - Copy printer attributes.
  *   copy_subscription_attrs()   - Copy subscription attributes.
  *   create_job()                - Print a file to a printer or class.
  *   create_requested_array()    - Create an array for the requested-attributes.
  *   get_default()               - Get the default destination.
  *   get_devices()               - Get the list of available devices on the
  *                                 local system.
- *   get_jobs()                  - Get a list of jobs for the specified printer.
+ *   get_document()              - Get a copy of a job file.
  *   get_job_attrs()             - Get job attributes.
+ *   get_jobs()                  - Get a list of jobs for the specified printer.
  *   get_notifications()         - Get events for a subscription.
+ *   get_ppd()                   - Get a named PPD from the local system.
  *   get_ppds()                  - Get the list of PPD files on the local
  *                                 system.
  *   get_printer_attrs()         - Get printer attributes.
- *   get_printers()              - Get a list of printers.
+ *   get_printers()              - Get a list of printers or classes.
  *   get_subscription_attrs()    - Get subscription attributes.
  *   get_subscriptions()         - Get subscriptions.
+ *   get_username()              - Get the username associated with a request.
  *   hold_job()                  - Hold a print job.
  *   move_job()                  - Move a job to a new destination.
- *   ppd_add_default()           - Add a PPD default choice.
  *   ppd_parse_line()            - Parse a PPD default line.
  *   print_job()                 - Print a file to a printer or class.
- *   read_ps_line()              - Read a line from a PS file...
- *   read_ps_job_ticket()        - Reads a job ticket embedded in a PS file.
+ *   read_job_ticket()           - Read a job ticket embedded in a print file.
  *   reject_jobs()               - Reject print jobs to a printer.
  *   release_job()               - Release a held print job.
+ *   renew_subscription()        - Renew an existing subscription...
  *   restart_job()               - Restart an old print job.
  *   save_auth_info()            - Save authentication information for a job.
+ *   save_krb5_creds()           - Save Kerberos credentials for the job.
  *   send_document()             - Send a file to a printer or class.
  *   send_http_error()           - Send a HTTP error back to the IPP client.
  *   send_ipp_status()           - Send a status back to the IPP client.
  *   set_default()               - Set the default destination...
  *   set_job_attrs()             - Set job attributes.
+ *   set_printer_attrs()         - Set printer attributes.
+ *   set_printer_defaults()      - Set printer default options from a request.
  *   start_printer()             - Start a printer.
  *   stop_printer()              - Stop a printer.
+ *   url_encode_attr()           - URL-encode a string attribute.
+ *   url_encode_string()         - URL-encode a string.
  *   user_allowed()              - See if a user is allowed to print to a queue.
  *   validate_job()              - Validate printer options and destination.
  *   validate_name()             - Make sure the printer name only contains
  */
 
 #include "cupsd.h"
+#include <cups/ppd-private.h>
 
 #ifdef HAVE_LIBPAPER
 #  include <paper.h>
 #endif /* HAVE_LIBPAPER */
 
-
-/*
- * PPD default choice structure...
- */
-
-typedef struct
-{
-  char option[PPD_MAX_NAME];           /* Main keyword (option name) */
-  char choice[PPD_MAX_NAME];           /* Option keyword (choice name) */
-} ppd_default_t;
+#ifdef __APPLE__
+#  include <ApplicationServices/ApplicationServices.h>
+#  include <CoreFoundation/CoreFoundation.h>
+#  ifdef HAVE_MEMBERSHIP_H
+#    include <membership.h>
+#  endif /* HAVE_MEMBERSHIP_H */
+#  ifdef HAVE_MEMBERSHIPPRIV_H
+#    include <membershipPriv.h>
+#  else
+extern int mbr_user_name_to_uuid(const char* name, uuid_t uu);
+extern int mbr_group_name_to_uuid(const char* name, uuid_t uu);
+extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
+#  endif /* HAVE_MEMBERSHIPPRIV_H */
+#endif /* __APPLE__ */
 
 
 /*
@@ -116,25 +135,48 @@ typedef struct
 
 static void    accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    add_class(cupsd_client_t *con, ipp_attribute_t *uri);
-static int     add_file(cupsd_client_t *con, cupsd_job_t *job, mime_type_t *filetype,
-                        int compression);
+static int     add_file(cupsd_client_t *con, cupsd_job_t *job,
+                        mime_type_t *filetype, int compression);
+static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer,
+                           mime_type_t *filetype);
 static void    add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job);
 static void    add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job);
+static void    add_job_uuid(cupsd_client_t *con, cupsd_job_t *job);
 static void    add_printer(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    add_printer_state_reasons(cupsd_client_t *con, cupsd_printer_t *p);
+static void    add_printer_state_reasons(cupsd_client_t *con,
+                                         cupsd_printer_t *p);
 static void    add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p);
+#ifdef __APPLE__
+static void    apple_init_profile(ppd_file_t *ppd, cups_array_t *languages,
+                                  CMDeviceProfileInfo *profile, unsigned id,
+                                  const char *name, const char *text,
+                                  const char *iccfile);
+static void    apple_register_profiles(cupsd_printer_t *p);
+static void    apple_unregister_profiles(cupsd_printer_t *p);
+#endif /* __APPLE__ */
+static void    apply_printer_defaults(cupsd_printer_t *printer,
+                                      cupsd_job_t *job);
 static void    authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    cancel_subscription(cupsd_client_t *con, int id);
+static int     check_rss_recipient(const char *recipient);
 static int     check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
 static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr,
                                        int quickcopy);
-static void    copy_attrs(ipp_t *to, ipp_t *from, ipp_attribute_t *req,
+static void    copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
                           ipp_tag_t group, int quickcopy);
-static int     copy_banner(cupsd_client_t *con, cupsd_job_t *job, const char *name);
+static int     copy_banner(cupsd_client_t *con, cupsd_job_t *job,
+                           const char *name);
 static int     copy_file(const char *from, const char *to);
-static int     copy_model(cupsd_client_t *con, const char *from, const char *to);
+static int     copy_model(cupsd_client_t *con, const char *from,
+                          const char *to);
+static void    copy_job_attrs(cupsd_client_t *con,
+                              cupsd_job_t *job,
+                              cups_array_t *ra);
+static void    copy_printer_attrs(cupsd_client_t *con,
+                                  cupsd_printer_t *printer,
+                                  cups_array_t *ra);
 static void    copy_subscription_attrs(cupsd_client_t *con,
                                        cupsd_subscription_t *sub,
                                        cups_array_t *ra);
@@ -144,29 +186,39 @@ static void       create_subscription(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    delete_printer(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    get_default(cupsd_client_t *con);
 static void    get_devices(cupsd_client_t *con);
+static void    get_document(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    get_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    get_notifications(cupsd_client_t *con, int id);
+static void    get_notifications(cupsd_client_t *con);
+static void    get_ppd(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    get_ppds(cupsd_client_t *con);
 static void    get_printers(cupsd_client_t *con, int type);
 static void    get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
+static void    get_printer_supported(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    get_subscription_attrs(cupsd_client_t *con, int sub_id);
 static void    get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri);
+static const char *get_username(cupsd_client_t *con);
 static void    hold_job(cupsd_client_t *con, ipp_attribute_t *uri);
+static void    hold_new_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    move_job(cupsd_client_t *con, ipp_attribute_t *uri);
-static int     ppd_add_default(const char *option, const char *choice,
-                               int num_defaults, ppd_default_t **defaults);
 static int     ppd_parse_line(const char *line, char *option, int olen,
                               char *choice, int clen);
 static void    print_job(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    read_ps_job_ticket(cupsd_client_t *con);
+static void    read_job_ticket(cupsd_client_t *con);
 static void    reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
+static void    release_held_new_jobs(cupsd_client_t *con,
+                                     ipp_attribute_t *uri);
 static void    release_job(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    renew_subscription(cupsd_client_t *con, int sub_id);
 static void    restart_job(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    save_auth_info(cupsd_client_t *con, cupsd_job_t *job);
+static void    save_auth_info(cupsd_client_t *con, cupsd_job_t *job,
+                              ipp_attribute_t *auth_info);
+#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
+static void    save_krb5_creds(cupsd_client_t *con, cupsd_job_t *job);
+#endif /* HAVE_GSSAPI && HAVE_KRB5_H */
 static void    send_document(cupsd_client_t *con, ipp_attribute_t *uri);
-static void    send_http_error(cupsd_client_t *con, http_status_t status);
+static void    send_http_error(cupsd_client_t *con, http_status_t status,
+                               cupsd_printer_t *printer);
 static void    send_ipp_status(cupsd_client_t *con, ipp_status_t status,
                                const char *message, ...)
 #    ifdef __GNUC__
@@ -175,8 +227,14 @@ __attribute__ ((__format__ (__printf__, 3, 4)))
 ;
 static void    set_default(cupsd_client_t *con, ipp_attribute_t *uri);
 static void    set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
+static void    set_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
+static void    set_printer_defaults(cupsd_client_t *con,
+                                    cupsd_printer_t *printer);
 static 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,
+                               int bufsize);
+static char    *url_encode_string(const char *s, char *buffer, int bufsize);
 static int     user_allowed(cupsd_printer_t *p, const char *username);
 static void    validate_job(cupsd_client_t *con, ipp_attribute_t *uri);
 static int     validate_name(const char *name);
@@ -186,7 +244,7 @@ static int  validate_user(cupsd_job_t *job, cupsd_client_t *con,
 
 
 /*
- * 'cupsdProcessIPPRequest()' - Process an incoming IPP request...
+ * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
  */
 
 int                                    /* O - 1 on success, 0 on failure */
@@ -197,7 +255,7 @@ cupsdProcessIPPRequest(
   ipp_attribute_t      *attr;          /* Current attribute */
   ipp_attribute_t      *charset;       /* Character set attribute */
   ipp_attribute_t      *language;      /* Language attribute */
-  ipp_attribute_t      *uri;           /* Printer URI attribute */
+  ipp_attribute_t      *uri = NULL;    /* Printer or job URI attribute */
   ipp_attribute_t      *username;      /* requesting-user-name attr */
   int                  sub_id;         /* Subscription ID */
 
@@ -212,18 +270,22 @@ cupsdProcessIPPRequest(
 
   con->response = ippNew();
 
-  con->response->request.status.version[0] = con->request->request.op.version[0];
-  con->response->request.status.version[1] = con->request->request.op.version[1];
-  con->response->request.status.request_id = con->request->request.op.request_id;
+  con->response->request.status.version[0] =
+      con->request->request.op.version[0];
+  con->response->request.status.version[1] =
+      con->request->request.op.version[1];
+  con->response->request.status.request_id =
+      con->request->request.op.request_id;
 
  /*
   * Then validate the request header and required attributes...
   */
-  
-  if (con->request->request.any.version[0] != 1)
+
+  if (con->request->request.any.version[0] != 1 &&
+      con->request->request.any.version[0] != 2)
   {
    /*
-    * Return an error, since we only support IPP 1.x.
+    * Return an error, since we only support IPP 1.x and 2.x.
     */
 
     cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
@@ -236,8 +298,22 @@ cupsdProcessIPPRequest(
                     _("Bad request version number %d.%d!"),
                    con->request->request.any.version[0],
                    con->request->request.any.version[1]);
-  }  
-  else if (con->request->attrs == NULL)
+  }
+  else if (con->request->request.any.request_id < 1)
+  {
+   /*
+    * Return an error, since request IDs must be between 1 and 2^31-1
+    */
+
+    cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
+                  "%04X %s Bad request ID %d",
+                 IPP_BAD_REQUEST, con->http.hostname,
+                  con->request->request.any.request_id);
+
+    send_ipp_status(con, IPP_BAD_REQUEST, _("Bad request ID %d!"),
+                   con->request->request.any.request_id);
+  }
+  else if (!con->request->attrs)
   {
     cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
                   "%04X %s No attributes in request",
@@ -253,7 +329,7 @@ cupsdProcessIPPRequest(
     */
 
     for (attr = con->request->attrs, group = attr->group_tag;
-        attr != NULL;
+        attr;
         attr = attr->next)
       if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO)
       {
@@ -273,7 +349,7 @@ cupsdProcessIPPRequest(
       else
        group = attr->group_tag;
 
-    if (attr == NULL)
+    if (!attr)
     {
      /*
       * Then make sure that the first three attributes are:
@@ -284,8 +360,9 @@ cupsdProcessIPPRequest(
       */
 
       attr = con->request->attrs;
-      if (attr && !strcmp(attr->name, "attributes-charset") &&
-         attr->value_tag == IPP_TAG_CHARSET)
+      if (attr && attr->name &&
+          !strcmp(attr->name, "attributes-charset") &&
+         (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
        charset = attr;
       else
        charset = NULL;
@@ -293,22 +370,28 @@ cupsdProcessIPPRequest(
       if (attr)
         attr = attr->next;
 
-      if (attr && !strcmp(attr->name, "attributes-natural-language") &&
-         attr->value_tag == IPP_TAG_LANGUAGE)
+      if (attr && attr->name &&
+          !strcmp(attr->name, "attributes-natural-language") &&
+         (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
        language = attr;
       else
        language = NULL;
 
-      if ((attr = ippFindAttribute(con->request, "printer-uri", IPP_TAG_URI)) != NULL)
+      if ((attr = ippFindAttribute(con->request, "printer-uri",
+                                   IPP_TAG_URI)) != NULL)
        uri = attr;
-      else if ((attr = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI)) != NULL)
+      else if ((attr = ippFindAttribute(con->request, "job-uri",
+                                        IPP_TAG_URI)) != NULL)
        uri = attr;
+      else if (con->request->request.op.operation_id == CUPS_GET_PPD)
+        uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
       else
        uri = NULL;
 
       if (charset)
        ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
-                    "attributes-charset", NULL, charset->values[0].string.text);
+                    "attributes-charset", NULL,
+                    charset->values[0].string.text);
       else
        ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
                     "attributes-charset", NULL, DefaultCharset);
@@ -321,13 +404,31 @@ cupsdProcessIPPRequest(
        ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
                      "attributes-natural-language", NULL, DefaultLanguage);
 
-      if (charset == NULL || language == NULL ||
-         (uri == NULL &&
-          con->request->request.op.operation_id != CUPS_GET_DEFAULT &&
-          con->request->request.op.operation_id != CUPS_GET_PRINTERS &&
-          con->request->request.op.operation_id != CUPS_GET_CLASSES &&
-          con->request->request.op.operation_id != CUPS_GET_DEVICES &&
-          con->request->request.op.operation_id != CUPS_GET_PPDS))
+      if (charset &&
+          strcasecmp(charset->values[0].string.text, "us-ascii") &&
+          strcasecmp(charset->values[0].string.text, "utf-8"))
+      {
+       /*
+        * Bad character set...
+       */
+
+        cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"!",
+                       charset->values[0].string.text);
+       cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
+                     "%04X %s Unsupported attributes-charset value \"%s\"",
+                     IPP_CHARSET, con->http.hostname,
+                     charset->values[0].string.text);
+       send_ipp_status(con, IPP_BAD_REQUEST,
+                       _("Unsupported character set \"%s\"!"),
+                       charset->values[0].string.text);
+      }
+      else if (!charset || !language ||
+              (!uri &&
+               con->request->request.op.operation_id != CUPS_GET_DEFAULT &&
+               con->request->request.op.operation_id != CUPS_GET_PRINTERS &&
+               con->request->request.op.operation_id != CUPS_GET_CLASSES &&
+               con->request->request.op.operation_id != CUPS_GET_DEVICES &&
+               con->request->request.op.operation_id != CUPS_GET_PPDS))
       {
        /*
        * Return an error, since attributes-charset,
@@ -358,17 +459,18 @@ cupsdProcessIPPRequest(
         if (!uri)
        {
          cupsdLogMessage(CUPSD_LOG_ERROR,
-                         "Missing printer-uri or job-uri attribute!");
+                         "Missing printer-uri, job-uri, or ppd-name "
+                         "attribute!");
 
          cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
-                       "%04X %s Missing printer-uri or job-uri attribute",
-                       IPP_BAD_REQUEST, con->http.hostname);
+                       "%04X %s Missing printer-uri, job-uri, or ppd-name "
+                       "attribute", IPP_BAD_REQUEST, con->http.hostname);
         }
 
        cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
 
-       for (attr = con->request->attrs; attr != NULL; attr = attr->next)
-         cupsdLogMessage(CUPSD_LOG_DEBUG, 
+       for (attr = con->request->attrs; attr; attr = attr->next)
+         cupsdLogMessage(CUPSD_LOG_DEBUG,
                          "attr \"%s\": group_tag = %x, value_tag = %x",
                          attr->name ? attr->name : "(null)", attr->group_tag,
                          attr->value_tag);
@@ -400,7 +502,8 @@ cupsdProcessIPPRequest(
            * Remote unauthenticated user masquerading as local root...
            */
 
-           cupsdSetString(&(username->values[0].string.text), RemoteRoot);
+           _cupsStrFree(username->values[0].string.text);
+           username->values[0].string.text = _cupsStrAlloc(RemoteRoot);
          }
        }
 
@@ -415,9 +518,12 @@ cupsdProcessIPPRequest(
        */
 
         if (uri)
-          cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                         "cupsdProcessIPPRequest: URI=\"%s\"",
-                         uri->values[0].string.text);
+         cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s",
+                         ippOpString(con->request->request.op.operation_id),
+                         uri->values[0].string.text);
+        else
+         cupsdLogMessage(CUPSD_LOG_DEBUG, "%s",
+                         ippOpString(con->request->request.op.operation_id));
 
        switch (con->request->request.op.operation_id)
        {
@@ -453,6 +559,10 @@ cupsdProcessIPPRequest(
               get_printer_attrs(con, uri);
               break;
 
+         case IPP_GET_PRINTER_SUPPORTED_VALUES :
+              get_printer_supported(con, uri);
+              break;
+
          case IPP_HOLD_JOB :
               hold_job(con, uri);
               break;
@@ -481,6 +591,18 @@ cupsdProcessIPPRequest(
               set_job_attrs(con, uri);
               break;
 
+         case IPP_SET_PRINTER_ATTRIBUTES :
+              set_printer_attrs(con, uri);
+              break;
+
+         case IPP_HOLD_NEW_JOBS :
+              hold_new_jobs(con, uri);
+              break;
+
+         case IPP_RELEASE_HELD_NEW_JOBS :
+              release_held_new_jobs(con, uri);
+              break;
+
          case CUPS_GET_DEFAULT :
               get_default(con);
               break;
@@ -527,6 +649,14 @@ cupsdProcessIPPRequest(
               get_devices(con);
               break;
 
+          case CUPS_GET_DOCUMENT :
+             get_document(con, uri);
+             break;
+
+         case CUPS_GET_PPD :
+              get_ppd(con, uri);
+              break;
+
          case CUPS_GET_PPDS :
               get_ppds(con);
               break;
@@ -561,7 +691,7 @@ cupsdProcessIPPRequest(
              break;
 
           case IPP_GET_NOTIFICATIONS :
-             get_notifications(con, sub_id);
+             get_notifications(con);
              break;
 
          default :
@@ -573,7 +703,8 @@ cupsdProcessIPPRequest(
 
               send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED,
                              _("%s not supported!"),
-                             ippOpString(con->request->request.op.operation_id));
+                             ippOpString(
+                                 con->request->request.op.operation_id));
              break;
        }
       }
@@ -586,38 +717,77 @@ cupsdProcessIPPRequest(
     * Sending data from the scheduler...
     */
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "cupsdProcessIPPRequest: %d status_code=%x (%s)",
-                    con->http.fd, con->response->request.status.status_code,
-                   ippErrorString(con->response->request.status.status_code));
+    cupsdLogMessage(con->response->request.status.status_code
+                        >= IPP_BAD_REQUEST &&
+                    con->response->request.status.status_code
+                       != IPP_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
+                    "Returning IPP %s for %s (%s) from %s",
+                   ippErrorString(con->response->request.status.status_code),
+                   ippOpString(con->request->request.op.operation_id),
+                   uri ? uri->values[0].string.text : "no URI",
+                   con->http.hostname);
+
+    if (LogLevel == CUPSD_LOG_DEBUG2)
+      cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                     "cupsdProcessIPPRequest: ippLength(response)=%ld",
+                     (long)ippLength(con->response));
 
-    if (cupsdSendHeader(con, HTTP_OK, "application/ipp"))
+    if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE))
     {
+#ifdef CUPSD_USE_CHUNKING
+     /*
+      * Because older versions of CUPS (1.1.17 and older) and some IPP
+      * clients do not implement chunking properly, we cannot use
+      * chunking by default.  This may become the default in future
+      * CUPS releases, or we might add a configuration directive for
+      * it.
+      */
+
       if (con->http.version == HTTP_1_1)
       {
-       con->http.data_encoding = HTTP_ENCODE_CHUNKED;
+       if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n") < 0)
+         return (0);
+
+       if (cupsdFlushHeader(con) < 0)
+         return (0);
 
-       httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n");
+       con->http.data_encoding = HTTP_ENCODE_CHUNKED;
       }
       else
+#endif /* CUPSD_USE_CHUNKING */
       {
+        size_t length;                 /* Length of response */
+
+
+       length = ippLength(con->response);
+
+       if (con->file >= 0 && !con->pipe_pid)
+       {
+         struct stat   fileinfo;       /* File information */
+
+
+          if (!fstat(con->file, &fileinfo))
+           length += fileinfo.st_size;
+       }
+
+       if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n",
+                      CUPS_LLCAST length) < 0)
+         return (0);
+
+       if (cupsdFlushHeader(con) < 0)
+         return (0);
+
        con->http.data_encoding  = HTTP_ENCODE_LENGTH;
-       con->http.data_remaining = ippLength(con->response);
+       con->http.data_remaining = length;
 
-        if (con->http.data_remaining < INT_MAX)
+       if (con->http.data_remaining <= INT_MAX)
          con->http._data_remaining = con->http.data_remaining;
        else
          con->http._data_remaining = INT_MAX;
-
-       httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n",
-                  CUPS_LLCAST con->http.data_remaining);
       }
 
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "cupsdProcessIPPRequest: Adding fd %d to OutputSet...",
-                     con->http.fd);
-
-      FD_SET(con->http.fd, OutputSet);
+      cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient,
+                     (cupsd_selfunc_t)cupsdWriteClient, con);
 
      /*
       * Tell the caller the response header was sent successfully...
@@ -646,6 +816,48 @@ cupsdProcessIPPRequest(
 }
 
 
+/*
+ * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
+ */
+
+int                                    /* O - 0 on success, -1 on error */
+cupsdTimeoutJob(cupsd_job_t *job)      /* I - Job to timeout */
+{
+  cupsd_printer_t      *printer;       /* Destination printer or class */
+  ipp_attribute_t      *attr;          /* job-sheets attribute */
+  int                  kbytes;         /* Kilobytes in banner */
+
+
+  job->pending_timeout = 0;
+
+ /*
+  * See if we need to add the ending sheet...
+  */
+
+  printer = cupsdFindDest(job->dest);
+  attr    = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
+
+  if (printer &&
+      !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
+      attr && attr->num_values > 1)
+  {
+   /*
+    * Yes...
+    */
+
+    cupsdLogJob(job, CUPSD_LOG_INFO, "Adding end banner page \"%s\".",
+                attr->values[1].string.text);
+
+    if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0)
+      return (-1);
+
+    cupsdUpdateQuota(printer, job->username, 0, kbytes);
+  }
+
+  return (0);
+}
+
+
 /*
  * 'accept_jobs()' - Accept print jobs to a printer.
  */
@@ -655,13 +867,7 @@ accept_jobs(cupsd_client_t  *con,  /* I - Client connection */
             ipp_attribute_t *uri)      /* I - Printer or class URI */
 {
   http_status_t        status;                 /* Policy status */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
-  const char   *name;                  /* Printer name */
+  cups_ptype_t dtype;                  /* Destination type (printer/class) */
   cupsd_printer_t *printer;            /* Printer data */
 
 
@@ -672,11 +878,7 @@ accept_jobs(cupsd_client_t  *con,  /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -693,7 +895,7 @@ accept_jobs(cupsd_client_t  *con,   /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
@@ -707,12 +909,20 @@ accept_jobs(cupsd_client_t  *con, /* I - Client connection */
   cupsdAddPrinterHistory(printer);
 
   if (dtype & CUPS_PRINTER_CLASS)
-    cupsdSaveAllClasses();
+  {
+    cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
+
+    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").",
+                    printer->name, get_username(con));
+  }
   else
-    cupsdSaveAllPrinters();
+  {
+    cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" now accepting jobs (\"%s\").", name,
-                  con->username);
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Printer \"%s\" now accepting jobs (\"%s\").",
+                    printer->name, get_username(con));
+  }
 
  /*
   * Everything was ok, so return OK status...
@@ -732,7 +942,7 @@ add_class(cupsd_client_t  *con,             /* I - Client connection */
 {
   http_status_t        status;                 /* Policy status */
   int          i;                      /* Looping var */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
@@ -740,9 +950,10 @@ add_class(cupsd_client_t  *con,            /* I - Client connection */
   cupsd_printer_t *pclass,             /* Class */
                *member;                /* Member printer/class */
   cups_ptype_t dtype;                  /* Destination type */
-  const char   *dest;                  /* Printer or class name */
   ipp_attribute_t *attr;               /* Printer attribute */
   int          modify;                 /* Non-zero if we just modified */
+  char         newname[IPP_MAX_NAME];  /* New class name */
+  int          need_restart_job;       /* Need to restart job? */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con,
@@ -752,12 +963,12 @@ add_class(cupsd_client_t  *con,           /* I - Client connection */
   * Do we have a valid URI?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                  sizeof(scheme), username, sizeof(username), host,
+                 sizeof(host), &port, resource, sizeof(resource));
 
 
-  if (strncmp(resource, "/classes/", 9) != 0 || strlen(resource) == 9)
+  if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9)
   {
    /*
     * No, return an error...
@@ -785,16 +996,6 @@ add_class(cupsd_client_t  *con,            /* I - Client connection */
     return;
   }
 
- /*
-  * Check policy...
-  */
-
-  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
-  {
-    send_http_error(con, status);
-    return;
-  }
-
  /*
   * See if the class already exists; if not, create a new class...
   */
@@ -806,7 +1007,7 @@ add_class(cupsd_client_t  *con,            /* I - Client connection */
     */
 
     if ((pclass = cupsdFindPrinter(resource + 9)) != NULL &&
-        !(pclass->type & CUPS_PRINTER_REMOTE))
+        !(pclass->type & CUPS_PRINTER_DISCOVERED))
     {
      /*
       * Yes, return an error...
@@ -819,23 +1020,35 @@ add_class(cupsd_client_t  *con,          /* I - Client connection */
     }
 
    /*
-    * No, add the pclass...
+    * No, check the default policy and then add the class...
     */
 
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, NULL);
+      return;
+    }
+
     pclass = cupsdAddClass(resource + 9);
     modify = 0;
   }
   else if (pclass->type & CUPS_PRINTER_IMPLICIT)
   {
    /*
-    * Rename the implicit class to "AnyClass" or remove it...
+    * Check the default policy, then rename the implicit class to "AnyClass"
+    * or remove it...
     */
 
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, NULL);
+      return;
+    }
+
     if (ImplicitAnyClasses)
     {
-      cupsArrayRemove(Printers, pclass);
-      cupsdSetStringf(&pclass->name, "Any%s", resource + 9);
-      cupsArrayAdd(Printers, pclass);
+      snprintf(newname, sizeof(newname), "Any%s", resource + 9);
+      cupsdRenamePrinter(pclass, newname);
     }
     else
       cupsdDeletePrinter(pclass, 1);
@@ -847,17 +1060,20 @@ add_class(cupsd_client_t  *con,          /* I - Client connection */
     pclass = cupsdAddClass(resource + 9);
     modify = 0;
   }
-  else if (pclass->type & CUPS_PRINTER_REMOTE)
+  else if (pclass->type & CUPS_PRINTER_DISCOVERED)
   {
    /*
-    * Rename the remote class to "Class"...
+    * Check the default policy, then rename the remote class to "Class"...
     */
 
-    cupsdDeletePrinterFilters(pclass);
-    cupsArrayRemove(Printers, pclass);
-    cupsdSetStringf(&pclass->name, "%s@%s", resource + 9, pclass->hostname);
-    cupsdSetPrinterAttrs(pclass);
-    cupsArrayAdd(Printers, pclass);
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, NULL);
+      return;
+    }
+
+    snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname);
+    cupsdRenamePrinter(pclass, newname);
 
    /*
     * Add the class as a new local class...
@@ -866,6 +1082,12 @@ add_class(cupsd_client_t  *con,           /* I - Client connection */
     pclass = cupsdAddClass(resource + 9);
     modify = 0;
   }
+  else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con,
+                                      NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, pclass);
+    return;
+  }
   else
     modify = 1;
 
@@ -873,25 +1095,32 @@ add_class(cupsd_client_t  *con,          /* I - Client connection */
   * Look for attributes and copy them over as needed...
   */
 
-  if ((attr = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT)) != NULL)
+  need_restart_job = 0;
+
+  if ((attr = ippFindAttribute(con->request, "printer-location",
+                               IPP_TAG_TEXT)) != NULL)
     cupsdSetString(&pclass->location, attr->values[0].string.text);
 
-  if ((attr = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "printer-info",
+                               IPP_TAG_TEXT)) != NULL)
     cupsdSetString(&pclass->info, attr->values[0].string.text);
 
-  if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", IPP_TAG_BOOLEAN)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
+                               IPP_TAG_BOOLEAN)) != NULL)
   {
-    cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-is-accepting-jobs to %d (was %d.)",
-               pclass->name, attr->values[0].boolean, pclass->accepting);
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Setting %s printer-is-accepting-jobs to %d (was %d.)",
+                    pclass->name, attr->values[0].boolean, pclass->accepting);
 
     pclass->accepting = attr->values[0].boolean;
     cupsdAddPrinterHistory(pclass);
   }
 
-  if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "printer-is-shared",
+                               IPP_TAG_BOOLEAN)) != NULL)
   {
     if (pclass->shared && !attr->values[0].boolean)
-      cupsdSendBrowseDelete(pclass);
+      cupsdDeregisterPrinter(pclass, 1);
 
     cupsdLogMessage(CUPSD_LOG_INFO,
                     "Setting %s printer-is-shared to %d (was %d.)",
@@ -900,7 +1129,8 @@ add_class(cupsd_client_t  *con,            /* I - Client connection */
     pclass->shared = attr->values[0].boolean;
   }
 
-  if ((attr = ippFindAttribute(con->request, "printer-state", IPP_TAG_ENUM)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "printer-state",
+                               IPP_TAG_ENUM)) != NULL)
   {
     if (attr->values[0].integer != IPP_PRINTER_IDLE &&
         attr->values[0].integer != IPP_PRINTER_STOPPED)
@@ -911,126 +1141,33 @@ add_class(cupsd_client_t  *con,         /* I - Client connection */
       return;
     }
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", pclass->name,
-                    attr->values[0].integer, pclass->state);
+    cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
+                    pclass->name, attr->values[0].integer, pclass->state);
 
     if (attr->values[0].integer == IPP_PRINTER_STOPPED)
       cupsdStopPrinter(pclass, 0);
     else
+    {
       cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0);
+      need_restart_job = 1;
+    }
   }
-  if ((attr = ippFindAttribute(con->request, "printer-state-message", IPP_TAG_TEXT)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "printer-state-message",
+                               IPP_TAG_TEXT)) != NULL)
   {
     strlcpy(pclass->state_message, attr->values[0].string.text,
             sizeof(pclass->state_message));
     cupsdAddPrinterHistory(pclass);
   }
-  if ((attr = ippFindAttribute(con->request, "job-sheets-default", IPP_TAG_ZERO)) != NULL &&
-      !Classification)
-  {
-    cupsdSetString(&pclass->job_sheets[0], attr->values[0].string.text);
-    if (attr->num_values > 1)
-      cupsdSetString(&pclass->job_sheets[1], attr->values[1].string.text);
-    else
-      cupsdSetString(&pclass->job_sheets[1], "none");
-  }
-  if ((attr = ippFindAttribute(con->request, "requesting-user-name-allowed",
-                               IPP_TAG_ZERO)) != NULL)
-  {
-    cupsdFreePrinterUsers(pclass);
-
-    pclass->deny_users = 0;
-    if (attr->value_tag == IPP_TAG_NAME &&
-        (attr->num_values > 1 ||
-        strcmp(attr->values[0].string.text, "all") != 0))
-      for (i = 0; i < attr->num_values; i ++)
-       cupsdAddPrinterUser(pclass, attr->values[i].string.text);
-  }
-  else if ((attr = ippFindAttribute(con->request, "requesting-user-name-denied",
-                                    IPP_TAG_ZERO)) != NULL)
-  {
-    cupsdFreePrinterUsers(pclass);
-
-    pclass->deny_users = 1;
-    if (attr->value_tag == IPP_TAG_NAME &&
-        (attr->num_values > 1 ||
-        strcmp(attr->values[0].string.text, "none") != 0))
-      for (i = 0; i < attr->num_values; i ++)
-       cupsdAddPrinterUser(pclass, attr->values[i].string.text);
-  }
-  if ((attr = ippFindAttribute(con->request, "job-quota-period",
-                               IPP_TAG_INTEGER)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "add_class: Setting job-quota-period to %d...",
-                    attr->values[0].integer);
-    cupsdFreeQuotas(pclass);
-    pclass->quota_period = attr->values[0].integer;
-  }
-  if ((attr = ippFindAttribute(con->request, "job-k-limit",
-                               IPP_TAG_INTEGER)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "add_class: Setting job-k-limit to %d...",
-                    attr->values[0].integer);
-    cupsdFreeQuotas(pclass);
-    pclass->k_limit = attr->values[0].integer;
-  }
-  if ((attr = ippFindAttribute(con->request, "job-page-limit",
-                               IPP_TAG_INTEGER)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "add_class: Setting job-page-limit to %d...",
-                    attr->values[0].integer);
-    cupsdFreeQuotas(pclass);
-    pclass->page_limit = attr->values[0].integer;
-  }
-  if ((attr = ippFindAttribute(con->request, "printer-op-policy",
-                               IPP_TAG_NAME)) != NULL)
-  {
-    cupsd_policy_t *p;                 /* Policy */
-
-
-    if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
-    {
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "add_class: Setting printer-op-policy to \"%s\"...",
-                      attr->values[0].string.text);
-      cupsdSetString(&pclass->op_policy, attr->values[0].string.text);
-      pclass->op_policy_ptr = p;
-    }
-    else
-    {
-      send_ipp_status(con, IPP_NOT_POSSIBLE,
-                      _("add_class: Unknown printer-op-policy \"%s\"."),
-                      attr->values[0].string.text);
-      return;
-    }
-  }
-  if ((attr = ippFindAttribute(con->request, "printer-error-policy",
-                               IPP_TAG_NAME)) != NULL)
-  {
-    if (strcmp(attr->values[0].string.text, "abort-job") &&
-        strcmp(attr->values[0].string.text, "retry-job") &&
-        strcmp(attr->values[0].string.text, "stop-printer"))
-    {
-      send_ipp_status(con, IPP_NOT_POSSIBLE,
-                      _("add_class: Unknown printer-error-policy \"%s\"."),
-                      attr->values[0].string.text);
-      return;
-    }
-
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "add_class: Setting printer-error-policy to \"%s\"...",
-                    attr->values[0].string.text);
-    cupsdSetString(&pclass->error_policy, attr->values[0].string.text);
-  }
-  if ((attr = ippFindAttribute(con->request, "member-uris", IPP_TAG_URI)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "member-uris",
+                               IPP_TAG_URI)) != NULL)
   {
    /*
     * Clear the printer array as needed...
     */
 
+    need_restart_job = 1;
+
     if (pclass->num_printers > 0)
     {
       free(pclass->printers);
@@ -1047,11 +1184,7 @@ add_class(cupsd_client_t  *con,          /* I - Client connection */
       * Search for the printer or class URI...
       */
 
-      httpSeparateURI(attr->values[i].string.text, method, sizeof(method),
-                      username, sizeof(username), host, sizeof(host), &port,
-                     resource, sizeof(resource));
-
-      if ((dest = cupsdValidateDest(host, resource, &dtype, &member)) == NULL)
+      if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member))
       {
        /*
        * Bad URI...
@@ -1070,24 +1203,39 @@ add_class(cupsd_client_t  *con,         /* I - Client connection */
     }
   }
 
+  set_printer_defaults(con, pclass);
+
+  if ((attr = ippFindAttribute(con->request, "auth-info-required",
+                               IPP_TAG_KEYWORD)) != NULL)
+    cupsdSetAuthInfoRequired(pclass, NULL, attr);
+
  /*
   * Update the printer class attributes and return...
   */
 
   cupsdSetPrinterAttrs(pclass);
-  cupsdSaveAllClasses();
-  cupsdCheckJobs();
+  cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
+
+  if (need_restart_job && pclass->job)
+  {
+   /*
+    * Reset the current job to a "pending" status...
+    */
 
-  cupsdWritePrintcap();
+    cupsdSetJobState(pclass->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
+                     "Job restarted because the class was modified.");
+  }
+
+  cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
 
   if (modify)
   {
     cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL,
                   "Class \"%s\" modified by \"%s\".", pclass->name,
-                 con->username);
+                 get_username(con));
 
     cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".",
-                    pclass->name, con->username);
+                    pclass->name, get_username(con));
   }
   else
   {
@@ -1095,10 +1243,10 @@ add_class(cupsd_client_t  *con,         /* I - Client connection */
 
     cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL,
                   "New class \"%s\" added by \"%s\".", pclass->name,
-                 con->username);
+                 get_username(con));
 
     cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".",
-                    pclass->name, con->username);
+                    pclass->name, get_username(con));
   }
 
   con->response->request.status.status_code = IPP_OK;
@@ -1120,9 +1268,9 @@ add_file(cupsd_client_t *con,             /* I - Connection to client */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                 "add_file(con=%p[%d], job=%d, filetype=%s/%s, compression=%d)",
-                 con, con->http.fd, job->id, filetype->super, filetype->type,
-                 compression);
+                 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
+                 "compression=%d)", con, con ? con->http.fd : -1, job->id,
+                 filetype->super, filetype->type, compression);
 
  /*
   * Add the file to the job...
@@ -1142,12 +1290,15 @@ add_file(cupsd_client_t *con,           /* I - Connection to client */
                                           sizeof(mime_type_t *));
   }
 
-  if (compressions == NULL || filetypes == NULL)
+  if (!compressions || !filetypes)
   {
-    cupsdCancelJob(job, 1);
+    cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
+                     "Job aborted because the scheduler ran out of memory.");
+
+    if (con)
+      send_ipp_status(con, IPP_INTERNAL_ERROR,
+                     _("Unable to allocate memory for file types!"));
 
-    send_ipp_status(con, IPP_INTERNAL_ERROR,
-                    _("Unable to allocate memory for file types!"));
     return (-1);
   }
 
@@ -1158,883 +1309,2284 @@ add_file(cupsd_client_t *con,                /* I - Connection to client */
 
   job->num_files ++;
 
+  job->dirty = 1;
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
+
   return (0);
 }
 
 
 /*
- * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
- *                             upon the job and printer state...
+ * 'add_job()' - Add a job to a print queue.
  */
 
-static void
-add_job_state_reasons(
-    cupsd_client_t *con,               /* I - Client connection */
-    cupsd_job_t    *job)               /* I - Job info */
+static cupsd_job_t *                   /* O - Job object */
+add_job(cupsd_client_t  *con,          /* I - Client connection */
+       cupsd_printer_t *printer,       /* I - Destination printer */
+       mime_type_t     *filetype)      /* I - First print file type, if any */
 {
-  cupsd_printer_t      *dest;          /* Destination printer */
+  http_status_t        status;                 /* Policy status */
+  ipp_attribute_t *attr,               /* Current attribute */
+               *auth_info;             /* auth-info attribute */
+  const char   *val;                   /* Default option value */
+  int          priority;               /* Job priority */
+  char         *title;                 /* Job name/title */
+  cupsd_job_t  *job;                   /* Current job */
+  char         job_uri[HTTP_MAX_URI];  /* Job URI */
+  int          kbytes;                 /* Size of print file */
+  int          i;                      /* Looping var */
+  int          lowerpagerange;         /* Page range bound */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)",
-                  con, con->http.fd, job ? job->id : 0);
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
+                  con, con->http.fd, printer, printer->name,
+                 filetype, filetype ? filetype->super : "none",
+                 filetype ? filetype->type : "none");
 
-  switch (job ? job->state->values[0].integer : IPP_JOB_CANCELLED)
-  {
-    case IPP_JOB_PENDING :
-        if (job->dtype & CUPS_PRINTER_CLASS)
-         dest = cupsdFindClass(job->dest);
-       else
-         dest = cupsdFindPrinter(job->dest);
+ /*
+  * Check remote printing to non-shared printer...
+  */
 
-        if (dest != NULL && dest->state == IPP_PRINTER_STOPPED)
-          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                      "job-state-reasons", NULL, "printer-stopped");
-        else
-          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                      "job-state-reasons", NULL, "none");
-        break;
+  if (!printer->shared &&
+      strcasecmp(con->http.hostname, "localhost") &&
+      strcasecmp(con->http.hostname, ServerName))
+  {
+    send_ipp_status(con, IPP_NOT_AUTHORIZED,
+                    _("The printer or class is not shared!"));
+    return (NULL);
+  }
 
-    case IPP_JOB_HELD :
-        if (ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD) != NULL ||
-           ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME) != NULL)
-          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                      "job-state-reasons", NULL, "job-hold-until-specified");
-        else
-          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                      "job-state-reasons", NULL, "job-incoming");
-        break;
+ /*
+  * Check policy...
+  */
 
-    case IPP_JOB_PROCESSING :
-        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                    "job-state-reasons", NULL, "job-printing");
-        break;
+  auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
+
+  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, printer);
+    return (NULL);
+  }
+  else if (printer->num_auth_info_required == 1 &&
+           !strcmp(printer->auth_info_required[0], "negotiate") &&
+           !con->username[0])
+  {
+    send_http_error(con, HTTP_UNAUTHORIZED, printer);
+    return (NULL);
+  }
+#ifdef HAVE_SSL
+  else if (auth_info && !con->http.tls &&
+           !httpAddrLocalhost(con->http.hostaddr))
+  {
+   /*
+    * Require encryption of auth-info over non-local connections...
+    */
+
+    send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
+    return (NULL);
+  }
+#endif /* HAVE_SSL */
+
+ /*
+  * See if the printer is accepting jobs...
+  */
+
+  if (!printer->accepting)
+  {
+    send_ipp_status(con, IPP_NOT_ACCEPTING,
+                    _("Destination \"%s\" is not accepting jobs."),
+                    printer->name);
+    return (NULL);
+  }
+
+ /*
+  * Validate job template attributes; for now just document-format,
+  * copies, number-up, and page-ranges...
+  */
+
+  if (filetype && printer->filetypes &&
+      !cupsArrayFind(printer->filetypes, filetype))
+  {
+    char       mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
+                                       /* MIME media type string */
+
+
+    snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
+             filetype->type);
+
+    send_ipp_status(con, IPP_DOCUMENT_FORMAT,
+                    _("Unsupported format \'%s\'!"), mimetype);
+
+    ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
+                 "document-format", NULL, mimetype);
+
+    return (NULL);
+  }
+
+  if ((attr = ippFindAttribute(con->request, "copies",
+                               IPP_TAG_INTEGER)) != NULL)
+  {
+    if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
+    {
+      send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
+                      attr->values[0].integer);
+      ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
+                   "copies", attr->values[0].integer);
+      return (NULL);
+    }
+  }
+
+  if ((attr = ippFindAttribute(con->request, "job-sheets",
+                               IPP_TAG_ZERO)) != NULL)
+  {
+    if (attr->value_tag != IPP_TAG_KEYWORD &&
+        attr->value_tag != IPP_TAG_NAME)
+    {
+      send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type!"));
+      return (NULL);
+    }
+
+    if (attr->num_values > 2)
+    {
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Too many job-sheets values (%d > 2)!"),
+                     attr->num_values);
+      return (NULL);
+    }
+
+    for (i = 0; i < attr->num_values; i ++)
+      if (strcmp(attr->values[i].string.text, "none") &&
+          !cupsdFindBanner(attr->values[i].string.text))
+      {
+       send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"!"),
+                       attr->values[i].string.text);
+       return (NULL);
+      }
+  }
+
+  if ((attr = ippFindAttribute(con->request, "number-up",
+                               IPP_TAG_INTEGER)) != NULL)
+  {
+    if (attr->values[0].integer != 1 &&
+        attr->values[0].integer != 2 &&
+        attr->values[0].integer != 4 &&
+        attr->values[0].integer != 6 &&
+        attr->values[0].integer != 9 &&
+        attr->values[0].integer != 16)
+    {
+      send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."),
+                      attr->values[0].integer);
+      ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
+                   "number-up", attr->values[0].integer);
+      return (NULL);
+    }
+  }
+
+  if ((attr = ippFindAttribute(con->request, "page-ranges",
+                               IPP_TAG_RANGE)) != NULL)
+  {
+    for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
+    {
+      if (attr->values[i].range.lower < lowerpagerange ||
+         attr->values[i].range.lower > attr->values[i].range.upper)
+      {
+       send_ipp_status(con, IPP_BAD_REQUEST,
+                       _("Bad page-ranges values %d-%d."),
+                       attr->values[i].range.lower,
+                       attr->values[i].range.upper);
+       return (NULL);
+      }
+
+      lowerpagerange = attr->values[i].range.upper + 1;
+    }
+  }
+
+ /*
+  * Make sure we aren't over our limit...
+  */
+
+  if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
+    cupsdCleanJobs();
+
+  if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
+  {
+    send_ipp_status(con, IPP_NOT_POSSIBLE,
+                    _("Too many active jobs."));
+    return (NULL);
+  }
+
+  if ((i = check_quotas(con, printer)) < 0)
+  {
+    send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
+    return (NULL);
+  }
+  else if (i == 0)
+  {
+    send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
+    return (NULL);
+  }
+
+ /*
+  * Create the job and set things up...
+  */
+
+  if ((attr = ippFindAttribute(con->request, "job-priority",
+                               IPP_TAG_INTEGER)) != NULL)
+    priority = attr->values[0].integer;
+  else
+  {
+    if ((val = cupsGetOption("job-priority", printer->num_options,
+                             printer->options)) != NULL)
+      priority = atoi(val);
+    else
+      priority = 50;
+
+    ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
+                  priority);
+  }
+
+  if ((attr = ippFindAttribute(con->request, "job-name",
+                               IPP_TAG_NAME)) != NULL)
+    title = attr->values[0].string.text;
+  else
+    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
+                 title = "Untitled");
+
+  if ((job = cupsdAddJob(priority, printer->name)) == NULL)
+  {
+    send_ipp_status(con, IPP_INTERNAL_ERROR,
+                    _("Unable to add job for destination \"%s\"!"),
+                   printer->name);
+    return (NULL);
+  }
+
+  job->dtype   = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
+                                  CUPS_PRINTER_REMOTE);
+  job->attrs   = con->request;
+  job->dirty   = 1;
+  con->request = ippNewRequest(job->attrs->request.op.operation_id);
+
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
+
+  add_job_uuid(con, 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);
+
+    if (attr)
+      cupsdSetString(&attr->values[0].string.text, con->username);
+  }
+  else if (attr)
+  {
+    cupsdLogMessage(CUPSD_LOG_DEBUG,
+                    "add_job: requesting-user-name=\"%s\"",
+                    attr->values[0].string.text);
+
+    cupsdSetString(&job->username, attr->values[0].string.text);
+  }
+  else
+    cupsdSetString(&job->username, "anonymous");
+
+  if (!attr)
+    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
+                 "job-originating-user-name", NULL, job->username);
+  else
+  {
+    attr->group_tag = IPP_TAG_JOB;
+    _cupsStrFree(attr->name);
+    attr->name = _cupsStrAlloc("job-originating-user-name");
+  }
+
+  if (con->username[0] || auth_info)
+  {
+    save_auth_info(con, job, auth_info);
+
+   /*
+    * Remove the auth-info attribute from the attribute data...
+    */
+
+    if (auth_info)
+      ippDeleteAttribute(job->attrs, auth_info);
+  }
+
+  if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
+                               IPP_TAG_ZERO)) != NULL)
+  {
+   /*
+    * Request contains a job-originating-host-name attribute; validate it...
+    */
+
+    if (attr->value_tag != IPP_TAG_NAME ||
+        attr->num_values != 1 ||
+        strcmp(con->http.hostname, "localhost"))
+    {
+     /*
+      * Can't override the value if we aren't connected via localhost.
+      * Also, we can only have 1 value and it must be a name value.
+      */
+
+      switch (attr->value_tag)
+      {
+        case IPP_TAG_STRING :
+       case IPP_TAG_TEXTLANG :
+       case IPP_TAG_NAMELANG :
+       case IPP_TAG_TEXT :
+       case IPP_TAG_NAME :
+       case IPP_TAG_KEYWORD :
+       case IPP_TAG_URI :
+       case IPP_TAG_URISCHEME :
+       case IPP_TAG_CHARSET :
+       case IPP_TAG_LANGUAGE :
+       case IPP_TAG_MIMETYPE :
+          /*
+           * Free old strings...
+           */
+
+           for (i = 0; i < attr->num_values; i ++)
+           {
+             _cupsStrFree(attr->values[i].string.text);
+             attr->values[i].string.text = NULL;
+             if (attr->values[i].string.charset)
+             {
+               _cupsStrFree(attr->values[i].string.charset);
+               attr->values[i].string.charset = NULL;
+             }
+            }
+
+       default :
+            break;
+      }
+
+     /*
+      * Use the default connection hostname instead...
+      */
+
+      attr->value_tag             = IPP_TAG_NAME;
+      attr->num_values            = 1;
+      attr->values[0].string.text = _cupsStrAlloc(con->http.hostname);
+    }
+
+    attr->group_tag = IPP_TAG_JOB;
+  }
+  else
+  {
+   /*
+    * No job-originating-host-name attribute, so use the hostname from
+    * the connection...
+    */
+
+    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
+                "job-originating-host-name", NULL, con->http.hostname);
+  }
+
+  ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation",
+                time(NULL));
+  attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
+                       "time-at-processing", 0);
+  attr->value_tag = IPP_TAG_NOVALUE;
+  attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
+                       "time-at-completed", 0);
+  attr->value_tag = IPP_TAG_NOVALUE;
+
+ /*
+  * Add remaining job attributes...
+  */
+
+  ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
+  job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
+                             "job-state", IPP_JOB_STOPPED);
+  job->state_value = (ipp_jstate_t)job->state->values[0].integer;
+  job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
+                              "job-media-sheets-completed", 0);
+  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
+               printer->uri);
+  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
+               title);
+
+  if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
+                               IPP_TAG_INTEGER)) != NULL)
+    attr->values[0].integer = 0;
+  else
+    ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", 0);
+
+  if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
+                               IPP_TAG_KEYWORD)) == NULL)
+    attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
+  if (!attr)
+  {
+    if ((val = cupsGetOption("job-hold-until", printer->num_options,
+                             printer->options)) == NULL)
+      val = "no-hold";
+
+    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") &&
+      !(printer->type & CUPS_PRINTER_REMOTE))
+  {
+   /*
+    * Hold job until specified time...
+    */
+
+    cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
+
+    job->state->values[0].integer = IPP_JOB_HELD;
+    job->state_value              = IPP_JOB_HELD;
+  }
+  else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB)
+  {
+    job->hold_until               = time(NULL) + MultipleOperationTimeout;
+    job->state->values[0].integer = IPP_JOB_HELD;
+    job->state_value              = IPP_JOB_HELD;
+  }
+  else
+  {
+    job->state->values[0].integer = IPP_JOB_PENDING;
+    job->state_value              = IPP_JOB_PENDING;
+  }
+
+  if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
+      Classification)
+  {
+   /*
+    * Add job sheets options...
+    */
+
+    if ((attr = ippFindAttribute(job->attrs, "job-sheets",
+                                 IPP_TAG_ZERO)) == NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                      "Adding default job-sheets values \"%s,%s\"...",
+                      printer->job_sheets[0], printer->job_sheets[1]);
+
+      attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
+                           2, NULL, NULL);
+      attr->values[0].string.text = _cupsStrRetain(printer->job_sheets[0]);
+      attr->values[1].string.text = _cupsStrRetain(printer->job_sheets[1]);
+    }
+
+    job->job_sheets = attr;
+
+   /*
+    * Enforce classification level if set...
+    */
+
+    if (Classification)
+    {
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                      "Classification=\"%s\", ClassifyOverride=%d",
+                      Classification ? Classification : "(null)",
+                     ClassifyOverride);
+
+      if (ClassifyOverride)
+      {
+        if (!strcmp(attr->values[0].string.text, "none") &&
+           (attr->num_values == 1 ||
+            !strcmp(attr->values[1].string.text, "none")))
+        {
+        /*
+          * Force the leading banner to have the classification on it...
+         */
+
+          cupsdSetString(&attr->values[0].string.text, Classification);
+
+         cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
+                                            "job-sheets=\"%s,none\", "
+                                            "job-originating-user-name=\"%s\"",
+                     Classification, job->username);
+       }
+       else if (attr->num_values == 2 &&
+                strcmp(attr->values[0].string.text,
+                       attr->values[1].string.text) &&
+                strcmp(attr->values[0].string.text, "none") &&
+                strcmp(attr->values[1].string.text, "none"))
+        {
+        /*
+         * Can't put two different security markings on the same document!
+         */
+
+          cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
+
+         cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
+                                            "job-sheets=\"%s,%s\", "
+                                            "job-originating-user-name=\"%s\"",
+                     attr->values[0].string.text,
+                     attr->values[1].string.text, job->username);
+       }
+       else if (strcmp(attr->values[0].string.text, Classification) &&
+                strcmp(attr->values[0].string.text, "none") &&
+                (attr->num_values == 1 ||
+                 (strcmp(attr->values[1].string.text, Classification) &&
+                  strcmp(attr->values[1].string.text, "none"))))
+        {
+         if (attr->num_values == 1)
+            cupsdLogJob(job, CUPSD_LOG_NOTICE,
+                       "CLASSIFICATION OVERRIDDEN "
+                       "job-sheets=\"%s\", "
+                       "job-originating-user-name=\"%s\"",
+                       attr->values[0].string.text, job->username);
+          else
+            cupsdLogJob(job, CUPSD_LOG_NOTICE,
+                       "CLASSIFICATION OVERRIDDEN "
+                       "job-sheets=\"%s,%s\",fffff "
+                       "job-originating-user-name=\"%s\"",
+                       attr->values[0].string.text,
+                       attr->values[1].string.text, job->username);
+        }
+      }
+      else if (strcmp(attr->values[0].string.text, Classification) &&
+               (attr->num_values == 1 ||
+              strcmp(attr->values[1].string.text, Classification)))
+      {
+       /*
+        * Force the banner to have the classification on it...
+       */
+
+        if (attr->num_values > 1 &&
+           !strcmp(attr->values[0].string.text, attr->values[1].string.text))
+       {
+          cupsdSetString(&(attr->values[0].string.text), Classification);
+          cupsdSetString(&(attr->values[1].string.text), Classification);
+       }
+        else
+       {
+          if (attr->num_values == 1 ||
+             strcmp(attr->values[0].string.text, "none"))
+            cupsdSetString(&(attr->values[0].string.text), Classification);
+
+          if (attr->num_values > 1 &&
+             strcmp(attr->values[1].string.text, "none"))
+            cupsdSetString(&(attr->values[1].string.text), Classification);
+        }
+
+        if (attr->num_values > 1)
+         cupsdLogJob(job, CUPSD_LOG_NOTICE,
+                     "CLASSIFICATION FORCED "
+                     "job-sheets=\"%s,%s\", "
+                     "job-originating-user-name=\"%s\"",
+                     attr->values[0].string.text,
+                     attr->values[1].string.text, job->username);
+        else
+         cupsdLogJob(job, CUPSD_LOG_NOTICE,
+                     "CLASSIFICATION FORCED "
+                     "job-sheets=\"%s\", "
+                     "job-originating-user-name=\"%s\"",
+                     Classification, job->username);
+      }
+    }
+
+   /*
+    * See if we need to add the starting sheet...
+    */
+
+    if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+    {
+      cupsdLogJob(job, CUPSD_LOG_INFO, "Adding start banner page \"%s\".",
+                 attr->values[0].string.text);
+
+      if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0)
+      {
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
+                        "Aborting job because the start banner could not be "
+                        "copied.");
+        return (NULL);
+      }
+
+      cupsdUpdateQuota(printer, job->username, 0, kbytes);
+    }
+  }
+  else if ((attr = ippFindAttribute(job->attrs, "job-sheets",
+                                    IPP_TAG_ZERO)) != NULL)
+    job->sheets = attr;
+
+ /*
+  * Fill in the response info...
+  */
+
+  snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
+          LocalPort, job->id);
+
+  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
+               job_uri);
+
+  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
+
+  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
+                job->state_value);
+  add_job_state_reasons(con, job);
+
+  con->response->request.status.status_code = IPP_OK;
+
+ /*
+  * Add any job subscriptions...
+  */
+
+  add_job_subscriptions(con, job);
+
+ /*
+  * Set all but the first two attributes to the job attributes group...
+  */
+
+  for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
+    attr->group_tag = IPP_TAG_JOB;
+
+ /*
+  * Fire the "job created" event...
+  */
+
+  cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
+
+ /*
+  * Return the new job...
+  */
+
+  return (job);
+}
+
+
+/*
+ * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
+ *                             upon the job and printer state...
+ */
+
+static void
+add_job_state_reasons(
+    cupsd_client_t *con,               /* I - Client connection */
+    cupsd_job_t    *job)               /* I - Job info */
+{
+  cupsd_printer_t      *dest;          /* Destination printer */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)",
+                  con, con->http.fd, job ? job->id : 0);
+
+  switch (job ? job->state_value : IPP_JOB_CANCELED)
+  {
+    case IPP_JOB_PENDING :
+       dest = cupsdFindDest(job->dest);
+
+        if (dest && dest->state == IPP_PRINTER_STOPPED)
+          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                      "job-state-reasons", NULL, "printer-stopped");
+        else
+          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                      "job-state-reasons", NULL, "none");
+        break;
+
+    case IPP_JOB_HELD :
+        if (ippFindAttribute(job->attrs, "job-hold-until",
+                            IPP_TAG_KEYWORD) != NULL ||
+           ippFindAttribute(job->attrs, "job-hold-until",
+                            IPP_TAG_NAME) != NULL)
+          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                      "job-state-reasons", NULL, "job-hold-until-specified");
+        else
+          ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                      "job-state-reasons", NULL, "job-incoming");
+        break;
+
+    case IPP_JOB_PROCESSING :
+        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                    "job-state-reasons", NULL, "job-printing");
+        break;
 
     case IPP_JOB_STOPPED :
         ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
                     "job-state-reasons", NULL, "job-stopped");
         break;
 
-    case IPP_JOB_CANCELLED :
-        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                    "job-state-reasons", NULL, "job-canceled-by-user");
-        break;
+    case IPP_JOB_CANCELED :
+        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                    "job-state-reasons", NULL, "job-canceled-by-user");
+        break;
+
+    case IPP_JOB_ABORTED :
+        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                    "job-state-reasons", NULL, "aborted-by-system");
+        break;
+
+    case IPP_JOB_COMPLETED :
+        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
+                    "job-state-reasons", NULL, "job-completed-successfully");
+        break;
+  }
+}
+
+
+/*
+ * 'add_job_subscriptions()' - Add any subscriptions for a job.
+ */
+
+static void
+add_job_subscriptions(
+    cupsd_client_t *con,               /* I - Client connection */
+    cupsd_job_t    *job)               /* I - Newly created job */
+{
+  int                  i;              /* Looping var */
+  ipp_attribute_t      *prev,          /* Previous attribute */
+                       *next,          /* Next attribute */
+                       *attr;          /* Current attribute */
+  cupsd_subscription_t *sub;           /* Subscription object */
+  const char           *recipient,     /* notify-recipient-uri */
+                       *pullmethod;    /* notify-pull-method */
+  ipp_attribute_t      *user_data;     /* notify-user-data */
+  int                  interval;       /* notify-time-interval */
+  unsigned             mask;           /* notify-events */
+
+
+ /*
+  * Find the first subscription group attribute; return if we have
+  * none...
+  */
+
+  for (attr = job->attrs->attrs; attr; attr = attr->next)
+    if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
+      break;
+
+  if (!attr)
+    return;
+
+ /*
+  * Process the subscription attributes in the request...
+  */
+
+  while (attr)
+  {
+    recipient = NULL;
+    pullmethod = NULL;
+    user_data  = NULL;
+    interval   = 0;
+    mask       = CUPSD_EVENT_NONE;
+
+    while (attr && attr->group_tag != IPP_TAG_ZERO)
+    {
+      if (!strcmp(attr->name, "notify-recipient-uri") &&
+          attr->value_tag == IPP_TAG_URI)
+      {
+       /*
+        * Validate the recipient scheme against the ServerBin/notifier
+       * directory...
+       */
+
+       char    notifier[1024],         /* Notifier filename */
+               scheme[HTTP_MAX_URI],   /* Scheme portion of URI */
+               userpass[HTTP_MAX_URI], /* Username portion of URI */
+               host[HTTP_MAX_URI],     /* Host portion of URI */
+               resource[HTTP_MAX_URI]; /* Resource portion of URI */
+        int    port;                   /* Port portion of URI */
+
+
+        recipient = attr->values[0].string.text;
+
+       if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
+                           scheme, sizeof(scheme), userpass, sizeof(userpass),
+                           host, sizeof(host), &port,
+                           resource, sizeof(resource)) < HTTP_URI_OK)
+        {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Bad notify-recipient-uri URI \"%s\"!"), recipient);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_URI_SCHEME);
+         return;
+       }
+
+        snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
+                scheme);
+        if (access(notifier, X_OK))
+       {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("notify-recipient-uri URI \"%s\" uses unknown "
+                           "scheme!"), recipient);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_URI_SCHEME);
+         return;
+       }
+
+        if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
+       {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("notify-recipient-uri URI \"%s\" is already used!"),
+                         recipient);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_ATTRIBUTES);
+         return;
+       }
+      }
+      else if (!strcmp(attr->name, "notify-pull-method") &&
+               attr->value_tag == IPP_TAG_KEYWORD)
+      {
+        pullmethod = attr->values[0].string.text;
+
+        if (strcmp(pullmethod, "ippget"))
+       {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Bad notify-pull-method \"%s\"!"), pullmethod);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_ATTRIBUTES);
+         return;
+       }
+      }
+      else if (!strcmp(attr->name, "notify-charset") &&
+               attr->value_tag == IPP_TAG_CHARSET &&
+              strcmp(attr->values[0].string.text, "us-ascii") &&
+              strcmp(attr->values[0].string.text, "utf-8"))
+      {
+        send_ipp_status(con, IPP_CHARSET,
+                       _("Character set \"%s\" not supported!"),
+                       attr->values[0].string.text);
+       return;
+      }
+      else if (!strcmp(attr->name, "notify-natural-language") &&
+               (attr->value_tag != IPP_TAG_LANGUAGE ||
+               strcmp(attr->values[0].string.text, DefaultLanguage)))
+      {
+        send_ipp_status(con, IPP_CHARSET,
+                       _("Language \"%s\" not supported!"),
+                       attr->values[0].string.text);
+       return;
+      }
+      else if (!strcmp(attr->name, "notify-user-data") &&
+               attr->value_tag == IPP_TAG_STRING)
+      {
+        if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
+       {
+          send_ipp_status(con, IPP_REQUEST_VALUE,
+                         _("The notify-user-data value is too large "
+                           "(%d > 63 octets)!"),
+                         attr->values[0].unknown.length);
+         return;
+       }
+
+        user_data = attr;
+      }
+      else if (!strcmp(attr->name, "notify-events") &&
+               attr->value_tag == IPP_TAG_KEYWORD)
+      {
+        for (i = 0; i < attr->num_values; i ++)
+         mask |= cupsdEventValue(attr->values[i].string.text);
+      }
+      else if (!strcmp(attr->name, "notify-lease-duration"))
+      {
+        send_ipp_status(con, IPP_BAD_REQUEST,
+                       _("The notify-lease-duration attribute cannot be "
+                         "used with job subscriptions."));
+       return;
+      }
+      else if (!strcmp(attr->name, "notify-time-interval") &&
+               attr->value_tag == IPP_TAG_INTEGER)
+        interval = attr->values[0].integer;
+
+      attr = attr->next;
+    }
+
+    if (!recipient && !pullmethod)
+      break;
+
+    if (mask == CUPSD_EVENT_NONE)
+      mask = CUPSD_EVENT_JOB_COMPLETED;
+
+    if ((sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job,
+                                    recipient, 0)) != NULL)
+    {
+      sub->interval = interval;
+
+      cupsdSetString(&sub->owner, job->username);
+
+      if (user_data)
+      {
+       sub->user_data_len = user_data->values[0].unknown.length;
+       memcpy(sub->user_data, user_data->values[0].unknown.data,
+              sub->user_data_len);
+      }
+
+      ippAddSeparator(con->response);
+      ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                   "notify-subscription-id", sub->id);
+    }
+
+    if (attr)
+      attr = attr->next;
+  }
+
+  cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
+
+ /*
+  * Remove all of the subscription attributes from the job request...
+  *
+  * TODO: Optimize this since subscription groups have to come at the
+  * end of the request...
+  */
+
+  for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
+  {
+    next = attr->next;
+
+    if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
+        attr->group_tag == IPP_TAG_ZERO)
+    {
+     /*
+      * Free and remove this attribute...
+      */
+
+      _ippFreeAttr(attr);
+
+      if (prev)
+        prev->next = next;
+      else
+        job->attrs->attrs = next;
+    }
+    else
+      prev = attr;
+  }
+
+  job->attrs->last    = prev;
+  job->attrs->current = prev;
+}
+
+
+/*
+ * 'add_job_uuid()' - Add job-uuid attribute to a job.
+ *
+ * See RFC 4122 for the definition of UUIDs and the format.
+ */
+
+static void
+add_job_uuid(cupsd_client_t *con,      /* I - Client connection */
+             cupsd_job_t    *job)      /* I - Job */
+{
+  char                 uuid[1024];     /* job-uuid string */
+  _cups_md5_state_t    md5state;       /* MD5 state */
+  unsigned char                md5sum[16];     /* MD5 digest/sum */
+
+
+ /*
+  * First see if the job already has a job-uuid attribute; if so, return...
+  */
+
+  if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
+    return;
+
+ /*
+  * No job-uuid attribute, so build a version 3 UUID with the local job
+  * ID at the end; see RFC 4122 for details.  Start with the MD5 sum of
+  * the ServerName, server name and port that the client connected to,
+  * and local job ID...
+  */
+
+  snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername,
+          con->serverport, job->id);
+
+  _cupsMD5Init(&md5state);
+  _cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid));
+  _cupsMD5Finish(&md5state, md5sum);
+
+ /*
+  * Format the UUID URI using the MD5 sum and job ID.
+  */
+
+  snprintf(uuid, sizeof(uuid),
+           "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
+          "%02x%02x%02x%02x%02x%02x",
+          md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5],
+          (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40,
+          md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13],
+          md5sum[14], md5sum[15]);
+
+  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid);
+}
+
+
+/*
+ * 'add_printer()' - Add a printer to the system.
+ */
+
+static void
+add_printer(cupsd_client_t  *con,      /* I - Client connection */
+            ipp_attribute_t *uri)      /* I - URI of printer */
+{
+  http_status_t        status;                 /* Policy status */
+  int          i;                      /* Looping var */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
+               username[HTTP_MAX_URI], /* Username portion of URI */
+               host[HTTP_MAX_URI],     /* Host portion of URI */
+               resource[HTTP_MAX_URI]; /* Resource portion of URI */
+  int          port;                   /* Port portion of URI */
+  cupsd_printer_t *printer;            /* Printer/class */
+  ipp_attribute_t *attr;               /* Printer attribute */
+  cups_file_t  *fp;                    /* Script/PPD file */
+  char         line[1024];             /* Line from file... */
+  char         srcfile[1024],          /* Source Script/PPD file */
+               dstfile[1024];          /* Destination Script/PPD file */
+  int          modify;                 /* Non-zero if we are modifying */
+  char         newname[IPP_MAX_NAME];  /* New printer name */
+  int          need_restart_job;       /* Need to restart job? */
+  int          set_device_uri,         /* Did we set the device URI? */
+               set_port_monitor;       /* Did we set the port monitor? */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
+
+ /*
+  * Do we have a valid URI?
+  */
+
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                  sizeof(scheme), username, sizeof(username), host,
+                 sizeof(host), &port, resource, sizeof(resource));
+
+  if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
+  {
+   /*
+    * No, return an error...
+    */
+
+    send_ipp_status(con, IPP_BAD_REQUEST,
+                    _("The printer-uri must be of the form "
+                     "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
+    return;
+  }
+
+ /*
+  * Do we have a valid printer name?
+  */
+
+  if (!validate_name(resource + 10))
+  {
+   /*
+    * No, return an error...
+    */
+
+    send_ipp_status(con, IPP_BAD_REQUEST,
+                    _("The printer-uri \"%s\" contains invalid characters."),
+                   uri->values[0].string.text);
+    return;
+  }
 
-    case IPP_JOB_ABORTED :
-        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                    "job-state-reasons", NULL, "aborted-by-system");
-        break;
+ /*
+  * See if the printer already exists; if not, create a new printer...
+  */
 
-    case IPP_JOB_COMPLETED :
-        ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                    "job-state-reasons", NULL, "job-completed-successfully");
-        break;
+  if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
+  {
+   /*
+    * Printer doesn't exist; see if we have a class of the same name...
+    */
+
+    if ((printer = cupsdFindClass(resource + 10)) != NULL &&
+        !(printer->type & CUPS_PRINTER_DISCOVERED))
+    {
+     /*
+      * Yes, return an error...
+      */
+
+      send_ipp_status(con, IPP_NOT_POSSIBLE,
+                      _("A class named \"%s\" already exists!"),
+                     resource + 10);
+      return;
+    }
+
+   /*
+    * No, check the default policy then add the printer...
+    */
+
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, NULL);
+      return;
+    }
+
+    printer = cupsdAddPrinter(resource + 10);
+    modify  = 0;
   }
-}
+  else if (printer->type & CUPS_PRINTER_IMPLICIT)
+  {
+   /*
+    * Check the default policy, then rename the implicit printer to
+    * "AnyPrinter" or delete it...
+    */
 
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, NULL);
+      return;
+    }
 
-/*
- * 'add_job_subscriptions()' - Add any subcriptions for a job.
- */
+    if (ImplicitAnyClasses)
+    {
+      snprintf(newname, sizeof(newname), "Any%s", resource + 10);
+      cupsdRenamePrinter(printer, newname);
+    }
+    else
+      cupsdDeletePrinter(printer, 1);
+
+   /*
+    * Add the printer as a new local printer...
+    */
+
+    printer = cupsdAddPrinter(resource + 10);
+    modify  = 0;
+  }
+  else if (printer->type & CUPS_PRINTER_DISCOVERED)
+  {
+   /*
+    * Check the default policy, then rename the remote printer to
+    * "Printer@server"...
+    */
+
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, NULL);
+      return;
+    }
+
+    snprintf(newname, sizeof(newname), "%s@%s", resource + 10,
+             printer->hostname);
+    cupsdRenamePrinter(printer, newname);
+
+   /*
+    * Add the printer as a new local printer...
+    */
+
+    printer = cupsdAddPrinter(resource + 10);
+    modify  = 0;
+  }
+  else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
+                                      NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, printer);
+    return;
+  }
+  else
+    modify = 1;
+
+ /*
+  * Look for attributes and copy them over as needed...
+  */
+
+  need_restart_job = 0;
+
+  if ((attr = ippFindAttribute(con->request, "printer-location",
+                               IPP_TAG_TEXT)) != NULL)
+    cupsdSetString(&printer->location, attr->values[0].string.text);
+
+  if ((attr = ippFindAttribute(con->request, "printer-info",
+                               IPP_TAG_TEXT)) != NULL)
+    cupsdSetString(&printer->info, attr->values[0].string.text);
+
+  set_device_uri = 0;
+
+  if ((attr = ippFindAttribute(con->request, "device-uri",
+                               IPP_TAG_URI)) != NULL)
+  {
+   /*
+    * Do we have a valid device URI?
+    */
+
+    http_uri_status_t  uri_status;     /* URI separation status */
+    char               old_device_uri[1024];
+                                       /* Old device URI */
+
+
+    need_restart_job = 1;
+
+    uri_status = httpSeparateURI(HTTP_URI_CODING_ALL,
+                                attr->values[0].string.text,
+                                scheme, sizeof(scheme),
+                                username, sizeof(username),
+                                host, sizeof(host), &port,
+                                resource, sizeof(resource));
+
+    if (uri_status < HTTP_URI_OK)
+    {
+      send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"),
+                     attr->values[0].string.text);
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                      "add_printer: httpSeparateURI returned %d", uri_status);
+      return;
+    }
+
+    if (!strcmp(scheme, "file"))
+    {
+     /*
+      * See if the administrator has enabled file devices...
+      */
+
+      if (!FileDevice && strcmp(resource, "/dev/null"))
+      {
+       /*
+        * File devices are disabled and the URL is not file:/dev/null...
+       */
+
+       send_ipp_status(con, IPP_NOT_POSSIBLE,
+                       _("File device URIs have been disabled! "
+                         "To enable, see the FileDevice directive in "
+                         "\"%s/cupsd.conf\"."),
+                       ServerRoot);
+       return;
+      }
+    }
+    else
+    {
+     /*
+      * See if the backend exists and is executable...
+      */
+
+      snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
+      if (access(srcfile, X_OK))
+      {
+       /*
+        * Could not find device in list!
+       */
+
+       send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri scheme \"%s\"!"),
+                       scheme);
+       return;
+      }
+    }
+
+    if (printer->sanitized_device_uri)
+      strlcpy(old_device_uri, printer->sanitized_device_uri,
+              sizeof(old_device_uri));
+    else
+      old_device_uri[0] = '\0';
+
+    cupsdSetDeviceURI(printer, attr->values[0].string.text);
+
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Setting %s device-uri to \"%s\" (was \"%s\".)",
+                   printer->name, printer->sanitized_device_uri,
+                   old_device_uri);
+
+    set_device_uri = 1;
+  }
+
+  set_port_monitor = 0;
+
+  if ((attr = ippFindAttribute(con->request, "port-monitor",
+                               IPP_TAG_NAME)) != NULL)
+  {
+    ipp_attribute_t    *supported;     /* port-monitor-supported attribute */
+
+
+    need_restart_job = 1;
+
+    supported = ippFindAttribute(printer->ppd_attrs, "port-monitor-supported",
+                                 IPP_TAG_NAME);
+    if (supported)
+    {
+      for (i = 0; i < supported->num_values; i ++)
+        if (!strcmp(supported->values[i].string.text,
+                    attr->values[0].string.text))
+          break;
+    }
+
+    if (!supported || i >= supported->num_values)
+    {
+      send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"),
+                     attr->values[0].string.text);
+      return;
+    }
+
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Setting %s port-monitor to \"%s\" (was \"%s\".)",
+                    printer->name, attr->values[0].string.text,
+                   printer->port_monitor ? printer->port_monitor : "none");
+
+    if (strcmp(attr->values[0].string.text, "none"))
+      cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
+    else
+      cupsdClearString(&printer->port_monitor);
+
+    set_port_monitor = 1;
+  }
+
+  if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
+                               IPP_TAG_BOOLEAN)) != NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Setting %s printer-is-accepting-jobs to %d (was %d.)",
+                    printer->name, attr->values[0].boolean, printer->accepting);
+
+    printer->accepting = attr->values[0].boolean;
+    cupsdAddPrinterHistory(printer);
+  }
+
+  if ((attr = ippFindAttribute(con->request, "printer-is-shared",
+                               IPP_TAG_BOOLEAN)) != NULL)
+  {
+    if (printer->shared && !attr->values[0].boolean)
+      cupsdDeregisterPrinter(printer, 1);
+
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Setting %s printer-is-shared to %d (was %d.)",
+                    printer->name, attr->values[0].boolean, printer->shared);
+
+    printer->shared = attr->values[0].boolean;
+  }
+
+  if ((attr = ippFindAttribute(con->request, "printer-state",
+                               IPP_TAG_ENUM)) != NULL)
+  {
+    if (attr->values[0].integer != IPP_PRINTER_IDLE &&
+        attr->values[0].integer != IPP_PRINTER_STOPPED)
+    {
+      send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"),
+                      attr->values[0].integer);
+      return;
+    }
+
+    cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
+                    printer->name, attr->values[0].integer, printer->state);
+
+    if (attr->values[0].integer == IPP_PRINTER_STOPPED)
+      cupsdStopPrinter(printer, 0);
+    else
+    {
+      need_restart_job = 1;
+      cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
+    }
+  }
+
+  if ((attr = ippFindAttribute(con->request, "printer-state-message",
+                               IPP_TAG_TEXT)) != NULL)
+  {
+    strlcpy(printer->state_message, attr->values[0].string.text,
+            sizeof(printer->state_message));
+    cupsdAddPrinterHistory(printer);
+  }
+
+  if ((attr = ippFindAttribute(con->request, "printer-state-reasons",
+                               IPP_TAG_KEYWORD)) != NULL)
+  {
+    if (attr->num_values >
+            (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0])))
+    {
+      send_ipp_status(con, IPP_NOT_POSSIBLE,
+                      _("Too many printer-state-reasons values (%d > %d)!"),
+                     attr->num_values,
+                     (int)(sizeof(printer->reasons) /
+                           sizeof(printer->reasons[0])));
+      return;
+    }
+
+    for (i = 0; i < printer->num_reasons; i ++)
+      _cupsStrFree(printer->reasons[i]);
+
+    printer->num_reasons = 0;
+    for (i = 0; i < attr->num_values; i ++)
+    {
+      if (!strcmp(attr->values[i].string.text, "none"))
+        continue;
+
+      printer->reasons[printer->num_reasons] =
+          _cupsStrRetain(attr->values[i].string.text);
+      printer->num_reasons ++;
+
+      if (!strcmp(attr->values[i].string.text, "paused") &&
+          printer->state != IPP_PRINTER_STOPPED)
+      {
+       cupsdLogMessage(CUPSD_LOG_INFO,
+                       "Setting %s printer-state to %d (was %d.)",
+                       printer->name, IPP_PRINTER_STOPPED, printer->state);
+       cupsdStopPrinter(printer, 0);
+      }
+    }
+
+    if (PrintcapFormat == PRINTCAP_PLIST)
+      cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
+  }
 
-static void
-add_job_subscriptions(
-    cupsd_client_t *con,               /* I - Client connection */
-    cupsd_job_t    *job)               /* I - Newly created job */
-{
-  int                  i;              /* Looping var */
-  ipp_attribute_t      *prev,          /* Previous attribute */
-                       *next,          /* Next attribute */
-                       *attr;          /* Current attribute */
-  cupsd_subscription_t *sub;           /* Subscription object */
-  const char           *recipient,     /* notify-recipient-uri */
-                       *pullmethod;    /* notify-pull-method */
-  ipp_attribute_t      *user_data;     /* notify-user-data */
-  int                  interval;       /* notify-time-interval */
-  unsigned             mask;           /* notify-events */
+  set_printer_defaults(con, printer);
 
+  if ((attr = ippFindAttribute(con->request, "auth-info-required",
+                               IPP_TAG_KEYWORD)) != NULL)
+    cupsdSetAuthInfoRequired(printer, NULL, attr);
 
  /*
-  * Find the first subscription group attribute; return if we have
-  * none...
+  * See if we have all required attributes...
   */
 
-  for (attr = job->attrs->attrs, prev = NULL;
-       attr;
-       prev = attr, attr = attr->next)
-    if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
-      break;
-
-  if (!attr)
-    return;
+  if (!printer->device_uri)
+    cupsdSetString(&printer->device_uri, "file:///dev/null");
 
  /*
-  * Process the subscription attributes in the request...
+  * See if we have an interface script or PPD file attached to the request...
   */
 
-  while (attr)
+  if (con->filename)
   {
-    recipient = NULL;
-    pullmethod = NULL;
-    user_data  = NULL;
-    interval   = 0;
-    mask       = CUPSD_EVENT_NONE;
+    need_restart_job = 1;
 
-    while (attr && attr->group_tag != IPP_TAG_ZERO)
+    strlcpy(srcfile, con->filename, sizeof(srcfile));
+
+    if ((fp = cupsFileOpen(srcfile, "rb")))
     {
-      if (!strcmp(attr->name, "notify-recipient") &&
-          attr->value_tag == IPP_TAG_URI)
-        recipient = attr->values[0].string.text;
-      else if (!strcmp(attr->name, "notify-pull-method") &&
-               attr->value_tag == IPP_TAG_KEYWORD)
-        pullmethod = attr->values[0].string.text;
-      else if (!strcmp(attr->name, "notify-charset") &&
-               attr->value_tag == IPP_TAG_CHARSET &&
-              strcmp(attr->values[0].string.text, "us-ascii") &&
-              strcmp(attr->values[0].string.text, "utf-8"))
-      {
-        send_ipp_status(con, IPP_CHARSET,
-                       _("Character set \"%s\" not supported!"),
-                       attr->values[0].string.text);
-       return;
-      }
-      else if (!strcmp(attr->name, "notify-natural-language") &&
-               (attr->value_tag != IPP_TAG_LANGUAGE ||
-               strcmp(attr->values[0].string.text, DefaultLanguage)))
+     /*
+      * Yes; get the first line from it...
+      */
+
+      line[0] = '\0';
+      cupsFileGets(fp, line, sizeof(line));
+      cupsFileClose(fp);
+
+     /*
+      * Then see what kind of file it is...
+      */
+
+      snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
+               printer->name);
+
+      if (!strncmp(line, "*PPD-Adobe", 10))
       {
-        send_ipp_status(con, IPP_CHARSET,
-                       _("Language \"%s\" not supported!"),
-                       attr->values[0].string.text);
-       return;
+       /*
+       * The new file is a PPD file, so remove any old interface script
+       * that might be lying around...
+       */
+
+       unlink(dstfile);
       }
-      else if (!strcmp(attr->name, "notify-user-data") &&
-               attr->value_tag == IPP_TAG_STRING)
+      else
       {
-        if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
+       /*
+       * This must be an interface script, so move the file over to the
+       * interfaces directory and make it executable...
+       */
+
+       if (copy_file(srcfile, dstfile))
        {
-          send_ipp_status(con, IPP_REQUEST_VALUE,
-                         _("The notify-user-data value is too large "
-                           "(%d > 63 octets)!"),
-                         attr->values[0].unknown.length);
+          send_ipp_status(con, IPP_INTERNAL_ERROR,
+                         _("Unable to copy interface script - %s!"),
+                         strerror(errno));
          return;
        }
 
-        user_data = attr;
-      }
-      else if (!strcmp(attr->name, "notify-events") &&
-               attr->value_tag == IPP_TAG_KEYWORD)
-      {
-        for (i = 0; i < attr->num_values; i ++)
-         mask |= cupsdEventValue(attr->values[i].string.text);
-      }
-      else if (!strcmp(attr->name, "notify-lease-duration"))
-      {
-        send_ipp_status(con, IPP_BAD_REQUEST,
-                       _("The notify-lease-duration attribute cannot be "
-                         "used with job subscriptions."));
-       return;
+       cupsdLogMessage(CUPSD_LOG_DEBUG,
+                       "Copied interface script successfully!");
+       chmod(dstfile, 0755);
       }
-      else if (!strcmp(attr->name, "notify-time-interval") &&
-               attr->value_tag == IPP_TAG_INTEGER)
-        interval = attr->values[0].integer;
 
-      attr = attr->next;
-    }
+      snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
+               printer->name);
 
-    if (!recipient && !pullmethod)
-      break;
+      if (!strncmp(line, "*PPD-Adobe", 10))
+      {
+       /*
+       * The new file is a PPD file, so move the file over to the
+       * ppd directory and make it readable by all...
+       */
 
-    if (mask == CUPSD_EVENT_NONE)
-      mask = CUPSD_EVENT_JOB_COMPLETED;
+       if (copy_file(srcfile, dstfile))
+       {
+          send_ipp_status(con, IPP_INTERNAL_ERROR,
+                         _("Unable to copy PPD file - %s!"),
+                         strerror(errno));
+         return;
+       }
 
-    sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient,
-                               0);
+       cupsdLogMessage(CUPSD_LOG_DEBUG,
+                       "Copied PPD file successfully!");
+       chmod(dstfile, 0644);
 
-    sub->interval = interval;
+#ifdef __APPLE__
+       /*
+        * (Re)register color profiles...
+       */
 
-    cupsdSetString(&sub->owner, job->username);
+        if (!RunUser)
+       {
+         apple_unregister_profiles(printer);
+         apple_register_profiles(printer);
+        }
+#endif /* __APPLE__ */
+      }
+      else
+      {
+       /*
+       * This must be an interface script, so remove any old PPD file that
+       * may be lying around...
+       */
 
-    if (user_data)
-    {
-      sub->user_data_len = user_data->values[0].unknown.length;
-      memcpy(sub->user_data, user_data->values[0].unknown.data,
-             sub->user_data_len);
+       unlink(dstfile);
+      }
     }
-
-    ippAddSeparator(con->response);
-    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                  "notify-subscription-id", sub->id);
-
-    if (attr)
-      attr = attr->next;
   }
-
-  cupsdSaveAllSubscriptions();
-
- /*
-  * Remove all of the subscription attributes from the job request...
-  */
-
-  for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
+  else if ((attr = ippFindAttribute(con->request, "ppd-name",
+                                    IPP_TAG_NAME)) != NULL)
   {
-    next = attr->next;
+    need_restart_job = 1;
 
-    if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
-        attr->group_tag == IPP_TAG_ZERO)
+    if (!strcmp(attr->values[0].string.text, "raw"))
     {
      /*
-      * Free and remove this attribute...
+      * Raw driver, remove any existing PPD or interface script files.
       */
 
-      _ipp_free_attr(attr);
+      snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
+               printer->name);
+      unlink(dstfile);
 
-      if (prev)
-        prev->next = next;
-      else
-        job->attrs->attrs = next;
+      snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
+               printer->name);
+      unlink(dstfile);
     }
     else
-      prev = attr;
-  }
-
-  job->attrs->last    = prev;
-  job->attrs->current = prev;
-}
-
-
-/*
- * 'add_printer()' - Add a printer to the system.
- */
-
-static void
-add_printer(cupsd_client_t  *con,      /* I - Client connection */
-            ipp_attribute_t *uri)      /* I - URI of printer */
-{
-  http_status_t        status;                 /* Policy status */
-  int          i;                      /* Looping var */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
-  cupsd_printer_t *printer;            /* Printer/class */
-  ipp_attribute_t *attr;               /* Printer attribute */
-  cups_file_t  *fp;                    /* Script/PPD file */
-  char         line[1024];             /* Line from file... */
-  char         srcfile[1024],          /* Source Script/PPD file */
-               dstfile[1024];          /* Destination Script/PPD file */
-  int          modify;                 /* Non-zero if we are modifying */
-
-
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
-                  con->http.fd, uri->values[0].string.text);
-
- /*
-  * Do we have a valid URI?
-  */
-
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+    {
+     /*
+      * PPD model file...
+      */
 
-  if (strncmp(resource, "/printers/", 10) != 0 || strlen(resource) == 10)
-  {
-   /*
-    * No, return an error...
-    */
+      snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
+               printer->name);
+      unlink(dstfile);
 
-    send_ipp_status(con, IPP_BAD_REQUEST,
-                    _("The printer-uri must be of the form "
-                     "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
-    return;
-  }
+      snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
+               printer->name);
 
- /*
-  * Do we have a valid printer name?
-  */
+      if (copy_model(con, attr->values[0].string.text, dstfile))
+      {
+        send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!"));
+       return;
+      }
 
-  if (!validate_name(resource + 10))
-  {
-   /*
-    * No, return an error...
-    */
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                     "Copied PPD file successfully!");
+      chmod(dstfile, 0644);
 
-    send_ipp_status(con, IPP_BAD_REQUEST,
-                    _("The printer-uri \"%s\" contains invalid characters."),
-                   uri->values[0].string.text);
-    return;
+#ifdef __APPLE__
+     /*
+      * (Re)register color profiles...
+      */
+
+      if (!RunUser)
+      {
+       apple_unregister_profiles(printer);
+       apple_register_profiles(printer);
+      }
+#endif /* __APPLE__ */
+    }
   }
 
  /*
-  * Check policy...
+  * If we set the device URI but not the port monitor, check which port
+  * monitor to use by default...
   */
 
-  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+  if (set_device_uri && !set_port_monitor)
   {
-    send_http_error(con, status);
-    return;
-  }
+    ppd_file_t *ppd;                   /* PPD file */
+    ppd_attr_t *ppdattr;               /* cupsPortMonitor attribute */
 
- /*
-  * See if the printer already exists; if not, create a new printer...
-  */
 
-  if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
-  {
-   /*
-    * Printer doesn't exist; see if we have a class of the same name...
-    */
+    httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
-    if ((printer = cupsdFindClass(resource + 10)) != NULL &&
-        !(printer->type & CUPS_PRINTER_REMOTE))
+    snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
+            printer->name);
+    if ((ppd = ppdOpenFile(srcfile)) != NULL)
     {
-     /*
-      * Yes, return an error...
-      */
+      for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
+          ppdattr;
+          ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
+        if (!strcmp(scheme, ppdattr->spec))
+       {
+         cupsdLogMessage(CUPSD_LOG_INFO,
+                         "Setting %s port-monitor to \"%s\" (was \"%s\".)",
+                         printer->name, ppdattr->value,
+                         printer->port_monitor ? printer->port_monitor
+                                               : "none");
 
-      send_ipp_status(con, IPP_NOT_POSSIBLE,
-                      _("A class named \"%s\" already exists!"),
-                     resource + 10);
-      return;
-    }
+         if (strcmp(ppdattr->value, "none"))
+           cupsdSetString(&printer->port_monitor, ppdattr->value);
+         else
+           cupsdClearString(&printer->port_monitor);
 
-   /*
-    * No, add the printer...
-    */
+         break;
+       }
 
-    printer = cupsdAddPrinter(resource + 10);
-    modify  = 0;
+      ppdClose(ppd);
+    }
   }
-  else if (printer->type & CUPS_PRINTER_IMPLICIT)
-  {
-   /*
-    * Rename the implicit printer to "AnyPrinter" or delete it...
-    */
 
-    if (ImplicitAnyClasses)
-    {
-      cupsArrayRemove(Printers, printer);
-      cupsdSetStringf(&printer->name, "Any%s", resource + 10);
-      cupsArrayAdd(Printers, printer);
-    }
-    else
-      cupsdDeletePrinter(printer, 1);
+ /*
+  * Update the printer attributes and return...
+  */
 
-   /*
-    * Add the printer as a new local printer...
-    */
+  cupsdSetPrinterAttrs(printer);
+  cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
 
-    printer = cupsdAddPrinter(resource + 10);
-    modify  = 0;
-  }
-  else if (printer->type & CUPS_PRINTER_REMOTE)
+  if (need_restart_job && printer->job)
   {
    /*
-    * Rename the remote printer to "Printer@server"...
-    */
-
-    cupsdDeletePrinterFilters(printer);
-    cupsArrayRemove(Printers, printer);
-    cupsdSetStringf(&printer->name, "%s@%s", resource + 10, printer->hostname);
-    cupsdSetPrinterAttrs(printer);
-    cupsArrayAdd(Printers, printer);
-
-   /*
-    * Add the printer as a new local printer...
+    * Restart the current job...
     */
 
-    printer = cupsdAddPrinter(resource + 10);
-    modify  = 0;
+    cupsdSetJobState(printer->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
+                     "Job restarted because the printer was modified.");
   }
-  else
-    modify = 1;
-
- /*
-  * Look for attributes and copy them over as needed...
-  */
 
-  if ((attr = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT)) != NULL)
-    cupsdSetString(&printer->location, attr->values[0].string.text);
+  cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
 
-  if ((attr = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT)) != NULL)
-    cupsdSetString(&printer->info, attr->values[0].string.text);
+  if (modify)
+  {
+    cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL,
+                  "Printer \"%s\" modified by \"%s\".", printer->name,
+                 get_username(con));
 
-  if ((attr = ippFindAttribute(con->request, "device-uri", IPP_TAG_URI)) != NULL)
+    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
+                    printer->name, get_username(con));
+  }
+  else
   {
-   /*
-    * Do we have a valid device URI?
-    */
+    cupsdAddPrinterHistory(printer);
 
-    httpSeparateURI(attr->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL,
+                  "New printer \"%s\" added by \"%s\".", printer->name,
+                 get_username(con));
 
-    if (!strcmp(method, "file"))
-    {
-     /*
-      * See if the administrator has enabled file devices...
-      */
+    cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
+                    printer->name, get_username(con));
+  }
 
-      if (!FileDevice && strcmp(resource, "/dev/null"))
-      {
-       /*
-        * File devices are disabled and the URL is not file:/dev/null...
-       */
+  con->response->request.status.status_code = IPP_OK;
+}
 
-       send_ipp_status(con, IPP_NOT_POSSIBLE,
-                       _("File device URIs have been disabled! "
-                         "To enable, see the FileDevice directive in "
-                         "\"%s/cupsd.conf\"."),
-                       ServerRoot);
-       return;
-      }
-    }
-    else
-    {
-     /*
-      * See if the backend exists and is executable...
-      */
 
-      snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, method);
-      if (access(srcfile, X_OK))
-      {
-       /*
-        * Could not find device in list!
-       */
+/*
+ * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
+ *                                 based upon the printer state...
+ */
 
-       send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"),
-                       attr->values[0].string.text);
-       return;
-      }
-    }
+static void
+add_printer_state_reasons(
+    cupsd_client_t  *con,              /* I - Client connection */
+    cupsd_printer_t *p)                        /* I - Printer info */
+{
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "add_printer_state_reasons(%p[%d], %p[%s])",
+                  con, con->http.fd, p, p->name);
 
-    cupsdLogMessage(CUPSD_LOG_INFO,
-                    "Setting %s device-uri to \"%s\" (was \"%s\".)",
-                   printer->name,
-                   cupsdSanitizeURI(attr->values[0].string.text, line,
-                                    sizeof(line)),
-                   cupsdSanitizeURI(printer->device_uri, resource,
-                                    sizeof(resource)));
+  if (p->num_reasons == 0)
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
+                 "printer-state-reasons", NULL, "none");
+  else
+    ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
+                  "printer-state-reasons", p->num_reasons, NULL,
+                 (const char * const *)p->reasons);
+}
 
-    cupsdSetString(&printer->device_uri, attr->values[0].string.text);
-  }
 
-  if ((attr = ippFindAttribute(con->request, "port-monitor", IPP_TAG_KEYWORD)) != NULL)
-  {
-    ipp_attribute_t    *supported;     /* port-monitor-supported attribute */
+/*
+ * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
+ *                            the specified printer or class.
+ */
 
+static void
+add_queued_job_count(
+    cupsd_client_t  *con,              /* I - Client connection */
+    cupsd_printer_t *p)                        /* I - Printer or class */
+{
+  int          count;                  /* Number of jobs on destination */
 
-    supported = ippFindAttribute(printer->attrs, "port-monitor-supported",
-                                 IPP_TAG_KEYWORD);
-    for (i = 0; i < supported->num_values; i ++)
-      if (!strcmp(supported->values[i].string.text,
-                  attr->values[0].string.text))
-        break;
 
-    if (i >= supported->num_values)
-    {
-      send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"),
-                     attr->values[0].string.text);
-      return;
-    }
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
+                  con, con->http.fd, p, p->name);
 
-    cupsdLogMessage(CUPSD_LOG_INFO,
-                    "Setting %s port-monitor to \"%s\" (was \"%s\".)",
-                    printer->name, attr->values[0].string.text,
-                   printer->port_monitor);
+  count = cupsdGetPrinterJobCount(p->name);
 
-    if (strcmp(attr->values[0].string.text, "none"))
-      cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
-    else
-      cupsdClearString(&printer->port_monitor);
-  }
+  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
+                "queued-job-count", count);
+}
 
-  if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs", IPP_TAG_BOOLEAN)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_INFO,
-                    "Setting %s printer-is-accepting-jobs to %d (was %d.)",
-                    printer->name, attr->values[0].boolean, printer->accepting);
 
-    printer->accepting = attr->values[0].boolean;
-    cupsdAddPrinterHistory(printer);
-  }
+#ifdef __APPLE__
+/*
+ * 'apple_init_profile()' - Initialize a color profile.
+ */
 
-  if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL)
-  {
-    if (printer->shared && !attr->values[0].boolean)
-      cupsdSendBrowseDelete(printer);
+static void
+apple_init_profile(
+    ppd_file_t          *ppd,          /* I - PPD file */
+    cups_array_t       *languages,     /* I - Languages in the PPD file */
+    CMDeviceProfileInfo *profile,      /* I - Profile record */
+    unsigned            id,            /* I - Profile ID */
+    const char          *name,         /* I - Profile name */
+    const char          *text,         /* I - Profile UI text */
+    const char          *iccfile)      /* I - ICC filename */
+{
+  char                 url[1024];      /* URL for profile filename */
+  CFMutableDictionaryRef dict;         /* Dictionary for name */
+  char                 *language;      /* Current language */
+  ppd_attr_t           *attr;          /* Profile attribute */
+  CFStringRef          cflang,         /* Language string */
+                       cftext;         /* Localized text */
 
-    cupsdLogMessage(CUPSD_LOG_INFO,
-                    "Setting %s printer-is-shared to %d (was %d.)",
-                    printer->name, attr->values[0].boolean, printer->shared);
 
-    printer->shared = attr->values[0].boolean;
-  }
+ /*
+  * Build the profile name dictionary...
+  */
 
-  if ((attr = ippFindAttribute(con->request, "printer-state", IPP_TAG_ENUM)) != NULL)
-  {
-    if (attr->values[0].integer != IPP_PRINTER_IDLE &&
-        attr->values[0].integer != IPP_PRINTER_STOPPED)
-    {
-      send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"),
-                      attr->values[0].integer);
-      return;
-    }
+  dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
+                                  &kCFTypeDictionaryKeyCallBacks,
+                                  &kCFTypeDictionaryValueCallBacks);
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)", printer->name,
-               attr->values[0].integer, printer->state);
+  cftext = CFStringCreateWithCString(kCFAllocatorDefault, text,
+                                    kCFStringEncodingUTF8);
 
-    if (attr->values[0].integer == IPP_PRINTER_STOPPED)
-      cupsdStopPrinter(printer, 0);
-    else
-      cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
-  }
-  if ((attr = ippFindAttribute(con->request, "printer-state-message", IPP_TAG_TEXT)) != NULL)
-  {
-    strlcpy(printer->state_message, attr->values[0].string.text,
-            sizeof(printer->state_message));
-    cupsdAddPrinterHistory(printer);
-  }
-  if ((attr = ippFindAttribute(con->request, "job-sheets-default", IPP_TAG_ZERO)) != NULL &&
-      !Classification)
-  {
-    cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
-    if (attr->num_values > 1)
-      cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
-    else
-      cupsdSetString(&printer->job_sheets[1], "none");
-  }
-  if ((attr = ippFindAttribute(con->request, "requesting-user-name-allowed",
-                               IPP_TAG_ZERO)) != NULL)
+  if (cftext)
   {
-    cupsdFreePrinterUsers(printer);
-
-    printer->deny_users = 0;
-    if (attr->value_tag == IPP_TAG_NAME &&
-        (attr->num_values > 1 ||
-        strcmp(attr->values[0].string.text, "all") != 0))
-      for (i = 0; i < attr->num_values; i ++)
-       cupsdAddPrinterUser(printer, attr->values[i].string.text);
+    CFDictionarySetValue(dict, CFSTR("en"), cftext);
+    CFRelease(cftext);
   }
-  else if ((attr = ippFindAttribute(con->request, "requesting-user-name-denied",
-                                    IPP_TAG_ZERO)) != NULL)
-  {
-    cupsdFreePrinterUsers(printer);
 
-    printer->deny_users = 1;
-    if (attr->value_tag == IPP_TAG_NAME &&
-        (attr->num_values > 1 ||
-        strcmp(attr->values[0].string.text, "none") != 0))
-      for (i = 0; i < attr->num_values; i ++)
-       cupsdAddPrinterUser(printer, attr->values[i].string.text);
-  }
-  if ((attr = ippFindAttribute(con->request, "job-quota-period",
-                               IPP_TAG_INTEGER)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
-               attr->values[0].integer);
-    cupsdFreeQuotas(printer);
-    printer->quota_period = attr->values[0].integer;
-  }
-  if ((attr = ippFindAttribute(con->request, "job-k-limit",
-                               IPP_TAG_INTEGER)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
-               attr->values[0].integer);
-    cupsdFreeQuotas(printer);
-    printer->k_limit = attr->values[0].integer;
-  }
-  if ((attr = ippFindAttribute(con->request, "job-page-limit",
-                               IPP_TAG_INTEGER)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
-               attr->values[0].integer);
-    cupsdFreeQuotas(printer);
-    printer->page_limit = attr->values[0].integer;
-  }
-  if ((attr = ippFindAttribute(con->request, "printer-op-policy",
-                               IPP_TAG_NAME)) != NULL)
+  if (languages)
   {
-    cupsd_policy_t *p;                 /* Policy */
+   /*
+    * Find localized names for the color profiles...
+    */
 
+    cupsArraySave(ppd->sorted_attrs);
 
-    if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
-    {
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "Setting printer-op-policy to \"%s\"...",
-                      attr->values[0].string.text);
-      cupsdSetString(&printer->op_policy, attr->values[0].string.text);
-      printer->op_policy_ptr = p;
-    }
-    else
-    {
-      send_ipp_status(con, IPP_NOT_POSSIBLE,
-                      _("Unknown printer-op-policy \"%s\"."),
-                      attr->values[0].string.text);
-      return;
-    }
-  }
-  if ((attr = ippFindAttribute(con->request, "printer-error-policy",
-                               IPP_TAG_NAME)) != NULL)
-  {
-    if (strcmp(attr->values[0].string.text, "abort-job") &&
-        strcmp(attr->values[0].string.text, "retry-job") &&
-        strcmp(attr->values[0].string.text, "stop-printer"))
+    for (language = (char *)cupsArrayFirst(languages);
+        language;
+        language = (char *)cupsArrayNext(languages))
     {
-      send_ipp_status(con, IPP_NOT_POSSIBLE,
-                      _("Unknown printer-error-policy \"%s\"."),
-                      attr->values[0].string.text);
-      return;
+      if (iccfile)
+      {
+        if ((attr = _ppdLocalizedAttr(ppd, "cupsICCProfile", name,
+                                     language)) == NULL)
+         attr = _ppdLocalizedAttr(ppd, "APTiogaProfile", name, language);
+      }
+      else
+        attr = _ppdLocalizedAttr(ppd, "ColorModel", name, language);
+
+      if (attr && attr->text[0])
+      {
+       cflang = CFStringCreateWithCString(kCFAllocatorDefault, language,
+                                          kCFStringEncodingUTF8);
+       cftext = CFStringCreateWithCString(kCFAllocatorDefault, attr->text,
+                                          kCFStringEncodingUTF8);
+
+        if (cflang && cftext)
+         CFDictionarySetValue(dict, cflang, cftext);
+
+        if (cflang)
+         CFRelease(cflang);
+
+        if (cftext)
+         CFRelease(cftext);
+      }
     }
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "Setting printer-error-policy to \"%s\"...",
-                    attr->values[0].string.text);
-    cupsdSetString(&printer->error_policy, attr->values[0].string.text);
+    cupsArrayRestore(ppd->sorted_attrs);
   }
 
  /*
-  * See if we have all required attributes...
+  * Fill in the profile data...
   */
 
-  if (!printer->device_uri)
-    cupsdSetString(&printer->device_uri, "file:/dev/null");
+  if (iccfile)
+    httpAssembleURI(HTTP_URI_CODING_ALL, url, sizeof(url), "file", NULL, "", 0,
+                   iccfile);
+
+  profile->dataVersion        = cmDeviceProfileInfoVersion1;
+  profile->profileID          = id;
+  profile->profileLoc.locType = iccfile ? cmPathBasedProfile : cmNoProfileBase;
+  profile->profileName        = dict;
+
+  if (iccfile)
+    strlcpy(profile->profileLoc.u.pathLoc.path, iccfile,
+           sizeof(profile->profileLoc.u.pathLoc.path));
+}
+
+
+/*
+ * 'apple_register_profiles()' - Register color profiles for a printer.
+ */
+
+static void
+apple_register_profiles(
+    cupsd_printer_t *p)                        /* I - Printer */
+{
+  int                  i;              /* Looping var */
+  char                 ppdfile[1024],  /* PPD filename */
+                       iccfile[1024],  /* ICC filename */
+                       selector[PPD_MAX_NAME];
+                                       /* Profile selection string */
+  ppd_file_t           *ppd;           /* PPD file */
+  ppd_attr_t           *attr,          /* Profile attributes */
+                       *profileid_attr,/* cupsProfileID attribute */
+                       *q1_attr,       /* ColorModel (or other) qualifier */
+                       *q2_attr,       /* MediaType (or other) qualifier */
+                       *q3_attr;       /* Resolution (or other) qualifier */
+  char                 q_keyword[PPD_MAX_NAME];
+                                       /* Qualifier keyword */
+  const char           *q1_choice,     /* ColorModel (or other) choice */
+                       *q2_choice,     /* MediaType (or other) choice */
+                       *q3_choice;     /* Resolution (or other) choice */
+  const char           *profile_key;   /* Profile keyword */
+  ppd_option_t         *cm_option;     /* Color model option */
+  ppd_choice_t         *cm_choice;     /* Color model choice */
+  int                  num_profiles;   /* Number of profiles */
+  CMError              error;          /* Last error */
+  unsigned             device_id,      /* Printer device ID */
+                       profile_id,     /* Profile ID */
+                       default_profile_id = 0;
+                                       /* Default profile ID */
+  CFMutableDictionaryRef device_name;  /* Printer device name dictionary */
+  CFStringRef          printer_name;   /* Printer name string */
+  CMDeviceScope                scope =         /* Scope of the registration */
+                       {
+                         kCFPreferencesAnyUser,
+                         kCFPreferencesCurrentHost
+                       };
+  CMDeviceProfileArrayPtr profiles;    /* Profiles */
+  CMDeviceProfileInfo  *profile;       /* Current profile */
+  cups_array_t         *languages;     /* Languages array */
+
+
+ /*
+  * Make sure ColorSync is available...
+  */
+
+  if (CMRegisterColorDevice == NULL)
+    return;
 
  /*
-  * See if we have an interface script or PPD file attached to the request...
+  * Try opening the PPD file for this printer...
   */
 
-  if (con->filename)
+  snprintf(ppdfile, sizeof(ppdfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
+  if ((ppd = ppdOpenFile(ppdfile)) == NULL)
+    return;
+
+ /*
+  * See if we have any profiles...
+  */
+
+  if ((attr = ppdFindAttr(ppd, "APTiogaProfile", NULL)) != NULL)
+    profile_key = "APTiogaProfile";
+  else
   {
-    strlcpy(srcfile, con->filename, sizeof(srcfile));
+    attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
+    profile_key = "cupsICCProfile";
+  }
+
+  for (num_profiles = 0; attr; attr = ppdFindNextAttr(ppd, profile_key, NULL))
+    if (attr->spec[0] && attr->value && attr->value[0])
+    {
+      if (attr->value[0] != '/')
+       snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
+                attr->value);
+      else
+       strlcpy(iccfile, attr->value, sizeof(iccfile));
+
+      if (access(iccfile, 0))
+       continue;
+
+      num_profiles ++;
+    }
+
+  
+ /*
+  * If we have profiles, add them...
+  */
 
-    if ((fp = cupsFileOpen(srcfile, "rb")) != NULL)
+  if (num_profiles > 0)
+  {
+    if (profile_key[0] == 'A')
     {
      /*
-      * Yes; get the first line from it...
+      * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
+      * attribute...
       */
 
-      line[0] = '\0';
-      cupsFileGets(fp, line, sizeof(line));
-      cupsFileClose(fp);
+      if ((attr = ppdFindAttr(ppd, "DefaultAPTiogaProfile", NULL)) != NULL &&
+         attr->value)
+        default_profile_id = atoi(attr->value);
 
+      q1_choice = q2_choice = q3_choice = NULL;
+    }
+    else
+    {
      /*
-      * Then see what kind of file it is...
+      * For CUPS PPDs, figure out the default profile selector values...
       */
 
-      snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
-               printer->name);
+      if ((attr = ppdFindAttr(ppd, "cupsICCQualifier1", NULL)) != NULL &&
+         attr->value && attr->value[0])
+      {
+       snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
+       q1_attr = ppdFindAttr(ppd, q_keyword, NULL);
+      }
+      else if ((q1_attr = ppdFindAttr(ppd, "DefaultColorModel", NULL)) == NULL)
+       q1_attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL);
 
-      if (!strncmp(line, "*PPD-Adobe", 10))
+      if (q1_attr && q1_attr->value && q1_attr->value[0])
+       q1_choice = q1_attr->value;
+      else
+       q1_choice = "";
+
+      if ((attr = ppdFindAttr(ppd, "cupsICCQualifier2", NULL)) != NULL &&
+         attr->value && attr->value[0])
       {
-       /*
-       * The new file is a PPD file, so remove any old interface script
-       * that might be lying around...
-       */
+       snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
+       q2_attr = ppdFindAttr(ppd, q_keyword, NULL);
+      }
+      else 
+       q2_attr = ppdFindAttr(ppd, "DefaultMediaType", NULL);
 
-       unlink(dstfile);
+      if (q2_attr && q2_attr->value && q2_attr->value[0])
+       q2_choice = q2_attr->value;
+      else
+       q2_choice = NULL;
+
+      if ((attr = ppdFindAttr(ppd, "cupsICCQualifier3", NULL)) != NULL &&
+         attr->value && attr->value[0])
+      {
+       snprintf(q_keyword, sizeof(q_keyword), "Default%s", attr->value);
+       q3_attr = ppdFindAttr(ppd, q_keyword, NULL);
       }
+      else 
+       q3_attr = ppdFindAttr(ppd, "DefaultResolution", NULL);
+
+      if (q3_attr && q3_attr->value && q3_attr->value[0])
+       q3_choice = q3_attr->value;
       else
+       q3_choice = NULL;
+    }
+
+   /*
+    * Build the array of profiles...
+    *
+    * Note: This calloc actually requests slightly more memory than needed.
+    */
+
+    if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+                      "Unable to allocate memory for %d profiles!",
+                     num_profiles);
+      ppdClose(ppd);
+      return;
+    }
+
+    profiles->profileCount = num_profiles;
+    languages              = _ppdGetLanguages(ppd);
+
+    for (profile = profiles->profiles,
+             attr = ppdFindAttr(ppd, profile_key, NULL);
+        attr;
+        attr = ppdFindNextAttr(ppd, profile_key, NULL))
+      if (attr->spec[0] && attr->value && attr->value[0])
       {
        /*
-       * This must be an interface script, so move the file over to the
-       * interfaces directory and make it executable...
+        * Add this profile...
        */
 
-       if (copy_file(srcfile, dstfile))
+        if (attr->value[0] != '/')
+         snprintf(iccfile, sizeof(iccfile), "%s/profiles/%s", DataDir,
+                  attr->value);
+        else
+         strlcpy(iccfile, attr->value, sizeof(iccfile));
+
+        if (access(iccfile, 0))
+         continue;
+
+        if (profile_key[0] == 'c')
        {
-          send_ipp_status(con, IPP_INTERNAL_ERROR,
-                         _("Unable to copy interface script - %s!"),
-                         strerror(errno));
-         return;
-       }
+         cupsArraySave(ppd->sorted_attrs);
+
+         if ((profileid_attr = ppdFindAttr(ppd, "cupsProfileID",
+                                           attr->spec)) != NULL &&
+             profileid_attr->value && isdigit(profileid_attr->value[0] & 255))
+           profile_id = (unsigned)strtoul(profileid_attr->value, NULL, 10);
+         else
+           profile_id = _ppdHashName(attr->spec);
+
+         cupsArrayRestore(ppd->sorted_attrs);
+        }
        else
-       {
-          cupsdLogMessage(CUPSD_LOG_DEBUG,
-                         "Copied interface script successfully!");
-          chmod(dstfile, 0755);
-       }
-      }
+         profile_id = atoi(attr->spec);
 
-      snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
-               printer->name);
+        apple_init_profile(ppd, languages, profile, profile_id, attr->spec,
+                          attr->text[0] ? attr->text : attr->spec, iccfile);
+
+       profile ++;
 
-      if (!strncmp(line, "*PPD-Adobe", 10))
-      {
        /*
-       * The new file is a PPD file, so move the file over to the
-       * ppd directory and make it readable by all...
+        * See if this is the default profile...
        */
 
-       if (copy_file(srcfile, dstfile))
-       {
-          send_ipp_status(con, IPP_INTERNAL_ERROR,
-                         _("Unable to copy PPD file - %s!"),
-                         strerror(errno));
-         return;
-       }
-       else
+        if (!default_profile_id)
        {
-          cupsdLogMessage(CUPSD_LOG_DEBUG,
-                         "Copied PPD file successfully!");
-          chmod(dstfile, 0644);
+         if (q2_choice)
+         {
+           if (q3_choice)
+           {
+             snprintf(selector, sizeof(selector), "%s.%s.%s",
+                      q1_choice, q2_choice, q3_choice);
+              if (!strcmp(selector, attr->spec))
+               default_profile_id = profile_id;
+            }
+
+            if (!default_profile_id)
+           {
+             snprintf(selector, sizeof(selector), "%s.%s.", q1_choice,
+                      q2_choice);
+              if (!strcmp(selector, attr->spec))
+               default_profile_id = profile_id;
+           }
+          }
+
+          if (!default_profile_id && q3_choice)
+         {
+           snprintf(selector, sizeof(selector), "%s..%s", q1_choice,
+                    q3_choice);
+           if (!strcmp(selector, attr->spec))
+             default_profile_id = profile_id;
+         }
+
+          if (!default_profile_id)
+         {
+           snprintf(selector, sizeof(selector), "%s..", q1_choice);
+           if (!strcmp(selector, attr->spec))
+             default_profile_id = profile_id;
+         }
        }
       }
-      else
-      {
-       /*
-       * This must be an interface script, so remove any old PPD file that
-       * may be lying around...
-       */
 
-       unlink(dstfile);
-      }
-    }
+    _ppdFreeLanguages(languages);
   }
-  else if ((attr = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME)) != NULL)
+  else if ((cm_option = ppdFindOption(ppd, "ColorModel")) != NULL)
   {
-    if (!strcmp(attr->values[0].string.text, "raw"))
-    {
-     /*
-      * Raw driver, remove any existing PPD or interface script files.
-      */
+   /*
+    * Extract profiles from ColorModel option...
+    */
 
-      snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
-               printer->name);
-      unlink(dstfile);
+    const char *profile_name;          /* Name of generic profile */
 
-      snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
-               printer->name);
-      unlink(dstfile);
+
+    num_profiles = cm_option->num_choices;
+
+    if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+                      "Unable to allocate memory for %d profiles!",
+                     num_profiles);
+      ppdClose(ppd);
+      return;
     }
-    else
+
+    profiles->profileCount = num_profiles;
+
+    for (profile = profiles->profiles, i = cm_option->num_choices,
+             cm_choice = cm_option->choices;
+         i > 0;
+        i --, cm_choice ++, profile ++)
     {
-     /*
-      * PPD model file...
-      */
+      if (!strcmp(cm_choice->choice, "Gray") ||
+          !strcmp(cm_choice->choice, "Black"))
+        profile_name = "Gray";
+      else if (!strcmp(cm_choice->choice, "RGB") ||
+               !strcmp(cm_choice->choice, "CMY"))
+        profile_name = "RGB";
+      else if (!strcmp(cm_choice->choice, "CMYK") ||
+               !strcmp(cm_choice->choice, "KCMY"))
+        profile_name = "CMYK";
+      else
+        profile_name = "DeviceN";
 
-      snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
-               printer->name);
-      unlink(dstfile);
+      snprintf(selector, sizeof(selector), "%s..", profile_name);
+      profile_id = _ppdHashName(selector);
 
-      snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
-               printer->name);
+      apple_init_profile(ppd, NULL, profile, profile_id, cm_choice->choice,
+                         cm_choice->text, NULL);
 
-      if (copy_model(con, attr->values[0].string.text, dstfile))
-      {
-        send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!"));
-       return;
-      }
-      else
-      {
-        cupsdLogMessage(CUPSD_LOG_DEBUG,
-                       "Copied PPD file successfully!");
-        chmod(dstfile, 0644);
-      }
+      if (cm_choice->marked)
+        default_profile_id = profile_id;
     }
   }
+  else
+  {
+   /*
+    * Use the default colorspace...
+    */
 
- /*
-  * Make this printer the default if there is none...
-  */
+    attr = ppdFindAttr(ppd, "DefaultColorSpace", NULL);
 
-  if (DefaultPrinter == NULL)
-    DefaultPrinter = printer;
+    num_profiles = (attr && ppd->colorspace == PPD_CS_GRAY) ? 1 : 2;
+      
+    if ((profiles = calloc(num_profiles, sizeof(CMDeviceProfileArray))) == NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+                      "Unable to allocate memory for %d profiles!",
+                     num_profiles);
+      ppdClose(ppd);
+      return;
+    }
 
- /*
-  * Update the printer attributes and return...
-  */
+    profiles->profileCount = num_profiles;
 
-  cupsdSetPrinterAttrs(printer);
-  cupsdSaveAllPrinters();
+    apple_init_profile(ppd, NULL, profiles->profiles, _ppdHashName("Gray.."),
+                       "Gray", "Gray", NULL);
+
+    switch (ppd->colorspace)
+    {
+      case PPD_CS_RGB :
+      case PPD_CS_CMY :
+          apple_init_profile(ppd, NULL, profiles->profiles + 1,
+                            _ppdHashName("RGB.."), "RGB", "RGB", NULL);
+          break;
+      case PPD_CS_RGBK :
+      case PPD_CS_CMYK :
+          apple_init_profile(ppd, NULL, profiles->profiles + 1,
+                            _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL);
+          break;
+
+      case PPD_CS_GRAY :
+          if (attr)
+           break;
+
+      case PPD_CS_N :
+          apple_init_profile(ppd, NULL, profiles->profiles + 1,
+                            _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
+                            NULL);
+          break;
+    }
+  }
 
-  if (printer->job != NULL)
+  if (num_profiles > 0)
   {
-    cupsd_job_t *job;
+   /*
+    * Make sure we have a default profile ID...
+    */
+
+    if (!default_profile_id)
+      default_profile_id = profiles->profiles[num_profiles - 1].profileID;
 
    /*
-    * Stop the current job and then restart it below...
+    * Get the device ID hash and pathelogical name dictionary.
     */
 
-    job = (cupsd_job_t *)printer->job;
+    cupsdLogMessage(CUPSD_LOG_INFO, "Registering ICC color profiles for \"%s\"",
+                   p->name);
 
-    cupsdStopJob(job, 1);
-    job->state->values[0].integer = IPP_JOB_PENDING;
-  }
+    device_id    = _ppdHashName(p->name);
+    device_name  = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
+                                            &kCFTypeDictionaryKeyCallBacks,
+                                            &kCFTypeDictionaryValueCallBacks);
+    printer_name = CFStringCreateWithCString(kCFAllocatorDefault,
+                                             p->name, kCFStringEncodingUTF8);
 
-  cupsdCheckJobs();
+    if (device_name && printer_name)
+    {
+      CFDictionarySetValue(device_name, CFSTR("en"), printer_name);
 
-  cupsdWritePrintcap();
+     /*
+      * Register the device with ColorSync...
+      */
 
-  if (modify)
-  {
-    cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL,
-                  "Printer \"%s\" modified by \"%s\".", printer->name,
-                 con->username);
+      error = CMRegisterColorDevice(cmPrinterDeviceClass, device_id,
+                                    device_name, &scope);
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
-                    printer->name, con->username);
-  }
-  else
-  {
-    cupsdAddPrinterHistory(printer);
+     /*
+      * Register the profiles...
+      */
 
-    cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL,
-                  "New printer \"%s\" added by \"%s\".", printer->name,
-                 con->username);
+      if (error == noErr)
+       error = CMSetDeviceFactoryProfiles(cmPrinterDeviceClass, device_id,
+                                          default_profile_id, profiles);
+    }
+    else
+      error = 1000;
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
-                    printer->name, con->username);
+   /*
+    * Clean up...
+    */
+
+    if (error != noErr)
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+                     "Unable to register ICC color profiles for \"%s\" - %d",
+                     p->name, (int)error);
+
+    for (profile = profiles->profiles;
+        num_profiles > 0;
+        profile ++, num_profiles --)
+      CFRelease(profile->profileName);
+
+    free(profiles);
+
+    if (printer_name)
+      CFRelease(printer_name);
+
+    if (device_name)
+      CFRelease(device_name);
   }
 
-  con->response->request.status.status_code = IPP_OK;
+  ppdClose(ppd);
 }
 
 
 /*
- * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
- *                                 based upon the printer state...
+ * 'apple_unregister_profiles()' - Remove color profiles for the specified
+ *                                 printer.
  */
 
 static void
-add_printer_state_reasons(
-    cupsd_client_t  *con,              /* I - Client connection */
-    cupsd_printer_t *p)                        /* I - Printer info */
+apple_unregister_profiles(
+    cupsd_printer_t *p)                        /* I - Printer */
 {
-  cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "add_printer_state_reasons(%p[%d], %p[%s])",
-                  con, con->http.fd, p, p->name);
+ /*
+  * Make sure ColorSync is available...
+  */
 
-  if (p->num_reasons == 0)
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
-                 "printer-state-reasons", NULL,
-                p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
-  else
-    ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
-                  "printer-state-reasons", p->num_reasons, NULL,
-                 (const char * const *)p->reasons);
+  if (CMUnregisterColorDevice != NULL)
+    CMUnregisterColorDevice(cmPrinterDeviceClass, _ppdHashName(p->name));
 }
-
+#endif /* __APPLE__ */
 
 /*
- * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
- *                            the specified printer or class.
+ * 'apply_printer_defaults()' - Apply printer default options to a job.
  */
 
 static void
-add_queued_job_count(
-    cupsd_client_t  *con,              /* I - Client connection */
-    cupsd_printer_t *p)                        /* I - Printer or class */
+apply_printer_defaults(
+    cupsd_printer_t *printer,          /* I - Printer */
+    cupsd_job_t     *job)              /* I - Job */
 {
-  int          count;                  /* Number of jobs on destination */
+  int          i,                      /* Looping var */
+               num_options;            /* Number of default options */
+  cups_option_t        *options,               /* Default options */
+               *option;                /* Current option */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
-                  con, con->http.fd, p, p->name);
+ /*
+  * Collect all of the default options and add the missing ones to the
+  * job object...
+  */
+
+  for (i = printer->num_options, num_options = 0, options = NULL,
+           option = printer->options;
+       i > 0;
+       i --, option ++)
+    if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
+    {
+      num_options = cupsAddOption(option->name, option->value, num_options,
+                                  &options);
+    }
 
-  count = cupsdGetPrinterJobCount(p->name);
+ /*
+  * Encode these options as attributes in the job object...
+  */
 
-  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                "queued-job-count", count);
+  cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB);
+  cupsFreeOptions(num_options, options);
 }
 
 
@@ -2046,10 +3598,11 @@ static void
 authenticate_job(cupsd_client_t  *con, /* I - Client connection */
                 ipp_attribute_t *uri)  /* I - Job URI */
 {
-  ipp_attribute_t      *attr;          /* Job-id attribute */
+  ipp_attribute_t      *attr,          /* job-id attribute */
+                       *auth_info;     /* auth-info attribute */
   int                  jobid;          /* Job ID */
   cupsd_job_t          *job;           /* Current job */
-  char                 method[HTTP_MAX_URI],
+  char                 scheme[HTTP_MAX_URI],
                                        /* Method portion of URI */
                        username[HTTP_MAX_URI],
                                        /* Username portion of URI */
@@ -2079,7 +3632,8 @@ authenticate_job(cupsd_client_t  *con,    /* I - Client connection */
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -2094,10 +3648,10 @@ authenticate_job(cupsd_client_t  *con,  /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
+
     if (strncmp(resource, "/jobs/", 6))
     {
      /*
@@ -2131,7 +3685,7 @@ authenticate_job(cupsd_client_t  *con,    /* I - Client connection */
   * See if the job has been completed...
   */
 
-  if (job->state->values[0].integer != IPP_JOB_HELD)
+  if (job->state_value != IPP_JOB_HELD)
   {
    /*
     * Return a "not-possible" error...
@@ -2147,10 +3701,26 @@ authenticate_job(cupsd_client_t  *con,  /* I - Client connection */
   * See if we have already authenticated...
   */
 
-  if (!con->username[0])
+  auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
+
+  if (!con->username[0] && !auth_info)
   {
-    send_ipp_status(con, IPP_NOT_AUTHORIZED,
-                    _("No authentication information provided!"));
+    cupsd_printer_t    *printer;       /* Job destination */
+
+
+   /*
+    * No auth data.  If we need to authenticate via Kerberos, send a
+    * HTTP auth challenge, otherwise just return an IPP error...
+    */
+
+    printer = cupsdFindDest(job->dest);
+
+    if (printer && printer->num_auth_info_required > 0 &&
+        !strcmp(printer->auth_info_required[0], "negotiate"))
+      send_http_error(con, HTTP_UNAUTHORIZED, printer);
+    else
+      send_ipp_status(con, IPP_NOT_AUTHORIZED,
+                     _("No authentication information provided!"));
     return;
   }
 
@@ -2160,10 +3730,7 @@ authenticate_job(cupsd_client_t  *con,   /* I - Client connection */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to authenticate "
-                     "job #%d owned by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
@@ -2171,16 +3738,17 @@ authenticate_job(cupsd_client_t  *con,  /* I - Client connection */
   * Save the authentication information for this job...
   */
 
-  save_auth_info(con, job);
+  save_auth_info(con, job, auth_info);
 
  /*
   * Reset the job-hold-until value to "no-hold"...
   */
 
-  if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
+  if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
+                               IPP_TAG_KEYWORD)) == NULL)
     attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
 
-  if (attr != NULL)
+  if (attr)
   {
     attr->value_tag = IPP_TAG_KEYWORD;
     cupsdSetString(&(attr->values[0].string.text), "no-hold");
@@ -2192,8 +3760,7 @@ authenticate_job(cupsd_client_t  *con,    /* I - Client connection */
 
   cupsdReleaseJob(job);
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was authenticated by \"%s\".", jobid,
-                  con->username);
+  cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username);
 }
 
 
@@ -2206,11 +3773,10 @@ cancel_all_jobs(cupsd_client_t  *con,   /* I - Client connection */
                ipp_attribute_t *uri)   /* I - Job or Printer URI */
 {
   http_status_t        status;                 /* Policy status */
-  const char   *dest;                  /* Destination */
   cups_ptype_t dtype;                  /* Destination type */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  char         scheme[HTTP_MAX_URI],   /* Scheme portion of URI */
                userpass[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
+               hostname[HTTP_MAX_URI], /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
   int          port;                   /* Port portion of URI */
   ipp_attribute_t *attr;               /* Attribute in request */
@@ -2238,10 +3804,12 @@ cancel_all_jobs(cupsd_client_t  *con,   /* I - Client connection */
   * "my-jobs" is specified...
   */
 
-  if ((attr = ippFindAttribute(con->request, "my-jobs", IPP_TAG_BOOLEAN)) != NULL &&
+  if ((attr = ippFindAttribute(con->request, "my-jobs",
+                               IPP_TAG_BOOLEAN)) != NULL &&
       attr->values[0].boolean)
   {
-    if ((attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME)) != NULL)
+    if ((attr = ippFindAttribute(con->request, "requesting-user-name",
+                                 IPP_TAG_NAME)) != NULL)
       username = attr->values[0].string.text;
     else
     {
@@ -2257,7 +3825,8 @@ cancel_all_jobs(cupsd_client_t  *con,     /* I - Client connection */
   * Look for the "purge-jobs" attribute...
   */
 
-  if ((attr = ippFindAttribute(con->request, "purge-jobs", IPP_TAG_BOOLEAN)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "purge-jobs",
+                               IPP_TAG_BOOLEAN)) != NULL)
     purge = attr->values[0].boolean;
   else
     purge = 1;
@@ -2266,30 +3835,24 @@ cancel_all_jobs(cupsd_client_t  *con,   /* I - Client connection */
   * And if the destination is valid...
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  userpass, sizeof(userpass), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI?
     */
 
-    if (!strncmp(resource, "/printers/", 10) ||
-        !strncmp(resource, "/classes/", 9))
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text,
+                    scheme, sizeof(scheme), userpass, sizeof(userpass),
+                   hostname, sizeof(hostname), &port,
+                   resource, sizeof(resource));
+
+    if ((!strncmp(resource, "/printers/", 10) && resource[10]) ||
+        (!strncmp(resource, "/classes/", 9) && resource[9]))
     {
       send_ipp_status(con, IPP_NOT_FOUND,
                       _("The printer or class was not found."));
       return;
     }
-    else if (strcmp(resource, "/printers/"))
-    {
-      send_ipp_status(con, IPP_NOT_FOUND,
-                      _("The printer-uri \"%s\" is not valid."),
-                     uri->values[0].string.text);
-      return;
-    }
 
    /*
     * Check policy...
@@ -2297,7 +3860,7 @@ cancel_all_jobs(cupsd_client_t  *con,     /* I - Client connection */
 
     if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
     {
-      send_http_error(con, status);
+      send_http_error(con, status, NULL);
       return;
     }
 
@@ -2308,7 +3871,7 @@ cancel_all_jobs(cupsd_client_t  *con,     /* I - Client connection */
     cupsdCancelJobs(NULL, username, purge);
 
     cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
-                    purge ? "purged" : "cancelled", con->username);
+                    purge ? "purged" : "canceled", get_username(con));
   }
   else
   {
@@ -2316,9 +3879,10 @@ cancel_all_jobs(cupsd_client_t  *con,    /* I - Client connection */
     * Check policy...
     */
 
-    if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+    if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
+                                   NULL)) != HTTP_OK)
     {
-      send_http_error(con, status);
+      send_http_error(con, status, printer);
       return;
     }
 
@@ -2326,10 +3890,11 @@ cancel_all_jobs(cupsd_client_t  *con,   /* I - Client connection */
     * Cancel all of the jobs on the named printer...
     */
 
-    cupsdCancelJobs(dest, username, purge);
+    cupsdCancelJobs(printer->name, username, purge);
 
     cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
-                    dest, purge ? "purged" : "cancelled", con->username);
+                    printer->name, purge ? "purged" : "canceled",
+                   get_username(con));
   }
 
   con->response->request.status.status_code = IPP_OK;
@@ -2346,15 +3911,15 @@ cancel_job(cupsd_client_t  *con,        /* I - Client connection */
 {
   ipp_attribute_t *attr;               /* Current attribute */
   int          jobid;                  /* Job ID */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  char         scheme[HTTP_MAX_URI],   /* Scheme portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
   int          port;                   /* Port portion of URI */
   cupsd_job_t  *job;                   /* Job information */
-  const char   *dest;                  /* Destination */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
+  cups_ptype_t dtype;                  /* Destination type (printer/class) */
   cupsd_printer_t *printer;            /* Printer data */
+  int          purge;                  /* Purge the job? */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
@@ -2370,7 +3935,8 @@ cancel_job(cupsd_client_t  *con,  /* I - Client connection */
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -2383,11 +3949,7 @@ cancel_job(cupsd_client_t  *con, /* I - Client connection */
       * Find the current job on the specified printer...
       */
 
-      httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                      username, sizeof(username), host, sizeof(host), &port,
-                     resource, sizeof(resource));
-
-      if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+      if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
       {
        /*
        * Bad URI...
@@ -2399,30 +3961,37 @@ cancel_job(cupsd_client_t  *con,        /* I - Client connection */
       }
 
      /*
-      * See if the printer is currently printing a job...
+      * See if there are any pending jobs...
       */
 
-      if (printer->job)
-        jobid = ((cupsd_job_t *)printer->job)->id;
+      for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
+          job;
+          job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
+       if (job->state_value <= IPP_JOB_PROCESSING &&
+           !strcasecmp(job->dest, printer->name))
+         break;
+
+      if (job)
+       jobid = job->id;
       else
       {
        /*
-        * No, see if there are any pending jobs...
+        * No, try stopped jobs...
        */
-        
-        for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
+
+       for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
             job;
             job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
-         if (job->state->values[0].integer <= IPP_JOB_PROCESSING &&
-             !strcasecmp(job->dest, dest))
+         if (job->state_value == IPP_JOB_STOPPED &&
+             !strcasecmp(job->dest, printer->name))
            break;
 
-       if (job != NULL)
+       if (job)
          jobid = job->id;
        else
        {
          send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"),
-                         dest);
+                         printer->name);
          return;
        }
       }
@@ -2434,10 +4003,10 @@ cancel_job(cupsd_client_t  *con,        /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
+
     if (strncmp(resource, "/jobs/", 6))
     {
      /*
@@ -2453,6 +4022,16 @@ cancel_job(cupsd_client_t  *con, /* I - Client connection */
     jobid = atoi(resource + 6);
   }
 
+ /*
+  * Look for the "purge-job" attribute...
+  */
+
+  if ((attr = ippFindAttribute(con->request, "purge-job",
+                               IPP_TAG_BOOLEAN)) != NULL)
+    purge = attr->values[0].boolean;
+  else
+    purge = 0;
+
  /*
   * See if the job exists...
   */
@@ -2473,25 +4052,38 @@ cancel_job(cupsd_client_t  *con,        /* I - Client connection */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to delete job #%d "
-                     "owned by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
  /*
-  * See if the job is already completed, cancelled, or aborted; if so,
+  * See if the job is already completed, canceled, or aborted; if so,
   * we can't cancel...
   */
 
-  if (job->state->values[0].integer >= IPP_JOB_CANCELLED)
+  if (job->state_value >= IPP_JOB_CANCELED && !purge)
   {
-    send_ipp_status(con, IPP_NOT_POSSIBLE,
-                    _("Job #%d is already %s - can\'t cancel."), jobid,
-                   job->state->values[0].integer == IPP_JOB_CANCELLED ? "cancelled" :
-                   job->state->values[0].integer == IPP_JOB_ABORTED ? "aborted" :
-                   "completed");
+    switch (job->state_value)
+    {
+      case IPP_JOB_CANCELED :
+         send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Job #%d is already canceled - can\'t cancel."),
+                         jobid);
+          break;
+
+      case IPP_JOB_ABORTED :
+         send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Job #%d is already aborted - can\'t cancel."),
+                         jobid);
+          break;
+
+      default :
+         send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Job #%d is already completed - can\'t cancel."),
+                         jobid);
+          break;
+    }
+
     return;
   }
 
@@ -2499,14 +4091,17 @@ cancel_job(cupsd_client_t  *con,        /* I - Client connection */
   * Cancel the job and return...
   */
 
-  cupsdAddEvent(CUPSD_EVENT_JOB_COMPLETED, job->printer, job,
-                "Job cancelled by \"%s\".", username);
-
-  cupsdCancelJob(job, 0);
+  cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
+                   purge ? "Job purged by \"%s\"" : "Job canceled by \"%s\"",
+                  username);
   cupsdCheckJobs();
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was cancelled by \"%s\".", jobid,
-                  username);
+  if (purge)
+    cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid,
+                   username);
+  else
+    cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
+                   username);
 
   con->response->request.status.status_code = IPP_OK;
 }
@@ -2521,6 +4116,82 @@ cancel_subscription(
     cupsd_client_t *con,               /* I - Client connection */
     int            sub_id)             /* I - Subscription ID */
 {
+  http_status_t                status;         /* Policy status */
+  cupsd_subscription_t *sub;           /* Subscription */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "cancel_subscription(con=%p[%d], sub_id=%d)",
+                  con, con->http.fd, sub_id);
+
+ /*
+  * Is the subscription ID valid?
+  */
+
+  if ((sub = cupsdFindSubscription(sub_id)) == NULL)
+  {
+   /*
+    * Bad subscription ID...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("notify-subscription-id %d no good!"), sub_id);
+    return;
+  }
+
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
+                                             DefaultPolicyPtr,
+                                 con, sub->owner)) != HTTP_OK)
+  {
+    send_http_error(con, status, sub->dest);
+    return;
+  }
+
+ /*
+  * Cancel the subscription...
+  */
+
+  cupsdDeleteSubscription(sub, 1);
+
+  con->response->request.status.status_code = IPP_OK;
+}
+
+
+/*
+ * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
+ */
+
+static int                             /* O - 1 if OK, 0 if not */
+check_rss_recipient(
+    const char *recipient)             /* I - Recipient URI */
+{
+  cupsd_subscription_t *sub;           /* Current subscription */
+
+
+  for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
+       sub;
+       sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
+    if (sub->recipient)
+    {
+     /*
+      * Compare the URIs up to the first ?...
+      */
+
+      const char *r1, *r2;
+
+      for (r1 = recipient, r2 = sub->recipient;
+           *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?';
+          r1 ++, r2 ++);
+
+      if (*r1 == *r2)
+        return (0);
+    }
+
+  return (1);
 }
 
 
@@ -2533,40 +4204,36 @@ check_quotas(cupsd_client_t  *con,      /* I - Client connection */
              cupsd_printer_t *p)       /* I - Printer or class */
 {
   int          i;                      /* Looping var */
-  ipp_attribute_t *attr;               /* Current attribute */
   char         username[33];           /* Username */
   cupsd_quota_t        *q;                     /* Quota data */
+#ifdef HAVE_MBR_UID_TO_UUID
+ /*
+  * Use Apple membership APIs which require that all names represent
+  * valid user account or group records accessible by the server.
+  */
+
+  uuid_t       usr_uuid;               /* UUID for job requesting user  */
+  uuid_t       usr2_uuid;              /* UUID for ACL user name entry  */
+  uuid_t       grp_uuid;               /* UUID for ACL group name entry */
+  int          mbr_err;                /* Error from membership function */
+  int          is_member;              /* Is this user a member? */
+#else
+ /*
+  * Use standard POSIX APIs for checking users and groups...
+  */
+
   struct passwd        *pw;                    /* User password data */
+#endif /* HAVE_MBR_UID_TO_UUID */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
                   con, con->http.fd, p, p->name);
 
- /*
-  * Check input...
-  */
-
-  if (con == NULL || p == NULL)
-    return (0);
-
  /*
   * Figure out who is printing...
   */
 
-  attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
-
-  if (con->username[0])
-    strlcpy(username, con->username, sizeof(username));
-  else if (attr != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "check_quotas: requesting-user-name = \"%s\"",
-                    attr->values[0].string.text);
-
-    strlcpy(username, attr->values[0].string.text, sizeof(username));
-  }
-  else
-    strcpy(username, "anonymous");
+  strlcpy(username, get_username(con), sizeof(username));
 
  /*
   * Check global active job limits for printers and users...
@@ -2609,8 +4276,34 @@ check_quotas(cupsd_client_t  *con,       /* I - Client connection */
 
   if (p->num_users)
   {
+#ifdef HAVE_MBR_UID_TO_UUID
+   /*
+    * Get UUID for job requesting user...
+    */
+
+    if (mbr_user_name_to_uuid((char *)username, usr_uuid))
+    {
+     /*
+      * Unknown user...
+      */
+
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                     "check_quotas: UUID lookup failed for user \"%s\"",
+                     username);
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                     "Denying user \"%s\" access to printer \"%s\" "
+                     "(unknown user)...",
+                     username, p->name);
+      return (0);
+    }
+#else
+   /*
+    * Get UID and GID of requesting user...
+    */
+
     pw = getpwnam(username);
     endpwent();
+#endif /* HAVE_MBR_UID_TO_UUID */
 
     for (i = 0; i < p->num_users; i ++)
       if (p->users[i][0] == '@')
@@ -2619,11 +4312,84 @@ check_quotas(cupsd_client_t  *con,      /* I - Client connection */
         * Check group membership...
        */
 
+#ifdef HAVE_MBR_UID_TO_UUID
+        if (p->users[i][1] == '#')
+       {
+         if (uuid_parse((char *)p->users[i] + 2, grp_uuid))
+           uuid_clear(grp_uuid);
+       }
+       else if ((mbr_err = mbr_group_name_to_uuid((char *)p->users[i] + 1,
+                                                  grp_uuid)) != 0)
+       {
+        /*
+         * Invalid ACL entries are ignored for matching; just record a
+         * warning in the log...
+         */
+
+         cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "check_quotas: UUID lookup failed for ACL entry "
+                         "\"%s\" (err=%d)", p->users[i], mbr_err);
+         cupsdLogMessage(CUPSD_LOG_WARN,
+                         "Access control entry \"%s\" not a valid group name; "
+                         "entry ignored", p->users[i]);
+       }
+
+       if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid,
+                                           &is_member)) != 0)
+       {
+        /*
+         * At this point, there should be no errors, but check anyways...
+         */
+
+         cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "check_quotas: group \"%s\" membership check "
+                         "failed (err=%d)", p->users[i] + 1, mbr_err);
+         is_member = 0;
+       }
+
+       /*
+       * Stop if we found a match...
+       */
+
+       if (is_member)
+         break;
+
+#else
         if (cupsdCheckGroup(username, pw, p->users[i] + 1))
          break;
+#endif /* HAVE_MBR_UID_TO_UUID */
+      }
+#ifdef HAVE_MBR_UID_TO_UUID
+      else
+      {
+        if (p->users[i][0] == '#')
+       {
+         if (uuid_parse((char *)p->users[i] + 1, usr2_uuid))
+           uuid_clear(usr2_uuid);
+        }
+        else if ((mbr_err = mbr_user_name_to_uuid((char *)p->users[i],
+                                                 usr2_uuid)) != 0)
+       {
+        /*
+         * Invalid ACL entries are ignored for matching; just record a
+         * warning in the log...
+         */
+
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "check_quotas: UUID lookup failed for ACL entry "
+                         "\"%s\" (err=%d)", p->users[i], mbr_err);
+          cupsdLogMessage(CUPSD_LOG_WARN,
+                         "Access control entry \"%s\" not a valid user name; "
+                         "entry ignored", p->users[i]);
+       }
+
+       if (!uuid_compare(usr_uuid, usr2_uuid))
+         break;
       }
+#else
       else if (!strcasecmp(username, p->users[i]))
        break;
+#endif /* HAVE_MBR_UID_TO_UUID */
 
     if ((i < p->num_users) == p->deny_users)
     {
@@ -2638,6 +4404,66 @@ check_quotas(cupsd_client_t  *con,       /* I - Client connection */
   * Check quotas...
   */
 
+#ifdef __APPLE__
+  if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL)
+  {
+   /*
+    * TODO: Define these special page count values as constants!
+    */
+
+    if (q->page_count == -4) /* special case: unlimited user */
+    {
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                      "User \"%s\" request approved for printer %s (%s): " 
+                     "unlimited quota.",
+                     username, p->name, p->info);
+      q->page_count = 0; /* allow user to print */
+      return (1);
+    }
+    else if (q->page_count == -3) /* quota exceeded */
+    {
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                      "User \"%s\" request denied for printer %s (%s): "
+                     "quota limit exceeded.",
+                     username, p->name, p->info);
+      q->page_count = 2; /* force quota exceeded failure */
+      return (-1);
+    }
+    else if (q->page_count == -2) /* quota disabled for user */
+    {
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                      "User \"%s\" request denied for printer %s (%s): "
+                     "printing disabled for user.",
+                     username, p->name, p->info);
+      q->page_count = 2; /* force quota exceeded failure */
+      return (-1);
+    }
+    else if (q->page_count == -1) /* quota access error */
+    {
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                      "User \"%s\" request denied for printer %s (%s): "
+                     "unable to determine quota limit.",
+                     username, p->name, p->info);
+      q->page_count = 2; /* force quota exceeded failure */
+      return (-1);
+    }
+    else if (q->page_count < 0) /* user not found or other error */
+    {
+      cupsdLogMessage(CUPSD_LOG_INFO,
+                      "User \"%s\" request denied for printer %s (%s): "
+                     "user disabled / missing quota.",
+                     username, p->name, p->info);
+      q->page_count = 2; /* force quota exceeded failure */
+      return (-1);
+    }
+    else /* page within user limits */
+    {
+      q->page_count = 0; /* allow user to print */
+      return (1);
+    }
+  }
+  else
+#endif /* __APPLE__ */
   if (p->k_limit || p->page_limit)
   {
     if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL)
@@ -2645,7 +4471,7 @@ check_quotas(cupsd_client_t  *con,        /* I - Client connection */
       cupsdLogMessage(CUPSD_LOG_ERROR,
                       "Unable to allocate quota data for user \"%s\"!",
                       username);
-      return (0);
+      return (-1);
     }
 
     if ((q->k_count >= p->k_limit && p->k_limit) ||
@@ -2653,7 +4479,7 @@ check_quotas(cupsd_client_t  *con,        /* I - Client connection */
     {
       cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...",
                       username);
-      return (0);
+      return (-1);
     }
   }
 
@@ -2725,10 +4551,17 @@ copy_attribute(
           for (i = 0; i < attr->num_values; i ++)
            toattr->values[i].string.text = attr->values[i].string.text;
         }
+       else if (attr->value_tag & IPP_TAG_COPY)
+       {
+          for (i = 0; i < attr->num_values; i ++)
+           toattr->values[i].string.text =
+               _cupsStrAlloc(attr->values[i].string.text);
+       }
        else
        {
           for (i = 0; i < attr->num_values; i ++)
-           toattr->values[i].string.text = strdup(attr->values[i].string.text);
+           toattr->values[i].string.text =
+               _cupsStrRetain(attr->values[i].string.text);
        }
         break;
 
@@ -2775,18 +4608,34 @@ copy_attribute(
            toattr->values[i].string.text    = attr->values[i].string.text;
           }
         }
+       else if (attr->value_tag & IPP_TAG_COPY)
+       {
+          for (i = 0; i < attr->num_values; i ++)
+         {
+           if (!i)
+              toattr->values[i].string.charset =
+                 _cupsStrAlloc(attr->values[i].string.charset);
+           else
+              toattr->values[i].string.charset =
+                 toattr->values[0].string.charset;
+
+           toattr->values[i].string.text =
+               _cupsStrAlloc(attr->values[i].string.text);
+          }
+        }
        else
        {
           for (i = 0; i < attr->num_values; i ++)
          {
            if (!i)
               toattr->values[i].string.charset =
-                 strdup(attr->values[i].string.charset);
+                 _cupsStrRetain(attr->values[i].string.charset);
            else
               toattr->values[i].string.charset =
                  toattr->values[0].string.charset;
 
-           toattr->values[i].string.text = strdup(attr->values[i].string.text);
+           toattr->values[i].string.text =
+               _cupsStrRetain(attr->values[i].string.text);
           }
         }
         break;
@@ -2813,7 +4662,8 @@ copy_attribute(
 
          if (toattr->values[i].unknown.length > 0)
          {
-           if ((toattr->values[i].unknown.data = malloc(toattr->values[i].unknown.length)) == NULL)
+           if ((toattr->values[i].unknown.data =
+                    malloc(toattr->values[i].unknown.length)) == NULL)
              toattr->values[i].unknown.length = 0;
            else
              memcpy(toattr->values[i].unknown.data,
@@ -2833,46 +4683,45 @@ copy_attribute(
  */
 
 static void
-copy_attrs(ipp_t           *to,                /* I - Destination request */
-           ipp_t           *from,      /* I - Source request */
-           ipp_attribute_t *req,       /* I - Requested attributes */
-          ipp_tag_t       group,       /* I - Group to copy */
-          int             quickcopy)   /* I - Do a quick copy? */
+copy_attrs(ipp_t        *to,           /* I - Destination request */
+           ipp_t        *from,         /* I - Source request */
+           cups_array_t *ra,           /* I - Requested attributes */
+          ipp_tag_t    group,          /* I - Group to copy */
+          int          quickcopy)      /* I - Do a quick copy? */
 {
-  int                  i;              /* Looping var */
   ipp_attribute_t      *fromattr;      /* Source attribute */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_attrs(%p, %p, %p, %x)", to, from,
-                  req, group);
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
+                 to, from, ra, group, quickcopy);
 
-  if (to == NULL || from == NULL)
+  if (!to || !from)
     return;
 
-  if (req != NULL && strcmp(req->values[0].string.text, "all") == 0)
-    req = NULL;                                /* "all" means no filter... */
-
-  for (fromattr = from->attrs; fromattr != NULL; fromattr = fromattr->next)
+  for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
   {
    /*
     * Filter attributes as needed...
     */
 
-    if (group != IPP_TAG_ZERO && fromattr->group_tag != group &&
-        fromattr->group_tag != IPP_TAG_ZERO)
+    if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
+         fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
       continue;
 
-    if (req != NULL && fromattr->name != NULL)
+    if (!ra || cupsArrayFind(ra, fromattr->name))
     {
-      for (i = 0; i < req->num_values; i ++)
-        if (strcmp(fromattr->name, req->values[i].string.text) == 0)
-         break;
+     /*
+      * Don't send collection attributes by default to IPP/1.x clients
+      * since many do not support collections...
+      */
 
-      if (i == req->num_values)
-        continue;
-    }
+      if (fromattr->value_tag == IPP_TAG_BEGIN_COLLECTION &&
+          !ra && to->request.status.version[0] == 1)
+       continue;
 
-    copy_attribute(to, fromattr, quickcopy);
+      copy_attribute(to, fromattr, quickcopy);
+    }
   }
 }
 
@@ -2899,15 +4748,16 @@ copy_banner(cupsd_client_t *con,        /* I - Client connection */
   ipp_attribute_t *attr;               /* Attribute */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_banner(%p[%d], %p[%d], %s)",
-                  con, con->http.fd, job, job->id, name ? name : "(null)");
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
+                  con, con ? con->http.fd : -1, job, job->id,
+                 name ? name : "(null)");
 
  /*
   * Find the banner; return if not found or "none"...
   */
 
-  if (name == NULL ||
-      strcmp(name, "none") == 0 ||
+  if (!name || !strcmp(name, "none") ||
       (banner = cupsdFindBanner(name)) == NULL)
     return (0);
 
@@ -2916,14 +4766,14 @@ copy_banner(cupsd_client_t *con,        /* I - Client connection */
   */
 
   if (add_file(con, job, banner->filetype, 0))
-    return (0);
+    return (-1);
 
   snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
            job->num_files);
   if ((out = cupsFileOpen(filename, "w")) == NULL)
   {
     cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "copy_banner: Unable to create banner job file %s - %s",
+                    "Unable to create banner job file %s - %s",
                     filename, strerror(errno));
     job->num_files --;
     return (0);
@@ -2936,7 +4786,7 @@ copy_banner(cupsd_client_t *con,  /* I - Client connection */
   * Try the localized banner file under the subdirectory...
   */
 
-  strlcpy(attrname, con->request->attrs->next->values[0].string.text,
+  strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
           sizeof(attrname));
   if (strlen(attrname) > 2 && attrname[2] == '-')
   {
@@ -2979,7 +4829,7 @@ copy_banner(cupsd_client_t *con,  /* I - Client connection */
     cupsFileClose(out);
     unlink(filename);
     cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "copy_banner: Unable to open banner template file %s - %s",
+                    "Unable to open banner template file %s - %s",
                     filename, strerror(errno));
     job->num_files --;
     return (0);
@@ -3025,7 +4875,7 @@ copy_banner(cupsd_client_t *con,  /* I - Client connection */
       else
         s = attrname;
 
-      if (strcmp(s, "printer-name") == 0)
+      if (!strcmp(s, "printer-name"))
       {
         cupsFilePuts(out, job->dest);
        continue;
@@ -3061,8 +4911,11 @@ copy_banner(cupsd_client_t *con, /* I - Client connection */
        {
          case IPP_TAG_INTEGER :
          case IPP_TAG_ENUM :
-             if (strncmp(s, "time-at-", 8) == 0)
-               cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer));
+             if (!strncmp(s, "time-at-", 8))
+             {
+               struct timeval tv = { attr->values[i].integer, 0 };
+               cupsFilePuts(out, cupsdGetDateTime(&tv, CUPSD_TIME_STANDARD));
+             }
              else
                cupsFilePrintf(out, "%d", attr->values[i].integer);
              break;
@@ -3094,7 +4947,7 @@ copy_banner(cupsd_client_t *con,  /* I - Client connection */
          case IPP_TAG_KEYWORD :
          case IPP_TAG_CHARSET :
          case IPP_TAG_LANGUAGE :
-             if (strcasecmp(banner->filetype->type, "postscript") == 0)
+             if (!strcasecmp(banner->filetype->type, "postscript"))
              {
               /*
                * Need to quote strings for PS banners...
@@ -3140,7 +4993,8 @@ copy_banner(cupsd_client_t *con,  /* I - Client connection */
 
   kbytes = (cupsFileTell(out) + 1023) / 1024;
 
-  if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
+  if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
+                               IPP_TAG_INTEGER)) != NULL)
     attr->values[0].integer += kbytes;
 
   cupsFileClose(out);
@@ -3210,34 +5064,28 @@ copy_model(cupsd_client_t *con,         /* I - Client connection */
            const char     *from,       /* I - Source file */
            const char     *to)         /* I - Destination file */
 {
-  fd_set       *input;                 /* select() input set */
+  fd_set       input;                  /* select() input set */
   struct timeval timeout;              /* select() timeout */
-  int          maxfd;                  /* Maximum file descriptor for select() */
+  int          maxfd;                  /* Max file descriptor for select() */
   char         tempfile[1024];         /* Temporary PPD file */
   int          tempfd;                 /* Temporary PPD file descriptor */
   int          temppid;                /* Process ID of cups-driverd */
   int          temppipe[2];            /* Temporary pipes */
   char         *argv[4],               /* Command-line arguments */
-               *envp[100];             /* Environment */
+               *envp[MAX_ENV];         /* Environment */
   cups_file_t  *src,                   /* Source file */
                *dst;                   /* Destination file */
+  ppd_file_t   *ppd;                   /* PPD file */
   int          bytes,                  /* Bytes from pipe */
                total;                  /* Total bytes from pipe */
-  char         buffer[2048],           /* Copy buffer */
-               *ptr;                   /* Pointer into buffer */
+  char         buffer[2048];           /* Copy buffer */
   int          i;                      /* Looping var */
   char         option[PPD_MAX_NAME],   /* Option name */
                choice[PPD_MAX_NAME];   /* Choice name */
   int          num_defaults;           /* Number of default options */
-  ppd_default_t        *defaults;              /* Default options */
+  cups_option_t        *defaults;              /* Default options */
   char         cups_protocol[PPD_MAX_LINE];
                                        /* cupsProtocol attribute */
-  int          have_letter,            /* Have Letter size */
-               have_a4;                /* Have A4 size */
-#ifdef HAVE_LIBPAPER
-  char         *paper_result;          /* Paper size name from libpaper */
-  char         system_paper[64];       /* Paper size name buffer */
-#endif /* HAVE_LIBPAPER */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
@@ -3263,25 +5111,15 @@ copy_model(cupsd_client_t *con,         /* I - Client connection */
 
   cupsdOpenPipe(temppipe);
 
-  if ((input = calloc(1, SetSize)) == NULL)
-  {
-    close(tempfd);
-    unlink(tempfile);
-
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "copy_model: Unable to allocate %d bytes for select()...",
-                    SetSize);
-    return (-1);
-  }
-
   cupsdLogMessage(CUPSD_LOG_DEBUG,
                   "copy_model: Running \"cups-driverd cat %s\"...", from);
 
   if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
-                         -1, 0, &temppid))
+                         -1, -1, 0, DefaultProfile, NULL, &temppid))
   {
     close(tempfd);
     unlink(tempfile);
+
     return (-1);
   }
 
@@ -3304,15 +5142,14 @@ copy_model(cupsd_client_t *con,         /* I - Client connection */
     * See if we have data ready...
     */
 
-    bytes = 0;
-
-    FD_SET(temppipe[0], input);
-    FD_SET(CGIPipes[0], input);
+    FD_ZERO(&input);
+    FD_SET(temppipe[0], &input);
+    FD_SET(CGIPipes[0], &input);
 
     timeout.tv_sec  = 30;
     timeout.tv_usec = 0;
 
-    if ((i = select(maxfd, input, NULL, NULL, &timeout)) < 0)
+    if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
     {
       if (errno == EINTR)
         continue;
@@ -3328,7 +5165,7 @@ copy_model(cupsd_client_t *con,           /* I - Client connection */
       break;
     }
 
-    if (FD_ISSET(temppipe[0], input))
+    if (FD_ISSET(temppipe[0], &input))
     {
      /*
       * Read the PPD file from the pipe, and write it to the PPD file.
@@ -3345,7 +5182,7 @@ copy_model(cupsd_client_t *con,           /* I - Client connection */
        break;
     }
 
-    if (FD_ISSET(CGIPipes[0], input))
+    if (FD_ISSET(CGIPipes[0], &input))
       cupsdUpdateCGI();
   }
 
@@ -3367,819 +5204,570 @@ copy_model(cupsd_client_t *con,               /* I - Client connection */
   * Read the source file and see what page sizes are supported...
   */
 
-  if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
-  {
-    unlink(tempfile);
-    return (-1);
-  }
-
-  have_letter = 0;
-  have_a4     = 0;
-
-  while (cupsFileGets(src, buffer, sizeof(buffer)) != NULL)
-    if (!strncmp(buffer, "*PageSize ", 10))
-    {
-     /*
-      * Strip UI text and command data from the end of the line...
-      */
-
-      if ((ptr = strchr(buffer + 10, '/')) != NULL)
-        *ptr = '\0';
-      if ((ptr = strchr(buffer + 10, ':')) != NULL)
-        *ptr = '\0';
-
-      for (ptr = buffer + 10; isspace(*ptr); ptr ++);
-
-     /*
-      * Look for Letter and A4 page sizes...
-      */
-
-      if (!strcmp(ptr, "Letter"))
-       have_letter = 1;
-
-      if (!strcmp(ptr, "A4"))
-       have_a4 = 1;
-    }
-
-  cupsFileRewind(src);
-
- /*
-  * Open the destination (if possible) and set the default options...
-  */
-
-  num_defaults     = 0;
-  defaults         = NULL;
-  cups_protocol[0] = '\0';
-
-  if ((dst = cupsFileOpen(to, "rb")) != NULL)
-  {
-   /*
-    * Read all of the default lines from the old PPD...
-    */
-
-    while (cupsFileGets(dst, buffer, sizeof(buffer)) != NULL)
-      if (!strncmp(buffer, "*Default", 8))
-      {
-       /*
-       * Add the default option...
-       */
-
-        if (!ppd_parse_line(buffer, option, sizeof(option),
-                           choice, sizeof(choice)))
-          num_defaults = ppd_add_default(option, choice, num_defaults,
-                                        &defaults);
-      }
-      else if (!strncmp(buffer, "*cupsProtocol:", 14))
-        strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
-
-    cupsFileClose(dst);
-  }
-#ifdef HAVE_LIBPAPER
-  else if ((paper_result = systempapername()) != NULL)
-  {
-   /*
-    * Set the default media sizes from the systemwide default...
-    */
-
-    strlcpy(system_paper, paper_result, sizeof(system_paper));
-    system_paper[0] = toupper(system_paper[0] & 255);
-
-    if ((!strcmp(system_paper, "Letter") && have_letter) ||
-        (!strcmp(system_paper, "A4") && have_a4))
-    {
-      num_defaults = ppd_add_default("PageSize", system_paper, 
-                                    num_defaults, &defaults);
-      num_defaults = ppd_add_default("PageRegion", system_paper, 
-                                    num_defaults, &defaults);
-      num_defaults = ppd_add_default("PaperDimension", system_paper, 
-                                    num_defaults, &defaults);
-      num_defaults = ppd_add_default("ImageableArea", system_paper, 
-                                    num_defaults, &defaults);
-    }
-  }
-#endif /* HAVE_LIBPAPER */
-  else
-  {
-   /*
-    * Add the default media sizes...
-    *
-    * Note: These values are generally not valid for large-format devices
-    *       like plotters, however it is probably safe to say that those
-    *       users will configure the media size after initially adding
-    *       the device anyways...
-    */
-
-    if (!DefaultLanguage ||
-        !strcasecmp(DefaultLanguage, "C") ||
-        !strcasecmp(DefaultLanguage, "POSIX") ||
-       !strcasecmp(DefaultLanguage, "en") ||
-       !strncasecmp(DefaultLanguage, "en_US", 5) ||
-       !strncasecmp(DefaultLanguage, "en_CA", 5) ||
-       !strncasecmp(DefaultLanguage, "fr_CA", 5))
-    {
-     /*
-      * These are the only locales that will default to "letter" size...
-      */
-
-      if (have_letter)
-      {
-       num_defaults = ppd_add_default("PageSize", "Letter", num_defaults,
-                                       &defaults);
-       num_defaults = ppd_add_default("PageRegion", "Letter", num_defaults,
-                                       &defaults);
-       num_defaults = ppd_add_default("PaperDimension", "Letter", num_defaults,
-                                       &defaults);
-       num_defaults = ppd_add_default("ImageableArea", "Letter", num_defaults,
-                                       &defaults);
-      }
-    }
-    else if (have_a4)
-    {
-     /*
-      * The rest default to "a4" size...
-      */
-
-      num_defaults = ppd_add_default("PageSize", "A4", num_defaults,
-                                     &defaults);
-      num_defaults = ppd_add_default("PageRegion", "A4", num_defaults,
-                                     &defaults);
-      num_defaults = ppd_add_default("PaperDimension", "A4", num_defaults,
-                                     &defaults);
-      num_defaults = ppd_add_default("ImageableArea", "A4", num_defaults,
-                                     &defaults);
-    }
-  }
-
- /*
-  * Open the destination file for a copy...
-  */
-
-  if ((dst = cupsFileOpen(to, "wb")) == NULL)
+  if ((ppd = ppdOpenFile(tempfile)) == NULL)
   {
-    if (num_defaults > 0)
-      free(defaults);
-
-    cupsFileClose(src);
     unlink(tempfile);
     return (-1);
   }
 
  /*
-  * Copy the source file to the destination...
-  */
-
-  while (cupsFileGets(src, buffer, sizeof(buffer)) != NULL)
-  {
-    if (!strncmp(buffer, "*Default", 8))
-    {
-     /*
-      * Check for an previous default option choice...
-      */
-
-      if (!ppd_parse_line(buffer, option, sizeof(option),
-                         choice, sizeof(choice)))
-      {
-        for (i = 0; i < num_defaults; i ++)
-         if (!strcmp(option, defaults[i].option))
-         {
-          /*
-           * Substitute the previous choice...
-           */
-
-           snprintf(buffer, sizeof(buffer), "*Default%s: %s", option,
-                    defaults[i].choice);
-           break;
-         }
-      }
-    }
-
-    cupsFilePrintf(dst, "%s\n", buffer);
-  }
-
-  if (cups_protocol[0])
-    cupsFilePrintf(dst, "%s\n", cups_protocol);
-
-  if (num_defaults > 0)
-    free(defaults);
-
- /*
-  * Close both files and return...
-  */
-
-  cupsFileClose(src);
-
-  unlink(tempfile);
-
-  return (cupsFileClose(dst));
-}
-
-
-/*
- * 'copy_subscription_attrs()' - Copy subscription attributes.
- */
-
-static void
-copy_subscription_attrs(
-    cupsd_client_t       *con,         /* I - Client connection */
-    cupsd_subscription_t *sub,         /* I - Subscription */
-    cups_array_t         *ra)          /* I - Requested attributes array */
-{
-  ipp_attribute_t      *attr;          /* Current attribute */
-  char                 printer_uri[HTTP_MAX_URI];
-                                       /* Printer URI */
-  int                  count;          /* Number of events */
-  unsigned             mask;           /* Current event mask */
-  const char           *name;          /* Current event name */
-
-
- /*
-  * Copy the subscription attributes to the response using the
-  * requested-attributes attribute that may be provided by the client.
-  */
-
-  if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
-    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                  "notify-subscription-id", sub->id);
-
-  if (!ra || cupsArrayFind(ra, "notify-events"))
-  {
-    if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
-    {
-     /*
-      * Simple event list...
-      */
-
-      ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
-                   IPP_TAG_KEYWORD | IPP_TAG_COPY,
-                   "notify-events", NULL, name);
-    }
-    else
-    {
-     /*
-      * Complex event list...
-      */
-
-      for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
-       if (sub->mask & mask)
-          count ++;
-
-      attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
-                           IPP_TAG_KEYWORD | IPP_TAG_COPY,
-                           "notify-events", count, NULL, NULL);
-
-      for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
-       if (sub->mask & mask)
-       {
-          attr->values[count].string.text =
-             (char *)cupsdEventName((cupsd_eventmask_t)mask);
-
-          count ++;
-       }
-    }
-  }
-
-  if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name"))
-    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
-                "notify-subscriber-user-name", NULL, sub->owner);
-
-  if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri")))
-    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
-                "notify-recipient-uri", NULL, sub->recipient);
-  else if (!ra || cupsArrayFind(ra, "notify-pull-method"))
-    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
-                 "notify-pull-method", NULL, "ippget");
-
-  if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data")))
-    ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
-                      sub->user_data, sub->user_data_len);
-
-  if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration")))
-    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                  "notify-lease-duration", sub->lease);
-
-  if (!ra || cupsArrayFind(ra, "notify-time-interval"))
-    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                  "notify-time-interval", sub->interval);
-
-  if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
-  {
-    httpAssembleURIf(printer_uri, sizeof(printer_uri), "ipp", NULL,
-                     con->servername, con->serverport, "/printers/%s",
-                    sub->dest->name);
-    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
-                "notify-printer-uri", NULL, printer_uri);
-  }
-
-  if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
-    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                  "notify-job-id", sub->job->id);
-}
-
-
-/*
- * 'create_job()' - Print a file to a printer or class.
- */
-
-static void
-create_job(cupsd_client_t  *con,       /* I - Client connection */
-          ipp_attribute_t *uri)        /* I - Printer URI */
-{
-  http_status_t        status;                 /* Policy status */
-  ipp_attribute_t *attr;               /* Current attribute */
-  const char   *dest;                  /* Destination */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
-  int          priority;               /* Job priority */
-  char         *title;                 /* Job name/title */
-  cupsd_job_t  *job;                   /* Current job */
-  char         job_uri[HTTP_MAX_URI],  /* Job URI */
-               method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
-  cupsd_printer_t *printer;            /* Printer data */
-  int          kbytes;                 /* Size of print file */
-  int          i;                      /* Looping var */
-  int          lowerpagerange;         /* Page range bound */
-
-
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
-                  con->http.fd, uri->values[0].string.text);
-
- /*
-  * Is the destination valid?
+  * Open the destination (if possible) and set the default options...
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  num_defaults     = 0;
+  defaults         = NULL;
+  cups_protocol[0] = '\0';
 
-  if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if ((dst = cupsFileOpen(to, "rb")) != NULL)
   {
    /*
-    * Bad URI...
+    * Read all of the default lines from the old PPD...
     */
 
-    send_ipp_status(con, IPP_NOT_FOUND,
-                    _("The printer or class was not found."));
-    return;
-  }
+    while (cupsFileGets(dst, buffer, sizeof(buffer)))
+      if (!strncmp(buffer, "*Default", 8))
+      {
+       /*
+       * Add the default option...
+       */
 
- /*
-  * Check remote printing to non-shared printer...
-  */
+        if (!ppd_parse_line(buffer, option, sizeof(option),
+                           choice, sizeof(choice)))
+        {
+         ppd_option_t  *ppdo;          /* PPD option */
 
-  if (!printer->shared &&
-      strcasecmp(con->http.hostname, "localhost") &&
-      strcasecmp(con->http.hostname, ServerName))
+
+         /*
+         * Only add the default if the default hasn't already been
+         * set and the choice exists in the new PPD...
+         */
+
+         if (!cupsGetOption(option, num_defaults, defaults) &&
+             (ppdo = ppdFindOption(ppd, option)) != NULL &&
+             ppdFindChoice(ppdo, choice))
+            num_defaults = cupsAddOption(option, choice, num_defaults,
+                                        &defaults);
+        }
+      }
+      else if (!strncmp(buffer, "*cupsProtocol:", 14))
+        strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
+
+    cupsFileClose(dst);
+  }
+  else if (ppdPageSize(ppd, DefaultPaperSize))
   {
-    send_ipp_status(con, IPP_NOT_AUTHORIZED,
-                    _("The printer or class is not shared!"));
-    return;
+   /*
+    * Add the default media sizes...
+    */
+
+    num_defaults = cupsAddOption("PageSize", DefaultPaperSize,
+                                 num_defaults, &defaults);
+    num_defaults = cupsAddOption("PageRegion", DefaultPaperSize,
+                                 num_defaults, &defaults);
+    num_defaults = cupsAddOption("PaperDimension", DefaultPaperSize,
+                                 num_defaults, &defaults);
+    num_defaults = cupsAddOption("ImageableArea", DefaultPaperSize,
+                                 num_defaults, &defaults);
   }
 
+  ppdClose(ppd);
+
  /*
-  * Check policy...
+  * Open the source file for a copy...
   */
 
-  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
-  {
-    send_http_error(con, status);
-    return;
-  }
-  else if ((printer->type & CUPS_PRINTER_AUTHENTICATED) && !con->username[0])
+  if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
   {
-    send_http_error(con, HTTP_UNAUTHORIZED);
-    return;
+    cupsFreeOptions(num_defaults, defaults);
+    unlink(tempfile);
+    return (-1);
   }
 
  /*
-  * See if the printer is accepting jobs...
+  * Open the destination file for a copy...
   */
 
-  if (!printer->accepting)
+  if ((dst = cupsFileOpen(to, "wb")) == NULL)
   {
-    send_ipp_status(con, IPP_NOT_ACCEPTING,
-                    _("Destination \"%s\" is not accepting jobs."),
-                    dest);
-    return;
+    cupsFreeOptions(num_defaults, defaults);
+    cupsFileClose(src);
+    unlink(tempfile);
+    return (-1);
   }
 
  /*
-  * Validate job template attributes; for now just copies and page-ranges...
+  * Copy the source file to the destination...
   */
 
-  if ((attr = ippFindAttribute(con->request, "copies", IPP_TAG_INTEGER)) != NULL)
+  while (cupsFileGets(src, buffer, sizeof(buffer)))
   {
-    if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
+    if (!strncmp(buffer, "*Default", 8))
     {
-      send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
-                      attr->values[0].integer);
-      ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
-                   "copies", attr->values[0].integer);
-      return;
-    }
-  }
+     /*
+      * Check for an previous default option choice...
+      */
 
-  if ((attr = ippFindAttribute(con->request, "page-ranges", IPP_TAG_RANGE)) != NULL)
-  {
-    for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
-    {
-      if (attr->values[i].range.lower < lowerpagerange || 
-         attr->values[i].range.lower > attr->values[i].range.upper)
+      if (!ppd_parse_line(buffer, option, sizeof(option),
+                         choice, sizeof(choice)))
       {
-       send_ipp_status(con, IPP_BAD_REQUEST,
-                       _("Bad page-ranges values %d-%d."),
-                       attr->values[i].range.lower,
-                       attr->values[i].range.upper);
-       return;
-      }
+        const char     *val;           /* Default option value */
 
-      lowerpagerange = attr->values[i].range.upper + 1;
+
+        if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL)
+       {
+        /*
+         * Substitute the previous choice...
+         */
+
+         snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val);
+       }
+      }
     }
+
+    cupsFilePrintf(dst, "%s\n", buffer);
   }
 
+  if (cups_protocol[0])
+    cupsFilePrintf(dst, "%s\n", cups_protocol);
+
+  cupsFreeOptions(num_defaults, defaults);
+
  /*
-  * Make sure we aren't over our limit...
+  * Close both files and return...
   */
 
-  if (cupsArrayCount(Jobs) >= MaxJobs && MaxJobs)
-    cupsdCleanJobs();
+  cupsFileClose(src);
 
-  if (cupsArrayCount(Jobs) >= MaxJobs && MaxJobs)
-  {
-    send_ipp_status(con, IPP_NOT_POSSIBLE,
-                    _("Too many active jobs."));
-    return;
-  }
+  unlink(tempfile);
+
+  return (cupsFileClose(dst));
+}
+
+
+/*
+ * 'copy_job_attrs()' - Copy job attributes.
+ */
+
+static void
+copy_job_attrs(cupsd_client_t *con,    /* I - Client connection */
+              cupsd_job_t    *job,     /* I - Job */
+              cups_array_t   *ra)      /* I - Requested attributes array */
+{
+  char job_uri[HTTP_MAX_URI];          /* Job URI */
 
-  if (!check_quotas(con, printer))
-  {
-    send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
-    return;
-  }
 
  /*
-  * Create the job and set things up...
+  * Send the requested attributes for each job...
   */
 
-  if ((attr = ippFindAttribute(con->request, "job-priority", IPP_TAG_INTEGER)) != NULL)
-    priority = attr->values[0].integer;
-  else
-    ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
-                  priority = 50);
+  httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
+                   con->servername, con->serverport, "/jobs/%d",
+                  job->id);
 
-  if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_NAME)) != NULL)
-    title = attr->values[0].string.text;
-  else
-    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
-                 title = "Untitled");
+  if (!ra || cupsArrayFind(ra, "document-count"))
+    ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
+                 "document-count", job->num_files);
 
-  if ((job = cupsdAddJob(priority, printer->name)) == NULL)
-  {
-    send_ipp_status(con, IPP_INTERNAL_ERROR,
-                    _("Unable to add job for destination \"%s\"!"), dest);
-    return;
-  }
+  if (!ra || cupsArrayFind(ra, "job-media-progress"))
+    ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
+                 "job-media-progress", job->progress);
 
-  job->dtype   = dtype;
-  job->attrs   = con->request;
-  con->request = NULL;
+  if (!ra || cupsArrayFind(ra, "job-more-info"))
+    ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
+                "job-more-info", NULL, job_uri);
 
-  attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
+  if (job->state_value > IPP_JOB_PROCESSING &&
+      (!ra || cupsArrayFind(ra, "job-preserved")))
+    ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved",
+                  job->num_files > 0);
 
-  if (con->username[0])
-  {
-    cupsdSetString(&job->username, con->username);
+  if (!ra || cupsArrayFind(ra, "job-printer-up-time"))
+    ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
+                  "job-printer-up-time", time(NULL));
 
-    if (attr)
-      cupsdSetString(&attr->values[0].string.text, con->username);
+  if (!ra || cupsArrayFind(ra, "job-state-reasons"))
+    add_job_state_reasons(con, job);
 
-    save_auth_info(con, job);
-  }
-  else if (attr != NULL)
+  if (!ra || cupsArrayFind(ra, "job-uri"))
+    ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
+                "job-uri", NULL, job_uri);
+
+  copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0);
+}
+
+
+/*
+ * 'copy_printer_attrs()' - Copy printer attributes.
+ */
+
+static void
+copy_printer_attrs(
+    cupsd_client_t  *con,              /* I - Client connection */
+    cupsd_printer_t *printer,          /* I - Printer */
+    cups_array_t    *ra)               /* I - Requested attributes array */
+{
+  char                 printer_uri[HTTP_MAX_URI];
+                                       /* Printer URI */
+  time_t               curtime;        /* Current time */
+  int                  i;              /* Looping var */
+  ipp_attribute_t      *history;       /* History collection */
+
+
+ /*
+  * Copy the printer attributes to the response using requested-attributes
+  * and document-format attributes that may be provided by the client.
+  */
+
+  curtime = time(NULL);
+
+#ifdef __APPLE__
+  if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) &&
+      printer->recoverable)
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
+                 "com.apple.print.recoverable-message", NULL,
+                printer->recoverable);
+#endif /* __APPLE__ */
+
+  if (!ra || cupsArrayFind(ra, "marker-change-time"))
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
+                  "marker-change-time", printer->marker_time);
+
+  if (printer->num_printers > 0 &&
+      (!ra || cupsArrayFind(ra, "member-uris")))
   {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "create_job: requesting-user-name = \"%s\"",
-                    attr->values[0].string.text);
+    ipp_attribute_t    *member_uris;   /* member-uris attribute */
+    cupsd_printer_t    *p2;            /* Printer in class */
+    ipp_attribute_t    *p2_uri;        /* printer-uri-supported for class printer */
 
-    cupsdSetString(&job->username, attr->values[0].string.text);
+
+    if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER,
+                                     IPP_TAG_URI, "member-uris",
+                                    printer->num_printers, NULL,
+                                    NULL)) != NULL)
+    {
+      for (i = 0; i < printer->num_printers; i ++)
+      {
+        p2 = printer->printers[i];
+
+        if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported",
+                                      IPP_TAG_URI)) != NULL)
+          member_uris->values[i].string.text =
+             _cupsStrRetain(p2_uri->values[0].string.text);
+        else
+       {
+         httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri,
+                          sizeof(printer_uri), "ipp", NULL, con->servername,
+                          con->serverport,
+                          (p2->type & CUPS_PRINTER_CLASS) ?
+                              "/classes/%s" : "/printers/%s", p2->name);
+         member_uris->values[i].string.text = _cupsStrAlloc(printer_uri);
+        }
+      }
+    }
   }
-  else
-    cupsdSetString(&job->username, "anonymous");
 
-  if (attr == NULL)
-    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
-                 "job-originating-user-name", NULL, job->username);
-  else
+  if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
+                 "printer-alert", NULL, printer->alert);
+
+  if (printer->alert_description &&
+      (!ra || cupsArrayFind(ra, "printer-alert-description")))
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
+                 "printer-alert-description", NULL,
+                printer->alert_description);
+
+  if (!ra || cupsArrayFind(ra, "printer-current-time"))
+    ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
+               ippTimeToDate(curtime));
+
+#ifdef HAVE_DNSSD
+  if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
   {
-    attr->group_tag = IPP_TAG_JOB;
-    cupsdSetString(&attr->name, "job-originating-user-name");
+    if (printer->reg_name)
+      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                   "printer-dns-sd-name", NULL, printer->reg_name);
+    else
+      ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
+                   "printer-dns-sd-name", 0);
   }
+#endif /* HAVE_DNSSD */
 
-  if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
-                               IPP_TAG_ZERO)) != NULL)
-  {
-   /*
-    * Request contains a job-originating-host-name attribute; validate it...
-    */
+  if (!ra || cupsArrayFind(ra, "printer-error-policy"))
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                "printer-error-policy", NULL, printer->error_policy);
 
-    if (attr->value_tag != IPP_TAG_NAME ||
-        attr->num_values != 1 ||
-        strcmp(con->http.hostname, "localhost") != 0)
-    {
-     /*
-      * Can't override the value if we aren't connected via localhost.
-      * Also, we can only have 1 value and it must be a name value.
-      */
+  if (!ra || cupsArrayFind(ra, "printer-error-policy-supported"))
+  {
+    static const char * const errors[] =/* printer-error-policy-supported values */
+                 {
+                   "abort-job",
+                   "retry-current-job",
+                   "retry-job",
+                   "stop-printer"
+                 };
+
+    if (printer->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_CLASS))
+      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
+                   "printer-error-policy-supported", NULL, "retry-current-job");
+    else
+      ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
+                   "printer-error-policy-supported",
+                   sizeof(errors) / sizeof(errors[0]), NULL, errors);
+  }
 
-      switch (attr->value_tag)
-      {
-        case IPP_TAG_STRING :
-       case IPP_TAG_TEXTLANG :
-       case IPP_TAG_NAMELANG :
-       case IPP_TAG_TEXT :
-       case IPP_TAG_NAME :
-       case IPP_TAG_KEYWORD :
-       case IPP_TAG_URI :
-       case IPP_TAG_URISCHEME :
-       case IPP_TAG_CHARSET :
-       case IPP_TAG_LANGUAGE :
-       case IPP_TAG_MIMETYPE :
-          /*
-           * Free old strings...
-           */
+  if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
+    ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
+                  printer->accepting);
 
-           for (i = 0; i < attr->num_values; i ++)
-           {
-             free(attr->values[i].string.text);
-             attr->values[i].string.text = NULL;
-             if (attr->values[i].string.charset)
-             {
-               free(attr->values[i].string.charset);
-               attr->values[i].string.charset = NULL;
-             }
-            }
+  if (!ra || cupsArrayFind(ra, "printer-is-shared"))
+    ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
+                  printer->shared);
 
-       default :
-            break;
-      }
+  if ((!ra || cupsArrayFind(ra, "printer-more-info")) &&
+      !(printer->type & CUPS_PRINTER_DISCOVERED))
+  {
+    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
+                     "http", NULL, con->servername, con->serverport,
+                    (printer->type & CUPS_PRINTER_CLASS) ?
+                        "/classes/%s" : "/printers/%s", printer->name);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
+                "printer-more-info", NULL, printer_uri);
+  }
 
-     /*
-      * Use the default connection hostname instead...
-      */
+  if (!ra || cupsArrayFind(ra, "printer-op-policy"))
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
+                "printer-op-policy", NULL, printer->op_policy);
 
-      attr->value_tag             = IPP_TAG_NAME;
-      attr->num_values            = 1;
-      attr->values[0].string.text = strdup(con->http.hostname);
-    }
+  if (!ra || cupsArrayFind(ra, "printer-state"))
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
+                  printer->state);
 
-    attr->group_tag = IPP_TAG_JOB;
-  }
-  else
+  if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
+                  "printer-state-change-time", printer->state_time);
+
+  if (MaxPrinterHistory > 0 && printer->num_history > 0 &&
+      cupsArrayFind(ra, "printer-state-history"))
   {
    /*
-    * No job-originating-host-name attribute, so use the hostname from
-    * the connection...
+    * Printer history is only sent if specifically requested, so that
+    * older CUPS/IPP clients won't barf on the collection attributes.
     */
 
-    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, 
-                "job-originating-host-name", NULL, con->http.hostname);
+    history = ippAddCollections(con->response, IPP_TAG_PRINTER,
+                                "printer-state-history",
+                                printer->num_history, NULL);
+
+    for (i = 0; i < printer->num_history; i ++)
+      copy_attrs(history->values[i].collection = ippNew(), printer->history[i],
+                 NULL, IPP_TAG_ZERO, 0);
   }
 
-  ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation",
-                time(NULL));
-  attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                       "time-at-processing", 0);
-  attr->value_tag = IPP_TAG_NOVALUE;
-  attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                       "time-at-completed", 0);
-  attr->value_tag = IPP_TAG_NOVALUE;
+  if (!ra || cupsArrayFind(ra, "printer-state-message"))
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
+                "printer-state-message", NULL, printer->state_message);
 
- /*
-  * Add remaining job attributes...
-  */
+  if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
+    add_printer_state_reasons(con, printer);
 
-  ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
-  job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
-                             "job-state", IPP_JOB_STOPPED);
-  job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                              "job-media-sheets-completed", 0);
-  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
-               printer->uri);
-  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
-               title);
+  if (!ra || cupsArrayFind(ra, "printer-type"))
+  {
+    int type;                          /* printer-type value */
 
-  if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
-    attr->values[0].integer = 0;
-  else
-    attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                         "job-k-octets", 0);
 
-  if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
-    attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
-  if (attr == NULL)
-    attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                        "job-hold-until", NULL, "no-hold");
-  if (attr != NULL && strcmp(attr->values[0].string.text, "no-hold") != 0 &&
-      !(printer->type & CUPS_PRINTER_REMOTE))
-  {
    /*
-    * Hold job until specified time...
+    * Add the CUPS-specific printer-type attribute...
     */
 
-    cupsdSetJobHoldUntil(job, attr->values[0].string.text);
+    type = printer->type;
+
+    if (printer == DefaultPrinter)
+      type |= CUPS_PRINTER_DEFAULT;
+
+    if (!printer->accepting)
+      type |= CUPS_PRINTER_REJECTING;
+
+    if (!printer->shared)
+      type |= CUPS_PRINTER_NOT_SHARED;
+
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
+                 type);
   }
-  else
-    job->hold_until = time(NULL) + 60;
 
-  job->state->values[0].integer = IPP_JOB_HELD;
+  if (!ra || cupsArrayFind(ra, "printer-up-time"))
+    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
+                  "printer-up-time", curtime);
 
-  if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
-      Classification)
+  if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) &&
+      !(printer->type & CUPS_PRINTER_DISCOVERED))
   {
-   /*
-    * Add job sheets options...
-    */
+    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
+                     "ipp", NULL, con->servername, con->serverport,
+                    (printer->type & CUPS_PRINTER_CLASS) ?
+                        "/classes/%s" : "/printers/%s", printer->name);
+    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
+                "printer-uri-supported", NULL, printer_uri);
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
+                    printer_uri);
+  }
 
-    if ((attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) == NULL)
-    {
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "Adding default job-sheets values \"%s,%s\"...",
-                      printer->job_sheets[0], printer->job_sheets[1]);
+  if (!ra || cupsArrayFind(ra, "queued-job-count"))
+    add_queued_job_count(con, printer);
 
-      attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
-                           2, NULL, NULL);
-      attr->values[0].string.text = strdup(printer->job_sheets[0]);
-      attr->values[1].string.text = strdup(printer->job_sheets[1]);
-    }
+  copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0);
+  if (printer->ppd_attrs)
+    copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0);
+  copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY);
+}
 
-    job->job_sheets = attr;
 
-   /*
   * Enforce classification level if set...
   */
+/*
* 'copy_subscription_attrs()' - Copy subscription attributes.
+ */
 
-    if (Classification)
-    {
-      cupsdLogMessage(CUPSD_LOG_INFO,
-                      "Classification=\"%s\", ClassifyOverride=%d",
-                      Classification ? Classification : "(null)",
-                     ClassifyOverride);
+static void
+copy_subscription_attrs(
+    cupsd_client_t       *con,         /* I - Client connection */
+    cupsd_subscription_t *sub,         /* I - Subscription */
+    cups_array_t         *ra)          /* I - Requested attributes array */
+{
+  ipp_attribute_t      *attr;          /* Current attribute */
+  char                 printer_uri[HTTP_MAX_URI];
+                                       /* Printer URI */
+  int                  count;          /* Number of events */
+  unsigned             mask;           /* Current event mask */
+  const char           *name;          /* Current event name */
 
-      if (ClassifyOverride)
-      {
-        if (!strcmp(attr->values[0].string.text, "none") &&
-           (attr->num_values == 1 ||
-            !strcmp(attr->values[1].string.text, "none")))
-        {
-        /*
-          * Force the leading banner to have the classification on it...
-         */
 
-          cupsdSetString(&attr->values[0].string.text, Classification);
+ /*
+  * Copy the subscription attributes to the response using the
+  * requested-attributes attribute that may be provided by the client.
+  */
 
-         cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED "
-                                           "job-sheets=\"%s,none\", "
-                                           "job-originating-user-name=\"%s\"",
-                        job->id, Classification, job->username);
-       }
-       else if (attr->num_values == 2 &&
-                strcmp(attr->values[0].string.text, attr->values[1].string.text) != 0 &&
-                strcmp(attr->values[0].string.text, "none") != 0 &&
-                strcmp(attr->values[1].string.text, "none") != 0)
-        {
-        /*
-         * Can't put two different security markings on the same document!
-         */
+  if (!ra || cupsArrayFind(ra, "notify-events"))
+  {
+    if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
+    {
+     /*
+      * Simple event list...
+      */
 
-          cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
+      ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
+                   (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
+                   "notify-events", NULL, name);
+    }
+    else
+    {
+     /*
+      * Complex event list...
+      */
 
-         cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED "
-                                           "job-sheets=\"%s,%s\", "
-                                           "job-originating-user-name=\"%s\"",
-                        job->id, attr->values[0].string.text,
-                        attr->values[1].string.text, job->username);
-       }
-       else if (strcmp(attr->values[0].string.text, Classification) &&
-                strcmp(attr->values[0].string.text, "none") &&
-                (attr->num_values == 1 ||
-                 (strcmp(attr->values[1].string.text, Classification) &&
-                  strcmp(attr->values[1].string.text, "none"))))
-        {
-         if (attr->num_values == 1)
-            cupsdLogMessage(CUPSD_LOG_NOTICE,
-                           "[Job %d] CLASSIFICATION OVERRIDDEN "
-                           "job-sheets=\"%s\", "
-                           "job-originating-user-name=\"%s\"",
-                      job->id, attr->values[0].string.text, job->username);
-          else
-            cupsdLogMessage(CUPSD_LOG_NOTICE,
-                           "[Job %d] CLASSIFICATION OVERRIDDEN "
-                           "job-sheets=\"%s,%s\",fffff "
-                           "job-originating-user-name=\"%s\"",
-                           job->id, attr->values[0].string.text,
-                           attr->values[1].string.text, job->username);
-        }
-      }
-      else if (strcmp(attr->values[0].string.text, Classification) != 0 &&
-               (attr->num_values == 1 ||
-              strcmp(attr->values[1].string.text, Classification) != 0))
-      {
-       /*
-        * Force the banner to have the classification on it...
-       */
+      for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
+       if (sub->mask & mask)
+          count ++;
 
-        if (attr->num_values > 1 &&
-           !strcmp(attr->values[0].string.text, attr->values[1].string.text))
+      attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
+                           (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
+                           "notify-events", count, NULL, NULL);
+
+      for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
+       if (sub->mask & mask)
        {
-          cupsdSetString(&(attr->values[0].string.text), Classification);
-          cupsdSetString(&(attr->values[1].string.text), Classification);
+          attr->values[count].string.text =
+             (char *)cupsdEventName((cupsd_eventmask_t)mask);
+
+          count ++;
        }
-        else
-       {
-          if (attr->num_values == 1 ||
-             strcmp(attr->values[0].string.text, "none"))
-            cupsdSetString(&(attr->values[0].string.text), Classification);
+    }
+  }
 
-          if (attr->num_values > 1 &&
-             strcmp(attr->values[1].string.text, "none"))
-            cupsdSetString(&(attr->values[1].string.text), Classification);
-        }
+  if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
+    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                  "notify-job-id", sub->job->id);
 
-        if (attr->num_values > 1)
-         cupsdLogMessage(CUPSD_LOG_NOTICE,
-                         "[Job %d] CLASSIFICATION FORCED "
-                         "job-sheets=\"%s,%s\", "
-                         "job-originating-user-name=\"%s\"",
-                         job->id, attr->values[0].string.text,
-                         attr->values[1].string.text, job->username);
-        else
-         cupsdLogMessage(CUPSD_LOG_NOTICE,
-                         "[Job %d] CLASSIFICATION FORCED "
-                         "job-sheets=\"%s\", "
-                         "job-originating-user-name=\"%s\"",
-                        job->id, Classification, job->username);
-      }
-    }
+  if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration")))
+    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                  "notify-lease-duration", sub->lease);
 
-   /*
-    * See if we need to add the starting sheet...
-    */
+  if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
+  {
+    httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
+                     "ipp", NULL, con->servername, con->serverport,
+                    "/printers/%s", sub->dest->name);
+    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
+                "notify-printer-uri", NULL, printer_uri);
+  }
 
-    if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
-    {
-      cupsdLogMessage(CUPSD_LOG_INFO,
-                      "Adding start banner page \"%s\" to job %d.",
-                      attr->values[0].string.text, job->id);
+  if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri")))
+    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
+                "notify-recipient-uri", NULL, sub->recipient);
+  else if (!ra || cupsArrayFind(ra, "notify-pull-method"))
+    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
+                 "notify-pull-method", NULL, "ippget");
 
-      kbytes = copy_banner(con, job, attr->values[0].string.text);
+  if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name"))
+    ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
+                "notify-subscriber-user-name", NULL, sub->owner);
 
-      cupsdUpdateQuota(printer, job->username, 0, kbytes);
-    }
-  }
-  else if ((attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
-    job->sheets = attr;
+  if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
+    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                  "notify-subscription-id", sub->id);
 
- /*
-  * Fill in the response info...
-  */
+  if (!ra || cupsArrayFind(ra, "notify-time-interval"))
+    ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                  "notify-time-interval", sub->interval);
 
-  snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
-          LocalPort, job->id);
+  if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data")))
+    ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
+                      sub->user_data, sub->user_data_len);
+}
 
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL, job_uri);
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
+/*
+ * 'create_job()' - Print a file to a printer or class.
+ */
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
-                job->state->values[0].integer);
+static void
+create_job(cupsd_client_t  *con,       /* I - Client connection */
+          ipp_attribute_t *uri)        /* I - Printer URI */
+{
+  cupsd_printer_t      *printer;       /* Printer */
+  cupsd_job_t          *job;           /* New job */
 
-  con->response->request.status.status_code = IPP_OK;
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
 
  /*
-  * Add any job subscriptions...
+  * Is the destination valid?
   */
 
-  add_job_subscriptions(con, job);
+  if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
+  {
+   /*
+    * Bad URI...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
+  }
 
  /*
-  * Set all but the first two attributes to the job attributes group...
+  * Create the job object...
   */
 
-  for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
-    attr->group_tag = IPP_TAG_JOB;
+  if ((job = add_job(con, printer, NULL)) == NULL)
+    return;
+
+  job->pending_timeout = 1;
 
  /*
   * Save and log the job...
   */
-   
-  cupsdSaveJob(job);
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d created on \"%s\" by \"%s\".",
-                  job->id, job->dest, job->username);
-
-  cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
+  cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
+             job->dest, job->username);
 }
 
 
@@ -4276,6 +5864,7 @@ create_requested_array(ipp_t *request)    /* I - IPP request */
       cupsArrayAdd(ra, "job-impressions-completed");
       cupsArrayAdd(ra, "job-k-octets");
       cupsArrayAdd(ra, "job-k-octets-processed");
+      cupsArrayAdd(ra, "job-media-progress");
       cupsArrayAdd(ra, "job-media-sheets");
       cupsArrayAdd(ra, "job-media-sheets-completed");
       cupsArrayAdd(ra, "job-message-from-operator");
@@ -4308,6 +5897,7 @@ create_requested_array(ipp_t *request)    /* I - IPP request */
       cupsArrayAdd(ra, "job-impressions-supported");
       cupsArrayAdd(ra, "job-k-octets-supported");
       cupsArrayAdd(ra, "job-media-sheets-supported");
+      cupsArrayAdd(ra, "job-settable-attributes-supported");
       cupsArrayAdd(ra, "multiple-document-jobs-supported");
       cupsArrayAdd(ra, "multiple-operation-time-out");
       cupsArrayAdd(ra, "natural-language-configured");
@@ -4315,16 +5905,20 @@ create_requested_array(ipp_t *request)  /* I - IPP request */
       cupsArrayAdd(ra, "notify-lease-duration-default");
       cupsArrayAdd(ra, "notify-lease-duration-supported");
       cupsArrayAdd(ra, "notify-max-events-supported");
-      cupsArrayAdd(ra, "notify-notify-events-default");
-      cupsArrayAdd(ra, "notify-notify-events-supported");
+      cupsArrayAdd(ra, "notify-events-default");
+      cupsArrayAdd(ra, "notify-events-supported");
       cupsArrayAdd(ra, "notify-pull-method-supported");
       cupsArrayAdd(ra, "notify-schemes-supported");
       cupsArrayAdd(ra, "operations-supported");
       cupsArrayAdd(ra, "pages-per-minute");
       cupsArrayAdd(ra, "pages-per-minute-color");
       cupsArrayAdd(ra, "pdl-override-supported");
+      cupsArrayAdd(ra, "printer-alert");
+      cupsArrayAdd(ra, "printer-alert-description");
+      cupsArrayAdd(ra, "printer-commands");
       cupsArrayAdd(ra, "printer-current-time");
       cupsArrayAdd(ra, "printer-driver-installer");
+      cupsArrayAdd(ra, "printer-dns-sd-name");
       cupsArrayAdd(ra, "printer-info");
       cupsArrayAdd(ra, "printer-is-accepting-jobs");
       cupsArrayAdd(ra, "printer-location");
@@ -4336,6 +5930,8 @@ create_requested_array(ipp_t *request)    /* I - IPP request */
       cupsArrayAdd(ra, "printer-state");
       cupsArrayAdd(ra, "printer-state-message");
       cupsArrayAdd(ra, "printer-state-reasons");
+      cupsArrayAdd(ra, "printer-settable-attributes-supported");
+      cupsArrayAdd(ra, "printer-type");
       cupsArrayAdd(ra, "printer-up-time");
       cupsArrayAdd(ra, "printer-uri-supported");
       cupsArrayAdd(ra, "queued-job-count");
@@ -4343,6 +5939,16 @@ create_requested_array(ipp_t *request)   /* I - IPP request */
       cupsArrayAdd(ra, "uri-authentication-supported");
       cupsArrayAdd(ra, "uri-security-supported");
     }
+    else if (!strcmp(value, "printer-defaults"))
+    {
+      char     *name;                  /* Option name */
+
+
+      for (name = (char *)cupsArrayFirst(CommonDefaults);
+           name;
+          name = (char *)cupsArrayNext(CommonDefaults))
+        cupsArrayAdd(ra, name);
+    }
     else if (!strcmp(value, "subscription-template"))
     {
       cupsArrayAdd(ra, "notify-attributes");
@@ -4375,10 +5981,9 @@ create_subscription(
   http_status_t        status;                 /* Policy status */
   int                  i;              /* Looping var */
   ipp_attribute_t      *attr;          /* Current attribute */
-  const char           *dest;          /* Destination */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  char                 scheme[HTTP_MAX_URI],
+                                       /* Scheme portion of URI */
                        userpass[HTTP_MAX_URI],
                                        /* Username portion of URI */
                        host[HTTP_MAX_URI],
@@ -4390,42 +5995,57 @@ create_subscription(
   cupsd_job_t          *job;           /* Job */
   int                  jobid;          /* Job ID */
   cupsd_subscription_t *sub;           /* Subscription object */
-  const char           *username,      /* requesting-user-name or authenticated username */
+  const char           *username,      /* requesting-user-name or
+                                          authenticated username */
                        *recipient,     /* notify-recipient-uri */
                        *pullmethod;    /* notify-pull-method */
   ipp_attribute_t      *user_data;     /* notify-user-data */
   int                  interval,       /* notify-time-interval */
                        lease;          /* notify-lease-duration */
   unsigned             mask;           /* notify-events */
+  ipp_attribute_t      *notify_events,/* notify-events(-default) */
+                       *notify_lease;  /* notify-lease-duration(-default) */
+
 
+#ifdef DEBUG
+  for (attr = con->request->attrs; attr; attr = attr->next)
+  {
+    if (attr->group_tag != IPP_TAG_ZERO)
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
+                      attr->value_tag, attr->name);
+    else
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
+  }
+#endif /* DEBUG */
 
  /*
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  userpass, sizeof(userpass), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  cupsdLogMessage(CUPSD_LOG_DEBUG,
+                  "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
+                  con, con->http.fd, uri->values[0].string.text);
+
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                  sizeof(scheme), userpass, sizeof(userpass), host,
+                 sizeof(host), &port, resource, sizeof(resource));
 
   if (!strcmp(resource, "/"))
   {
-    dest    = NULL;
     dtype   = (cups_ptype_t)0;
     printer = NULL;
   }
   else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
   {
-    dest    = NULL;
     dtype   = (cups_ptype_t)0;
     printer = NULL;
   }
   else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
   {
-    dest    = NULL;
     dtype   = CUPS_PRINTER_CLASS;
     printer = NULL;
   }
-  else if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -4442,15 +6062,16 @@ create_subscription(
 
   if (printer)
   {
-    if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+    if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
+                                   NULL)) != HTTP_OK)
     {
-      send_http_error(con, status);
+      send_http_error(con, status, printer);
       return;
     }
   }
   else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, NULL);
     return;
   }
 
@@ -4458,19 +6079,7 @@ create_subscription(
   * Get the user that is requesting the subscription...
   */
 
-  if (con->username[0])
-    username = con->username;
-  else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
-                                    IPP_TAG_NAME)) != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "create_subscription: requesting-user-name = \"%s\"",
-                    attr->values[0].string.text);
-
-    username = attr->values[0].string.text;
-  }
-  else
-    username = "anonymous";
+  username = get_username(con);
 
  /*
   * Find the first subscription group attribute; return if we have
@@ -4492,6 +6101,8 @@ create_subscription(
   * Process the subscription attributes in the request...
   */
 
+  con->response->request.status.status_code = IPP_BAD_REQUEST;
+
   while (attr)
   {
     recipient = NULL;
@@ -4502,14 +6113,86 @@ create_subscription(
     jobid      = 0;
     mask       = CUPSD_EVENT_NONE;
 
+    if (printer)
+    {
+      notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
+                                       IPP_TAG_KEYWORD);
+      notify_lease  = ippFindAttribute(printer->attrs,
+                                       "notify-lease-duration-default",
+                                       IPP_TAG_INTEGER);
+
+      if (notify_lease)
+        lease = notify_lease->values[0].integer;
+    }
+    else
+    {
+      notify_events = NULL;
+      notify_lease  = NULL;
+    }
+
     while (attr && attr->group_tag != IPP_TAG_ZERO)
     {
-      if (!strcmp(attr->name, "notify-recipient") &&
+      if (!strcmp(attr->name, "notify-recipient-uri") &&
           attr->value_tag == IPP_TAG_URI)
+      {
+       /*
+        * Validate the recipient scheme against the ServerBin/notifier
+       * directory...
+       */
+
+       char    notifier[1024];         /* Notifier filename */
+
+
         recipient = attr->values[0].string.text;
+
+       if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
+                           scheme, sizeof(scheme), userpass, sizeof(userpass),
+                           host, sizeof(host), &port,
+                           resource, sizeof(resource)) < HTTP_URI_OK)
+        {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Bad notify-recipient-uri URI \"%s\"!"), recipient);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_URI_SCHEME);
+         return;
+       }
+
+        snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
+                scheme);
+        if (access(notifier, X_OK))
+       {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("notify-recipient-uri URI \"%s\" uses unknown "
+                           "scheme!"), recipient);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_URI_SCHEME);
+         return;
+       }
+
+        if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
+       {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("notify-recipient-uri URI \"%s\" is already used!"),
+                         recipient);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_ATTRIBUTES);
+         return;
+       }
+      }
       else if (!strcmp(attr->name, "notify-pull-method") &&
                attr->value_tag == IPP_TAG_KEYWORD)
+      {
         pullmethod = attr->values[0].string.text;
+
+        if (strcmp(pullmethod, "ippget"))
+       {
+          send_ipp_status(con, IPP_NOT_POSSIBLE,
+                         _("Bad notify-pull-method \"%s\"!"), pullmethod);
+         ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
+                       "notify-status-code", IPP_ATTRIBUTES);
+         return;
+       }
+      }
       else if (!strcmp(attr->name, "notify-charset") &&
                attr->value_tag == IPP_TAG_CHARSET &&
               strcmp(attr->values[0].string.text, "us-ascii") &&
@@ -4545,10 +6228,7 @@ create_subscription(
       }
       else if (!strcmp(attr->name, "notify-events") &&
                attr->value_tag == IPP_TAG_KEYWORD)
-      {
-        for (i = 0; i < attr->num_values; i ++)
-         mask |= cupsdEventValue(attr->values[i].string.text);
-      }
+        notify_events = attr;
       else if (!strcmp(attr->name, "notify-lease-duration") &&
                attr->value_tag == IPP_TAG_INTEGER)
         lease = attr->values[0].integer;
@@ -4562,6 +6242,19 @@ create_subscription(
       attr = attr->next;
     }
 
+    if (notify_events)
+    {
+      for (i = 0; i < notify_events->num_values; i ++)
+       mask |= cupsdEventValue(notify_events->values[i].string.text);
+    }
+
+    if (recipient)
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
+    if (pullmethod)
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
+
     if (!recipient && !pullmethod)
       break;
 
@@ -4579,7 +6272,7 @@ create_subscription(
       }
     }
 
-    if (MaxLeaseDuration && lease > MaxLeaseDuration)
+    if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
     {
       cupsdLogMessage(CUPSD_LOG_INFO,
                       "create_subscription: Limiting notify-lease-duration to "
@@ -4599,11 +6292,27 @@ create_subscription(
     else
       job = NULL;
 
-    sub = cupsdAddSubscription(mask, printer, job, recipient, 0);
+    if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL)
+    {
+      send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS,
+                     _("There are too many subscriptions."));
+      return;
+    }
+
+    if (job)
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d",
+                     sub->id, job->id);
+    else if (printer)
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                      "Added subscription %d for printer \"%s\"",
+                     sub->id, printer->name);
+    else
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server",
+                     sub->id);
 
     sub->interval = interval;
     sub->lease    = lease;
-    sub->expire   = time(NULL) + lease;
+    sub->expire   = lease ? time(NULL) + lease : 0;
 
     cupsdSetString(&sub->owner, username);
 
@@ -4618,13 +6327,13 @@ create_subscription(
     ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
                   "notify-subscription-id", sub->id);
 
+    con->response->request.status.status_code = IPP_OK;
+
     if (attr)
       attr = attr->next;
   }
 
-  cupsdSaveAllSubscriptions();
-
-  con->response->request.status.status_code = IPP_OK;
+  cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
 }
 
 
@@ -4637,13 +6346,7 @@ delete_printer(cupsd_client_t  *con,     /* I - Client connection */
                ipp_attribute_t *uri)   /* I - URI of printer or class */
 {
   http_status_t        status;                 /* Policy status */
-  const char   *dest;                  /* Destination */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
+  cups_ptype_t dtype;                  /* Destination type (printer/class) */
   cupsd_printer_t *printer;            /* Printer/class */
   char         filename[1024];         /* Script/PPD filename */
 
@@ -4655,11 +6358,7 @@ delete_printer(cupsd_client_t  *con,     /* I - Client connection */
   * Do we have a valid URI?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -4676,7 +6375,7 @@ delete_printer(cupsd_client_t  *con,      /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, NULL);
     return;
   }
 
@@ -4684,7 +6383,7 @@ delete_printer(cupsd_client_t  *con,      /* I - Client connection */
   * Remove old jobs...
   */
 
-  cupsdCancelJobs(dest, NULL, 1);
+  cupsdCancelJobs(printer->name, NULL, 1);
 
  /*
   * Remove old subscriptions and send a "deleted printer" event...
@@ -4693,38 +6392,51 @@ delete_printer(cupsd_client_t  *con,    /* I - Client connection */
   cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
                 "%s \"%s\" deleted by \"%s\".",
                (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
-               dest, con->username);
+               printer->name, get_username(con));
 
   cupsdExpireSubscriptions(printer, NULL);
+
  /*
   * Remove any old PPD or script files...
   */
 
-  snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot, dest);
+  snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
+           printer->name);
   unlink(filename);
 
-  snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest);
+  snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
+           printer->name);
   unlink(filename);
 
+  snprintf(filename, sizeof(filename), "%s/%s.ipp", CacheDir, printer->name);
+  unlink(filename);
+
+#ifdef __APPLE__
+ /*
+  * Unregister color profiles...
+  */
+
+  apple_unregister_profiles(printer);
+#endif /* __APPLE__ */
+
   if (dtype & CUPS_PRINTER_CLASS)
   {
-    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".", dest,
-                    con->username);
+    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
+                    printer->name, get_username(con));
 
     cupsdDeletePrinter(printer, 0);
-    cupsdSaveAllClasses();
+    cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
   }
   else
   {
-    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".", dest,
-                    con->username);
+    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
+                    printer->name, get_username(con));
 
     cupsdDeletePrinter(printer, 0);
-    cupsdSaveAllPrinters();
+    cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
   }
 
-  cupsdWritePrintcap();
+  cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
 
  /*
   * Return with no errors...
@@ -4741,14 +6453,8 @@ delete_printer(cupsd_client_t  *con,     /* I - Client connection */
 static void
 get_default(cupsd_client_t *con)       /* I - Client connection */
 {
-  http_status_t                status;         /* Policy status */
-  int                  i;              /* Looping var */
-  ipp_attribute_t      *requested,     /* requested-attributes */
-                       *history;       /* History collection */
-  int                  need_history;   /* Need to send history collection? */
-  char                 printer_uri[HTTP_MAX_URI];
-                                       /* Printer URI */
-  time_t               curtime;        /* Current time */
+  http_status_t        status;                 /* Policy status */
+  cups_array_t *ra;                    /* Requested attributes array */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd);
@@ -4759,196 +6465,366 @@ get_default(cupsd_client_t *con)      /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, NULL);
+    return;
+  }
+
+  if (DefaultPrinter)
+  {
+    ra = create_requested_array(con->request);
+
+    copy_printer_attrs(con, DefaultPrinter, ra);
+
+    cupsArrayDelete(ra);
+
+    con->response->request.status.status_code = IPP_OK;
+  }
+  else
+    send_ipp_status(con, IPP_NOT_FOUND, _("No default printer"));
+}
+
+
+/*
+ * 'get_devices()' - Get the list of available devices on the local system.
+ */
+
+static void
+get_devices(cupsd_client_t *con)       /* I - Client connection */
+{
+  http_status_t                status;         /* Policy status */
+  ipp_attribute_t      *limit,         /* limit attribute */
+                       *timeout,       /* timeout attribute */
+                       *requested,     /* requested-attributes attribute */
+                       *exclude,       /* exclude-schemes attribute */
+                       *include;       /* include-schemes attribute */
+  char                 command[1024],  /* cups-deviced command */
+                       options[2048],  /* Options to pass to command */
+                       requested_str[256],
+                                       /* String for requested attributes */
+                       exclude_str[512],
+                                       /* String for excluded schemes */
+                       include_str[512];
+                                       /* String for included schemes */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd);
+
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, NULL);
+    return;
+  }
+
+ /*
+  * Run cups-deviced command with the given options...
+  */
+
+  limit     = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
+  timeout   = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
+  requested = ippFindAttribute(con->request, "requested-attributes",
+                               IPP_TAG_KEYWORD);
+  exclude   = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
+  include   = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME);
+
+  if (requested)
+    url_encode_attr(requested, requested_str, sizeof(requested_str));
+  else
+    strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
+
+  if (exclude)
+    url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
+  else
+    exclude_str[0] = '\0';
+
+  if (include)
+    url_encode_attr(include, include_str, sizeof(include_str));
+  else
+    include_str[0] = '\0';
+
+  snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
+  snprintf(options, sizeof(options),
+           "%d+%d+%d+%d+%s%s%s%s%s",
+           con->request->request.op.request_id,
+           limit ? limit->values[0].integer : 0,
+          timeout ? timeout->values[0].integer : 10,
+          (int)User,
+          requested_str,
+          exclude_str[0] ? "%20" : "", exclude_str,
+          include_str[0] ? "%20" : "", include_str);
+
+  if (cupsdSendCommand(con, command, options, 1))
+  {
+   /*
+    * Command started successfully, don't send an IPP response here...
+    */
+
+    ippDelete(con->response);
+    con->response = NULL;
+  }
+  else
+  {
+   /*
+    * Command failed, return "internal error" so the user knows something
+    * went wrong...
+    */
+
+    send_ipp_status(con, IPP_INTERNAL_ERROR,
+                    _("cups-deviced failed to execute."));
+  }
+}
+
+
+/*
+ * 'get_document()' - Get a copy of a job file.
+ */
+
+static void
+get_document(cupsd_client_t  *con,     /* I - Client connection */
+             ipp_attribute_t *uri)     /* I - Job URI */
+{
+  http_status_t        status;                 /* Policy status */
+  ipp_attribute_t *attr;               /* Current attribute */
+  int          jobid;                  /* Job ID */
+  int          docnum;                 /* Document number */
+  cupsd_job_t  *job;                   /* Current job */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
+               username[HTTP_MAX_URI], /* Username portion of URI */
+               host[HTTP_MAX_URI],     /* Host portion of URI */
+               resource[HTTP_MAX_URI]; /* Resource portion of URI */
+  int          port;                   /* Port portion of URI */
+  char         filename[1024],         /* Filename for document */
+               format[1024];           /* Format for document */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
+
+ /*
+  * See if we have a job URI or a printer URI...
+  */
+
+  if (!strcmp(uri->name, "printer-uri"))
+  {
+   /*
+    * Got a printer URI; see if we also have a job-id attribute...
+    */
+
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
+    {
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Got a printer-uri attribute but no job-id!"));
+      return;
+    }
+
+    jobid = attr->values[0].integer;
+  }
+  else
+  {
+   /*
+    * Got a job URI; parse it to get the job ID...
+    */
+
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
+
+    if (strncmp(resource, "/jobs/", 6))
+    {
+     /*
+      * Not a valid URI!
+      */
+
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Bad job-uri attribute \"%s\"!"),
+                      uri->values[0].string.text);
+      return;
+    }
+
+    jobid = atoi(resource + 6);
+  }
+
+ /*
+  * See if the job exists...
+  */
+
+  if ((job = cupsdFindJob(jobid)) == NULL)
+  {
+   /*
+    * Nope - return a "not found" error...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
+    return;
+  }
+
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, NULL);
     return;
   }
 
-  if (DefaultPrinter != NULL)
-  {
-   /*
-    * Copy the printer attributes to the response using requested-attributes
-    * and document-format attributes that may be provided by the client.
-    */
-
-    if (!ippFindAttribute(DefaultPrinter->attrs, "printer-uri-supported",
-                          IPP_TAG_URI))
-    {
-      httpAssembleURIf(printer_uri, sizeof(printer_uri), "ipp", NULL,
-                       con->servername, con->serverport, "/printers/%s",
-                      DefaultPrinter->name);
-      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
-                  "printer-uri-supported", NULL, printer_uri);
-      cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
-                      printer_uri);
-    }
-
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
-                  DefaultPrinter->state);
-
-    add_printer_state_reasons(con, DefaultPrinter);
-
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
-                "printer-state-message", NULL, DefaultPrinter->state_message);
-
-    ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
-                  DefaultPrinter->accepting);
-    ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
-                  DefaultPrinter->shared);
-
-    curtime = time(NULL);
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                  "printer-up-time", curtime);
-    ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                  "printer-state-change-time", DefaultPrinter->state_time);
-    ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
-               ippTimeToDate(curtime));
-
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                "printer-error-policy", NULL, DefaultPrinter->error_policy);
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                "printer-op-policy", NULL, DefaultPrinter->op_policy);
-
-    add_queued_job_count(con, DefaultPrinter);
+ /*
+  * Get the document number...
+  */
 
-    requested = ippFindAttribute(con->request, "requested-attributes",
-                                IPP_TAG_KEYWORD);
+  if ((attr = ippFindAttribute(con->request, "document-number",
+                               IPP_TAG_INTEGER)) == NULL)
+  {
+    send_ipp_status(con, IPP_BAD_REQUEST,
+                    _("Missing document-number attribute!"));
+    return;
+  }
 
-    copy_attrs(con->response, DefaultPrinter->attrs, requested, IPP_TAG_ZERO, 0);
-    copy_attrs(con->response, CommonData, requested, IPP_TAG_ZERO, IPP_TAG_COPY);
+  if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
+      attr->num_values > 1)
+  {
+    send_ipp_status(con, IPP_NOT_FOUND, _("Document %d not found in job %d."),
+                    docnum, jobid);
+    return;
+  }
 
-    need_history = 0;
+  snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
+           docnum);
+  if ((con->file = open(filename, O_RDONLY)) == -1)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR,
+                    "Unable to open document %d in job %d - %s", docnum, jobid,
+                   strerror(errno));
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("Unable to open document %d in job %d!"), docnum, jobid);
+    return;
+  }
 
-    if (MaxPrinterHistory > 0 && DefaultPrinter->num_history > 0 && requested)
-    {
-      for (i = 0; i < requested->num_values; i ++)
-       if (!strcmp(requested->values[i].string.text, "all") ||
-            !strcmp(requested->values[i].string.text, "printer-state-history"))
-       {
-          need_history = 1;
-          break;
-       }
-    }
+  fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
 
-    if (need_history)
-    {
-      history = ippAddCollections(con->response, IPP_TAG_PRINTER,
-                                  "printer-state-history",
-                                  DefaultPrinter->num_history, NULL);
+  cupsdLoadJob(job);
 
-      for (i = 0; i < DefaultPrinter->num_history; i ++)
-       copy_attrs(history->values[i].collection = ippNew(),
-                  DefaultPrinter->history[i],
-                   NULL, IPP_TAG_ZERO, 0);
-    }
+  snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
+           job->filetypes[docnum - 1]->type);
 
-    con->response->request.status.status_code = requested ? IPP_OK_SUBST : IPP_OK;
-  }
-  else
-    send_ipp_status(con, IPP_NOT_FOUND, _("No default printer"));
+  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
+               NULL, format);
+  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
+                docnum);
+  if ((attr = ippFindAttribute(job->attrs, "document-name",
+                               IPP_TAG_NAME)) != NULL)
+    ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
+                 NULL, attr->values[0].string.text);
 }
 
 
 /*
- * 'get_devices()' - Get the list of available devices on the local system.
+ * 'get_job_attrs()' - Get job attributes.
  */
 
 static void
-get_devices(cupsd_client_t *con)       /* I - Client connection */
+get_job_attrs(cupsd_client_t  *con,    /* I - Client connection */
+             ipp_attribute_t *uri)     /* I - Job URI */
 {
-  http_status_t                status;         /* Policy status */
-  int                  i;              /* Looping var */
-  ipp_attribute_t      *limit,         /* Limit attribute */
-                       *requested;     /* requested-attributes attribute */
-  char                 command[1024],  /* cups-deviced command */
-                       options[1024],  /* Options to pass to command */
-                       attrs[1024],    /* String for requested attributes */
-                       *aptr;          /* Pointer into string */
-  int                  alen;           /* Length of attribute value */
+  http_status_t        status;                 /* Policy status */
+  ipp_attribute_t *attr;               /* Current attribute */
+  int          jobid;                  /* Job ID */
+  cupsd_job_t  *job;                   /* Current job */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
+               username[HTTP_MAX_URI], /* Username portion of URI */
+               host[HTTP_MAX_URI],     /* Host portion of URI */
+               resource[HTTP_MAX_URI]; /* Resource portion of URI */
+  int          port;                   /* Port portion of URI */
+  cups_array_t *ra;                    /* Requested attributes array */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd);
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
 
  /*
-  * Check policy...
+  * See if we have a job URI or a printer URI...
   */
 
-  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+  if (!strcmp(uri->name, "printer-uri"))
   {
-    send_http_error(con, status);
-    return;
-  }
-
- /*
-  * Run cups-deviced command with the given options...
-  */
-
-  limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
-  requested = ippFindAttribute(con->request, "requested-attributes",
-                               IPP_TAG_KEYWORD);
+   /*
+    * Got a printer URI; see if we also have a job-id attribute...
+    */
 
-  if (requested)
-  {
-    for (i = 0, aptr = attrs; i < requested->num_values; i ++)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
-     /*
-      * Check that we have enough room...
-      */
-
-      alen = strlen(requested->values[i].string.text);
-      if (alen > (sizeof(attrs) - (aptr - attrs) - 2))
-        break;
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Got a printer-uri attribute but no job-id!"));
+      return;
+    }
 
-     /*
-      * Put commas between values...
-      */
+    jobid = attr->values[0].integer;
+  }
+  else
+  {
+   /*
+    * Got a job URI; parse it to get the job ID...
+    */
 
-      if (i)
-        *aptr++ = ',';
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
+    if (strncmp(resource, "/jobs/", 6))
+    {
      /*
-      * Add the value to the end of the string...
+      * Not a valid URI!
       */
 
-      strcpy(aptr, requested->values[i].string.text);
-      aptr += alen;
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Bad job-uri attribute \"%s\"!"),
+                      uri->values[0].string.text);
+      return;
     }
 
-   /*
-    * If we have more attribute names than will fit, default to "all"...
-    */
-
-    if (i < requested->num_values)
-      strcpy(attrs, "all");
+    jobid = atoi(resource + 6);
   }
-  else
-    strcpy(attrs, "all");
 
-  snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
-  snprintf(options, sizeof(options),
-           "cups-deviced %d+%d+requested-attributes=%s",
-           con->request->request.op.request_id,
-           limit ? limit->values[0].integer : 0,
-          attrs);
+ /*
+  * See if the job exists...
+  */
 
-  if (cupsdSendCommand(con, command, options, 1))
+  if ((job = cupsdFindJob(jobid)) == NULL)
   {
    /*
-    * Command started successfully, don't send an IPP response here...
+    * Nope - return a "not found" error...
     */
 
-    ippDelete(con->response);
-    con->response = NULL;
+    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
+    return;
   }
-  else
-  {
-   /*
-    * Command failed, return "internal error" so the user knows something
-    * went wrong...
-    */
 
-    send_ipp_status(con, IPP_INTERNAL_ERROR,
-                    _("cups-deviced failed to execute."));
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, NULL);
+    return;
   }
+
+ /*
+  * Copy attributes...
+  */
+
+  cupsdLoadJob(job);
+
+  ra = create_requested_array(con->request);
+  copy_job_attrs(con, job, ra);
+  cupsArrayDelete(ra);
+
+  con->response->request.status.status_code = IPP_OK;
 }
 
 
@@ -4961,12 +6837,11 @@ get_jobs(cupsd_client_t  *con,          /* I - Client connection */
         ipp_attribute_t *uri)          /* I - Printer URI */
 {
   http_status_t        status;                 /* Policy status */
-  ipp_attribute_t *attr,               /* Current attribute */
-               *requested;             /* Requested attributes */
+  ipp_attribute_t *attr;               /* Current attribute */
   const char   *dest;                  /* Destination */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
+  cups_ptype_t dtype;                  /* Destination type (printer/class) */
   cups_ptype_t dmask;                  /* Destination type mask */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  char         scheme[HTTP_MAX_URI],   /* Scheme portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
@@ -4976,9 +6851,9 @@ get_jobs(cupsd_client_t  *con,            /* I - Client connection */
   int          limit;                  /* Maximum number of jobs to return */
   int          count;                  /* Number of jobs that match */
   cupsd_job_t  *job;                   /* Current job pointer */
-  char         job_uri[HTTP_MAX_URI];  /* Job URI... */
   cupsd_printer_t *printer;            /* Printer */
   cups_array_t *list;                  /* Which job list... */
+  cups_array_t *ra;                    /* Requested attributes array */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd,
@@ -4988,33 +6863,39 @@ get_jobs(cupsd_client_t  *con,          /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  if (strcmp(uri->name, "printer-uri"))
+  {
+    send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request!"));
+    return;
+  }
+
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                  sizeof(scheme), username, sizeof(username), host,
+                 sizeof(host), &port, resource, sizeof(resource));
 
-  if (strcmp(resource, "/") == 0 ||
-      (strncmp(resource, "/jobs", 5) == 0 && strlen(resource) <= 6))
+  if (!strcmp(resource, "/") || !strcmp(resource, "/jobs"))
   {
     dest    = NULL;
     dtype   = (cups_ptype_t)0;
     dmask   = (cups_ptype_t)0;
     printer = NULL;
   }
-  else if (strncmp(resource, "/printers", 9) == 0 && strlen(resource) <= 10)
+  else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
   {
     dest    = NULL;
     dtype   = (cups_ptype_t)0;
     dmask   = CUPS_PRINTER_CLASS;
     printer = NULL;
   }
-  else if (strncmp(resource, "/classes", 8) == 0 && strlen(resource) <= 9)
+  else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
   {
     dest    = NULL;
     dtype   = CUPS_PRINTER_CLASS;
     dmask   = CUPS_PRINTER_CLASS;
     printer = NULL;
   }
-  else if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
+                                     &printer)) == NULL)
   {
    /*
     * Bad URI...
@@ -5025,7 +6906,10 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
     return;
   }
   else
+  {
+    dtype &= CUPS_PRINTER_CLASS;
     dmask = CUPS_PRINTER_CLASS;
+  }
 
  /*
   * Check policy...
@@ -5033,15 +6917,16 @@ get_jobs(cupsd_client_t  *con,          /* I - Client connection */
 
   if (printer)
   {
-    if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+    if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
+                                   NULL)) != HTTP_OK)
     {
-      send_http_error(con, status);
+      send_http_error(con, status, printer);
       return;
     }
   }
   else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, NULL);
     return;
   }
 
@@ -5049,7 +6934,8 @@ get_jobs(cupsd_client_t  *con,            /* I - Client connection */
   * See if the "which-jobs" attribute have been specified...
   */
 
-  if ((attr = ippFindAttribute(con->request, "which-jobs", IPP_TAG_KEYWORD)) != NULL &&
+  if ((attr = ippFindAttribute(con->request, "which-jobs",
+                               IPP_TAG_KEYWORD)) != NULL &&
       !strcmp(attr->values[0].string.text, "completed"))
   {
     completed = 1;
@@ -5060,6 +6946,11 @@ get_jobs(cupsd_client_t  *con,           /* I - Client connection */
     completed = 0;
     list      = Jobs;
   }
+  else if (attr && !strcmp(attr->values[0].string.text, "processing"))
+  {
+    completed = 0;
+    list      = PrintingJobs;
+  }
   else
   {
     completed = 0;
@@ -5070,10 +6961,11 @@ get_jobs(cupsd_client_t  *con,          /* I - Client connection */
   * See if they want to limit the number of jobs reported...
   */
 
-  if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "limit",
+                               IPP_TAG_INTEGER)) != NULL)
     limit = attr->values[0].integer;
   else
-    limit = 1000000;
+    limit = 0;
 
   if ((attr = ippFindAttribute(con->request, "first-job-id",
                                IPP_TAG_INTEGER)) != NULL)
@@ -5085,228 +6977,358 @@ get_jobs(cupsd_client_t  *con,                /* I - Client connection */
   * See if we only want to see jobs for a specific user...
   */
 
-  if ((attr = ippFindAttribute(con->request, "my-jobs", IPP_TAG_BOOLEAN)) != NULL &&
+  if ((attr = ippFindAttribute(con->request, "my-jobs",
+                               IPP_TAG_BOOLEAN)) != NULL &&
       attr->values[0].boolean)
-  {
-    if (con->username[0])
-      strlcpy(username, con->username, sizeof(username));
-    else if ((attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME)) != NULL)
-      strlcpy(username, attr->values[0].string.text, sizeof(username));
-    else
-      strcpy(username, "anonymous");
-  }
+    strlcpy(username, get_username(con), sizeof(username));
   else
     username[0] = '\0';
 
-  requested = ippFindAttribute(con->request, "requested-attributes",
-                              IPP_TAG_KEYWORD);
+  ra = create_requested_array(con->request);
 
  /*
   * OK, build a list of jobs for this printer...
   */
 
   for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
-       count < limit && job;
+       (limit <= 0 || count < limit) && job;
        job = (cupsd_job_t *)cupsArrayNext(list))
   {
    /*
     * Filter out jobs that don't match...
     */
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id);
+    cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                    "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
+                   "state_value=%d, attrs=%p", job->id, job->dest,
+                   job->username, job->state_value, job->attrs);
+
+    if (!job->dest || !job->username)
+      cupsdLoadJob(job);
+
+    if (!job->dest || !job->username)
+      continue;
+
+    if ((dest && strcmp(job->dest, dest)) &&
+        (!job->printer || !dest || strcmp(job->printer->name, dest)))
+      continue;
+    if ((job->dtype & dmask) != dtype &&
+        (!job->printer || (job->printer->type & dmask) != dtype))
+      continue;
+    if (completed && job->state_value <= IPP_JOB_STOPPED)
+      continue;
+
+    if (job->id < first_job_id)
+      continue;
+
+    cupsdLoadJob(job);
+
+    if (!job->attrs)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d!",
+                      job->id);
+      continue;
+    }
+
+    if (username[0] && strcasecmp(username, job->username))
+      continue;
+
+    if (count > 0)
+      ippAddSeparator(con->response);
+
+    count ++;
+
+    copy_job_attrs(con, job, ra);
+  }
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count);
+
+  cupsArrayDelete(ra);
+
+  con->response->request.status.status_code = IPP_OK;
+}
+
+
+/*
+ * 'get_notifications()' - Get events for a subscription.
+ */
+
+static void
+get_notifications(cupsd_client_t *con) /* I - Client connection */
+{
+  int                  i, j;           /* Looping vars */
+  http_status_t                status;         /* Policy status */
+  cupsd_subscription_t *sub;           /* Subscription */
+  ipp_attribute_t      *ids,           /* notify-subscription-ids */
+                       *sequences;     /* notify-sequence-numbers */
+  int                  min_seq;        /* Minimum sequence number */
+  int                  interval;       /* Poll interval */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
+                  con, con->http.fd);
+
+ /*
+  * Get subscription attributes...
+  */
+
+  ids       = ippFindAttribute(con->request, "notify-subscription-ids",
+                               IPP_TAG_INTEGER);
+  sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
+                               IPP_TAG_INTEGER);
+
+  if (!ids)
+  {
+    send_ipp_status(con, IPP_BAD_REQUEST,
+                    _("Missing notify-subscription-ids attribute!"));
+    return;
+  }
+
+ /*
+  * Are the subscription IDs valid?
+  */
+
+  for (i = 0, interval = 60; i < ids->num_values; i ++)
+  {
+    if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
+    {
+     /*
+      * Bad subscription ID...
+      */
+
+      send_ipp_status(con, IPP_NOT_FOUND,
+                      _("notify-subscription-id %d no good!"),
+                     ids->values[i].integer);
+      return;
+    }
+
+   /*
+    * Check policy...
+    */
+
+    if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
+                                               DefaultPolicyPtr,
+                                   con, sub->owner)) != HTTP_OK)
+    {
+      send_http_error(con, status, sub->dest);
+      return;
+    }
+
+   /*
+    * Check the subscription type and update the interval accordingly.
+    */
+
+    if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
+        interval > 10)
+      interval = 10;
+    else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
+      interval = 0;
+    else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
+             interval > 30)
+      interval = 30;
+  }
 
-    if ((dest != NULL && strcmp(job->dest, dest) != 0) &&
-        (job->printer == NULL || dest == NULL ||
-        strcmp(job->printer->name, dest) != 0))
-      continue;
-    if ((job->dtype & dmask) != dtype &&
-        (job->printer == NULL || (job->printer->type & dmask) != dtype))
-      continue;
-    if (username[0] != '\0' && strcasecmp(username, job->username) != 0)
-      continue;
+ /*
+  * Tell the client to poll again in N seconds...
+  */
 
-    if (completed && job->state->values[0].integer <= IPP_JOB_STOPPED)
-      continue;
+  if (interval > 0)
+    ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
+                  "notify-get-interval", interval);
 
-    if (job->id < first_job_id)
-      continue;
+  ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
+                "printer-up-time", time(NULL));
 
-    count ++;
+ /*
+  * Copy the subscription event attributes to the response.
+  */
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count);
+  con->response->request.status.status_code =
+      interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
 
+  for (i = 0; i < ids->num_values; i ++)
+  {
    /*
-    * Send the requested attributes for each job...
+    * Get the subscription and sequence number...
     */
 
-    snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
-            LocalPort, job->id);
+    sub = cupsdFindSubscription(ids->values[i].integer);
 
-    ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
-                 "job-more-info", NULL, job_uri);
+    if (sequences && i < sequences->num_values)
+      min_seq = sequences->values[i].integer;
+    else
+      min_seq = 1;
 
-    ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
-                 "job-uri", NULL, job_uri);
+   /*
+    * If we don't have any new events, nothing to do here...
+    */
 
-    ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                  "job-printer-up-time", time(NULL));
+    if (min_seq > (sub->first_event_id + sub->num_events))
+      continue;
 
    /*
-    * Copy the job attributes to the response using the requested-attributes
-    * attribute that may be provided by the client.
+    * Otherwise copy all of the new events...
     */
 
-    copy_attrs(con->response, job->attrs, requested, IPP_TAG_JOB, 0);
+    if (sub->first_event_id > min_seq)
+      j = 0;
+    else
+      j = min_seq - sub->first_event_id;
 
-    add_job_state_reasons(con, job);
+    for (; j < sub->num_events; j ++)
+    {
+      ippAddSeparator(con->response);
 
-    ippAddSeparator(con->response);
+      copy_attrs(con->response, sub->events[j]->attrs, NULL,
+                IPP_TAG_EVENT_NOTIFICATION, 0);
+    }
   }
-
-  if (requested != NULL)
-    con->response->request.status.status_code = IPP_OK_SUBST;
-  else
-    con->response->request.status.status_code = IPP_OK;
 }
 
 
 /*
- * 'get_job_attrs()' - Get job attributes.
+ * 'get_ppd()' - Get a named PPD from the local system.
  */
 
 static void
-get_job_attrs(cupsd_client_t  *con,    /* I - Client connection */
-             ipp_attribute_t *uri)     /* I - Job URI */
+get_ppd(cupsd_client_t  *con,          /* I - Client connection */
+        ipp_attribute_t *uri)          /* I - Printer URI or PPD name */
 {
-  http_status_t        status;                 /* Policy status */
-  ipp_attribute_t *attr,               /* Current attribute */
-               *requested;             /* Requested attributes */
-  int          jobid;                  /* Job ID */
-  cupsd_job_t  *job;                   /* Current job */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
-  char         job_uri[HTTP_MAX_URI];  /* Job URI... */
-
+  http_status_t                status;         /* Policy status */
+  cupsd_printer_t      *dest;          /* Destination */
+  cups_ptype_t         dtype;          /* Destination type */
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
-                  con->http.fd, uri->values[0].string.text);
 
- /*
-  * See if we have a job URI or a printer URI...
-  */
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
+                  con->http.fd, uri, uri->name, uri->values[0].string.text);
 
-  if (strcmp(uri->name, "printer-uri") == 0)
+  if (!strcmp(uri->name, "ppd-name"))
   {
    /*
-    * Got a printer URI; see if we also have a job-id attribute...
+    * Return a PPD file from cups-driverd...
+    */
+
+    char       command[1024],  /* cups-driverd command */
+               options[1024],  /* Options to pass to command */
+               ppd_name[1024]; /* ppd-name */
+
+
+   /*
+    * Check policy...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
     {
-      send_ipp_status(con, IPP_BAD_REQUEST,
-                      _("Got a printer-uri attribute but no job-id!"));
+      send_http_error(con, status, NULL);
       return;
     }
 
-    jobid = attr->values[0].integer;
-  }
-  else
-  {
    /*
-    * Got a job URI; parse it to get the job ID...
+    * Run cups-driverd command with the given options...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
+    url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name));
+    snprintf(options, sizeof(options), "get+%d+%s",
+             con->request->request.op.request_id, ppd_name);
 
-    if (strncmp(resource, "/jobs/", 6) != 0)
+    if (cupsdSendCommand(con, command, options, 0))
     {
      /*
-      * Not a valid URI!
+      * Command started successfully, don't send an IPP response here...
       */
 
-      send_ipp_status(con, IPP_BAD_REQUEST,
-                      _("Bad job-uri attribute \"%s\"!"),
-                      uri->values[0].string.text);
-      return;
+      ippDelete(con->response);
+      con->response = NULL;
     }
+    else
+    {
+     /*
+      * Command failed, return "internal error" so the user knows something
+      * went wrong...
+      */
 
-    jobid = atoi(resource + 6);
+      send_ipp_status(con, IPP_INTERNAL_ERROR,
+                     _("cups-driverd failed to execute."));
+    }
   }
+  else if (!strcmp(uri->name, "printer-uri") &&
+           cupsdValidateDest(uri->values[0].string.text, &dtype, &dest))
+  {
+    int        i;                      /* Looping var */
+    char       filename[1024];         /* PPD filename */
 
- /*
-  * See if the job exists...
-  */
 
-  if ((job = cupsdFindJob(jobid)) == NULL)
-  {
    /*
-    * Nope - return a "not found" error...
+    * Check policy...
     */
 
-    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
-    return;
-  }
-
- /*
-  * Check policy...
-  */
-
-  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
-  {
-    send_http_error(con, status);
-    return;
-  }
-
- /*
-  * Put out the standard attributes...
-  */
+    if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
+    {
+      send_http_error(con, status, dest);
+      return;
+    }
 
-  snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d",
-          ServerName, LocalPort, job->id);
+   /*
+    * See if we need the PPD for a class or remote printer...
+    */
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
+    snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
+             dest->name);
 
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
-               "job-more-info", NULL, job_uri);
+    if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
+    {
+      con->response->request.status.status_code = CUPS_SEE_OTHER;
+      ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
+                   "printer-uri", NULL, dest->uri);
+      return;
+    }
+    else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
+    {
+      for (i = 0; i < dest->num_printers; i ++)
+        if (!(dest->printers[i]->type &
+             (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)))
+       {
+         snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
+                  dest->printers[i]->name);
 
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
-               "job-uri", NULL, job_uri);
+          if (!access(filename, 0))
+           break;
+        }
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                "job-printer-up-time", time(NULL));
+      if (i < dest->num_printers)
+        dest = dest->printers[i];
+      else
+      {
+        con->response->request.status.status_code = CUPS_SEE_OTHER;
+       ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
+                    "printer-uri", NULL, dest->printers[0]->uri);
+        return;
+      }
+    }
 
- /*
-  * Copy the job attributes to the response using the requested-attributes
-  * attribute that may be provided by the client.
-  */
+   /*
+    * Found the printer with the PPD file, now see if there is one...
+    */
 
-  requested = ippFindAttribute(con->request, "requested-attributes",
-                              IPP_TAG_KEYWORD);
+    if ((con->file = open(filename, O_RDONLY)) < 0)
+    {
+      send_ipp_status(con, IPP_NOT_FOUND,
+                      _("The PPD file \"%s\" could not be opened: %s"),
+                     uri->values[0].string.text, strerror(errno));
+      return;
+    }
 
-  copy_attrs(con->response, job->attrs, requested, IPP_TAG_JOB, 0);
+    fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
 
-  add_job_state_reasons(con, job);
+    con->pipe_pid = 0;
 
-  if (requested != NULL)
-    con->response->request.status.status_code = IPP_OK_SUBST;
-  else
     con->response->request.status.status_code = IPP_OK;
-}
-
-
-/*
- * 'get_notifications()' - Get events for a subscription.
- */
-
-static void
-get_notifications(cupsd_client_t *con, /* I - Client connection */
-                  int            id)   /* I - Subscription ID */
-{
+  }
+  else
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The PPD file \"%s\" could not be found."),
+                    uri->values[0].string.text);
 }
 
 
@@ -5318,15 +7340,38 @@ static void
 get_ppds(cupsd_client_t *con)          /* I - Client connection */
 {
   http_status_t                status;         /* Policy status */
-  int                  i;              /* Looping var */
   ipp_attribute_t      *limit,         /* Limit attribute */
+                       *device,        /* ppd-device-id attribute */
+                       *language,      /* ppd-natural-language attribute */
                        *make,          /* ppd-make attribute */
-                       *requested;     /* requested-attributes attribute */
-  char                 command[1024],  /* cups-deviced command */
-                       options[1024],  /* Options to pass to command */
-                       attrs[1024],    /* String for requested attributes */
-                       *aptr;          /* Pointer into string */
-  int                  alen;           /* Length of attribute value */
+                       *model,         /* ppd-make-and-model attribute */
+                       *model_number,  /* ppd-model-number attribute */
+                       *product,       /* ppd-product attribute */
+                       *psversion,     /* ppd-psverion attribute */
+                       *type,          /* ppd-type attribute */
+                       *requested,     /* requested-attributes attribute */
+                       *exclude,       /* exclude-schemes attribute */
+                       *include;       /* include-schemes attribute */
+  char                 command[1024],  /* cups-driverd command */
+                       options[4096],  /* Options to pass to command */
+                       device_str[256],/* Escaped ppd-device-id string */
+                       language_str[256],
+                                       /* Escaped ppd-natural-language */
+                       make_str[256],  /* Escaped ppd-make string */
+                       model_str[256], /* Escaped ppd-make-and-model string */
+                       model_number_str[256],
+                                       /* ppd-model-number string */
+                       product_str[256],
+                                       /* Escaped ppd-product string */
+                       psversion_str[256],
+                                       /* Escaped ppd-psversion string */
+                       type_str[256],  /* Escaped ppd-type string */
+                       requested_str[256],
+                                       /* String for requested attributes */
+                       exclude_str[512],
+                                       /* String for excluded schemes */
+                       include_str[512];
+                                       /* String for included schemes */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd);
@@ -5337,7 +7382,7 @@ get_ppds(cupsd_client_t *con)             /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, NULL);
     return;
   }
 
@@ -5345,56 +7390,97 @@ get_ppds(cupsd_client_t *con)           /* I - Client connection */
   * Run cups-driverd command with the given options...
   */
 
-  limit     = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
-  make      = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
-  requested = ippFindAttribute(con->request, "requested-attributes",
-                               IPP_TAG_KEYWORD);
+  limit        = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
+  device       = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
+  language     = ippFindAttribute(con->request, "ppd-natural-language",
+                                  IPP_TAG_LANGUAGE);
+  make         = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
+  model        = ippFindAttribute(con->request, "ppd-make-and-model",
+                                  IPP_TAG_TEXT);
+  model_number = ippFindAttribute(con->request, "ppd-model-number",
+                                  IPP_TAG_INTEGER);
+  product      = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
+  psversion    = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
+  type         = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
+  requested    = ippFindAttribute(con->request, "requested-attributes",
+                                  IPP_TAG_KEYWORD);
+  exclude      = ippFindAttribute(con->request, "exclude-schemes",
+                                  IPP_TAG_NAME);
+  include      = ippFindAttribute(con->request, "include-schemes",
+                                  IPP_TAG_NAME);
 
   if (requested)
-  {
-    for (i = 0, aptr = attrs; i < requested->num_values; i ++)
-    {
-     /*
-      * Check that we have enough room...
-      */
+    url_encode_attr(requested, requested_str, sizeof(requested_str));
+  else
+    strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
 
-      alen = strlen(requested->values[i].string.text);
-      if (alen > (sizeof(attrs) - (aptr - attrs) - 2))
-        break;
+  if (device)
+    url_encode_attr(device, device_str, sizeof(device_str));
+  else
+    device_str[0] = '\0';
 
-     /*
-      * Put commas between values...
-      */
+  if (language)
+    url_encode_attr(language, language_str, sizeof(language_str));
+  else
+    language_str[0] = '\0';
 
-      if (i)
-        *aptr++ = ',';
+  if (make)
+    url_encode_attr(make, make_str, sizeof(make_str));
+  else
+    make_str[0] = '\0';
 
-     /*
-      * Add the value to the end of the string...
-      */
+  if (model)
+    url_encode_attr(model, model_str, sizeof(model_str));
+  else
+    model_str[0] = '\0';
 
-      strcpy(aptr, requested->values[i].string.text);
-      aptr += alen;
-    }
+  if (model_number)
+    snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
+             model_number->values[0].integer);
+  else
+    model_number_str[0] = '\0';
 
-   /*
-    * If we have more attribute names than will fit, default to "all"...
-    */
+  if (product)
+    url_encode_attr(product, product_str, sizeof(product_str));
+  else
+    product_str[0] = '\0';
 
-    if (i < requested->num_values)
-      strcpy(attrs, "all");
-  }
+  if (psversion)
+    url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
+  else
+    psversion_str[0] = '\0';
+
+  if (type)
+    url_encode_attr(type, type_str, sizeof(type_str));
+  else
+    type_str[0] = '\0';
+
+  if (exclude)
+    url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
   else
-    strcpy(attrs, "all");
+    exclude_str[0] = '\0';
+
+  if (include)
+    url_encode_attr(include, include_str, sizeof(include_str));
+  else
+    include_str[0] = '\0';
 
   snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
   snprintf(options, sizeof(options),
-           "cups-driverd list+%d+%d+requested-attributes=%s%s%s",
+           "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
            con->request->request.op.request_id,
            limit ? limit->values[0].integer : 0,
-          attrs,
-          make ? "%20ppd-make=" : "",
-          make ? make->values[0].string.text : "");
+          requested_str,
+          device ? "%20" : "", device_str,
+          language ? "%20" : "", language_str,
+          make ? "%20" : "", make_str,
+          model ? "%20" : "", model_str,
+          model_number ? "%20" : "", model_number_str,
+          product ? "%20" : "", product_str,
+          psversion ? "%20" : "", psversion_str,
+          type ? "%20" : "", type_str,
+          exclude_str[0] ? "%20" : "", exclude_str,
+          include_str[0] ? "%20" : "", include_str);
 
   if (cupsdSendCommand(con, command, options, 0))
   {
@@ -5427,25 +7513,9 @@ get_printer_attrs(cupsd_client_t  *con,  /* I - Client connection */
                  ipp_attribute_t *uri) /* I - Printer URI */
 {
   http_status_t                status;         /* Policy status */
-  const char           *dest;          /* Destination */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
-                       username[HTTP_MAX_URI],
-                                       /* Username portion of URI */
-                       host[HTTP_MAX_URI],
-                                       /* Host portion of URI */
-                       resource[HTTP_MAX_URI];
-                                       /* Resource portion of URI */
-  int                  port;           /* Port portion of URI */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
   cupsd_printer_t      *printer;       /* Printer/class */
-  char                 printer_uri[HTTP_MAX_URI];
-                                       /* Printer URI */
-  time_t               curtime;        /* Current time */
-  int                  i;              /* Looping var */
-  ipp_attribute_t      *requested,     /* requested-attributes */
-                       *history;       /* History collection */
-  int                  need_history;   /* Need to send history collection? */
+  cups_array_t         *ra;            /* Requested attributes array */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
@@ -5455,11 +7525,7 @@ get_printer_attrs(cupsd_client_t  *con,  /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -5476,87 +7542,76 @@ get_printer_attrs(cupsd_client_t  *con, /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
-  curtime = time(NULL);
-
  /*
-  * Copy the printer attributes to the response using requested-attributes
-  * and document-format attributes that may be provided by the client.
+  * Send the attributes...
   */
 
-  if (!ippFindAttribute(printer->attrs, "printer-uri-supported",
-                        IPP_TAG_URI))
-  {
-    httpAssembleURIf(printer_uri, sizeof(printer_uri), "ipp", NULL,
-                     con->servername, con->serverport, "/printers/%s",
-                    printer->name);
-    ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
-                "printer-uri-supported", NULL, printer_uri);
-    cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
-                    printer_uri);
-  }
+  ra = create_requested_array(con->request);
 
-  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
-                printer->state);
+  copy_printer_attrs(con, printer, ra);
 
-  add_printer_state_reasons(con, printer);
+  cupsArrayDelete(ra);
 
-  ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
-               "printer-state-message", NULL, printer->state_message);
+  con->response->request.status.status_code = IPP_OK;
+}
 
-  ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
-                printer->accepting);
-  ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
-                printer->shared);
 
-  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                "printer-up-time", curtime);
-  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                "printer-state-change-time", printer->state_time);
-  ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
-             ippTimeToDate(curtime));
-                
-  ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-               "printer-error-policy", NULL, printer->error_policy);
-  ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-               "printer-op-policy", NULL, printer->op_policy);
+/*
+ * 'get_printer_supported()' - Get printer supported values.
+ */
 
-  add_queued_job_count(con, printer);
+static void
+get_printer_supported(
+    cupsd_client_t  *con,              /* I - Client connection */
+    ipp_attribute_t *uri)              /* I - Printer URI */
+{
+  http_status_t                status;         /* Policy status */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  cupsd_printer_t      *printer;       /* Printer/class */
 
-  requested = ippFindAttribute(con->request, "requested-attributes",
-                              IPP_TAG_KEYWORD);
 
-  copy_attrs(con->response, printer->attrs, requested, IPP_TAG_ZERO, 0);
-  copy_attrs(con->response, CommonData, requested, IPP_TAG_ZERO, IPP_TAG_COPY);
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
 
-  need_history = 0;
+ /*
+  * Is the destination valid?
+  */
 
-  if (MaxPrinterHistory > 0 && printer->num_history > 0 && requested)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
-    for (i = 0; i < requested->num_values; i ++)
-      if (!strcmp(requested->values[i].string.text, "all") ||
-          !strcmp(requested->values[i].string.text, "printer-state-history"))
-      {
-        need_history = 1;
-        break;
-      }
+   /*
+    * Bad URI...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
   }
 
-  if (need_history)
-  {
-    history = ippAddCollections(con->response, IPP_TAG_PRINTER,
-                                "printer-state-history",
-                                printer->num_history, NULL);
+ /*
+  * Check policy...
+  */
 
-    for (i = 0; i < printer->num_history; i ++)
-      copy_attrs(history->values[i].collection = ippNew(), printer->history[i],
-                 NULL, IPP_TAG_ZERO, 0);
+  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, printer);
+    return;
   }
 
-  con->response->request.status.status_code = requested ? IPP_OK_SUBST : IPP_OK;
+ /*
+  * Return a list of attributes that can be set via Set-Printer-Attributes.
+  */
+
+  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
+                "printer-info", 0);
+  ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
+                "printer-location", 0);
+
+  con->response->request.status.status_code = IPP_OK;
 }
 
 
@@ -5569,40 +7624,39 @@ get_printers(cupsd_client_t *con,       /* I - Client connection */
              int            type)      /* I - 0 or CUPS_PRINTER_CLASS */
 {
   http_status_t        status;                 /* Policy status */
-  int          i;                      /* Looping var */
-  ipp_attribute_t *requested,          /* requested-attributes */
-               *history,               /* History collection */
-               *attr;                  /* Current attribute */
-  int          need_history;           /* Need to send history collection? */
-  int          limit;                  /* Maximum number of printers to return */
+  ipp_attribute_t *attr;               /* Current attribute */
+  int          limit;                  /* Max number of printers to return */
   int          count;                  /* Number of printers that match */
   cupsd_printer_t *printer;            /* Current printer pointer */
-  time_t       curtime;                /* Current time */
   int          printer_type,           /* printer-type attribute */
                printer_mask;           /* printer-type-mask attribute */
   char         *location;              /* Location string */
   const char   *username;              /* Current user */
   char         *first_printer_name;    /* first-printer-name attribute */
-  char         printer_uri[HTTP_MAX_URI];
-                                       /* Printer URI */
+  cups_array_t *ra;                    /* Requested attributes array */
+  int          local;                  /* Local connection? */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
                   con->http.fd, type);
 
-  if (!Printers || !cupsArrayCount(Printers))
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
+    send_http_error(con, status, NULL);
     return;
   }
 
  /*
-  * Check policy...
+  * Check for printers...
   */
 
-  if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
+  if (!Printers || !cupsArrayCount(Printers))
   {
-    send_http_error(con, status);
+    send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
     return;
   }
 
@@ -5610,7 +7664,8 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
   * See if they want to limit the number of printers reported...
   */
 
-  if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "limit",
+                               IPP_TAG_INTEGER)) != NULL)
     limit = attr->values[0].integer;
   else
     limit = 10000000;
@@ -5637,6 +7692,8 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
   else
     printer_mask = 0;
 
+  local = httpAddrLocalhost(&(con->clientaddr));
+
   if ((attr = ippFindAttribute(con->request, "printer-location",
                                IPP_TAG_TEXT)) != NULL)
     location = attr->values[0].string.text;
@@ -5651,28 +7708,12 @@ get_printers(cupsd_client_t *con,       /* I - Client connection */
   else
     username = NULL;
 
-  requested = ippFindAttribute(con->request, "requested-attributes",
-                              IPP_TAG_KEYWORD);
-
-  need_history = 0;
-
-  if (MaxPrinterHistory > 0 && requested)
-  {
-    for (i = 0; i < requested->num_values; i ++)
-      if (!strcmp(requested->values[i].string.text, "all") ||
-          !strcmp(requested->values[i].string.text, "printer-state-history"))
-      {
-        need_history = 1;
-        break;
-      }
-  }
+  ra = create_requested_array(con->request);
 
  /*
   * OK, build a list of printers for this printer...
   */
 
-  curtime = time(NULL);
-
   if (first_printer_name)
   {
     if ((printer = cupsdFindDest(first_printer_name)) == NULL)
@@ -5682,12 +7723,16 @@ get_printers(cupsd_client_t *con,       /* I - Client connection */
     printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
 
   for (count = 0;
-       count < limit && printer != NULL;
+       count < limit && printer;
        printer = (cupsd_printer_t *)cupsArrayNext(Printers))
+  {
+    if (!local && !printer->shared)
+      continue;
+
     if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
         (printer->type & printer_mask) == printer_type &&
-       (location == NULL || printer->location == NULL ||
-        !strcasecmp(printer->location, location)))
+       (!location ||
+        (printer->location && !strcasecmp(printer->location, location))))
     {
      /*
       * If HideImplicitMembers is enabled, see if this printer or class
@@ -5703,7 +7748,8 @@ get_printers(cupsd_client_t *con, /* I - Client connection */
       * access...
       */
 
-      if (printer->num_users && username && !user_allowed(printer, username))
+      if (!(printer->type & CUPS_PRINTER_AUTHENTICATED) &&
+          printer->num_users && username && !user_allowed(printer, username))
         continue;
 
      /*
@@ -5716,73 +7762,16 @@ get_printers(cupsd_client_t *con,       /* I - Client connection */
       count ++;
 
      /*
-      * Send the following attributes for each printer:
-      *
-      *    printer-state
-      *    printer-state-message
-      *    printer-is-accepting-jobs
-      *    printer-is-shared
-      *    printer-up-time
-      *    printer-state-change-time
-      *    + all printer attributes
+      * Send the attributes...
       */
 
-      if (!ippFindAttribute(printer->attrs, "printer-uri-supported",
-                            IPP_TAG_URI))
-      {
-       httpAssembleURIf(printer_uri, sizeof(printer_uri), "ipp", NULL,
-                        con->servername, con->serverport, "/printers/%s",
-                        printer->name);
-       ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
-                    "printer-uri-supported", NULL, printer_uri);
-        cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"", printer_uri);
-      }
-
-      ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM,
-                    "printer-state", printer->state);
-
-      add_printer_state_reasons(con, printer);
-
-      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
-                   "printer-state-message", NULL, printer->state_message);
-
-      ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
-                    printer->accepting);
-      ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
-                    printer->shared);
-
-      ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                    "printer-up-time", curtime);
-      ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
-                    "printer-state-change-time", printer->state_time);
-      ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
-                ippTimeToDate(curtime));
-
-      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                   "printer-error-policy", NULL, printer->error_policy);
-      ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
-                   "printer-op-policy", NULL, printer->op_policy);
-
-      add_queued_job_count(con, printer);
-
-      copy_attrs(con->response, printer->attrs, requested, IPP_TAG_ZERO, 0);
-
-      copy_attrs(con->response, CommonData, requested, IPP_TAG_ZERO,
-                 IPP_TAG_COPY);
-
-      if (need_history && printer->num_history > 0)
-      {
-       history = ippAddCollections(con->response, IPP_TAG_PRINTER,
-                                    "printer-state-history",
-                                    printer->num_history, NULL);
-
-       for (i = 0; i < printer->num_history; i ++)
-         copy_attrs(history->values[i].collection = ippNew(),
-                    printer->history[i], NULL, IPP_TAG_ZERO, 0);
-      }
+      copy_printer_attrs(con, printer, ra);
     }
+  }
+
+  cupsArrayDelete(ra);
 
-  con->response->request.status.status_code = requested ? IPP_OK_SUBST : IPP_OK;
+  con->response->request.status.status_code = IPP_OK;
 }
 
 
@@ -5827,7 +7816,7 @@ get_subscription_attrs(
                                              DefaultPolicyPtr,
                                  con, sub->owner)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, sub->dest);
     return;
   }
 
@@ -5860,9 +7849,9 @@ get_subscriptions(cupsd_client_t  *con,   /* I - Client connection */
   cupsd_subscription_t *sub;           /* Subscription */
   cups_array_t         *ra;            /* Requested attributes array */
   ipp_attribute_t      *attr;          /* Attribute */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  char                 scheme[HTTP_MAX_URI],
+                                       /* Scheme portion of URI */
                        username[HTTP_MAX_URI],
                                        /* Username portion of URI */
                        host[HTTP_MAX_URI],
@@ -5882,9 +7871,9 @@ get_subscriptions(cupsd_client_t  *con,   /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                  sizeof(scheme), username, sizeof(username), host,
+                 sizeof(host), &port, resource, sizeof(resource));
 
   if (!strcmp(resource, "/") ||
       (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
@@ -5906,7 +7895,7 @@ get_subscriptions(cupsd_client_t  *con,   /* I - Client connection */
       return;
     }
   }
-  else if (!cupsdValidateDest(host, resource, &dtype, &printer))
+  else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -5939,7 +7928,7 @@ get_subscriptions(cupsd_client_t  *con,   /* I - Client connection */
                                            DefaultPolicyPtr,
                                  con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
@@ -5950,7 +7939,8 @@ get_subscriptions(cupsd_client_t  *con,   /* I - Client connection */
 
   ra = create_requested_array(con->request);
 
-  if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "limit",
+                               IPP_TAG_INTEGER)) != NULL)
     limit = attr->values[0].integer;
   else
     limit = 0;
@@ -5962,14 +7952,7 @@ get_subscriptions(cupsd_client_t  *con,  /* I - Client connection */
   if ((attr = ippFindAttribute(con->request, "my-subscriptions",
                                IPP_TAG_BOOLEAN)) != NULL &&
       attr->values[0].boolean)
-  {
-    if (con->username[0])
-      strlcpy(username, con->username, sizeof(username));
-    else if ((attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME)) != NULL)
-      strlcpy(username, attr->values[0].string.text, sizeof(username));
-    else
-      strcpy(username, "anonymous");
-  }
+    strlcpy(username, get_username(con), sizeof(username));
   else
     username[0] = '\0';
 
@@ -5997,179 +7980,59 @@ get_subscriptions(cupsd_client_t  *con,        /* I - Client connection */
 
 
 /*
- * 'hold_job()' - Hold a print job.
+ * 'get_username()' - Get the username associated with a request.
  */
 
-static void
-hold_job(cupsd_client_t  *con,         /* I - Client connection */
-         ipp_attribute_t *uri)         /* I - Job or Printer URI */
+static const char *                    /* O - Username */
+get_username(cupsd_client_t *con)      /* I - Connection */
 {
-  ipp_attribute_t *attr,               /* Current job-hold-until */
-               *newattr;               /* New job-hold-until */
-  int          jobid;                  /* Job ID */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
-  cupsd_job_t  *job;                   /* Job information */
-
-
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
-                  uri->values[0].string.text);
-
- /*
-  * See if we have a job URI or a printer URI...
-  */
-
-  if (!strcmp(uri->name, "printer-uri"))
-  {
-   /*
-    * Got a printer URI; see if we also have a job-id attribute...
-    */
+  ipp_attribute_t      *attr;          /* Attribute */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
-    {
-      send_ipp_status(con, IPP_BAD_REQUEST,
-                      _("Got a printer-uri attribute but no job-id!"));
-      return;
-    }
 
-    jobid = attr->values[0].integer;
-  }
+  if (con->username[0])
+    return (con->username);
+  else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
+                                    IPP_TAG_NAME)) != NULL)
+    return (attr->values[0].string.text);
   else
-  {
-   /*
-    * Got a job URI; parse it to get the job ID...
-    */
-
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
-
-    if (strncmp(resource, "/jobs/", 6))
-    {
-     /*
-      * Not a valid URI!
-      */
-
-      send_ipp_status(con, IPP_BAD_REQUEST,
-                      _("Bad job-uri attribute \"%s\"!"),
-                      uri->values[0].string.text);
-      return;
-    }
-
-    jobid = atoi(resource + 6);
-  }
-
- /*
-  * See if the job exists...
-  */
-
-  if ((job = cupsdFindJob(jobid)) == NULL)
-  {
-   /*
-    * Nope - return a "not found" error...
-    */
-
-    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
-    return;
-  }
-
- /*
-  * See if the job is owned by the requesting user...
-  */
-
-  if (!validate_user(job, con, job->username, username, sizeof(username)))
-  {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("Not authorized to hold job #%d owned by \"%s\"!"),
-                    jobid, job->username);
-    return;
-  }
-
- /*
-  * Hold the job and return...
-  */
-
-  cupsdHoldJob(job);
-
-  if ((newattr = ippFindAttribute(con->request, "job-hold-until",
-                                  IPP_TAG_KEYWORD)) == NULL)
-    newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
-
-  if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
-                               IPP_TAG_KEYWORD)) == NULL)
-    attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
-
-  if (attr != NULL)
-  {
-   /*
-    * Free the old hold value and copy the new one over...
-    */
-
-    free(attr->values[0].string.text);
-
-    if (newattr != NULL)
-    {
-      attr->value_tag = newattr->value_tag;
-      attr->values[0].string.text = strdup(newattr->values[0].string.text);
-    }
-    else
-    {
-      attr->value_tag = IPP_TAG_KEYWORD;
-      attr->values[0].string.text = strdup("indefinite");
-    }
-
-   /*
-    * Hold job until specified time...
-    */
-
-    cupsdSetJobHoldUntil(job, attr->values[0].string.text);
-  }
-
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was held by \"%s\".", jobid, username);
-
-  con->response->request.status.status_code = IPP_OK;
+    return ("anonymous");
 }
 
 
 /*
- * 'move_job()' - Move a job to a new destination.
+ * 'hold_job()' - Hold a print job.
  */
 
 static void
-move_job(cupsd_client_t  *con,         /* I - Client connection */
-        ipp_attribute_t *uri)          /* I - Job URI */
+hold_job(cupsd_client_t  *con,         /* I - Client connection */
+         ipp_attribute_t *uri)         /* I - Job or Printer URI */
 {
-  http_status_t        status;                 /* Policy status */
-  ipp_attribute_t *attr;               /* Current attribute */
+  ipp_attribute_t *attr;               /* Current job-hold-until */
+  const char   *when;                  /* New value */
   int          jobid;                  /* Job ID */
-  cupsd_job_t  *job;                   /* Current job */
-  const char   *dest;                  /* Destination */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
   int          port;                   /* Port portion of URI */
-  cupsd_printer_t *printer;            /* Printer */
+  cupsd_job_t  *job;                   /* Job information */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
                   uri->values[0].string.text);
 
  /*
   * See if we have a job URI or a printer URI...
   */
 
-  if (strcmp(uri->name, "printer-uri") == 0)
+  if (!strcmp(uri->name, "printer-uri"))
   {
    /*
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -6184,9 +8047,9 @@ move_job(cupsd_client_t  *con,            /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
     if (strncmp(resource, "/jobs/", 6))
     {
@@ -6213,24 +8076,7 @@ move_job(cupsd_client_t  *con,           /* I - Client connection */
     * Nope - return a "not found" error...
     */
 
-    send_ipp_status(con, IPP_NOT_FOUND,
-                    _("Job #%d does not exist!"), jobid);
-    return;
-  }
-
- /*
-  * See if the job has been completed...
-  */
-
-  if (job->state->values[0].integer > IPP_JOB_STOPPED)
-  {
-   /*
-    * Return a "not-possible" error...
-    */
-
-    send_ipp_status(con, IPP_NOT_POSSIBLE,
-                    _("Job #%d is finished and cannot be altered!"),
-                   jobid);
+    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
     return;
   }
 
@@ -6240,874 +8086,677 @@ move_job(cupsd_client_t  *con,                /* I - Client connection */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to move job #%d owned "
-                     "by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
-  if ((attr = ippFindAttribute(con->request, "job-printer-uri", IPP_TAG_URI)) == NULL)
-  {
-   /*
-    * Need job-printer-uri...
-    */
-
-    send_ipp_status(con, IPP_BAD_REQUEST,
-                    _("job-printer-uri attribute missing!"));
-    return;
-  }
-    
  /*
-  * Get the new printer or class...
+  * Hold the job and return...
   */
 
-  httpSeparateURI(attr->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  if ((attr = ippFindAttribute(con->request, "job-hold-until",
+                              IPP_TAG_KEYWORD)) == NULL)
+    attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
 
-  if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (attr)
   {
-   /*
-    * Bad URI...
-    */
-
-    send_ipp_status(con, IPP_NOT_FOUND,
-                    _("The printer or class was not found."));
-    return;
-  }
-
- /*
-  * Check policy...
-  */
+    when = attr->values[0].string.text;
 
-  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
-  {
-    send_http_error(con, status);
-    return;
+    cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
+                 "Job job-hold-until value changed by user.");
   }
-
- /*
-  * Move the job to a different printer or class...
-  */
-
-  cupsdMoveJob(job, dest);
-
- /*
-  * Start jobs if possible...
-  */
-
-  cupsdCheckJobs();
-
- /*
-  * Return with "everything is OK" status...
-  */
-
-  con->response->request.status.status_code = IPP_OK;
-}
-
-
-/*
- * 'ppd_add_default()' - Add a PPD default choice.
- */
-
-static int                             /* O  - Number of defaults */
-ppd_add_default(
-    const char    *option,             /* I  - Option name */
-    const char    *choice,             /* I  - Choice name */
-    int           num_defaults,                /* I  - Number of defaults */
-    ppd_default_t **defaults)          /* IO - Defaults */
-{
-  int          i;                      /* Looping var */
-  ppd_default_t        *temp;                  /* Temporary defaults array */
-
-
- /*
-  * First check if the option already has a default value; the PPD spec
-  * says that the first one is used...
-  */
-
-  for (i = 0, temp = *defaults; i < num_defaults; i ++)
-    if (!strcmp(option, temp[i].option))
-      return (num_defaults);
-
- /*
-  * Now add the option...
-  */
-
-  if (num_defaults == 0)
-    temp = malloc(sizeof(ppd_default_t));
   else
-    temp = realloc(*defaults, (num_defaults + 1) * sizeof(ppd_default_t));
-
-  if (!temp)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR, "ppd_add_default: Unable to add default value for \"%s\" - %s",
-               option, strerror(errno));
-    return (num_defaults);
-  }
+    when = "indefinite";
 
-  *defaults = temp;
-  temp      += num_defaults;
+  cupsdSetJobHoldUntil(job, when, 1);
+  cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".",
+                   username);
 
-  strlcpy(temp->option, option, sizeof(temp->option));
-  strlcpy(temp->choice, choice, sizeof(temp->choice));
-
-  return (num_defaults + 1);
+  con->response->request.status.status_code = IPP_OK;
 }
 
 
 /*
- * 'ppd_parse_line()' - Parse a PPD default line.
+ * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
  */
 
-static int                             /* O - 0 on success, -1 on failure */
-ppd_parse_line(const char *line,       /* I - Line */
-               char       *option,     /* O - Option name */
-              int        olen,         /* I - Size of option name */
-               char       *choice,     /* O - Choice name */
-              int        clen)         /* I - Size of choice name */
+static void
+hold_new_jobs(cupsd_client_t  *con,    /* I - Connection */
+              ipp_attribute_t *uri)    /* I - Printer URI */
 {
- /*
-  * Verify this is a default option line...
-  */
+  http_status_t                status;         /* Policy status */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  cupsd_printer_t      *printer;       /* Printer data */
 
-  if (strncmp(line, "*Default", 8))
-    return (-1);
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
 
  /*
-  * Read the option name...
+  * Is the destination valid?
   */
 
-  for (line += 8, olen --; isalnum(*line & 255); line ++)
-    if (olen > 0)
-    {
-      *option++ = *line;
-      olen --;
-    }
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
+  {
+   /*
+    * Bad URI...
+    */
 
-  *option = '\0';
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
+  }
 
  /*
-  * Skip everything else up to the colon (:)...
+  * Check policy...
   */
 
-  while (*line && *line != ':')
-    line ++;
-
-  if (!*line)
-    return (-1);
-
-  line ++;
+  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, printer);
+    return;
+  }
 
  /*
-  * Now grab the option choice, skipping leading whitespace...
+  * Hold pending/new jobs sent to the printer...
   */
 
-  while (isspace(*line & 255))
-    line ++;
+  printer->holding_new_jobs = 1;
 
-  for (clen --; isalnum(*line & 255); line ++)
-    if (clen > 0)
-    {
-      *choice++ = *line;
-      clen --;
-    }
+  cupsdSetPrinterReasons(printer, "+hold-new-jobs");
+  cupsdAddPrinterHistory(printer);
 
-  *choice = '\0';
+  if (dtype & CUPS_PRINTER_CLASS)
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Class \"%s\" now holding pending/new jobs (\"%s\").",
+                    printer->name, get_username(con));
+  else
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Printer \"%s\" now holding pending/new jobs (\"%s\").",
+                    printer->name, get_username(con));
 
  /*
-  * Return with no errors...
+  * Everything was ok, so return OK status...
   */
 
-  return (0);
+  con->response->request.status.status_code = IPP_OK;
 }
 
 
 /*
- * 'print_job()' - Print a file to a printer or class.
+ * 'move_job()' - Move a job to a new destination.
  */
 
 static void
-print_job(cupsd_client_t  *con,                /* I - Client connection */
-         ipp_attribute_t *uri)         /* I - Printer URI */
+move_job(cupsd_client_t  *con,         /* I - Client connection */
+        ipp_attribute_t *uri)          /* I - Job URI */
 {
   http_status_t        status;                 /* Policy status */
   ipp_attribute_t *attr;               /* Current attribute */
-  ipp_attribute_t *format;             /* Document-format attribute */
-  const char   *dest;                  /* Destination */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
-  int          priority;               /* Job priority */
-  char         *title;                 /* Job name/title */
+  int          jobid;                  /* Job ID */
   cupsd_job_t  *job;                   /* Current job */
-  char         job_uri[HTTP_MAX_URI],  /* Job URI */
-               method[HTTP_MAX_URI],   /* Method portion of URI */
+  const char   *src;                   /* Source printer/class */
+  cups_ptype_t stype,                  /* Source type (printer or class) */
+               dtype;                  /* Destination type (printer/class) */
+  char         scheme[HTTP_MAX_URI],   /* Scheme portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI], /* Resource portion of URI */
-               filename[1024];         /* Job filename */
+               resource[HTTP_MAX_URI]; /* Resource portion of URI */
   int          port;                   /* Port portion of URI */
-  mime_type_t  *filetype;              /* Type of file */
-  char         super[MIME_MAX_SUPER],  /* Supertype of file */
-               type[MIME_MAX_TYPE],    /* Subtype of file */
-               mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
-                                       /* Textual name of mime type */
-  cupsd_printer_t *printer;            /* Printer data */
-  struct stat  fileinfo;               /* File information */
-  int          kbytes;                 /* Size of file */
-  int          i;                      /* Looping var */
-  int          lowerpagerange;         /* Page range bound */
-  int          compression;            /* Document compression */
+  cupsd_printer_t *sprinter,           /* Source printer */
+               *dprinter;              /* Destination printer */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
                   uri->values[0].string.text);
 
  /*
-  * Validate job template attributes; for now just copies and page-ranges...
+  * Get the new printer or class...
   */
 
-  if ((attr = ippFindAttribute(con->request, "copies", IPP_TAG_INTEGER)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "job-printer-uri",
+                               IPP_TAG_URI)) == NULL)
   {
-    if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
-    {
-      send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
-                      attr->values[0].integer);
-      ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
-                   "copies", attr->values[0].integer);
-      return;
-    }
+   /*
+    * Need job-printer-uri...
+    */
+
+    send_ipp_status(con, IPP_BAD_REQUEST,
+                    _("job-printer-uri attribute missing!"));
+    return;
   }
 
-  if ((attr = ippFindAttribute(con->request, "page-ranges", IPP_TAG_RANGE)) != NULL)
+  if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
   {
-    for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
+   /*
+    * Bad URI...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
+  }
+
+ /*
+  * See if we have a job URI or a printer URI...
+  */
+
+  httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                  sizeof(scheme), username, sizeof(username), host,
+                 sizeof(host), &port, resource, sizeof(resource));
+
+  if (!strcmp(uri->name, "printer-uri"))
+  {
+   /*
+    * Got a printer URI; see if we also have a job-id attribute...
+    */
+
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
-      if (attr->values[i].range.lower < lowerpagerange || 
-         attr->values[i].range.lower > attr->values[i].range.upper)
+     /*
+      * Move all jobs...
+      */
+
+      if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
+                                   &sprinter)) == NULL)
       {
-       send_ipp_status(con, IPP_BAD_REQUEST,
-                       _("Bad page-ranges values %d-%d."),
-                       attr->values[i].range.lower,
-                       attr->values[i].range.upper);
+       /*
+       * Bad URI...
+       */
+
+       send_ipp_status(con, IPP_NOT_FOUND,
+                       _("The printer or class was not found."));
        return;
       }
 
-      lowerpagerange = attr->values[i].range.upper + 1;
+      job = NULL;
     }
-  }
+    else
+    {
+     /*
+      * Otherwise, just move a single job...
+      */
 
- /*
-  * OK, see if the client is sending the document compressed - CUPS
-  * only supports "none" and "gzip".
-  */
+      if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
+      {
+       /*
+       * Nope - return a "not found" error...
+       */
 
-  compression = CUPS_FILE_NONE;
+       send_ipp_status(con, IPP_NOT_FOUND,
+                       _("Job #%d does not exist!"), attr->values[0].integer);
+       return;
+      }
+      else
+      {
+       /*
+        * Job found, initialize source pointers...
+       */
 
-  if ((attr = ippFindAttribute(con->request, "compression", IPP_TAG_KEYWORD)) != NULL)
+       src      = NULL;
+       sprinter = NULL;
+      }
+    }
+  }
+  else
   {
-    if (strcmp(attr->values[0].string.text, "none")
-#ifdef HAVE_LIBZ
-        && strcmp(attr->values[0].string.text, "gzip")
-#endif /* HAVE_LIBZ */
-      )
+   /*
+    * Got a job URI; parse it to get the job ID...
+    */
+
+    if (strncmp(resource, "/jobs/", 6))
     {
-      send_ipp_status(con, IPP_ATTRIBUTES,
-                      _("Unsupported compression \"%s\"!"),
-                     attr->values[0].string.text);
-      ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
-                  "compression", NULL, attr->values[0].string.text);
+     /*
+      * Not a valid URI!
+      */
+
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Bad job-uri attribute \"%s\"!"),
+                      uri->values[0].string.text);
       return;
     }
 
-#ifdef HAVE_LIBZ
-    if (!strcmp(attr->values[0].string.text, "gzip"))
-      compression = CUPS_FILE_GZIP;
-#endif /* HAVE_LIBZ */
+   /*
+    * See if the job exists...
+    */
+
+    jobid = atoi(resource + 6);
+
+    if ((job = cupsdFindJob(jobid)) == NULL)
+    {
+     /*
+      * Nope - return a "not found" error...
+      */
+
+      send_ipp_status(con, IPP_NOT_FOUND,
+                      _("Job #%d does not exist!"), jobid);
+      return;
+    }
+    else
+    {
+     /*
+      * Job found, initialize source pointers...
+      */
+
+      src      = NULL;
+      sprinter = NULL;
+    }
   }
 
  /*
-  * Do we have a file to print?
+  * Check the policy of the destination printer...
   */
 
-  if (!con->filename)
+  if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
+                                 job ? job->username : NULL)) != HTTP_OK)
   {
-    send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
+    send_http_error(con, status, dprinter);
     return;
   }
 
  /*
-  * Is it a format we support?
+  * Now move the job or jobs...
   */
 
-  if ((format = ippFindAttribute(con->request, "document-format",
-                                 IPP_TAG_MIMETYPE)) != NULL)
+  if (job)
   {
    /*
-    * Grab format from client...
+    * See if the job has been completed...
     */
 
-    if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2)
+    if (job->state_value > IPP_JOB_STOPPED)
     {
-      send_ipp_status(con, IPP_BAD_REQUEST,
-                      _("Could not scan type \"%s\"!"),
-                     format->values[0].string.text);
+     /*
+      * Return a "not-possible" error...
+      */
+
+      send_ipp_status(con, IPP_NOT_POSSIBLE,
+                      _("Job #%d is finished and cannot be altered!"),
+                     job->id);
       return;
     }
-  }
-  else
-  {
+
    /*
-    * No document format attribute?  Auto-type it!
+    * See if the job is owned by the requesting user...
     */
 
-    strcpy(super, "application");
-    strcpy(type, "octet-stream");
-  }
+    if (!validate_user(job, con, job->username, username, sizeof(username)))
+    {
+      send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
+      return;
+    }
 
-  if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
-  {
    /*
-    * Auto-type the file...
+    * Move the job to a different printer or class...
     */
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: auto-typing file...");
-
-    filetype = mimeFileType(MimeDatabase, con->filename, &compression);
+    cupsdMoveJob(job, dprinter);
+  }
+  else
+  {
+   /*
+    * Got the source printer, now look through the jobs...
+    */
 
-    if (filetype != NULL)
+    for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
+         job;
+        job = (cupsd_job_t *)cupsArrayNext(Jobs))
     {
      /*
-      * Replace the document-format attribute value with the auto-typed one.
+      * See if the job is pointing at the source printer or has not been
+      * completed...
       */
 
-      snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
-               filetype->type);
+      if (strcasecmp(job->dest, src) ||
+          job->state_value > IPP_JOB_STOPPED)
+       continue;
 
-      if (format != NULL)
-      {
-       free(format->values[0].string.text);
-       format->values[0].string.text = strdup(mimetype);
-      }
-      else
-        ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
-                    "document-format", NULL, mimetype);
-    }
-    else
-      filetype = mimeType(MimeDatabase, super, type);
-  }
-  else
-    filetype = mimeType(MimeDatabase, super, type);
+     /*
+      * See if the job can be moved by the requesting user...
+      */
 
-  if (filetype == NULL)
-  {
-    send_ipp_status(con, IPP_DOCUMENT_FORMAT,
-                    _("Unsupported format \'%s/%s\'!"), super, type);
-    cupsdLogMessage(CUPSD_LOG_INFO,
-                    "Hint: Do you have the raw file printing rules enabled?");
+      if (!validate_user(job, con, job->username, username, sizeof(username)))
+        continue;
 
-    if (format)
-      ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
-                   "document-format", NULL, format->values[0].string.text);
+     /*
+      * Move the job to a different printer or class...
+      */
 
-    return;
+      cupsdMoveJob(job, dprinter);
+    }
   }
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: request file type is %s/%s.",
-            filetype->super, filetype->type);
-
  /*
-  * Read any embedded job ticket info from PS files...
+  * Start jobs if possible...
   */
 
-  if (!strcasecmp(filetype->super, "application") &&
-      !strcasecmp(filetype->type, "postscript"))
-    read_ps_job_ticket(con);
+  cupsdCheckJobs();
 
  /*
-  * Is the destination valid?
+  * Return with "everything is OK" status...
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
+  con->response->request.status.status_code = IPP_OK;
+}
 
-  if ((dest = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
-  {
-   /*
-    * Bad URI...
-    */
 
-    send_ipp_status(con, IPP_NOT_FOUND,
-                    _("The printer or class was not found."));
-    return;
-  }
+/*
+ * 'ppd_parse_line()' - Parse a PPD default line.
+ */
 
+static int                             /* O - 0 on success, -1 on failure */
+ppd_parse_line(const char *line,       /* I - Line */
+               char       *option,     /* O - Option name */
+              int        olen,         /* I - Size of option name */
+               char       *choice,     /* O - Choice name */
+              int        clen)         /* I - Size of choice name */
+{
  /*
-  * Check remote printing to non-shared printer...
+  * Verify this is a default option line...
   */
 
-  if (!printer->shared &&
-      strcasecmp(con->http.hostname, "localhost") &&
-      strcasecmp(con->http.hostname, ServerName))
-  {
-    send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Printer not shared!"));
-    return;
-  }
+  if (strncmp(line, "*Default", 8))
+    return (-1);
 
  /*
-  * Check policy...
+  * Read the option name...
   */
 
-  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
-  {
-    send_http_error(con, status);
-    return;
-  }
-  else if ((printer->type & CUPS_PRINTER_AUTHENTICATED) && !con->username[0])
-  {
-    send_http_error(con, status);
-    return;
-  }
-
- /*
-  * See if the printer is accepting jobs...
-  */
+  for (line += 8, olen --; isalnum(*line & 255); line ++)
+    if (olen > 0)
+    {
+      *option++ = *line;
+      olen --;
+    }
 
-  if (!printer->accepting)
-  {
-    send_ipp_status(con, IPP_NOT_ACCEPTING,
-                    _("Destination \"%s\" is not accepting jobs."), dest);
-    return;
-  }
+  *option = '\0';
 
  /*
-  * Make sure we aren't over our limit...
+  * Skip everything else up to the colon (:)...
   */
 
-  if (cupsArrayCount(Jobs) >= MaxJobs && MaxJobs)
-    cupsdCleanJobs();
+  while (*line && *line != ':')
+    line ++;
 
-  if (cupsArrayCount(Jobs) >= MaxJobs && MaxJobs)
-  {
-    send_ipp_status(con, IPP_NOT_POSSIBLE,
-                    _("Too many jobs - %d jobs, max jobs is %d."),
-                    cupsArrayCount(Jobs), MaxJobs);
-    return;
-  }
+  if (!*line)
+    return (-1);
 
-  if (!check_quotas(con, printer))
-  {
-    send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
-    return;
-  }
+  line ++;
 
  /*
-  * Create the job and set things up...
+  * Now grab the option choice, skipping leading whitespace...
   */
 
-  if ((attr = ippFindAttribute(con->request, "job-priority", IPP_TAG_INTEGER)) != NULL)
-    priority = attr->values[0].integer;
-  else
-    ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
-                  priority = 50);
-
-  if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_NAME)) != NULL)
-    title = attr->values[0].string.text;
-  else
-    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
-                 title = "Untitled");
+  while (isspace(*line & 255))
+    line ++;
 
-  if ((job = cupsdAddJob(priority, printer->name)) == NULL)
-  {
-    send_ipp_status(con, IPP_INTERNAL_ERROR,
-                    _("Unable to add job for destination \"%s\"!"), dest);
-    return;
-  }
+  for (clen --; isalnum(*line & 255); line ++)
+    if (clen > 0)
+    {
+      *choice++ = *line;
+      clen --;
+    }
 
-  job->dtype   = dtype;
-  job->attrs   = con->request;
-  con->request = NULL;
+  *choice = '\0';
 
  /*
-  * Copy the rest of the job info...
+  * Return with no errors...
   */
 
-  attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
+  return (0);
+}
 
-  if (con->username[0])
-  {
-    cupsdSetString(&job->username, con->username);
 
-    if (attr)
-      cupsdSetString(&attr->values[0].string.text, con->username);
+/*
+ * 'print_job()' - Print a file to a printer or class.
+ */
 
-    save_auth_info(con, job);
-  }
-  else if (attr != NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: requesting-user-name = \"%s\"",
-               attr->values[0].string.text);
+static void
+print_job(cupsd_client_t  *con,                /* I - Client connection */
+         ipp_attribute_t *uri)         /* I - Printer URI */
+{
+  ipp_attribute_t *attr;               /* Current attribute */
+  ipp_attribute_t *format;             /* Document-format attribute */
+  const char   *default_format;        /* document-format-default value */
+  cupsd_job_t  *job;                   /* New job */
+  char         filename[1024];         /* Job filename */
+  mime_type_t  *filetype;              /* Type of file */
+  char         super[MIME_MAX_SUPER],  /* Supertype of file */
+               type[MIME_MAX_TYPE],    /* Subtype of file */
+               mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
+                                       /* Textual name of mime type */
+  cupsd_printer_t *printer;            /* Printer data */
+  struct stat  fileinfo;               /* File information */
+  int          kbytes;                 /* Size of file */
+  int          compression;            /* Document compression */
 
-    cupsdSetString(&job->username, attr->values[0].string.text);
-  }
-  else
-    cupsdSetString(&job->username, "anonymous");
 
-  if (attr == NULL)
-    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
-                 "job-originating-user-name", NULL, job->username);
-  else
-  {
-    attr->group_tag = IPP_TAG_JOB;
-    cupsdSetString(&attr->name, "job-originating-user-name");
-  }
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
+                  uri->values[0].string.text);
 
  /*
-  * Add remaining job attributes...
+  * Validate print file attributes, for now just document-format and
+  * compression (CUPS only supports "none" and "gzip")...
   */
 
-  if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
-                               IPP_TAG_ZERO)) != NULL)
-  {
-   /*
-    * Request contains a job-originating-host-name attribute; validate it...
-    */
+  compression = CUPS_FILE_NONE;
 
-    if (attr->value_tag != IPP_TAG_NAME ||
-        attr->num_values != 1 ||
-        strcmp(con->http.hostname, "localhost") != 0)
+  if ((attr = ippFindAttribute(con->request, "compression",
+                               IPP_TAG_KEYWORD)) != NULL)
+  {
+    if (strcmp(attr->values[0].string.text, "none")
+#ifdef HAVE_LIBZ
+        && strcmp(attr->values[0].string.text, "gzip")
+#endif /* HAVE_LIBZ */
+      )
     {
-     /*
-      * Can't override the value if we aren't connected via localhost.
-      * Also, we can only have 1 value and it must be a name value.
-      */
-
-      switch (attr->value_tag)
-      {
-        case IPP_TAG_STRING :
-       case IPP_TAG_TEXTLANG :
-       case IPP_TAG_NAMELANG :
-       case IPP_TAG_TEXT :
-       case IPP_TAG_NAME :
-       case IPP_TAG_KEYWORD :
-       case IPP_TAG_URI :
-       case IPP_TAG_URISCHEME :
-       case IPP_TAG_CHARSET :
-       case IPP_TAG_LANGUAGE :
-       case IPP_TAG_MIMETYPE :
-          /*
-           * Free old strings...
-           */
-
-           for (i = 0; i < attr->num_values; i ++)
-           {
-             free(attr->values[i].string.text);
-             attr->values[i].string.text = NULL;
-             if (attr->values[i].string.charset)
-             {
-               free(attr->values[i].string.charset);
-               attr->values[i].string.charset = NULL;
-             }
-            }
-
-       default :
-            break;
-      }
-
-     /*
-      * Use the default connection hostname instead...
-      */
-
-      attr->value_tag             = IPP_TAG_NAME;
-      attr->num_values            = 1;
-      attr->values[0].string.text = strdup(con->http.hostname);
+      send_ipp_status(con, IPP_ATTRIBUTES,
+                      _("Unsupported compression \"%s\"!"),
+                     attr->values[0].string.text);
+      ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
+                  "compression", NULL, attr->values[0].string.text);
+      return;
     }
 
-    attr->group_tag = IPP_TAG_JOB;
-  }
-  else
-  {
-   /*
-    * No job-originating-host-name attribute, so use the hostname from
-    * the connection...
-    */
-
-    ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, 
-                "job-originating-host-name", NULL, con->http.hostname);
+#ifdef HAVE_LIBZ
+    if (!strcmp(attr->values[0].string.text, "gzip"))
+      compression = CUPS_FILE_GZIP;
+#endif /* HAVE_LIBZ */
   }
 
-  ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
-  job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
-                             "job-state", IPP_JOB_PENDING);
-  job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                              "job-media-sheets-completed", 0);
-  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
-               printer->uri);
-  ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
-               title);
-
-  if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) == NULL)
-    attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                         "job-k-octets", 0);
-
-  if (stat(con->filename, &fileinfo))
-    kbytes = 0;
-  else
-    kbytes = (fileinfo.st_size + 1023) / 1024;
-
-  cupsdUpdateQuota(printer, job->username, 0, kbytes);
-  attr->values[0].integer += kbytes;
-
-  ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation",
-                time(NULL));
-  attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                       "time-at-processing", 0);
-  attr->value_tag = IPP_TAG_NOVALUE;
-  attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
-                       "time-at-completed", 0);
-  attr->value_tag = IPP_TAG_NOVALUE;
+ /*
+  * Do we have a file to print?
+  */
 
-  if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
-    attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
-  if (attr == NULL)
-    attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                        "job-hold-until", NULL, "no-hold");
+  if (!con->filename)
+  {
+    send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
+    return;
+  }
 
-  if (attr != NULL && strcmp(attr->values[0].string.text, "no-hold") != 0 &&
-      !(printer->type & CUPS_PRINTER_REMOTE))
+ /*
+  * Is the destination valid?
+  */
+
+  if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
   {
    /*
-    * Hold job until specified time...
+    * Bad URI...
     */
 
-    job->state->values[0].integer = IPP_JOB_HELD;
-    cupsdSetJobHoldUntil(job, attr->values[0].string.text);
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
   }
 
-  if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
-      Classification)
+ /*
+  * Is it a format we support?
+  */
+
+  if ((format = ippFindAttribute(con->request, "document-format",
+                                 IPP_TAG_MIMETYPE)) != NULL)
   {
    /*
-    * Add job sheets options...
+    * Grab format from client...
     */
 
-    if ((attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) == NULL)
+    if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
+               type) != 2)
     {
-      cupsdLogMessage(CUPSD_LOG_DEBUG,
-                      "Adding default job-sheets values \"%s,%s\"...",
-                      printer->job_sheets[0], printer->job_sheets[1]);
-
-      attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
-                           2, NULL, NULL);
-      attr->values[0].string.text = strdup(printer->job_sheets[0]);
-      attr->values[1].string.text = strdup(printer->job_sheets[1]);
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Could not scan type \"%s\"!"),
+                     format->values[0].string.text);
+      return;
     }
-
-    job->job_sheets = attr;
-
+  }
+  else if ((default_format = cupsGetOption("document-format",
+                                           printer->num_options,
+                                          printer->options)) != NULL)
+  {
    /*
-    * Enforce classification level if set...
+    * Use default document format...
     */
 
-    if (Classification)
+    if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
     {
-      cupsdLogMessage(CUPSD_LOG_INFO,
-                      "Classification=\"%s\", ClassifyOverride=%d",
-                      Classification ? Classification : "(null)",
-                     ClassifyOverride);
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Could not scan type \"%s\"!"),
+                     default_format);
+      return;
+    }
+  }
+  else
+  {
+   /*
+    * Auto-type it!
+    */
 
-      if (ClassifyOverride)
-      {
-        if (strcmp(attr->values[0].string.text, "none") == 0 &&
-           (attr->num_values == 1 ||
-            strcmp(attr->values[1].string.text, "none") == 0))
-        {
-        /*
-          * Force the leading banner to have the classification on it...
-         */
+    strcpy(super, "application");
+    strcpy(type, "octet-stream");
+  }
 
-          cupsdSetString(&attr->values[0].string.text, Classification);
+  if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
+  {
+   /*
+    * Auto-type the file...
+    */
 
-         cupsdLogMessage(CUPSD_LOG_NOTICE,
-                         "[Job %d] CLASSIFICATION FORCED "
-                         "job-sheets=\"%s,none\", "
-                         "job-originating-user-name=\"%s\"",
-                         job->id, Classification, job->username);
-       }
-       else if (attr->num_values == 2 &&
-                strcmp(attr->values[0].string.text, attr->values[1].string.text) != 0 &&
-                strcmp(attr->values[0].string.text, "none") != 0 &&
-                strcmp(attr->values[1].string.text, "none") != 0)
-        {
-        /*
-         * Can't put two different security markings on the same document!
-         */
+    ipp_attribute_t    *doc_name;      /* document-name attribute */
 
-          cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
 
-         cupsdLogMessage(CUPSD_LOG_NOTICE,
-                         "[Job %d] CLASSIFICATION FORCED "
-                         "job-sheets=\"%s,%s\", "
-                         "job-originating-user-name=\"%s\"",
-                         job->id, attr->values[0].string.text,
-                         attr->values[1].string.text, job->username);
-       }
-       else if (strcmp(attr->values[0].string.text, Classification) &&
-                strcmp(attr->values[0].string.text, "none") &&
-                (attr->num_values == 1 ||
-                 (strcmp(attr->values[1].string.text, Classification) &&
-                  strcmp(attr->values[1].string.text, "none"))))
-        {
-         if (attr->num_values == 1)
-            cupsdLogMessage(CUPSD_LOG_NOTICE,
-                           "[Job %d] CLASSIFICATION OVERRIDDEN "
-                           "job-sheets=\"%s\", "
-                           "job-originating-user-name=\"%s\"",
-                           job->id, attr->values[0].string.text,
-                           job->username);
-          else
-            cupsdLogMessage(CUPSD_LOG_NOTICE,
-                           "[Job %d] CLASSIFICATION OVERRIDDEN "
-                           "job-sheets=\"%s,%s\", "
-                           "job-originating-user-name=\"%s\"",
-                           job->id, attr->values[0].string.text,
-                           attr->values[1].string.text, job->username);
-        }
-      }
-      else if (strcmp(attr->values[0].string.text, Classification) != 0 &&
-               (attr->num_values == 1 ||
-              strcmp(attr->values[1].string.text, Classification) != 0))
-      {
-       /*
-        * Force the banner to have the classification on it...
-       */
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
 
-        if (attr->num_values > 1 &&
-           !strcmp(attr->values[0].string.text, attr->values[1].string.text))
-       {
-          cupsdSetString(&(attr->values[0].string.text), Classification);
-          cupsdSetString(&(attr->values[1].string.text), Classification);
-       }
-        else
-       {
-          if (attr->num_values == 1 ||
-             strcmp(attr->values[0].string.text, "none"))
-            cupsdSetString(&(attr->values[0].string.text), Classification);
+    doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
+    filetype = mimeFileType(MimeDatabase, con->filename,
+                            doc_name ? doc_name->values[0].string.text : NULL,
+                           &compression);
 
-          if (attr->num_values > 1 &&
-             strcmp(attr->values[1].string.text, "none"))
-            cupsdSetString(&(attr->values[1].string.text), Classification);
-        }
+    if (!filetype)
+      filetype = mimeType(MimeDatabase, super, type);
 
-        if (attr->num_values > 1)
-         cupsdLogMessage(CUPSD_LOG_NOTICE,
-                         "[Job %d] CLASSIFICATION FORCED "
-                         "job-sheets=\"%s,%s\", "
-                         "job-originating-user-name=\"%s\"",
-                         job->id, attr->values[0].string.text,
-                         attr->values[1].string.text, job->username);
-        else
-         cupsdLogMessage(CUPSD_LOG_NOTICE,
-                         "[Job %d] CLASSIFICATION FORCED "
-                         "job-sheets=\"%s\", "
-                         "job-originating-user-name=\"%s\"",
-                         job->id, Classification, job->username);
-      }
-    }
+    cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
+                   filetype->super, filetype->type);
+  }
+  else
+    filetype = mimeType(MimeDatabase, super, type);
 
+  if (filetype &&
+      (!format ||
+       (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
+  {
    /*
-    * Add the starting sheet...
+    * Replace the document-format attribute value with the auto-typed or
+    * default one.
     */
 
-    if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
-    {
-      cupsdLogMessage(CUPSD_LOG_INFO,
-                      "Adding start banner page \"%s\" to job %d.",
-                     attr->values[0].string.text, job->id);
+    snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
+             filetype->type);
 
-      kbytes = copy_banner(con, job, attr->values[0].string.text);
+    if (format)
+    {
+      _cupsStrFree(format->values[0].string.text);
 
-      cupsdUpdateQuota(printer, job->username, 0, kbytes);
+      format->values[0].string.text = _cupsStrAlloc(mimetype);
     }
+    else
+      ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
+                  "document-format", NULL, mimetype);
   }
-  else if ((attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL)
-    job->sheets = attr;
-   
- /*
-  * Add the job file...
-  */
+  else if (!filetype)
+  {
+    send_ipp_status(con, IPP_DOCUMENT_FORMAT,
+                    _("Unsupported format \'%s/%s\'!"), super, type);
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Hint: Do you have the raw file printing rules enabled?");
 
-  if (add_file(con, job, filetype, compression))
-    return;
+    if (format)
+      ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
+                   "document-format", NULL, format->values[0].string.text);
 
-  snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
-           job->num_files);
-  rename(con->filename, filename);
-  cupsdClearString(&con->filename);
+    return;
+  }
 
  /*
-  * See if we need to add the ending sheet...
+  * Read any embedded job ticket info from PS files...
   */
 
-  if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
-      attr->num_values > 1)
-  {
-   /*
-    * Yes...
-    */
-
-    cupsdLogMessage(CUPSD_LOG_INFO, "Adding end banner page \"%s\" to job %d.",
-                    attr->values[1].string.text, job->id);
-
-    kbytes = copy_banner(con, job, attr->values[1].string.text);
-
-    cupsdUpdateQuota(printer, job->username, 0, kbytes);
-  }
+  if (!strcasecmp(filetype->super, "application") &&
+      (!strcasecmp(filetype->type, "postscript") ||
+       !strcasecmp(filetype->type, "pdf")))
+    read_job_ticket(con);
 
  /*
-  * Fill in the response info...
+  * Create the job object...
   */
 
-  snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
-          LocalPort, job->id);
+  if ((job = add_job(con, printer, filetype)) == NULL)
+    return;
 
-  ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
-               job_uri);
+ /*
+  * Update quota data...
+  */
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
+  if (stat(con->filename, &fileinfo))
+    kbytes = 0;
+  else
+    kbytes = (fileinfo.st_size + 1023) / 1024;
 
-  ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
-                job->state->values[0].integer);
-  add_job_state_reasons(con, job);
+  cupsdUpdateQuota(printer, job->username, 0, kbytes);
 
-  con->response->request.status.status_code = IPP_OK;
+  if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
+                               IPP_TAG_INTEGER)) != NULL)
+    attr->values[0].integer += kbytes;
 
  /*
-  * Add any job subscriptions...
+  * Add the job file...
   */
 
-  add_job_subscriptions(con, job);
+  if (add_file(con, job, filetype, compression))
+    return;
+
+  snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
+           job->num_files);
+  rename(con->filename, filename);
+  cupsdClearString(&con->filename);
 
  /*
-  * Set all but the first two attributes to the job attributes group...
+  * See if we need to add the ending sheet...
   */
 
-  for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
-    attr->group_tag = IPP_TAG_JOB;
+  if (cupsdTimeoutJob(job))
+    return;
 
  /*
   * Log and save the job...
   */
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d queued on \"%s\" by \"%s\".", job->id,
-                  job->dest, job->username);
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "Job %d hold_until = %d", job->id,
-                  (int)job->hold_until);
-
-  cupsdSaveJob(job);
-
-  cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
+  cupsdLogJob(job, CUPSD_LOG_INFO,
+             "File of type %s/%s queued by \"%s\".",
+             filetype->super, filetype->type, job->username);
+  cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
+  cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
+             job->dest, job->username);
 
  /*
   * Start the job if possible...
@@ -7118,16 +8767,16 @@ print_job(cupsd_client_t  *con,         /* I - Client connection */
 
 
 /*
- * 'read_ps_job_ticket()' - Reads a job ticket embedded in a PS file.
+ * 'read_job_ticket()' - Read a job ticket embedded in a print file.
  *
- * This function only gets called when printing a single PostScript
+ * This function only gets called when printing a single PDF or PostScript
  * file using the Print-Job operation.  It doesn't work for Create-Job +
  * Send-File, since the job attributes need to be set at job creation
- * time for banners to work.  The embedded PS job ticket stuff is here
- * only to allow the Windows printer driver for CUPS to pass in JCL
+ * time for banners to work.  The embedded job ticket stuff is here
+ * primarily to allow the Windows printer driver for CUPS to pass in JCL
  * options and IPP attributes which otherwise would be lost.
  *
- * The format of a PS job ticket is simple:
+ * The format of a job ticket is simple:
  *
  *     %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
  *
@@ -7137,8 +8786,8 @@ print_job(cupsd_client_t  *con,           /* I - Client connection */
  *     %cupsJobTicket: attrN=valueN
  *
  * Job ticket lines must appear immediately after the first line that
- * specifies PostScript format (%!PS-Adobe-3.0), and CUPS will stop
- * looking for job ticket info when it finds a line that does not begin
+ * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
+ * stops looking for job ticket info when it finds a line that does not begin
  * with "%cupsJobTicket:".
  *
  * The maximum length of a job ticket line, including the prefix, is
@@ -7151,7 +8800,7 @@ print_job(cupsd_client_t  *con,           /* I - Client connection */
  */
 
 static void
-read_ps_job_ticket(cupsd_client_t *con)        /* I - Client connection */
+read_job_ticket(cupsd_client_t *con)   /* I - Client connection */
 {
   cups_file_t          *fp;            /* File to read from */
   char                 line[256];      /* Line data */
@@ -7170,8 +8819,7 @@ read_ps_job_ticket(cupsd_client_t *con)   /* I - Client connection */
   if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
   {
     cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "read_ps_job_ticket: Unable to open PostScript print file "
-                   "- %s",
+                    "Unable to open print file for job ticket - %s",
                     strerror(errno));
     return;
   }
@@ -7183,14 +8831,13 @@ read_ps_job_ticket(cupsd_client_t *con) /* I - Client connection */
   if (cupsFileGets(fp, line, sizeof(line)) == NULL)
   {
     cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "read_ps_job_ticket: Unable to read from PostScript print "
-                   "file - %s",
+                    "Unable to read from print file for job ticket - %s",
                     strerror(errno));
     cupsFileClose(fp);
     return;
   }
 
-  if (strncmp(line, "%!PS-Adobe-", 11) != 0)
+  if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
   {
    /*
     * Not a DSC-compliant file, so no job ticket info will be available...
@@ -7207,13 +8854,13 @@ read_ps_job_ticket(cupsd_client_t *con) /* I - Client connection */
   num_options = 0;
   options     = NULL;
 
-  while (cupsFileGets(fp, line, sizeof(line)) != NULL)
+  while (cupsFileGets(fp, line, sizeof(line)))
   {
    /*
     * Stop at the first non-ticket line...
     */
 
-    if (strncmp(line, "%cupsJobTicket:", 15) != 0)
+    if (strncmp(line, "%cupsJobTicket:", 15))
       break;
 
    /*
@@ -7244,7 +8891,7 @@ read_ps_job_ticket(cupsd_client_t *con)   /* I - Client connection */
   * See what the user wants to change.
   */
 
-  for (attr = ticket->attrs; attr != NULL; attr = attr->next)
+  for (attr = ticket->attrs; attr; attr = attr->next)
   {
     if (attr->group_tag != IPP_TAG_JOB || !attr->name)
       continue;
@@ -7258,7 +8905,8 @@ read_ps_job_ticket(cupsd_client_t *con)   /* I - Client connection */
        !strncmp(attr->name, "time-at-", 8))
       continue; /* Read-only attrs */
 
-    if ((attr2 = ippFindAttribute(con->request, attr->name, IPP_TAG_ZERO)) != NULL)
+    if ((attr2 = ippFindAttribute(con->request, attr->name,
+                                  IPP_TAG_ZERO)) != NULL)
     {
      /*
       * Some other value; first free the old value...
@@ -7271,7 +8919,7 @@ read_ps_job_ticket(cupsd_client_t *con)   /* I - Client connection */
       }
       else
       {
-       for (prev2 = con->request->attrs; prev2 != NULL; prev2 = prev2->next)
+       for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
          if (prev2->next == attr2)
          {
            prev2->next = attr2->next;
@@ -7282,7 +8930,7 @@ read_ps_job_ticket(cupsd_client_t *con)   /* I - Client connection */
       if (con->request->last == attr2)
         con->request->last = prev2;
 
-      _ipp_free_attr(attr2);
+      _ippFreeAttr(attr2);
     }
 
    /*
@@ -7310,13 +8958,7 @@ reject_jobs(cupsd_client_t  *con,        /* I - Client connection */
             ipp_attribute_t *uri)      /* I - Printer or class URI */
 {
   http_status_t        status;                 /* Policy status */
-  cups_ptype_t dtype;                  /* Destination type (printer or class) */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
-               username[HTTP_MAX_URI], /* Username portion of URI */
-               host[HTTP_MAX_URI],     /* Host portion of URI */
-               resource[HTTP_MAX_URI]; /* Resource portion of URI */
-  int          port;                   /* Port portion of URI */
-  const char   *name;                  /* Printer name */
+  cups_ptype_t dtype;                  /* Destination type (printer/class) */
   cupsd_printer_t *printer;            /* Printer data */
   ipp_attribute_t *attr;               /* printer-state-message text */
 
@@ -7328,11 +8970,7 @@ reject_jobs(cupsd_client_t  *con,        /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -7349,7 +8987,7 @@ reject_jobs(cupsd_client_t  *con, /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
@@ -7370,18 +9008,86 @@ reject_jobs(cupsd_client_t  *con,       /* I - Client connection */
 
   if (dtype & CUPS_PRINTER_CLASS)
   {
-    cupsdSaveAllClasses();
+    cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
 
     cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
-                    name, con->username);
+                    printer->name, get_username(con));
   }
   else
   {
-    cupsdSaveAllPrinters();
+    cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
+
+    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
+                    printer->name, get_username(con));
+  }
+
+ /*
+  * Everything was ok, so return OK status...
+  */
+
+  con->response->request.status.status_code = IPP_OK;
+}
+
+
+/*
+ * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
+ */
+
+static void
+release_held_new_jobs(
+    cupsd_client_t  *con,              /* I - Connection */
+    ipp_attribute_t *uri)              /* I - Printer URI */
+{
+  http_status_t                status;         /* Policy status */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  cupsd_printer_t      *printer;       /* Printer data */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
+
+ /*
+  * Is the destination valid?
+  */
+
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
+  {
+   /*
+    * Bad URI...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
+  }
+
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, printer);
+    return;
+  }
+
+ /*
+  * Hold pending/new jobs sent to the printer...
+  */
+
+  printer->holding_new_jobs = 0;
+
+  cupsdSetPrinterReasons(printer, "-hold-new-jobs");
+  cupsdAddPrinterHistory(printer);
 
-    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
-                    name, con->username);
-  }
+  if (dtype & CUPS_PRINTER_CLASS)
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Class \"%s\" now printing pending/new jobs (\"%s\").",
+                    printer->name, get_username(con));
+  else
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Printer \"%s\" now printing pending/new jobs (\"%s\").",
+                    printer->name, get_username(con));
 
  /*
   * Everything was ok, so return OK status...
@@ -7401,7 +9107,7 @@ release_job(cupsd_client_t  *con, /* I - Client connection */
 {
   ipp_attribute_t *attr;               /* Current attribute */
   int          jobid;                  /* Job ID */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
@@ -7422,7 +9128,8 @@ release_job(cupsd_client_t  *con, /* I - Client connection */
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -7437,9 +9144,9 @@ release_job(cupsd_client_t  *con, /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
     if (strncmp(resource, "/jobs/", 6))
     {
@@ -7474,7 +9181,7 @@ release_job(cupsd_client_t  *con, /* I - Client connection */
   * See if job is "held"...
   */
 
-  if (job->state->values[0].integer != IPP_JOB_HELD)
+  if (job->state_value != IPP_JOB_HELD)
   {
    /*
     * Nope - return a "not possible" error...
@@ -7490,10 +9197,7 @@ release_job(cupsd_client_t  *con,        /* I - Client connection */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to release job id "
-                     "%d owned by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
@@ -7501,14 +9205,19 @@ release_job(cupsd_client_t  *con,       /* I - Client connection */
   * Reset the job-hold-until value to "no-hold"...
   */
 
-  if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
+  if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
+                               IPP_TAG_KEYWORD)) == NULL)
     attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
 
-  if (attr != NULL)
+  if (attr)
   {
-    free(attr->values[0].string.text);
+    _cupsStrFree(attr->values[0].string.text);
+
     attr->value_tag = IPP_TAG_KEYWORD;
-    attr->values[0].string.text = strdup("no-hold");
+    attr->values[0].string.text = _cupsStrAlloc("no-hold");
+
+    cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
+                  "Job job-hold-until value changed by user.");
   }
 
  /*
@@ -7517,10 +9226,14 @@ release_job(cupsd_client_t  *con,       /* I - Client connection */
 
   cupsdReleaseJob(job);
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was released by \"%s\".", jobid,
-                  username);
+  cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
+                "Job released by user.");
+
+  cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
 
   con->response->request.status.status_code = IPP_OK;
+
+  cupsdCheckJobs();
 }
 
 
@@ -7533,6 +9246,79 @@ renew_subscription(
     cupsd_client_t *con,               /* I - Client connection */
     int            sub_id)             /* I - Subscription ID */
 {
+  http_status_t                status;         /* Policy status */
+  cupsd_subscription_t *sub;           /* Subscription */
+  ipp_attribute_t      *lease;         /* notify-lease-duration */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "renew_subscription(con=%p[%d], sub_id=%d)",
+                  con, con->http.fd, sub_id);
+
+ /*
+  * Is the subscription ID valid?
+  */
+
+  if ((sub = cupsdFindSubscription(sub_id)) == NULL)
+  {
+   /*
+    * Bad subscription ID...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("notify-subscription-id %d no good!"), sub_id);
+    return;
+  }
+
+  if (sub->job)
+  {
+   /*
+    * Job subscriptions cannot be renewed...
+    */
+
+    send_ipp_status(con, IPP_NOT_POSSIBLE,
+                    _("Job subscriptions cannot be renewed!"));
+    return;
+  }
+
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
+                                             DefaultPolicyPtr,
+                                 con, sub->owner)) != HTTP_OK)
+  {
+    send_http_error(con, status, sub->dest);
+    return;
+  }
+
+ /*
+  * Renew the subscription...
+  */
+
+  lease = ippFindAttribute(con->request, "notify-lease-duration",
+                           IPP_TAG_INTEGER);
+
+  sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
+
+  if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
+  {
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "renew_subscription: Limiting notify-lease-duration to "
+                   "%d seconds.",
+                   MaxLeaseDuration);
+    sub->lease = MaxLeaseDuration;
+  }
+
+  sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
+
+  cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
+
+  con->response->request.status.status_code = IPP_OK;
+
+  ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                "notify-lease-duration", sub->lease);
 }
 
 
@@ -7546,12 +9332,12 @@ restart_job(cupsd_client_t  *con,       /* I - Client connection */
 {
   ipp_attribute_t *attr;               /* Current attribute */
   int          jobid;                  /* Job ID */
-  char         method[HTTP_MAX_URI],   /* Method portion of URI */
+  cupsd_job_t  *job;                   /* Job information */
+  char         scheme[HTTP_MAX_URI],   /* Method portion of URI */
                username[HTTP_MAX_URI], /* Username portion of URI */
                host[HTTP_MAX_URI],     /* Host portion of URI */
                resource[HTTP_MAX_URI]; /* Resource portion of URI */
   int          port;                   /* Port portion of URI */
-  cupsd_job_t  *job;                   /* Job information */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
@@ -7567,7 +9353,8 @@ restart_job(cupsd_client_t  *con, /* I - Client connection */
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -7582,11 +9369,11 @@ restart_job(cupsd_client_t  *con,       /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
-    if (strncmp(resource, "/jobs/", 6) != 0)
+    if (strncmp(resource, "/jobs/", 6))
     {
      /*
       * Not a valid URI!
@@ -7619,7 +9406,7 @@ restart_job(cupsd_client_t  *con, /* I - Client connection */
   * See if job is in any of the "completed" states...
   */
 
-  if (job->state->values[0].integer <= IPP_JOB_PROCESSING)
+  if (job->state_value <= IPP_JOB_PROCESSING)
   {
    /*
     * Nope - return a "not possible" error...
@@ -7634,7 +9421,9 @@ restart_job(cupsd_client_t  *con, /* I - Client connection */
   * See if we have retained the job files...
   */
 
-  if (!JobFiles && job->state->values[0].integer > IPP_JOB_STOPPED)
+  cupsdLoadJob(job);
+
+  if (!job->attrs || job->num_files == 0)
   {
    /*
     * Nope - return a "not possible" error...
@@ -7651,21 +9440,44 @@ restart_job(cupsd_client_t  *con,       /* I - Client connection */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to restart job id "
-                     "%d owned by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
  /*
-  * Restart the job and return...
+  * See if the job-hold-until attribute is specified...
   */
 
-  cupsdRestartJob(job);
+  if ((attr = ippFindAttribute(con->request, "job-hold-until",
+                               IPP_TAG_KEYWORD)) == NULL)
+    attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
+
+  if (attr && strcmp(attr->values[0].string.text, "no-hold"))
+  {
+   /*
+    * Return the job to a held state...
+    */
+
+    cupsdLogJob(job, CUPSD_LOG_DEBUG,
+               "Restarted by \"%s\" with job-hold-until=%s.",
+                username, attr->values[0].string.text);
+    cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
+
+    cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE,
+                  NULL, job, "Job restarted by user with job-hold-until=%s",
+                 attr->values[0].string.text);
+  }
+  else
+  {
+   /*
+    * Restart the job...
+    */
+
+    cupsdRestartJob(job);
+    cupsdCheckJobs();
+  }
 
-  cupsdLogMessage(CUPSD_LOG_INFO, "Job %d was restarted by \"%s\".", jobid,
-                  username);
+  cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
 
   con->response->request.status.status_code = IPP_OK;
 }
@@ -7676,22 +9488,25 @@ restart_job(cupsd_client_t  *con,       /* I - Client connection */
  */
 
 static void
-save_auth_info(cupsd_client_t *con,    /* I - Client connection */
-               cupsd_job_t    *job)    /* I - Job */
+save_auth_info(
+    cupsd_client_t  *con,              /* I - Client connection */
+    cupsd_job_t     *job,              /* I - Job */
+    ipp_attribute_t *auth_info)                /* I - auth-info attribute, if any */
 {
-  int          i;                      /* Looping var */
-  char         filename[1024];         /* Job authentication filename */
-  cups_file_t  *fp;                    /* Job authentication file */
-  char         line[1024];             /* Line for file */
+  int                  i;              /* Looping var */
+  char                 filename[1024]; /* Job authentication filename */
+  cups_file_t          *fp;            /* Job authentication file */
+  char                 line[2048];     /* Line for file */
+  cupsd_printer_t      *dest;          /* Destination printer/class */
 
 
  /*
   * This function saves the in-memory authentication information for
   * a job so that it can be used to authenticate with a remote host.
   * The information is stored in a file that is readable only by the
-  * root user.  The username and password are Base-64 encoded, each
-  * on a separate line, followed by random number (up to 1024) of
-  * newlines to limit the amount of information that is exposed.
+  * root user.  The fields are Base-64 encoded, each on a separate line,
+  * followed by random number (up to 1024) of newlines to limit the
+  * amount of information that is exposed.
   *
   * Because of the potential for exposing of authentication information,
   * this functionality is only enabled when running cupsd as root.
@@ -7711,6 +9526,9 @@ save_auth_info(cupsd_client_t *con,       /* I - Client connection */
   if (RunUser)
     return;
 
+  if ((dest = cupsdFindDest(job->dest)) == NULL)
+    return;
+
  /*
   * Create the authentication file and change permissions...
   */
@@ -7727,19 +9545,54 @@ save_auth_info(cupsd_client_t *con,     /* I - Client connection */
   fchown(cupsFileNumber(fp), 0, 0);
   fchmod(cupsFileNumber(fp), 0400);
 
- /*
-  * Write the authenticated username...
-  */
+  if (auth_info && auth_info->num_values == dest->num_auth_info_required)
+  {
+   /*
+    * Write 1 to 3 auth values...
+    */
 
-  httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
-  cupsFilePrintf(fp, "%s\n", line);
+    cupsdClearString(&job->auth_username);
+    cupsdClearString(&job->auth_domain);
+    cupsdClearString(&job->auth_password);
 
- /*
-  * Write the authenticated password...
-  */
+    for (i = 0; i < auth_info->num_values; i ++)
+    {
+      httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
+                     strlen(auth_info->values[i].string.text));
+      cupsFilePrintf(fp, "%s\n", line);
+
+      if (!strcmp(dest->auth_info_required[i], "username"))
+        cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s",
+                       auth_info->values[i].string.text);
+      else if (!strcmp(dest->auth_info_required[i], "domain"))
+        cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s",
+                       auth_info->values[i].string.text);
+      else if (!strcmp(dest->auth_info_required[i], "password"))
+        cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s",
+                       auth_info->values[i].string.text);
+    }
+  }
+  else if (con->username[0])
+  {
+   /*
+    * Write the authenticated username...
+    */
+
+    httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
+    cupsFilePrintf(fp, "%s\n", line);
+
+    cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username);
+    cupsdClearString(&job->auth_domain);
+
+   /*
+    * Write the authenticated password...
+    */
+
+    httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
+    cupsFilePrintf(fp, "%s\n", line);
 
-  httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
-  cupsFilePrintf(fp, "%s\n", line);
+    cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password);
+  }
 
  /*
   * Write a random number of newlines to the end of the file...
@@ -7753,7 +9606,47 @@ save_auth_info(cupsd_client_t *con,      /* I - Client connection */
   */
 
   cupsFileClose(fp);
+
+#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
+  if (con->gss_creds)
+    save_krb5_creds(con, job);
+  else if (job->ccname)
+    cupsdClearString(&(job->ccname));
+#endif /* HAVE_GSSAPI && HAVE_KRB5_H */
+}
+
+
+#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
+/*
+ * 'save_krb5_creds()' - Save Kerberos credentials for the job.
+ */
+
+static void
+save_krb5_creds(cupsd_client_t *con,   /* I - Client connection */
+                cupsd_job_t    *job)   /* I - Job */
+{
+ /*
+  * Get the credentials...
+  */
+
+  job->ccache = cupsdCopyKrb5Creds(con);
+
+ /*
+  * Add the KRB5CCNAME environment variable to the job so that the
+  * backend can use the credentials when printing.
+  */
+
+  if (job->ccache)
+  {
+    cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s",
+                   krb5_cc_get_name(KerberosContext, job->ccache));
+
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "save_krb5_creds: %s", job->ccname);
+  }
+  else
+    cupsdClearString(&(job->ccname));
 }
+#endif /* HAVE_GSSAPI && HAVE_KRB5_H */
 
 
 /*
@@ -7765,12 +9658,14 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
              ipp_attribute_t *uri)     /* I - Printer URI */
 {
   ipp_attribute_t      *attr;          /* Current attribute */
-  ipp_attribute_t      *format;        /* Document-format attribute */
+  ipp_attribute_t      *format;        /* Request's document-format attribute */
+  ipp_attribute_t      *jformat;       /* Job's document-format attribute */
+  const char           *default_format;/* document-format-default value */
   int                  jobid;          /* Job ID number */
   cupsd_job_t          *job;           /* Current job */
   char                 job_uri[HTTP_MAX_URI],
                                        /* Job URI */
-                       method[HTTP_MAX_URI],
+                       scheme[HTTP_MAX_URI],
                                        /* Method portion of URI */
                        username[HTTP_MAX_URI],
                                        /* Username portion of URI */
@@ -7791,6 +9686,7 @@ send_document(cupsd_client_t  *con,       /* I - Client connection */
   struct stat          fileinfo;       /* File information */
   int                  kbytes;         /* Size of file */
   int                  compression;    /* Type of compression */
+  int                  start_job;      /* Start the job? */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
@@ -7806,7 +9702,8 @@ send_document(cupsd_client_t  *con,       /* I - Client connection */
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -7821,9 +9718,9 @@ send_document(cupsd_client_t  *con,       /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
     if (strncmp(resource, "/jobs/", 6))
     {
@@ -7854,16 +9751,15 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
     return;
   }
 
+  printer = cupsdFindDest(job->dest);
+
  /*
   * See if the job is owned by the requesting user...
   */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to send document "
-                     "for job #%d owned by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
@@ -7874,7 +9770,8 @@ send_document(cupsd_client_t  *con,       /* I - Client connection */
 
   compression = CUPS_FILE_NONE;
 
-  if ((attr = ippFindAttribute(con->request, "compression", IPP_TAG_KEYWORD)) != NULL)
+  if ((attr = ippFindAttribute(con->request, "compression",
+                               IPP_TAG_KEYWORD)) != NULL)
   {
     if (strcmp(attr->values[0].string.text, "none")
 #ifdef HAVE_LIBZ
@@ -7916,13 +9813,30 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
     * Grab format from client...
     */
 
-    if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2)
+    if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
+               super, type) != 2)
     {
       send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
                      format->values[0].string.text);
       return;
     }
   }
+  else if ((default_format = cupsGetOption("document-format",
+                                           printer->num_options,
+                                          printer->options)) != NULL)
+  {
+   /*
+    * Use default document format...
+    */
+
+    if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
+    {
+      send_ipp_status(con, IPP_BAD_REQUEST,
+                      _("Could not scan type \"%s\"!"),
+                     default_format);
+      return;
+    }
+  }
   else
   {
    /*
@@ -7933,42 +9847,53 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
     strcpy(type, "octet-stream");
   }
 
-  if (strcmp(super, "application") == 0 &&
-      strcmp(type, "octet-stream") == 0)
+  if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
   {
    /*
     * Auto-type the file...
     */
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "send_document: auto-typing file...");
+    ipp_attribute_t    *doc_name;      /* document-name attribute */
 
-    filetype = mimeFileType(MimeDatabase, con->filename, &compression);
 
-    if (filetype != NULL)
-    {
-     /*
-      * Replace the document-format attribute value with the auto-typed one.
-      */
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
 
-      snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
-               filetype->type);
+    doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
+    filetype = mimeFileType(MimeDatabase, con->filename,
+                            doc_name ? doc_name->values[0].string.text : NULL,
+                           &compression);
 
-      if (format != NULL)
-      {
-       free(format->values[0].string.text);
-       format->values[0].string.text = strdup(mimetype);
-      }
-      else
-        ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
-                    "document-format", NULL, mimetype);
-    }
-    else
+    if (!filetype)
       filetype = mimeType(MimeDatabase, super, type);
+
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
+               filetype->super, filetype->type);
   }
   else
     filetype = mimeType(MimeDatabase, super, type);
 
-  if (filetype == NULL)
+  if (filetype)
+  {
+   /*
+    * Replace the document-format attribute value with the auto-typed or
+    * default one.
+    */
+
+    snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
+             filetype->type);
+
+    if ((jformat = ippFindAttribute(job->attrs, "document-format",
+                                    IPP_TAG_MIMETYPE)) != NULL)
+    {
+      _cupsStrFree(jformat->values[0].string.text);
+
+      jformat->values[0].string.text = _cupsStrAlloc(mimetype);
+    }
+    else
+      ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
+                  "document-format", NULL, mimetype);
+  }
+  else if (!filetype)
   {
     send_ipp_status(con, IPP_DOCUMENT_FORMAT,
                     _("Unsupported format \'%s/%s\'!"), super, type);
@@ -7982,22 +9907,29 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
     return;
   }
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG,
-                  "send_document: request file type is %s/%s.",
-                 filetype->super, filetype->type);
+  if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
+  {
+    snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
+             filetype->type);
+
+    send_ipp_status(con, IPP_DOCUMENT_FORMAT,
+                    _("Unsupported format \'%s\'!"), mimetype);
+
+    ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
+                 "document-format", NULL, mimetype);
+
+    return;
+  }
 
  /*
   * Add the file to the job...
   */
 
+  cupsdLoadJob(job);
+
   if (add_file(con, job, filetype, compression))
     return;
 
-  if (job->dtype & CUPS_PRINTER_CLASS)
-    printer = cupsdFindClass(job->dest);
-  else
-    printer = cupsdFindPrinter(job->dest);
-
   if (stat(con->filename, &fileinfo))
     kbytes = 0;
   else
@@ -8005,7 +9937,8 @@ send_document(cupsd_client_t  *con,       /* I - Client connection */
 
   cupsdUpdateQuota(printer, job->username, 0, kbytes);
 
-  if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
+  if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
+                               IPP_TAG_INTEGER)) != NULL)
     attr->values[0].integer += kbytes;
 
   snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
@@ -8014,74 +9947,64 @@ send_document(cupsd_client_t  *con,     /* I - Client connection */
 
   cupsdClearString(&con->filename);
 
-  cupsdLogMessage(CUPSD_LOG_INFO,
-                  "File of type %s/%s queued in job #%d by \"%s\".",
-                  filetype->super, filetype->type, job->id, job->username);
+  cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
+             filetype->super, filetype->type, job->username);
 
  /*
   * Start the job if this is the last document...
   */
 
-  if ((attr = ippFindAttribute(con->request, "last-document", IPP_TAG_BOOLEAN)) != NULL &&
+  if ((attr = ippFindAttribute(con->request, "last-document",
+                               IPP_TAG_BOOLEAN)) != NULL &&
       attr->values[0].boolean)
   {
    /*
     * See if we need to add the ending sheet...
     */
 
-    if (printer != NULL &&
-        !(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
-        (attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_ZERO)) != NULL &&
-        attr->num_values > 1)
-    {
-     /*
-      * Yes...
-      */
-
-      cupsdLogMessage(CUPSD_LOG_INFO,
-                      "Adding end banner page \"%s\" to job %d.",
-                     attr->values[1].string.text, job->id);
-
-      kbytes = copy_banner(con, job, attr->values[1].string.text);
-
-      cupsdUpdateQuota(printer, job->username, 0, kbytes);
-    }
+    if (cupsdTimeoutJob(job))
+      return;
 
-    if (job->state->values[0].integer == IPP_JOB_STOPPED)
+    if (job->state_value == IPP_JOB_STOPPED)
+    {
       job->state->values[0].integer = IPP_JOB_PENDING;
-    else if (job->state->values[0].integer == IPP_JOB_HELD)
+      job->state_value              = IPP_JOB_PENDING;
+    }
+    else if (job->state_value == IPP_JOB_HELD)
     {
-      if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
+      if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
+                                   IPP_TAG_KEYWORD)) == NULL)
        attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
 
-      if (attr == NULL || strcmp(attr->values[0].string.text, "no-hold") == 0)
+      if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
+      {
        job->state->values[0].integer = IPP_JOB_PENDING;
+       job->state_value              = IPP_JOB_PENDING;
+      }
     }
 
-    cupsdSaveJob(job);
-
-   /*
-    * Start the job if possible...  Since cupsdCheckJobs() can cancel a
-    * job if it doesn't print, we need to re-find the job afterwards...
-    */
-
-    jobid = job->id;
-
-    cupsdCheckJobs();
+    job->dirty = 1;
+    cupsdMarkDirty(CUPSD_DIRTY_JOBS);
 
-    job = cupsdFindJob(jobid);
+    start_job = 1;
   }
   else
   {
-    if ((attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD)) == NULL)
+    if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
+                                 IPP_TAG_KEYWORD)) == NULL)
       attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
 
-    if (attr == NULL || strcmp(attr->values[0].string.text, "no-hold") == 0)
+    if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
     {
       job->state->values[0].integer = IPP_JOB_HELD;
-      job->hold_until               = time(NULL) + 60;
-      cupsdSaveJob(job);
+      job->state_value              = IPP_JOB_HELD;
+      job->hold_until               = time(NULL) + MultipleOperationTimeout;
+      job->dirty                    = 1;
+
+      cupsdMarkDirty(CUPSD_DIRTY_JOBS);
     }
+
+    start_job = 0;
   }
 
  /*
@@ -8097,10 +10020,17 @@ send_document(cupsd_client_t  *con,    /* I - Client connection */
   ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
 
   ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
-                job ? job->state->values[0].integer : IPP_JOB_CANCELLED);
+                job->state_value);
   add_job_state_reasons(con, job);
 
   con->response->request.status.status_code = IPP_OK;
+
+ /*
+  * Start the job if necessary...
+  */
+
+  if (start_job)
+    cupsdCheckJobs();
 }
 
 
@@ -8109,14 +10039,81 @@ send_document(cupsd_client_t  *con,    /* I - Client connection */
  */
 
 static void
-send_http_error(cupsd_client_t *con,   /* I - Client connection */
-                http_status_t  status) /* I - HTTP status code */
+send_http_error(
+    cupsd_client_t  *con,              /* I - Client connection */
+    http_status_t   status,            /* I - HTTP status code */
+    cupsd_printer_t *printer)          /* I - Printer, if any */
 {
-  cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s",
-                  ippOpString(con->request->request.op.operation_id),
-                 httpStatus(status));
+  ipp_attribute_t      *uri;           /* Request URI, if any */
+
+
+  if ((uri = ippFindAttribute(con->request, "printer-uri",
+                              IPP_TAG_URI)) == NULL)
+    uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
+
+  cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
+                  "Returning HTTP %s for %s (%s) from %s",
+                  httpStatus(status),
+                 ippOpString(con->request->request.op.operation_id),
+                 uri ? uri->values[0].string.text : "no URI",
+                 con->http.hostname);
+
+  if (printer)
+  {
+    int                auth_type;              /* Type of authentication required */
+
+
+    auth_type = CUPSD_AUTH_NONE;
+
+    if (status == HTTP_UNAUTHORIZED &&
+        printer->num_auth_info_required > 0 &&
+        !strcmp(printer->auth_info_required[0], "negotiate") &&
+       con->request &&
+       (con->request->request.op.operation_id == IPP_PRINT_JOB ||
+        con->request->request.op.operation_id == IPP_CREATE_JOB ||
+        con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
+    {
+     /*
+      * Creating and authenticating jobs requires Kerberos...
+      */
+
+      auth_type = CUPSD_AUTH_NEGOTIATE;
+    }
+    else
+    {
+     /*
+      * Use policy/location-defined authentication requirements...
+      */
+
+      char     resource[HTTP_MAX_URI]; /* Resource portion of URI */
+      cupsd_location_t *auth;          /* Pointer to authentication element */
+
+
+      if (printer->type & CUPS_PRINTER_CLASS)
+       snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
+      else
+       snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
+
+      if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
+         auth->type == CUPSD_AUTH_NONE)
+       auth = cupsdFindPolicyOp(printer->op_policy_ptr,
+                                con->request ?
+                                    con->request->request.op.operation_id :
+                                    IPP_PRINT_JOB);
+
+      if (auth)
+      {
+        if (auth->type == CUPSD_AUTH_DEFAULT)
+         auth_type = DefaultAuthType;
+       else
+         auth_type = auth->type;
+      }
+    }
 
-  cupsdSendError(con, status);
+    cupsdSendError(con, status, auth_type);
+  }
+  else
+    cupsdSendError(con, status, CUPSD_AUTH_NONE);
 
   ippDelete(con->response);
   con->response = NULL;
@@ -8131,35 +10128,27 @@ send_http_error(cupsd_client_t *con,   /* I - Client connection */
 
 static void
 send_ipp_status(cupsd_client_t *con,   /* I - Client connection */
-               ipp_status_t   status,  /* I - IPP status code */
-              const char     *message, /* I - Status message */
-              ...)                     /* I - Additional args as needed */
+                ipp_status_t   status, /* I - IPP status code */
+               const char     *message,/* I - Status message */
+               ...)                    /* I - Additional args as needed */
 {
   va_list      ap;                     /* Pointer to additional args */
   char         formatted[1024];        /* Formatted errror message */
 
 
-  if (message)
-  {
-    va_start(ap, message);
-    vsnprintf(formatted, sizeof(formatted),
-              _cupsLangString(con->language, message), ap);
-    va_end(ap);
+  va_start(ap, message);
+  vsnprintf(formatted, sizeof(formatted),
+            _cupsLangString(con->language, message), ap);
+  va_end(ap);
 
-    cupsdLogMessage(status >= IPP_BAD_REQUEST ? CUPSD_LOG_ERROR : CUPSD_LOG_INFO,
-                    "%s %s: %s",
-                   ippOpString(con->request->request.op.operation_id),
-                   ippErrorString(status), formatted);
-  }
-  else
-    cupsdLogMessage(status >= IPP_BAD_REQUEST ? CUPSD_LOG_ERROR : CUPSD_LOG_INFO,
-                    "%s %s",
-                   ippOpString(con->request->request.op.operation_id),
-                   ippErrorString(status));
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
+                 ippOpString(con->request->request.op.operation_id),
+                 ippErrorString(status), formatted);
 
   con->response->request.status.status_code = status;
 
-  if (ippFindAttribute(con->response, "attributes-charset", IPP_TAG_ZERO) == NULL)
+  if (ippFindAttribute(con->response, "attributes-charset",
+                       IPP_TAG_ZERO) == NULL)
     ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
                  "attributes-charset", NULL, DefaultCharset);
 
@@ -8168,9 +10157,8 @@ send_ipp_status(cupsd_client_t *con,     /* I - Client connection */
     ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
                  "attributes-natural-language", NULL, DefaultLanguage);
 
-  if (message)
-    ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
-                "status-message", NULL, formatted);
+  ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
+               "status-message", NULL, formatted);
 }
 
 
@@ -8183,18 +10171,9 @@ set_default(cupsd_client_t  *con,       /* I - Client connection */
             ipp_attribute_t *uri)      /* I - Printer URI */
 {
   http_status_t                status;         /* Policy status */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
-                       username[HTTP_MAX_URI],
-                                       /* Username portion of URI */
-                       host[HTTP_MAX_URI],
-                                       /* Host portion of URI */
-                       resource[HTTP_MAX_URI];
-                                       /* Resource portion of URI */
-  int                  port;           /* Port portion of URI */
-  const char           *name;          /* Printer name */
-  cupsd_printer_t      *printer;       /* Printer */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  cupsd_printer_t      *printer,       /* Printer */
+                       *oldprinter;    /* Old default printer */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
@@ -8204,11 +10183,7 @@ set_default(cupsd_client_t  *con,       /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -8225,7 +10200,7 @@ set_default(cupsd_client_t  *con,        /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, NULL);
     return;
   }
 
@@ -8233,16 +10208,22 @@ set_default(cupsd_client_t  *con,      /* I - Client connection */
   * Set it as the default...
   */
 
+  oldprinter     = DefaultPrinter;
   DefaultPrinter = printer;
 
-  cupsdSaveAllPrinters();
-  cupsdSaveAllClasses();
+  if (oldprinter)
+    cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
+                  "%s is no longer the default printer.", oldprinter->name);
 
-  cupsdWritePrintcap();
+  cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
+               "%s is now the default printer.", printer->name);
+
+  cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
+                 CUPSD_DIRTY_REMOTE | CUPSD_DIRTY_PRINTCAP);
 
   cupsdLogMessage(CUPSD_LOG_INFO,
-                  "Default destination set to \"%s\" by \"%s\".", name,
-                  con->username);
+                  "Default destination set to \"%s\" by \"%s\".",
+                 printer->name, get_username(con));
 
  /*
   * Everything was ok, so return OK status...
@@ -8264,7 +10245,7 @@ set_job_attrs(cupsd_client_t  *con,      /* I - Client connection */
                        *attr2;         /* Job attribute */
   int                  jobid;          /* Job ID */
   cupsd_job_t          *job;           /* Current job */
-  char                 method[HTTP_MAX_URI],
+  char                 scheme[HTTP_MAX_URI],
                                        /* Method portion of URI */
                        username[HTTP_MAX_URI],
                                        /* Username portion of URI */
@@ -8273,6 +10254,8 @@ set_job_attrs(cupsd_client_t  *con,      /* I - Client connection */
                        resource[HTTP_MAX_URI];
                                        /* Resource portion of URI */
   int                  port;           /* Port portion of URI */
+  int                  event;          /* Events? */
+  int                  check_jobs;     /* Check jobs? */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
@@ -8288,13 +10271,14 @@ set_job_attrs(cupsd_client_t  *con,    /* I - Client connection */
   * See if we have a job URI or a printer URI...
   */
 
-  if (strcmp(uri->name, "printer-uri") == 0)
+  if (!strcmp(uri->name, "printer-uri"))
   {
    /*
     * Got a printer URI; see if we also have a job-id attribute...
     */
 
-    if ((attr = ippFindAttribute(con->request, "job-id", IPP_TAG_INTEGER)) == NULL)
+    if ((attr = ippFindAttribute(con->request, "job-id",
+                                 IPP_TAG_INTEGER)) == NULL)
     {
       send_ipp_status(con, IPP_BAD_REQUEST,
                       _("Got a printer-uri attribute but no job-id!"));
@@ -8309,11 +10293,11 @@ set_job_attrs(cupsd_client_t  *con,    /* I - Client connection */
     * Got a job URI; parse it to get the job ID...
     */
 
-    httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                    username, sizeof(username), host, sizeof(host), &port,
-                   resource, sizeof(resource));
+    httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
+                    sizeof(scheme), username, sizeof(username), host,
+                   sizeof(host), &port, resource, sizeof(resource));
 
-    if (strncmp(resource, "/jobs/", 6) != 0)
+    if (strncmp(resource, "/jobs/", 6))
     {
      /*
       * Not a valid URI!
@@ -8346,7 +10330,7 @@ set_job_attrs(cupsd_client_t  *con,      /* I - Client connection */
   * See if the job has been completed...
   */
 
-  if (job->state->values[0].integer > IPP_JOB_STOPPED)
+  if (job->state_value > IPP_JOB_STOPPED)
   {
    /*
     * Return a "not-possible" error...
@@ -8363,10 +10347,7 @@ set_job_attrs(cupsd_client_t  *con,     /* I - Client connection */
 
   if (!validate_user(job, con, job->username, username, sizeof(username)))
   {
-    send_ipp_status(con, IPP_FORBIDDEN,
-                    _("You are not authorized to alter job id "
-                     "%d owned by \"%s\"!"),
-                    jobid, job->username);
+    send_http_error(con, HTTP_UNAUTHORIZED, cupsdFindDest(job->dest));
     return;
   }
 
@@ -8374,7 +10355,12 @@ set_job_attrs(cupsd_client_t  *con,     /* I - Client connection */
   * See what the user wants to change.
   */
 
-  for (attr = con->request->attrs; attr != NULL; attr = attr->next)
+  cupsdLoadJob(job);
+
+  check_jobs = 0;
+  event      = 0;
+
+  for (attr = con->request->attrs; attr; attr = attr->next)
   {
     if (attr->group_tag != IPP_TAG_JOB || !attr->name)
       continue;
@@ -8386,6 +10372,7 @@ set_job_attrs(cupsd_client_t  *con,      /* I - Client connection */
        !strcmp(attr->name, "job-detailed-status-messages") ||
        !strcmp(attr->name, "job-document-access-errors") ||
        !strcmp(attr->name, "job-id") ||
+       !strcmp(attr->name, "job-impressions-completed") ||
        !strcmp(attr->name, "job-k-octets") ||
         !strcmp(attr->name, "job-originating-host-name") ||
         !strcmp(attr->name, "job-originating-user-name") ||
@@ -8399,7 +10386,6 @@ set_job_attrs(cupsd_client_t  *con,      /* I - Client connection */
        !strcmp(attr->name, "number-of-intervening-jobs") ||
        !strcmp(attr->name, "output-device-assigned") ||
        !strncmp(attr->name, "date-time-at-", 13) ||
-       !strncmp(attr->name, "job-impressions", 15) ||
        !strncmp(attr->name, "job-k-octets", 12) ||
        !strncmp(attr->name, "job-media-sheets", 16) ||
        !strncmp(attr->name, "time-at-", 8))
@@ -8423,163 +10409,541 @@ set_job_attrs(cupsd_client_t  *con,  /* I - Client connection */
       * Change the job priority...
       */
 
-      if (attr->value_tag != IPP_TAG_INTEGER)
-      {
-       send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
+      if (attr->value_tag != IPP_TAG_INTEGER)
+      {
+       send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
+
+       if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
+          attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
+      }
+      else if (job->state_value >= IPP_JOB_PROCESSING)
+      {
+       send_ipp_status(con, IPP_NOT_POSSIBLE,
+                       _("Job is completed and cannot be changed."));
+       return;
+      }
+      else if (con->response->request.status.status_code == IPP_OK)
+      {
+        cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
+                   attr->values[0].integer);
+        cupsdSetJobPriority(job, attr->values[0].integer);
+
+       check_jobs = 1;
+        event      |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
+                     CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
+      }
+    }
+    else if (!strcmp(attr->name, "job-state"))
+    {
+     /*
+      * Change the job state...
+      */
+
+      if (attr->value_tag != IPP_TAG_ENUM)
+      {
+       send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
+
+       if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
+          attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
+      }
+      else
+      {
+        switch (attr->values[0].integer)
+       {
+         case IPP_JOB_PENDING :
+         case IPP_JOB_HELD :
+             if (job->state_value > IPP_JOB_HELD)
+             {
+               send_ipp_status(con, IPP_NOT_POSSIBLE,
+                               _("Job state cannot be changed."));
+               return;
+             }
+              else if (con->response->request.status.status_code == IPP_OK)
+             {
+               cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
+                           attr->values[0].integer);
+                cupsdSetJobState(job, attr->values[0].integer,
+                                CUPSD_JOB_DEFAULT,
+                                "Job state changed by \"%s\"", username);
+               check_jobs = 1;
+             }
+             break;
+
+         case IPP_JOB_PROCESSING :
+         case IPP_JOB_STOPPED :
+             if (job->state_value != attr->values[0].integer)
+             {
+               send_ipp_status(con, IPP_NOT_POSSIBLE,
+                               _("Job state cannot be changed."));
+               return;
+             }
+             break;
+
+         case IPP_JOB_CANCELED :
+         case IPP_JOB_ABORTED :
+         case IPP_JOB_COMPLETED :
+             if (job->state_value > IPP_JOB_PROCESSING)
+             {
+               send_ipp_status(con, IPP_NOT_POSSIBLE,
+                               _("Job state cannot be changed."));
+               return;
+             }
+              else if (con->response->request.status.status_code == IPP_OK)
+             {
+               cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
+                           attr->values[0].integer);
+                cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer,
+                                CUPSD_JOB_DEFAULT,
+                                "Job state changed by \"%s\"", username);
+                check_jobs = 1;
+             }
+             break;
+       }
+      }
+    }
+    else if (con->response->request.status.status_code != IPP_OK)
+      continue;
+    else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
+                                       IPP_TAG_ZERO)) != NULL)
+    {
+     /*
+      * Some other value; first free the old value...
+      */
+
+      if (job->attrs->prev)
+        job->attrs->prev->next = attr2->next;
+      else
+        job->attrs->attrs = attr2->next;
+
+      if (job->attrs->last == attr2)
+        job->attrs->last = job->attrs->prev;
+
+      _ippFreeAttr(attr2);
+
+     /*
+      * Then copy the attribute...
+      */
+
+      copy_attribute(job->attrs, attr, 0);
+
+     /*
+      * See if the job-name or job-hold-until is being changed.
+      */
+
+      if (!strcmp(attr->name, "job-hold-until"))
+      {
+        cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
+                   attr->values[0].string.text);
+        cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
+
+       if (!strcmp(attr->values[0].string.text, "no-hold"))
+       {
+         cupsdReleaseJob(job);
+          check_jobs = 1;
+       }
+       else
+         cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT,
+                          "Job held by \"%s\".", username);
+
+        event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
+      }
+    }
+    else if (attr->value_tag == IPP_TAG_DELETEATTR)
+    {
+     /*
+      * Delete the attribute...
+      */
+
+      if ((attr2 = ippFindAttribute(job->attrs, attr->name,
+                                    IPP_TAG_ZERO)) != NULL)
+      {
+        if (job->attrs->prev)
+         job->attrs->prev->next = attr2->next;
+       else
+         job->attrs->attrs = attr2->next;
+
+        if (attr2 == job->attrs->last)
+         job->attrs->last = job->attrs->prev;
+
+        _ippFreeAttr(attr2);
+
+        event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
+      }
+    }
+    else
+    {
+     /*
+      * Add new option by copying it...
+      */
+
+      copy_attribute(job->attrs, attr, 0);
+
+      event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
+    }
+  }
+
+ /*
+  * Save the job...
+  */
+
+  job->dirty = 1;
+  cupsdMarkDirty(CUPSD_DIRTY_JOBS);
+
+ /*
+  * Send events as needed...
+  */
+
+  if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
+    cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
+                  cupsdFindDest(job->dest), job,
+                  "Job priority changed by user.");
+
+  if (event & CUPSD_EVENT_JOB_STATE)
+    cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
+                  job->state_value == IPP_JOB_HELD ?
+                     "Job held by user." : "Job restarted by user.");
+
+  if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
+    cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
+                  "Job options changed by user.");
+
+ /*
+  * Start jobs if possible...
+  */
+
+  if (check_jobs)
+    cupsdCheckJobs();
+}
+
+
+/*
+ * 'set_printer_attrs()' - Set printer attributes.
+ */
+
+static void
+set_printer_attrs(cupsd_client_t  *con,        /* I - Client connection */
+                  ipp_attribute_t *uri)        /* I - Printer */
+{
+  http_status_t                status;         /* Policy status */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
+  cupsd_printer_t      *printer;       /* Printer/class */
+  ipp_attribute_t      *attr;          /* Printer attribute */
+  int                  changed = 0;    /* Was anything changed? */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
+                  con->http.fd, uri->values[0].string.text);
+
+ /*
+  * Is the destination valid?
+  */
+
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
+  {
+   /*
+    * Bad URI...
+    */
+
+    send_ipp_status(con, IPP_NOT_FOUND,
+                    _("The printer or class was not found."));
+    return;
+  }
+
+ /*
+  * Check policy...
+  */
+
+  if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
+  {
+    send_http_error(con, status, printer);
+    return;
+  }
+
+ /*
+  * Return a list of attributes that can be set via Set-Printer-Attributes.
+  */
+
+  if ((attr = ippFindAttribute(con->request, "printer-location",
+                               IPP_TAG_TEXT)) != NULL)
+  {
+    cupsdSetString(&printer->location, attr->values[0].string.text);
+    changed = 1;
+  }
+
+  if ((attr = ippFindAttribute(con->request, "printer-info",
+                               IPP_TAG_TEXT)) != NULL)
+  {
+    cupsdSetString(&printer->info, attr->values[0].string.text);
+    changed = 1;
+  }
+
+ /*
+  * Update the printer attributes and return...
+  */
+
+  if (changed)
+  {
+    cupsdSetPrinterAttrs(printer);
+    cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
+
+    cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
+                  "Printer \"%s\" description or location changed by \"%s\".",
+                 printer->name, get_username(con));
+
+    cupsdLogMessage(CUPSD_LOG_INFO,
+                    "Printer \"%s\" description or location changed by \"%s\".",
+                    printer->name, get_username(con));
+  }
+
+  con->response->request.status.status_code = IPP_OK;
+}
+
+
+/*
+ * 'set_printer_defaults()' - Set printer default options from a request.
+ */
+
+static void
+set_printer_defaults(
+    cupsd_client_t  *con,              /* I - Client connection */
+    cupsd_printer_t *printer)          /* I - Printer */
+{
+  int                  i;              /* Looping var */
+  ipp_attribute_t      *attr;          /* Current attribute */
+  int                  namelen;        /* Length of attribute name */
+  char                 name[256],      /* New attribute name */
+                       value[256];     /* String version of integer attrs */
+
+
+  for (attr = con->request->attrs; attr; attr = attr->next)
+  {
+   /*
+    * Skip non-printer attributes...
+    */
+
+    if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
+      continue;
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
+
+    if (!strcmp(attr->name, "job-sheets-default"))
+    {
+     /*
+      * Only allow keywords and names...
+      */
+
+      if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
+        continue;
+
+     /*
+      * Only allow job-sheets-default to be set when running without a
+      * system high classification level...
+      */
+
+      if (Classification)
+        continue;
+
+      cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
+
+      if (attr->num_values > 1)
+       cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
+      else
+       cupsdSetString(&printer->job_sheets[1], "none");
+    }
+    else if (!strcmp(attr->name, "requesting-user-name-allowed"))
+    {
+      cupsdFreePrinterUsers(printer);
+
+      printer->deny_users = 0;
 
-       if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
-          attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
-      }
-      else if (job->state->values[0].integer >= IPP_JOB_PROCESSING)
+      if (attr->value_tag == IPP_TAG_NAME &&
+          (attr->num_values > 1 ||
+          strcmp(attr->values[0].string.text, "all")))
       {
-       send_ipp_status(con, IPP_NOT_POSSIBLE,
-                       _("Job is completed and cannot be changed."));
-       return;
+       for (i = 0; i < attr->num_values; i ++)
+         cupsdAddPrinterUser(printer, attr->values[i].string.text);
       }
-      else if (con->response->request.status.status_code == IPP_OK)
-        cupsdSetJobPriority(job, attr->values[0].integer);
     }
-    else if (!strcmp(attr->name, "job-state"))
+    else if (!strcmp(attr->name, "requesting-user-name-denied"))
     {
-     /*
-      * Change the job state...
-      */
+      cupsdFreePrinterUsers(printer);
 
-      if (attr->value_tag != IPP_TAG_ENUM)
-      {
-       send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
+      printer->deny_users = 1;
 
-       if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
-          attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
-      }
-      else
+      if (attr->value_tag == IPP_TAG_NAME &&
+          (attr->num_values > 1 ||
+          strcmp(attr->values[0].string.text, "none")))
       {
-        switch (attr->values[0].integer)
-       {
-         case IPP_JOB_PENDING :
-         case IPP_JOB_HELD :
-             if (job->state->values[0].integer > IPP_JOB_HELD)
-             {
-               send_ipp_status(con, IPP_NOT_POSSIBLE,
-                               _("Job state cannot be changed."));
-               return;
-             }
-              else if (con->response->request.status.status_code == IPP_OK)
-               job->state->values[0].integer = attr->values[0].integer;
-             break;
-
-         case IPP_JOB_PROCESSING :
-         case IPP_JOB_STOPPED :
-             if (job->state->values[0].integer != attr->values[0].integer)
-             {
-               send_ipp_status(con, IPP_NOT_POSSIBLE,
-                               _("Job state cannot be changed."));
-               return;
-             }
-             break;
+       for (i = 0; i < attr->num_values; i ++)
+         cupsdAddPrinterUser(printer, attr->values[i].string.text);
+      }
+    }
+    else if (!strcmp(attr->name, "job-quota-period"))
+    {
+      if (attr->value_tag != IPP_TAG_INTEGER)
+        continue;
 
-         case IPP_JOB_CANCELLED :
-         case IPP_JOB_ABORTED :
-         case IPP_JOB_COMPLETED :
-             if (job->state->values[0].integer > IPP_JOB_PROCESSING)
-             {
-               send_ipp_status(con, IPP_NOT_POSSIBLE,
-                               _("Job state cannot be changed."));
-               return;
-             }
-              else if (con->response->request.status.status_code == IPP_OK)
-             {
-                cupsdCancelJob(job, 0);
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
+                     attr->values[0].integer);
+      cupsdFreeQuotas(printer);
 
-               if (JobHistory)
-               {
-                  job->state->values[0].integer = attr->values[0].integer;
-                 cupsdSaveJob(job);
-               }
-             }
-             break;
-       }
-      }
+      printer->quota_period = attr->values[0].integer;
     }
-    else if (con->response->request.status.status_code != IPP_OK)
-      continue;
-    else if ((attr2 = ippFindAttribute(job->attrs, attr->name, IPP_TAG_ZERO)) != NULL)
+    else if (!strcmp(attr->name, "job-k-limit"))
     {
-     /*
-      * Some other value; first free the old value...
-      */
+      if (attr->value_tag != IPP_TAG_INTEGER)
+        continue;
 
-      if (job->attrs->prev)
-        job->attrs->prev->next = attr2->next;
-      else
-        job->attrs->attrs = attr2->next;
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
+                     attr->values[0].integer);
+      cupsdFreeQuotas(printer);
 
-      if (job->attrs->last == attr2)
-        job->attrs->last = job->attrs->prev;
+      printer->k_limit = attr->values[0].integer;
+    }
+    else if (!strcmp(attr->name, "job-page-limit"))
+    {
+      if (attr->value_tag != IPP_TAG_INTEGER)
+        continue;
 
-      _ipp_free_attr(attr2);
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
+                     attr->values[0].integer);
+      cupsdFreeQuotas(printer);
 
-     /*
-      * Then copy the attribute...
-      */
+      printer->page_limit = attr->values[0].integer;
+    }
+    else if (!strcmp(attr->name, "printer-op-policy"))
+    {
+      cupsd_policy_t *p;               /* Policy */
 
-      copy_attribute(job->attrs, attr, 0);
 
-     /*
-      * See if the job-name or job-hold-until is being changed.
-      */
+      if (attr->value_tag != IPP_TAG_NAME)
+        continue;
 
-      if (!strcmp(attr->name, "job-hold-until"))
+      if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
       {
-        cupsdSetJobHoldUntil(job, attr->values[0].string.text);
-
-       if (!strcmp(attr->values[0].string.text, "no-hold"))
-         cupsdReleaseJob(job);
-       else
-         cupsdHoldJob(job);
+       cupsdLogMessage(CUPSD_LOG_DEBUG,
+                       "Setting printer-op-policy to \"%s\"...",
+                       attr->values[0].string.text);
+       cupsdSetString(&printer->op_policy, attr->values[0].string.text);
+       printer->op_policy_ptr = p;
+      }
+      else
+      {
+       send_ipp_status(con, IPP_NOT_POSSIBLE,
+                       _("Unknown printer-op-policy \"%s\"."),
+                       attr->values[0].string.text);
+       return;
       }
     }
-    else if (attr->value_tag == IPP_TAG_DELETEATTR)
+    else if (!strcmp(attr->name, "printer-error-policy"))
     {
-     /*
-      * Delete the attribute...
-      */
+      if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
+        continue;
 
-      if ((attr2 = ippFindAttribute(job->attrs, attr->name,
-                                    IPP_TAG_ZERO)) != NULL)
+      if (strcmp(attr->values[0].string.text, "retry-current-job") &&
+          ((printer->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_CLASS)) ||
+          (strcmp(attr->values[0].string.text, "abort-job") &&
+           strcmp(attr->values[0].string.text, "retry-job") &&
+           strcmp(attr->values[0].string.text, "stop-printer"))))
       {
-        if (job->attrs->prev)
-         job->attrs->prev->next = attr2->next;
-       else
-         job->attrs->attrs = attr2->next;
-
-        if (attr2 == job->attrs->last)
-         job->attrs->last = job->attrs->prev;
-
-        _ipp_free_attr(attr2);
+       send_ipp_status(con, IPP_NOT_POSSIBLE,
+                       _("Unknown printer-error-policy \"%s\"."),
+                       attr->values[0].string.text);
+       return;
       }
+
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                      "Setting printer-error-policy to \"%s\"...",
+                      attr->values[0].string.text);
+      cupsdSetString(&printer->error_policy, attr->values[0].string.text);
     }
-    else
+
+   /*
+    * Skip any other non-default attributes...
+    */
+
+    namelen = strlen(attr->name);
+    if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
+        namelen > (sizeof(name) - 1) || attr->num_values != 1)
+      continue;
+
+   /*
+    * OK, anything else must be a user-defined default...
+    */
+
+    strlcpy(name, attr->name, sizeof(name));
+    name[namelen - 8] = '\0';          /* Strip "-default" */
+
+    switch (attr->value_tag)
     {
-     /*
-      * Add new option by copying it...
-      */
+      case IPP_TAG_DELETEATTR :
+          printer->num_options = cupsRemoveOption(name,
+                                                 printer->num_options,
+                                                 &(printer->options));
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "Deleting %s", attr->name);
+          break;
 
-      copy_attribute(job->attrs, attr, 0);
-    }
-  }
+      case IPP_TAG_NAME :
+      case IPP_TAG_KEYWORD :
+      case IPP_TAG_URI :
+          printer->num_options = cupsAddOption(name,
+                                              attr->values[0].string.text,
+                                              printer->num_options,
+                                              &(printer->options));
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "Setting %s to \"%s\"...", attr->name,
+                         attr->values[0].string.text);
+          break;
 
- /*
-  * Save the job...
-  */
+      case IPP_TAG_BOOLEAN :
+          printer->num_options = cupsAddOption(name,
+                                              attr->values[0].boolean ?
+                                                  "true" : "false",
+                                              printer->num_options,
+                                              &(printer->options));
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "Setting %s to %s...", attr->name,
+                         attr->values[0].boolean ? "true" : "false");
+          break;
 
-  cupsdSaveJob(job);
+      case IPP_TAG_INTEGER :
+      case IPP_TAG_ENUM :
+          sprintf(value, "%d", attr->values[0].integer);
+          printer->num_options = cupsAddOption(name, value,
+                                              printer->num_options,
+                                              &(printer->options));
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "Setting %s to %s...", attr->name, value);
+          break;
 
- /*
-  * Start jobs if possible...
-  */
+      case IPP_TAG_RANGE :
+          sprintf(value, "%d-%d", attr->values[0].range.lower,
+                 attr->values[0].range.upper);
+          printer->num_options = cupsAddOption(name, value,
+                                              printer->num_options,
+                                              &(printer->options));
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "Setting %s to %s...", attr->name, value);
+          break;
 
-  cupsdCheckJobs();
+      case IPP_TAG_RESOLUTION :
+          sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
+                 attr->values[0].resolution.yres,
+                 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
+                     "dpi" : "dpc");
+          printer->num_options = cupsAddOption(name, value,
+                                              printer->num_options,
+                                              &(printer->options));
+          cupsdLogMessage(CUPSD_LOG_DEBUG,
+                         "Setting %s to %s...", attr->name, value);
+          break;
+
+      default :
+          /* Do nothing for other values */
+         break;
+    }
+  }
 }
 
 
@@ -8592,17 +10956,7 @@ start_printer(cupsd_client_t  *con,     /* I - Client connection */
               ipp_attribute_t *uri)    /* I - Printer URI */
 {
   http_status_t                status;         /* Policy status */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
-                       username[HTTP_MAX_URI],
-                                       /* Username portion of URI */
-                       host[HTTP_MAX_URI],
-                                       /* Host portion of URI */
-                       resource[HTTP_MAX_URI];
-                                       /* Resource portion of URI */
-  int                  port;           /* Port portion of URI */
-  const char           *name;          /* Printer name */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
   cupsd_printer_t      *printer;       /* Printer data */
 
 
@@ -8613,11 +10967,7 @@ start_printer(cupsd_client_t  *con,     /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -8634,7 +10984,7 @@ start_printer(cupsd_client_t  *con,      /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
@@ -8647,10 +10997,11 @@ start_printer(cupsd_client_t  *con,    /* I - Client connection */
   cupsdStartPrinter(printer, 1);
 
   if (dtype & CUPS_PRINTER_CLASS)
-    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".", name,
-                    con->username);
-    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".", name,
-                    con->username);
+    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
+                    printer->name, get_username(con));
+  else
+    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
+                    printer->name, get_username(con));
 
   cupsdCheckJobs();
 
@@ -8671,17 +11022,7 @@ stop_printer(cupsd_client_t  *con,      /* I - Client connection */
              ipp_attribute_t *uri)     /* I - Printer URI */
 {
   http_status_t                status;         /* Policy status */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
-                       username[HTTP_MAX_URI],
-                                       /* Username portion of URI */
-                       host[HTTP_MAX_URI],
-                                       /* Host portion of URI */
-                       resource[HTTP_MAX_URI];
-                                       /* Resource portion of URI */
-  int                  port;           /* Port portion of URI */
-  const char           *name;          /* Printer name */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
   cupsd_printer_t      *printer;       /* Printer data */
   ipp_attribute_t      *attr;          /* printer-state-message attribute */
 
@@ -8693,11 +11034,7 @@ stop_printer(cupsd_client_t  *con,      /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if ((name = cupsdValidateDest(host, resource, &dtype, &printer)) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -8714,7 +11051,7 @@ stop_printer(cupsd_client_t  *con,       /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
@@ -8734,11 +11071,11 @@ stop_printer(cupsd_client_t  *con,     /* I - Client connection */
   cupsdStopPrinter(printer, 1);
 
   if (dtype & CUPS_PRINTER_CLASS)
-    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".", name,
-                    con->username);
+    cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
+                    printer->name, get_username(con));
   else
-    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".", name,
-                    con->username);
+    cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
+                    printer->name, get_username(con));
 
  /*
   * Everything was ok, so return OK status...
@@ -8748,6 +11085,101 @@ stop_printer(cupsd_client_t  *con,     /* I - Client connection */
 }
 
 
+/*
+ * 'url_encode_attr()' - URL-encode a string attribute.
+ */
+
+static void
+url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
+                char            *buffer,/* I - String buffer */
+               int             bufsize)/* I - Size of buffer */
+{
+  int  i;                              /* Looping var */
+  char *bufptr,                        /* Pointer into buffer */
+       *bufend;                        /* End of buffer */
+
+
+  strlcpy(buffer, attr->name, bufsize);
+  bufptr = buffer + strlen(buffer);
+  bufend = buffer + bufsize - 1;
+
+  for (i = 0; i < attr->num_values; i ++)
+  {
+    if (bufptr >= bufend)
+      break;
+
+    if (i)
+      *bufptr++ = ',';
+    else
+      *bufptr++ = '=';
+
+    if (bufptr >= bufend)
+      break;
+
+    *bufptr++ = '\'';
+
+    bufptr = url_encode_string(attr->values[i].string.text,
+                               bufptr, bufend - bufptr + 1);
+
+    if (bufptr >= bufend)
+      break;
+
+    *bufptr++ = '\'';
+  }
+
+  *bufptr = '\0';
+}
+
+
+/*
+ * 'url_encode_string()' - URL-encode a string.
+ */
+
+static char *                          /* O - End of string */
+url_encode_string(const char *s,       /* I - String */
+                  char       *buffer,  /* I - String buffer */
+                 int        bufsize)   /* I - Size of buffer */
+{
+  char *bufptr,                        /* Pointer into buffer */
+       *bufend;                        /* End of buffer */
+  static const char *hex = "0123456789ABCDEF";
+                                       /* Hex digits */
+
+
+  bufptr = buffer;
+  bufend = buffer + bufsize - 1;
+
+  while (*s && bufptr < bufend)
+  {
+    if (*s == ' ' || *s == '%')
+    {
+      if (bufptr >= (bufend - 2))
+       break;
+
+      *bufptr++ = '%';
+      *bufptr++ = hex[(*s >> 4) & 15];
+      *bufptr++ = hex[*s & 15];
+
+      s ++;
+    }
+    else if (*s == '\'' || *s == '\\')
+    {
+      if (bufptr >= (bufend - 1))
+       break;
+
+      *bufptr++ = '\\';
+      *bufptr++ = *s++;
+    }
+    else
+      *bufptr++ = *s++;
+  }
+
+  *bufptr = '\0';
+
+  return (bufptr);
+}
+
+
 /*
  * 'user_allowed()' - See if a user is allowed to print to a queue.
  */
@@ -8758,6 +11190,8 @@ user_allowed(cupsd_printer_t *p, /* I - Printer or class */
 {
   int          i;                      /* Looping var */
   struct passwd        *pw;                    /* User password data */
+  char         baseuser[256],          /* Base username */
+               *baseptr;               /* Pointer to "@" in base username */
 
 
   if (p->num_users == 0)
@@ -8766,6 +11200,20 @@ user_allowed(cupsd_printer_t *p,        /* I - Printer or class */
   if (!strcmp(username, "root"))
     return (1);
 
+  if (strchr(username, '@'))
+  {
+   /*
+    * Strip @REALM for username check...
+    */
+
+    strlcpy(baseuser, username, sizeof(baseuser));
+
+    if ((baseptr = strchr(baseuser, '@')) != NULL)
+      *baseptr = '\0';
+
+    username = baseuser;
+  }
+
   pw = getpwnam(username);
   endpwent();
 
@@ -8780,6 +11228,15 @@ user_allowed(cupsd_printer_t *p,        /* I - Printer or class */
       if (cupsdCheckGroup(username, pw, p->users[i] + 1))
         break;
     }
+    else if (p->users[i][0] == '#')
+    {
+     /*
+      * Check UUID...
+      */
+
+      if (cupsdCheckGroup(username, pw, p->users[i]))
+        break;
+    }
     else if (!strcasecmp(username, p->users[i]))
       break;
   }
@@ -8799,16 +11256,7 @@ validate_job(cupsd_client_t  *con,      /* I - Client connection */
   http_status_t                status;         /* Policy status */
   ipp_attribute_t      *attr;          /* Current attribute */
   ipp_attribute_t      *format;        /* Document-format attribute */
-  cups_ptype_t         dtype;          /* Destination type (printer or class) */
-  char                 method[HTTP_MAX_URI],
-                                       /* Method portion of URI */
-                       username[HTTP_MAX_URI],
-                                       /* Username portion of URI */
-                       host[HTTP_MAX_URI],
-                                       /* Host portion of URI */
-                       resource[HTTP_MAX_URI];
-                                       /* Resource portion of URI */
-  int                  port;           /* Port portion of URI */
+  cups_ptype_t         dtype;          /* Destination type (printer/class) */
   char                 super[MIME_MAX_SUPER],
                                        /* Supertype of file */
                        type[MIME_MAX_TYPE];
@@ -8824,8 +11272,9 @@ validate_job(cupsd_client_t  *con,       /* I - Client connection */
   * doesn't support compression yet...
   */
 
-  if ((attr = ippFindAttribute(con->request, "compression", IPP_TAG_KEYWORD)) != NULL &&
-      strcmp(attr->values[0].string.text, "none") == 0)
+  if ((attr = ippFindAttribute(con->request, "compression",
+                               IPP_TAG_KEYWORD)) != NULL &&
+      !strcmp(attr->values[0].string.text, "none"))
   {
     send_ipp_status(con, IPP_ATTRIBUTES,
                     _("Unsupported compression attribute %s!"),
@@ -8842,16 +11291,16 @@ validate_job(cupsd_client_t  *con,     /* I - Client connection */
   if ((format = ippFindAttribute(con->request, "document-format",
                                  IPP_TAG_MIMETYPE)) != NULL)
   {
-    if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super, type) != 2)
+    if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
+               super, type) != 2)
     {
       send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
                      format->values[0].string.text);
       return;
     }
 
-    if ((strcmp(super, "application") != 0 ||
-        strcmp(type, "octet-stream") != 0) &&
-       mimeType(MimeDatabase, super, type) == NULL)
+    if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
+       !mimeType(MimeDatabase, super, type))
     {
       cupsdLogMessage(CUPSD_LOG_INFO,
                       "Hint: Do you have the raw file printing rules enabled?");
@@ -8868,11 +11317,7 @@ validate_job(cupsd_client_t  *con,      /* I - Client connection */
   * Is the destination valid?
   */
 
-  httpSeparateURI(uri->values[0].string.text, method, sizeof(method),
-                  username, sizeof(username), host, sizeof(host), &port,
-                 resource, sizeof(resource));
-
-  if (cupsdValidateDest(host, resource, &dtype, &printer) == NULL)
+  if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
   {
    /*
     * Bad URI...
@@ -8889,7 +11334,7 @@ validate_job(cupsd_client_t  *con,       /* I - Client connection */
 
   if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
   {
-    send_http_error(con, status);
+    send_http_error(con, status, printer);
     return;
   }
 
@@ -8905,7 +11350,7 @@ validate_job(cupsd_client_t  *con,       /* I - Client connection */
  * 'validate_name()' - Make sure the printer name only contains valid chars.
  */
 
-static int                     /* O - 0 if name is no good, 1 if name is good */
+static int                     /* O - 0 if name is no good, 1 if good */
 validate_name(const char *name)        /* I - Name to check */
 {
   const char   *ptr;           /* Pointer into name */
@@ -8916,7 +11361,7 @@ validate_name(const char *name)  /* I - Name to check */
   */
 
   for (ptr = name; *ptr; ptr ++)
-    if ((*ptr >= 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
+    if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
       return (0);
 
  /*
@@ -8938,15 +11383,14 @@ validate_user(cupsd_job_t    *job,     /* I - Job */
               char           *username,        /* O - Authenticated username */
              int            userlen)   /* I - Length of username */
 {
-  ipp_attribute_t      *attr;          /* requesting-user-name attribute */
   cupsd_printer_t      *printer;       /* Printer for job */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
                   "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
                  "userlen=%d)",
-                 job ? job->id : 0, con->http.fd, owner ? owner : "(null)",
-                 username, userlen);
+                 job->id, con ? con->http.fd : 0,
+                 owner ? owner : "(null)", username, userlen);
 
  /*
   * Validate input...
@@ -8959,22 +11403,13 @@ validate_user(cupsd_job_t    *job,     /* I - Job */
   * Get the best authenticated username that is available.
   */
 
-  if (con->username[0])
-    strlcpy(username, con->username, userlen);
-  else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
-                                    IPP_TAG_NAME)) != NULL)
-    strlcpy(username, attr->values[0].string.text, userlen);
-  else
-    strlcpy(username, "anonymous", userlen);
+  strlcpy(username, get_username(con), userlen);
 
  /*
   * Check the username against the owner...
   */
 
-  if (job->dtype & CUPS_PRINTER_CLASS)
-    printer = cupsdFindClass(job->dest);
-  else
-    printer = cupsdFindPrinter(job->dest);
+  printer = cupsdFindDest(job->dest);
 
   return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
                            con, owner) == HTTP_OK);
@@ -8982,5 +11417,5 @@ validate_user(cupsd_job_t    *job,       /* I - Job */
 
 
 /*
- * End of "$Id: ipp.c 4906 2006-01-10 20:53:28Z mike $".
+ * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".
  */