2 * IPP routines for the CUPS scheduler.
4 * Copyright © 2007-2018 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
15 * Include necessary headers...
19 #include <cups/ppd-private.h>
22 /*# include <ApplicationServices/ApplicationServices.h>
23 extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
24 # include <CoreFoundation/CoreFoundation.h>*/
25 # ifdef HAVE_MEMBERSHIP_H
26 # include <membership.h>
27 # endif /* HAVE_MEMBERSHIP_H */
28 # ifdef HAVE_MEMBERSHIPPRIV_H
29 # include <membershipPriv.h>
31 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
32 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
33 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
34 # endif /* HAVE_MEMBERSHIPPRIV_H */
35 #endif /* __APPLE__ */
42 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
43 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
44 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
45 mime_type_t
*filetype
, int compression
);
46 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
47 mime_type_t
*filetype
);
48 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
49 static void add_job_uuid(cupsd_job_t
*job
);
50 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
51 static void add_printer_state_reasons(cupsd_client_t
*con
,
53 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
54 static void apply_printer_defaults(cupsd_printer_t
*printer
,
56 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
57 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
58 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
59 static void cancel_subscription(cupsd_client_t
*con
, int id
);
60 static int check_rss_recipient(const char *recipient
);
61 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
62 static void close_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
63 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
64 ipp_tag_t group
, int quickcopy
,
65 cups_array_t
*exclude
);
66 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
68 static int copy_file(const char *from
, const char *to
, mode_t mode
);
69 static int copy_model(cupsd_client_t
*con
, const char *from
,
71 static void copy_job_attrs(cupsd_client_t
*con
,
73 cups_array_t
*ra
, cups_array_t
*exclude
);
74 static void copy_printer_attrs(cupsd_client_t
*con
,
75 cupsd_printer_t
*printer
,
77 static void copy_subscription_attrs(cupsd_client_t
*con
,
78 cupsd_subscription_t
*sub
,
80 cups_array_t
*exclude
);
81 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
82 static void create_local_printer(cupsd_client_t
*con
);
83 static cups_array_t
*create_requested_array(ipp_t
*request
);
84 static void create_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
85 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
86 static void get_default(cupsd_client_t
*con
);
87 static void get_devices(cupsd_client_t
*con
);
88 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
89 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
90 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
91 static void get_notifications(cupsd_client_t
*con
);
92 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
93 static void get_ppds(cupsd_client_t
*con
);
94 static void get_printers(cupsd_client_t
*con
, int type
);
95 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
96 static void get_printer_supported(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
97 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
98 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
99 static const char *get_username(cupsd_client_t
*con
);
100 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
101 static void hold_new_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
102 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
103 static int ppd_parse_line(const char *line
, char *option
, int olen
,
104 char *choice
, int clen
);
105 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
106 static void read_job_ticket(cupsd_client_t
*con
);
107 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
108 static void release_held_new_jobs(cupsd_client_t
*con
,
109 ipp_attribute_t
*uri
);
110 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
111 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
112 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
113 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
114 ipp_attribute_t
*auth_info
);
115 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
116 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
117 cupsd_printer_t
*printer
);
118 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
, const char *message
, ...) _CUPS_FORMAT(3, 4);
119 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
120 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
121 static void set_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
122 static int set_printer_defaults(cupsd_client_t
*con
, cupsd_printer_t
*printer
);
123 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
124 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
125 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
, size_t bufsize
);
126 static char *url_encode_string(const char *s
, char *buffer
, size_t bufsize
);
127 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
128 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
129 static int validate_name(const char *name
);
130 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
, const char *owner
, char *username
, size_t userlen
);
134 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
137 int /* O - 1 on success, 0 on failure */
138 cupsdProcessIPPRequest(
139 cupsd_client_t
*con
) /* I - Client connection */
141 ipp_tag_t group
; /* Current group tag */
142 ipp_attribute_t
*attr
; /* Current attribute */
143 ipp_attribute_t
*charset
; /* Character set attribute */
144 ipp_attribute_t
*language
; /* Language attribute */
145 ipp_attribute_t
*uri
= NULL
; /* Printer or job URI attribute */
146 ipp_attribute_t
*username
; /* requesting-user-name attr */
147 int sub_id
; /* Subscription ID */
148 int valid
= 1; /* Valid request? */
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] = con
->request
->request
.op
.version
[0];
183 con
->response
->request
.status
.version
[1] = con
->request
->request
.op
.version
[1];
184 con
->response
->request
.status
.request_id
= con
->request
->request
.op
.request_id
;
187 * Then validate the request header and required attributes...
190 if (con
->request
->request
.any
.version
[0] != 1 && con
->request
->request
.any
.version
[0] != 2)
193 * Return an error, since we only support IPP 1.x and 2.x.
196 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Bad request version number %d.%d.", IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, con
->http
->hostname
, con
->request
->request
.any
.version
[0], con
->request
->request
.any
.version
[1]);
198 send_ipp_status(con
, IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, _("Bad request version number %d.%d."), con
->request
->request
.any
.version
[0], con
->request
->request
.any
.version
[1]);
200 else if (con
->request
->request
.any
.request_id
< 1)
203 * Return an error, since request IDs must be between 1 and 2^31-1
206 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Bad request ID %d.", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
, con
->request
->request
.any
.request_id
);
208 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Bad request ID %d."), con
->request
->request
.any
.request_id
);
210 else if (!con
->request
->attrs
)
212 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s No attributes in request.", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
);
214 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("No attributes in request."));
219 * Make sure that the attributes are provided in the correct order and
220 * don't repeat groups...
223 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
226 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
229 * Out of order; return an error...
232 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Attribute groups are out of order", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
);
234 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute groups are out of order (%x < %x)."), attr
->group_tag
, group
);
238 group
= attr
->group_tag
;
243 * Then make sure that the first three attributes are:
246 * attributes-natural-language
247 * printer-uri/job-uri
250 attr
= con
->request
->attrs
;
251 if (attr
&& attr
->name
&& !strcmp(attr
->name
, "attributes-charset") && (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
259 if (attr
&& attr
->name
&& !strcmp(attr
->name
, "attributes-natural-language") && (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
264 * Reset language for this request if different from Accept-Language.
267 if (!con
->language
||
268 strcmp(attr
->values
[0].string
.text
, con
->language
->language
))
270 cupsLangFree(con
->language
);
271 con
->language
= cupsLangGet(attr
->values
[0].string
.text
);
277 if ((attr
= ippFindAttribute(con
->request
, "printer-uri", IPP_TAG_URI
)) != NULL
)
279 else if ((attr
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
)) != NULL
)
281 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
282 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
287 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
, "attributes-charset", NULL
, charset
->values
[0].string
.text
);
289 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
, "attributes-charset", NULL
, "utf-8");
292 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "attributes-natural-language", NULL
, language
->values
[0].string
.text
);
294 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "attributes-natural-language", NULL
, DefaultLanguage
);
296 if (charset
&& _cups_strcasecmp(charset
->values
[0].string
.text
, "us-ascii") && _cups_strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
299 * Bad character set...
302 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"",
303 charset
->values
[0].string
.text
);
304 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Unsupported attributes-charset value \"%s\".", IPP_STATUS_ERROR_CHARSET
, con
->http
->hostname
, charset
->values
[0].string
.text
);
305 send_ipp_status(con
, IPP_STATUS_ERROR_CHARSET
, _("Unsupported character set \"%s\"."), charset
->values
[0].string
.text
);
307 else if (!charset
|| !language
||
309 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
310 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
311 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
312 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
313 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
316 * Return an error, since attributes-charset,
317 * attributes-natural-language, and printer-uri/job-uri are required
318 * for all operations.
323 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing attributes-charset attribute.");
325 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Missing attributes-charset attribute.", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
);
330 cupsdLogMessage(CUPSD_LOG_ERROR
,
331 "Missing attributes-natural-language attribute.");
333 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Missing attributes-natural-language attribute.", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
);
338 cupsdLogMessage(CUPSD_LOG_ERROR
, "Missing printer-uri, job-uri, or ppd-name attribute.");
340 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Missing printer-uri, job-uri, or ppd-name attribute.", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
);
343 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
345 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
346 cupsdLogMessage(CUPSD_LOG_DEBUG
,
347 "attr \"%s\": group_tag = %x, value_tag = %x",
348 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
351 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
353 send_ipp_status(con
, IPP_BAD_REQUEST
,
354 _("Missing required attributes."));
359 * OK, all the checks pass so far; validate "requesting-user-name"
363 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_ZERO
)) != NULL
)
366 * Validate "requesting-user-name"...
369 if (username
->group_tag
!= IPP_TAG_OPERATION
&& StrictConformance
)
371 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s \"requesting-user-name\" attribute in wrong group.", IPP_STATUS_ERROR_BAD_REQUEST
, con
->http
->hostname
);
372 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("\"requesting-user-name\" attribute in wrong group."));
375 else if (username
->value_tag
!= IPP_TAG_NAME
&& username
->value_tag
!= IPP_TAG_NAMELANG
)
377 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s \"requesting-user-name\" attribute with wrong syntax.", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, con
->http
->hostname
);
378 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("\"requesting-user-name\" attribute with wrong syntax."));
379 if ((attr
= ippCopyAttribute(con
->response
, username
, 0)) != NULL
)
380 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
383 else if (!ippValidateAttribute(username
))
385 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s \"requesting-user-name\" attribute with bad value.", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, con
->http
->hostname
);
387 if (StrictConformance
)
393 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("\"requesting-user-name\" attribute with wrong syntax."));
394 if ((attr
= ippCopyAttribute(con
->response
, username
, 0)) != NULL
)
395 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
401 * Map bad "requesting-user-name" to 'anonymous'...
404 ippSetString(con
->request
, &username
, 0, "anonymous");
407 else if (!strcmp(username
->values
[0].string
.text
, "root") && _cups_strcasecmp(con
->http
->hostname
, "localhost") && strcmp(con
->username
, "root"))
410 * Remote unauthenticated user masquerading as local root...
413 ippSetString(con
->request
, &username
, 0, RemoteRoot
);
417 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id", IPP_TAG_INTEGER
)) != NULL
)
418 sub_id
= attr
->values
[0].integer
;
425 * Try processing the operation...
429 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s", ippOpString(con
->request
->request
.op
.operation_id
), uri
->values
[0].string
.text
);
431 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s", ippOpString(con
->request
->request
.op
.operation_id
));
433 switch (con
->request
->request
.op
.operation_id
)
435 case IPP_OP_PRINT_JOB
:
439 case IPP_OP_VALIDATE_JOB
:
440 validate_job(con
, uri
);
443 case IPP_OP_CREATE_JOB
:
444 create_job(con
, uri
);
447 case IPP_OP_SEND_DOCUMENT
:
448 send_document(con
, uri
);
451 case IPP_OP_CANCEL_JOB
:
452 cancel_job(con
, uri
);
455 case IPP_OP_GET_JOB_ATTRIBUTES
:
456 get_job_attrs(con
, uri
);
459 case IPP_OP_GET_JOBS
:
463 case IPP_OP_GET_PRINTER_ATTRIBUTES
:
464 get_printer_attrs(con
, uri
);
467 case IPP_OP_GET_PRINTER_SUPPORTED_VALUES
:
468 get_printer_supported(con
, uri
);
471 case IPP_OP_HOLD_JOB
:
475 case IPP_OP_RELEASE_JOB
:
476 release_job(con
, uri
);
479 case IPP_OP_RESTART_JOB
:
480 restart_job(con
, uri
);
483 case IPP_OP_PAUSE_PRINTER
:
484 stop_printer(con
, uri
);
487 case IPP_OP_RESUME_PRINTER
:
488 start_printer(con
, uri
);
491 case IPP_OP_PURGE_JOBS
:
492 case IPP_OP_CANCEL_JOBS
:
493 case IPP_OP_CANCEL_MY_JOBS
:
494 cancel_all_jobs(con
, uri
);
497 case IPP_OP_SET_JOB_ATTRIBUTES
:
498 set_job_attrs(con
, uri
);
501 case IPP_OP_SET_PRINTER_ATTRIBUTES
:
502 set_printer_attrs(con
, uri
);
505 case IPP_OP_HOLD_NEW_JOBS
:
506 hold_new_jobs(con
, uri
);
509 case IPP_OP_RELEASE_HELD_NEW_JOBS
:
510 release_held_new_jobs(con
, uri
);
513 case IPP_OP_CLOSE_JOB
:
517 case IPP_OP_CUPS_GET_DEFAULT
:
521 case IPP_OP_CUPS_GET_PRINTERS
:
522 get_printers(con
, 0);
525 case IPP_OP_CUPS_GET_CLASSES
:
526 get_printers(con
, CUPS_PRINTER_CLASS
);
529 case IPP_OP_CUPS_ADD_MODIFY_PRINTER
:
530 add_printer(con
, uri
);
533 case IPP_OP_CUPS_DELETE_PRINTER
:
534 delete_printer(con
, uri
);
537 case IPP_OP_CUPS_ADD_MODIFY_CLASS
:
541 case IPP_OP_CUPS_DELETE_CLASS
:
542 delete_printer(con
, uri
);
545 case IPP_OP_CUPS_ACCEPT_JOBS
:
546 case IPP_OP_ENABLE_PRINTER
:
547 accept_jobs(con
, uri
);
550 case IPP_OP_CUPS_REJECT_JOBS
:
551 case IPP_OP_DISABLE_PRINTER
:
552 reject_jobs(con
, uri
);
555 case IPP_OP_CUPS_SET_DEFAULT
:
556 set_default(con
, uri
);
559 case IPP_OP_CUPS_GET_DEVICES
:
563 case IPP_OP_CUPS_GET_DOCUMENT
:
564 get_document(con
, uri
);
567 case IPP_OP_CUPS_GET_PPD
:
571 case IPP_OP_CUPS_GET_PPDS
:
575 case IPP_OP_CUPS_MOVE_JOB
:
579 case IPP_OP_CUPS_AUTHENTICATE_JOB
:
580 authenticate_job(con
, uri
);
583 case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS
:
584 case IPP_OP_CREATE_JOB_SUBSCRIPTIONS
:
585 create_subscriptions(con
, uri
);
588 case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES
:
589 get_subscription_attrs(con
, sub_id
);
592 case IPP_OP_GET_SUBSCRIPTIONS
:
593 get_subscriptions(con
, uri
);
596 case IPP_OP_RENEW_SUBSCRIPTION
:
597 renew_subscription(con
, sub_id
);
600 case IPP_OP_CANCEL_SUBSCRIPTION
:
601 cancel_subscription(con
, sub_id
);
604 case IPP_OP_GET_NOTIFICATIONS
:
605 get_notifications(con
);
608 case IPP_OP_CUPS_CREATE_LOCAL_PRINTER
:
609 create_local_printer(con
);
613 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
, "%04X %s Operation %04X (%s) not supported.", IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, con
->http
->hostname
, con
->request
->request
.op
.operation_id
, ippOpString(con
->request
->request
.op
.operation_id
));
615 send_ipp_status(con
, IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, _("%s not supported."), ippOpString(con
->request
->request
.op
.operation_id
));
626 * Sending data from the scheduler...
629 cupsdLogClient(con
, con
->response
->request
.status
.status_code
>= IPP_STATUS_ERROR_BAD_REQUEST
&& con
->response
->request
.status
.status_code
!= IPP_STATUS_ERROR_NOT_FOUND
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
, "Returning IPP %s for %s (%s) from %s.", ippErrorString(con
->response
->request
.status
.status_code
), ippOpString(con
->request
->request
.op
.operation_id
), uri
? uri
->values
[0].string
.text
: "no URI", con
->http
->hostname
);
631 httpClearFields(con
->http
);
633 #ifdef CUPSD_USE_CHUNKING
635 * Because older versions of CUPS (1.1.17 and older) and some IPP
636 * clients do not implement chunking properly, we cannot use
637 * chunking by default. This may become the default in future
638 * CUPS releases, or we might add a configuration directive for
642 if (con
->http
->version
== HTTP_1_1
)
644 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Transfer-Encoding: chunked");
645 cupsdSetLength(con
->http
, 0);
648 #endif /* CUPSD_USE_CHUNKING */
650 size_t length
; /* Length of response */
653 length
= ippLength(con
->response
);
655 if (con
->file
>= 0 && !con
->pipe_pid
)
657 struct stat fileinfo
; /* File information */
659 if (!fstat(con
->file
, &fileinfo
))
660 length
+= (size_t)fileinfo
.st_size
;
663 cupsdLogClient(con
, CUPSD_LOG_DEBUG
, "Content-Length: " CUPS_LLFMT
, CUPS_LLCAST length
);
664 httpSetLength(con
->http
, length
);
667 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
670 * Tell the caller the response header was sent successfully...
673 cupsdAddSelect(httpGetFd(con
->http
), (cupsd_selfunc_t
)cupsdReadClient
, (cupsd_selfunc_t
)cupsdWriteClient
, con
);
680 * Tell the caller the response header could not be sent...
689 * Sending data from a subprocess like cups-deviced; tell the caller
690 * everything is A-OK so far...
699 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
702 int /* O - 0 on success, -1 on error */
703 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
705 cupsd_printer_t
*printer
; /* Destination printer or class */
706 ipp_attribute_t
*attr
; /* job-sheets attribute */
707 int kbytes
; /* Kilobytes in banner */
710 job
->pending_timeout
= 0;
713 * See if we need to add the ending sheet...
716 if (!cupsdLoadJob(job
))
719 printer
= cupsdFindDest(job
->dest
);
720 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
722 if (printer
&& !(printer
->type
& CUPS_PRINTER_REMOTE
) &&
723 attr
&& attr
->num_values
> 1)
729 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding end banner page \"%s\".",
730 attr
->values
[1].string
.text
);
732 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
735 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
743 * 'accept_jobs()' - Accept print jobs to a printer.
747 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
748 ipp_attribute_t
*uri
) /* I - Printer or class URI */
750 http_status_t status
; /* Policy status */
751 cups_ptype_t dtype
; /* Destination type (printer/class) */
752 cupsd_printer_t
*printer
; /* Printer data */
755 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
756 con
->number
, uri
->values
[0].string
.text
);
759 * Is the destination valid?
762 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
768 send_ipp_status(con
, IPP_NOT_FOUND
,
769 _("The printer or class does not exist."));
777 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
779 send_http_error(con
, status
, printer
);
784 * Accept jobs sent to the printer...
787 printer
->accepting
= 1;
788 printer
->state_message
[0] = '\0';
790 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
791 "Now accepting jobs.");
793 if (dtype
& CUPS_PRINTER_CLASS
)
795 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
797 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
798 printer
->name
, get_username(con
));
802 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
804 cupsdLogMessage(CUPSD_LOG_INFO
,
805 "Printer \"%s\" now accepting jobs (\"%s\").",
806 printer
->name
, get_username(con
));
810 * Everything was ok, so return OK status...
813 con
->response
->request
.status
.status_code
= IPP_OK
;
818 * 'add_class()' - Add a class to the system.
822 add_class(cupsd_client_t
*con
, /* I - Client connection */
823 ipp_attribute_t
*uri
) /* I - URI of class */
825 http_status_t status
; /* Policy status */
826 int i
; /* Looping var */
827 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
828 username
[HTTP_MAX_URI
], /* Username portion of URI */
829 host
[HTTP_MAX_URI
], /* Host portion of URI */
830 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
831 int port
; /* Port portion of URI */
832 cupsd_printer_t
*pclass
, /* Class */
833 *member
; /* Member printer/class */
834 cups_ptype_t dtype
; /* Destination type */
835 ipp_attribute_t
*attr
; /* Printer attribute */
836 int modify
; /* Non-zero if we just modified */
837 int need_restart_job
; /* Need to restart job? */
840 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
841 con
->number
, uri
->values
[0].string
.text
);
844 * Do we have a valid URI?
847 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
848 sizeof(scheme
), username
, sizeof(username
), host
,
849 sizeof(host
), &port
, resource
, sizeof(resource
));
852 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
855 * No, return an error...
858 send_ipp_status(con
, IPP_BAD_REQUEST
,
859 _("The printer-uri must be of the form "
860 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
865 * Do we have a valid printer name?
868 if (!validate_name(resource
+ 9))
871 * No, return an error...
874 send_ipp_status(con
, IPP_BAD_REQUEST
,
875 _("The printer-uri \"%s\" contains invalid characters."),
876 uri
->values
[0].string
.text
);
881 * See if the class already exists; if not, create a new class...
884 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
887 * Class doesn't exist; see if we have a printer of the same name...
890 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
)
893 * Yes, return an error...
896 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
897 _("A printer named \"%s\" already exists."),
903 * No, check the default policy and then add the class...
906 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
908 send_http_error(con
, status
, NULL
);
912 pclass
= cupsdAddClass(resource
+ 9);
915 pclass
->printer_id
= NextPrinterId
++;
917 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
920 send_http_error(con
, status
, pclass
);
927 * Look for attributes and copy them over as needed...
930 need_restart_job
= 0;
932 if ((attr
= ippFindAttribute(con
->request
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
933 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
935 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
936 cupsdSetString(&pclass
->geo_location
, attr
->values
[0].string
.text
);
938 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
939 cupsdSetString(&pclass
->organization
, attr
->values
[0].string
.text
);
941 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
942 cupsdSetString(&pclass
->organizational_unit
, attr
->values
[0].string
.text
);
944 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
945 IPP_TAG_TEXT
)) != NULL
)
946 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
948 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
949 IPP_TAG_BOOLEAN
)) != NULL
&&
950 attr
->values
[0].boolean
!= pclass
->accepting
)
952 cupsdLogMessage(CUPSD_LOG_INFO
,
953 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
954 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
956 pclass
->accepting
= attr
->values
[0].boolean
;
958 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s accepting jobs.",
959 pclass
->accepting
? "Now" : "No longer");
962 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared", IPP_TAG_BOOLEAN
)) != NULL
)
964 if (pclass
->type
& CUPS_PRINTER_REMOTE
)
967 * Cannot re-share remote printers.
970 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Cannot change printer-is-shared for remote queues."));
972 cupsdDeletePrinter(pclass
, 0);
977 if (pclass
->shared
&& !ippGetBoolean(attr
, 0))
978 cupsdDeregisterPrinter(pclass
, 1);
980 cupsdLogMessage(CUPSD_LOG_INFO
,
981 "Setting %s printer-is-shared to %d (was %d.)",
982 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
984 pclass
->shared
= ippGetBoolean(attr
, 0);
987 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
988 IPP_TAG_ENUM
)) != NULL
)
990 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
991 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
993 send_ipp_status(con
, IPP_BAD_REQUEST
,
994 _("Attempt to set %s printer-state to bad value %d."),
995 pclass
->name
, attr
->values
[0].integer
);
997 cupsdDeletePrinter(pclass
, 0);
1002 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1003 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1005 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1006 cupsdStopPrinter(pclass
, 0);
1009 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1010 need_restart_job
= 1;
1013 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1014 IPP_TAG_TEXT
)) != NULL
)
1016 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1017 sizeof(pclass
->state_message
));
1019 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s",
1020 pclass
->state_message
);
1022 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1023 IPP_TAG_URI
)) != NULL
)
1026 * Clear the printer array as needed...
1029 need_restart_job
= 1;
1031 if (pclass
->num_printers
> 0)
1033 free(pclass
->printers
);
1034 pclass
->num_printers
= 0;
1038 * Add each printer or class that is listed...
1041 for (i
= 0; i
< attr
->num_values
; i
++)
1044 * Search for the printer or class URI...
1047 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1053 send_ipp_status(con
, IPP_NOT_FOUND
,
1054 _("The printer or class does not exist."));
1056 cupsdDeletePrinter(pclass
, 0);
1060 else if (dtype
& CUPS_PRINTER_CLASS
)
1062 send_ipp_status(con
, IPP_BAD_REQUEST
,
1063 _("Nested classes are not allowed."));
1065 cupsdDeletePrinter(pclass
, 0);
1071 * Add it to the class...
1074 cupsdAddPrinterToClass(pclass
, member
);
1078 if (!set_printer_defaults(con
, pclass
))
1081 cupsdDeletePrinter(pclass
, 0);
1086 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1087 IPP_TAG_KEYWORD
)) != NULL
)
1088 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1090 pclass
->config_time
= time(NULL
);
1093 * Update the printer class attributes and return...
1096 cupsdSetPrinterAttrs(pclass
);
1097 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1099 if (need_restart_job
&& pclass
->job
)
1102 * Reset the current job to a "pending" status...
1105 cupsdSetJobState(pclass
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
1106 "Job restarted because the class was modified.");
1109 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1113 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
1114 pclass
, NULL
, "Class \"%s\" modified by \"%s\".",
1115 pclass
->name
, get_username(con
));
1117 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1118 pclass
->name
, get_username(con
));
1122 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
1123 pclass
, NULL
, "New class \"%s\" added by \"%s\".",
1124 pclass
->name
, get_username(con
));
1126 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1127 pclass
->name
, get_username(con
));
1130 con
->response
->request
.status
.status_code
= IPP_OK
;
1135 * 'add_file()' - Add a file to a job.
1138 static int /* O - 0 on success, -1 on error */
1139 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1140 cupsd_job_t
*job
, /* I - Job to add to */
1141 mime_type_t
*filetype
, /* I - Type of file */
1142 int compression
) /* I - Compression */
1144 mime_type_t
**filetypes
; /* New filetypes array... */
1145 int *compressions
; /* New compressions array... */
1148 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1149 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1150 "compression=%d)", con
, con
? con
->number
: -1, job
->id
,
1151 filetype
->super
, filetype
->type
, compression
);
1154 * Add the file to the job...
1157 if (job
->num_files
== 0)
1159 compressions
= (int *)malloc(sizeof(int));
1160 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1164 compressions
= (int *)realloc(job
->compressions
,
1165 (size_t)(job
->num_files
+ 1) * sizeof(int));
1166 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1167 (size_t)(job
->num_files
+ 1) *
1168 sizeof(mime_type_t
*));
1172 job
->compressions
= compressions
;
1175 job
->filetypes
= filetypes
;
1177 if (!compressions
|| !filetypes
)
1179 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1180 "Job aborted because the scheduler ran out of memory.");
1183 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1184 _("Unable to allocate memory for file types."));
1189 job
->compressions
[job
->num_files
] = compression
;
1190 job
->filetypes
[job
->num_files
] = filetype
;
1195 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1202 * 'add_job()' - Add a job to a print queue.
1205 static cupsd_job_t
* /* O - Job object */
1206 add_job(cupsd_client_t
*con
, /* I - Client connection */
1207 cupsd_printer_t
*printer
, /* I - Destination printer */
1208 mime_type_t
*filetype
) /* I - First print file type, if any */
1210 http_status_t status
; /* Policy status */
1211 ipp_attribute_t
*attr
, /* Current attribute */
1212 *auth_info
; /* auth-info attribute */
1213 const char *mandatory
; /* Current mandatory job attribute */
1214 const char *val
; /* Default option value */
1215 int priority
; /* Job priority */
1216 cupsd_job_t
*job
; /* Current job */
1217 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1218 int kbytes
; /* Size of print file */
1219 int i
; /* Looping var */
1220 int lowerpagerange
; /* Page range bound */
1221 int exact
; /* Did we have an exact match? */
1222 ipp_attribute_t
*media_col
, /* media-col attribute */
1223 *media_margin
; /* media-*-margin attribute */
1224 ipp_t
*unsup_col
; /* media-col in unsupported response */
1225 static const char * const readonly
[] =/* List of read-only attributes */
1227 "date-time-at-completed",
1228 "date-time-at-creation",
1229 "date-time-at-processing",
1230 "job-detailed-status-messages",
1231 "job-document-access-errors",
1233 "job-impressions-completed",
1234 "job-k-octets-completed",
1235 "job-media-sheets-completed",
1236 "job-pages-completed",
1237 "job-printer-up-time",
1240 "job-state-message",
1241 "job-state-reasons",
1243 "number-of-documents",
1244 "number-of-intervening-jobs",
1245 "output-device-assigned",
1246 "time-at-completed",
1248 "time-at-processing"
1252 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1253 con
, con
->number
, printer
, printer
->name
,
1254 filetype
, filetype
? filetype
->super
: "none",
1255 filetype
? filetype
->type
: "none");
1258 * Check remote printing to non-shared printer...
1261 if (!printer
->shared
&&
1262 _cups_strcasecmp(con
->http
->hostname
, "localhost") &&
1263 _cups_strcasecmp(con
->http
->hostname
, ServerName
))
1265 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1266 _("The printer or class is not shared."));
1274 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1276 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1278 send_http_error(con
, status
, printer
);
1281 else if (printer
->num_auth_info_required
== 1 &&
1282 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
1285 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1289 else if (auth_info
&& !con
->http
->tls
&&
1290 !httpAddrLocalhost(con
->http
->hostaddr
))
1293 * Require encryption of auth-info over non-local connections...
1296 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1299 #endif /* HAVE_SSL */
1302 * See if the printer is accepting jobs...
1305 if (!printer
->accepting
)
1307 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1308 _("Destination \"%s\" is not accepting jobs."),
1314 * Validate job template attributes; for now just document-format,
1315 * copies, job-sheets, number-up, page-ranges, mandatory attributes, and
1319 for (i
= 0; i
< (int)(sizeof(readonly
) / sizeof(readonly
[0])); i
++)
1321 if ((attr
= ippFindAttribute(con
->request
, readonly
[i
], IPP_TAG_ZERO
)) != NULL
)
1323 ippDeleteAttribute(con
->request
, attr
);
1325 if (StrictConformance
)
1327 send_ipp_status(con
, IPP_BAD_REQUEST
, _("The '%s' Job Status attribute cannot be supplied in a job creation request."), readonly
[i
]);
1331 cupsdLogMessage(CUPSD_LOG_INFO
, "Unexpected '%s' Job Status attribute in a job creation request.", readonly
[i
]);
1337 for (mandatory
= (char *)cupsArrayFirst(printer
->pc
->mandatory
);
1339 mandatory
= (char *)cupsArrayNext(printer
->pc
->mandatory
))
1341 if (!ippFindAttribute(con
->request
, mandatory
, IPP_TAG_ZERO
))
1344 * Missing a required attribute...
1347 send_ipp_status(con
, IPP_CONFLICT
,
1348 _("The \"%s\" attribute is required for print jobs."),
1355 if (filetype
&& printer
->filetypes
&&
1356 !cupsArrayFind(printer
->filetypes
, filetype
))
1358 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1359 /* MIME media type string */
1362 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1365 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1366 _("Unsupported format \"%s\"."), mimetype
);
1368 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1369 "document-format", NULL
, mimetype
);
1374 if ((attr
= ippFindAttribute(con
->request
, "copies",
1375 IPP_TAG_INTEGER
)) != NULL
)
1377 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1379 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1380 attr
->values
[0].integer
);
1381 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1382 "copies", attr
->values
[0].integer
);
1387 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1388 IPP_TAG_ZERO
)) != NULL
)
1390 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1391 attr
->value_tag
!= IPP_TAG_NAME
)
1393 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type."));
1397 if (attr
->num_values
> 2)
1399 send_ipp_status(con
, IPP_BAD_REQUEST
,
1400 _("Too many job-sheets values (%d > 2)."),
1405 for (i
= 0; i
< attr
->num_values
; i
++)
1406 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1407 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1409 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\"."),
1410 attr
->values
[i
].string
.text
);
1415 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1416 IPP_TAG_INTEGER
)) != NULL
)
1418 if (attr
->values
[0].integer
!= 1 &&
1419 attr
->values
[0].integer
!= 2 &&
1420 attr
->values
[0].integer
!= 4 &&
1421 attr
->values
[0].integer
!= 6 &&
1422 attr
->values
[0].integer
!= 9 &&
1423 attr
->values
[0].integer
!= 16)
1425 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1426 attr
->values
[0].integer
);
1427 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1428 "number-up", attr
->values
[0].integer
);
1433 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1434 IPP_TAG_RANGE
)) != NULL
)
1436 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1438 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1439 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1441 send_ipp_status(con
, IPP_BAD_REQUEST
,
1442 _("Bad page-ranges values %d-%d."),
1443 attr
->values
[i
].range
.lower
,
1444 attr
->values
[i
].range
.upper
);
1448 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1453 * Do media selection as needed...
1456 if (!ippFindAttribute(con
->request
, "PageRegion", IPP_TAG_ZERO
) &&
1457 !ippFindAttribute(con
->request
, "PageSize", IPP_TAG_ZERO
) &&
1458 _ppdCacheGetPageSize(printer
->pc
, con
->request
, NULL
, &exact
))
1461 (media_col
= ippFindAttribute(con
->request
, "media-col",
1462 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1464 send_ipp_status(con
, IPP_OK_SUBST
, _("Unsupported margins."));
1466 unsup_col
= ippNew();
1467 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1468 "media-bottom-margin",
1469 IPP_TAG_INTEGER
)) != NULL
)
1470 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1471 "media-bottom-margin", media_margin
->values
[0].integer
);
1473 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1474 "media-left-margin",
1475 IPP_TAG_INTEGER
)) != NULL
)
1476 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1477 "media-left-margin", media_margin
->values
[0].integer
);
1479 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1480 "media-right-margin",
1481 IPP_TAG_INTEGER
)) != NULL
)
1482 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1483 "media-right-margin", media_margin
->values
[0].integer
);
1485 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1487 IPP_TAG_INTEGER
)) != NULL
)
1488 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1489 "media-top-margin", media_margin
->values
[0].integer
);
1491 ippAddCollection(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, "media-col",
1493 ippDelete(unsup_col
);
1498 * Make sure we aren't over our limit...
1501 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1504 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1506 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Too many active jobs."));
1510 if ((i
= check_quotas(con
, printer
)) < 0)
1512 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1517 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1522 * Create the job and set things up...
1525 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1526 IPP_TAG_INTEGER
)) != NULL
)
1527 priority
= attr
->values
[0].integer
;
1530 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1531 printer
->options
)) != NULL
)
1532 priority
= atoi(val
);
1536 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1540 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) == NULL
)
1541 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1542 else if ((attr
->value_tag
!= IPP_TAG_NAME
&&
1543 attr
->value_tag
!= IPP_TAG_NAMELANG
) ||
1544 attr
->num_values
!= 1)
1546 send_ipp_status(con
, IPP_ATTRIBUTES
,
1547 _("Bad job-name value: Wrong type or count."));
1548 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1549 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1551 if (StrictConformance
)
1554 /* Don't use invalid attribute */
1555 ippDeleteAttribute(con
->request
, attr
);
1557 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1559 else if (!ippValidateAttribute(attr
))
1561 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad job-name value: %s"),
1562 cupsLastErrorString());
1564 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1565 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1567 if (StrictConformance
)
1570 /* Don't use invalid attribute */
1571 ippDeleteAttribute(con
->request
, attr
);
1573 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1576 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1578 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1580 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1581 _("Unable to add job for destination \"%s\"."),
1586 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
1587 job
->attrs
= con
->request
;
1589 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1591 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1594 apply_printer_defaults(printer
, job
);
1596 if (con
->username
[0])
1598 cupsdSetString(&job
->username
, con
->username
);
1601 ippSetString(job
->attrs
, &attr
, 0, con
->username
);
1605 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1606 "add_job: requesting-user-name=\"%s\"",
1607 attr
->values
[0].string
.text
);
1609 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1612 cupsdSetString(&job
->username
, "anonymous");
1615 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1616 "job-originating-user-name", NULL
, job
->username
);
1619 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1620 ippSetName(job
->attrs
, &attr
, "job-originating-user-name");
1623 if (con
->username
[0] || auth_info
)
1625 save_auth_info(con
, job
, auth_info
);
1628 * Remove the auth-info attribute from the attribute data...
1632 ippDeleteAttribute(job
->attrs
, auth_info
);
1635 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_NAME
)) != NULL
)
1636 cupsdSetString(&(job
->name
), attr
->values
[0].string
.text
);
1638 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1639 IPP_TAG_ZERO
)) != NULL
)
1642 * Request contains a job-originating-host-name attribute; validate it...
1645 if (attr
->value_tag
!= IPP_TAG_NAME
||
1646 attr
->num_values
!= 1 ||
1647 strcmp(con
->http
->hostname
, "localhost"))
1650 * Can't override the value if we aren't connected via localhost.
1651 * Also, we can only have 1 value and it must be a name value.
1654 ippDeleteAttribute(job
->attrs
, attr
);
1655 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-originating-host-name", NULL
, con
->http
->hostname
);
1658 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1663 * No job-originating-host-name attribute, so use the hostname from
1667 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1668 "job-originating-host-name", NULL
, con
->http
->hostname
);
1671 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-completed");
1672 ippAddDate(job
->attrs
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(time(NULL
)));
1673 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-processing");
1674 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-completed");
1675 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", time(NULL
));
1676 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-processing");
1679 * Add remaining job attributes...
1682 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1683 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1684 "job-state", IPP_JOB_STOPPED
);
1685 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1686 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1687 "job-state-reasons", NULL
, "job-incoming");
1688 job
->impressions
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", 0);
1689 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1690 "job-media-sheets-completed", 0);
1691 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1694 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1695 attr
->values
[0].integer
= 0;
1697 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1699 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1700 IPP_TAG_KEYWORD
)) == NULL
)
1701 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1704 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1705 printer
->options
)) == NULL
)
1708 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1709 "job-hold-until", NULL
, val
);
1712 if (printer
->holding_new_jobs
)
1715 * Hold all new jobs on this printer...
1718 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1719 cupsdSetJobHoldUntil(job
, ippGetString(attr
, 0, NULL
), 0);
1721 cupsdSetJobHoldUntil(job
, "indefinite", 0);
1723 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1724 job
->state_value
= IPP_JOB_HELD
;
1726 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-held-on-create");
1728 else if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1731 * Hold job until specified time...
1734 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
1736 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1737 job
->state_value
= IPP_JOB_HELD
;
1739 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
1741 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1743 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
1744 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1745 job
->state_value
= IPP_JOB_HELD
;
1749 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1750 job
->state_value
= IPP_JOB_PENDING
;
1752 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1755 if (!(printer
->type
& CUPS_PRINTER_REMOTE
) || Classification
)
1758 * Add job sheets options...
1761 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1762 IPP_TAG_ZERO
)) == NULL
)
1764 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1765 "Adding default job-sheets values \"%s,%s\"...",
1766 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1768 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1770 ippSetString(job
->attrs
, &attr
, 0, printer
->job_sheets
[0]);
1771 ippSetString(job
->attrs
, &attr
, 1, printer
->job_sheets
[1]);
1774 job
->job_sheets
= attr
;
1777 * Enforce classification level if set...
1782 cupsdLogMessage(CUPSD_LOG_INFO
,
1783 "Classification=\"%s\", ClassifyOverride=%d",
1784 Classification
? Classification
: "(null)",
1787 if (ClassifyOverride
)
1789 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1790 (attr
->num_values
== 1 ||
1791 !strcmp(attr
->values
[1].string
.text
, "none")))
1794 * Force the leading banner to have the classification on it...
1797 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1799 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1800 "job-sheets=\"%s,none\", "
1801 "job-originating-user-name=\"%s\"",
1802 Classification
, job
->username
);
1804 else if (attr
->num_values
== 2 &&
1805 strcmp(attr
->values
[0].string
.text
,
1806 attr
->values
[1].string
.text
) &&
1807 strcmp(attr
->values
[0].string
.text
, "none") &&
1808 strcmp(attr
->values
[1].string
.text
, "none"))
1811 * Can't put two different security markings on the same document!
1814 ippSetString(job
->attrs
, &attr
, 1, attr
->values
[0].string
.text
);
1816 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1817 "job-sheets=\"%s,%s\", "
1818 "job-originating-user-name=\"%s\"",
1819 attr
->values
[0].string
.text
,
1820 attr
->values
[1].string
.text
, job
->username
);
1822 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1823 strcmp(attr
->values
[0].string
.text
, "none") &&
1824 (attr
->num_values
== 1 ||
1825 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1826 strcmp(attr
->values
[1].string
.text
, "none"))))
1828 if (attr
->num_values
== 1)
1829 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1830 "CLASSIFICATION OVERRIDDEN "
1831 "job-sheets=\"%s\", "
1832 "job-originating-user-name=\"%s\"",
1833 attr
->values
[0].string
.text
, job
->username
);
1835 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1836 "CLASSIFICATION OVERRIDDEN "
1837 "job-sheets=\"%s,%s\",fffff "
1838 "job-originating-user-name=\"%s\"",
1839 attr
->values
[0].string
.text
,
1840 attr
->values
[1].string
.text
, job
->username
);
1843 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1844 (attr
->num_values
== 1 ||
1845 strcmp(attr
->values
[1].string
.text
, Classification
)))
1848 * Force the banner to have the classification on it...
1851 if (attr
->num_values
> 1 &&
1852 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1854 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1855 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1859 if (attr
->num_values
== 1 ||
1860 strcmp(attr
->values
[0].string
.text
, "none"))
1861 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1863 if (attr
->num_values
> 1 &&
1864 strcmp(attr
->values
[1].string
.text
, "none"))
1865 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1868 if (attr
->num_values
> 1)
1869 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1870 "CLASSIFICATION FORCED "
1871 "job-sheets=\"%s,%s\", "
1872 "job-originating-user-name=\"%s\"",
1873 attr
->values
[0].string
.text
,
1874 attr
->values
[1].string
.text
, job
->username
);
1876 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1877 "CLASSIFICATION FORCED "
1878 "job-sheets=\"%s\", "
1879 "job-originating-user-name=\"%s\"",
1880 Classification
, job
->username
);
1885 * See if we need to add the starting sheet...
1888 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1890 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1891 attr
->values
[0].string
.text
);
1893 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1895 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1896 "Aborting job because the start banner could not be "
1901 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1904 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1905 IPP_TAG_ZERO
)) != NULL
)
1906 job
->job_sheets
= attr
;
1909 * Fill in the response info...
1912 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
1913 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
1914 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1917 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1919 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1921 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_TEXT
, "job-state-message", NULL
, "");
1922 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
1923 NULL
, job
->reasons
->values
[0].string
.text
);
1925 con
->response
->request
.status
.status_code
= IPP_OK
;
1928 * Add any job subscriptions...
1931 add_job_subscriptions(con
, job
);
1934 * Set all but the first two attributes to the job attributes group...
1937 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1938 attr
->group_tag
= IPP_TAG_JOB
;
1941 * Fire the "job created" event...
1944 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1947 * Return the new job...
1955 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1959 add_job_subscriptions(
1960 cupsd_client_t
*con
, /* I - Client connection */
1961 cupsd_job_t
*job
) /* I - Newly created job */
1963 int i
; /* Looping var */
1964 ipp_attribute_t
*prev
, /* Previous attribute */
1965 *next
, /* Next attribute */
1966 *attr
; /* Current attribute */
1967 cupsd_subscription_t
*sub
; /* Subscription object */
1968 const char *recipient
, /* notify-recipient-uri */
1969 *pullmethod
; /* notify-pull-method */
1970 ipp_attribute_t
*user_data
; /* notify-user-data */
1971 int interval
; /* notify-time-interval */
1972 unsigned mask
; /* notify-events */
1976 * Find the first subscription group attribute; return if we have
1980 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
1981 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1988 * Process the subscription attributes in the request...
1997 mask
= CUPSD_EVENT_NONE
;
1999 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
2001 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
2002 attr
->value_tag
== IPP_TAG_URI
)
2005 * Validate the recipient scheme against the ServerBin/notifier
2009 char notifier
[1024], /* Notifier filename */
2010 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2011 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2012 host
[HTTP_MAX_URI
], /* Host portion of URI */
2013 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2014 int port
; /* Port portion of URI */
2017 recipient
= attr
->values
[0].string
.text
;
2019 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
2020 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2021 host
, sizeof(host
), &port
,
2022 resource
, sizeof(resource
)) < HTTP_URI_OK
)
2024 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2025 _("Bad notify-recipient-uri \"%s\"."), recipient
);
2026 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2027 "notify-status-code", IPP_URI_SCHEME
);
2031 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
2033 if (access(notifier
, X_OK
) || !strcmp(scheme
, ".") || !strcmp(scheme
, ".."))
2035 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2036 _("notify-recipient-uri URI \"%s\" uses unknown "
2037 "scheme."), recipient
);
2038 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2039 "notify-status-code", IPP_URI_SCHEME
);
2043 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2045 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2046 _("notify-recipient-uri URI \"%s\" is already used."),
2048 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2049 "notify-status-code", IPP_ATTRIBUTES
);
2053 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2054 attr
->value_tag
== IPP_TAG_KEYWORD
)
2056 pullmethod
= attr
->values
[0].string
.text
;
2058 if (strcmp(pullmethod
, "ippget"))
2060 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2061 _("Bad notify-pull-method \"%s\"."), pullmethod
);
2062 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2063 "notify-status-code", IPP_ATTRIBUTES
);
2067 else if (!strcmp(attr
->name
, "notify-charset") &&
2068 attr
->value_tag
== IPP_TAG_CHARSET
&&
2069 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2070 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2072 send_ipp_status(con
, IPP_CHARSET
,
2073 _("Character set \"%s\" not supported."),
2074 attr
->values
[0].string
.text
);
2077 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2078 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2079 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2081 send_ipp_status(con
, IPP_CHARSET
,
2082 _("Language \"%s\" not supported."),
2083 attr
->values
[0].string
.text
);
2086 else if (!strcmp(attr
->name
, "notify-user-data") &&
2087 attr
->value_tag
== IPP_TAG_STRING
)
2089 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2091 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2092 _("The notify-user-data value is too large "
2093 "(%d > 63 octets)."),
2094 attr
->values
[0].unknown
.length
);
2100 else if (!strcmp(attr
->name
, "notify-events") &&
2101 attr
->value_tag
== IPP_TAG_KEYWORD
)
2103 for (i
= 0; i
< attr
->num_values
; i
++)
2104 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2106 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2108 send_ipp_status(con
, IPP_BAD_REQUEST
,
2109 _("The notify-lease-duration attribute cannot be "
2110 "used with job subscriptions."));
2113 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2114 attr
->value_tag
== IPP_TAG_INTEGER
)
2115 interval
= attr
->values
[0].integer
;
2120 if (!recipient
&& !pullmethod
)
2123 if (mask
== CUPSD_EVENT_NONE
)
2124 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2126 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2127 recipient
, 0)) != NULL
)
2129 sub
->interval
= interval
;
2131 cupsdSetString(&sub
->owner
, job
->username
);
2135 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2136 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2137 (size_t)sub
->user_data_len
);
2140 ippAddSeparator(con
->response
);
2141 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2142 "notify-subscription-id", sub
->id
);
2144 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
2152 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2155 * Remove all of the subscription attributes from the job request...
2157 * TODO: Optimize this since subscription groups have to come at the
2158 * end of the request...
2161 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2165 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2166 attr
->group_tag
== IPP_TAG_ZERO
)
2169 * Free and remove this attribute...
2172 ippDeleteAttribute(NULL
, attr
);
2177 job
->attrs
->attrs
= next
;
2183 job
->attrs
->last
= prev
;
2184 job
->attrs
->current
= prev
;
2189 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2191 * See RFC 4122 for the definition of UUIDs and the format.
2195 add_job_uuid(cupsd_job_t
*job
) /* I - Job */
2197 char uuid
[64]; /* job-uuid string */
2201 * Add a job-uuid attribute if none exists...
2204 if (!ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2205 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
,
2206 httpAssembleUUID(ServerName
, RemotePort
, job
->dest
, job
->id
,
2207 uuid
, sizeof(uuid
)));
2212 * 'add_printer()' - Add a printer to the system.
2216 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2217 ipp_attribute_t
*uri
) /* I - URI of printer */
2219 http_status_t status
; /* Policy status */
2220 int i
; /* Looping var */
2221 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2222 username
[HTTP_MAX_URI
], /* Username portion of URI */
2223 host
[HTTP_MAX_URI
], /* Host portion of URI */
2224 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2225 int port
; /* Port portion of URI */
2226 cupsd_printer_t
*printer
; /* Printer/class */
2227 ipp_attribute_t
*attr
; /* Printer attribute */
2228 cups_file_t
*fp
; /* Script/PPD file */
2229 char line
[1024]; /* Line from file... */
2230 char srcfile
[1024], /* Source Script/PPD file */
2231 dstfile
[1024]; /* Destination Script/PPD file */
2232 int modify
; /* Non-zero if we are modifying */
2233 int changed_driver
, /* Changed the PPD? */
2234 need_restart_job
, /* Need to restart job? */
2235 set_device_uri
, /* Did we set the device URI? */
2236 set_port_monitor
; /* Did we set the port monitor? */
2239 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2240 con
->number
, uri
->values
[0].string
.text
);
2243 * Do we have a valid URI?
2246 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2247 sizeof(scheme
), username
, sizeof(username
), host
,
2248 sizeof(host
), &port
, resource
, sizeof(resource
));
2250 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2253 * No, return an error...
2256 send_ipp_status(con
, IPP_BAD_REQUEST
,
2257 _("The printer-uri must be of the form "
2258 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2263 * Do we have a valid printer name?
2266 if (!validate_name(resource
+ 10))
2269 * No, return an error...
2272 send_ipp_status(con
, IPP_BAD_REQUEST
,
2273 _("The printer-uri \"%s\" contains invalid characters."),
2274 uri
->values
[0].string
.text
);
2279 * See if the printer already exists; if not, create a new printer...
2282 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2285 * Printer doesn't exist; see if we have a class of the same name...
2288 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
)
2291 * Yes, return an error...
2294 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2295 _("A class named \"%s\" already exists."),
2301 * No, check the default policy then add the printer...
2304 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2306 send_http_error(con
, status
, NULL
);
2310 printer
= cupsdAddPrinter(resource
+ 10);
2313 printer
->printer_id
= NextPrinterId
++;
2315 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2318 send_http_error(con
, status
, printer
);
2325 * Look for attributes and copy them over as needed...
2329 need_restart_job
= 0;
2331 if ((attr
= ippFindAttribute(con
->request
, "printer-is-temporary", IPP_TAG_BOOLEAN
)) != NULL
)
2332 printer
->temporary
= ippGetBoolean(attr
, 0);
2334 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2335 IPP_TAG_TEXT
)) != NULL
)
2336 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2338 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
2339 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
2341 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
2342 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
2344 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
2345 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
2347 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2348 IPP_TAG_TEXT
)) != NULL
)
2349 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2353 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2354 IPP_TAG_URI
)) != NULL
)
2357 * Do we have a valid device URI?
2360 http_uri_status_t uri_status
; /* URI separation status */
2361 char old_device_uri
[1024];
2362 /* Old device URI */
2364 need_restart_job
= 1;
2366 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2367 attr
->values
[0].string
.text
,
2368 scheme
, sizeof(scheme
),
2369 username
, sizeof(username
),
2370 host
, sizeof(host
), &port
,
2371 resource
, sizeof(resource
));
2373 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s device-uri: %s", printer
->name
, httpURIStatusString(uri_status
));
2375 if (uri_status
< HTTP_URI_OK
)
2377 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"."),
2378 attr
->values
[0].string
.text
);
2380 cupsdDeletePrinter(printer
, 0);
2385 if (!strcmp(scheme
, "file"))
2388 * See if the administrator has enabled file devices...
2391 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2394 * File devices are disabled and the URL is not file:/dev/null...
2397 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2398 _("File device URIs have been disabled. "
2399 "To enable, see the FileDevice directive in "
2400 "\"%s/cups-files.conf\"."),
2403 cupsdDeletePrinter(printer
, 0);
2411 * See if the backend exists and is executable...
2414 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2415 if (access(srcfile
, X_OK
))
2418 * Could not find device in list!
2421 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2422 _("Bad device-uri scheme \"%s\"."), scheme
);
2424 cupsdDeletePrinter(printer
, 0);
2430 if (printer
->sanitized_device_uri
)
2431 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2432 sizeof(old_device_uri
));
2434 old_device_uri
[0] = '\0';
2436 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2438 cupsdLogMessage(CUPSD_LOG_INFO
,
2439 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2440 printer
->name
, printer
->sanitized_device_uri
,
2446 set_port_monitor
= 0;
2448 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2449 IPP_TAG_NAME
)) != NULL
)
2451 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2454 need_restart_job
= 1;
2456 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2460 for (i
= 0; i
< supported
->num_values
; i
++)
2461 if (!strcmp(supported
->values
[i
].string
.text
,
2462 attr
->values
[0].string
.text
))
2466 if (!supported
|| i
>= supported
->num_values
)
2468 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"."),
2469 attr
->values
[0].string
.text
);
2471 cupsdDeletePrinter(printer
, 0);
2476 cupsdLogMessage(CUPSD_LOG_INFO
,
2477 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2478 printer
->name
, attr
->values
[0].string
.text
,
2479 printer
->port_monitor
? printer
->port_monitor
: "none");
2481 if (strcmp(attr
->values
[0].string
.text
, "none"))
2482 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2484 cupsdClearString(&printer
->port_monitor
);
2486 set_port_monitor
= 1;
2489 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2490 IPP_TAG_BOOLEAN
)) != NULL
&&
2491 attr
->values
[0].boolean
!= printer
->accepting
)
2493 cupsdLogMessage(CUPSD_LOG_INFO
,
2494 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2495 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2497 printer
->accepting
= attr
->values
[0].boolean
;
2499 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2500 "%s accepting jobs.",
2501 printer
->accepting
? "Now" : "No longer");
2504 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared", IPP_TAG_BOOLEAN
)) != NULL
)
2506 if (ippGetBoolean(attr
, 0) &&
2507 printer
->num_auth_info_required
== 1 &&
2508 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2510 send_ipp_status(con
, IPP_BAD_REQUEST
,
2511 _("Cannot share a remote Kerberized printer."));
2513 cupsdDeletePrinter(printer
, 0);
2518 if (printer
->type
& CUPS_PRINTER_REMOTE
)
2521 * Cannot re-share remote printers.
2524 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Cannot change printer-is-shared for remote queues."));
2526 cupsdDeletePrinter(printer
, 0);
2531 if (printer
->shared
&& !ippGetBoolean(attr
, 0))
2532 cupsdDeregisterPrinter(printer
, 1);
2534 cupsdLogMessage(CUPSD_LOG_INFO
,
2535 "Setting %s printer-is-shared to %d (was %d.)",
2536 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2538 printer
->shared
= ippGetBoolean(attr
, 0);
2539 if (printer
->shared
&& printer
->temporary
)
2540 printer
->temporary
= 0;
2543 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2544 IPP_TAG_ENUM
)) != NULL
)
2546 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2547 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2549 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d."),
2550 attr
->values
[0].integer
);
2552 cupsdDeletePrinter(printer
, 0);
2557 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2558 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2560 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2561 cupsdStopPrinter(printer
, 0);
2564 need_restart_job
= 1;
2565 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2569 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2570 IPP_TAG_TEXT
)) != NULL
)
2572 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2573 sizeof(printer
->state_message
));
2575 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
, "%s",
2576 printer
->state_message
);
2579 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2580 IPP_TAG_KEYWORD
)) != NULL
)
2582 if (attr
->num_values
>
2583 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2585 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2586 _("Too many printer-state-reasons values (%d > %d)."),
2588 (int)(sizeof(printer
->reasons
) /
2589 sizeof(printer
->reasons
[0])));
2591 cupsdDeletePrinter(printer
, 0);
2596 for (i
= 0; i
< printer
->num_reasons
; i
++)
2597 _cupsStrFree(printer
->reasons
[i
]);
2599 printer
->num_reasons
= 0;
2600 for (i
= 0; i
< attr
->num_values
; i
++)
2602 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2605 printer
->reasons
[printer
->num_reasons
] =
2606 _cupsStrRetain(attr
->values
[i
].string
.text
);
2607 printer
->num_reasons
++;
2609 if (!strcmp(attr
->values
[i
].string
.text
, "paused") &&
2610 printer
->state
!= IPP_PRINTER_STOPPED
)
2612 cupsdLogMessage(CUPSD_LOG_INFO
,
2613 "Setting %s printer-state to %d (was %d.)",
2614 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2615 cupsdStopPrinter(printer
, 0);
2619 if (PrintcapFormat
== PRINTCAP_PLIST
)
2620 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2622 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2623 "Printer \"%s\" state changed.", printer
->name
);
2626 if (!set_printer_defaults(con
, printer
))
2629 cupsdDeletePrinter(printer
, 0);
2634 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2635 IPP_TAG_KEYWORD
)) != NULL
)
2636 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2639 * See if we have all required attributes...
2642 if (!printer
->device_uri
)
2643 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2646 * See if we have a PPD file attached to the request...
2651 need_restart_job
= 1;
2654 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2656 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2659 * Yes; get the first line from it...
2663 cupsFileGets(fp
, line
, sizeof(line
));
2667 * Then see what kind of file it is...
2670 if (strncmp(line
, "*PPD-Adobe", 10))
2672 send_ipp_status(con
, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED
, _("Bad PPD file."));
2674 cupsdDeletePrinter(printer
, 0);
2679 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2683 * The new file is a PPD file, so move the file over to the ppd
2687 if (copy_file(srcfile
, dstfile
, ConfigFilePerm
))
2689 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file - %s"), strerror(errno
));
2691 cupsdDeletePrinter(printer
, 0);
2696 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2699 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
)) != NULL
)
2701 const char *ppd_name
= ippGetString(attr
, 0, NULL
);
2702 /* ppd-name value */
2704 need_restart_job
= 1;
2707 if (!strcmp(ppd_name
, "raw"))
2710 * Raw driver, remove any existing PPD file.
2713 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2716 else if (strstr(ppd_name
, "../"))
2718 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
2720 cupsdDeletePrinter(printer
, 0);
2730 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2732 if (copy_model(con
, ppd_name
, dstfile
))
2734 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file."));
2736 cupsdDeletePrinter(printer
, 0);
2741 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2748 * If we changed the PPD, then remove the printer's cache file and clear the
2749 * printer-state-reasons...
2752 char cache_name
[1024]; /* Cache filename for printer attrs */
2754 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.data", CacheDir
, printer
->name
);
2757 cupsdSetPrinterReasons(printer
, "none");
2760 * (Re)register color profiles...
2763 cupsdRegisterColor(printer
);
2767 * If we set the device URI but not the port monitor, check which port
2768 * monitor to use by default...
2771 if (set_device_uri
&& !set_port_monitor
)
2773 ppd_file_t
*ppd
; /* PPD file */
2774 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2777 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2778 sizeof(scheme
), username
, sizeof(username
), host
,
2779 sizeof(host
), &port
, resource
, sizeof(resource
));
2781 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2783 if ((ppd
= _ppdOpenFile(srcfile
, _PPD_LOCALIZATION_NONE
)) != NULL
)
2785 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2787 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2788 if (!strcmp(scheme
, ppdattr
->spec
))
2790 cupsdLogMessage(CUPSD_LOG_INFO
,
2791 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2792 printer
->name
, ppdattr
->value
,
2793 printer
->port_monitor
? printer
->port_monitor
2796 if (strcmp(ppdattr
->value
, "none"))
2797 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2799 cupsdClearString(&printer
->port_monitor
);
2808 printer
->config_time
= time(NULL
);
2811 * Update the printer attributes and return...
2814 if (!printer
->temporary
)
2816 if (!printer
->printer_id
)
2817 printer
->printer_id
= NextPrinterId
++;
2819 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2822 cupsdSetPrinterAttrs(printer
);
2824 if (need_restart_job
&& printer
->job
)
2827 * Restart the current job...
2830 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
2831 "Job restarted because the printer was modified.");
2834 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2838 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
2839 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
2840 printer
->name
, get_username(con
));
2842 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2843 printer
->name
, get_username(con
));
2847 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
2848 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
2849 printer
->name
, get_username(con
));
2851 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2852 printer
->name
, get_username(con
));
2855 con
->response
->request
.status
.status_code
= IPP_OK
;
2860 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2861 * based upon the printer state...
2865 add_printer_state_reasons(
2866 cupsd_client_t
*con
, /* I - Client connection */
2867 cupsd_printer_t
*p
) /* I - Printer info */
2869 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2870 "add_printer_state_reasons(%p[%d], %p[%s])",
2871 con
, con
->number
, p
, p
->name
);
2873 if (p
->num_reasons
== 0)
2874 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2875 "printer-state-reasons", NULL
, "none");
2877 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2878 "printer-state-reasons", p
->num_reasons
, NULL
,
2879 (const char * const *)p
->reasons
);
2884 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2885 * the specified printer or class.
2889 add_queued_job_count(
2890 cupsd_client_t
*con
, /* I - Client connection */
2891 cupsd_printer_t
*p
) /* I - Printer or class */
2893 int count
; /* Number of jobs on destination */
2896 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2897 con
, con
->number
, p
, p
->name
);
2899 count
= cupsdGetPrinterJobCount(p
->name
);
2901 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2902 "queued-job-count", count
);
2907 * 'apply_printer_defaults()' - Apply printer default options to a job.
2911 apply_printer_defaults(
2912 cupsd_printer_t
*printer
, /* I - Printer */
2913 cupsd_job_t
*job
) /* I - Job */
2915 int i
, /* Looping var */
2916 num_options
; /* Number of default options */
2917 cups_option_t
*options
, /* Default options */
2918 *option
; /* Current option */
2921 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Applying default options...");
2924 * Collect all of the default options and add the missing ones to the
2928 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
2929 option
= printer
->options
;
2932 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2934 if (!strcmp(option
->name
, "print-quality") && ippFindAttribute(job
->attrs
, "cupsPrintQuality", IPP_TAG_NAME
))
2935 continue; /* Don't override cupsPrintQuality */
2937 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Adding default %s=%s", option
->name
, option
->value
);
2939 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
, &options
);
2943 * Encode these options as attributes in the job object...
2946 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2947 cupsFreeOptions(num_options
, options
);
2952 * 'authenticate_job()' - Set job authentication info.
2956 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2957 ipp_attribute_t
*uri
) /* I - Job URI */
2959 ipp_attribute_t
*attr
, /* job-id attribute */
2960 *auth_info
; /* auth-info attribute */
2961 int jobid
; /* Job ID */
2962 cupsd_job_t
*job
; /* Current job */
2963 char scheme
[HTTP_MAX_URI
],
2964 /* Method portion of URI */
2965 username
[HTTP_MAX_URI
],
2966 /* Username portion of URI */
2968 /* Host portion of URI */
2969 resource
[HTTP_MAX_URI
];
2970 /* Resource portion of URI */
2971 int port
; /* Port portion of URI */
2974 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2975 con
, con
->number
, uri
->values
[0].string
.text
);
2978 * Start with "everything is OK" status...
2981 con
->response
->request
.status
.status_code
= IPP_OK
;
2984 * See if we have a job URI or a printer URI...
2987 if (!strcmp(uri
->name
, "printer-uri"))
2990 * Got a printer URI; see if we also have a job-id attribute...
2993 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2994 IPP_TAG_INTEGER
)) == NULL
)
2996 send_ipp_status(con
, IPP_BAD_REQUEST
,
2997 _("Got a printer-uri attribute but no job-id."));
3001 jobid
= attr
->values
[0].integer
;
3006 * Got a job URI; parse it to get the job ID...
3009 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3010 sizeof(scheme
), username
, sizeof(username
), host
,
3011 sizeof(host
), &port
, resource
, sizeof(resource
));
3013 if (strncmp(resource
, "/jobs/", 6))
3019 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3020 uri
->values
[0].string
.text
);
3024 jobid
= atoi(resource
+ 6);
3028 * See if the job exists...
3031 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3034 * Nope - return a "not found" error...
3037 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3042 * See if the job has been completed...
3045 if (job
->state_value
!= IPP_JOB_HELD
)
3048 * Return a "not-possible" error...
3051 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3052 _("Job #%d is not held for authentication."),
3058 * See if we have already authenticated...
3061 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3063 if (!con
->username
[0] && !auth_info
)
3065 cupsd_printer_t
*printer
; /* Job destination */
3068 * No auth data. If we need to authenticate via Kerberos, send a
3069 * HTTP auth challenge, otherwise just return an IPP error...
3072 printer
= cupsdFindDest(job
->dest
);
3074 if (printer
&& printer
->num_auth_info_required
> 0 &&
3075 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3076 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3078 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3079 _("No authentication information provided."));
3084 * See if the job is owned by the requesting user...
3087 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3089 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3090 cupsdFindDest(job
->dest
));
3095 * Save the authentication information for this job...
3098 save_auth_info(con
, job
, auth_info
);
3101 * Reset the job-hold-until value to "no-hold"...
3104 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3105 IPP_TAG_KEYWORD
)) == NULL
)
3106 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3110 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
3111 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
3115 * Release the job and return...
3118 cupsdReleaseJob(job
);
3120 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, NULL
, job
, "Job authenticated by user");
3122 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3129 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3133 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3134 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3136 int i
; /* Looping var */
3137 http_status_t status
; /* Policy status */
3138 cups_ptype_t dtype
; /* Destination type */
3139 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3140 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3141 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3142 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3143 int port
; /* Port portion of URI */
3144 ipp_attribute_t
*attr
; /* Attribute in request */
3145 const char *username
= NULL
; /* Username */
3146 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3148 cupsd_printer_t
*printer
; /* Printer */
3149 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3150 cupsd_job_t
*job
; /* Job */
3153 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3154 con
->number
, uri
->values
[0].string
.text
);
3157 * Get the jobs to cancel/purge...
3160 switch (con
->request
->request
.op
.operation_id
)
3162 case IPP_PURGE_JOBS
:
3164 * Get the username (if any) for the jobs we want to cancel (only if
3165 * "my-jobs" is specified...
3168 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3169 IPP_TAG_BOOLEAN
)) != NULL
&&
3170 attr
->values
[0].boolean
)
3172 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3173 IPP_TAG_NAME
)) != NULL
)
3174 username
= attr
->values
[0].string
.text
;
3177 send_ipp_status(con
, IPP_BAD_REQUEST
,
3178 _("Missing requesting-user-name attribute."));
3184 * Look for the "purge-jobs" attribute...
3187 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3188 IPP_TAG_BOOLEAN
)) != NULL
)
3189 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3191 purge
= CUPSD_JOB_PURGE
;
3194 case IPP_CANCEL_MY_JOBS
:
3195 if (con
->username
[0])
3196 username
= con
->username
;
3197 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3198 IPP_TAG_NAME
)) != NULL
)
3199 username
= attr
->values
[0].string
.text
;
3202 send_ipp_status(con
, IPP_BAD_REQUEST
,
3203 _("Missing requesting-user-name attribute."));
3211 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3214 * See if we have a printer URI...
3217 if (strcmp(uri
->name
, "printer-uri"))
3219 send_ipp_status(con
, IPP_BAD_REQUEST
,
3220 _("The printer-uri attribute is required."));
3225 * And if the destination is valid...
3228 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3234 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3235 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3236 hostname
, sizeof(hostname
), &port
,
3237 resource
, sizeof(resource
));
3239 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3240 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3242 send_ipp_status(con
, IPP_NOT_FOUND
,
3243 _("The printer or class does not exist."));
3251 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3253 send_http_error(con
, status
, NULL
);
3259 for (i
= 0; i
< job_ids
->num_values
; i
++)
3261 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
3264 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3265 _cups_strcasecmp(job
->username
, username
))
3269 if (i
< job_ids
->num_values
)
3271 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3272 job_ids
->values
[i
].integer
);
3276 for (i
= 0; i
< job_ids
->num_values
; i
++)
3278 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3280 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3281 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3282 "Job canceled by user.");
3285 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3286 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3292 * Cancel all jobs on all printers...
3295 cupsdCancelJobs(NULL
, username
, purge
);
3297 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3298 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3308 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3311 send_http_error(con
, status
, printer
);
3317 for (i
= 0; i
< job_ids
->num_values
; i
++)
3319 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
3320 _cups_strcasecmp(job
->dest
, printer
->name
))
3323 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3324 _cups_strcasecmp(job
->username
, username
))
3328 if (i
< job_ids
->num_values
)
3330 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3331 job_ids
->values
[i
].integer
);
3335 for (i
= 0; i
< job_ids
->num_values
; i
++)
3337 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3339 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3340 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3341 "Job canceled by user.");
3344 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3345 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3351 * Cancel all of the jobs on the named printer...
3354 cupsdCancelJobs(printer
->name
, username
, purge
);
3356 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3358 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3363 con
->response
->request
.status
.status_code
= IPP_OK
;
3370 * 'cancel_job()' - Cancel a print job.
3374 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3375 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3377 ipp_attribute_t
*attr
; /* Current attribute */
3378 int jobid
; /* Job ID */
3379 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3380 username
[HTTP_MAX_URI
], /* Username portion of URI */
3381 host
[HTTP_MAX_URI
], /* Host portion of URI */
3382 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3383 int port
; /* Port portion of URI */
3384 cupsd_job_t
*job
; /* Job information */
3385 cups_ptype_t dtype
; /* Destination type (printer/class) */
3386 cupsd_printer_t
*printer
; /* Printer data */
3387 cupsd_jobaction_t purge
; /* Purge the job? */
3390 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3391 con
->number
, uri
->values
[0].string
.text
);
3394 * See if we have a job URI or a printer URI...
3397 if (!strcmp(uri
->name
, "printer-uri"))
3400 * Got a printer URI; see if we also have a job-id attribute...
3403 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3404 IPP_TAG_INTEGER
)) == NULL
)
3406 send_ipp_status(con
, IPP_BAD_REQUEST
,
3407 _("Got a printer-uri attribute but no job-id."));
3411 if ((jobid
= attr
->values
[0].integer
) == 0)
3414 * Find the current job on the specified printer...
3417 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3423 send_ipp_status(con
, IPP_NOT_FOUND
,
3424 _("The printer or class does not exist."));
3429 * See if there are any pending jobs...
3432 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3434 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3435 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3436 !_cups_strcasecmp(job
->dest
, printer
->name
))
3444 * No, try stopped jobs...
3447 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3449 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3450 if (job
->state_value
== IPP_JOB_STOPPED
&&
3451 !_cups_strcasecmp(job
->dest
, printer
->name
))
3458 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3468 * Got a job URI; parse it to get the job ID...
3471 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3472 sizeof(scheme
), username
, sizeof(username
), host
,
3473 sizeof(host
), &port
, resource
, sizeof(resource
));
3475 if (strncmp(resource
, "/jobs/", 6))
3481 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3482 uri
->values
[0].string
.text
);
3486 jobid
= atoi(resource
+ 6);
3490 * Look for the "purge-job" attribute...
3493 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3494 IPP_TAG_BOOLEAN
)) != NULL
)
3495 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3497 purge
= CUPSD_JOB_DEFAULT
;
3500 * See if the job exists...
3503 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3506 * Nope - return a "not found" error...
3509 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3514 * See if the job is owned by the requesting user...
3517 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3519 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3520 cupsdFindDest(job
->dest
));
3525 * See if the job is already completed, canceled, or aborted; if so,
3526 * we can't cancel...
3529 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3531 switch (job
->state_value
)
3533 case IPP_JOB_CANCELED
:
3534 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3535 _("Job #%d is already canceled - can\'t cancel."),
3539 case IPP_JOB_ABORTED
:
3540 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3541 _("Job #%d is already aborted - can\'t cancel."),
3546 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3547 _("Job #%d is already completed - can\'t cancel."),
3556 * Cancel the job and return...
3559 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3560 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3561 "Job canceled by \"%s\"",
3565 if (purge
== CUPSD_JOB_PURGE
)
3566 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3569 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3572 con
->response
->request
.status
.status_code
= IPP_OK
;
3577 * 'cancel_subscription()' - Cancel a subscription.
3581 cancel_subscription(
3582 cupsd_client_t
*con
, /* I - Client connection */
3583 int sub_id
) /* I - Subscription ID */
3585 http_status_t status
; /* Policy status */
3586 cupsd_subscription_t
*sub
; /* Subscription */
3589 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3590 "cancel_subscription(con=%p[%d], sub_id=%d)",
3591 con
, con
->number
, sub_id
);
3594 * Is the subscription ID valid?
3597 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3600 * Bad subscription ID...
3603 send_ipp_status(con
, IPP_NOT_FOUND
,
3604 _("Subscription #%d does not exist."), sub_id
);
3612 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3614 con
, sub
->owner
)) != HTTP_OK
)
3616 send_http_error(con
, status
, sub
->dest
);
3621 * Cancel the subscription...
3624 cupsdDeleteSubscription(sub
, 1);
3626 con
->response
->request
.status
.status_code
= IPP_OK
;
3631 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3634 static int /* O - 1 if OK, 0 if not */
3635 check_rss_recipient(
3636 const char *recipient
) /* I - Recipient URI */
3638 cupsd_subscription_t
*sub
; /* Current subscription */
3641 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3643 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3647 * Compare the URIs up to the first ?...
3650 const char *r1
, *r2
;
3652 for (r1
= recipient
, r2
= sub
->recipient
;
3653 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3665 * 'check_quotas()' - Check quotas for a printer and user.
3668 static int /* O - 1 if OK, 0 if forbidden,
3669 -1 if limit reached */
3670 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3671 cupsd_printer_t
*p
) /* I - Printer or class */
3673 char username
[33], /* Username */
3674 *name
; /* Current user name */
3675 cupsd_quota_t
*q
; /* Quota data */
3676 #ifdef HAVE_MBR_UID_TO_UUID
3678 * Use Apple membership APIs which require that all names represent
3679 * valid user account or group records accessible by the server.
3682 uuid_t usr_uuid
; /* UUID for job requesting user */
3683 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3684 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3685 int mbr_err
; /* Error from membership function */
3686 int is_member
; /* Is this user a member? */
3689 * Use standard POSIX APIs for checking users and groups...
3692 struct passwd
*pw
; /* User password data */
3693 #endif /* HAVE_MBR_UID_TO_UUID */
3696 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3697 con
, con
->number
, p
, p
->name
);
3700 * Figure out who is printing...
3703 strlcpy(username
, get_username(con
), sizeof(username
));
3705 if ((name
= strchr(username
, '@')) != NULL
)
3706 *name
= '\0'; /* Strip @REALM */
3709 * Check global active job limits for printers and users...
3712 if (MaxJobsPerPrinter
)
3715 * Check if there are too many pending jobs on this printer...
3718 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3720 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3729 * Check if there are too many pending jobs for this user...
3732 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3734 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3741 * Check against users...
3744 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3747 if (cupsArrayCount(p
->users
))
3749 #ifdef HAVE_MBR_UID_TO_UUID
3751 * Get UUID for job requesting user...
3754 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3760 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3761 "check_quotas: UUID lookup failed for user \"%s\"",
3763 cupsdLogMessage(CUPSD_LOG_INFO
,
3764 "Denying user \"%s\" access to printer \"%s\" "
3765 "(unknown user)...",
3771 * Get UID and GID of requesting user...
3774 pw
= getpwnam(username
);
3776 #endif /* HAVE_MBR_UID_TO_UUID */
3778 for (name
= (char *)cupsArrayFirst(p
->users
);
3780 name
= (char *)cupsArrayNext(p
->users
))
3784 * Check group membership...
3787 #ifdef HAVE_MBR_UID_TO_UUID
3790 if (uuid_parse(name
+ 2, grp_uuid
))
3791 uuid_clear(grp_uuid
);
3793 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3796 * Invalid ACL entries are ignored for matching; just record a
3797 * warning in the log...
3800 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3801 "check_quotas: UUID lookup failed for ACL entry "
3802 "\"%s\" (err=%d)", name
, mbr_err
);
3803 cupsdLogMessage(CUPSD_LOG_WARN
,
3804 "Access control entry \"%s\" not a valid group name; "
3805 "entry ignored", name
);
3808 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3812 * At this point, there should be no errors, but check anyways...
3815 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3816 "check_quotas: group \"%s\" membership check "
3817 "failed (err=%d)", name
+ 1, mbr_err
);
3822 * Stop if we found a match...
3829 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3831 #endif /* HAVE_MBR_UID_TO_UUID */
3833 #ifdef HAVE_MBR_UID_TO_UUID
3838 if (uuid_parse(name
+ 1, usr2_uuid
))
3839 uuid_clear(usr2_uuid
);
3841 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3844 * Invalid ACL entries are ignored for matching; just record a
3845 * warning in the log...
3848 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3849 "check_quotas: UUID lookup failed for ACL entry "
3850 "\"%s\" (err=%d)", name
, mbr_err
);
3851 cupsdLogMessage(CUPSD_LOG_WARN
,
3852 "Access control entry \"%s\" not a valid user name; "
3853 "entry ignored", name
);
3856 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3860 else if (!_cups_strcasecmp(username
, name
))
3862 #endif /* HAVE_MBR_UID_TO_UUID */
3864 if ((name
!= NULL
) == p
->deny_users
)
3866 cupsdLogMessage(CUPSD_LOG_INFO
,
3867 "Denying user \"%s\" access to printer \"%s\"...",
3877 if (p
->k_limit
|| p
->page_limit
)
3879 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3881 cupsdLogMessage(CUPSD_LOG_ERROR
,
3882 "Unable to allocate quota data for user \"%s\"",
3887 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3888 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3890 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3897 * If we have gotten this far, we're done!
3905 * 'close_job()' - Close a multi-file job.
3909 close_job(cupsd_client_t
*con
, /* I - Client connection */
3910 ipp_attribute_t
*uri
) /* I - Printer URI */
3912 cupsd_job_t
*job
; /* Job */
3913 ipp_attribute_t
*attr
; /* Attribute */
3914 char job_uri
[HTTP_MAX_URI
],
3916 username
[256]; /* User name */
3919 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3920 con
->number
, uri
->values
[0].string
.text
);
3923 * See if we have a job URI or a printer URI...
3926 if (strcmp(uri
->name
, "printer-uri"))
3929 * job-uri is not supported by Close-Job!
3932 send_ipp_status(con
, IPP_BAD_REQUEST
,
3933 _("Close-Job doesn't support the job-uri attribute."));
3938 * Got a printer URI; see if we also have a job-id attribute...
3941 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3942 IPP_TAG_INTEGER
)) == NULL
)
3944 send_ipp_status(con
, IPP_BAD_REQUEST
,
3945 _("Got a printer-uri attribute but no job-id."));
3949 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
3952 * Nope - return a "not found" error...
3955 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3956 attr
->values
[0].integer
);
3961 * See if the job is owned by the requesting user...
3964 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3966 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3967 cupsdFindDest(job
->dest
));
3972 * Add any ending sheet...
3975 if (cupsdTimeoutJob(job
))
3978 if (job
->state_value
== IPP_JOB_STOPPED
)
3980 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3981 job
->state_value
= IPP_JOB_PENDING
;
3983 else if (job
->state_value
== IPP_JOB_HELD
)
3985 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3986 IPP_TAG_KEYWORD
)) == NULL
)
3987 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3989 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
3991 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3992 job
->state_value
= IPP_JOB_PENDING
;
3997 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
4000 * Fill in the response info...
4003 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4004 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
4005 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
4008 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4010 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
4013 con
->response
->request
.status
.status_code
= IPP_OK
;
4016 * Start the job if necessary...
4024 * 'copy_attrs()' - Copy attributes from one request to another.
4028 copy_attrs(ipp_t
*to
, /* I - Destination request */
4029 ipp_t
*from
, /* I - Source request */
4030 cups_array_t
*ra
, /* I - Requested attributes */
4031 ipp_tag_t group
, /* I - Group to copy */
4032 int quickcopy
, /* I - Do a quick copy? */
4033 cups_array_t
*exclude
) /* I - Attributes to exclude? */
4035 ipp_attribute_t
*fromattr
; /* Source attribute */
4038 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4039 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4040 to
, from
, ra
, group
, quickcopy
);
4045 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4048 * Filter attributes as needed...
4051 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4052 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4055 if (!strcmp(fromattr
->name
, "document-password") ||
4056 !strcmp(fromattr
->name
, "job-authorization-uri") ||
4057 !strcmp(fromattr
->name
, "job-password") ||
4058 !strcmp(fromattr
->name
, "job-password-encryption") ||
4059 !strcmp(fromattr
->name
, "job-printer-uri"))
4063 (cupsArrayFind(exclude
, fromattr
->name
) ||
4064 cupsArrayFind(exclude
, "all")))
4067 * We need to exclude this attribute for security reasons; we require the
4068 * job-id attribute regardless of the security settings for IPP
4071 * The job-printer-uri attribute is handled by copy_job_attrs().
4073 * Subscription attribute security is handled by copy_subscription_attrs().
4076 if (strcmp(fromattr
->name
, "job-id"))
4080 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4083 * Don't send collection attributes by default to IPP/1.x clients
4084 * since many do not support collections. Also don't send
4085 * media-col-database unless specifically requested by the client.
4088 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4090 (to
->request
.status
.version
[0] == 1 ||
4091 !strcmp(fromattr
->name
, "media-col-database")))
4094 ippCopyAttribute(to
, fromattr
, quickcopy
);
4101 * 'copy_banner()' - Copy a banner file to the requests directory for the
4105 static int /* O - Size of banner file in kbytes */
4106 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4107 cupsd_job_t
*job
, /* I - Job information */
4108 const char *name
) /* I - Name of banner */
4110 int i
; /* Looping var */
4111 int kbytes
; /* Size of banner file in kbytes */
4112 char filename
[1024]; /* Job filename */
4113 cupsd_banner_t
*banner
; /* Pointer to banner */
4114 cups_file_t
*in
; /* Input file */
4115 cups_file_t
*out
; /* Output file */
4116 int ch
; /* Character from file */
4117 char attrname
[255], /* Name of attribute */
4118 *s
; /* Pointer into name */
4119 ipp_attribute_t
*attr
; /* Attribute */
4122 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4123 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4124 con
, con
? con
->number
: -1, job
, job
->id
,
4125 name
? name
: "(null)");
4128 * Find the banner; return if not found or "none"...
4131 if (!name
|| !strcmp(name
, "none") ||
4132 (banner
= cupsdFindBanner(name
)) == NULL
)
4136 * Open the banner and job files...
4139 if (add_file(con
, job
, banner
->filetype
, 0))
4142 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4144 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4146 cupsdLogMessage(CUPSD_LOG_ERROR
,
4147 "Unable to create banner job file %s - %s",
4148 filename
, strerror(errno
));
4153 fchmod(cupsFileNumber(out
), 0640);
4154 fchown(cupsFileNumber(out
), RunUser
, Group
);
4157 * Try the localized banner file under the subdirectory...
4160 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4162 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4165 * Convert ll-cc to ll_CC...
4169 attrname
[3] = (char)toupper(attrname
[3] & 255);
4170 attrname
[4] = (char)toupper(attrname
[4] & 255);
4173 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4176 if (access(filename
, 0) && strlen(attrname
) > 2)
4179 * Wasn't able to find "ll_CC" locale file; try the non-national
4180 * localization banner directory.
4185 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4189 if (access(filename
, 0))
4192 * Use the non-localized banner file.
4195 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4198 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4202 cupsdLogMessage(CUPSD_LOG_ERROR
,
4203 "Unable to open banner template file %s - %s",
4204 filename
, strerror(errno
));
4210 * Parse the file to the end...
4213 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4217 * Get an attribute name...
4220 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4221 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4223 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4233 * Ignore { followed by stuff that is not an attribute name...
4236 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4241 * See if it is defined...
4244 if (attrname
[0] == '?')
4249 if (!strcmp(s
, "printer-name"))
4251 cupsFilePuts(out
, job
->dest
);
4254 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4257 * See if we have a leading question mark...
4260 if (attrname
[0] != '?')
4263 * Nope, write to file as-is; probably a PostScript procedure...
4266 cupsFilePrintf(out
, "{%s}", attrname
);
4273 * Output value(s)...
4276 for (i
= 0; i
< attr
->num_values
; i
++)
4279 cupsFilePutChar(out
, ',');
4281 switch (attr
->value_tag
)
4283 case IPP_TAG_INTEGER
:
4285 if (!strncmp(s
, "time-at-", 8))
4287 struct timeval tv
; /* Time value */
4289 tv
.tv_sec
= attr
->values
[i
].integer
;
4292 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4295 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4298 case IPP_TAG_BOOLEAN
:
4299 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4302 case IPP_TAG_NOVALUE
:
4303 cupsFilePuts(out
, "novalue");
4306 case IPP_TAG_RANGE
:
4307 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4308 attr
->values
[i
].range
.upper
);
4311 case IPP_TAG_RESOLUTION
:
4312 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4313 attr
->values
[i
].resolution
.yres
,
4314 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4319 case IPP_TAG_STRING
:
4322 case IPP_TAG_KEYWORD
:
4323 case IPP_TAG_CHARSET
:
4324 case IPP_TAG_LANGUAGE
:
4325 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4328 * Need to quote strings for PS banners...
4333 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4335 if (*p
== '(' || *p
== ')' || *p
== '\\')
4337 cupsFilePutChar(out
, '\\');
4338 cupsFilePutChar(out
, *p
);
4340 else if (*p
< 32 || *p
> 126)
4341 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4343 cupsFilePutChar(out
, *p
);
4347 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4351 break; /* anti-compiler-warning-code */
4355 else if (ch
== '\\') /* Quoted char */
4357 ch
= cupsFileGetChar(in
);
4359 if (ch
!= '{') /* Only do special handling for \{ */
4360 cupsFilePutChar(out
, '\\');
4362 cupsFilePutChar(out
, ch
);
4365 cupsFilePutChar(out
, ch
);
4369 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4371 job
->koctets
+= kbytes
;
4373 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4374 attr
->values
[0].integer
+= kbytes
;
4383 * 'copy_file()' - Copy a PPD file...
4386 static int /* O - 0 = success, -1 = error */
4387 copy_file(const char *from
, /* I - Source file */
4388 const char *to
, /* I - Destination file */
4389 mode_t mode
) /* I - Permissions */
4391 cups_file_t
*src
, /* Source file */
4392 *dst
; /* Destination file */
4393 int bytes
; /* Bytes to read/write */
4394 char buffer
[2048]; /* Copy buffer */
4397 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4400 * Open the source and destination file for a copy...
4403 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4406 if ((dst
= cupsdCreateConfFile(to
, mode
)) == NULL
)
4413 * Copy the source file to the destination...
4416 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4417 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4425 * Close both files and return...
4430 return (cupsdCloseCreatedConfFile(dst
, to
));
4435 * 'copy_model()' - Copy a PPD model file, substituting default values
4439 static int /* O - 0 = success, -1 = error */
4440 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4441 const char *from
, /* I - Source file */
4442 const char *to
) /* I - Destination file */
4444 fd_set input
; /* select() input set */
4445 struct timeval timeout
; /* select() timeout */
4446 int maxfd
; /* Max file descriptor for select() */
4447 char tempfile
[1024]; /* Temporary PPD file */
4448 int tempfd
; /* Temporary PPD file descriptor */
4449 int temppid
; /* Process ID of cups-driverd */
4450 int temppipe
[2]; /* Temporary pipes */
4451 char *argv
[4], /* Command-line arguments */
4452 *envp
[MAX_ENV
]; /* Environment */
4453 cups_file_t
*src
, /* Source file */
4454 *dst
; /* Destination file */
4455 ppd_file_t
*ppd
; /* PPD file */
4456 int bytes
, /* Bytes from pipe */
4457 total
; /* Total bytes from pipe */
4458 char buffer
[2048]; /* Copy buffer */
4459 int i
; /* Looping var */
4460 char option
[PPD_MAX_NAME
], /* Option name */
4461 choice
[PPD_MAX_NAME
]; /* Choice name */
4462 ppd_size_t
*size
; /* Default size */
4463 int num_defaults
; /* Number of default options */
4464 cups_option_t
*defaults
; /* Default options */
4465 char cups_protocol
[PPD_MAX_LINE
];
4466 /* cupsProtocol attribute */
4469 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_model(con=%p, from=\"%s\", to=\"%s\")", con
, from
, to
);
4472 * Run cups-driverd to get the PPD file...
4475 argv
[0] = "cups-driverd";
4477 argv
[2] = (char *)from
;
4480 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4482 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4483 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4484 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4485 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4488 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4489 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4491 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4492 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4503 * Wait up to 30 seconds for the PPD file to be copied...
4508 if (temppipe
[0] > CGIPipes
[0])
4509 maxfd
= temppipe
[0] + 1;
4511 maxfd
= CGIPipes
[0] + 1;
4516 * See if we have data ready...
4520 FD_SET(temppipe
[0], &input
);
4521 FD_SET(CGIPipes
[0], &input
);
4523 timeout
.tv_sec
= 30;
4524 timeout
.tv_usec
= 0;
4526 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4536 * We have timed out...
4542 if (FD_ISSET(temppipe
[0], &input
))
4545 * Read the PPD file from the pipe, and write it to the PPD file.
4548 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4550 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4559 if (FD_ISSET(CGIPipes
[0], &input
))
4569 * No data from cups-deviced...
4572 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4578 * Open the source file for a copy...
4581 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4588 * Read the source file and see what page sizes are supported...
4591 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4599 * Open the destination (if possible) and set the default options...
4604 cups_protocol
[0] = '\0';
4606 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4609 * Read all of the default lines from the old PPD...
4612 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4613 if (!strncmp(buffer
, "*Default", 8))
4616 * Add the default option...
4619 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4620 choice
, sizeof(choice
)))
4622 ppd_option_t
*ppdo
; /* PPD option */
4626 * Only add the default if the default hasn't already been
4627 * set and the choice exists in the new PPD...
4630 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4631 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4632 ppdFindChoice(ppdo
, choice
))
4633 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4637 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4638 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4642 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4645 * Add the default media sizes...
4648 num_defaults
= cupsAddOption("PageSize", size
->name
,
4649 num_defaults
, &defaults
);
4650 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4651 num_defaults
, &defaults
);
4652 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4653 num_defaults
, &defaults
);
4654 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4655 num_defaults
, &defaults
);
4661 * Open the destination file for a copy...
4664 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4666 cupsFreeOptions(num_defaults
, defaults
);
4673 * Copy the source file to the destination...
4676 cupsFileRewind(src
);
4678 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4680 if (!strncmp(buffer
, "*Default", 8))
4683 * Check for an previous default option choice...
4686 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4687 choice
, sizeof(choice
)))
4689 const char *val
; /* Default option value */
4692 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4695 * Substitute the previous choice...
4698 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4703 cupsFilePrintf(dst
, "%s\n", buffer
);
4706 if (cups_protocol
[0])
4707 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4709 cupsFreeOptions(num_defaults
, defaults
);
4712 * Close both files and return...
4719 return (cupsdCloseCreatedConfFile(dst
, to
));
4724 * 'copy_job_attrs()' - Copy job attributes.
4728 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4729 cupsd_job_t
*job
, /* I - Job */
4730 cups_array_t
*ra
, /* I - Requested attributes array */
4731 cups_array_t
*exclude
) /* I - Private attributes array */
4733 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4737 * Send the requested attributes for each job...
4740 if (!cupsArrayFind(exclude
, "all"))
4742 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4743 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4744 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4745 "number-of-documents", job
->num_files
);
4747 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4748 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4749 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4750 "job-media-progress", job
->progress
);
4752 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4753 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4755 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4756 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4758 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4759 "job-more-info", NULL
, job_uri
);
4762 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4763 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4764 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4765 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4766 job
->num_files
> 0);
4768 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4769 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4770 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4771 "job-printer-up-time", time(NULL
));
4774 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4776 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4777 con
->clientname
, con
->clientport
,
4778 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4781 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4782 "job-printer-uri", NULL
, job_uri
);
4785 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4787 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4788 con
->clientname
, con
->clientport
, "/jobs/%d",
4790 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4791 "job-uri", NULL
, job_uri
);
4796 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4801 * Generate attributes from the job structure...
4804 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-completed")))
4805 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-completed", ippTimeToDate(job
->completed_time
));
4807 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-creation")))
4808 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(job
->creation_time
));
4810 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4811 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4813 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4814 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4816 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4817 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4819 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4820 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4822 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4823 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4825 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4827 switch (job
->state_value
)
4829 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4831 case IPP_JSTATE_ABORTED
:
4832 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4834 case IPP_JSTATE_CANCELED
:
4835 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4837 case IPP_JSTATE_COMPLETED
:
4838 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4843 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4844 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4846 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4847 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4853 * 'copy_printer_attrs()' - Copy printer attributes.
4858 cupsd_client_t
*con
, /* I - Client connection */
4859 cupsd_printer_t
*printer
, /* I - Printer */
4860 cups_array_t
*ra
) /* I - Requested attributes array */
4862 char uri
[HTTP_MAX_URI
]; /* URI value */
4863 time_t curtime
; /* Current time */
4864 int i
; /* Looping var */
4865 int is_encrypted
= httpIsEncrypted(con
->http
);
4866 /* Is the connection encrypted? */
4870 * Copy the printer attributes to the response using requested-attributes
4871 * and document-format attributes that may be provided by the client.
4874 _cupsRWLockRead(&printer
->lock
);
4876 curtime
= time(NULL
);
4878 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4879 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "marker-change-time", printer
->marker_time
);
4881 if (printer
->num_printers
> 0 && (!ra
|| cupsArrayFind(ra
, "member-uris")))
4883 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4884 cupsd_printer_t
*p2
; /* Printer in class */
4885 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4888 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", printer
->num_printers
, NULL
, NULL
)) != NULL
)
4890 for (i
= 0; i
< printer
->num_printers
; i
++)
4892 p2
= printer
->printers
[i
];
4894 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
4896 member_uris
->values
[i
].string
.text
= _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4900 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), is_encrypted
? "ipps" : "ipp", NULL
, con
->clientname
, con
->clientport
, (p2
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" : "/printers/%s", p2
->name
);
4901 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
4907 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4908 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
, "printer-alert", NULL
, printer
->alert
);
4910 if (printer
->alert_description
&& (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4911 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-alert-description", NULL
, printer
->alert_description
);
4913 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-date-time"))
4914 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-config-change-date-time", ippTimeToDate(printer
->config_time
));
4916 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-time"))
4917 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "printer-config-change-time", printer
->config_time
);
4919 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4920 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time", ippTimeToDate(curtime
));
4922 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4923 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4925 if (printer
->reg_name
)
4926 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-dns-sd-name", NULL
, printer
->reg_name
);
4928 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
, "printer-dns-sd-name", 0);
4930 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4932 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4933 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-error-policy", NULL
, printer
->error_policy
);
4935 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
4937 static const char * const errors
[] =/* printer-error-policy-supported values */
4940 "retry-current-job",
4945 if (printer
->type
& CUPS_PRINTER_CLASS
)
4946 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
, "printer-error-policy-supported", NULL
, "retry-current-job");
4948 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
, "printer-error-policy-supported", sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
4951 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
4953 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), is_encrypted
? "https" : "http", NULL
, con
->clientname
, con
->clientport
, "/icons/%s.png", printer
->name
);
4954 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons", NULL
, uri
);
4955 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", uri
);
4958 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4959 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
4961 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4962 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
4964 if (!ra
|| cupsArrayFind(ra
, "printer-is-temporary"))
4965 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-temporary", (char)printer
->temporary
);
4967 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
4969 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), is_encrypted
? "https" : "http", NULL
, con
->clientname
, con
->clientport
, (printer
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" : "/printers/%s", printer
->name
);
4970 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info", NULL
, uri
);
4973 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4974 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-op-policy", NULL
, printer
->op_policy
);
4976 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4977 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", printer
->state
);
4979 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-date-time"))
4980 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-state-change-date-time", ippTimeToDate(printer
->state_time
));
4982 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4983 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "printer-state-change-time", printer
->state_time
);
4985 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4986 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message", NULL
, printer
->state_message
);
4988 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4989 add_printer_state_reasons(con
, printer
);
4991 if (!ra
|| cupsArrayFind(ra
, "printer-strings-uri"))
4993 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), is_encrypted
? "https" : "http", NULL
, con
->clientname
, con
->clientport
, "/strings/%s.strings", printer
->name
);
4994 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-strings-uri", NULL
, uri
);
4995 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-strings-uri=\"%s\"", uri
);
4998 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5000 cups_ptype_t type
; /* printer-type value */
5003 * Add the CUPS-specific printer-type attribute...
5006 type
= printer
->type
;
5008 if (printer
== DefaultPrinter
)
5009 type
|= CUPS_PRINTER_DEFAULT
;
5011 if (!printer
->accepting
)
5012 type
|= CUPS_PRINTER_REJECTING
;
5014 if (!printer
->shared
)
5015 type
|= CUPS_PRINTER_NOT_SHARED
;
5017 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
5020 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5021 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "printer-up-time", curtime
);
5023 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
5025 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), is_encrypted
? "ipps" : "ipp", NULL
, con
->clientname
, con
->clientport
, (printer
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" : "/printers/%s", printer
->name
);
5026 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported", NULL
, uri
);
5027 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"", uri
);
5030 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5031 add_queued_job_count(con
, printer
);
5033 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5034 if (printer
->ppd_attrs
)
5035 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5036 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
5038 _cupsRWUnlock(&printer
->lock
);
5043 * 'copy_subscription_attrs()' - Copy subscription attributes.
5047 copy_subscription_attrs(
5048 cupsd_client_t
*con
, /* I - Client connection */
5049 cupsd_subscription_t
*sub
, /* I - Subscription */
5050 cups_array_t
*ra
, /* I - Requested attributes array */
5051 cups_array_t
*exclude
) /* I - Private attributes array */
5053 ipp_attribute_t
*attr
; /* Current attribute */
5054 char printer_uri
[HTTP_MAX_URI
];
5056 int count
; /* Number of events */
5057 unsigned mask
; /* Current event mask */
5058 const char *name
; /* Current event name */
5061 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5062 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5063 con
, sub
, ra
, exclude
);
5066 * Copy the subscription attributes to the response using the
5067 * requested-attributes attribute that may be provided by the client.
5070 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5072 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5073 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5075 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5077 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5080 * Simple event list...
5083 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5084 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5085 "notify-events", NULL
, name
);
5090 * Complex event list...
5093 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5094 if (sub
->mask
& mask
)
5097 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5098 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5099 "notify-events", count
, NULL
, NULL
);
5101 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5102 if (sub
->mask
& mask
)
5104 attr
->values
[count
].string
.text
=
5105 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5112 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5113 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5114 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5115 "notify-lease-duration", sub
->lease
);
5117 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5118 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5119 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5120 "notify-recipient-uri", NULL
, sub
->recipient
);
5121 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5122 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5123 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5124 "notify-pull-method", NULL
, "ippget");
5126 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5127 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5128 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5129 "notify-subscriber-user-name", NULL
, sub
->owner
);
5131 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5132 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5133 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5134 "notify-time-interval", sub
->interval
);
5136 if (sub
->user_data_len
> 0 &&
5137 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5138 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5139 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5140 sub
->user_data
, sub
->user_data_len
);
5143 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5144 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5145 "notify-job-id", sub
->job
->id
);
5147 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5149 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5150 "ipp", NULL
, con
->clientname
, con
->clientport
,
5151 "/printers/%s", sub
->dest
->name
);
5152 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5153 "notify-printer-uri", NULL
, printer_uri
);
5156 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5157 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5158 "notify-subscription-id", sub
->id
);
5163 * 'create_job()' - Print a file to a printer or class.
5167 create_job(cupsd_client_t
*con
, /* I - Client connection */
5168 ipp_attribute_t
*uri
) /* I - Printer URI */
5170 int i
; /* Looping var */
5171 cupsd_printer_t
*printer
; /* Printer */
5172 cupsd_job_t
*job
; /* New job */
5173 static const char * const forbidden_attrs
[] =
5174 { /* List of forbidden attributes */
5178 "document-natural-language"
5182 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5183 con
->number
, uri
->values
[0].string
.text
);
5186 * Is the destination valid?
5189 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5195 send_ipp_status(con
, IPP_NOT_FOUND
,
5196 _("The printer or class does not exist."));
5201 * Check for invalid Create-Job attributes and log a warning or error depending
5202 * on whether cupsd is running in "strict conformance" mode...
5206 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5208 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5210 if (StrictConformance
)
5212 send_ipp_status(con
, IPP_BAD_REQUEST
,
5213 _("The '%s' operation attribute cannot be supplied in a "
5214 "Create-Job request."), forbidden_attrs
[i
]);
5218 cupsdLogMessage(CUPSD_LOG_WARN
,
5219 "Unexpected '%s' operation attribute in a Create-Job "
5220 "request.", forbidden_attrs
[i
]);
5224 * Create the job object...
5227 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5230 job
->pending_timeout
= 1;
5233 * Save and log the job...
5236 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5237 job
->dest
, job
->username
);
5242 * 'create_local_bg_thread()' - Background thread for creating a local print queue.
5245 static void * /* O - Exit status */
5246 create_local_bg_thread(
5247 cupsd_printer_t
*printer
) /* I - Printer */
5249 cups_file_t
*from
, /* Source file */
5250 *to
; /* Destination file */
5251 char fromppd
[1024], /* Source PPD */
5252 toppd
[1024], /* Destination PPD */
5253 scheme
[32], /* URI scheme */
5254 userpass
[256], /* User:pass */
5255 host
[256], /* Hostname */
5256 resource
[1024], /* Resource path */
5257 line
[1024]; /* Line from PPD */
5258 int port
; /* Port number */
5259 http_encryption_t encryption
; /* Type of encryption to use */
5260 http_t
*http
; /* Connection to printer */
5261 ipp_t
*request
, /* Request to printer */
5262 *response
; /* Response from printer */
5263 ipp_attribute_t
*attr
; /* Attribute in response */
5264 ipp_status_t status
; /* Status code */
5268 * Try connecting to the printer...
5271 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Generating PPD file from \"%s\"...", printer
->name
, printer
->device_uri
);
5273 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
)
5275 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Bad device URI \"%s\".", printer
->name
, printer
->device_uri
);
5279 if (!strcmp(scheme
, "ipps") || port
== 443)
5280 encryption
= HTTP_ENCRYPTION_ALWAYS
;
5282 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
5284 if ((http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
)) == NULL
)
5286 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to connect to %s:%d: %s", printer
->name
, host
, port
, cupsLastErrorString());
5291 * Query the printer for its capabilities...
5294 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Connected to %s:%d, sending Get-Printer-Attributes request...", printer
->name
, host
, port
);
5296 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
5297 ippSetVersion(request
, 2, 0);
5298 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, printer
->device_uri
);
5299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "all");
5301 response
= cupsDoRequest(http
, request
, resource
);
5302 status
= cupsLastError();
5304 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Get-Printer-Attributes returned %s (%s)", printer
->name
, ippErrorString(cupsLastError()), cupsLastErrorString());
5306 if (status
== IPP_STATUS_ERROR_BAD_REQUEST
|| status
== IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
5309 * Try request using IPP/1.1, in case we are talking to an old CUPS server or
5313 ippDelete(response
);
5315 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: Re-sending Get-Printer-Attributes request using IPP/1.1...", printer
->name
);
5317 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
5318 ippSetVersion(request
, 1, 1);
5319 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, printer
->device_uri
);
5320 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "all");
5322 response
= cupsDoRequest(http
, request
, resource
);
5324 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s: IPP/1.1 Get-Printer-Attributes returned %s (%s)", printer
->name
, ippErrorString(cupsLastError()), cupsLastErrorString());
5327 // TODO: Grab printer icon file...
5331 * Write the PPD for the queue...
5334 if (_ppdCreateFromIPP(fromppd
, sizeof(fromppd
), response
))
5336 _cupsRWLockWrite(&printer
->lock
);
5338 if ((!printer
->info
|| !*(printer
->info
)) && (attr
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
)) != NULL
)
5339 cupsdSetString(&printer
->info
, ippGetString(attr
, 0, NULL
));
5341 if ((!printer
->location
|| !*(printer
->location
)) && (attr
= ippFindAttribute(response
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
5342 cupsdSetString(&printer
->location
, ippGetString(attr
, 0, NULL
));
5344 if ((!printer
->geo_location
|| !*(printer
->geo_location
)) && (attr
= ippFindAttribute(response
, "printer-geo-location", IPP_TAG_URI
)) != NULL
)
5345 cupsdSetString(&printer
->geo_location
, ippGetString(attr
, 0, NULL
));
5347 _cupsRWUnlock(&printer
->lock
);
5349 if ((from
= cupsFileOpen(fromppd
, "r")) == NULL
)
5351 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to read generated PPD: %s", printer
->name
, strerror(errno
));
5355 snprintf(toppd
, sizeof(toppd
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
5356 if ((to
= cupsdCreateConfFile(toppd
, ConfigFilePerm
)) == NULL
)
5358 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: Unable to create PPD for printer: %s", printer
->name
, strerror(errno
));
5359 cupsFileClose(from
);
5363 while (cupsFileGets(from
, line
, sizeof(line
)))
5364 cupsFilePrintf(to
, "%s\n", line
);
5366 cupsFileClose(from
);
5367 if (!cupsdCloseCreatedConfFile(to
, toppd
))
5369 printer
->config_time
= time(NULL
);
5370 printer
->state
= IPP_PSTATE_IDLE
;
5371 printer
->accepting
= 1;
5373 cupsdSetPrinterAttrs(printer
);
5375 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
, "Printer \"%s\" is now available.", printer
->name
);
5376 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" is now available.", printer
->name
);
5380 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: PPD creation failed: %s", printer
->name
, cupsLastErrorString());
5387 * 'create_local_printer()' - Create a local (temporary) print queue.
5391 create_local_printer(
5392 cupsd_client_t
*con
) /* I - Client connection */
5394 ipp_attribute_t
*device_uri
, /* device-uri attribute */
5395 *printer_geo_location
, /* printer-geo-location attribute */
5396 *printer_info
, /* printer-info attribute */
5397 *printer_location
, /* printer-location attribute */
5398 *printer_name
; /* printer-name attribute */
5399 cupsd_printer_t
*printer
; /* New printer */
5400 http_status_t status
; /* Policy status */
5401 char name
[128], /* Sanitized printer name */
5402 *nameptr
, /* Pointer into name */
5403 uri
[1024]; /* printer-uri-supported value */
5404 const char *ptr
; /* Pointer into attribute value */
5408 * Require local access to create a local printer...
5411 if (!httpAddrLocalhost(httpGetAddress(con
->http
)))
5413 send_ipp_status(con
, IPP_STATUS_ERROR_FORBIDDEN
, _("Only local users can create a local printer."));
5418 * Check any other policy limits...
5421 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5423 send_http_error(con
, status
, NULL
);
5428 * Grab needed attributes...
5431 if ((printer_name
= ippFindAttribute(con
->request
, "printer-name", IPP_TAG_ZERO
)) == NULL
|| ippGetGroupTag(printer_name
) != IPP_TAG_PRINTER
|| ippGetValueTag(printer_name
) != IPP_TAG_NAME
)
5434 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Missing required attribute \"%s\"."), "printer-name");
5435 else if (ippGetGroupTag(printer_name
) != IPP_TAG_PRINTER
)
5436 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is in the wrong group."), "printer-name");
5438 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is the wrong value type."), "printer-name");
5443 for (nameptr
= name
, ptr
= ippGetString(printer_name
, 0, NULL
); *ptr
&& nameptr
< (name
+ sizeof(name
) - 1); ptr
++)
5446 * Sanitize the printer name...
5449 if (_cups_isalnum(*ptr
))
5451 else if (nameptr
== name
|| nameptr
[-1] != '_')
5457 if ((device_uri
= ippFindAttribute(con
->request
, "device-uri", IPP_TAG_ZERO
)) == NULL
|| ippGetGroupTag(device_uri
) != IPP_TAG_PRINTER
|| ippGetValueTag(device_uri
) != IPP_TAG_URI
)
5460 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Missing required attribute \"%s\"."), "device-uri");
5461 else if (ippGetGroupTag(device_uri
) != IPP_TAG_PRINTER
)
5462 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is in the wrong group."), "device-uri");
5464 send_ipp_status(con
, IPP_STATUS_ERROR_BAD_REQUEST
, _("Attribute \"%s\" is the wrong value type."), "device-uri");
5469 printer_geo_location
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
);
5470 printer_info
= ippFindAttribute(con
->request
, "printer-info", IPP_TAG_TEXT
);
5471 printer_location
= ippFindAttribute(con
->request
, "printer-location", IPP_TAG_TEXT
);
5474 * See if the printer already exists...
5477 if ((printer
= cupsdFindDest(name
)) != NULL
)
5479 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_POSSIBLE
, _("Printer \"%s\" already exists."), name
);
5480 goto add_printer_attributes
;
5484 * Create the printer...
5487 if ((printer
= cupsdAddPrinter(name
)) == NULL
)
5489 send_ipp_status(con
, IPP_STATUS_ERROR_INTERNAL
, _("Unable to create printer."));
5493 printer
->shared
= 0;
5494 printer
->temporary
= 1;
5496 cupsdSetDeviceURI(printer
, ippGetString(device_uri
, 0, NULL
));
5498 if (printer_geo_location
)
5499 cupsdSetString(&printer
->geo_location
, ippGetString(printer_geo_location
, 0, NULL
));
5501 cupsdSetString(&printer
->info
, ippGetString(printer_info
, 0, NULL
));
5502 if (printer_location
)
5503 cupsdSetString(&printer
->location
, ippGetString(printer_location
, 0, NULL
));
5505 cupsdSetPrinterAttrs(printer
);
5508 * Run a background thread to create the PPD...
5511 _cupsThreadCreate((_cups_thread_func_t
)create_local_bg_thread
, printer
);
5514 * Return printer attributes...
5517 send_ipp_status(con
, IPP_STATUS_OK
, _("Local printer created."));
5519 add_printer_attributes
:
5521 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
5522 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", printer
->state
);
5523 add_printer_state_reasons(con
, printer
);
5525 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), httpIsEncrypted(con
->http
) ? "ipps" : "ipp", NULL
, con
->clientname
, con
->clientport
, "/printers/%s", printer
->name
);
5526 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported", NULL
, uri
);
5531 * 'create_requested_array()' - Create an array for the requested-attributes.
5534 static cups_array_t
* /* O - Array of attributes or NULL */
5535 create_requested_array(ipp_t
*request
) /* I - IPP request */
5537 cups_array_t
*ra
; /* Requested attributes array */
5541 * Create the array for standard attributes...
5544 ra
= ippCreateRequestedArray(request
);
5547 * Add CUPS defaults as needed...
5550 if (cupsArrayFind(ra
, "printer-defaults"))
5553 * Include user-set defaults...
5556 char *name
; /* Option name */
5558 cupsArrayRemove(ra
, "printer-defaults");
5560 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5562 name
= (char *)cupsArrayNext(CommonDefaults
))
5563 if (!cupsArrayFind(ra
, name
))
5564 cupsArrayAdd(ra
, name
);
5572 * 'create_subscriptions()' - Create one or more notification subscriptions.
5576 create_subscriptions(
5577 cupsd_client_t
*con
, /* I - Client connection */
5578 ipp_attribute_t
*uri
) /* I - Printer URI */
5580 http_status_t status
; /* Policy status */
5581 int i
; /* Looping var */
5582 ipp_attribute_t
*attr
; /* Current attribute */
5583 cups_ptype_t dtype
; /* Destination type (printer/class) */
5584 char scheme
[HTTP_MAX_URI
],
5585 /* Scheme portion of URI */
5586 userpass
[HTTP_MAX_URI
],
5587 /* Username portion of URI */
5589 /* Host portion of URI */
5590 resource
[HTTP_MAX_URI
];
5591 /* Resource portion of URI */
5592 int port
; /* Port portion of URI */
5593 cupsd_printer_t
*printer
; /* Printer/class */
5594 cupsd_job_t
*job
; /* Job */
5595 int jobid
; /* Job ID */
5596 cupsd_subscription_t
*sub
; /* Subscription object */
5597 const char *username
, /* requesting-user-name or
5598 authenticated username */
5599 *recipient
, /* notify-recipient-uri */
5600 *pullmethod
; /* notify-pull-method */
5601 ipp_attribute_t
*user_data
; /* notify-user-data */
5602 int interval
, /* notify-time-interval */
5603 lease
; /* notify-lease-duration */
5604 unsigned mask
; /* notify-events */
5605 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5606 *notify_lease
; /* notify-lease-duration(-default) */
5610 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5612 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5613 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5614 attr
->value_tag
, attr
->name
);
5616 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5621 * Is the destination valid?
5624 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5626 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5627 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5628 sizeof(host
), &port
, resource
, sizeof(resource
));
5630 if (!strcmp(resource
, "/"))
5632 dtype
= (cups_ptype_t
)0;
5635 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5637 dtype
= (cups_ptype_t
)0;
5640 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5642 dtype
= CUPS_PRINTER_CLASS
;
5645 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5651 send_ipp_status(con
, IPP_NOT_FOUND
,
5652 _("The printer or class does not exist."));
5662 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5665 send_http_error(con
, status
, printer
);
5669 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5671 send_http_error(con
, status
, NULL
);
5676 * Get the user that is requesting the subscription...
5679 username
= get_username(con
);
5682 * Find the first subscription group attribute; return if we have
5686 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5687 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5692 send_ipp_status(con
, IPP_BAD_REQUEST
,
5693 _("No subscription attributes in request."));
5698 * Process the subscription attributes in the request...
5701 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5709 lease
= DefaultLeaseDuration
;
5711 mask
= CUPSD_EVENT_NONE
;
5715 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5717 notify_lease
= ippFindAttribute(printer
->attrs
,
5718 "notify-lease-duration-default",
5722 lease
= notify_lease
->values
[0].integer
;
5726 notify_events
= NULL
;
5727 notify_lease
= NULL
;
5730 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5732 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5733 attr
->value_tag
== IPP_TAG_URI
)
5736 * Validate the recipient scheme against the ServerBin/notifier
5740 char notifier
[1024]; /* Notifier filename */
5743 recipient
= attr
->values
[0].string
.text
;
5745 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5746 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5747 host
, sizeof(host
), &port
,
5748 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5750 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5751 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5752 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5753 "notify-status-code", IPP_URI_SCHEME
);
5757 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5759 if (access(notifier
, X_OK
))
5761 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5762 _("notify-recipient-uri URI \"%s\" uses unknown "
5763 "scheme."), recipient
);
5764 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5765 "notify-status-code", IPP_URI_SCHEME
);
5769 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5771 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5772 _("notify-recipient-uri URI \"%s\" is already used."),
5774 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5775 "notify-status-code", IPP_ATTRIBUTES
);
5779 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5780 attr
->value_tag
== IPP_TAG_KEYWORD
)
5782 pullmethod
= attr
->values
[0].string
.text
;
5784 if (strcmp(pullmethod
, "ippget"))
5786 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5787 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5788 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5789 "notify-status-code", IPP_ATTRIBUTES
);
5793 else if (!strcmp(attr
->name
, "notify-charset") &&
5794 attr
->value_tag
== IPP_TAG_CHARSET
&&
5795 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5796 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5798 send_ipp_status(con
, IPP_CHARSET
,
5799 _("Character set \"%s\" not supported."),
5800 attr
->values
[0].string
.text
);
5803 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5804 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5805 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5807 send_ipp_status(con
, IPP_CHARSET
,
5808 _("Language \"%s\" not supported."),
5809 attr
->values
[0].string
.text
);
5812 else if (!strcmp(attr
->name
, "notify-user-data") &&
5813 attr
->value_tag
== IPP_TAG_STRING
)
5815 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5817 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5818 _("The notify-user-data value is too large "
5819 "(%d > 63 octets)."),
5820 attr
->values
[0].unknown
.length
);
5826 else if (!strcmp(attr
->name
, "notify-events") &&
5827 attr
->value_tag
== IPP_TAG_KEYWORD
)
5828 notify_events
= attr
;
5829 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5830 attr
->value_tag
== IPP_TAG_INTEGER
)
5831 lease
= attr
->values
[0].integer
;
5832 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5833 attr
->value_tag
== IPP_TAG_INTEGER
)
5834 interval
= attr
->values
[0].integer
;
5835 else if (!strcmp(attr
->name
, "notify-job-id") &&
5836 attr
->value_tag
== IPP_TAG_INTEGER
)
5837 jobid
= attr
->values
[0].integer
;
5844 for (i
= 0; i
< notify_events
->num_values
; i
++)
5845 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5850 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5853 if (!strncmp(recipient
, "mailto:", 7) && user_data
)
5855 char temp
[64]; /* Temporary string */
5857 memcpy(temp
, user_data
->values
[0].unknown
.data
, user_data
->values
[0].unknown
.length
);
5858 temp
[user_data
->values
[0].unknown
.length
] = '\0';
5860 if (httpSeparateURI(HTTP_URI_CODING_ALL
, temp
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_OK
)
5862 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad notify-user-data \"%s\"."), temp
);
5863 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
, "notify-status-code", IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
);
5870 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5871 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5872 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5874 if (!recipient
&& !pullmethod
)
5877 if (mask
== CUPSD_EVENT_NONE
)
5880 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5882 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5885 send_ipp_status(con
, IPP_BAD_REQUEST
,
5886 _("notify-events not specified."));
5891 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5893 cupsdLogMessage(CUPSD_LOG_INFO
,
5894 "create_subscriptions: Limiting notify-lease-duration to "
5897 lease
= MaxLeaseDuration
;
5902 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5904 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5912 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5914 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5915 _("There are too many subscriptions."));
5920 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5923 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5924 "Added subscription #%d for printer \"%s\".",
5925 sub
->id
, printer
->name
);
5927 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5930 sub
->interval
= interval
;
5932 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5934 cupsdSetString(&sub
->owner
, username
);
5938 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5939 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5940 (size_t)sub
->user_data_len
);
5943 ippAddSeparator(con
->response
);
5944 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5945 "notify-subscription-id", sub
->id
);
5947 con
->response
->request
.status
.status_code
= IPP_OK
;
5953 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5958 * 'delete_printer()' - Remove a printer or class from the system.
5962 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5963 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5965 http_status_t status
; /* Policy status */
5966 cups_ptype_t dtype
; /* Destination type (printer/class) */
5967 cupsd_printer_t
*printer
; /* Printer/class */
5968 char filename
[1024]; /* Script/PPD filename */
5969 int temporary
; /* Temporary queue? */
5972 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5973 con
->number
, uri
->values
[0].string
.text
);
5976 * Do we have a valid URI?
5979 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5985 send_ipp_status(con
, IPP_NOT_FOUND
,
5986 _("The printer or class does not exist."));
5994 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5996 send_http_error(con
, status
, NULL
);
6001 * Remove old jobs...
6004 cupsdCancelJobs(printer
->name
, NULL
, 1);
6007 * Remove old subscriptions and send a "deleted printer" event...
6010 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6011 "%s \"%s\" deleted by \"%s\".",
6012 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6013 printer
->name
, get_username(con
));
6015 cupsdExpireSubscriptions(printer
, NULL
);
6018 * Remove any old PPD or script files...
6021 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6024 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
6028 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
6031 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
6035 * Unregister color profiles...
6038 cupsdUnregisterColor(printer
);
6040 temporary
= printer
->temporary
;
6042 if (dtype
& CUPS_PRINTER_CLASS
)
6044 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6045 printer
->name
, get_username(con
));
6047 cupsdDeletePrinter(printer
, 0);
6049 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6053 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6054 printer
->name
, get_username(con
));
6056 if (cupsdDeletePrinter(printer
, 0) && !temporary
)
6057 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6060 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6064 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6067 * Return with no errors...
6070 con
->response
->request
.status
.status_code
= IPP_OK
;
6075 * 'get_default()' - Get the default destination.
6079 get_default(cupsd_client_t
*con
) /* I - Client connection */
6081 http_status_t status
; /* Policy status */
6082 cups_array_t
*ra
; /* Requested attributes array */
6085 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
6091 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6093 send_http_error(con
, status
, NULL
);
6099 ra
= create_requested_array(con
->request
);
6101 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6103 cupsArrayDelete(ra
);
6105 con
->response
->request
.status
.status_code
= IPP_OK
;
6108 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
6113 * 'get_devices()' - Get the list of available devices on the local system.
6117 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6119 http_status_t status
; /* Policy status */
6120 ipp_attribute_t
*limit
, /* limit attribute */
6121 *timeout
, /* timeout attribute */
6122 *requested
, /* requested-attributes attribute */
6123 *exclude
, /* exclude-schemes attribute */
6124 *include
; /* include-schemes attribute */
6125 char command
[1024], /* cups-deviced command */
6126 options
[2048], /* Options to pass to command */
6128 /* String for requested attributes */
6130 /* String for excluded schemes */
6132 /* String for included schemes */
6135 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
6141 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6143 send_http_error(con
, status
, NULL
);
6148 * Run cups-deviced command with the given options...
6151 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6152 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6153 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6155 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6156 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
6159 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6161 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6164 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6166 exclude_str
[0] = '\0';
6169 url_encode_attr(include
, include_str
, sizeof(include_str
));
6171 include_str
[0] = '\0';
6173 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6174 snprintf(options
, sizeof(options
),
6175 "%d+%d+%d+%d+%s%s%s%s%s",
6176 con
->request
->request
.op
.request_id
,
6177 limit
? limit
->values
[0].integer
: 0,
6178 timeout
? timeout
->values
[0].integer
: 15,
6181 exclude_str
[0] ? "%20" : "", exclude_str
,
6182 include_str
[0] ? "%20" : "", include_str
);
6184 if (cupsdSendCommand(con
, command
, options
, 1))
6187 * Command started successfully, don't send an IPP response here...
6190 ippDelete(con
->response
);
6191 con
->response
= NULL
;
6196 * Command failed, return "internal error" so the user knows something
6200 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6201 _("cups-deviced failed to execute."));
6207 * 'get_document()' - Get a copy of a job file.
6211 get_document(cupsd_client_t
*con
, /* I - Client connection */
6212 ipp_attribute_t
*uri
) /* I - Job URI */
6214 http_status_t status
; /* Policy status */
6215 ipp_attribute_t
*attr
; /* Current attribute */
6216 int jobid
; /* Job ID */
6217 int docnum
; /* Document number */
6218 cupsd_job_t
*job
; /* Current job */
6219 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6220 username
[HTTP_MAX_URI
], /* Username portion of URI */
6221 host
[HTTP_MAX_URI
], /* Host portion of URI */
6222 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6223 int port
; /* Port portion of URI */
6224 char filename
[1024], /* Filename for document */
6225 format
[1024]; /* Format for document */
6228 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6229 con
->number
, uri
->values
[0].string
.text
);
6232 * See if we have a job URI or a printer URI...
6235 if (!strcmp(uri
->name
, "printer-uri"))
6238 * Got a printer URI; see if we also have a job-id attribute...
6241 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6242 IPP_TAG_INTEGER
)) == NULL
)
6244 send_ipp_status(con
, IPP_BAD_REQUEST
,
6245 _("Got a printer-uri attribute but no job-id."));
6249 jobid
= attr
->values
[0].integer
;
6254 * Got a job URI; parse it to get the job ID...
6257 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6258 sizeof(scheme
), username
, sizeof(username
), host
,
6259 sizeof(host
), &port
, resource
, sizeof(resource
));
6261 if (strncmp(resource
, "/jobs/", 6))
6267 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6268 uri
->values
[0].string
.text
);
6272 jobid
= atoi(resource
+ 6);
6276 * See if the job exists...
6279 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6282 * Nope - return a "not found" error...
6285 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6293 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
6294 job
->username
)) != HTTP_OK
)
6296 send_http_error(con
, status
, NULL
);
6301 * Get the document number...
6304 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6305 IPP_TAG_INTEGER
)) == NULL
)
6307 send_ipp_status(con
, IPP_BAD_REQUEST
,
6308 _("Missing document-number attribute."));
6312 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6313 attr
->num_values
> 1)
6315 send_ipp_status(con
, IPP_NOT_FOUND
,
6316 _("Document #%d does not exist in job #%d."), docnum
,
6321 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6323 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6325 cupsdLogMessage(CUPSD_LOG_ERROR
,
6326 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6328 send_ipp_status(con
, IPP_NOT_FOUND
,
6329 _("Unable to open document #%d in job #%d."), docnum
,
6334 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6338 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6339 job
->filetypes
[docnum
- 1]->type
);
6341 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6343 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6345 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6346 IPP_TAG_NAME
)) != NULL
)
6347 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6348 NULL
, attr
->values
[0].string
.text
);
6353 * 'get_job_attrs()' - Get job attributes.
6357 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6358 ipp_attribute_t
*uri
) /* I - Job URI */
6360 http_status_t status
; /* Policy status */
6361 ipp_attribute_t
*attr
; /* Current attribute */
6362 int jobid
; /* Job ID */
6363 cupsd_job_t
*job
; /* Current job */
6364 cupsd_printer_t
*printer
; /* Current printer */
6365 cupsd_policy_t
*policy
; /* Current security policy */
6366 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6367 username
[HTTP_MAX_URI
], /* Username portion of URI */
6368 host
[HTTP_MAX_URI
], /* Host portion of URI */
6369 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6370 int port
; /* Port portion of URI */
6371 cups_array_t
*ra
, /* Requested attributes array */
6372 *exclude
; /* Private attributes array */
6375 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6376 con
->number
, uri
->values
[0].string
.text
);
6379 * See if we have a job URI or a printer URI...
6382 if (!strcmp(uri
->name
, "printer-uri"))
6385 * Got a printer URI; see if we also have a job-id attribute...
6388 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6389 IPP_TAG_INTEGER
)) == NULL
)
6391 send_ipp_status(con
, IPP_BAD_REQUEST
,
6392 _("Got a printer-uri attribute but no job-id."));
6396 jobid
= attr
->values
[0].integer
;
6401 * Got a job URI; parse it to get the job ID...
6404 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6405 sizeof(scheme
), username
, sizeof(username
), host
,
6406 sizeof(host
), &port
, resource
, sizeof(resource
));
6408 if (strncmp(resource
, "/jobs/", 6))
6414 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6415 uri
->values
[0].string
.text
);
6419 jobid
= atoi(resource
+ 6);
6423 * See if the job exists...
6426 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6429 * Nope - return a "not found" error...
6432 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6440 if ((printer
= job
->printer
) == NULL
)
6441 printer
= cupsdFindDest(job
->dest
);
6444 policy
= printer
->op_policy_ptr
;
6446 policy
= DefaultPolicyPtr
;
6448 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6450 send_http_error(con
, status
, NULL
);
6454 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6457 * Copy attributes...
6462 ra
= create_requested_array(con
->request
);
6463 copy_job_attrs(con
, job
, ra
, exclude
);
6464 cupsArrayDelete(ra
);
6466 con
->response
->request
.status
.status_code
= IPP_OK
;
6471 * 'get_jobs()' - Get a list of jobs for the specified printer.
6475 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6476 ipp_attribute_t
*uri
) /* I - Printer URI */
6478 http_status_t status
; /* Policy status */
6479 ipp_attribute_t
*attr
; /* Current attribute */
6480 const char *dest
; /* Destination */
6481 cups_ptype_t dtype
; /* Destination type (printer/class) */
6482 cups_ptype_t dmask
; /* Destination type mask */
6483 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6484 username
[HTTP_MAX_URI
], /* Username portion of URI */
6485 host
[HTTP_MAX_URI
], /* Host portion of URI */
6486 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6487 int port
; /* Port portion of URI */
6488 int job_comparison
; /* Job comparison */
6489 ipp_jstate_t job_state
; /* job-state value */
6490 int first_job_id
= 1, /* First job ID */
6491 first_index
= 1, /* First index */
6492 limit
= 0, /* Maximum number of jobs to return */
6493 count
, /* Number of jobs that match */
6494 need_load_job
= 0; /* Do we need to load the job? */
6495 const char *job_attr
; /* Job attribute requested */
6496 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6497 cupsd_job_t
*job
; /* Current job pointer */
6498 cupsd_printer_t
*printer
; /* Printer */
6499 cups_array_t
*list
; /* Which job list... */
6500 int delete_list
= 0; /* Delete the list afterwards? */
6501 cups_array_t
*ra
, /* Requested attributes array */
6502 *exclude
; /* Private attributes array */
6503 cupsd_policy_t
*policy
; /* Current policy */
6506 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6507 uri
->values
[0].string
.text
);
6510 * Is the destination valid?
6513 if (strcmp(uri
->name
, "printer-uri"))
6515 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6519 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6520 sizeof(scheme
), username
, sizeof(username
), host
,
6521 sizeof(host
), &port
, resource
, sizeof(resource
));
6523 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6526 dtype
= (cups_ptype_t
)0;
6527 dmask
= (cups_ptype_t
)0;
6530 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6533 dtype
= (cups_ptype_t
)0;
6534 dmask
= CUPS_PRINTER_CLASS
;
6537 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6540 dtype
= CUPS_PRINTER_CLASS
;
6541 dmask
= CUPS_PRINTER_CLASS
;
6544 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6551 send_ipp_status(con
, IPP_NOT_FOUND
,
6552 _("The printer or class does not exist."));
6557 dtype
&= CUPS_PRINTER_CLASS
;
6558 dmask
= CUPS_PRINTER_CLASS
;
6566 policy
= printer
->op_policy_ptr
;
6568 policy
= DefaultPolicyPtr
;
6570 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6572 send_http_error(con
, status
, NULL
);
6576 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6579 * See if the "which-jobs" attribute have been specified...
6582 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6583 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6585 send_ipp_status(con
, IPP_CONFLICT
,
6586 _("The %s attribute cannot be provided with job-ids."),
6590 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6592 job_comparison
= -1;
6593 job_state
= IPP_JOB_STOPPED
;
6596 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6599 job_state
= IPP_JOB_CANCELED
;
6600 list
= cupsdGetCompletedJobs(printer
);
6603 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6606 job_state
= IPP_JOB_ABORTED
;
6607 list
= cupsdGetCompletedJobs(printer
);
6610 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6613 job_state
= IPP_JOB_PENDING
;
6616 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6619 job_state
= IPP_JOB_CANCELED
;
6620 list
= cupsdGetCompletedJobs(printer
);
6623 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6626 job_state
= IPP_JOB_PENDING
;
6629 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6632 job_state
= IPP_JOB_HELD
;
6635 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6638 job_state
= IPP_JOB_PROCESSING
;
6639 list
= PrintingJobs
;
6641 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6644 job_state
= IPP_JOB_STOPPED
;
6649 send_ipp_status(con
, IPP_ATTRIBUTES
,
6650 _("The which-jobs value \"%s\" is not supported."),
6651 attr
->values
[0].string
.text
);
6652 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6653 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6658 * See if they want to limit the number of jobs reported...
6661 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6665 send_ipp_status(con
, IPP_CONFLICT
,
6666 _("The %s attribute cannot be provided with job-ids."),
6671 limit
= attr
->values
[0].integer
;
6674 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6678 send_ipp_status(con
, IPP_CONFLICT
,
6679 _("The %s attribute cannot be provided with job-ids."),
6684 first_index
= attr
->values
[0].integer
;
6686 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6690 send_ipp_status(con
, IPP_CONFLICT
,
6691 _("The %s attribute cannot be provided with job-ids."),
6696 first_job_id
= attr
->values
[0].integer
;
6700 * See if we only want to see jobs for a specific user...
6703 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6705 send_ipp_status(con
, IPP_CONFLICT
,
6706 _("The %s attribute cannot be provided with job-ids."),
6710 else if (attr
&& attr
->values
[0].boolean
)
6711 strlcpy(username
, get_username(con
), sizeof(username
));
6715 ra
= create_requested_array(con
->request
);
6716 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6717 if (strcmp(job_attr
, "job-id") &&
6718 strcmp(job_attr
, "job-k-octets") &&
6719 strcmp(job_attr
, "job-media-progress") &&
6720 strcmp(job_attr
, "job-more-info") &&
6721 strcmp(job_attr
, "job-name") &&
6722 strcmp(job_attr
, "job-originating-user-name") &&
6723 strcmp(job_attr
, "job-preserved") &&
6724 strcmp(job_attr
, "job-printer-up-time") &&
6725 strcmp(job_attr
, "job-printer-uri") &&
6726 strcmp(job_attr
, "job-state") &&
6727 strcmp(job_attr
, "job-state-reasons") &&
6728 strcmp(job_attr
, "job-uri") &&
6729 strcmp(job_attr
, "time-at-completed") &&
6730 strcmp(job_attr
, "time-at-creation") &&
6731 strcmp(job_attr
, "number-of-documents"))
6737 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6740 * Limit expensive Get-Jobs for job history to 500 jobs...
6743 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6746 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6750 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6754 * OK, build a list of jobs for this printer...
6759 int i
; /* Looping var */
6761 for (i
= 0; i
< job_ids
->num_values
; i
++)
6763 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6767 if (i
< job_ids
->num_values
)
6769 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6770 job_ids
->values
[i
].integer
);
6774 for (i
= 0; i
< job_ids
->num_values
; i
++)
6776 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6778 if (need_load_job
&& !job
->attrs
)
6784 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6790 ippAddSeparator(con
->response
);
6792 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6793 job
->printer
->op_policy_ptr
:
6794 policy
, con
, job
->printer
,
6797 copy_job_attrs(con
, job
, ra
, exclude
);
6802 if (first_index
> 1)
6803 job
= (cupsd_job_t
*)cupsArrayIndex(list
, first_index
- 1);
6805 job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6807 for (count
= 0; (limit
<= 0 || count
< limit
) && job
; job
= (cupsd_job_t
*)cupsArrayNext(list
))
6810 * Filter out jobs that don't match...
6813 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6814 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6815 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6816 job
->username
, job
->state_value
, job
->attrs
);
6818 if (!job
->dest
|| !job
->username
)
6821 if (!job
->dest
|| !job
->username
)
6824 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6825 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6827 if ((job
->dtype
& dmask
) != dtype
&&
6828 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6831 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6832 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6833 (job_comparison
> 0 && job
->state_value
< job_state
))
6836 if (job
->id
< first_job_id
)
6839 if (need_load_job
&& !job
->attrs
)
6845 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6850 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6854 ippAddSeparator(con
->response
);
6858 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6859 job
->printer
->op_policy_ptr
:
6860 policy
, con
, job
->printer
,
6863 copy_job_attrs(con
, job
, ra
, exclude
);
6866 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6869 cupsArrayDelete(ra
);
6872 cupsArrayDelete(list
);
6874 con
->response
->request
.status
.status_code
= IPP_OK
;
6879 * 'get_notifications()' - Get events for a subscription.
6883 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6885 int i
, j
; /* Looping vars */
6886 http_status_t status
; /* Policy status */
6887 cupsd_subscription_t
*sub
; /* Subscription */
6888 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6889 *sequences
; /* notify-sequence-numbers */
6890 int min_seq
; /* Minimum sequence number */
6891 int interval
; /* Poll interval */
6894 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6898 * Get subscription attributes...
6901 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6903 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6908 send_ipp_status(con
, IPP_BAD_REQUEST
,
6909 _("Missing notify-subscription-ids attribute."));
6914 * Are the subscription IDs valid?
6917 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6919 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6922 * Bad subscription ID...
6925 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6926 ids
->values
[i
].integer
);
6934 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6936 con
, sub
->owner
)) != HTTP_OK
)
6938 send_http_error(con
, status
, sub
->dest
);
6943 * Check the subscription type and update the interval accordingly.
6946 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6949 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6951 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6957 * Tell the client to poll again in N seconds...
6961 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6962 "notify-get-interval", interval
);
6964 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6965 "printer-up-time", time(NULL
));
6968 * Copy the subscription event attributes to the response.
6971 con
->response
->request
.status
.status_code
=
6972 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6974 for (i
= 0; i
< ids
->num_values
; i
++)
6977 * Get the subscription and sequence number...
6980 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6982 if (sequences
&& i
< sequences
->num_values
)
6983 min_seq
= sequences
->values
[i
].integer
;
6988 * If we don't have any new events, nothing to do here...
6991 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
6995 * Otherwise copy all of the new events...
6998 if (sub
->first_event_id
> min_seq
)
7001 j
= min_seq
- sub
->first_event_id
;
7003 for (; j
< cupsArrayCount(sub
->events
); j
++)
7005 ippAddSeparator(con
->response
);
7007 copy_attrs(con
->response
,
7008 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
7009 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
7016 * 'get_ppd()' - Get a named PPD from the local system.
7020 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7021 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7023 http_status_t status
; /* Policy status */
7024 cupsd_printer_t
*dest
; /* Destination */
7025 cups_ptype_t dtype
; /* Destination type */
7028 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7029 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7031 if (!strcmp(ippGetName(uri
), "ppd-name"))
7034 * Return a PPD file from cups-driverd...
7037 const char *ppd_name
= ippGetString(uri
, 0, NULL
);
7038 /* ppd-name value */
7039 char command
[1024], /* cups-driverd command */
7040 options
[1024], /* Options to pass to command */
7041 oppd_name
[1024]; /* Escaped ppd-name */
7047 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7049 send_http_error(con
, status
, NULL
);
7054 * Check ppd-name value...
7057 if (strstr(ppd_name
, "../"))
7059 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
7064 * Run cups-driverd command with the given options...
7067 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7068 url_encode_string(ppd_name
, oppd_name
, sizeof(oppd_name
));
7069 snprintf(options
, sizeof(options
), "get+%d+%s", ippGetRequestId(con
->request
), oppd_name
);
7071 if (cupsdSendCommand(con
, command
, options
, 0))
7074 * Command started successfully, don't send an IPP response here...
7077 ippDelete(con
->response
);
7078 con
->response
= NULL
;
7083 * Command failed, return "internal error" so the user knows something
7087 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("cups-driverd failed to execute."));
7090 else if (!strcmp(ippGetName(uri
), "printer-uri") && cupsdValidateDest(ippGetString(uri
, 0, NULL
), &dtype
, &dest
))
7092 int i
; /* Looping var */
7093 char filename
[1024]; /* PPD filename */
7099 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7101 send_http_error(con
, status
, dest
);
7106 * See if we need the PPD for a class or remote printer...
7109 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->name
);
7111 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7113 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
7114 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->uri
);
7117 else if (dtype
& CUPS_PRINTER_CLASS
)
7119 for (i
= 0; i
< dest
->num_printers
; i
++)
7120 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
7122 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->printers
[i
]->name
);
7124 if (!access(filename
, 0))
7128 if (i
< dest
->num_printers
)
7129 dest
= dest
->printers
[i
];
7132 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
7133 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->printers
[0]->uri
);
7139 * Found the printer with the PPD file, now see if there is one...
7142 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7144 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be opened: %s"), ippGetString(uri
, 0, NULL
), strerror(errno
));
7148 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7152 ippSetStatusCode(con
->response
, IPP_STATUS_OK
);
7155 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be found."), ippGetString(uri
, 0, NULL
));
7160 * 'get_ppds()' - Get the list of PPD files on the local system.
7164 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7166 http_status_t status
; /* Policy status */
7167 ipp_attribute_t
*limit
, /* Limit attribute */
7168 *device
, /* ppd-device-id attribute */
7169 *language
, /* ppd-natural-language attribute */
7170 *make
, /* ppd-make attribute */
7171 *model
, /* ppd-make-and-model attribute */
7172 *model_number
, /* ppd-model-number attribute */
7173 *product
, /* ppd-product attribute */
7174 *psversion
, /* ppd-psverion attribute */
7175 *type
, /* ppd-type attribute */
7176 *requested
, /* requested-attributes attribute */
7177 *exclude
, /* exclude-schemes attribute */
7178 *include
; /* include-schemes attribute */
7179 char command
[1024], /* cups-driverd command */
7180 options
[4096], /* Options to pass to command */
7181 device_str
[256],/* Escaped ppd-device-id string */
7183 /* Escaped ppd-natural-language */
7184 make_str
[256], /* Escaped ppd-make string */
7185 model_str
[256], /* Escaped ppd-make-and-model string */
7186 model_number_str
[256],
7187 /* ppd-model-number string */
7189 /* Escaped ppd-product string */
7191 /* Escaped ppd-psversion string */
7192 type_str
[256], /* Escaped ppd-type string */
7194 /* String for requested attributes */
7196 /* String for excluded schemes */
7198 /* String for included schemes */
7201 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
7207 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7209 send_http_error(con
, status
, NULL
);
7214 * Run cups-driverd command with the given options...
7217 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7218 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7219 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7221 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7222 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7224 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7226 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7227 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7228 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7229 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7231 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
7233 include
= ippFindAttribute(con
->request
, "include-schemes",
7237 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7239 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7242 url_encode_attr(device
, device_str
, sizeof(device_str
));
7244 device_str
[0] = '\0';
7247 url_encode_attr(language
, language_str
, sizeof(language_str
));
7249 language_str
[0] = '\0';
7252 url_encode_attr(make
, make_str
, sizeof(make_str
));
7257 url_encode_attr(model
, model_str
, sizeof(model_str
));
7259 model_str
[0] = '\0';
7262 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7263 model_number
->values
[0].integer
);
7265 model_number_str
[0] = '\0';
7268 url_encode_attr(product
, product_str
, sizeof(product_str
));
7270 product_str
[0] = '\0';
7273 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7275 psversion_str
[0] = '\0';
7278 url_encode_attr(type
, type_str
, sizeof(type_str
));
7283 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
7285 exclude_str
[0] = '\0';
7288 url_encode_attr(include
, include_str
, sizeof(include_str
));
7290 include_str
[0] = '\0';
7292 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7293 snprintf(options
, sizeof(options
),
7294 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7295 con
->request
->request
.op
.request_id
,
7296 limit
? limit
->values
[0].integer
: 0,
7298 device
? "%20" : "", device_str
,
7299 language
? "%20" : "", language_str
,
7300 make
? "%20" : "", make_str
,
7301 model
? "%20" : "", model_str
,
7302 model_number
? "%20" : "", model_number_str
,
7303 product
? "%20" : "", product_str
,
7304 psversion
? "%20" : "", psversion_str
,
7305 type
? "%20" : "", type_str
,
7306 exclude_str
[0] ? "%20" : "", exclude_str
,
7307 include_str
[0] ? "%20" : "", include_str
);
7309 if (cupsdSendCommand(con
, command
, options
, 0))
7312 * Command started successfully, don't send an IPP response here...
7315 ippDelete(con
->response
);
7316 con
->response
= NULL
;
7321 * Command failed, return "internal error" so the user knows something
7325 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7326 _("cups-driverd failed to execute."));
7332 * 'get_printer_attrs()' - Get printer attributes.
7336 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7337 ipp_attribute_t
*uri
) /* I - Printer URI */
7339 http_status_t status
; /* Policy status */
7340 cups_ptype_t dtype
; /* Destination type (printer/class) */
7341 cupsd_printer_t
*printer
; /* Printer/class */
7342 cups_array_t
*ra
; /* Requested attributes array */
7345 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7346 con
->number
, uri
->values
[0].string
.text
);
7349 * Is the destination valid?
7352 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7358 send_ipp_status(con
, IPP_NOT_FOUND
,
7359 _("The printer or class does not exist."));
7367 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7369 send_http_error(con
, status
, printer
);
7374 * Send the attributes...
7377 ra
= create_requested_array(con
->request
);
7379 copy_printer_attrs(con
, printer
, ra
);
7381 cupsArrayDelete(ra
);
7383 con
->response
->request
.status
.status_code
= IPP_OK
;
7388 * 'get_printer_supported()' - Get printer supported values.
7392 get_printer_supported(
7393 cupsd_client_t
*con
, /* I - Client connection */
7394 ipp_attribute_t
*uri
) /* I - Printer URI */
7396 http_status_t status
; /* Policy status */
7397 cups_ptype_t dtype
; /* Destination type (printer/class) */
7398 cupsd_printer_t
*printer
; /* Printer/class */
7401 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7402 con
->number
, uri
->values
[0].string
.text
);
7405 * Is the destination valid?
7408 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7414 send_ipp_status(con
, IPP_NOT_FOUND
,
7415 _("The printer or class does not exist."));
7423 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7425 send_http_error(con
, status
, printer
);
7430 * Return a list of attributes that can be set via Set-Printer-Attributes.
7433 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7434 "printer-geo-location", 0);
7435 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7437 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7438 "printer-location", 0);
7439 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7440 "printer-organization", 0);
7441 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7442 "printer-organizational-unit", 0);
7444 con
->response
->request
.status
.status_code
= IPP_OK
;
7449 * 'get_printers()' - Get a list of printers or classes.
7453 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7454 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7456 http_status_t status
; /* Policy status */
7457 ipp_attribute_t
*attr
; /* Current attribute */
7458 int limit
; /* Max number of printers to return */
7459 int count
; /* Number of printers that match */
7460 int printer_id
; /* Printer we are interested in */
7461 cupsd_printer_t
*printer
; /* Current printer pointer */
7462 cups_ptype_t printer_type
, /* printer-type attribute */
7463 printer_mask
; /* printer-type-mask attribute */
7464 char *location
; /* Location string */
7465 const char *username
; /* Current user */
7466 char *first_printer_name
; /* first-printer-name attribute */
7467 cups_array_t
*ra
; /* Requested attributes array */
7468 int local
; /* Local connection? */
7471 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7478 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7480 send_http_error(con
, status
, NULL
);
7485 * Check for printers...
7488 if (!Printers
|| !cupsArrayCount(Printers
))
7490 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7495 * See if they want to limit the number of printers reported...
7498 if ((attr
= ippFindAttribute(con
->request
, "limit",
7499 IPP_TAG_INTEGER
)) != NULL
)
7500 limit
= attr
->values
[0].integer
;
7504 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7505 IPP_TAG_NAME
)) != NULL
)
7506 first_printer_name
= attr
->values
[0].string
.text
;
7508 first_printer_name
= NULL
;
7511 * Support filtering...
7514 if ((attr
= ippFindAttribute(con
->request
, "printer-id", IPP_TAG_INTEGER
)) != NULL
)
7516 if ((printer_id
= ippGetInteger(attr
, 0)) <= 0)
7518 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Bad \"printer-id\" value %d."), printer_id
);
7525 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7526 IPP_TAG_ENUM
)) != NULL
)
7527 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7529 printer_type
= (cups_ptype_t
)0;
7531 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7532 IPP_TAG_ENUM
)) != NULL
)
7533 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7535 printer_mask
= (cups_ptype_t
)0;
7537 local
= httpAddrLocalhost(&(con
->clientaddr
));
7539 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7540 IPP_TAG_TEXT
)) != NULL
)
7541 location
= attr
->values
[0].string
.text
;
7545 if (con
->username
[0])
7546 username
= con
->username
;
7547 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7548 IPP_TAG_NAME
)) != NULL
)
7549 username
= attr
->values
[0].string
.text
;
7553 ra
= create_requested_array(con
->request
);
7556 * OK, build a list of printers for this printer...
7559 if (first_printer_name
)
7561 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7562 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7565 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7568 count
< limit
&& printer
;
7569 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7571 if (!local
&& !printer
->shared
)
7574 if (printer_id
&& printer
->printer_id
!= printer_id
)
7577 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7578 (printer
->type
& printer_mask
) == printer_type
&&
7580 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7583 * If a username is specified, see if it is allowed or denied
7587 if (cupsArrayCount(printer
->users
) && username
&&
7588 !user_allowed(printer
, username
))
7592 * Add the group separator as needed...
7596 ippAddSeparator(con
->response
);
7601 * Send the attributes...
7604 copy_printer_attrs(con
, printer
, ra
);
7608 cupsArrayDelete(ra
);
7610 con
->response
->request
.status
.status_code
= IPP_OK
;
7615 * 'get_subscription_attrs()' - Get subscription attributes.
7619 get_subscription_attrs(
7620 cupsd_client_t
*con
, /* I - Client connection */
7621 int sub_id
) /* I - Subscription ID */
7623 http_status_t status
; /* Policy status */
7624 cupsd_subscription_t
*sub
; /* Subscription */
7625 cupsd_policy_t
*policy
; /* Current security policy */
7626 cups_array_t
*ra
, /* Requested attributes array */
7627 *exclude
; /* Private attributes array */
7630 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7631 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7632 con
, con
->number
, sub_id
);
7635 * Expire subscriptions as needed...
7638 cupsdExpireSubscriptions(NULL
, NULL
);
7641 * Is the subscription ID valid?
7644 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7647 * Bad subscription ID...
7650 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7660 policy
= sub
->dest
->op_policy_ptr
;
7662 policy
= DefaultPolicyPtr
;
7664 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7666 send_http_error(con
, status
, sub
->dest
);
7670 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7673 * Copy the subscription attributes to the response using the
7674 * requested-attributes attribute that may be provided by the client.
7677 ra
= create_requested_array(con
->request
);
7679 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7681 cupsArrayDelete(ra
);
7683 con
->response
->request
.status
.status_code
= IPP_OK
;
7688 * 'get_subscriptions()' - Get subscriptions.
7692 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7693 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7695 http_status_t status
; /* Policy status */
7696 int count
; /* Number of subscriptions */
7697 int limit
; /* Limit */
7698 cupsd_subscription_t
*sub
; /* Subscription */
7699 cups_array_t
*ra
; /* Requested attributes array */
7700 ipp_attribute_t
*attr
; /* Attribute */
7701 cups_ptype_t dtype
; /* Destination type (printer/class) */
7702 char scheme
[HTTP_MAX_URI
],
7703 /* Scheme portion of URI */
7704 username
[HTTP_MAX_URI
],
7705 /* Username portion of URI */
7707 /* Host portion of URI */
7708 resource
[HTTP_MAX_URI
];
7709 /* Resource portion of URI */
7710 int port
; /* Port portion of URI */
7711 cupsd_job_t
*job
; /* Job pointer */
7712 cupsd_printer_t
*printer
; /* Printer */
7713 cupsd_policy_t
*policy
; /* Policy */
7714 cups_array_t
*exclude
; /* Private attributes array */
7717 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7718 "get_subscriptions(con=%p[%d], uri=%s)",
7719 con
, con
->number
, uri
->values
[0].string
.text
);
7722 * Is the destination valid?
7725 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7726 sizeof(scheme
), username
, sizeof(username
), host
,
7727 sizeof(host
), &port
, resource
, sizeof(resource
));
7729 if (!strcmp(resource
, "/") ||
7730 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7731 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7732 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7737 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7740 job
= cupsdFindJob(atoi(resource
+ 6));
7744 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7745 atoi(resource
+ 6));
7749 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7755 send_ipp_status(con
, IPP_NOT_FOUND
,
7756 _("The printer or class does not exist."));
7759 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7760 IPP_TAG_INTEGER
)) != NULL
)
7762 job
= cupsdFindJob(attr
->values
[0].integer
);
7766 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7767 attr
->values
[0].integer
);
7779 policy
= printer
->op_policy_ptr
;
7781 policy
= DefaultPolicyPtr
;
7783 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7785 send_http_error(con
, status
, printer
);
7790 * Expire subscriptions as needed...
7793 cupsdExpireSubscriptions(NULL
, NULL
);
7796 * Copy the subscription attributes to the response using the
7797 * requested-attributes attribute that may be provided by the client.
7800 ra
= create_requested_array(con
->request
);
7802 if ((attr
= ippFindAttribute(con
->request
, "limit",
7803 IPP_TAG_INTEGER
)) != NULL
)
7804 limit
= attr
->values
[0].integer
;
7809 * See if we only want to see subscriptions for a specific user...
7812 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7813 IPP_TAG_BOOLEAN
)) != NULL
&&
7814 attr
->values
[0].boolean
)
7815 strlcpy(username
, get_username(con
), sizeof(username
));
7819 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7821 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7822 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7823 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7825 ippAddSeparator(con
->response
);
7827 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7828 policy
, con
, sub
->dest
,
7831 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7834 if (limit
&& count
>= limit
)
7838 cupsArrayDelete(ra
);
7841 con
->response
->request
.status
.status_code
= IPP_OK
;
7843 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7848 * 'get_username()' - Get the username associated with a request.
7851 static const char * /* O - Username */
7852 get_username(cupsd_client_t
*con
) /* I - Connection */
7854 ipp_attribute_t
*attr
; /* Attribute */
7857 if (con
->username
[0])
7858 return (con
->username
);
7859 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7860 IPP_TAG_NAME
)) != NULL
)
7861 return (attr
->values
[0].string
.text
);
7863 return ("anonymous");
7868 * 'hold_job()' - Hold a print job.
7872 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7873 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7875 ipp_attribute_t
*attr
; /* Current job-hold-until */
7876 const char *when
; /* New value */
7877 int jobid
; /* Job ID */
7878 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7879 username
[HTTP_MAX_URI
], /* Username portion of URI */
7880 host
[HTTP_MAX_URI
], /* Host portion of URI */
7881 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7882 int port
; /* Port portion of URI */
7883 cupsd_job_t
*job
; /* Job information */
7886 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7887 uri
->values
[0].string
.text
);
7890 * See if we have a job URI or a printer URI...
7893 if (!strcmp(uri
->name
, "printer-uri"))
7896 * Got a printer URI; see if we also have a job-id attribute...
7899 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7900 IPP_TAG_INTEGER
)) == NULL
)
7902 send_ipp_status(con
, IPP_BAD_REQUEST
,
7903 _("Got a printer-uri attribute but no job-id."));
7907 jobid
= attr
->values
[0].integer
;
7912 * Got a job URI; parse it to get the job ID...
7915 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7916 sizeof(scheme
), username
, sizeof(username
), host
,
7917 sizeof(host
), &port
, resource
, sizeof(resource
));
7919 if (strncmp(resource
, "/jobs/", 6))
7925 send_ipp_status(con
, IPP_BAD_REQUEST
,
7926 _("Bad job-uri \"%s\"."),
7927 uri
->values
[0].string
.text
);
7931 jobid
= atoi(resource
+ 6);
7935 * See if the job exists...
7938 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7941 * Nope - return a "not found" error...
7944 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7949 * See if the job is owned by the requesting user...
7952 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7954 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7955 cupsdFindDest(job
->dest
));
7960 * See if the job is in a state that allows holding...
7963 if (job
->state_value
> IPP_JOB_STOPPED
)
7966 * Return a "not-possible" error...
7969 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7970 _("Job #%d is finished and cannot be altered."),
7976 * Hold the job and return...
7979 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_ZERO
)) != NULL
)
7981 if ((ippGetValueTag(attr
) != IPP_TAG_KEYWORD
&& ippGetValueTag(attr
) != IPP_TAG_NAME
&& ippGetValueTag(attr
) != IPP_TAG_NAMELANG
) || ippGetCount(attr
) != 1 || !ippValidateAttribute(attr
))
7983 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Unsupported 'job-hold-until' value."));
7984 ippCopyAttribute(con
->response
, attr
, 0);
7988 when
= ippGetString(attr
, 0, NULL
);
7990 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7991 "Job job-hold-until value changed by user.");
7994 when
= "indefinite";
7996 cupsdSetJobHoldUntil(job
, when
, 1);
7997 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
8000 con
->response
->request
.status
.status_code
= IPP_OK
;
8005 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8009 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
8010 ipp_attribute_t
*uri
) /* I - Printer URI */
8012 http_status_t status
; /* Policy status */
8013 cups_ptype_t dtype
; /* Destination type (printer/class) */
8014 cupsd_printer_t
*printer
; /* Printer data */
8017 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
8018 con
->number
, uri
->values
[0].string
.text
);
8021 * Is the destination valid?
8024 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8030 send_ipp_status(con
, IPP_NOT_FOUND
,
8031 _("The printer or class does not exist."));
8039 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8041 send_http_error(con
, status
, printer
);
8046 * Hold pending/new jobs sent to the printer...
8049 printer
->holding_new_jobs
= 1;
8051 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
8053 if (dtype
& CUPS_PRINTER_CLASS
)
8054 cupsdLogMessage(CUPSD_LOG_INFO
,
8055 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8056 printer
->name
, get_username(con
));
8058 cupsdLogMessage(CUPSD_LOG_INFO
,
8059 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8060 printer
->name
, get_username(con
));
8063 * Everything was ok, so return OK status...
8066 con
->response
->request
.status
.status_code
= IPP_OK
;
8071 * 'move_job()' - Move a job to a new destination.
8075 move_job(cupsd_client_t
*con
, /* I - Client connection */
8076 ipp_attribute_t
*uri
) /* I - Job URI */
8078 http_status_t status
; /* Policy status */
8079 ipp_attribute_t
*attr
; /* Current attribute */
8080 int jobid
; /* Job ID */
8081 cupsd_job_t
*job
; /* Current job */
8082 const char *src
; /* Source printer/class */
8083 cups_ptype_t stype
, /* Source type (printer or class) */
8084 dtype
; /* Destination type (printer/class) */
8085 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
8086 username
[HTTP_MAX_URI
], /* Username portion of URI */
8087 host
[HTTP_MAX_URI
], /* Host portion of URI */
8088 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8089 int port
; /* Port portion of URI */
8090 cupsd_printer_t
*sprinter
, /* Source printer */
8091 *dprinter
; /* Destination printer */
8094 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
8095 uri
->values
[0].string
.text
);
8098 * Get the new printer or class...
8101 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
8102 IPP_TAG_URI
)) == NULL
)
8105 * Need job-printer-uri...
8108 send_ipp_status(con
, IPP_BAD_REQUEST
,
8109 _("job-printer-uri attribute missing."));
8113 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
8119 send_ipp_status(con
, IPP_NOT_FOUND
,
8120 _("The printer or class does not exist."));
8125 * See if we have a job URI or a printer URI...
8128 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8129 sizeof(scheme
), username
, sizeof(username
), host
,
8130 sizeof(host
), &port
, resource
, sizeof(resource
));
8132 if (!strcmp(uri
->name
, "printer-uri"))
8135 * Got a printer URI; see if we also have a job-id attribute...
8138 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8139 IPP_TAG_INTEGER
)) == NULL
)
8145 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8146 &sprinter
)) == NULL
)
8152 send_ipp_status(con
, IPP_NOT_FOUND
,
8153 _("The printer or class does not exist."));
8162 * Otherwise, just move a single job...
8165 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8168 * Nope - return a "not found" error...
8171 send_ipp_status(con
, IPP_NOT_FOUND
,
8172 _("Job #%d does not exist."), attr
->values
[0].integer
);
8178 * Job found, initialize source pointers...
8189 * Got a job URI; parse it to get the job ID...
8192 if (strncmp(resource
, "/jobs/", 6))
8198 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8199 uri
->values
[0].string
.text
);
8204 * See if the job exists...
8207 jobid
= atoi(resource
+ 6);
8209 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8212 * Nope - return a "not found" error...
8215 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8221 * Job found, initialize source pointers...
8230 * Check the policy of the destination printer...
8233 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8234 job
? job
->username
: NULL
)) != HTTP_OK
)
8236 send_http_error(con
, status
, dprinter
);
8241 * Now move the job or jobs...
8247 * See if the job has been completed...
8250 if (job
->state_value
> IPP_JOB_STOPPED
)
8253 * Return a "not-possible" error...
8256 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8257 _("Job #%d is finished and cannot be altered."),
8263 * See if the job is owned by the requesting user...
8266 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8268 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8269 cupsdFindDest(job
->dest
));
8274 * Move the job to a different printer or class...
8277 cupsdMoveJob(job
, dprinter
);
8282 * Got the source printer, now look through the jobs...
8285 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8287 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8290 * See if the job is pointing at the source printer or has not been
8294 if (_cups_strcasecmp(job
->dest
, src
) ||
8295 job
->state_value
> IPP_JOB_STOPPED
)
8299 * See if the job can be moved by the requesting user...
8302 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8306 * Move the job to a different printer or class...
8309 cupsdMoveJob(job
, dprinter
);
8314 * Start jobs if possible...
8320 * Return with "everything is OK" status...
8323 con
->response
->request
.status
.status_code
= IPP_OK
;
8328 * 'ppd_parse_line()' - Parse a PPD default line.
8331 static int /* O - 0 on success, -1 on failure */
8332 ppd_parse_line(const char *line
, /* I - Line */
8333 char *option
, /* O - Option name */
8334 int olen
, /* I - Size of option name */
8335 char *choice
, /* O - Choice name */
8336 int clen
) /* I - Size of choice name */
8339 * Verify this is a default option line...
8342 if (strncmp(line
, "*Default", 8))
8346 * Read the option name...
8349 for (line
+= 8, olen
--;
8350 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8361 * Skip everything else up to the colon (:)...
8364 while (*line
&& *line
!= ':')
8373 * Now grab the option choice, skipping leading whitespace...
8376 while (isspace(*line
& 255))
8380 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8391 * Return with no errors...
8399 * 'print_job()' - Print a file to a printer or class.
8403 print_job(cupsd_client_t
*con
, /* I - Client connection */
8404 ipp_attribute_t
*uri
) /* I - Printer URI */
8406 ipp_attribute_t
*attr
; /* Current attribute */
8407 ipp_attribute_t
*doc_name
; /* document-name attribute */
8408 ipp_attribute_t
*format
; /* Document-format attribute */
8409 const char *default_format
; /* document-format-default value */
8410 cupsd_job_t
*job
; /* New job */
8411 char filename
[1024]; /* Job filename */
8412 mime_type_t
*filetype
; /* Type of file */
8413 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8414 type
[MIME_MAX_TYPE
], /* Subtype of file */
8415 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8416 /* Textual name of mime type */
8417 cupsd_printer_t
*printer
; /* Printer data */
8418 struct stat fileinfo
; /* File information */
8419 int kbytes
; /* Size of file */
8420 int compression
; /* Document compression */
8423 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8424 uri
->values
[0].string
.text
);
8427 * Validate print file attributes, for now just document-format and
8428 * compression (CUPS only supports "none" and "gzip")...
8431 compression
= CUPS_FILE_NONE
;
8433 if ((attr
= ippFindAttribute(con
->request
, "compression",
8434 IPP_TAG_KEYWORD
)) != NULL
)
8436 if (strcmp(attr
->values
[0].string
.text
, "none")
8438 && strcmp(attr
->values
[0].string
.text
, "gzip")
8439 #endif /* HAVE_LIBZ */
8442 send_ipp_status(con
, IPP_ATTRIBUTES
,
8443 _("Unsupported compression \"%s\"."),
8444 attr
->values
[0].string
.text
);
8445 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8446 "compression", NULL
, attr
->values
[0].string
.text
);
8451 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8452 compression
= CUPS_FILE_GZIP
;
8453 #endif /* HAVE_LIBZ */
8457 * Do we have a file to print?
8462 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8467 * Is the destination valid?
8470 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8476 send_ipp_status(con
, IPP_NOT_FOUND
,
8477 _("The printer or class does not exist."));
8482 * Is it a format we support?
8485 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8487 ippSetName(con
->request
, &doc_name
, "document-name-supplied");
8489 if ((format
= ippFindAttribute(con
->request
, "document-format",
8490 IPP_TAG_MIMETYPE
)) != NULL
)
8493 * Grab format from client...
8496 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8499 send_ipp_status(con
, IPP_BAD_REQUEST
,
8500 _("Bad document-format \"%s\"."),
8501 format
->values
[0].string
.text
);
8505 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
8507 else if ((default_format
= cupsGetOption("document-format",
8508 printer
->num_options
,
8509 printer
->options
)) != NULL
)
8512 * Use default document format...
8515 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8517 send_ipp_status(con
, IPP_BAD_REQUEST
,
8518 _("Bad document-format \"%s\"."),
8529 strlcpy(super
, "application", sizeof(super
));
8530 strlcpy(type
, "octet-stream", sizeof(type
));
8533 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8536 * Auto-type the file...
8539 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8542 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8543 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8547 filetype
= mimeType(MimeDatabase
, super
, type
);
8549 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8550 filetype
->super
, filetype
->type
);
8552 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
8553 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
8556 filetype
= mimeType(MimeDatabase
, super
, type
);
8560 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8563 * Replace the document-format attribute value with the auto-typed or
8567 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8571 ippSetString(con
->request
, &format
, 0, mimetype
);
8573 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8574 "document-format", NULL
, mimetype
);
8578 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8579 _("Unsupported document-format \"%s\"."),
8580 format
? format
->values
[0].string
.text
:
8581 "application/octet-stream");
8582 cupsdLogMessage(CUPSD_LOG_INFO
,
8583 "Hint: Do you have the raw file printing rules enabled?");
8586 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8587 "document-format", NULL
, format
->values
[0].string
.text
);
8593 * Read any embedded job ticket info from PS files...
8596 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8597 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8598 !_cups_strcasecmp(filetype
->type
, "pdf")))
8599 read_job_ticket(con
);
8602 * Create the job object...
8605 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8609 * Update quota data...
8612 if (stat(con
->filename
, &fileinfo
))
8615 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8617 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8619 job
->koctets
+= kbytes
;
8621 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8622 attr
->values
[0].integer
+= kbytes
;
8625 * Add the job file...
8628 if (add_file(con
, job
, filetype
, compression
))
8631 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
8632 if (rename(con
->filename
, filename
))
8634 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
8636 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
8640 cupsdClearString(&con
->filename
);
8643 * See if we need to add the ending sheet...
8646 if (cupsdTimeoutJob(job
))
8650 * Log and save the job...
8653 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8654 "File of type %s/%s queued by \"%s\".",
8655 filetype
->super
, filetype
->type
, job
->username
);
8656 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8657 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8658 job
->dest
, job
->username
);
8661 * Start the job if possible...
8669 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8671 * This function only gets called when printing a single PDF or PostScript
8672 * file using the Print-Job operation. It doesn't work for Create-Job +
8673 * Send-File, since the job attributes need to be set at job creation
8674 * time for banners to work. The embedded job ticket stuff is here
8675 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8676 * options and IPP attributes which otherwise would be lost.
8678 * The format of a job ticket is simple:
8680 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8682 * %cupsJobTicket: attr1=value1
8683 * %cupsJobTicket: attr2=value2
8685 * %cupsJobTicket: attrN=valueN
8687 * Job ticket lines must appear immediately after the first line that
8688 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8689 * stops looking for job ticket info when it finds a line that does not begin
8690 * with "%cupsJobTicket:".
8692 * The maximum length of a job ticket line, including the prefix, is
8693 * 255 characters to conform with the Adobe DSC.
8695 * Read-only attributes are rejected with a notice to the error log in
8696 * case a malicious user tries anything. Since the job ticket is read
8697 * prior to attribute validation in print_job(), job ticket attributes
8698 * will go through the same validation as IPP attributes...
8702 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8704 cups_file_t
*fp
; /* File to read from */
8705 char line
[256]; /* Line data */
8706 int num_options
; /* Number of options */
8707 cups_option_t
*options
; /* Options */
8708 ipp_t
*ticket
; /* New attributes */
8709 ipp_attribute_t
*attr
, /* Current attribute */
8710 *attr2
, /* Job attribute */
8711 *prev2
; /* Previous job attribute */
8715 * First open the print file...
8718 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8720 cupsdLogMessage(CUPSD_LOG_ERROR
,
8721 "Unable to open print file for job ticket - %s",
8727 * Skip the first line...
8730 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8732 cupsdLogMessage(CUPSD_LOG_ERROR
,
8733 "Unable to read from print file for job ticket - %s",
8739 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8742 * Not a DSC-compliant file, so no job ticket info will be available...
8750 * Read job ticket info from the file...
8756 while (cupsFileGets(fp
, line
, sizeof(line
)))
8759 * Stop at the first non-ticket line...
8762 if (strncmp(line
, "%cupsJobTicket:", 15))
8766 * Add the options to the option array...
8769 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8773 * Done with the file; see if we have any options...
8778 if (num_options
== 0)
8782 * OK, convert the options to an attribute list, and apply them to
8787 cupsEncodeOptions(ticket
, num_options
, options
);
8790 * See what the user wants to change.
8793 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8795 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8798 if (!strncmp(attr
->name
, "date-time-at-", 13) ||
8799 !strcmp(attr
->name
, "job-impressions-completed") ||
8800 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8801 !strncmp(attr
->name
, "job-k-octets", 12) ||
8802 !strcmp(attr
->name
, "job-id") ||
8803 !strcmp(attr
->name
, "job-originating-host-name") ||
8804 !strcmp(attr
->name
, "job-originating-user-name") ||
8805 !strcmp(attr
->name
, "job-pages-completed") ||
8806 !strcmp(attr
->name
, "job-printer-uri") ||
8807 !strncmp(attr
->name
, "job-state", 9) ||
8808 !strcmp(attr
->name
, "job-uri") ||
8809 !strncmp(attr
->name
, "time-at-", 8))
8810 continue; /* Read-only attrs */
8812 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8813 IPP_TAG_ZERO
)) != NULL
)
8816 * Some other value; first free the old value...
8819 if (con
->request
->attrs
== attr2
)
8821 con
->request
->attrs
= attr2
->next
;
8826 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8827 if (prev2
->next
== attr2
)
8829 prev2
->next
= attr2
->next
;
8834 if (con
->request
->last
== attr2
)
8835 con
->request
->last
= prev2
;
8837 ippDeleteAttribute(NULL
, attr2
);
8841 * Add new option by copying it...
8844 ippCopyAttribute(con
->request
, attr
, 0);
8848 * Then free the attribute list and option array...
8852 cupsFreeOptions(num_options
, options
);
8857 * 'reject_jobs()' - Reject print jobs to a printer.
8861 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8862 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8864 http_status_t status
; /* Policy status */
8865 cups_ptype_t dtype
; /* Destination type (printer/class) */
8866 cupsd_printer_t
*printer
; /* Printer data */
8867 ipp_attribute_t
*attr
; /* printer-state-message text */
8870 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8871 con
->number
, uri
->values
[0].string
.text
);
8874 * Is the destination valid?
8877 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8883 send_ipp_status(con
, IPP_NOT_FOUND
,
8884 _("The printer or class does not exist."));
8892 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8894 send_http_error(con
, status
, printer
);
8899 * Reject jobs sent to the printer...
8902 printer
->accepting
= 0;
8904 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8905 IPP_TAG_TEXT
)) == NULL
)
8906 strlcpy(printer
->state_message
, "Rejecting Jobs",
8907 sizeof(printer
->state_message
));
8909 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8910 sizeof(printer
->state_message
));
8912 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8913 "No longer accepting jobs.");
8915 if (dtype
& CUPS_PRINTER_CLASS
)
8917 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8919 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8920 printer
->name
, get_username(con
));
8924 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8926 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8927 printer
->name
, get_username(con
));
8931 * Everything was ok, so return OK status...
8934 con
->response
->request
.status
.status_code
= IPP_OK
;
8939 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8943 release_held_new_jobs(
8944 cupsd_client_t
*con
, /* I - Connection */
8945 ipp_attribute_t
*uri
) /* I - Printer URI */
8947 http_status_t status
; /* Policy status */
8948 cups_ptype_t dtype
; /* Destination type (printer/class) */
8949 cupsd_printer_t
*printer
; /* Printer data */
8952 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8953 con
->number
, uri
->values
[0].string
.text
);
8956 * Is the destination valid?
8959 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8965 send_ipp_status(con
, IPP_NOT_FOUND
,
8966 _("The printer or class does not exist."));
8974 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8976 send_http_error(con
, status
, printer
);
8981 * Hold pending/new jobs sent to the printer...
8984 printer
->holding_new_jobs
= 0;
8986 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8988 if (dtype
& CUPS_PRINTER_CLASS
)
8989 cupsdLogMessage(CUPSD_LOG_INFO
,
8990 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8991 printer
->name
, get_username(con
));
8993 cupsdLogMessage(CUPSD_LOG_INFO
,
8994 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8995 printer
->name
, get_username(con
));
9000 * Everything was ok, so return OK status...
9003 con
->response
->request
.status
.status_code
= IPP_OK
;
9008 * 'release_job()' - Release a held print job.
9012 release_job(cupsd_client_t
*con
, /* I - Client connection */
9013 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9015 ipp_attribute_t
*attr
; /* Current attribute */
9016 int jobid
; /* Job ID */
9017 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9018 username
[HTTP_MAX_URI
], /* Username portion of URI */
9019 host
[HTTP_MAX_URI
], /* Host portion of URI */
9020 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9021 int port
; /* Port portion of URI */
9022 cupsd_job_t
*job
; /* Job information */
9025 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
9026 con
->number
, uri
->values
[0].string
.text
);
9029 * See if we have a job URI or a printer URI...
9032 if (!strcmp(uri
->name
, "printer-uri"))
9035 * Got a printer URI; see if we also have a job-id attribute...
9038 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9039 IPP_TAG_INTEGER
)) == NULL
)
9041 send_ipp_status(con
, IPP_BAD_REQUEST
,
9042 _("Got a printer-uri attribute but no job-id."));
9046 jobid
= attr
->values
[0].integer
;
9051 * Got a job URI; parse it to get the job ID...
9054 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9055 sizeof(scheme
), username
, sizeof(username
), host
,
9056 sizeof(host
), &port
, resource
, sizeof(resource
));
9058 if (strncmp(resource
, "/jobs/", 6))
9064 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9065 uri
->values
[0].string
.text
);
9069 jobid
= atoi(resource
+ 6);
9073 * See if the job exists...
9076 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9079 * Nope - return a "not found" error...
9082 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9087 * See if job is "held"...
9090 if (job
->state_value
!= IPP_JOB_HELD
)
9093 * Nope - return a "not possible" error...
9096 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
9101 * See if the job is owned by the requesting user...
9104 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9106 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9107 cupsdFindDest(job
->dest
));
9112 * Reset the job-hold-until value to "no-hold"...
9115 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9116 IPP_TAG_KEYWORD
)) == NULL
)
9117 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9121 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
9122 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
9124 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
9125 "Job job-hold-until value changed by user.");
9126 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9130 * Release the job and return...
9133 cupsdReleaseJob(job
);
9135 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
9136 "Job released by user.");
9138 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
9140 con
->response
->request
.status
.status_code
= IPP_OK
;
9147 * 'renew_subscription()' - Renew an existing subscription...
9152 cupsd_client_t
*con
, /* I - Client connection */
9153 int sub_id
) /* I - Subscription ID */
9155 http_status_t status
; /* Policy status */
9156 cupsd_subscription_t
*sub
; /* Subscription */
9157 ipp_attribute_t
*lease
; /* notify-lease-duration */
9160 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9161 "renew_subscription(con=%p[%d], sub_id=%d)",
9162 con
, con
->number
, sub_id
);
9165 * Is the subscription ID valid?
9168 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
9171 * Bad subscription ID...
9174 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
9182 * Job subscriptions cannot be renewed...
9185 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9186 _("Job subscriptions cannot be renewed."));
9194 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
9196 con
, sub
->owner
)) != HTTP_OK
)
9198 send_http_error(con
, status
, sub
->dest
);
9203 * Renew the subscription...
9206 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
9209 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
9211 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9213 cupsdLogMessage(CUPSD_LOG_INFO
,
9214 "renew_subscription: Limiting notify-lease-duration to "
9217 sub
->lease
= MaxLeaseDuration
;
9220 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9222 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9224 con
->response
->request
.status
.status_code
= IPP_OK
;
9226 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9227 "notify-lease-duration", sub
->lease
);
9232 * 'restart_job()' - Restart an old print job.
9236 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9237 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9239 ipp_attribute_t
*attr
; /* Current attribute */
9240 int jobid
; /* Job ID */
9241 cupsd_job_t
*job
; /* Job information */
9242 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9243 username
[HTTP_MAX_URI
], /* Username portion of URI */
9244 host
[HTTP_MAX_URI
], /* Host portion of URI */
9245 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9246 int port
; /* Port portion of URI */
9249 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9250 con
->number
, uri
->values
[0].string
.text
);
9253 * See if we have a job URI or a printer URI...
9256 if (!strcmp(uri
->name
, "printer-uri"))
9259 * Got a printer URI; see if we also have a job-id attribute...
9262 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9263 IPP_TAG_INTEGER
)) == NULL
)
9265 send_ipp_status(con
, IPP_BAD_REQUEST
,
9266 _("Got a printer-uri attribute but no job-id."));
9270 jobid
= attr
->values
[0].integer
;
9275 * Got a job URI; parse it to get the job ID...
9278 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9279 sizeof(scheme
), username
, sizeof(username
), host
,
9280 sizeof(host
), &port
, resource
, sizeof(resource
));
9282 if (strncmp(resource
, "/jobs/", 6))
9288 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9289 uri
->values
[0].string
.text
);
9293 jobid
= atoi(resource
+ 6);
9297 * See if the job exists...
9300 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9303 * Nope - return a "not found" error...
9306 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9311 * See if job is in any of the "completed" states...
9314 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9317 * Nope - return a "not possible" error...
9320 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
9326 * See if we have retained the job files...
9331 if (!job
->attrs
|| job
->num_files
== 0)
9334 * Nope - return a "not possible" error...
9337 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9338 _("Job #%d cannot be restarted - no files."), jobid
);
9343 * See if the job is owned by the requesting user...
9346 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9348 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9349 cupsdFindDest(job
->dest
));
9354 * See if the job-hold-until attribute is specified...
9357 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
9358 IPP_TAG_KEYWORD
)) == NULL
)
9359 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
9361 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
9364 * Return the job to a held state...
9367 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
9368 "Restarted by \"%s\" with job-hold-until=%s.",
9369 username
, attr
->values
[0].string
.text
);
9370 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
9372 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
9373 NULL
, job
, "Job restarted by user with job-hold-until=%s",
9374 attr
->values
[0].string
.text
);
9379 * Restart the job...
9382 cupsdRestartJob(job
);
9386 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9388 con
->response
->request
.status
.status_code
= IPP_OK
;
9393 * 'save_auth_info()' - Save authentication information for a job.
9398 cupsd_client_t
*con
, /* I - Client connection */
9399 cupsd_job_t
*job
, /* I - Job */
9400 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9402 int i
; /* Looping var */
9403 char filename
[1024]; /* Job authentication filename */
9404 cups_file_t
*fp
; /* Job authentication file */
9405 char line
[65536]; /* Line for file */
9406 cupsd_printer_t
*dest
; /* Destination printer/class */
9410 * This function saves the in-memory authentication information for
9411 * a job so that it can be used to authenticate with a remote host.
9412 * The information is stored in a file that is readable only by the
9413 * root user. The fields are Base-64 encoded, each on a separate line,
9414 * followed by random number (up to 1024) of newlines to limit the
9415 * amount of information that is exposed.
9417 * Because of the potential for exposing of authentication information,
9418 * this functionality is only enabled when running cupsd as root.
9420 * This caching only works for the Basic and BasicDigest authentication
9421 * types. Digest authentication cannot be cached this way, and in
9422 * the future Kerberos authentication may make all of this obsolete.
9424 * Authentication information is saved whenever an authenticated
9425 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9428 * This information is deleted after a job is completed or canceled,
9429 * so reprints may require subsequent re-authentication.
9435 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9439 * Create the authentication file and change permissions...
9442 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9443 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9445 cupsdLogMessage(CUPSD_LOG_ERROR
,
9446 "Unable to save authentication info to \"%s\" - %s",
9447 filename
, strerror(errno
));
9451 fchown(cupsFileNumber(fp
), 0, 0);
9452 fchmod(cupsFileNumber(fp
), 0400);
9454 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9457 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9459 cupsdClearString(job
->auth_env
+ i
);
9461 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9464 * Write 1 to 3 auth values...
9468 i
< auth_info
->num_values
&&
9469 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9472 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9474 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9475 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9478 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9479 auth_info
->values
[i
].string
.text
);
9481 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9482 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9483 auth_info
->values
[i
].string
.text
);
9484 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9485 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9486 auth_info
->values
[i
].string
.text
);
9487 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9488 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9489 auth_info
->values
[i
].string
.text
);
9490 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9491 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9492 auth_info
->values
[i
].string
.text
);
9497 else if (auth_info
&& auth_info
->num_values
== 2 &&
9498 dest
->num_auth_info_required
== 1 &&
9499 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9502 * Allow fallback to username+password for Kerberized queues...
9505 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9506 cupsFilePutConf(fp
, "username", line
);
9508 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9509 auth_info
->values
[0].string
.text
);
9511 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9512 cupsFilePutConf(fp
, "password", line
);
9514 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9515 auth_info
->values
[1].string
.text
);
9517 else if (con
->username
[0])
9520 * Write the authenticated username...
9523 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9524 cupsFilePutConf(fp
, "username", line
);
9526 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9529 * Write the authenticated password...
9532 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9533 cupsFilePutConf(fp
, "password", line
);
9535 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9539 if (con
->gss_uid
> 0)
9541 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9542 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9544 #endif /* HAVE_GSSAPI */
9547 * Write a random number of newlines to the end of the file...
9550 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9551 cupsFilePutChar(fp
, '\n');
9554 * Close the file and return...
9562 * 'send_document()' - Send a file to a printer or class.
9566 send_document(cupsd_client_t
*con
, /* I - Client connection */
9567 ipp_attribute_t
*uri
) /* I - Printer URI */
9569 ipp_attribute_t
*attr
; /* Current attribute */
9570 ipp_attribute_t
*format
; /* Request's document-format attribute */
9571 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9572 const char *default_format
;/* document-format-default value */
9573 int jobid
; /* Job ID number */
9574 cupsd_job_t
*job
; /* Current job */
9575 char job_uri
[HTTP_MAX_URI
],
9577 scheme
[HTTP_MAX_URI
],
9578 /* Method portion of URI */
9579 username
[HTTP_MAX_URI
],
9580 /* Username portion of URI */
9582 /* Host portion of URI */
9583 resource
[HTTP_MAX_URI
];
9584 /* Resource portion of URI */
9585 int port
; /* Port portion of URI */
9586 mime_type_t
*filetype
; /* Type of file */
9587 char super
[MIME_MAX_SUPER
],
9588 /* Supertype of file */
9589 type
[MIME_MAX_TYPE
],
9590 /* Subtype of file */
9591 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9592 /* Textual name of mime type */
9593 char filename
[1024]; /* Job filename */
9594 cupsd_printer_t
*printer
; /* Current printer */
9595 struct stat fileinfo
; /* File information */
9596 int kbytes
; /* Size of file */
9597 int compression
; /* Type of compression */
9598 int start_job
; /* Start the job? */
9601 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9602 con
->number
, uri
->values
[0].string
.text
);
9605 * See if we have a job URI or a printer URI...
9608 if (!strcmp(uri
->name
, "printer-uri"))
9611 * Got a printer URI; see if we also have a job-id attribute...
9614 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9615 IPP_TAG_INTEGER
)) == NULL
)
9617 send_ipp_status(con
, IPP_BAD_REQUEST
,
9618 _("Got a printer-uri attribute but no job-id."));
9622 jobid
= attr
->values
[0].integer
;
9627 * Got a job URI; parse it to get the job ID...
9630 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9631 sizeof(scheme
), username
, sizeof(username
), host
,
9632 sizeof(host
), &port
, resource
, sizeof(resource
));
9634 if (strncmp(resource
, "/jobs/", 6))
9640 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9641 uri
->values
[0].string
.text
);
9645 jobid
= atoi(resource
+ 6);
9649 * See if the job exists...
9652 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9655 * Nope - return a "not found" error...
9658 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9662 printer
= cupsdFindDest(job
->dest
);
9665 * See if the job is owned by the requesting user...
9668 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9670 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9671 cupsdFindDest(job
->dest
));
9676 * OK, see if the client is sending the document compressed - CUPS
9677 * only supports "none" and "gzip".
9680 compression
= CUPS_FILE_NONE
;
9682 if ((attr
= ippFindAttribute(con
->request
, "compression",
9683 IPP_TAG_KEYWORD
)) != NULL
)
9685 if (strcmp(attr
->values
[0].string
.text
, "none")
9687 && strcmp(attr
->values
[0].string
.text
, "gzip")
9688 #endif /* HAVE_LIBZ */
9691 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9692 attr
->values
[0].string
.text
);
9693 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9694 "compression", NULL
, attr
->values
[0].string
.text
);
9699 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9700 compression
= CUPS_FILE_GZIP
;
9701 #endif /* HAVE_LIBZ */
9705 * Do we have a file to print?
9708 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9709 IPP_TAG_BOOLEAN
)) == NULL
)
9711 send_ipp_status(con
, IPP_BAD_REQUEST
,
9712 _("Missing last-document attribute in request."));
9719 * Check for an empty request with "last-document" set to true, which is
9720 * used to close an "open" job by RFC 2911, section 3.3.2.
9723 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9726 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9731 * Is it a format we support?
9736 if ((format
= ippFindAttribute(con
->request
, "document-format",
9737 IPP_TAG_MIMETYPE
)) != NULL
)
9740 * Grab format from client...
9743 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9746 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9747 format
->values
[0].string
.text
);
9751 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
9753 else if ((default_format
= cupsGetOption("document-format",
9754 printer
->num_options
,
9755 printer
->options
)) != NULL
)
9758 * Use default document format...
9761 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9763 send_ipp_status(con
, IPP_BAD_REQUEST
,
9764 _("Bad document-format-default \"%s\"."), default_format
);
9771 * No document format attribute? Auto-type it!
9774 strlcpy(super
, "application", sizeof(super
));
9775 strlcpy(type
, "octet-stream", sizeof(type
));
9778 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9781 * Auto-type the file...
9784 ipp_attribute_t
*doc_name
; /* document-name attribute */
9787 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9789 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9790 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9791 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9795 filetype
= mimeType(MimeDatabase
, super
, type
);
9798 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9799 filetype
->super
, filetype
->type
);
9801 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
9802 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
9805 filetype
= mimeType(MimeDatabase
, super
, type
);
9810 * Replace the document-format attribute value with the auto-typed or
9814 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9817 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9818 IPP_TAG_MIMETYPE
)) != NULL
)
9819 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9821 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9822 "document-format", NULL
, mimetype
);
9826 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9827 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9828 cupsdLogMessage(CUPSD_LOG_INFO
,
9829 "Hint: Do you have the raw file printing rules enabled?");
9832 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9833 "document-format", NULL
, format
->values
[0].string
.text
);
9838 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9840 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9843 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9844 _("Unsupported document-format \"%s\"."), mimetype
);
9846 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9847 "document-format", NULL
, mimetype
);
9853 * Add the file to the job...
9856 if (add_file(con
, job
, filetype
, compression
))
9859 if ((attr
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
)) != NULL
)
9860 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name-supplied", NULL
, ippGetString(attr
, 0, NULL
));
9862 if (stat(con
->filename
, &fileinfo
))
9865 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9867 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9869 job
->koctets
+= kbytes
;
9871 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9872 attr
->values
[0].integer
+= kbytes
;
9874 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
9875 if (rename(con
->filename
, filename
))
9877 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
9879 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
9883 cupsdClearString(&con
->filename
);
9885 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9886 filetype
->super
, filetype
->type
, job
->username
);
9889 * Start the job if this is the last document...
9894 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9895 IPP_TAG_BOOLEAN
)) != NULL
&&
9896 attr
->values
[0].boolean
)
9899 * See if we need to add the ending sheet...
9902 if (cupsdTimeoutJob(job
))
9905 if (job
->state_value
== IPP_JOB_STOPPED
)
9907 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9908 job
->state_value
= IPP_JOB_PENDING
;
9910 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9912 else if (job
->state_value
== IPP_JOB_HELD
)
9914 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9915 IPP_TAG_KEYWORD
)) == NULL
)
9916 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9918 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9920 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9921 job
->state_value
= IPP_JOB_PENDING
;
9923 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9926 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9930 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9936 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9937 IPP_TAG_KEYWORD
)) == NULL
)
9938 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9940 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9942 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9943 job
->state_value
= IPP_JOB_HELD
;
9944 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9946 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9949 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9956 * Fill in the response info...
9959 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9960 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9961 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9964 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9966 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9968 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
9969 NULL
, job
->reasons
->values
[0].string
.text
);
9971 con
->response
->request
.status
.status_code
= IPP_OK
;
9974 * Start the job if necessary...
9983 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9988 cupsd_client_t
*con
, /* I - Client connection */
9989 http_status_t status
, /* I - HTTP status code */
9990 cupsd_printer_t
*printer
) /* I - Printer, if any */
9992 ipp_attribute_t
*uri
; /* Request URI, if any */
9995 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9996 IPP_TAG_URI
)) == NULL
)
9997 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9999 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
10000 "[Client %d] Returning HTTP %s for %s (%s) from %s",
10001 con
->number
, httpStatus(status
),
10003 ippOpString(con
->request
->request
.op
.operation_id
) :
10005 uri
? uri
->values
[0].string
.text
: "no URI",
10006 con
->http
->hostname
);
10010 int auth_type
; /* Type of authentication required */
10013 auth_type
= CUPSD_AUTH_NONE
;
10015 if (status
== HTTP_UNAUTHORIZED
&&
10016 printer
->num_auth_info_required
> 0 &&
10017 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
10019 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
10020 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
10021 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
10024 * Creating and authenticating jobs requires Kerberos...
10027 auth_type
= CUPSD_AUTH_NEGOTIATE
;
10032 * Use policy/location-defined authentication requirements...
10035 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
10036 cupsd_location_t
*auth
; /* Pointer to authentication element */
10039 if (printer
->type
& CUPS_PRINTER_CLASS
)
10040 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
10042 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
10044 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
10045 auth
->type
== CUPSD_AUTH_NONE
)
10046 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
10048 con
->request
->request
.op
.operation_id
:
10053 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
10054 auth_type
= cupsdDefaultAuthType();
10056 auth_type
= auth
->type
;
10060 cupsdSendError(con
, status
, auth_type
);
10063 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
10065 ippDelete(con
->response
);
10066 con
->response
= NULL
;
10073 * 'send_ipp_status()' - Send a status back to the IPP client.
10077 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
10078 ipp_status_t status
, /* I - IPP status code */
10079 const char *message
,/* I - Status message */
10080 ...) /* I - Additional args as needed */
10082 va_list ap
; /* Pointer to additional args */
10083 char formatted
[1024]; /* Formatted errror message */
10086 va_start(ap
, message
);
10087 vsnprintf(formatted
, sizeof(formatted
),
10088 _cupsLangString(con
->language
, message
), ap
);
10091 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
10092 ippOpString(con
->request
->request
.op
.operation_id
),
10093 ippErrorString(status
), formatted
);
10095 con
->response
->request
.status
.status_code
= status
;
10097 if (ippFindAttribute(con
->response
, "attributes-charset",
10098 IPP_TAG_ZERO
) == NULL
)
10099 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
10100 "attributes-charset", NULL
, "utf-8");
10102 if (ippFindAttribute(con
->response
, "attributes-natural-language",
10103 IPP_TAG_ZERO
) == NULL
)
10104 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
10105 "attributes-natural-language", NULL
, DefaultLanguage
);
10107 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
10108 "status-message", NULL
, formatted
);
10113 * 'set_default()' - Set the default destination...
10117 set_default(cupsd_client_t
*con
, /* I - Client connection */
10118 ipp_attribute_t
*uri
) /* I - Printer URI */
10120 http_status_t status
; /* Policy status */
10121 cups_ptype_t dtype
; /* Destination type (printer/class) */
10122 cupsd_printer_t
*printer
, /* Printer */
10123 *oldprinter
; /* Old default printer */
10126 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
10127 con
->number
, uri
->values
[0].string
.text
);
10130 * Is the destination valid?
10133 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10139 send_ipp_status(con
, IPP_NOT_FOUND
,
10140 _("The printer or class does not exist."));
10148 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
10150 send_http_error(con
, status
, NULL
);
10155 * Set it as the default...
10158 oldprinter
= DefaultPrinter
;
10159 DefaultPrinter
= printer
;
10162 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
10163 "%s is no longer the default printer.", oldprinter
->name
);
10165 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
10166 "%s is now the default printer.", printer
->name
);
10168 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
10169 CUPSD_DIRTY_PRINTCAP
);
10171 cupsdLogMessage(CUPSD_LOG_INFO
,
10172 "Default destination set to \"%s\" by \"%s\".",
10173 printer
->name
, get_username(con
));
10176 * Everything was ok, so return OK status...
10179 con
->response
->request
.status
.status_code
= IPP_OK
;
10184 * 'set_job_attrs()' - Set job attributes.
10188 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10189 ipp_attribute_t
*uri
) /* I - Job URI */
10191 ipp_attribute_t
*attr
, /* Current attribute */
10192 *attr2
; /* Job attribute */
10193 int jobid
; /* Job ID */
10194 cupsd_job_t
*job
; /* Current job */
10195 char scheme
[HTTP_MAX_URI
],
10196 /* Method portion of URI */
10197 username
[HTTP_MAX_URI
],
10198 /* Username portion of URI */
10199 host
[HTTP_MAX_URI
],
10200 /* Host portion of URI */
10201 resource
[HTTP_MAX_URI
];
10202 /* Resource portion of URI */
10203 int port
; /* Port portion of URI */
10204 int event
; /* Events? */
10205 int check_jobs
; /* Check jobs? */
10208 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10209 con
->number
, uri
->values
[0].string
.text
);
10212 * Start with "everything is OK" status...
10215 con
->response
->request
.status
.status_code
= IPP_OK
;
10218 * See if we have a job URI or a printer URI...
10221 if (!strcmp(uri
->name
, "printer-uri"))
10224 * Got a printer URI; see if we also have a job-id attribute...
10227 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10228 IPP_TAG_INTEGER
)) == NULL
)
10230 send_ipp_status(con
, IPP_BAD_REQUEST
,
10231 _("Got a printer-uri attribute but no job-id."));
10235 jobid
= attr
->values
[0].integer
;
10240 * Got a job URI; parse it to get the job ID...
10243 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10244 sizeof(scheme
), username
, sizeof(username
), host
,
10245 sizeof(host
), &port
, resource
, sizeof(resource
));
10247 if (strncmp(resource
, "/jobs/", 6))
10253 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
10254 uri
->values
[0].string
.text
);
10258 jobid
= atoi(resource
+ 6);
10262 * See if the job exists...
10265 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10268 * Nope - return a "not found" error...
10271 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
10276 * See if the job has been completed...
10279 if (job
->state_value
> IPP_JOB_STOPPED
)
10282 * Return a "not-possible" error...
10285 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10286 _("Job #%d is finished and cannot be altered."), jobid
);
10291 * See if the job is owned by the requesting user...
10294 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10296 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
10297 cupsdFindDest(job
->dest
));
10302 * See what the user wants to change.
10310 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10312 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10315 if (!strcmp(attr
->name
, "attributes-charset") ||
10316 !strcmp(attr
->name
, "attributes-natural-language") ||
10317 !strncmp(attr
->name
, "date-time-at-", 13) ||
10318 !strncmp(attr
->name
, "document-compression", 20) ||
10319 !strncmp(attr
->name
, "document-format", 15) ||
10320 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10321 !strcmp(attr
->name
, "job-document-access-errors") ||
10322 !strcmp(attr
->name
, "job-id") ||
10323 !strcmp(attr
->name
, "job-impressions-completed") ||
10324 !strcmp(attr
->name
, "job-k-octets-completed") ||
10325 !strcmp(attr
->name
, "job-media-sheets-completed") ||
10326 !strcmp(attr
->name
, "job-originating-host-name") ||
10327 !strcmp(attr
->name
, "job-originating-user-name") ||
10328 !strcmp(attr
->name
, "job-pages-completed") ||
10329 !strcmp(attr
->name
, "job-printer-up-time") ||
10330 !strcmp(attr
->name
, "job-printer-uri") ||
10331 !strcmp(attr
->name
, "job-sheets") ||
10332 !strcmp(attr
->name
, "job-state-message") ||
10333 !strcmp(attr
->name
, "job-state-reasons") ||
10334 !strcmp(attr
->name
, "job-uri") ||
10335 !strcmp(attr
->name
, "number-of-documents") ||
10336 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10337 !strcmp(attr
->name
, "output-device-assigned") ||
10338 !strncmp(attr
->name
, "time-at-", 8))
10344 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10345 _("%s cannot be changed."), attr
->name
);
10347 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10348 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10352 if (!ippValidateAttribute(attr
))
10354 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Bad '%s' value."), attr
->name
);
10355 ippCopyAttribute(con
->response
, attr
, 0);
10359 if (!strcmp(attr
->name
, "job-hold-until"))
10361 const char *when
= ippGetString(attr
, 0, NULL
);
10362 /* job-hold-until value */
10364 if ((ippGetValueTag(attr
) != IPP_TAG_KEYWORD
&& ippGetValueTag(attr
) != IPP_TAG_NAME
&& ippGetValueTag(attr
) != IPP_TAG_NAMELANG
) || ippGetCount(attr
) != 1)
10366 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Unsupported 'job-hold-until' value."));
10367 ippCopyAttribute(con
->response
, attr
, 0);
10371 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s", when
);
10372 cupsdSetJobHoldUntil(job
, when
, 0);
10374 if (!strcmp(when
, "no-hold"))
10376 cupsdReleaseJob(job
);
10380 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".", username
);
10382 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10384 else if (!strcmp(attr
->name
, "job-priority"))
10387 * Change the job priority...
10390 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10392 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
10394 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10395 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10397 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10399 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10400 _("Job is completed and cannot be changed."));
10403 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10405 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10406 attr
->values
[0].integer
);
10407 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10410 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10411 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10414 else if (!strcmp(attr
->name
, "job-state"))
10417 * Change the job state...
10420 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10422 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
10424 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10425 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10429 switch (attr
->values
[0].integer
)
10431 case IPP_JOB_PENDING
:
10432 case IPP_JOB_HELD
:
10433 if (job
->state_value
> IPP_JOB_HELD
)
10435 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10436 _("Job state cannot be changed."));
10439 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10441 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10442 attr
->values
[0].integer
);
10443 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
10448 case IPP_JOB_PROCESSING
:
10449 case IPP_JOB_STOPPED
:
10450 if (job
->state_value
!= attr
->values
[0].integer
)
10452 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10453 _("Job state cannot be changed."));
10458 case IPP_JOB_CANCELED
:
10459 case IPP_JOB_ABORTED
:
10460 case IPP_JOB_COMPLETED
:
10461 if (job
->state_value
> IPP_JOB_PROCESSING
)
10463 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10464 _("Job state cannot be changed."));
10467 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10469 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10470 attr
->values
[0].integer
);
10471 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10473 "Job state changed by \"%s\"", username
);
10480 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10482 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10483 IPP_TAG_ZERO
)) != NULL
)
10486 * Some other value; first free the old value...
10489 if (job
->attrs
->prev
)
10490 job
->attrs
->prev
->next
= attr2
->next
;
10492 job
->attrs
->attrs
= attr2
->next
;
10494 if (job
->attrs
->last
== attr2
)
10495 job
->attrs
->last
= job
->attrs
->prev
;
10497 ippDeleteAttribute(NULL
, attr2
);
10500 * Then copy the attribute...
10503 ippCopyAttribute(job
->attrs
, attr
, 0);
10505 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10508 * Delete the attribute...
10511 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10512 IPP_TAG_ZERO
)) != NULL
)
10514 if (job
->attrs
->prev
)
10515 job
->attrs
->prev
->next
= attr2
->next
;
10517 job
->attrs
->attrs
= attr2
->next
;
10519 if (attr2
== job
->attrs
->last
)
10520 job
->attrs
->last
= job
->attrs
->prev
;
10522 ippDeleteAttribute(NULL
, attr2
);
10524 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10530 * Add new option by copying it...
10533 ippCopyAttribute(job
->attrs
, attr
, 0);
10535 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10544 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10547 * Send events as needed...
10550 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10551 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10552 cupsdFindDest(job
->dest
), job
,
10553 "Job priority changed by user.");
10555 if (event
& CUPSD_EVENT_JOB_STATE
)
10556 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10557 job
->state_value
== IPP_JOB_HELD
?
10558 "Job held by user." : "Job restarted by user.");
10560 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10561 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10562 "Job options changed by user.");
10565 * Start jobs if possible...
10574 * 'set_printer_attrs()' - Set printer attributes.
10578 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10579 ipp_attribute_t
*uri
) /* I - Printer */
10581 http_status_t status
; /* Policy status */
10582 cups_ptype_t dtype
; /* Destination type (printer/class) */
10583 cupsd_printer_t
*printer
; /* Printer/class */
10584 ipp_attribute_t
*attr
; /* Printer attribute */
10585 int changed
= 0; /* Was anything changed? */
10588 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10589 con
->number
, uri
->values
[0].string
.text
);
10592 * Is the destination valid?
10595 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10601 send_ipp_status(con
, IPP_NOT_FOUND
,
10602 _("The printer or class does not exist."));
10610 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10612 send_http_error(con
, status
, printer
);
10617 * Return a list of attributes that can be set via Set-Printer-Attributes.
10620 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10621 IPP_TAG_TEXT
)) != NULL
)
10623 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10627 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
10629 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
10633 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
10635 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
10639 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
10641 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
10645 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10646 IPP_TAG_TEXT
)) != NULL
)
10648 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10653 * Update the printer attributes and return...
10658 printer
->config_time
= time(NULL
);
10660 cupsdSetPrinterAttrs(printer
);
10661 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10663 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10664 "Printer \"%s\" description or location changed by \"%s\".",
10665 printer
->name
, get_username(con
));
10667 cupsdLogMessage(CUPSD_LOG_INFO
,
10668 "Printer \"%s\" description or location changed by \"%s\".",
10669 printer
->name
, get_username(con
));
10672 con
->response
->request
.status
.status_code
= IPP_OK
;
10677 * 'set_printer_defaults()' - Set printer default options from a request.
10680 static int /* O - 1 on success, 0 on failure */
10681 set_printer_defaults(
10682 cupsd_client_t
*con
, /* I - Client connection */
10683 cupsd_printer_t
*printer
) /* I - Printer */
10685 int i
; /* Looping var */
10686 ipp_attribute_t
*attr
; /* Current attribute */
10687 size_t namelen
; /* Length of attribute name */
10688 char name
[256], /* New attribute name */
10689 value
[256]; /* String version of integer attrs */
10692 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10695 * Skip non-printer attributes...
10698 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10701 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10703 if (!strcmp(attr
->name
, "job-sheets-default"))
10706 * Only allow keywords and names...
10709 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10713 * Only allow job-sheets-default to be set when running without a
10714 * system high classification level...
10717 if (Classification
)
10720 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10722 if (attr
->num_values
> 1)
10723 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10725 cupsdSetString(&printer
->job_sheets
[1], "none");
10727 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10729 cupsdFreeStrings(&(printer
->users
));
10731 printer
->deny_users
= 0;
10733 if (attr
->value_tag
== IPP_TAG_NAME
&&
10734 (attr
->num_values
> 1 ||
10735 strcmp(attr
->values
[0].string
.text
, "all")))
10737 for (i
= 0; i
< attr
->num_values
; i
++)
10738 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10741 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10743 cupsdFreeStrings(&(printer
->users
));
10745 printer
->deny_users
= 1;
10747 if (attr
->value_tag
== IPP_TAG_NAME
&&
10748 (attr
->num_values
> 1 ||
10749 strcmp(attr
->values
[0].string
.text
, "none")))
10751 for (i
= 0; i
< attr
->num_values
; i
++)
10752 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10755 else if (!strcmp(attr
->name
, "job-quota-period"))
10757 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10760 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10761 attr
->values
[0].integer
);
10762 cupsdFreeQuotas(printer
);
10764 printer
->quota_period
= attr
->values
[0].integer
;
10766 else if (!strcmp(attr
->name
, "job-k-limit"))
10768 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10771 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10772 attr
->values
[0].integer
);
10773 cupsdFreeQuotas(printer
);
10775 printer
->k_limit
= attr
->values
[0].integer
;
10777 else if (!strcmp(attr
->name
, "job-page-limit"))
10779 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10782 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10783 attr
->values
[0].integer
);
10784 cupsdFreeQuotas(printer
);
10786 printer
->page_limit
= attr
->values
[0].integer
;
10788 else if (!strcmp(attr
->name
, "printer-op-policy"))
10790 cupsd_policy_t
*p
; /* Policy */
10793 if (attr
->value_tag
!= IPP_TAG_NAME
)
10796 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10798 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10799 "Setting printer-op-policy to \"%s\"...",
10800 attr
->values
[0].string
.text
);
10801 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10802 printer
->op_policy_ptr
= p
;
10806 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10807 _("Unknown printer-op-policy \"%s\"."),
10808 attr
->values
[0].string
.text
);
10812 else if (!strcmp(attr
->name
, "printer-error-policy"))
10814 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10817 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10818 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10819 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10820 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10821 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10823 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10824 _("Unknown printer-error-policy \"%s\"."),
10825 attr
->values
[0].string
.text
);
10829 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10830 "Setting printer-error-policy to \"%s\"...",
10831 attr
->values
[0].string
.text
);
10832 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10836 * Skip any other non-default attributes...
10839 namelen
= strlen(attr
->name
);
10840 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10841 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10845 * OK, anything else must be a user-defined default...
10848 strlcpy(name
, attr
->name
, sizeof(name
));
10849 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10851 switch (attr
->value_tag
)
10853 case IPP_TAG_DELETEATTR
:
10854 printer
->num_options
= cupsRemoveOption(name
,
10855 printer
->num_options
,
10856 &(printer
->options
));
10857 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10858 "Deleting %s", attr
->name
);
10861 case IPP_TAG_NAME
:
10862 case IPP_TAG_TEXT
:
10863 case IPP_TAG_KEYWORD
:
10865 printer
->num_options
= cupsAddOption(name
,
10866 attr
->values
[0].string
.text
,
10867 printer
->num_options
,
10868 &(printer
->options
));
10869 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10870 "Setting %s to \"%s\"...", attr
->name
,
10871 attr
->values
[0].string
.text
);
10874 case IPP_TAG_BOOLEAN
:
10875 printer
->num_options
= cupsAddOption(name
,
10876 attr
->values
[0].boolean
?
10878 printer
->num_options
,
10879 &(printer
->options
));
10880 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10881 "Setting %s to %s...", attr
->name
,
10882 attr
->values
[0].boolean
? "true" : "false");
10885 case IPP_TAG_INTEGER
:
10886 case IPP_TAG_ENUM
:
10887 sprintf(value
, "%d", attr
->values
[0].integer
);
10888 printer
->num_options
= cupsAddOption(name
, value
,
10889 printer
->num_options
,
10890 &(printer
->options
));
10891 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10892 "Setting %s to %s...", attr
->name
, value
);
10895 case IPP_TAG_RANGE
:
10896 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10897 attr
->values
[0].range
.upper
);
10898 printer
->num_options
= cupsAddOption(name
, value
,
10899 printer
->num_options
,
10900 &(printer
->options
));
10901 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10902 "Setting %s to %s...", attr
->name
, value
);
10905 case IPP_TAG_RESOLUTION
:
10906 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10907 attr
->values
[0].resolution
.yres
,
10908 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10910 printer
->num_options
= cupsAddOption(name
, value
,
10911 printer
->num_options
,
10912 &(printer
->options
));
10913 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10914 "Setting %s to %s...", attr
->name
, value
);
10918 /* Do nothing for other values */
10928 * 'start_printer()' - Start a printer.
10932 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10933 ipp_attribute_t
*uri
) /* I - Printer URI */
10935 int i
; /* Temporary variable */
10936 http_status_t status
; /* Policy status */
10937 cups_ptype_t dtype
; /* Destination type (printer/class) */
10938 cupsd_printer_t
*printer
; /* Printer data */
10941 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10942 con
->number
, uri
->values
[0].string
.text
);
10945 * Is the destination valid?
10948 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10954 send_ipp_status(con
, IPP_NOT_FOUND
,
10955 _("The printer or class does not exist."));
10963 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10965 send_http_error(con
, status
, printer
);
10970 * Start the printer...
10973 printer
->state_message
[0] = '\0';
10975 cupsdStartPrinter(printer
, 1);
10977 if (dtype
& CUPS_PRINTER_CLASS
)
10978 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10979 printer
->name
, get_username(con
));
10981 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10982 printer
->name
, get_username(con
));
10990 if ((i
= check_quotas(con
, printer
)) < 0)
10992 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
10997 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
11002 * Everything was ok, so return OK status...
11005 con
->response
->request
.status
.status_code
= IPP_OK
;
11010 * 'stop_printer()' - Stop a printer.
11014 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
11015 ipp_attribute_t
*uri
) /* I - Printer URI */
11017 http_status_t status
; /* Policy status */
11018 cups_ptype_t dtype
; /* Destination type (printer/class) */
11019 cupsd_printer_t
*printer
; /* Printer data */
11020 ipp_attribute_t
*attr
; /* printer-state-message attribute */
11023 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
11024 con
->number
, uri
->values
[0].string
.text
);
11027 * Is the destination valid?
11030 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11036 send_ipp_status(con
, IPP_NOT_FOUND
,
11037 _("The printer or class does not exist."));
11045 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11047 send_http_error(con
, status
, printer
);
11052 * Stop the printer...
11055 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
11056 IPP_TAG_TEXT
)) == NULL
)
11057 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
11060 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
11061 sizeof(printer
->state_message
));
11064 cupsdStopPrinter(printer
, 1);
11066 if (dtype
& CUPS_PRINTER_CLASS
)
11067 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
11068 printer
->name
, get_username(con
));
11070 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
11071 printer
->name
, get_username(con
));
11074 * Everything was ok, so return OK status...
11077 con
->response
->request
.status
.status_code
= IPP_OK
;
11082 * 'url_encode_attr()' - URL-encode a string attribute.
11086 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
11087 char *buffer
,/* I - String buffer */
11088 size_t bufsize
)/* I - Size of buffer */
11090 int i
; /* Looping var */
11091 char *bufptr
, /* Pointer into buffer */
11092 *bufend
; /* End of buffer */
11095 strlcpy(buffer
, attr
->name
, bufsize
);
11096 bufptr
= buffer
+ strlen(buffer
);
11097 bufend
= buffer
+ bufsize
- 1;
11099 for (i
= 0; i
< attr
->num_values
; i
++)
11101 if (bufptr
>= bufend
)
11109 if (bufptr
>= bufend
)
11114 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
11116 if (bufptr
>= bufend
)
11127 * 'url_encode_string()' - URL-encode a string.
11130 static char * /* O - End of string */
11131 url_encode_string(const char *s
, /* I - String */
11132 char *buffer
, /* I - String buffer */
11133 size_t bufsize
) /* I - Size of buffer */
11135 char *bufptr
, /* Pointer into buffer */
11136 *bufend
; /* End of buffer */
11137 static const char *hex
= "0123456789ABCDEF";
11142 bufend
= buffer
+ bufsize
- 1;
11144 while (*s
&& bufptr
< bufend
)
11146 if (*s
== ' ' || *s
== '%' || *s
== '+')
11148 if (bufptr
>= (bufend
- 2))
11152 *bufptr
++ = hex
[(*s
>> 4) & 15];
11153 *bufptr
++ = hex
[*s
& 15];
11157 else if (*s
== '\'' || *s
== '\\')
11159 if (bufptr
>= (bufend
- 1))
11176 * 'user_allowed()' - See if a user is allowed to print to a queue.
11179 static int /* O - 0 if not allowed, 1 if allowed */
11180 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
11181 const char *username
) /* I - Username */
11183 struct passwd
*pw
; /* User password data */
11184 char baseuser
[256], /* Base username */
11185 *baseptr
, /* Pointer to "@" in base username */
11186 *name
; /* Current user name */
11189 if (cupsArrayCount(p
->users
) == 0)
11192 if (!strcmp(username
, "root"))
11195 if (strchr(username
, '@'))
11198 * Strip @REALM for username check...
11201 strlcpy(baseuser
, username
, sizeof(baseuser
));
11203 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
11206 username
= baseuser
;
11209 pw
= getpwnam(username
);
11212 for (name
= (char *)cupsArrayFirst(p
->users
);
11214 name
= (char *)cupsArrayNext(p
->users
))
11216 if (name
[0] == '@')
11219 * Check group membership...
11222 if (cupsdCheckGroup(username
, pw
, name
+ 1))
11225 else if (name
[0] == '#')
11231 if (cupsdCheckGroup(username
, pw
, name
))
11234 else if (!_cups_strcasecmp(username
, name
))
11238 return ((name
!= NULL
) != p
->deny_users
);
11243 * 'validate_job()' - Validate printer options and destination.
11247 validate_job(cupsd_client_t
*con
, /* I - Client connection */
11248 ipp_attribute_t
*uri
) /* I - Printer URI */
11250 http_status_t status
; /* Policy status */
11251 ipp_attribute_t
*attr
; /* Current attribute */
11253 ipp_attribute_t
*auth_info
; /* auth-info attribute */
11254 #endif /* HAVE_SSL */
11255 ipp_attribute_t
*format
, /* Document-format attribute */
11256 *name
; /* Job-name attribute */
11257 cups_ptype_t dtype
; /* Destination type (printer/class) */
11258 char super
[MIME_MAX_SUPER
],
11259 /* Supertype of file */
11260 type
[MIME_MAX_TYPE
];
11261 /* Subtype of file */
11262 cupsd_printer_t
*printer
; /* Printer */
11265 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
11266 con
->number
, uri
->values
[0].string
.text
);
11269 * OK, see if the client is sending the document compressed - CUPS
11270 * doesn't support compression yet...
11273 if ((attr
= ippFindAttribute(con
->request
, "compression",
11274 IPP_TAG_KEYWORD
)) != NULL
)
11276 if (strcmp(attr
->values
[0].string
.text
, "none")
11278 && strcmp(attr
->values
[0].string
.text
, "gzip")
11279 #endif /* HAVE_LIBZ */
11282 send_ipp_status(con
, IPP_ATTRIBUTES
,
11283 _("Unsupported 'compression' value \"%s\"."),
11284 attr
->values
[0].string
.text
);
11285 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
11286 "compression", NULL
, attr
->values
[0].string
.text
);
11292 * Is it a format we support?
11295 if ((format
= ippFindAttribute(con
->request
, "document-format",
11296 IPP_TAG_MIMETYPE
)) != NULL
)
11298 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
11301 send_ipp_status(con
, IPP_BAD_REQUEST
,
11302 _("Bad 'document-format' value \"%s\"."),
11303 format
->values
[0].string
.text
);
11307 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
11308 !mimeType(MimeDatabase
, super
, type
))
11310 cupsdLogMessage(CUPSD_LOG_INFO
,
11311 "Hint: Do you have the raw file printing rules enabled?");
11312 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
11313 _("Unsupported 'document-format' value \"%s\"."),
11314 format
->values
[0].string
.text
);
11315 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
11316 "document-format", NULL
, format
->values
[0].string
.text
);
11322 * Is the job-hold-until value valid?
11325 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_ZERO
)) != NULL
&& ((ippGetValueTag(attr
) != IPP_TAG_KEYWORD
&& ippGetValueTag(attr
) != IPP_TAG_NAME
&& ippGetValueTag(attr
) != IPP_TAG_NAMELANG
) || ippGetCount(attr
) != 1 || !ippValidateAttribute(attr
)))
11327 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Unsupported 'job-hold-until' value."));
11328 ippCopyAttribute(con
->response
, attr
, 0);
11333 * Is the job-name valid?
11336 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
11338 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
11339 name
->num_values
!= 1 || !ippValidateAttribute(name
))
11341 if (StrictConformance
)
11343 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Unsupported 'job-name' value."));
11344 ippCopyAttribute(con
->response
, name
, 0);
11349 cupsdLogMessage(CUPSD_LOG_WARN
, "Unsupported 'job-name' value, deleting from request.");
11350 ippDeleteAttribute(con
->request
, name
);
11356 * Is the destination valid?
11359 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11365 send_ipp_status(con
, IPP_NOT_FOUND
,
11366 _("The printer or class does not exist."));
11375 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
11376 #endif /* HAVE_SSL */
11378 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11380 send_http_error(con
, status
, printer
);
11383 else if (printer
->num_auth_info_required
== 1 &&
11384 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
11387 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
11391 else if (auth_info
&& !con
->http
->tls
&&
11392 !httpAddrLocalhost(con
->http
->hostaddr
))
11395 * Require encryption of auth-info over non-local connections...
11398 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
11401 #endif /* HAVE_SSL */
11404 * Everything was ok, so return OK status...
11407 con
->response
->request
.status
.status_code
= IPP_OK
;
11412 * 'validate_name()' - Make sure the printer name only contains valid chars.
11415 static int /* O - 0 if name is no good, 1 if good */
11416 validate_name(const char *name
) /* I - Name to check */
11418 const char *ptr
; /* Pointer into name */
11422 * Scan the whole name...
11425 for (ptr
= name
; *ptr
; ptr
++)
11426 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11430 * All the characters are good; validate the length, too...
11433 return ((ptr
- name
) < 128);
11438 * 'validate_user()' - Validate the user for the request.
11441 static int /* O - 1 if permitted, 0 otherwise */
11442 validate_user(cupsd_job_t
*job
, /* I - Job */
11443 cupsd_client_t
*con
, /* I - Client connection */
11444 const char *owner
, /* I - Owner of job/resource */
11445 char *username
, /* O - Authenticated username */
11446 size_t userlen
) /* I - Length of username */
11448 cupsd_printer_t
*printer
; /* Printer for job */
11451 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
);
11454 * Validate input...
11457 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11461 * Get the best authenticated username that is available.
11464 strlcpy(username
, get_username(con
), userlen
);
11467 * Check the username against the owner...
11470 printer
= cupsdFindDest(job
->dest
);
11472 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11473 con
, owner
) == HTTP_OK
);