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 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1602 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1603 _("Unable to add job for destination \"%s\"."),
1608 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
1609 job
->attrs
= con
->request
;
1611 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1613 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1616 apply_printer_defaults(printer
, job
);
1618 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1620 if (con
->username
[0])
1622 cupsdSetString(&job
->username
, con
->username
);
1625 ippSetString(job
->attrs
, &attr
, 0, con
->username
);
1629 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1630 "add_job: requesting-user-name=\"%s\"",
1631 attr
->values
[0].string
.text
);
1633 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1636 cupsdSetString(&job
->username
, "anonymous");
1639 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1640 "job-originating-user-name", NULL
, job
->username
);
1643 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1644 ippSetName(job
->attrs
, &attr
, "job-originating-user-name");
1647 if (con
->username
[0] || auth_info
)
1649 save_auth_info(con
, job
, auth_info
);
1652 * Remove the auth-info attribute from the attribute data...
1656 ippDeleteAttribute(job
->attrs
, auth_info
);
1659 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_NAME
)) != NULL
)
1660 cupsdSetString(&(job
->name
), attr
->values
[0].string
.text
);
1662 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1663 IPP_TAG_ZERO
)) != NULL
)
1666 * Request contains a job-originating-host-name attribute; validate it...
1669 if (attr
->value_tag
!= IPP_TAG_NAME
||
1670 attr
->num_values
!= 1 ||
1671 strcmp(con
->http
->hostname
, "localhost"))
1674 * Can't override the value if we aren't connected via localhost.
1675 * Also, we can only have 1 value and it must be a name value.
1678 ippDeleteAttribute(job
->attrs
, attr
);
1679 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-originating-host-name", NULL
, con
->http
->hostname
);
1682 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1687 * No job-originating-host-name attribute, so use the hostname from
1691 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1692 "job-originating-host-name", NULL
, con
->http
->hostname
);
1695 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-completed");
1696 ippAddDate(job
->attrs
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(time(NULL
)));
1697 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-processing");
1698 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-completed");
1699 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", time(NULL
));
1700 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-processing");
1703 * Add remaining job attributes...
1706 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1707 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1708 "job-state", IPP_JOB_STOPPED
);
1709 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1710 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1711 "job-state-reasons", NULL
, "job-incoming");
1712 job
->impressions
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", 0);
1713 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1714 "job-media-sheets-completed", 0);
1715 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1718 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1719 attr
->values
[0].integer
= 0;
1721 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1723 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1724 IPP_TAG_KEYWORD
)) == NULL
)
1725 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1728 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1729 printer
->options
)) == NULL
)
1732 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1733 "job-hold-until", NULL
, val
);
1736 if (printer
->holding_new_jobs
)
1739 * Hold all new jobs on this printer...
1742 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1743 cupsdSetJobHoldUntil(job
, ippGetString(attr
, 0, NULL
), 0);
1745 cupsdSetJobHoldUntil(job
, "indefinite", 0);
1747 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1748 job
->state_value
= IPP_JOB_HELD
;
1750 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-held-on-create");
1752 else if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1755 * Hold job until specified time...
1758 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
1760 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1761 job
->state_value
= IPP_JOB_HELD
;
1763 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
1765 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1767 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
1768 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1769 job
->state_value
= IPP_JOB_HELD
;
1773 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1774 job
->state_value
= IPP_JOB_PENDING
;
1776 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1779 if (!(printer
->type
& CUPS_PRINTER_REMOTE
) || Classification
)
1782 * Add job sheets options...
1785 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1786 IPP_TAG_ZERO
)) == NULL
)
1788 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1789 "Adding default job-sheets values \"%s,%s\"...",
1790 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1792 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1794 ippSetString(job
->attrs
, &attr
, 0, printer
->job_sheets
[0]);
1795 ippSetString(job
->attrs
, &attr
, 1, printer
->job_sheets
[1]);
1798 job
->job_sheets
= attr
;
1801 * Enforce classification level if set...
1806 cupsdLogMessage(CUPSD_LOG_INFO
,
1807 "Classification=\"%s\", ClassifyOverride=%d",
1808 Classification
? Classification
: "(null)",
1811 if (ClassifyOverride
)
1813 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1814 (attr
->num_values
== 1 ||
1815 !strcmp(attr
->values
[1].string
.text
, "none")))
1818 * Force the leading banner to have the classification on it...
1821 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1823 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1824 "job-sheets=\"%s,none\", "
1825 "job-originating-user-name=\"%s\"",
1826 Classification
, job
->username
);
1828 else if (attr
->num_values
== 2 &&
1829 strcmp(attr
->values
[0].string
.text
,
1830 attr
->values
[1].string
.text
) &&
1831 strcmp(attr
->values
[0].string
.text
, "none") &&
1832 strcmp(attr
->values
[1].string
.text
, "none"))
1835 * Can't put two different security markings on the same document!
1838 ippSetString(job
->attrs
, &attr
, 1, attr
->values
[0].string
.text
);
1840 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1841 "job-sheets=\"%s,%s\", "
1842 "job-originating-user-name=\"%s\"",
1843 attr
->values
[0].string
.text
,
1844 attr
->values
[1].string
.text
, job
->username
);
1846 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1847 strcmp(attr
->values
[0].string
.text
, "none") &&
1848 (attr
->num_values
== 1 ||
1849 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1850 strcmp(attr
->values
[1].string
.text
, "none"))))
1852 if (attr
->num_values
== 1)
1853 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1854 "CLASSIFICATION OVERRIDDEN "
1855 "job-sheets=\"%s\", "
1856 "job-originating-user-name=\"%s\"",
1857 attr
->values
[0].string
.text
, job
->username
);
1859 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1860 "CLASSIFICATION OVERRIDDEN "
1861 "job-sheets=\"%s,%s\",fffff "
1862 "job-originating-user-name=\"%s\"",
1863 attr
->values
[0].string
.text
,
1864 attr
->values
[1].string
.text
, job
->username
);
1867 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1868 (attr
->num_values
== 1 ||
1869 strcmp(attr
->values
[1].string
.text
, Classification
)))
1872 * Force the banner to have the classification on it...
1875 if (attr
->num_values
> 1 &&
1876 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1878 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1879 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1883 if (attr
->num_values
== 1 ||
1884 strcmp(attr
->values
[0].string
.text
, "none"))
1885 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1887 if (attr
->num_values
> 1 &&
1888 strcmp(attr
->values
[1].string
.text
, "none"))
1889 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1892 if (attr
->num_values
> 1)
1893 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1894 "CLASSIFICATION FORCED "
1895 "job-sheets=\"%s,%s\", "
1896 "job-originating-user-name=\"%s\"",
1897 attr
->values
[0].string
.text
,
1898 attr
->values
[1].string
.text
, job
->username
);
1900 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1901 "CLASSIFICATION FORCED "
1902 "job-sheets=\"%s\", "
1903 "job-originating-user-name=\"%s\"",
1904 Classification
, job
->username
);
1909 * See if we need to add the starting sheet...
1912 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1914 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1915 attr
->values
[0].string
.text
);
1917 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1919 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1920 "Aborting job because the start banner could not be "
1925 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1928 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1929 IPP_TAG_ZERO
)) != NULL
)
1930 job
->job_sheets
= attr
;
1933 * Fill in the response info...
1936 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
1937 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
1938 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1941 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1943 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1945 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_TEXT
, "job-state-message", NULL
, "");
1946 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
1947 NULL
, job
->reasons
->values
[0].string
.text
);
1949 con
->response
->request
.status
.status_code
= IPP_OK
;
1952 * Add any job subscriptions...
1955 add_job_subscriptions(con
, job
);
1958 * Set all but the first two attributes to the job attributes group...
1961 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1962 attr
->group_tag
= IPP_TAG_JOB
;
1965 * Fire the "job created" event...
1968 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1971 * Return the new job...
1979 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1983 add_job_subscriptions(
1984 cupsd_client_t
*con
, /* I - Client connection */
1985 cupsd_job_t
*job
) /* I - Newly created job */
1987 int i
; /* Looping var */
1988 ipp_attribute_t
*prev
, /* Previous attribute */
1989 *next
, /* Next attribute */
1990 *attr
; /* Current attribute */
1991 cupsd_subscription_t
*sub
; /* Subscription object */
1992 const char *recipient
, /* notify-recipient-uri */
1993 *pullmethod
; /* notify-pull-method */
1994 ipp_attribute_t
*user_data
; /* notify-user-data */
1995 int interval
; /* notify-time-interval */
1996 unsigned mask
; /* notify-events */
2000 * Find the first subscription group attribute; return if we have
2004 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
2005 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
2012 * Process the subscription attributes in the request...
2021 mask
= CUPSD_EVENT_NONE
;
2023 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
2025 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
2026 attr
->value_tag
== IPP_TAG_URI
)
2029 * Validate the recipient scheme against the ServerBin/notifier
2033 char notifier
[1024], /* Notifier filename */
2034 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2035 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2036 host
[HTTP_MAX_URI
], /* Host portion of URI */
2037 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2038 int port
; /* Port portion of URI */
2041 recipient
= attr
->values
[0].string
.text
;
2043 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
2044 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2045 host
, sizeof(host
), &port
,
2046 resource
, sizeof(resource
)) < HTTP_URI_OK
)
2048 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2049 _("Bad notify-recipient-uri \"%s\"."), recipient
);
2050 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2051 "notify-status-code", IPP_URI_SCHEME
);
2055 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
2057 if (access(notifier
, X_OK
))
2059 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2060 _("notify-recipient-uri URI \"%s\" uses unknown "
2061 "scheme."), recipient
);
2062 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2063 "notify-status-code", IPP_URI_SCHEME
);
2067 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2069 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2070 _("notify-recipient-uri URI \"%s\" is already used."),
2072 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2073 "notify-status-code", IPP_ATTRIBUTES
);
2077 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2078 attr
->value_tag
== IPP_TAG_KEYWORD
)
2080 pullmethod
= attr
->values
[0].string
.text
;
2082 if (strcmp(pullmethod
, "ippget"))
2084 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2085 _("Bad notify-pull-method \"%s\"."), pullmethod
);
2086 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2087 "notify-status-code", IPP_ATTRIBUTES
);
2091 else if (!strcmp(attr
->name
, "notify-charset") &&
2092 attr
->value_tag
== IPP_TAG_CHARSET
&&
2093 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2094 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2096 send_ipp_status(con
, IPP_CHARSET
,
2097 _("Character set \"%s\" not supported."),
2098 attr
->values
[0].string
.text
);
2101 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2102 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2103 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2105 send_ipp_status(con
, IPP_CHARSET
,
2106 _("Language \"%s\" not supported."),
2107 attr
->values
[0].string
.text
);
2110 else if (!strcmp(attr
->name
, "notify-user-data") &&
2111 attr
->value_tag
== IPP_TAG_STRING
)
2113 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2115 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2116 _("The notify-user-data value is too large "
2117 "(%d > 63 octets)."),
2118 attr
->values
[0].unknown
.length
);
2124 else if (!strcmp(attr
->name
, "notify-events") &&
2125 attr
->value_tag
== IPP_TAG_KEYWORD
)
2127 for (i
= 0; i
< attr
->num_values
; i
++)
2128 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2130 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2132 send_ipp_status(con
, IPP_BAD_REQUEST
,
2133 _("The notify-lease-duration attribute cannot be "
2134 "used with job subscriptions."));
2137 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2138 attr
->value_tag
== IPP_TAG_INTEGER
)
2139 interval
= attr
->values
[0].integer
;
2144 if (!recipient
&& !pullmethod
)
2147 if (mask
== CUPSD_EVENT_NONE
)
2148 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2150 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2151 recipient
, 0)) != NULL
)
2153 sub
->interval
= interval
;
2155 cupsdSetString(&sub
->owner
, job
->username
);
2159 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2160 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2161 (size_t)sub
->user_data_len
);
2164 ippAddSeparator(con
->response
);
2165 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2166 "notify-subscription-id", sub
->id
);
2168 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
2176 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2179 * Remove all of the subscription attributes from the job request...
2181 * TODO: Optimize this since subscription groups have to come at the
2182 * end of the request...
2185 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2189 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2190 attr
->group_tag
== IPP_TAG_ZERO
)
2193 * Free and remove this attribute...
2196 ippDeleteAttribute(NULL
, attr
);
2201 job
->attrs
->attrs
= next
;
2207 job
->attrs
->last
= prev
;
2208 job
->attrs
->current
= prev
;
2213 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2215 * See RFC 4122 for the definition of UUIDs and the format.
2219 add_job_uuid(cupsd_job_t
*job
) /* I - Job */
2221 char uuid
[64]; /* job-uuid string */
2225 * Add a job-uuid attribute if none exists...
2228 if (!ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2229 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
,
2230 httpAssembleUUID(ServerName
, RemotePort
, job
->dest
, job
->id
,
2231 uuid
, sizeof(uuid
)));
2236 * 'add_printer()' - Add a printer to the system.
2240 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2241 ipp_attribute_t
*uri
) /* I - URI of printer */
2243 http_status_t status
; /* Policy status */
2244 int i
; /* Looping var */
2245 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2246 username
[HTTP_MAX_URI
], /* Username portion of URI */
2247 host
[HTTP_MAX_URI
], /* Host portion of URI */
2248 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2249 int port
; /* Port portion of URI */
2250 cupsd_printer_t
*printer
; /* Printer/class */
2251 ipp_attribute_t
*attr
; /* Printer attribute */
2252 cups_file_t
*fp
; /* Script/PPD file */
2253 char line
[1024]; /* Line from file... */
2254 char srcfile
[1024], /* Source Script/PPD file */
2255 dstfile
[1024]; /* Destination Script/PPD file */
2256 int modify
; /* Non-zero if we are modifying */
2257 int changed_driver
, /* Changed the PPD? */
2258 need_restart_job
, /* Need to restart job? */
2259 set_device_uri
, /* Did we set the device URI? */
2260 set_port_monitor
; /* Did we set the port monitor? */
2263 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2264 con
->number
, uri
->values
[0].string
.text
);
2267 * Do we have a valid URI?
2270 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2271 sizeof(scheme
), username
, sizeof(username
), host
,
2272 sizeof(host
), &port
, resource
, sizeof(resource
));
2274 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2277 * No, return an error...
2280 send_ipp_status(con
, IPP_BAD_REQUEST
,
2281 _("The printer-uri must be of the form "
2282 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2287 * Do we have a valid printer name?
2290 if (!validate_name(resource
+ 10))
2293 * No, return an error...
2296 send_ipp_status(con
, IPP_BAD_REQUEST
,
2297 _("The printer-uri \"%s\" contains invalid characters."),
2298 uri
->values
[0].string
.text
);
2303 * See if the printer already exists; if not, create a new printer...
2306 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2309 * Printer doesn't exist; see if we have a class of the same name...
2312 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
)
2315 * Yes, return an error...
2318 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2319 _("A class named \"%s\" already exists."),
2325 * No, check the default policy then add the printer...
2328 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2330 send_http_error(con
, status
, NULL
);
2334 printer
= cupsdAddPrinter(resource
+ 10);
2337 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2340 send_http_error(con
, status
, printer
);
2347 * Look for attributes and copy them over as needed...
2351 need_restart_job
= 0;
2353 if ((attr
= ippFindAttribute(con
->request
, "printer-is-temporary", IPP_TAG_BOOLEAN
)) != NULL
)
2354 printer
->temporary
= ippGetBoolean(attr
, 0);
2356 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2357 IPP_TAG_TEXT
)) != NULL
)
2358 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2360 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
2361 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
2363 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
2364 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
2366 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
2367 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
2369 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2370 IPP_TAG_TEXT
)) != NULL
)
2371 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2375 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2376 IPP_TAG_URI
)) != NULL
)
2379 * Do we have a valid device URI?
2382 http_uri_status_t uri_status
; /* URI separation status */
2383 char old_device_uri
[1024];
2384 /* Old device URI */
2386 need_restart_job
= 1;
2388 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2389 attr
->values
[0].string
.text
,
2390 scheme
, sizeof(scheme
),
2391 username
, sizeof(username
),
2392 host
, sizeof(host
), &port
,
2393 resource
, sizeof(resource
));
2395 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s device-uri: %s", printer
->name
, httpURIStatusString(uri_status
));
2397 if (uri_status
< HTTP_URI_OK
)
2399 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"."),
2400 attr
->values
[0].string
.text
);
2402 cupsdDeletePrinter(printer
, 0);
2407 if (!strcmp(scheme
, "file"))
2410 * See if the administrator has enabled file devices...
2413 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2416 * File devices are disabled and the URL is not file:/dev/null...
2419 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2420 _("File device URIs have been disabled. "
2421 "To enable, see the FileDevice directive in "
2422 "\"%s/cups-files.conf\"."),
2425 cupsdDeletePrinter(printer
, 0);
2433 * See if the backend exists and is executable...
2436 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2437 if (access(srcfile
, X_OK
))
2440 * Could not find device in list!
2443 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2444 _("Bad device-uri scheme \"%s\"."), scheme
);
2446 cupsdDeletePrinter(printer
, 0);
2452 if (printer
->sanitized_device_uri
)
2453 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2454 sizeof(old_device_uri
));
2456 old_device_uri
[0] = '\0';
2458 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2460 cupsdLogMessage(CUPSD_LOG_INFO
,
2461 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2462 printer
->name
, printer
->sanitized_device_uri
,
2468 set_port_monitor
= 0;
2470 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2471 IPP_TAG_NAME
)) != NULL
)
2473 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2476 need_restart_job
= 1;
2478 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2482 for (i
= 0; i
< supported
->num_values
; i
++)
2483 if (!strcmp(supported
->values
[i
].string
.text
,
2484 attr
->values
[0].string
.text
))
2488 if (!supported
|| i
>= supported
->num_values
)
2490 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"."),
2491 attr
->values
[0].string
.text
);
2493 cupsdDeletePrinter(printer
, 0);
2498 cupsdLogMessage(CUPSD_LOG_INFO
,
2499 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2500 printer
->name
, attr
->values
[0].string
.text
,
2501 printer
->port_monitor
? printer
->port_monitor
: "none");
2503 if (strcmp(attr
->values
[0].string
.text
, "none"))
2504 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2506 cupsdClearString(&printer
->port_monitor
);
2508 set_port_monitor
= 1;
2511 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2512 IPP_TAG_BOOLEAN
)) != NULL
&&
2513 attr
->values
[0].boolean
!= printer
->accepting
)
2515 cupsdLogMessage(CUPSD_LOG_INFO
,
2516 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2517 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2519 printer
->accepting
= attr
->values
[0].boolean
;
2521 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2522 "%s accepting jobs.",
2523 printer
->accepting
? "Now" : "No longer");
2526 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared", IPP_TAG_BOOLEAN
)) != NULL
)
2528 if (ippGetBoolean(attr
, 0) &&
2529 printer
->num_auth_info_required
== 1 &&
2530 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2532 send_ipp_status(con
, IPP_BAD_REQUEST
,
2533 _("Cannot share a remote Kerberized printer."));
2535 cupsdDeletePrinter(printer
, 0);
2540 if (printer
->type
& CUPS_PRINTER_REMOTE
)
2543 * Cannot re-share remote printers.
2546 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Cannot change printer-is-shared for remote queues."));
2548 cupsdDeletePrinter(printer
, 0);
2553 if (printer
->shared
&& !ippGetBoolean(attr
, 0))
2554 cupsdDeregisterPrinter(printer
, 1);
2556 cupsdLogMessage(CUPSD_LOG_INFO
,
2557 "Setting %s printer-is-shared to %d (was %d.)",
2558 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2560 printer
->shared
= ippGetBoolean(attr
, 0);
2561 if (printer
->shared
&& printer
->temporary
)
2562 printer
->temporary
= 0;
2565 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2566 IPP_TAG_ENUM
)) != NULL
)
2568 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2569 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2571 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d."),
2572 attr
->values
[0].integer
);
2574 cupsdDeletePrinter(printer
, 0);
2579 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2580 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2582 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2583 cupsdStopPrinter(printer
, 0);
2586 need_restart_job
= 1;
2587 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2591 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2592 IPP_TAG_TEXT
)) != NULL
)
2594 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2595 sizeof(printer
->state_message
));
2597 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
, "%s",
2598 printer
->state_message
);
2601 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2602 IPP_TAG_KEYWORD
)) != NULL
)
2604 if (attr
->num_values
>
2605 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2607 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2608 _("Too many printer-state-reasons values (%d > %d)."),
2610 (int)(sizeof(printer
->reasons
) /
2611 sizeof(printer
->reasons
[0])));
2613 cupsdDeletePrinter(printer
, 0);
2618 for (i
= 0; i
< printer
->num_reasons
; i
++)
2619 _cupsStrFree(printer
->reasons
[i
]);
2621 printer
->num_reasons
= 0;
2622 for (i
= 0; i
< attr
->num_values
; i
++)
2624 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2627 printer
->reasons
[printer
->num_reasons
] =
2628 _cupsStrRetain(attr
->values
[i
].string
.text
);
2629 printer
->num_reasons
++;
2631 if (!strcmp(attr
->values
[i
].string
.text
, "paused") &&
2632 printer
->state
!= IPP_PRINTER_STOPPED
)
2634 cupsdLogMessage(CUPSD_LOG_INFO
,
2635 "Setting %s printer-state to %d (was %d.)",
2636 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2637 cupsdStopPrinter(printer
, 0);
2641 if (PrintcapFormat
== PRINTCAP_PLIST
)
2642 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2644 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2645 "Printer \"%s\" state changed.", printer
->name
);
2648 if (!set_printer_defaults(con
, printer
))
2651 cupsdDeletePrinter(printer
, 0);
2656 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2657 IPP_TAG_KEYWORD
)) != NULL
)
2658 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2661 * See if we have all required attributes...
2664 if (!printer
->device_uri
)
2665 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2668 * See if we have a PPD file attached to the request...
2673 need_restart_job
= 1;
2676 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2678 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2681 * Yes; get the first line from it...
2685 cupsFileGets(fp
, line
, sizeof(line
));
2689 * Then see what kind of file it is...
2692 if (strncmp(line
, "*PPD-Adobe", 10))
2694 send_ipp_status(con
, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED
, _("Bad PPD file."));
2696 cupsdDeletePrinter(printer
, 0);
2701 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2705 * The new file is a PPD file, so move the file over to the ppd
2709 if (copy_file(srcfile
, dstfile
, ConfigFilePerm
))
2711 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file - %s"), strerror(errno
));
2713 cupsdDeletePrinter(printer
, 0);
2718 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2721 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
)) != NULL
)
2723 const char *ppd_name
= ippGetString(attr
, 0, NULL
);
2724 /* ppd-name value */
2726 need_restart_job
= 1;
2729 if (!strcmp(ppd_name
, "raw"))
2732 * Raw driver, remove any existing PPD file.
2735 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2738 else if (strstr(ppd_name
, "../"))
2740 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
2742 cupsdDeletePrinter(printer
, 0);
2752 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2754 if (copy_model(con
, ppd_name
, dstfile
))
2756 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file."));
2758 cupsdDeletePrinter(printer
, 0);
2763 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2770 * If we changed the PPD, then remove the printer's cache file and clear the
2771 * printer-state-reasons...
2774 char cache_name
[1024]; /* Cache filename for printer attrs */
2776 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.data", CacheDir
, printer
->name
);
2779 cupsdSetPrinterReasons(printer
, "none");
2782 * (Re)register color profiles...
2785 cupsdRegisterColor(printer
);
2789 * If we set the device URI but not the port monitor, check which port
2790 * monitor to use by default...
2793 if (set_device_uri
&& !set_port_monitor
)
2795 ppd_file_t
*ppd
; /* PPD file */
2796 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2799 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2800 sizeof(scheme
), username
, sizeof(username
), host
,
2801 sizeof(host
), &port
, resource
, sizeof(resource
));
2803 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2805 if ((ppd
= _ppdOpenFile(srcfile
, _PPD_LOCALIZATION_NONE
)) != NULL
)
2807 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2809 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2810 if (!strcmp(scheme
, ppdattr
->spec
))
2812 cupsdLogMessage(CUPSD_LOG_INFO
,
2813 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2814 printer
->name
, ppdattr
->value
,
2815 printer
->port_monitor
? printer
->port_monitor
2818 if (strcmp(ppdattr
->value
, "none"))
2819 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2821 cupsdClearString(&printer
->port_monitor
);
2830 printer
->config_time
= time(NULL
);
2833 * Update the printer attributes and return...
2836 cupsdSetPrinterAttrs(printer
);
2837 if (!printer
->temporary
)
2838 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2840 if (need_restart_job
&& printer
->job
)
2843 * Restart the current job...
2846 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
2847 "Job restarted because the printer was modified.");
2850 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2854 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
2855 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
2856 printer
->name
, get_username(con
));
2858 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2859 printer
->name
, get_username(con
));
2863 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
2864 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
2865 printer
->name
, get_username(con
));
2867 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2868 printer
->name
, get_username(con
));
2871 con
->response
->request
.status
.status_code
= IPP_OK
;
2876 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2877 * based upon the printer state...
2881 add_printer_state_reasons(
2882 cupsd_client_t
*con
, /* I - Client connection */
2883 cupsd_printer_t
*p
) /* I - Printer info */
2885 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2886 "add_printer_state_reasons(%p[%d], %p[%s])",
2887 con
, con
->number
, p
, p
->name
);
2889 if (p
->num_reasons
== 0)
2890 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2891 "printer-state-reasons", NULL
, "none");
2893 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2894 "printer-state-reasons", p
->num_reasons
, NULL
,
2895 (const char * const *)p
->reasons
);
2900 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2901 * the specified printer or class.
2905 add_queued_job_count(
2906 cupsd_client_t
*con
, /* I - Client connection */
2907 cupsd_printer_t
*p
) /* I - Printer or class */
2909 int count
; /* Number of jobs on destination */
2912 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2913 con
, con
->number
, p
, p
->name
);
2915 count
= cupsdGetPrinterJobCount(p
->name
);
2917 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2918 "queued-job-count", count
);
2923 * 'apply_printer_defaults()' - Apply printer default options to a job.
2927 apply_printer_defaults(
2928 cupsd_printer_t
*printer
, /* I - Printer */
2929 cupsd_job_t
*job
) /* I - Job */
2931 int i
, /* Looping var */
2932 num_options
; /* Number of default options */
2933 cups_option_t
*options
, /* Default options */
2934 *option
; /* Current option */
2937 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Applying default options...");
2940 * Collect all of the default options and add the missing ones to the
2944 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
2945 option
= printer
->options
;
2948 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2950 if (!strcmp(option
->name
, "print-quality") && ippFindAttribute(job
->attrs
, "cupsPrintQuality", IPP_TAG_NAME
))
2951 continue; /* Don't override cupsPrintQuality */
2953 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Adding default %s=%s", option
->name
, option
->value
);
2955 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
, &options
);
2959 * Encode these options as attributes in the job object...
2962 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2963 cupsFreeOptions(num_options
, options
);
2968 * 'authenticate_job()' - Set job authentication info.
2972 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2973 ipp_attribute_t
*uri
) /* I - Job URI */
2975 ipp_attribute_t
*attr
, /* job-id attribute */
2976 *auth_info
; /* auth-info attribute */
2977 int jobid
; /* Job ID */
2978 cupsd_job_t
*job
; /* Current job */
2979 char scheme
[HTTP_MAX_URI
],
2980 /* Method portion of URI */
2981 username
[HTTP_MAX_URI
],
2982 /* Username portion of URI */
2984 /* Host portion of URI */
2985 resource
[HTTP_MAX_URI
];
2986 /* Resource portion of URI */
2987 int port
; /* Port portion of URI */
2990 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2991 con
, con
->number
, uri
->values
[0].string
.text
);
2994 * Start with "everything is OK" status...
2997 con
->response
->request
.status
.status_code
= IPP_OK
;
3000 * See if we have a job URI or a printer URI...
3003 if (!strcmp(uri
->name
, "printer-uri"))
3006 * Got a printer URI; see if we also have a job-id attribute...
3009 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3010 IPP_TAG_INTEGER
)) == NULL
)
3012 send_ipp_status(con
, IPP_BAD_REQUEST
,
3013 _("Got a printer-uri attribute but no job-id."));
3017 jobid
= attr
->values
[0].integer
;
3022 * Got a job URI; parse it to get the job ID...
3025 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3026 sizeof(scheme
), username
, sizeof(username
), host
,
3027 sizeof(host
), &port
, resource
, sizeof(resource
));
3029 if (strncmp(resource
, "/jobs/", 6))
3035 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3036 uri
->values
[0].string
.text
);
3040 jobid
= atoi(resource
+ 6);
3044 * See if the job exists...
3047 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3050 * Nope - return a "not found" error...
3053 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3058 * See if the job has been completed...
3061 if (job
->state_value
!= IPP_JOB_HELD
)
3064 * Return a "not-possible" error...
3067 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3068 _("Job #%d is not held for authentication."),
3074 * See if we have already authenticated...
3077 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3079 if (!con
->username
[0] && !auth_info
)
3081 cupsd_printer_t
*printer
; /* Job destination */
3084 * No auth data. If we need to authenticate via Kerberos, send a
3085 * HTTP auth challenge, otherwise just return an IPP error...
3088 printer
= cupsdFindDest(job
->dest
);
3090 if (printer
&& printer
->num_auth_info_required
> 0 &&
3091 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3092 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3094 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3095 _("No authentication information provided."));
3100 * See if the job is owned by the requesting user...
3103 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3105 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3106 cupsdFindDest(job
->dest
));
3111 * Save the authentication information for this job...
3114 save_auth_info(con
, job
, auth_info
);
3117 * Reset the job-hold-until value to "no-hold"...
3120 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3121 IPP_TAG_KEYWORD
)) == NULL
)
3122 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3126 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
3127 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
3131 * Release the job and return...
3134 cupsdReleaseJob(job
);
3136 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, NULL
, job
, "Job authenticated by user");
3138 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3145 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3149 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3150 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3152 int i
; /* Looping var */
3153 http_status_t status
; /* Policy status */
3154 cups_ptype_t dtype
; /* Destination type */
3155 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3156 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3157 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3158 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3159 int port
; /* Port portion of URI */
3160 ipp_attribute_t
*attr
; /* Attribute in request */
3161 const char *username
= NULL
; /* Username */
3162 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3164 cupsd_printer_t
*printer
; /* Printer */
3165 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3166 cupsd_job_t
*job
; /* Job */
3169 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3170 con
->number
, uri
->values
[0].string
.text
);
3173 * Get the jobs to cancel/purge...
3176 switch (con
->request
->request
.op
.operation_id
)
3178 case IPP_PURGE_JOBS
:
3180 * Get the username (if any) for the jobs we want to cancel (only if
3181 * "my-jobs" is specified...
3184 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3185 IPP_TAG_BOOLEAN
)) != NULL
&&
3186 attr
->values
[0].boolean
)
3188 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3189 IPP_TAG_NAME
)) != NULL
)
3190 username
= attr
->values
[0].string
.text
;
3193 send_ipp_status(con
, IPP_BAD_REQUEST
,
3194 _("Missing requesting-user-name attribute."));
3200 * Look for the "purge-jobs" attribute...
3203 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3204 IPP_TAG_BOOLEAN
)) != NULL
)
3205 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3207 purge
= CUPSD_JOB_PURGE
;
3210 case IPP_CANCEL_MY_JOBS
:
3211 if (con
->username
[0])
3212 username
= con
->username
;
3213 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3214 IPP_TAG_NAME
)) != NULL
)
3215 username
= attr
->values
[0].string
.text
;
3218 send_ipp_status(con
, IPP_BAD_REQUEST
,
3219 _("Missing requesting-user-name attribute."));
3227 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3230 * See if we have a printer URI...
3233 if (strcmp(uri
->name
, "printer-uri"))
3235 send_ipp_status(con
, IPP_BAD_REQUEST
,
3236 _("The printer-uri attribute is required."));
3241 * And if the destination is valid...
3244 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3250 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3251 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3252 hostname
, sizeof(hostname
), &port
,
3253 resource
, sizeof(resource
));
3255 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3256 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3258 send_ipp_status(con
, IPP_NOT_FOUND
,
3259 _("The printer or class does not exist."));
3267 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3269 send_http_error(con
, status
, NULL
);
3275 for (i
= 0; i
< job_ids
->num_values
; i
++)
3277 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
3280 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3281 _cups_strcasecmp(job
->username
, username
))
3285 if (i
< job_ids
->num_values
)
3287 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3288 job_ids
->values
[i
].integer
);
3292 for (i
= 0; i
< job_ids
->num_values
; i
++)
3294 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3296 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3297 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3298 "Job canceled by user.");
3301 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3302 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3308 * Cancel all jobs on all printers...
3311 cupsdCancelJobs(NULL
, username
, purge
);
3313 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3314 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3324 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3327 send_http_error(con
, status
, printer
);
3333 for (i
= 0; i
< job_ids
->num_values
; i
++)
3335 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
3336 _cups_strcasecmp(job
->dest
, printer
->name
))
3339 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3340 _cups_strcasecmp(job
->username
, username
))
3344 if (i
< job_ids
->num_values
)
3346 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3347 job_ids
->values
[i
].integer
);
3351 for (i
= 0; i
< job_ids
->num_values
; i
++)
3353 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3355 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3356 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3357 "Job canceled by user.");
3360 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3361 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3367 * Cancel all of the jobs on the named printer...
3370 cupsdCancelJobs(printer
->name
, username
, purge
);
3372 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3374 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3379 con
->response
->request
.status
.status_code
= IPP_OK
;
3386 * 'cancel_job()' - Cancel a print job.
3390 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3391 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3393 ipp_attribute_t
*attr
; /* Current attribute */
3394 int jobid
; /* Job ID */
3395 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3396 username
[HTTP_MAX_URI
], /* Username portion of URI */
3397 host
[HTTP_MAX_URI
], /* Host portion of URI */
3398 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3399 int port
; /* Port portion of URI */
3400 cupsd_job_t
*job
; /* Job information */
3401 cups_ptype_t dtype
; /* Destination type (printer/class) */
3402 cupsd_printer_t
*printer
; /* Printer data */
3403 cupsd_jobaction_t purge
; /* Purge the job? */
3406 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3407 con
->number
, uri
->values
[0].string
.text
);
3410 * See if we have a job URI or a printer URI...
3413 if (!strcmp(uri
->name
, "printer-uri"))
3416 * Got a printer URI; see if we also have a job-id attribute...
3419 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3420 IPP_TAG_INTEGER
)) == NULL
)
3422 send_ipp_status(con
, IPP_BAD_REQUEST
,
3423 _("Got a printer-uri attribute but no job-id."));
3427 if ((jobid
= attr
->values
[0].integer
) == 0)
3430 * Find the current job on the specified printer...
3433 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3439 send_ipp_status(con
, IPP_NOT_FOUND
,
3440 _("The printer or class does not exist."));
3445 * See if there are any pending jobs...
3448 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3450 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3451 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3452 !_cups_strcasecmp(job
->dest
, printer
->name
))
3460 * No, try stopped jobs...
3463 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3465 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3466 if (job
->state_value
== IPP_JOB_STOPPED
&&
3467 !_cups_strcasecmp(job
->dest
, printer
->name
))
3474 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3484 * Got a job URI; parse it to get the job ID...
3487 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3488 sizeof(scheme
), username
, sizeof(username
), host
,
3489 sizeof(host
), &port
, resource
, sizeof(resource
));
3491 if (strncmp(resource
, "/jobs/", 6))
3497 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3498 uri
->values
[0].string
.text
);
3502 jobid
= atoi(resource
+ 6);
3506 * Look for the "purge-job" attribute...
3509 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3510 IPP_TAG_BOOLEAN
)) != NULL
)
3511 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3513 purge
= CUPSD_JOB_DEFAULT
;
3516 * See if the job exists...
3519 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3522 * Nope - return a "not found" error...
3525 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3530 * See if the job is owned by the requesting user...
3533 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3535 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3536 cupsdFindDest(job
->dest
));
3541 * See if the job is already completed, canceled, or aborted; if so,
3542 * we can't cancel...
3545 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3547 switch (job
->state_value
)
3549 case IPP_JOB_CANCELED
:
3550 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3551 _("Job #%d is already canceled - can\'t cancel."),
3555 case IPP_JOB_ABORTED
:
3556 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3557 _("Job #%d is already aborted - can\'t cancel."),
3562 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3563 _("Job #%d is already completed - can\'t cancel."),
3572 * Cancel the job and return...
3575 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3576 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3577 "Job canceled by \"%s\"",
3581 if (purge
== CUPSD_JOB_PURGE
)
3582 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3585 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3588 con
->response
->request
.status
.status_code
= IPP_OK
;
3593 * 'cancel_subscription()' - Cancel a subscription.
3597 cancel_subscription(
3598 cupsd_client_t
*con
, /* I - Client connection */
3599 int sub_id
) /* I - Subscription ID */
3601 http_status_t status
; /* Policy status */
3602 cupsd_subscription_t
*sub
; /* Subscription */
3605 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3606 "cancel_subscription(con=%p[%d], sub_id=%d)",
3607 con
, con
->number
, sub_id
);
3610 * Is the subscription ID valid?
3613 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3616 * Bad subscription ID...
3619 send_ipp_status(con
, IPP_NOT_FOUND
,
3620 _("Subscription #%d does not exist."), sub_id
);
3628 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3630 con
, sub
->owner
)) != HTTP_OK
)
3632 send_http_error(con
, status
, sub
->dest
);
3637 * Cancel the subscription...
3640 cupsdDeleteSubscription(sub
, 1);
3642 con
->response
->request
.status
.status_code
= IPP_OK
;
3647 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3650 static int /* O - 1 if OK, 0 if not */
3651 check_rss_recipient(
3652 const char *recipient
) /* I - Recipient URI */
3654 cupsd_subscription_t
*sub
; /* Current subscription */
3657 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3659 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3663 * Compare the URIs up to the first ?...
3666 const char *r1
, *r2
;
3668 for (r1
= recipient
, r2
= sub
->recipient
;
3669 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3681 * 'check_quotas()' - Check quotas for a printer and user.
3684 static int /* O - 1 if OK, 0 if forbidden,
3685 -1 if limit reached */
3686 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3687 cupsd_printer_t
*p
) /* I - Printer or class */
3689 char username
[33], /* Username */
3690 *name
; /* Current user name */
3691 cupsd_quota_t
*q
; /* Quota data */
3692 #ifdef HAVE_MBR_UID_TO_UUID
3694 * Use Apple membership APIs which require that all names represent
3695 * valid user account or group records accessible by the server.
3698 uuid_t usr_uuid
; /* UUID for job requesting user */
3699 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3700 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3701 int mbr_err
; /* Error from membership function */
3702 int is_member
; /* Is this user a member? */
3705 * Use standard POSIX APIs for checking users and groups...
3708 struct passwd
*pw
; /* User password data */
3709 #endif /* HAVE_MBR_UID_TO_UUID */
3712 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3713 con
, con
->number
, p
, p
->name
);
3716 * Figure out who is printing...
3719 strlcpy(username
, get_username(con
), sizeof(username
));
3721 if ((name
= strchr(username
, '@')) != NULL
)
3722 *name
= '\0'; /* Strip @REALM */
3725 * Check global active job limits for printers and users...
3728 if (MaxJobsPerPrinter
)
3731 * Check if there are too many pending jobs on this printer...
3734 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3736 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3745 * Check if there are too many pending jobs for this user...
3748 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3750 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3757 * Check against users...
3760 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3763 if (cupsArrayCount(p
->users
))
3765 #ifdef HAVE_MBR_UID_TO_UUID
3767 * Get UUID for job requesting user...
3770 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3776 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3777 "check_quotas: UUID lookup failed for user \"%s\"",
3779 cupsdLogMessage(CUPSD_LOG_INFO
,
3780 "Denying user \"%s\" access to printer \"%s\" "
3781 "(unknown user)...",
3787 * Get UID and GID of requesting user...
3790 pw
= getpwnam(username
);
3792 #endif /* HAVE_MBR_UID_TO_UUID */
3794 for (name
= (char *)cupsArrayFirst(p
->users
);
3796 name
= (char *)cupsArrayNext(p
->users
))
3800 * Check group membership...
3803 #ifdef HAVE_MBR_UID_TO_UUID
3806 if (uuid_parse(name
+ 2, grp_uuid
))
3807 uuid_clear(grp_uuid
);
3809 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3812 * Invalid ACL entries are ignored for matching; just record a
3813 * warning in the log...
3816 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3817 "check_quotas: UUID lookup failed for ACL entry "
3818 "\"%s\" (err=%d)", name
, mbr_err
);
3819 cupsdLogMessage(CUPSD_LOG_WARN
,
3820 "Access control entry \"%s\" not a valid group name; "
3821 "entry ignored", name
);
3824 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3828 * At this point, there should be no errors, but check anyways...
3831 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3832 "check_quotas: group \"%s\" membership check "
3833 "failed (err=%d)", name
+ 1, mbr_err
);
3838 * Stop if we found a match...
3845 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3847 #endif /* HAVE_MBR_UID_TO_UUID */
3849 #ifdef HAVE_MBR_UID_TO_UUID
3854 if (uuid_parse(name
+ 1, usr2_uuid
))
3855 uuid_clear(usr2_uuid
);
3857 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3860 * Invalid ACL entries are ignored for matching; just record a
3861 * warning in the log...
3864 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3865 "check_quotas: UUID lookup failed for ACL entry "
3866 "\"%s\" (err=%d)", name
, mbr_err
);
3867 cupsdLogMessage(CUPSD_LOG_WARN
,
3868 "Access control entry \"%s\" not a valid user name; "
3869 "entry ignored", name
);
3872 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3876 else if (!_cups_strcasecmp(username
, name
))
3878 #endif /* HAVE_MBR_UID_TO_UUID */
3880 if ((name
!= NULL
) == p
->deny_users
)
3882 cupsdLogMessage(CUPSD_LOG_INFO
,
3883 "Denying user \"%s\" access to printer \"%s\"...",
3893 if (p
->k_limit
|| p
->page_limit
)
3895 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3897 cupsdLogMessage(CUPSD_LOG_ERROR
,
3898 "Unable to allocate quota data for user \"%s\"",
3903 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3904 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3906 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3913 * If we have gotten this far, we're done!
3921 * 'close_job()' - Close a multi-file job.
3925 close_job(cupsd_client_t
*con
, /* I - Client connection */
3926 ipp_attribute_t
*uri
) /* I - Printer URI */
3928 cupsd_job_t
*job
; /* Job */
3929 ipp_attribute_t
*attr
; /* Attribute */
3930 char job_uri
[HTTP_MAX_URI
],
3932 username
[256]; /* User name */
3935 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3936 con
->number
, uri
->values
[0].string
.text
);
3939 * See if we have a job URI or a printer URI...
3942 if (strcmp(uri
->name
, "printer-uri"))
3945 * job-uri is not supported by Close-Job!
3948 send_ipp_status(con
, IPP_BAD_REQUEST
,
3949 _("Close-Job doesn't support the job-uri attribute."));
3954 * Got a printer URI; see if we also have a job-id attribute...
3957 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3958 IPP_TAG_INTEGER
)) == NULL
)
3960 send_ipp_status(con
, IPP_BAD_REQUEST
,
3961 _("Got a printer-uri attribute but no job-id."));
3965 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
3968 * Nope - return a "not found" error...
3971 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3972 attr
->values
[0].integer
);
3977 * See if the job is owned by the requesting user...
3980 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3982 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3983 cupsdFindDest(job
->dest
));
3988 * Add any ending sheet...
3991 if (cupsdTimeoutJob(job
))
3994 if (job
->state_value
== IPP_JOB_STOPPED
)
3996 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3997 job
->state_value
= IPP_JOB_PENDING
;
3999 else if (job
->state_value
== IPP_JOB_HELD
)
4001 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
4002 IPP_TAG_KEYWORD
)) == NULL
)
4003 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
4005 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
4007 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
4008 job
->state_value
= IPP_JOB_PENDING
;
4013 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4016 * Fill in the response info...
4019 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4020 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
4021 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
4024 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4026 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
4029 con
->response
->request
.status
.status_code
= IPP_OK
;
4032 * Start the job if necessary...
4040 * 'copy_attrs()' - Copy attributes from one request to another.
4044 copy_attrs(ipp_t
*to
, /* I - Destination request */
4045 ipp_t
*from
, /* I - Source request */
4046 cups_array_t
*ra
, /* I - Requested attributes */
4047 ipp_tag_t group
, /* I - Group to copy */
4048 int quickcopy
, /* I - Do a quick copy? */
4049 cups_array_t
*exclude
) /* I - Attributes to exclude? */
4051 ipp_attribute_t
*fromattr
; /* Source attribute */
4054 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4055 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4056 to
, from
, ra
, group
, quickcopy
);
4061 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4064 * Filter attributes as needed...
4067 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4068 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4071 if (!strcmp(fromattr
->name
, "document-password") ||
4072 !strcmp(fromattr
->name
, "job-authorization-uri") ||
4073 !strcmp(fromattr
->name
, "job-password") ||
4074 !strcmp(fromattr
->name
, "job-password-encryption") ||
4075 !strcmp(fromattr
->name
, "job-printer-uri"))
4079 (cupsArrayFind(exclude
, fromattr
->name
) ||
4080 cupsArrayFind(exclude
, "all")))
4083 * We need to exclude this attribute for security reasons; we require the
4084 * job-id attribute regardless of the security settings for IPP
4087 * The job-printer-uri attribute is handled by copy_job_attrs().
4089 * Subscription attribute security is handled by copy_subscription_attrs().
4092 if (strcmp(fromattr
->name
, "job-id"))
4096 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4099 * Don't send collection attributes by default to IPP/1.x clients
4100 * since many do not support collections. Also don't send
4101 * media-col-database unless specifically requested by the client.
4104 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4106 (to
->request
.status
.version
[0] == 1 ||
4107 !strcmp(fromattr
->name
, "media-col-database")))
4110 ippCopyAttribute(to
, fromattr
, quickcopy
);
4117 * 'copy_banner()' - Copy a banner file to the requests directory for the
4121 static int /* O - Size of banner file in kbytes */
4122 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4123 cupsd_job_t
*job
, /* I - Job information */
4124 const char *name
) /* I - Name of banner */
4126 int i
; /* Looping var */
4127 int kbytes
; /* Size of banner file in kbytes */
4128 char filename
[1024]; /* Job filename */
4129 cupsd_banner_t
*banner
; /* Pointer to banner */
4130 cups_file_t
*in
; /* Input file */
4131 cups_file_t
*out
; /* Output file */
4132 int ch
; /* Character from file */
4133 char attrname
[255], /* Name of attribute */
4134 *s
; /* Pointer into name */
4135 ipp_attribute_t
*attr
; /* Attribute */
4138 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4139 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4140 con
, con
? con
->number
: -1, job
, job
->id
,
4141 name
? name
: "(null)");
4144 * Find the banner; return if not found or "none"...
4147 if (!name
|| !strcmp(name
, "none") ||
4148 (banner
= cupsdFindBanner(name
)) == NULL
)
4152 * Open the banner and job files...
4155 if (add_file(con
, job
, banner
->filetype
, 0))
4158 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4160 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4162 cupsdLogMessage(CUPSD_LOG_ERROR
,
4163 "Unable to create banner job file %s - %s",
4164 filename
, strerror(errno
));
4169 fchmod(cupsFileNumber(out
), 0640);
4170 fchown(cupsFileNumber(out
), RunUser
, Group
);
4173 * Try the localized banner file under the subdirectory...
4176 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4178 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4181 * Convert ll-cc to ll_CC...
4185 attrname
[3] = (char)toupper(attrname
[3] & 255);
4186 attrname
[4] = (char)toupper(attrname
[4] & 255);
4189 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4192 if (access(filename
, 0) && strlen(attrname
) > 2)
4195 * Wasn't able to find "ll_CC" locale file; try the non-national
4196 * localization banner directory.
4201 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4205 if (access(filename
, 0))
4208 * Use the non-localized banner file.
4211 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4214 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4218 cupsdLogMessage(CUPSD_LOG_ERROR
,
4219 "Unable to open banner template file %s - %s",
4220 filename
, strerror(errno
));
4226 * Parse the file to the end...
4229 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4233 * Get an attribute name...
4236 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4237 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4239 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4249 * Ignore { followed by stuff that is not an attribute name...
4252 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4257 * See if it is defined...
4260 if (attrname
[0] == '?')
4265 if (!strcmp(s
, "printer-name"))
4267 cupsFilePuts(out
, job
->dest
);
4270 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4273 * See if we have a leading question mark...
4276 if (attrname
[0] != '?')
4279 * Nope, write to file as-is; probably a PostScript procedure...
4282 cupsFilePrintf(out
, "{%s}", attrname
);
4289 * Output value(s)...
4292 for (i
= 0; i
< attr
->num_values
; i
++)
4295 cupsFilePutChar(out
, ',');
4297 switch (attr
->value_tag
)
4299 case IPP_TAG_INTEGER
:
4301 if (!strncmp(s
, "time-at-", 8))
4303 struct timeval tv
; /* Time value */
4305 tv
.tv_sec
= attr
->values
[i
].integer
;
4308 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4311 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4314 case IPP_TAG_BOOLEAN
:
4315 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4318 case IPP_TAG_NOVALUE
:
4319 cupsFilePuts(out
, "novalue");
4322 case IPP_TAG_RANGE
:
4323 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4324 attr
->values
[i
].range
.upper
);
4327 case IPP_TAG_RESOLUTION
:
4328 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4329 attr
->values
[i
].resolution
.yres
,
4330 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4335 case IPP_TAG_STRING
:
4338 case IPP_TAG_KEYWORD
:
4339 case IPP_TAG_CHARSET
:
4340 case IPP_TAG_LANGUAGE
:
4341 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4344 * Need to quote strings for PS banners...
4349 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4351 if (*p
== '(' || *p
== ')' || *p
== '\\')
4353 cupsFilePutChar(out
, '\\');
4354 cupsFilePutChar(out
, *p
);
4356 else if (*p
< 32 || *p
> 126)
4357 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4359 cupsFilePutChar(out
, *p
);
4363 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4367 break; /* anti-compiler-warning-code */
4371 else if (ch
== '\\') /* Quoted char */
4373 ch
= cupsFileGetChar(in
);
4375 if (ch
!= '{') /* Only do special handling for \{ */
4376 cupsFilePutChar(out
, '\\');
4378 cupsFilePutChar(out
, ch
);
4381 cupsFilePutChar(out
, ch
);
4385 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4387 job
->koctets
+= kbytes
;
4389 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4390 attr
->values
[0].integer
+= kbytes
;
4399 * 'copy_file()' - Copy a PPD file...
4402 static int /* O - 0 = success, -1 = error */
4403 copy_file(const char *from
, /* I - Source file */
4404 const char *to
, /* I - Destination file */
4405 mode_t mode
) /* I - Permissions */
4407 cups_file_t
*src
, /* Source file */
4408 *dst
; /* Destination file */
4409 int bytes
; /* Bytes to read/write */
4410 char buffer
[2048]; /* Copy buffer */
4413 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4416 * Open the source and destination file for a copy...
4419 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4422 if ((dst
= cupsdCreateConfFile(to
, mode
)) == NULL
)
4429 * Copy the source file to the destination...
4432 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4433 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4441 * Close both files and return...
4446 return (cupsdCloseCreatedConfFile(dst
, to
));
4451 * 'copy_model()' - Copy a PPD model file, substituting default values
4455 static int /* O - 0 = success, -1 = error */
4456 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4457 const char *from
, /* I - Source file */
4458 const char *to
) /* I - Destination file */
4460 fd_set input
; /* select() input set */
4461 struct timeval timeout
; /* select() timeout */
4462 int maxfd
; /* Max file descriptor for select() */
4463 char tempfile
[1024]; /* Temporary PPD file */
4464 int tempfd
; /* Temporary PPD file descriptor */
4465 int temppid
; /* Process ID of cups-driverd */
4466 int temppipe
[2]; /* Temporary pipes */
4467 char *argv
[4], /* Command-line arguments */
4468 *envp
[MAX_ENV
]; /* Environment */
4469 cups_file_t
*src
, /* Source file */
4470 *dst
; /* Destination file */
4471 ppd_file_t
*ppd
; /* PPD file */
4472 int bytes
, /* Bytes from pipe */
4473 total
; /* Total bytes from pipe */
4474 char buffer
[2048]; /* Copy buffer */
4475 int i
; /* Looping var */
4476 char option
[PPD_MAX_NAME
], /* Option name */
4477 choice
[PPD_MAX_NAME
]; /* Choice name */
4478 ppd_size_t
*size
; /* Default size */
4479 int num_defaults
; /* Number of default options */
4480 cups_option_t
*defaults
; /* Default options */
4481 char cups_protocol
[PPD_MAX_LINE
];
4482 /* cupsProtocol attribute */
4485 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_model(con=%p, from=\"%s\", to=\"%s\")", con
, from
, to
);
4488 * Run cups-driverd to get the PPD file...
4491 argv
[0] = "cups-driverd";
4493 argv
[2] = (char *)from
;
4496 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4498 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4499 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4500 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4501 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4504 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4505 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4507 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4508 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4519 * Wait up to 30 seconds for the PPD file to be copied...
4524 if (temppipe
[0] > CGIPipes
[0])
4525 maxfd
= temppipe
[0] + 1;
4527 maxfd
= CGIPipes
[0] + 1;
4532 * See if we have data ready...
4536 FD_SET(temppipe
[0], &input
);
4537 FD_SET(CGIPipes
[0], &input
);
4539 timeout
.tv_sec
= 30;
4540 timeout
.tv_usec
= 0;
4542 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4552 * We have timed out...
4558 if (FD_ISSET(temppipe
[0], &input
))
4561 * Read the PPD file from the pipe, and write it to the PPD file.
4564 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4566 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4575 if (FD_ISSET(CGIPipes
[0], &input
))
4585 * No data from cups-deviced...
4588 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4594 * Open the source file for a copy...
4597 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4604 * Read the source file and see what page sizes are supported...
4607 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4615 * Open the destination (if possible) and set the default options...
4620 cups_protocol
[0] = '\0';
4622 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4625 * Read all of the default lines from the old PPD...
4628 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4629 if (!strncmp(buffer
, "*Default", 8))
4632 * Add the default option...
4635 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4636 choice
, sizeof(choice
)))
4638 ppd_option_t
*ppdo
; /* PPD option */
4642 * Only add the default if the default hasn't already been
4643 * set and the choice exists in the new PPD...
4646 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4647 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4648 ppdFindChoice(ppdo
, choice
))
4649 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4653 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4654 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4658 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4661 * Add the default media sizes...
4664 num_defaults
= cupsAddOption("PageSize", size
->name
,
4665 num_defaults
, &defaults
);
4666 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4667 num_defaults
, &defaults
);
4668 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4669 num_defaults
, &defaults
);
4670 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4671 num_defaults
, &defaults
);
4677 * Open the destination file for a copy...
4680 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4682 cupsFreeOptions(num_defaults
, defaults
);
4689 * Copy the source file to the destination...
4692 cupsFileRewind(src
);
4694 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4696 if (!strncmp(buffer
, "*Default", 8))
4699 * Check for an previous default option choice...
4702 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4703 choice
, sizeof(choice
)))
4705 const char *val
; /* Default option value */
4708 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4711 * Substitute the previous choice...
4714 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4719 cupsFilePrintf(dst
, "%s\n", buffer
);
4722 if (cups_protocol
[0])
4723 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4725 cupsFreeOptions(num_defaults
, defaults
);
4728 * Close both files and return...
4735 return (cupsdCloseCreatedConfFile(dst
, to
));
4740 * 'copy_job_attrs()' - Copy job attributes.
4744 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4745 cupsd_job_t
*job
, /* I - Job */
4746 cups_array_t
*ra
, /* I - Requested attributes array */
4747 cups_array_t
*exclude
) /* I - Private attributes array */
4749 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4753 * Send the requested attributes for each job...
4756 if (!cupsArrayFind(exclude
, "all"))
4758 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4759 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4760 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4761 "number-of-documents", job
->num_files
);
4763 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4764 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4765 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4766 "job-media-progress", job
->progress
);
4768 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4769 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4771 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4772 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4774 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4775 "job-more-info", NULL
, job_uri
);
4778 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4779 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4780 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4781 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4782 job
->num_files
> 0);
4784 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4785 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4786 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4787 "job-printer-up-time", time(NULL
));
4790 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4792 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4793 con
->clientname
, con
->clientport
,
4794 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4797 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4798 "job-printer-uri", NULL
, job_uri
);
4801 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4803 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4804 con
->clientname
, con
->clientport
, "/jobs/%d",
4806 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4807 "job-uri", NULL
, job_uri
);
4812 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4817 * Generate attributes from the job structure...
4820 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-completed")))
4821 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-completed", ippTimeToDate(job
->completed_time
));
4823 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-creation")))
4824 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(job
->creation_time
));
4826 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4827 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4829 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4830 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4832 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4833 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4835 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4836 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4838 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4839 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4841 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4843 switch (job
->state_value
)
4845 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4847 case IPP_JSTATE_ABORTED
:
4848 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4850 case IPP_JSTATE_CANCELED
:
4851 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4853 case IPP_JSTATE_COMPLETED
:
4854 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4859 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4860 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4862 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4863 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4869 * 'copy_printer_attrs()' - Copy printer attributes.
4874 cupsd_client_t
*con
, /* I - Client connection */
4875 cupsd_printer_t
*printer
, /* I - Printer */
4876 cups_array_t
*ra
) /* I - Requested attributes array */
4878 char printer_uri
[HTTP_MAX_URI
];
4880 char printer_icons
[HTTP_MAX_URI
];
4882 time_t curtime
; /* Current time */
4883 int i
; /* Looping var */
4887 * Copy the printer attributes to the response using requested-attributes
4888 * and document-format attributes that may be provided by the client.
4891 curtime
= time(NULL
);
4893 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4894 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4895 "marker-change-time", printer
->marker_time
);
4897 if (printer
->num_printers
> 0 &&
4898 (!ra
|| cupsArrayFind(ra
, "member-uris")))
4900 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4901 cupsd_printer_t
*p2
; /* Printer in class */
4902 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4905 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
4906 IPP_TAG_URI
, "member-uris",
4907 printer
->num_printers
, NULL
,
4910 for (i
= 0; i
< printer
->num_printers
; i
++)
4912 p2
= printer
->printers
[i
];
4914 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
4915 IPP_TAG_URI
)) != NULL
)
4916 member_uris
->values
[i
].string
.text
=
4917 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4920 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
4921 sizeof(printer_uri
), "ipp", NULL
, con
->clientname
,
4923 (p2
->type
& CUPS_PRINTER_CLASS
) ?
4924 "/classes/%s" : "/printers/%s", p2
->name
);
4925 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
4931 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4932 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4933 "printer-alert", NULL
, printer
->alert
);
4935 if (printer
->alert_description
&&
4936 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4937 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4938 "printer-alert-description", NULL
,
4939 printer
->alert_description
);
4941 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-date-time"))
4942 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-config-change-date-time", ippTimeToDate(printer
->config_time
));
4944 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-time"))
4945 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4946 "printer-config-change-time", printer
->config_time
);
4948 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4949 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4950 ippTimeToDate(curtime
));
4952 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4953 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4955 if (printer
->reg_name
)
4956 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4957 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4959 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4960 "printer-dns-sd-name", 0);
4962 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4964 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4965 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4966 "printer-error-policy", NULL
, printer
->error_policy
);
4968 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
4970 static const char * const errors
[] =/* printer-error-policy-supported values */
4973 "retry-current-job",
4978 if (printer
->type
& CUPS_PRINTER_CLASS
)
4979 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4980 "printer-error-policy-supported", NULL
, "retry-current-job");
4982 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4983 "printer-error-policy-supported",
4984 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
4987 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
4989 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_icons
, sizeof(printer_icons
),
4990 "http", NULL
, con
->clientname
, con
->clientport
,
4991 "/icons/%s.png", printer
->name
);
4992 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons",
4993 NULL
, printer_icons
);
4994 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", printer_icons
);
4997 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4998 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
5000 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5001 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
5003 if (!ra
|| cupsArrayFind(ra
, "printer-is-temporary"))
5004 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-temporary", (char)printer
->temporary
);
5006 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
5008 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5009 "http", NULL
, con
->clientname
, con
->clientport
,
5010 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5011 "/classes/%s" : "/printers/%s", printer
->name
);
5012 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5013 "printer-more-info", NULL
, printer_uri
);
5016 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5017 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5018 "printer-op-policy", NULL
, printer
->op_policy
);
5020 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5021 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5024 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-date-time"))
5025 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-state-change-date-time", ippTimeToDate(printer
->state_time
));
5027 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5028 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5029 "printer-state-change-time", printer
->state_time
);
5031 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5032 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5033 "printer-state-message", NULL
, printer
->state_message
);
5035 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5036 add_printer_state_reasons(con
, printer
);
5038 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5040 cups_ptype_t type
; /* printer-type value */
5043 * Add the CUPS-specific printer-type attribute...
5046 type
= printer
->type
;
5048 if (printer
== DefaultPrinter
)
5049 type
|= CUPS_PRINTER_DEFAULT
;
5051 if (!printer
->accepting
)
5052 type
|= CUPS_PRINTER_REJECTING
;
5054 if (!printer
->shared
)
5055 type
|= CUPS_PRINTER_NOT_SHARED
;
5057 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
5060 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5061 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5062 "printer-up-time", curtime
);
5064 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
5066 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5067 "ipp", NULL
, con
->clientname
, con
->clientport
,
5068 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5069 "/classes/%s" : "/printers/%s", printer
->name
);
5070 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5071 "printer-uri-supported", NULL
, printer_uri
);
5072 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5076 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5077 add_queued_job_count(con
, printer
);
5079 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5080 if (printer
->ppd_attrs
)
5081 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5082 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
5087 * 'copy_subscription_attrs()' - Copy subscription attributes.
5091 copy_subscription_attrs(
5092 cupsd_client_t
*con
, /* I - Client connection */
5093 cupsd_subscription_t
*sub
, /* I - Subscription */
5094 cups_array_t
*ra
, /* I - Requested attributes array */
5095 cups_array_t
*exclude
) /* I - Private attributes array */
5097 ipp_attribute_t
*attr
; /* Current attribute */
5098 char printer_uri
[HTTP_MAX_URI
];
5100 int count
; /* Number of events */
5101 unsigned mask
; /* Current event mask */
5102 const char *name
; /* Current event name */
5105 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5106 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5107 con
, sub
, ra
, exclude
);
5110 * Copy the subscription attributes to the response using the
5111 * requested-attributes attribute that may be provided by the client.
5114 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5116 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5117 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5119 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5121 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5124 * Simple event list...
5127 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5128 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5129 "notify-events", NULL
, name
);
5134 * Complex event list...
5137 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5138 if (sub
->mask
& mask
)
5141 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5142 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5143 "notify-events", count
, NULL
, NULL
);
5145 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5146 if (sub
->mask
& mask
)
5148 attr
->values
[count
].string
.text
=
5149 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5156 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5157 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5158 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5159 "notify-lease-duration", sub
->lease
);
5161 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5162 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5163 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5164 "notify-recipient-uri", NULL
, sub
->recipient
);
5165 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5166 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5167 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5168 "notify-pull-method", NULL
, "ippget");
5170 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5171 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5172 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5173 "notify-subscriber-user-name", NULL
, sub
->owner
);
5175 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5176 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5177 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5178 "notify-time-interval", sub
->interval
);
5180 if (sub
->user_data_len
> 0 &&
5181 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5182 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5183 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5184 sub
->user_data
, sub
->user_data_len
);
5187 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5188 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5189 "notify-job-id", sub
->job
->id
);
5191 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5193 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5194 "ipp", NULL
, con
->clientname
, con
->clientport
,
5195 "/printers/%s", sub
->dest
->name
);
5196 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5197 "notify-printer-uri", NULL
, printer_uri
);
5200 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5201 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5202 "notify-subscription-id", sub
->id
);
5207 * 'create_job()' - Print a file to a printer or class.
5211 create_job(cupsd_client_t
*con
, /* I - Client connection */
5212 ipp_attribute_t
*uri
) /* I - Printer URI */
5214 int i
; /* Looping var */
5215 cupsd_printer_t
*printer
; /* Printer */
5216 cupsd_job_t
*job
; /* New job */
5217 static const char * const forbidden_attrs
[] =
5218 { /* List of forbidden attributes */
5222 "document-natural-language"
5226 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5227 con
->number
, uri
->values
[0].string
.text
);
5230 * Is the destination valid?
5233 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5239 send_ipp_status(con
, IPP_NOT_FOUND
,
5240 _("The printer or class does not exist."));
5245 * Check for invalid Create-Job attributes and log a warning or error depending
5246 * on whether cupsd is running in "strict conformance" mode...
5250 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5252 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5254 if (StrictConformance
)
5256 send_ipp_status(con
, IPP_BAD_REQUEST
,
5257 _("The '%s' operation attribute cannot be supplied in a "
5258 "Create-Job request."), forbidden_attrs
[i
]);
5262 cupsdLogMessage(CUPSD_LOG_WARN
,
5263 "Unexpected '%s' operation attribute in a Create-Job "
5264 "request.", forbidden_attrs
[i
]);
5268 * Create the job object...
5271 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5274 job
->pending_timeout
= 1;
5277 * Save and log the job...
5280 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5281 job
->dest
, job
->username
);
5286 * 'create_local_bg_thread()' - Background thread for creating a local print queue.
5289 static void * /* O - Exit status */
5290 create_local_bg_thread(
5291 cupsd_printer_t
*printer
) /* I - Printer */
5293 cups_file_t
*from
, /* Source file */
5294 *to
; /* Destination file */
5295 char fromppd
[1024], /* Source PPD */
5296 toppd
[1024], /* Destination PPD */
5297 scheme
[32], /* URI scheme */
5298 userpass
[256], /* User:pass */
5299 host
[256], /* Hostname */
5300 resource
[1024], /* Resource path */
5301 line
[1024]; /* Line from PPD */
5302 int port
; /* Port number */
5303 http_encryption_t encryption
; /* Type of encryption to use */
5304 http_t
*http
; /* Connection to printer */
5305 ipp_t
*request
, /* Request to printer */
5306 *response
; /* Response from printer */
5307 ipp_attribute_t
*attr
; /* Attribute in response */
5311 * Try connecting to the printer...
5314 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Generating PPD file from \"%s\"...", printer
->name
, printer
->device_uri
);
5316 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
)
5318 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Bad device URI \"%s\".", printer
->name
, printer
->device_uri
);
5322 if (!strcmp(scheme
, "ipps") || port
== 443)
5323 encryption
= HTTP_ENCRYPTION_ALWAYS
;
5325 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
5327 if ((http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
)) == NULL
)
5329 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to connect to %s:%d: %s", printer
->name
, host
, port
, cupsLastErrorString());
5334 * Query the printer for its capabilities...
5337 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer
->name
, host
, port
);
5339 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
5340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, printer
->device_uri
);
5341 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "all");
5343 response
= cupsDoRequest(http
, request
, resource
);
5345 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Get-Printer-Attributes returned %s", printer
->name
, ippErrorString(cupsLastError()));
5347 // TODO: Grab printer icon file...
5351 * Write the PPD for the queue...
5354 if (_ppdCreateFromIPP(fromppd
, sizeof(fromppd
), response
))
5356 if ((!printer
->info
|| !*(printer
->info
)) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
5357 cupsdSetString(&printer
->info
, ippGetString(attr
, 0, NULL
));
5359 if ((!printer
->location
|| !*(printer
->location
)) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
5360 cupsdSetString(&printer
->location
, ippGetString(attr
, 0, NULL
));
5362 if ((!printer
->geo_location
|| !*(printer
->geo_location
)) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
5363 cupsdSetString(&printer
->geo_location
, ippGetString(attr
, 0, NULL
));
5365 if ((from
= cupsFileOpen(fromppd
, "r")) == NULL
)
5367 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to read generated PPD: %s", printer
->name
, strerror(errno
));
5371 snprintf(toppd
, sizeof(toppd
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
5372 if ((to
= cupsdCreateConfFile(toppd
, ConfigFilePerm
)) == NULL
)
5374 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to create PPD for printer: %s", printer
->name
, strerror(errno
));
5375 cupsFileClose(from
);
5379 while (cupsFileGets(from
, line
, sizeof(line
)))
5380 cupsFilePrintf(to
, "%s\n", line
);
5382 cupsFileClose(from
);
5383 if (!cupsdCloseCreatedConfFile(to
, toppd
))
5385 printer
->config_time
= time(NULL
);
5386 printer
->state
= IPP_PSTATE_IDLE
;
5387 printer
->accepting
= 1;
5389 cupsdSetPrinterAttrs(printer
);
5391 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
, "Printer \"%s\" is now available.", printer
->name
);
5392 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" is now available.", printer
->name
);
5396 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: PPD creation failed: %s", printer
->name
, cupsLastErrorString());
5403 * 'create_local_printer()' - Create a local (temporary) print queue.
5407 create_local_printer(
5408 cupsd_client_t
*con
) /* I - Client connection */
5410 ipp_attribute_t
*device_uri
, /* device-uri attribute */
5411 *printer_geo_location
, /* printer-geo-location attribute */
5412 *printer_info
, /* printer-info attribute */
5413 *printer_location
, /* printer-location attribute */
5414 *printer_name
; /* printer-name attribute */
5415 cupsd_printer_t
*printer
; /* New printer */
5416 http_status_t status
; /* Policy status */
5417 char name
[128], /* Sanitized printer name */
5418 *nameptr
, /* Pointer into name */
5419 uri
[1024]; /* printer-uri-supported value */
5420 const char *ptr
; /* Pointer into attribute value */
5424 * Require local access to create a local printer...
5427 if (!httpAddrLocalhost(httpGetAddress(con
->http
)))
5429 send_ipp_status(con
, IPP_STATUS_ERROR_FORBIDDEN
, _("Only local users can create a local printer."));
5434 * Check any other policy limits...
5437 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5439 send_http_error(con
, status
, NULL
);
5444 * Grab needed attributes...
5447 if ((printer_name
= ippFindAttribute(con
->request
, "printer-name", IPP_TAG_ZERO
)) == NULL
|| ippGetGroupTag(printer_name
) != IPP_TAG_PRINTER
|| ippGetValueTag(printer_name
) != IPP_TAG_NAME
)
5450 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Missing required attribute \"%s\"."), "printer-name");
5451 else if (ippGetGroupTag(printer_name
) != IPP_TAG_PRINTER
)
5452 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is in the wrong group."), "printer-name");
5454 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is the wrong value type."), "printer-name");
5459 for (nameptr
= name
, ptr
= ippGetString(printer_name
, 0, NULL
); *ptr
&& nameptr
< (name
+ sizeof(name
) - 1); ptr
++)
5462 * Sanitize the printer name...
5465 if (_cups_isalnum(*ptr
))
5467 else if (nameptr
== name
|| nameptr
[-1] != '_')
5473 if ((device_uri
= ippFindAttribute(con
->request
, "device-uri", IPP_TAG_ZERO
)) == NULL
|| ippGetGroupTag(device_uri
) != IPP_TAG_PRINTER
|| ippGetValueTag(device_uri
) != IPP_TAG_URI
)
5476 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Missing required attribute \"%s\"."), "device-uri");
5477 else if (ippGetGroupTag(device_uri
) != IPP_TAG_PRINTER
)
5478 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is in the wrong group."), "device-uri");
5480 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is the wrong value type."), "device-uri");
5485 printer_geo_location
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
);
5486 printer_info
= ippFindAttribute(con
->request
, "printer-info", IPP_TAG_TEXT
);
5487 printer_location
= ippFindAttribute(con
->request
, "printer-location", IPP_TAG_TEXT
);
5490 * See if the printer already exists...
5493 if ((printer
= cupsdFindDest(name
)) != NULL
)
5495 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_POSSIBLE
, _("Printer \"%s\" already exists."), name
);
5496 goto add_printer_attributes
;
5500 * Create the printer...
5503 if ((printer
= cupsdAddPrinter(name
)) == NULL
)
5505 send_ipp_status(con
, IPP_STATUS_ERROR_INTERNAL
, _("Unable to create printer."));
5509 printer
->shared
= 0;
5510 printer
->temporary
= 1;
5512 cupsdSetDeviceURI(printer
, ippGetString(device_uri
, 0, NULL
));
5514 if (printer_geo_location
)
5515 cupsdSetString(&printer
->geo_location
, ippGetString(printer_geo_location
, 0, NULL
));
5517 cupsdSetString(&printer
->info
, ippGetString(printer_info
, 0, NULL
));
5518 if (printer_location
)
5519 cupsdSetString(&printer
->location
, ippGetString(printer_location
, 0, NULL
));
5521 cupsdSetPrinterAttrs(printer
);
5524 * Run a background thread to create the PPD...
5527 _cupsThreadCreate((_cups_thread_func_t
)create_local_bg_thread
, printer
);
5530 * Return printer attributes...
5533 send_ipp_status(con
, IPP_STATUS_OK
, _("Local printer created."));
5535 add_printer_attributes
:
5537 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
5538 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", printer
->state
);
5539 add_printer_state_reasons(con
, printer
);
5541 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), httpIsEncrypted(con
->http
) ? "ipps" : "ipp", NULL
, con
->clientname
, con
->clientport
, "/printers/%s", printer
->name
);
5542 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported", NULL
, uri
);
5547 * 'create_requested_array()' - Create an array for the requested-attributes.
5550 static cups_array_t
* /* O - Array of attributes or NULL */
5551 create_requested_array(ipp_t
*request
) /* I - IPP request */
5553 cups_array_t
*ra
; /* Requested attributes array */
5557 * Create the array for standard attributes...
5560 ra
= ippCreateRequestedArray(request
);
5563 * Add CUPS defaults as needed...
5566 if (cupsArrayFind(ra
, "printer-defaults"))
5569 * Include user-set defaults...
5572 char *name
; /* Option name */
5574 cupsArrayRemove(ra
, "printer-defaults");
5576 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5578 name
= (char *)cupsArrayNext(CommonDefaults
))
5579 if (!cupsArrayFind(ra
, name
))
5580 cupsArrayAdd(ra
, name
);
5588 * 'create_subscriptions()' - Create one or more notification subscriptions.
5592 create_subscriptions(
5593 cupsd_client_t
*con
, /* I - Client connection */
5594 ipp_attribute_t
*uri
) /* I - Printer URI */
5596 http_status_t status
; /* Policy status */
5597 int i
; /* Looping var */
5598 ipp_attribute_t
*attr
; /* Current attribute */
5599 cups_ptype_t dtype
; /* Destination type (printer/class) */
5600 char scheme
[HTTP_MAX_URI
],
5601 /* Scheme portion of URI */
5602 userpass
[HTTP_MAX_URI
],
5603 /* Username portion of URI */
5605 /* Host portion of URI */
5606 resource
[HTTP_MAX_URI
];
5607 /* Resource portion of URI */
5608 int port
; /* Port portion of URI */
5609 cupsd_printer_t
*printer
; /* Printer/class */
5610 cupsd_job_t
*job
; /* Job */
5611 int jobid
; /* Job ID */
5612 cupsd_subscription_t
*sub
; /* Subscription object */
5613 const char *username
, /* requesting-user-name or
5614 authenticated username */
5615 *recipient
, /* notify-recipient-uri */
5616 *pullmethod
; /* notify-pull-method */
5617 ipp_attribute_t
*user_data
; /* notify-user-data */
5618 int interval
, /* notify-time-interval */
5619 lease
; /* notify-lease-duration */
5620 unsigned mask
; /* notify-events */
5621 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5622 *notify_lease
; /* notify-lease-duration(-default) */
5626 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5628 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5629 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5630 attr
->value_tag
, attr
->name
);
5632 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5637 * Is the destination valid?
5640 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5642 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5643 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5644 sizeof(host
), &port
, resource
, sizeof(resource
));
5646 if (!strcmp(resource
, "/"))
5648 dtype
= (cups_ptype_t
)0;
5651 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5653 dtype
= (cups_ptype_t
)0;
5656 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5658 dtype
= CUPS_PRINTER_CLASS
;
5661 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5667 send_ipp_status(con
, IPP_NOT_FOUND
,
5668 _("The printer or class does not exist."));
5678 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5681 send_http_error(con
, status
, printer
);
5685 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5687 send_http_error(con
, status
, NULL
);
5692 * Get the user that is requesting the subscription...
5695 username
= get_username(con
);
5698 * Find the first subscription group attribute; return if we have
5702 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5703 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5708 send_ipp_status(con
, IPP_BAD_REQUEST
,
5709 _("No subscription attributes in request."));
5714 * Process the subscription attributes in the request...
5717 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5725 lease
= DefaultLeaseDuration
;
5727 mask
= CUPSD_EVENT_NONE
;
5731 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5733 notify_lease
= ippFindAttribute(printer
->attrs
,
5734 "notify-lease-duration-default",
5738 lease
= notify_lease
->values
[0].integer
;
5742 notify_events
= NULL
;
5743 notify_lease
= NULL
;
5746 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5748 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5749 attr
->value_tag
== IPP_TAG_URI
)
5752 * Validate the recipient scheme against the ServerBin/notifier
5756 char notifier
[1024]; /* Notifier filename */
5759 recipient
= attr
->values
[0].string
.text
;
5761 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5762 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5763 host
, sizeof(host
), &port
,
5764 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5766 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5767 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5768 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5769 "notify-status-code", IPP_URI_SCHEME
);
5773 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5775 if (access(notifier
, X_OK
))
5777 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5778 _("notify-recipient-uri URI \"%s\" uses unknown "
5779 "scheme."), recipient
);
5780 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5781 "notify-status-code", IPP_URI_SCHEME
);
5785 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5787 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5788 _("notify-recipient-uri URI \"%s\" is already used."),
5790 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5791 "notify-status-code", IPP_ATTRIBUTES
);
5795 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5796 attr
->value_tag
== IPP_TAG_KEYWORD
)
5798 pullmethod
= attr
->values
[0].string
.text
;
5800 if (strcmp(pullmethod
, "ippget"))
5802 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5803 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5804 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5805 "notify-status-code", IPP_ATTRIBUTES
);
5809 else if (!strcmp(attr
->name
, "notify-charset") &&
5810 attr
->value_tag
== IPP_TAG_CHARSET
&&
5811 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5812 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5814 send_ipp_status(con
, IPP_CHARSET
,
5815 _("Character set \"%s\" not supported."),
5816 attr
->values
[0].string
.text
);
5819 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5820 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5821 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5823 send_ipp_status(con
, IPP_CHARSET
,
5824 _("Language \"%s\" not supported."),
5825 attr
->values
[0].string
.text
);
5828 else if (!strcmp(attr
->name
, "notify-user-data") &&
5829 attr
->value_tag
== IPP_TAG_STRING
)
5831 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5833 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5834 _("The notify-user-data value is too large "
5835 "(%d > 63 octets)."),
5836 attr
->values
[0].unknown
.length
);
5842 else if (!strcmp(attr
->name
, "notify-events") &&
5843 attr
->value_tag
== IPP_TAG_KEYWORD
)
5844 notify_events
= attr
;
5845 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5846 attr
->value_tag
== IPP_TAG_INTEGER
)
5847 lease
= attr
->values
[0].integer
;
5848 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5849 attr
->value_tag
== IPP_TAG_INTEGER
)
5850 interval
= attr
->values
[0].integer
;
5851 else if (!strcmp(attr
->name
, "notify-job-id") &&
5852 attr
->value_tag
== IPP_TAG_INTEGER
)
5853 jobid
= attr
->values
[0].integer
;
5860 for (i
= 0; i
< notify_events
->num_values
; i
++)
5861 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5865 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5867 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5868 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5869 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5871 if (!recipient
&& !pullmethod
)
5874 if (mask
== CUPSD_EVENT_NONE
)
5877 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5879 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5882 send_ipp_status(con
, IPP_BAD_REQUEST
,
5883 _("notify-events not specified."));
5888 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5890 cupsdLogMessage(CUPSD_LOG_INFO
,
5891 "create_subscriptions: Limiting notify-lease-duration to "
5894 lease
= MaxLeaseDuration
;
5899 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5901 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5909 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5911 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5912 _("There are too many subscriptions."));
5917 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5920 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5921 "Added subscription #%d for printer \"%s\".",
5922 sub
->id
, printer
->name
);
5924 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5927 sub
->interval
= interval
;
5929 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5931 cupsdSetString(&sub
->owner
, username
);
5935 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5936 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5937 (size_t)sub
->user_data_len
);
5940 ippAddSeparator(con
->response
);
5941 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5942 "notify-subscription-id", sub
->id
);
5944 con
->response
->request
.status
.status_code
= IPP_OK
;
5950 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5955 * 'delete_printer()' - Remove a printer or class from the system.
5959 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5960 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5962 http_status_t status
; /* Policy status */
5963 cups_ptype_t dtype
; /* Destination type (printer/class) */
5964 cupsd_printer_t
*printer
; /* Printer/class */
5965 char filename
[1024]; /* Script/PPD filename */
5966 int temporary
; /* Temporary queue? */
5969 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5970 con
->number
, uri
->values
[0].string
.text
);
5973 * Do we have a valid URI?
5976 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5982 send_ipp_status(con
, IPP_NOT_FOUND
,
5983 _("The printer or class does not exist."));
5991 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5993 send_http_error(con
, status
, NULL
);
5998 * Remove old jobs...
6001 cupsdCancelJobs(printer
->name
, NULL
, 1);
6004 * Remove old subscriptions and send a "deleted printer" event...
6007 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6008 "%s \"%s\" deleted by \"%s\".",
6009 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6010 printer
->name
, get_username(con
));
6012 cupsdExpireSubscriptions(printer
, NULL
);
6015 * Remove any old PPD or script files...
6018 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6021 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
6025 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
6028 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
6032 * Unregister color profiles...
6035 cupsdUnregisterColor(printer
);
6037 temporary
= printer
->temporary
;
6039 if (dtype
& CUPS_PRINTER_CLASS
)
6041 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6042 printer
->name
, get_username(con
));
6044 cupsdDeletePrinter(printer
, 0);
6046 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6050 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6051 printer
->name
, get_username(con
));
6053 if (cupsdDeletePrinter(printer
, 0) && !temporary
)
6054 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6057 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6061 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6064 * Return with no errors...
6067 con
->response
->request
.status
.status_code
= IPP_OK
;
6072 * 'get_default()' - Get the default destination.
6076 get_default(cupsd_client_t
*con
) /* I - Client connection */
6078 http_status_t status
; /* Policy status */
6079 cups_array_t
*ra
; /* Requested attributes array */
6082 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
6088 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6090 send_http_error(con
, status
, NULL
);
6096 ra
= create_requested_array(con
->request
);
6098 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6100 cupsArrayDelete(ra
);
6102 con
->response
->request
.status
.status_code
= IPP_OK
;
6105 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
6110 * 'get_devices()' - Get the list of available devices on the local system.
6114 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6116 http_status_t status
; /* Policy status */
6117 ipp_attribute_t
*limit
, /* limit attribute */
6118 *timeout
, /* timeout attribute */
6119 *requested
, /* requested-attributes attribute */
6120 *exclude
, /* exclude-schemes attribute */
6121 *include
; /* include-schemes attribute */
6122 char command
[1024], /* cups-deviced command */
6123 options
[2048], /* Options to pass to command */
6125 /* String for requested attributes */
6127 /* String for excluded schemes */
6129 /* String for included schemes */
6132 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
6138 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6140 send_http_error(con
, status
, NULL
);
6145 * Run cups-deviced command with the given options...
6148 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6149 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6150 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6152 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6153 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
6156 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6158 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6161 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6163 exclude_str
[0] = '\0';
6166 url_encode_attr(include
, include_str
, sizeof(include_str
));
6168 include_str
[0] = '\0';
6170 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6171 snprintf(options
, sizeof(options
),
6172 "%d+%d+%d+%d+%s%s%s%s%s",
6173 con
->request
->request
.op
.request_id
,
6174 limit
? limit
->values
[0].integer
: 0,
6175 timeout
? timeout
->values
[0].integer
: 15,
6178 exclude_str
[0] ? "%20" : "", exclude_str
,
6179 include_str
[0] ? "%20" : "", include_str
);
6181 if (cupsdSendCommand(con
, command
, options
, 1))
6184 * Command started successfully, don't send an IPP response here...
6187 ippDelete(con
->response
);
6188 con
->response
= NULL
;
6193 * Command failed, return "internal error" so the user knows something
6197 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6198 _("cups-deviced failed to execute."));
6204 * 'get_document()' - Get a copy of a job file.
6208 get_document(cupsd_client_t
*con
, /* I - Client connection */
6209 ipp_attribute_t
*uri
) /* I - Job URI */
6211 http_status_t status
; /* Policy status */
6212 ipp_attribute_t
*attr
; /* Current attribute */
6213 int jobid
; /* Job ID */
6214 int docnum
; /* Document number */
6215 cupsd_job_t
*job
; /* Current job */
6216 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6217 username
[HTTP_MAX_URI
], /* Username portion of URI */
6218 host
[HTTP_MAX_URI
], /* Host portion of URI */
6219 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6220 int port
; /* Port portion of URI */
6221 char filename
[1024], /* Filename for document */
6222 format
[1024]; /* Format for document */
6225 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6226 con
->number
, uri
->values
[0].string
.text
);
6229 * See if we have a job URI or a printer URI...
6232 if (!strcmp(uri
->name
, "printer-uri"))
6235 * Got a printer URI; see if we also have a job-id attribute...
6238 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6239 IPP_TAG_INTEGER
)) == NULL
)
6241 send_ipp_status(con
, IPP_BAD_REQUEST
,
6242 _("Got a printer-uri attribute but no job-id."));
6246 jobid
= attr
->values
[0].integer
;
6251 * Got a job URI; parse it to get the job ID...
6254 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6255 sizeof(scheme
), username
, sizeof(username
), host
,
6256 sizeof(host
), &port
, resource
, sizeof(resource
));
6258 if (strncmp(resource
, "/jobs/", 6))
6264 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6265 uri
->values
[0].string
.text
);
6269 jobid
= atoi(resource
+ 6);
6273 * See if the job exists...
6276 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6279 * Nope - return a "not found" error...
6282 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6290 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
6291 job
->username
)) != HTTP_OK
)
6293 send_http_error(con
, status
, NULL
);
6298 * Get the document number...
6301 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6302 IPP_TAG_INTEGER
)) == NULL
)
6304 send_ipp_status(con
, IPP_BAD_REQUEST
,
6305 _("Missing document-number attribute."));
6309 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6310 attr
->num_values
> 1)
6312 send_ipp_status(con
, IPP_NOT_FOUND
,
6313 _("Document #%d does not exist in job #%d."), docnum
,
6318 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6320 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6322 cupsdLogMessage(CUPSD_LOG_ERROR
,
6323 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6325 send_ipp_status(con
, IPP_NOT_FOUND
,
6326 _("Unable to open document #%d in job #%d."), docnum
,
6331 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6335 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6336 job
->filetypes
[docnum
- 1]->type
);
6338 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6340 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6342 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6343 IPP_TAG_NAME
)) != NULL
)
6344 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6345 NULL
, attr
->values
[0].string
.text
);
6350 * 'get_job_attrs()' - Get job attributes.
6354 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6355 ipp_attribute_t
*uri
) /* I - Job URI */
6357 http_status_t status
; /* Policy status */
6358 ipp_attribute_t
*attr
; /* Current attribute */
6359 int jobid
; /* Job ID */
6360 cupsd_job_t
*job
; /* Current job */
6361 cupsd_printer_t
*printer
; /* Current printer */
6362 cupsd_policy_t
*policy
; /* Current security policy */
6363 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6364 username
[HTTP_MAX_URI
], /* Username portion of URI */
6365 host
[HTTP_MAX_URI
], /* Host portion of URI */
6366 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6367 int port
; /* Port portion of URI */
6368 cups_array_t
*ra
, /* Requested attributes array */
6369 *exclude
; /* Private attributes array */
6372 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6373 con
->number
, uri
->values
[0].string
.text
);
6376 * See if we have a job URI or a printer URI...
6379 if (!strcmp(uri
->name
, "printer-uri"))
6382 * Got a printer URI; see if we also have a job-id attribute...
6385 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6386 IPP_TAG_INTEGER
)) == NULL
)
6388 send_ipp_status(con
, IPP_BAD_REQUEST
,
6389 _("Got a printer-uri attribute but no job-id."));
6393 jobid
= attr
->values
[0].integer
;
6398 * Got a job URI; parse it to get the job ID...
6401 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6402 sizeof(scheme
), username
, sizeof(username
), host
,
6403 sizeof(host
), &port
, resource
, sizeof(resource
));
6405 if (strncmp(resource
, "/jobs/", 6))
6411 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6412 uri
->values
[0].string
.text
);
6416 jobid
= atoi(resource
+ 6);
6420 * See if the job exists...
6423 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6426 * Nope - return a "not found" error...
6429 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6437 if ((printer
= job
->printer
) == NULL
)
6438 printer
= cupsdFindDest(job
->dest
);
6441 policy
= printer
->op_policy_ptr
;
6443 policy
= DefaultPolicyPtr
;
6445 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6447 send_http_error(con
, status
, NULL
);
6451 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6454 * Copy attributes...
6459 ra
= create_requested_array(con
->request
);
6460 copy_job_attrs(con
, job
, ra
, exclude
);
6461 cupsArrayDelete(ra
);
6463 con
->response
->request
.status
.status_code
= IPP_OK
;
6468 * 'get_jobs()' - Get a list of jobs for the specified printer.
6472 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6473 ipp_attribute_t
*uri
) /* I - Printer URI */
6475 http_status_t status
; /* Policy status */
6476 ipp_attribute_t
*attr
; /* Current attribute */
6477 const char *dest
; /* Destination */
6478 cups_ptype_t dtype
; /* Destination type (printer/class) */
6479 cups_ptype_t dmask
; /* Destination type mask */
6480 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6481 username
[HTTP_MAX_URI
], /* Username portion of URI */
6482 host
[HTTP_MAX_URI
], /* Host portion of URI */
6483 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6484 int port
; /* Port portion of URI */
6485 int job_comparison
; /* Job comparison */
6486 ipp_jstate_t job_state
; /* job-state value */
6487 int first_job_id
= 1, /* First job ID */
6488 first_index
= 1, /* First index */
6489 limit
= 0, /* Maximum number of jobs to return */
6490 count
, /* Number of jobs that match */
6491 need_load_job
= 0; /* Do we need to load the job? */
6492 const char *job_attr
; /* Job attribute requested */
6493 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6494 cupsd_job_t
*job
; /* Current job pointer */
6495 cupsd_printer_t
*printer
; /* Printer */
6496 cups_array_t
*list
; /* Which job list... */
6497 int delete_list
= 0; /* Delete the list afterwards? */
6498 cups_array_t
*ra
, /* Requested attributes array */
6499 *exclude
; /* Private attributes array */
6500 cupsd_policy_t
*policy
; /* Current policy */
6503 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6504 uri
->values
[0].string
.text
);
6507 * Is the destination valid?
6510 if (strcmp(uri
->name
, "printer-uri"))
6512 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6516 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6517 sizeof(scheme
), username
, sizeof(username
), host
,
6518 sizeof(host
), &port
, resource
, sizeof(resource
));
6520 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6523 dtype
= (cups_ptype_t
)0;
6524 dmask
= (cups_ptype_t
)0;
6527 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6530 dtype
= (cups_ptype_t
)0;
6531 dmask
= CUPS_PRINTER_CLASS
;
6534 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6537 dtype
= CUPS_PRINTER_CLASS
;
6538 dmask
= CUPS_PRINTER_CLASS
;
6541 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6548 send_ipp_status(con
, IPP_NOT_FOUND
,
6549 _("The printer or class does not exist."));
6554 dtype
&= CUPS_PRINTER_CLASS
;
6555 dmask
= CUPS_PRINTER_CLASS
;
6563 policy
= printer
->op_policy_ptr
;
6565 policy
= DefaultPolicyPtr
;
6567 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6569 send_http_error(con
, status
, NULL
);
6573 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6576 * See if the "which-jobs" attribute have been specified...
6579 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6580 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6582 send_ipp_status(con
, IPP_CONFLICT
,
6583 _("The %s attribute cannot be provided with job-ids."),
6587 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6589 job_comparison
= -1;
6590 job_state
= IPP_JOB_STOPPED
;
6593 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6596 job_state
= IPP_JOB_CANCELED
;
6597 list
= cupsdGetCompletedJobs(printer
);
6600 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6603 job_state
= IPP_JOB_ABORTED
;
6604 list
= cupsdGetCompletedJobs(printer
);
6607 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6610 job_state
= IPP_JOB_PENDING
;
6613 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6616 job_state
= IPP_JOB_CANCELED
;
6617 list
= cupsdGetCompletedJobs(printer
);
6620 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6623 job_state
= IPP_JOB_PENDING
;
6626 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6629 job_state
= IPP_JOB_HELD
;
6632 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6635 job_state
= IPP_JOB_PROCESSING
;
6636 list
= PrintingJobs
;
6638 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6641 job_state
= IPP_JOB_STOPPED
;
6646 send_ipp_status(con
, IPP_ATTRIBUTES
,
6647 _("The which-jobs value \"%s\" is not supported."),
6648 attr
->values
[0].string
.text
);
6649 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6650 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6655 * See if they want to limit the number of jobs reported...
6658 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6662 send_ipp_status(con
, IPP_CONFLICT
,
6663 _("The %s attribute cannot be provided with job-ids."),
6668 limit
= attr
->values
[0].integer
;
6671 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6675 send_ipp_status(con
, IPP_CONFLICT
,
6676 _("The %s attribute cannot be provided with job-ids."),
6681 first_index
= attr
->values
[0].integer
;
6683 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6687 send_ipp_status(con
, IPP_CONFLICT
,
6688 _("The %s attribute cannot be provided with job-ids."),
6693 first_job_id
= attr
->values
[0].integer
;
6697 * See if we only want to see jobs for a specific user...
6700 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6702 send_ipp_status(con
, IPP_CONFLICT
,
6703 _("The %s attribute cannot be provided with job-ids."),
6707 else if (attr
&& attr
->values
[0].boolean
)
6708 strlcpy(username
, get_username(con
), sizeof(username
));
6712 ra
= create_requested_array(con
->request
);
6713 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6714 if (strcmp(job_attr
, "job-id") &&
6715 strcmp(job_attr
, "job-k-octets") &&
6716 strcmp(job_attr
, "job-media-progress") &&
6717 strcmp(job_attr
, "job-more-info") &&
6718 strcmp(job_attr
, "job-name") &&
6719 strcmp(job_attr
, "job-originating-user-name") &&
6720 strcmp(job_attr
, "job-preserved") &&
6721 strcmp(job_attr
, "job-printer-up-time") &&
6722 strcmp(job_attr
, "job-printer-uri") &&
6723 strcmp(job_attr
, "job-state") &&
6724 strcmp(job_attr
, "job-state-reasons") &&
6725 strcmp(job_attr
, "job-uri") &&
6726 strcmp(job_attr
, "time-at-completed") &&
6727 strcmp(job_attr
, "time-at-creation") &&
6728 strcmp(job_attr
, "number-of-documents"))
6734 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6737 * Limit expensive Get-Jobs for job history to 500 jobs...
6740 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6743 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6747 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6751 * OK, build a list of jobs for this printer...
6756 int i
; /* Looping var */
6758 for (i
= 0; i
< job_ids
->num_values
; i
++)
6760 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6764 if (i
< job_ids
->num_values
)
6766 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6767 job_ids
->values
[i
].integer
);
6771 for (i
= 0; i
< job_ids
->num_values
; i
++)
6773 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6775 if (need_load_job
&& !job
->attrs
)
6781 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6787 ippAddSeparator(con
->response
);
6789 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6790 job
->printer
->op_policy_ptr
:
6791 policy
, con
, job
->printer
,
6794 copy_job_attrs(con
, job
, ra
, exclude
);
6799 if (first_index
> 1)
6800 job
= (cupsd_job_t
*)cupsArrayIndex(list
, first_index
- 1);
6802 job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6804 for (count
= 0; (limit
<= 0 || count
< limit
) && job
; job
= (cupsd_job_t
*)cupsArrayNext(list
))
6807 * Filter out jobs that don't match...
6810 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6811 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6812 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6813 job
->username
, job
->state_value
, job
->attrs
);
6815 if (!job
->dest
|| !job
->username
)
6818 if (!job
->dest
|| !job
->username
)
6821 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6822 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6824 if ((job
->dtype
& dmask
) != dtype
&&
6825 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6828 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6829 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6830 (job_comparison
> 0 && job
->state_value
< job_state
))
6833 if (job
->id
< first_job_id
)
6836 if (need_load_job
&& !job
->attrs
)
6842 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6847 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6851 ippAddSeparator(con
->response
);
6855 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6856 job
->printer
->op_policy_ptr
:
6857 policy
, con
, job
->printer
,
6860 copy_job_attrs(con
, job
, ra
, exclude
);
6863 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6866 cupsArrayDelete(ra
);
6869 cupsArrayDelete(list
);
6871 con
->response
->request
.status
.status_code
= IPP_OK
;
6876 * 'get_notifications()' - Get events for a subscription.
6880 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6882 int i
, j
; /* Looping vars */
6883 http_status_t status
; /* Policy status */
6884 cupsd_subscription_t
*sub
; /* Subscription */
6885 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6886 *sequences
; /* notify-sequence-numbers */
6887 int min_seq
; /* Minimum sequence number */
6888 int interval
; /* Poll interval */
6891 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6895 * Get subscription attributes...
6898 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6900 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6905 send_ipp_status(con
, IPP_BAD_REQUEST
,
6906 _("Missing notify-subscription-ids attribute."));
6911 * Are the subscription IDs valid?
6914 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6916 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6919 * Bad subscription ID...
6922 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6923 ids
->values
[i
].integer
);
6931 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6933 con
, sub
->owner
)) != HTTP_OK
)
6935 send_http_error(con
, status
, sub
->dest
);
6940 * Check the subscription type and update the interval accordingly.
6943 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6946 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6948 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6954 * Tell the client to poll again in N seconds...
6958 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6959 "notify-get-interval", interval
);
6961 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6962 "printer-up-time", time(NULL
));
6965 * Copy the subscription event attributes to the response.
6968 con
->response
->request
.status
.status_code
=
6969 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6971 for (i
= 0; i
< ids
->num_values
; i
++)
6974 * Get the subscription and sequence number...
6977 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6979 if (sequences
&& i
< sequences
->num_values
)
6980 min_seq
= sequences
->values
[i
].integer
;
6985 * If we don't have any new events, nothing to do here...
6988 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
6992 * Otherwise copy all of the new events...
6995 if (sub
->first_event_id
> min_seq
)
6998 j
= min_seq
- sub
->first_event_id
;
7000 for (; j
< cupsArrayCount(sub
->events
); j
++)
7002 ippAddSeparator(con
->response
);
7004 copy_attrs(con
->response
,
7005 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
7006 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
7013 * 'get_ppd()' - Get a named PPD from the local system.
7017 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7018 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7020 http_status_t status
; /* Policy status */
7021 cupsd_printer_t
*dest
; /* Destination */
7022 cups_ptype_t dtype
; /* Destination type */
7025 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7026 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7028 if (!strcmp(ippGetName(uri
), "ppd-name"))
7031 * Return a PPD file from cups-driverd...
7034 const char *ppd_name
= ippGetString(uri
, 0, NULL
);
7035 /* ppd-name value */
7036 char command
[1024], /* cups-driverd command */
7037 options
[1024], /* Options to pass to command */
7038 oppd_name
[1024]; /* Escaped ppd-name */
7044 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7046 send_http_error(con
, status
, NULL
);
7051 * Check ppd-name value...
7054 if (strstr(ppd_name
, "../"))
7056 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
7061 * Run cups-driverd command with the given options...
7064 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7065 url_encode_string(ppd_name
, oppd_name
, sizeof(oppd_name
));
7066 snprintf(options
, sizeof(options
), "get+%d+%s", ippGetRequestId(con
->request
), oppd_name
);
7068 if (cupsdSendCommand(con
, command
, options
, 0))
7071 * Command started successfully, don't send an IPP response here...
7074 ippDelete(con
->response
);
7075 con
->response
= NULL
;
7080 * Command failed, return "internal error" so the user knows something
7084 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("cups-driverd failed to execute."));
7087 else if (!strcmp(ippGetName(uri
), "printer-uri") && cupsdValidateDest(ippGetString(uri
, 0, NULL
), &dtype
, &dest
))
7089 int i
; /* Looping var */
7090 char filename
[1024]; /* PPD filename */
7096 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7098 send_http_error(con
, status
, dest
);
7103 * See if we need the PPD for a class or remote printer...
7106 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->name
);
7108 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7110 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
7111 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->uri
);
7114 else if (dtype
& CUPS_PRINTER_CLASS
)
7116 for (i
= 0; i
< dest
->num_printers
; i
++)
7117 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
7119 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->printers
[i
]->name
);
7121 if (!access(filename
, 0))
7125 if (i
< dest
->num_printers
)
7126 dest
= dest
->printers
[i
];
7129 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
7130 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->printers
[0]->uri
);
7136 * Found the printer with the PPD file, now see if there is one...
7139 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7141 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be opened: %s"), ippGetString(uri
, 0, NULL
), strerror(errno
));
7145 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7149 ippSetStatusCode(con
->response
, IPP_STATUS_OK
);
7152 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be found."), ippGetString(uri
, 0, NULL
));
7157 * 'get_ppds()' - Get the list of PPD files on the local system.
7161 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7163 http_status_t status
; /* Policy status */
7164 ipp_attribute_t
*limit
, /* Limit attribute */
7165 *device
, /* ppd-device-id attribute */
7166 *language
, /* ppd-natural-language attribute */
7167 *make
, /* ppd-make attribute */
7168 *model
, /* ppd-make-and-model attribute */
7169 *model_number
, /* ppd-model-number attribute */
7170 *product
, /* ppd-product attribute */
7171 *psversion
, /* ppd-psverion attribute */
7172 *type
, /* ppd-type attribute */
7173 *requested
, /* requested-attributes attribute */
7174 *exclude
, /* exclude-schemes attribute */
7175 *include
; /* include-schemes attribute */
7176 char command
[1024], /* cups-driverd command */
7177 options
[4096], /* Options to pass to command */
7178 device_str
[256],/* Escaped ppd-device-id string */
7180 /* Escaped ppd-natural-language */
7181 make_str
[256], /* Escaped ppd-make string */
7182 model_str
[256], /* Escaped ppd-make-and-model string */
7183 model_number_str
[256],
7184 /* ppd-model-number string */
7186 /* Escaped ppd-product string */
7188 /* Escaped ppd-psversion string */
7189 type_str
[256], /* Escaped ppd-type string */
7191 /* String for requested attributes */
7193 /* String for excluded schemes */
7195 /* String for included schemes */
7198 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
7204 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7206 send_http_error(con
, status
, NULL
);
7211 * Run cups-driverd command with the given options...
7214 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7215 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7216 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7218 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7219 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7221 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7223 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7224 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7225 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7226 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7228 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
7230 include
= ippFindAttribute(con
->request
, "include-schemes",
7234 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7236 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7239 url_encode_attr(device
, device_str
, sizeof(device_str
));
7241 device_str
[0] = '\0';
7244 url_encode_attr(language
, language_str
, sizeof(language_str
));
7246 language_str
[0] = '\0';
7249 url_encode_attr(make
, make_str
, sizeof(make_str
));
7254 url_encode_attr(model
, model_str
, sizeof(model_str
));
7256 model_str
[0] = '\0';
7259 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7260 model_number
->values
[0].integer
);
7262 model_number_str
[0] = '\0';
7265 url_encode_attr(product
, product_str
, sizeof(product_str
));
7267 product_str
[0] = '\0';
7270 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7272 psversion_str
[0] = '\0';
7275 url_encode_attr(type
, type_str
, sizeof(type_str
));
7280 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
7282 exclude_str
[0] = '\0';
7285 url_encode_attr(include
, include_str
, sizeof(include_str
));
7287 include_str
[0] = '\0';
7289 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7290 snprintf(options
, sizeof(options
),
7291 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7292 con
->request
->request
.op
.request_id
,
7293 limit
? limit
->values
[0].integer
: 0,
7295 device
? "%20" : "", device_str
,
7296 language
? "%20" : "", language_str
,
7297 make
? "%20" : "", make_str
,
7298 model
? "%20" : "", model_str
,
7299 model_number
? "%20" : "", model_number_str
,
7300 product
? "%20" : "", product_str
,
7301 psversion
? "%20" : "", psversion_str
,
7302 type
? "%20" : "", type_str
,
7303 exclude_str
[0] ? "%20" : "", exclude_str
,
7304 include_str
[0] ? "%20" : "", include_str
);
7306 if (cupsdSendCommand(con
, command
, options
, 0))
7309 * Command started successfully, don't send an IPP response here...
7312 ippDelete(con
->response
);
7313 con
->response
= NULL
;
7318 * Command failed, return "internal error" so the user knows something
7322 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7323 _("cups-driverd failed to execute."));
7329 * 'get_printer_attrs()' - Get printer attributes.
7333 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7334 ipp_attribute_t
*uri
) /* I - Printer URI */
7336 http_status_t status
; /* Policy status */
7337 cups_ptype_t dtype
; /* Destination type (printer/class) */
7338 cupsd_printer_t
*printer
; /* Printer/class */
7339 cups_array_t
*ra
; /* Requested attributes array */
7342 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7343 con
->number
, uri
->values
[0].string
.text
);
7346 * Is the destination valid?
7349 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7355 send_ipp_status(con
, IPP_NOT_FOUND
,
7356 _("The printer or class does not exist."));
7364 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7366 send_http_error(con
, status
, printer
);
7371 * Send the attributes...
7374 ra
= create_requested_array(con
->request
);
7376 copy_printer_attrs(con
, printer
, ra
);
7378 cupsArrayDelete(ra
);
7380 con
->response
->request
.status
.status_code
= IPP_OK
;
7385 * 'get_printer_supported()' - Get printer supported values.
7389 get_printer_supported(
7390 cupsd_client_t
*con
, /* I - Client connection */
7391 ipp_attribute_t
*uri
) /* I - Printer URI */
7393 http_status_t status
; /* Policy status */
7394 cups_ptype_t dtype
; /* Destination type (printer/class) */
7395 cupsd_printer_t
*printer
; /* Printer/class */
7398 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7399 con
->number
, uri
->values
[0].string
.text
);
7402 * Is the destination valid?
7405 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7411 send_ipp_status(con
, IPP_NOT_FOUND
,
7412 _("The printer or class does not exist."));
7420 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7422 send_http_error(con
, status
, printer
);
7427 * Return a list of attributes that can be set via Set-Printer-Attributes.
7430 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7431 "printer-geo-location", 0);
7432 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7434 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7435 "printer-location", 0);
7436 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7437 "printer-organization", 0);
7438 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7439 "printer-organizational-unit", 0);
7441 con
->response
->request
.status
.status_code
= IPP_OK
;
7446 * 'get_printers()' - Get a list of printers or classes.
7450 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7451 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7453 http_status_t status
; /* Policy status */
7454 ipp_attribute_t
*attr
; /* Current attribute */
7455 int limit
; /* Max number of printers to return */
7456 int count
; /* Number of printers that match */
7457 cupsd_printer_t
*printer
; /* Current printer pointer */
7458 cups_ptype_t printer_type
, /* printer-type attribute */
7459 printer_mask
; /* printer-type-mask attribute */
7460 char *location
; /* Location string */
7461 const char *username
; /* Current user */
7462 char *first_printer_name
; /* first-printer-name attribute */
7463 cups_array_t
*ra
; /* Requested attributes array */
7464 int local
; /* Local connection? */
7467 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7474 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7476 send_http_error(con
, status
, NULL
);
7481 * Check for printers...
7484 if (!Printers
|| !cupsArrayCount(Printers
))
7486 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7491 * See if they want to limit the number of printers reported...
7494 if ((attr
= ippFindAttribute(con
->request
, "limit",
7495 IPP_TAG_INTEGER
)) != NULL
)
7496 limit
= attr
->values
[0].integer
;
7500 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7501 IPP_TAG_NAME
)) != NULL
)
7502 first_printer_name
= attr
->values
[0].string
.text
;
7504 first_printer_name
= NULL
;
7507 * Support filtering...
7510 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7511 IPP_TAG_ENUM
)) != NULL
)
7512 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7514 printer_type
= (cups_ptype_t
)0;
7516 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7517 IPP_TAG_ENUM
)) != NULL
)
7518 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7520 printer_mask
= (cups_ptype_t
)0;
7522 local
= httpAddrLocalhost(&(con
->clientaddr
));
7524 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7525 IPP_TAG_TEXT
)) != NULL
)
7526 location
= attr
->values
[0].string
.text
;
7530 if (con
->username
[0])
7531 username
= con
->username
;
7532 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7533 IPP_TAG_NAME
)) != NULL
)
7534 username
= attr
->values
[0].string
.text
;
7538 ra
= create_requested_array(con
->request
);
7541 * OK, build a list of printers for this printer...
7544 if (first_printer_name
)
7546 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7547 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7550 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7553 count
< limit
&& printer
;
7554 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7556 if (!local
&& !printer
->shared
)
7559 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7560 (printer
->type
& printer_mask
) == printer_type
&&
7562 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7565 * If a username is specified, see if it is allowed or denied
7569 if (cupsArrayCount(printer
->users
) && username
&&
7570 !user_allowed(printer
, username
))
7574 * Add the group separator as needed...
7578 ippAddSeparator(con
->response
);
7583 * Send the attributes...
7586 copy_printer_attrs(con
, printer
, ra
);
7590 cupsArrayDelete(ra
);
7592 con
->response
->request
.status
.status_code
= IPP_OK
;
7597 * 'get_subscription_attrs()' - Get subscription attributes.
7601 get_subscription_attrs(
7602 cupsd_client_t
*con
, /* I - Client connection */
7603 int sub_id
) /* I - Subscription ID */
7605 http_status_t status
; /* Policy status */
7606 cupsd_subscription_t
*sub
; /* Subscription */
7607 cupsd_policy_t
*policy
; /* Current security policy */
7608 cups_array_t
*ra
, /* Requested attributes array */
7609 *exclude
; /* Private attributes array */
7612 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7613 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7614 con
, con
->number
, sub_id
);
7617 * Expire subscriptions as needed...
7620 cupsdExpireSubscriptions(NULL
, NULL
);
7623 * Is the subscription ID valid?
7626 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7629 * Bad subscription ID...
7632 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7642 policy
= sub
->dest
->op_policy_ptr
;
7644 policy
= DefaultPolicyPtr
;
7646 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7648 send_http_error(con
, status
, sub
->dest
);
7652 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7655 * Copy the subscription attributes to the response using the
7656 * requested-attributes attribute that may be provided by the client.
7659 ra
= create_requested_array(con
->request
);
7661 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7663 cupsArrayDelete(ra
);
7665 con
->response
->request
.status
.status_code
= IPP_OK
;
7670 * 'get_subscriptions()' - Get subscriptions.
7674 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7675 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7677 http_status_t status
; /* Policy status */
7678 int count
; /* Number of subscriptions */
7679 int limit
; /* Limit */
7680 cupsd_subscription_t
*sub
; /* Subscription */
7681 cups_array_t
*ra
; /* Requested attributes array */
7682 ipp_attribute_t
*attr
; /* Attribute */
7683 cups_ptype_t dtype
; /* Destination type (printer/class) */
7684 char scheme
[HTTP_MAX_URI
],
7685 /* Scheme portion of URI */
7686 username
[HTTP_MAX_URI
],
7687 /* Username portion of URI */
7689 /* Host portion of URI */
7690 resource
[HTTP_MAX_URI
];
7691 /* Resource portion of URI */
7692 int port
; /* Port portion of URI */
7693 cupsd_job_t
*job
; /* Job pointer */
7694 cupsd_printer_t
*printer
; /* Printer */
7695 cupsd_policy_t
*policy
; /* Policy */
7696 cups_array_t
*exclude
; /* Private attributes array */
7699 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7700 "get_subscriptions(con=%p[%d], uri=%s)",
7701 con
, con
->number
, uri
->values
[0].string
.text
);
7704 * Is the destination valid?
7707 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7708 sizeof(scheme
), username
, sizeof(username
), host
,
7709 sizeof(host
), &port
, resource
, sizeof(resource
));
7711 if (!strcmp(resource
, "/") ||
7712 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7713 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7714 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7719 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7722 job
= cupsdFindJob(atoi(resource
+ 6));
7726 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7727 atoi(resource
+ 6));
7731 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7737 send_ipp_status(con
, IPP_NOT_FOUND
,
7738 _("The printer or class does not exist."));
7741 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7742 IPP_TAG_INTEGER
)) != NULL
)
7744 job
= cupsdFindJob(attr
->values
[0].integer
);
7748 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7749 attr
->values
[0].integer
);
7761 policy
= printer
->op_policy_ptr
;
7763 policy
= DefaultPolicyPtr
;
7765 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7767 send_http_error(con
, status
, printer
);
7772 * Expire subscriptions as needed...
7775 cupsdExpireSubscriptions(NULL
, NULL
);
7778 * Copy the subscription attributes to the response using the
7779 * requested-attributes attribute that may be provided by the client.
7782 ra
= create_requested_array(con
->request
);
7784 if ((attr
= ippFindAttribute(con
->request
, "limit",
7785 IPP_TAG_INTEGER
)) != NULL
)
7786 limit
= attr
->values
[0].integer
;
7791 * See if we only want to see subscriptions for a specific user...
7794 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7795 IPP_TAG_BOOLEAN
)) != NULL
&&
7796 attr
->values
[0].boolean
)
7797 strlcpy(username
, get_username(con
), sizeof(username
));
7801 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7803 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7804 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7805 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7807 ippAddSeparator(con
->response
);
7809 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7810 policy
, con
, sub
->dest
,
7813 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7816 if (limit
&& count
>= limit
)
7820 cupsArrayDelete(ra
);
7823 con
->response
->request
.status
.status_code
= IPP_OK
;
7825 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7830 * 'get_username()' - Get the username associated with a request.
7833 static const char * /* O - Username */
7834 get_username(cupsd_client_t
*con
) /* I - Connection */
7836 ipp_attribute_t
*attr
; /* Attribute */
7839 if (con
->username
[0])
7840 return (con
->username
);
7841 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7842 IPP_TAG_NAME
)) != NULL
)
7843 return (attr
->values
[0].string
.text
);
7845 return ("anonymous");
7850 * 'hold_job()' - Hold a print job.
7854 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7855 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7857 ipp_attribute_t
*attr
; /* Current job-hold-until */
7858 const char *when
; /* New value */
7859 int jobid
; /* Job ID */
7860 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7861 username
[HTTP_MAX_URI
], /* Username portion of URI */
7862 host
[HTTP_MAX_URI
], /* Host portion of URI */
7863 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7864 int port
; /* Port portion of URI */
7865 cupsd_job_t
*job
; /* Job information */
7868 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7869 uri
->values
[0].string
.text
);
7872 * See if we have a job URI or a printer URI...
7875 if (!strcmp(uri
->name
, "printer-uri"))
7878 * Got a printer URI; see if we also have a job-id attribute...
7881 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7882 IPP_TAG_INTEGER
)) == NULL
)
7884 send_ipp_status(con
, IPP_BAD_REQUEST
,
7885 _("Got a printer-uri attribute but no job-id."));
7889 jobid
= attr
->values
[0].integer
;
7894 * Got a job URI; parse it to get the job ID...
7897 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7898 sizeof(scheme
), username
, sizeof(username
), host
,
7899 sizeof(host
), &port
, resource
, sizeof(resource
));
7901 if (strncmp(resource
, "/jobs/", 6))
7907 send_ipp_status(con
, IPP_BAD_REQUEST
,
7908 _("Bad job-uri \"%s\"."),
7909 uri
->values
[0].string
.text
);
7913 jobid
= atoi(resource
+ 6);
7917 * See if the job exists...
7920 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7923 * Nope - return a "not found" error...
7926 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7931 * See if the job is owned by the requesting user...
7934 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7936 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7937 cupsdFindDest(job
->dest
));
7942 * See if the job is in a state that allows holding...
7945 if (job
->state_value
> IPP_JOB_STOPPED
)
7948 * Return a "not-possible" error...
7951 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7952 _("Job #%d is finished and cannot be altered."),
7958 * Hold the job and return...
7961 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
7962 IPP_TAG_KEYWORD
)) == NULL
)
7963 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7967 when
= attr
->values
[0].string
.text
;
7969 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7970 "Job job-hold-until value changed by user.");
7973 when
= "indefinite";
7975 cupsdSetJobHoldUntil(job
, when
, 1);
7976 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
7979 con
->response
->request
.status
.status_code
= IPP_OK
;
7984 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7988 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
7989 ipp_attribute_t
*uri
) /* I - Printer URI */
7991 http_status_t status
; /* Policy status */
7992 cups_ptype_t dtype
; /* Destination type (printer/class) */
7993 cupsd_printer_t
*printer
; /* Printer data */
7996 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
7997 con
->number
, uri
->values
[0].string
.text
);
8000 * Is the destination valid?
8003 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8009 send_ipp_status(con
, IPP_NOT_FOUND
,
8010 _("The printer or class does not exist."));
8018 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8020 send_http_error(con
, status
, printer
);
8025 * Hold pending/new jobs sent to the printer...
8028 printer
->holding_new_jobs
= 1;
8030 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
8032 if (dtype
& CUPS_PRINTER_CLASS
)
8033 cupsdLogMessage(CUPSD_LOG_INFO
,
8034 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8035 printer
->name
, get_username(con
));
8037 cupsdLogMessage(CUPSD_LOG_INFO
,
8038 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8039 printer
->name
, get_username(con
));
8042 * Everything was ok, so return OK status...
8045 con
->response
->request
.status
.status_code
= IPP_OK
;
8050 * 'move_job()' - Move a job to a new destination.
8054 move_job(cupsd_client_t
*con
, /* I - Client connection */
8055 ipp_attribute_t
*uri
) /* I - Job URI */
8057 http_status_t status
; /* Policy status */
8058 ipp_attribute_t
*attr
; /* Current attribute */
8059 int jobid
; /* Job ID */
8060 cupsd_job_t
*job
; /* Current job */
8061 const char *src
; /* Source printer/class */
8062 cups_ptype_t stype
, /* Source type (printer or class) */
8063 dtype
; /* Destination type (printer/class) */
8064 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
8065 username
[HTTP_MAX_URI
], /* Username portion of URI */
8066 host
[HTTP_MAX_URI
], /* Host portion of URI */
8067 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8068 int port
; /* Port portion of URI */
8069 cupsd_printer_t
*sprinter
, /* Source printer */
8070 *dprinter
; /* Destination printer */
8073 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
8074 uri
->values
[0].string
.text
);
8077 * Get the new printer or class...
8080 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
8081 IPP_TAG_URI
)) == NULL
)
8084 * Need job-printer-uri...
8087 send_ipp_status(con
, IPP_BAD_REQUEST
,
8088 _("job-printer-uri attribute missing."));
8092 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
8098 send_ipp_status(con
, IPP_NOT_FOUND
,
8099 _("The printer or class does not exist."));
8104 * See if we have a job URI or a printer URI...
8107 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8108 sizeof(scheme
), username
, sizeof(username
), host
,
8109 sizeof(host
), &port
, resource
, sizeof(resource
));
8111 if (!strcmp(uri
->name
, "printer-uri"))
8114 * Got a printer URI; see if we also have a job-id attribute...
8117 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8118 IPP_TAG_INTEGER
)) == NULL
)
8124 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8125 &sprinter
)) == NULL
)
8131 send_ipp_status(con
, IPP_NOT_FOUND
,
8132 _("The printer or class does not exist."));
8141 * Otherwise, just move a single job...
8144 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8147 * Nope - return a "not found" error...
8150 send_ipp_status(con
, IPP_NOT_FOUND
,
8151 _("Job #%d does not exist."), attr
->values
[0].integer
);
8157 * Job found, initialize source pointers...
8168 * Got a job URI; parse it to get the job ID...
8171 if (strncmp(resource
, "/jobs/", 6))
8177 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8178 uri
->values
[0].string
.text
);
8183 * See if the job exists...
8186 jobid
= atoi(resource
+ 6);
8188 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8191 * Nope - return a "not found" error...
8194 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8200 * Job found, initialize source pointers...
8209 * Check the policy of the destination printer...
8212 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8213 job
? job
->username
: NULL
)) != HTTP_OK
)
8215 send_http_error(con
, status
, dprinter
);
8220 * Now move the job or jobs...
8226 * See if the job has been completed...
8229 if (job
->state_value
> IPP_JOB_STOPPED
)
8232 * Return a "not-possible" error...
8235 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8236 _("Job #%d is finished and cannot be altered."),
8242 * See if the job is owned by the requesting user...
8245 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8247 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8248 cupsdFindDest(job
->dest
));
8253 * Move the job to a different printer or class...
8256 cupsdMoveJob(job
, dprinter
);
8261 * Got the source printer, now look through the jobs...
8264 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8266 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8269 * See if the job is pointing at the source printer or has not been
8273 if (_cups_strcasecmp(job
->dest
, src
) ||
8274 job
->state_value
> IPP_JOB_STOPPED
)
8278 * See if the job can be moved by the requesting user...
8281 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8285 * Move the job to a different printer or class...
8288 cupsdMoveJob(job
, dprinter
);
8293 * Start jobs if possible...
8299 * Return with "everything is OK" status...
8302 con
->response
->request
.status
.status_code
= IPP_OK
;
8307 * 'ppd_parse_line()' - Parse a PPD default line.
8310 static int /* O - 0 on success, -1 on failure */
8311 ppd_parse_line(const char *line
, /* I - Line */
8312 char *option
, /* O - Option name */
8313 int olen
, /* I - Size of option name */
8314 char *choice
, /* O - Choice name */
8315 int clen
) /* I - Size of choice name */
8318 * Verify this is a default option line...
8321 if (strncmp(line
, "*Default", 8))
8325 * Read the option name...
8328 for (line
+= 8, olen
--;
8329 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8340 * Skip everything else up to the colon (:)...
8343 while (*line
&& *line
!= ':')
8352 * Now grab the option choice, skipping leading whitespace...
8355 while (isspace(*line
& 255))
8359 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8370 * Return with no errors...
8378 * 'print_job()' - Print a file to a printer or class.
8382 print_job(cupsd_client_t
*con
, /* I - Client connection */
8383 ipp_attribute_t
*uri
) /* I - Printer URI */
8385 ipp_attribute_t
*attr
; /* Current attribute */
8386 ipp_attribute_t
*doc_name
; /* document-name attribute */
8387 ipp_attribute_t
*format
; /* Document-format attribute */
8388 const char *default_format
; /* document-format-default value */
8389 cupsd_job_t
*job
; /* New job */
8390 char filename
[1024]; /* Job filename */
8391 mime_type_t
*filetype
; /* Type of file */
8392 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8393 type
[MIME_MAX_TYPE
], /* Subtype of file */
8394 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8395 /* Textual name of mime type */
8396 cupsd_printer_t
*printer
; /* Printer data */
8397 struct stat fileinfo
; /* File information */
8398 int kbytes
; /* Size of file */
8399 int compression
; /* Document compression */
8402 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8403 uri
->values
[0].string
.text
);
8406 * Validate print file attributes, for now just document-format and
8407 * compression (CUPS only supports "none" and "gzip")...
8410 compression
= CUPS_FILE_NONE
;
8412 if ((attr
= ippFindAttribute(con
->request
, "compression",
8413 IPP_TAG_KEYWORD
)) != NULL
)
8415 if (strcmp(attr
->values
[0].string
.text
, "none")
8417 && strcmp(attr
->values
[0].string
.text
, "gzip")
8418 #endif /* HAVE_LIBZ */
8421 send_ipp_status(con
, IPP_ATTRIBUTES
,
8422 _("Unsupported compression \"%s\"."),
8423 attr
->values
[0].string
.text
);
8424 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8425 "compression", NULL
, attr
->values
[0].string
.text
);
8430 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8431 compression
= CUPS_FILE_GZIP
;
8432 #endif /* HAVE_LIBZ */
8436 * Do we have a file to print?
8441 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8446 * Is the destination valid?
8449 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8455 send_ipp_status(con
, IPP_NOT_FOUND
,
8456 _("The printer or class does not exist."));
8461 * Is it a format we support?
8464 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8466 ippSetName(con
->request
, &doc_name
, "document-name-supplied");
8468 if ((format
= ippFindAttribute(con
->request
, "document-format",
8469 IPP_TAG_MIMETYPE
)) != NULL
)
8472 * Grab format from client...
8475 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8478 send_ipp_status(con
, IPP_BAD_REQUEST
,
8479 _("Bad document-format \"%s\"."),
8480 format
->values
[0].string
.text
);
8484 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
8486 else if ((default_format
= cupsGetOption("document-format",
8487 printer
->num_options
,
8488 printer
->options
)) != NULL
)
8491 * Use default document format...
8494 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8496 send_ipp_status(con
, IPP_BAD_REQUEST
,
8497 _("Bad document-format \"%s\"."),
8508 strlcpy(super
, "application", sizeof(super
));
8509 strlcpy(type
, "octet-stream", sizeof(type
));
8512 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8515 * Auto-type the file...
8518 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8521 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8522 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8526 filetype
= mimeType(MimeDatabase
, super
, type
);
8528 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8529 filetype
->super
, filetype
->type
);
8531 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
8532 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
8535 filetype
= mimeType(MimeDatabase
, super
, type
);
8539 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8542 * Replace the document-format attribute value with the auto-typed or
8546 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8550 ippSetString(con
->request
, &format
, 0, mimetype
);
8552 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8553 "document-format", NULL
, mimetype
);
8557 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8558 _("Unsupported document-format \"%s\"."),
8559 format
? format
->values
[0].string
.text
:
8560 "application/octet-stream");
8561 cupsdLogMessage(CUPSD_LOG_INFO
,
8562 "Hint: Do you have the raw file printing rules enabled?");
8565 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8566 "document-format", NULL
, format
->values
[0].string
.text
);
8572 * Read any embedded job ticket info from PS files...
8575 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8576 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8577 !_cups_strcasecmp(filetype
->type
, "pdf")))
8578 read_job_ticket(con
);
8581 * Create the job object...
8584 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8588 * Update quota data...
8591 if (stat(con
->filename
, &fileinfo
))
8594 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8596 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8598 job
->koctets
+= kbytes
;
8600 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8601 attr
->values
[0].integer
+= kbytes
;
8604 * Add the job file...
8607 if (add_file(con
, job
, filetype
, compression
))
8610 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
8611 if (rename(con
->filename
, filename
))
8613 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
8615 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
8619 cupsdClearString(&con
->filename
);
8622 * See if we need to add the ending sheet...
8625 if (cupsdTimeoutJob(job
))
8629 * Log and save the job...
8632 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8633 "File of type %s/%s queued by \"%s\".",
8634 filetype
->super
, filetype
->type
, job
->username
);
8635 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8636 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8637 job
->dest
, job
->username
);
8640 * Start the job if possible...
8648 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8650 * This function only gets called when printing a single PDF or PostScript
8651 * file using the Print-Job operation. It doesn't work for Create-Job +
8652 * Send-File, since the job attributes need to be set at job creation
8653 * time for banners to work. The embedded job ticket stuff is here
8654 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8655 * options and IPP attributes which otherwise would be lost.
8657 * The format of a job ticket is simple:
8659 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8661 * %cupsJobTicket: attr1=value1
8662 * %cupsJobTicket: attr2=value2
8664 * %cupsJobTicket: attrN=valueN
8666 * Job ticket lines must appear immediately after the first line that
8667 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8668 * stops looking for job ticket info when it finds a line that does not begin
8669 * with "%cupsJobTicket:".
8671 * The maximum length of a job ticket line, including the prefix, is
8672 * 255 characters to conform with the Adobe DSC.
8674 * Read-only attributes are rejected with a notice to the error log in
8675 * case a malicious user tries anything. Since the job ticket is read
8676 * prior to attribute validation in print_job(), job ticket attributes
8677 * will go through the same validation as IPP attributes...
8681 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8683 cups_file_t
*fp
; /* File to read from */
8684 char line
[256]; /* Line data */
8685 int num_options
; /* Number of options */
8686 cups_option_t
*options
; /* Options */
8687 ipp_t
*ticket
; /* New attributes */
8688 ipp_attribute_t
*attr
, /* Current attribute */
8689 *attr2
, /* Job attribute */
8690 *prev2
; /* Previous job attribute */
8694 * First open the print file...
8697 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8699 cupsdLogMessage(CUPSD_LOG_ERROR
,
8700 "Unable to open print file for job ticket - %s",
8706 * Skip the first line...
8709 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8711 cupsdLogMessage(CUPSD_LOG_ERROR
,
8712 "Unable to read from print file for job ticket - %s",
8718 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8721 * Not a DSC-compliant file, so no job ticket info will be available...
8729 * Read job ticket info from the file...
8735 while (cupsFileGets(fp
, line
, sizeof(line
)))
8738 * Stop at the first non-ticket line...
8741 if (strncmp(line
, "%cupsJobTicket:", 15))
8745 * Add the options to the option array...
8748 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8752 * Done with the file; see if we have any options...
8757 if (num_options
== 0)
8761 * OK, convert the options to an attribute list, and apply them to
8766 cupsEncodeOptions(ticket
, num_options
, options
);
8769 * See what the user wants to change.
8772 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8774 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8777 if (!strncmp(attr
->name
, "date-time-at-", 13) ||
8778 !strcmp(attr
->name
, "job-impressions-completed") ||
8779 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8780 !strncmp(attr
->name
, "job-k-octets", 12) ||
8781 !strcmp(attr
->name
, "job-id") ||
8782 !strcmp(attr
->name
, "job-originating-host-name") ||
8783 !strcmp(attr
->name
, "job-originating-user-name") ||
8784 !strcmp(attr
->name
, "job-pages-completed") ||
8785 !strcmp(attr
->name
, "job-printer-uri") ||
8786 !strncmp(attr
->name
, "job-state", 9) ||
8787 !strcmp(attr
->name
, "job-uri") ||
8788 !strncmp(attr
->name
, "time-at-", 8))
8789 continue; /* Read-only attrs */
8791 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8792 IPP_TAG_ZERO
)) != NULL
)
8795 * Some other value; first free the old value...
8798 if (con
->request
->attrs
== attr2
)
8800 con
->request
->attrs
= attr2
->next
;
8805 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8806 if (prev2
->next
== attr2
)
8808 prev2
->next
= attr2
->next
;
8813 if (con
->request
->last
== attr2
)
8814 con
->request
->last
= prev2
;
8816 ippDeleteAttribute(NULL
, attr2
);
8820 * Add new option by copying it...
8823 ippCopyAttribute(con
->request
, attr
, 0);
8827 * Then free the attribute list and option array...
8831 cupsFreeOptions(num_options
, options
);
8836 * 'reject_jobs()' - Reject print jobs to a printer.
8840 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8841 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8843 http_status_t status
; /* Policy status */
8844 cups_ptype_t dtype
; /* Destination type (printer/class) */
8845 cupsd_printer_t
*printer
; /* Printer data */
8846 ipp_attribute_t
*attr
; /* printer-state-message text */
8849 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8850 con
->number
, uri
->values
[0].string
.text
);
8853 * Is the destination valid?
8856 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8862 send_ipp_status(con
, IPP_NOT_FOUND
,
8863 _("The printer or class does not exist."));
8871 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8873 send_http_error(con
, status
, printer
);
8878 * Reject jobs sent to the printer...
8881 printer
->accepting
= 0;
8883 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8884 IPP_TAG_TEXT
)) == NULL
)
8885 strlcpy(printer
->state_message
, "Rejecting Jobs",
8886 sizeof(printer
->state_message
));
8888 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8889 sizeof(printer
->state_message
));
8891 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8892 "No longer accepting jobs.");
8894 if (dtype
& CUPS_PRINTER_CLASS
)
8896 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8898 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8899 printer
->name
, get_username(con
));
8903 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8905 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8906 printer
->name
, get_username(con
));
8910 * Everything was ok, so return OK status...
8913 con
->response
->request
.status
.status_code
= IPP_OK
;
8918 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8922 release_held_new_jobs(
8923 cupsd_client_t
*con
, /* I - Connection */
8924 ipp_attribute_t
*uri
) /* I - Printer URI */
8926 http_status_t status
; /* Policy status */
8927 cups_ptype_t dtype
; /* Destination type (printer/class) */
8928 cupsd_printer_t
*printer
; /* Printer data */
8931 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8932 con
->number
, uri
->values
[0].string
.text
);
8935 * Is the destination valid?
8938 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8944 send_ipp_status(con
, IPP_NOT_FOUND
,
8945 _("The printer or class does not exist."));
8953 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8955 send_http_error(con
, status
, printer
);
8960 * Hold pending/new jobs sent to the printer...
8963 printer
->holding_new_jobs
= 0;
8965 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8967 if (dtype
& CUPS_PRINTER_CLASS
)
8968 cupsdLogMessage(CUPSD_LOG_INFO
,
8969 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8970 printer
->name
, get_username(con
));
8972 cupsdLogMessage(CUPSD_LOG_INFO
,
8973 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8974 printer
->name
, get_username(con
));
8979 * Everything was ok, so return OK status...
8982 con
->response
->request
.status
.status_code
= IPP_OK
;
8987 * 'release_job()' - Release a held print job.
8991 release_job(cupsd_client_t
*con
, /* I - Client connection */
8992 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8994 ipp_attribute_t
*attr
; /* Current attribute */
8995 int jobid
; /* Job ID */
8996 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8997 username
[HTTP_MAX_URI
], /* Username portion of URI */
8998 host
[HTTP_MAX_URI
], /* Host portion of URI */
8999 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9000 int port
; /* Port portion of URI */
9001 cupsd_job_t
*job
; /* Job information */
9004 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
9005 con
->number
, uri
->values
[0].string
.text
);
9008 * See if we have a job URI or a printer URI...
9011 if (!strcmp(uri
->name
, "printer-uri"))
9014 * Got a printer URI; see if we also have a job-id attribute...
9017 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9018 IPP_TAG_INTEGER
)) == NULL
)
9020 send_ipp_status(con
, IPP_BAD_REQUEST
,
9021 _("Got a printer-uri attribute but no job-id."));
9025 jobid
= attr
->values
[0].integer
;
9030 * Got a job URI; parse it to get the job ID...
9033 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9034 sizeof(scheme
), username
, sizeof(username
), host
,
9035 sizeof(host
), &port
, resource
, sizeof(resource
));
9037 if (strncmp(resource
, "/jobs/", 6))
9043 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9044 uri
->values
[0].string
.text
);
9048 jobid
= atoi(resource
+ 6);
9052 * See if the job exists...
9055 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9058 * Nope - return a "not found" error...
9061 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9066 * See if job is "held"...
9069 if (job
->state_value
!= IPP_JOB_HELD
)
9072 * Nope - return a "not possible" error...
9075 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
9080 * See if the job is owned by the requesting user...
9083 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9085 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9086 cupsdFindDest(job
->dest
));
9091 * Reset the job-hold-until value to "no-hold"...
9094 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9095 IPP_TAG_KEYWORD
)) == NULL
)
9096 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9100 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
9101 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
9103 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
9104 "Job job-hold-until value changed by user.");
9105 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9109 * Release the job and return...
9112 cupsdReleaseJob(job
);
9114 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
9115 "Job released by user.");
9117 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
9119 con
->response
->request
.status
.status_code
= IPP_OK
;
9126 * 'renew_subscription()' - Renew an existing subscription...
9131 cupsd_client_t
*con
, /* I - Client connection */
9132 int sub_id
) /* I - Subscription ID */
9134 http_status_t status
; /* Policy status */
9135 cupsd_subscription_t
*sub
; /* Subscription */
9136 ipp_attribute_t
*lease
; /* notify-lease-duration */
9139 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9140 "renew_subscription(con=%p[%d], sub_id=%d)",
9141 con
, con
->number
, sub_id
);
9144 * Is the subscription ID valid?
9147 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
9150 * Bad subscription ID...
9153 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
9161 * Job subscriptions cannot be renewed...
9164 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9165 _("Job subscriptions cannot be renewed."));
9173 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
9175 con
, sub
->owner
)) != HTTP_OK
)
9177 send_http_error(con
, status
, sub
->dest
);
9182 * Renew the subscription...
9185 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
9188 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
9190 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9192 cupsdLogMessage(CUPSD_LOG_INFO
,
9193 "renew_subscription: Limiting notify-lease-duration to "
9196 sub
->lease
= MaxLeaseDuration
;
9199 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9201 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9203 con
->response
->request
.status
.status_code
= IPP_OK
;
9205 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9206 "notify-lease-duration", sub
->lease
);
9211 * 'restart_job()' - Restart an old print job.
9215 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9216 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9218 ipp_attribute_t
*attr
; /* Current attribute */
9219 int jobid
; /* Job ID */
9220 cupsd_job_t
*job
; /* Job information */
9221 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9222 username
[HTTP_MAX_URI
], /* Username portion of URI */
9223 host
[HTTP_MAX_URI
], /* Host portion of URI */
9224 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9225 int port
; /* Port portion of URI */
9228 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9229 con
->number
, uri
->values
[0].string
.text
);
9232 * See if we have a job URI or a printer URI...
9235 if (!strcmp(uri
->name
, "printer-uri"))
9238 * Got a printer URI; see if we also have a job-id attribute...
9241 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9242 IPP_TAG_INTEGER
)) == NULL
)
9244 send_ipp_status(con
, IPP_BAD_REQUEST
,
9245 _("Got a printer-uri attribute but no job-id."));
9249 jobid
= attr
->values
[0].integer
;
9254 * Got a job URI; parse it to get the job ID...
9257 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9258 sizeof(scheme
), username
, sizeof(username
), host
,
9259 sizeof(host
), &port
, resource
, sizeof(resource
));
9261 if (strncmp(resource
, "/jobs/", 6))
9267 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9268 uri
->values
[0].string
.text
);
9272 jobid
= atoi(resource
+ 6);
9276 * See if the job exists...
9279 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9282 * Nope - return a "not found" error...
9285 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9290 * See if job is in any of the "completed" states...
9293 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9296 * Nope - return a "not possible" error...
9299 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
9305 * See if we have retained the job files...
9310 if (!job
->attrs
|| job
->num_files
== 0)
9313 * Nope - return a "not possible" error...
9316 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9317 _("Job #%d cannot be restarted - no files."), jobid
);
9322 * See if the job is owned by the requesting user...
9325 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9327 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9328 cupsdFindDest(job
->dest
));
9333 * See if the job-hold-until attribute is specified...
9336 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
9337 IPP_TAG_KEYWORD
)) == NULL
)
9338 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
9340 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
9343 * Return the job to a held state...
9346 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
9347 "Restarted by \"%s\" with job-hold-until=%s.",
9348 username
, attr
->values
[0].string
.text
);
9349 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
9351 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
9352 NULL
, job
, "Job restarted by user with job-hold-until=%s",
9353 attr
->values
[0].string
.text
);
9358 * Restart the job...
9361 cupsdRestartJob(job
);
9365 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9367 con
->response
->request
.status
.status_code
= IPP_OK
;
9372 * 'save_auth_info()' - Save authentication information for a job.
9377 cupsd_client_t
*con
, /* I - Client connection */
9378 cupsd_job_t
*job
, /* I - Job */
9379 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9381 int i
; /* Looping var */
9382 char filename
[1024]; /* Job authentication filename */
9383 cups_file_t
*fp
; /* Job authentication file */
9384 char line
[65536]; /* Line for file */
9385 cupsd_printer_t
*dest
; /* Destination printer/class */
9389 * This function saves the in-memory authentication information for
9390 * a job so that it can be used to authenticate with a remote host.
9391 * The information is stored in a file that is readable only by the
9392 * root user. The fields are Base-64 encoded, each on a separate line,
9393 * followed by random number (up to 1024) of newlines to limit the
9394 * amount of information that is exposed.
9396 * Because of the potential for exposing of authentication information,
9397 * this functionality is only enabled when running cupsd as root.
9399 * This caching only works for the Basic and BasicDigest authentication
9400 * types. Digest authentication cannot be cached this way, and in
9401 * the future Kerberos authentication may make all of this obsolete.
9403 * Authentication information is saved whenever an authenticated
9404 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9407 * This information is deleted after a job is completed or canceled,
9408 * so reprints may require subsequent re-authentication.
9414 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9418 * Create the authentication file and change permissions...
9421 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9422 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9424 cupsdLogMessage(CUPSD_LOG_ERROR
,
9425 "Unable to save authentication info to \"%s\" - %s",
9426 filename
, strerror(errno
));
9430 fchown(cupsFileNumber(fp
), 0, 0);
9431 fchmod(cupsFileNumber(fp
), 0400);
9433 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9436 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9438 cupsdClearString(job
->auth_env
+ i
);
9440 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9443 * Write 1 to 3 auth values...
9447 i
< auth_info
->num_values
&&
9448 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9451 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9453 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9454 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9457 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9458 auth_info
->values
[i
].string
.text
);
9460 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9461 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9462 auth_info
->values
[i
].string
.text
);
9463 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9464 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9465 auth_info
->values
[i
].string
.text
);
9466 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9467 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9468 auth_info
->values
[i
].string
.text
);
9469 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9470 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9471 auth_info
->values
[i
].string
.text
);
9476 else if (auth_info
&& auth_info
->num_values
== 2 &&
9477 dest
->num_auth_info_required
== 1 &&
9478 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9481 * Allow fallback to username+password for Kerberized queues...
9484 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9485 cupsFilePutConf(fp
, "username", line
);
9487 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9488 auth_info
->values
[0].string
.text
);
9490 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9491 cupsFilePutConf(fp
, "password", line
);
9493 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9494 auth_info
->values
[1].string
.text
);
9496 else if (con
->username
[0])
9499 * Write the authenticated username...
9502 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9503 cupsFilePutConf(fp
, "username", line
);
9505 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9508 * Write the authenticated password...
9511 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9512 cupsFilePutConf(fp
, "password", line
);
9514 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9518 if (con
->gss_uid
> 0)
9520 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9521 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9523 #endif /* HAVE_GSSAPI */
9526 * Write a random number of newlines to the end of the file...
9529 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9530 cupsFilePutChar(fp
, '\n');
9533 * Close the file and return...
9541 * 'send_document()' - Send a file to a printer or class.
9545 send_document(cupsd_client_t
*con
, /* I - Client connection */
9546 ipp_attribute_t
*uri
) /* I - Printer URI */
9548 ipp_attribute_t
*attr
; /* Current attribute */
9549 ipp_attribute_t
*format
; /* Request's document-format attribute */
9550 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9551 const char *default_format
;/* document-format-default value */
9552 int jobid
; /* Job ID number */
9553 cupsd_job_t
*job
; /* Current job */
9554 char job_uri
[HTTP_MAX_URI
],
9556 scheme
[HTTP_MAX_URI
],
9557 /* Method portion of URI */
9558 username
[HTTP_MAX_URI
],
9559 /* Username portion of URI */
9561 /* Host portion of URI */
9562 resource
[HTTP_MAX_URI
];
9563 /* Resource portion of URI */
9564 int port
; /* Port portion of URI */
9565 mime_type_t
*filetype
; /* Type of file */
9566 char super
[MIME_MAX_SUPER
],
9567 /* Supertype of file */
9568 type
[MIME_MAX_TYPE
],
9569 /* Subtype of file */
9570 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9571 /* Textual name of mime type */
9572 char filename
[1024]; /* Job filename */
9573 cupsd_printer_t
*printer
; /* Current printer */
9574 struct stat fileinfo
; /* File information */
9575 int kbytes
; /* Size of file */
9576 int compression
; /* Type of compression */
9577 int start_job
; /* Start the job? */
9580 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9581 con
->number
, uri
->values
[0].string
.text
);
9584 * See if we have a job URI or a printer URI...
9587 if (!strcmp(uri
->name
, "printer-uri"))
9590 * Got a printer URI; see if we also have a job-id attribute...
9593 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9594 IPP_TAG_INTEGER
)) == NULL
)
9596 send_ipp_status(con
, IPP_BAD_REQUEST
,
9597 _("Got a printer-uri attribute but no job-id."));
9601 jobid
= attr
->values
[0].integer
;
9606 * Got a job URI; parse it to get the job ID...
9609 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9610 sizeof(scheme
), username
, sizeof(username
), host
,
9611 sizeof(host
), &port
, resource
, sizeof(resource
));
9613 if (strncmp(resource
, "/jobs/", 6))
9619 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9620 uri
->values
[0].string
.text
);
9624 jobid
= atoi(resource
+ 6);
9628 * See if the job exists...
9631 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9634 * Nope - return a "not found" error...
9637 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9641 printer
= cupsdFindDest(job
->dest
);
9644 * See if the job is owned by the requesting user...
9647 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9649 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9650 cupsdFindDest(job
->dest
));
9655 * OK, see if the client is sending the document compressed - CUPS
9656 * only supports "none" and "gzip".
9659 compression
= CUPS_FILE_NONE
;
9661 if ((attr
= ippFindAttribute(con
->request
, "compression",
9662 IPP_TAG_KEYWORD
)) != NULL
)
9664 if (strcmp(attr
->values
[0].string
.text
, "none")
9666 && strcmp(attr
->values
[0].string
.text
, "gzip")
9667 #endif /* HAVE_LIBZ */
9670 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9671 attr
->values
[0].string
.text
);
9672 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9673 "compression", NULL
, attr
->values
[0].string
.text
);
9678 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9679 compression
= CUPS_FILE_GZIP
;
9680 #endif /* HAVE_LIBZ */
9684 * Do we have a file to print?
9687 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9688 IPP_TAG_BOOLEAN
)) == NULL
)
9690 send_ipp_status(con
, IPP_BAD_REQUEST
,
9691 _("Missing last-document attribute in request."));
9698 * Check for an empty request with "last-document" set to true, which is
9699 * used to close an "open" job by RFC 2911, section 3.3.2.
9702 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9705 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9710 * Is it a format we support?
9715 if ((format
= ippFindAttribute(con
->request
, "document-format",
9716 IPP_TAG_MIMETYPE
)) != NULL
)
9719 * Grab format from client...
9722 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9725 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9726 format
->values
[0].string
.text
);
9730 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
9732 else if ((default_format
= cupsGetOption("document-format",
9733 printer
->num_options
,
9734 printer
->options
)) != NULL
)
9737 * Use default document format...
9740 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9742 send_ipp_status(con
, IPP_BAD_REQUEST
,
9743 _("Bad document-format-default \"%s\"."), default_format
);
9750 * No document format attribute? Auto-type it!
9753 strlcpy(super
, "application", sizeof(super
));
9754 strlcpy(type
, "octet-stream", sizeof(type
));
9757 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9760 * Auto-type the file...
9763 ipp_attribute_t
*doc_name
; /* document-name attribute */
9766 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9768 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9769 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9770 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9774 filetype
= mimeType(MimeDatabase
, super
, type
);
9777 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9778 filetype
->super
, filetype
->type
);
9780 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
9781 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
9784 filetype
= mimeType(MimeDatabase
, super
, type
);
9789 * Replace the document-format attribute value with the auto-typed or
9793 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9796 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9797 IPP_TAG_MIMETYPE
)) != NULL
)
9798 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9800 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9801 "document-format", NULL
, mimetype
);
9805 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9806 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9807 cupsdLogMessage(CUPSD_LOG_INFO
,
9808 "Hint: Do you have the raw file printing rules enabled?");
9811 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9812 "document-format", NULL
, format
->values
[0].string
.text
);
9817 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9819 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9822 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9823 _("Unsupported document-format \"%s\"."), mimetype
);
9825 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9826 "document-format", NULL
, mimetype
);
9832 * Add the file to the job...
9835 if (add_file(con
, job
, filetype
, compression
))
9838 if ((attr
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
)) != NULL
)
9839 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name-supplied", NULL
, ippGetString(attr
, 0, NULL
));
9841 if (stat(con
->filename
, &fileinfo
))
9844 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9846 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9848 job
->koctets
+= kbytes
;
9850 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9851 attr
->values
[0].integer
+= kbytes
;
9853 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
9854 if (rename(con
->filename
, filename
))
9856 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
9858 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
9862 cupsdClearString(&con
->filename
);
9864 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9865 filetype
->super
, filetype
->type
, job
->username
);
9868 * Start the job if this is the last document...
9873 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9874 IPP_TAG_BOOLEAN
)) != NULL
&&
9875 attr
->values
[0].boolean
)
9878 * See if we need to add the ending sheet...
9881 if (cupsdTimeoutJob(job
))
9884 if (job
->state_value
== IPP_JOB_STOPPED
)
9886 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9887 job
->state_value
= IPP_JOB_PENDING
;
9889 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9891 else if (job
->state_value
== IPP_JOB_HELD
)
9893 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9894 IPP_TAG_KEYWORD
)) == NULL
)
9895 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9897 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9899 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9900 job
->state_value
= IPP_JOB_PENDING
;
9902 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9905 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9909 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9915 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9916 IPP_TAG_KEYWORD
)) == NULL
)
9917 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9919 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9921 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9922 job
->state_value
= IPP_JOB_HELD
;
9923 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9925 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9928 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9935 * Fill in the response info...
9938 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9939 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9940 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9943 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9945 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9947 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
9948 NULL
, job
->reasons
->values
[0].string
.text
);
9950 con
->response
->request
.status
.status_code
= IPP_OK
;
9953 * Start the job if necessary...
9962 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9967 cupsd_client_t
*con
, /* I - Client connection */
9968 http_status_t status
, /* I - HTTP status code */
9969 cupsd_printer_t
*printer
) /* I - Printer, if any */
9971 ipp_attribute_t
*uri
; /* Request URI, if any */
9974 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9975 IPP_TAG_URI
)) == NULL
)
9976 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9978 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9979 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9980 con
->number
, httpStatus(status
),
9982 ippOpString(con
->request
->request
.op
.operation_id
) :
9984 uri
? uri
->values
[0].string
.text
: "no URI",
9985 con
->http
->hostname
);
9989 int auth_type
; /* Type of authentication required */
9992 auth_type
= CUPSD_AUTH_NONE
;
9994 if (status
== HTTP_UNAUTHORIZED
&&
9995 printer
->num_auth_info_required
> 0 &&
9996 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9998 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9999 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
10000 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
10003 * Creating and authenticating jobs requires Kerberos...
10006 auth_type
= CUPSD_AUTH_NEGOTIATE
;
10011 * Use policy/location-defined authentication requirements...
10014 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
10015 cupsd_location_t
*auth
; /* Pointer to authentication element */
10018 if (printer
->type
& CUPS_PRINTER_CLASS
)
10019 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
10021 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
10023 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
10024 auth
->type
== CUPSD_AUTH_NONE
)
10025 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
10027 con
->request
->request
.op
.operation_id
:
10032 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
10033 auth_type
= cupsdDefaultAuthType();
10035 auth_type
= auth
->type
;
10039 cupsdSendError(con
, status
, auth_type
);
10042 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
10044 ippDelete(con
->response
);
10045 con
->response
= NULL
;
10052 * 'send_ipp_status()' - Send a status back to the IPP client.
10056 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
10057 ipp_status_t status
, /* I - IPP status code */
10058 const char *message
,/* I - Status message */
10059 ...) /* I - Additional args as needed */
10061 va_list ap
; /* Pointer to additional args */
10062 char formatted
[1024]; /* Formatted errror message */
10065 va_start(ap
, message
);
10066 vsnprintf(formatted
, sizeof(formatted
),
10067 _cupsLangString(con
->language
, message
), ap
);
10070 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
10071 ippOpString(con
->request
->request
.op
.operation_id
),
10072 ippErrorString(status
), formatted
);
10074 con
->response
->request
.status
.status_code
= status
;
10076 if (ippFindAttribute(con
->response
, "attributes-charset",
10077 IPP_TAG_ZERO
) == NULL
)
10078 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
10079 "attributes-charset", NULL
, "utf-8");
10081 if (ippFindAttribute(con
->response
, "attributes-natural-language",
10082 IPP_TAG_ZERO
) == NULL
)
10083 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
10084 "attributes-natural-language", NULL
, DefaultLanguage
);
10086 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
10087 "status-message", NULL
, formatted
);
10092 * 'set_default()' - Set the default destination...
10096 set_default(cupsd_client_t
*con
, /* I - Client connection */
10097 ipp_attribute_t
*uri
) /* I - Printer URI */
10099 http_status_t status
; /* Policy status */
10100 cups_ptype_t dtype
; /* Destination type (printer/class) */
10101 cupsd_printer_t
*printer
, /* Printer */
10102 *oldprinter
; /* Old default printer */
10105 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
10106 con
->number
, uri
->values
[0].string
.text
);
10109 * Is the destination valid?
10112 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10118 send_ipp_status(con
, IPP_NOT_FOUND
,
10119 _("The printer or class does not exist."));
10127 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
10129 send_http_error(con
, status
, NULL
);
10134 * Set it as the default...
10137 oldprinter
= DefaultPrinter
;
10138 DefaultPrinter
= printer
;
10141 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
10142 "%s is no longer the default printer.", oldprinter
->name
);
10144 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
10145 "%s is now the default printer.", printer
->name
);
10147 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
10148 CUPSD_DIRTY_PRINTCAP
);
10150 cupsdLogMessage(CUPSD_LOG_INFO
,
10151 "Default destination set to \"%s\" by \"%s\".",
10152 printer
->name
, get_username(con
));
10155 * Everything was ok, so return OK status...
10158 con
->response
->request
.status
.status_code
= IPP_OK
;
10163 * 'set_job_attrs()' - Set job attributes.
10167 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10168 ipp_attribute_t
*uri
) /* I - Job URI */
10170 ipp_attribute_t
*attr
, /* Current attribute */
10171 *attr2
; /* Job attribute */
10172 int jobid
; /* Job ID */
10173 cupsd_job_t
*job
; /* Current job */
10174 char scheme
[HTTP_MAX_URI
],
10175 /* Method portion of URI */
10176 username
[HTTP_MAX_URI
],
10177 /* Username portion of URI */
10178 host
[HTTP_MAX_URI
],
10179 /* Host portion of URI */
10180 resource
[HTTP_MAX_URI
];
10181 /* Resource portion of URI */
10182 int port
; /* Port portion of URI */
10183 int event
; /* Events? */
10184 int check_jobs
; /* Check jobs? */
10187 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10188 con
->number
, uri
->values
[0].string
.text
);
10191 * Start with "everything is OK" status...
10194 con
->response
->request
.status
.status_code
= IPP_OK
;
10197 * See if we have a job URI or a printer URI...
10200 if (!strcmp(uri
->name
, "printer-uri"))
10203 * Got a printer URI; see if we also have a job-id attribute...
10206 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10207 IPP_TAG_INTEGER
)) == NULL
)
10209 send_ipp_status(con
, IPP_BAD_REQUEST
,
10210 _("Got a printer-uri attribute but no job-id."));
10214 jobid
= attr
->values
[0].integer
;
10219 * Got a job URI; parse it to get the job ID...
10222 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10223 sizeof(scheme
), username
, sizeof(username
), host
,
10224 sizeof(host
), &port
, resource
, sizeof(resource
));
10226 if (strncmp(resource
, "/jobs/", 6))
10232 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
10233 uri
->values
[0].string
.text
);
10237 jobid
= atoi(resource
+ 6);
10241 * See if the job exists...
10244 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10247 * Nope - return a "not found" error...
10250 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
10255 * See if the job has been completed...
10258 if (job
->state_value
> IPP_JOB_STOPPED
)
10261 * Return a "not-possible" error...
10264 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10265 _("Job #%d is finished and cannot be altered."), jobid
);
10270 * See if the job is owned by the requesting user...
10273 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10275 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
10276 cupsdFindDest(job
->dest
));
10281 * See what the user wants to change.
10289 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10291 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10294 if (!strcmp(attr
->name
, "attributes-charset") ||
10295 !strcmp(attr
->name
, "attributes-natural-language") ||
10296 !strncmp(attr
->name
, "date-time-at-", 13) ||
10297 !strncmp(attr
->name
, "document-compression", 20) ||
10298 !strncmp(attr
->name
, "document-format", 15) ||
10299 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10300 !strcmp(attr
->name
, "job-document-access-errors") ||
10301 !strcmp(attr
->name
, "job-id") ||
10302 !strcmp(attr
->name
, "job-impressions-completed") ||
10303 !strcmp(attr
->name
, "job-k-octets-completed") ||
10304 !strcmp(attr
->name
, "job-media-sheets-completed") ||
10305 !strcmp(attr
->name
, "job-originating-host-name") ||
10306 !strcmp(attr
->name
, "job-originating-user-name") ||
10307 !strcmp(attr
->name
, "job-pages-completed") ||
10308 !strcmp(attr
->name
, "job-printer-up-time") ||
10309 !strcmp(attr
->name
, "job-printer-uri") ||
10310 !strcmp(attr
->name
, "job-sheets") ||
10311 !strcmp(attr
->name
, "job-state-message") ||
10312 !strcmp(attr
->name
, "job-state-reasons") ||
10313 !strcmp(attr
->name
, "job-uri") ||
10314 !strcmp(attr
->name
, "number-of-documents") ||
10315 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10316 !strcmp(attr
->name
, "output-device-assigned") ||
10317 !strncmp(attr
->name
, "time-at-", 8))
10323 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10324 _("%s cannot be changed."), attr
->name
);
10326 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10327 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10331 if (!strcmp(attr
->name
, "job-priority"))
10334 * Change the job priority...
10337 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10339 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
10341 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10342 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10344 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10346 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10347 _("Job is completed and cannot be changed."));
10350 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10352 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10353 attr
->values
[0].integer
);
10354 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10357 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10358 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10361 else if (!strcmp(attr
->name
, "job-state"))
10364 * Change the job state...
10367 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10369 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
10371 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10372 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10376 switch (attr
->values
[0].integer
)
10378 case IPP_JOB_PENDING
:
10379 case IPP_JOB_HELD
:
10380 if (job
->state_value
> IPP_JOB_HELD
)
10382 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10383 _("Job state cannot be changed."));
10386 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10388 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10389 attr
->values
[0].integer
);
10390 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
10395 case IPP_JOB_PROCESSING
:
10396 case IPP_JOB_STOPPED
:
10397 if (job
->state_value
!= attr
->values
[0].integer
)
10399 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10400 _("Job state cannot be changed."));
10405 case IPP_JOB_CANCELED
:
10406 case IPP_JOB_ABORTED
:
10407 case IPP_JOB_COMPLETED
:
10408 if (job
->state_value
> IPP_JOB_PROCESSING
)
10410 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10411 _("Job state cannot be changed."));
10414 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10416 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10417 attr
->values
[0].integer
);
10418 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10420 "Job state changed by \"%s\"", username
);
10427 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10429 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10430 IPP_TAG_ZERO
)) != NULL
)
10433 * Some other value; first free the old value...
10436 if (job
->attrs
->prev
)
10437 job
->attrs
->prev
->next
= attr2
->next
;
10439 job
->attrs
->attrs
= attr2
->next
;
10441 if (job
->attrs
->last
== attr2
)
10442 job
->attrs
->last
= job
->attrs
->prev
;
10444 ippDeleteAttribute(NULL
, attr2
);
10447 * Then copy the attribute...
10450 ippCopyAttribute(job
->attrs
, attr
, 0);
10453 * See if the job-name or job-hold-until is being changed.
10456 if (!strcmp(attr
->name
, "job-hold-until"))
10458 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10459 attr
->values
[0].string
.text
);
10460 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10462 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10464 cupsdReleaseJob(job
);
10468 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10469 "Job held by \"%s\".", username
);
10471 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10474 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10477 * Delete the attribute...
10480 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10481 IPP_TAG_ZERO
)) != NULL
)
10483 if (job
->attrs
->prev
)
10484 job
->attrs
->prev
->next
= attr2
->next
;
10486 job
->attrs
->attrs
= attr2
->next
;
10488 if (attr2
== job
->attrs
->last
)
10489 job
->attrs
->last
= job
->attrs
->prev
;
10491 ippDeleteAttribute(NULL
, attr2
);
10493 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10499 * Add new option by copying it...
10502 ippCopyAttribute(job
->attrs
, attr
, 0);
10504 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10513 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10516 * Send events as needed...
10519 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10520 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10521 cupsdFindDest(job
->dest
), job
,
10522 "Job priority changed by user.");
10524 if (event
& CUPSD_EVENT_JOB_STATE
)
10525 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10526 job
->state_value
== IPP_JOB_HELD
?
10527 "Job held by user." : "Job restarted by user.");
10529 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10530 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10531 "Job options changed by user.");
10534 * Start jobs if possible...
10543 * 'set_printer_attrs()' - Set printer attributes.
10547 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10548 ipp_attribute_t
*uri
) /* I - Printer */
10550 http_status_t status
; /* Policy status */
10551 cups_ptype_t dtype
; /* Destination type (printer/class) */
10552 cupsd_printer_t
*printer
; /* Printer/class */
10553 ipp_attribute_t
*attr
; /* Printer attribute */
10554 int changed
= 0; /* Was anything changed? */
10557 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10558 con
->number
, uri
->values
[0].string
.text
);
10561 * Is the destination valid?
10564 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10570 send_ipp_status(con
, IPP_NOT_FOUND
,
10571 _("The printer or class does not exist."));
10579 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10581 send_http_error(con
, status
, printer
);
10586 * Return a list of attributes that can be set via Set-Printer-Attributes.
10589 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10590 IPP_TAG_TEXT
)) != NULL
)
10592 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10596 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
10598 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
10602 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
10604 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
10608 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
10610 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
10614 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10615 IPP_TAG_TEXT
)) != NULL
)
10617 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10622 * Update the printer attributes and return...
10627 printer
->config_time
= time(NULL
);
10629 cupsdSetPrinterAttrs(printer
);
10630 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10632 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10633 "Printer \"%s\" description or location changed by \"%s\".",
10634 printer
->name
, get_username(con
));
10636 cupsdLogMessage(CUPSD_LOG_INFO
,
10637 "Printer \"%s\" description or location changed by \"%s\".",
10638 printer
->name
, get_username(con
));
10641 con
->response
->request
.status
.status_code
= IPP_OK
;
10646 * 'set_printer_defaults()' - Set printer default options from a request.
10649 static int /* O - 1 on success, 0 on failure */
10650 set_printer_defaults(
10651 cupsd_client_t
*con
, /* I - Client connection */
10652 cupsd_printer_t
*printer
) /* I - Printer */
10654 int i
; /* Looping var */
10655 ipp_attribute_t
*attr
; /* Current attribute */
10656 size_t namelen
; /* Length of attribute name */
10657 char name
[256], /* New attribute name */
10658 value
[256]; /* String version of integer attrs */
10661 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10664 * Skip non-printer attributes...
10667 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10670 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10672 if (!strcmp(attr
->name
, "job-sheets-default"))
10675 * Only allow keywords and names...
10678 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10682 * Only allow job-sheets-default to be set when running without a
10683 * system high classification level...
10686 if (Classification
)
10689 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10691 if (attr
->num_values
> 1)
10692 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10694 cupsdSetString(&printer
->job_sheets
[1], "none");
10696 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10698 cupsdFreeStrings(&(printer
->users
));
10700 printer
->deny_users
= 0;
10702 if (attr
->value_tag
== IPP_TAG_NAME
&&
10703 (attr
->num_values
> 1 ||
10704 strcmp(attr
->values
[0].string
.text
, "all")))
10706 for (i
= 0; i
< attr
->num_values
; i
++)
10707 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10710 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10712 cupsdFreeStrings(&(printer
->users
));
10714 printer
->deny_users
= 1;
10716 if (attr
->value_tag
== IPP_TAG_NAME
&&
10717 (attr
->num_values
> 1 ||
10718 strcmp(attr
->values
[0].string
.text
, "none")))
10720 for (i
= 0; i
< attr
->num_values
; i
++)
10721 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10724 else if (!strcmp(attr
->name
, "job-quota-period"))
10726 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10729 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10730 attr
->values
[0].integer
);
10731 cupsdFreeQuotas(printer
);
10733 printer
->quota_period
= attr
->values
[0].integer
;
10735 else if (!strcmp(attr
->name
, "job-k-limit"))
10737 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10740 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10741 attr
->values
[0].integer
);
10742 cupsdFreeQuotas(printer
);
10744 printer
->k_limit
= attr
->values
[0].integer
;
10746 else if (!strcmp(attr
->name
, "job-page-limit"))
10748 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10751 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10752 attr
->values
[0].integer
);
10753 cupsdFreeQuotas(printer
);
10755 printer
->page_limit
= attr
->values
[0].integer
;
10757 else if (!strcmp(attr
->name
, "printer-op-policy"))
10759 cupsd_policy_t
*p
; /* Policy */
10762 if (attr
->value_tag
!= IPP_TAG_NAME
)
10765 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10767 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10768 "Setting printer-op-policy to \"%s\"...",
10769 attr
->values
[0].string
.text
);
10770 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10771 printer
->op_policy_ptr
= p
;
10775 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10776 _("Unknown printer-op-policy \"%s\"."),
10777 attr
->values
[0].string
.text
);
10781 else if (!strcmp(attr
->name
, "printer-error-policy"))
10783 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10786 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10787 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10788 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10789 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10790 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10792 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10793 _("Unknown printer-error-policy \"%s\"."),
10794 attr
->values
[0].string
.text
);
10798 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10799 "Setting printer-error-policy to \"%s\"...",
10800 attr
->values
[0].string
.text
);
10801 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10805 * Skip any other non-default attributes...
10808 namelen
= strlen(attr
->name
);
10809 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10810 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10814 * OK, anything else must be a user-defined default...
10817 strlcpy(name
, attr
->name
, sizeof(name
));
10818 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10820 switch (attr
->value_tag
)
10822 case IPP_TAG_DELETEATTR
:
10823 printer
->num_options
= cupsRemoveOption(name
,
10824 printer
->num_options
,
10825 &(printer
->options
));
10826 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10827 "Deleting %s", attr
->name
);
10830 case IPP_TAG_NAME
:
10831 case IPP_TAG_TEXT
:
10832 case IPP_TAG_KEYWORD
:
10834 printer
->num_options
= cupsAddOption(name
,
10835 attr
->values
[0].string
.text
,
10836 printer
->num_options
,
10837 &(printer
->options
));
10838 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10839 "Setting %s to \"%s\"...", attr
->name
,
10840 attr
->values
[0].string
.text
);
10843 case IPP_TAG_BOOLEAN
:
10844 printer
->num_options
= cupsAddOption(name
,
10845 attr
->values
[0].boolean
?
10847 printer
->num_options
,
10848 &(printer
->options
));
10849 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10850 "Setting %s to %s...", attr
->name
,
10851 attr
->values
[0].boolean
? "true" : "false");
10854 case IPP_TAG_INTEGER
:
10855 case IPP_TAG_ENUM
:
10856 sprintf(value
, "%d", attr
->values
[0].integer
);
10857 printer
->num_options
= cupsAddOption(name
, value
,
10858 printer
->num_options
,
10859 &(printer
->options
));
10860 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10861 "Setting %s to %s...", attr
->name
, value
);
10864 case IPP_TAG_RANGE
:
10865 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10866 attr
->values
[0].range
.upper
);
10867 printer
->num_options
= cupsAddOption(name
, value
,
10868 printer
->num_options
,
10869 &(printer
->options
));
10870 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10871 "Setting %s to %s...", attr
->name
, value
);
10874 case IPP_TAG_RESOLUTION
:
10875 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10876 attr
->values
[0].resolution
.yres
,
10877 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
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
);
10887 /* Do nothing for other values */
10897 * 'start_printer()' - Start a printer.
10901 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10902 ipp_attribute_t
*uri
) /* I - Printer URI */
10904 int i
; /* Temporary variable */
10905 http_status_t status
; /* Policy status */
10906 cups_ptype_t dtype
; /* Destination type (printer/class) */
10907 cupsd_printer_t
*printer
; /* Printer data */
10910 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10911 con
->number
, uri
->values
[0].string
.text
);
10914 * Is the destination valid?
10917 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10923 send_ipp_status(con
, IPP_NOT_FOUND
,
10924 _("The printer or class does not exist."));
10932 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10934 send_http_error(con
, status
, printer
);
10939 * Start the printer...
10942 printer
->state_message
[0] = '\0';
10944 cupsdStartPrinter(printer
, 1);
10946 if (dtype
& CUPS_PRINTER_CLASS
)
10947 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10948 printer
->name
, get_username(con
));
10950 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10951 printer
->name
, get_username(con
));
10959 if ((i
= check_quotas(con
, printer
)) < 0)
10961 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
10966 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
10971 * Everything was ok, so return OK status...
10974 con
->response
->request
.status
.status_code
= IPP_OK
;
10979 * 'stop_printer()' - Stop a printer.
10983 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10984 ipp_attribute_t
*uri
) /* I - Printer URI */
10986 http_status_t status
; /* Policy status */
10987 cups_ptype_t dtype
; /* Destination type (printer/class) */
10988 cupsd_printer_t
*printer
; /* Printer data */
10989 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10992 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10993 con
->number
, uri
->values
[0].string
.text
);
10996 * Is the destination valid?
10999 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11005 send_ipp_status(con
, IPP_NOT_FOUND
,
11006 _("The printer or class does not exist."));
11014 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11016 send_http_error(con
, status
, printer
);
11021 * Stop the printer...
11024 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
11025 IPP_TAG_TEXT
)) == NULL
)
11026 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
11029 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
11030 sizeof(printer
->state_message
));
11033 cupsdStopPrinter(printer
, 1);
11035 if (dtype
& CUPS_PRINTER_CLASS
)
11036 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
11037 printer
->name
, get_username(con
));
11039 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
11040 printer
->name
, get_username(con
));
11043 * Everything was ok, so return OK status...
11046 con
->response
->request
.status
.status_code
= IPP_OK
;
11051 * 'url_encode_attr()' - URL-encode a string attribute.
11055 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
11056 char *buffer
,/* I - String buffer */
11057 size_t bufsize
)/* I - Size of buffer */
11059 int i
; /* Looping var */
11060 char *bufptr
, /* Pointer into buffer */
11061 *bufend
; /* End of buffer */
11064 strlcpy(buffer
, attr
->name
, bufsize
);
11065 bufptr
= buffer
+ strlen(buffer
);
11066 bufend
= buffer
+ bufsize
- 1;
11068 for (i
= 0; i
< attr
->num_values
; i
++)
11070 if (bufptr
>= bufend
)
11078 if (bufptr
>= bufend
)
11083 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
11085 if (bufptr
>= bufend
)
11096 * 'url_encode_string()' - URL-encode a string.
11099 static char * /* O - End of string */
11100 url_encode_string(const char *s
, /* I - String */
11101 char *buffer
, /* I - String buffer */
11102 size_t bufsize
) /* I - Size of buffer */
11104 char *bufptr
, /* Pointer into buffer */
11105 *bufend
; /* End of buffer */
11106 static const char *hex
= "0123456789ABCDEF";
11111 bufend
= buffer
+ bufsize
- 1;
11113 while (*s
&& bufptr
< bufend
)
11115 if (*s
== ' ' || *s
== '%' || *s
== '+')
11117 if (bufptr
>= (bufend
- 2))
11121 *bufptr
++ = hex
[(*s
>> 4) & 15];
11122 *bufptr
++ = hex
[*s
& 15];
11126 else if (*s
== '\'' || *s
== '\\')
11128 if (bufptr
>= (bufend
- 1))
11145 * 'user_allowed()' - See if a user is allowed to print to a queue.
11148 static int /* O - 0 if not allowed, 1 if allowed */
11149 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
11150 const char *username
) /* I - Username */
11152 struct passwd
*pw
; /* User password data */
11153 char baseuser
[256], /* Base username */
11154 *baseptr
, /* Pointer to "@" in base username */
11155 *name
; /* Current user name */
11158 if (cupsArrayCount(p
->users
) == 0)
11161 if (!strcmp(username
, "root"))
11164 if (strchr(username
, '@'))
11167 * Strip @REALM for username check...
11170 strlcpy(baseuser
, username
, sizeof(baseuser
));
11172 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
11175 username
= baseuser
;
11178 pw
= getpwnam(username
);
11181 for (name
= (char *)cupsArrayFirst(p
->users
);
11183 name
= (char *)cupsArrayNext(p
->users
))
11185 if (name
[0] == '@')
11188 * Check group membership...
11191 if (cupsdCheckGroup(username
, pw
, name
+ 1))
11194 else if (name
[0] == '#')
11200 if (cupsdCheckGroup(username
, pw
, name
))
11203 else if (!_cups_strcasecmp(username
, name
))
11207 return ((name
!= NULL
) != p
->deny_users
);
11212 * 'validate_job()' - Validate printer options and destination.
11216 validate_job(cupsd_client_t
*con
, /* I - Client connection */
11217 ipp_attribute_t
*uri
) /* I - Printer URI */
11219 http_status_t status
; /* Policy status */
11220 ipp_attribute_t
*attr
; /* Current attribute */
11222 ipp_attribute_t
*auth_info
; /* auth-info attribute */
11223 #endif /* HAVE_SSL */
11224 ipp_attribute_t
*format
, /* Document-format attribute */
11225 *name
; /* Job-name attribute */
11226 cups_ptype_t dtype
; /* Destination type (printer/class) */
11227 char super
[MIME_MAX_SUPER
],
11228 /* Supertype of file */
11229 type
[MIME_MAX_TYPE
];
11230 /* Subtype of file */
11231 cupsd_printer_t
*printer
; /* Printer */
11234 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
11235 con
->number
, uri
->values
[0].string
.text
);
11238 * OK, see if the client is sending the document compressed - CUPS
11239 * doesn't support compression yet...
11242 if ((attr
= ippFindAttribute(con
->request
, "compression",
11243 IPP_TAG_KEYWORD
)) != NULL
)
11245 if (strcmp(attr
->values
[0].string
.text
, "none")
11247 && strcmp(attr
->values
[0].string
.text
, "gzip")
11248 #endif /* HAVE_LIBZ */
11251 send_ipp_status(con
, IPP_ATTRIBUTES
,
11252 _("Unsupported 'compression' value \"%s\"."),
11253 attr
->values
[0].string
.text
);
11254 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
11255 "compression", NULL
, attr
->values
[0].string
.text
);
11261 * Is it a format we support?
11264 if ((format
= ippFindAttribute(con
->request
, "document-format",
11265 IPP_TAG_MIMETYPE
)) != NULL
)
11267 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
11270 send_ipp_status(con
, IPP_BAD_REQUEST
,
11271 _("Bad 'document-format' value \"%s\"."),
11272 format
->values
[0].string
.text
);
11276 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
11277 !mimeType(MimeDatabase
, super
, type
))
11279 cupsdLogMessage(CUPSD_LOG_INFO
,
11280 "Hint: Do you have the raw file printing rules enabled?");
11281 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
11282 _("Unsupported 'document-format' value \"%s\"."),
11283 format
->values
[0].string
.text
);
11284 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
11285 "document-format", NULL
, format
->values
[0].string
.text
);
11291 * Is the job-name valid?
11294 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
11296 int bad_name
= 0; /* Is the job-name value bad? */
11298 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
11299 name
->num_values
!= 1)
11306 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
11307 * IPP Everywhere requirements for "name" values...
11310 const unsigned char *nameptr
; /* Pointer into "job-name" attribute */
11312 for (nameptr
= (unsigned char *)name
->values
[0].string
.text
;
11316 if (*nameptr
< ' ' && *nameptr
!= '\t')
11318 else if (*nameptr
== 0x7f)
11320 else if ((*nameptr
& 0xe0) == 0xc0)
11322 if ((nameptr
[1] & 0xc0) != 0x80)
11327 else if ((*nameptr
& 0xf0) == 0xe0)
11329 if ((nameptr
[1] & 0xc0) != 0x80 ||
11330 (nameptr
[2] & 0xc0) != 0x80)
11335 else if ((*nameptr
& 0xf8) == 0xf0)
11337 if ((nameptr
[1] & 0xc0) != 0x80 ||
11338 (nameptr
[2] & 0xc0) != 0x80 ||
11339 (nameptr
[3] & 0xc0) != 0x80)
11344 else if (*nameptr
& 0x80)
11354 if (StrictConformance
)
11356 send_ipp_status(con
, IPP_ATTRIBUTES
,
11357 _("Unsupported 'job-name' value."));
11358 ippCopyAttribute(con
->response
, name
, 0);
11363 cupsdLogMessage(CUPSD_LOG_WARN
,
11364 "Unsupported 'job-name' value, deleting from request.");
11365 ippDeleteAttribute(con
->request
, name
);
11371 * Is the destination valid?
11374 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11380 send_ipp_status(con
, IPP_NOT_FOUND
,
11381 _("The printer or class does not exist."));
11390 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
11391 #endif /* HAVE_SSL */
11393 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11395 send_http_error(con
, status
, printer
);
11398 else if (printer
->num_auth_info_required
== 1 &&
11399 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
11402 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
11406 else if (auth_info
&& !con
->http
->tls
&&
11407 !httpAddrLocalhost(con
->http
->hostaddr
))
11410 * Require encryption of auth-info over non-local connections...
11413 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
11416 #endif /* HAVE_SSL */
11419 * Everything was ok, so return OK status...
11422 con
->response
->request
.status
.status_code
= IPP_OK
;
11427 * 'validate_name()' - Make sure the printer name only contains valid chars.
11430 static int /* O - 0 if name is no good, 1 if good */
11431 validate_name(const char *name
) /* I - Name to check */
11433 const char *ptr
; /* Pointer into name */
11437 * Scan the whole name...
11440 for (ptr
= name
; *ptr
; ptr
++)
11441 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11445 * All the characters are good; validate the length, too...
11448 return ((ptr
- name
) < 128);
11453 * 'validate_user()' - Validate the user for the request.
11456 static int /* O - 1 if permitted, 0 otherwise */
11457 validate_user(cupsd_job_t
*job
, /* I - Job */
11458 cupsd_client_t
*con
, /* I - Client connection */
11459 const char *owner
, /* I - Owner of job/resource */
11460 char *username
, /* O - Authenticated username */
11461 size_t userlen
) /* I - Length of username */
11463 cupsd_printer_t
*printer
; /* Printer for job */
11466 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
);
11469 * Validate input...
11472 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11476 * Get the best authenticated username that is available.
11479 strlcpy(username
, get_username(con
), userlen
);
11482 * Check the username against the owner...
11485 printer
= cupsdFindDest(job
->dest
);
11487 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11488 con
, owner
) == HTTP_OK
);