2 * IPP routines for the CUPS scheduler.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * This file contains Kerberos support code, copyright 2006 by
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * missing or damaged, see the license at "http://www.cups.org/".
18 * Include necessary headers...
22 #include <cups/ppd-private.h>
25 /*# include <ApplicationServices/ApplicationServices.h>
26 extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
27 # include <CoreFoundation/CoreFoundation.h>*/
28 # ifdef HAVE_MEMBERSHIP_H
29 # include <membership.h>
30 # endif /* HAVE_MEMBERSHIP_H */
31 # ifdef HAVE_MEMBERSHIPPRIV_H
32 # include <membershipPriv.h>
34 extern int mbr_user_name_to_uuid(const char* name, uuid_t uu);
35 extern int mbr_group_name_to_uuid(const char* name, uuid_t uu);
36 extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
37 # endif /* HAVE_MEMBERSHIPPRIV_H */
38 #endif /* __APPLE__ */
45 static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
46 static void add_class(cupsd_client_t *con, ipp_attribute_t *uri);
47 static int add_file(cupsd_client_t *con, cupsd_job_t *job,
48 mime_type_t *filetype, int compression);
49 static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer,
50 mime_type_t *filetype);
51 static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job);
52 static void add_job_uuid(cupsd_job_t *job);
53 static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri);
54 static void add_printer_state_reasons(cupsd_client_t *con,
56 static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p);
57 static void apply_printer_defaults(cupsd_printer_t *printer,
59 static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri);
60 static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
61 static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
62 static void cancel_subscription(cupsd_client_t *con, int id);
63 static int check_rss_recipient(const char *recipient);
64 static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
65 static void close_job(cupsd_client_t *con, ipp_attribute_t *uri);
66 static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
67 ipp_tag_t group, int quickcopy,
68 cups_array_t *exclude);
69 static int copy_banner(cupsd_client_t *con, cupsd_job_t *job,
71 static int copy_file(const char *from, const char *to, mode_t mode);
72 static int copy_model(cupsd_client_t *con, const char *from,
74 static void copy_job_attrs(cupsd_client_t *con,
76 cups_array_t *ra, cups_array_t *exclude);
77 static void copy_printer_attrs(cupsd_client_t *con,
78 cupsd_printer_t *printer,
80 static void copy_subscription_attrs(cupsd_client_t *con,
81 cupsd_subscription_t *sub,
83 cups_array_t *exclude);
84 static void create_job(cupsd_client_t *con, ipp_attribute_t *uri);
85 static void create_local_printer(cupsd_client_t *con);
86 static cups_array_t *create_requested_array(ipp_t *request);
87 static void create_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri);
88 static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri);
89 static void get_default(cupsd_client_t *con);
90 static void get_devices(cupsd_client_t *con);
91 static void get_document(cupsd_client_t *con, ipp_attribute_t *uri);
92 static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
93 static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
94 static void get_notifications(cupsd_client_t *con);
95 static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri);
96 static void get_ppds(cupsd_client_t *con);
97 static void get_printers(cupsd_client_t *con, int type);
98 static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
99 static void get_printer_supported(cupsd_client_t *con, ipp_attribute_t *uri);
100 static void get_subscription_attrs(cupsd_client_t *con, int sub_id);
101 static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri);
102 static const char *get_username(cupsd_client_t *con);
103 static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri);
104 static void hold_new_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
105 static void move_job(cupsd_client_t *con, ipp_attribute_t *uri);
106 static int ppd_parse_line(const char *line, char *option, int olen,
107 char *choice, int clen);
108 static void print_job(cupsd_client_t *con, ipp_attribute_t *uri);
109 static void read_job_ticket(cupsd_client_t *con);
110 static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
111 static void release_held_new_jobs(cupsd_client_t *con,
112 ipp_attribute_t *uri);
113 static void release_job(cupsd_client_t *con, ipp_attribute_t *uri);
114 static void renew_subscription(cupsd_client_t *con, int sub_id);
115 static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri);
116 static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job,
117 ipp_attribute_t *auth_info);
118 static void send_document(cupsd_client_t *con, ipp_attribute_t *uri);
119 static void send_http_error(cupsd_client_t *con, http_status_t status,
120 cupsd_printer_t *printer);
121 static void send_ipp_status(cupsd_client_t *con, ipp_status_t status,
122 const char *message, ...)
123 __attribute__((__format__(__printf__, 3, 4)));
124 static void set_default(cupsd_client_t *con, ipp_attribute_t *uri);
125 static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
126 static void set_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
127 static int set_printer_defaults(cupsd_client_t *con, cupsd_printer_t *printer);
128 static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri);
129 static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri);
130 static void url_encode_attr(ipp_attribute_t *attr, char *buffer, size_t bufsize);
131 static char *url_encode_string(const char *s, char *buffer, size_t bufsize);
132 static int user_allowed(cupsd_printer_t *p, const char *username);
133 static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri);
134 static int validate_name(const char *name);
135 static int validate_user(cupsd_job_t *job, cupsd_client_t *con, const char *owner, char *username, size_t userlen);
139 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
142 int /* O - 1 on success, 0 on failure */
143 cupsdProcessIPPRequest(
144 cupsd_client_t *con) /* I - Client connection */
146 ipp_tag_t group; /* Current group tag */
147 ipp_attribute_t *attr; /* Current attribute */
148 ipp_attribute_t *charset; /* Character set attribute */
149 ipp_attribute_t *language; /* Language attribute */
150 ipp_attribute_t *uri = NULL; /* Printer or job URI attribute */
151 ipp_attribute_t *username; /* requesting-user-name attr */
152 int sub_id; /* Subscription ID */
155 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest(%p[%d]): operation_id=%04x(%s)", con, con->number, con->request->request.op.operation_id, ippOpString(con->request->request.op.operation_id));
157 if (LogLevel >= CUPSD_LOG_DEBUG2)
159 for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(con->request); attr; attr = ippNextAttribute(con->request))
161 const char *name; /* Attribute name */
162 char value[1024]; /* Attribute value */
164 if (group != ippGetGroupTag(attr))
166 group = ippGetGroupTag(attr);
167 if (group != IPP_TAG_ZERO)
168 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s", ippTagString(group));
171 if ((name = ippGetName(attr)) == NULL)
174 ippAttributeString(attr, value, sizeof(value));
176 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdProcessIPPRequest: %s %s%s '%s'", name, ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr)), value);
181 * First build an empty response message for this request...
184 con->response = ippNew();
186 con->response->request.status.version[0] =
187 con->request->request.op.version[0];
188 con->response->request.status.version[1] =
189 con->request->request.op.version[1];
190 con->response->request.status.request_id =
191 con->request->request.op.request_id;
194 * Then validate the request header and required attributes...
197 if (con->request->request.any.version[0] != 1 &&
198 con->request->request.any.version[0] != 2)
201 * Return an error, since we only support IPP 1.x and 2.x.
204 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
205 "%04X %s Bad request version number %d.%d",
206 IPP_VERSION_NOT_SUPPORTED, con->http->hostname,
207 con->request->request.any.version[0],
208 con->request->request.any.version[1]);
210 send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED,
211 _("Bad request version number %d.%d."),
212 con->request->request.any.version[0],
213 con->request->request.any.version[1]);
215 else if (con->request->request.any.request_id < 1)
218 * Return an error, since request IDs must be between 1 and 2^31-1
221 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
222 "%04X %s Bad request ID %d",
223 IPP_BAD_REQUEST, con->http->hostname,
224 con->request->request.any.request_id);
226 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad request ID %d."),
227 con->request->request.any.request_id);
229 else if (!con->request->attrs)
231 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
232 "%04X %s No attributes in request",
233 IPP_BAD_REQUEST, con->http->hostname);
235 send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request."));
240 * Make sure that the attributes are provided in the correct order and
241 * don't repeat groups...
244 for (attr = con->request->attrs, group = attr->group_tag;
247 if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO)
250 * Out of order; return an error...
253 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
254 "%04X %s Attribute groups are out of order",
255 IPP_BAD_REQUEST, con->http->hostname);
257 send_ipp_status(con, IPP_BAD_REQUEST,
258 _("Attribute groups are out of order (%x < %x)."),
259 attr->group_tag, group);
263 group = attr->group_tag;
268 * Then make sure that the first three attributes are:
271 * attributes-natural-language
272 * printer-uri/job-uri
275 attr = con->request->attrs;
276 if (attr && attr->name &&
277 !strcmp(attr->name, "attributes-charset") &&
278 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
286 if (attr && attr->name &&
287 !strcmp(attr->name, "attributes-natural-language") &&
288 (attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
293 * Reset language for this request if different from Accept-Language.
296 if (!con->language ||
297 strcmp(attr->values[0].string.text, con->language->language))
299 cupsLangFree(con->language);
300 con->language = cupsLangGet(attr->values[0].string.text);
306 if ((attr = ippFindAttribute(con->request, "printer-uri",
307 IPP_TAG_URI)) != NULL)
309 else if ((attr = ippFindAttribute(con->request, "job-uri",
310 IPP_TAG_URI)) != NULL)
312 else if (con->request->request.op.operation_id == CUPS_GET_PPD)
313 uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
318 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
319 "attributes-charset", NULL,
320 charset->values[0].string.text);
322 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
323 "attributes-charset", NULL, "utf-8");
326 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
327 "attributes-natural-language", NULL,
328 language->values[0].string.text);
330 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
331 "attributes-natural-language", NULL, DefaultLanguage);
334 _cups_strcasecmp(charset->values[0].string.text, "us-ascii") &&
335 _cups_strcasecmp(charset->values[0].string.text, "utf-8"))
338 * Bad character set...
341 cupsdLogMessage(CUPSD_LOG_ERROR, "Unsupported character set \"%s\"",
342 charset->values[0].string.text);
343 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
344 "%04X %s Unsupported attributes-charset value \"%s\"",
345 IPP_CHARSET, con->http->hostname,
346 charset->values[0].string.text);
347 send_ipp_status(con, IPP_BAD_REQUEST,
348 _("Unsupported character set \"%s\"."),
349 charset->values[0].string.text);
351 else if (!charset || !language ||
353 con->request->request.op.operation_id != CUPS_GET_DEFAULT &&
354 con->request->request.op.operation_id != CUPS_GET_PRINTERS &&
355 con->request->request.op.operation_id != CUPS_GET_CLASSES &&
356 con->request->request.op.operation_id != CUPS_GET_DEVICES &&
357 con->request->request.op.operation_id != CUPS_GET_PPDS))
360 * Return an error, since attributes-charset,
361 * attributes-natural-language, and printer-uri/job-uri are required
362 * for all operations.
367 cupsdLogMessage(CUPSD_LOG_ERROR,
368 "Missing attributes-charset attribute");
370 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
371 "%04X %s Missing attributes-charset attribute",
372 IPP_BAD_REQUEST, con->http->hostname);
377 cupsdLogMessage(CUPSD_LOG_ERROR,
378 "Missing attributes-natural-language attribute");
380 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
381 "%04X %s Missing attributes-natural-language attribute",
382 IPP_BAD_REQUEST, con->http->hostname);
387 cupsdLogMessage(CUPSD_LOG_ERROR,
388 "Missing printer-uri, job-uri, or ppd-name "
391 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
392 "%04X %s Missing printer-uri, job-uri, or ppd-name "
393 "attribute", IPP_BAD_REQUEST, con->http->hostname);
396 cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
398 for (attr = con->request->attrs; attr; attr = attr->next)
399 cupsdLogMessage(CUPSD_LOG_DEBUG,
400 "attr \"%s\": group_tag = %x, value_tag = %x",
401 attr->name ? attr->name : "(null)", attr->group_tag,
404 cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes...");
406 send_ipp_status(con, IPP_BAD_REQUEST,
407 _("Missing required attributes."));
412 * OK, all the checks pass so far; make sure requesting-user-name is
413 * not "root" from a remote host...
416 if ((username = ippFindAttribute(con->request, "requesting-user-name",
417 IPP_TAG_NAME)) != NULL)
420 * Check for root user...
423 if (!strcmp(username->values[0].string.text, "root") &&
424 _cups_strcasecmp(con->http->hostname, "localhost") &&
425 strcmp(con->username, "root"))
428 * Remote unauthenticated user masquerading as local root...
431 ippSetString(con->request, &username, 0, RemoteRoot);
435 if ((attr = ippFindAttribute(con->request, "notify-subscription-id",
436 IPP_TAG_INTEGER)) != NULL)
437 sub_id = attr->values[0].integer;
442 * Then try processing the operation...
446 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s",
447 ippOpString(con->request->request.op.operation_id),
448 uri->values[0].string.text);
450 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s",
451 ippOpString(con->request->request.op.operation_id));
453 switch (con->request->request.op.operation_id)
455 case IPP_OP_PRINT_JOB :
459 case IPP_OP_VALIDATE_JOB :
460 validate_job(con, uri);
463 case IPP_OP_CREATE_JOB :
464 create_job(con, uri);
467 case IPP_OP_SEND_DOCUMENT :
468 send_document(con, uri);
471 case IPP_OP_CANCEL_JOB :
472 cancel_job(con, uri);
475 case IPP_OP_GET_JOB_ATTRIBUTES :
476 get_job_attrs(con, uri);
479 case IPP_OP_GET_JOBS :
483 case IPP_OP_GET_PRINTER_ATTRIBUTES :
484 get_printer_attrs(con, uri);
487 case IPP_OP_GET_PRINTER_SUPPORTED_VALUES :
488 get_printer_supported(con, uri);
491 case IPP_OP_HOLD_JOB :
495 case IPP_OP_RELEASE_JOB :
496 release_job(con, uri);
499 case IPP_OP_RESTART_JOB :
500 restart_job(con, uri);
503 case IPP_OP_PAUSE_PRINTER :
504 stop_printer(con, uri);
507 case IPP_OP_RESUME_PRINTER :
508 start_printer(con, uri);
511 case IPP_OP_PURGE_JOBS :
512 case IPP_OP_CANCEL_JOBS :
513 case IPP_OP_CANCEL_MY_JOBS :
514 cancel_all_jobs(con, uri);
517 case IPP_OP_SET_JOB_ATTRIBUTES :
518 set_job_attrs(con, uri);
521 case IPP_OP_SET_PRINTER_ATTRIBUTES :
522 set_printer_attrs(con, uri);
525 case IPP_OP_HOLD_NEW_JOBS :
526 hold_new_jobs(con, uri);
529 case IPP_OP_RELEASE_HELD_NEW_JOBS :
530 release_held_new_jobs(con, uri);
533 case IPP_OP_CLOSE_JOB :
537 case IPP_OP_CUPS_GET_DEFAULT :
541 case IPP_OP_CUPS_GET_PRINTERS :
542 get_printers(con, 0);
545 case IPP_OP_CUPS_GET_CLASSES :
546 get_printers(con, CUPS_PRINTER_CLASS);
549 case IPP_OP_CUPS_ADD_MODIFY_PRINTER :
550 add_printer(con, uri);
553 case IPP_OP_CUPS_DELETE_PRINTER :
554 delete_printer(con, uri);
557 case IPP_OP_CUPS_ADD_MODIFY_CLASS :
561 case IPP_OP_CUPS_DELETE_CLASS :
562 delete_printer(con, uri);
565 case IPP_OP_CUPS_ACCEPT_JOBS :
566 case IPP_OP_ENABLE_PRINTER :
567 accept_jobs(con, uri);
570 case IPP_OP_CUPS_REJECT_JOBS :
571 case IPP_OP_DISABLE_PRINTER :
572 reject_jobs(con, uri);
575 case IPP_OP_CUPS_SET_DEFAULT :
576 set_default(con, uri);
579 case IPP_OP_CUPS_GET_DEVICES :
583 case IPP_OP_CUPS_GET_DOCUMENT :
584 get_document(con, uri);
587 case IPP_OP_CUPS_GET_PPD :
591 case IPP_OP_CUPS_GET_PPDS :
595 case IPP_OP_CUPS_MOVE_JOB :
599 case IPP_OP_CUPS_AUTHENTICATE_JOB :
600 authenticate_job(con, uri);
603 case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS :
604 case IPP_OP_CREATE_JOB_SUBSCRIPTIONS :
605 create_subscriptions(con, uri);
608 case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES :
609 get_subscription_attrs(con, sub_id);
612 case IPP_OP_GET_SUBSCRIPTIONS :
613 get_subscriptions(con, uri);
616 case IPP_OP_RENEW_SUBSCRIPTION :
617 renew_subscription(con, sub_id);
620 case IPP_OP_CANCEL_SUBSCRIPTION :
621 cancel_subscription(con, sub_id);
624 case IPP_OP_GET_NOTIFICATIONS :
625 get_notifications(con);
628 case IPP_OP_CUPS_CREATE_LOCAL_PRINTER :
629 create_local_printer(con);
633 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
634 "%04X %s Operation %04X (%s) not supported",
635 IPP_OPERATION_NOT_SUPPORTED, con->http->hostname,
636 con->request->request.op.operation_id,
637 ippOpString(con->request->request.op.operation_id));
639 send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED,
640 _("%s not supported."),
642 con->request->request.op.operation_id));
652 * Sending data from the scheduler...
655 cupsdLogMessage(con->response->request.status.status_code
656 >= IPP_BAD_REQUEST &&
657 con->response->request.status.status_code
658 != IPP_NOT_FOUND ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
659 "[Client %d] Returning IPP %s for %s (%s) from %s",
661 ippErrorString(con->response->request.status.status_code),
662 ippOpString(con->request->request.op.operation_id),
663 uri ? uri->values[0].string.text : "no URI",
664 con->http->hostname);
666 httpClearFields(con->http);
668 #ifdef CUPSD_USE_CHUNKING
670 * Because older versions of CUPS (1.1.17 and older) and some IPP
671 * clients do not implement chunking properly, we cannot use
672 * chunking by default. This may become the default in future
673 * CUPS releases, or we might add a configuration directive for
677 if (con->http->version == HTTP_1_1)
679 cupsdLogMessage(CUPSD_LOG_DEBUG,
680 "[Client %d] Transfer-Encoding: chunked",
683 cupsdSetLength(con->http, 0);
686 #endif /* CUPSD_USE_CHUNKING */
688 size_t length; /* Length of response */
691 length = ippLength(con->response);
693 if (con->file >= 0 && !con->pipe_pid)
695 struct stat fileinfo; /* File information */
697 if (!fstat(con->file, &fileinfo))
698 length += (size_t)fileinfo.st_size;
701 cupsdLogMessage(CUPSD_LOG_DEBUG,
702 "[Client %d] Content-Length: " CUPS_LLFMT,
703 con->number, CUPS_LLCAST length);
704 httpSetLength(con->http, length);
707 if (cupsdSendHeader(con, HTTP_OK, "application/ipp", CUPSD_AUTH_NONE))
710 * Tell the caller the response header was sent successfully...
713 cupsdAddSelect(httpGetFd(con->http), (cupsd_selfunc_t)cupsdReadClient,
714 (cupsd_selfunc_t)cupsdWriteClient, con);
721 * Tell the caller the response header could not be sent...
730 * Sending data from a subprocess like cups-deviced; tell the caller
731 * everything is A-OK so far...
740 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
743 int /* O - 0 on success, -1 on error */
744 cupsdTimeoutJob(cupsd_job_t *job) /* I - Job to timeout */
746 cupsd_printer_t *printer; /* Destination printer or class */
747 ipp_attribute_t *attr; /* job-sheets attribute */
748 int kbytes; /* Kilobytes in banner */
751 job->pending_timeout = 0;
754 * See if we need to add the ending sheet...
757 if (!cupsdLoadJob(job))
760 printer = cupsdFindDest(job->dest);
761 attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
763 if (printer && !(printer->type & CUPS_PRINTER_REMOTE) &&
764 attr && attr->num_values > 1)
770 cupsdLogJob(job, CUPSD_LOG_INFO, "Adding end banner page \"%s\".",
771 attr->values[1].string.text);
773 if ((kbytes = copy_banner(NULL, job, attr->values[1].string.text)) < 0)
776 cupsdUpdateQuota(printer, job->username, 0, kbytes);
784 * 'accept_jobs()' - Accept print jobs to a printer.
788 accept_jobs(cupsd_client_t *con, /* I - Client connection */
789 ipp_attribute_t *uri) /* I - Printer or class URI */
791 http_status_t status; /* Policy status */
792 cups_ptype_t dtype; /* Destination type (printer/class) */
793 cupsd_printer_t *printer; /* Printer data */
796 cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con,
797 con->number, uri->values[0].string.text);
800 * Is the destination valid?
803 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
809 send_ipp_status(con, IPP_NOT_FOUND,
810 _("The printer or class does not exist."));
818 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
820 send_http_error(con, status, printer);
825 * Accept jobs sent to the printer...
828 printer->accepting = 1;
829 printer->state_message[0] = '\0';
831 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
832 "Now accepting jobs.");
834 if (dtype & CUPS_PRINTER_CLASS)
836 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
838 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").",
839 printer->name, get_username(con));
843 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
845 cupsdLogMessage(CUPSD_LOG_INFO,
846 "Printer \"%s\" now accepting jobs (\"%s\").",
847 printer->name, get_username(con));
851 * Everything was ok, so return OK status...
854 con->response->request.status.status_code = IPP_OK;
859 * 'add_class()' - Add a class to the system.
863 add_class(cupsd_client_t *con, /* I - Client connection */
864 ipp_attribute_t *uri) /* I - URI of class */
866 http_status_t status; /* Policy status */
867 int i; /* Looping var */
868 char scheme[HTTP_MAX_URI], /* Method portion of URI */
869 username[HTTP_MAX_URI], /* Username portion of URI */
870 host[HTTP_MAX_URI], /* Host portion of URI */
871 resource[HTTP_MAX_URI]; /* Resource portion of URI */
872 int port; /* Port portion of URI */
873 cupsd_printer_t *pclass, /* Class */
874 *member; /* Member printer/class */
875 cups_ptype_t dtype; /* Destination type */
876 ipp_attribute_t *attr; /* Printer attribute */
877 int modify; /* Non-zero if we just modified */
878 int need_restart_job; /* Need to restart job? */
881 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con,
882 con->number, uri->values[0].string.text);
885 * Do we have a valid URI?
888 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
889 sizeof(scheme), username, sizeof(username), host,
890 sizeof(host), &port, resource, sizeof(resource));
893 if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9)
896 * No, return an error...
899 send_ipp_status(con, IPP_BAD_REQUEST,
900 _("The printer-uri must be of the form "
901 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
906 * Do we have a valid printer name?
909 if (!validate_name(resource + 9))
912 * No, return an error...
915 send_ipp_status(con, IPP_BAD_REQUEST,
916 _("The printer-uri \"%s\" contains invalid characters."),
917 uri->values[0].string.text);
922 * See if the class already exists; if not, create a new class...
925 if ((pclass = cupsdFindClass(resource + 9)) == NULL)
928 * Class doesn't exist; see if we have a printer of the same name...
931 if ((pclass = cupsdFindPrinter(resource + 9)) != NULL)
934 * Yes, return an error...
937 send_ipp_status(con, IPP_NOT_POSSIBLE,
938 _("A printer named \"%s\" already exists."),
944 * No, check the default policy and then add the class...
947 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
949 send_http_error(con, status, NULL);
953 pclass = cupsdAddClass(resource + 9);
956 else if ((status = cupsdCheckPolicy(pclass->op_policy_ptr, con,
959 send_http_error(con, status, pclass);
966 * Look for attributes and copy them over as needed...
969 need_restart_job = 0;
971 if ((attr = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT)) != NULL)
972 cupsdSetString(&pclass->location, attr->values[0].string.text);
974 if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4))
975 cupsdSetString(&pclass->geo_location, attr->values[0].string.text);
977 if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL)
978 cupsdSetString(&pclass->organization, attr->values[0].string.text);
980 if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL)
981 cupsdSetString(&pclass->organizational_unit, attr->values[0].string.text);
983 if ((attr = ippFindAttribute(con->request, "printer-info",
984 IPP_TAG_TEXT)) != NULL)
985 cupsdSetString(&pclass->info, attr->values[0].string.text);
987 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
988 IPP_TAG_BOOLEAN)) != NULL &&
989 attr->values[0].boolean != pclass->accepting)
991 cupsdLogMessage(CUPSD_LOG_INFO,
992 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
993 pclass->name, attr->values[0].boolean, pclass->accepting);
995 pclass->accepting = attr->values[0].boolean;
997 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, pclass, NULL, "%s accepting jobs.",
998 pclass->accepting ? "Now" : "No longer");
1001 if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL)
1003 if (pclass->type & CUPS_PRINTER_REMOTE)
1006 * Cannot re-share remote printers.
1009 send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
1011 cupsdDeletePrinter(pclass, 0);
1016 if (pclass->shared && !ippGetBoolean(attr, 0))
1017 cupsdDeregisterPrinter(pclass, 1);
1019 cupsdLogMessage(CUPSD_LOG_INFO,
1020 "Setting %s printer-is-shared to %d (was %d.)",
1021 pclass->name, attr->values[0].boolean, pclass->shared);
1023 pclass->shared = ippGetBoolean(attr, 0);
1026 if ((attr = ippFindAttribute(con->request, "printer-state",
1027 IPP_TAG_ENUM)) != NULL)
1029 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
1030 attr->values[0].integer != IPP_PRINTER_STOPPED)
1032 send_ipp_status(con, IPP_BAD_REQUEST,
1033 _("Attempt to set %s printer-state to bad value %d."),
1034 pclass->name, attr->values[0].integer);
1036 cupsdDeletePrinter(pclass, 0);
1041 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
1042 pclass->name, attr->values[0].integer, pclass->state);
1044 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1045 cupsdStopPrinter(pclass, 0);
1048 cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0);
1049 need_restart_job = 1;
1052 if ((attr = ippFindAttribute(con->request, "printer-state-message",
1053 IPP_TAG_TEXT)) != NULL)
1055 strlcpy(pclass->state_message, attr->values[0].string.text,
1056 sizeof(pclass->state_message));
1058 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, pclass, NULL, "%s",
1059 pclass->state_message);
1061 if ((attr = ippFindAttribute(con->request, "member-uris",
1062 IPP_TAG_URI)) != NULL)
1065 * Clear the printer array as needed...
1068 need_restart_job = 1;
1070 if (pclass->num_printers > 0)
1072 free(pclass->printers);
1073 pclass->num_printers = 0;
1077 * Add each printer or class that is listed...
1080 for (i = 0; i < attr->num_values; i ++)
1083 * Search for the printer or class URI...
1086 if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member))
1092 send_ipp_status(con, IPP_NOT_FOUND,
1093 _("The printer or class does not exist."));
1095 cupsdDeletePrinter(pclass, 0);
1099 else if (dtype & CUPS_PRINTER_CLASS)
1101 send_ipp_status(con, IPP_BAD_REQUEST,
1102 _("Nested classes are not allowed."));
1104 cupsdDeletePrinter(pclass, 0);
1110 * Add it to the class...
1113 cupsdAddPrinterToClass(pclass, member);
1117 if (!set_printer_defaults(con, pclass))
1120 cupsdDeletePrinter(pclass, 0);
1125 if ((attr = ippFindAttribute(con->request, "auth-info-required",
1126 IPP_TAG_KEYWORD)) != NULL)
1127 cupsdSetAuthInfoRequired(pclass, NULL, attr);
1129 pclass->config_time = time(NULL);
1132 * Update the printer class attributes and return...
1135 cupsdSetPrinterAttrs(pclass);
1136 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
1138 if (need_restart_job && pclass->job)
1141 * Reset the current job to a "pending" status...
1144 cupsdSetJobState(pclass->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
1145 "Job restarted because the class was modified.");
1148 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
1152 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED,
1153 pclass, NULL, "Class \"%s\" modified by \"%s\".",
1154 pclass->name, get_username(con));
1156 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".",
1157 pclass->name, get_username(con));
1161 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED,
1162 pclass, NULL, "New class \"%s\" added by \"%s\".",
1163 pclass->name, get_username(con));
1165 cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".",
1166 pclass->name, get_username(con));
1169 con->response->request.status.status_code = IPP_OK;
1174 * 'add_file()' - Add a file to a job.
1177 static int /* O - 0 on success, -1 on error */
1178 add_file(cupsd_client_t *con, /* I - Connection to client */
1179 cupsd_job_t *job, /* I - Job to add to */
1180 mime_type_t *filetype, /* I - Type of file */
1181 int compression) /* I - Compression */
1183 mime_type_t **filetypes; /* New filetypes array... */
1184 int *compressions; /* New compressions array... */
1187 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1188 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1189 "compression=%d)", con, con ? con->number : -1, job->id,
1190 filetype->super, filetype->type, compression);
1193 * Add the file to the job...
1196 if (job->num_files == 0)
1198 compressions = (int *)malloc(sizeof(int));
1199 filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *));
1203 compressions = (int *)realloc(job->compressions,
1204 (size_t)(job->num_files + 1) * sizeof(int));
1205 filetypes = (mime_type_t **)realloc(job->filetypes,
1206 (size_t)(job->num_files + 1) *
1207 sizeof(mime_type_t *));
1211 job->compressions = compressions;
1214 job->filetypes = filetypes;
1216 if (!compressions || !filetypes)
1218 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
1219 "Job aborted because the scheduler ran out of memory.");
1222 send_ipp_status(con, IPP_INTERNAL_ERROR,
1223 _("Unable to allocate memory for file types."));
1228 job->compressions[job->num_files] = compression;
1229 job->filetypes[job->num_files] = filetype;
1234 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1241 * 'add_job()' - Add a job to a print queue.
1244 static cupsd_job_t * /* O - Job object */
1245 add_job(cupsd_client_t *con, /* I - Client connection */
1246 cupsd_printer_t *printer, /* I - Destination printer */
1247 mime_type_t *filetype) /* I - First print file type, if any */
1249 http_status_t status; /* Policy status */
1250 ipp_attribute_t *attr, /* Current attribute */
1251 *auth_info; /* auth-info attribute */
1252 const char *mandatory; /* Current mandatory job attribute */
1253 const char *val; /* Default option value */
1254 int priority; /* Job priority */
1255 cupsd_job_t *job; /* Current job */
1256 char job_uri[HTTP_MAX_URI]; /* Job URI */
1257 int kbytes; /* Size of print file */
1258 int i; /* Looping var */
1259 int lowerpagerange; /* Page range bound */
1260 int exact; /* Did we have an exact match? */
1261 ipp_attribute_t *media_col, /* media-col attribute */
1262 *media_margin; /* media-*-margin attribute */
1263 ipp_t *unsup_col; /* media-col in unsupported response */
1264 static const char * const readonly[] =/* List of read-only attributes */
1266 "date-time-at-completed",
1267 "date-time-at-creation",
1268 "date-time-at-processing",
1269 "job-detailed-status-messages",
1270 "job-document-access-errors",
1272 "job-impressions-completed",
1273 "job-k-octets-completed",
1274 "job-media-sheets-completed",
1275 "job-pages-completed",
1276 "job-printer-up-time",
1279 "job-state-message",
1280 "job-state-reasons",
1282 "number-of-documents",
1283 "number-of-intervening-jobs",
1284 "output-device-assigned",
1285 "time-at-completed",
1287 "time-at-processing"
1291 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1292 con, con->number, printer, printer->name,
1293 filetype, filetype ? filetype->super : "none",
1294 filetype ? filetype->type : "none");
1297 * Check remote printing to non-shared printer...
1300 if (!printer->shared &&
1301 _cups_strcasecmp(con->http->hostname, "localhost") &&
1302 _cups_strcasecmp(con->http->hostname, ServerName))
1304 send_ipp_status(con, IPP_NOT_AUTHORIZED,
1305 _("The printer or class is not shared."));
1313 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
1315 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
1317 send_http_error(con, status, printer);
1320 else if (printer->num_auth_info_required == 1 &&
1321 !strcmp(printer->auth_info_required[0], "negotiate") &&
1324 send_http_error(con, HTTP_UNAUTHORIZED, printer);
1328 else if (auth_info && !con->http->tls &&
1329 !httpAddrLocalhost(con->http->hostaddr))
1332 * Require encryption of auth-info over non-local connections...
1335 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
1338 #endif /* HAVE_SSL */
1341 * See if the printer is accepting jobs...
1344 if (!printer->accepting)
1346 send_ipp_status(con, IPP_NOT_ACCEPTING,
1347 _("Destination \"%s\" is not accepting jobs."),
1353 * Validate job template attributes; for now just document-format,
1354 * copies, job-sheets, number-up, page-ranges, mandatory attributes, and
1358 for (i = 0; i < (int)(sizeof(readonly) / sizeof(readonly[0])); i ++)
1360 if ((attr = ippFindAttribute(con->request, readonly[i], IPP_TAG_ZERO)) != NULL)
1362 ippDeleteAttribute(con->request, attr);
1364 if (StrictConformance)
1366 send_ipp_status(con, IPP_BAD_REQUEST, _("The '%s' Job Status attribute cannot be supplied in a job creation request."), readonly[i]);
1370 cupsdLogMessage(CUPSD_LOG_INFO, "Unexpected '%s' Job Status attribute in a job creation request.", readonly[i]);
1376 for (mandatory = (char *)cupsArrayFirst(printer->pc->mandatory);
1378 mandatory = (char *)cupsArrayNext(printer->pc->mandatory))
1380 if (!ippFindAttribute(con->request, mandatory, IPP_TAG_ZERO))
1383 * Missing a required attribute...
1386 send_ipp_status(con, IPP_CONFLICT,
1387 _("The \"%s\" attribute is required for print jobs."),
1394 if (filetype && printer->filetypes &&
1395 !cupsArrayFind(printer->filetypes, filetype))
1397 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
1398 /* MIME media type string */
1401 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
1404 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
1405 _("Unsupported format \"%s\"."), mimetype);
1407 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
1408 "document-format", NULL, mimetype);
1413 if ((attr = ippFindAttribute(con->request, "copies",
1414 IPP_TAG_INTEGER)) != NULL)
1416 if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
1418 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
1419 attr->values[0].integer);
1420 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
1421 "copies", attr->values[0].integer);
1426 if ((attr = ippFindAttribute(con->request, "job-sheets",
1427 IPP_TAG_ZERO)) != NULL)
1429 if (attr->value_tag != IPP_TAG_KEYWORD &&
1430 attr->value_tag != IPP_TAG_NAME)
1432 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value type."));
1436 if (attr->num_values > 2)
1438 send_ipp_status(con, IPP_BAD_REQUEST,
1439 _("Too many job-sheets values (%d > 2)."),
1444 for (i = 0; i < attr->num_values; i ++)
1445 if (strcmp(attr->values[i].string.text, "none") &&
1446 !cupsdFindBanner(attr->values[i].string.text))
1448 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-sheets value \"%s\"."),
1449 attr->values[i].string.text);
1454 if ((attr = ippFindAttribute(con->request, "number-up",
1455 IPP_TAG_INTEGER)) != NULL)
1457 if (attr->values[0].integer != 1 &&
1458 attr->values[0].integer != 2 &&
1459 attr->values[0].integer != 4 &&
1460 attr->values[0].integer != 6 &&
1461 attr->values[0].integer != 9 &&
1462 attr->values[0].integer != 16)
1464 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."),
1465 attr->values[0].integer);
1466 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
1467 "number-up", attr->values[0].integer);
1472 if ((attr = ippFindAttribute(con->request, "page-ranges",
1473 IPP_TAG_RANGE)) != NULL)
1475 for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
1477 if (attr->values[i].range.lower < lowerpagerange ||
1478 attr->values[i].range.lower > attr->values[i].range.upper)
1480 send_ipp_status(con, IPP_BAD_REQUEST,
1481 _("Bad page-ranges values %d-%d."),
1482 attr->values[i].range.lower,
1483 attr->values[i].range.upper);
1487 lowerpagerange = attr->values[i].range.upper + 1;
1492 * Do media selection as needed...
1495 if (!ippFindAttribute(con->request, "PageRegion", IPP_TAG_ZERO) &&
1496 !ippFindAttribute(con->request, "PageSize", IPP_TAG_ZERO) &&
1497 _ppdCacheGetPageSize(printer->pc, con->request, NULL, &exact))
1500 (media_col = ippFindAttribute(con->request, "media-col",
1501 IPP_TAG_BEGIN_COLLECTION)) != NULL)
1503 send_ipp_status(con, IPP_OK_SUBST, _("Unsupported margins."));
1505 unsup_col = ippNew();
1506 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1507 "media-bottom-margin",
1508 IPP_TAG_INTEGER)) != NULL)
1509 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1510 "media-bottom-margin", media_margin->values[0].integer);
1512 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1513 "media-left-margin",
1514 IPP_TAG_INTEGER)) != NULL)
1515 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1516 "media-left-margin", media_margin->values[0].integer);
1518 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1519 "media-right-margin",
1520 IPP_TAG_INTEGER)) != NULL)
1521 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1522 "media-right-margin", media_margin->values[0].integer);
1524 if ((media_margin = ippFindAttribute(media_col->values[0].collection,
1526 IPP_TAG_INTEGER)) != NULL)
1527 ippAddInteger(unsup_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
1528 "media-top-margin", media_margin->values[0].integer);
1530 ippAddCollection(con->response, IPP_TAG_UNSUPPORTED_GROUP, "media-col",
1532 ippDelete(unsup_col);
1537 * Make sure we aren't over our limit...
1540 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1543 if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
1545 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Too many active jobs."));
1549 if ((i = check_quotas(con, printer)) < 0)
1551 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
1556 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
1561 * Create the job and set things up...
1564 if ((attr = ippFindAttribute(con->request, "job-priority",
1565 IPP_TAG_INTEGER)) != NULL)
1566 priority = attr->values[0].integer;
1569 if ((val = cupsGetOption("job-priority", printer->num_options,
1570 printer->options)) != NULL)
1571 priority = atoi(val);
1575 ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
1579 if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) == NULL)
1580 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL, "Untitled");
1581 else if ((attr->value_tag != IPP_TAG_NAME &&
1582 attr->value_tag != IPP_TAG_NAMELANG) ||
1583 attr->num_values != 1)
1585 send_ipp_status(con, IPP_ATTRIBUTES,
1586 _("Bad job-name value: Wrong type or count."));
1587 if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
1588 attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
1591 else if (!ippValidateAttribute(attr))
1593 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad job-name value: %s"),
1594 cupsLastErrorString());
1595 if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
1596 attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
1600 attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
1602 if (attr && !ippValidateAttribute(attr))
1604 send_ipp_status(con, IPP_ATTRIBUTES, _("Bad requesting-user-name value: %s"), cupsLastErrorString());
1605 if ((attr = ippCopyAttribute(con->response, attr, 0)) != NULL)
1606 attr->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
1610 if ((job = cupsdAddJob(priority, printer->name)) == NULL)
1612 send_ipp_status(con, IPP_INTERNAL_ERROR,
1613 _("Unable to add job for destination \"%s\"."),
1618 job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
1619 job->attrs = con->request;
1621 con->request = ippNewRequest(job->attrs->request.op.operation_id);
1623 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
1626 apply_printer_defaults(printer, job);
1628 if (con->username[0])
1630 cupsdSetString(&job->username, con->username);
1633 ippSetString(job->attrs, &attr, 0, con->username);
1637 cupsdLogMessage(CUPSD_LOG_DEBUG,
1638 "add_job: requesting-user-name=\"%s\"",
1639 attr->values[0].string.text);
1641 cupsdSetString(&job->username, attr->values[0].string.text);
1644 cupsdSetString(&job->username, "anonymous");
1647 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1648 "job-originating-user-name", NULL, job->username);
1651 ippSetGroupTag(job->attrs, &attr, IPP_TAG_JOB);
1652 ippSetName(job->attrs, &attr, "job-originating-user-name");
1655 if (con->username[0] || auth_info)
1657 save_auth_info(con, job, auth_info);
1660 * Remove the auth-info attribute from the attribute data...
1664 ippDeleteAttribute(job->attrs, auth_info);
1667 if ((attr = ippFindAttribute(con->request, "job-name", IPP_TAG_NAME)) != NULL)
1668 cupsdSetString(&(job->name), attr->values[0].string.text);
1670 if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
1671 IPP_TAG_ZERO)) != NULL)
1674 * Request contains a job-originating-host-name attribute; validate it...
1677 if (attr->value_tag != IPP_TAG_NAME ||
1678 attr->num_values != 1 ||
1679 strcmp(con->http->hostname, "localhost"))
1682 * Can't override the value if we aren't connected via localhost.
1683 * Also, we can only have 1 value and it must be a name value.
1686 ippDeleteAttribute(job->attrs, attr);
1687 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-originating-host-name", NULL, con->http->hostname);
1690 ippSetGroupTag(job->attrs, &attr, IPP_TAG_JOB);
1695 * No job-originating-host-name attribute, so use the hostname from
1699 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
1700 "job-originating-host-name", NULL, con->http->hostname);
1703 ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "date-time-at-completed");
1704 ippAddDate(job->attrs, IPP_TAG_JOB, "date-time-at-creation", ippTimeToDate(time(NULL)));
1705 ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "date-time-at-processing");
1706 ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "time-at-completed");
1707 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", time(NULL));
1708 ippAddOutOfBand(job->attrs, IPP_TAG_JOB, IPP_TAG_NOVALUE, "time-at-processing");
1711 * Add remaining job attributes...
1714 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1715 job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
1716 "job-state", IPP_JOB_STOPPED);
1717 job->state_value = (ipp_jstate_t)job->state->values[0].integer;
1718 job->reasons = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1719 "job-state-reasons", NULL, "job-incoming");
1720 job->impressions = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-impressions-completed", 0);
1721 job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
1722 "job-media-sheets-completed", 0);
1723 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
1726 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
1727 attr->values[0].integer = 0;
1729 ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", 0);
1731 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
1732 IPP_TAG_KEYWORD)) == NULL)
1733 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
1736 if ((val = cupsGetOption("job-hold-until", printer->num_options,
1737 printer->options)) == NULL)
1740 attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
1741 "job-hold-until", NULL, val);
1744 if (printer->holding_new_jobs)
1747 * Hold all new jobs on this printer...
1750 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
1751 cupsdSetJobHoldUntil(job, ippGetString(attr, 0, NULL), 0);
1753 cupsdSetJobHoldUntil(job, "indefinite", 0);
1755 job->state->values[0].integer = IPP_JOB_HELD;
1756 job->state_value = IPP_JOB_HELD;
1758 ippSetString(job->attrs, &job->reasons, 0, "job-held-on-create");
1760 else if (attr && strcmp(attr->values[0].string.text, "no-hold"))
1763 * Hold job until specified time...
1766 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
1768 job->state->values[0].integer = IPP_JOB_HELD;
1769 job->state_value = IPP_JOB_HELD;
1771 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
1773 else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB)
1775 job->hold_until = time(NULL) + MultipleOperationTimeout;
1776 job->state->values[0].integer = IPP_JOB_HELD;
1777 job->state_value = IPP_JOB_HELD;
1781 job->state->values[0].integer = IPP_JOB_PENDING;
1782 job->state_value = IPP_JOB_PENDING;
1784 ippSetString(job->attrs, &job->reasons, 0, "none");
1787 if (!(printer->type & CUPS_PRINTER_REMOTE) || Classification)
1790 * Add job sheets options...
1793 if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1794 IPP_TAG_ZERO)) == NULL)
1796 cupsdLogMessage(CUPSD_LOG_DEBUG,
1797 "Adding default job-sheets values \"%s,%s\"...",
1798 printer->job_sheets[0], printer->job_sheets[1]);
1800 attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
1802 ippSetString(job->attrs, &attr, 0, printer->job_sheets[0]);
1803 ippSetString(job->attrs, &attr, 1, printer->job_sheets[1]);
1806 job->job_sheets = attr;
1809 * Enforce classification level if set...
1814 cupsdLogMessage(CUPSD_LOG_INFO,
1815 "Classification=\"%s\", ClassifyOverride=%d",
1816 Classification ? Classification : "(null)",
1819 if (ClassifyOverride)
1821 if (!strcmp(attr->values[0].string.text, "none") &&
1822 (attr->num_values == 1 ||
1823 !strcmp(attr->values[1].string.text, "none")))
1826 * Force the leading banner to have the classification on it...
1829 ippSetString(job->attrs, &attr, 0, Classification);
1831 cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
1832 "job-sheets=\"%s,none\", "
1833 "job-originating-user-name=\"%s\"",
1834 Classification, job->username);
1836 else if (attr->num_values == 2 &&
1837 strcmp(attr->values[0].string.text,
1838 attr->values[1].string.text) &&
1839 strcmp(attr->values[0].string.text, "none") &&
1840 strcmp(attr->values[1].string.text, "none"))
1843 * Can't put two different security markings on the same document!
1846 ippSetString(job->attrs, &attr, 1, attr->values[0].string.text);
1848 cupsdLogJob(job, CUPSD_LOG_NOTICE, "CLASSIFICATION FORCED "
1849 "job-sheets=\"%s,%s\", "
1850 "job-originating-user-name=\"%s\"",
1851 attr->values[0].string.text,
1852 attr->values[1].string.text, job->username);
1854 else if (strcmp(attr->values[0].string.text, Classification) &&
1855 strcmp(attr->values[0].string.text, "none") &&
1856 (attr->num_values == 1 ||
1857 (strcmp(attr->values[1].string.text, Classification) &&
1858 strcmp(attr->values[1].string.text, "none"))))
1860 if (attr->num_values == 1)
1861 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1862 "CLASSIFICATION OVERRIDDEN "
1863 "job-sheets=\"%s\", "
1864 "job-originating-user-name=\"%s\"",
1865 attr->values[0].string.text, job->username);
1867 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1868 "CLASSIFICATION OVERRIDDEN "
1869 "job-sheets=\"%s,%s\",fffff "
1870 "job-originating-user-name=\"%s\"",
1871 attr->values[0].string.text,
1872 attr->values[1].string.text, job->username);
1875 else if (strcmp(attr->values[0].string.text, Classification) &&
1876 (attr->num_values == 1 ||
1877 strcmp(attr->values[1].string.text, Classification)))
1880 * Force the banner to have the classification on it...
1883 if (attr->num_values > 1 &&
1884 !strcmp(attr->values[0].string.text, attr->values[1].string.text))
1886 ippSetString(job->attrs, &attr, 0, Classification);
1887 ippSetString(job->attrs, &attr, 1, Classification);
1891 if (attr->num_values == 1 ||
1892 strcmp(attr->values[0].string.text, "none"))
1893 ippSetString(job->attrs, &attr, 0, Classification);
1895 if (attr->num_values > 1 &&
1896 strcmp(attr->values[1].string.text, "none"))
1897 ippSetString(job->attrs, &attr, 1, Classification);
1900 if (attr->num_values > 1)
1901 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1902 "CLASSIFICATION FORCED "
1903 "job-sheets=\"%s,%s\", "
1904 "job-originating-user-name=\"%s\"",
1905 attr->values[0].string.text,
1906 attr->values[1].string.text, job->username);
1908 cupsdLogJob(job, CUPSD_LOG_NOTICE,
1909 "CLASSIFICATION FORCED "
1910 "job-sheets=\"%s\", "
1911 "job-originating-user-name=\"%s\"",
1912 Classification, job->username);
1917 * See if we need to add the starting sheet...
1920 if (!(printer->type & CUPS_PRINTER_REMOTE))
1922 cupsdLogJob(job, CUPSD_LOG_INFO, "Adding start banner page \"%s\".",
1923 attr->values[0].string.text);
1925 if ((kbytes = copy_banner(con, job, attr->values[0].string.text)) < 0)
1927 cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_PURGE,
1928 "Aborting job because the start banner could not be "
1933 cupsdUpdateQuota(printer, job->username, 0, kbytes);
1936 else if ((attr = ippFindAttribute(job->attrs, "job-sheets",
1937 IPP_TAG_ZERO)) != NULL)
1938 job->job_sheets = attr;
1941 * Fill in the response info...
1944 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
1945 con->clientname, con->clientport, "/jobs/%d", job->id);
1946 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
1949 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
1951 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
1953 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_TEXT, "job-state-message", NULL, "");
1954 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
1955 NULL, job->reasons->values[0].string.text);
1957 con->response->request.status.status_code = IPP_OK;
1960 * Add any job subscriptions...
1963 add_job_subscriptions(con, job);
1966 * Set all but the first two attributes to the job attributes group...
1969 for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
1970 attr->group_tag = IPP_TAG_JOB;
1973 * Fire the "job created" event...
1976 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
1979 * Return the new job...
1987 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1991 add_job_subscriptions(
1992 cupsd_client_t *con, /* I - Client connection */
1993 cupsd_job_t *job) /* I - Newly created job */
1995 int i; /* Looping var */
1996 ipp_attribute_t *prev, /* Previous attribute */
1997 *next, /* Next attribute */
1998 *attr; /* Current attribute */
1999 cupsd_subscription_t *sub; /* Subscription object */
2000 const char *recipient, /* notify-recipient-uri */
2001 *pullmethod; /* notify-pull-method */
2002 ipp_attribute_t *user_data; /* notify-user-data */
2003 int interval; /* notify-time-interval */
2004 unsigned mask; /* notify-events */
2008 * Find the first subscription group attribute; return if we have
2012 for (attr = job->attrs->attrs; attr; attr = attr->next)
2013 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
2020 * Process the subscription attributes in the request...
2029 mask = CUPSD_EVENT_NONE;
2031 while (attr && attr->group_tag != IPP_TAG_ZERO)
2033 if (!strcmp(attr->name, "notify-recipient-uri") &&
2034 attr->value_tag == IPP_TAG_URI)
2037 * Validate the recipient scheme against the ServerBin/notifier
2041 char notifier[1024], /* Notifier filename */
2042 scheme[HTTP_MAX_URI], /* Scheme portion of URI */
2043 userpass[HTTP_MAX_URI], /* Username portion of URI */
2044 host[HTTP_MAX_URI], /* Host portion of URI */
2045 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2046 int port; /* Port portion of URI */
2049 recipient = attr->values[0].string.text;
2051 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
2052 scheme, sizeof(scheme), userpass, sizeof(userpass),
2053 host, sizeof(host), &port,
2054 resource, sizeof(resource)) < HTTP_URI_OK)
2056 send_ipp_status(con, IPP_NOT_POSSIBLE,
2057 _("Bad notify-recipient-uri \"%s\"."), recipient);
2058 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2059 "notify-status-code", IPP_URI_SCHEME);
2063 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
2065 if (access(notifier, X_OK))
2067 send_ipp_status(con, IPP_NOT_POSSIBLE,
2068 _("notify-recipient-uri URI \"%s\" uses unknown "
2069 "scheme."), recipient);
2070 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2071 "notify-status-code", IPP_URI_SCHEME);
2075 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
2077 send_ipp_status(con, IPP_NOT_POSSIBLE,
2078 _("notify-recipient-uri URI \"%s\" is already used."),
2080 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2081 "notify-status-code", IPP_ATTRIBUTES);
2085 else if (!strcmp(attr->name, "notify-pull-method") &&
2086 attr->value_tag == IPP_TAG_KEYWORD)
2088 pullmethod = attr->values[0].string.text;
2090 if (strcmp(pullmethod, "ippget"))
2092 send_ipp_status(con, IPP_NOT_POSSIBLE,
2093 _("Bad notify-pull-method \"%s\"."), pullmethod);
2094 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
2095 "notify-status-code", IPP_ATTRIBUTES);
2099 else if (!strcmp(attr->name, "notify-charset") &&
2100 attr->value_tag == IPP_TAG_CHARSET &&
2101 strcmp(attr->values[0].string.text, "us-ascii") &&
2102 strcmp(attr->values[0].string.text, "utf-8"))
2104 send_ipp_status(con, IPP_CHARSET,
2105 _("Character set \"%s\" not supported."),
2106 attr->values[0].string.text);
2109 else if (!strcmp(attr->name, "notify-natural-language") &&
2110 (attr->value_tag != IPP_TAG_LANGUAGE ||
2111 strcmp(attr->values[0].string.text, DefaultLanguage)))
2113 send_ipp_status(con, IPP_CHARSET,
2114 _("Language \"%s\" not supported."),
2115 attr->values[0].string.text);
2118 else if (!strcmp(attr->name, "notify-user-data") &&
2119 attr->value_tag == IPP_TAG_STRING)
2121 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
2123 send_ipp_status(con, IPP_REQUEST_VALUE,
2124 _("The notify-user-data value is too large "
2125 "(%d > 63 octets)."),
2126 attr->values[0].unknown.length);
2132 else if (!strcmp(attr->name, "notify-events") &&
2133 attr->value_tag == IPP_TAG_KEYWORD)
2135 for (i = 0; i < attr->num_values; i ++)
2136 mask |= cupsdEventValue(attr->values[i].string.text);
2138 else if (!strcmp(attr->name, "notify-lease-duration"))
2140 send_ipp_status(con, IPP_BAD_REQUEST,
2141 _("The notify-lease-duration attribute cannot be "
2142 "used with job subscriptions."));
2145 else if (!strcmp(attr->name, "notify-time-interval") &&
2146 attr->value_tag == IPP_TAG_INTEGER)
2147 interval = attr->values[0].integer;
2152 if (!recipient && !pullmethod)
2155 if (mask == CUPSD_EVENT_NONE)
2156 mask = CUPSD_EVENT_JOB_COMPLETED;
2158 if ((sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job,
2159 recipient, 0)) != NULL)
2161 sub->interval = interval;
2163 cupsdSetString(&sub->owner, job->username);
2167 sub->user_data_len = user_data->values[0].unknown.length;
2168 memcpy(sub->user_data, user_data->values[0].unknown.data,
2169 (size_t)sub->user_data_len);
2172 ippAddSeparator(con->response);
2173 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
2174 "notify-subscription-id", sub->id);
2176 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d",
2184 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
2187 * Remove all of the subscription attributes from the job request...
2189 * TODO: Optimize this since subscription groups have to come at the
2190 * end of the request...
2193 for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
2197 if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
2198 attr->group_tag == IPP_TAG_ZERO)
2201 * Free and remove this attribute...
2204 ippDeleteAttribute(NULL, attr);
2209 job->attrs->attrs = next;
2215 job->attrs->last = prev;
2216 job->attrs->current = prev;
2221 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2223 * See RFC 4122 for the definition of UUIDs and the format.
2227 add_job_uuid(cupsd_job_t *job) /* I - Job */
2229 char uuid[64]; /* job-uuid string */
2233 * Add a job-uuid attribute if none exists...
2236 if (!ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
2237 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL,
2238 httpAssembleUUID(ServerName, RemotePort, job->dest, job->id,
2239 uuid, sizeof(uuid)));
2244 * 'add_printer()' - Add a printer to the system.
2248 add_printer(cupsd_client_t *con, /* I - Client connection */
2249 ipp_attribute_t *uri) /* I - URI of printer */
2251 http_status_t status; /* Policy status */
2252 int i; /* Looping var */
2253 char scheme[HTTP_MAX_URI], /* Method portion of URI */
2254 username[HTTP_MAX_URI], /* Username portion of URI */
2255 host[HTTP_MAX_URI], /* Host portion of URI */
2256 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2257 int port; /* Port portion of URI */
2258 cupsd_printer_t *printer; /* Printer/class */
2259 ipp_attribute_t *attr; /* Printer attribute */
2260 cups_file_t *fp; /* Script/PPD file */
2261 char line[1024]; /* Line from file... */
2262 char srcfile[1024], /* Source Script/PPD file */
2263 dstfile[1024]; /* Destination Script/PPD file */
2264 int modify; /* Non-zero if we are modifying */
2265 int changed_driver, /* Changed the PPD? */
2266 need_restart_job, /* Need to restart job? */
2267 set_device_uri, /* Did we set the device URI? */
2268 set_port_monitor; /* Did we set the port monitor? */
2271 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
2272 con->number, uri->values[0].string.text);
2275 * Do we have a valid URI?
2278 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
2279 sizeof(scheme), username, sizeof(username), host,
2280 sizeof(host), &port, resource, sizeof(resource));
2282 if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
2285 * No, return an error...
2288 send_ipp_status(con, IPP_BAD_REQUEST,
2289 _("The printer-uri must be of the form "
2290 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2295 * Do we have a valid printer name?
2298 if (!validate_name(resource + 10))
2301 * No, return an error...
2304 send_ipp_status(con, IPP_BAD_REQUEST,
2305 _("The printer-uri \"%s\" contains invalid characters."),
2306 uri->values[0].string.text);
2311 * See if the printer already exists; if not, create a new printer...
2314 if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
2317 * Printer doesn't exist; see if we have a class of the same name...
2320 if ((printer = cupsdFindClass(resource + 10)) != NULL)
2323 * Yes, return an error...
2326 send_ipp_status(con, IPP_NOT_POSSIBLE,
2327 _("A class named \"%s\" already exists."),
2333 * No, check the default policy then add the printer...
2336 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
2338 send_http_error(con, status, NULL);
2342 printer = cupsdAddPrinter(resource + 10);
2345 else if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
2348 send_http_error(con, status, printer);
2355 * Look for attributes and copy them over as needed...
2359 need_restart_job = 0;
2361 if ((attr = ippFindAttribute(con->request, "printer-is-temporary", IPP_TAG_BOOLEAN)) != NULL)
2362 printer->temporary = ippGetBoolean(attr, 0);
2364 if ((attr = ippFindAttribute(con->request, "printer-location",
2365 IPP_TAG_TEXT)) != NULL)
2366 cupsdSetString(&printer->location, attr->values[0].string.text);
2368 if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4))
2369 cupsdSetString(&printer->geo_location, attr->values[0].string.text);
2371 if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL)
2372 cupsdSetString(&printer->organization, attr->values[0].string.text);
2374 if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL)
2375 cupsdSetString(&printer->organizational_unit, attr->values[0].string.text);
2377 if ((attr = ippFindAttribute(con->request, "printer-info",
2378 IPP_TAG_TEXT)) != NULL)
2379 cupsdSetString(&printer->info, attr->values[0].string.text);
2383 if ((attr = ippFindAttribute(con->request, "device-uri",
2384 IPP_TAG_URI)) != NULL)
2387 * Do we have a valid device URI?
2390 http_uri_status_t uri_status; /* URI separation status */
2391 char old_device_uri[1024];
2392 /* Old device URI */
2394 need_restart_job = 1;
2396 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL,
2397 attr->values[0].string.text,
2398 scheme, sizeof(scheme),
2399 username, sizeof(username),
2400 host, sizeof(host), &port,
2401 resource, sizeof(resource));
2403 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s device-uri: %s", printer->name, httpURIStatusString(uri_status));
2405 if (uri_status < HTTP_URI_OK)
2407 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"."),
2408 attr->values[0].string.text);
2410 cupsdDeletePrinter(printer, 0);
2415 if (!strcmp(scheme, "file"))
2418 * See if the administrator has enabled file devices...
2421 if (!FileDevice && strcmp(resource, "/dev/null"))
2424 * File devices are disabled and the URL is not file:/dev/null...
2427 send_ipp_status(con, IPP_NOT_POSSIBLE,
2428 _("File device URIs have been disabled. "
2429 "To enable, see the FileDevice directive in "
2430 "\"%s/cups-files.conf\"."),
2433 cupsdDeletePrinter(printer, 0);
2441 * See if the backend exists and is executable...
2444 snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
2445 if (access(srcfile, X_OK))
2448 * Could not find device in list!
2451 send_ipp_status(con, IPP_NOT_POSSIBLE,
2452 _("Bad device-uri scheme \"%s\"."), scheme);
2454 cupsdDeletePrinter(printer, 0);
2460 if (printer->sanitized_device_uri)
2461 strlcpy(old_device_uri, printer->sanitized_device_uri,
2462 sizeof(old_device_uri));
2464 old_device_uri[0] = '\0';
2466 cupsdSetDeviceURI(printer, attr->values[0].string.text);
2468 cupsdLogMessage(CUPSD_LOG_INFO,
2469 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2470 printer->name, printer->sanitized_device_uri,
2476 set_port_monitor = 0;
2478 if ((attr = ippFindAttribute(con->request, "port-monitor",
2479 IPP_TAG_NAME)) != NULL)
2481 ipp_attribute_t *supported; /* port-monitor-supported attribute */
2484 need_restart_job = 1;
2486 supported = ippFindAttribute(printer->ppd_attrs, "port-monitor-supported",
2490 for (i = 0; i < supported->num_values; i ++)
2491 if (!strcmp(supported->values[i].string.text,
2492 attr->values[0].string.text))
2496 if (!supported || i >= supported->num_values)
2498 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"."),
2499 attr->values[0].string.text);
2501 cupsdDeletePrinter(printer, 0);
2506 cupsdLogMessage(CUPSD_LOG_INFO,
2507 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2508 printer->name, attr->values[0].string.text,
2509 printer->port_monitor ? printer->port_monitor : "none");
2511 if (strcmp(attr->values[0].string.text, "none"))
2512 cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
2514 cupsdClearString(&printer->port_monitor);
2516 set_port_monitor = 1;
2519 if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
2520 IPP_TAG_BOOLEAN)) != NULL &&
2521 attr->values[0].boolean != printer->accepting)
2523 cupsdLogMessage(CUPSD_LOG_INFO,
2524 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2525 printer->name, attr->values[0].boolean, printer->accepting);
2527 printer->accepting = attr->values[0].boolean;
2529 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
2530 "%s accepting jobs.",
2531 printer->accepting ? "Now" : "No longer");
2534 if ((attr = ippFindAttribute(con->request, "printer-is-shared", IPP_TAG_BOOLEAN)) != NULL)
2536 if (ippGetBoolean(attr, 0) &&
2537 printer->num_auth_info_required == 1 &&
2538 !strcmp(printer->auth_info_required[0], "negotiate"))
2540 send_ipp_status(con, IPP_BAD_REQUEST,
2541 _("Cannot share a remote Kerberized printer."));
2543 cupsdDeletePrinter(printer, 0);
2548 if (printer->type & CUPS_PRINTER_REMOTE)
2551 * Cannot re-share remote printers.
2554 send_ipp_status(con, IPP_BAD_REQUEST, _("Cannot change printer-is-shared for remote queues."));
2556 cupsdDeletePrinter(printer, 0);
2561 if (printer->shared && !ippGetBoolean(attr, 0))
2562 cupsdDeregisterPrinter(printer, 1);
2564 cupsdLogMessage(CUPSD_LOG_INFO,
2565 "Setting %s printer-is-shared to %d (was %d.)",
2566 printer->name, attr->values[0].boolean, printer->shared);
2568 printer->shared = ippGetBoolean(attr, 0);
2569 if (printer->shared && printer->temporary)
2570 printer->temporary = 0;
2573 if ((attr = ippFindAttribute(con->request, "printer-state",
2574 IPP_TAG_ENUM)) != NULL)
2576 if (attr->values[0].integer != IPP_PRINTER_IDLE &&
2577 attr->values[0].integer != IPP_PRINTER_STOPPED)
2579 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d."),
2580 attr->values[0].integer);
2582 cupsdDeletePrinter(printer, 0);
2587 cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
2588 printer->name, attr->values[0].integer, printer->state);
2590 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
2591 cupsdStopPrinter(printer, 0);
2594 need_restart_job = 1;
2595 cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
2599 if ((attr = ippFindAttribute(con->request, "printer-state-message",
2600 IPP_TAG_TEXT)) != NULL)
2602 strlcpy(printer->state_message, attr->values[0].string.text,
2603 sizeof(printer->state_message));
2605 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL, "%s",
2606 printer->state_message);
2609 if ((attr = ippFindAttribute(con->request, "printer-state-reasons",
2610 IPP_TAG_KEYWORD)) != NULL)
2612 if (attr->num_values >
2613 (int)(sizeof(printer->reasons) / sizeof(printer->reasons[0])))
2615 send_ipp_status(con, IPP_NOT_POSSIBLE,
2616 _("Too many printer-state-reasons values (%d > %d)."),
2618 (int)(sizeof(printer->reasons) /
2619 sizeof(printer->reasons[0])));
2621 cupsdDeletePrinter(printer, 0);
2626 for (i = 0; i < printer->num_reasons; i ++)
2627 _cupsStrFree(printer->reasons[i]);
2629 printer->num_reasons = 0;
2630 for (i = 0; i < attr->num_values; i ++)
2632 if (!strcmp(attr->values[i].string.text, "none"))
2635 printer->reasons[printer->num_reasons] =
2636 _cupsStrRetain(attr->values[i].string.text);
2637 printer->num_reasons ++;
2639 if (!strcmp(attr->values[i].string.text, "paused") &&
2640 printer->state != IPP_PRINTER_STOPPED)
2642 cupsdLogMessage(CUPSD_LOG_INFO,
2643 "Setting %s printer-state to %d (was %d.)",
2644 printer->name, IPP_PRINTER_STOPPED, printer->state);
2645 cupsdStopPrinter(printer, 0);
2649 if (PrintcapFormat == PRINTCAP_PLIST)
2650 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2652 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
2653 "Printer \"%s\" state changed.", printer->name);
2656 if (!set_printer_defaults(con, printer))
2659 cupsdDeletePrinter(printer, 0);
2664 if ((attr = ippFindAttribute(con->request, "auth-info-required",
2665 IPP_TAG_KEYWORD)) != NULL)
2666 cupsdSetAuthInfoRequired(printer, NULL, attr);
2669 * See if we have all required attributes...
2672 if (!printer->device_uri)
2673 cupsdSetString(&printer->device_uri, "file:///dev/null");
2676 * See if we have a PPD file attached to the request...
2681 need_restart_job = 1;
2684 strlcpy(srcfile, con->filename, sizeof(srcfile));
2686 if ((fp = cupsFileOpen(srcfile, "rb")))
2689 * Yes; get the first line from it...
2693 cupsFileGets(fp, line, sizeof(line));
2697 * Then see what kind of file it is...
2700 if (strncmp(line, "*PPD-Adobe", 10))
2702 send_ipp_status(con, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED, _("Bad PPD file."));
2704 cupsdDeletePrinter(printer, 0);
2709 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
2713 * The new file is a PPD file, so move the file over to the ppd
2717 if (copy_file(srcfile, dstfile, ConfigFilePerm))
2719 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file - %s"), strerror(errno));
2721 cupsdDeletePrinter(printer, 0);
2726 cupsdLogMessage(CUPSD_LOG_DEBUG, "Copied PPD file successfully");
2729 else if ((attr = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME)) != NULL)
2731 const char *ppd_name = ippGetString(attr, 0, NULL);
2732 /* ppd-name value */
2734 need_restart_job = 1;
2737 if (!strcmp(ppd_name, "raw"))
2740 * Raw driver, remove any existing PPD file.
2743 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, printer->name);
2746 else if (strstr(ppd_name, "../"))
2748 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
2750 cupsdDeletePrinter(printer, 0);
2760 snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot, printer->name);
2762 if (copy_model(con, ppd_name, dstfile))
2764 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file."));
2766 cupsdDeletePrinter(printer, 0);
2771 cupsdLogMessage(CUPSD_LOG_DEBUG, "Copied PPD file successfully");
2778 * If we changed the PPD, then remove the printer's cache file and clear the
2779 * printer-state-reasons...
2782 char cache_name[1024]; /* Cache filename for printer attrs */
2784 snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir, printer->name);
2787 cupsdSetPrinterReasons(printer, "none");
2790 * (Re)register color profiles...
2793 cupsdRegisterColor(printer);
2797 * If we set the device URI but not the port monitor, check which port
2798 * monitor to use by default...
2801 if (set_device_uri && !set_port_monitor)
2803 ppd_file_t *ppd; /* PPD file */
2804 ppd_attr_t *ppdattr; /* cupsPortMonitor attribute */
2807 httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
2808 sizeof(scheme), username, sizeof(username), host,
2809 sizeof(host), &port, resource, sizeof(resource));
2811 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
2813 if ((ppd = _ppdOpenFile(srcfile, _PPD_LOCALIZATION_NONE)) != NULL)
2815 for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2817 ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2818 if (!strcmp(scheme, ppdattr->spec))
2820 cupsdLogMessage(CUPSD_LOG_INFO,
2821 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2822 printer->name, ppdattr->value,
2823 printer->port_monitor ? printer->port_monitor
2826 if (strcmp(ppdattr->value, "none"))
2827 cupsdSetString(&printer->port_monitor, ppdattr->value);
2829 cupsdClearString(&printer->port_monitor);
2838 printer->config_time = time(NULL);
2841 * Update the printer attributes and return...
2844 cupsdSetPrinterAttrs(printer);
2845 if (!printer->temporary)
2846 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
2848 if (need_restart_job && printer->job)
2851 * Restart the current job...
2854 cupsdSetJobState(printer->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
2855 "Job restarted because the printer was modified.");
2858 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
2862 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED,
2863 printer, NULL, "Printer \"%s\" modified by \"%s\".",
2864 printer->name, get_username(con));
2866 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
2867 printer->name, get_username(con));
2871 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED,
2872 printer, NULL, "New printer \"%s\" added by \"%s\".",
2873 printer->name, get_username(con));
2875 cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
2876 printer->name, get_username(con));
2879 con->response->request.status.status_code = IPP_OK;
2884 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2885 * based upon the printer state...
2889 add_printer_state_reasons(
2890 cupsd_client_t *con, /* I - Client connection */
2891 cupsd_printer_t *p) /* I - Printer info */
2893 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2894 "add_printer_state_reasons(%p[%d], %p[%s])",
2895 con, con->number, p, p->name);
2897 if (p->num_reasons == 0)
2898 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2899 "printer-state-reasons", NULL, "none");
2901 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2902 "printer-state-reasons", p->num_reasons, NULL,
2903 (const char * const *)p->reasons);
2908 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2909 * the specified printer or class.
2913 add_queued_job_count(
2914 cupsd_client_t *con, /* I - Client connection */
2915 cupsd_printer_t *p) /* I - Printer or class */
2917 int count; /* Number of jobs on destination */
2920 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
2921 con, con->number, p, p->name);
2923 count = cupsdGetPrinterJobCount(p->name);
2925 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2926 "queued-job-count", count);
2931 * 'apply_printer_defaults()' - Apply printer default options to a job.
2935 apply_printer_defaults(
2936 cupsd_printer_t *printer, /* I - Printer */
2937 cupsd_job_t *job) /* I - Job */
2939 int i, /* Looping var */
2940 num_options; /* Number of default options */
2941 cups_option_t *options, /* Default options */
2942 *option; /* Current option */
2945 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Applying default options...");
2948 * Collect all of the default options and add the missing ones to the
2952 for (i = printer->num_options, num_options = 0, options = NULL,
2953 option = printer->options;
2956 if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
2958 if (!strcmp(option->name, "print-quality") && ippFindAttribute(job->attrs, "cupsPrintQuality", IPP_TAG_NAME))
2959 continue; /* Don't override cupsPrintQuality */
2961 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Adding default %s=%s", option->name, option->value);
2963 num_options = cupsAddOption(option->name, option->value, num_options, &options);
2967 * Encode these options as attributes in the job object...
2970 cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB);
2971 cupsFreeOptions(num_options, options);
2976 * 'authenticate_job()' - Set job authentication info.
2980 authenticate_job(cupsd_client_t *con, /* I - Client connection */
2981 ipp_attribute_t *uri) /* I - Job URI */
2983 ipp_attribute_t *attr, /* job-id attribute */
2984 *auth_info; /* auth-info attribute */
2985 int jobid; /* Job ID */
2986 cupsd_job_t *job; /* Current job */
2987 char scheme[HTTP_MAX_URI],
2988 /* Method portion of URI */
2989 username[HTTP_MAX_URI],
2990 /* Username portion of URI */
2992 /* Host portion of URI */
2993 resource[HTTP_MAX_URI];
2994 /* Resource portion of URI */
2995 int port; /* Port portion of URI */
2998 cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)",
2999 con, con->number, uri->values[0].string.text);
3002 * Start with "everything is OK" status...
3005 con->response->request.status.status_code = IPP_OK;
3008 * See if we have a job URI or a printer URI...
3011 if (!strcmp(uri->name, "printer-uri"))
3014 * Got a printer URI; see if we also have a job-id attribute...
3017 if ((attr = ippFindAttribute(con->request, "job-id",
3018 IPP_TAG_INTEGER)) == NULL)
3020 send_ipp_status(con, IPP_BAD_REQUEST,
3021 _("Got a printer-uri attribute but no job-id."));
3025 jobid = attr->values[0].integer;
3030 * Got a job URI; parse it to get the job ID...
3033 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3034 sizeof(scheme), username, sizeof(username), host,
3035 sizeof(host), &port, resource, sizeof(resource));
3037 if (strncmp(resource, "/jobs/", 6))
3043 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
3044 uri->values[0].string.text);
3048 jobid = atoi(resource + 6);
3052 * See if the job exists...
3055 if ((job = cupsdFindJob(jobid)) == NULL)
3058 * Nope - return a "not found" error...
3061 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
3066 * See if the job has been completed...
3069 if (job->state_value != IPP_JOB_HELD)
3072 * Return a "not-possible" error...
3075 send_ipp_status(con, IPP_NOT_POSSIBLE,
3076 _("Job #%d is not held for authentication."),
3082 * See if we have already authenticated...
3085 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
3087 if (!con->username[0] && !auth_info)
3089 cupsd_printer_t *printer; /* Job destination */
3092 * No auth data. If we need to authenticate via Kerberos, send a
3093 * HTTP auth challenge, otherwise just return an IPP error...
3096 printer = cupsdFindDest(job->dest);
3098 if (printer && printer->num_auth_info_required > 0 &&
3099 !strcmp(printer->auth_info_required[0], "negotiate"))
3100 send_http_error(con, HTTP_UNAUTHORIZED, printer);
3102 send_ipp_status(con, IPP_NOT_AUTHORIZED,
3103 _("No authentication information provided."));
3108 * See if the job is owned by the requesting user...
3111 if (!validate_user(job, con, job->username, username, sizeof(username)))
3113 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3114 cupsdFindDest(job->dest));
3119 * Save the authentication information for this job...
3122 save_auth_info(con, job, auth_info);
3125 * Reset the job-hold-until value to "no-hold"...
3128 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
3129 IPP_TAG_KEYWORD)) == NULL)
3130 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
3134 ippSetValueTag(job->attrs, &attr, IPP_TAG_KEYWORD);
3135 ippSetString(job->attrs, &attr, 0, "no-hold");
3139 * Release the job and return...
3142 cupsdReleaseJob(job);
3144 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, NULL, job, "Job authenticated by user");
3146 cupsdLogJob(job, CUPSD_LOG_INFO, "Authenticated by \"%s\".", con->username);
3153 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3157 cancel_all_jobs(cupsd_client_t *con, /* I - Client connection */
3158 ipp_attribute_t *uri) /* I - Job or Printer URI */
3160 int i; /* Looping var */
3161 http_status_t status; /* Policy status */
3162 cups_ptype_t dtype; /* Destination type */
3163 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3164 userpass[HTTP_MAX_URI], /* Username portion of URI */
3165 hostname[HTTP_MAX_URI], /* Host portion of URI */
3166 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3167 int port; /* Port portion of URI */
3168 ipp_attribute_t *attr; /* Attribute in request */
3169 const char *username = NULL; /* Username */
3170 cupsd_jobaction_t purge = CUPSD_JOB_DEFAULT;
3172 cupsd_printer_t *printer; /* Printer */
3173 ipp_attribute_t *job_ids; /* job-ids attribute */
3174 cupsd_job_t *job; /* Job */
3177 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con,
3178 con->number, uri->values[0].string.text);
3181 * Get the jobs to cancel/purge...
3184 switch (con->request->request.op.operation_id)
3186 case IPP_PURGE_JOBS :
3188 * Get the username (if any) for the jobs we want to cancel (only if
3189 * "my-jobs" is specified...
3192 if ((attr = ippFindAttribute(con->request, "my-jobs",
3193 IPP_TAG_BOOLEAN)) != NULL &&
3194 attr->values[0].boolean)
3196 if ((attr = ippFindAttribute(con->request, "requesting-user-name",
3197 IPP_TAG_NAME)) != NULL)
3198 username = attr->values[0].string.text;
3201 send_ipp_status(con, IPP_BAD_REQUEST,
3202 _("Missing requesting-user-name attribute."));
3208 * Look for the "purge-jobs" attribute...
3211 if ((attr = ippFindAttribute(con->request, "purge-jobs",
3212 IPP_TAG_BOOLEAN)) != NULL)
3213 purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT;
3215 purge = CUPSD_JOB_PURGE;
3218 case IPP_CANCEL_MY_JOBS :
3219 if (con->username[0])
3220 username = con->username;
3221 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
3222 IPP_TAG_NAME)) != NULL)
3223 username = attr->values[0].string.text;
3226 send_ipp_status(con, IPP_BAD_REQUEST,
3227 _("Missing requesting-user-name attribute."));
3235 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
3238 * See if we have a printer URI...
3241 if (strcmp(uri->name, "printer-uri"))
3243 send_ipp_status(con, IPP_BAD_REQUEST,
3244 _("The printer-uri attribute is required."));
3249 * And if the destination is valid...
3252 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3258 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text,
3259 scheme, sizeof(scheme), userpass, sizeof(userpass),
3260 hostname, sizeof(hostname), &port,
3261 resource, sizeof(resource));
3263 if ((!strncmp(resource, "/printers/", 10) && resource[10]) ||
3264 (!strncmp(resource, "/classes/", 9) && resource[9]))
3266 send_ipp_status(con, IPP_NOT_FOUND,
3267 _("The printer or class does not exist."));
3275 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
3277 send_http_error(con, status, NULL);
3283 for (i = 0; i < job_ids->num_values; i ++)
3285 if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL)
3288 if (con->request->request.op.operation_id == IPP_CANCEL_MY_JOBS &&
3289 _cups_strcasecmp(job->username, username))
3293 if (i < job_ids->num_values)
3295 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
3296 job_ids->values[i].integer);
3300 for (i = 0; i < job_ids->num_values; i ++)
3302 job = cupsdFindJob(job_ids->values[i].integer);
3304 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3305 purge == CUPSD_JOB_PURGE ? "Job purged by user." :
3306 "Job canceled by user.");
3309 cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".",
3310 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3316 * Cancel all jobs on all printers...
3319 cupsdCancelJobs(NULL, username, purge);
3321 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
3322 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3332 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
3335 send_http_error(con, status, printer);
3341 for (i = 0; i < job_ids->num_values; i ++)
3343 if ((job = cupsdFindJob(job_ids->values[i].integer)) == NULL ||
3344 _cups_strcasecmp(job->dest, printer->name))
3347 if (con->request->request.op.operation_id == IPP_CANCEL_MY_JOBS &&
3348 _cups_strcasecmp(job->username, username))
3352 if (i < job_ids->num_values)
3354 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
3355 job_ids->values[i].integer);
3359 for (i = 0; i < job_ids->num_values; i ++)
3361 job = cupsdFindJob(job_ids->values[i].integer);
3363 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3364 purge == CUPSD_JOB_PURGE ? "Job purged by user." :
3365 "Job canceled by user.");
3368 cupsdLogMessage(CUPSD_LOG_INFO, "Selected jobs were %s by \"%s\".",
3369 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3375 * Cancel all of the jobs on the named printer...
3378 cupsdCancelJobs(printer->name, username, purge);
3380 cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
3382 purge == CUPSD_JOB_PURGE ? "purged" : "canceled",
3387 con->response->request.status.status_code = IPP_OK;
3394 * 'cancel_job()' - Cancel a print job.
3398 cancel_job(cupsd_client_t *con, /* I - Client connection */
3399 ipp_attribute_t *uri) /* I - Job or Printer URI */
3401 ipp_attribute_t *attr; /* Current attribute */
3402 int jobid; /* Job ID */
3403 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
3404 username[HTTP_MAX_URI], /* Username portion of URI */
3405 host[HTTP_MAX_URI], /* Host portion of URI */
3406 resource[HTTP_MAX_URI]; /* Resource portion of URI */
3407 int port; /* Port portion of URI */
3408 cupsd_job_t *job; /* Job information */
3409 cups_ptype_t dtype; /* Destination type (printer/class) */
3410 cupsd_printer_t *printer; /* Printer data */
3411 cupsd_jobaction_t purge; /* Purge the job? */
3414 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
3415 con->number, uri->values[0].string.text);
3418 * See if we have a job URI or a printer URI...
3421 if (!strcmp(uri->name, "printer-uri"))
3424 * Got a printer URI; see if we also have a job-id attribute...
3427 if ((attr = ippFindAttribute(con->request, "job-id",
3428 IPP_TAG_INTEGER)) == NULL)
3430 send_ipp_status(con, IPP_BAD_REQUEST,
3431 _("Got a printer-uri attribute but no job-id."));
3435 if ((jobid = attr->values[0].integer) == 0)
3438 * Find the current job on the specified printer...
3441 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
3447 send_ipp_status(con, IPP_NOT_FOUND,
3448 _("The printer or class does not exist."));
3453 * See if there are any pending jobs...
3456 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3458 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3459 if (job->state_value <= IPP_JOB_PROCESSING &&
3460 !_cups_strcasecmp(job->dest, printer->name))
3468 * No, try stopped jobs...
3471 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
3473 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
3474 if (job->state_value == IPP_JOB_STOPPED &&
3475 !_cups_strcasecmp(job->dest, printer->name))
3482 send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s."),
3492 * Got a job URI; parse it to get the job ID...
3495 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
3496 sizeof(scheme), username, sizeof(username), host,
3497 sizeof(host), &port, resource, sizeof(resource));
3499 if (strncmp(resource, "/jobs/", 6))
3505 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
3506 uri->values[0].string.text);
3510 jobid = atoi(resource + 6);
3514 * Look for the "purge-job" attribute...
3517 if ((attr = ippFindAttribute(con->request, "purge-job",
3518 IPP_TAG_BOOLEAN)) != NULL)
3519 purge = attr->values[0].boolean ? CUPSD_JOB_PURGE : CUPSD_JOB_DEFAULT;
3521 purge = CUPSD_JOB_DEFAULT;
3524 * See if the job exists...
3527 if ((job = cupsdFindJob(jobid)) == NULL)
3530 * Nope - return a "not found" error...
3533 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
3538 * See if the job is owned by the requesting user...
3541 if (!validate_user(job, con, job->username, username, sizeof(username)))
3543 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3544 cupsdFindDest(job->dest));
3549 * See if the job is already completed, canceled, or aborted; if so,
3550 * we can't cancel...
3553 if (job->state_value >= IPP_JOB_CANCELED && purge != CUPSD_JOB_PURGE)
3555 switch (job->state_value)
3557 case IPP_JOB_CANCELED :
3558 send_ipp_status(con, IPP_NOT_POSSIBLE,
3559 _("Job #%d is already canceled - can\'t cancel."),
3563 case IPP_JOB_ABORTED :
3564 send_ipp_status(con, IPP_NOT_POSSIBLE,
3565 _("Job #%d is already aborted - can\'t cancel."),
3570 send_ipp_status(con, IPP_NOT_POSSIBLE,
3571 _("Job #%d is already completed - can\'t cancel."),
3580 * Cancel the job and return...
3583 cupsdSetJobState(job, IPP_JOB_CANCELED, purge,
3584 purge == CUPSD_JOB_PURGE ? "Job purged by \"%s\"" :
3585 "Job canceled by \"%s\"",
3589 if (purge == CUPSD_JOB_PURGE)
3590 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Purged by \"%s\".", jobid,
3593 cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
3596 con->response->request.status.status_code = IPP_OK;
3601 * 'cancel_subscription()' - Cancel a subscription.
3605 cancel_subscription(
3606 cupsd_client_t *con, /* I - Client connection */
3607 int sub_id) /* I - Subscription ID */
3609 http_status_t status; /* Policy status */
3610 cupsd_subscription_t *sub; /* Subscription */
3613 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3614 "cancel_subscription(con=%p[%d], sub_id=%d)",
3615 con, con->number, sub_id);
3618 * Is the subscription ID valid?
3621 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
3624 * Bad subscription ID...
3627 send_ipp_status(con, IPP_NOT_FOUND,
3628 _("Subscription #%d does not exist."), sub_id);
3636 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
3638 con, sub->owner)) != HTTP_OK)
3640 send_http_error(con, status, sub->dest);
3645 * Cancel the subscription...
3648 cupsdDeleteSubscription(sub, 1);
3650 con->response->request.status.status_code = IPP_OK;
3655 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3658 static int /* O - 1 if OK, 0 if not */
3659 check_rss_recipient(
3660 const char *recipient) /* I - Recipient URI */
3662 cupsd_subscription_t *sub; /* Current subscription */
3665 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
3667 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
3671 * Compare the URIs up to the first ?...
3674 const char *r1, *r2;
3676 for (r1 = recipient, r2 = sub->recipient;
3677 *r1 == *r2 && *r1 && *r1 != '?' && *r2 && *r2 != '?';
3689 * 'check_quotas()' - Check quotas for a printer and user.
3692 static int /* O - 1 if OK, 0 if forbidden,
3693 -1 if limit reached */
3694 check_quotas(cupsd_client_t *con, /* I - Client connection */
3695 cupsd_printer_t *p) /* I - Printer or class */
3697 char username[33], /* Username */
3698 *name; /* Current user name */
3699 cupsd_quota_t *q; /* Quota data */
3700 #ifdef HAVE_MBR_UID_TO_UUID
3702 * Use Apple membership APIs which require that all names represent
3703 * valid user account or group records accessible by the server.
3706 uuid_t usr_uuid; /* UUID for job requesting user */
3707 uuid_t usr2_uuid; /* UUID for ACL user name entry */
3708 uuid_t grp_uuid; /* UUID for ACL group name entry */
3709 int mbr_err; /* Error from membership function */
3710 int is_member; /* Is this user a member? */
3713 * Use standard POSIX APIs for checking users and groups...
3716 struct passwd *pw; /* User password data */
3717 #endif /* HAVE_MBR_UID_TO_UUID */
3720 cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
3721 con, con->number, p, p->name);
3724 * Figure out who is printing...
3727 strlcpy(username, get_username(con), sizeof(username));
3729 if ((name = strchr(username, '@')) != NULL)
3730 *name = '\0'; /* Strip @REALM */
3733 * Check global active job limits for printers and users...
3736 if (MaxJobsPerPrinter)
3739 * Check if there are too many pending jobs on this printer...
3742 if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter)
3744 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...",
3753 * Check if there are too many pending jobs for this user...
3756 if (cupsdGetUserJobCount(username) >= MaxJobsPerUser)
3758 cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...",
3765 * Check against users...
3768 if (cupsArrayCount(p->users) == 0 && p->k_limit == 0 && p->page_limit == 0)
3771 if (cupsArrayCount(p->users))
3773 #ifdef HAVE_MBR_UID_TO_UUID
3775 * Get UUID for job requesting user...
3778 if (mbr_user_name_to_uuid((char *)username, usr_uuid))
3784 cupsdLogMessage(CUPSD_LOG_DEBUG,
3785 "check_quotas: UUID lookup failed for user \"%s\"",
3787 cupsdLogMessage(CUPSD_LOG_INFO,
3788 "Denying user \"%s\" access to printer \"%s\" "
3789 "(unknown user)...",
3795 * Get UID and GID of requesting user...
3798 pw = getpwnam(username);
3800 #endif /* HAVE_MBR_UID_TO_UUID */
3802 for (name = (char *)cupsArrayFirst(p->users);
3804 name = (char *)cupsArrayNext(p->users))
3808 * Check group membership...
3811 #ifdef HAVE_MBR_UID_TO_UUID
3814 if (uuid_parse(name + 2, grp_uuid))
3815 uuid_clear(grp_uuid);
3817 else if ((mbr_err = mbr_group_name_to_uuid(name + 1, grp_uuid)) != 0)
3820 * Invalid ACL entries are ignored for matching; just record a
3821 * warning in the log...
3824 cupsdLogMessage(CUPSD_LOG_DEBUG,
3825 "check_quotas: UUID lookup failed for ACL entry "
3826 "\"%s\" (err=%d)", name, mbr_err);
3827 cupsdLogMessage(CUPSD_LOG_WARN,
3828 "Access control entry \"%s\" not a valid group name; "
3829 "entry ignored", name);
3832 if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid,
3836 * At this point, there should be no errors, but check anyways...
3839 cupsdLogMessage(CUPSD_LOG_DEBUG,
3840 "check_quotas: group \"%s\" membership check "
3841 "failed (err=%d)", name + 1, mbr_err);
3846 * Stop if we found a match...
3853 if (cupsdCheckGroup(username, pw, name + 1))
3855 #endif /* HAVE_MBR_UID_TO_UUID */
3857 #ifdef HAVE_MBR_UID_TO_UUID
3862 if (uuid_parse(name + 1, usr2_uuid))
3863 uuid_clear(usr2_uuid);
3865 else if ((mbr_err = mbr_user_name_to_uuid(name, usr2_uuid)) != 0)
3868 * Invalid ACL entries are ignored for matching; just record a
3869 * warning in the log...
3872 cupsdLogMessage(CUPSD_LOG_DEBUG,
3873 "check_quotas: UUID lookup failed for ACL entry "
3874 "\"%s\" (err=%d)", name, mbr_err);
3875 cupsdLogMessage(CUPSD_LOG_WARN,
3876 "Access control entry \"%s\" not a valid user name; "
3877 "entry ignored", name);
3880 if (!uuid_compare(usr_uuid, usr2_uuid))
3884 else if (!_cups_strcasecmp(username, name))
3886 #endif /* HAVE_MBR_UID_TO_UUID */
3888 if ((name != NULL) == p->deny_users)
3890 cupsdLogMessage(CUPSD_LOG_INFO,
3891 "Denying user \"%s\" access to printer \"%s\"...",
3901 if (p->k_limit || p->page_limit)
3903 if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL)
3905 cupsdLogMessage(CUPSD_LOG_ERROR,
3906 "Unable to allocate quota data for user \"%s\"",
3911 if ((q->k_count >= p->k_limit && p->k_limit) ||
3912 (q->page_count >= p->page_limit && p->page_limit))
3914 cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...",
3921 * If we have gotten this far, we're done!
3929 * 'close_job()' - Close a multi-file job.
3933 close_job(cupsd_client_t *con, /* I - Client connection */
3934 ipp_attribute_t *uri) /* I - Printer URI */
3936 cupsd_job_t *job; /* Job */
3937 ipp_attribute_t *attr; /* Attribute */
3938 char job_uri[HTTP_MAX_URI],
3940 username[256]; /* User name */
3943 cupsdLogMessage(CUPSD_LOG_DEBUG2, "close_job(%p[%d], %s)", con,
3944 con->number, uri->values[0].string.text);
3947 * See if we have a job URI or a printer URI...
3950 if (strcmp(uri->name, "printer-uri"))
3953 * job-uri is not supported by Close-Job!
3956 send_ipp_status(con, IPP_BAD_REQUEST,
3957 _("Close-Job doesn't support the job-uri attribute."));
3962 * Got a printer URI; see if we also have a job-id attribute...
3965 if ((attr = ippFindAttribute(con->request, "job-id",
3966 IPP_TAG_INTEGER)) == NULL)
3968 send_ipp_status(con, IPP_BAD_REQUEST,
3969 _("Got a printer-uri attribute but no job-id."));
3973 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
3976 * Nope - return a "not found" error...
3979 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
3980 attr->values[0].integer);
3985 * See if the job is owned by the requesting user...
3988 if (!validate_user(job, con, job->username, username, sizeof(username)))
3990 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
3991 cupsdFindDest(job->dest));
3996 * Add any ending sheet...
3999 if (cupsdTimeoutJob(job))
4002 if (job->state_value == IPP_JOB_STOPPED)
4004 job->state->values[0].integer = IPP_JOB_PENDING;
4005 job->state_value = IPP_JOB_PENDING;
4007 else if (job->state_value == IPP_JOB_HELD)
4009 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
4010 IPP_TAG_KEYWORD)) == NULL)
4011 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
4013 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
4015 job->state->values[0].integer = IPP_JOB_PENDING;
4016 job->state_value = IPP_JOB_PENDING;
4021 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
4024 * Fill in the response info...
4027 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4028 con->clientname, con->clientport, "/jobs/%d", job->id);
4029 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
4032 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
4034 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
4037 con->response->request.status.status_code = IPP_OK;
4040 * Start the job if necessary...
4048 * 'copy_attrs()' - Copy attributes from one request to another.
4052 copy_attrs(ipp_t *to, /* I - Destination request */
4053 ipp_t *from, /* I - Source request */
4054 cups_array_t *ra, /* I - Requested attributes */
4055 ipp_tag_t group, /* I - Group to copy */
4056 int quickcopy, /* I - Do a quick copy? */
4057 cups_array_t *exclude) /* I - Attributes to exclude? */
4059 ipp_attribute_t *fromattr; /* Source attribute */
4062 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4063 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4064 to, from, ra, group, quickcopy);
4069 for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
4072 * Filter attributes as needed...
4075 if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
4076 fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
4079 if (!strcmp(fromattr->name, "document-password") ||
4080 !strcmp(fromattr->name, "job-authorization-uri") ||
4081 !strcmp(fromattr->name, "job-password") ||
4082 !strcmp(fromattr->name, "job-password-encryption") ||
4083 !strcmp(fromattr->name, "job-printer-uri"))
4087 (cupsArrayFind(exclude, fromattr->name) ||
4088 cupsArrayFind(exclude, "all")))
4091 * We need to exclude this attribute for security reasons; we require the
4092 * job-id attribute regardless of the security settings for IPP
4095 * The job-printer-uri attribute is handled by copy_job_attrs().
4097 * Subscription attribute security is handled by copy_subscription_attrs().
4100 if (strcmp(fromattr->name, "job-id"))
4104 if (!ra || cupsArrayFind(ra, fromattr->name))
4107 * Don't send collection attributes by default to IPP/1.x clients
4108 * since many do not support collections. Also don't send
4109 * media-col-database unless specifically requested by the client.
4112 if (fromattr->value_tag == IPP_TAG_BEGIN_COLLECTION &&
4114 (to->request.status.version[0] == 1 ||
4115 !strcmp(fromattr->name, "media-col-database")))
4118 ippCopyAttribute(to, fromattr, quickcopy);
4125 * 'copy_banner()' - Copy a banner file to the requests directory for the
4129 static int /* O - Size of banner file in kbytes */
4130 copy_banner(cupsd_client_t *con, /* I - Client connection */
4131 cupsd_job_t *job, /* I - Job information */
4132 const char *name) /* I - Name of banner */
4134 int i; /* Looping var */
4135 int kbytes; /* Size of banner file in kbytes */
4136 char filename[1024]; /* Job filename */
4137 cupsd_banner_t *banner; /* Pointer to banner */
4138 cups_file_t *in; /* Input file */
4139 cups_file_t *out; /* Output file */
4140 int ch; /* Character from file */
4141 char attrname[255], /* Name of attribute */
4142 *s; /* Pointer into name */
4143 ipp_attribute_t *attr; /* Attribute */
4146 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4147 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4148 con, con ? con->number : -1, job, job->id,
4149 name ? name : "(null)");
4152 * Find the banner; return if not found or "none"...
4155 if (!name || !strcmp(name, "none") ||
4156 (banner = cupsdFindBanner(name)) == NULL)
4160 * Open the banner and job files...
4163 if (add_file(con, job, banner->filetype, 0))
4166 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
4168 if ((out = cupsFileOpen(filename, "w")) == NULL)
4170 cupsdLogMessage(CUPSD_LOG_ERROR,
4171 "Unable to create banner job file %s - %s",
4172 filename, strerror(errno));
4177 fchmod(cupsFileNumber(out), 0640);
4178 fchown(cupsFileNumber(out), RunUser, Group);
4181 * Try the localized banner file under the subdirectory...
4184 strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
4186 if (strlen(attrname) > 2 && attrname[2] == '-')
4189 * Convert ll-cc to ll_CC...
4193 attrname[3] = (char)toupper(attrname[3] & 255);
4194 attrname[4] = (char)toupper(attrname[4] & 255);
4197 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4200 if (access(filename, 0) && strlen(attrname) > 2)
4203 * Wasn't able to find "ll_CC" locale file; try the non-national
4204 * localization banner directory.
4209 snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
4213 if (access(filename, 0))
4216 * Use the non-localized banner file.
4219 snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name);
4222 if ((in = cupsFileOpen(filename, "r")) == NULL)
4226 cupsdLogMessage(CUPSD_LOG_ERROR,
4227 "Unable to open banner template file %s - %s",
4228 filename, strerror(errno));
4234 * Parse the file to the end...
4237 while ((ch = cupsFileGetChar(in)) != EOF)
4241 * Get an attribute name...
4244 for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;)
4245 if (!isalpha(ch & 255) && ch != '-' && ch != '?')
4247 else if (s < (attrname + sizeof(attrname) - 1))
4257 * Ignore { followed by stuff that is not an attribute name...
4260 cupsFilePrintf(out, "{%s%c", attrname, ch);
4265 * See if it is defined...
4268 if (attrname[0] == '?')
4273 if (!strcmp(s, "printer-name"))
4275 cupsFilePuts(out, job->dest);
4278 else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL)
4281 * See if we have a leading question mark...
4284 if (attrname[0] != '?')
4287 * Nope, write to file as-is; probably a PostScript procedure...
4290 cupsFilePrintf(out, "{%s}", attrname);
4297 * Output value(s)...
4300 for (i = 0; i < attr->num_values; i ++)
4303 cupsFilePutChar(out, ',');
4305 switch (attr->value_tag)
4307 case IPP_TAG_INTEGER :
4309 if (!strncmp(s, "time-at-", 8))
4311 struct timeval tv; /* Time value */
4313 tv.tv_sec = attr->values[i].integer;
4316 cupsFilePuts(out, cupsdGetDateTime(&tv, CUPSD_TIME_STANDARD));
4319 cupsFilePrintf(out, "%d", attr->values[i].integer);
4322 case IPP_TAG_BOOLEAN :
4323 cupsFilePrintf(out, "%d", attr->values[i].boolean);
4326 case IPP_TAG_NOVALUE :
4327 cupsFilePuts(out, "novalue");
4330 case IPP_TAG_RANGE :
4331 cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower,
4332 attr->values[i].range.upper);
4335 case IPP_TAG_RESOLUTION :
4336 cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres,
4337 attr->values[i].resolution.yres,
4338 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
4343 case IPP_TAG_STRING :
4346 case IPP_TAG_KEYWORD :
4347 case IPP_TAG_CHARSET :
4348 case IPP_TAG_LANGUAGE :
4349 if (!_cups_strcasecmp(banner->filetype->type, "postscript"))
4352 * Need to quote strings for PS banners...
4357 for (p = attr->values[i].string.text; *p; p ++)
4359 if (*p == '(' || *p == ')' || *p == '\\')
4361 cupsFilePutChar(out, '\\');
4362 cupsFilePutChar(out, *p);
4364 else if (*p < 32 || *p > 126)
4365 cupsFilePrintf(out, "\\%03o", *p & 255);
4367 cupsFilePutChar(out, *p);
4371 cupsFilePuts(out, attr->values[i].string.text);
4375 break; /* anti-compiler-warning-code */
4379 else if (ch == '\\') /* Quoted char */
4381 ch = cupsFileGetChar(in);
4383 if (ch != '{') /* Only do special handling for \{ */
4384 cupsFilePutChar(out, '\\');
4386 cupsFilePutChar(out, ch);
4389 cupsFilePutChar(out, ch);
4393 kbytes = (cupsFileTell(out) + 1023) / 1024;
4395 job->koctets += kbytes;
4397 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
4398 attr->values[0].integer += kbytes;
4407 * 'copy_file()' - Copy a PPD file...
4410 static int /* O - 0 = success, -1 = error */
4411 copy_file(const char *from, /* I - Source file */
4412 const char *to, /* I - Destination file */
4413 mode_t mode) /* I - Permissions */
4415 cups_file_t *src, /* Source file */
4416 *dst; /* Destination file */
4417 int bytes; /* Bytes to read/write */
4418 char buffer[2048]; /* Copy buffer */
4421 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to);
4424 * Open the source and destination file for a copy...
4427 if ((src = cupsFileOpen(from, "rb")) == NULL)
4430 if ((dst = cupsdCreateConfFile(to, mode)) == NULL)
4437 * Copy the source file to the destination...
4440 while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0)
4441 if (cupsFileWrite(dst, buffer, (size_t)bytes) < bytes)
4449 * Close both files and return...
4454 return (cupsdCloseCreatedConfFile(dst, to));
4459 * 'copy_model()' - Copy a PPD model file, substituting default values
4463 static int /* O - 0 = success, -1 = error */
4464 copy_model(cupsd_client_t *con, /* I - Client connection */
4465 const char *from, /* I - Source file */
4466 const char *to) /* I - Destination file */
4468 fd_set input; /* select() input set */
4469 struct timeval timeout; /* select() timeout */
4470 int maxfd; /* Max file descriptor for select() */
4471 char tempfile[1024]; /* Temporary PPD file */
4472 int tempfd; /* Temporary PPD file descriptor */
4473 int temppid; /* Process ID of cups-driverd */
4474 int temppipe[2]; /* Temporary pipes */
4475 char *argv[4], /* Command-line arguments */
4476 *envp[MAX_ENV]; /* Environment */
4477 cups_file_t *src, /* Source file */
4478 *dst; /* Destination file */
4479 ppd_file_t *ppd; /* PPD file */
4480 int bytes, /* Bytes from pipe */
4481 total; /* Total bytes from pipe */
4482 char buffer[2048]; /* Copy buffer */
4483 int i; /* Looping var */
4484 char option[PPD_MAX_NAME], /* Option name */
4485 choice[PPD_MAX_NAME]; /* Choice name */
4486 ppd_size_t *size; /* Default size */
4487 int num_defaults; /* Number of default options */
4488 cups_option_t *defaults; /* Default options */
4489 char cups_protocol[PPD_MAX_LINE];
4490 /* cupsProtocol attribute */
4493 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_model(con=%p, from=\"%s\", to=\"%s\")", con, from, to);
4496 * Run cups-driverd to get the PPD file...
4499 argv[0] = "cups-driverd";
4501 argv[2] = (char *)from;
4504 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
4506 snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin);
4507 snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->number);
4508 tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
4509 if (tempfd < 0 || cupsdOpenPipe(temppipe))
4512 cupsdLogMessage(CUPSD_LOG_DEBUG,
4513 "copy_model: Running \"cups-driverd cat %s\"...", from);
4515 if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
4516 -1, -1, 0, DefaultProfile, NULL, &temppid))
4527 * Wait up to 30 seconds for the PPD file to be copied...
4532 if (temppipe[0] > CGIPipes[0])
4533 maxfd = temppipe[0] + 1;
4535 maxfd = CGIPipes[0] + 1;
4540 * See if we have data ready...
4544 FD_SET(temppipe[0], &input);
4545 FD_SET(CGIPipes[0], &input);
4547 timeout.tv_sec = 30;
4548 timeout.tv_usec = 0;
4550 if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
4560 * We have timed out...
4566 if (FD_ISSET(temppipe[0], &input))
4569 * Read the PPD file from the pipe, and write it to the PPD file.
4572 if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0)
4574 if (write(tempfd, buffer, (size_t)bytes) < bytes)
4583 if (FD_ISSET(CGIPipes[0], &input))
4593 * No data from cups-deviced...
4596 cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file");
4602 * Open the source file for a copy...
4605 if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
4612 * Read the source file and see what page sizes are supported...
4615 if ((ppd = _ppdOpen(src, _PPD_LOCALIZATION_NONE)) == NULL)
4623 * Open the destination (if possible) and set the default options...
4628 cups_protocol[0] = '\0';
4630 if ((dst = cupsFileOpen(to, "rb")) != NULL)
4633 * Read all of the default lines from the old PPD...
4636 while (cupsFileGets(dst, buffer, sizeof(buffer)))
4637 if (!strncmp(buffer, "*Default", 8))
4640 * Add the default option...
4643 if (!ppd_parse_line(buffer, option, sizeof(option),
4644 choice, sizeof(choice)))
4646 ppd_option_t *ppdo; /* PPD option */
4650 * Only add the default if the default hasn't already been
4651 * set and the choice exists in the new PPD...
4654 if (!cupsGetOption(option, num_defaults, defaults) &&
4655 (ppdo = ppdFindOption(ppd, option)) != NULL &&
4656 ppdFindChoice(ppdo, choice))
4657 num_defaults = cupsAddOption(option, choice, num_defaults,
4661 else if (!strncmp(buffer, "*cupsProtocol:", 14))
4662 strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
4666 else if ((size = ppdPageSize(ppd, DefaultPaperSize)) != NULL)
4669 * Add the default media sizes...
4672 num_defaults = cupsAddOption("PageSize", size->name,
4673 num_defaults, &defaults);
4674 num_defaults = cupsAddOption("PageRegion", size->name,
4675 num_defaults, &defaults);
4676 num_defaults = cupsAddOption("PaperDimension", size->name,
4677 num_defaults, &defaults);
4678 num_defaults = cupsAddOption("ImageableArea", size->name,
4679 num_defaults, &defaults);
4685 * Open the destination file for a copy...
4688 if ((dst = cupsdCreateConfFile(to, ConfigFilePerm)) == NULL)
4690 cupsFreeOptions(num_defaults, defaults);
4697 * Copy the source file to the destination...
4700 cupsFileRewind(src);
4702 while (cupsFileGets(src, buffer, sizeof(buffer)))
4704 if (!strncmp(buffer, "*Default", 8))
4707 * Check for an previous default option choice...
4710 if (!ppd_parse_line(buffer, option, sizeof(option),
4711 choice, sizeof(choice)))
4713 const char *val; /* Default option value */
4716 if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL)
4719 * Substitute the previous choice...
4722 snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val);
4727 cupsFilePrintf(dst, "%s\n", buffer);
4730 if (cups_protocol[0])
4731 cupsFilePrintf(dst, "%s\n", cups_protocol);
4733 cupsFreeOptions(num_defaults, defaults);
4736 * Close both files and return...
4743 return (cupsdCloseCreatedConfFile(dst, to));
4748 * 'copy_job_attrs()' - Copy job attributes.
4752 copy_job_attrs(cupsd_client_t *con, /* I - Client connection */
4753 cupsd_job_t *job, /* I - Job */
4754 cups_array_t *ra, /* I - Requested attributes array */
4755 cups_array_t *exclude) /* I - Private attributes array */
4757 char job_uri[HTTP_MAX_URI]; /* Job URI */
4761 * Send the requested attributes for each job...
4764 if (!cupsArrayFind(exclude, "all"))
4766 if ((!exclude || !cupsArrayFind(exclude, "number-of-documents")) &&
4767 (!ra || cupsArrayFind(ra, "number-of-documents")))
4768 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4769 "number-of-documents", job->num_files);
4771 if ((!exclude || !cupsArrayFind(exclude, "job-media-progress")) &&
4772 (!ra || cupsArrayFind(ra, "job-media-progress")))
4773 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4774 "job-media-progress", job->progress);
4776 if ((!exclude || !cupsArrayFind(exclude, "job-more-info")) &&
4777 (!ra || cupsArrayFind(ra, "job-more-info")))
4779 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "http",
4780 NULL, con->clientname, con->clientport, "/jobs/%d",
4782 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4783 "job-more-info", NULL, job_uri);
4786 if (job->state_value > IPP_JOB_PROCESSING &&
4787 (!exclude || !cupsArrayFind(exclude, "job-preserved")) &&
4788 (!ra || cupsArrayFind(ra, "job-preserved")))
4789 ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved",
4790 job->num_files > 0);
4792 if ((!exclude || !cupsArrayFind(exclude, "job-printer-up-time")) &&
4793 (!ra || cupsArrayFind(ra, "job-printer-up-time")))
4794 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
4795 "job-printer-up-time", time(NULL));
4798 if (!ra || cupsArrayFind(ra, "job-printer-uri"))
4800 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4801 con->clientname, con->clientport,
4802 (job->dtype & CUPS_PRINTER_CLASS) ? "/classes/%s" :
4805 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4806 "job-printer-uri", NULL, job_uri);
4809 if (!ra || cupsArrayFind(ra, "job-uri"))
4811 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
4812 con->clientname, con->clientport, "/jobs/%d",
4814 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
4815 "job-uri", NULL, job_uri);
4820 copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0, exclude);
4825 * Generate attributes from the job structure...
4828 if (job->completed_time && (!ra || cupsArrayFind(ra, "date-time-at-completed")))
4829 ippAddDate(con->response, IPP_TAG_JOB, "date-time-at-completed", ippTimeToDate(job->completed_time));
4831 if (job->creation_time && (!ra || cupsArrayFind(ra, "date-time-at-creation")))
4832 ippAddDate(con->response, IPP_TAG_JOB, "date-time-at-creation", ippTimeToDate(job->creation_time));
4834 if (!ra || cupsArrayFind(ra, "job-id"))
4835 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
4837 if (!ra || cupsArrayFind(ra, "job-k-octets"))
4838 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-k-octets", job->koctets);
4840 if (job->name && (!ra || cupsArrayFind(ra, "job-name")))
4841 ippAddString(con->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_NAME), "job-name", NULL, job->name);
4843 if (job->username && (!ra || cupsArrayFind(ra, "job-originating-user-name")))
4844 ippAddString(con->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_NAME), "job-originating-user-name", NULL, job->username);
4846 if (!ra || cupsArrayFind(ra, "job-state"))
4847 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state", (int)job->state_value);
4849 if (!ra || cupsArrayFind(ra, "job-state-reasons"))
4851 switch (job->state_value)
4853 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4855 case IPP_JSTATE_ABORTED :
4856 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, "job-aborted-by-system");
4858 case IPP_JSTATE_CANCELED :
4859 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, "job-canceled-by-user");
4861 case IPP_JSTATE_COMPLETED :
4862 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons", NULL, "job-completed-successfully");
4867 if (job->completed_time && (!ra || cupsArrayFind(ra, "time-at-completed")))
4868 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-completed", (int)job->completed_time);
4870 if (job->creation_time && (!ra || cupsArrayFind(ra, "time-at-creation")))
4871 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation", (int)job->creation_time);
4877 * 'copy_printer_attrs()' - Copy printer attributes.
4882 cupsd_client_t *con, /* I - Client connection */
4883 cupsd_printer_t *printer, /* I - Printer */
4884 cups_array_t *ra) /* I - Requested attributes array */
4886 char printer_uri[HTTP_MAX_URI];
4888 char printer_icons[HTTP_MAX_URI];
4890 time_t curtime; /* Current time */
4891 int i; /* Looping var */
4895 * Copy the printer attributes to the response using requested-attributes
4896 * and document-format attributes that may be provided by the client.
4899 _cupsRWLockRead(&printer->lock);
4901 curtime = time(NULL);
4903 if (!ra || cupsArrayFind(ra, "marker-change-time"))
4904 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4905 "marker-change-time", printer->marker_time);
4907 if (printer->num_printers > 0 &&
4908 (!ra || cupsArrayFind(ra, "member-uris")))
4910 ipp_attribute_t *member_uris; /* member-uris attribute */
4911 cupsd_printer_t *p2; /* Printer in class */
4912 ipp_attribute_t *p2_uri; /* printer-uri-supported for class printer */
4915 if ((member_uris = ippAddStrings(con->response, IPP_TAG_PRINTER,
4916 IPP_TAG_URI, "member-uris",
4917 printer->num_printers, NULL,
4920 for (i = 0; i < printer->num_printers; i ++)
4922 p2 = printer->printers[i];
4924 if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported",
4925 IPP_TAG_URI)) != NULL)
4926 member_uris->values[i].string.text =
4927 _cupsStrRetain(p2_uri->values[0].string.text);
4930 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri,
4931 sizeof(printer_uri), "ipp", NULL, con->clientname,
4933 (p2->type & CUPS_PRINTER_CLASS) ?
4934 "/classes/%s" : "/printers/%s", p2->name);
4935 member_uris->values[i].string.text = _cupsStrAlloc(printer_uri);
4941 if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
4942 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
4943 "printer-alert", NULL, printer->alert);
4945 if (printer->alert_description &&
4946 (!ra || cupsArrayFind(ra, "printer-alert-description")))
4947 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4948 "printer-alert-description", NULL,
4949 printer->alert_description);
4951 if (!ra || cupsArrayFind(ra, "printer-config-change-date-time"))
4952 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-config-change-date-time", ippTimeToDate(printer->config_time));
4954 if (!ra || cupsArrayFind(ra, "printer-config-change-time"))
4955 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4956 "printer-config-change-time", printer->config_time);
4958 if (!ra || cupsArrayFind(ra, "printer-current-time"))
4959 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
4960 ippTimeToDate(curtime));
4962 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4963 if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
4965 if (printer->reg_name)
4966 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4967 "printer-dns-sd-name", NULL, printer->reg_name);
4969 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
4970 "printer-dns-sd-name", 0);
4972 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4974 if (!ra || cupsArrayFind(ra, "printer-error-policy"))
4975 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
4976 "printer-error-policy", NULL, printer->error_policy);
4978 if (!ra || cupsArrayFind(ra, "printer-error-policy-supported"))
4980 static const char * const errors[] =/* printer-error-policy-supported values */
4983 "retry-current-job",
4988 if (printer->type & CUPS_PRINTER_CLASS)
4989 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4990 "printer-error-policy-supported", NULL, "retry-current-job");
4992 ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
4993 "printer-error-policy-supported",
4994 sizeof(errors) / sizeof(errors[0]), NULL, errors);
4997 if (!ra || cupsArrayFind(ra, "printer-icons"))
4999 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_icons, sizeof(printer_icons),
5000 "http", NULL, con->clientname, con->clientport,
5001 "/icons/%s.png", printer->name);
5002 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-icons",
5003 NULL, printer_icons);
5004 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-icons=\"%s\"", printer_icons);
5007 if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
5008 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting);
5010 if (!ra || cupsArrayFind(ra, "printer-is-shared"))
5011 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared", (char)printer->shared);
5013 if (!ra || cupsArrayFind(ra, "printer-is-temporary"))
5014 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-temporary", (char)printer->temporary);
5016 if (!ra || cupsArrayFind(ra, "printer-more-info"))
5018 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5019 "http", NULL, con->clientname, con->clientport,
5020 (printer->type & CUPS_PRINTER_CLASS) ?
5021 "/classes/%s" : "/printers/%s", printer->name);
5022 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
5023 "printer-more-info", NULL, printer_uri);
5026 if (!ra || cupsArrayFind(ra, "printer-op-policy"))
5027 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
5028 "printer-op-policy", NULL, printer->op_policy);
5030 if (!ra || cupsArrayFind(ra, "printer-state"))
5031 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
5034 if (!ra || cupsArrayFind(ra, "printer-state-change-date-time"))
5035 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-state-change-date-time", ippTimeToDate(printer->state_time));
5037 if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
5038 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5039 "printer-state-change-time", printer->state_time);
5041 if (!ra || cupsArrayFind(ra, "printer-state-message"))
5042 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5043 "printer-state-message", NULL, printer->state_message);
5045 if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
5046 add_printer_state_reasons(con, printer);
5048 if (!ra || cupsArrayFind(ra, "printer-type"))
5050 cups_ptype_t type; /* printer-type value */
5053 * Add the CUPS-specific printer-type attribute...
5056 type = printer->type;
5058 if (printer == DefaultPrinter)
5059 type |= CUPS_PRINTER_DEFAULT;
5061 if (!printer->accepting)
5062 type |= CUPS_PRINTER_REJECTING;
5064 if (!printer->shared)
5065 type |= CUPS_PRINTER_NOT_SHARED;
5067 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type", (int)type);
5070 if (!ra || cupsArrayFind(ra, "printer-up-time"))
5071 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5072 "printer-up-time", curtime);
5074 if (!ra || cupsArrayFind(ra, "printer-uri-supported"))
5076 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5077 "ipp", NULL, con->clientname, con->clientport,
5078 (printer->type & CUPS_PRINTER_CLASS) ?
5079 "/classes/%s" : "/printers/%s", printer->name);
5080 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
5081 "printer-uri-supported", NULL, printer_uri);
5082 cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
5086 if (!ra || cupsArrayFind(ra, "queued-job-count"))
5087 add_queued_job_count(con, printer);
5089 copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0, NULL);
5090 if (printer->ppd_attrs)
5091 copy_attrs(con->response, printer->ppd_attrs, ra, IPP_TAG_ZERO, 0, NULL);
5092 copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY, NULL);
5094 _cupsRWUnlock(&printer->lock);
5099 * 'copy_subscription_attrs()' - Copy subscription attributes.
5103 copy_subscription_attrs(
5104 cupsd_client_t *con, /* I - Client connection */
5105 cupsd_subscription_t *sub, /* I - Subscription */
5106 cups_array_t *ra, /* I - Requested attributes array */
5107 cups_array_t *exclude) /* I - Private attributes array */
5109 ipp_attribute_t *attr; /* Current attribute */
5110 char printer_uri[HTTP_MAX_URI];
5112 int count; /* Number of events */
5113 unsigned mask; /* Current event mask */
5114 const char *name; /* Current event name */
5117 cupsdLogMessage(CUPSD_LOG_DEBUG2,
5118 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5119 con, sub, ra, exclude);
5122 * Copy the subscription attributes to the response using the
5123 * requested-attributes attribute that may be provided by the client.
5126 if (!exclude || !cupsArrayFind(exclude, "all"))
5128 if ((!exclude || !cupsArrayFind(exclude, "notify-events")) &&
5129 (!ra || cupsArrayFind(ra, "notify-events")))
5131 cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_subscription_attrs: notify-events");
5133 if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
5136 * Simple event list...
5139 ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
5140 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5141 "notify-events", NULL, name);
5146 * Complex event list...
5149 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5150 if (sub->mask & mask)
5153 attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
5154 (ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
5155 "notify-events", count, NULL, NULL);
5157 for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
5158 if (sub->mask & mask)
5160 attr->values[count].string.text =
5161 (char *)cupsdEventName((cupsd_eventmask_t)mask);
5168 if ((!exclude || !cupsArrayFind(exclude, "notify-lease-duration")) &&
5169 (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration"))))
5170 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5171 "notify-lease-duration", sub->lease);
5173 if ((!exclude || !cupsArrayFind(exclude, "notify-recipient-uri")) &&
5174 (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri"))))
5175 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5176 "notify-recipient-uri", NULL, sub->recipient);
5177 else if ((!exclude || !cupsArrayFind(exclude, "notify-pull-method")) &&
5178 (!ra || cupsArrayFind(ra, "notify-pull-method")))
5179 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
5180 "notify-pull-method", NULL, "ippget");
5182 if ((!exclude || !cupsArrayFind(exclude, "notify-subscriber-user-name")) &&
5183 (!ra || cupsArrayFind(ra, "notify-subscriber-user-name")))
5184 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
5185 "notify-subscriber-user-name", NULL, sub->owner);
5187 if ((!exclude || !cupsArrayFind(exclude, "notify-time-interval")) &&
5188 (!ra || cupsArrayFind(ra, "notify-time-interval")))
5189 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5190 "notify-time-interval", sub->interval);
5192 if (sub->user_data_len > 0 &&
5193 (!exclude || !cupsArrayFind(exclude, "notify-user-data")) &&
5194 (!ra || cupsArrayFind(ra, "notify-user-data")))
5195 ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
5196 sub->user_data, sub->user_data_len);
5199 if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
5200 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5201 "notify-job-id", sub->job->id);
5203 if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
5205 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
5206 "ipp", NULL, con->clientname, con->clientport,
5207 "/printers/%s", sub->dest->name);
5208 ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
5209 "notify-printer-uri", NULL, printer_uri);
5212 if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
5213 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5214 "notify-subscription-id", sub->id);
5219 * 'create_job()' - Print a file to a printer or class.
5223 create_job(cupsd_client_t *con, /* I - Client connection */
5224 ipp_attribute_t *uri) /* I - Printer URI */
5226 int i; /* Looping var */
5227 cupsd_printer_t *printer; /* Printer */
5228 cupsd_job_t *job; /* New job */
5229 static const char * const forbidden_attrs[] =
5230 { /* List of forbidden attributes */
5234 "document-natural-language"
5238 cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
5239 con->number, uri->values[0].string.text);
5242 * Is the destination valid?
5245 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
5251 send_ipp_status(con, IPP_NOT_FOUND,
5252 _("The printer or class does not exist."));
5257 * Check for invalid Create-Job attributes and log a warning or error depending
5258 * on whether cupsd is running in "strict conformance" mode...
5262 i < (int)(sizeof(forbidden_attrs) / sizeof(forbidden_attrs[0]));
5264 if (ippFindAttribute(con->request, forbidden_attrs[i], IPP_TAG_ZERO))
5266 if (StrictConformance)
5268 send_ipp_status(con, IPP_BAD_REQUEST,
5269 _("The '%s' operation attribute cannot be supplied in a "
5270 "Create-Job request."), forbidden_attrs[i]);
5274 cupsdLogMessage(CUPSD_LOG_WARN,
5275 "Unexpected '%s' operation attribute in a Create-Job "
5276 "request.", forbidden_attrs[i]);
5280 * Create the job object...
5283 if ((job = add_job(con, printer, NULL)) == NULL)
5286 job->pending_timeout = 1;
5289 * Save and log the job...
5292 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
5293 job->dest, job->username);
5298 * 'create_local_bg_thread()' - Background thread for creating a local print queue.
5301 static void * /* O - Exit status */
5302 create_local_bg_thread(
5303 cupsd_printer_t *printer) /* I - Printer */
5305 cups_file_t *from, /* Source file */
5306 *to; /* Destination file */
5307 char fromppd[1024], /* Source PPD */
5308 toppd[1024], /* Destination PPD */
5309 scheme[32], /* URI scheme */
5310 userpass[256], /* User:pass */
5311 host[256], /* Hostname */
5312 resource[1024], /* Resource path */
5313 line[1024]; /* Line from PPD */
5314 int port; /* Port number */
5315 http_encryption_t encryption; /* Type of encryption to use */
5316 http_t *http; /* Connection to printer */
5317 ipp_t *request, /* Request to printer */
5318 *response; /* Response from printer */
5319 ipp_attribute_t *attr; /* Attribute in response */
5323 * Try connecting to the printer...
5326 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Generating PPD file from \"%s\"...", printer->name, printer->device_uri);
5328 if (httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
5330 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Bad device URI \"%s\".", printer->name, printer->device_uri);
5334 if (!strcmp(scheme, "ipps") || port == 443)
5335 encryption = HTTP_ENCRYPTION_ALWAYS;
5337 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
5339 if ((http = httpConnect2(host, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL)) == NULL)
5341 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to connect to %s:%d: %s", printer->name, host, port, cupsLastErrorString());
5346 * Query the printer for its capabilities...
5349 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer->name, host, port);
5351 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
5352 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer->device_uri);
5353 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "all");
5355 response = cupsDoRequest(http, request, resource);
5357 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s: Get-Printer-Attributes returned %s", printer->name, ippErrorString(cupsLastError()));
5359 // TODO: Grab printer icon file...
5363 * Write the PPD for the queue...
5366 if (_ppdCreateFromIPP(fromppd, sizeof(fromppd), response))
5368 if ((!printer->info || !*(printer->info)) && (attr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXT)) != NULL)
5369 cupsdSetString(&printer->info, ippGetString(attr, 0, NULL));
5371 if ((!printer->location || !*(printer->location)) && (attr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL)
5372 cupsdSetString(&printer->location, ippGetString(attr, 0, NULL));
5374 if ((!printer->geo_location || !*(printer->geo_location)) && (attr = ippFindAttribute(response, "printer-geo-location", IPP_TAG_URI)) != NULL)
5375 cupsdSetString(&printer->geo_location, ippGetString(attr, 0, NULL));
5377 if ((from = cupsFileOpen(fromppd, "r")) == NULL)
5379 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to read generated PPD: %s", printer->name, strerror(errno));
5383 snprintf(toppd, sizeof(toppd), "%s/ppd/%s.ppd", ServerRoot, printer->name);
5384 if ((to = cupsdCreateConfFile(toppd, ConfigFilePerm)) == NULL)
5386 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: Unable to create PPD for printer: %s", printer->name, strerror(errno));
5387 cupsFileClose(from);
5391 while (cupsFileGets(from, line, sizeof(line)))
5392 cupsFilePrintf(to, "%s\n", line);
5394 cupsFileClose(from);
5395 if (!cupsdCloseCreatedConfFile(to, toppd))
5397 printer->config_time = time(NULL);
5398 printer->state = IPP_PSTATE_IDLE;
5399 printer->accepting = 1;
5401 cupsdSetPrinterAttrs(printer);
5403 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL, "Printer \"%s\" is now available.", printer->name);
5404 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" is now available.", printer->name);
5408 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: PPD creation failed: %s", printer->name, cupsLastErrorString());
5415 * 'create_local_printer()' - Create a local (temporary) print queue.
5419 create_local_printer(
5420 cupsd_client_t *con) /* I - Client connection */
5422 ipp_attribute_t *device_uri, /* device-uri attribute */
5423 *printer_geo_location, /* printer-geo-location attribute */
5424 *printer_info, /* printer-info attribute */
5425 *printer_location, /* printer-location attribute */
5426 *printer_name; /* printer-name attribute */
5427 cupsd_printer_t *printer; /* New printer */
5428 http_status_t status; /* Policy status */
5429 char name[128], /* Sanitized printer name */
5430 *nameptr, /* Pointer into name */
5431 uri[1024]; /* printer-uri-supported value */
5432 const char *ptr; /* Pointer into attribute value */
5436 * Require local access to create a local printer...
5439 if (!httpAddrLocalhost(httpGetAddress(con->http)))
5441 send_ipp_status(con, IPP_STATUS_ERROR_FORBIDDEN, _("Only local users can create a local printer."));
5446 * Check any other policy limits...
5449 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5451 send_http_error(con, status, NULL);
5456 * Grab needed attributes...
5459 if ((printer_name = ippFindAttribute(con->request, "printer-name", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(printer_name) != IPP_TAG_PRINTER || ippGetValueTag(printer_name) != IPP_TAG_NAME)
5462 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "printer-name");
5463 else if (ippGetGroupTag(printer_name) != IPP_TAG_PRINTER)
5464 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "printer-name");
5466 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "printer-name");
5471 for (nameptr = name, ptr = ippGetString(printer_name, 0, NULL); *ptr && nameptr < (name + sizeof(name) - 1); ptr ++)
5474 * Sanitize the printer name...
5477 if (_cups_isalnum(*ptr))
5479 else if (nameptr == name || nameptr[-1] != '_')
5485 if ((device_uri = ippFindAttribute(con->request, "device-uri", IPP_TAG_ZERO)) == NULL || ippGetGroupTag(device_uri) != IPP_TAG_PRINTER || ippGetValueTag(device_uri) != IPP_TAG_URI)
5488 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Missing required attribute \"%s\"."), "device-uri");
5489 else if (ippGetGroupTag(device_uri) != IPP_TAG_PRINTER)
5490 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is in the wrong group."), "device-uri");
5492 send_ipp_status(con, IPP_STATUS_ERROR_BAD_REQUEST, _("Attribute \"%s\" is the wrong value type."), "device-uri");
5497 printer_geo_location = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI);
5498 printer_info = ippFindAttribute(con->request, "printer-info", IPP_TAG_TEXT);
5499 printer_location = ippFindAttribute(con->request, "printer-location", IPP_TAG_TEXT);
5502 * See if the printer already exists...
5505 if ((printer = cupsdFindDest(name)) != NULL)
5507 send_ipp_status(con, IPP_STATUS_ERROR_NOT_POSSIBLE, _("Printer \"%s\" already exists."), name);
5508 goto add_printer_attributes;
5512 * Create the printer...
5515 if ((printer = cupsdAddPrinter(name)) == NULL)
5517 send_ipp_status(con, IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer."));
5521 printer->shared = 0;
5522 printer->temporary = 1;
5524 cupsdSetDeviceURI(printer, ippGetString(device_uri, 0, NULL));
5526 if (printer_geo_location)
5527 cupsdSetString(&printer->geo_location, ippGetString(printer_geo_location, 0, NULL));
5529 cupsdSetString(&printer->info, ippGetString(printer_info, 0, NULL));
5530 if (printer_location)
5531 cupsdSetString(&printer->location, ippGetString(printer_location, 0, NULL));
5533 cupsdSetPrinterAttrs(printer);
5536 * Run a background thread to create the PPD...
5539 _cupsThreadCreate((_cups_thread_func_t)create_local_bg_thread, printer);
5542 * Return printer attributes...
5545 send_ipp_status(con, IPP_STATUS_OK, _("Local printer created."));
5547 add_printer_attributes:
5549 ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs", (char)printer->accepting);
5550 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", printer->state);
5551 add_printer_state_reasons(con, printer);
5553 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), httpIsEncrypted(con->http) ? "ipps" : "ipp", NULL, con->clientname, con->clientport, "/printers/%s", printer->name);
5554 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported", NULL, uri);
5559 * 'create_requested_array()' - Create an array for the requested-attributes.
5562 static cups_array_t * /* O - Array of attributes or NULL */
5563 create_requested_array(ipp_t *request) /* I - IPP request */
5565 cups_array_t *ra; /* Requested attributes array */
5569 * Create the array for standard attributes...
5572 ra = ippCreateRequestedArray(request);
5575 * Add CUPS defaults as needed...
5578 if (cupsArrayFind(ra, "printer-defaults"))
5581 * Include user-set defaults...
5584 char *name; /* Option name */
5586 cupsArrayRemove(ra, "printer-defaults");
5588 for (name = (char *)cupsArrayFirst(CommonDefaults);
5590 name = (char *)cupsArrayNext(CommonDefaults))
5591 if (!cupsArrayFind(ra, name))
5592 cupsArrayAdd(ra, name);
5600 * 'create_subscriptions()' - Create one or more notification subscriptions.
5604 create_subscriptions(
5605 cupsd_client_t *con, /* I - Client connection */
5606 ipp_attribute_t *uri) /* I - Printer URI */
5608 http_status_t status; /* Policy status */
5609 int i; /* Looping var */
5610 ipp_attribute_t *attr; /* Current attribute */
5611 cups_ptype_t dtype; /* Destination type (printer/class) */
5612 char scheme[HTTP_MAX_URI],
5613 /* Scheme portion of URI */
5614 userpass[HTTP_MAX_URI],
5615 /* Username portion of URI */
5617 /* Host portion of URI */
5618 resource[HTTP_MAX_URI];
5619 /* Resource portion of URI */
5620 int port; /* Port portion of URI */
5621 cupsd_printer_t *printer; /* Printer/class */
5622 cupsd_job_t *job; /* Job */
5623 int jobid; /* Job ID */
5624 cupsd_subscription_t *sub; /* Subscription object */
5625 const char *username, /* requesting-user-name or
5626 authenticated username */
5627 *recipient, /* notify-recipient-uri */
5628 *pullmethod; /* notify-pull-method */
5629 ipp_attribute_t *user_data; /* notify-user-data */
5630 int interval, /* notify-time-interval */
5631 lease; /* notify-lease-duration */
5632 unsigned mask; /* notify-events */
5633 ipp_attribute_t *notify_events,/* notify-events(-default) */
5634 *notify_lease; /* notify-lease-duration(-default) */
5638 for (attr = con->request->attrs; attr; attr = attr->next)
5640 if (attr->group_tag != IPP_TAG_ZERO)
5641 cupsdLogMessage(CUPSD_LOG_DEBUG2, "g%04x v%04x %s", attr->group_tag,
5642 attr->value_tag, attr->name);
5644 cupsdLogMessage(CUPSD_LOG_DEBUG2, "----SEP----");
5649 * Is the destination valid?
5652 cupsdLogMessage(CUPSD_LOG_DEBUG, "create_subscriptions(con=%p(%d), uri=\"%s\")", con, con->number, uri->values[0].string.text);
5654 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
5655 sizeof(scheme), userpass, sizeof(userpass), host,
5656 sizeof(host), &port, resource, sizeof(resource));
5658 if (!strcmp(resource, "/"))
5660 dtype = (cups_ptype_t)0;
5663 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
5665 dtype = (cups_ptype_t)0;
5668 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
5670 dtype = CUPS_PRINTER_CLASS;
5673 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5679 send_ipp_status(con, IPP_NOT_FOUND,
5680 _("The printer or class does not exist."));
5690 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
5693 send_http_error(con, status, printer);
5697 else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
5699 send_http_error(con, status, NULL);
5704 * Get the user that is requesting the subscription...
5707 username = get_username(con);
5710 * Find the first subscription group attribute; return if we have
5714 for (attr = con->request->attrs; attr; attr = attr->next)
5715 if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
5720 send_ipp_status(con, IPP_BAD_REQUEST,
5721 _("No subscription attributes in request."));
5726 * Process the subscription attributes in the request...
5729 con->response->request.status.status_code = IPP_BAD_REQUEST;
5737 lease = DefaultLeaseDuration;
5739 mask = CUPSD_EVENT_NONE;
5743 notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
5745 notify_lease = ippFindAttribute(printer->attrs,
5746 "notify-lease-duration-default",
5750 lease = notify_lease->values[0].integer;
5754 notify_events = NULL;
5755 notify_lease = NULL;
5758 while (attr && attr->group_tag != IPP_TAG_ZERO)
5760 if (!strcmp(attr->name, "notify-recipient-uri") &&
5761 attr->value_tag == IPP_TAG_URI)
5764 * Validate the recipient scheme against the ServerBin/notifier
5768 char notifier[1024]; /* Notifier filename */
5771 recipient = attr->values[0].string.text;
5773 if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
5774 scheme, sizeof(scheme), userpass, sizeof(userpass),
5775 host, sizeof(host), &port,
5776 resource, sizeof(resource)) < HTTP_URI_OK)
5778 send_ipp_status(con, IPP_NOT_POSSIBLE,
5779 _("Bad notify-recipient-uri \"%s\"."), recipient);
5780 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5781 "notify-status-code", IPP_URI_SCHEME);
5785 snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
5787 if (access(notifier, X_OK))
5789 send_ipp_status(con, IPP_NOT_POSSIBLE,
5790 _("notify-recipient-uri URI \"%s\" uses unknown "
5791 "scheme."), recipient);
5792 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5793 "notify-status-code", IPP_URI_SCHEME);
5797 if (!strcmp(scheme, "rss") && !check_rss_recipient(recipient))
5799 send_ipp_status(con, IPP_NOT_POSSIBLE,
5800 _("notify-recipient-uri URI \"%s\" is already used."),
5802 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5803 "notify-status-code", IPP_ATTRIBUTES);
5807 else if (!strcmp(attr->name, "notify-pull-method") &&
5808 attr->value_tag == IPP_TAG_KEYWORD)
5810 pullmethod = attr->values[0].string.text;
5812 if (strcmp(pullmethod, "ippget"))
5814 send_ipp_status(con, IPP_NOT_POSSIBLE,
5815 _("Bad notify-pull-method \"%s\"."), pullmethod);
5816 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
5817 "notify-status-code", IPP_ATTRIBUTES);
5821 else if (!strcmp(attr->name, "notify-charset") &&
5822 attr->value_tag == IPP_TAG_CHARSET &&
5823 strcmp(attr->values[0].string.text, "us-ascii") &&
5824 strcmp(attr->values[0].string.text, "utf-8"))
5826 send_ipp_status(con, IPP_CHARSET,
5827 _("Character set \"%s\" not supported."),
5828 attr->values[0].string.text);
5831 else if (!strcmp(attr->name, "notify-natural-language") &&
5832 (attr->value_tag != IPP_TAG_LANGUAGE ||
5833 strcmp(attr->values[0].string.text, DefaultLanguage)))
5835 send_ipp_status(con, IPP_CHARSET,
5836 _("Language \"%s\" not supported."),
5837 attr->values[0].string.text);
5840 else if (!strcmp(attr->name, "notify-user-data") &&
5841 attr->value_tag == IPP_TAG_STRING)
5843 if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
5845 send_ipp_status(con, IPP_REQUEST_VALUE,
5846 _("The notify-user-data value is too large "
5847 "(%d > 63 octets)."),
5848 attr->values[0].unknown.length);
5854 else if (!strcmp(attr->name, "notify-events") &&
5855 attr->value_tag == IPP_TAG_KEYWORD)
5856 notify_events = attr;
5857 else if (!strcmp(attr->name, "notify-lease-duration") &&
5858 attr->value_tag == IPP_TAG_INTEGER)
5859 lease = attr->values[0].integer;
5860 else if (!strcmp(attr->name, "notify-time-interval") &&
5861 attr->value_tag == IPP_TAG_INTEGER)
5862 interval = attr->values[0].integer;
5863 else if (!strcmp(attr->name, "notify-job-id") &&
5864 attr->value_tag == IPP_TAG_INTEGER)
5865 jobid = attr->values[0].integer;
5872 for (i = 0; i < notify_events->num_values; i ++)
5873 mask |= cupsdEventValue(notify_events->values[i].string.text);
5877 cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
5879 cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
5880 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
5881 cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
5883 if (!recipient && !pullmethod)
5886 if (mask == CUPSD_EVENT_NONE)
5889 mask = CUPSD_EVENT_JOB_COMPLETED;
5891 mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
5894 send_ipp_status(con, IPP_BAD_REQUEST,
5895 _("notify-events not specified."));
5900 if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
5902 cupsdLogMessage(CUPSD_LOG_INFO,
5903 "create_subscriptions: Limiting notify-lease-duration to "
5906 lease = MaxLeaseDuration;
5911 if ((job = cupsdFindJob(jobid)) == NULL)
5913 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
5921 if ((sub = cupsdAddSubscription(mask, printer, job, recipient, 0)) == NULL)
5923 send_ipp_status(con, IPP_TOO_MANY_SUBSCRIPTIONS,
5924 _("There are too many subscriptions."));
5929 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for job %d.",
5932 cupsdLogMessage(CUPSD_LOG_DEBUG,
5933 "Added subscription #%d for printer \"%s\".",
5934 sub->id, printer->name);
5936 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription #%d for server.",
5939 sub->interval = interval;
5941 sub->expire = lease ? time(NULL) + lease : 0;
5943 cupsdSetString(&sub->owner, username);
5947 sub->user_data_len = user_data->values[0].unknown.length;
5948 memcpy(sub->user_data, user_data->values[0].unknown.data,
5949 (size_t)sub->user_data_len);
5952 ippAddSeparator(con->response);
5953 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
5954 "notify-subscription-id", sub->id);
5956 con->response->request.status.status_code = IPP_OK;
5962 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
5967 * 'delete_printer()' - Remove a printer or class from the system.
5971 delete_printer(cupsd_client_t *con, /* I - Client connection */
5972 ipp_attribute_t *uri) /* I - URI of printer or class */
5974 http_status_t status; /* Policy status */
5975 cups_ptype_t dtype; /* Destination type (printer/class) */
5976 cupsd_printer_t *printer; /* Printer/class */
5977 char filename[1024]; /* Script/PPD filename */
5978 int temporary; /* Temporary queue? */
5981 cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
5982 con->number, uri->values[0].string.text);
5985 * Do we have a valid URI?
5988 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
5994 send_ipp_status(con, IPP_NOT_FOUND,
5995 _("The printer or class does not exist."));
6003 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6005 send_http_error(con, status, NULL);
6010 * Remove old jobs...
6013 cupsdCancelJobs(printer->name, NULL, 1);
6016 * Remove old subscriptions and send a "deleted printer" event...
6019 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
6020 "%s \"%s\" deleted by \"%s\".",
6021 (dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
6022 printer->name, get_username(con));
6024 cupsdExpireSubscriptions(printer, NULL);
6027 * Remove any old PPD or script files...
6030 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
6033 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd.O", ServerRoot,
6037 snprintf(filename, sizeof(filename), "%s/%s.png", CacheDir, printer->name);
6040 snprintf(filename, sizeof(filename), "%s/%s.data", CacheDir, printer->name);
6044 * Unregister color profiles...
6047 cupsdUnregisterColor(printer);
6049 temporary = printer->temporary;
6051 if (dtype & CUPS_PRINTER_CLASS)
6053 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
6054 printer->name, get_username(con));
6056 cupsdDeletePrinter(printer, 0);
6058 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6062 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
6063 printer->name, get_username(con));
6065 if (cupsdDeletePrinter(printer, 0) && !temporary)
6066 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
6069 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
6073 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
6076 * Return with no errors...
6079 con->response->request.status.status_code = IPP_OK;
6084 * 'get_default()' - Get the default destination.
6088 get_default(cupsd_client_t *con) /* I - Client connection */
6090 http_status_t status; /* Policy status */
6091 cups_array_t *ra; /* Requested attributes array */
6094 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->number);
6100 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6102 send_http_error(con, status, NULL);
6108 ra = create_requested_array(con->request);
6110 copy_printer_attrs(con, DefaultPrinter, ra);
6112 cupsArrayDelete(ra);
6114 con->response->request.status.status_code = IPP_OK;
6117 send_ipp_status(con, IPP_NOT_FOUND, _("No default printer."));
6122 * 'get_devices()' - Get the list of available devices on the local system.
6126 get_devices(cupsd_client_t *con) /* I - Client connection */
6128 http_status_t status; /* Policy status */
6129 ipp_attribute_t *limit, /* limit attribute */
6130 *timeout, /* timeout attribute */
6131 *requested, /* requested-attributes attribute */
6132 *exclude, /* exclude-schemes attribute */
6133 *include; /* include-schemes attribute */
6134 char command[1024], /* cups-deviced command */
6135 options[2048], /* Options to pass to command */
6137 /* String for requested attributes */
6139 /* String for excluded schemes */
6141 /* String for included schemes */
6144 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->number);
6150 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
6152 send_http_error(con, status, NULL);
6157 * Run cups-deviced command with the given options...
6160 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
6161 timeout = ippFindAttribute(con->request, "timeout", IPP_TAG_INTEGER);
6162 requested = ippFindAttribute(con->request, "requested-attributes",
6164 exclude = ippFindAttribute(con->request, "exclude-schemes", IPP_TAG_NAME);
6165 include = ippFindAttribute(con->request, "include-schemes", IPP_TAG_NAME);
6168 url_encode_attr(requested, requested_str, sizeof(requested_str));
6170 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
6173 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
6175 exclude_str[0] = '\0';
6178 url_encode_attr(include, include_str, sizeof(include_str));
6180 include_str[0] = '\0';
6182 snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
6183 snprintf(options, sizeof(options),
6184 "%d+%d+%d+%d+%s%s%s%s%s",
6185 con->request->request.op.request_id,
6186 limit ? limit->values[0].integer : 0,
6187 timeout ? timeout->values[0].integer : 15,
6190 exclude_str[0] ? "%20" : "", exclude_str,
6191 include_str[0] ? "%20" : "", include_str);
6193 if (cupsdSendCommand(con, command, options, 1))
6196 * Command started successfully, don't send an IPP response here...
6199 ippDelete(con->response);
6200 con->response = NULL;
6205 * Command failed, return "internal error" so the user knows something
6209 send_ipp_status(con, IPP_INTERNAL_ERROR,
6210 _("cups-deviced failed to execute."));
6216 * 'get_document()' - Get a copy of a job file.
6220 get_document(cupsd_client_t *con, /* I - Client connection */
6221 ipp_attribute_t *uri) /* I - Job URI */
6223 http_status_t status; /* Policy status */
6224 ipp_attribute_t *attr; /* Current attribute */
6225 int jobid; /* Job ID */
6226 int docnum; /* Document number */
6227 cupsd_job_t *job; /* Current job */
6228 char scheme[HTTP_MAX_URI], /* Method portion of URI */
6229 username[HTTP_MAX_URI], /* Username portion of URI */
6230 host[HTTP_MAX_URI], /* Host portion of URI */
6231 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6232 int port; /* Port portion of URI */
6233 char filename[1024], /* Filename for document */
6234 format[1024]; /* Format for document */
6237 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_document(%p[%d], %s)", con,
6238 con->number, uri->values[0].string.text);
6241 * See if we have a job URI or a printer URI...
6244 if (!strcmp(uri->name, "printer-uri"))
6247 * Got a printer URI; see if we also have a job-id attribute...
6250 if ((attr = ippFindAttribute(con->request, "job-id",
6251 IPP_TAG_INTEGER)) == NULL)
6253 send_ipp_status(con, IPP_BAD_REQUEST,
6254 _("Got a printer-uri attribute but no job-id."));
6258 jobid = attr->values[0].integer;
6263 * Got a job URI; parse it to get the job ID...
6266 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6267 sizeof(scheme), username, sizeof(username), host,
6268 sizeof(host), &port, resource, sizeof(resource));
6270 if (strncmp(resource, "/jobs/", 6))
6276 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
6277 uri->values[0].string.text);
6281 jobid = atoi(resource + 6);
6285 * See if the job exists...
6288 if ((job = cupsdFindJob(jobid)) == NULL)
6291 * Nope - return a "not found" error...
6294 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
6302 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con,
6303 job->username)) != HTTP_OK)
6305 send_http_error(con, status, NULL);
6310 * Get the document number...
6313 if ((attr = ippFindAttribute(con->request, "document-number",
6314 IPP_TAG_INTEGER)) == NULL)
6316 send_ipp_status(con, IPP_BAD_REQUEST,
6317 _("Missing document-number attribute."));
6321 if ((docnum = attr->values[0].integer) < 1 || docnum > job->num_files ||
6322 attr->num_values > 1)
6324 send_ipp_status(con, IPP_NOT_FOUND,
6325 _("Document #%d does not exist in job #%d."), docnum,
6330 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, jobid,
6332 if ((con->file = open(filename, O_RDONLY)) == -1)
6334 cupsdLogMessage(CUPSD_LOG_ERROR,
6335 "Unable to open document %d in job %d - %s", docnum, jobid,
6337 send_ipp_status(con, IPP_NOT_FOUND,
6338 _("Unable to open document #%d in job #%d."), docnum,
6343 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
6347 snprintf(format, sizeof(format), "%s/%s", job->filetypes[docnum - 1]->super,
6348 job->filetypes[docnum - 1]->type);
6350 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
6352 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "document-number",
6354 if ((attr = ippFindAttribute(job->attrs, "document-name",
6355 IPP_TAG_NAME)) != NULL)
6356 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_NAME, "document-name",
6357 NULL, attr->values[0].string.text);
6362 * 'get_job_attrs()' - Get job attributes.
6366 get_job_attrs(cupsd_client_t *con, /* I - Client connection */
6367 ipp_attribute_t *uri) /* I - Job URI */
6369 http_status_t status; /* Policy status */
6370 ipp_attribute_t *attr; /* Current attribute */
6371 int jobid; /* Job ID */
6372 cupsd_job_t *job; /* Current job */
6373 cupsd_printer_t *printer; /* Current printer */
6374 cupsd_policy_t *policy; /* Current security policy */
6375 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6376 username[HTTP_MAX_URI], /* Username portion of URI */
6377 host[HTTP_MAX_URI], /* Host portion of URI */
6378 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6379 int port; /* Port portion of URI */
6380 cups_array_t *ra, /* Requested attributes array */
6381 *exclude; /* Private attributes array */
6384 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
6385 con->number, uri->values[0].string.text);
6388 * See if we have a job URI or a printer URI...
6391 if (!strcmp(uri->name, "printer-uri"))
6394 * Got a printer URI; see if we also have a job-id attribute...
6397 if ((attr = ippFindAttribute(con->request, "job-id",
6398 IPP_TAG_INTEGER)) == NULL)
6400 send_ipp_status(con, IPP_BAD_REQUEST,
6401 _("Got a printer-uri attribute but no job-id."));
6405 jobid = attr->values[0].integer;
6410 * Got a job URI; parse it to get the job ID...
6413 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6414 sizeof(scheme), username, sizeof(username), host,
6415 sizeof(host), &port, resource, sizeof(resource));
6417 if (strncmp(resource, "/jobs/", 6))
6423 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
6424 uri->values[0].string.text);
6428 jobid = atoi(resource + 6);
6432 * See if the job exists...
6435 if ((job = cupsdFindJob(jobid)) == NULL)
6438 * Nope - return a "not found" error...
6441 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
6449 if ((printer = job->printer) == NULL)
6450 printer = cupsdFindDest(job->dest);
6453 policy = printer->op_policy_ptr;
6455 policy = DefaultPolicyPtr;
6457 if ((status = cupsdCheckPolicy(policy, con, job->username)) != HTTP_OK)
6459 send_http_error(con, status, NULL);
6463 exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
6466 * Copy attributes...
6471 ra = create_requested_array(con->request);
6472 copy_job_attrs(con, job, ra, exclude);
6473 cupsArrayDelete(ra);
6475 con->response->request.status.status_code = IPP_OK;
6480 * 'get_jobs()' - Get a list of jobs for the specified printer.
6484 get_jobs(cupsd_client_t *con, /* I - Client connection */
6485 ipp_attribute_t *uri) /* I - Printer URI */
6487 http_status_t status; /* Policy status */
6488 ipp_attribute_t *attr; /* Current attribute */
6489 const char *dest; /* Destination */
6490 cups_ptype_t dtype; /* Destination type (printer/class) */
6491 cups_ptype_t dmask; /* Destination type mask */
6492 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
6493 username[HTTP_MAX_URI], /* Username portion of URI */
6494 host[HTTP_MAX_URI], /* Host portion of URI */
6495 resource[HTTP_MAX_URI]; /* Resource portion of URI */
6496 int port; /* Port portion of URI */
6497 int job_comparison; /* Job comparison */
6498 ipp_jstate_t job_state; /* job-state value */
6499 int first_job_id = 1, /* First job ID */
6500 first_index = 1, /* First index */
6501 limit = 0, /* Maximum number of jobs to return */
6502 count, /* Number of jobs that match */
6503 need_load_job = 0; /* Do we need to load the job? */
6504 const char *job_attr; /* Job attribute requested */
6505 ipp_attribute_t *job_ids; /* job-ids attribute */
6506 cupsd_job_t *job; /* Current job pointer */
6507 cupsd_printer_t *printer; /* Printer */
6508 cups_array_t *list; /* Which job list... */
6509 int delete_list = 0; /* Delete the list afterwards? */
6510 cups_array_t *ra, /* Requested attributes array */
6511 *exclude; /* Private attributes array */
6512 cupsd_policy_t *policy; /* Current policy */
6515 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->number,
6516 uri->values[0].string.text);
6519 * Is the destination valid?
6522 if (strcmp(uri->name, "printer-uri"))
6524 send_ipp_status(con, IPP_BAD_REQUEST, _("No printer-uri in request."));
6528 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
6529 sizeof(scheme), username, sizeof(username), host,
6530 sizeof(host), &port, resource, sizeof(resource));
6532 if (!strcmp(resource, "/") || !strcmp(resource, "/jobs"))
6535 dtype = (cups_ptype_t)0;
6536 dmask = (cups_ptype_t)0;
6539 else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
6542 dtype = (cups_ptype_t)0;
6543 dmask = CUPS_PRINTER_CLASS;
6546 else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
6549 dtype = CUPS_PRINTER_CLASS;
6550 dmask = CUPS_PRINTER_CLASS;
6553 else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
6560 send_ipp_status(con, IPP_NOT_FOUND,
6561 _("The printer or class does not exist."));
6566 dtype &= CUPS_PRINTER_CLASS;
6567 dmask = CUPS_PRINTER_CLASS;
6575 policy = printer->op_policy_ptr;
6577 policy = DefaultPolicyPtr;
6579 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
6581 send_http_error(con, status, NULL);
6585 job_ids = ippFindAttribute(con->request, "job-ids", IPP_TAG_INTEGER);
6588 * See if the "which-jobs" attribute have been specified...
6591 if ((attr = ippFindAttribute(con->request, "which-jobs",
6592 IPP_TAG_KEYWORD)) != NULL && job_ids)
6594 send_ipp_status(con, IPP_CONFLICT,
6595 _("The %s attribute cannot be provided with job-ids."),
6599 else if (!attr || !strcmp(attr->values[0].string.text, "not-completed"))
6601 job_comparison = -1;
6602 job_state = IPP_JOB_STOPPED;
6605 else if (!strcmp(attr->values[0].string.text, "completed"))
6608 job_state = IPP_JOB_CANCELED;
6609 list = cupsdGetCompletedJobs(printer);
6612 else if (!strcmp(attr->values[0].string.text, "aborted"))
6615 job_state = IPP_JOB_ABORTED;
6616 list = cupsdGetCompletedJobs(printer);
6619 else if (!strcmp(attr->values[0].string.text, "all"))
6622 job_state = IPP_JOB_PENDING;
6625 else if (!strcmp(attr->values[0].string.text, "canceled"))
6628 job_state = IPP_JOB_CANCELED;
6629 list = cupsdGetCompletedJobs(printer);
6632 else if (!strcmp(attr->values[0].string.text, "pending"))
6635 job_state = IPP_JOB_PENDING;
6638 else if (!strcmp(attr->values[0].string.text, "pending-held"))
6641 job_state = IPP_JOB_HELD;
6644 else if (!strcmp(attr->values[0].string.text, "processing"))
6647 job_state = IPP_JOB_PROCESSING;
6648 list = PrintingJobs;
6650 else if (!strcmp(attr->values[0].string.text, "processing-stopped"))
6653 job_state = IPP_JOB_STOPPED;
6658 send_ipp_status(con, IPP_ATTRIBUTES,
6659 _("The which-jobs value \"%s\" is not supported."),
6660 attr->values[0].string.text);
6661 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
6662 "which-jobs", NULL, attr->values[0].string.text);
6667 * See if they want to limit the number of jobs reported...
6670 if ((attr = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER)) != NULL)
6674 send_ipp_status(con, IPP_CONFLICT,
6675 _("The %s attribute cannot be provided with job-ids."),
6680 limit = attr->values[0].integer;
6683 if ((attr = ippFindAttribute(con->request, "first-index", IPP_TAG_INTEGER)) != NULL)
6687 send_ipp_status(con, IPP_CONFLICT,
6688 _("The %s attribute cannot be provided with job-ids."),
6693 first_index = attr->values[0].integer;
6695 else if ((attr = ippFindAttribute(con->request, "first-job-id", IPP_TAG_INTEGER)) != NULL)
6699 send_ipp_status(con, IPP_CONFLICT,
6700 _("The %s attribute cannot be provided with job-ids."),
6705 first_job_id = attr->values[0].integer;
6709 * See if we only want to see jobs for a specific user...
6712 if ((attr = ippFindAttribute(con->request, "my-jobs", IPP_TAG_BOOLEAN)) != NULL && job_ids)
6714 send_ipp_status(con, IPP_CONFLICT,
6715 _("The %s attribute cannot be provided with job-ids."),
6719 else if (attr && attr->values[0].boolean)
6720 strlcpy(username, get_username(con), sizeof(username));
6724 ra = create_requested_array(con->request);
6725 for (job_attr = (char *)cupsArrayFirst(ra); job_attr; job_attr = (char *)cupsArrayNext(ra))
6726 if (strcmp(job_attr, "job-id") &&
6727 strcmp(job_attr, "job-k-octets") &&
6728 strcmp(job_attr, "job-media-progress") &&
6729 strcmp(job_attr, "job-more-info") &&
6730 strcmp(job_attr, "job-name") &&
6731 strcmp(job_attr, "job-originating-user-name") &&
6732 strcmp(job_attr, "job-preserved") &&
6733 strcmp(job_attr, "job-printer-up-time") &&
6734 strcmp(job_attr, "job-printer-uri") &&
6735 strcmp(job_attr, "job-state") &&
6736 strcmp(job_attr, "job-state-reasons") &&
6737 strcmp(job_attr, "job-uri") &&
6738 strcmp(job_attr, "time-at-completed") &&
6739 strcmp(job_attr, "time-at-creation") &&
6740 strcmp(job_attr, "number-of-documents"))
6746 if (need_load_job && (limit == 0 || limit > 500) && (list == Jobs || delete_list))
6749 * Limit expensive Get-Jobs for job history to 500 jobs...
6752 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "limit", 500);
6755 ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER, "limit", limit);
6759 cupsdLogClient(con, CUPSD_LOG_INFO, "Limiting Get-Jobs response to %d jobs.", limit);
6763 * OK, build a list of jobs for this printer...
6768 int i; /* Looping var */
6770 for (i = 0; i < job_ids->num_values; i ++)
6772 if (!cupsdFindJob(job_ids->values[i].integer))
6776 if (i < job_ids->num_values)
6778 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
6779 job_ids->values[i].integer);
6783 for (i = 0; i < job_ids->num_values; i ++)
6785 job = cupsdFindJob(job_ids->values[i].integer);
6787 if (need_load_job && !job->attrs)
6793 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id);
6799 ippAddSeparator(con->response);
6801 exclude = cupsdGetPrivateAttrs(job->printer ?
6802 job->printer->op_policy_ptr :
6803 policy, con, job->printer,
6806 copy_job_attrs(con, job, ra, exclude);
6811 if (first_index > 1)
6812 job = (cupsd_job_t *)cupsArrayIndex(list, first_index - 1);
6814 job = (cupsd_job_t *)cupsArrayFirst(list);
6816 for (count = 0; (limit <= 0 || count < limit) && job; job = (cupsd_job_t *)cupsArrayNext(list))
6819 * Filter out jobs that don't match...
6822 cupsdLogMessage(CUPSD_LOG_DEBUG2,
6823 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6824 "state_value=%d, attrs=%p", job->id, job->dest,
6825 job->username, job->state_value, job->attrs);
6827 if (!job->dest || !job->username)
6830 if (!job->dest || !job->username)
6833 if ((dest && strcmp(job->dest, dest)) &&
6834 (!job->printer || !dest || strcmp(job->printer->name, dest)))
6836 if ((job->dtype & dmask) != dtype &&
6837 (!job->printer || (job->printer->type & dmask) != dtype))
6840 if ((job_comparison < 0 && job->state_value > job_state) ||
6841 (job_comparison == 0 && job->state_value != job_state) ||
6842 (job_comparison > 0 && job->state_value < job_state))
6845 if (job->id < first_job_id)
6848 if (need_load_job && !job->attrs)
6854 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: No attributes for job %d", job->id);
6859 if (username[0] && _cups_strcasecmp(username, job->username))
6863 ippAddSeparator(con->response);
6867 exclude = cupsdGetPrivateAttrs(job->printer ?
6868 job->printer->op_policy_ptr :
6869 policy, con, job->printer,
6872 copy_job_attrs(con, job, ra, exclude);
6875 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count=%d", count);
6878 cupsArrayDelete(ra);
6881 cupsArrayDelete(list);
6883 con->response->request.status.status_code = IPP_OK;
6888 * 'get_notifications()' - Get events for a subscription.
6892 get_notifications(cupsd_client_t *con) /* I - Client connection */
6894 int i, j; /* Looping vars */
6895 http_status_t status; /* Policy status */
6896 cupsd_subscription_t *sub; /* Subscription */
6897 ipp_attribute_t *ids, /* notify-subscription-ids */
6898 *sequences; /* notify-sequence-numbers */
6899 int min_seq; /* Minimum sequence number */
6900 int interval; /* Poll interval */
6903 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
6907 * Get subscription attributes...
6910 ids = ippFindAttribute(con->request, "notify-subscription-ids",
6912 sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
6917 send_ipp_status(con, IPP_BAD_REQUEST,
6918 _("Missing notify-subscription-ids attribute."));
6923 * Are the subscription IDs valid?
6926 for (i = 0, interval = 60; i < ids->num_values; i ++)
6928 if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
6931 * Bad subscription ID...
6934 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
6935 ids->values[i].integer);
6943 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
6945 con, sub->owner)) != HTTP_OK)
6947 send_http_error(con, status, sub->dest);
6952 * Check the subscription type and update the interval accordingly.
6955 if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
6958 else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
6960 else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
6966 * Tell the client to poll again in N seconds...
6970 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6971 "notify-get-interval", interval);
6973 ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
6974 "printer-up-time", time(NULL));
6977 * Copy the subscription event attributes to the response.
6980 con->response->request.status.status_code =
6981 interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
6983 for (i = 0; i < ids->num_values; i ++)
6986 * Get the subscription and sequence number...
6989 sub = cupsdFindSubscription(ids->values[i].integer);
6991 if (sequences && i < sequences->num_values)
6992 min_seq = sequences->values[i].integer;
6997 * If we don't have any new events, nothing to do here...
7000 if (min_seq > (sub->first_event_id + cupsArrayCount(sub->events)))
7004 * Otherwise copy all of the new events...
7007 if (sub->first_event_id > min_seq)
7010 j = min_seq - sub->first_event_id;
7012 for (; j < cupsArrayCount(sub->events); j ++)
7014 ippAddSeparator(con->response);
7016 copy_attrs(con->response,
7017 ((cupsd_event_t *)cupsArrayIndex(sub->events, j))->attrs, NULL,
7018 IPP_TAG_EVENT_NOTIFICATION, 0, NULL);
7025 * 'get_ppd()' - Get a named PPD from the local system.
7029 get_ppd(cupsd_client_t *con, /* I - Client connection */
7030 ipp_attribute_t *uri) /* I - Printer URI or PPD name */
7032 http_status_t status; /* Policy status */
7033 cupsd_printer_t *dest; /* Destination */
7034 cups_ptype_t dtype; /* Destination type */
7037 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
7038 con->number, uri, uri->name, uri->values[0].string.text);
7040 if (!strcmp(ippGetName(uri), "ppd-name"))
7043 * Return a PPD file from cups-driverd...
7046 const char *ppd_name = ippGetString(uri, 0, NULL);
7047 /* ppd-name value */
7048 char command[1024], /* cups-driverd command */
7049 options[1024], /* Options to pass to command */
7050 oppd_name[1024]; /* Escaped ppd-name */
7056 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7058 send_http_error(con, status, NULL);
7063 * Check ppd-name value...
7066 if (strstr(ppd_name, "../"))
7068 send_ipp_status(con, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, _("Invalid ppd-name value."));
7073 * Run cups-driverd command with the given options...
7076 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7077 url_encode_string(ppd_name, oppd_name, sizeof(oppd_name));
7078 snprintf(options, sizeof(options), "get+%d+%s", ippGetRequestId(con->request), oppd_name);
7080 if (cupsdSendCommand(con, command, options, 0))
7083 * Command started successfully, don't send an IPP response here...
7086 ippDelete(con->response);
7087 con->response = NULL;
7092 * Command failed, return "internal error" so the user knows something
7096 send_ipp_status(con, IPP_INTERNAL_ERROR, _("cups-driverd failed to execute."));
7099 else if (!strcmp(ippGetName(uri), "printer-uri") && cupsdValidateDest(ippGetString(uri, 0, NULL), &dtype, &dest))
7101 int i; /* Looping var */
7102 char filename[1024]; /* PPD filename */
7108 if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
7110 send_http_error(con, status, dest);
7115 * See if we need the PPD for a class or remote printer...
7118 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->name);
7120 if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
7122 send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer."));
7123 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->uri);
7126 else if (dtype & CUPS_PRINTER_CLASS)
7128 for (i = 0; i < dest->num_printers; i ++)
7129 if (!(dest->printers[i]->type & CUPS_PRINTER_CLASS))
7131 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot, dest->printers[i]->name);
7133 if (!access(filename, 0))
7137 if (i < dest->num_printers)
7138 dest = dest->printers[i];
7141 send_ipp_status(con, IPP_STATUS_CUPS_SEE_OTHER, _("See remote printer."));
7142 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, dest->printers[0]->uri);
7148 * Found the printer with the PPD file, now see if there is one...
7151 if ((con->file = open(filename, O_RDONLY)) < 0)
7153 send_ipp_status(con, IPP_STATUS_ERROR_NOT_FOUND, _("The PPD file \"%s\" could not be opened: %s"), ippGetString(uri, 0, NULL), strerror(errno));
7157 fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
7161 ippSetStatusCode(con->response, IPP_STATUS_OK);
7164 send_ipp_status(con, IPP_STATUS_ERROR_NOT_FOUND, _("The PPD file \"%s\" could not be found."), ippGetString(uri, 0, NULL));
7169 * 'get_ppds()' - Get the list of PPD files on the local system.
7173 get_ppds(cupsd_client_t *con) /* I - Client connection */
7175 http_status_t status; /* Policy status */
7176 ipp_attribute_t *limit, /* Limit attribute */
7177 *device, /* ppd-device-id attribute */
7178 *language, /* ppd-natural-language attribute */
7179 *make, /* ppd-make attribute */
7180 *model, /* ppd-make-and-model attribute */
7181 *model_number, /* ppd-model-number attribute */
7182 *product, /* ppd-product attribute */
7183 *psversion, /* ppd-psverion attribute */
7184 *type, /* ppd-type attribute */
7185 *requested, /* requested-attributes attribute */
7186 *exclude, /* exclude-schemes attribute */
7187 *include; /* include-schemes attribute */
7188 char command[1024], /* cups-driverd command */
7189 options[4096], /* Options to pass to command */
7190 device_str[256],/* Escaped ppd-device-id string */
7192 /* Escaped ppd-natural-language */
7193 make_str[256], /* Escaped ppd-make string */
7194 model_str[256], /* Escaped ppd-make-and-model string */
7195 model_number_str[256],
7196 /* ppd-model-number string */
7198 /* Escaped ppd-product string */
7200 /* Escaped ppd-psversion string */
7201 type_str[256], /* Escaped ppd-type string */
7203 /* String for requested attributes */
7205 /* String for excluded schemes */
7207 /* String for included schemes */
7210 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->number);
7216 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7218 send_http_error(con, status, NULL);
7223 * Run cups-driverd command with the given options...
7226 limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
7227 device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
7228 language = ippFindAttribute(con->request, "ppd-natural-language",
7230 make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
7231 model = ippFindAttribute(con->request, "ppd-make-and-model",
7233 model_number = ippFindAttribute(con->request, "ppd-model-number",
7235 product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
7236 psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
7237 type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
7238 requested = ippFindAttribute(con->request, "requested-attributes",
7240 exclude = ippFindAttribute(con->request, "exclude-schemes",
7242 include = ippFindAttribute(con->request, "include-schemes",
7246 url_encode_attr(requested, requested_str, sizeof(requested_str));
7248 strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
7251 url_encode_attr(device, device_str, sizeof(device_str));
7253 device_str[0] = '\0';
7256 url_encode_attr(language, language_str, sizeof(language_str));
7258 language_str[0] = '\0';
7261 url_encode_attr(make, make_str, sizeof(make_str));
7266 url_encode_attr(model, model_str, sizeof(model_str));
7268 model_str[0] = '\0';
7271 snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
7272 model_number->values[0].integer);
7274 model_number_str[0] = '\0';
7277 url_encode_attr(product, product_str, sizeof(product_str));
7279 product_str[0] = '\0';
7282 url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
7284 psversion_str[0] = '\0';
7287 url_encode_attr(type, type_str, sizeof(type_str));
7292 url_encode_attr(exclude, exclude_str, sizeof(exclude_str));
7294 exclude_str[0] = '\0';
7297 url_encode_attr(include, include_str, sizeof(include_str));
7299 include_str[0] = '\0';
7301 snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
7302 snprintf(options, sizeof(options),
7303 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7304 con->request->request.op.request_id,
7305 limit ? limit->values[0].integer : 0,
7307 device ? "%20" : "", device_str,
7308 language ? "%20" : "", language_str,
7309 make ? "%20" : "", make_str,
7310 model ? "%20" : "", model_str,
7311 model_number ? "%20" : "", model_number_str,
7312 product ? "%20" : "", product_str,
7313 psversion ? "%20" : "", psversion_str,
7314 type ? "%20" : "", type_str,
7315 exclude_str[0] ? "%20" : "", exclude_str,
7316 include_str[0] ? "%20" : "", include_str);
7318 if (cupsdSendCommand(con, command, options, 0))
7321 * Command started successfully, don't send an IPP response here...
7324 ippDelete(con->response);
7325 con->response = NULL;
7330 * Command failed, return "internal error" so the user knows something
7334 send_ipp_status(con, IPP_INTERNAL_ERROR,
7335 _("cups-driverd failed to execute."));
7341 * 'get_printer_attrs()' - Get printer attributes.
7345 get_printer_attrs(cupsd_client_t *con, /* I - Client connection */
7346 ipp_attribute_t *uri) /* I - Printer URI */
7348 http_status_t status; /* Policy status */
7349 cups_ptype_t dtype; /* Destination type (printer/class) */
7350 cupsd_printer_t *printer; /* Printer/class */
7351 cups_array_t *ra; /* Requested attributes array */
7354 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
7355 con->number, uri->values[0].string.text);
7358 * Is the destination valid?
7361 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7367 send_ipp_status(con, IPP_NOT_FOUND,
7368 _("The printer or class does not exist."));
7376 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7378 send_http_error(con, status, printer);
7383 * Send the attributes...
7386 ra = create_requested_array(con->request);
7388 copy_printer_attrs(con, printer, ra);
7390 cupsArrayDelete(ra);
7392 con->response->request.status.status_code = IPP_OK;
7397 * 'get_printer_supported()' - Get printer supported values.
7401 get_printer_supported(
7402 cupsd_client_t *con, /* I - Client connection */
7403 ipp_attribute_t *uri) /* I - Printer URI */
7405 http_status_t status; /* Policy status */
7406 cups_ptype_t dtype; /* Destination type (printer/class) */
7407 cupsd_printer_t *printer; /* Printer/class */
7410 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_supported(%p[%d], %s)", con,
7411 con->number, uri->values[0].string.text);
7414 * Is the destination valid?
7417 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7423 send_ipp_status(con, IPP_NOT_FOUND,
7424 _("The printer or class does not exist."));
7432 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
7434 send_http_error(con, status, printer);
7439 * Return a list of attributes that can be set via Set-Printer-Attributes.
7442 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7443 "printer-geo-location", 0);
7444 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7446 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7447 "printer-location", 0);
7448 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7449 "printer-organization", 0);
7450 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ADMINDEFINE,
7451 "printer-organizational-unit", 0);
7453 con->response->request.status.status_code = IPP_OK;
7458 * 'get_printers()' - Get a list of printers or classes.
7462 get_printers(cupsd_client_t *con, /* I - Client connection */
7463 int type) /* I - 0 or CUPS_PRINTER_CLASS */
7465 http_status_t status; /* Policy status */
7466 ipp_attribute_t *attr; /* Current attribute */
7467 int limit; /* Max number of printers to return */
7468 int count; /* Number of printers that match */
7469 cupsd_printer_t *printer; /* Current printer pointer */
7470 cups_ptype_t printer_type, /* printer-type attribute */
7471 printer_mask; /* printer-type-mask attribute */
7472 char *location; /* Location string */
7473 const char *username; /* Current user */
7474 char *first_printer_name; /* first-printer-name attribute */
7475 cups_array_t *ra; /* Requested attributes array */
7476 int local; /* Local connection? */
7479 cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
7486 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
7488 send_http_error(con, status, NULL);
7493 * Check for printers...
7496 if (!Printers || !cupsArrayCount(Printers))
7498 send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
7503 * See if they want to limit the number of printers reported...
7506 if ((attr = ippFindAttribute(con->request, "limit",
7507 IPP_TAG_INTEGER)) != NULL)
7508 limit = attr->values[0].integer;
7512 if ((attr = ippFindAttribute(con->request, "first-printer-name",
7513 IPP_TAG_NAME)) != NULL)
7514 first_printer_name = attr->values[0].string.text;
7516 first_printer_name = NULL;
7519 * Support filtering...
7522 if ((attr = ippFindAttribute(con->request, "printer-type",
7523 IPP_TAG_ENUM)) != NULL)
7524 printer_type = (cups_ptype_t)attr->values[0].integer;
7526 printer_type = (cups_ptype_t)0;
7528 if ((attr = ippFindAttribute(con->request, "printer-type-mask",
7529 IPP_TAG_ENUM)) != NULL)
7530 printer_mask = (cups_ptype_t)attr->values[0].integer;
7532 printer_mask = (cups_ptype_t)0;
7534 local = httpAddrLocalhost(&(con->clientaddr));
7536 if ((attr = ippFindAttribute(con->request, "printer-location",
7537 IPP_TAG_TEXT)) != NULL)
7538 location = attr->values[0].string.text;
7542 if (con->username[0])
7543 username = con->username;
7544 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7545 IPP_TAG_NAME)) != NULL)
7546 username = attr->values[0].string.text;
7550 ra = create_requested_array(con->request);
7553 * OK, build a list of printers for this printer...
7556 if (first_printer_name)
7558 if ((printer = cupsdFindDest(first_printer_name)) == NULL)
7559 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7562 printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
7565 count < limit && printer;
7566 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
7568 if (!local && !printer->shared)
7571 if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
7572 (printer->type & printer_mask) == printer_type &&
7574 (printer->location && !_cups_strcasecmp(printer->location, location))))
7577 * If a username is specified, see if it is allowed or denied
7581 if (cupsArrayCount(printer->users) && username &&
7582 !user_allowed(printer, username))
7586 * Add the group separator as needed...
7590 ippAddSeparator(con->response);
7595 * Send the attributes...
7598 copy_printer_attrs(con, printer, ra);
7602 cupsArrayDelete(ra);
7604 con->response->request.status.status_code = IPP_OK;
7609 * 'get_subscription_attrs()' - Get subscription attributes.
7613 get_subscription_attrs(
7614 cupsd_client_t *con, /* I - Client connection */
7615 int sub_id) /* I - Subscription ID */
7617 http_status_t status; /* Policy status */
7618 cupsd_subscription_t *sub; /* Subscription */
7619 cupsd_policy_t *policy; /* Current security policy */
7620 cups_array_t *ra, /* Requested attributes array */
7621 *exclude; /* Private attributes array */
7624 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7625 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7626 con, con->number, sub_id);
7629 * Expire subscriptions as needed...
7632 cupsdExpireSubscriptions(NULL, NULL);
7635 * Is the subscription ID valid?
7638 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
7641 * Bad subscription ID...
7644 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
7654 policy = sub->dest->op_policy_ptr;
7656 policy = DefaultPolicyPtr;
7658 if ((status = cupsdCheckPolicy(policy, con, sub->owner)) != HTTP_OK)
7660 send_http_error(con, status, sub->dest);
7664 exclude = cupsdGetPrivateAttrs(policy, con, sub->dest, sub->owner);
7667 * Copy the subscription attributes to the response using the
7668 * requested-attributes attribute that may be provided by the client.
7671 ra = create_requested_array(con->request);
7673 copy_subscription_attrs(con, sub, ra, exclude);
7675 cupsArrayDelete(ra);
7677 con->response->request.status.status_code = IPP_OK;
7682 * 'get_subscriptions()' - Get subscriptions.
7686 get_subscriptions(cupsd_client_t *con, /* I - Client connection */
7687 ipp_attribute_t *uri) /* I - Printer/job URI */
7689 http_status_t status; /* Policy status */
7690 int count; /* Number of subscriptions */
7691 int limit; /* Limit */
7692 cupsd_subscription_t *sub; /* Subscription */
7693 cups_array_t *ra; /* Requested attributes array */
7694 ipp_attribute_t *attr; /* Attribute */
7695 cups_ptype_t dtype; /* Destination type (printer/class) */
7696 char scheme[HTTP_MAX_URI],
7697 /* Scheme portion of URI */
7698 username[HTTP_MAX_URI],
7699 /* Username portion of URI */
7701 /* Host portion of URI */
7702 resource[HTTP_MAX_URI];
7703 /* Resource portion of URI */
7704 int port; /* Port portion of URI */
7705 cupsd_job_t *job; /* Job pointer */
7706 cupsd_printer_t *printer; /* Printer */
7707 cupsd_policy_t *policy; /* Policy */
7708 cups_array_t *exclude; /* Private attributes array */
7711 cupsdLogMessage(CUPSD_LOG_DEBUG2,
7712 "get_subscriptions(con=%p[%d], uri=%s)",
7713 con, con->number, uri->values[0].string.text);
7716 * Is the destination valid?
7719 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7720 sizeof(scheme), username, sizeof(username), host,
7721 sizeof(host), &port, resource, sizeof(resource));
7723 if (!strcmp(resource, "/") ||
7724 (!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
7725 (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
7726 (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
7731 else if (!strncmp(resource, "/jobs/", 6) && resource[6])
7734 job = cupsdFindJob(atoi(resource + 6));
7738 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7739 atoi(resource + 6));
7743 else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
7749 send_ipp_status(con, IPP_NOT_FOUND,
7750 _("The printer or class does not exist."));
7753 else if ((attr = ippFindAttribute(con->request, "notify-job-id",
7754 IPP_TAG_INTEGER)) != NULL)
7756 job = cupsdFindJob(attr->values[0].integer);
7760 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."),
7761 attr->values[0].integer);
7773 policy = printer->op_policy_ptr;
7775 policy = DefaultPolicyPtr;
7777 if ((status = cupsdCheckPolicy(policy, con, NULL)) != HTTP_OK)
7779 send_http_error(con, status, printer);
7784 * Expire subscriptions as needed...
7787 cupsdExpireSubscriptions(NULL, NULL);
7790 * Copy the subscription attributes to the response using the
7791 * requested-attributes attribute that may be provided by the client.
7794 ra = create_requested_array(con->request);
7796 if ((attr = ippFindAttribute(con->request, "limit",
7797 IPP_TAG_INTEGER)) != NULL)
7798 limit = attr->values[0].integer;
7803 * See if we only want to see subscriptions for a specific user...
7806 if ((attr = ippFindAttribute(con->request, "my-subscriptions",
7807 IPP_TAG_BOOLEAN)) != NULL &&
7808 attr->values[0].boolean)
7809 strlcpy(username, get_username(con), sizeof(username));
7813 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
7815 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
7816 if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
7817 (!username[0] || !_cups_strcasecmp(username, sub->owner)))
7819 ippAddSeparator(con->response);
7821 exclude = cupsdGetPrivateAttrs(sub->dest ? sub->dest->op_policy_ptr :
7822 policy, con, sub->dest,
7825 copy_subscription_attrs(con, sub, ra, exclude);
7828 if (limit && count >= limit)
7832 cupsArrayDelete(ra);
7835 con->response->request.status.status_code = IPP_OK;
7837 send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
7842 * 'get_username()' - Get the username associated with a request.
7845 static const char * /* O - Username */
7846 get_username(cupsd_client_t *con) /* I - Connection */
7848 ipp_attribute_t *attr; /* Attribute */
7851 if (con->username[0])
7852 return (con->username);
7853 else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
7854 IPP_TAG_NAME)) != NULL)
7855 return (attr->values[0].string.text);
7857 return ("anonymous");
7862 * 'hold_job()' - Hold a print job.
7866 hold_job(cupsd_client_t *con, /* I - Client connection */
7867 ipp_attribute_t *uri) /* I - Job or Printer URI */
7869 ipp_attribute_t *attr; /* Current job-hold-until */
7870 const char *when; /* New value */
7871 int jobid; /* Job ID */
7872 char scheme[HTTP_MAX_URI], /* Method portion of URI */
7873 username[HTTP_MAX_URI], /* Username portion of URI */
7874 host[HTTP_MAX_URI], /* Host portion of URI */
7875 resource[HTTP_MAX_URI]; /* Resource portion of URI */
7876 int port; /* Port portion of URI */
7877 cupsd_job_t *job; /* Job information */
7880 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->number,
7881 uri->values[0].string.text);
7884 * See if we have a job URI or a printer URI...
7887 if (!strcmp(uri->name, "printer-uri"))
7890 * Got a printer URI; see if we also have a job-id attribute...
7893 if ((attr = ippFindAttribute(con->request, "job-id",
7894 IPP_TAG_INTEGER)) == NULL)
7896 send_ipp_status(con, IPP_BAD_REQUEST,
7897 _("Got a printer-uri attribute but no job-id."));
7901 jobid = attr->values[0].integer;
7906 * Got a job URI; parse it to get the job ID...
7909 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
7910 sizeof(scheme), username, sizeof(username), host,
7911 sizeof(host), &port, resource, sizeof(resource));
7913 if (strncmp(resource, "/jobs/", 6))
7919 send_ipp_status(con, IPP_BAD_REQUEST,
7920 _("Bad job-uri \"%s\"."),
7921 uri->values[0].string.text);
7925 jobid = atoi(resource + 6);
7929 * See if the job exists...
7932 if ((job = cupsdFindJob(jobid)) == NULL)
7935 * Nope - return a "not found" error...
7938 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
7943 * See if the job is owned by the requesting user...
7946 if (!validate_user(job, con, job->username, username, sizeof(username)))
7948 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
7949 cupsdFindDest(job->dest));
7954 * See if the job is in a state that allows holding...
7957 if (job->state_value > IPP_JOB_STOPPED)
7960 * Return a "not-possible" error...
7963 send_ipp_status(con, IPP_NOT_POSSIBLE,
7964 _("Job #%d is finished and cannot be altered."),
7970 * Hold the job and return...
7973 if ((attr = ippFindAttribute(con->request, "job-hold-until",
7974 IPP_TAG_KEYWORD)) == NULL)
7975 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
7979 when = attr->values[0].string.text;
7981 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
7982 "Job job-hold-until value changed by user.");
7985 when = "indefinite";
7987 cupsdSetJobHoldUntil(job, when, 1);
7988 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT, "Job held by \"%s\".",
7991 con->response->request.status.status_code = IPP_OK;
7996 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8000 hold_new_jobs(cupsd_client_t *con, /* I - Connection */
8001 ipp_attribute_t *uri) /* I - Printer URI */
8003 http_status_t status; /* Policy status */
8004 cups_ptype_t dtype; /* Destination type (printer/class) */
8005 cupsd_printer_t *printer; /* Printer data */
8008 cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_new_jobs(%p[%d], %s)", con,
8009 con->number, uri->values[0].string.text);
8012 * Is the destination valid?
8015 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8021 send_ipp_status(con, IPP_NOT_FOUND,
8022 _("The printer or class does not exist."));
8030 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8032 send_http_error(con, status, printer);
8037 * Hold pending/new jobs sent to the printer...
8040 printer->holding_new_jobs = 1;
8042 cupsdSetPrinterReasons(printer, "+hold-new-jobs");
8044 if (dtype & CUPS_PRINTER_CLASS)
8045 cupsdLogMessage(CUPSD_LOG_INFO,
8046 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8047 printer->name, get_username(con));
8049 cupsdLogMessage(CUPSD_LOG_INFO,
8050 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8051 printer->name, get_username(con));
8054 * Everything was ok, so return OK status...
8057 con->response->request.status.status_code = IPP_OK;
8062 * 'move_job()' - Move a job to a new destination.
8066 move_job(cupsd_client_t *con, /* I - Client connection */
8067 ipp_attribute_t *uri) /* I - Job URI */
8069 http_status_t status; /* Policy status */
8070 ipp_attribute_t *attr; /* Current attribute */
8071 int jobid; /* Job ID */
8072 cupsd_job_t *job; /* Current job */
8073 const char *src; /* Source printer/class */
8074 cups_ptype_t stype, /* Source type (printer or class) */
8075 dtype; /* Destination type (printer/class) */
8076 char scheme[HTTP_MAX_URI], /* Scheme portion of URI */
8077 username[HTTP_MAX_URI], /* Username portion of URI */
8078 host[HTTP_MAX_URI], /* Host portion of URI */
8079 resource[HTTP_MAX_URI]; /* Resource portion of URI */
8080 int port; /* Port portion of URI */
8081 cupsd_printer_t *sprinter, /* Source printer */
8082 *dprinter; /* Destination printer */
8085 cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->number,
8086 uri->values[0].string.text);
8089 * Get the new printer or class...
8092 if ((attr = ippFindAttribute(con->request, "job-printer-uri",
8093 IPP_TAG_URI)) == NULL)
8096 * Need job-printer-uri...
8099 send_ipp_status(con, IPP_BAD_REQUEST,
8100 _("job-printer-uri attribute missing."));
8104 if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
8110 send_ipp_status(con, IPP_NOT_FOUND,
8111 _("The printer or class does not exist."));
8116 * See if we have a job URI or a printer URI...
8119 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
8120 sizeof(scheme), username, sizeof(username), host,
8121 sizeof(host), &port, resource, sizeof(resource));
8123 if (!strcmp(uri->name, "printer-uri"))
8126 * Got a printer URI; see if we also have a job-id attribute...
8129 if ((attr = ippFindAttribute(con->request, "job-id",
8130 IPP_TAG_INTEGER)) == NULL)
8136 if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
8137 &sprinter)) == NULL)
8143 send_ipp_status(con, IPP_NOT_FOUND,
8144 _("The printer or class does not exist."));
8153 * Otherwise, just move a single job...
8156 if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
8159 * Nope - return a "not found" error...
8162 send_ipp_status(con, IPP_NOT_FOUND,
8163 _("Job #%d does not exist."), attr->values[0].integer);
8169 * Job found, initialize source pointers...
8180 * Got a job URI; parse it to get the job ID...
8183 if (strncmp(resource, "/jobs/", 6))
8189 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
8190 uri->values[0].string.text);
8195 * See if the job exists...
8198 jobid = atoi(resource + 6);
8200 if ((job = cupsdFindJob(jobid)) == NULL)
8203 * Nope - return a "not found" error...
8206 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
8212 * Job found, initialize source pointers...
8221 * Check the policy of the destination printer...
8224 if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
8225 job ? job->username : NULL)) != HTTP_OK)
8227 send_http_error(con, status, dprinter);
8232 * Now move the job or jobs...
8238 * See if the job has been completed...
8241 if (job->state_value > IPP_JOB_STOPPED)
8244 * Return a "not-possible" error...
8247 send_ipp_status(con, IPP_NOT_POSSIBLE,
8248 _("Job #%d is finished and cannot be altered."),
8254 * See if the job is owned by the requesting user...
8257 if (!validate_user(job, con, job->username, username, sizeof(username)))
8259 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
8260 cupsdFindDest(job->dest));
8265 * Move the job to a different printer or class...
8268 cupsdMoveJob(job, dprinter);
8273 * Got the source printer, now look through the jobs...
8276 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
8278 job = (cupsd_job_t *)cupsArrayNext(Jobs))
8281 * See if the job is pointing at the source printer or has not been
8285 if (_cups_strcasecmp(job->dest, src) ||
8286 job->state_value > IPP_JOB_STOPPED)
8290 * See if the job can be moved by the requesting user...
8293 if (!validate_user(job, con, job->username, username, sizeof(username)))
8297 * Move the job to a different printer or class...
8300 cupsdMoveJob(job, dprinter);
8305 * Start jobs if possible...
8311 * Return with "everything is OK" status...
8314 con->response->request.status.status_code = IPP_OK;
8319 * 'ppd_parse_line()' - Parse a PPD default line.
8322 static int /* O - 0 on success, -1 on failure */
8323 ppd_parse_line(const char *line, /* I - Line */
8324 char *option, /* O - Option name */
8325 int olen, /* I - Size of option name */
8326 char *choice, /* O - Choice name */
8327 int clen) /* I - Size of choice name */
8330 * Verify this is a default option line...
8333 if (strncmp(line, "*Default", 8))
8337 * Read the option name...
8340 for (line += 8, olen --;
8341 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8352 * Skip everything else up to the colon (:)...
8355 while (*line && *line != ':')
8364 * Now grab the option choice, skipping leading whitespace...
8367 while (isspace(*line & 255))
8371 *line > ' ' && *line < 0x7f && *line != ':' && *line != '/';
8382 * Return with no errors...
8390 * 'print_job()' - Print a file to a printer or class.
8394 print_job(cupsd_client_t *con, /* I - Client connection */
8395 ipp_attribute_t *uri) /* I - Printer URI */
8397 ipp_attribute_t *attr; /* Current attribute */
8398 ipp_attribute_t *doc_name; /* document-name attribute */
8399 ipp_attribute_t *format; /* Document-format attribute */
8400 const char *default_format; /* document-format-default value */
8401 cupsd_job_t *job; /* New job */
8402 char filename[1024]; /* Job filename */
8403 mime_type_t *filetype; /* Type of file */
8404 char super[MIME_MAX_SUPER], /* Supertype of file */
8405 type[MIME_MAX_TYPE], /* Subtype of file */
8406 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
8407 /* Textual name of mime type */
8408 cupsd_printer_t *printer; /* Printer data */
8409 struct stat fileinfo; /* File information */
8410 int kbytes; /* Size of file */
8411 int compression; /* Document compression */
8414 cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->number,
8415 uri->values[0].string.text);
8418 * Validate print file attributes, for now just document-format and
8419 * compression (CUPS only supports "none" and "gzip")...
8422 compression = CUPS_FILE_NONE;
8424 if ((attr = ippFindAttribute(con->request, "compression",
8425 IPP_TAG_KEYWORD)) != NULL)
8427 if (strcmp(attr->values[0].string.text, "none")
8429 && strcmp(attr->values[0].string.text, "gzip")
8430 #endif /* HAVE_LIBZ */
8433 send_ipp_status(con, IPP_ATTRIBUTES,
8434 _("Unsupported compression \"%s\"."),
8435 attr->values[0].string.text);
8436 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
8437 "compression", NULL, attr->values[0].string.text);
8442 if (!strcmp(attr->values[0].string.text, "gzip"))
8443 compression = CUPS_FILE_GZIP;
8444 #endif /* HAVE_LIBZ */
8448 * Do we have a file to print?
8453 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
8458 * Is the destination valid?
8461 if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
8467 send_ipp_status(con, IPP_NOT_FOUND,
8468 _("The printer or class does not exist."));
8473 * Is it a format we support?
8476 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
8478 ippSetName(con->request, &doc_name, "document-name-supplied");
8480 if ((format = ippFindAttribute(con->request, "document-format",
8481 IPP_TAG_MIMETYPE)) != NULL)
8484 * Grab format from client...
8487 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]", super,
8490 send_ipp_status(con, IPP_BAD_REQUEST,
8491 _("Bad document-format \"%s\"."),
8492 format->values[0].string.text);
8496 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL));
8498 else if ((default_format = cupsGetOption("document-format",
8499 printer->num_options,
8500 printer->options)) != NULL)
8503 * Use default document format...
8506 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
8508 send_ipp_status(con, IPP_BAD_REQUEST,
8509 _("Bad document-format \"%s\"."),
8520 strlcpy(super, "application", sizeof(super));
8521 strlcpy(type, "octet-stream", sizeof(type));
8524 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
8527 * Auto-type the file...
8530 cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job ???] Auto-typing file...");
8533 filetype = mimeFileType(MimeDatabase, con->filename,
8534 doc_name ? doc_name->values[0].string.text : NULL,
8538 filetype = mimeType(MimeDatabase, super, type);
8540 cupsdLogMessage(CUPSD_LOG_INFO, "[Job ???] Request file type is %s/%s.",
8541 filetype->super, filetype->type);
8543 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type);
8544 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype);
8547 filetype = mimeType(MimeDatabase, super, type);
8551 (!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
8554 * Replace the document-format attribute value with the auto-typed or
8558 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
8562 ippSetString(con->request, &format, 0, mimetype);
8564 ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
8565 "document-format", NULL, mimetype);
8569 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
8570 _("Unsupported document-format \"%s\"."),
8571 format ? format->values[0].string.text :
8572 "application/octet-stream");
8573 cupsdLogMessage(CUPSD_LOG_INFO,
8574 "Hint: Do you have the raw file printing rules enabled?");
8577 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
8578 "document-format", NULL, format->values[0].string.text);
8584 * Read any embedded job ticket info from PS files...
8587 if (!_cups_strcasecmp(filetype->super, "application") &&
8588 (!_cups_strcasecmp(filetype->type, "postscript") ||
8589 !_cups_strcasecmp(filetype->type, "pdf")))
8590 read_job_ticket(con);
8593 * Create the job object...
8596 if ((job = add_job(con, printer, filetype)) == NULL)
8600 * Update quota data...
8603 if (stat(con->filename, &fileinfo))
8606 kbytes = (fileinfo.st_size + 1023) / 1024;
8608 cupsdUpdateQuota(printer, job->username, 0, kbytes);
8610 job->koctets += kbytes;
8612 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
8613 attr->values[0].integer += kbytes;
8616 * Add the job file...
8619 if (add_file(con, job, filetype, compression))
8622 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files);
8623 if (rename(con->filename, filename))
8625 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno));
8627 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file."));
8631 cupsdClearString(&con->filename);
8634 * See if we need to add the ending sheet...
8637 if (cupsdTimeoutJob(job))
8641 * Log and save the job...
8644 cupsdLogJob(job, CUPSD_LOG_INFO,
8645 "File of type %s/%s queued by \"%s\".",
8646 filetype->super, filetype->type, job->username);
8647 cupsdLogJob(job, CUPSD_LOG_DEBUG, "hold_until=%d", (int)job->hold_until);
8648 cupsdLogJob(job, CUPSD_LOG_INFO, "Queued on \"%s\" by \"%s\".",
8649 job->dest, job->username);
8652 * Start the job if possible...
8660 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8662 * This function only gets called when printing a single PDF or PostScript
8663 * file using the Print-Job operation. It doesn't work for Create-Job +
8664 * Send-File, since the job attributes need to be set at job creation
8665 * time for banners to work. The embedded job ticket stuff is here
8666 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8667 * options and IPP attributes which otherwise would be lost.
8669 * The format of a job ticket is simple:
8671 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8673 * %cupsJobTicket: attr1=value1
8674 * %cupsJobTicket: attr2=value2
8676 * %cupsJobTicket: attrN=valueN
8678 * Job ticket lines must appear immediately after the first line that
8679 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8680 * stops looking for job ticket info when it finds a line that does not begin
8681 * with "%cupsJobTicket:".
8683 * The maximum length of a job ticket line, including the prefix, is
8684 * 255 characters to conform with the Adobe DSC.
8686 * Read-only attributes are rejected with a notice to the error log in
8687 * case a malicious user tries anything. Since the job ticket is read
8688 * prior to attribute validation in print_job(), job ticket attributes
8689 * will go through the same validation as IPP attributes...
8693 read_job_ticket(cupsd_client_t *con) /* I - Client connection */
8695 cups_file_t *fp; /* File to read from */
8696 char line[256]; /* Line data */
8697 int num_options; /* Number of options */
8698 cups_option_t *options; /* Options */
8699 ipp_t *ticket; /* New attributes */
8700 ipp_attribute_t *attr, /* Current attribute */
8701 *attr2, /* Job attribute */
8702 *prev2; /* Previous job attribute */
8706 * First open the print file...
8709 if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
8711 cupsdLogMessage(CUPSD_LOG_ERROR,
8712 "Unable to open print file for job ticket - %s",
8718 * Skip the first line...
8721 if (cupsFileGets(fp, line, sizeof(line)) == NULL)
8723 cupsdLogMessage(CUPSD_LOG_ERROR,
8724 "Unable to read from print file for job ticket - %s",
8730 if (strncmp(line, "%!PS-Adobe-", 11) && strncmp(line, "%PDF-", 5))
8733 * Not a DSC-compliant file, so no job ticket info will be available...
8741 * Read job ticket info from the file...
8747 while (cupsFileGets(fp, line, sizeof(line)))
8750 * Stop at the first non-ticket line...
8753 if (strncmp(line, "%cupsJobTicket:", 15))
8757 * Add the options to the option array...
8760 num_options = cupsParseOptions(line + 15, num_options, &options);
8764 * Done with the file; see if we have any options...
8769 if (num_options == 0)
8773 * OK, convert the options to an attribute list, and apply them to
8778 cupsEncodeOptions(ticket, num_options, options);
8781 * See what the user wants to change.
8784 for (attr = ticket->attrs; attr; attr = attr->next)
8786 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
8789 if (!strncmp(attr->name, "date-time-at-", 13) ||
8790 !strcmp(attr->name, "job-impressions-completed") ||
8791 !strcmp(attr->name, "job-media-sheets-completed") ||
8792 !strncmp(attr->name, "job-k-octets", 12) ||
8793 !strcmp(attr->name, "job-id") ||
8794 !strcmp(attr->name, "job-originating-host-name") ||
8795 !strcmp(attr->name, "job-originating-user-name") ||
8796 !strcmp(attr->name, "job-pages-completed") ||
8797 !strcmp(attr->name, "job-printer-uri") ||
8798 !strncmp(attr->name, "job-state", 9) ||
8799 !strcmp(attr->name, "job-uri") ||
8800 !strncmp(attr->name, "time-at-", 8))
8801 continue; /* Read-only attrs */
8803 if ((attr2 = ippFindAttribute(con->request, attr->name,
8804 IPP_TAG_ZERO)) != NULL)
8807 * Some other value; first free the old value...
8810 if (con->request->attrs == attr2)
8812 con->request->attrs = attr2->next;
8817 for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
8818 if (prev2->next == attr2)
8820 prev2->next = attr2->next;
8825 if (con->request->last == attr2)
8826 con->request->last = prev2;
8828 ippDeleteAttribute(NULL, attr2);
8832 * Add new option by copying it...
8835 ippCopyAttribute(con->request, attr, 0);
8839 * Then free the attribute list and option array...
8843 cupsFreeOptions(num_options, options);
8848 * 'reject_jobs()' - Reject print jobs to a printer.
8852 reject_jobs(cupsd_client_t *con, /* I - Client connection */
8853 ipp_attribute_t *uri) /* I - Printer or class URI */
8855 http_status_t status; /* Policy status */
8856 cups_ptype_t dtype; /* Destination type (printer/class) */
8857 cupsd_printer_t *printer; /* Printer data */
8858 ipp_attribute_t *attr; /* printer-state-message text */
8861 cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
8862 con->number, uri->values[0].string.text);
8865 * Is the destination valid?
8868 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8874 send_ipp_status(con, IPP_NOT_FOUND,
8875 _("The printer or class does not exist."));
8883 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8885 send_http_error(con, status, printer);
8890 * Reject jobs sent to the printer...
8893 printer->accepting = 0;
8895 if ((attr = ippFindAttribute(con->request, "printer-state-message",
8896 IPP_TAG_TEXT)) == NULL)
8897 strlcpy(printer->state_message, "Rejecting Jobs",
8898 sizeof(printer->state_message));
8900 strlcpy(printer->state_message, attr->values[0].string.text,
8901 sizeof(printer->state_message));
8903 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
8904 "No longer accepting jobs.");
8906 if (dtype & CUPS_PRINTER_CLASS)
8908 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
8910 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
8911 printer->name, get_username(con));
8915 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
8917 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
8918 printer->name, get_username(con));
8922 * Everything was ok, so return OK status...
8925 con->response->request.status.status_code = IPP_OK;
8930 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8934 release_held_new_jobs(
8935 cupsd_client_t *con, /* I - Connection */
8936 ipp_attribute_t *uri) /* I - Printer URI */
8938 http_status_t status; /* Policy status */
8939 cups_ptype_t dtype; /* Destination type (printer/class) */
8940 cupsd_printer_t *printer; /* Printer data */
8943 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_held_new_jobs(%p[%d], %s)", con,
8944 con->number, uri->values[0].string.text);
8947 * Is the destination valid?
8950 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
8956 send_ipp_status(con, IPP_NOT_FOUND,
8957 _("The printer or class does not exist."));
8965 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
8967 send_http_error(con, status, printer);
8972 * Hold pending/new jobs sent to the printer...
8975 printer->holding_new_jobs = 0;
8977 cupsdSetPrinterReasons(printer, "-hold-new-jobs");
8979 if (dtype & CUPS_PRINTER_CLASS)
8980 cupsdLogMessage(CUPSD_LOG_INFO,
8981 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8982 printer->name, get_username(con));
8984 cupsdLogMessage(CUPSD_LOG_INFO,
8985 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8986 printer->name, get_username(con));
8991 * Everything was ok, so return OK status...
8994 con->response->request.status.status_code = IPP_OK;
8999 * 'release_job()' - Release a held print job.
9003 release_job(cupsd_client_t *con, /* I - Client connection */
9004 ipp_attribute_t *uri) /* I - Job or Printer URI */
9006 ipp_attribute_t *attr; /* Current attribute */
9007 int jobid; /* Job ID */
9008 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9009 username[HTTP_MAX_URI], /* Username portion of URI */
9010 host[HTTP_MAX_URI], /* Host portion of URI */
9011 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9012 int port; /* Port portion of URI */
9013 cupsd_job_t *job; /* Job information */
9016 cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
9017 con->number, uri->values[0].string.text);
9020 * See if we have a job URI or a printer URI...
9023 if (!strcmp(uri->name, "printer-uri"))
9026 * Got a printer URI; see if we also have a job-id attribute...
9029 if ((attr = ippFindAttribute(con->request, "job-id",
9030 IPP_TAG_INTEGER)) == NULL)
9032 send_ipp_status(con, IPP_BAD_REQUEST,
9033 _("Got a printer-uri attribute but no job-id."));
9037 jobid = attr->values[0].integer;
9042 * Got a job URI; parse it to get the job ID...
9045 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9046 sizeof(scheme), username, sizeof(username), host,
9047 sizeof(host), &port, resource, sizeof(resource));
9049 if (strncmp(resource, "/jobs/", 6))
9055 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9056 uri->values[0].string.text);
9060 jobid = atoi(resource + 6);
9064 * See if the job exists...
9067 if ((job = cupsdFindJob(jobid)) == NULL)
9070 * Nope - return a "not found" error...
9073 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9078 * See if job is "held"...
9081 if (job->state_value != IPP_JOB_HELD)
9084 * Nope - return a "not possible" error...
9087 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held."), jobid);
9092 * See if the job is owned by the requesting user...
9095 if (!validate_user(job, con, job->username, username, sizeof(username)))
9097 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9098 cupsdFindDest(job->dest));
9103 * Reset the job-hold-until value to "no-hold"...
9106 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9107 IPP_TAG_KEYWORD)) == NULL)
9108 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9112 ippSetValueTag(job->attrs, &attr, IPP_TAG_KEYWORD);
9113 ippSetString(job->attrs, &attr, 0, "no-hold");
9115 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
9116 "Job job-hold-until value changed by user.");
9117 ippSetString(job->attrs, &job->reasons, 0, "none");
9121 * Release the job and return...
9124 cupsdReleaseJob(job);
9126 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
9127 "Job released by user.");
9129 cupsdLogJob(job, CUPSD_LOG_INFO, "Released by \"%s\".", username);
9131 con->response->request.status.status_code = IPP_OK;
9138 * 'renew_subscription()' - Renew an existing subscription...
9143 cupsd_client_t *con, /* I - Client connection */
9144 int sub_id) /* I - Subscription ID */
9146 http_status_t status; /* Policy status */
9147 cupsd_subscription_t *sub; /* Subscription */
9148 ipp_attribute_t *lease; /* notify-lease-duration */
9151 cupsdLogMessage(CUPSD_LOG_DEBUG2,
9152 "renew_subscription(con=%p[%d], sub_id=%d)",
9153 con, con->number, sub_id);
9156 * Is the subscription ID valid?
9159 if ((sub = cupsdFindSubscription(sub_id)) == NULL)
9162 * Bad subscription ID...
9165 send_ipp_status(con, IPP_NOT_FOUND, _("Subscription #%d does not exist."),
9173 * Job subscriptions cannot be renewed...
9176 send_ipp_status(con, IPP_NOT_POSSIBLE,
9177 _("Job subscriptions cannot be renewed."));
9185 if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
9187 con, sub->owner)) != HTTP_OK)
9189 send_http_error(con, status, sub->dest);
9194 * Renew the subscription...
9197 lease = ippFindAttribute(con->request, "notify-lease-duration",
9200 sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
9202 if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
9204 cupsdLogMessage(CUPSD_LOG_INFO,
9205 "renew_subscription: Limiting notify-lease-duration to "
9208 sub->lease = MaxLeaseDuration;
9211 sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
9213 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
9215 con->response->request.status.status_code = IPP_OK;
9217 ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
9218 "notify-lease-duration", sub->lease);
9223 * 'restart_job()' - Restart an old print job.
9227 restart_job(cupsd_client_t *con, /* I - Client connection */
9228 ipp_attribute_t *uri) /* I - Job or Printer URI */
9230 ipp_attribute_t *attr; /* Current attribute */
9231 int jobid; /* Job ID */
9232 cupsd_job_t *job; /* Job information */
9233 char scheme[HTTP_MAX_URI], /* Method portion of URI */
9234 username[HTTP_MAX_URI], /* Username portion of URI */
9235 host[HTTP_MAX_URI], /* Host portion of URI */
9236 resource[HTTP_MAX_URI]; /* Resource portion of URI */
9237 int port; /* Port portion of URI */
9240 cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
9241 con->number, uri->values[0].string.text);
9244 * See if we have a job URI or a printer URI...
9247 if (!strcmp(uri->name, "printer-uri"))
9250 * Got a printer URI; see if we also have a job-id attribute...
9253 if ((attr = ippFindAttribute(con->request, "job-id",
9254 IPP_TAG_INTEGER)) == NULL)
9256 send_ipp_status(con, IPP_BAD_REQUEST,
9257 _("Got a printer-uri attribute but no job-id."));
9261 jobid = attr->values[0].integer;
9266 * Got a job URI; parse it to get the job ID...
9269 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9270 sizeof(scheme), username, sizeof(username), host,
9271 sizeof(host), &port, resource, sizeof(resource));
9273 if (strncmp(resource, "/jobs/", 6))
9279 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9280 uri->values[0].string.text);
9284 jobid = atoi(resource + 6);
9288 * See if the job exists...
9291 if ((job = cupsdFindJob(jobid)) == NULL)
9294 * Nope - return a "not found" error...
9297 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9302 * See if job is in any of the "completed" states...
9305 if (job->state_value <= IPP_JOB_PROCESSING)
9308 * Nope - return a "not possible" error...
9311 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete."),
9317 * See if we have retained the job files...
9322 if (!job->attrs || job->num_files == 0)
9325 * Nope - return a "not possible" error...
9328 send_ipp_status(con, IPP_NOT_POSSIBLE,
9329 _("Job #%d cannot be restarted - no files."), jobid);
9334 * See if the job is owned by the requesting user...
9337 if (!validate_user(job, con, job->username, username, sizeof(username)))
9339 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9340 cupsdFindDest(job->dest));
9345 * See if the job-hold-until attribute is specified...
9348 if ((attr = ippFindAttribute(con->request, "job-hold-until",
9349 IPP_TAG_KEYWORD)) == NULL)
9350 attr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
9352 if (attr && strcmp(attr->values[0].string.text, "no-hold"))
9355 * Return the job to a held state...
9358 cupsdLogJob(job, CUPSD_LOG_DEBUG,
9359 "Restarted by \"%s\" with job-hold-until=%s.",
9360 username, attr->values[0].string.text);
9361 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
9363 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE,
9364 NULL, job, "Job restarted by user with job-hold-until=%s",
9365 attr->values[0].string.text);
9370 * Restart the job...
9373 cupsdRestartJob(job);
9377 cupsdLogJob(job, CUPSD_LOG_INFO, "Restarted by \"%s\".", username);
9379 con->response->request.status.status_code = IPP_OK;
9384 * 'save_auth_info()' - Save authentication information for a job.
9389 cupsd_client_t *con, /* I - Client connection */
9390 cupsd_job_t *job, /* I - Job */
9391 ipp_attribute_t *auth_info) /* I - auth-info attribute, if any */
9393 int i; /* Looping var */
9394 char filename[1024]; /* Job authentication filename */
9395 cups_file_t *fp; /* Job authentication file */
9396 char line[65536]; /* Line for file */
9397 cupsd_printer_t *dest; /* Destination printer/class */
9401 * This function saves the in-memory authentication information for
9402 * a job so that it can be used to authenticate with a remote host.
9403 * The information is stored in a file that is readable only by the
9404 * root user. The fields are Base-64 encoded, each on a separate line,
9405 * followed by random number (up to 1024) of newlines to limit the
9406 * amount of information that is exposed.
9408 * Because of the potential for exposing of authentication information,
9409 * this functionality is only enabled when running cupsd as root.
9411 * This caching only works for the Basic and BasicDigest authentication
9412 * types. Digest authentication cannot be cached this way, and in
9413 * the future Kerberos authentication may make all of this obsolete.
9415 * Authentication information is saved whenever an authenticated
9416 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9419 * This information is deleted after a job is completed or canceled,
9420 * so reprints may require subsequent re-authentication.
9426 if ((dest = cupsdFindDest(job->dest)) == NULL)
9430 * Create the authentication file and change permissions...
9433 snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
9434 if ((fp = cupsFileOpen(filename, "w")) == NULL)
9436 cupsdLogMessage(CUPSD_LOG_ERROR,
9437 "Unable to save authentication info to \"%s\" - %s",
9438 filename, strerror(errno));
9442 fchown(cupsFileNumber(fp), 0, 0);
9443 fchmod(cupsFileNumber(fp), 0400);
9445 cupsFilePuts(fp, "CUPSD-AUTH-V3\n");
9448 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9450 cupsdClearString(job->auth_env + i);
9452 if (auth_info && auth_info->num_values == dest->num_auth_info_required)
9455 * Write 1 to 3 auth values...
9459 i < auth_info->num_values &&
9460 i < (int)(sizeof(job->auth_env) / sizeof(job->auth_env[0]));
9463 if (strcmp(dest->auth_info_required[i], "negotiate"))
9465 httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text, (int)strlen(auth_info->values[i].string.text));
9466 cupsFilePutConf(fp, dest->auth_info_required[i], line);
9469 cupsFilePutConf(fp, dest->auth_info_required[i],
9470 auth_info->values[i].string.text);
9472 if (!strcmp(dest->auth_info_required[i], "username"))
9473 cupsdSetStringf(job->auth_env + i, "AUTH_USERNAME=%s",
9474 auth_info->values[i].string.text);
9475 else if (!strcmp(dest->auth_info_required[i], "domain"))
9476 cupsdSetStringf(job->auth_env + i, "AUTH_DOMAIN=%s",
9477 auth_info->values[i].string.text);
9478 else if (!strcmp(dest->auth_info_required[i], "password"))
9479 cupsdSetStringf(job->auth_env + i, "AUTH_PASSWORD=%s",
9480 auth_info->values[i].string.text);
9481 else if (!strcmp(dest->auth_info_required[i], "negotiate"))
9482 cupsdSetStringf(job->auth_env + i, "AUTH_NEGOTIATE=%s",
9483 auth_info->values[i].string.text);
9488 else if (auth_info && auth_info->num_values == 2 &&
9489 dest->num_auth_info_required == 1 &&
9490 !strcmp(dest->auth_info_required[0], "negotiate"))
9493 * Allow fallback to username+password for Kerberized queues...
9496 httpEncode64_2(line, sizeof(line), auth_info->values[0].string.text, (int)strlen(auth_info->values[0].string.text));
9497 cupsFilePutConf(fp, "username", line);
9499 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s",
9500 auth_info->values[0].string.text);
9502 httpEncode64_2(line, sizeof(line), auth_info->values[1].string.text, (int)strlen(auth_info->values[1].string.text));
9503 cupsFilePutConf(fp, "password", line);
9505 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s",
9506 auth_info->values[1].string.text);
9508 else if (con->username[0])
9511 * Write the authenticated username...
9514 httpEncode64_2(line, sizeof(line), con->username, (int)strlen(con->username));
9515 cupsFilePutConf(fp, "username", line);
9517 cupsdSetStringf(job->auth_env + 0, "AUTH_USERNAME=%s", con->username);
9520 * Write the authenticated password...
9523 httpEncode64_2(line, sizeof(line), con->password, (int)strlen(con->password));
9524 cupsFilePutConf(fp, "password", line);
9526 cupsdSetStringf(job->auth_env + 1, "AUTH_PASSWORD=%s", con->password);
9530 if (con->gss_uid > 0)
9532 cupsFilePrintf(fp, "uid %d\n", (int)con->gss_uid);
9533 cupsdSetStringf(&job->auth_uid, "AUTH_UID=%d", (int)con->gss_uid);
9535 #endif /* HAVE_GSSAPI */
9538 * Write a random number of newlines to the end of the file...
9541 for (i = (CUPS_RAND() % 1024); i >= 0; i --)
9542 cupsFilePutChar(fp, '\n');
9545 * Close the file and return...
9553 * 'send_document()' - Send a file to a printer or class.
9557 send_document(cupsd_client_t *con, /* I - Client connection */
9558 ipp_attribute_t *uri) /* I - Printer URI */
9560 ipp_attribute_t *attr; /* Current attribute */
9561 ipp_attribute_t *format; /* Request's document-format attribute */
9562 ipp_attribute_t *jformat; /* Job's document-format attribute */
9563 const char *default_format;/* document-format-default value */
9564 int jobid; /* Job ID number */
9565 cupsd_job_t *job; /* Current job */
9566 char job_uri[HTTP_MAX_URI],
9568 scheme[HTTP_MAX_URI],
9569 /* Method portion of URI */
9570 username[HTTP_MAX_URI],
9571 /* Username portion of URI */
9573 /* Host portion of URI */
9574 resource[HTTP_MAX_URI];
9575 /* Resource portion of URI */
9576 int port; /* Port portion of URI */
9577 mime_type_t *filetype; /* Type of file */
9578 char super[MIME_MAX_SUPER],
9579 /* Supertype of file */
9580 type[MIME_MAX_TYPE],
9581 /* Subtype of file */
9582 mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
9583 /* Textual name of mime type */
9584 char filename[1024]; /* Job filename */
9585 cupsd_printer_t *printer; /* Current printer */
9586 struct stat fileinfo; /* File information */
9587 int kbytes; /* Size of file */
9588 int compression; /* Type of compression */
9589 int start_job; /* Start the job? */
9592 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
9593 con->number, uri->values[0].string.text);
9596 * See if we have a job URI or a printer URI...
9599 if (!strcmp(uri->name, "printer-uri"))
9602 * Got a printer URI; see if we also have a job-id attribute...
9605 if ((attr = ippFindAttribute(con->request, "job-id",
9606 IPP_TAG_INTEGER)) == NULL)
9608 send_ipp_status(con, IPP_BAD_REQUEST,
9609 _("Got a printer-uri attribute but no job-id."));
9613 jobid = attr->values[0].integer;
9618 * Got a job URI; parse it to get the job ID...
9621 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
9622 sizeof(scheme), username, sizeof(username), host,
9623 sizeof(host), &port, resource, sizeof(resource));
9625 if (strncmp(resource, "/jobs/", 6))
9631 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
9632 uri->values[0].string.text);
9636 jobid = atoi(resource + 6);
9640 * See if the job exists...
9643 if ((job = cupsdFindJob(jobid)) == NULL)
9646 * Nope - return a "not found" error...
9649 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
9653 printer = cupsdFindDest(job->dest);
9656 * See if the job is owned by the requesting user...
9659 if (!validate_user(job, con, job->username, username, sizeof(username)))
9661 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
9662 cupsdFindDest(job->dest));
9667 * OK, see if the client is sending the document compressed - CUPS
9668 * only supports "none" and "gzip".
9671 compression = CUPS_FILE_NONE;
9673 if ((attr = ippFindAttribute(con->request, "compression",
9674 IPP_TAG_KEYWORD)) != NULL)
9676 if (strcmp(attr->values[0].string.text, "none")
9678 && strcmp(attr->values[0].string.text, "gzip")
9679 #endif /* HAVE_LIBZ */
9682 send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"."),
9683 attr->values[0].string.text);
9684 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
9685 "compression", NULL, attr->values[0].string.text);
9690 if (!strcmp(attr->values[0].string.text, "gzip"))
9691 compression = CUPS_FILE_GZIP;
9692 #endif /* HAVE_LIBZ */
9696 * Do we have a file to print?
9699 if ((attr = ippFindAttribute(con->request, "last-document",
9700 IPP_TAG_BOOLEAN)) == NULL)
9702 send_ipp_status(con, IPP_BAD_REQUEST,
9703 _("Missing last-document attribute in request."));
9710 * Check for an empty request with "last-document" set to true, which is
9711 * used to close an "open" job by RFC 2911, section 3.3.2.
9714 if (job->num_files > 0 && attr->values[0].boolean)
9717 send_ipp_status(con, IPP_BAD_REQUEST, _("No file in print request."));
9722 * Is it a format we support?
9727 if ((format = ippFindAttribute(con->request, "document-format",
9728 IPP_TAG_MIMETYPE)) != NULL)
9731 * Grab format from client...
9734 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
9737 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"."),
9738 format->values[0].string.text);
9742 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-supplied", NULL, ippGetString(format, 0, NULL));
9744 else if ((default_format = cupsGetOption("document-format",
9745 printer->num_options,
9746 printer->options)) != NULL)
9749 * Use default document format...
9752 if (sscanf(default_format, "%15[^/]/%255[^;]", super, type) != 2)
9754 send_ipp_status(con, IPP_BAD_REQUEST,
9755 _("Bad document-format-default \"%s\"."), default_format);
9762 * No document format attribute? Auto-type it!
9765 strlcpy(super, "application", sizeof(super));
9766 strlcpy(type, "octet-stream", sizeof(type));
9769 if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
9772 * Auto-type the file...
9775 ipp_attribute_t *doc_name; /* document-name attribute */
9778 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Auto-typing file...");
9780 doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
9781 filetype = mimeFileType(MimeDatabase, con->filename,
9782 doc_name ? doc_name->values[0].string.text : NULL,
9786 filetype = mimeType(MimeDatabase, super, type);
9789 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Request file type is %s/%s.",
9790 filetype->super, filetype->type);
9792 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super, filetype->type);
9793 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format-detected", NULL, mimetype);
9796 filetype = mimeType(MimeDatabase, super, type);
9801 * Replace the document-format attribute value with the auto-typed or
9805 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9808 if ((jformat = ippFindAttribute(job->attrs, "document-format",
9809 IPP_TAG_MIMETYPE)) != NULL)
9810 ippSetString(job->attrs, &jformat, 0, mimetype);
9812 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
9813 "document-format", NULL, mimetype);
9817 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9818 _("Unsupported document-format \"%s/%s\"."), super, type);
9819 cupsdLogMessage(CUPSD_LOG_INFO,
9820 "Hint: Do you have the raw file printing rules enabled?");
9823 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9824 "document-format", NULL, format->values[0].string.text);
9829 if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
9831 snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
9834 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
9835 _("Unsupported document-format \"%s\"."), mimetype);
9837 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
9838 "document-format", NULL, mimetype);
9844 * Add the file to the job...
9847 if (add_file(con, job, filetype, compression))
9850 if ((attr = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME)) != NULL)
9851 ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "document-name-supplied", NULL, ippGetString(attr, 0, NULL));
9853 if (stat(con->filename, &fileinfo))
9856 kbytes = (fileinfo.st_size + 1023) / 1024;
9858 cupsdUpdateQuota(printer, job->username, 0, kbytes);
9860 job->koctets += kbytes;
9862 if ((attr = ippFindAttribute(job->attrs, "job-k-octets", IPP_TAG_INTEGER)) != NULL)
9863 attr->values[0].integer += kbytes;
9865 snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id, job->num_files);
9866 if (rename(con->filename, filename))
9868 cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to rename job document file \"%s\": %s", filename, strerror(errno));
9870 send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to rename job document file."));
9874 cupsdClearString(&con->filename);
9876 cupsdLogJob(job, CUPSD_LOG_INFO, "File of type %s/%s queued by \"%s\".",
9877 filetype->super, filetype->type, job->username);
9880 * Start the job if this is the last document...
9885 if ((attr = ippFindAttribute(con->request, "last-document",
9886 IPP_TAG_BOOLEAN)) != NULL &&
9887 attr->values[0].boolean)
9890 * See if we need to add the ending sheet...
9893 if (cupsdTimeoutJob(job))
9896 if (job->state_value == IPP_JOB_STOPPED)
9898 job->state->values[0].integer = IPP_JOB_PENDING;
9899 job->state_value = IPP_JOB_PENDING;
9901 ippSetString(job->attrs, &job->reasons, 0, "none");
9903 else if (job->state_value == IPP_JOB_HELD)
9905 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9906 IPP_TAG_KEYWORD)) == NULL)
9907 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9909 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9911 job->state->values[0].integer = IPP_JOB_PENDING;
9912 job->state_value = IPP_JOB_PENDING;
9914 ippSetString(job->attrs, &job->reasons, 0, "none");
9917 ippSetString(job->attrs, &job->reasons, 0, "job-hold-until-specified");
9921 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9927 if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
9928 IPP_TAG_KEYWORD)) == NULL)
9929 attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
9931 if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
9933 job->state->values[0].integer = IPP_JOB_HELD;
9934 job->state_value = IPP_JOB_HELD;
9935 job->hold_until = time(NULL) + MultipleOperationTimeout;
9937 ippSetString(job->attrs, &job->reasons, 0, "job-incoming");
9940 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
9947 * Fill in the response info...
9950 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
9951 con->clientname, con->clientport, "/jobs/%d", jobid);
9952 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
9955 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
9957 ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
9959 ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-state-reasons",
9960 NULL, job->reasons->values[0].string.text);
9962 con->response->request.status.status_code = IPP_OK;
9965 * Start the job if necessary...
9974 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9979 cupsd_client_t *con, /* I - Client connection */
9980 http_status_t status, /* I - HTTP status code */
9981 cupsd_printer_t *printer) /* I - Printer, if any */
9983 ipp_attribute_t *uri; /* Request URI, if any */
9986 if ((uri = ippFindAttribute(con->request, "printer-uri",
9987 IPP_TAG_URI)) == NULL)
9988 uri = ippFindAttribute(con->request, "job-uri", IPP_TAG_URI);
9990 cupsdLogMessage(status == HTTP_FORBIDDEN ? CUPSD_LOG_ERROR : CUPSD_LOG_DEBUG,
9991 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9992 con->number, httpStatus(status),
9994 ippOpString(con->request->request.op.operation_id) :
9996 uri ? uri->values[0].string.text : "no URI",
9997 con->http->hostname);
10001 int auth_type; /* Type of authentication required */
10004 auth_type = CUPSD_AUTH_NONE;
10006 if (status == HTTP_UNAUTHORIZED &&
10007 printer->num_auth_info_required > 0 &&
10008 !strcmp(printer->auth_info_required[0], "negotiate") &&
10010 (con->request->request.op.operation_id == IPP_PRINT_JOB ||
10011 con->request->request.op.operation_id == IPP_CREATE_JOB ||
10012 con->request->request.op.operation_id == CUPS_AUTHENTICATE_JOB))
10015 * Creating and authenticating jobs requires Kerberos...
10018 auth_type = CUPSD_AUTH_NEGOTIATE;
10023 * Use policy/location-defined authentication requirements...
10026 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
10027 cupsd_location_t *auth; /* Pointer to authentication element */
10030 if (printer->type & CUPS_PRINTER_CLASS)
10031 snprintf(resource, sizeof(resource), "/classes/%s", printer->name);
10033 snprintf(resource, sizeof(resource), "/printers/%s", printer->name);
10035 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
10036 auth->type == CUPSD_AUTH_NONE)
10037 auth = cupsdFindPolicyOp(printer->op_policy_ptr,
10039 con->request->request.op.operation_id :
10044 if (auth->type == CUPSD_AUTH_DEFAULT)
10045 auth_type = cupsdDefaultAuthType();
10047 auth_type = auth->type;
10051 cupsdSendError(con, status, auth_type);
10054 cupsdSendError(con, status, CUPSD_AUTH_NONE);
10056 ippDelete(con->response);
10057 con->response = NULL;
10064 * 'send_ipp_status()' - Send a status back to the IPP client.
10068 send_ipp_status(cupsd_client_t *con, /* I - Client connection */
10069 ipp_status_t status, /* I - IPP status code */
10070 const char *message,/* I - Status message */
10071 ...) /* I - Additional args as needed */
10073 va_list ap; /* Pointer to additional args */
10074 char formatted[1024]; /* Formatted errror message */
10077 va_start(ap, message);
10078 vsnprintf(formatted, sizeof(formatted),
10079 _cupsLangString(con->language, message), ap);
10082 cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
10083 ippOpString(con->request->request.op.operation_id),
10084 ippErrorString(status), formatted);
10086 con->response->request.status.status_code = status;
10088 if (ippFindAttribute(con->response, "attributes-charset",
10089 IPP_TAG_ZERO) == NULL)
10090 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
10091 "attributes-charset", NULL, "utf-8");
10093 if (ippFindAttribute(con->response, "attributes-natural-language",
10094 IPP_TAG_ZERO) == NULL)
10095 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
10096 "attributes-natural-language", NULL, DefaultLanguage);
10098 ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
10099 "status-message", NULL, formatted);
10104 * 'set_default()' - Set the default destination...
10108 set_default(cupsd_client_t *con, /* I - Client connection */
10109 ipp_attribute_t *uri) /* I - Printer URI */
10111 http_status_t status; /* Policy status */
10112 cups_ptype_t dtype; /* Destination type (printer/class) */
10113 cupsd_printer_t *printer, /* Printer */
10114 *oldprinter; /* Old default printer */
10117 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
10118 con->number, uri->values[0].string.text);
10121 * Is the destination valid?
10124 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10130 send_ipp_status(con, IPP_NOT_FOUND,
10131 _("The printer or class does not exist."));
10139 if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
10141 send_http_error(con, status, NULL);
10146 * Set it as the default...
10149 oldprinter = DefaultPrinter;
10150 DefaultPrinter = printer;
10153 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, oldprinter, NULL,
10154 "%s is no longer the default printer.", oldprinter->name);
10156 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, printer, NULL,
10157 "%s is now the default printer.", printer->name);
10159 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS | CUPSD_DIRTY_CLASSES |
10160 CUPSD_DIRTY_PRINTCAP);
10162 cupsdLogMessage(CUPSD_LOG_INFO,
10163 "Default destination set to \"%s\" by \"%s\".",
10164 printer->name, get_username(con));
10167 * Everything was ok, so return OK status...
10170 con->response->request.status.status_code = IPP_OK;
10175 * 'set_job_attrs()' - Set job attributes.
10179 set_job_attrs(cupsd_client_t *con, /* I - Client connection */
10180 ipp_attribute_t *uri) /* I - Job URI */
10182 ipp_attribute_t *attr, /* Current attribute */
10183 *attr2; /* Job attribute */
10184 int jobid; /* Job ID */
10185 cupsd_job_t *job; /* Current job */
10186 char scheme[HTTP_MAX_URI],
10187 /* Method portion of URI */
10188 username[HTTP_MAX_URI],
10189 /* Username portion of URI */
10190 host[HTTP_MAX_URI],
10191 /* Host portion of URI */
10192 resource[HTTP_MAX_URI];
10193 /* Resource portion of URI */
10194 int port; /* Port portion of URI */
10195 int event; /* Events? */
10196 int check_jobs; /* Check jobs? */
10199 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
10200 con->number, uri->values[0].string.text);
10203 * Start with "everything is OK" status...
10206 con->response->request.status.status_code = IPP_OK;
10209 * See if we have a job URI or a printer URI...
10212 if (!strcmp(uri->name, "printer-uri"))
10215 * Got a printer URI; see if we also have a job-id attribute...
10218 if ((attr = ippFindAttribute(con->request, "job-id",
10219 IPP_TAG_INTEGER)) == NULL)
10221 send_ipp_status(con, IPP_BAD_REQUEST,
10222 _("Got a printer-uri attribute but no job-id."));
10226 jobid = attr->values[0].integer;
10231 * Got a job URI; parse it to get the job ID...
10234 httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
10235 sizeof(scheme), username, sizeof(username), host,
10236 sizeof(host), &port, resource, sizeof(resource));
10238 if (strncmp(resource, "/jobs/", 6))
10244 send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri \"%s\"."),
10245 uri->values[0].string.text);
10249 jobid = atoi(resource + 6);
10253 * See if the job exists...
10256 if ((job = cupsdFindJob(jobid)) == NULL)
10259 * Nope - return a "not found" error...
10262 send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist."), jobid);
10267 * See if the job has been completed...
10270 if (job->state_value > IPP_JOB_STOPPED)
10273 * Return a "not-possible" error...
10276 send_ipp_status(con, IPP_NOT_POSSIBLE,
10277 _("Job #%d is finished and cannot be altered."), jobid);
10282 * See if the job is owned by the requesting user...
10285 if (!validate_user(job, con, job->username, username, sizeof(username)))
10287 send_http_error(con, con->username[0] ? HTTP_FORBIDDEN : HTTP_UNAUTHORIZED,
10288 cupsdFindDest(job->dest));
10293 * See what the user wants to change.
10301 for (attr = con->request->attrs; attr; attr = attr->next)
10303 if (attr->group_tag != IPP_TAG_JOB || !attr->name)
10306 if (!strcmp(attr->name, "attributes-charset") ||
10307 !strcmp(attr->name, "attributes-natural-language") ||
10308 !strncmp(attr->name, "date-time-at-", 13) ||
10309 !strncmp(attr->name, "document-compression", 20) ||
10310 !strncmp(attr->name, "document-format", 15) ||
10311 !strcmp(attr->name, "job-detailed-status-messages") ||
10312 !strcmp(attr->name, "job-document-access-errors") ||
10313 !strcmp(attr->name, "job-id") ||
10314 !strcmp(attr->name, "job-impressions-completed") ||
10315 !strcmp(attr->name, "job-k-octets-completed") ||
10316 !strcmp(attr->name, "job-media-sheets-completed") ||
10317 !strcmp(attr->name, "job-originating-host-name") ||
10318 !strcmp(attr->name, "job-originating-user-name") ||
10319 !strcmp(attr->name, "job-pages-completed") ||
10320 !strcmp(attr->name, "job-printer-up-time") ||
10321 !strcmp(attr->name, "job-printer-uri") ||
10322 !strcmp(attr->name, "job-sheets") ||
10323 !strcmp(attr->name, "job-state-message") ||
10324 !strcmp(attr->name, "job-state-reasons") ||
10325 !strcmp(attr->name, "job-uri") ||
10326 !strcmp(attr->name, "number-of-documents") ||
10327 !strcmp(attr->name, "number-of-intervening-jobs") ||
10328 !strcmp(attr->name, "output-device-assigned") ||
10329 !strncmp(attr->name, "time-at-", 8))
10335 send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
10336 _("%s cannot be changed."), attr->name);
10338 attr2 = ippCopyAttribute(con->response, attr, 0);
10339 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
10343 if (!strcmp(attr->name, "job-priority"))
10346 * Change the job priority...
10349 if (attr->value_tag != IPP_TAG_INTEGER)
10351 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value."));
10353 attr2 = ippCopyAttribute(con->response, attr, 0);
10354 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
10356 else if (job->state_value >= IPP_JOB_PROCESSING)
10358 send_ipp_status(con, IPP_NOT_POSSIBLE,
10359 _("Job is completed and cannot be changed."));
10362 else if (con->response->request.status.status_code == IPP_OK)
10364 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-priority to %d",
10365 attr->values[0].integer);
10366 cupsdSetJobPriority(job, attr->values[0].integer);
10369 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED |
10370 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED;
10373 else if (!strcmp(attr->name, "job-state"))
10376 * Change the job state...
10379 if (attr->value_tag != IPP_TAG_ENUM)
10381 send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value."));
10383 attr2 = ippCopyAttribute(con->response, attr, 0);
10384 ippSetGroupTag(con->response, &attr2, IPP_TAG_UNSUPPORTED_GROUP);
10388 switch (attr->values[0].integer)
10390 case IPP_JOB_PENDING :
10391 case IPP_JOB_HELD :
10392 if (job->state_value > IPP_JOB_HELD)
10394 send_ipp_status(con, IPP_NOT_POSSIBLE,
10395 _("Job state cannot be changed."));
10398 else if (con->response->request.status.status_code == IPP_OK)
10400 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10401 attr->values[0].integer);
10402 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer, CUPSD_JOB_DEFAULT, "Job state changed by \"%s\"", username);
10407 case IPP_JOB_PROCESSING :
10408 case IPP_JOB_STOPPED :
10409 if (job->state_value != attr->values[0].integer)
10411 send_ipp_status(con, IPP_NOT_POSSIBLE,
10412 _("Job state cannot be changed."));
10417 case IPP_JOB_CANCELED :
10418 case IPP_JOB_ABORTED :
10419 case IPP_JOB_COMPLETED :
10420 if (job->state_value > IPP_JOB_PROCESSING)
10422 send_ipp_status(con, IPP_NOT_POSSIBLE,
10423 _("Job state cannot be changed."));
10426 else if (con->response->request.status.status_code == IPP_OK)
10428 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-state to %d",
10429 attr->values[0].integer);
10430 cupsdSetJobState(job, (ipp_jstate_t)attr->values[0].integer,
10432 "Job state changed by \"%s\"", username);
10439 else if (con->response->request.status.status_code != IPP_OK)
10441 else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10442 IPP_TAG_ZERO)) != NULL)
10445 * Some other value; first free the old value...
10448 if (job->attrs->prev)
10449 job->attrs->prev->next = attr2->next;
10451 job->attrs->attrs = attr2->next;
10453 if (job->attrs->last == attr2)
10454 job->attrs->last = job->attrs->prev;
10456 ippDeleteAttribute(NULL, attr2);
10459 * Then copy the attribute...
10462 ippCopyAttribute(job->attrs, attr, 0);
10465 * See if the job-name or job-hold-until is being changed.
10468 if (!strcmp(attr->name, "job-hold-until"))
10470 cupsdLogJob(job, CUPSD_LOG_DEBUG, "Setting job-hold-until to %s",
10471 attr->values[0].string.text);
10472 cupsdSetJobHoldUntil(job, attr->values[0].string.text, 0);
10474 if (!strcmp(attr->values[0].string.text, "no-hold"))
10476 cupsdReleaseJob(job);
10480 cupsdSetJobState(job, IPP_JOB_HELD, CUPSD_JOB_DEFAULT,
10481 "Job held by \"%s\".", username);
10483 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
10486 else if (attr->value_tag == IPP_TAG_DELETEATTR)
10489 * Delete the attribute...
10492 if ((attr2 = ippFindAttribute(job->attrs, attr->name,
10493 IPP_TAG_ZERO)) != NULL)
10495 if (job->attrs->prev)
10496 job->attrs->prev->next = attr2->next;
10498 job->attrs->attrs = attr2->next;
10500 if (attr2 == job->attrs->last)
10501 job->attrs->last = job->attrs->prev;
10503 ippDeleteAttribute(NULL, attr2);
10505 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10511 * Add new option by copying it...
10514 ippCopyAttribute(job->attrs, attr, 0);
10516 event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
10525 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
10528 * Send events as needed...
10531 if (event & CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED)
10532 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED,
10533 cupsdFindDest(job->dest), job,
10534 "Job priority changed by user.");
10536 if (event & CUPSD_EVENT_JOB_STATE)
10537 cupsdAddEvent(CUPSD_EVENT_JOB_STATE, cupsdFindDest(job->dest), job,
10538 job->state_value == IPP_JOB_HELD ?
10539 "Job held by user." : "Job restarted by user.");
10541 if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
10542 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, cupsdFindDest(job->dest), job,
10543 "Job options changed by user.");
10546 * Start jobs if possible...
10555 * 'set_printer_attrs()' - Set printer attributes.
10559 set_printer_attrs(cupsd_client_t *con, /* I - Client connection */
10560 ipp_attribute_t *uri) /* I - Printer */
10562 http_status_t status; /* Policy status */
10563 cups_ptype_t dtype; /* Destination type (printer/class) */
10564 cupsd_printer_t *printer; /* Printer/class */
10565 ipp_attribute_t *attr; /* Printer attribute */
10566 int changed = 0; /* Was anything changed? */
10569 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_attrs(%p[%d], %s)", con,
10570 con->number, uri->values[0].string.text);
10573 * Is the destination valid?
10576 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10582 send_ipp_status(con, IPP_NOT_FOUND,
10583 _("The printer or class does not exist."));
10591 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10593 send_http_error(con, status, printer);
10598 * Return a list of attributes that can be set via Set-Printer-Attributes.
10601 if ((attr = ippFindAttribute(con->request, "printer-location",
10602 IPP_TAG_TEXT)) != NULL)
10604 cupsdSetString(&printer->location, attr->values[0].string.text);
10608 if ((attr = ippFindAttribute(con->request, "printer-geo-location", IPP_TAG_URI)) != NULL && !strncmp(attr->values[0].string.text, "geo:", 4))
10610 cupsdSetString(&printer->geo_location, attr->values[0].string.text);
10614 if ((attr = ippFindAttribute(con->request, "printer-organization", IPP_TAG_TEXT)) != NULL)
10616 cupsdSetString(&printer->organization, attr->values[0].string.text);
10620 if ((attr = ippFindAttribute(con->request, "printer-organizational-unit", IPP_TAG_TEXT)) != NULL)
10622 cupsdSetString(&printer->organizational_unit, attr->values[0].string.text);
10626 if ((attr = ippFindAttribute(con->request, "printer-info",
10627 IPP_TAG_TEXT)) != NULL)
10629 cupsdSetString(&printer->info, attr->values[0].string.text);
10634 * Update the printer attributes and return...
10639 printer->config_time = time(NULL);
10641 cupsdSetPrinterAttrs(printer);
10642 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
10644 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG, printer, NULL,
10645 "Printer \"%s\" description or location changed by \"%s\".",
10646 printer->name, get_username(con));
10648 cupsdLogMessage(CUPSD_LOG_INFO,
10649 "Printer \"%s\" description or location changed by \"%s\".",
10650 printer->name, get_username(con));
10653 con->response->request.status.status_code = IPP_OK;
10658 * 'set_printer_defaults()' - Set printer default options from a request.
10661 static int /* O - 1 on success, 0 on failure */
10662 set_printer_defaults(
10663 cupsd_client_t *con, /* I - Client connection */
10664 cupsd_printer_t *printer) /* I - Printer */
10666 int i; /* Looping var */
10667 ipp_attribute_t *attr; /* Current attribute */
10668 size_t namelen; /* Length of attribute name */
10669 char name[256], /* New attribute name */
10670 value[256]; /* String version of integer attrs */
10673 for (attr = con->request->attrs; attr; attr = attr->next)
10676 * Skip non-printer attributes...
10679 if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
10682 cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
10684 if (!strcmp(attr->name, "job-sheets-default"))
10687 * Only allow keywords and names...
10690 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10694 * Only allow job-sheets-default to be set when running without a
10695 * system high classification level...
10698 if (Classification)
10701 cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
10703 if (attr->num_values > 1)
10704 cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
10706 cupsdSetString(&printer->job_sheets[1], "none");
10708 else if (!strcmp(attr->name, "requesting-user-name-allowed"))
10710 cupsdFreeStrings(&(printer->users));
10712 printer->deny_users = 0;
10714 if (attr->value_tag == IPP_TAG_NAME &&
10715 (attr->num_values > 1 ||
10716 strcmp(attr->values[0].string.text, "all")))
10718 for (i = 0; i < attr->num_values; i ++)
10719 cupsdAddString(&(printer->users), attr->values[i].string.text);
10722 else if (!strcmp(attr->name, "requesting-user-name-denied"))
10724 cupsdFreeStrings(&(printer->users));
10726 printer->deny_users = 1;
10728 if (attr->value_tag == IPP_TAG_NAME &&
10729 (attr->num_values > 1 ||
10730 strcmp(attr->values[0].string.text, "none")))
10732 for (i = 0; i < attr->num_values; i ++)
10733 cupsdAddString(&(printer->users), attr->values[i].string.text);
10736 else if (!strcmp(attr->name, "job-quota-period"))
10738 if (attr->value_tag != IPP_TAG_INTEGER)
10741 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
10742 attr->values[0].integer);
10743 cupsdFreeQuotas(printer);
10745 printer->quota_period = attr->values[0].integer;
10747 else if (!strcmp(attr->name, "job-k-limit"))
10749 if (attr->value_tag != IPP_TAG_INTEGER)
10752 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
10753 attr->values[0].integer);
10754 cupsdFreeQuotas(printer);
10756 printer->k_limit = attr->values[0].integer;
10758 else if (!strcmp(attr->name, "job-page-limit"))
10760 if (attr->value_tag != IPP_TAG_INTEGER)
10763 cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
10764 attr->values[0].integer);
10765 cupsdFreeQuotas(printer);
10767 printer->page_limit = attr->values[0].integer;
10769 else if (!strcmp(attr->name, "printer-op-policy"))
10771 cupsd_policy_t *p; /* Policy */
10774 if (attr->value_tag != IPP_TAG_NAME)
10777 if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
10779 cupsdLogMessage(CUPSD_LOG_DEBUG,
10780 "Setting printer-op-policy to \"%s\"...",
10781 attr->values[0].string.text);
10782 cupsdSetString(&printer->op_policy, attr->values[0].string.text);
10783 printer->op_policy_ptr = p;
10787 send_ipp_status(con, IPP_NOT_POSSIBLE,
10788 _("Unknown printer-op-policy \"%s\"."),
10789 attr->values[0].string.text);
10793 else if (!strcmp(attr->name, "printer-error-policy"))
10795 if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
10798 if (strcmp(attr->values[0].string.text, "retry-current-job") &&
10799 ((printer->type & CUPS_PRINTER_CLASS) ||
10800 (strcmp(attr->values[0].string.text, "abort-job") &&
10801 strcmp(attr->values[0].string.text, "retry-job") &&
10802 strcmp(attr->values[0].string.text, "stop-printer"))))
10804 send_ipp_status(con, IPP_NOT_POSSIBLE,
10805 _("Unknown printer-error-policy \"%s\"."),
10806 attr->values[0].string.text);
10810 cupsdLogMessage(CUPSD_LOG_DEBUG,
10811 "Setting printer-error-policy to \"%s\"...",
10812 attr->values[0].string.text);
10813 cupsdSetString(&printer->error_policy, attr->values[0].string.text);
10817 * Skip any other non-default attributes...
10820 namelen = strlen(attr->name);
10821 if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
10822 namelen > (sizeof(name) - 1) || attr->num_values != 1)
10826 * OK, anything else must be a user-defined default...
10829 strlcpy(name, attr->name, sizeof(name));
10830 name[namelen - 8] = '\0'; /* Strip "-default" */
10832 switch (attr->value_tag)
10834 case IPP_TAG_DELETEATTR :
10835 printer->num_options = cupsRemoveOption(name,
10836 printer->num_options,
10837 &(printer->options));
10838 cupsdLogMessage(CUPSD_LOG_DEBUG,
10839 "Deleting %s", attr->name);
10842 case IPP_TAG_NAME :
10843 case IPP_TAG_TEXT :
10844 case IPP_TAG_KEYWORD :
10846 printer->num_options = cupsAddOption(name,
10847 attr->values[0].string.text,
10848 printer->num_options,
10849 &(printer->options));
10850 cupsdLogMessage(CUPSD_LOG_DEBUG,
10851 "Setting %s to \"%s\"...", attr->name,
10852 attr->values[0].string.text);
10855 case IPP_TAG_BOOLEAN :
10856 printer->num_options = cupsAddOption(name,
10857 attr->values[0].boolean ?
10859 printer->num_options,
10860 &(printer->options));
10861 cupsdLogMessage(CUPSD_LOG_DEBUG,
10862 "Setting %s to %s...", attr->name,
10863 attr->values[0].boolean ? "true" : "false");
10866 case IPP_TAG_INTEGER :
10867 case IPP_TAG_ENUM :
10868 sprintf(value, "%d", attr->values[0].integer);
10869 printer->num_options = cupsAddOption(name, value,
10870 printer->num_options,
10871 &(printer->options));
10872 cupsdLogMessage(CUPSD_LOG_DEBUG,
10873 "Setting %s to %s...", attr->name, value);
10876 case IPP_TAG_RANGE :
10877 sprintf(value, "%d-%d", attr->values[0].range.lower,
10878 attr->values[0].range.upper);
10879 printer->num_options = cupsAddOption(name, value,
10880 printer->num_options,
10881 &(printer->options));
10882 cupsdLogMessage(CUPSD_LOG_DEBUG,
10883 "Setting %s to %s...", attr->name, value);
10886 case IPP_TAG_RESOLUTION :
10887 sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
10888 attr->values[0].resolution.yres,
10889 attr->values[0].resolution.units == IPP_RES_PER_INCH ?
10891 printer->num_options = cupsAddOption(name, value,
10892 printer->num_options,
10893 &(printer->options));
10894 cupsdLogMessage(CUPSD_LOG_DEBUG,
10895 "Setting %s to %s...", attr->name, value);
10899 /* Do nothing for other values */
10909 * 'start_printer()' - Start a printer.
10913 start_printer(cupsd_client_t *con, /* I - Client connection */
10914 ipp_attribute_t *uri) /* I - Printer URI */
10916 int i; /* Temporary variable */
10917 http_status_t status; /* Policy status */
10918 cups_ptype_t dtype; /* Destination type (printer/class) */
10919 cupsd_printer_t *printer; /* Printer data */
10922 cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
10923 con->number, uri->values[0].string.text);
10926 * Is the destination valid?
10929 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
10935 send_ipp_status(con, IPP_NOT_FOUND,
10936 _("The printer or class does not exist."));
10944 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
10946 send_http_error(con, status, printer);
10951 * Start the printer...
10954 printer->state_message[0] = '\0';
10956 cupsdStartPrinter(printer, 1);
10958 if (dtype & CUPS_PRINTER_CLASS)
10959 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
10960 printer->name, get_username(con));
10962 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
10963 printer->name, get_username(con));
10971 if ((i = check_quotas(con, printer)) < 0)
10973 send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
10978 send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
10983 * Everything was ok, so return OK status...
10986 con->response->request.status.status_code = IPP_OK;
10991 * 'stop_printer()' - Stop a printer.
10995 stop_printer(cupsd_client_t *con, /* I - Client connection */
10996 ipp_attribute_t *uri) /* I - Printer URI */
10998 http_status_t status; /* Policy status */
10999 cups_ptype_t dtype; /* Destination type (printer/class) */
11000 cupsd_printer_t *printer; /* Printer data */
11001 ipp_attribute_t *attr; /* printer-state-message attribute */
11004 cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
11005 con->number, uri->values[0].string.text);
11008 * Is the destination valid?
11011 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11017 send_ipp_status(con, IPP_NOT_FOUND,
11018 _("The printer or class does not exist."));
11026 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11028 send_http_error(con, status, printer);
11033 * Stop the printer...
11036 if ((attr = ippFindAttribute(con->request, "printer-state-message",
11037 IPP_TAG_TEXT)) == NULL)
11038 strlcpy(printer->state_message, "Paused", sizeof(printer->state_message));
11041 strlcpy(printer->state_message, attr->values[0].string.text,
11042 sizeof(printer->state_message));
11045 cupsdStopPrinter(printer, 1);
11047 if (dtype & CUPS_PRINTER_CLASS)
11048 cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
11049 printer->name, get_username(con));
11051 cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
11052 printer->name, get_username(con));
11055 * Everything was ok, so return OK status...
11058 con->response->request.status.status_code = IPP_OK;
11063 * 'url_encode_attr()' - URL-encode a string attribute.
11067 url_encode_attr(ipp_attribute_t *attr, /* I - Attribute */
11068 char *buffer,/* I - String buffer */
11069 size_t bufsize)/* I - Size of buffer */
11071 int i; /* Looping var */
11072 char *bufptr, /* Pointer into buffer */
11073 *bufend; /* End of buffer */
11076 strlcpy(buffer, attr->name, bufsize);
11077 bufptr = buffer + strlen(buffer);
11078 bufend = buffer + bufsize - 1;
11080 for (i = 0; i < attr->num_values; i ++)
11082 if (bufptr >= bufend)
11090 if (bufptr >= bufend)
11095 bufptr = url_encode_string(attr->values[i].string.text, bufptr, (size_t)(bufend - bufptr + 1));
11097 if (bufptr >= bufend)
11108 * 'url_encode_string()' - URL-encode a string.
11111 static char * /* O - End of string */
11112 url_encode_string(const char *s, /* I - String */
11113 char *buffer, /* I - String buffer */
11114 size_t bufsize) /* I - Size of buffer */
11116 char *bufptr, /* Pointer into buffer */
11117 *bufend; /* End of buffer */
11118 static const char *hex = "0123456789ABCDEF";
11123 bufend = buffer + bufsize - 1;
11125 while (*s && bufptr < bufend)
11127 if (*s == ' ' || *s == '%' || *s == '+')
11129 if (bufptr >= (bufend - 2))
11133 *bufptr++ = hex[(*s >> 4) & 15];
11134 *bufptr++ = hex[*s & 15];
11138 else if (*s == '\'' || *s == '\\')
11140 if (bufptr >= (bufend - 1))
11157 * 'user_allowed()' - See if a user is allowed to print to a queue.
11160 static int /* O - 0 if not allowed, 1 if allowed */
11161 user_allowed(cupsd_printer_t *p, /* I - Printer or class */
11162 const char *username) /* I - Username */
11164 struct passwd *pw; /* User password data */
11165 char baseuser[256], /* Base username */
11166 *baseptr, /* Pointer to "@" in base username */
11167 *name; /* Current user name */
11170 if (cupsArrayCount(p->users) == 0)
11173 if (!strcmp(username, "root"))
11176 if (strchr(username, '@'))
11179 * Strip @REALM for username check...
11182 strlcpy(baseuser, username, sizeof(baseuser));
11184 if ((baseptr = strchr(baseuser, '@')) != NULL)
11187 username = baseuser;
11190 pw = getpwnam(username);
11193 for (name = (char *)cupsArrayFirst(p->users);
11195 name = (char *)cupsArrayNext(p->users))
11197 if (name[0] == '@')
11200 * Check group membership...
11203 if (cupsdCheckGroup(username, pw, name + 1))
11206 else if (name[0] == '#')
11212 if (cupsdCheckGroup(username, pw, name))
11215 else if (!_cups_strcasecmp(username, name))
11219 return ((name != NULL) != p->deny_users);
11224 * 'validate_job()' - Validate printer options and destination.
11228 validate_job(cupsd_client_t *con, /* I - Client connection */
11229 ipp_attribute_t *uri) /* I - Printer URI */
11231 http_status_t status; /* Policy status */
11232 ipp_attribute_t *attr; /* Current attribute */
11234 ipp_attribute_t *auth_info; /* auth-info attribute */
11235 #endif /* HAVE_SSL */
11236 ipp_attribute_t *format, /* Document-format attribute */
11237 *name; /* Job-name attribute */
11238 cups_ptype_t dtype; /* Destination type (printer/class) */
11239 char super[MIME_MAX_SUPER],
11240 /* Supertype of file */
11241 type[MIME_MAX_TYPE];
11242 /* Subtype of file */
11243 cupsd_printer_t *printer; /* Printer */
11246 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
11247 con->number, uri->values[0].string.text);
11250 * OK, see if the client is sending the document compressed - CUPS
11251 * doesn't support compression yet...
11254 if ((attr = ippFindAttribute(con->request, "compression",
11255 IPP_TAG_KEYWORD)) != NULL)
11257 if (strcmp(attr->values[0].string.text, "none")
11259 && strcmp(attr->values[0].string.text, "gzip")
11260 #endif /* HAVE_LIBZ */
11263 send_ipp_status(con, IPP_ATTRIBUTES,
11264 _("Unsupported 'compression' value \"%s\"."),
11265 attr->values[0].string.text);
11266 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
11267 "compression", NULL, attr->values[0].string.text);
11273 * Is it a format we support?
11276 if ((format = ippFindAttribute(con->request, "document-format",
11277 IPP_TAG_MIMETYPE)) != NULL)
11279 if (sscanf(format->values[0].string.text, "%15[^/]/%255[^;]",
11282 send_ipp_status(con, IPP_BAD_REQUEST,
11283 _("Bad 'document-format' value \"%s\"."),
11284 format->values[0].string.text);
11288 if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
11289 !mimeType(MimeDatabase, super, type))
11291 cupsdLogMessage(CUPSD_LOG_INFO,
11292 "Hint: Do you have the raw file printing rules enabled?");
11293 send_ipp_status(con, IPP_DOCUMENT_FORMAT,
11294 _("Unsupported 'document-format' value \"%s\"."),
11295 format->values[0].string.text);
11296 ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
11297 "document-format", NULL, format->values[0].string.text);
11303 * Is the job-name valid?
11306 if ((name = ippFindAttribute(con->request, "job-name", IPP_TAG_ZERO)) != NULL)
11308 int bad_name = 0; /* Is the job-name value bad? */
11310 if ((name->value_tag != IPP_TAG_NAME && name->value_tag != IPP_TAG_NAMELANG) ||
11311 name->num_values != 1)
11318 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
11319 * IPP Everywhere requirements for "name" values...
11322 const unsigned char *nameptr; /* Pointer into "job-name" attribute */
11324 for (nameptr = (unsigned char *)name->values[0].string.text;
11328 if (*nameptr < ' ' && *nameptr != '\t')
11330 else if (*nameptr == 0x7f)
11332 else if ((*nameptr & 0xe0) == 0xc0)
11334 if ((nameptr[1] & 0xc0) != 0x80)
11339 else if ((*nameptr & 0xf0) == 0xe0)
11341 if ((nameptr[1] & 0xc0) != 0x80 ||
11342 (nameptr[2] & 0xc0) != 0x80)
11347 else if ((*nameptr & 0xf8) == 0xf0)
11349 if ((nameptr[1] & 0xc0) != 0x80 ||
11350 (nameptr[2] & 0xc0) != 0x80 ||
11351 (nameptr[3] & 0xc0) != 0x80)
11356 else if (*nameptr & 0x80)
11366 if (StrictConformance)
11368 send_ipp_status(con, IPP_ATTRIBUTES,
11369 _("Unsupported 'job-name' value."));
11370 ippCopyAttribute(con->response, name, 0);
11375 cupsdLogMessage(CUPSD_LOG_WARN,
11376 "Unsupported 'job-name' value, deleting from request.");
11377 ippDeleteAttribute(con->request, name);
11383 * Is the destination valid?
11386 if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
11392 send_ipp_status(con, IPP_NOT_FOUND,
11393 _("The printer or class does not exist."));
11402 auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
11403 #endif /* HAVE_SSL */
11405 if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
11407 send_http_error(con, status, printer);
11410 else if (printer->num_auth_info_required == 1 &&
11411 !strcmp(printer->auth_info_required[0], "negotiate") &&
11414 send_http_error(con, HTTP_UNAUTHORIZED, printer);
11418 else if (auth_info && !con->http->tls &&
11419 !httpAddrLocalhost(con->http->hostaddr))
11422 * Require encryption of auth-info over non-local connections...
11425 send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
11428 #endif /* HAVE_SSL */
11431 * Everything was ok, so return OK status...
11434 con->response->request.status.status_code = IPP_OK;
11439 * 'validate_name()' - Make sure the printer name only contains valid chars.
11442 static int /* O - 0 if name is no good, 1 if good */
11443 validate_name(const char *name) /* I - Name to check */
11445 const char *ptr; /* Pointer into name */
11449 * Scan the whole name...
11452 for (ptr = name; *ptr; ptr ++)
11453 if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
11457 * All the characters are good; validate the length, too...
11460 return ((ptr - name) < 128);
11465 * 'validate_user()' - Validate the user for the request.
11468 static int /* O - 1 if permitted, 0 otherwise */
11469 validate_user(cupsd_job_t *job, /* I - Job */
11470 cupsd_client_t *con, /* I - Client connection */
11471 const char *owner, /* I - Owner of job/resource */
11472 char *username, /* O - Authenticated username */
11473 size_t userlen) /* I - Length of username */
11475 cupsd_printer_t *printer; /* Printer for job */
11478 cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, userlen=" CUPS_LLFMT ")", job->id, con ? con->number : 0, owner ? owner : "(null)", username, CUPS_LLCAST userlen);
11481 * Validate input...
11484 if (!con || !owner || !username || userlen <= 0)
11488 * Get the best authenticated username that is available.
11491 strlcpy(username, get_username(con), userlen);
11494 * Check the username against the owner...
11497 printer = cupsdFindDest(job->dest);
11499 return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
11500 con, owner) == HTTP_OK);