2 * IPP routines for the CUPS scheduler.
4 * Copyright 2007-2017 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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
14 * Include necessary headers...
18 #include <cups/ppd-private.h>
21 /*# include <ApplicationServices/ApplicationServices.h>
22 extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
23 # include <CoreFoundation/CoreFoundation.h>*/
24 # ifdef HAVE_MEMBERSHIP_H
25 # include <membership.h>
26 # endif /* HAVE_MEMBERSHIP_H */
27 # ifdef HAVE_MEMBERSHIPPRIV_H
28 # include <membershipPriv.h>
30 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
31 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
32 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
33 # endif /* HAVE_MEMBERSHIPPRIV_H */
34 #endif /* __APPLE__ */
41 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
42 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
43 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
44 mime_type_t
*filetype
, int compression
);
45 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
46 mime_type_t
*filetype
);
47 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
48 static void add_job_uuid(cupsd_job_t
*job
);
49 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
50 static void add_printer_state_reasons(cupsd_client_t
*con
,
52 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
53 static void apply_printer_defaults(cupsd_printer_t
*printer
,
55 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
56 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
57 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
58 static void cancel_subscription(cupsd_client_t
*con
, int id
);
59 static int check_rss_recipient(const char *recipient
);
60 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
61 static void close_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
62 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
63 ipp_tag_t group
, int quickcopy
,
64 cups_array_t
*exclude
);
65 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
67 static int copy_file(const char *from
, const char *to
, mode_t mode
);
68 static int copy_model(cupsd_client_t
*con
, const char *from
,
70 static void copy_job_attrs(cupsd_client_t
*con
,
72 cups_array_t
*ra
, cups_array_t
*exclude
);
73 static void copy_printer_attrs(cupsd_client_t
*con
,
74 cupsd_printer_t
*printer
,
76 static void copy_subscription_attrs(cupsd_client_t
*con
,
77 cupsd_subscription_t
*sub
,
79 cups_array_t
*exclude
);
80 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
81 static void create_local_printer(cupsd_client_t
*con
);
82 static cups_array_t
*create_requested_array(ipp_t
*request
);
83 static void create_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
84 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
85 static void get_default(cupsd_client_t
*con
);
86 static void get_devices(cupsd_client_t
*con
);
87 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
88 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
89 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
90 static void get_notifications(cupsd_client_t
*con
);
91 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
92 static void get_ppds(cupsd_client_t
*con
);
93 static void get_printers(cupsd_client_t
*con
, int type
);
94 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
95 static void get_printer_supported(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
96 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
97 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
98 static const char *get_username(cupsd_client_t
*con
);
99 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
100 static void hold_new_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
101 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
102 static int ppd_parse_line(const char *line
, char *option
, int olen
,
103 char *choice
, int clen
);
104 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
105 static void read_job_ticket(cupsd_client_t
*con
);
106 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
107 static void release_held_new_jobs(cupsd_client_t
*con
,
108 ipp_attribute_t
*uri
);
109 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
110 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
111 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
112 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
113 ipp_attribute_t
*auth_info
);
114 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
115 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
116 cupsd_printer_t
*printer
);
117 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
118 const char *message
, ...)
119 __attribute__((__format__(__printf__
, 3, 4)));
120 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
121 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
122 static void set_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
123 static int set_printer_defaults(cupsd_client_t
*con
, cupsd_printer_t
*printer
);
124 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
125 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
126 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
, size_t bufsize
);
127 static char *url_encode_string(const char *s
, char *buffer
, size_t bufsize
);
128 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
129 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
130 static int validate_name(const char *name
);
131 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
, const char *owner
, char *username
, size_t userlen
);
135 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
138 int /* O - 1 on success, 0 on failure */
139 cupsdProcessIPPRequest(
140 cupsd_client_t
*con
) /* I - Client connection */
142 ipp_tag_t group
; /* Current group tag */
143 ipp_attribute_t
*attr
; /* Current attribute */
144 ipp_attribute_t
*charset
; /* Character set attribute */
145 ipp_attribute_t
*language
; /* Language attribute */
146 ipp_attribute_t
*uri
= NULL
; /* Printer or job URI attribute */
147 ipp_attribute_t
*username
; /* requesting-user-name attr */
148 int sub_id
; /* Subscription ID */
151 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
));
153 if (LogLevel
>= CUPSD_LOG_DEBUG2
)
155 for (group
= IPP_TAG_ZERO
, attr
= ippFirstAttribute(con
->request
); attr
; attr
= ippNextAttribute(con
->request
))
157 const char *name
; /* Attribute name */
158 char value
[1024]; /* Attribute value */
160 if (group
!= ippGetGroupTag(attr
))
162 group
= ippGetGroupTag(attr
);
163 if (group
!= IPP_TAG_ZERO
)
164 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdProcessIPPRequest: %s", ippTagString(group
));
167 if ((name
= ippGetName(attr
)) == NULL
)
170 ippAttributeString(attr
, value
, sizeof(value
));
172 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdProcessIPPRequest: %s %s%s '%s'", name
, ippGetCount(attr
) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr
)), value
);
177 * First build an empty response message for this request...
180 con
->response
= ippNew();
182 con
->response
->request
.status
.version
[0] =
183 con
->request
->request
.op
.version
[0];
184 con
->response
->request
.status
.version
[1] =
185 con
->request
->request
.op
.version
[1];
186 con
->response
->request
.status
.request_id
=
187 con
->request
->request
.op
.request_id
;
190 * Then validate the request header and required attributes...
193 if (con
->request
->request
.any
.version
[0] != 1 &&
194 con
->request
->request
.any
.version
[0] != 2)
197 * Return an error, since we only support IPP 1.x and 2.x.
200 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
201 "%04X %s Bad request version number %d.%d",
202 IPP_VERSION_NOT_SUPPORTED
, con
->http
->hostname
,
203 con
->request
->request
.any
.version
[0],
204 con
->request
->request
.any
.version
[1]);
206 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
207 _("Bad request version number %d.%d."),
208 con
->request
->request
.any
.version
[0],
209 con
->request
->request
.any
.version
[1]);
211 else if (con
->request
->request
.any
.request_id
< 1)
214 * Return an error, since request IDs must be between 1 and 2^31-1
217 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
218 "%04X %s Bad request ID %d",
219 IPP_BAD_REQUEST
, con
->http
->hostname
,
220 con
->request
->request
.any
.request_id
);
222 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad request ID %d."),
223 con
->request
->request
.any
.request_id
);
225 else if (!con
->request
->attrs
)
227 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
228 "%04X %s No attributes in request",
229 IPP_BAD_REQUEST
, con
->http
->hostname
);
231 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request."));
236 * Make sure that the attributes are provided in the correct order and
237 * don't repeat groups...
240 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
243 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
246 * Out of order; return an error...
249 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
250 "%04X %s Attribute groups are out of order",
251 IPP_BAD_REQUEST
, con
->http
->hostname
);
253 send_ipp_status(con
, IPP_BAD_REQUEST
,
254 _("Attribute groups are out of order (%x < %x)."),
255 attr
->group_tag
, group
);
259 group
= attr
->group_tag
;
264 * Then make sure that the first three attributes are:
267 * attributes-natural-language
268 * printer-uri/job-uri
271 attr
= con
->request
->attrs
;
272 if (attr
&& attr
->name
&&
273 !strcmp(attr
->name
, "attributes-charset") &&
274 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
282 if (attr
&& attr
->name
&&
283 !strcmp(attr
->name
, "attributes-natural-language") &&
284 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
289 * Reset language for this request if different from Accept-Language.
292 if (!con
->language
||
293 strcmp(attr
->values
[0].string
.text
, con
->language
->language
))
295 cupsLangFree(con
->language
);
296 con
->language
= cupsLangGet(attr
->values
[0].string
.text
);
302 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
303 IPP_TAG_URI
)) != NULL
)
305 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
306 IPP_TAG_URI
)) != NULL
)
308 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
309 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
314 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
315 "attributes-charset", NULL
,
316 charset
->values
[0].string
.text
);
318 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
319 "attributes-charset", NULL
, "utf-8");
322 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
323 "attributes-natural-language", NULL
,
324 language
->values
[0].string
.text
);
326 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
327 "attributes-natural-language", NULL
, DefaultLanguage
);
330 _cups_strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
331 _cups_strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
334 * Bad character set...
337 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"",
338 charset
->values
[0].string
.text
);
339 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
340 "%04X %s Unsupported attributes-charset value \"%s\"",
341 IPP_CHARSET
, con
->http
->hostname
,
342 charset
->values
[0].string
.text
);
343 send_ipp_status(con
, IPP_BAD_REQUEST
,
344 _("Unsupported character set \"%s\"."),
345 charset
->values
[0].string
.text
);
347 else if (!charset
|| !language
||
349 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
350 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
351 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
352 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
353 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
356 * Return an error, since attributes-charset,
357 * attributes-natural-language, and printer-uri/job-uri are required
358 * for all operations.
363 cupsdLogMessage(CUPSD_LOG_ERROR
,
364 "Missing attributes-charset attribute");
366 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
367 "%04X %s Missing attributes-charset attribute",
368 IPP_BAD_REQUEST
, con
->http
->hostname
);
373 cupsdLogMessage(CUPSD_LOG_ERROR
,
374 "Missing attributes-natural-language attribute");
376 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
377 "%04X %s Missing attributes-natural-language attribute",
378 IPP_BAD_REQUEST
, con
->http
->hostname
);
383 cupsdLogMessage(CUPSD_LOG_ERROR
,
384 "Missing printer-uri, job-uri, or ppd-name "
387 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
388 "%04X %s Missing printer-uri, job-uri, or ppd-name "
389 "attribute", IPP_BAD_REQUEST
, con
->http
->hostname
);
392 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
394 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
395 cupsdLogMessage(CUPSD_LOG_DEBUG
,
396 "attr \"%s\": group_tag = %x, value_tag = %x",
397 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
400 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
402 send_ipp_status(con
, IPP_BAD_REQUEST
,
403 _("Missing required attributes."));
408 * OK, all the checks pass so far; make sure requesting-user-name is
409 * not "root" from a remote host...
412 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
413 IPP_TAG_NAME
)) != NULL
)
416 * Check for root user...
419 if (!strcmp(username
->values
[0].string
.text
, "root") &&
420 _cups_strcasecmp(con
->http
->hostname
, "localhost") &&
421 strcmp(con
->username
, "root"))
424 * Remote unauthenticated user masquerading as local root...
427 ippSetString(con
->request
, &username
, 0, RemoteRoot
);
431 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
432 IPP_TAG_INTEGER
)) != NULL
)
433 sub_id
= attr
->values
[0].integer
;
438 * Then try processing the operation...
442 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
443 ippOpString(con
->request
->request
.op
.operation_id
),
444 uri
->values
[0].string
.text
);
446 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
447 ippOpString(con
->request
->request
.op
.operation_id
));
449 switch (con
->request
->request
.op
.operation_id
)
451 case IPP_OP_PRINT_JOB
:
455 case IPP_OP_VALIDATE_JOB
:
456 validate_job(con
, uri
);
459 case IPP_OP_CREATE_JOB
:
460 create_job(con
, uri
);
463 case IPP_OP_SEND_DOCUMENT
:
464 send_document(con
, uri
);
467 case IPP_OP_CANCEL_JOB
:
468 cancel_job(con
, uri
);
471 case IPP_OP_GET_JOB_ATTRIBUTES
:
472 get_job_attrs(con
, uri
);
475 case IPP_OP_GET_JOBS
:
479 case IPP_OP_GET_PRINTER_ATTRIBUTES
:
480 get_printer_attrs(con
, uri
);
483 case IPP_OP_GET_PRINTER_SUPPORTED_VALUES
:
484 get_printer_supported(con
, uri
);
487 case IPP_OP_HOLD_JOB
:
491 case IPP_OP_RELEASE_JOB
:
492 release_job(con
, uri
);
495 case IPP_OP_RESTART_JOB
:
496 restart_job(con
, uri
);
499 case IPP_OP_PAUSE_PRINTER
:
500 stop_printer(con
, uri
);
503 case IPP_OP_RESUME_PRINTER
:
504 start_printer(con
, uri
);
507 case IPP_OP_PURGE_JOBS
:
508 case IPP_OP_CANCEL_JOBS
:
509 case IPP_OP_CANCEL_MY_JOBS
:
510 cancel_all_jobs(con
, uri
);
513 case IPP_OP_SET_JOB_ATTRIBUTES
:
514 set_job_attrs(con
, uri
);
517 case IPP_OP_SET_PRINTER_ATTRIBUTES
:
518 set_printer_attrs(con
, uri
);
521 case IPP_OP_HOLD_NEW_JOBS
:
522 hold_new_jobs(con
, uri
);
525 case IPP_OP_RELEASE_HELD_NEW_JOBS
:
526 release_held_new_jobs(con
, uri
);
529 case IPP_OP_CLOSE_JOB
:
533 case IPP_OP_CUPS_GET_DEFAULT
:
537 case IPP_OP_CUPS_GET_PRINTERS
:
538 get_printers(con
, 0);
541 case IPP_OP_CUPS_GET_CLASSES
:
542 get_printers(con
, CUPS_PRINTER_CLASS
);
545 case IPP_OP_CUPS_ADD_MODIFY_PRINTER
:
546 add_printer(con
, uri
);
549 case IPP_OP_CUPS_DELETE_PRINTER
:
550 delete_printer(con
, uri
);
553 case IPP_OP_CUPS_ADD_MODIFY_CLASS
:
557 case IPP_OP_CUPS_DELETE_CLASS
:
558 delete_printer(con
, uri
);
561 case IPP_OP_CUPS_ACCEPT_JOBS
:
562 case IPP_OP_ENABLE_PRINTER
:
563 accept_jobs(con
, uri
);
566 case IPP_OP_CUPS_REJECT_JOBS
:
567 case IPP_OP_DISABLE_PRINTER
:
568 reject_jobs(con
, uri
);
571 case IPP_OP_CUPS_SET_DEFAULT
:
572 set_default(con
, uri
);
575 case IPP_OP_CUPS_GET_DEVICES
:
579 case IPP_OP_CUPS_GET_DOCUMENT
:
580 get_document(con
, uri
);
583 case IPP_OP_CUPS_GET_PPD
:
587 case IPP_OP_CUPS_GET_PPDS
:
591 case IPP_OP_CUPS_MOVE_JOB
:
595 case IPP_OP_CUPS_AUTHENTICATE_JOB
:
596 authenticate_job(con
, uri
);
599 case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS
:
600 case IPP_OP_CREATE_JOB_SUBSCRIPTIONS
:
601 create_subscriptions(con
, uri
);
604 case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES
:
605 get_subscription_attrs(con
, sub_id
);
608 case IPP_OP_GET_SUBSCRIPTIONS
:
609 get_subscriptions(con
, uri
);
612 case IPP_OP_RENEW_SUBSCRIPTION
:
613 renew_subscription(con
, sub_id
);
616 case IPP_OP_CANCEL_SUBSCRIPTION
:
617 cancel_subscription(con
, sub_id
);
620 case IPP_OP_GET_NOTIFICATIONS
:
621 get_notifications(con
);
624 case IPP_OP_CUPS_CREATE_LOCAL_PRINTER
:
625 create_local_printer(con
);
629 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
630 "%04X %s Operation %04X (%s) not supported",
631 IPP_OPERATION_NOT_SUPPORTED
, con
->http
->hostname
,
632 con
->request
->request
.op
.operation_id
,
633 ippOpString(con
->request
->request
.op
.operation_id
));
635 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
636 _("%s not supported."),
638 con
->request
->request
.op
.operation_id
));
648 * Sending data from the scheduler...
651 cupsdLogMessage(con
->response
->request
.status
.status_code
652 >= IPP_BAD_REQUEST
&&
653 con
->response
->request
.status
.status_code
654 != IPP_NOT_FOUND
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
655 "[Client %d] Returning IPP %s for %s (%s) from %s",
657 ippErrorString(con
->response
->request
.status
.status_code
),
658 ippOpString(con
->request
->request
.op
.operation_id
),
659 uri
? uri
->values
[0].string
.text
: "no URI",
660 con
->http
->hostname
);
662 httpClearFields(con
->http
);
664 #ifdef CUPSD_USE_CHUNKING
666 * Because older versions of CUPS (1.1.17 and older) and some IPP
667 * clients do not implement chunking properly, we cannot use
668 * chunking by default. This may become the default in future
669 * CUPS releases, or we might add a configuration directive for
673 if (con
->http
->version
== HTTP_1_1
)
675 cupsdLogMessage(CUPSD_LOG_DEBUG
,
676 "[Client %d] Transfer-Encoding: chunked",
679 cupsdSetLength(con
->http
, 0);
682 #endif /* CUPSD_USE_CHUNKING */
684 size_t length
; /* Length of response */
687 length
= ippLength(con
->response
);
689 if (con
->file
>= 0 && !con
->pipe_pid
)
691 struct stat fileinfo
; /* File information */
693 if (!fstat(con
->file
, &fileinfo
))
694 length
+= (size_t)fileinfo
.st_size
;
697 cupsdLogMessage(CUPSD_LOG_DEBUG
,
698 "[Client %d] Content-Length: " CUPS_LLFMT
,
699 con
->number
, CUPS_LLCAST length
);
700 httpSetLength(con
->http
, length
);
703 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
706 * Tell the caller the response header was sent successfully...
709 cupsdAddSelect(httpGetFd(con
->http
), (cupsd_selfunc_t
)cupsdReadClient
,
710 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
717 * Tell the caller the response header could not be sent...
726 * Sending data from a subprocess like cups-deviced; tell the caller
727 * everything is A-OK so far...
736 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
739 int /* O - 0 on success, -1 on error */
740 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
742 cupsd_printer_t
*printer
; /* Destination printer or class */
743 ipp_attribute_t
*attr
; /* job-sheets attribute */
744 int kbytes
; /* Kilobytes in banner */
747 job
->pending_timeout
= 0;
750 * See if we need to add the ending sheet...
753 if (!cupsdLoadJob(job
))
756 printer
= cupsdFindDest(job
->dest
);
757 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
759 if (printer
&& !(printer
->type
& CUPS_PRINTER_REMOTE
) &&
760 attr
&& attr
->num_values
> 1)
766 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding end banner page \"%s\".",
767 attr
->values
[1].string
.text
);
769 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
772 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
780 * 'accept_jobs()' - Accept print jobs to a printer.
784 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
785 ipp_attribute_t
*uri
) /* I - Printer or class URI */
787 http_status_t status
; /* Policy status */
788 cups_ptype_t dtype
; /* Destination type (printer/class) */
789 cupsd_printer_t
*printer
; /* Printer data */
792 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
793 con
->number
, uri
->values
[0].string
.text
);
796 * Is the destination valid?
799 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
805 send_ipp_status(con
, IPP_NOT_FOUND
,
806 _("The printer or class does not exist."));
814 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
816 send_http_error(con
, status
, printer
);
821 * Accept jobs sent to the printer...
824 printer
->accepting
= 1;
825 printer
->state_message
[0] = '\0';
827 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
828 "Now accepting jobs.");
830 if (dtype
& CUPS_PRINTER_CLASS
)
832 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
834 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
835 printer
->name
, get_username(con
));
839 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
841 cupsdLogMessage(CUPSD_LOG_INFO
,
842 "Printer \"%s\" now accepting jobs (\"%s\").",
843 printer
->name
, get_username(con
));
847 * Everything was ok, so return OK status...
850 con
->response
->request
.status
.status_code
= IPP_OK
;
855 * 'add_class()' - Add a class to the system.
859 add_class(cupsd_client_t
*con
, /* I - Client connection */
860 ipp_attribute_t
*uri
) /* I - URI of class */
862 http_status_t status
; /* Policy status */
863 int i
; /* Looping var */
864 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
865 username
[HTTP_MAX_URI
], /* Username portion of URI */
866 host
[HTTP_MAX_URI
], /* Host portion of URI */
867 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
868 int port
; /* Port portion of URI */
869 cupsd_printer_t
*pclass
, /* Class */
870 *member
; /* Member printer/class */
871 cups_ptype_t dtype
; /* Destination type */
872 ipp_attribute_t
*attr
; /* Printer attribute */
873 int modify
; /* Non-zero if we just modified */
874 int need_restart_job
; /* Need to restart job? */
877 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
878 con
->number
, uri
->values
[0].string
.text
);
881 * Do we have a valid URI?
884 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
885 sizeof(scheme
), username
, sizeof(username
), host
,
886 sizeof(host
), &port
, resource
, sizeof(resource
));
889 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
892 * No, return an error...
895 send_ipp_status(con
, IPP_BAD_REQUEST
,
896 _("The printer-uri must be of the form "
897 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
902 * Do we have a valid printer name?
905 if (!validate_name(resource
+ 9))
908 * No, return an error...
911 send_ipp_status(con
, IPP_BAD_REQUEST
,
912 _("The printer-uri \"%s\" contains invalid characters."),
913 uri
->values
[0].string
.text
);
918 * See if the class already exists; if not, create a new class...
921 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
924 * Class doesn't exist; see if we have a printer of the same name...
927 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
)
930 * Yes, return an error...
933 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
934 _("A printer named \"%s\" already exists."),
940 * No, check the default policy and then add the class...
943 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
945 send_http_error(con
, status
, NULL
);
949 pclass
= cupsdAddClass(resource
+ 9);
952 pclass
->printer_id
= NextPrinterId
++;
954 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
957 send_http_error(con
, status
, pclass
);
964 * Look for attributes and copy them over as needed...
967 need_restart_job
= 0;
969 if ((attr
= ippFindAttribute(con
->request
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
970 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
972 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
973 cupsdSetString(&pclass
->geo_location
, attr
->values
[0].string
.text
);
975 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
976 cupsdSetString(&pclass
->organization
, attr
->values
[0].string
.text
);
978 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
979 cupsdSetString(&pclass
->organizational_unit
, attr
->values
[0].string
.text
);
981 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
982 IPP_TAG_TEXT
)) != NULL
)
983 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
985 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
986 IPP_TAG_BOOLEAN
)) != NULL
&&
987 attr
->values
[0].boolean
!= pclass
->accepting
)
989 cupsdLogMessage(CUPSD_LOG_INFO
,
990 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
991 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
993 pclass
->accepting
= attr
->values
[0].boolean
;
995 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s accepting jobs.",
996 pclass
->accepting
? "Now" : "No longer");
999 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared", IPP_TAG_BOOLEAN
)) != NULL
)
1001 if (pclass
->type
& CUPS_PRINTER_REMOTE
)
1004 * Cannot re-share remote printers.
1007 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Cannot change printer-is-shared for remote queues."));
1009 cupsdDeletePrinter(pclass
, 0);
1014 if (pclass
->shared
&& !ippGetBoolean(attr
, 0))
1015 cupsdDeregisterPrinter(pclass
, 1);
1017 cupsdLogMessage(CUPSD_LOG_INFO
,
1018 "Setting %s printer-is-shared to %d (was %d.)",
1019 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1021 pclass
->shared
= ippGetBoolean(attr
, 0);
1024 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1025 IPP_TAG_ENUM
)) != NULL
)
1027 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1028 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1030 send_ipp_status(con
, IPP_BAD_REQUEST
,
1031 _("Attempt to set %s printer-state to bad value %d."),
1032 pclass
->name
, attr
->values
[0].integer
);
1034 cupsdDeletePrinter(pclass
, 0);
1039 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1040 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1042 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1043 cupsdStopPrinter(pclass
, 0);
1046 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1047 need_restart_job
= 1;
1050 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1051 IPP_TAG_TEXT
)) != NULL
)
1053 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1054 sizeof(pclass
->state_message
));
1056 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s",
1057 pclass
->state_message
);
1059 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1060 IPP_TAG_URI
)) != NULL
)
1063 * Clear the printer array as needed...
1066 need_restart_job
= 1;
1068 if (pclass
->num_printers
> 0)
1070 free(pclass
->printers
);
1071 pclass
->num_printers
= 0;
1075 * Add each printer or class that is listed...
1078 for (i
= 0; i
< attr
->num_values
; i
++)
1081 * Search for the printer or class URI...
1084 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1090 send_ipp_status(con
, IPP_NOT_FOUND
,
1091 _("The printer or class does not exist."));
1093 cupsdDeletePrinter(pclass
, 0);
1097 else if (dtype
& CUPS_PRINTER_CLASS
)
1099 send_ipp_status(con
, IPP_BAD_REQUEST
,
1100 _("Nested classes are not allowed."));
1102 cupsdDeletePrinter(pclass
, 0);
1108 * Add it to the class...
1111 cupsdAddPrinterToClass(pclass
, member
);
1115 if (!set_printer_defaults(con
, pclass
))
1118 cupsdDeletePrinter(pclass
, 0);
1123 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1124 IPP_TAG_KEYWORD
)) != NULL
)
1125 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1127 pclass
->config_time
= time(NULL
);
1130 * Update the printer class attributes and return...
1133 cupsdSetPrinterAttrs(pclass
);
1134 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1136 if (need_restart_job
&& pclass
->job
)
1139 * Reset the current job to a "pending" status...
1142 cupsdSetJobState(pclass
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
1143 "Job restarted because the class was modified.");
1146 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1150 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
1151 pclass
, NULL
, "Class \"%s\" modified by \"%s\".",
1152 pclass
->name
, get_username(con
));
1154 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1155 pclass
->name
, get_username(con
));
1159 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
1160 pclass
, NULL
, "New class \"%s\" added by \"%s\".",
1161 pclass
->name
, get_username(con
));
1163 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1164 pclass
->name
, get_username(con
));
1167 con
->response
->request
.status
.status_code
= IPP_OK
;
1172 * 'add_file()' - Add a file to a job.
1175 static int /* O - 0 on success, -1 on error */
1176 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1177 cupsd_job_t
*job
, /* I - Job to add to */
1178 mime_type_t
*filetype
, /* I - Type of file */
1179 int compression
) /* I - Compression */
1181 mime_type_t
**filetypes
; /* New filetypes array... */
1182 int *compressions
; /* New compressions array... */
1185 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1186 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1187 "compression=%d)", con
, con
? con
->number
: -1, job
->id
,
1188 filetype
->super
, filetype
->type
, compression
);
1191 * Add the file to the job...
1194 if (job
->num_files
== 0)
1196 compressions
= (int *)malloc(sizeof(int));
1197 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1201 compressions
= (int *)realloc(job
->compressions
,
1202 (size_t)(job
->num_files
+ 1) * sizeof(int));
1203 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1204 (size_t)(job
->num_files
+ 1) *
1205 sizeof(mime_type_t
*));
1209 job
->compressions
= compressions
;
1212 job
->filetypes
= filetypes
;
1214 if (!compressions
|| !filetypes
)
1216 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1217 "Job aborted because the scheduler ran out of memory.");
1220 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1221 _("Unable to allocate memory for file types."));
1226 job
->compressions
[job
->num_files
] = compression
;
1227 job
->filetypes
[job
->num_files
] = filetype
;
1232 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1239 * 'add_job()' - Add a job to a print queue.
1242 static cupsd_job_t
* /* O - Job object */
1243 add_job(cupsd_client_t
*con
, /* I - Client connection */
1244 cupsd_printer_t
*printer
, /* I - Destination printer */
1245 mime_type_t
*filetype
) /* I - First print file type, if any */
1247 http_status_t status
; /* Policy status */
1248 ipp_attribute_t
*attr
, /* Current attribute */
1249 *auth_info
; /* auth-info attribute */
1250 const char *mandatory
; /* Current mandatory job attribute */
1251 const char *val
; /* Default option value */
1252 int priority
; /* Job priority */
1253 cupsd_job_t
*job
; /* Current job */
1254 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1255 int kbytes
; /* Size of print file */
1256 int i
; /* Looping var */
1257 int lowerpagerange
; /* Page range bound */
1258 int exact
; /* Did we have an exact match? */
1259 ipp_attribute_t
*media_col
, /* media-col attribute */
1260 *media_margin
; /* media-*-margin attribute */
1261 ipp_t
*unsup_col
; /* media-col in unsupported response */
1262 static const char * const readonly
[] =/* List of read-only attributes */
1264 "date-time-at-completed",
1265 "date-time-at-creation",
1266 "date-time-at-processing",
1267 "job-detailed-status-messages",
1268 "job-document-access-errors",
1270 "job-impressions-completed",
1271 "job-k-octets-completed",
1272 "job-media-sheets-completed",
1273 "job-pages-completed",
1274 "job-printer-up-time",
1277 "job-state-message",
1278 "job-state-reasons",
1280 "number-of-documents",
1281 "number-of-intervening-jobs",
1282 "output-device-assigned",
1283 "time-at-completed",
1285 "time-at-processing"
1289 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1290 con
, con
->number
, printer
, printer
->name
,
1291 filetype
, filetype
? filetype
->super
: "none",
1292 filetype
? filetype
->type
: "none");
1295 * Check remote printing to non-shared printer...
1298 if (!printer
->shared
&&
1299 _cups_strcasecmp(con
->http
->hostname
, "localhost") &&
1300 _cups_strcasecmp(con
->http
->hostname
, ServerName
))
1302 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1303 _("The printer or class is not shared."));
1311 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1313 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1315 send_http_error(con
, status
, printer
);
1318 else if (printer
->num_auth_info_required
== 1 &&
1319 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
1322 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1326 else if (auth_info
&& !con
->http
->tls
&&
1327 !httpAddrLocalhost(con
->http
->hostaddr
))
1330 * Require encryption of auth-info over non-local connections...
1333 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1336 #endif /* HAVE_SSL */
1339 * See if the printer is accepting jobs...
1342 if (!printer
->accepting
)
1344 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1345 _("Destination \"%s\" is not accepting jobs."),
1351 * Validate job template attributes; for now just document-format,
1352 * copies, job-sheets, number-up, page-ranges, mandatory attributes, and
1356 for (i
= 0; i
< (int)(sizeof(readonly
) / sizeof(readonly
[0])); i
++)
1358 if ((attr
= ippFindAttribute(con
->request
, readonly
[i
], IPP_TAG_ZERO
)) != NULL
)
1360 ippDeleteAttribute(con
->request
, attr
);
1362 if (StrictConformance
)
1364 send_ipp_status(con
, IPP_BAD_REQUEST
, _("The '%s' Job Status attribute cannot be supplied in a job creation request."), readonly
[i
]);
1368 cupsdLogMessage(CUPSD_LOG_INFO
, "Unexpected '%s' Job Status attribute in a job creation request.", readonly
[i
]);
1374 for (mandatory
= (char *)cupsArrayFirst(printer
->pc
->mandatory
);
1376 mandatory
= (char *)cupsArrayNext(printer
->pc
->mandatory
))
1378 if (!ippFindAttribute(con
->request
, mandatory
, IPP_TAG_ZERO
))
1381 * Missing a required attribute...
1384 send_ipp_status(con
, IPP_CONFLICT
,
1385 _("The \"%s\" attribute is required for print jobs."),
1392 if (filetype
&& printer
->filetypes
&&
1393 !cupsArrayFind(printer
->filetypes
, filetype
))
1395 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1396 /* MIME media type string */
1399 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1402 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1403 _("Unsupported format \"%s\"."), mimetype
);
1405 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1406 "document-format", NULL
, mimetype
);
1411 if ((attr
= ippFindAttribute(con
->request
, "copies",
1412 IPP_TAG_INTEGER
)) != NULL
)
1414 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1416 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1417 attr
->values
[0].integer
);
1418 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1419 "copies", attr
->values
[0].integer
);
1424 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1425 IPP_TAG_ZERO
)) != NULL
)
1427 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1428 attr
->value_tag
!= IPP_TAG_NAME
)
1430 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type."));
1434 if (attr
->num_values
> 2)
1436 send_ipp_status(con
, IPP_BAD_REQUEST
,
1437 _("Too many job-sheets values (%d > 2)."),
1442 for (i
= 0; i
< attr
->num_values
; i
++)
1443 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1444 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1446 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\"."),
1447 attr
->values
[i
].string
.text
);
1452 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1453 IPP_TAG_INTEGER
)) != NULL
)
1455 if (attr
->values
[0].integer
!= 1 &&
1456 attr
->values
[0].integer
!= 2 &&
1457 attr
->values
[0].integer
!= 4 &&
1458 attr
->values
[0].integer
!= 6 &&
1459 attr
->values
[0].integer
!= 9 &&
1460 attr
->values
[0].integer
!= 16)
1462 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1463 attr
->values
[0].integer
);
1464 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1465 "number-up", attr
->values
[0].integer
);
1470 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1471 IPP_TAG_RANGE
)) != NULL
)
1473 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1475 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1476 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1478 send_ipp_status(con
, IPP_BAD_REQUEST
,
1479 _("Bad page-ranges values %d-%d."),
1480 attr
->values
[i
].range
.lower
,
1481 attr
->values
[i
].range
.upper
);
1485 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1490 * Do media selection as needed...
1493 if (!ippFindAttribute(con
->request
, "PageRegion", IPP_TAG_ZERO
) &&
1494 !ippFindAttribute(con
->request
, "PageSize", IPP_TAG_ZERO
) &&
1495 _ppdCacheGetPageSize(printer
->pc
, con
->request
, NULL
, &exact
))
1498 (media_col
= ippFindAttribute(con
->request
, "media-col",
1499 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1501 send_ipp_status(con
, IPP_OK_SUBST
, _("Unsupported margins."));
1503 unsup_col
= ippNew();
1504 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1505 "media-bottom-margin",
1506 IPP_TAG_INTEGER
)) != NULL
)
1507 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1508 "media-bottom-margin", media_margin
->values
[0].integer
);
1510 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1511 "media-left-margin",
1512 IPP_TAG_INTEGER
)) != NULL
)
1513 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1514 "media-left-margin", media_margin
->values
[0].integer
);
1516 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1517 "media-right-margin",
1518 IPP_TAG_INTEGER
)) != NULL
)
1519 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1520 "media-right-margin", media_margin
->values
[0].integer
);
1522 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1524 IPP_TAG_INTEGER
)) != NULL
)
1525 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1526 "media-top-margin", media_margin
->values
[0].integer
);
1528 ippAddCollection(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, "media-col",
1530 ippDelete(unsup_col
);
1535 * Make sure we aren't over our limit...
1538 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1541 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1543 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Too many active jobs."));
1547 if ((i
= check_quotas(con
, printer
)) < 0)
1549 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1554 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1559 * Create the job and set things up...
1562 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1563 IPP_TAG_INTEGER
)) != NULL
)
1564 priority
= attr
->values
[0].integer
;
1567 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1568 printer
->options
)) != NULL
)
1569 priority
= atoi(val
);
1573 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1577 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) == NULL
)
1578 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1579 else if ((attr
->value_tag
!= IPP_TAG_NAME
&&
1580 attr
->value_tag
!= IPP_TAG_NAMELANG
) ||
1581 attr
->num_values
!= 1)
1583 send_ipp_status(con
, IPP_ATTRIBUTES
,
1584 _("Bad job-name value: Wrong type or count."));
1585 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1586 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1588 if (StrictConformance
)
1591 /* Don't use invalid attribute */
1592 ippDeleteAttribute(con
->request
, attr
);
1594 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1596 else if (!ippValidateAttribute(attr
))
1598 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad job-name value: %s"),
1599 cupsLastErrorString());
1601 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1602 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1604 if (StrictConformance
)
1607 /* Don't use invalid attribute */
1608 ippDeleteAttribute(con
->request
, attr
);
1610 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1613 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1615 if (attr
&& !ippValidateAttribute(attr
))
1617 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad requesting-user-name value: %s"), cupsLastErrorString());
1619 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1620 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1622 if (StrictConformance
)
1625 /* Don't use invalid attribute */
1626 ippDeleteAttribute(con
->request
, attr
);
1628 attr
= ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "requesting-user-name", NULL
, "anonymous");
1631 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1633 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1634 _("Unable to add job for destination \"%s\"."),
1639 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
1640 job
->attrs
= con
->request
;
1642 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1644 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1647 apply_printer_defaults(printer
, job
);
1649 if (con
->username
[0])
1651 cupsdSetString(&job
->username
, con
->username
);
1654 ippSetString(job
->attrs
, &attr
, 0, con
->username
);
1658 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1659 "add_job: requesting-user-name=\"%s\"",
1660 attr
->values
[0].string
.text
);
1662 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1665 cupsdSetString(&job
->username
, "anonymous");
1668 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1669 "job-originating-user-name", NULL
, job
->username
);
1672 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1673 ippSetName(job
->attrs
, &attr
, "job-originating-user-name");
1676 if (con
->username
[0] || auth_info
)
1678 save_auth_info(con
, job
, auth_info
);
1681 * Remove the auth-info attribute from the attribute data...
1685 ippDeleteAttribute(job
->attrs
, auth_info
);
1688 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_NAME
)) != NULL
)
1689 cupsdSetString(&(job
->name
), attr
->values
[0].string
.text
);
1691 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1692 IPP_TAG_ZERO
)) != NULL
)
1695 * Request contains a job-originating-host-name attribute; validate it...
1698 if (attr
->value_tag
!= IPP_TAG_NAME
||
1699 attr
->num_values
!= 1 ||
1700 strcmp(con
->http
->hostname
, "localhost"))
1703 * Can't override the value if we aren't connected via localhost.
1704 * Also, we can only have 1 value and it must be a name value.
1707 ippDeleteAttribute(job
->attrs
, attr
);
1708 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-originating-host-name", NULL
, con
->http
->hostname
);
1711 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1716 * No job-originating-host-name attribute, so use the hostname from
1720 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1721 "job-originating-host-name", NULL
, con
->http
->hostname
);
1724 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-completed");
1725 ippAddDate(job
->attrs
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(time(NULL
)));
1726 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-processing");
1727 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-completed");
1728 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", time(NULL
));
1729 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-processing");
1732 * Add remaining job attributes...
1735 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1736 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1737 "job-state", IPP_JOB_STOPPED
);
1738 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1739 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1740 "job-state-reasons", NULL
, "job-incoming");
1741 job
->impressions
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", 0);
1742 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1743 "job-media-sheets-completed", 0);
1744 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1747 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1748 attr
->values
[0].integer
= 0;
1750 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1752 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1753 IPP_TAG_KEYWORD
)) == NULL
)
1754 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1757 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1758 printer
->options
)) == NULL
)
1761 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1762 "job-hold-until", NULL
, val
);
1765 if (printer
->holding_new_jobs
)
1768 * Hold all new jobs on this printer...
1771 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1772 cupsdSetJobHoldUntil(job
, ippGetString(attr
, 0, NULL
), 0);
1774 cupsdSetJobHoldUntil(job
, "indefinite", 0);
1776 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1777 job
->state_value
= IPP_JOB_HELD
;
1779 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-held-on-create");
1781 else if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1784 * Hold job until specified time...
1787 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
1789 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1790 job
->state_value
= IPP_JOB_HELD
;
1792 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
1794 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1796 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
1797 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1798 job
->state_value
= IPP_JOB_HELD
;
1802 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1803 job
->state_value
= IPP_JOB_PENDING
;
1805 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1808 if (!(printer
->type
& CUPS_PRINTER_REMOTE
) || Classification
)
1811 * Add job sheets options...
1814 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1815 IPP_TAG_ZERO
)) == NULL
)
1817 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1818 "Adding default job-sheets values \"%s,%s\"...",
1819 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1821 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1823 ippSetString(job
->attrs
, &attr
, 0, printer
->job_sheets
[0]);
1824 ippSetString(job
->attrs
, &attr
, 1, printer
->job_sheets
[1]);
1827 job
->job_sheets
= attr
;
1830 * Enforce classification level if set...
1835 cupsdLogMessage(CUPSD_LOG_INFO
,
1836 "Classification=\"%s\", ClassifyOverride=%d",
1837 Classification
? Classification
: "(null)",
1840 if (ClassifyOverride
)
1842 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1843 (attr
->num_values
== 1 ||
1844 !strcmp(attr
->values
[1].string
.text
, "none")))
1847 * Force the leading banner to have the classification on it...
1850 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1852 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1853 "job-sheets=\"%s,none\", "
1854 "job-originating-user-name=\"%s\"",
1855 Classification
, job
->username
);
1857 else if (attr
->num_values
== 2 &&
1858 strcmp(attr
->values
[0].string
.text
,
1859 attr
->values
[1].string
.text
) &&
1860 strcmp(attr
->values
[0].string
.text
, "none") &&
1861 strcmp(attr
->values
[1].string
.text
, "none"))
1864 * Can't put two different security markings on the same document!
1867 ippSetString(job
->attrs
, &attr
, 1, attr
->values
[0].string
.text
);
1869 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1870 "job-sheets=\"%s,%s\", "
1871 "job-originating-user-name=\"%s\"",
1872 attr
->values
[0].string
.text
,
1873 attr
->values
[1].string
.text
, job
->username
);
1875 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1876 strcmp(attr
->values
[0].string
.text
, "none") &&
1877 (attr
->num_values
== 1 ||
1878 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1879 strcmp(attr
->values
[1].string
.text
, "none"))))
1881 if (attr
->num_values
== 1)
1882 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1883 "CLASSIFICATION OVERRIDDEN "
1884 "job-sheets=\"%s\", "
1885 "job-originating-user-name=\"%s\"",
1886 attr
->values
[0].string
.text
, job
->username
);
1888 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1889 "CLASSIFICATION OVERRIDDEN "
1890 "job-sheets=\"%s,%s\",fffff "
1891 "job-originating-user-name=\"%s\"",
1892 attr
->values
[0].string
.text
,
1893 attr
->values
[1].string
.text
, job
->username
);
1896 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1897 (attr
->num_values
== 1 ||
1898 strcmp(attr
->values
[1].string
.text
, Classification
)))
1901 * Force the banner to have the classification on it...
1904 if (attr
->num_values
> 1 &&
1905 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1907 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1908 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1912 if (attr
->num_values
== 1 ||
1913 strcmp(attr
->values
[0].string
.text
, "none"))
1914 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1916 if (attr
->num_values
> 1 &&
1917 strcmp(attr
->values
[1].string
.text
, "none"))
1918 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1921 if (attr
->num_values
> 1)
1922 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1923 "CLASSIFICATION FORCED "
1924 "job-sheets=\"%s,%s\", "
1925 "job-originating-user-name=\"%s\"",
1926 attr
->values
[0].string
.text
,
1927 attr
->values
[1].string
.text
, job
->username
);
1929 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1930 "CLASSIFICATION FORCED "
1931 "job-sheets=\"%s\", "
1932 "job-originating-user-name=\"%s\"",
1933 Classification
, job
->username
);
1938 * See if we need to add the starting sheet...
1941 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1943 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1944 attr
->values
[0].string
.text
);
1946 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1948 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1949 "Aborting job because the start banner could not be "
1954 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1957 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1958 IPP_TAG_ZERO
)) != NULL
)
1959 job
->job_sheets
= attr
;
1962 * Fill in the response info...
1965 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
1966 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
1967 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1970 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1972 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1974 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_TEXT
, "job-state-message", NULL
, "");
1975 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
1976 NULL
, job
->reasons
->values
[0].string
.text
);
1978 con
->response
->request
.status
.status_code
= IPP_OK
;
1981 * Add any job subscriptions...
1984 add_job_subscriptions(con
, job
);
1987 * Set all but the first two attributes to the job attributes group...
1990 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1991 attr
->group_tag
= IPP_TAG_JOB
;
1994 * Fire the "job created" event...
1997 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
2000 * Return the new job...
2008 * 'add_job_subscriptions()' - Add any subscriptions for a job.
2012 add_job_subscriptions(
2013 cupsd_client_t
*con
, /* I - Client connection */
2014 cupsd_job_t
*job
) /* I - Newly created job */
2016 int i
; /* Looping var */
2017 ipp_attribute_t
*prev
, /* Previous attribute */
2018 *next
, /* Next attribute */
2019 *attr
; /* Current attribute */
2020 cupsd_subscription_t
*sub
; /* Subscription object */
2021 const char *recipient
, /* notify-recipient-uri */
2022 *pullmethod
; /* notify-pull-method */
2023 ipp_attribute_t
*user_data
; /* notify-user-data */
2024 int interval
; /* notify-time-interval */
2025 unsigned mask
; /* notify-events */
2029 * Find the first subscription group attribute; return if we have
2033 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
2034 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
2041 * Process the subscription attributes in the request...
2050 mask
= CUPSD_EVENT_NONE
;
2052 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
2054 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
2055 attr
->value_tag
== IPP_TAG_URI
)
2058 * Validate the recipient scheme against the ServerBin/notifier
2062 char notifier
[1024], /* Notifier filename */
2063 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2064 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2065 host
[HTTP_MAX_URI
], /* Host portion of URI */
2066 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2067 int port
; /* Port portion of URI */
2070 recipient
= attr
->values
[0].string
.text
;
2072 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
2073 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2074 host
, sizeof(host
), &port
,
2075 resource
, sizeof(resource
)) < HTTP_URI_OK
)
2077 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2078 _("Bad notify-recipient-uri \"%s\"."), recipient
);
2079 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2080 "notify-status-code", IPP_URI_SCHEME
);
2084 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
2086 if (access(notifier
, X_OK
))
2088 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2089 _("notify-recipient-uri URI \"%s\" uses unknown "
2090 "scheme."), recipient
);
2091 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2092 "notify-status-code", IPP_URI_SCHEME
);
2096 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2098 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2099 _("notify-recipient-uri URI \"%s\" is already used."),
2101 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2102 "notify-status-code", IPP_ATTRIBUTES
);
2106 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2107 attr
->value_tag
== IPP_TAG_KEYWORD
)
2109 pullmethod
= attr
->values
[0].string
.text
;
2111 if (strcmp(pullmethod
, "ippget"))
2113 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2114 _("Bad notify-pull-method \"%s\"."), pullmethod
);
2115 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2116 "notify-status-code", IPP_ATTRIBUTES
);
2120 else if (!strcmp(attr
->name
, "notify-charset") &&
2121 attr
->value_tag
== IPP_TAG_CHARSET
&&
2122 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2123 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2125 send_ipp_status(con
, IPP_CHARSET
,
2126 _("Character set \"%s\" not supported."),
2127 attr
->values
[0].string
.text
);
2130 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2131 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2132 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2134 send_ipp_status(con
, IPP_CHARSET
,
2135 _("Language \"%s\" not supported."),
2136 attr
->values
[0].string
.text
);
2139 else if (!strcmp(attr
->name
, "notify-user-data") &&
2140 attr
->value_tag
== IPP_TAG_STRING
)
2142 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2144 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2145 _("The notify-user-data value is too large "
2146 "(%d > 63 octets)."),
2147 attr
->values
[0].unknown
.length
);
2153 else if (!strcmp(attr
->name
, "notify-events") &&
2154 attr
->value_tag
== IPP_TAG_KEYWORD
)
2156 for (i
= 0; i
< attr
->num_values
; i
++)
2157 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2159 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2161 send_ipp_status(con
, IPP_BAD_REQUEST
,
2162 _("The notify-lease-duration attribute cannot be "
2163 "used with job subscriptions."));
2166 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2167 attr
->value_tag
== IPP_TAG_INTEGER
)
2168 interval
= attr
->values
[0].integer
;
2173 if (!recipient
&& !pullmethod
)
2176 if (mask
== CUPSD_EVENT_NONE
)
2177 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2179 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2180 recipient
, 0)) != NULL
)
2182 sub
->interval
= interval
;
2184 cupsdSetString(&sub
->owner
, job
->username
);
2188 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2189 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2190 (size_t)sub
->user_data_len
);
2193 ippAddSeparator(con
->response
);
2194 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2195 "notify-subscription-id", sub
->id
);
2197 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
2205 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2208 * Remove all of the subscription attributes from the job request...
2210 * TODO: Optimize this since subscription groups have to come at the
2211 * end of the request...
2214 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2218 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2219 attr
->group_tag
== IPP_TAG_ZERO
)
2222 * Free and remove this attribute...
2225 ippDeleteAttribute(NULL
, attr
);
2230 job
->attrs
->attrs
= next
;
2236 job
->attrs
->last
= prev
;
2237 job
->attrs
->current
= prev
;
2242 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2244 * See RFC 4122 for the definition of UUIDs and the format.
2248 add_job_uuid(cupsd_job_t
*job
) /* I - Job */
2250 char uuid
[64]; /* job-uuid string */
2254 * Add a job-uuid attribute if none exists...
2257 if (!ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2258 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
,
2259 httpAssembleUUID(ServerName
, RemotePort
, job
->dest
, job
->id
,
2260 uuid
, sizeof(uuid
)));
2265 * 'add_printer()' - Add a printer to the system.
2269 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2270 ipp_attribute_t
*uri
) /* I - URI of printer */
2272 http_status_t status
; /* Policy status */
2273 int i
; /* Looping var */
2274 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2275 username
[HTTP_MAX_URI
], /* Username portion of URI */
2276 host
[HTTP_MAX_URI
], /* Host portion of URI */
2277 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2278 int port
; /* Port portion of URI */
2279 cupsd_printer_t
*printer
; /* Printer/class */
2280 ipp_attribute_t
*attr
; /* Printer attribute */
2281 cups_file_t
*fp
; /* Script/PPD file */
2282 char line
[1024]; /* Line from file... */
2283 char srcfile
[1024], /* Source Script/PPD file */
2284 dstfile
[1024]; /* Destination Script/PPD file */
2285 int modify
; /* Non-zero if we are modifying */
2286 int changed_driver
, /* Changed the PPD? */
2287 need_restart_job
, /* Need to restart job? */
2288 set_device_uri
, /* Did we set the device URI? */
2289 set_port_monitor
; /* Did we set the port monitor? */
2292 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2293 con
->number
, uri
->values
[0].string
.text
);
2296 * Do we have a valid URI?
2299 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2300 sizeof(scheme
), username
, sizeof(username
), host
,
2301 sizeof(host
), &port
, resource
, sizeof(resource
));
2303 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2306 * No, return an error...
2309 send_ipp_status(con
, IPP_BAD_REQUEST
,
2310 _("The printer-uri must be of the form "
2311 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2316 * Do we have a valid printer name?
2319 if (!validate_name(resource
+ 10))
2322 * No, return an error...
2325 send_ipp_status(con
, IPP_BAD_REQUEST
,
2326 _("The printer-uri \"%s\" contains invalid characters."),
2327 uri
->values
[0].string
.text
);
2332 * See if the printer already exists; if not, create a new printer...
2335 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2338 * Printer doesn't exist; see if we have a class of the same name...
2341 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
)
2344 * Yes, return an error...
2347 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2348 _("A class named \"%s\" already exists."),
2354 * No, check the default policy then add the printer...
2357 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2359 send_http_error(con
, status
, NULL
);
2363 printer
= cupsdAddPrinter(resource
+ 10);
2366 printer
->printer_id
= NextPrinterId
++;
2368 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2371 send_http_error(con
, status
, printer
);
2378 * Look for attributes and copy them over as needed...
2382 need_restart_job
= 0;
2384 if ((attr
= ippFindAttribute(con
->request
, "printer-is-temporary", IPP_TAG_BOOLEAN
)) != NULL
)
2385 printer
->temporary
= ippGetBoolean(attr
, 0);
2387 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2388 IPP_TAG_TEXT
)) != NULL
)
2389 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2391 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
2392 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
2394 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
2395 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
2397 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
2398 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
2400 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2401 IPP_TAG_TEXT
)) != NULL
)
2402 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2406 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2407 IPP_TAG_URI
)) != NULL
)
2410 * Do we have a valid device URI?
2413 http_uri_status_t uri_status
; /* URI separation status */
2414 char old_device_uri
[1024];
2415 /* Old device URI */
2417 need_restart_job
= 1;
2419 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2420 attr
->values
[0].string
.text
,
2421 scheme
, sizeof(scheme
),
2422 username
, sizeof(username
),
2423 host
, sizeof(host
), &port
,
2424 resource
, sizeof(resource
));
2426 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s device-uri: %s", printer
->name
, httpURIStatusString(uri_status
));
2428 if (uri_status
< HTTP_URI_OK
)
2430 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"."),
2431 attr
->values
[0].string
.text
);
2433 cupsdDeletePrinter(printer
, 0);
2438 if (!strcmp(scheme
, "file"))
2441 * See if the administrator has enabled file devices...
2444 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2447 * File devices are disabled and the URL is not file:/dev/null...
2450 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2451 _("File device URIs have been disabled. "
2452 "To enable, see the FileDevice directive in "
2453 "\"%s/cups-files.conf\"."),
2456 cupsdDeletePrinter(printer
, 0);
2464 * See if the backend exists and is executable...
2467 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2468 if (access(srcfile
, X_OK
))
2471 * Could not find device in list!
2474 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2475 _("Bad device-uri scheme \"%s\"."), scheme
);
2477 cupsdDeletePrinter(printer
, 0);
2483 if (printer
->sanitized_device_uri
)
2484 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2485 sizeof(old_device_uri
));
2487 old_device_uri
[0] = '\0';
2489 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2491 cupsdLogMessage(CUPSD_LOG_INFO
,
2492 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2493 printer
->name
, printer
->sanitized_device_uri
,
2499 set_port_monitor
= 0;
2501 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2502 IPP_TAG_NAME
)) != NULL
)
2504 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2507 need_restart_job
= 1;
2509 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2513 for (i
= 0; i
< supported
->num_values
; i
++)
2514 if (!strcmp(supported
->values
[i
].string
.text
,
2515 attr
->values
[0].string
.text
))
2519 if (!supported
|| i
>= supported
->num_values
)
2521 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"."),
2522 attr
->values
[0].string
.text
);
2524 cupsdDeletePrinter(printer
, 0);
2529 cupsdLogMessage(CUPSD_LOG_INFO
,
2530 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2531 printer
->name
, attr
->values
[0].string
.text
,
2532 printer
->port_monitor
? printer
->port_monitor
: "none");
2534 if (strcmp(attr
->values
[0].string
.text
, "none"))
2535 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2537 cupsdClearString(&printer
->port_monitor
);
2539 set_port_monitor
= 1;
2542 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2543 IPP_TAG_BOOLEAN
)) != NULL
&&
2544 attr
->values
[0].boolean
!= printer
->accepting
)
2546 cupsdLogMessage(CUPSD_LOG_INFO
,
2547 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2548 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2550 printer
->accepting
= attr
->values
[0].boolean
;
2552 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2553 "%s accepting jobs.",
2554 printer
->accepting
? "Now" : "No longer");
2557 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared", IPP_TAG_BOOLEAN
)) != NULL
)
2559 if (ippGetBoolean(attr
, 0) &&
2560 printer
->num_auth_info_required
== 1 &&
2561 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2563 send_ipp_status(con
, IPP_BAD_REQUEST
,
2564 _("Cannot share a remote Kerberized printer."));
2566 cupsdDeletePrinter(printer
, 0);
2571 if (printer
->type
& CUPS_PRINTER_REMOTE
)
2574 * Cannot re-share remote printers.
2577 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Cannot change printer-is-shared for remote queues."));
2579 cupsdDeletePrinter(printer
, 0);
2584 if (printer
->shared
&& !ippGetBoolean(attr
, 0))
2585 cupsdDeregisterPrinter(printer
, 1);
2587 cupsdLogMessage(CUPSD_LOG_INFO
,
2588 "Setting %s printer-is-shared to %d (was %d.)",
2589 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2591 printer
->shared
= ippGetBoolean(attr
, 0);
2592 if (printer
->shared
&& printer
->temporary
)
2593 printer
->temporary
= 0;
2596 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2597 IPP_TAG_ENUM
)) != NULL
)
2599 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2600 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2602 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d."),
2603 attr
->values
[0].integer
);
2605 cupsdDeletePrinter(printer
, 0);
2610 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2611 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2613 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2614 cupsdStopPrinter(printer
, 0);
2617 need_restart_job
= 1;
2618 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2622 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2623 IPP_TAG_TEXT
)) != NULL
)
2625 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2626 sizeof(printer
->state_message
));
2628 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
, "%s",
2629 printer
->state_message
);
2632 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2633 IPP_TAG_KEYWORD
)) != NULL
)
2635 if (attr
->num_values
>
2636 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2638 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2639 _("Too many printer-state-reasons values (%d > %d)."),
2641 (int)(sizeof(printer
->reasons
) /
2642 sizeof(printer
->reasons
[0])));
2644 cupsdDeletePrinter(printer
, 0);
2649 for (i
= 0; i
< printer
->num_reasons
; i
++)
2650 _cupsStrFree(printer
->reasons
[i
]);
2652 printer
->num_reasons
= 0;
2653 for (i
= 0; i
< attr
->num_values
; i
++)
2655 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2658 printer
->reasons
[printer
->num_reasons
] =
2659 _cupsStrRetain(attr
->values
[i
].string
.text
);
2660 printer
->num_reasons
++;
2662 if (!strcmp(attr
->values
[i
].string
.text
, "paused") &&
2663 printer
->state
!= IPP_PRINTER_STOPPED
)
2665 cupsdLogMessage(CUPSD_LOG_INFO
,
2666 "Setting %s printer-state to %d (was %d.)",
2667 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2668 cupsdStopPrinter(printer
, 0);
2672 if (PrintcapFormat
== PRINTCAP_PLIST
)
2673 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2675 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2676 "Printer \"%s\" state changed.", printer
->name
);
2679 if (!set_printer_defaults(con
, printer
))
2682 cupsdDeletePrinter(printer
, 0);
2687 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2688 IPP_TAG_KEYWORD
)) != NULL
)
2689 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2692 * See if we have all required attributes...
2695 if (!printer
->device_uri
)
2696 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2699 * See if we have a PPD file attached to the request...
2704 need_restart_job
= 1;
2707 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2709 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2712 * Yes; get the first line from it...
2716 cupsFileGets(fp
, line
, sizeof(line
));
2720 * Then see what kind of file it is...
2723 if (strncmp(line
, "*PPD-Adobe", 10))
2725 send_ipp_status(con
, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED
, _("Bad PPD file."));
2727 cupsdDeletePrinter(printer
, 0);
2732 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2736 * The new file is a PPD file, so move the file over to the ppd
2740 if (copy_file(srcfile
, dstfile
, ConfigFilePerm
))
2742 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file - %s"), strerror(errno
));
2744 cupsdDeletePrinter(printer
, 0);
2749 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2752 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
)) != NULL
)
2754 const char *ppd_name
= ippGetString(attr
, 0, NULL
);
2755 /* ppd-name value */
2757 need_restart_job
= 1;
2760 if (!strcmp(ppd_name
, "raw"))
2763 * Raw driver, remove any existing PPD file.
2766 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2769 else if (strstr(ppd_name
, "../"))
2771 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
2773 cupsdDeletePrinter(printer
, 0);
2783 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2785 if (copy_model(con
, ppd_name
, dstfile
))
2787 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file."));
2789 cupsdDeletePrinter(printer
, 0);
2794 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2801 * If we changed the PPD, then remove the printer's cache file and clear the
2802 * printer-state-reasons...
2805 char cache_name
[1024]; /* Cache filename for printer attrs */
2807 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.data", CacheDir
, printer
->name
);
2810 cupsdSetPrinterReasons(printer
, "none");
2813 * (Re)register color profiles...
2816 cupsdRegisterColor(printer
);
2820 * If we set the device URI but not the port monitor, check which port
2821 * monitor to use by default...
2824 if (set_device_uri
&& !set_port_monitor
)
2826 ppd_file_t
*ppd
; /* PPD file */
2827 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2830 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2831 sizeof(scheme
), username
, sizeof(username
), host
,
2832 sizeof(host
), &port
, resource
, sizeof(resource
));
2834 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2836 if ((ppd
= _ppdOpenFile(srcfile
, _PPD_LOCALIZATION_NONE
)) != NULL
)
2838 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2840 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2841 if (!strcmp(scheme
, ppdattr
->spec
))
2843 cupsdLogMessage(CUPSD_LOG_INFO
,
2844 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2845 printer
->name
, ppdattr
->value
,
2846 printer
->port_monitor
? printer
->port_monitor
2849 if (strcmp(ppdattr
->value
, "none"))
2850 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2852 cupsdClearString(&printer
->port_monitor
);
2861 printer
->config_time
= time(NULL
);
2864 * Update the printer attributes and return...
2867 if (!printer
->temporary
)
2869 if (!printer
->printer_id
)
2870 printer
->printer_id
= NextPrinterId
++;
2872 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2875 cupsdSetPrinterAttrs(printer
);
2877 if (need_restart_job
&& printer
->job
)
2880 * Restart the current job...
2883 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
2884 "Job restarted because the printer was modified.");
2887 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2891 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
2892 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
2893 printer
->name
, get_username(con
));
2895 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2896 printer
->name
, get_username(con
));
2900 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
2901 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
2902 printer
->name
, get_username(con
));
2904 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2905 printer
->name
, get_username(con
));
2908 con
->response
->request
.status
.status_code
= IPP_OK
;
2913 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2914 * based upon the printer state...
2918 add_printer_state_reasons(
2919 cupsd_client_t
*con
, /* I - Client connection */
2920 cupsd_printer_t
*p
) /* I - Printer info */
2922 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2923 "add_printer_state_reasons(%p[%d], %p[%s])",
2924 con
, con
->number
, p
, p
->name
);
2926 if (p
->num_reasons
== 0)
2927 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2928 "printer-state-reasons", NULL
, "none");
2930 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2931 "printer-state-reasons", p
->num_reasons
, NULL
,
2932 (const char * const *)p
->reasons
);
2937 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2938 * the specified printer or class.
2942 add_queued_job_count(
2943 cupsd_client_t
*con
, /* I - Client connection */
2944 cupsd_printer_t
*p
) /* I - Printer or class */
2946 int count
; /* Number of jobs on destination */
2949 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2950 con
, con
->number
, p
, p
->name
);
2952 count
= cupsdGetPrinterJobCount(p
->name
);
2954 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2955 "queued-job-count", count
);
2960 * 'apply_printer_defaults()' - Apply printer default options to a job.
2964 apply_printer_defaults(
2965 cupsd_printer_t
*printer
, /* I - Printer */
2966 cupsd_job_t
*job
) /* I - Job */
2968 int i
, /* Looping var */
2969 num_options
; /* Number of default options */
2970 cups_option_t
*options
, /* Default options */
2971 *option
; /* Current option */
2974 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Applying default options...");
2977 * Collect all of the default options and add the missing ones to the
2981 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
2982 option
= printer
->options
;
2985 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2987 if (!strcmp(option
->name
, "print-quality") && ippFindAttribute(job
->attrs
, "cupsPrintQuality", IPP_TAG_NAME
))
2988 continue; /* Don't override cupsPrintQuality */
2990 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Adding default %s=%s", option
->name
, option
->value
);
2992 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
, &options
);
2996 * Encode these options as attributes in the job object...
2999 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3000 cupsFreeOptions(num_options
, options
);
3005 * 'authenticate_job()' - Set job authentication info.
3009 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3010 ipp_attribute_t
*uri
) /* I - Job URI */
3012 ipp_attribute_t
*attr
, /* job-id attribute */
3013 *auth_info
; /* auth-info attribute */
3014 int jobid
; /* Job ID */
3015 cupsd_job_t
*job
; /* Current job */
3016 char scheme
[HTTP_MAX_URI
],
3017 /* Method portion of URI */
3018 username
[HTTP_MAX_URI
],
3019 /* Username portion of URI */
3021 /* Host portion of URI */
3022 resource
[HTTP_MAX_URI
];
3023 /* Resource portion of URI */
3024 int port
; /* Port portion of URI */
3027 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3028 con
, con
->number
, uri
->values
[0].string
.text
);
3031 * Start with "everything is OK" status...
3034 con
->response
->request
.status
.status_code
= IPP_OK
;
3037 * See if we have a job URI or a printer URI...
3040 if (!strcmp(uri
->name
, "printer-uri"))
3043 * Got a printer URI; see if we also have a job-id attribute...
3046 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3047 IPP_TAG_INTEGER
)) == NULL
)
3049 send_ipp_status(con
, IPP_BAD_REQUEST
,
3050 _("Got a printer-uri attribute but no job-id."));
3054 jobid
= attr
->values
[0].integer
;
3059 * Got a job URI; parse it to get the job ID...
3062 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3063 sizeof(scheme
), username
, sizeof(username
), host
,
3064 sizeof(host
), &port
, resource
, sizeof(resource
));
3066 if (strncmp(resource
, "/jobs/", 6))
3072 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3073 uri
->values
[0].string
.text
);
3077 jobid
= atoi(resource
+ 6);
3081 * See if the job exists...
3084 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3087 * Nope - return a "not found" error...
3090 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3095 * See if the job has been completed...
3098 if (job
->state_value
!= IPP_JOB_HELD
)
3101 * Return a "not-possible" error...
3104 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3105 _("Job #%d is not held for authentication."),
3111 * See if we have already authenticated...
3114 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3116 if (!con
->username
[0] && !auth_info
)
3118 cupsd_printer_t
*printer
; /* Job destination */
3121 * No auth data. If we need to authenticate via Kerberos, send a
3122 * HTTP auth challenge, otherwise just return an IPP error...
3125 printer
= cupsdFindDest(job
->dest
);
3127 if (printer
&& printer
->num_auth_info_required
> 0 &&
3128 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3129 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3131 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3132 _("No authentication information provided."));
3137 * See if the job is owned by the requesting user...
3140 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3142 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3143 cupsdFindDest(job
->dest
));
3148 * Save the authentication information for this job...
3151 save_auth_info(con
, job
, auth_info
);
3154 * Reset the job-hold-until value to "no-hold"...
3157 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3158 IPP_TAG_KEYWORD
)) == NULL
)
3159 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3163 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
3164 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
3168 * Release the job and return...
3171 cupsdReleaseJob(job
);
3173 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, NULL
, job
, "Job authenticated by user");
3175 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3182 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3186 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3187 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3189 int i
; /* Looping var */
3190 http_status_t status
; /* Policy status */
3191 cups_ptype_t dtype
; /* Destination type */
3192 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3193 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3194 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3195 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3196 int port
; /* Port portion of URI */
3197 ipp_attribute_t
*attr
; /* Attribute in request */
3198 const char *username
= NULL
; /* Username */
3199 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3201 cupsd_printer_t
*printer
; /* Printer */
3202 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3203 cupsd_job_t
*job
; /* Job */
3206 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3207 con
->number
, uri
->values
[0].string
.text
);
3210 * Get the jobs to cancel/purge...
3213 switch (con
->request
->request
.op
.operation_id
)
3215 case IPP_PURGE_JOBS
:
3217 * Get the username (if any) for the jobs we want to cancel (only if
3218 * "my-jobs" is specified...
3221 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3222 IPP_TAG_BOOLEAN
)) != NULL
&&
3223 attr
->values
[0].boolean
)
3225 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3226 IPP_TAG_NAME
)) != NULL
)
3227 username
= attr
->values
[0].string
.text
;
3230 send_ipp_status(con
, IPP_BAD_REQUEST
,
3231 _("Missing requesting-user-name attribute."));
3237 * Look for the "purge-jobs" attribute...
3240 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3241 IPP_TAG_BOOLEAN
)) != NULL
)
3242 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3244 purge
= CUPSD_JOB_PURGE
;
3247 case IPP_CANCEL_MY_JOBS
:
3248 if (con
->username
[0])
3249 username
= con
->username
;
3250 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3251 IPP_TAG_NAME
)) != NULL
)
3252 username
= attr
->values
[0].string
.text
;
3255 send_ipp_status(con
, IPP_BAD_REQUEST
,
3256 _("Missing requesting-user-name attribute."));
3264 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3267 * See if we have a printer URI...
3270 if (strcmp(uri
->name
, "printer-uri"))
3272 send_ipp_status(con
, IPP_BAD_REQUEST
,
3273 _("The printer-uri attribute is required."));
3278 * And if the destination is valid...
3281 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3287 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3288 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3289 hostname
, sizeof(hostname
), &port
,
3290 resource
, sizeof(resource
));
3292 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3293 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3295 send_ipp_status(con
, IPP_NOT_FOUND
,
3296 _("The printer or class does not exist."));
3304 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3306 send_http_error(con
, status
, NULL
);
3312 for (i
= 0; i
< job_ids
->num_values
; i
++)
3314 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
3317 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3318 _cups_strcasecmp(job
->username
, username
))
3322 if (i
< job_ids
->num_values
)
3324 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3325 job_ids
->values
[i
].integer
);
3329 for (i
= 0; i
< job_ids
->num_values
; i
++)
3331 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3333 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3334 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3335 "Job canceled by user.");
3338 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3339 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3345 * Cancel all jobs on all printers...
3348 cupsdCancelJobs(NULL
, username
, purge
);
3350 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3351 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3361 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3364 send_http_error(con
, status
, printer
);
3370 for (i
= 0; i
< job_ids
->num_values
; i
++)
3372 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
3373 _cups_strcasecmp(job
->dest
, printer
->name
))
3376 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3377 _cups_strcasecmp(job
->username
, username
))
3381 if (i
< job_ids
->num_values
)
3383 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3384 job_ids
->values
[i
].integer
);
3388 for (i
= 0; i
< job_ids
->num_values
; i
++)
3390 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3392 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3393 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3394 "Job canceled by user.");
3397 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3398 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3404 * Cancel all of the jobs on the named printer...
3407 cupsdCancelJobs(printer
->name
, username
, purge
);
3409 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3411 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3416 con
->response
->request
.status
.status_code
= IPP_OK
;
3423 * 'cancel_job()' - Cancel a print job.
3427 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3428 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3430 ipp_attribute_t
*attr
; /* Current attribute */
3431 int jobid
; /* Job ID */
3432 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3433 username
[HTTP_MAX_URI
], /* Username portion of URI */
3434 host
[HTTP_MAX_URI
], /* Host portion of URI */
3435 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3436 int port
; /* Port portion of URI */
3437 cupsd_job_t
*job
; /* Job information */
3438 cups_ptype_t dtype
; /* Destination type (printer/class) */
3439 cupsd_printer_t
*printer
; /* Printer data */
3440 cupsd_jobaction_t purge
; /* Purge the job? */
3443 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3444 con
->number
, uri
->values
[0].string
.text
);
3447 * See if we have a job URI or a printer URI...
3450 if (!strcmp(uri
->name
, "printer-uri"))
3453 * Got a printer URI; see if we also have a job-id attribute...
3456 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3457 IPP_TAG_INTEGER
)) == NULL
)
3459 send_ipp_status(con
, IPP_BAD_REQUEST
,
3460 _("Got a printer-uri attribute but no job-id."));
3464 if ((jobid
= attr
->values
[0].integer
) == 0)
3467 * Find the current job on the specified printer...
3470 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3476 send_ipp_status(con
, IPP_NOT_FOUND
,
3477 _("The printer or class does not exist."));
3482 * See if there are any pending jobs...
3485 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3487 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3488 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3489 !_cups_strcasecmp(job
->dest
, printer
->name
))
3497 * No, try stopped jobs...
3500 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3502 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3503 if (job
->state_value
== IPP_JOB_STOPPED
&&
3504 !_cups_strcasecmp(job
->dest
, printer
->name
))
3511 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3521 * Got a job URI; parse it to get the job ID...
3524 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3525 sizeof(scheme
), username
, sizeof(username
), host
,
3526 sizeof(host
), &port
, resource
, sizeof(resource
));
3528 if (strncmp(resource
, "/jobs/", 6))
3534 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3535 uri
->values
[0].string
.text
);
3539 jobid
= atoi(resource
+ 6);
3543 * Look for the "purge-job" attribute...
3546 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3547 IPP_TAG_BOOLEAN
)) != NULL
)
3548 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3550 purge
= CUPSD_JOB_DEFAULT
;
3553 * See if the job exists...
3556 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3559 * Nope - return a "not found" error...
3562 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3567 * See if the job is owned by the requesting user...
3570 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3572 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3573 cupsdFindDest(job
->dest
));
3578 * See if the job is already completed, canceled, or aborted; if so,
3579 * we can't cancel...
3582 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3584 switch (job
->state_value
)
3586 case IPP_JOB_CANCELED
:
3587 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3588 _("Job #%d is already canceled - can\'t cancel."),
3592 case IPP_JOB_ABORTED
:
3593 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3594 _("Job #%d is already aborted - can\'t cancel."),
3599 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3600 _("Job #%d is already completed - can\'t cancel."),
3609 * Cancel the job and return...
3612 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3613 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3614 "Job canceled by \"%s\"",
3618 if (purge
== CUPSD_JOB_PURGE
)
3619 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3622 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3625 con
->response
->request
.status
.status_code
= IPP_OK
;
3630 * 'cancel_subscription()' - Cancel a subscription.
3634 cancel_subscription(
3635 cupsd_client_t
*con
, /* I - Client connection */
3636 int sub_id
) /* I - Subscription ID */
3638 http_status_t status
; /* Policy status */
3639 cupsd_subscription_t
*sub
; /* Subscription */
3642 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3643 "cancel_subscription(con=%p[%d], sub_id=%d)",
3644 con
, con
->number
, sub_id
);
3647 * Is the subscription ID valid?
3650 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3653 * Bad subscription ID...
3656 send_ipp_status(con
, IPP_NOT_FOUND
,
3657 _("Subscription #%d does not exist."), sub_id
);
3665 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3667 con
, sub
->owner
)) != HTTP_OK
)
3669 send_http_error(con
, status
, sub
->dest
);
3674 * Cancel the subscription...
3677 cupsdDeleteSubscription(sub
, 1);
3679 con
->response
->request
.status
.status_code
= IPP_OK
;
3684 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3687 static int /* O - 1 if OK, 0 if not */
3688 check_rss_recipient(
3689 const char *recipient
) /* I - Recipient URI */
3691 cupsd_subscription_t
*sub
; /* Current subscription */
3694 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3696 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3700 * Compare the URIs up to the first ?...
3703 const char *r1
, *r2
;
3705 for (r1
= recipient
, r2
= sub
->recipient
;
3706 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3718 * 'check_quotas()' - Check quotas for a printer and user.
3721 static int /* O - 1 if OK, 0 if forbidden,
3722 -1 if limit reached */
3723 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3724 cupsd_printer_t
*p
) /* I - Printer or class */
3726 char username
[33], /* Username */
3727 *name
; /* Current user name */
3728 cupsd_quota_t
*q
; /* Quota data */
3729 #ifdef HAVE_MBR_UID_TO_UUID
3731 * Use Apple membership APIs which require that all names represent
3732 * valid user account or group records accessible by the server.
3735 uuid_t usr_uuid
; /* UUID for job requesting user */
3736 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3737 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3738 int mbr_err
; /* Error from membership function */
3739 int is_member
; /* Is this user a member? */
3742 * Use standard POSIX APIs for checking users and groups...
3745 struct passwd
*pw
; /* User password data */
3746 #endif /* HAVE_MBR_UID_TO_UUID */
3749 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3750 con
, con
->number
, p
, p
->name
);
3753 * Figure out who is printing...
3756 strlcpy(username
, get_username(con
), sizeof(username
));
3758 if ((name
= strchr(username
, '@')) != NULL
)
3759 *name
= '\0'; /* Strip @REALM */
3762 * Check global active job limits for printers and users...
3765 if (MaxJobsPerPrinter
)
3768 * Check if there are too many pending jobs on this printer...
3771 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3773 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3782 * Check if there are too many pending jobs for this user...
3785 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3787 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3794 * Check against users...
3797 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3800 if (cupsArrayCount(p
->users
))
3802 #ifdef HAVE_MBR_UID_TO_UUID
3804 * Get UUID for job requesting user...
3807 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3813 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3814 "check_quotas: UUID lookup failed for user \"%s\"",
3816 cupsdLogMessage(CUPSD_LOG_INFO
,
3817 "Denying user \"%s\" access to printer \"%s\" "
3818 "(unknown user)...",
3824 * Get UID and GID of requesting user...
3827 pw
= getpwnam(username
);
3829 #endif /* HAVE_MBR_UID_TO_UUID */
3831 for (name
= (char *)cupsArrayFirst(p
->users
);
3833 name
= (char *)cupsArrayNext(p
->users
))
3837 * Check group membership...
3840 #ifdef HAVE_MBR_UID_TO_UUID
3843 if (uuid_parse(name
+ 2, grp_uuid
))
3844 uuid_clear(grp_uuid
);
3846 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3849 * Invalid ACL entries are ignored for matching; just record a
3850 * warning in the log...
3853 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3854 "check_quotas: UUID lookup failed for ACL entry "
3855 "\"%s\" (err=%d)", name
, mbr_err
);
3856 cupsdLogMessage(CUPSD_LOG_WARN
,
3857 "Access control entry \"%s\" not a valid group name; "
3858 "entry ignored", name
);
3861 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3865 * At this point, there should be no errors, but check anyways...
3868 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3869 "check_quotas: group \"%s\" membership check "
3870 "failed (err=%d)", name
+ 1, mbr_err
);
3875 * Stop if we found a match...
3882 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3884 #endif /* HAVE_MBR_UID_TO_UUID */
3886 #ifdef HAVE_MBR_UID_TO_UUID
3891 if (uuid_parse(name
+ 1, usr2_uuid
))
3892 uuid_clear(usr2_uuid
);
3894 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3897 * Invalid ACL entries are ignored for matching; just record a
3898 * warning in the log...
3901 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3902 "check_quotas: UUID lookup failed for ACL entry "
3903 "\"%s\" (err=%d)", name
, mbr_err
);
3904 cupsdLogMessage(CUPSD_LOG_WARN
,
3905 "Access control entry \"%s\" not a valid user name; "
3906 "entry ignored", name
);
3909 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3913 else if (!_cups_strcasecmp(username
, name
))
3915 #endif /* HAVE_MBR_UID_TO_UUID */
3917 if ((name
!= NULL
) == p
->deny_users
)
3919 cupsdLogMessage(CUPSD_LOG_INFO
,
3920 "Denying user \"%s\" access to printer \"%s\"...",
3930 if (p
->k_limit
|| p
->page_limit
)
3932 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3934 cupsdLogMessage(CUPSD_LOG_ERROR
,
3935 "Unable to allocate quota data for user \"%s\"",
3940 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3941 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3943 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3950 * If we have gotten this far, we're done!
3958 * 'close_job()' - Close a multi-file job.
3962 close_job(cupsd_client_t
*con
, /* I - Client connection */
3963 ipp_attribute_t
*uri
) /* I - Printer URI */
3965 cupsd_job_t
*job
; /* Job */
3966 ipp_attribute_t
*attr
; /* Attribute */
3967 char job_uri
[HTTP_MAX_URI
],
3969 username
[256]; /* User name */
3972 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3973 con
->number
, uri
->values
[0].string
.text
);
3976 * See if we have a job URI or a printer URI...
3979 if (strcmp(uri
->name
, "printer-uri"))
3982 * job-uri is not supported by Close-Job!
3985 send_ipp_status(con
, IPP_BAD_REQUEST
,
3986 _("Close-Job doesn't support the job-uri attribute."));
3991 * Got a printer URI; see if we also have a job-id attribute...
3994 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3995 IPP_TAG_INTEGER
)) == NULL
)
3997 send_ipp_status(con
, IPP_BAD_REQUEST
,
3998 _("Got a printer-uri attribute but no job-id."));
4002 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
4005 * Nope - return a "not found" error...
4008 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
4009 attr
->values
[0].integer
);
4014 * See if the job is owned by the requesting user...
4017 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4019 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
4020 cupsdFindDest(job
->dest
));
4025 * Add any ending sheet...
4028 if (cupsdTimeoutJob(job
))
4031 if (job
->state_value
== IPP_JOB_STOPPED
)
4033 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
4034 job
->state_value
= IPP_JOB_PENDING
;
4036 else if (job
->state_value
== IPP_JOB_HELD
)
4038 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
4039 IPP_TAG_KEYWORD
)) == NULL
)
4040 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
4042 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
4044 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
4045 job
->state_value
= IPP_JOB_PENDING
;
4050 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4053 * Fill in the response info...
4056 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4057 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
4058 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
4061 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4063 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
4066 con
->response
->request
.status
.status_code
= IPP_OK
;
4069 * Start the job if necessary...
4077 * 'copy_attrs()' - Copy attributes from one request to another.
4081 copy_attrs(ipp_t
*to
, /* I - Destination request */
4082 ipp_t
*from
, /* I - Source request */
4083 cups_array_t
*ra
, /* I - Requested attributes */
4084 ipp_tag_t group
, /* I - Group to copy */
4085 int quickcopy
, /* I - Do a quick copy? */
4086 cups_array_t
*exclude
) /* I - Attributes to exclude? */
4088 ipp_attribute_t
*fromattr
; /* Source attribute */
4091 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4092 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4093 to
, from
, ra
, group
, quickcopy
);
4098 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4101 * Filter attributes as needed...
4104 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4105 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4108 if (!strcmp(fromattr
->name
, "document-password") ||
4109 !strcmp(fromattr
->name
, "job-authorization-uri") ||
4110 !strcmp(fromattr
->name
, "job-password") ||
4111 !strcmp(fromattr
->name
, "job-password-encryption") ||
4112 !strcmp(fromattr
->name
, "job-printer-uri"))
4116 (cupsArrayFind(exclude
, fromattr
->name
) ||
4117 cupsArrayFind(exclude
, "all")))
4120 * We need to exclude this attribute for security reasons; we require the
4121 * job-id attribute regardless of the security settings for IPP
4124 * The job-printer-uri attribute is handled by copy_job_attrs().
4126 * Subscription attribute security is handled by copy_subscription_attrs().
4129 if (strcmp(fromattr
->name
, "job-id"))
4133 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4136 * Don't send collection attributes by default to IPP/1.x clients
4137 * since many do not support collections. Also don't send
4138 * media-col-database unless specifically requested by the client.
4141 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4143 (to
->request
.status
.version
[0] == 1 ||
4144 !strcmp(fromattr
->name
, "media-col-database")))
4147 ippCopyAttribute(to
, fromattr
, quickcopy
);
4154 * 'copy_banner()' - Copy a banner file to the requests directory for the
4158 static int /* O - Size of banner file in kbytes */
4159 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4160 cupsd_job_t
*job
, /* I - Job information */
4161 const char *name
) /* I - Name of banner */
4163 int i
; /* Looping var */
4164 int kbytes
; /* Size of banner file in kbytes */
4165 char filename
[1024]; /* Job filename */
4166 cupsd_banner_t
*banner
; /* Pointer to banner */
4167 cups_file_t
*in
; /* Input file */
4168 cups_file_t
*out
; /* Output file */
4169 int ch
; /* Character from file */
4170 char attrname
[255], /* Name of attribute */
4171 *s
; /* Pointer into name */
4172 ipp_attribute_t
*attr
; /* Attribute */
4175 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4176 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4177 con
, con
? con
->number
: -1, job
, job
->id
,
4178 name
? name
: "(null)");
4181 * Find the banner; return if not found or "none"...
4184 if (!name
|| !strcmp(name
, "none") ||
4185 (banner
= cupsdFindBanner(name
)) == NULL
)
4189 * Open the banner and job files...
4192 if (add_file(con
, job
, banner
->filetype
, 0))
4195 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4197 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4199 cupsdLogMessage(CUPSD_LOG_ERROR
,
4200 "Unable to create banner job file %s - %s",
4201 filename
, strerror(errno
));
4206 fchmod(cupsFileNumber(out
), 0640);
4207 fchown(cupsFileNumber(out
), RunUser
, Group
);
4210 * Try the localized banner file under the subdirectory...
4213 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4215 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4218 * Convert ll-cc to ll_CC...
4222 attrname
[3] = (char)toupper(attrname
[3] & 255);
4223 attrname
[4] = (char)toupper(attrname
[4] & 255);
4226 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4229 if (access(filename
, 0) && strlen(attrname
) > 2)
4232 * Wasn't able to find "ll_CC" locale file; try the non-national
4233 * localization banner directory.
4238 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4242 if (access(filename
, 0))
4245 * Use the non-localized banner file.
4248 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4251 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4255 cupsdLogMessage(CUPSD_LOG_ERROR
,
4256 "Unable to open banner template file %s - %s",
4257 filename
, strerror(errno
));
4263 * Parse the file to the end...
4266 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4270 * Get an attribute name...
4273 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4274 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4276 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4286 * Ignore { followed by stuff that is not an attribute name...
4289 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4294 * See if it is defined...
4297 if (attrname
[0] == '?')
4302 if (!strcmp(s
, "printer-name"))
4304 cupsFilePuts(out
, job
->dest
);
4307 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4310 * See if we have a leading question mark...
4313 if (attrname
[0] != '?')
4316 * Nope, write to file as-is; probably a PostScript procedure...
4319 cupsFilePrintf(out
, "{%s}", attrname
);
4326 * Output value(s)...
4329 for (i
= 0; i
< attr
->num_values
; i
++)
4332 cupsFilePutChar(out
, ',');
4334 switch (attr
->value_tag
)
4336 case IPP_TAG_INTEGER
:
4338 if (!strncmp(s
, "time-at-", 8))
4340 struct timeval tv
; /* Time value */
4342 tv
.tv_sec
= attr
->values
[i
].integer
;
4345 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4348 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4351 case IPP_TAG_BOOLEAN
:
4352 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4355 case IPP_TAG_NOVALUE
:
4356 cupsFilePuts(out
, "novalue");
4359 case IPP_TAG_RANGE
:
4360 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4361 attr
->values
[i
].range
.upper
);
4364 case IPP_TAG_RESOLUTION
:
4365 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4366 attr
->values
[i
].resolution
.yres
,
4367 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4372 case IPP_TAG_STRING
:
4375 case IPP_TAG_KEYWORD
:
4376 case IPP_TAG_CHARSET
:
4377 case IPP_TAG_LANGUAGE
:
4378 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4381 * Need to quote strings for PS banners...
4386 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4388 if (*p
== '(' || *p
== ')' || *p
== '\\')
4390 cupsFilePutChar(out
, '\\');
4391 cupsFilePutChar(out
, *p
);
4393 else if (*p
< 32 || *p
> 126)
4394 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4396 cupsFilePutChar(out
, *p
);
4400 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4404 break; /* anti-compiler-warning-code */
4408 else if (ch
== '\\') /* Quoted char */
4410 ch
= cupsFileGetChar(in
);
4412 if (ch
!= '{') /* Only do special handling for \{ */
4413 cupsFilePutChar(out
, '\\');
4415 cupsFilePutChar(out
, ch
);
4418 cupsFilePutChar(out
, ch
);
4422 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4424 job
->koctets
+= kbytes
;
4426 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4427 attr
->values
[0].integer
+= kbytes
;
4436 * 'copy_file()' - Copy a PPD file...
4439 static int /* O - 0 = success, -1 = error */
4440 copy_file(const char *from
, /* I - Source file */
4441 const char *to
, /* I - Destination file */
4442 mode_t mode
) /* I - Permissions */
4444 cups_file_t
*src
, /* Source file */
4445 *dst
; /* Destination file */
4446 int bytes
; /* Bytes to read/write */
4447 char buffer
[2048]; /* Copy buffer */
4450 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4453 * Open the source and destination file for a copy...
4456 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4459 if ((dst
= cupsdCreateConfFile(to
, mode
)) == NULL
)
4466 * Copy the source file to the destination...
4469 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4470 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4478 * Close both files and return...
4483 return (cupsdCloseCreatedConfFile(dst
, to
));
4488 * 'copy_model()' - Copy a PPD model file, substituting default values
4492 static int /* O - 0 = success, -1 = error */
4493 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4494 const char *from
, /* I - Source file */
4495 const char *to
) /* I - Destination file */
4497 fd_set input
; /* select() input set */
4498 struct timeval timeout
; /* select() timeout */
4499 int maxfd
; /* Max file descriptor for select() */
4500 char tempfile
[1024]; /* Temporary PPD file */
4501 int tempfd
; /* Temporary PPD file descriptor */
4502 int temppid
; /* Process ID of cups-driverd */
4503 int temppipe
[2]; /* Temporary pipes */
4504 char *argv
[4], /* Command-line arguments */
4505 *envp
[MAX_ENV
]; /* Environment */
4506 cups_file_t
*src
, /* Source file */
4507 *dst
; /* Destination file */
4508 ppd_file_t
*ppd
; /* PPD file */
4509 int bytes
, /* Bytes from pipe */
4510 total
; /* Total bytes from pipe */
4511 char buffer
[2048]; /* Copy buffer */
4512 int i
; /* Looping var */
4513 char option
[PPD_MAX_NAME
], /* Option name */
4514 choice
[PPD_MAX_NAME
]; /* Choice name */
4515 ppd_size_t
*size
; /* Default size */
4516 int num_defaults
; /* Number of default options */
4517 cups_option_t
*defaults
; /* Default options */
4518 char cups_protocol
[PPD_MAX_LINE
];
4519 /* cupsProtocol attribute */
4522 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_model(con=%p, from=\"%s\", to=\"%s\")", con
, from
, to
);
4525 * Run cups-driverd to get the PPD file...
4528 argv
[0] = "cups-driverd";
4530 argv
[2] = (char *)from
;
4533 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4535 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4536 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4537 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4538 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4541 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4542 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4544 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4545 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4556 * Wait up to 30 seconds for the PPD file to be copied...
4561 if (temppipe
[0] > CGIPipes
[0])
4562 maxfd
= temppipe
[0] + 1;
4564 maxfd
= CGIPipes
[0] + 1;
4569 * See if we have data ready...
4573 FD_SET(temppipe
[0], &input
);
4574 FD_SET(CGIPipes
[0], &input
);
4576 timeout
.tv_sec
= 30;
4577 timeout
.tv_usec
= 0;
4579 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4589 * We have timed out...
4595 if (FD_ISSET(temppipe
[0], &input
))
4598 * Read the PPD file from the pipe, and write it to the PPD file.
4601 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4603 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4612 if (FD_ISSET(CGIPipes
[0], &input
))
4622 * No data from cups-deviced...
4625 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4631 * Open the source file for a copy...
4634 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4641 * Read the source file and see what page sizes are supported...
4644 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4652 * Open the destination (if possible) and set the default options...
4657 cups_protocol
[0] = '\0';
4659 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4662 * Read all of the default lines from the old PPD...
4665 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4666 if (!strncmp(buffer
, "*Default", 8))
4669 * Add the default option...
4672 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4673 choice
, sizeof(choice
)))
4675 ppd_option_t
*ppdo
; /* PPD option */
4679 * Only add the default if the default hasn't already been
4680 * set and the choice exists in the new PPD...
4683 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4684 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4685 ppdFindChoice(ppdo
, choice
))
4686 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4690 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4691 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4695 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4698 * Add the default media sizes...
4701 num_defaults
= cupsAddOption("PageSize", size
->name
,
4702 num_defaults
, &defaults
);
4703 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4704 num_defaults
, &defaults
);
4705 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4706 num_defaults
, &defaults
);
4707 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4708 num_defaults
, &defaults
);
4714 * Open the destination file for a copy...
4717 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4719 cupsFreeOptions(num_defaults
, defaults
);
4726 * Copy the source file to the destination...
4729 cupsFileRewind(src
);
4731 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4733 if (!strncmp(buffer
, "*Default", 8))
4736 * Check for an previous default option choice...
4739 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4740 choice
, sizeof(choice
)))
4742 const char *val
; /* Default option value */
4745 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4748 * Substitute the previous choice...
4751 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4756 cupsFilePrintf(dst
, "%s\n", buffer
);
4759 if (cups_protocol
[0])
4760 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4762 cupsFreeOptions(num_defaults
, defaults
);
4765 * Close both files and return...
4772 return (cupsdCloseCreatedConfFile(dst
, to
));
4777 * 'copy_job_attrs()' - Copy job attributes.
4781 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4782 cupsd_job_t
*job
, /* I - Job */
4783 cups_array_t
*ra
, /* I - Requested attributes array */
4784 cups_array_t
*exclude
) /* I - Private attributes array */
4786 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4790 * Send the requested attributes for each job...
4793 if (!cupsArrayFind(exclude
, "all"))
4795 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4796 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4797 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4798 "number-of-documents", job
->num_files
);
4800 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4801 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4802 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4803 "job-media-progress", job
->progress
);
4805 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4806 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4808 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4809 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4811 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4812 "job-more-info", NULL
, job_uri
);
4815 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4816 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4817 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4818 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4819 job
->num_files
> 0);
4821 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4822 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4823 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4824 "job-printer-up-time", time(NULL
));
4827 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4829 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4830 con
->clientname
, con
->clientport
,
4831 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4834 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4835 "job-printer-uri", NULL
, job_uri
);
4838 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4840 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4841 con
->clientname
, con
->clientport
, "/jobs/%d",
4843 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4844 "job-uri", NULL
, job_uri
);
4849 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4854 * Generate attributes from the job structure...
4857 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-completed")))
4858 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-completed", ippTimeToDate(job
->completed_time
));
4860 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-creation")))
4861 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(job
->creation_time
));
4863 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4864 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4866 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4867 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4869 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4870 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4872 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4873 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4875 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4876 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4878 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4880 switch (job
->state_value
)
4882 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4884 case IPP_JSTATE_ABORTED
:
4885 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4887 case IPP_JSTATE_CANCELED
:
4888 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4890 case IPP_JSTATE_COMPLETED
:
4891 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4896 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4897 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4899 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4900 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4906 * 'copy_printer_attrs()' - Copy printer attributes.
4911 cupsd_client_t
*con
, /* I - Client connection */
4912 cupsd_printer_t
*printer
, /* I - Printer */
4913 cups_array_t
*ra
) /* I - Requested attributes array */
4915 char printer_uri
[HTTP_MAX_URI
];
4917 char printer_icons
[HTTP_MAX_URI
];
4919 time_t curtime
; /* Current time */
4920 int i
; /* Looping var */
4924 * Copy the printer attributes to the response using requested-attributes
4925 * and document-format attributes that may be provided by the client.
4928 curtime
= time(NULL
);
4930 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4931 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4932 "marker-change-time", printer
->marker_time
);
4934 if (printer
->num_printers
> 0 &&
4935 (!ra
|| cupsArrayFind(ra
, "member-uris")))
4937 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4938 cupsd_printer_t
*p2
; /* Printer in class */
4939 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4942 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
4943 IPP_TAG_URI
, "member-uris",
4944 printer
->num_printers
, NULL
,
4947 for (i
= 0; i
< printer
->num_printers
; i
++)
4949 p2
= printer
->printers
[i
];
4951 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
4952 IPP_TAG_URI
)) != NULL
)
4953 member_uris
->values
[i
].string
.text
=
4954 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4957 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
4958 sizeof(printer_uri
), "ipp", NULL
, con
->clientname
,
4960 (p2
->type
& CUPS_PRINTER_CLASS
) ?
4961 "/classes/%s" : "/printers/%s", p2
->name
);
4962 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
4968 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4969 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4970 "printer-alert", NULL
, printer
->alert
);
4972 if (printer
->alert_description
&&
4973 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4974 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4975 "printer-alert-description", NULL
,
4976 printer
->alert_description
);
4978 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-date-time"))
4979 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-config-change-date-time", ippTimeToDate(printer
->config_time
));
4981 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-time"))
4982 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4983 "printer-config-change-time", printer
->config_time
);
4985 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4986 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4987 ippTimeToDate(curtime
));
4989 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4990 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4992 if (printer
->reg_name
)
4993 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4994 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4996 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4997 "printer-dns-sd-name", 0);
4999 #endif /* HAVE_DNSSD || HAVE_AVAHI */
5001 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5002 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5003 "printer-error-policy", NULL
, printer
->error_policy
);
5005 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
5007 static const char * const errors
[] =/* printer-error-policy-supported values */
5010 "retry-current-job",
5015 if (printer
->type
& CUPS_PRINTER_CLASS
)
5016 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
5017 "printer-error-policy-supported", NULL
, "retry-current-job");
5019 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
5020 "printer-error-policy-supported",
5021 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
5024 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
5026 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_icons
, sizeof(printer_icons
),
5027 "http", NULL
, con
->clientname
, con
->clientport
,
5028 "/icons/%s.png", printer
->name
);
5029 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons",
5030 NULL
, printer_icons
);
5031 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", printer_icons
);
5034 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5035 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
5037 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5038 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
5040 if (!ra
|| cupsArrayFind(ra
, "printer-is-temporary"))
5041 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-temporary", (char)printer
->temporary
);
5043 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
5045 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5046 "http", NULL
, con
->clientname
, con
->clientport
,
5047 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5048 "/classes/%s" : "/printers/%s", printer
->name
);
5049 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5050 "printer-more-info", NULL
, printer_uri
);
5053 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5054 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5055 "printer-op-policy", NULL
, printer
->op_policy
);
5057 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5058 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5061 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-date-time"))
5062 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-state-change-date-time", ippTimeToDate(printer
->state_time
));
5064 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5065 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5066 "printer-state-change-time", printer
->state_time
);
5068 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5069 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5070 "printer-state-message", NULL
, printer
->state_message
);
5072 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5073 add_printer_state_reasons(con
, printer
);
5075 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5077 cups_ptype_t type
; /* printer-type value */
5080 * Add the CUPS-specific printer-type attribute...
5083 type
= printer
->type
;
5085 if (printer
== DefaultPrinter
)
5086 type
|= CUPS_PRINTER_DEFAULT
;
5088 if (!printer
->accepting
)
5089 type
|= CUPS_PRINTER_REJECTING
;
5091 if (!printer
->shared
)
5092 type
|= CUPS_PRINTER_NOT_SHARED
;
5094 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
5097 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5098 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5099 "printer-up-time", curtime
);
5101 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
5103 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5104 "ipp", NULL
, con
->clientname
, con
->clientport
,
5105 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5106 "/classes/%s" : "/printers/%s", printer
->name
);
5107 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5108 "printer-uri-supported", NULL
, printer_uri
);
5109 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5113 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5114 add_queued_job_count(con
, printer
);
5116 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5117 if (printer
->ppd_attrs
)
5118 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5119 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
5124 * 'copy_subscription_attrs()' - Copy subscription attributes.
5128 copy_subscription_attrs(
5129 cupsd_client_t
*con
, /* I - Client connection */
5130 cupsd_subscription_t
*sub
, /* I - Subscription */
5131 cups_array_t
*ra
, /* I - Requested attributes array */
5132 cups_array_t
*exclude
) /* I - Private attributes array */
5134 ipp_attribute_t
*attr
; /* Current attribute */
5135 char printer_uri
[HTTP_MAX_URI
];
5137 int count
; /* Number of events */
5138 unsigned mask
; /* Current event mask */
5139 const char *name
; /* Current event name */
5142 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5143 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5144 con
, sub
, ra
, exclude
);
5147 * Copy the subscription attributes to the response using the
5148 * requested-attributes attribute that may be provided by the client.
5151 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5153 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5154 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5156 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5158 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5161 * Simple event list...
5164 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5165 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5166 "notify-events", NULL
, name
);
5171 * Complex event list...
5174 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5175 if (sub
->mask
& mask
)
5178 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5179 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5180 "notify-events", count
, NULL
, NULL
);
5182 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5183 if (sub
->mask
& mask
)
5185 attr
->values
[count
].string
.text
=
5186 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5193 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5194 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5195 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5196 "notify-lease-duration", sub
->lease
);
5198 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5199 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5200 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5201 "notify-recipient-uri", NULL
, sub
->recipient
);
5202 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5203 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5204 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5205 "notify-pull-method", NULL
, "ippget");
5207 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5208 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5209 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5210 "notify-subscriber-user-name", NULL
, sub
->owner
);
5212 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5213 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5214 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5215 "notify-time-interval", sub
->interval
);
5217 if (sub
->user_data_len
> 0 &&
5218 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5219 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5220 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5221 sub
->user_data
, sub
->user_data_len
);
5224 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5225 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5226 "notify-job-id", sub
->job
->id
);
5228 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5230 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5231 "ipp", NULL
, con
->clientname
, con
->clientport
,
5232 "/printers/%s", sub
->dest
->name
);
5233 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5234 "notify-printer-uri", NULL
, printer_uri
);
5237 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5238 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5239 "notify-subscription-id", sub
->id
);
5244 * 'create_job()' - Print a file to a printer or class.
5248 create_job(cupsd_client_t
*con
, /* I - Client connection */
5249 ipp_attribute_t
*uri
) /* I - Printer URI */
5251 int i
; /* Looping var */
5252 cupsd_printer_t
*printer
; /* Printer */
5253 cupsd_job_t
*job
; /* New job */
5254 static const char * const forbidden_attrs
[] =
5255 { /* List of forbidden attributes */
5259 "document-natural-language"
5263 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5264 con
->number
, uri
->values
[0].string
.text
);
5267 * Is the destination valid?
5270 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5276 send_ipp_status(con
, IPP_NOT_FOUND
,
5277 _("The printer or class does not exist."));
5282 * Check for invalid Create-Job attributes and log a warning or error depending
5283 * on whether cupsd is running in "strict conformance" mode...
5287 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5289 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5291 if (StrictConformance
)
5293 send_ipp_status(con
, IPP_BAD_REQUEST
,
5294 _("The '%s' operation attribute cannot be supplied in a "
5295 "Create-Job request."), forbidden_attrs
[i
]);
5299 cupsdLogMessage(CUPSD_LOG_WARN
,
5300 "Unexpected '%s' operation attribute in a Create-Job "
5301 "request.", forbidden_attrs
[i
]);
5305 * Create the job object...
5308 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5311 job
->pending_timeout
= 1;
5314 * Save and log the job...
5317 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5318 job
->dest
, job
->username
);
5323 * 'create_local_bg_thread()' - Background thread for creating a local print queue.
5326 static void * /* O - Exit status */
5327 create_local_bg_thread(
5328 cupsd_printer_t
*printer
) /* I - Printer */
5330 cups_file_t
*from
, /* Source file */
5331 *to
; /* Destination file */
5332 char fromppd
[1024], /* Source PPD */
5333 toppd
[1024], /* Destination PPD */
5334 scheme
[32], /* URI scheme */
5335 userpass
[256], /* User:pass */
5336 host
[256], /* Hostname */
5337 resource
[1024], /* Resource path */
5338 line
[1024]; /* Line from PPD */
5339 int port
; /* Port number */
5340 http_encryption_t encryption
; /* Type of encryption to use */
5341 http_t
*http
; /* Connection to printer */
5342 ipp_t
*request
, /* Request to printer */
5343 *response
; /* Response from printer */
5344 ipp_attribute_t
*attr
; /* Attribute in response */
5348 * Try connecting to the printer...
5351 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Generating PPD file from \"%s\"...", printer
->name
, printer
->device_uri
);
5353 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
)
5355 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Bad device URI \"%s\".", printer
->name
, printer
->device_uri
);
5359 if (!strcmp(scheme
, "ipps") || port
== 443)
5360 encryption
= HTTP_ENCRYPTION_ALWAYS
;
5362 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
5364 if ((http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
)) == NULL
)
5366 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to connect to %s:%d: %s", printer
->name
, host
, port
, cupsLastErrorString());
5371 * Query the printer for its capabilities...
5374 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer
->name
, host
, port
);
5376 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
5377 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, printer
->device_uri
);
5378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "all");
5380 response
= cupsDoRequest(http
, request
, resource
);
5382 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Get-Printer-Attributes returned %s", printer
->name
, ippErrorString(cupsLastError()));
5384 // TODO: Grab printer icon file...
5388 * Write the PPD for the queue...
5391 if (_ppdCreateFromIPP(fromppd
, sizeof(fromppd
), response
))
5393 if ((!printer
->info
|| !*(printer
->info
)) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
5394 cupsdSetString(&printer
->info
, ippGetString(attr
, 0, NULL
));
5396 if ((!printer
->location
|| !*(printer
->location
)) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
5397 cupsdSetString(&printer
->location
, ippGetString(attr
, 0, NULL
));
5399 if ((!printer
->geo_location
|| !*(printer
->geo_location
)) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
5400 cupsdSetString(&printer
->geo_location
, ippGetString(attr
, 0, NULL
));
5402 if ((from
= cupsFileOpen(fromppd
, "r")) == NULL
)
5404 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to read generated PPD: %s", printer
->name
, strerror(errno
));
5408 snprintf(toppd
, sizeof(toppd
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
5409 if ((to
= cupsdCreateConfFile(toppd
, ConfigFilePerm
)) == NULL
)
5411 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to create PPD for printer: %s", printer
->name
, strerror(errno
));
5412 cupsFileClose(from
);
5416 while (cupsFileGets(from
, line
, sizeof(line
)))
5417 cupsFilePrintf(to
, "%s\n", line
);
5419 cupsFileClose(from
);
5420 if (!cupsdCloseCreatedConfFile(to
, toppd
))
5422 printer
->config_time
= time(NULL
);
5423 printer
->state
= IPP_PSTATE_IDLE
;
5424 printer
->accepting
= 1;
5426 cupsdSetPrinterAttrs(printer
);
5428 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
, "Printer \"%s\" is now available.", printer
->name
);
5429 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" is now available.", printer
->name
);
5433 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: PPD creation failed: %s", printer
->name
, cupsLastErrorString());
5440 * 'create_local_printer()' - Create a local (temporary) print queue.
5444 create_local_printer(
5445 cupsd_client_t
*con
) /* I - Client connection */
5447 ipp_attribute_t
*device_uri
, /* device-uri attribute */
5448 *printer_geo_location
, /* printer-geo-location attribute */
5449 *printer_info
, /* printer-info attribute */
5450 *printer_location
, /* printer-location attribute */
5451 *printer_name
; /* printer-name attribute */
5452 cupsd_printer_t
*printer
; /* New printer */
5453 http_status_t status
; /* Policy status */
5454 char name
[128], /* Sanitized printer name */
5455 *nameptr
, /* Pointer into name */
5456 uri
[1024]; /* printer-uri-supported value */
5457 const char *ptr
; /* Pointer into attribute value */
5461 * Require local access to create a local printer...
5464 if (!httpAddrLocalhost(httpGetAddress(con
->http
)))
5466 send_ipp_status(con
, IPP_STATUS_ERROR_FORBIDDEN
, _("Only local users can create a local printer."));
5471 * Check any other policy limits...
5474 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5476 send_http_error(con
, status
, NULL
);
5481 * Grab needed attributes...
5484 if ((printer_name
= ippFindAttribute(con
->request
, "printer-name", IPP_TAG_ZERO
)) == NULL
|| ippGetGroupTag(printer_name
) != IPP_TAG_PRINTER
|| ippGetValueTag(printer_name
) != IPP_TAG_NAME
)
5487 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Missing required attribute \"%s\"."), "printer-name");
5488 else if (ippGetGroupTag(printer_name
) != IPP_TAG_PRINTER
)
5489 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is in the wrong group."), "printer-name");
5491 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is the wrong value type."), "printer-name");
5496 for (nameptr
= name
, ptr
= ippGetString(printer_name
, 0, NULL
); *ptr
&& nameptr
< (name
+ sizeof(name
) - 1); ptr
++)
5499 * Sanitize the printer name...
5502 if (_cups_isalnum(*ptr
))
5504 else if (nameptr
== name
|| nameptr
[-1] != '_')
5510 if ((device_uri
= ippFindAttribute(con
->request
, "device-uri", IPP_TAG_ZERO
)) == NULL
|| ippGetGroupTag(device_uri
) != IPP_TAG_PRINTER
|| ippGetValueTag(device_uri
) != IPP_TAG_URI
)
5513 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Missing required attribute \"%s\"."), "device-uri");
5514 else if (ippGetGroupTag(device_uri
) != IPP_TAG_PRINTER
)
5515 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is in the wrong group."), "device-uri");
5517 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is the wrong value type."), "device-uri");
5522 printer_geo_location
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
);
5523 printer_info
= ippFindAttribute(con
->request
, "printer-info", IPP_TAG_TEXT
);
5524 printer_location
= ippFindAttribute(con
->request
, "printer-location", IPP_TAG_TEXT
);
5527 * See if the printer already exists...
5530 if ((printer
= cupsdFindDest(name
)) != NULL
)
5532 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_POSSIBLE
, _("Printer \"%s\" already exists."), name
);
5533 goto add_printer_attributes
;
5537 * Create the printer...
5540 if ((printer
= cupsdAddPrinter(name
)) == NULL
)
5542 send_ipp_status(con
, IPP_STATUS_ERROR_INTERNAL
, _("Unable to create printer."));
5546 printer
->shared
= 0;
5547 printer
->temporary
= 1;
5549 cupsdSetDeviceURI(printer
, ippGetString(device_uri
, 0, NULL
));
5551 if (printer_geo_location
)
5552 cupsdSetString(&printer
->geo_location
, ippGetString(printer_geo_location
, 0, NULL
));
5554 cupsdSetString(&printer
->info
, ippGetString(printer_info
, 0, NULL
));
5555 if (printer_location
)
5556 cupsdSetString(&printer
->location
, ippGetString(printer_location
, 0, NULL
));
5558 cupsdSetPrinterAttrs(printer
);
5561 * Run a background thread to create the PPD...
5564 _cupsThreadCreate((_cups_thread_func_t
)create_local_bg_thread
, printer
);
5567 * Return printer attributes...
5570 send_ipp_status(con
, IPP_STATUS_OK
, _("Local printer created."));
5572 add_printer_attributes
:
5574 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
5575 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", printer
->state
);
5576 add_printer_state_reasons(con
, printer
);
5578 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), httpIsEncrypted(con
->http
) ? "ipps" : "ipp", NULL
, con
->clientname
, con
->clientport
, "/printers/%s", printer
->name
);
5579 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported", NULL
, uri
);
5584 * 'create_requested_array()' - Create an array for the requested-attributes.
5587 static cups_array_t
* /* O - Array of attributes or NULL */
5588 create_requested_array(ipp_t
*request
) /* I - IPP request */
5590 cups_array_t
*ra
; /* Requested attributes array */
5594 * Create the array for standard attributes...
5597 ra
= ippCreateRequestedArray(request
);
5600 * Add CUPS defaults as needed...
5603 if (cupsArrayFind(ra
, "printer-defaults"))
5606 * Include user-set defaults...
5609 char *name
; /* Option name */
5611 cupsArrayRemove(ra
, "printer-defaults");
5613 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5615 name
= (char *)cupsArrayNext(CommonDefaults
))
5616 if (!cupsArrayFind(ra
, name
))
5617 cupsArrayAdd(ra
, name
);
5625 * 'create_subscriptions()' - Create one or more notification subscriptions.
5629 create_subscriptions(
5630 cupsd_client_t
*con
, /* I - Client connection */
5631 ipp_attribute_t
*uri
) /* I - Printer URI */
5633 http_status_t status
; /* Policy status */
5634 int i
; /* Looping var */
5635 ipp_attribute_t
*attr
; /* Current attribute */
5636 cups_ptype_t dtype
; /* Destination type (printer/class) */
5637 char scheme
[HTTP_MAX_URI
],
5638 /* Scheme portion of URI */
5639 userpass
[HTTP_MAX_URI
],
5640 /* Username portion of URI */
5642 /* Host portion of URI */
5643 resource
[HTTP_MAX_URI
];
5644 /* Resource portion of URI */
5645 int port
; /* Port portion of URI */
5646 cupsd_printer_t
*printer
; /* Printer/class */
5647 cupsd_job_t
*job
; /* Job */
5648 int jobid
; /* Job ID */
5649 cupsd_subscription_t
*sub
; /* Subscription object */
5650 const char *username
, /* requesting-user-name or
5651 authenticated username */
5652 *recipient
, /* notify-recipient-uri */
5653 *pullmethod
; /* notify-pull-method */
5654 ipp_attribute_t
*user_data
; /* notify-user-data */
5655 int interval
, /* notify-time-interval */
5656 lease
; /* notify-lease-duration */
5657 unsigned mask
; /* notify-events */
5658 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5659 *notify_lease
; /* notify-lease-duration(-default) */
5663 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5665 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5666 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5667 attr
->value_tag
, attr
->name
);
5669 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5674 * Is the destination valid?
5677 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5679 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5680 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5681 sizeof(host
), &port
, resource
, sizeof(resource
));
5683 if (!strcmp(resource
, "/"))
5685 dtype
= (cups_ptype_t
)0;
5688 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5690 dtype
= (cups_ptype_t
)0;
5693 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5695 dtype
= CUPS_PRINTER_CLASS
;
5698 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5704 send_ipp_status(con
, IPP_NOT_FOUND
,
5705 _("The printer or class does not exist."));
5715 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5718 send_http_error(con
, status
, printer
);
5722 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5724 send_http_error(con
, status
, NULL
);
5729 * Get the user that is requesting the subscription...
5732 username
= get_username(con
);
5735 * Find the first subscription group attribute; return if we have
5739 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5740 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5745 send_ipp_status(con
, IPP_BAD_REQUEST
,
5746 _("No subscription attributes in request."));
5751 * Process the subscription attributes in the request...
5754 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5762 lease
= DefaultLeaseDuration
;
5764 mask
= CUPSD_EVENT_NONE
;
5768 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5770 notify_lease
= ippFindAttribute(printer
->attrs
,
5771 "notify-lease-duration-default",
5775 lease
= notify_lease
->values
[0].integer
;
5779 notify_events
= NULL
;
5780 notify_lease
= NULL
;
5783 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5785 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5786 attr
->value_tag
== IPP_TAG_URI
)
5789 * Validate the recipient scheme against the ServerBin/notifier
5793 char notifier
[1024]; /* Notifier filename */
5796 recipient
= attr
->values
[0].string
.text
;
5798 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5799 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5800 host
, sizeof(host
), &port
,
5801 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5803 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5804 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5805 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5806 "notify-status-code", IPP_URI_SCHEME
);
5810 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5812 if (access(notifier
, X_OK
))
5814 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5815 _("notify-recipient-uri URI \"%s\" uses unknown "
5816 "scheme."), recipient
);
5817 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5818 "notify-status-code", IPP_URI_SCHEME
);
5822 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5824 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5825 _("notify-recipient-uri URI \"%s\" is already used."),
5827 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5828 "notify-status-code", IPP_ATTRIBUTES
);
5832 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5833 attr
->value_tag
== IPP_TAG_KEYWORD
)
5835 pullmethod
= attr
->values
[0].string
.text
;
5837 if (strcmp(pullmethod
, "ippget"))
5839 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5840 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5841 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5842 "notify-status-code", IPP_ATTRIBUTES
);
5846 else if (!strcmp(attr
->name
, "notify-charset") &&
5847 attr
->value_tag
== IPP_TAG_CHARSET
&&
5848 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5849 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5851 send_ipp_status(con
, IPP_CHARSET
,
5852 _("Character set \"%s\" not supported."),
5853 attr
->values
[0].string
.text
);
5856 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5857 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5858 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5860 send_ipp_status(con
, IPP_CHARSET
,
5861 _("Language \"%s\" not supported."),
5862 attr
->values
[0].string
.text
);
5865 else if (!strcmp(attr
->name
, "notify-user-data") &&
5866 attr
->value_tag
== IPP_TAG_STRING
)
5868 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5870 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5871 _("The notify-user-data value is too large "
5872 "(%d > 63 octets)."),
5873 attr
->values
[0].unknown
.length
);
5879 else if (!strcmp(attr
->name
, "notify-events") &&
5880 attr
->value_tag
== IPP_TAG_KEYWORD
)
5881 notify_events
= attr
;
5882 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5883 attr
->value_tag
== IPP_TAG_INTEGER
)
5884 lease
= attr
->values
[0].integer
;
5885 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5886 attr
->value_tag
== IPP_TAG_INTEGER
)
5887 interval
= attr
->values
[0].integer
;
5888 else if (!strcmp(attr
->name
, "notify-job-id") &&
5889 attr
->value_tag
== IPP_TAG_INTEGER
)
5890 jobid
= attr
->values
[0].integer
;
5897 for (i
= 0; i
< notify_events
->num_values
; i
++)
5898 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5902 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5904 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5905 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5906 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5908 if (!recipient
&& !pullmethod
)
5911 if (mask
== CUPSD_EVENT_NONE
)
5914 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5916 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5919 send_ipp_status(con
, IPP_BAD_REQUEST
,
5920 _("notify-events not specified."));
5925 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5927 cupsdLogMessage(CUPSD_LOG_INFO
,
5928 "create_subscriptions: Limiting notify-lease-duration to "
5931 lease
= MaxLeaseDuration
;
5936 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5938 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5946 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5948 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5949 _("There are too many subscriptions."));
5954 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5957 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5958 "Added subscription #%d for printer \"%s\".",
5959 sub
->id
, printer
->name
);
5961 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5964 sub
->interval
= interval
;
5966 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5968 cupsdSetString(&sub
->owner
, username
);
5972 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5973 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5974 (size_t)sub
->user_data_len
);
5977 ippAddSeparator(con
->response
);
5978 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5979 "notify-subscription-id", sub
->id
);
5981 con
->response
->request
.status
.status_code
= IPP_OK
;
5987 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5992 * 'delete_printer()' - Remove a printer or class from the system.
5996 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5997 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5999 http_status_t status
; /* Policy status */
6000 cups_ptype_t dtype
; /* Destination type (printer/class) */
6001 cupsd_printer_t
*printer
; /* Printer/class */
6002 char filename
[1024]; /* Script/PPD filename */
6003 int temporary
; /* Temporary queue? */
6006 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6007 con
->number
, uri
->values
[0].string
.text
);
6010 * Do we have a valid URI?
6013 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6019 send_ipp_status(con
, IPP_NOT_FOUND
,
6020 _("The printer or class does not exist."));
6028 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6030 send_http_error(con
, status
, NULL
);
6035 * Remove old jobs...
6038 cupsdCancelJobs(printer
->name
, NULL
, 1);
6041 * Remove old subscriptions and send a "deleted printer" event...
6044 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6045 "%s \"%s\" deleted by \"%s\".",
6046 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6047 printer
->name
, get_username(con
));
6049 cupsdExpireSubscriptions(printer
, NULL
);
6052 * Remove any old PPD or script files...
6055 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6058 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
6062 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
6065 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
6069 * Unregister color profiles...
6072 cupsdUnregisterColor(printer
);
6074 temporary
= printer
->temporary
;
6076 if (dtype
& CUPS_PRINTER_CLASS
)
6078 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6079 printer
->name
, get_username(con
));
6081 cupsdDeletePrinter(printer
, 0);
6083 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6087 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6088 printer
->name
, get_username(con
));
6090 if (cupsdDeletePrinter(printer
, 0) && !temporary
)
6091 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6094 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6098 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6101 * Return with no errors...
6104 con
->response
->request
.status
.status_code
= IPP_OK
;
6109 * 'get_default()' - Get the default destination.
6113 get_default(cupsd_client_t
*con
) /* I - Client connection */
6115 http_status_t status
; /* Policy status */
6116 cups_array_t
*ra
; /* Requested attributes array */
6119 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
6125 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6127 send_http_error(con
, status
, NULL
);
6133 ra
= create_requested_array(con
->request
);
6135 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6137 cupsArrayDelete(ra
);
6139 con
->response
->request
.status
.status_code
= IPP_OK
;
6142 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
6147 * 'get_devices()' - Get the list of available devices on the local system.
6151 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6153 http_status_t status
; /* Policy status */
6154 ipp_attribute_t
*limit
, /* limit attribute */
6155 *timeout
, /* timeout attribute */
6156 *requested
, /* requested-attributes attribute */
6157 *exclude
, /* exclude-schemes attribute */
6158 *include
; /* include-schemes attribute */
6159 char command
[1024], /* cups-deviced command */
6160 options
[2048], /* Options to pass to command */
6162 /* String for requested attributes */
6164 /* String for excluded schemes */
6166 /* String for included schemes */
6169 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
6175 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6177 send_http_error(con
, status
, NULL
);
6182 * Run cups-deviced command with the given options...
6185 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6186 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6187 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6189 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6190 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
6193 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6195 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6198 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6200 exclude_str
[0] = '\0';
6203 url_encode_attr(include
, include_str
, sizeof(include_str
));
6205 include_str
[0] = '\0';
6207 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6208 snprintf(options
, sizeof(options
),
6209 "%d+%d+%d+%d+%s%s%s%s%s",
6210 con
->request
->request
.op
.request_id
,
6211 limit
? limit
->values
[0].integer
: 0,
6212 timeout
? timeout
->values
[0].integer
: 15,
6215 exclude_str
[0] ? "%20" : "", exclude_str
,
6216 include_str
[0] ? "%20" : "", include_str
);
6218 if (cupsdSendCommand(con
, command
, options
, 1))
6221 * Command started successfully, don't send an IPP response here...
6224 ippDelete(con
->response
);
6225 con
->response
= NULL
;
6230 * Command failed, return "internal error" so the user knows something
6234 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6235 _("cups-deviced failed to execute."));
6241 * 'get_document()' - Get a copy of a job file.
6245 get_document(cupsd_client_t
*con
, /* I - Client connection */
6246 ipp_attribute_t
*uri
) /* I - Job URI */
6248 http_status_t status
; /* Policy status */
6249 ipp_attribute_t
*attr
; /* Current attribute */
6250 int jobid
; /* Job ID */
6251 int docnum
; /* Document number */
6252 cupsd_job_t
*job
; /* Current job */
6253 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6254 username
[HTTP_MAX_URI
], /* Username portion of URI */
6255 host
[HTTP_MAX_URI
], /* Host portion of URI */
6256 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6257 int port
; /* Port portion of URI */
6258 char filename
[1024], /* Filename for document */
6259 format
[1024]; /* Format for document */
6262 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6263 con
->number
, uri
->values
[0].string
.text
);
6266 * See if we have a job URI or a printer URI...
6269 if (!strcmp(uri
->name
, "printer-uri"))
6272 * Got a printer URI; see if we also have a job-id attribute...
6275 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6276 IPP_TAG_INTEGER
)) == NULL
)
6278 send_ipp_status(con
, IPP_BAD_REQUEST
,
6279 _("Got a printer-uri attribute but no job-id."));
6283 jobid
= attr
->values
[0].integer
;
6288 * Got a job URI; parse it to get the job ID...
6291 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6292 sizeof(scheme
), username
, sizeof(username
), host
,
6293 sizeof(host
), &port
, resource
, sizeof(resource
));
6295 if (strncmp(resource
, "/jobs/", 6))
6301 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6302 uri
->values
[0].string
.text
);
6306 jobid
= atoi(resource
+ 6);
6310 * See if the job exists...
6313 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6316 * Nope - return a "not found" error...
6319 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6327 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
6328 job
->username
)) != HTTP_OK
)
6330 send_http_error(con
, status
, NULL
);
6335 * Get the document number...
6338 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6339 IPP_TAG_INTEGER
)) == NULL
)
6341 send_ipp_status(con
, IPP_BAD_REQUEST
,
6342 _("Missing document-number attribute."));
6346 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6347 attr
->num_values
> 1)
6349 send_ipp_status(con
, IPP_NOT_FOUND
,
6350 _("Document #%d does not exist in job #%d."), docnum
,
6355 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6357 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6359 cupsdLogMessage(CUPSD_LOG_ERROR
,
6360 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6362 send_ipp_status(con
, IPP_NOT_FOUND
,
6363 _("Unable to open document #%d in job #%d."), docnum
,
6368 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6372 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6373 job
->filetypes
[docnum
- 1]->type
);
6375 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6377 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6379 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6380 IPP_TAG_NAME
)) != NULL
)
6381 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6382 NULL
, attr
->values
[0].string
.text
);
6387 * 'get_job_attrs()' - Get job attributes.
6391 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6392 ipp_attribute_t
*uri
) /* I - Job URI */
6394 http_status_t status
; /* Policy status */
6395 ipp_attribute_t
*attr
; /* Current attribute */
6396 int jobid
; /* Job ID */
6397 cupsd_job_t
*job
; /* Current job */
6398 cupsd_printer_t
*printer
; /* Current printer */
6399 cupsd_policy_t
*policy
; /* Current security policy */
6400 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6401 username
[HTTP_MAX_URI
], /* Username portion of URI */
6402 host
[HTTP_MAX_URI
], /* Host portion of URI */
6403 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6404 int port
; /* Port portion of URI */
6405 cups_array_t
*ra
, /* Requested attributes array */
6406 *exclude
; /* Private attributes array */
6409 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6410 con
->number
, uri
->values
[0].string
.text
);
6413 * See if we have a job URI or a printer URI...
6416 if (!strcmp(uri
->name
, "printer-uri"))
6419 * Got a printer URI; see if we also have a job-id attribute...
6422 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6423 IPP_TAG_INTEGER
)) == NULL
)
6425 send_ipp_status(con
, IPP_BAD_REQUEST
,
6426 _("Got a printer-uri attribute but no job-id."));
6430 jobid
= attr
->values
[0].integer
;
6435 * Got a job URI; parse it to get the job ID...
6438 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6439 sizeof(scheme
), username
, sizeof(username
), host
,
6440 sizeof(host
), &port
, resource
, sizeof(resource
));
6442 if (strncmp(resource
, "/jobs/", 6))
6448 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6449 uri
->values
[0].string
.text
);
6453 jobid
= atoi(resource
+ 6);
6457 * See if the job exists...
6460 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6463 * Nope - return a "not found" error...
6466 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6474 if ((printer
= job
->printer
) == NULL
)
6475 printer
= cupsdFindDest(job
->dest
);
6478 policy
= printer
->op_policy_ptr
;
6480 policy
= DefaultPolicyPtr
;
6482 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6484 send_http_error(con
, status
, NULL
);
6488 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6491 * Copy attributes...
6496 ra
= create_requested_array(con
->request
);
6497 copy_job_attrs(con
, job
, ra
, exclude
);
6498 cupsArrayDelete(ra
);
6500 con
->response
->request
.status
.status_code
= IPP_OK
;
6505 * 'get_jobs()' - Get a list of jobs for the specified printer.
6509 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6510 ipp_attribute_t
*uri
) /* I - Printer URI */
6512 http_status_t status
; /* Policy status */
6513 ipp_attribute_t
*attr
; /* Current attribute */
6514 const char *dest
; /* Destination */
6515 cups_ptype_t dtype
; /* Destination type (printer/class) */
6516 cups_ptype_t dmask
; /* Destination type mask */
6517 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6518 username
[HTTP_MAX_URI
], /* Username portion of URI */
6519 host
[HTTP_MAX_URI
], /* Host portion of URI */
6520 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6521 int port
; /* Port portion of URI */
6522 int job_comparison
; /* Job comparison */
6523 ipp_jstate_t job_state
; /* job-state value */
6524 int first_job_id
= 1, /* First job ID */
6525 first_index
= 1, /* First index */
6526 limit
= 0, /* Maximum number of jobs to return */
6527 count
, /* Number of jobs that match */
6528 need_load_job
= 0; /* Do we need to load the job? */
6529 const char *job_attr
; /* Job attribute requested */
6530 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6531 cupsd_job_t
*job
; /* Current job pointer */
6532 cupsd_printer_t
*printer
; /* Printer */
6533 cups_array_t
*list
; /* Which job list... */
6534 int delete_list
= 0; /* Delete the list afterwards? */
6535 cups_array_t
*ra
, /* Requested attributes array */
6536 *exclude
; /* Private attributes array */
6537 cupsd_policy_t
*policy
; /* Current policy */
6540 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6541 uri
->values
[0].string
.text
);
6544 * Is the destination valid?
6547 if (strcmp(uri
->name
, "printer-uri"))
6549 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6553 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6554 sizeof(scheme
), username
, sizeof(username
), host
,
6555 sizeof(host
), &port
, resource
, sizeof(resource
));
6557 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6560 dtype
= (cups_ptype_t
)0;
6561 dmask
= (cups_ptype_t
)0;
6564 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6567 dtype
= (cups_ptype_t
)0;
6568 dmask
= CUPS_PRINTER_CLASS
;
6571 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6574 dtype
= CUPS_PRINTER_CLASS
;
6575 dmask
= CUPS_PRINTER_CLASS
;
6578 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6585 send_ipp_status(con
, IPP_NOT_FOUND
,
6586 _("The printer or class does not exist."));
6591 dtype
&= CUPS_PRINTER_CLASS
;
6592 dmask
= CUPS_PRINTER_CLASS
;
6600 policy
= printer
->op_policy_ptr
;
6602 policy
= DefaultPolicyPtr
;
6604 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6606 send_http_error(con
, status
, NULL
);
6610 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6613 * See if the "which-jobs" attribute have been specified...
6616 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6617 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6619 send_ipp_status(con
, IPP_CONFLICT
,
6620 _("The %s attribute cannot be provided with job-ids."),
6624 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6626 job_comparison
= -1;
6627 job_state
= IPP_JOB_STOPPED
;
6630 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6633 job_state
= IPP_JOB_CANCELED
;
6634 list
= cupsdGetCompletedJobs(printer
);
6637 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6640 job_state
= IPP_JOB_ABORTED
;
6641 list
= cupsdGetCompletedJobs(printer
);
6644 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6647 job_state
= IPP_JOB_PENDING
;
6650 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6653 job_state
= IPP_JOB_CANCELED
;
6654 list
= cupsdGetCompletedJobs(printer
);
6657 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6660 job_state
= IPP_JOB_PENDING
;
6663 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6666 job_state
= IPP_JOB_HELD
;
6669 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6672 job_state
= IPP_JOB_PROCESSING
;
6673 list
= PrintingJobs
;
6675 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6678 job_state
= IPP_JOB_STOPPED
;
6683 send_ipp_status(con
, IPP_ATTRIBUTES
,
6684 _("The which-jobs value \"%s\" is not supported."),
6685 attr
->values
[0].string
.text
);
6686 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6687 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6692 * See if they want to limit the number of jobs reported...
6695 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6699 send_ipp_status(con
, IPP_CONFLICT
,
6700 _("The %s attribute cannot be provided with job-ids."),
6705 limit
= attr
->values
[0].integer
;
6708 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6712 send_ipp_status(con
, IPP_CONFLICT
,
6713 _("The %s attribute cannot be provided with job-ids."),
6718 first_index
= attr
->values
[0].integer
;
6720 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6724 send_ipp_status(con
, IPP_CONFLICT
,
6725 _("The %s attribute cannot be provided with job-ids."),
6730 first_job_id
= attr
->values
[0].integer
;
6734 * See if we only want to see jobs for a specific user...
6737 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6739 send_ipp_status(con
, IPP_CONFLICT
,
6740 _("The %s attribute cannot be provided with job-ids."),
6744 else if (attr
&& attr
->values
[0].boolean
)
6745 strlcpy(username
, get_username(con
), sizeof(username
));
6749 ra
= create_requested_array(con
->request
);
6750 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6751 if (strcmp(job_attr
, "job-id") &&
6752 strcmp(job_attr
, "job-k-octets") &&
6753 strcmp(job_attr
, "job-media-progress") &&
6754 strcmp(job_attr
, "job-more-info") &&
6755 strcmp(job_attr
, "job-name") &&
6756 strcmp(job_attr
, "job-originating-user-name") &&
6757 strcmp(job_attr
, "job-preserved") &&
6758 strcmp(job_attr
, "job-printer-up-time") &&
6759 strcmp(job_attr
, "job-printer-uri") &&
6760 strcmp(job_attr
, "job-state") &&
6761 strcmp(job_attr
, "job-state-reasons") &&
6762 strcmp(job_attr
, "job-uri") &&
6763 strcmp(job_attr
, "time-at-completed") &&
6764 strcmp(job_attr
, "time-at-creation") &&
6765 strcmp(job_attr
, "number-of-documents"))
6771 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6774 * Limit expensive Get-Jobs for job history to 500 jobs...
6777 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6780 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6784 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6788 * OK, build a list of jobs for this printer...
6793 int i
; /* Looping var */
6795 for (i
= 0; i
< job_ids
->num_values
; i
++)
6797 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6801 if (i
< job_ids
->num_values
)
6803 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6804 job_ids
->values
[i
].integer
);
6808 for (i
= 0; i
< job_ids
->num_values
; i
++)
6810 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6812 if (need_load_job
&& !job
->attrs
)
6818 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6824 ippAddSeparator(con
->response
);
6826 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6827 job
->printer
->op_policy_ptr
:
6828 policy
, con
, job
->printer
,
6831 copy_job_attrs(con
, job
, ra
, exclude
);
6836 if (first_index
> 1)
6837 job
= (cupsd_job_t
*)cupsArrayIndex(list
, first_index
- 1);
6839 job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6841 for (count
= 0; (limit
<= 0 || count
< limit
) && job
; job
= (cupsd_job_t
*)cupsArrayNext(list
))
6844 * Filter out jobs that don't match...
6847 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6848 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6849 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6850 job
->username
, job
->state_value
, job
->attrs
);
6852 if (!job
->dest
|| !job
->username
)
6855 if (!job
->dest
|| !job
->username
)
6858 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6859 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6861 if ((job
->dtype
& dmask
) != dtype
&&
6862 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6865 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6866 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6867 (job_comparison
> 0 && job
->state_value
< job_state
))
6870 if (job
->id
< first_job_id
)
6873 if (need_load_job
&& !job
->attrs
)
6879 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6884 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6888 ippAddSeparator(con
->response
);
6892 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6893 job
->printer
->op_policy_ptr
:
6894 policy
, con
, job
->printer
,
6897 copy_job_attrs(con
, job
, ra
, exclude
);
6900 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6903 cupsArrayDelete(ra
);
6906 cupsArrayDelete(list
);
6908 con
->response
->request
.status
.status_code
= IPP_OK
;
6913 * 'get_notifications()' - Get events for a subscription.
6917 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6919 int i
, j
; /* Looping vars */
6920 http_status_t status
; /* Policy status */
6921 cupsd_subscription_t
*sub
; /* Subscription */
6922 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6923 *sequences
; /* notify-sequence-numbers */
6924 int min_seq
; /* Minimum sequence number */
6925 int interval
; /* Poll interval */
6928 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6932 * Get subscription attributes...
6935 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6937 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6942 send_ipp_status(con
, IPP_BAD_REQUEST
,
6943 _("Missing notify-subscription-ids attribute."));
6948 * Are the subscription IDs valid?
6951 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6953 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6956 * Bad subscription ID...
6959 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6960 ids
->values
[i
].integer
);
6968 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6970 con
, sub
->owner
)) != HTTP_OK
)
6972 send_http_error(con
, status
, sub
->dest
);
6977 * Check the subscription type and update the interval accordingly.
6980 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6983 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6985 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6991 * Tell the client to poll again in N seconds...
6995 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6996 "notify-get-interval", interval
);
6998 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6999 "printer-up-time", time(NULL
));
7002 * Copy the subscription event attributes to the response.
7005 con
->response
->request
.status
.status_code
=
7006 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
7008 for (i
= 0; i
< ids
->num_values
; i
++)
7011 * Get the subscription and sequence number...
7014 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
7016 if (sequences
&& i
< sequences
->num_values
)
7017 min_seq
= sequences
->values
[i
].integer
;
7022 * If we don't have any new events, nothing to do here...
7025 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
7029 * Otherwise copy all of the new events...
7032 if (sub
->first_event_id
> min_seq
)
7035 j
= min_seq
- sub
->first_event_id
;
7037 for (; j
< cupsArrayCount(sub
->events
); j
++)
7039 ippAddSeparator(con
->response
);
7041 copy_attrs(con
->response
,
7042 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
7043 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
7050 * 'get_ppd()' - Get a named PPD from the local system.
7054 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7055 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7057 http_status_t status
; /* Policy status */
7058 cupsd_printer_t
*dest
; /* Destination */
7059 cups_ptype_t dtype
; /* Destination type */
7062 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7063 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7065 if (!strcmp(ippGetName(uri
), "ppd-name"))
7068 * Return a PPD file from cups-driverd...
7071 const char *ppd_name
= ippGetString(uri
, 0, NULL
);
7072 /* ppd-name value */
7073 char command
[1024], /* cups-driverd command */
7074 options
[1024], /* Options to pass to command */
7075 oppd_name
[1024]; /* Escaped ppd-name */
7081 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7083 send_http_error(con
, status
, NULL
);
7088 * Check ppd-name value...
7091 if (strstr(ppd_name
, "../"))
7093 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
7098 * Run cups-driverd command with the given options...
7101 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7102 url_encode_string(ppd_name
, oppd_name
, sizeof(oppd_name
));
7103 snprintf(options
, sizeof(options
), "get+%d+%s", ippGetRequestId(con
->request
), oppd_name
);
7105 if (cupsdSendCommand(con
, command
, options
, 0))
7108 * Command started successfully, don't send an IPP response here...
7111 ippDelete(con
->response
);
7112 con
->response
= NULL
;
7117 * Command failed, return "internal error" so the user knows something
7121 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("cups-driverd failed to execute."));
7124 else if (!strcmp(ippGetName(uri
), "printer-uri") && cupsdValidateDest(ippGetString(uri
, 0, NULL
), &dtype
, &dest
))
7126 int i
; /* Looping var */
7127 char filename
[1024]; /* PPD filename */
7133 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7135 send_http_error(con
, status
, dest
);
7140 * See if we need the PPD for a class or remote printer...
7143 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->name
);
7145 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7147 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
7148 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->uri
);
7151 else if (dtype
& CUPS_PRINTER_CLASS
)
7153 for (i
= 0; i
< dest
->num_printers
; i
++)
7154 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
7156 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->printers
[i
]->name
);
7158 if (!access(filename
, 0))
7162 if (i
< dest
->num_printers
)
7163 dest
= dest
->printers
[i
];
7166 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
7167 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->printers
[0]->uri
);
7173 * Found the printer with the PPD file, now see if there is one...
7176 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7178 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be opened: %s"), ippGetString(uri
, 0, NULL
), strerror(errno
));
7182 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7186 ippSetStatusCode(con
->response
, IPP_STATUS_OK
);
7189 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be found."), ippGetString(uri
, 0, NULL
));
7194 * 'get_ppds()' - Get the list of PPD files on the local system.
7198 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7200 http_status_t status
; /* Policy status */
7201 ipp_attribute_t
*limit
, /* Limit attribute */
7202 *device
, /* ppd-device-id attribute */
7203 *language
, /* ppd-natural-language attribute */
7204 *make
, /* ppd-make attribute */
7205 *model
, /* ppd-make-and-model attribute */
7206 *model_number
, /* ppd-model-number attribute */
7207 *product
, /* ppd-product attribute */
7208 *psversion
, /* ppd-psverion attribute */
7209 *type
, /* ppd-type attribute */
7210 *requested
, /* requested-attributes attribute */
7211 *exclude
, /* exclude-schemes attribute */
7212 *include
; /* include-schemes attribute */
7213 char command
[1024], /* cups-driverd command */
7214 options
[4096], /* Options to pass to command */
7215 device_str
[256],/* Escaped ppd-device-id string */
7217 /* Escaped ppd-natural-language */
7218 make_str
[256], /* Escaped ppd-make string */
7219 model_str
[256], /* Escaped ppd-make-and-model string */
7220 model_number_str
[256],
7221 /* ppd-model-number string */
7223 /* Escaped ppd-product string */
7225 /* Escaped ppd-psversion string */
7226 type_str
[256], /* Escaped ppd-type string */
7228 /* String for requested attributes */
7230 /* String for excluded schemes */
7232 /* String for included schemes */
7235 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
7241 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7243 send_http_error(con
, status
, NULL
);
7248 * Run cups-driverd command with the given options...
7251 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7252 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7253 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7255 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7256 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7258 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7260 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7261 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7262 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7263 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7265 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
7267 include
= ippFindAttribute(con
->request
, "include-schemes",
7271 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7273 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7276 url_encode_attr(device
, device_str
, sizeof(device_str
));
7278 device_str
[0] = '\0';
7281 url_encode_attr(language
, language_str
, sizeof(language_str
));
7283 language_str
[0] = '\0';
7286 url_encode_attr(make
, make_str
, sizeof(make_str
));
7291 url_encode_attr(model
, model_str
, sizeof(model_str
));
7293 model_str
[0] = '\0';
7296 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7297 model_number
->values
[0].integer
);
7299 model_number_str
[0] = '\0';
7302 url_encode_attr(product
, product_str
, sizeof(product_str
));
7304 product_str
[0] = '\0';
7307 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7309 psversion_str
[0] = '\0';
7312 url_encode_attr(type
, type_str
, sizeof(type_str
));
7317 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
7319 exclude_str
[0] = '\0';
7322 url_encode_attr(include
, include_str
, sizeof(include_str
));
7324 include_str
[0] = '\0';
7326 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7327 snprintf(options
, sizeof(options
),
7328 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7329 con
->request
->request
.op
.request_id
,
7330 limit
? limit
->values
[0].integer
: 0,
7332 device
? "%20" : "", device_str
,
7333 language
? "%20" : "", language_str
,
7334 make
? "%20" : "", make_str
,
7335 model
? "%20" : "", model_str
,
7336 model_number
? "%20" : "", model_number_str
,
7337 product
? "%20" : "", product_str
,
7338 psversion
? "%20" : "", psversion_str
,
7339 type
? "%20" : "", type_str
,
7340 exclude_str
[0] ? "%20" : "", exclude_str
,
7341 include_str
[0] ? "%20" : "", include_str
);
7343 if (cupsdSendCommand(con
, command
, options
, 0))
7346 * Command started successfully, don't send an IPP response here...
7349 ippDelete(con
->response
);
7350 con
->response
= NULL
;
7355 * Command failed, return "internal error" so the user knows something
7359 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7360 _("cups-driverd failed to execute."));
7366 * 'get_printer_attrs()' - Get printer attributes.
7370 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7371 ipp_attribute_t
*uri
) /* I - Printer URI */
7373 http_status_t status
; /* Policy status */
7374 cups_ptype_t dtype
; /* Destination type (printer/class) */
7375 cupsd_printer_t
*printer
; /* Printer/class */
7376 cups_array_t
*ra
; /* Requested attributes array */
7379 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7380 con
->number
, uri
->values
[0].string
.text
);
7383 * Is the destination valid?
7386 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7392 send_ipp_status(con
, IPP_NOT_FOUND
,
7393 _("The printer or class does not exist."));
7401 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7403 send_http_error(con
, status
, printer
);
7408 * Send the attributes...
7411 ra
= create_requested_array(con
->request
);
7413 copy_printer_attrs(con
, printer
, ra
);
7415 cupsArrayDelete(ra
);
7417 con
->response
->request
.status
.status_code
= IPP_OK
;
7422 * 'get_printer_supported()' - Get printer supported values.
7426 get_printer_supported(
7427 cupsd_client_t
*con
, /* I - Client connection */
7428 ipp_attribute_t
*uri
) /* I - Printer URI */
7430 http_status_t status
; /* Policy status */
7431 cups_ptype_t dtype
; /* Destination type (printer/class) */
7432 cupsd_printer_t
*printer
; /* Printer/class */
7435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7436 con
->number
, uri
->values
[0].string
.text
);
7439 * Is the destination valid?
7442 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7448 send_ipp_status(con
, IPP_NOT_FOUND
,
7449 _("The printer or class does not exist."));
7457 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7459 send_http_error(con
, status
, printer
);
7464 * Return a list of attributes that can be set via Set-Printer-Attributes.
7467 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7468 "printer-geo-location", 0);
7469 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7471 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7472 "printer-location", 0);
7473 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7474 "printer-organization", 0);
7475 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7476 "printer-organizational-unit", 0);
7478 con
->response
->request
.status
.status_code
= IPP_OK
;
7483 * 'get_printers()' - Get a list of printers or classes.
7487 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7488 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7490 http_status_t status
; /* Policy status */
7491 ipp_attribute_t
*attr
; /* Current attribute */
7492 int limit
; /* Max number of printers to return */
7493 int count
; /* Number of printers that match */
7494 int printer_id
; /* Printer we are interested in */
7495 cupsd_printer_t
*printer
; /* Current printer pointer */
7496 cups_ptype_t printer_type
, /* printer-type attribute */
7497 printer_mask
; /* printer-type-mask attribute */
7498 char *location
; /* Location string */
7499 const char *username
; /* Current user */
7500 char *first_printer_name
; /* first-printer-name attribute */
7501 cups_array_t
*ra
; /* Requested attributes array */
7502 int local
; /* Local connection? */
7505 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7512 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7514 send_http_error(con
, status
, NULL
);
7519 * Check for printers...
7522 if (!Printers
|| !cupsArrayCount(Printers
))
7524 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7529 * See if they want to limit the number of printers reported...
7532 if ((attr
= ippFindAttribute(con
->request
, "limit",
7533 IPP_TAG_INTEGER
)) != NULL
)
7534 limit
= attr
->values
[0].integer
;
7538 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7539 IPP_TAG_NAME
)) != NULL
)
7540 first_printer_name
= attr
->values
[0].string
.text
;
7542 first_printer_name
= NULL
;
7545 * Support filtering...
7548 if ((attr
= ippFindAttribute(con
->request
, "printer-id", IPP_TAG_INTEGER
)) != NULL
)
7550 if ((printer_id
= ippGetInteger(attr
, 0)) <= 0)
7552 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Bad \"printer-id\" value %d."), printer_id
);
7559 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7560 IPP_TAG_ENUM
)) != NULL
)
7561 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7563 printer_type
= (cups_ptype_t
)0;
7565 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7566 IPP_TAG_ENUM
)) != NULL
)
7567 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7569 printer_mask
= (cups_ptype_t
)0;
7571 local
= httpAddrLocalhost(&(con
->clientaddr
));
7573 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7574 IPP_TAG_TEXT
)) != NULL
)
7575 location
= attr
->values
[0].string
.text
;
7579 if (con
->username
[0])
7580 username
= con
->username
;
7581 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7582 IPP_TAG_NAME
)) != NULL
)
7583 username
= attr
->values
[0].string
.text
;
7587 ra
= create_requested_array(con
->request
);
7590 * OK, build a list of printers for this printer...
7593 if (first_printer_name
)
7595 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7596 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7599 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7602 count
< limit
&& printer
;
7603 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7605 if (!local
&& !printer
->shared
)
7608 if (printer_id
&& printer
->printer_id
!= printer_id
)
7611 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7612 (printer
->type
& printer_mask
) == printer_type
&&
7614 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7617 * If a username is specified, see if it is allowed or denied
7621 if (cupsArrayCount(printer
->users
) && username
&&
7622 !user_allowed(printer
, username
))
7626 * Add the group separator as needed...
7630 ippAddSeparator(con
->response
);
7635 * Send the attributes...
7638 copy_printer_attrs(con
, printer
, ra
);
7642 cupsArrayDelete(ra
);
7644 con
->response
->request
.status
.status_code
= IPP_OK
;
7649 * 'get_subscription_attrs()' - Get subscription attributes.
7653 get_subscription_attrs(
7654 cupsd_client_t
*con
, /* I - Client connection */
7655 int sub_id
) /* I - Subscription ID */
7657 http_status_t status
; /* Policy status */
7658 cupsd_subscription_t
*sub
; /* Subscription */
7659 cupsd_policy_t
*policy
; /* Current security policy */
7660 cups_array_t
*ra
, /* Requested attributes array */
7661 *exclude
; /* Private attributes array */
7664 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7665 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7666 con
, con
->number
, sub_id
);
7669 * Expire subscriptions as needed...
7672 cupsdExpireSubscriptions(NULL
, NULL
);
7675 * Is the subscription ID valid?
7678 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7681 * Bad subscription ID...
7684 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7694 policy
= sub
->dest
->op_policy_ptr
;
7696 policy
= DefaultPolicyPtr
;
7698 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7700 send_http_error(con
, status
, sub
->dest
);
7704 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7707 * Copy the subscription attributes to the response using the
7708 * requested-attributes attribute that may be provided by the client.
7711 ra
= create_requested_array(con
->request
);
7713 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7715 cupsArrayDelete(ra
);
7717 con
->response
->request
.status
.status_code
= IPP_OK
;
7722 * 'get_subscriptions()' - Get subscriptions.
7726 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7727 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7729 http_status_t status
; /* Policy status */
7730 int count
; /* Number of subscriptions */
7731 int limit
; /* Limit */
7732 cupsd_subscription_t
*sub
; /* Subscription */
7733 cups_array_t
*ra
; /* Requested attributes array */
7734 ipp_attribute_t
*attr
; /* Attribute */
7735 cups_ptype_t dtype
; /* Destination type (printer/class) */
7736 char scheme
[HTTP_MAX_URI
],
7737 /* Scheme portion of URI */
7738 username
[HTTP_MAX_URI
],
7739 /* Username portion of URI */
7741 /* Host portion of URI */
7742 resource
[HTTP_MAX_URI
];
7743 /* Resource portion of URI */
7744 int port
; /* Port portion of URI */
7745 cupsd_job_t
*job
; /* Job pointer */
7746 cupsd_printer_t
*printer
; /* Printer */
7747 cupsd_policy_t
*policy
; /* Policy */
7748 cups_array_t
*exclude
; /* Private attributes array */
7751 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7752 "get_subscriptions(con=%p[%d], uri=%s)",
7753 con
, con
->number
, uri
->values
[0].string
.text
);
7756 * Is the destination valid?
7759 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7760 sizeof(scheme
), username
, sizeof(username
), host
,
7761 sizeof(host
), &port
, resource
, sizeof(resource
));
7763 if (!strcmp(resource
, "/") ||
7764 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7765 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7766 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7771 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7774 job
= cupsdFindJob(atoi(resource
+ 6));
7778 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7779 atoi(resource
+ 6));
7783 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7789 send_ipp_status(con
, IPP_NOT_FOUND
,
7790 _("The printer or class does not exist."));
7793 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7794 IPP_TAG_INTEGER
)) != NULL
)
7796 job
= cupsdFindJob(attr
->values
[0].integer
);
7800 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7801 attr
->values
[0].integer
);
7813 policy
= printer
->op_policy_ptr
;
7815 policy
= DefaultPolicyPtr
;
7817 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7819 send_http_error(con
, status
, printer
);
7824 * Expire subscriptions as needed...
7827 cupsdExpireSubscriptions(NULL
, NULL
);
7830 * Copy the subscription attributes to the response using the
7831 * requested-attributes attribute that may be provided by the client.
7834 ra
= create_requested_array(con
->request
);
7836 if ((attr
= ippFindAttribute(con
->request
, "limit",
7837 IPP_TAG_INTEGER
)) != NULL
)
7838 limit
= attr
->values
[0].integer
;
7843 * See if we only want to see subscriptions for a specific user...
7846 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7847 IPP_TAG_BOOLEAN
)) != NULL
&&
7848 attr
->values
[0].boolean
)
7849 strlcpy(username
, get_username(con
), sizeof(username
));
7853 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7855 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7856 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7857 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7859 ippAddSeparator(con
->response
);
7861 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7862 policy
, con
, sub
->dest
,
7865 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7868 if (limit
&& count
>= limit
)
7872 cupsArrayDelete(ra
);
7875 con
->response
->request
.status
.status_code
= IPP_OK
;
7877 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7882 * 'get_username()' - Get the username associated with a request.
7885 static const char * /* O - Username */
7886 get_username(cupsd_client_t
*con
) /* I - Connection */
7888 ipp_attribute_t
*attr
; /* Attribute */
7891 if (con
->username
[0])
7892 return (con
->username
);
7893 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7894 IPP_TAG_NAME
)) != NULL
)
7895 return (attr
->values
[0].string
.text
);
7897 return ("anonymous");
7902 * 'hold_job()' - Hold a print job.
7906 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7907 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7909 ipp_attribute_t
*attr
; /* Current job-hold-until */
7910 const char *when
; /* New value */
7911 int jobid
; /* Job ID */
7912 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7913 username
[HTTP_MAX_URI
], /* Username portion of URI */
7914 host
[HTTP_MAX_URI
], /* Host portion of URI */
7915 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7916 int port
; /* Port portion of URI */
7917 cupsd_job_t
*job
; /* Job information */
7920 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7921 uri
->values
[0].string
.text
);
7924 * See if we have a job URI or a printer URI...
7927 if (!strcmp(uri
->name
, "printer-uri"))
7930 * Got a printer URI; see if we also have a job-id attribute...
7933 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7934 IPP_TAG_INTEGER
)) == NULL
)
7936 send_ipp_status(con
, IPP_BAD_REQUEST
,
7937 _("Got a printer-uri attribute but no job-id."));
7941 jobid
= attr
->values
[0].integer
;
7946 * Got a job URI; parse it to get the job ID...
7949 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7950 sizeof(scheme
), username
, sizeof(username
), host
,
7951 sizeof(host
), &port
, resource
, sizeof(resource
));
7953 if (strncmp(resource
, "/jobs/", 6))
7959 send_ipp_status(con
, IPP_BAD_REQUEST
,
7960 _("Bad job-uri \"%s\"."),
7961 uri
->values
[0].string
.text
);
7965 jobid
= atoi(resource
+ 6);
7969 * See if the job exists...
7972 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7975 * Nope - return a "not found" error...
7978 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7983 * See if the job is owned by the requesting user...
7986 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7988 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7989 cupsdFindDest(job
->dest
));
7994 * See if the job is in a state that allows holding...
7997 if (job
->state_value
> IPP_JOB_STOPPED
)
8000 * Return a "not-possible" error...
8003 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8004 _("Job #%d is finished and cannot be altered."),
8010 * Hold the job and return...
8013 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
8014 IPP_TAG_KEYWORD
)) == NULL
)
8015 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
8019 when
= attr
->values
[0].string
.text
;
8021 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8022 "Job job-hold-until value changed by user.");
8025 when
= "indefinite";
8027 cupsdSetJobHoldUntil(job
, when
, 1);
8028 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
8031 con
->response
->request
.status
.status_code
= IPP_OK
;
8036 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8040 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
8041 ipp_attribute_t
*uri
) /* I - Printer URI */
8043 http_status_t status
; /* Policy status */
8044 cups_ptype_t dtype
; /* Destination type (printer/class) */
8045 cupsd_printer_t
*printer
; /* Printer data */
8048 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
8049 con
->number
, uri
->values
[0].string
.text
);
8052 * Is the destination valid?
8055 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8061 send_ipp_status(con
, IPP_NOT_FOUND
,
8062 _("The printer or class does not exist."));
8070 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8072 send_http_error(con
, status
, printer
);
8077 * Hold pending/new jobs sent to the printer...
8080 printer
->holding_new_jobs
= 1;
8082 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
8084 if (dtype
& CUPS_PRINTER_CLASS
)
8085 cupsdLogMessage(CUPSD_LOG_INFO
,
8086 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8087 printer
->name
, get_username(con
));
8089 cupsdLogMessage(CUPSD_LOG_INFO
,
8090 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8091 printer
->name
, get_username(con
));
8094 * Everything was ok, so return OK status...
8097 con
->response
->request
.status
.status_code
= IPP_OK
;
8102 * 'move_job()' - Move a job to a new destination.
8106 move_job(cupsd_client_t
*con
, /* I - Client connection */
8107 ipp_attribute_t
*uri
) /* I - Job URI */
8109 http_status_t status
; /* Policy status */
8110 ipp_attribute_t
*attr
; /* Current attribute */
8111 int jobid
; /* Job ID */
8112 cupsd_job_t
*job
; /* Current job */
8113 const char *src
; /* Source printer/class */
8114 cups_ptype_t stype
, /* Source type (printer or class) */
8115 dtype
; /* Destination type (printer/class) */
8116 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
8117 username
[HTTP_MAX_URI
], /* Username portion of URI */
8118 host
[HTTP_MAX_URI
], /* Host portion of URI */
8119 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8120 int port
; /* Port portion of URI */
8121 cupsd_printer_t
*sprinter
, /* Source printer */
8122 *dprinter
; /* Destination printer */
8125 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
8126 uri
->values
[0].string
.text
);
8129 * Get the new printer or class...
8132 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
8133 IPP_TAG_URI
)) == NULL
)
8136 * Need job-printer-uri...
8139 send_ipp_status(con
, IPP_BAD_REQUEST
,
8140 _("job-printer-uri attribute missing."));
8144 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
8150 send_ipp_status(con
, IPP_NOT_FOUND
,
8151 _("The printer or class does not exist."));
8156 * See if we have a job URI or a printer URI...
8159 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8160 sizeof(scheme
), username
, sizeof(username
), host
,
8161 sizeof(host
), &port
, resource
, sizeof(resource
));
8163 if (!strcmp(uri
->name
, "printer-uri"))
8166 * Got a printer URI; see if we also have a job-id attribute...
8169 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8170 IPP_TAG_INTEGER
)) == NULL
)
8176 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8177 &sprinter
)) == NULL
)
8183 send_ipp_status(con
, IPP_NOT_FOUND
,
8184 _("The printer or class does not exist."));
8193 * Otherwise, just move a single job...
8196 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8199 * Nope - return a "not found" error...
8202 send_ipp_status(con
, IPP_NOT_FOUND
,
8203 _("Job #%d does not exist."), attr
->values
[0].integer
);
8209 * Job found, initialize source pointers...
8220 * Got a job URI; parse it to get the job ID...
8223 if (strncmp(resource
, "/jobs/", 6))
8229 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8230 uri
->values
[0].string
.text
);
8235 * See if the job exists...
8238 jobid
= atoi(resource
+ 6);
8240 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8243 * Nope - return a "not found" error...
8246 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8252 * Job found, initialize source pointers...
8261 * Check the policy of the destination printer...
8264 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8265 job
? job
->username
: NULL
)) != HTTP_OK
)
8267 send_http_error(con
, status
, dprinter
);
8272 * Now move the job or jobs...
8278 * See if the job has been completed...
8281 if (job
->state_value
> IPP_JOB_STOPPED
)
8284 * Return a "not-possible" error...
8287 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8288 _("Job #%d is finished and cannot be altered."),
8294 * See if the job is owned by the requesting user...
8297 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8299 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8300 cupsdFindDest(job
->dest
));
8305 * Move the job to a different printer or class...
8308 cupsdMoveJob(job
, dprinter
);
8313 * Got the source printer, now look through the jobs...
8316 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8318 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8321 * See if the job is pointing at the source printer or has not been
8325 if (_cups_strcasecmp(job
->dest
, src
) ||
8326 job
->state_value
> IPP_JOB_STOPPED
)
8330 * See if the job can be moved by the requesting user...
8333 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8337 * Move the job to a different printer or class...
8340 cupsdMoveJob(job
, dprinter
);
8345 * Start jobs if possible...
8351 * Return with "everything is OK" status...
8354 con
->response
->request
.status
.status_code
= IPP_OK
;
8359 * 'ppd_parse_line()' - Parse a PPD default line.
8362 static int /* O - 0 on success, -1 on failure */
8363 ppd_parse_line(const char *line
, /* I - Line */
8364 char *option
, /* O - Option name */
8365 int olen
, /* I - Size of option name */
8366 char *choice
, /* O - Choice name */
8367 int clen
) /* I - Size of choice name */
8370 * Verify this is a default option line...
8373 if (strncmp(line
, "*Default", 8))
8377 * Read the option name...
8380 for (line
+= 8, olen
--;
8381 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8392 * Skip everything else up to the colon (:)...
8395 while (*line
&& *line
!= ':')
8404 * Now grab the option choice, skipping leading whitespace...
8407 while (isspace(*line
& 255))
8411 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8422 * Return with no errors...
8430 * 'print_job()' - Print a file to a printer or class.
8434 print_job(cupsd_client_t
*con
, /* I - Client connection */
8435 ipp_attribute_t
*uri
) /* I - Printer URI */
8437 ipp_attribute_t
*attr
; /* Current attribute */
8438 ipp_attribute_t
*doc_name
; /* document-name attribute */
8439 ipp_attribute_t
*format
; /* Document-format attribute */
8440 const char *default_format
; /* document-format-default value */
8441 cupsd_job_t
*job
; /* New job */
8442 char filename
[1024]; /* Job filename */
8443 mime_type_t
*filetype
; /* Type of file */
8444 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8445 type
[MIME_MAX_TYPE
], /* Subtype of file */
8446 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8447 /* Textual name of mime type */
8448 cupsd_printer_t
*printer
; /* Printer data */
8449 struct stat fileinfo
; /* File information */
8450 int kbytes
; /* Size of file */
8451 int compression
; /* Document compression */
8454 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8455 uri
->values
[0].string
.text
);
8458 * Validate print file attributes, for now just document-format and
8459 * compression (CUPS only supports "none" and "gzip")...
8462 compression
= CUPS_FILE_NONE
;
8464 if ((attr
= ippFindAttribute(con
->request
, "compression",
8465 IPP_TAG_KEYWORD
)) != NULL
)
8467 if (strcmp(attr
->values
[0].string
.text
, "none")
8469 && strcmp(attr
->values
[0].string
.text
, "gzip")
8470 #endif /* HAVE_LIBZ */
8473 send_ipp_status(con
, IPP_ATTRIBUTES
,
8474 _("Unsupported compression \"%s\"."),
8475 attr
->values
[0].string
.text
);
8476 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8477 "compression", NULL
, attr
->values
[0].string
.text
);
8482 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8483 compression
= CUPS_FILE_GZIP
;
8484 #endif /* HAVE_LIBZ */
8488 * Do we have a file to print?
8493 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8498 * Is the destination valid?
8501 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8507 send_ipp_status(con
, IPP_NOT_FOUND
,
8508 _("The printer or class does not exist."));
8513 * Is it a format we support?
8516 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8518 ippSetName(con
->request
, &doc_name
, "document-name-supplied");
8520 if ((format
= ippFindAttribute(con
->request
, "document-format",
8521 IPP_TAG_MIMETYPE
)) != NULL
)
8524 * Grab format from client...
8527 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8530 send_ipp_status(con
, IPP_BAD_REQUEST
,
8531 _("Bad document-format \"%s\"."),
8532 format
->values
[0].string
.text
);
8536 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
8538 else if ((default_format
= cupsGetOption("document-format",
8539 printer
->num_options
,
8540 printer
->options
)) != NULL
)
8543 * Use default document format...
8546 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8548 send_ipp_status(con
, IPP_BAD_REQUEST
,
8549 _("Bad document-format \"%s\"."),
8560 strlcpy(super
, "application", sizeof(super
));
8561 strlcpy(type
, "octet-stream", sizeof(type
));
8564 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8567 * Auto-type the file...
8570 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8573 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8574 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8578 filetype
= mimeType(MimeDatabase
, super
, type
);
8580 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8581 filetype
->super
, filetype
->type
);
8583 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
8584 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
8587 filetype
= mimeType(MimeDatabase
, super
, type
);
8591 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8594 * Replace the document-format attribute value with the auto-typed or
8598 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8602 ippSetString(con
->request
, &format
, 0, mimetype
);
8604 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8605 "document-format", NULL
, mimetype
);
8609 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8610 _("Unsupported document-format \"%s\"."),
8611 format
? format
->values
[0].string
.text
:
8612 "application/octet-stream");
8613 cupsdLogMessage(CUPSD_LOG_INFO
,
8614 "Hint: Do you have the raw file printing rules enabled?");
8617 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8618 "document-format", NULL
, format
->values
[0].string
.text
);
8624 * Read any embedded job ticket info from PS files...
8627 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8628 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8629 !_cups_strcasecmp(filetype
->type
, "pdf")))
8630 read_job_ticket(con
);
8633 * Create the job object...
8636 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8640 * Update quota data...
8643 if (stat(con
->filename
, &fileinfo
))
8646 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8648 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8650 job
->koctets
+= kbytes
;
8652 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8653 attr
->values
[0].integer
+= kbytes
;
8656 * Add the job file...
8659 if (add_file(con
, job
, filetype
, compression
))
8662 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
8663 if (rename(con
->filename
, filename
))
8665 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
8667 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
8671 cupsdClearString(&con
->filename
);
8674 * See if we need to add the ending sheet...
8677 if (cupsdTimeoutJob(job
))
8681 * Log and save the job...
8684 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8685 "File of type %s/%s queued by \"%s\".",
8686 filetype
->super
, filetype
->type
, job
->username
);
8687 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8688 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8689 job
->dest
, job
->username
);
8692 * Start the job if possible...
8700 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8702 * This function only gets called when printing a single PDF or PostScript
8703 * file using the Print-Job operation. It doesn't work for Create-Job +
8704 * Send-File, since the job attributes need to be set at job creation
8705 * time for banners to work. The embedded job ticket stuff is here
8706 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8707 * options and IPP attributes which otherwise would be lost.
8709 * The format of a job ticket is simple:
8711 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8713 * %cupsJobTicket: attr1=value1
8714 * %cupsJobTicket: attr2=value2
8716 * %cupsJobTicket: attrN=valueN
8718 * Job ticket lines must appear immediately after the first line that
8719 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8720 * stops looking for job ticket info when it finds a line that does not begin
8721 * with "%cupsJobTicket:".
8723 * The maximum length of a job ticket line, including the prefix, is
8724 * 255 characters to conform with the Adobe DSC.
8726 * Read-only attributes are rejected with a notice to the error log in
8727 * case a malicious user tries anything. Since the job ticket is read
8728 * prior to attribute validation in print_job(), job ticket attributes
8729 * will go through the same validation as IPP attributes...
8733 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8735 cups_file_t
*fp
; /* File to read from */
8736 char line
[256]; /* Line data */
8737 int num_options
; /* Number of options */
8738 cups_option_t
*options
; /* Options */
8739 ipp_t
*ticket
; /* New attributes */
8740 ipp_attribute_t
*attr
, /* Current attribute */
8741 *attr2
, /* Job attribute */
8742 *prev2
; /* Previous job attribute */
8746 * First open the print file...
8749 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8751 cupsdLogMessage(CUPSD_LOG_ERROR
,
8752 "Unable to open print file for job ticket - %s",
8758 * Skip the first line...
8761 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8763 cupsdLogMessage(CUPSD_LOG_ERROR
,
8764 "Unable to read from print file for job ticket - %s",
8770 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8773 * Not a DSC-compliant file, so no job ticket info will be available...
8781 * Read job ticket info from the file...
8787 while (cupsFileGets(fp
, line
, sizeof(line
)))
8790 * Stop at the first non-ticket line...
8793 if (strncmp(line
, "%cupsJobTicket:", 15))
8797 * Add the options to the option array...
8800 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8804 * Done with the file; see if we have any options...
8809 if (num_options
== 0)
8813 * OK, convert the options to an attribute list, and apply them to
8818 cupsEncodeOptions(ticket
, num_options
, options
);
8821 * See what the user wants to change.
8824 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8826 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8829 if (!strncmp(attr
->name
, "date-time-at-", 13) ||
8830 !strcmp(attr
->name
, "job-impressions-completed") ||
8831 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8832 !strncmp(attr
->name
, "job-k-octets", 12) ||
8833 !strcmp(attr
->name
, "job-id") ||
8834 !strcmp(attr
->name
, "job-originating-host-name") ||
8835 !strcmp(attr
->name
, "job-originating-user-name") ||
8836 !strcmp(attr
->name
, "job-pages-completed") ||
8837 !strcmp(attr
->name
, "job-printer-uri") ||
8838 !strncmp(attr
->name
, "job-state", 9) ||
8839 !strcmp(attr
->name
, "job-uri") ||
8840 !strncmp(attr
->name
, "time-at-", 8))
8841 continue; /* Read-only attrs */
8843 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8844 IPP_TAG_ZERO
)) != NULL
)
8847 * Some other value; first free the old value...
8850 if (con
->request
->attrs
== attr2
)
8852 con
->request
->attrs
= attr2
->next
;
8857 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8858 if (prev2
->next
== attr2
)
8860 prev2
->next
= attr2
->next
;
8865 if (con
->request
->last
== attr2
)
8866 con
->request
->last
= prev2
;
8868 ippDeleteAttribute(NULL
, attr2
);
8872 * Add new option by copying it...
8875 ippCopyAttribute(con
->request
, attr
, 0);
8879 * Then free the attribute list and option array...
8883 cupsFreeOptions(num_options
, options
);
8888 * 'reject_jobs()' - Reject print jobs to a printer.
8892 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8893 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8895 http_status_t status
; /* Policy status */
8896 cups_ptype_t dtype
; /* Destination type (printer/class) */
8897 cupsd_printer_t
*printer
; /* Printer data */
8898 ipp_attribute_t
*attr
; /* printer-state-message text */
8901 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8902 con
->number
, uri
->values
[0].string
.text
);
8905 * Is the destination valid?
8908 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8914 send_ipp_status(con
, IPP_NOT_FOUND
,
8915 _("The printer or class does not exist."));
8923 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8925 send_http_error(con
, status
, printer
);
8930 * Reject jobs sent to the printer...
8933 printer
->accepting
= 0;
8935 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8936 IPP_TAG_TEXT
)) == NULL
)
8937 strlcpy(printer
->state_message
, "Rejecting Jobs",
8938 sizeof(printer
->state_message
));
8940 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8941 sizeof(printer
->state_message
));
8943 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8944 "No longer accepting jobs.");
8946 if (dtype
& CUPS_PRINTER_CLASS
)
8948 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8950 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8951 printer
->name
, get_username(con
));
8955 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8957 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8958 printer
->name
, get_username(con
));
8962 * Everything was ok, so return OK status...
8965 con
->response
->request
.status
.status_code
= IPP_OK
;
8970 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8974 release_held_new_jobs(
8975 cupsd_client_t
*con
, /* I - Connection */
8976 ipp_attribute_t
*uri
) /* I - Printer URI */
8978 http_status_t status
; /* Policy status */
8979 cups_ptype_t dtype
; /* Destination type (printer/class) */
8980 cupsd_printer_t
*printer
; /* Printer data */
8983 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8984 con
->number
, uri
->values
[0].string
.text
);
8987 * Is the destination valid?
8990 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8996 send_ipp_status(con
, IPP_NOT_FOUND
,
8997 _("The printer or class does not exist."));
9005 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
9007 send_http_error(con
, status
, printer
);
9012 * Hold pending/new jobs sent to the printer...
9015 printer
->holding_new_jobs
= 0;
9017 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
9019 if (dtype
& CUPS_PRINTER_CLASS
)
9020 cupsdLogMessage(CUPSD_LOG_INFO
,
9021 "Class \"%s\" now printing pending/new jobs (\"%s\").",
9022 printer
->name
, get_username(con
));
9024 cupsdLogMessage(CUPSD_LOG_INFO
,
9025 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
9026 printer
->name
, get_username(con
));
9031 * Everything was ok, so return OK status...
9034 con
->response
->request
.status
.status_code
= IPP_OK
;
9039 * 'release_job()' - Release a held print job.
9043 release_job(cupsd_client_t
*con
, /* I - Client connection */
9044 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9046 ipp_attribute_t
*attr
; /* Current attribute */
9047 int jobid
; /* Job ID */
9048 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9049 username
[HTTP_MAX_URI
], /* Username portion of URI */
9050 host
[HTTP_MAX_URI
], /* Host portion of URI */
9051 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9052 int port
; /* Port portion of URI */
9053 cupsd_job_t
*job
; /* Job information */
9056 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
9057 con
->number
, uri
->values
[0].string
.text
);
9060 * See if we have a job URI or a printer URI...
9063 if (!strcmp(uri
->name
, "printer-uri"))
9066 * Got a printer URI; see if we also have a job-id attribute...
9069 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9070 IPP_TAG_INTEGER
)) == NULL
)
9072 send_ipp_status(con
, IPP_BAD_REQUEST
,
9073 _("Got a printer-uri attribute but no job-id."));
9077 jobid
= attr
->values
[0].integer
;
9082 * Got a job URI; parse it to get the job ID...
9085 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9086 sizeof(scheme
), username
, sizeof(username
), host
,
9087 sizeof(host
), &port
, resource
, sizeof(resource
));
9089 if (strncmp(resource
, "/jobs/", 6))
9095 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9096 uri
->values
[0].string
.text
);
9100 jobid
= atoi(resource
+ 6);
9104 * See if the job exists...
9107 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9110 * Nope - return a "not found" error...
9113 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9118 * See if job is "held"...
9121 if (job
->state_value
!= IPP_JOB_HELD
)
9124 * Nope - return a "not possible" error...
9127 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
9132 * See if the job is owned by the requesting user...
9135 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9137 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9138 cupsdFindDest(job
->dest
));
9143 * Reset the job-hold-until value to "no-hold"...
9146 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9147 IPP_TAG_KEYWORD
)) == NULL
)
9148 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9152 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
9153 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
9155 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
9156 "Job job-hold-until value changed by user.");
9157 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9161 * Release the job and return...
9164 cupsdReleaseJob(job
);
9166 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
9167 "Job released by user.");
9169 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
9171 con
->response
->request
.status
.status_code
= IPP_OK
;
9178 * 'renew_subscription()' - Renew an existing subscription...
9183 cupsd_client_t
*con
, /* I - Client connection */
9184 int sub_id
) /* I - Subscription ID */
9186 http_status_t status
; /* Policy status */
9187 cupsd_subscription_t
*sub
; /* Subscription */
9188 ipp_attribute_t
*lease
; /* notify-lease-duration */
9191 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9192 "renew_subscription(con=%p[%d], sub_id=%d)",
9193 con
, con
->number
, sub_id
);
9196 * Is the subscription ID valid?
9199 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
9202 * Bad subscription ID...
9205 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
9213 * Job subscriptions cannot be renewed...
9216 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9217 _("Job subscriptions cannot be renewed."));
9225 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
9227 con
, sub
->owner
)) != HTTP_OK
)
9229 send_http_error(con
, status
, sub
->dest
);
9234 * Renew the subscription...
9237 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
9240 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
9242 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9244 cupsdLogMessage(CUPSD_LOG_INFO
,
9245 "renew_subscription: Limiting notify-lease-duration to "
9248 sub
->lease
= MaxLeaseDuration
;
9251 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9253 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9255 con
->response
->request
.status
.status_code
= IPP_OK
;
9257 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9258 "notify-lease-duration", sub
->lease
);
9263 * 'restart_job()' - Restart an old print job.
9267 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9268 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9270 ipp_attribute_t
*attr
; /* Current attribute */
9271 int jobid
; /* Job ID */
9272 cupsd_job_t
*job
; /* Job information */
9273 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9274 username
[HTTP_MAX_URI
], /* Username portion of URI */
9275 host
[HTTP_MAX_URI
], /* Host portion of URI */
9276 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9277 int port
; /* Port portion of URI */
9280 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9281 con
->number
, uri
->values
[0].string
.text
);
9284 * See if we have a job URI or a printer URI...
9287 if (!strcmp(uri
->name
, "printer-uri"))
9290 * Got a printer URI; see if we also have a job-id attribute...
9293 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9294 IPP_TAG_INTEGER
)) == NULL
)
9296 send_ipp_status(con
, IPP_BAD_REQUEST
,
9297 _("Got a printer-uri attribute but no job-id."));
9301 jobid
= attr
->values
[0].integer
;
9306 * Got a job URI; parse it to get the job ID...
9309 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9310 sizeof(scheme
), username
, sizeof(username
), host
,
9311 sizeof(host
), &port
, resource
, sizeof(resource
));
9313 if (strncmp(resource
, "/jobs/", 6))
9319 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9320 uri
->values
[0].string
.text
);
9324 jobid
= atoi(resource
+ 6);
9328 * See if the job exists...
9331 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9334 * Nope - return a "not found" error...
9337 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9342 * See if job is in any of the "completed" states...
9345 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9348 * Nope - return a "not possible" error...
9351 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
9357 * See if we have retained the job files...
9362 if (!job
->attrs
|| job
->num_files
== 0)
9365 * Nope - return a "not possible" error...
9368 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9369 _("Job #%d cannot be restarted - no files."), jobid
);
9374 * See if the job is owned by the requesting user...
9377 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9379 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9380 cupsdFindDest(job
->dest
));
9385 * See if the job-hold-until attribute is specified...
9388 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
9389 IPP_TAG_KEYWORD
)) == NULL
)
9390 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
9392 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
9395 * Return the job to a held state...
9398 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
9399 "Restarted by \"%s\" with job-hold-until=%s.",
9400 username
, attr
->values
[0].string
.text
);
9401 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
9403 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
9404 NULL
, job
, "Job restarted by user with job-hold-until=%s",
9405 attr
->values
[0].string
.text
);
9410 * Restart the job...
9413 cupsdRestartJob(job
);
9417 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9419 con
->response
->request
.status
.status_code
= IPP_OK
;
9424 * 'save_auth_info()' - Save authentication information for a job.
9429 cupsd_client_t
*con
, /* I - Client connection */
9430 cupsd_job_t
*job
, /* I - Job */
9431 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9433 int i
; /* Looping var */
9434 char filename
[1024]; /* Job authentication filename */
9435 cups_file_t
*fp
; /* Job authentication file */
9436 char line
[65536]; /* Line for file */
9437 cupsd_printer_t
*dest
; /* Destination printer/class */
9441 * This function saves the in-memory authentication information for
9442 * a job so that it can be used to authenticate with a remote host.
9443 * The information is stored in a file that is readable only by the
9444 * root user. The fields are Base-64 encoded, each on a separate line,
9445 * followed by random number (up to 1024) of newlines to limit the
9446 * amount of information that is exposed.
9448 * Because of the potential for exposing of authentication information,
9449 * this functionality is only enabled when running cupsd as root.
9451 * This caching only works for the Basic and BasicDigest authentication
9452 * types. Digest authentication cannot be cached this way, and in
9453 * the future Kerberos authentication may make all of this obsolete.
9455 * Authentication information is saved whenever an authenticated
9456 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9459 * This information is deleted after a job is completed or canceled,
9460 * so reprints may require subsequent re-authentication.
9466 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9470 * Create the authentication file and change permissions...
9473 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9474 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9476 cupsdLogMessage(CUPSD_LOG_ERROR
,
9477 "Unable to save authentication info to \"%s\" - %s",
9478 filename
, strerror(errno
));
9482 fchown(cupsFileNumber(fp
), 0, 0);
9483 fchmod(cupsFileNumber(fp
), 0400);
9485 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9488 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9490 cupsdClearString(job
->auth_env
+ i
);
9492 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9495 * Write 1 to 3 auth values...
9499 i
< auth_info
->num_values
&&
9500 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9503 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9505 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9506 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9509 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9510 auth_info
->values
[i
].string
.text
);
9512 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9513 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9514 auth_info
->values
[i
].string
.text
);
9515 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9516 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9517 auth_info
->values
[i
].string
.text
);
9518 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9519 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9520 auth_info
->values
[i
].string
.text
);
9521 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9522 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9523 auth_info
->values
[i
].string
.text
);
9528 else if (auth_info
&& auth_info
->num_values
== 2 &&
9529 dest
->num_auth_info_required
== 1 &&
9530 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9533 * Allow fallback to username+password for Kerberized queues...
9536 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9537 cupsFilePutConf(fp
, "username", line
);
9539 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9540 auth_info
->values
[0].string
.text
);
9542 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9543 cupsFilePutConf(fp
, "password", line
);
9545 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9546 auth_info
->values
[1].string
.text
);
9548 else if (con
->username
[0])
9551 * Write the authenticated username...
9554 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9555 cupsFilePutConf(fp
, "username", line
);
9557 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9560 * Write the authenticated password...
9563 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9564 cupsFilePutConf(fp
, "password", line
);
9566 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9570 if (con
->gss_uid
> 0)
9572 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9573 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9575 #endif /* HAVE_GSSAPI */
9578 * Write a random number of newlines to the end of the file...
9581 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9582 cupsFilePutChar(fp
, '\n');
9585 * Close the file and return...
9593 * 'send_document()' - Send a file to a printer or class.
9597 send_document(cupsd_client_t
*con
, /* I - Client connection */
9598 ipp_attribute_t
*uri
) /* I - Printer URI */
9600 ipp_attribute_t
*attr
; /* Current attribute */
9601 ipp_attribute_t
*format
; /* Request's document-format attribute */
9602 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9603 const char *default_format
;/* document-format-default value */
9604 int jobid
; /* Job ID number */
9605 cupsd_job_t
*job
; /* Current job */
9606 char job_uri
[HTTP_MAX_URI
],
9608 scheme
[HTTP_MAX_URI
],
9609 /* Method portion of URI */
9610 username
[HTTP_MAX_URI
],
9611 /* Username portion of URI */
9613 /* Host portion of URI */
9614 resource
[HTTP_MAX_URI
];
9615 /* Resource portion of URI */
9616 int port
; /* Port portion of URI */
9617 mime_type_t
*filetype
; /* Type of file */
9618 char super
[MIME_MAX_SUPER
],
9619 /* Supertype of file */
9620 type
[MIME_MAX_TYPE
],
9621 /* Subtype of file */
9622 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9623 /* Textual name of mime type */
9624 char filename
[1024]; /* Job filename */
9625 cupsd_printer_t
*printer
; /* Current printer */
9626 struct stat fileinfo
; /* File information */
9627 int kbytes
; /* Size of file */
9628 int compression
; /* Type of compression */
9629 int start_job
; /* Start the job? */
9632 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9633 con
->number
, uri
->values
[0].string
.text
);
9636 * See if we have a job URI or a printer URI...
9639 if (!strcmp(uri
->name
, "printer-uri"))
9642 * Got a printer URI; see if we also have a job-id attribute...
9645 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9646 IPP_TAG_INTEGER
)) == NULL
)
9648 send_ipp_status(con
, IPP_BAD_REQUEST
,
9649 _("Got a printer-uri attribute but no job-id."));
9653 jobid
= attr
->values
[0].integer
;
9658 * Got a job URI; parse it to get the job ID...
9661 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9662 sizeof(scheme
), username
, sizeof(username
), host
,
9663 sizeof(host
), &port
, resource
, sizeof(resource
));
9665 if (strncmp(resource
, "/jobs/", 6))
9671 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9672 uri
->values
[0].string
.text
);
9676 jobid
= atoi(resource
+ 6);
9680 * See if the job exists...
9683 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9686 * Nope - return a "not found" error...
9689 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9693 printer
= cupsdFindDest(job
->dest
);
9696 * See if the job is owned by the requesting user...
9699 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9701 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9702 cupsdFindDest(job
->dest
));
9707 * OK, see if the client is sending the document compressed - CUPS
9708 * only supports "none" and "gzip".
9711 compression
= CUPS_FILE_NONE
;
9713 if ((attr
= ippFindAttribute(con
->request
, "compression",
9714 IPP_TAG_KEYWORD
)) != NULL
)
9716 if (strcmp(attr
->values
[0].string
.text
, "none")
9718 && strcmp(attr
->values
[0].string
.text
, "gzip")
9719 #endif /* HAVE_LIBZ */
9722 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9723 attr
->values
[0].string
.text
);
9724 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9725 "compression", NULL
, attr
->values
[0].string
.text
);
9730 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9731 compression
= CUPS_FILE_GZIP
;
9732 #endif /* HAVE_LIBZ */
9736 * Do we have a file to print?
9739 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9740 IPP_TAG_BOOLEAN
)) == NULL
)
9742 send_ipp_status(con
, IPP_BAD_REQUEST
,
9743 _("Missing last-document attribute in request."));
9750 * Check for an empty request with "last-document" set to true, which is
9751 * used to close an "open" job by RFC 2911, section 3.3.2.
9754 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9757 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9762 * Is it a format we support?
9767 if ((format
= ippFindAttribute(con
->request
, "document-format",
9768 IPP_TAG_MIMETYPE
)) != NULL
)
9771 * Grab format from client...
9774 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9777 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9778 format
->values
[0].string
.text
);
9782 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
9784 else if ((default_format
= cupsGetOption("document-format",
9785 printer
->num_options
,
9786 printer
->options
)) != NULL
)
9789 * Use default document format...
9792 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9794 send_ipp_status(con
, IPP_BAD_REQUEST
,
9795 _("Bad document-format-default \"%s\"."), default_format
);
9802 * No document format attribute? Auto-type it!
9805 strlcpy(super
, "application", sizeof(super
));
9806 strlcpy(type
, "octet-stream", sizeof(type
));
9809 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9812 * Auto-type the file...
9815 ipp_attribute_t
*doc_name
; /* document-name attribute */
9818 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9820 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9821 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9822 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9826 filetype
= mimeType(MimeDatabase
, super
, type
);
9829 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9830 filetype
->super
, filetype
->type
);
9832 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
9833 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
9836 filetype
= mimeType(MimeDatabase
, super
, type
);
9841 * Replace the document-format attribute value with the auto-typed or
9845 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9848 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9849 IPP_TAG_MIMETYPE
)) != NULL
)
9850 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9852 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9853 "document-format", NULL
, mimetype
);
9857 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9858 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9859 cupsdLogMessage(CUPSD_LOG_INFO
,
9860 "Hint: Do you have the raw file printing rules enabled?");
9863 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9864 "document-format", NULL
, format
->values
[0].string
.text
);
9869 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9871 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9874 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9875 _("Unsupported document-format \"%s\"."), mimetype
);
9877 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9878 "document-format", NULL
, mimetype
);
9884 * Add the file to the job...
9887 if (add_file(con
, job
, filetype
, compression
))
9890 if ((attr
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
)) != NULL
)
9891 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name-supplied", NULL
, ippGetString(attr
, 0, NULL
));
9893 if (stat(con
->filename
, &fileinfo
))
9896 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9898 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9900 job
->koctets
+= kbytes
;
9902 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9903 attr
->values
[0].integer
+= kbytes
;
9905 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
9906 if (rename(con
->filename
, filename
))
9908 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
9910 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
9914 cupsdClearString(&con
->filename
);
9916 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9917 filetype
->super
, filetype
->type
, job
->username
);
9920 * Start the job if this is the last document...
9925 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9926 IPP_TAG_BOOLEAN
)) != NULL
&&
9927 attr
->values
[0].boolean
)
9930 * See if we need to add the ending sheet...
9933 if (cupsdTimeoutJob(job
))
9936 if (job
->state_value
== IPP_JOB_STOPPED
)
9938 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9939 job
->state_value
= IPP_JOB_PENDING
;
9941 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9943 else if (job
->state_value
== IPP_JOB_HELD
)
9945 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9946 IPP_TAG_KEYWORD
)) == NULL
)
9947 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9949 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9951 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9952 job
->state_value
= IPP_JOB_PENDING
;
9954 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9957 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9961 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9967 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9968 IPP_TAG_KEYWORD
)) == NULL
)
9969 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9971 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9973 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9974 job
->state_value
= IPP_JOB_HELD
;
9975 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9977 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9980 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9987 * Fill in the response info...
9990 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9991 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9992 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9995 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9997 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9999 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
10000 NULL
, job
->reasons
->values
[0].string
.text
);
10002 con
->response
->request
.status
.status_code
= IPP_OK
;
10005 * Start the job if necessary...
10014 * 'send_http_error()' - Send a HTTP error back to the IPP client.
10019 cupsd_client_t
*con
, /* I - Client connection */
10020 http_status_t status
, /* I - HTTP status code */
10021 cupsd_printer_t
*printer
) /* I - Printer, if any */
10023 ipp_attribute_t
*uri
; /* Request URI, if any */
10026 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
10027 IPP_TAG_URI
)) == NULL
)
10028 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
10030 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
10031 "[Client %d] Returning HTTP %s for %s (%s) from %s",
10032 con
->number
, httpStatus(status
),
10034 ippOpString(con
->request
->request
.op
.operation_id
) :
10036 uri
? uri
->values
[0].string
.text
: "no URI",
10037 con
->http
->hostname
);
10041 int auth_type
; /* Type of authentication required */
10044 auth_type
= CUPSD_AUTH_NONE
;
10046 if (status
== HTTP_UNAUTHORIZED
&&
10047 printer
->num_auth_info_required
> 0 &&
10048 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
10050 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
10051 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
10052 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
10055 * Creating and authenticating jobs requires Kerberos...
10058 auth_type
= CUPSD_AUTH_NEGOTIATE
;
10063 * Use policy/location-defined authentication requirements...
10066 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
10067 cupsd_location_t
*auth
; /* Pointer to authentication element */
10070 if (printer
->type
& CUPS_PRINTER_CLASS
)
10071 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
10073 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
10075 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
10076 auth
->type
== CUPSD_AUTH_NONE
)
10077 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
10079 con
->request
->request
.op
.operation_id
:
10084 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
10085 auth_type
= cupsdDefaultAuthType();
10087 auth_type
= auth
->type
;
10091 cupsdSendError(con
, status
, auth_type
);
10094 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
10096 ippDelete(con
->response
);
10097 con
->response
= NULL
;
10104 * 'send_ipp_status()' - Send a status back to the IPP client.
10108 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
10109 ipp_status_t status
, /* I - IPP status code */
10110 const char *message
,/* I - Status message */
10111 ...) /* I - Additional args as needed */
10113 va_list ap
; /* Pointer to additional args */
10114 char formatted
[1024]; /* Formatted errror message */
10117 va_start(ap
, message
);
10118 vsnprintf(formatted
, sizeof(formatted
),
10119 _cupsLangString(con
->language
, message
), ap
);
10122 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
10123 ippOpString(con
->request
->request
.op
.operation_id
),
10124 ippErrorString(status
), formatted
);
10126 con
->response
->request
.status
.status_code
= status
;
10128 if (ippFindAttribute(con
->response
, "attributes-charset",
10129 IPP_TAG_ZERO
) == NULL
)
10130 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
10131 "attributes-charset", NULL
, "utf-8");
10133 if (ippFindAttribute(con
->response
, "attributes-natural-language",
10134 IPP_TAG_ZERO
) == NULL
)
10135 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
10136 "attributes-natural-language", NULL
, DefaultLanguage
);
10138 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
10139 "status-message", NULL
, formatted
);
10144 * 'set_default()' - Set the default destination...
10148 set_default(cupsd_client_t
*con
, /* I - Client connection */
10149 ipp_attribute_t
*uri
) /* I - Printer URI */
10151 http_status_t status
; /* Policy status */
10152 cups_ptype_t dtype
; /* Destination type (printer/class) */
10153 cupsd_printer_t
*printer
, /* Printer */
10154 *oldprinter
; /* Old default printer */
10157 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
10158 con
->number
, uri
->values
[0].string
.text
);
10161 * Is the destination valid?
10164 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10170 send_ipp_status(con
, IPP_NOT_FOUND
,
10171 _("The printer or class does not exist."));
10179 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
10181 send_http_error(con
, status
, NULL
);
10186 * Set it as the default...
10189 oldprinter
= DefaultPrinter
;
10190 DefaultPrinter
= printer
;
10193 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
10194 "%s is no longer the default printer.", oldprinter
->name
);
10196 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
10197 "%s is now the default printer.", printer
->name
);
10199 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
10200 CUPSD_DIRTY_PRINTCAP
);
10202 cupsdLogMessage(CUPSD_LOG_INFO
,
10203 "Default destination set to \"%s\" by \"%s\".",
10204 printer
->name
, get_username(con
));
10207 * Everything was ok, so return OK status...
10210 con
->response
->request
.status
.status_code
= IPP_OK
;
10215 * 'set_job_attrs()' - Set job attributes.
10219 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10220 ipp_attribute_t
*uri
) /* I - Job URI */
10222 ipp_attribute_t
*attr
, /* Current attribute */
10223 *attr2
; /* Job attribute */
10224 int jobid
; /* Job ID */
10225 cupsd_job_t
*job
; /* Current job */
10226 char scheme
[HTTP_MAX_URI
],
10227 /* Method portion of URI */
10228 username
[HTTP_MAX_URI
],
10229 /* Username portion of URI */
10230 host
[HTTP_MAX_URI
],
10231 /* Host portion of URI */
10232 resource
[HTTP_MAX_URI
];
10233 /* Resource portion of URI */
10234 int port
; /* Port portion of URI */
10235 int event
; /* Events? */
10236 int check_jobs
; /* Check jobs? */
10239 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10240 con
->number
, uri
->values
[0].string
.text
);
10243 * Start with "everything is OK" status...
10246 con
->response
->request
.status
.status_code
= IPP_OK
;
10249 * See if we have a job URI or a printer URI...
10252 if (!strcmp(uri
->name
, "printer-uri"))
10255 * Got a printer URI; see if we also have a job-id attribute...
10258 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10259 IPP_TAG_INTEGER
)) == NULL
)
10261 send_ipp_status(con
, IPP_BAD_REQUEST
,
10262 _("Got a printer-uri attribute but no job-id."));
10266 jobid
= attr
->values
[0].integer
;
10271 * Got a job URI; parse it to get the job ID...
10274 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10275 sizeof(scheme
), username
, sizeof(username
), host
,
10276 sizeof(host
), &port
, resource
, sizeof(resource
));
10278 if (strncmp(resource
, "/jobs/", 6))
10284 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
10285 uri
->values
[0].string
.text
);
10289 jobid
= atoi(resource
+ 6);
10293 * See if the job exists...
10296 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10299 * Nope - return a "not found" error...
10302 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
10307 * See if the job has been completed...
10310 if (job
->state_value
> IPP_JOB_STOPPED
)
10313 * Return a "not-possible" error...
10316 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10317 _("Job #%d is finished and cannot be altered."), jobid
);
10322 * See if the job is owned by the requesting user...
10325 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10327 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
10328 cupsdFindDest(job
->dest
));
10333 * See what the user wants to change.
10341 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10343 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10346 if (!strcmp(attr
->name
, "attributes-charset") ||
10347 !strcmp(attr
->name
, "attributes-natural-language") ||
10348 !strncmp(attr
->name
, "date-time-at-", 13) ||
10349 !strncmp(attr
->name
, "document-compression", 20) ||
10350 !strncmp(attr
->name
, "document-format", 15) ||
10351 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10352 !strcmp(attr
->name
, "job-document-access-errors") ||
10353 !strcmp(attr
->name
, "job-id") ||
10354 !strcmp(attr
->name
, "job-impressions-completed") ||
10355 !strcmp(attr
->name
, "job-k-octets-completed") ||
10356 !strcmp(attr
->name
, "job-media-sheets-completed") ||
10357 !strcmp(attr
->name
, "job-originating-host-name") ||
10358 !strcmp(attr
->name
, "job-originating-user-name") ||
10359 !strcmp(attr
->name
, "job-pages-completed") ||
10360 !strcmp(attr
->name
, "job-printer-up-time") ||
10361 !strcmp(attr
->name
, "job-printer-uri") ||
10362 !strcmp(attr
->name
, "job-sheets") ||
10363 !strcmp(attr
->name
, "job-state-message") ||
10364 !strcmp(attr
->name
, "job-state-reasons") ||
10365 !strcmp(attr
->name
, "job-uri") ||
10366 !strcmp(attr
->name
, "number-of-documents") ||
10367 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10368 !strcmp(attr
->name
, "output-device-assigned") ||
10369 !strncmp(attr
->name
, "time-at-", 8))
10375 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10376 _("%s cannot be changed."), attr
->name
);
10378 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10379 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10383 if (!strcmp(attr
->name
, "job-priority"))
10386 * Change the job priority...
10389 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10391 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
10393 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10394 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10396 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10398 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10399 _("Job is completed and cannot be changed."));
10402 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10404 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10405 attr
->values
[0].integer
);
10406 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10409 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10410 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10413 else if (!strcmp(attr
->name
, "job-state"))
10416 * Change the job state...
10419 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10421 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
10423 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10424 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10428 switch (attr
->values
[0].integer
)
10430 case IPP_JOB_PENDING
:
10431 case IPP_JOB_HELD
:
10432 if (job
->state_value
> IPP_JOB_HELD
)
10434 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10435 _("Job state cannot be changed."));
10438 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10440 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10441 attr
->values
[0].integer
);
10442 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
10447 case IPP_JOB_PROCESSING
:
10448 case IPP_JOB_STOPPED
:
10449 if (job
->state_value
!= attr
->values
[0].integer
)
10451 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10452 _("Job state cannot be changed."));
10457 case IPP_JOB_CANCELED
:
10458 case IPP_JOB_ABORTED
:
10459 case IPP_JOB_COMPLETED
:
10460 if (job
->state_value
> IPP_JOB_PROCESSING
)
10462 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10463 _("Job state cannot be changed."));
10466 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10468 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10469 attr
->values
[0].integer
);
10470 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10472 "Job state changed by \"%s\"", username
);
10479 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10481 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10482 IPP_TAG_ZERO
)) != NULL
)
10485 * Some other value; first free the old value...
10488 if (job
->attrs
->prev
)
10489 job
->attrs
->prev
->next
= attr2
->next
;
10491 job
->attrs
->attrs
= attr2
->next
;
10493 if (job
->attrs
->last
== attr2
)
10494 job
->attrs
->last
= job
->attrs
->prev
;
10496 ippDeleteAttribute(NULL
, attr2
);
10499 * Then copy the attribute...
10502 ippCopyAttribute(job
->attrs
, attr
, 0);
10505 * See if the job-name or job-hold-until is being changed.
10508 if (!strcmp(attr
->name
, "job-hold-until"))
10510 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10511 attr
->values
[0].string
.text
);
10512 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10514 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10516 cupsdReleaseJob(job
);
10520 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10521 "Job held by \"%s\".", username
);
10523 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10526 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10529 * Delete the attribute...
10532 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10533 IPP_TAG_ZERO
)) != NULL
)
10535 if (job
->attrs
->prev
)
10536 job
->attrs
->prev
->next
= attr2
->next
;
10538 job
->attrs
->attrs
= attr2
->next
;
10540 if (attr2
== job
->attrs
->last
)
10541 job
->attrs
->last
= job
->attrs
->prev
;
10543 ippDeleteAttribute(NULL
, attr2
);
10545 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10551 * Add new option by copying it...
10554 ippCopyAttribute(job
->attrs
, attr
, 0);
10556 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10565 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10568 * Send events as needed...
10571 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10572 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10573 cupsdFindDest(job
->dest
), job
,
10574 "Job priority changed by user.");
10576 if (event
& CUPSD_EVENT_JOB_STATE
)
10577 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10578 job
->state_value
== IPP_JOB_HELD
?
10579 "Job held by user." : "Job restarted by user.");
10581 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10582 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10583 "Job options changed by user.");
10586 * Start jobs if possible...
10595 * 'set_printer_attrs()' - Set printer attributes.
10599 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10600 ipp_attribute_t
*uri
) /* I - Printer */
10602 http_status_t status
; /* Policy status */
10603 cups_ptype_t dtype
; /* Destination type (printer/class) */
10604 cupsd_printer_t
*printer
; /* Printer/class */
10605 ipp_attribute_t
*attr
; /* Printer attribute */
10606 int changed
= 0; /* Was anything changed? */
10609 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10610 con
->number
, uri
->values
[0].string
.text
);
10613 * Is the destination valid?
10616 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10622 send_ipp_status(con
, IPP_NOT_FOUND
,
10623 _("The printer or class does not exist."));
10631 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10633 send_http_error(con
, status
, printer
);
10638 * Return a list of attributes that can be set via Set-Printer-Attributes.
10641 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10642 IPP_TAG_TEXT
)) != NULL
)
10644 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10648 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
10650 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
10654 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
10656 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
10660 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
10662 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
10666 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10667 IPP_TAG_TEXT
)) != NULL
)
10669 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10674 * Update the printer attributes and return...
10679 printer
->config_time
= time(NULL
);
10681 cupsdSetPrinterAttrs(printer
);
10682 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10684 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10685 "Printer \"%s\" description or location changed by \"%s\".",
10686 printer
->name
, get_username(con
));
10688 cupsdLogMessage(CUPSD_LOG_INFO
,
10689 "Printer \"%s\" description or location changed by \"%s\".",
10690 printer
->name
, get_username(con
));
10693 con
->response
->request
.status
.status_code
= IPP_OK
;
10698 * 'set_printer_defaults()' - Set printer default options from a request.
10701 static int /* O - 1 on success, 0 on failure */
10702 set_printer_defaults(
10703 cupsd_client_t
*con
, /* I - Client connection */
10704 cupsd_printer_t
*printer
) /* I - Printer */
10706 int i
; /* Looping var */
10707 ipp_attribute_t
*attr
; /* Current attribute */
10708 size_t namelen
; /* Length of attribute name */
10709 char name
[256], /* New attribute name */
10710 value
[256]; /* String version of integer attrs */
10713 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10716 * Skip non-printer attributes...
10719 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10722 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10724 if (!strcmp(attr
->name
, "job-sheets-default"))
10727 * Only allow keywords and names...
10730 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10734 * Only allow job-sheets-default to be set when running without a
10735 * system high classification level...
10738 if (Classification
)
10741 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10743 if (attr
->num_values
> 1)
10744 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10746 cupsdSetString(&printer
->job_sheets
[1], "none");
10748 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10750 cupsdFreeStrings(&(printer
->users
));
10752 printer
->deny_users
= 0;
10754 if (attr
->value_tag
== IPP_TAG_NAME
&&
10755 (attr
->num_values
> 1 ||
10756 strcmp(attr
->values
[0].string
.text
, "all")))
10758 for (i
= 0; i
< attr
->num_values
; i
++)
10759 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10762 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10764 cupsdFreeStrings(&(printer
->users
));
10766 printer
->deny_users
= 1;
10768 if (attr
->value_tag
== IPP_TAG_NAME
&&
10769 (attr
->num_values
> 1 ||
10770 strcmp(attr
->values
[0].string
.text
, "none")))
10772 for (i
= 0; i
< attr
->num_values
; i
++)
10773 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10776 else if (!strcmp(attr
->name
, "job-quota-period"))
10778 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10781 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10782 attr
->values
[0].integer
);
10783 cupsdFreeQuotas(printer
);
10785 printer
->quota_period
= attr
->values
[0].integer
;
10787 else if (!strcmp(attr
->name
, "job-k-limit"))
10789 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10792 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10793 attr
->values
[0].integer
);
10794 cupsdFreeQuotas(printer
);
10796 printer
->k_limit
= attr
->values
[0].integer
;
10798 else if (!strcmp(attr
->name
, "job-page-limit"))
10800 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10803 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10804 attr
->values
[0].integer
);
10805 cupsdFreeQuotas(printer
);
10807 printer
->page_limit
= attr
->values
[0].integer
;
10809 else if (!strcmp(attr
->name
, "printer-op-policy"))
10811 cupsd_policy_t
*p
; /* Policy */
10814 if (attr
->value_tag
!= IPP_TAG_NAME
)
10817 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10819 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10820 "Setting printer-op-policy to \"%s\"...",
10821 attr
->values
[0].string
.text
);
10822 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10823 printer
->op_policy_ptr
= p
;
10827 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10828 _("Unknown printer-op-policy \"%s\"."),
10829 attr
->values
[0].string
.text
);
10833 else if (!strcmp(attr
->name
, "printer-error-policy"))
10835 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10838 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10839 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10840 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10841 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10842 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10844 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10845 _("Unknown printer-error-policy \"%s\"."),
10846 attr
->values
[0].string
.text
);
10850 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10851 "Setting printer-error-policy to \"%s\"...",
10852 attr
->values
[0].string
.text
);
10853 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10857 * Skip any other non-default attributes...
10860 namelen
= strlen(attr
->name
);
10861 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10862 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10866 * OK, anything else must be a user-defined default...
10869 strlcpy(name
, attr
->name
, sizeof(name
));
10870 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10872 switch (attr
->value_tag
)
10874 case IPP_TAG_DELETEATTR
:
10875 printer
->num_options
= cupsRemoveOption(name
,
10876 printer
->num_options
,
10877 &(printer
->options
));
10878 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10879 "Deleting %s", attr
->name
);
10882 case IPP_TAG_NAME
:
10883 case IPP_TAG_TEXT
:
10884 case IPP_TAG_KEYWORD
:
10886 printer
->num_options
= cupsAddOption(name
,
10887 attr
->values
[0].string
.text
,
10888 printer
->num_options
,
10889 &(printer
->options
));
10890 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10891 "Setting %s to \"%s\"...", attr
->name
,
10892 attr
->values
[0].string
.text
);
10895 case IPP_TAG_BOOLEAN
:
10896 printer
->num_options
= cupsAddOption(name
,
10897 attr
->values
[0].boolean
?
10899 printer
->num_options
,
10900 &(printer
->options
));
10901 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10902 "Setting %s to %s...", attr
->name
,
10903 attr
->values
[0].boolean
? "true" : "false");
10906 case IPP_TAG_INTEGER
:
10907 case IPP_TAG_ENUM
:
10908 sprintf(value
, "%d", attr
->values
[0].integer
);
10909 printer
->num_options
= cupsAddOption(name
, value
,
10910 printer
->num_options
,
10911 &(printer
->options
));
10912 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10913 "Setting %s to %s...", attr
->name
, value
);
10916 case IPP_TAG_RANGE
:
10917 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10918 attr
->values
[0].range
.upper
);
10919 printer
->num_options
= cupsAddOption(name
, value
,
10920 printer
->num_options
,
10921 &(printer
->options
));
10922 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10923 "Setting %s to %s...", attr
->name
, value
);
10926 case IPP_TAG_RESOLUTION
:
10927 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10928 attr
->values
[0].resolution
.yres
,
10929 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10931 printer
->num_options
= cupsAddOption(name
, value
,
10932 printer
->num_options
,
10933 &(printer
->options
));
10934 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10935 "Setting %s to %s...", attr
->name
, value
);
10939 /* Do nothing for other values */
10949 * 'start_printer()' - Start a printer.
10953 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10954 ipp_attribute_t
*uri
) /* I - Printer URI */
10956 int i
; /* Temporary variable */
10957 http_status_t status
; /* Policy status */
10958 cups_ptype_t dtype
; /* Destination type (printer/class) */
10959 cupsd_printer_t
*printer
; /* Printer data */
10962 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10963 con
->number
, uri
->values
[0].string
.text
);
10966 * Is the destination valid?
10969 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10975 send_ipp_status(con
, IPP_NOT_FOUND
,
10976 _("The printer or class does not exist."));
10984 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10986 send_http_error(con
, status
, printer
);
10991 * Start the printer...
10994 printer
->state_message
[0] = '\0';
10996 cupsdStartPrinter(printer
, 1);
10998 if (dtype
& CUPS_PRINTER_CLASS
)
10999 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
11000 printer
->name
, get_username(con
));
11002 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
11003 printer
->name
, get_username(con
));
11011 if ((i
= check_quotas(con
, printer
)) < 0)
11013 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
11018 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
11023 * Everything was ok, so return OK status...
11026 con
->response
->request
.status
.status_code
= IPP_OK
;
11031 * 'stop_printer()' - Stop a printer.
11035 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
11036 ipp_attribute_t
*uri
) /* I - Printer URI */
11038 http_status_t status
; /* Policy status */
11039 cups_ptype_t dtype
; /* Destination type (printer/class) */
11040 cupsd_printer_t
*printer
; /* Printer data */
11041 ipp_attribute_t
*attr
; /* printer-state-message attribute */
11044 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
11045 con
->number
, uri
->values
[0].string
.text
);
11048 * Is the destination valid?
11051 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11057 send_ipp_status(con
, IPP_NOT_FOUND
,
11058 _("The printer or class does not exist."));
11066 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11068 send_http_error(con
, status
, printer
);
11073 * Stop the printer...
11076 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
11077 IPP_TAG_TEXT
)) == NULL
)
11078 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
11081 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
11082 sizeof(printer
->state_message
));
11085 cupsdStopPrinter(printer
, 1);
11087 if (dtype
& CUPS_PRINTER_CLASS
)
11088 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
11089 printer
->name
, get_username(con
));
11091 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
11092 printer
->name
, get_username(con
));
11095 * Everything was ok, so return OK status...
11098 con
->response
->request
.status
.status_code
= IPP_OK
;
11103 * 'url_encode_attr()' - URL-encode a string attribute.
11107 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
11108 char *buffer
,/* I - String buffer */
11109 size_t bufsize
)/* I - Size of buffer */
11111 int i
; /* Looping var */
11112 char *bufptr
, /* Pointer into buffer */
11113 *bufend
; /* End of buffer */
11116 strlcpy(buffer
, attr
->name
, bufsize
);
11117 bufptr
= buffer
+ strlen(buffer
);
11118 bufend
= buffer
+ bufsize
- 1;
11120 for (i
= 0; i
< attr
->num_values
; i
++)
11122 if (bufptr
>= bufend
)
11130 if (bufptr
>= bufend
)
11135 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
11137 if (bufptr
>= bufend
)
11148 * 'url_encode_string()' - URL-encode a string.
11151 static char * /* O - End of string */
11152 url_encode_string(const char *s
, /* I - String */
11153 char *buffer
, /* I - String buffer */
11154 size_t bufsize
) /* I - Size of buffer */
11156 char *bufptr
, /* Pointer into buffer */
11157 *bufend
; /* End of buffer */
11158 static const char *hex
= "0123456789ABCDEF";
11163 bufend
= buffer
+ bufsize
- 1;
11165 while (*s
&& bufptr
< bufend
)
11167 if (*s
== ' ' || *s
== '%' || *s
== '+')
11169 if (bufptr
>= (bufend
- 2))
11173 *bufptr
++ = hex
[(*s
>> 4) & 15];
11174 *bufptr
++ = hex
[*s
& 15];
11178 else if (*s
== '\'' || *s
== '\\')
11180 if (bufptr
>= (bufend
- 1))
11197 * 'user_allowed()' - See if a user is allowed to print to a queue.
11200 static int /* O - 0 if not allowed, 1 if allowed */
11201 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
11202 const char *username
) /* I - Username */
11204 struct passwd
*pw
; /* User password data */
11205 char baseuser
[256], /* Base username */
11206 *baseptr
, /* Pointer to "@" in base username */
11207 *name
; /* Current user name */
11210 if (cupsArrayCount(p
->users
) == 0)
11213 if (!strcmp(username
, "root"))
11216 if (strchr(username
, '@'))
11219 * Strip @REALM for username check...
11222 strlcpy(baseuser
, username
, sizeof(baseuser
));
11224 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
11227 username
= baseuser
;
11230 pw
= getpwnam(username
);
11233 for (name
= (char *)cupsArrayFirst(p
->users
);
11235 name
= (char *)cupsArrayNext(p
->users
))
11237 if (name
[0] == '@')
11240 * Check group membership...
11243 if (cupsdCheckGroup(username
, pw
, name
+ 1))
11246 else if (name
[0] == '#')
11252 if (cupsdCheckGroup(username
, pw
, name
))
11255 else if (!_cups_strcasecmp(username
, name
))
11259 return ((name
!= NULL
) != p
->deny_users
);
11264 * 'validate_job()' - Validate printer options and destination.
11268 validate_job(cupsd_client_t
*con
, /* I - Client connection */
11269 ipp_attribute_t
*uri
) /* I - Printer URI */
11271 http_status_t status
; /* Policy status */
11272 ipp_attribute_t
*attr
; /* Current attribute */
11274 ipp_attribute_t
*auth_info
; /* auth-info attribute */
11275 #endif /* HAVE_SSL */
11276 ipp_attribute_t
*format
, /* Document-format attribute */
11277 *name
; /* Job-name attribute */
11278 cups_ptype_t dtype
; /* Destination type (printer/class) */
11279 char super
[MIME_MAX_SUPER
],
11280 /* Supertype of file */
11281 type
[MIME_MAX_TYPE
];
11282 /* Subtype of file */
11283 cupsd_printer_t
*printer
; /* Printer */
11286 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
11287 con
->number
, uri
->values
[0].string
.text
);
11290 * OK, see if the client is sending the document compressed - CUPS
11291 * doesn't support compression yet...
11294 if ((attr
= ippFindAttribute(con
->request
, "compression",
11295 IPP_TAG_KEYWORD
)) != NULL
)
11297 if (strcmp(attr
->values
[0].string
.text
, "none")
11299 && strcmp(attr
->values
[0].string
.text
, "gzip")
11300 #endif /* HAVE_LIBZ */
11303 send_ipp_status(con
, IPP_ATTRIBUTES
,
11304 _("Unsupported 'compression' value \"%s\"."),
11305 attr
->values
[0].string
.text
);
11306 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
11307 "compression", NULL
, attr
->values
[0].string
.text
);
11313 * Is it a format we support?
11316 if ((format
= ippFindAttribute(con
->request
, "document-format",
11317 IPP_TAG_MIMETYPE
)) != NULL
)
11319 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
11322 send_ipp_status(con
, IPP_BAD_REQUEST
,
11323 _("Bad 'document-format' value \"%s\"."),
11324 format
->values
[0].string
.text
);
11328 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
11329 !mimeType(MimeDatabase
, super
, type
))
11331 cupsdLogMessage(CUPSD_LOG_INFO
,
11332 "Hint: Do you have the raw file printing rules enabled?");
11333 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
11334 _("Unsupported 'document-format' value \"%s\"."),
11335 format
->values
[0].string
.text
);
11336 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
11337 "document-format", NULL
, format
->values
[0].string
.text
);
11343 * Is the job-name valid?
11346 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
11348 int bad_name
= 0; /* Is the job-name value bad? */
11350 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
11351 name
->num_values
!= 1)
11358 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
11359 * IPP Everywhere requirements for "name" values...
11362 const unsigned char *nameptr
; /* Pointer into "job-name" attribute */
11364 for (nameptr
= (unsigned char *)name
->values
[0].string
.text
;
11368 if (*nameptr
< ' ' && *nameptr
!= '\t')
11370 else if (*nameptr
== 0x7f)
11372 else if ((*nameptr
& 0xe0) == 0xc0)
11374 if ((nameptr
[1] & 0xc0) != 0x80)
11379 else if ((*nameptr
& 0xf0) == 0xe0)
11381 if ((nameptr
[1] & 0xc0) != 0x80 ||
11382 (nameptr
[2] & 0xc0) != 0x80)
11387 else if ((*nameptr
& 0xf8) == 0xf0)
11389 if ((nameptr
[1] & 0xc0) != 0x80 ||
11390 (nameptr
[2] & 0xc0) != 0x80 ||
11391 (nameptr
[3] & 0xc0) != 0x80)
11396 else if (*nameptr
& 0x80)
11406 if (StrictConformance
)
11408 send_ipp_status(con
, IPP_ATTRIBUTES
,
11409 _("Unsupported 'job-name' value."));
11410 ippCopyAttribute(con
->response
, name
, 0);
11415 cupsdLogMessage(CUPSD_LOG_WARN
,
11416 "Unsupported 'job-name' value, deleting from request.");
11417 ippDeleteAttribute(con
->request
, name
);
11423 * Is the destination valid?
11426 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11432 send_ipp_status(con
, IPP_NOT_FOUND
,
11433 _("The printer or class does not exist."));
11442 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
11443 #endif /* HAVE_SSL */
11445 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11447 send_http_error(con
, status
, printer
);
11450 else if (printer
->num_auth_info_required
== 1 &&
11451 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
11454 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
11458 else if (auth_info
&& !con
->http
->tls
&&
11459 !httpAddrLocalhost(con
->http
->hostaddr
))
11462 * Require encryption of auth-info over non-local connections...
11465 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
11468 #endif /* HAVE_SSL */
11471 * Everything was ok, so return OK status...
11474 con
->response
->request
.status
.status_code
= IPP_OK
;
11479 * 'validate_name()' - Make sure the printer name only contains valid chars.
11482 static int /* O - 0 if name is no good, 1 if good */
11483 validate_name(const char *name
) /* I - Name to check */
11485 const char *ptr
; /* Pointer into name */
11489 * Scan the whole name...
11492 for (ptr
= name
; *ptr
; ptr
++)
11493 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11497 * All the characters are good; validate the length, too...
11500 return ((ptr
- name
) < 128);
11505 * 'validate_user()' - Validate the user for the request.
11508 static int /* O - 1 if permitted, 0 otherwise */
11509 validate_user(cupsd_job_t
*job
, /* I - Job */
11510 cupsd_client_t
*con
, /* I - Client connection */
11511 const char *owner
, /* I - Owner of job/resource */
11512 char *username
, /* O - Authenticated username */
11513 size_t userlen
) /* I - Length of username */
11515 cupsd_printer_t
*printer
; /* Printer for job */
11518 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
);
11521 * Validate input...
11524 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11528 * Get the best authenticated username that is available.
11531 strlcpy(username
, get_username(con
), userlen
);
11534 * Check the username against the owner...
11537 printer
= cupsdFindDest(job
->dest
);
11539 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11540 con
, owner
) == HTTP_OK
);