4 * IPP routines for the CUPS scheduler.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * Include necessary headers...
24 #include <cups/ppd-private.h>
27 /*# include <ApplicationServices/ApplicationServices.h>
28 extern CFUUIDRef ColorSyncCreateUUIDFromUInt32(unsigned id);
29 # include <CoreFoundation/CoreFoundation.h>*/
30 # ifdef HAVE_MEMBERSHIP_H
31 # include <membership.h>
32 # endif /* HAVE_MEMBERSHIP_H */
33 # ifdef HAVE_MEMBERSHIPPRIV_H
34 # include <membershipPriv.h>
36 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
37 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
38 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
39 # endif /* HAVE_MEMBERSHIPPRIV_H */
40 #endif /* __APPLE__ */
47 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
48 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
49 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
50 mime_type_t
*filetype
, int compression
);
51 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
52 mime_type_t
*filetype
);
53 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
54 static void add_job_uuid(cupsd_job_t
*job
);
55 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
56 static void add_printer_state_reasons(cupsd_client_t
*con
,
58 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
59 static void apply_printer_defaults(cupsd_printer_t
*printer
,
61 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
62 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
63 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
64 static void cancel_subscription(cupsd_client_t
*con
, int id
);
65 static int check_rss_recipient(const char *recipient
);
66 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
67 static void close_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
68 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
69 ipp_tag_t group
, int quickcopy
,
70 cups_array_t
*exclude
);
71 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
73 static int copy_file(const char *from
, const char *to
, mode_t mode
);
74 static int copy_model(cupsd_client_t
*con
, const char *from
,
76 static void copy_job_attrs(cupsd_client_t
*con
,
78 cups_array_t
*ra
, cups_array_t
*exclude
);
79 static void copy_printer_attrs(cupsd_client_t
*con
,
80 cupsd_printer_t
*printer
,
82 static void copy_subscription_attrs(cupsd_client_t
*con
,
83 cupsd_subscription_t
*sub
,
85 cups_array_t
*exclude
);
86 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
87 static cups_array_t
*create_requested_array(ipp_t
*request
);
88 static void create_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
89 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
90 static void get_default(cupsd_client_t
*con
);
91 static void get_devices(cupsd_client_t
*con
);
92 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
93 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
94 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
95 static void get_notifications(cupsd_client_t
*con
);
96 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
97 static void get_ppds(cupsd_client_t
*con
);
98 static void get_printers(cupsd_client_t
*con
, int type
);
99 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
100 static void get_printer_supported(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
101 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
102 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
103 static const char *get_username(cupsd_client_t
*con
);
104 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
105 static void hold_new_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
106 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
107 static int ppd_parse_line(const char *line
, char *option
, int olen
,
108 char *choice
, int clen
);
109 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
110 static void read_job_ticket(cupsd_client_t
*con
);
111 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
112 static void release_held_new_jobs(cupsd_client_t
*con
,
113 ipp_attribute_t
*uri
);
114 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
115 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
116 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
117 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
118 ipp_attribute_t
*auth_info
);
119 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
120 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
121 cupsd_printer_t
*printer
);
122 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
123 const char *message
, ...)
124 __attribute__((__format__(__printf__
, 3, 4)));
125 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
126 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
127 static void set_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
128 static void set_printer_defaults(cupsd_client_t
*con
,
129 cupsd_printer_t
*printer
);
130 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
131 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
132 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
, size_t bufsize
);
133 static char *url_encode_string(const char *s
, char *buffer
, size_t bufsize
);
134 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
135 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
136 static int validate_name(const char *name
);
137 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
, const char *owner
, char *username
, size_t userlen
);
141 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
144 int /* O - 1 on success, 0 on failure */
145 cupsdProcessIPPRequest(
146 cupsd_client_t
*con
) /* I - Client connection */
148 ipp_tag_t group
; /* Current group tag */
149 ipp_attribute_t
*attr
; /* Current attribute */
150 ipp_attribute_t
*charset
; /* Character set attribute */
151 ipp_attribute_t
*language
; /* Language attribute */
152 ipp_attribute_t
*uri
= NULL
; /* Printer or job URI attribute */
153 ipp_attribute_t
*username
; /* requesting-user-name attr */
154 int sub_id
; /* Subscription ID */
157 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
158 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
159 con
, con
->number
, con
->request
->request
.op
.operation_id
);
162 * First build an empty response message for this request...
165 con
->response
= ippNew();
167 con
->response
->request
.status
.version
[0] =
168 con
->request
->request
.op
.version
[0];
169 con
->response
->request
.status
.version
[1] =
170 con
->request
->request
.op
.version
[1];
171 con
->response
->request
.status
.request_id
=
172 con
->request
->request
.op
.request_id
;
175 * Then validate the request header and required attributes...
178 if (con
->request
->request
.any
.version
[0] != 1 &&
179 con
->request
->request
.any
.version
[0] != 2)
182 * Return an error, since we only support IPP 1.x and 2.x.
185 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
186 "%04X %s Bad request version number %d.%d",
187 IPP_VERSION_NOT_SUPPORTED
, con
->http
->hostname
,
188 con
->request
->request
.any
.version
[0],
189 con
->request
->request
.any
.version
[1]);
191 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
192 _("Bad request version number %d.%d."),
193 con
->request
->request
.any
.version
[0],
194 con
->request
->request
.any
.version
[1]);
196 else if (con
->request
->request
.any
.request_id
< 1)
199 * Return an error, since request IDs must be between 1 and 2^31-1
202 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
203 "%04X %s Bad request ID %d",
204 IPP_BAD_REQUEST
, con
->http
->hostname
,
205 con
->request
->request
.any
.request_id
);
207 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad request ID %d."),
208 con
->request
->request
.any
.request_id
);
210 else if (!con
->request
->attrs
)
212 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
213 "%04X %s No attributes in request",
214 IPP_BAD_REQUEST
, con
->http
->hostname
);
216 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request."));
221 * Make sure that the attributes are provided in the correct order and
222 * don't repeat groups...
225 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
228 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
231 * Out of order; return an error...
234 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
235 "%04X %s Attribute groups are out of order",
236 IPP_BAD_REQUEST
, con
->http
->hostname
);
238 send_ipp_status(con
, IPP_BAD_REQUEST
,
239 _("Attribute groups are out of order (%x < %x)."),
240 attr
->group_tag
, group
);
244 group
= attr
->group_tag
;
249 * Then make sure that the first three attributes are:
252 * attributes-natural-language
253 * printer-uri/job-uri
256 attr
= con
->request
->attrs
;
257 if (attr
&& attr
->name
&&
258 !strcmp(attr
->name
, "attributes-charset") &&
259 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
267 if (attr
&& attr
->name
&&
268 !strcmp(attr
->name
, "attributes-natural-language") &&
269 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
274 * Reset language for this request if different from Accept-Language.
277 if (!con
->language
||
278 strcmp(attr
->values
[0].string
.text
, con
->language
->language
))
280 cupsLangFree(con
->language
);
281 con
->language
= cupsLangGet(attr
->values
[0].string
.text
);
287 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
288 IPP_TAG_URI
)) != NULL
)
290 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
291 IPP_TAG_URI
)) != NULL
)
293 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
294 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
299 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
300 "attributes-charset", NULL
,
301 charset
->values
[0].string
.text
);
303 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
304 "attributes-charset", NULL
, "utf-8");
307 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
308 "attributes-natural-language", NULL
,
309 language
->values
[0].string
.text
);
311 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
312 "attributes-natural-language", NULL
, DefaultLanguage
);
315 _cups_strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
316 _cups_strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
319 * Bad character set...
322 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"",
323 charset
->values
[0].string
.text
);
324 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
325 "%04X %s Unsupported attributes-charset value \"%s\"",
326 IPP_CHARSET
, con
->http
->hostname
,
327 charset
->values
[0].string
.text
);
328 send_ipp_status(con
, IPP_BAD_REQUEST
,
329 _("Unsupported character set \"%s\"."),
330 charset
->values
[0].string
.text
);
332 else if (!charset
|| !language
||
334 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
335 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
336 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
337 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
338 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
341 * Return an error, since attributes-charset,
342 * attributes-natural-language, and printer-uri/job-uri are required
343 * for all operations.
348 cupsdLogMessage(CUPSD_LOG_ERROR
,
349 "Missing attributes-charset attribute");
351 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
352 "%04X %s Missing attributes-charset attribute",
353 IPP_BAD_REQUEST
, con
->http
->hostname
);
358 cupsdLogMessage(CUPSD_LOG_ERROR
,
359 "Missing attributes-natural-language attribute");
361 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
362 "%04X %s Missing attributes-natural-language attribute",
363 IPP_BAD_REQUEST
, con
->http
->hostname
);
368 cupsdLogMessage(CUPSD_LOG_ERROR
,
369 "Missing printer-uri, job-uri, or ppd-name "
372 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
373 "%04X %s Missing printer-uri, job-uri, or ppd-name "
374 "attribute", IPP_BAD_REQUEST
, con
->http
->hostname
);
377 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
379 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
380 cupsdLogMessage(CUPSD_LOG_DEBUG
,
381 "attr \"%s\": group_tag = %x, value_tag = %x",
382 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
385 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
387 send_ipp_status(con
, IPP_BAD_REQUEST
,
388 _("Missing required attributes."));
393 * OK, all the checks pass so far; make sure requesting-user-name is
394 * not "root" from a remote host...
397 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
398 IPP_TAG_NAME
)) != NULL
)
401 * Check for root user...
404 if (!strcmp(username
->values
[0].string
.text
, "root") &&
405 _cups_strcasecmp(con
->http
->hostname
, "localhost") &&
406 strcmp(con
->username
, "root"))
409 * Remote unauthenticated user masquerading as local root...
412 ippSetString(con
->request
, &username
, 0, RemoteRoot
);
416 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
417 IPP_TAG_INTEGER
)) != NULL
)
418 sub_id
= attr
->values
[0].integer
;
423 * Then try processing the operation...
427 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
428 ippOpString(con
->request
->request
.op
.operation_id
),
429 uri
->values
[0].string
.text
);
431 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
432 ippOpString(con
->request
->request
.op
.operation_id
));
434 switch (con
->request
->request
.op
.operation_id
)
436 case IPP_OP_PRINT_JOB
:
440 case IPP_OP_VALIDATE_JOB
:
441 validate_job(con
, uri
);
444 case IPP_OP_CREATE_JOB
:
445 create_job(con
, uri
);
448 case IPP_OP_SEND_DOCUMENT
:
449 send_document(con
, uri
);
452 case IPP_OP_CANCEL_JOB
:
453 cancel_job(con
, uri
);
456 case IPP_OP_GET_JOB_ATTRIBUTES
:
457 get_job_attrs(con
, uri
);
460 case IPP_OP_GET_JOBS
:
464 case IPP_OP_GET_PRINTER_ATTRIBUTES
:
465 get_printer_attrs(con
, uri
);
468 case IPP_OP_GET_PRINTER_SUPPORTED_VALUES
:
469 get_printer_supported(con
, uri
);
472 case IPP_OP_HOLD_JOB
:
476 case IPP_OP_RELEASE_JOB
:
477 release_job(con
, uri
);
480 case IPP_OP_RESTART_JOB
:
481 restart_job(con
, uri
);
484 case IPP_OP_PAUSE_PRINTER
:
485 stop_printer(con
, uri
);
488 case IPP_OP_RESUME_PRINTER
:
489 start_printer(con
, uri
);
492 case IPP_OP_PURGE_JOBS
:
493 case IPP_OP_CANCEL_JOBS
:
494 case IPP_OP_CANCEL_MY_JOBS
:
495 cancel_all_jobs(con
, uri
);
498 case IPP_OP_SET_JOB_ATTRIBUTES
:
499 set_job_attrs(con
, uri
);
502 case IPP_OP_SET_PRINTER_ATTRIBUTES
:
503 set_printer_attrs(con
, uri
);
506 case IPP_OP_HOLD_NEW_JOBS
:
507 hold_new_jobs(con
, uri
);
510 case IPP_OP_RELEASE_HELD_NEW_JOBS
:
511 release_held_new_jobs(con
, uri
);
514 case IPP_OP_CLOSE_JOB
:
518 case IPP_OP_CUPS_GET_DEFAULT
:
522 case IPP_OP_CUPS_GET_PRINTERS
:
523 get_printers(con
, 0);
526 case IPP_OP_CUPS_GET_CLASSES
:
527 get_printers(con
, CUPS_PRINTER_CLASS
);
530 case IPP_OP_CUPS_ADD_MODIFY_PRINTER
:
531 add_printer(con
, uri
);
534 case IPP_OP_CUPS_DELETE_PRINTER
:
535 delete_printer(con
, uri
);
538 case IPP_OP_CUPS_ADD_MODIFY_CLASS
:
542 case IPP_OP_CUPS_DELETE_CLASS
:
543 delete_printer(con
, uri
);
546 case IPP_OP_CUPS_ACCEPT_JOBS
:
547 case IPP_OP_ENABLE_PRINTER
:
548 accept_jobs(con
, uri
);
551 case IPP_OP_CUPS_REJECT_JOBS
:
552 case IPP_OP_DISABLE_PRINTER
:
553 reject_jobs(con
, uri
);
556 case IPP_OP_CUPS_SET_DEFAULT
:
557 set_default(con
, uri
);
560 case IPP_OP_CUPS_GET_DEVICES
:
564 case IPP_OP_CUPS_GET_DOCUMENT
:
565 get_document(con
, uri
);
568 case IPP_OP_CUPS_GET_PPD
:
572 case IPP_OP_CUPS_GET_PPDS
:
576 case IPP_OP_CUPS_MOVE_JOB
:
580 case IPP_OP_CUPS_AUTHENTICATE_JOB
:
581 authenticate_job(con
, uri
);
584 case IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS
:
585 case IPP_OP_CREATE_JOB_SUBSCRIPTIONS
:
586 create_subscriptions(con
, uri
);
589 case IPP_OP_GET_SUBSCRIPTION_ATTRIBUTES
:
590 get_subscription_attrs(con
, sub_id
);
593 case IPP_OP_GET_SUBSCRIPTIONS
:
594 get_subscriptions(con
, uri
);
597 case IPP_OP_RENEW_SUBSCRIPTION
:
598 renew_subscription(con
, sub_id
);
601 case IPP_OP_CANCEL_SUBSCRIPTION
:
602 cancel_subscription(con
, sub_id
);
605 case IPP_OP_GET_NOTIFICATIONS
:
606 get_notifications(con
);
610 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
611 "%04X %s Operation %04X (%s) not supported",
612 IPP_OPERATION_NOT_SUPPORTED
, con
->http
->hostname
,
613 con
->request
->request
.op
.operation_id
,
614 ippOpString(con
->request
->request
.op
.operation_id
));
616 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
617 _("%s not supported."),
619 con
->request
->request
.op
.operation_id
));
629 * Sending data from the scheduler...
632 cupsdLogMessage(con
->response
->request
.status
.status_code
633 >= IPP_BAD_REQUEST
&&
634 con
->response
->request
.status
.status_code
635 != IPP_NOT_FOUND
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
636 "[Client %d] Returning IPP %s for %s (%s) from %s",
638 ippErrorString(con
->response
->request
.status
.status_code
),
639 ippOpString(con
->request
->request
.op
.operation_id
),
640 uri
? uri
->values
[0].string
.text
: "no URI",
641 con
->http
->hostname
);
643 httpClearFields(con
->http
);
645 #ifdef CUPSD_USE_CHUNKING
647 * Because older versions of CUPS (1.1.17 and older) and some IPP
648 * clients do not implement chunking properly, we cannot use
649 * chunking by default. This may become the default in future
650 * CUPS releases, or we might add a configuration directive for
654 if (con
->http
->version
== HTTP_1_1
)
656 cupsdLogMessage(CUPSD_LOG_DEBUG
,
657 "[Client %d] Transfer-Encoding: chunked",
660 cupsdSetLength(con
->http
, 0);
663 #endif /* CUPSD_USE_CHUNKING */
665 size_t length
; /* Length of response */
668 length
= ippLength(con
->response
);
670 if (con
->file
>= 0 && !con
->pipe_pid
)
672 struct stat fileinfo
; /* File information */
674 if (!fstat(con
->file
, &fileinfo
))
675 length
+= (size_t)fileinfo
.st_size
;
678 cupsdLogMessage(CUPSD_LOG_DEBUG
,
679 "[Client %d] Content-Length: " CUPS_LLFMT
,
680 con
->number
, CUPS_LLCAST length
);
681 httpSetLength(con
->http
, length
);
684 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
687 * Tell the caller the response header was sent successfully...
690 cupsdAddSelect(httpGetFd(con
->http
), (cupsd_selfunc_t
)cupsdReadClient
,
691 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
698 * Tell the caller the response header could not be sent...
707 * Sending data from a subprocess like cups-deviced; tell the caller
708 * everything is A-OK so far...
717 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
720 int /* O - 0 on success, -1 on error */
721 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
723 cupsd_printer_t
*printer
; /* Destination printer or class */
724 ipp_attribute_t
*attr
; /* job-sheets attribute */
725 int kbytes
; /* Kilobytes in banner */
728 job
->pending_timeout
= 0;
731 * See if we need to add the ending sheet...
734 if (!cupsdLoadJob(job
))
737 printer
= cupsdFindDest(job
->dest
);
738 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
740 if (printer
&& !(printer
->type
& CUPS_PRINTER_REMOTE
) &&
741 attr
&& attr
->num_values
> 1)
747 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding end banner page \"%s\".",
748 attr
->values
[1].string
.text
);
750 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
753 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
761 * 'accept_jobs()' - Accept print jobs to a printer.
765 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
766 ipp_attribute_t
*uri
) /* I - Printer or class URI */
768 http_status_t status
; /* Policy status */
769 cups_ptype_t dtype
; /* Destination type (printer/class) */
770 cupsd_printer_t
*printer
; /* Printer data */
773 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
774 con
->number
, uri
->values
[0].string
.text
);
777 * Is the destination valid?
780 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
786 send_ipp_status(con
, IPP_NOT_FOUND
,
787 _("The printer or class does not exist."));
795 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
797 send_http_error(con
, status
, printer
);
802 * Accept jobs sent to the printer...
805 printer
->accepting
= 1;
806 printer
->state_message
[0] = '\0';
808 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
809 "Now accepting jobs.");
811 if (dtype
& CUPS_PRINTER_CLASS
)
813 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
815 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
816 printer
->name
, get_username(con
));
820 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
822 cupsdLogMessage(CUPSD_LOG_INFO
,
823 "Printer \"%s\" now accepting jobs (\"%s\").",
824 printer
->name
, get_username(con
));
828 * Everything was ok, so return OK status...
831 con
->response
->request
.status
.status_code
= IPP_OK
;
836 * 'add_class()' - Add a class to the system.
840 add_class(cupsd_client_t
*con
, /* I - Client connection */
841 ipp_attribute_t
*uri
) /* I - URI of class */
843 http_status_t status
; /* Policy status */
844 int i
; /* Looping var */
845 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
846 username
[HTTP_MAX_URI
], /* Username portion of URI */
847 host
[HTTP_MAX_URI
], /* Host portion of URI */
848 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
849 int port
; /* Port portion of URI */
850 cupsd_printer_t
*pclass
, /* Class */
851 *member
; /* Member printer/class */
852 cups_ptype_t dtype
; /* Destination type */
853 ipp_attribute_t
*attr
; /* Printer attribute */
854 int modify
; /* Non-zero if we just modified */
855 int need_restart_job
; /* Need to restart job? */
858 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
859 con
->number
, uri
->values
[0].string
.text
);
862 * Do we have a valid URI?
865 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
866 sizeof(scheme
), username
, sizeof(username
), host
,
867 sizeof(host
), &port
, resource
, sizeof(resource
));
870 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
873 * No, return an error...
876 send_ipp_status(con
, IPP_BAD_REQUEST
,
877 _("The printer-uri must be of the form "
878 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
883 * Do we have a valid printer name?
886 if (!validate_name(resource
+ 9))
889 * No, return an error...
892 send_ipp_status(con
, IPP_BAD_REQUEST
,
893 _("The printer-uri \"%s\" contains invalid characters."),
894 uri
->values
[0].string
.text
);
899 * See if the class already exists; if not, create a new class...
902 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
905 * Class doesn't exist; see if we have a printer of the same name...
908 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
)
911 * Yes, return an error...
914 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
915 _("A printer named \"%s\" already exists."),
921 * No, check the default policy and then add the class...
924 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
926 send_http_error(con
, status
, NULL
);
930 pclass
= cupsdAddClass(resource
+ 9);
933 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
936 send_http_error(con
, status
, pclass
);
943 * Look for attributes and copy them over as needed...
946 need_restart_job
= 0;
948 if ((attr
= ippFindAttribute(con
->request
, "printer-location", IPP_TAG_TEXT
)) != NULL
)
949 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
951 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
952 cupsdSetString(&pclass
->geo_location
, attr
->values
[0].string
.text
);
954 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
955 cupsdSetString(&pclass
->organization
, attr
->values
[0].string
.text
);
957 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
958 cupsdSetString(&pclass
->organizational_unit
, attr
->values
[0].string
.text
);
960 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
961 IPP_TAG_TEXT
)) != NULL
)
962 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
964 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
965 IPP_TAG_BOOLEAN
)) != NULL
&&
966 attr
->values
[0].boolean
!= pclass
->accepting
)
968 cupsdLogMessage(CUPSD_LOG_INFO
,
969 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
970 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
972 pclass
->accepting
= attr
->values
[0].boolean
;
974 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s accepting jobs.",
975 pclass
->accepting
? "Now" : "No longer");
978 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
979 IPP_TAG_BOOLEAN
)) != NULL
)
981 if (pclass
->shared
&& !attr
->values
[0].boolean
)
982 cupsdDeregisterPrinter(pclass
, 1);
984 cupsdLogMessage(CUPSD_LOG_INFO
,
985 "Setting %s printer-is-shared to %d (was %d.)",
986 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
988 pclass
->shared
= attr
->values
[0].boolean
;
991 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
992 IPP_TAG_ENUM
)) != NULL
)
994 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
995 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
997 send_ipp_status(con
, IPP_BAD_REQUEST
,
998 _("Attempt to set %s printer-state to bad value %d."),
999 pclass
->name
, attr
->values
[0].integer
);
1003 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1004 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1006 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1007 cupsdStopPrinter(pclass
, 0);
1010 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1011 need_restart_job
= 1;
1014 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1015 IPP_TAG_TEXT
)) != NULL
)
1017 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1018 sizeof(pclass
->state_message
));
1020 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s",
1021 pclass
->state_message
);
1023 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1024 IPP_TAG_URI
)) != NULL
)
1027 * Clear the printer array as needed...
1030 need_restart_job
= 1;
1032 if (pclass
->num_printers
> 0)
1034 free(pclass
->printers
);
1035 pclass
->num_printers
= 0;
1039 * Add each printer or class that is listed...
1042 for (i
= 0; i
< attr
->num_values
; i
++)
1045 * Search for the printer or class URI...
1048 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1054 send_ipp_status(con
, IPP_NOT_FOUND
,
1055 _("The printer or class does not exist."));
1058 else if (dtype
& CUPS_PRINTER_CLASS
)
1060 send_ipp_status(con
, IPP_BAD_REQUEST
,
1061 _("Nested classes are not allowed."));
1066 * Add it to the class...
1069 cupsdAddPrinterToClass(pclass
, member
);
1073 set_printer_defaults(con
, pclass
);
1075 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1076 IPP_TAG_KEYWORD
)) != NULL
)
1077 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1079 pclass
->config_time
= time(NULL
);
1082 * Update the printer class attributes and return...
1085 cupsdSetPrinterAttrs(pclass
);
1086 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1088 if (need_restart_job
&& pclass
->job
)
1091 * Reset the current job to a "pending" status...
1094 cupsdSetJobState(pclass
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
1095 "Job restarted because the class was modified.");
1098 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1102 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
1103 pclass
, NULL
, "Class \"%s\" modified by \"%s\".",
1104 pclass
->name
, get_username(con
));
1106 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1107 pclass
->name
, get_username(con
));
1111 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
1112 pclass
, NULL
, "New class \"%s\" added by \"%s\".",
1113 pclass
->name
, get_username(con
));
1115 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1116 pclass
->name
, get_username(con
));
1119 con
->response
->request
.status
.status_code
= IPP_OK
;
1124 * 'add_file()' - Add a file to a job.
1127 static int /* O - 0 on success, -1 on error */
1128 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1129 cupsd_job_t
*job
, /* I - Job to add to */
1130 mime_type_t
*filetype
, /* I - Type of file */
1131 int compression
) /* I - Compression */
1133 mime_type_t
**filetypes
; /* New filetypes array... */
1134 int *compressions
; /* New compressions array... */
1137 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1138 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1139 "compression=%d)", con
, con
? con
->number
: -1, job
->id
,
1140 filetype
->super
, filetype
->type
, compression
);
1143 * Add the file to the job...
1146 if (job
->num_files
== 0)
1148 compressions
= (int *)malloc(sizeof(int));
1149 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1153 compressions
= (int *)realloc(job
->compressions
,
1154 (size_t)(job
->num_files
+ 1) * sizeof(int));
1155 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1156 (size_t)(job
->num_files
+ 1) *
1157 sizeof(mime_type_t
*));
1161 job
->compressions
= compressions
;
1164 job
->filetypes
= filetypes
;
1166 if (!compressions
|| !filetypes
)
1168 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1169 "Job aborted because the scheduler ran out of memory.");
1172 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1173 _("Unable to allocate memory for file types."));
1178 job
->compressions
[job
->num_files
] = compression
;
1179 job
->filetypes
[job
->num_files
] = filetype
;
1184 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1191 * 'add_job()' - Add a job to a print queue.
1194 static cupsd_job_t
* /* O - Job object */
1195 add_job(cupsd_client_t
*con
, /* I - Client connection */
1196 cupsd_printer_t
*printer
, /* I - Destination printer */
1197 mime_type_t
*filetype
) /* I - First print file type, if any */
1199 http_status_t status
; /* Policy status */
1200 ipp_attribute_t
*attr
, /* Current attribute */
1201 *auth_info
; /* auth-info attribute */
1202 const char *mandatory
; /* Current mandatory job attribute */
1203 const char *val
; /* Default option value */
1204 int priority
; /* Job priority */
1205 cupsd_job_t
*job
; /* Current job */
1206 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1207 int kbytes
; /* Size of print file */
1208 int i
; /* Looping var */
1209 int lowerpagerange
; /* Page range bound */
1210 int exact
; /* Did we have an exact match? */
1211 ipp_attribute_t
*media_col
, /* media-col attribute */
1212 *media_margin
; /* media-*-margin attribute */
1213 ipp_t
*unsup_col
; /* media-col in unsupported response */
1214 static const char * const readonly
[] =/* List of read-only attributes */
1216 "date-time-at-completed",
1217 "date-time-at-creation",
1218 "date-time-at-processing",
1219 "job-detailed-status-messages",
1220 "job-document-access-errors",
1222 "job-impressions-completed",
1223 "job-k-octets-completed",
1224 "job-media-sheets-completed",
1225 "job-pages-completed",
1226 "job-printer-up-time",
1229 "job-state-message",
1230 "job-state-reasons",
1232 "number-of-documents",
1233 "number-of-intervening-jobs",
1234 "output-device-assigned",
1235 "time-at-completed",
1237 "time-at-processing"
1241 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1242 con
, con
->number
, printer
, printer
->name
,
1243 filetype
, filetype
? filetype
->super
: "none",
1244 filetype
? filetype
->type
: "none");
1247 * Check remote printing to non-shared printer...
1250 if (!printer
->shared
&&
1251 _cups_strcasecmp(con
->http
->hostname
, "localhost") &&
1252 _cups_strcasecmp(con
->http
->hostname
, ServerName
))
1254 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1255 _("The printer or class is not shared."));
1263 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1265 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1267 send_http_error(con
, status
, printer
);
1270 else if (printer
->num_auth_info_required
== 1 &&
1271 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
1274 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1278 else if (auth_info
&& !con
->http
->tls
&&
1279 !httpAddrLocalhost(con
->http
->hostaddr
))
1282 * Require encryption of auth-info over non-local connections...
1285 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1288 #endif /* HAVE_SSL */
1291 * See if the printer is accepting jobs...
1294 if (!printer
->accepting
)
1296 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1297 _("Destination \"%s\" is not accepting jobs."),
1303 * Validate job template attributes; for now just document-format,
1304 * copies, job-sheets, number-up, page-ranges, mandatory attributes, and
1308 for (i
= 0; i
< (int)(sizeof(readonly
) / sizeof(readonly
[0])); i
++)
1310 if ((attr
= ippFindAttribute(con
->request
, readonly
[i
], IPP_TAG_ZERO
)) != NULL
)
1312 ippDeleteAttribute(con
->request
, attr
);
1314 if (StrictConformance
)
1316 send_ipp_status(con
, IPP_BAD_REQUEST
, _("The '%s' Job Status attribute cannot be supplied in a job creation request."), readonly
[i
]);
1320 cupsdLogMessage(CUPSD_LOG_INFO
, "Unexpected '%s' Job Status attribute in a job creation request.", readonly
[i
]);
1326 for (mandatory
= (char *)cupsArrayFirst(printer
->pc
->mandatory
);
1328 mandatory
= (char *)cupsArrayNext(printer
->pc
->mandatory
))
1330 if (!ippFindAttribute(con
->request
, mandatory
, IPP_TAG_ZERO
))
1333 * Missing a required attribute...
1336 send_ipp_status(con
, IPP_CONFLICT
,
1337 _("The \"%s\" attribute is required for print jobs."),
1344 if (filetype
&& printer
->filetypes
&&
1345 !cupsArrayFind(printer
->filetypes
, filetype
))
1347 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1348 /* MIME media type string */
1351 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1354 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1355 _("Unsupported format \"%s\"."), mimetype
);
1357 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1358 "document-format", NULL
, mimetype
);
1363 if ((attr
= ippFindAttribute(con
->request
, "copies",
1364 IPP_TAG_INTEGER
)) != NULL
)
1366 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1368 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1369 attr
->values
[0].integer
);
1370 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1371 "copies", attr
->values
[0].integer
);
1376 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1377 IPP_TAG_ZERO
)) != NULL
)
1379 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1380 attr
->value_tag
!= IPP_TAG_NAME
)
1382 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type."));
1386 if (attr
->num_values
> 2)
1388 send_ipp_status(con
, IPP_BAD_REQUEST
,
1389 _("Too many job-sheets values (%d > 2)."),
1394 for (i
= 0; i
< attr
->num_values
; i
++)
1395 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1396 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1398 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\"."),
1399 attr
->values
[i
].string
.text
);
1404 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1405 IPP_TAG_INTEGER
)) != NULL
)
1407 if (attr
->values
[0].integer
!= 1 &&
1408 attr
->values
[0].integer
!= 2 &&
1409 attr
->values
[0].integer
!= 4 &&
1410 attr
->values
[0].integer
!= 6 &&
1411 attr
->values
[0].integer
!= 9 &&
1412 attr
->values
[0].integer
!= 16)
1414 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1415 attr
->values
[0].integer
);
1416 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1417 "number-up", attr
->values
[0].integer
);
1422 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1423 IPP_TAG_RANGE
)) != NULL
)
1425 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1427 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1428 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1430 send_ipp_status(con
, IPP_BAD_REQUEST
,
1431 _("Bad page-ranges values %d-%d."),
1432 attr
->values
[i
].range
.lower
,
1433 attr
->values
[i
].range
.upper
);
1437 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1442 * Do media selection as needed...
1445 if (!ippFindAttribute(con
->request
, "PageRegion", IPP_TAG_ZERO
) &&
1446 !ippFindAttribute(con
->request
, "PageSize", IPP_TAG_ZERO
) &&
1447 _ppdCacheGetPageSize(printer
->pc
, con
->request
, NULL
, &exact
))
1450 (media_col
= ippFindAttribute(con
->request
, "media-col",
1451 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1453 send_ipp_status(con
, IPP_OK_SUBST
, _("Unsupported margins."));
1455 unsup_col
= ippNew();
1456 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1457 "media-bottom-margin",
1458 IPP_TAG_INTEGER
)) != NULL
)
1459 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1460 "media-bottom-margin", media_margin
->values
[0].integer
);
1462 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1463 "media-left-margin",
1464 IPP_TAG_INTEGER
)) != NULL
)
1465 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1466 "media-left-margin", media_margin
->values
[0].integer
);
1468 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1469 "media-right-margin",
1470 IPP_TAG_INTEGER
)) != NULL
)
1471 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1472 "media-right-margin", media_margin
->values
[0].integer
);
1474 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1476 IPP_TAG_INTEGER
)) != NULL
)
1477 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1478 "media-top-margin", media_margin
->values
[0].integer
);
1480 ippAddCollection(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, "media-col",
1482 ippDelete(unsup_col
);
1487 * Make sure we aren't over our limit...
1490 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1493 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1495 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Too many active jobs."));
1499 if ((i
= check_quotas(con
, printer
)) < 0)
1501 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1506 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1511 * Create the job and set things up...
1514 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1515 IPP_TAG_INTEGER
)) != NULL
)
1516 priority
= attr
->values
[0].integer
;
1519 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1520 printer
->options
)) != NULL
)
1521 priority
= atoi(val
);
1525 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1529 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) == NULL
)
1530 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1531 else if ((attr
->value_tag
!= IPP_TAG_NAME
&&
1532 attr
->value_tag
!= IPP_TAG_NAMELANG
) ||
1533 attr
->num_values
!= 1)
1535 send_ipp_status(con
, IPP_ATTRIBUTES
,
1536 _("Bad job-name value: Wrong type or count."));
1537 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1538 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1541 else if (!ippValidateAttribute(attr
))
1543 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad job-name value: %s"),
1544 cupsLastErrorString());
1545 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1546 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1550 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1552 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1553 _("Unable to add job for destination \"%s\"."),
1558 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
1559 job
->attrs
= con
->request
;
1561 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1563 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1566 apply_printer_defaults(printer
, job
);
1568 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1570 if (con
->username
[0])
1572 cupsdSetString(&job
->username
, con
->username
);
1575 ippSetString(job
->attrs
, &attr
, 0, con
->username
);
1579 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1580 "add_job: requesting-user-name=\"%s\"",
1581 attr
->values
[0].string
.text
);
1583 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1586 cupsdSetString(&job
->username
, "anonymous");
1589 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1590 "job-originating-user-name", NULL
, job
->username
);
1593 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1594 ippSetName(job
->attrs
, &attr
, "job-originating-user-name");
1597 if (con
->username
[0] || auth_info
)
1599 save_auth_info(con
, job
, auth_info
);
1602 * Remove the auth-info attribute from the attribute data...
1606 ippDeleteAttribute(job
->attrs
, auth_info
);
1609 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_NAME
)) != NULL
)
1610 cupsdSetString(&(job
->name
), attr
->values
[0].string
.text
);
1612 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1613 IPP_TAG_ZERO
)) != NULL
)
1616 * Request contains a job-originating-host-name attribute; validate it...
1619 if (attr
->value_tag
!= IPP_TAG_NAME
||
1620 attr
->num_values
!= 1 ||
1621 strcmp(con
->http
->hostname
, "localhost"))
1624 * Can't override the value if we aren't connected via localhost.
1625 * Also, we can only have 1 value and it must be a name value.
1628 ippDeleteAttribute(job
->attrs
, attr
);
1629 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-originating-host-name", NULL
, con
->http
->hostname
);
1632 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1637 * No job-originating-host-name attribute, so use the hostname from
1641 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1642 "job-originating-host-name", NULL
, con
->http
->hostname
);
1645 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-completed");
1646 ippAddDate(job
->attrs
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(time(NULL
)));
1647 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-processing");
1648 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-completed");
1649 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", time(NULL
));
1650 ippAddOutOfBand(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "time-at-processing");
1653 * Add remaining job attributes...
1656 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1657 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1658 "job-state", IPP_JOB_STOPPED
);
1659 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1660 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1661 "job-state-reasons", NULL
, "job-incoming");
1662 job
->impressions
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", 0);
1663 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1664 "job-media-sheets-completed", 0);
1665 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1668 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1669 attr
->values
[0].integer
= 0;
1671 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1673 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1674 IPP_TAG_KEYWORD
)) == NULL
)
1675 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1678 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1679 printer
->options
)) == NULL
)
1682 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1683 "job-hold-until", NULL
, val
);
1685 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1688 * Hold job until specified time...
1691 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
1693 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1694 job
->state_value
= IPP_JOB_HELD
;
1696 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
1698 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1700 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
1701 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1702 job
->state_value
= IPP_JOB_HELD
;
1706 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1707 job
->state_value
= IPP_JOB_PENDING
;
1709 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1712 if (!(printer
->type
& CUPS_PRINTER_REMOTE
) || Classification
)
1715 * Add job sheets options...
1718 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1719 IPP_TAG_ZERO
)) == NULL
)
1721 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1722 "Adding default job-sheets values \"%s,%s\"...",
1723 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1725 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1727 ippSetString(job
->attrs
, &attr
, 0, printer
->job_sheets
[0]);
1728 ippSetString(job
->attrs
, &attr
, 1, printer
->job_sheets
[1]);
1731 job
->job_sheets
= attr
;
1734 * Enforce classification level if set...
1739 cupsdLogMessage(CUPSD_LOG_INFO
,
1740 "Classification=\"%s\", ClassifyOverride=%d",
1741 Classification
? Classification
: "(null)",
1744 if (ClassifyOverride
)
1746 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1747 (attr
->num_values
== 1 ||
1748 !strcmp(attr
->values
[1].string
.text
, "none")))
1751 * Force the leading banner to have the classification on it...
1754 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1756 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1757 "job-sheets=\"%s,none\", "
1758 "job-originating-user-name=\"%s\"",
1759 Classification
, job
->username
);
1761 else if (attr
->num_values
== 2 &&
1762 strcmp(attr
->values
[0].string
.text
,
1763 attr
->values
[1].string
.text
) &&
1764 strcmp(attr
->values
[0].string
.text
, "none") &&
1765 strcmp(attr
->values
[1].string
.text
, "none"))
1768 * Can't put two different security markings on the same document!
1771 ippSetString(job
->attrs
, &attr
, 1, attr
->values
[0].string
.text
);
1773 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1774 "job-sheets=\"%s,%s\", "
1775 "job-originating-user-name=\"%s\"",
1776 attr
->values
[0].string
.text
,
1777 attr
->values
[1].string
.text
, job
->username
);
1779 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1780 strcmp(attr
->values
[0].string
.text
, "none") &&
1781 (attr
->num_values
== 1 ||
1782 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1783 strcmp(attr
->values
[1].string
.text
, "none"))))
1785 if (attr
->num_values
== 1)
1786 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1787 "CLASSIFICATION OVERRIDDEN "
1788 "job-sheets=\"%s\", "
1789 "job-originating-user-name=\"%s\"",
1790 attr
->values
[0].string
.text
, job
->username
);
1792 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1793 "CLASSIFICATION OVERRIDDEN "
1794 "job-sheets=\"%s,%s\",fffff "
1795 "job-originating-user-name=\"%s\"",
1796 attr
->values
[0].string
.text
,
1797 attr
->values
[1].string
.text
, job
->username
);
1800 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1801 (attr
->num_values
== 1 ||
1802 strcmp(attr
->values
[1].string
.text
, Classification
)))
1805 * Force the banner to have the classification on it...
1808 if (attr
->num_values
> 1 &&
1809 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1811 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1812 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1816 if (attr
->num_values
== 1 ||
1817 strcmp(attr
->values
[0].string
.text
, "none"))
1818 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1820 if (attr
->num_values
> 1 &&
1821 strcmp(attr
->values
[1].string
.text
, "none"))
1822 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1825 if (attr
->num_values
> 1)
1826 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1827 "CLASSIFICATION FORCED "
1828 "job-sheets=\"%s,%s\", "
1829 "job-originating-user-name=\"%s\"",
1830 attr
->values
[0].string
.text
,
1831 attr
->values
[1].string
.text
, job
->username
);
1833 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1834 "CLASSIFICATION FORCED "
1835 "job-sheets=\"%s\", "
1836 "job-originating-user-name=\"%s\"",
1837 Classification
, job
->username
);
1842 * See if we need to add the starting sheet...
1845 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1847 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1848 attr
->values
[0].string
.text
);
1850 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1852 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1853 "Aborting job because the start banner could not be "
1858 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1861 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1862 IPP_TAG_ZERO
)) != NULL
)
1863 job
->job_sheets
= attr
;
1866 * Fill in the response info...
1869 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
1870 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
1871 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1874 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1876 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1878 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_TEXT
, "job-state-message", NULL
, "");
1879 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
1880 NULL
, job
->reasons
->values
[0].string
.text
);
1882 con
->response
->request
.status
.status_code
= IPP_OK
;
1885 * Add any job subscriptions...
1888 add_job_subscriptions(con
, job
);
1891 * Set all but the first two attributes to the job attributes group...
1894 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1895 attr
->group_tag
= IPP_TAG_JOB
;
1898 * Fire the "job created" event...
1901 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1904 * Return the new job...
1912 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1916 add_job_subscriptions(
1917 cupsd_client_t
*con
, /* I - Client connection */
1918 cupsd_job_t
*job
) /* I - Newly created job */
1920 int i
; /* Looping var */
1921 ipp_attribute_t
*prev
, /* Previous attribute */
1922 *next
, /* Next attribute */
1923 *attr
; /* Current attribute */
1924 cupsd_subscription_t
*sub
; /* Subscription object */
1925 const char *recipient
, /* notify-recipient-uri */
1926 *pullmethod
; /* notify-pull-method */
1927 ipp_attribute_t
*user_data
; /* notify-user-data */
1928 int interval
; /* notify-time-interval */
1929 unsigned mask
; /* notify-events */
1933 * Find the first subscription group attribute; return if we have
1937 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
1938 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1945 * Process the subscription attributes in the request...
1954 mask
= CUPSD_EVENT_NONE
;
1956 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1958 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1959 attr
->value_tag
== IPP_TAG_URI
)
1962 * Validate the recipient scheme against the ServerBin/notifier
1966 char notifier
[1024], /* Notifier filename */
1967 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
1968 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
1969 host
[HTTP_MAX_URI
], /* Host portion of URI */
1970 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1971 int port
; /* Port portion of URI */
1974 recipient
= attr
->values
[0].string
.text
;
1976 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
1977 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
1978 host
, sizeof(host
), &port
,
1979 resource
, sizeof(resource
)) < HTTP_URI_OK
)
1981 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1982 _("Bad notify-recipient-uri \"%s\"."), recipient
);
1983 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
1984 "notify-status-code", IPP_URI_SCHEME
);
1988 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
1990 if (access(notifier
, X_OK
))
1992 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1993 _("notify-recipient-uri URI \"%s\" uses unknown "
1994 "scheme."), recipient
);
1995 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
1996 "notify-status-code", IPP_URI_SCHEME
);
2000 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2002 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2003 _("notify-recipient-uri URI \"%s\" is already used."),
2005 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2006 "notify-status-code", IPP_ATTRIBUTES
);
2010 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2011 attr
->value_tag
== IPP_TAG_KEYWORD
)
2013 pullmethod
= attr
->values
[0].string
.text
;
2015 if (strcmp(pullmethod
, "ippget"))
2017 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2018 _("Bad notify-pull-method \"%s\"."), pullmethod
);
2019 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2020 "notify-status-code", IPP_ATTRIBUTES
);
2024 else if (!strcmp(attr
->name
, "notify-charset") &&
2025 attr
->value_tag
== IPP_TAG_CHARSET
&&
2026 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2027 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2029 send_ipp_status(con
, IPP_CHARSET
,
2030 _("Character set \"%s\" not supported."),
2031 attr
->values
[0].string
.text
);
2034 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2035 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2036 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2038 send_ipp_status(con
, IPP_CHARSET
,
2039 _("Language \"%s\" not supported."),
2040 attr
->values
[0].string
.text
);
2043 else if (!strcmp(attr
->name
, "notify-user-data") &&
2044 attr
->value_tag
== IPP_TAG_STRING
)
2046 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2048 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2049 _("The notify-user-data value is too large "
2050 "(%d > 63 octets)."),
2051 attr
->values
[0].unknown
.length
);
2057 else if (!strcmp(attr
->name
, "notify-events") &&
2058 attr
->value_tag
== IPP_TAG_KEYWORD
)
2060 for (i
= 0; i
< attr
->num_values
; i
++)
2061 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2063 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2065 send_ipp_status(con
, IPP_BAD_REQUEST
,
2066 _("The notify-lease-duration attribute cannot be "
2067 "used with job subscriptions."));
2070 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2071 attr
->value_tag
== IPP_TAG_INTEGER
)
2072 interval
= attr
->values
[0].integer
;
2077 if (!recipient
&& !pullmethod
)
2080 if (mask
== CUPSD_EVENT_NONE
)
2081 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2083 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2084 recipient
, 0)) != NULL
)
2086 sub
->interval
= interval
;
2088 cupsdSetString(&sub
->owner
, job
->username
);
2092 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2093 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2094 (size_t)sub
->user_data_len
);
2097 ippAddSeparator(con
->response
);
2098 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2099 "notify-subscription-id", sub
->id
);
2101 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
2109 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2112 * Remove all of the subscription attributes from the job request...
2114 * TODO: Optimize this since subscription groups have to come at the
2115 * end of the request...
2118 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2122 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2123 attr
->group_tag
== IPP_TAG_ZERO
)
2126 * Free and remove this attribute...
2129 ippDeleteAttribute(NULL
, attr
);
2134 job
->attrs
->attrs
= next
;
2140 job
->attrs
->last
= prev
;
2141 job
->attrs
->current
= prev
;
2146 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2148 * See RFC 4122 for the definition of UUIDs and the format.
2152 add_job_uuid(cupsd_job_t
*job
) /* I - Job */
2154 char uuid
[64]; /* job-uuid string */
2158 * Add a job-uuid attribute if none exists...
2161 if (!ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2162 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
,
2163 httpAssembleUUID(ServerName
, RemotePort
, job
->dest
, job
->id
,
2164 uuid
, sizeof(uuid
)));
2169 * 'add_printer()' - Add a printer to the system.
2173 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2174 ipp_attribute_t
*uri
) /* I - URI of printer */
2176 http_status_t status
; /* Policy status */
2177 int i
; /* Looping var */
2178 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2179 username
[HTTP_MAX_URI
], /* Username portion of URI */
2180 host
[HTTP_MAX_URI
], /* Host portion of URI */
2181 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2182 int port
; /* Port portion of URI */
2183 cupsd_printer_t
*printer
; /* Printer/class */
2184 ipp_attribute_t
*attr
; /* Printer attribute */
2185 cups_file_t
*fp
; /* Script/PPD file */
2186 char line
[1024]; /* Line from file... */
2187 char srcfile
[1024], /* Source Script/PPD file */
2188 dstfile
[1024]; /* Destination Script/PPD file */
2189 int modify
; /* Non-zero if we are modifying */
2190 int changed_driver
, /* Changed the PPD? */
2191 need_restart_job
, /* Need to restart job? */
2192 set_device_uri
, /* Did we set the device URI? */
2193 set_port_monitor
; /* Did we set the port monitor? */
2196 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2197 con
->number
, uri
->values
[0].string
.text
);
2200 * Do we have a valid URI?
2203 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2204 sizeof(scheme
), username
, sizeof(username
), host
,
2205 sizeof(host
), &port
, resource
, sizeof(resource
));
2207 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2210 * No, return an error...
2213 send_ipp_status(con
, IPP_BAD_REQUEST
,
2214 _("The printer-uri must be of the form "
2215 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2220 * Do we have a valid printer name?
2223 if (!validate_name(resource
+ 10))
2226 * No, return an error...
2229 send_ipp_status(con
, IPP_BAD_REQUEST
,
2230 _("The printer-uri \"%s\" contains invalid characters."),
2231 uri
->values
[0].string
.text
);
2236 * See if the printer already exists; if not, create a new printer...
2239 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2242 * Printer doesn't exist; see if we have a class of the same name...
2245 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
)
2248 * Yes, return an error...
2251 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2252 _("A class named \"%s\" already exists."),
2258 * No, check the default policy then add the printer...
2261 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2263 send_http_error(con
, status
, NULL
);
2267 printer
= cupsdAddPrinter(resource
+ 10);
2270 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2273 send_http_error(con
, status
, printer
);
2280 * Look for attributes and copy them over as needed...
2284 need_restart_job
= 0;
2286 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2287 IPP_TAG_TEXT
)) != NULL
)
2288 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2290 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
2291 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
2293 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
2294 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
2296 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
2297 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
2299 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2300 IPP_TAG_TEXT
)) != NULL
)
2301 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2305 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2306 IPP_TAG_URI
)) != NULL
)
2309 * Do we have a valid device URI?
2312 http_uri_status_t uri_status
; /* URI separation status */
2313 char old_device_uri
[1024];
2314 /* Old device URI */
2315 static const char * const uri_status_strings
[] =
2318 "Bad arguments to function.",
2319 "Bad resource path.",
2321 "Bad hostname/address.",
2322 "Bad username/password.",
2326 "Missing URI scheme.",
2327 "Unknown URI scheme",
2328 "Missing resource path."
2332 need_restart_job
= 1;
2334 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2335 attr
->values
[0].string
.text
,
2336 scheme
, sizeof(scheme
),
2337 username
, sizeof(username
),
2338 host
, sizeof(host
), &port
,
2339 resource
, sizeof(resource
));
2341 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2342 "%s device-uri: %s", printer
->name
,
2343 uri_status_strings
[uri_status
- HTTP_URI_STATUS_OVERFLOW
]);
2345 if (uri_status
< HTTP_URI_OK
)
2347 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"."),
2348 attr
->values
[0].string
.text
);
2352 if (!strcmp(scheme
, "file"))
2355 * See if the administrator has enabled file devices...
2358 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2361 * File devices are disabled and the URL is not file:/dev/null...
2364 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2365 _("File device URIs have been disabled. "
2366 "To enable, see the FileDevice directive in "
2367 "\"%s/cups-files.conf\"."),
2375 * See if the backend exists and is executable...
2378 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2379 if (access(srcfile
, X_OK
))
2382 * Could not find device in list!
2385 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2386 _("Bad device-uri scheme \"%s\"."), scheme
);
2391 if (printer
->sanitized_device_uri
)
2392 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2393 sizeof(old_device_uri
));
2395 old_device_uri
[0] = '\0';
2397 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2399 cupsdLogMessage(CUPSD_LOG_INFO
,
2400 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2401 printer
->name
, printer
->sanitized_device_uri
,
2407 set_port_monitor
= 0;
2409 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2410 IPP_TAG_NAME
)) != NULL
)
2412 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2415 need_restart_job
= 1;
2417 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2421 for (i
= 0; i
< supported
->num_values
; i
++)
2422 if (!strcmp(supported
->values
[i
].string
.text
,
2423 attr
->values
[0].string
.text
))
2427 if (!supported
|| i
>= supported
->num_values
)
2429 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"."),
2430 attr
->values
[0].string
.text
);
2434 cupsdLogMessage(CUPSD_LOG_INFO
,
2435 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2436 printer
->name
, attr
->values
[0].string
.text
,
2437 printer
->port_monitor
? printer
->port_monitor
: "none");
2439 if (strcmp(attr
->values
[0].string
.text
, "none"))
2440 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2442 cupsdClearString(&printer
->port_monitor
);
2444 set_port_monitor
= 1;
2447 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2448 IPP_TAG_BOOLEAN
)) != NULL
&&
2449 attr
->values
[0].boolean
!= printer
->accepting
)
2451 cupsdLogMessage(CUPSD_LOG_INFO
,
2452 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2453 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2455 printer
->accepting
= attr
->values
[0].boolean
;
2457 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2458 "%s accepting jobs.",
2459 printer
->accepting
? "Now" : "No longer");
2462 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2463 IPP_TAG_BOOLEAN
)) != NULL
)
2465 if (attr
->values
[0].boolean
&&
2466 printer
->num_auth_info_required
== 1 &&
2467 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2469 send_ipp_status(con
, IPP_BAD_REQUEST
,
2470 _("Cannot share a remote Kerberized printer."));
2474 if (printer
->shared
&& !attr
->values
[0].boolean
)
2475 cupsdDeregisterPrinter(printer
, 1);
2477 cupsdLogMessage(CUPSD_LOG_INFO
,
2478 "Setting %s printer-is-shared to %d (was %d.)",
2479 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2481 printer
->shared
= attr
->values
[0].boolean
;
2484 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2485 IPP_TAG_ENUM
)) != NULL
)
2487 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2488 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2490 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d."),
2491 attr
->values
[0].integer
);
2495 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2496 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2498 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2499 cupsdStopPrinter(printer
, 0);
2502 need_restart_job
= 1;
2503 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2507 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2508 IPP_TAG_TEXT
)) != NULL
)
2510 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2511 sizeof(printer
->state_message
));
2513 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
, "%s",
2514 printer
->state_message
);
2517 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2518 IPP_TAG_KEYWORD
)) != NULL
)
2520 if (attr
->num_values
>
2521 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2523 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2524 _("Too many printer-state-reasons values (%d > %d)."),
2526 (int)(sizeof(printer
->reasons
) /
2527 sizeof(printer
->reasons
[0])));
2531 for (i
= 0; i
< printer
->num_reasons
; i
++)
2532 _cupsStrFree(printer
->reasons
[i
]);
2534 printer
->num_reasons
= 0;
2535 for (i
= 0; i
< attr
->num_values
; i
++)
2537 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2540 printer
->reasons
[printer
->num_reasons
] =
2541 _cupsStrRetain(attr
->values
[i
].string
.text
);
2542 printer
->num_reasons
++;
2544 if (!strcmp(attr
->values
[i
].string
.text
, "paused") &&
2545 printer
->state
!= IPP_PRINTER_STOPPED
)
2547 cupsdLogMessage(CUPSD_LOG_INFO
,
2548 "Setting %s printer-state to %d (was %d.)",
2549 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2550 cupsdStopPrinter(printer
, 0);
2554 if (PrintcapFormat
== PRINTCAP_PLIST
)
2555 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2557 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2558 "Printer \"%s\" state changed.", printer
->name
);
2561 set_printer_defaults(con
, printer
);
2563 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2564 IPP_TAG_KEYWORD
)) != NULL
)
2565 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2568 * See if we have all required attributes...
2571 if (!printer
->device_uri
)
2572 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2575 * See if we have a PPD file attached to the request...
2580 need_restart_job
= 1;
2583 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2585 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2588 * Yes; get the first line from it...
2592 cupsFileGets(fp
, line
, sizeof(line
));
2596 * Then see what kind of file it is...
2599 if (strncmp(line
, "*PPD-Adobe", 10))
2601 send_ipp_status(con
, IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED
, _("Bad PPD file."));
2605 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2609 * The new file is a PPD file, so move the file over to the ppd
2613 if (copy_file(srcfile
, dstfile
, ConfigFilePerm
))
2615 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file - %s"), strerror(errno
));
2619 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2622 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
)) != NULL
)
2624 const char *ppd_name
= ippGetString(attr
, 0, NULL
);
2625 /* ppd-name value */
2627 need_restart_job
= 1;
2630 if (!strcmp(ppd_name
, "raw"))
2633 * Raw driver, remove any existing PPD file.
2636 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2639 else if (strstr(ppd_name
, "../"))
2641 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
2650 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
, printer
->name
);
2652 if (copy_model(con
, ppd_name
, dstfile
))
2654 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file."));
2658 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Copied PPD file successfully");
2665 * If we changed the PPD, then remove the printer's cache file and clear the
2666 * printer-state-reasons...
2669 char cache_name
[1024]; /* Cache filename for printer attrs */
2671 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.data", CacheDir
,
2675 cupsdSetPrinterReasons(printer
, "none");
2678 * (Re)register color profiles...
2681 cupsdRegisterColor(printer
);
2685 * If we set the device URI but not the port monitor, check which port
2686 * monitor to use by default...
2689 if (set_device_uri
&& !set_port_monitor
)
2691 ppd_file_t
*ppd
; /* PPD file */
2692 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2695 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2696 sizeof(scheme
), username
, sizeof(username
), host
,
2697 sizeof(host
), &port
, resource
, sizeof(resource
));
2699 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2701 if ((ppd
= _ppdOpenFile(srcfile
, _PPD_LOCALIZATION_NONE
)) != NULL
)
2703 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2705 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2706 if (!strcmp(scheme
, ppdattr
->spec
))
2708 cupsdLogMessage(CUPSD_LOG_INFO
,
2709 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2710 printer
->name
, ppdattr
->value
,
2711 printer
->port_monitor
? printer
->port_monitor
2714 if (strcmp(ppdattr
->value
, "none"))
2715 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2717 cupsdClearString(&printer
->port_monitor
);
2726 printer
->config_time
= time(NULL
);
2729 * Update the printer attributes and return...
2732 cupsdSetPrinterAttrs(printer
);
2733 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2735 if (need_restart_job
&& printer
->job
)
2738 * Restart the current job...
2741 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
2742 "Job restarted because the printer was modified.");
2745 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2749 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
2750 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
2751 printer
->name
, get_username(con
));
2753 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2754 printer
->name
, get_username(con
));
2758 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
2759 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
2760 printer
->name
, get_username(con
));
2762 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2763 printer
->name
, get_username(con
));
2766 con
->response
->request
.status
.status_code
= IPP_OK
;
2771 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2772 * based upon the printer state...
2776 add_printer_state_reasons(
2777 cupsd_client_t
*con
, /* I - Client connection */
2778 cupsd_printer_t
*p
) /* I - Printer info */
2780 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2781 "add_printer_state_reasons(%p[%d], %p[%s])",
2782 con
, con
->number
, p
, p
->name
);
2784 if (p
->num_reasons
== 0)
2785 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2786 "printer-state-reasons", NULL
, "none");
2788 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2789 "printer-state-reasons", p
->num_reasons
, NULL
,
2790 (const char * const *)p
->reasons
);
2795 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2796 * the specified printer or class.
2800 add_queued_job_count(
2801 cupsd_client_t
*con
, /* I - Client connection */
2802 cupsd_printer_t
*p
) /* I - Printer or class */
2804 int count
; /* Number of jobs on destination */
2807 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2808 con
, con
->number
, p
, p
->name
);
2810 count
= cupsdGetPrinterJobCount(p
->name
);
2812 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2813 "queued-job-count", count
);
2818 * 'apply_printer_defaults()' - Apply printer default options to a job.
2822 apply_printer_defaults(
2823 cupsd_printer_t
*printer
, /* I - Printer */
2824 cupsd_job_t
*job
) /* I - Job */
2826 int i
, /* Looping var */
2827 num_options
; /* Number of default options */
2828 cups_option_t
*options
, /* Default options */
2829 *option
; /* Current option */
2833 * Collect all of the default options and add the missing ones to the
2837 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
2838 option
= printer
->options
;
2841 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2843 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2848 * Encode these options as attributes in the job object...
2851 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2852 cupsFreeOptions(num_options
, options
);
2857 * 'authenticate_job()' - Set job authentication info.
2861 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2862 ipp_attribute_t
*uri
) /* I - Job URI */
2864 ipp_attribute_t
*attr
, /* job-id attribute */
2865 *auth_info
; /* auth-info attribute */
2866 int jobid
; /* Job ID */
2867 cupsd_job_t
*job
; /* Current job */
2868 char scheme
[HTTP_MAX_URI
],
2869 /* Method portion of URI */
2870 username
[HTTP_MAX_URI
],
2871 /* Username portion of URI */
2873 /* Host portion of URI */
2874 resource
[HTTP_MAX_URI
];
2875 /* Resource portion of URI */
2876 int port
; /* Port portion of URI */
2879 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2880 con
, con
->number
, uri
->values
[0].string
.text
);
2883 * Start with "everything is OK" status...
2886 con
->response
->request
.status
.status_code
= IPP_OK
;
2889 * See if we have a job URI or a printer URI...
2892 if (!strcmp(uri
->name
, "printer-uri"))
2895 * Got a printer URI; see if we also have a job-id attribute...
2898 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2899 IPP_TAG_INTEGER
)) == NULL
)
2901 send_ipp_status(con
, IPP_BAD_REQUEST
,
2902 _("Got a printer-uri attribute but no job-id."));
2906 jobid
= attr
->values
[0].integer
;
2911 * Got a job URI; parse it to get the job ID...
2914 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2915 sizeof(scheme
), username
, sizeof(username
), host
,
2916 sizeof(host
), &port
, resource
, sizeof(resource
));
2918 if (strncmp(resource
, "/jobs/", 6))
2924 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
2925 uri
->values
[0].string
.text
);
2929 jobid
= atoi(resource
+ 6);
2933 * See if the job exists...
2936 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2939 * Nope - return a "not found" error...
2942 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
2947 * See if the job has been completed...
2950 if (job
->state_value
!= IPP_JOB_HELD
)
2953 * Return a "not-possible" error...
2956 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2957 _("Job #%d is not held for authentication."),
2963 * See if we have already authenticated...
2966 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2968 if (!con
->username
[0] && !auth_info
)
2970 cupsd_printer_t
*printer
; /* Job destination */
2973 * No auth data. If we need to authenticate via Kerberos, send a
2974 * HTTP auth challenge, otherwise just return an IPP error...
2977 printer
= cupsdFindDest(job
->dest
);
2979 if (printer
&& printer
->num_auth_info_required
> 0 &&
2980 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2981 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
2983 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
2984 _("No authentication information provided."));
2989 * See if the job is owned by the requesting user...
2992 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
2994 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
2995 cupsdFindDest(job
->dest
));
3000 * Save the authentication information for this job...
3003 save_auth_info(con
, job
, auth_info
);
3006 * Reset the job-hold-until value to "no-hold"...
3009 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3010 IPP_TAG_KEYWORD
)) == NULL
)
3011 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3015 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
3016 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
3020 * Release the job and return...
3023 cupsdReleaseJob(job
);
3025 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, NULL
, job
, "Job authenticated by user");
3027 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3034 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3038 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3039 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3041 int i
; /* Looping var */
3042 http_status_t status
; /* Policy status */
3043 cups_ptype_t dtype
; /* Destination type */
3044 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3045 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3046 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3047 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3048 int port
; /* Port portion of URI */
3049 ipp_attribute_t
*attr
; /* Attribute in request */
3050 const char *username
= NULL
; /* Username */
3051 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3053 cupsd_printer_t
*printer
; /* Printer */
3054 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3055 cupsd_job_t
*job
; /* Job */
3058 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3059 con
->number
, uri
->values
[0].string
.text
);
3062 * Get the jobs to cancel/purge...
3065 switch (con
->request
->request
.op
.operation_id
)
3067 case IPP_PURGE_JOBS
:
3069 * Get the username (if any) for the jobs we want to cancel (only if
3070 * "my-jobs" is specified...
3073 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3074 IPP_TAG_BOOLEAN
)) != NULL
&&
3075 attr
->values
[0].boolean
)
3077 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3078 IPP_TAG_NAME
)) != NULL
)
3079 username
= attr
->values
[0].string
.text
;
3082 send_ipp_status(con
, IPP_BAD_REQUEST
,
3083 _("Missing requesting-user-name attribute."));
3089 * Look for the "purge-jobs" attribute...
3092 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3093 IPP_TAG_BOOLEAN
)) != NULL
)
3094 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3096 purge
= CUPSD_JOB_PURGE
;
3099 case IPP_CANCEL_MY_JOBS
:
3100 if (con
->username
[0])
3101 username
= con
->username
;
3102 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3103 IPP_TAG_NAME
)) != NULL
)
3104 username
= attr
->values
[0].string
.text
;
3107 send_ipp_status(con
, IPP_BAD_REQUEST
,
3108 _("Missing requesting-user-name attribute."));
3116 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3119 * See if we have a printer URI...
3122 if (strcmp(uri
->name
, "printer-uri"))
3124 send_ipp_status(con
, IPP_BAD_REQUEST
,
3125 _("The printer-uri attribute is required."));
3130 * And if the destination is valid...
3133 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3139 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3140 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3141 hostname
, sizeof(hostname
), &port
,
3142 resource
, sizeof(resource
));
3144 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3145 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3147 send_ipp_status(con
, IPP_NOT_FOUND
,
3148 _("The printer or class does not exist."));
3156 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3158 send_http_error(con
, status
, NULL
);
3164 for (i
= 0; i
< job_ids
->num_values
; i
++)
3166 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
3169 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3170 _cups_strcasecmp(job
->username
, username
))
3174 if (i
< job_ids
->num_values
)
3176 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3177 job_ids
->values
[i
].integer
);
3181 for (i
= 0; i
< job_ids
->num_values
; i
++)
3183 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3185 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3186 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3187 "Job canceled by user.");
3190 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3191 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3197 * Cancel all jobs on all printers...
3200 cupsdCancelJobs(NULL
, username
, purge
);
3202 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3203 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3213 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3216 send_http_error(con
, status
, printer
);
3222 for (i
= 0; i
< job_ids
->num_values
; i
++)
3224 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
3225 _cups_strcasecmp(job
->dest
, printer
->name
))
3228 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3229 _cups_strcasecmp(job
->username
, username
))
3233 if (i
< job_ids
->num_values
)
3235 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3236 job_ids
->values
[i
].integer
);
3240 for (i
= 0; i
< job_ids
->num_values
; i
++)
3242 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3244 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3245 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3246 "Job canceled by user.");
3249 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3250 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3256 * Cancel all of the jobs on the named printer...
3259 cupsdCancelJobs(printer
->name
, username
, purge
);
3261 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3263 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3268 con
->response
->request
.status
.status_code
= IPP_OK
;
3275 * 'cancel_job()' - Cancel a print job.
3279 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3280 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3282 ipp_attribute_t
*attr
; /* Current attribute */
3283 int jobid
; /* Job ID */
3284 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3285 username
[HTTP_MAX_URI
], /* Username portion of URI */
3286 host
[HTTP_MAX_URI
], /* Host portion of URI */
3287 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3288 int port
; /* Port portion of URI */
3289 cupsd_job_t
*job
; /* Job information */
3290 cups_ptype_t dtype
; /* Destination type (printer/class) */
3291 cupsd_printer_t
*printer
; /* Printer data */
3292 cupsd_jobaction_t purge
; /* Purge the job? */
3295 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3296 con
->number
, uri
->values
[0].string
.text
);
3299 * See if we have a job URI or a printer URI...
3302 if (!strcmp(uri
->name
, "printer-uri"))
3305 * Got a printer URI; see if we also have a job-id attribute...
3308 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3309 IPP_TAG_INTEGER
)) == NULL
)
3311 send_ipp_status(con
, IPP_BAD_REQUEST
,
3312 _("Got a printer-uri attribute but no job-id."));
3316 if ((jobid
= attr
->values
[0].integer
) == 0)
3319 * Find the current job on the specified printer...
3322 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3328 send_ipp_status(con
, IPP_NOT_FOUND
,
3329 _("The printer or class does not exist."));
3334 * See if there are any pending jobs...
3337 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3339 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3340 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3341 !_cups_strcasecmp(job
->dest
, printer
->name
))
3349 * No, try stopped jobs...
3352 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3354 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3355 if (job
->state_value
== IPP_JOB_STOPPED
&&
3356 !_cups_strcasecmp(job
->dest
, printer
->name
))
3363 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3373 * Got a job URI; parse it to get the job ID...
3376 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3377 sizeof(scheme
), username
, sizeof(username
), host
,
3378 sizeof(host
), &port
, resource
, sizeof(resource
));
3380 if (strncmp(resource
, "/jobs/", 6))
3386 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3387 uri
->values
[0].string
.text
);
3391 jobid
= atoi(resource
+ 6);
3395 * Look for the "purge-job" attribute...
3398 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3399 IPP_TAG_BOOLEAN
)) != NULL
)
3400 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3402 purge
= CUPSD_JOB_DEFAULT
;
3405 * See if the job exists...
3408 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3411 * Nope - return a "not found" error...
3414 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3419 * See if the job is owned by the requesting user...
3422 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3424 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3425 cupsdFindDest(job
->dest
));
3430 * See if the job is already completed, canceled, or aborted; if so,
3431 * we can't cancel...
3434 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3436 switch (job
->state_value
)
3438 case IPP_JOB_CANCELED
:
3439 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3440 _("Job #%d is already canceled - can\'t cancel."),
3444 case IPP_JOB_ABORTED
:
3445 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3446 _("Job #%d is already aborted - can\'t cancel."),
3451 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3452 _("Job #%d is already completed - can\'t cancel."),
3461 * Cancel the job and return...
3464 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3465 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3466 "Job canceled by \"%s\"",
3470 if (purge
== CUPSD_JOB_PURGE
)
3471 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3474 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3477 con
->response
->request
.status
.status_code
= IPP_OK
;
3482 * 'cancel_subscription()' - Cancel a subscription.
3486 cancel_subscription(
3487 cupsd_client_t
*con
, /* I - Client connection */
3488 int sub_id
) /* I - Subscription ID */
3490 http_status_t status
; /* Policy status */
3491 cupsd_subscription_t
*sub
; /* Subscription */
3494 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3495 "cancel_subscription(con=%p[%d], sub_id=%d)",
3496 con
, con
->number
, sub_id
);
3499 * Is the subscription ID valid?
3502 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3505 * Bad subscription ID...
3508 send_ipp_status(con
, IPP_NOT_FOUND
,
3509 _("Subscription #%d does not exist."), sub_id
);
3517 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3519 con
, sub
->owner
)) != HTTP_OK
)
3521 send_http_error(con
, status
, sub
->dest
);
3526 * Cancel the subscription...
3529 cupsdDeleteSubscription(sub
, 1);
3531 con
->response
->request
.status
.status_code
= IPP_OK
;
3536 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3539 static int /* O - 1 if OK, 0 if not */
3540 check_rss_recipient(
3541 const char *recipient
) /* I - Recipient URI */
3543 cupsd_subscription_t
*sub
; /* Current subscription */
3546 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3548 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3552 * Compare the URIs up to the first ?...
3555 const char *r1
, *r2
;
3557 for (r1
= recipient
, r2
= sub
->recipient
;
3558 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3570 * 'check_quotas()' - Check quotas for a printer and user.
3573 static int /* O - 1 if OK, 0 if forbidden,
3574 -1 if limit reached */
3575 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3576 cupsd_printer_t
*p
) /* I - Printer or class */
3578 char username
[33], /* Username */
3579 *name
; /* Current user name */
3580 cupsd_quota_t
*q
; /* Quota data */
3581 #ifdef HAVE_MBR_UID_TO_UUID
3583 * Use Apple membership APIs which require that all names represent
3584 * valid user account or group records accessible by the server.
3587 uuid_t usr_uuid
; /* UUID for job requesting user */
3588 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3589 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3590 int mbr_err
; /* Error from membership function */
3591 int is_member
; /* Is this user a member? */
3594 * Use standard POSIX APIs for checking users and groups...
3597 struct passwd
*pw
; /* User password data */
3598 #endif /* HAVE_MBR_UID_TO_UUID */
3601 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3602 con
, con
->number
, p
, p
->name
);
3605 * Figure out who is printing...
3608 strlcpy(username
, get_username(con
), sizeof(username
));
3610 if ((name
= strchr(username
, '@')) != NULL
)
3611 *name
= '\0'; /* Strip @REALM */
3614 * Check global active job limits for printers and users...
3617 if (MaxJobsPerPrinter
)
3620 * Check if there are too many pending jobs on this printer...
3623 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3625 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3634 * Check if there are too many pending jobs for this user...
3637 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3639 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3646 * Check against users...
3649 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3652 if (cupsArrayCount(p
->users
))
3654 #ifdef HAVE_MBR_UID_TO_UUID
3656 * Get UUID for job requesting user...
3659 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3665 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3666 "check_quotas: UUID lookup failed for user \"%s\"",
3668 cupsdLogMessage(CUPSD_LOG_INFO
,
3669 "Denying user \"%s\" access to printer \"%s\" "
3670 "(unknown user)...",
3676 * Get UID and GID of requesting user...
3679 pw
= getpwnam(username
);
3681 #endif /* HAVE_MBR_UID_TO_UUID */
3683 for (name
= (char *)cupsArrayFirst(p
->users
);
3685 name
= (char *)cupsArrayNext(p
->users
))
3689 * Check group membership...
3692 #ifdef HAVE_MBR_UID_TO_UUID
3695 if (uuid_parse(name
+ 2, grp_uuid
))
3696 uuid_clear(grp_uuid
);
3698 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3701 * Invalid ACL entries are ignored for matching; just record a
3702 * warning in the log...
3705 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3706 "check_quotas: UUID lookup failed for ACL entry "
3707 "\"%s\" (err=%d)", name
, mbr_err
);
3708 cupsdLogMessage(CUPSD_LOG_WARN
,
3709 "Access control entry \"%s\" not a valid group name; "
3710 "entry ignored", name
);
3713 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3717 * At this point, there should be no errors, but check anyways...
3720 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3721 "check_quotas: group \"%s\" membership check "
3722 "failed (err=%d)", name
+ 1, mbr_err
);
3727 * Stop if we found a match...
3734 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3736 #endif /* HAVE_MBR_UID_TO_UUID */
3738 #ifdef HAVE_MBR_UID_TO_UUID
3743 if (uuid_parse(name
+ 1, usr2_uuid
))
3744 uuid_clear(usr2_uuid
);
3746 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3749 * Invalid ACL entries are ignored for matching; just record a
3750 * warning in the log...
3753 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3754 "check_quotas: UUID lookup failed for ACL entry "
3755 "\"%s\" (err=%d)", name
, mbr_err
);
3756 cupsdLogMessage(CUPSD_LOG_WARN
,
3757 "Access control entry \"%s\" not a valid user name; "
3758 "entry ignored", name
);
3761 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3765 else if (!_cups_strcasecmp(username
, name
))
3767 #endif /* HAVE_MBR_UID_TO_UUID */
3769 if ((name
!= NULL
) == p
->deny_users
)
3771 cupsdLogMessage(CUPSD_LOG_INFO
,
3772 "Denying user \"%s\" access to printer \"%s\"...",
3782 if (p
->k_limit
|| p
->page_limit
)
3784 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3786 cupsdLogMessage(CUPSD_LOG_ERROR
,
3787 "Unable to allocate quota data for user \"%s\"",
3792 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3793 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3795 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3802 * If we have gotten this far, we're done!
3810 * 'close_job()' - Close a multi-file job.
3814 close_job(cupsd_client_t
*con
, /* I - Client connection */
3815 ipp_attribute_t
*uri
) /* I - Printer URI */
3817 cupsd_job_t
*job
; /* Job */
3818 ipp_attribute_t
*attr
; /* Attribute */
3819 char job_uri
[HTTP_MAX_URI
],
3821 username
[256]; /* User name */
3824 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3825 con
->number
, uri
->values
[0].string
.text
);
3828 * See if we have a job URI or a printer URI...
3831 if (strcmp(uri
->name
, "printer-uri"))
3834 * job-uri is not supported by Close-Job!
3837 send_ipp_status(con
, IPP_BAD_REQUEST
,
3838 _("Close-Job doesn't support the job-uri attribute."));
3843 * Got a printer URI; see if we also have a job-id attribute...
3846 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3847 IPP_TAG_INTEGER
)) == NULL
)
3849 send_ipp_status(con
, IPP_BAD_REQUEST
,
3850 _("Got a printer-uri attribute but no job-id."));
3854 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
3857 * Nope - return a "not found" error...
3860 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3861 attr
->values
[0].integer
);
3866 * See if the job is owned by the requesting user...
3869 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3871 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3872 cupsdFindDest(job
->dest
));
3877 * Add any ending sheet...
3880 if (cupsdTimeoutJob(job
))
3883 if (job
->state_value
== IPP_JOB_STOPPED
)
3885 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3886 job
->state_value
= IPP_JOB_PENDING
;
3888 else if (job
->state_value
== IPP_JOB_HELD
)
3890 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3891 IPP_TAG_KEYWORD
)) == NULL
)
3892 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3894 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
3896 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3897 job
->state_value
= IPP_JOB_PENDING
;
3902 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3905 * Fill in the response info...
3908 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
3909 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
3910 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
3913 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
3915 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
3918 con
->response
->request
.status
.status_code
= IPP_OK
;
3921 * Start the job if necessary...
3929 * 'copy_attrs()' - Copy attributes from one request to another.
3933 copy_attrs(ipp_t
*to
, /* I - Destination request */
3934 ipp_t
*from
, /* I - Source request */
3935 cups_array_t
*ra
, /* I - Requested attributes */
3936 ipp_tag_t group
, /* I - Group to copy */
3937 int quickcopy
, /* I - Do a quick copy? */
3938 cups_array_t
*exclude
) /* I - Attributes to exclude? */
3940 ipp_attribute_t
*fromattr
; /* Source attribute */
3943 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3944 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3945 to
, from
, ra
, group
, quickcopy
);
3950 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3953 * Filter attributes as needed...
3956 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3957 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3960 if (!strcmp(fromattr
->name
, "document-password") ||
3961 !strcmp(fromattr
->name
, "job-authorization-uri") ||
3962 !strcmp(fromattr
->name
, "job-password") ||
3963 !strcmp(fromattr
->name
, "job-password-encryption") ||
3964 !strcmp(fromattr
->name
, "job-printer-uri"))
3968 (cupsArrayFind(exclude
, fromattr
->name
) ||
3969 cupsArrayFind(exclude
, "all")))
3972 * We need to exclude this attribute for security reasons; we require the
3973 * job-id attribute regardless of the security settings for IPP
3976 * The job-printer-uri attribute is handled by copy_job_attrs().
3978 * Subscription attribute security is handled by copy_subscription_attrs().
3981 if (strcmp(fromattr
->name
, "job-id"))
3985 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
3988 * Don't send collection attributes by default to IPP/1.x clients
3989 * since many do not support collections. Also don't send
3990 * media-col-database unless specifically requested by the client.
3993 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
3995 (to
->request
.status
.version
[0] == 1 ||
3996 !strcmp(fromattr
->name
, "media-col-database")))
3999 ippCopyAttribute(to
, fromattr
, quickcopy
);
4006 * 'copy_banner()' - Copy a banner file to the requests directory for the
4010 static int /* O - Size of banner file in kbytes */
4011 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4012 cupsd_job_t
*job
, /* I - Job information */
4013 const char *name
) /* I - Name of banner */
4015 int i
; /* Looping var */
4016 int kbytes
; /* Size of banner file in kbytes */
4017 char filename
[1024]; /* Job filename */
4018 cupsd_banner_t
*banner
; /* Pointer to banner */
4019 cups_file_t
*in
; /* Input file */
4020 cups_file_t
*out
; /* Output file */
4021 int ch
; /* Character from file */
4022 char attrname
[255], /* Name of attribute */
4023 *s
; /* Pointer into name */
4024 ipp_attribute_t
*attr
; /* Attribute */
4027 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4028 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4029 con
, con
? con
->number
: -1, job
, job
->id
,
4030 name
? name
: "(null)");
4033 * Find the banner; return if not found or "none"...
4036 if (!name
|| !strcmp(name
, "none") ||
4037 (banner
= cupsdFindBanner(name
)) == NULL
)
4041 * Open the banner and job files...
4044 if (add_file(con
, job
, banner
->filetype
, 0))
4047 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4049 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4051 cupsdLogMessage(CUPSD_LOG_ERROR
,
4052 "Unable to create banner job file %s - %s",
4053 filename
, strerror(errno
));
4058 fchmod(cupsFileNumber(out
), 0640);
4059 fchown(cupsFileNumber(out
), RunUser
, Group
);
4062 * Try the localized banner file under the subdirectory...
4065 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4067 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4070 * Convert ll-cc to ll_CC...
4074 attrname
[3] = (char)toupper(attrname
[3] & 255);
4075 attrname
[4] = (char)toupper(attrname
[4] & 255);
4078 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4081 if (access(filename
, 0) && strlen(attrname
) > 2)
4084 * Wasn't able to find "ll_CC" locale file; try the non-national
4085 * localization banner directory.
4090 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4094 if (access(filename
, 0))
4097 * Use the non-localized banner file.
4100 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4103 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4107 cupsdLogMessage(CUPSD_LOG_ERROR
,
4108 "Unable to open banner template file %s - %s",
4109 filename
, strerror(errno
));
4115 * Parse the file to the end...
4118 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4122 * Get an attribute name...
4125 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4126 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4128 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4138 * Ignore { followed by stuff that is not an attribute name...
4141 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4146 * See if it is defined...
4149 if (attrname
[0] == '?')
4154 if (!strcmp(s
, "printer-name"))
4156 cupsFilePuts(out
, job
->dest
);
4159 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4162 * See if we have a leading question mark...
4165 if (attrname
[0] != '?')
4168 * Nope, write to file as-is; probably a PostScript procedure...
4171 cupsFilePrintf(out
, "{%s}", attrname
);
4178 * Output value(s)...
4181 for (i
= 0; i
< attr
->num_values
; i
++)
4184 cupsFilePutChar(out
, ',');
4186 switch (attr
->value_tag
)
4188 case IPP_TAG_INTEGER
:
4190 if (!strncmp(s
, "time-at-", 8))
4192 struct timeval tv
; /* Time value */
4194 tv
.tv_sec
= attr
->values
[i
].integer
;
4197 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4200 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4203 case IPP_TAG_BOOLEAN
:
4204 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4207 case IPP_TAG_NOVALUE
:
4208 cupsFilePuts(out
, "novalue");
4211 case IPP_TAG_RANGE
:
4212 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4213 attr
->values
[i
].range
.upper
);
4216 case IPP_TAG_RESOLUTION
:
4217 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4218 attr
->values
[i
].resolution
.yres
,
4219 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4224 case IPP_TAG_STRING
:
4227 case IPP_TAG_KEYWORD
:
4228 case IPP_TAG_CHARSET
:
4229 case IPP_TAG_LANGUAGE
:
4230 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4233 * Need to quote strings for PS banners...
4238 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4240 if (*p
== '(' || *p
== ')' || *p
== '\\')
4242 cupsFilePutChar(out
, '\\');
4243 cupsFilePutChar(out
, *p
);
4245 else if (*p
< 32 || *p
> 126)
4246 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4248 cupsFilePutChar(out
, *p
);
4252 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4256 break; /* anti-compiler-warning-code */
4260 else if (ch
== '\\') /* Quoted char */
4262 ch
= cupsFileGetChar(in
);
4264 if (ch
!= '{') /* Only do special handling for \{ */
4265 cupsFilePutChar(out
, '\\');
4267 cupsFilePutChar(out
, ch
);
4270 cupsFilePutChar(out
, ch
);
4274 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4276 job
->koctets
+= kbytes
;
4278 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4279 attr
->values
[0].integer
+= kbytes
;
4288 * 'copy_file()' - Copy a PPD file...
4291 static int /* O - 0 = success, -1 = error */
4292 copy_file(const char *from
, /* I - Source file */
4293 const char *to
, /* I - Destination file */
4294 mode_t mode
) /* I - Permissions */
4296 cups_file_t
*src
, /* Source file */
4297 *dst
; /* Destination file */
4298 int bytes
; /* Bytes to read/write */
4299 char buffer
[2048]; /* Copy buffer */
4302 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4305 * Open the source and destination file for a copy...
4308 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4311 if ((dst
= cupsdCreateConfFile(to
, mode
)) == NULL
)
4318 * Copy the source file to the destination...
4321 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4322 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4330 * Close both files and return...
4335 return (cupsdCloseCreatedConfFile(dst
, to
));
4340 * 'copy_model()' - Copy a PPD model file, substituting default values
4344 static int /* O - 0 = success, -1 = error */
4345 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4346 const char *from
, /* I - Source file */
4347 const char *to
) /* I - Destination file */
4349 fd_set input
; /* select() input set */
4350 struct timeval timeout
; /* select() timeout */
4351 int maxfd
; /* Max file descriptor for select() */
4352 char tempfile
[1024]; /* Temporary PPD file */
4353 int tempfd
; /* Temporary PPD file descriptor */
4354 int temppid
; /* Process ID of cups-driverd */
4355 int temppipe
[2]; /* Temporary pipes */
4356 char *argv
[4], /* Command-line arguments */
4357 *envp
[MAX_ENV
]; /* Environment */
4358 cups_file_t
*src
, /* Source file */
4359 *dst
; /* Destination file */
4360 ppd_file_t
*ppd
; /* PPD file */
4361 int bytes
, /* Bytes from pipe */
4362 total
; /* Total bytes from pipe */
4363 char buffer
[2048]; /* Copy buffer */
4364 int i
; /* Looping var */
4365 char option
[PPD_MAX_NAME
], /* Option name */
4366 choice
[PPD_MAX_NAME
]; /* Choice name */
4367 ppd_size_t
*size
; /* Default size */
4368 int num_defaults
; /* Number of default options */
4369 cups_option_t
*defaults
; /* Default options */
4370 char cups_protocol
[PPD_MAX_LINE
];
4371 /* cupsProtocol attribute */
4374 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_model(con=%p, from=\"%s\", to=\"%s\")", con
, from
, to
);
4377 * Run cups-driverd to get the PPD file...
4380 argv
[0] = "cups-driverd";
4382 argv
[2] = (char *)from
;
4385 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4387 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4388 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4389 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4390 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4393 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4394 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4396 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4397 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4408 * Wait up to 30 seconds for the PPD file to be copied...
4413 if (temppipe
[0] > CGIPipes
[0])
4414 maxfd
= temppipe
[0] + 1;
4416 maxfd
= CGIPipes
[0] + 1;
4421 * See if we have data ready...
4425 FD_SET(temppipe
[0], &input
);
4426 FD_SET(CGIPipes
[0], &input
);
4428 timeout
.tv_sec
= 30;
4429 timeout
.tv_usec
= 0;
4431 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4441 * We have timed out...
4447 if (FD_ISSET(temppipe
[0], &input
))
4450 * Read the PPD file from the pipe, and write it to the PPD file.
4453 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4455 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4464 if (FD_ISSET(CGIPipes
[0], &input
))
4474 * No data from cups-deviced...
4477 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4483 * Open the source file for a copy...
4486 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4493 * Read the source file and see what page sizes are supported...
4496 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4504 * Open the destination (if possible) and set the default options...
4509 cups_protocol
[0] = '\0';
4511 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4514 * Read all of the default lines from the old PPD...
4517 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4518 if (!strncmp(buffer
, "*Default", 8))
4521 * Add the default option...
4524 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4525 choice
, sizeof(choice
)))
4527 ppd_option_t
*ppdo
; /* PPD option */
4531 * Only add the default if the default hasn't already been
4532 * set and the choice exists in the new PPD...
4535 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4536 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4537 ppdFindChoice(ppdo
, choice
))
4538 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4542 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4543 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4547 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4550 * Add the default media sizes...
4553 num_defaults
= cupsAddOption("PageSize", size
->name
,
4554 num_defaults
, &defaults
);
4555 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4556 num_defaults
, &defaults
);
4557 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4558 num_defaults
, &defaults
);
4559 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4560 num_defaults
, &defaults
);
4566 * Open the destination file for a copy...
4569 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4571 cupsFreeOptions(num_defaults
, defaults
);
4578 * Copy the source file to the destination...
4581 cupsFileRewind(src
);
4583 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4585 if (!strncmp(buffer
, "*Default", 8))
4588 * Check for an previous default option choice...
4591 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4592 choice
, sizeof(choice
)))
4594 const char *val
; /* Default option value */
4597 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4600 * Substitute the previous choice...
4603 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4608 cupsFilePrintf(dst
, "%s\n", buffer
);
4611 if (cups_protocol
[0])
4612 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4614 cupsFreeOptions(num_defaults
, defaults
);
4617 * Close both files and return...
4624 return (cupsdCloseCreatedConfFile(dst
, to
));
4629 * 'copy_job_attrs()' - Copy job attributes.
4633 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4634 cupsd_job_t
*job
, /* I - Job */
4635 cups_array_t
*ra
, /* I - Requested attributes array */
4636 cups_array_t
*exclude
) /* I - Private attributes array */
4638 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4642 * Send the requested attributes for each job...
4645 if (!cupsArrayFind(exclude
, "all"))
4647 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4648 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4649 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4650 "number-of-documents", job
->num_files
);
4652 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4653 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4654 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4655 "job-media-progress", job
->progress
);
4657 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4658 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4660 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4661 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4663 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4664 "job-more-info", NULL
, job_uri
);
4667 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4668 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4669 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4670 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4671 job
->num_files
> 0);
4673 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4674 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4675 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4676 "job-printer-up-time", time(NULL
));
4679 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4681 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4682 con
->clientname
, con
->clientport
,
4683 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4686 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4687 "job-printer-uri", NULL
, job_uri
);
4690 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4692 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4693 con
->clientname
, con
->clientport
, "/jobs/%d",
4695 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4696 "job-uri", NULL
, job_uri
);
4701 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4706 * Generate attributes from the job structure...
4709 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-completed")))
4710 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-completed", ippTimeToDate(job
->completed_time
));
4712 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-creation")))
4713 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(job
->creation_time
));
4715 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4716 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4718 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4719 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4721 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4722 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4724 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4725 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4727 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4728 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4730 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4732 switch (job
->state_value
)
4734 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4736 case IPP_JSTATE_ABORTED
:
4737 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4739 case IPP_JSTATE_CANCELED
:
4740 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4742 case IPP_JSTATE_COMPLETED
:
4743 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4748 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4749 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4751 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4752 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4758 * 'copy_printer_attrs()' - Copy printer attributes.
4763 cupsd_client_t
*con
, /* I - Client connection */
4764 cupsd_printer_t
*printer
, /* I - Printer */
4765 cups_array_t
*ra
) /* I - Requested attributes array */
4767 char printer_uri
[HTTP_MAX_URI
];
4769 char printer_icons
[HTTP_MAX_URI
];
4771 time_t curtime
; /* Current time */
4772 int i
; /* Looping var */
4776 * Copy the printer attributes to the response using requested-attributes
4777 * and document-format attributes that may be provided by the client.
4780 curtime
= time(NULL
);
4782 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4783 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4784 "marker-change-time", printer
->marker_time
);
4786 if (printer
->num_printers
> 0 &&
4787 (!ra
|| cupsArrayFind(ra
, "member-uris")))
4789 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4790 cupsd_printer_t
*p2
; /* Printer in class */
4791 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4794 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
4795 IPP_TAG_URI
, "member-uris",
4796 printer
->num_printers
, NULL
,
4799 for (i
= 0; i
< printer
->num_printers
; i
++)
4801 p2
= printer
->printers
[i
];
4803 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
4804 IPP_TAG_URI
)) != NULL
)
4805 member_uris
->values
[i
].string
.text
=
4806 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4809 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
4810 sizeof(printer_uri
), "ipp", NULL
, con
->clientname
,
4812 (p2
->type
& CUPS_PRINTER_CLASS
) ?
4813 "/classes/%s" : "/printers/%s", p2
->name
);
4814 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
4820 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4821 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4822 "printer-alert", NULL
, printer
->alert
);
4824 if (printer
->alert_description
&&
4825 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4826 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4827 "printer-alert-description", NULL
,
4828 printer
->alert_description
);
4830 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-date-time"))
4831 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-config-change-date-time", ippTimeToDate(printer
->config_time
));
4833 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-time"))
4834 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4835 "printer-config-change-time", printer
->config_time
);
4837 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4838 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4839 ippTimeToDate(curtime
));
4841 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4842 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4844 if (printer
->reg_name
)
4845 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4846 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4848 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4849 "printer-dns-sd-name", 0);
4851 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4853 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4854 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4855 "printer-error-policy", NULL
, printer
->error_policy
);
4857 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
4859 static const char * const errors
[] =/* printer-error-policy-supported values */
4862 "retry-current-job",
4867 if (printer
->type
& CUPS_PRINTER_CLASS
)
4868 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4869 "printer-error-policy-supported", NULL
, "retry-current-job");
4871 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4872 "printer-error-policy-supported",
4873 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
4876 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
4878 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_icons
, sizeof(printer_icons
),
4879 "http", NULL
, con
->clientname
, con
->clientport
,
4880 "/icons/%s.png", printer
->name
);
4881 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons",
4882 NULL
, printer_icons
);
4883 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", printer_icons
);
4886 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4887 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
4889 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4890 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
4892 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
4894 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4895 "http", NULL
, con
->clientname
, con
->clientport
,
4896 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4897 "/classes/%s" : "/printers/%s", printer
->name
);
4898 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4899 "printer-more-info", NULL
, printer_uri
);
4902 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4903 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4904 "printer-op-policy", NULL
, printer
->op_policy
);
4906 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4907 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4910 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-date-time"))
4911 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-state-change-date-time", ippTimeToDate(printer
->state_time
));
4913 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4914 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4915 "printer-state-change-time", printer
->state_time
);
4917 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4918 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4919 "printer-state-message", NULL
, printer
->state_message
);
4921 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4922 add_printer_state_reasons(con
, printer
);
4924 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4926 cups_ptype_t type
; /* printer-type value */
4929 * Add the CUPS-specific printer-type attribute...
4932 type
= printer
->type
;
4934 if (printer
== DefaultPrinter
)
4935 type
|= CUPS_PRINTER_DEFAULT
;
4937 if (!printer
->accepting
)
4938 type
|= CUPS_PRINTER_REJECTING
;
4940 if (!printer
->shared
)
4941 type
|= CUPS_PRINTER_NOT_SHARED
;
4943 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
4946 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4947 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4948 "printer-up-time", curtime
);
4950 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
4952 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4953 "ipp", NULL
, con
->clientname
, con
->clientport
,
4954 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4955 "/classes/%s" : "/printers/%s", printer
->name
);
4956 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4957 "printer-uri-supported", NULL
, printer_uri
);
4958 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4962 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4963 add_queued_job_count(con
, printer
);
4965 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
4966 if (printer
->ppd_attrs
)
4967 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
4968 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
4973 * 'copy_subscription_attrs()' - Copy subscription attributes.
4977 copy_subscription_attrs(
4978 cupsd_client_t
*con
, /* I - Client connection */
4979 cupsd_subscription_t
*sub
, /* I - Subscription */
4980 cups_array_t
*ra
, /* I - Requested attributes array */
4981 cups_array_t
*exclude
) /* I - Private attributes array */
4983 ipp_attribute_t
*attr
; /* Current attribute */
4984 char printer_uri
[HTTP_MAX_URI
];
4986 int count
; /* Number of events */
4987 unsigned mask
; /* Current event mask */
4988 const char *name
; /* Current event name */
4991 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4992 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
4993 con
, sub
, ra
, exclude
);
4996 * Copy the subscription attributes to the response using the
4997 * requested-attributes attribute that may be provided by the client.
5000 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5002 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5003 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5005 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5007 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5010 * Simple event list...
5013 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5014 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5015 "notify-events", NULL
, name
);
5020 * Complex event list...
5023 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5024 if (sub
->mask
& mask
)
5027 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5028 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5029 "notify-events", count
, NULL
, NULL
);
5031 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5032 if (sub
->mask
& mask
)
5034 attr
->values
[count
].string
.text
=
5035 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5042 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5043 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5044 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5045 "notify-lease-duration", sub
->lease
);
5047 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5048 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5049 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5050 "notify-recipient-uri", NULL
, sub
->recipient
);
5051 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5052 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5053 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5054 "notify-pull-method", NULL
, "ippget");
5056 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5057 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5058 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5059 "notify-subscriber-user-name", NULL
, sub
->owner
);
5061 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5062 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5063 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5064 "notify-time-interval", sub
->interval
);
5066 if (sub
->user_data_len
> 0 &&
5067 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5068 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5069 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5070 sub
->user_data
, sub
->user_data_len
);
5073 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5074 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5075 "notify-job-id", sub
->job
->id
);
5077 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5079 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5080 "ipp", NULL
, con
->clientname
, con
->clientport
,
5081 "/printers/%s", sub
->dest
->name
);
5082 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5083 "notify-printer-uri", NULL
, printer_uri
);
5086 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5087 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5088 "notify-subscription-id", sub
->id
);
5093 * 'create_job()' - Print a file to a printer or class.
5097 create_job(cupsd_client_t
*con
, /* I - Client connection */
5098 ipp_attribute_t
*uri
) /* I - Printer URI */
5100 int i
; /* Looping var */
5101 cupsd_printer_t
*printer
; /* Printer */
5102 cupsd_job_t
*job
; /* New job */
5103 static const char * const forbidden_attrs
[] =
5104 { /* List of forbidden attributes */
5108 "document-natural-language"
5112 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5113 con
->number
, uri
->values
[0].string
.text
);
5116 * Is the destination valid?
5119 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5125 send_ipp_status(con
, IPP_NOT_FOUND
,
5126 _("The printer or class does not exist."));
5131 * Check for invalid Create-Job attributes and log a warning or error depending
5132 * on whether cupsd is running in "strict conformance" mode...
5136 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5138 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5140 if (StrictConformance
)
5142 send_ipp_status(con
, IPP_BAD_REQUEST
,
5143 _("The '%s' operation attribute cannot be supplied in a "
5144 "Create-Job request."), forbidden_attrs
[i
]);
5148 cupsdLogMessage(CUPSD_LOG_WARN
,
5149 "Unexpected '%s' operation attribute in a Create-Job "
5150 "request.", forbidden_attrs
[i
]);
5154 * Create the job object...
5157 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5160 job
->pending_timeout
= 1;
5163 * Save and log the job...
5166 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5167 job
->dest
, job
->username
);
5172 * 'create_requested_array()' - Create an array for the requested-attributes.
5175 static cups_array_t
* /* O - Array of attributes or NULL */
5176 create_requested_array(ipp_t
*request
) /* I - IPP request */
5178 cups_array_t
*ra
; /* Requested attributes array */
5182 * Create the array for standard attributes...
5185 ra
= ippCreateRequestedArray(request
);
5188 * Add CUPS defaults as needed...
5191 if (cupsArrayFind(ra
, "printer-defaults"))
5194 * Include user-set defaults...
5197 char *name
; /* Option name */
5199 cupsArrayRemove(ra
, "printer-defaults");
5201 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5203 name
= (char *)cupsArrayNext(CommonDefaults
))
5204 if (!cupsArrayFind(ra
, name
))
5205 cupsArrayAdd(ra
, name
);
5213 * 'create_subscriptions()' - Create one or more notification subscriptions.
5217 create_subscriptions(
5218 cupsd_client_t
*con
, /* I - Client connection */
5219 ipp_attribute_t
*uri
) /* I - Printer URI */
5221 http_status_t status
; /* Policy status */
5222 int i
; /* Looping var */
5223 ipp_attribute_t
*attr
; /* Current attribute */
5224 cups_ptype_t dtype
; /* Destination type (printer/class) */
5225 char scheme
[HTTP_MAX_URI
],
5226 /* Scheme portion of URI */
5227 userpass
[HTTP_MAX_URI
],
5228 /* Username portion of URI */
5230 /* Host portion of URI */
5231 resource
[HTTP_MAX_URI
];
5232 /* Resource portion of URI */
5233 int port
; /* Port portion of URI */
5234 cupsd_printer_t
*printer
; /* Printer/class */
5235 cupsd_job_t
*job
; /* Job */
5236 int jobid
; /* Job ID */
5237 cupsd_subscription_t
*sub
; /* Subscription object */
5238 const char *username
, /* requesting-user-name or
5239 authenticated username */
5240 *recipient
, /* notify-recipient-uri */
5241 *pullmethod
; /* notify-pull-method */
5242 ipp_attribute_t
*user_data
; /* notify-user-data */
5243 int interval
, /* notify-time-interval */
5244 lease
; /* notify-lease-duration */
5245 unsigned mask
; /* notify-events */
5246 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5247 *notify_lease
; /* notify-lease-duration(-default) */
5251 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5253 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5254 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5255 attr
->value_tag
, attr
->name
);
5257 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5262 * Is the destination valid?
5265 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5267 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5268 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5269 sizeof(host
), &port
, resource
, sizeof(resource
));
5271 if (!strcmp(resource
, "/"))
5273 dtype
= (cups_ptype_t
)0;
5276 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5278 dtype
= (cups_ptype_t
)0;
5281 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5283 dtype
= CUPS_PRINTER_CLASS
;
5286 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5292 send_ipp_status(con
, IPP_NOT_FOUND
,
5293 _("The printer or class does not exist."));
5303 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5306 send_http_error(con
, status
, printer
);
5310 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5312 send_http_error(con
, status
, NULL
);
5317 * Get the user that is requesting the subscription...
5320 username
= get_username(con
);
5323 * Find the first subscription group attribute; return if we have
5327 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5328 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5333 send_ipp_status(con
, IPP_BAD_REQUEST
,
5334 _("No subscription attributes in request."));
5339 * Process the subscription attributes in the request...
5342 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5350 lease
= DefaultLeaseDuration
;
5352 mask
= CUPSD_EVENT_NONE
;
5356 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5358 notify_lease
= ippFindAttribute(printer
->attrs
,
5359 "notify-lease-duration-default",
5363 lease
= notify_lease
->values
[0].integer
;
5367 notify_events
= NULL
;
5368 notify_lease
= NULL
;
5371 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5373 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5374 attr
->value_tag
== IPP_TAG_URI
)
5377 * Validate the recipient scheme against the ServerBin/notifier
5381 char notifier
[1024]; /* Notifier filename */
5384 recipient
= attr
->values
[0].string
.text
;
5386 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5387 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5388 host
, sizeof(host
), &port
,
5389 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5391 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5392 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5393 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5394 "notify-status-code", IPP_URI_SCHEME
);
5398 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5400 if (access(notifier
, X_OK
))
5402 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5403 _("notify-recipient-uri URI \"%s\" uses unknown "
5404 "scheme."), recipient
);
5405 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5406 "notify-status-code", IPP_URI_SCHEME
);
5410 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5412 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5413 _("notify-recipient-uri URI \"%s\" is already used."),
5415 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5416 "notify-status-code", IPP_ATTRIBUTES
);
5420 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5421 attr
->value_tag
== IPP_TAG_KEYWORD
)
5423 pullmethod
= attr
->values
[0].string
.text
;
5425 if (strcmp(pullmethod
, "ippget"))
5427 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5428 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5429 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5430 "notify-status-code", IPP_ATTRIBUTES
);
5434 else if (!strcmp(attr
->name
, "notify-charset") &&
5435 attr
->value_tag
== IPP_TAG_CHARSET
&&
5436 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5437 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5439 send_ipp_status(con
, IPP_CHARSET
,
5440 _("Character set \"%s\" not supported."),
5441 attr
->values
[0].string
.text
);
5444 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5445 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5446 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5448 send_ipp_status(con
, IPP_CHARSET
,
5449 _("Language \"%s\" not supported."),
5450 attr
->values
[0].string
.text
);
5453 else if (!strcmp(attr
->name
, "notify-user-data") &&
5454 attr
->value_tag
== IPP_TAG_STRING
)
5456 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5458 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5459 _("The notify-user-data value is too large "
5460 "(%d > 63 octets)."),
5461 attr
->values
[0].unknown
.length
);
5467 else if (!strcmp(attr
->name
, "notify-events") &&
5468 attr
->value_tag
== IPP_TAG_KEYWORD
)
5469 notify_events
= attr
;
5470 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5471 attr
->value_tag
== IPP_TAG_INTEGER
)
5472 lease
= attr
->values
[0].integer
;
5473 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5474 attr
->value_tag
== IPP_TAG_INTEGER
)
5475 interval
= attr
->values
[0].integer
;
5476 else if (!strcmp(attr
->name
, "notify-job-id") &&
5477 attr
->value_tag
== IPP_TAG_INTEGER
)
5478 jobid
= attr
->values
[0].integer
;
5485 for (i
= 0; i
< notify_events
->num_values
; i
++)
5486 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5490 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5492 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5493 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5494 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5496 if (!recipient
&& !pullmethod
)
5499 if (mask
== CUPSD_EVENT_NONE
)
5502 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5504 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5507 send_ipp_status(con
, IPP_BAD_REQUEST
,
5508 _("notify-events not specified."));
5513 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5515 cupsdLogMessage(CUPSD_LOG_INFO
,
5516 "create_subscriptions: Limiting notify-lease-duration to "
5519 lease
= MaxLeaseDuration
;
5524 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5526 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5534 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5536 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5537 _("There are too many subscriptions."));
5542 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5545 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5546 "Added subscription #%d for printer \"%s\".",
5547 sub
->id
, printer
->name
);
5549 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5552 sub
->interval
= interval
;
5554 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5556 cupsdSetString(&sub
->owner
, username
);
5560 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5561 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5562 (size_t)sub
->user_data_len
);
5565 ippAddSeparator(con
->response
);
5566 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5567 "notify-subscription-id", sub
->id
);
5569 con
->response
->request
.status
.status_code
= IPP_OK
;
5575 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5580 * 'delete_printer()' - Remove a printer or class from the system.
5584 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5585 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5587 http_status_t status
; /* Policy status */
5588 cups_ptype_t dtype
; /* Destination type (printer/class) */
5589 cupsd_printer_t
*printer
; /* Printer/class */
5590 char filename
[1024]; /* Script/PPD filename */
5593 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5594 con
->number
, uri
->values
[0].string
.text
);
5597 * Do we have a valid URI?
5600 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5606 send_ipp_status(con
, IPP_NOT_FOUND
,
5607 _("The printer or class does not exist."));
5615 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5617 send_http_error(con
, status
, NULL
);
5622 * Remove old jobs...
5625 cupsdCancelJobs(printer
->name
, NULL
, 1);
5628 * Remove old subscriptions and send a "deleted printer" event...
5631 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5632 "%s \"%s\" deleted by \"%s\".",
5633 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5634 printer
->name
, get_username(con
));
5636 cupsdExpireSubscriptions(printer
, NULL
);
5639 * Remove any old PPD or script files...
5642 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5645 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
5649 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
5652 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
5656 * Unregister color profiles...
5659 cupsdUnregisterColor(printer
);
5661 if (dtype
& CUPS_PRINTER_CLASS
)
5663 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5664 printer
->name
, get_username(con
));
5666 cupsdDeletePrinter(printer
, 0);
5667 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5671 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5672 printer
->name
, get_username(con
));
5674 if (cupsdDeletePrinter(printer
, 0))
5675 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5677 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5680 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
5683 * Return with no errors...
5686 con
->response
->request
.status
.status_code
= IPP_OK
;
5691 * 'get_default()' - Get the default destination.
5695 get_default(cupsd_client_t
*con
) /* I - Client connection */
5697 http_status_t status
; /* Policy status */
5698 cups_array_t
*ra
; /* Requested attributes array */
5701 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
5707 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5709 send_http_error(con
, status
, NULL
);
5715 ra
= create_requested_array(con
->request
);
5717 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5719 cupsArrayDelete(ra
);
5721 con
->response
->request
.status
.status_code
= IPP_OK
;
5724 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
5729 * 'get_devices()' - Get the list of available devices on the local system.
5733 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5735 http_status_t status
; /* Policy status */
5736 ipp_attribute_t
*limit
, /* limit attribute */
5737 *timeout
, /* timeout attribute */
5738 *requested
, /* requested-attributes attribute */
5739 *exclude
, /* exclude-schemes attribute */
5740 *include
; /* include-schemes attribute */
5741 char command
[1024], /* cups-deviced command */
5742 options
[2048], /* Options to pass to command */
5744 /* String for requested attributes */
5746 /* String for excluded schemes */
5748 /* String for included schemes */
5751 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
5757 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5759 send_http_error(con
, status
, NULL
);
5764 * Run cups-deviced command with the given options...
5767 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5768 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
5769 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5771 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
5772 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
5775 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5777 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5780 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
5782 exclude_str
[0] = '\0';
5785 url_encode_attr(include
, include_str
, sizeof(include_str
));
5787 include_str
[0] = '\0';
5789 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5790 snprintf(options
, sizeof(options
),
5791 "%d+%d+%d+%d+%s%s%s%s%s",
5792 con
->request
->request
.op
.request_id
,
5793 limit
? limit
->values
[0].integer
: 0,
5794 timeout
? timeout
->values
[0].integer
: 15,
5797 exclude_str
[0] ? "%20" : "", exclude_str
,
5798 include_str
[0] ? "%20" : "", include_str
);
5800 if (cupsdSendCommand(con
, command
, options
, 1))
5803 * Command started successfully, don't send an IPP response here...
5806 ippDelete(con
->response
);
5807 con
->response
= NULL
;
5812 * Command failed, return "internal error" so the user knows something
5816 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5817 _("cups-deviced failed to execute."));
5823 * 'get_document()' - Get a copy of a job file.
5827 get_document(cupsd_client_t
*con
, /* I - Client connection */
5828 ipp_attribute_t
*uri
) /* I - Job URI */
5830 http_status_t status
; /* Policy status */
5831 ipp_attribute_t
*attr
; /* Current attribute */
5832 int jobid
; /* Job ID */
5833 int docnum
; /* Document number */
5834 cupsd_job_t
*job
; /* Current job */
5835 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
5836 username
[HTTP_MAX_URI
], /* Username portion of URI */
5837 host
[HTTP_MAX_URI
], /* Host portion of URI */
5838 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5839 int port
; /* Port portion of URI */
5840 char filename
[1024], /* Filename for document */
5841 format
[1024]; /* Format for document */
5844 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5845 con
->number
, uri
->values
[0].string
.text
);
5848 * See if we have a job URI or a printer URI...
5851 if (!strcmp(uri
->name
, "printer-uri"))
5854 * Got a printer URI; see if we also have a job-id attribute...
5857 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5858 IPP_TAG_INTEGER
)) == NULL
)
5860 send_ipp_status(con
, IPP_BAD_REQUEST
,
5861 _("Got a printer-uri attribute but no job-id."));
5865 jobid
= attr
->values
[0].integer
;
5870 * Got a job URI; parse it to get the job ID...
5873 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5874 sizeof(scheme
), username
, sizeof(username
), host
,
5875 sizeof(host
), &port
, resource
, sizeof(resource
));
5877 if (strncmp(resource
, "/jobs/", 6))
5883 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
5884 uri
->values
[0].string
.text
);
5888 jobid
= atoi(resource
+ 6);
5892 * See if the job exists...
5895 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5898 * Nope - return a "not found" error...
5901 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
5909 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
5910 job
->username
)) != HTTP_OK
)
5912 send_http_error(con
, status
, NULL
);
5917 * Get the document number...
5920 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5921 IPP_TAG_INTEGER
)) == NULL
)
5923 send_ipp_status(con
, IPP_BAD_REQUEST
,
5924 _("Missing document-number attribute."));
5928 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5929 attr
->num_values
> 1)
5931 send_ipp_status(con
, IPP_NOT_FOUND
,
5932 _("Document #%d does not exist in job #%d."), docnum
,
5937 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5939 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5941 cupsdLogMessage(CUPSD_LOG_ERROR
,
5942 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5944 send_ipp_status(con
, IPP_NOT_FOUND
,
5945 _("Unable to open document #%d in job #%d."), docnum
,
5950 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5954 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
5955 job
->filetypes
[docnum
- 1]->type
);
5957 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
5959 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
5961 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
5962 IPP_TAG_NAME
)) != NULL
)
5963 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
5964 NULL
, attr
->values
[0].string
.text
);
5969 * 'get_job_attrs()' - Get job attributes.
5973 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5974 ipp_attribute_t
*uri
) /* I - Job URI */
5976 http_status_t status
; /* Policy status */
5977 ipp_attribute_t
*attr
; /* Current attribute */
5978 int jobid
; /* Job ID */
5979 cupsd_job_t
*job
; /* Current job */
5980 cupsd_printer_t
*printer
; /* Current printer */
5981 cupsd_policy_t
*policy
; /* Current security policy */
5982 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5983 username
[HTTP_MAX_URI
], /* Username portion of URI */
5984 host
[HTTP_MAX_URI
], /* Host portion of URI */
5985 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5986 int port
; /* Port portion of URI */
5987 cups_array_t
*ra
, /* Requested attributes array */
5988 *exclude
; /* Private attributes array */
5991 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
5992 con
->number
, uri
->values
[0].string
.text
);
5995 * See if we have a job URI or a printer URI...
5998 if (!strcmp(uri
->name
, "printer-uri"))
6001 * Got a printer URI; see if we also have a job-id attribute...
6004 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6005 IPP_TAG_INTEGER
)) == NULL
)
6007 send_ipp_status(con
, IPP_BAD_REQUEST
,
6008 _("Got a printer-uri attribute but no job-id."));
6012 jobid
= attr
->values
[0].integer
;
6017 * Got a job URI; parse it to get the job ID...
6020 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6021 sizeof(scheme
), username
, sizeof(username
), host
,
6022 sizeof(host
), &port
, resource
, sizeof(resource
));
6024 if (strncmp(resource
, "/jobs/", 6))
6030 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6031 uri
->values
[0].string
.text
);
6035 jobid
= atoi(resource
+ 6);
6039 * See if the job exists...
6042 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6045 * Nope - return a "not found" error...
6048 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6056 if ((printer
= job
->printer
) == NULL
)
6057 printer
= cupsdFindDest(job
->dest
);
6060 policy
= printer
->op_policy_ptr
;
6062 policy
= DefaultPolicyPtr
;
6064 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6066 send_http_error(con
, status
, NULL
);
6070 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6073 * Copy attributes...
6078 ra
= create_requested_array(con
->request
);
6079 copy_job_attrs(con
, job
, ra
, exclude
);
6080 cupsArrayDelete(ra
);
6082 con
->response
->request
.status
.status_code
= IPP_OK
;
6087 * 'get_jobs()' - Get a list of jobs for the specified printer.
6091 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6092 ipp_attribute_t
*uri
) /* I - Printer URI */
6094 http_status_t status
; /* Policy status */
6095 ipp_attribute_t
*attr
; /* Current attribute */
6096 const char *dest
; /* Destination */
6097 cups_ptype_t dtype
; /* Destination type (printer/class) */
6098 cups_ptype_t dmask
; /* Destination type mask */
6099 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6100 username
[HTTP_MAX_URI
], /* Username portion of URI */
6101 host
[HTTP_MAX_URI
], /* Host portion of URI */
6102 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6103 int port
; /* Port portion of URI */
6104 int job_comparison
; /* Job comparison */
6105 ipp_jstate_t job_state
; /* job-state value */
6106 int first_job_id
= 1, /* First job ID */
6107 first_index
= 1, /* First index */
6108 current_index
= 0; /* Current index */
6109 int limit
= 0; /* Maximum number of jobs to return */
6110 int count
; /* Number of jobs that match */
6111 int need_load_job
= 0; /* Do we need to load the job? */
6112 const char *job_attr
; /* Job attribute requested */
6113 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6114 cupsd_job_t
*job
; /* Current job pointer */
6115 cupsd_printer_t
*printer
; /* Printer */
6116 cups_array_t
*list
; /* Which job list... */
6117 int delete_list
= 0; /* Delete the list afterwards? */
6118 cups_array_t
*ra
, /* Requested attributes array */
6119 *exclude
; /* Private attributes array */
6120 cupsd_policy_t
*policy
; /* Current policy */
6123 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6124 uri
->values
[0].string
.text
);
6127 * Is the destination valid?
6130 if (strcmp(uri
->name
, "printer-uri"))
6132 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6136 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6137 sizeof(scheme
), username
, sizeof(username
), host
,
6138 sizeof(host
), &port
, resource
, sizeof(resource
));
6140 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6143 dtype
= (cups_ptype_t
)0;
6144 dmask
= (cups_ptype_t
)0;
6147 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6150 dtype
= (cups_ptype_t
)0;
6151 dmask
= CUPS_PRINTER_CLASS
;
6154 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6157 dtype
= CUPS_PRINTER_CLASS
;
6158 dmask
= CUPS_PRINTER_CLASS
;
6161 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6168 send_ipp_status(con
, IPP_NOT_FOUND
,
6169 _("The printer or class does not exist."));
6174 dtype
&= CUPS_PRINTER_CLASS
;
6175 dmask
= CUPS_PRINTER_CLASS
;
6183 policy
= printer
->op_policy_ptr
;
6185 policy
= DefaultPolicyPtr
;
6187 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6189 send_http_error(con
, status
, NULL
);
6193 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6196 * See if the "which-jobs" attribute have been specified...
6199 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6200 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6202 send_ipp_status(con
, IPP_CONFLICT
,
6203 _("The %s attribute cannot be provided with job-ids."),
6207 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6209 job_comparison
= -1;
6210 job_state
= IPP_JOB_STOPPED
;
6213 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6216 job_state
= IPP_JOB_CANCELED
;
6217 list
= cupsdGetCompletedJobs(printer
);
6220 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6223 job_state
= IPP_JOB_ABORTED
;
6224 list
= cupsdGetCompletedJobs(printer
);
6227 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6230 job_state
= IPP_JOB_PENDING
;
6233 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6236 job_state
= IPP_JOB_CANCELED
;
6237 list
= cupsdGetCompletedJobs(printer
);
6240 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6243 job_state
= IPP_JOB_PENDING
;
6246 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6249 job_state
= IPP_JOB_HELD
;
6252 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6255 job_state
= IPP_JOB_PROCESSING
;
6256 list
= PrintingJobs
;
6258 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6261 job_state
= IPP_JOB_STOPPED
;
6266 send_ipp_status(con
, IPP_ATTRIBUTES
,
6267 _("The which-jobs value \"%s\" is not supported."),
6268 attr
->values
[0].string
.text
);
6269 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6270 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6275 * See if they want to limit the number of jobs reported...
6278 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6282 send_ipp_status(con
, IPP_CONFLICT
,
6283 _("The %s attribute cannot be provided with job-ids."),
6288 limit
= attr
->values
[0].integer
;
6291 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6295 send_ipp_status(con
, IPP_CONFLICT
,
6296 _("The %s attribute cannot be provided with job-ids."),
6301 first_index
= attr
->values
[0].integer
;
6303 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6307 send_ipp_status(con
, IPP_CONFLICT
,
6308 _("The %s attribute cannot be provided with job-ids."),
6313 first_job_id
= attr
->values
[0].integer
;
6317 * See if we only want to see jobs for a specific user...
6320 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6322 send_ipp_status(con
, IPP_CONFLICT
,
6323 _("The %s attribute cannot be provided with job-ids."),
6327 else if (attr
&& attr
->values
[0].boolean
)
6328 strlcpy(username
, get_username(con
), sizeof(username
));
6332 ra
= create_requested_array(con
->request
);
6333 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6334 if (strcmp(job_attr
, "job-id") &&
6335 strcmp(job_attr
, "job-k-octets") &&
6336 strcmp(job_attr
, "job-media-progress") &&
6337 strcmp(job_attr
, "job-more-info") &&
6338 strcmp(job_attr
, "job-name") &&
6339 strcmp(job_attr
, "job-originating-user-name") &&
6340 strcmp(job_attr
, "job-preserved") &&
6341 strcmp(job_attr
, "job-printer-up-time") &&
6342 strcmp(job_attr
, "job-printer-uri") &&
6343 strcmp(job_attr
, "job-state") &&
6344 strcmp(job_attr
, "job-state-reasons") &&
6345 strcmp(job_attr
, "job-uri") &&
6346 strcmp(job_attr
, "time-at-completed") &&
6347 strcmp(job_attr
, "time-at-creation") &&
6348 strcmp(job_attr
, "number-of-documents"))
6354 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6357 * Limit expensive Get-Jobs for job history to 500 jobs...
6360 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6363 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6367 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6371 * OK, build a list of jobs for this printer...
6376 int i
; /* Looping var */
6378 for (i
= 0; i
< job_ids
->num_values
; i
++)
6380 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6384 if (i
< job_ids
->num_values
)
6386 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6387 job_ids
->values
[i
].integer
);
6391 for (i
= 0; i
< job_ids
->num_values
; i
++)
6393 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6395 if (need_load_job
&& !job
->attrs
)
6401 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6407 ippAddSeparator(con
->response
);
6409 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6410 job
->printer
->op_policy_ptr
:
6411 policy
, con
, job
->printer
,
6414 copy_job_attrs(con
, job
, ra
, exclude
);
6419 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6420 (limit
<= 0 || count
< limit
) && job
;
6421 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6424 * Filter out jobs that don't match...
6427 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6428 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6429 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6430 job
->username
, job
->state_value
, job
->attrs
);
6432 if (!job
->dest
|| !job
->username
)
6435 if (!job
->dest
|| !job
->username
)
6438 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6439 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6441 if ((job
->dtype
& dmask
) != dtype
&&
6442 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6445 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6446 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6447 (job_comparison
> 0 && job
->state_value
< job_state
))
6450 if (job
->id
< first_job_id
)
6454 if (current_index
< first_index
)
6457 if (need_load_job
&& !job
->attrs
)
6463 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6468 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6472 ippAddSeparator(con
->response
);
6476 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6477 job
->printer
->op_policy_ptr
:
6478 policy
, con
, job
->printer
,
6481 copy_job_attrs(con
, job
, ra
, exclude
);
6484 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6487 cupsArrayDelete(ra
);
6490 cupsArrayDelete(list
);
6492 con
->response
->request
.status
.status_code
= IPP_OK
;
6497 * 'get_notifications()' - Get events for a subscription.
6501 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6503 int i
, j
; /* Looping vars */
6504 http_status_t status
; /* Policy status */
6505 cupsd_subscription_t
*sub
; /* Subscription */
6506 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6507 *sequences
; /* notify-sequence-numbers */
6508 int min_seq
; /* Minimum sequence number */
6509 int interval
; /* Poll interval */
6512 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6516 * Get subscription attributes...
6519 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6521 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6526 send_ipp_status(con
, IPP_BAD_REQUEST
,
6527 _("Missing notify-subscription-ids attribute."));
6532 * Are the subscription IDs valid?
6535 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6537 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6540 * Bad subscription ID...
6543 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6544 ids
->values
[i
].integer
);
6552 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6554 con
, sub
->owner
)) != HTTP_OK
)
6556 send_http_error(con
, status
, sub
->dest
);
6561 * Check the subscription type and update the interval accordingly.
6564 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6567 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6569 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6575 * Tell the client to poll again in N seconds...
6579 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6580 "notify-get-interval", interval
);
6582 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6583 "printer-up-time", time(NULL
));
6586 * Copy the subscription event attributes to the response.
6589 con
->response
->request
.status
.status_code
=
6590 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6592 for (i
= 0; i
< ids
->num_values
; i
++)
6595 * Get the subscription and sequence number...
6598 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6600 if (sequences
&& i
< sequences
->num_values
)
6601 min_seq
= sequences
->values
[i
].integer
;
6606 * If we don't have any new events, nothing to do here...
6609 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
6613 * Otherwise copy all of the new events...
6616 if (sub
->first_event_id
> min_seq
)
6619 j
= min_seq
- sub
->first_event_id
;
6621 for (; j
< cupsArrayCount(sub
->events
); j
++)
6623 ippAddSeparator(con
->response
);
6625 copy_attrs(con
->response
,
6626 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
6627 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
6634 * 'get_ppd()' - Get a named PPD from the local system.
6638 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6639 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6641 http_status_t status
; /* Policy status */
6642 cupsd_printer_t
*dest
; /* Destination */
6643 cups_ptype_t dtype
; /* Destination type */
6646 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6647 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6649 if (!strcmp(ippGetName(uri
), "ppd-name"))
6652 * Return a PPD file from cups-driverd...
6655 const char *ppd_name
= ippGetString(uri
, 0, NULL
);
6656 /* ppd-name value */
6657 char command
[1024], /* cups-driverd command */
6658 options
[1024], /* Options to pass to command */
6659 oppd_name
[1024]; /* Escaped ppd-name */
6665 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6667 send_http_error(con
, status
, NULL
);
6672 * Check ppd-name value...
6675 if (strstr(ppd_name
, "../"))
6677 send_ipp_status(con
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, _("Invalid ppd-name value."));
6682 * Run cups-driverd command with the given options...
6685 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6686 url_encode_string(ppd_name
, oppd_name
, sizeof(oppd_name
));
6687 snprintf(options
, sizeof(options
), "get+%d+%s", ippGetRequestId(con
->request
), oppd_name
);
6689 if (cupsdSendCommand(con
, command
, options
, 0))
6692 * Command started successfully, don't send an IPP response here...
6695 ippDelete(con
->response
);
6696 con
->response
= NULL
;
6701 * Command failed, return "internal error" so the user knows something
6705 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("cups-driverd failed to execute."));
6708 else if (!strcmp(ippGetName(uri
), "printer-uri") && cupsdValidateDest(ippGetString(uri
, 0, NULL
), &dtype
, &dest
))
6710 int i
; /* Looping var */
6711 char filename
[1024]; /* PPD filename */
6717 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6719 send_http_error(con
, status
, dest
);
6724 * See if we need the PPD for a class or remote printer...
6727 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->name
);
6729 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6731 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
6732 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->uri
);
6735 else if (dtype
& CUPS_PRINTER_CLASS
)
6737 for (i
= 0; i
< dest
->num_printers
; i
++)
6738 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
6740 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
, dest
->printers
[i
]->name
);
6742 if (!access(filename
, 0))
6746 if (i
< dest
->num_printers
)
6747 dest
= dest
->printers
[i
];
6750 send_ipp_status(con
, IPP_STATUS_CUPS_SEE_OTHER
, _("See remote printer."));
6751 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, dest
->printers
[0]->uri
);
6757 * Found the printer with the PPD file, now see if there is one...
6760 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6762 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be opened: %s"), ippGetString(uri
, 0, NULL
), strerror(errno
));
6766 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6770 ippSetStatusCode(con
->response
, IPP_STATUS_OK
);
6773 send_ipp_status(con
, IPP_STATUS_ERROR_NOT_FOUND
, _("The PPD file \"%s\" could not be found."), ippGetString(uri
, 0, NULL
));
6778 * 'get_ppds()' - Get the list of PPD files on the local system.
6782 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6784 http_status_t status
; /* Policy status */
6785 ipp_attribute_t
*limit
, /* Limit attribute */
6786 *device
, /* ppd-device-id attribute */
6787 *language
, /* ppd-natural-language attribute */
6788 *make
, /* ppd-make attribute */
6789 *model
, /* ppd-make-and-model attribute */
6790 *model_number
, /* ppd-model-number attribute */
6791 *product
, /* ppd-product attribute */
6792 *psversion
, /* ppd-psverion attribute */
6793 *type
, /* ppd-type attribute */
6794 *requested
, /* requested-attributes attribute */
6795 *exclude
, /* exclude-schemes attribute */
6796 *include
; /* include-schemes attribute */
6797 char command
[1024], /* cups-driverd command */
6798 options
[4096], /* Options to pass to command */
6799 device_str
[256],/* Escaped ppd-device-id string */
6801 /* Escaped ppd-natural-language */
6802 make_str
[256], /* Escaped ppd-make string */
6803 model_str
[256], /* Escaped ppd-make-and-model string */
6804 model_number_str
[256],
6805 /* ppd-model-number string */
6807 /* Escaped ppd-product string */
6809 /* Escaped ppd-psversion string */
6810 type_str
[256], /* Escaped ppd-type string */
6812 /* String for requested attributes */
6814 /* String for excluded schemes */
6816 /* String for included schemes */
6819 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
6825 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6827 send_http_error(con
, status
, NULL
);
6832 * Run cups-driverd command with the given options...
6835 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6836 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6837 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6839 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6840 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6842 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6844 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6845 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6846 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6847 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6849 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
6851 include
= ippFindAttribute(con
->request
, "include-schemes",
6855 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6857 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6860 url_encode_attr(device
, device_str
, sizeof(device_str
));
6862 device_str
[0] = '\0';
6865 url_encode_attr(language
, language_str
, sizeof(language_str
));
6867 language_str
[0] = '\0';
6870 url_encode_attr(make
, make_str
, sizeof(make_str
));
6875 url_encode_attr(model
, model_str
, sizeof(model_str
));
6877 model_str
[0] = '\0';
6880 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6881 model_number
->values
[0].integer
);
6883 model_number_str
[0] = '\0';
6886 url_encode_attr(product
, product_str
, sizeof(product_str
));
6888 product_str
[0] = '\0';
6891 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6893 psversion_str
[0] = '\0';
6896 url_encode_attr(type
, type_str
, sizeof(type_str
));
6901 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6903 exclude_str
[0] = '\0';
6906 url_encode_attr(include
, include_str
, sizeof(include_str
));
6908 include_str
[0] = '\0';
6910 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6911 snprintf(options
, sizeof(options
),
6912 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6913 con
->request
->request
.op
.request_id
,
6914 limit
? limit
->values
[0].integer
: 0,
6916 device
? "%20" : "", device_str
,
6917 language
? "%20" : "", language_str
,
6918 make
? "%20" : "", make_str
,
6919 model
? "%20" : "", model_str
,
6920 model_number
? "%20" : "", model_number_str
,
6921 product
? "%20" : "", product_str
,
6922 psversion
? "%20" : "", psversion_str
,
6923 type
? "%20" : "", type_str
,
6924 exclude_str
[0] ? "%20" : "", exclude_str
,
6925 include_str
[0] ? "%20" : "", include_str
);
6927 if (cupsdSendCommand(con
, command
, options
, 0))
6930 * Command started successfully, don't send an IPP response here...
6933 ippDelete(con
->response
);
6934 con
->response
= NULL
;
6939 * Command failed, return "internal error" so the user knows something
6943 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6944 _("cups-driverd failed to execute."));
6950 * 'get_printer_attrs()' - Get printer attributes.
6954 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6955 ipp_attribute_t
*uri
) /* I - Printer URI */
6957 http_status_t status
; /* Policy status */
6958 cups_ptype_t dtype
; /* Destination type (printer/class) */
6959 cupsd_printer_t
*printer
; /* Printer/class */
6960 cups_array_t
*ra
; /* Requested attributes array */
6963 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6964 con
->number
, uri
->values
[0].string
.text
);
6967 * Is the destination valid?
6970 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6976 send_ipp_status(con
, IPP_NOT_FOUND
,
6977 _("The printer or class does not exist."));
6985 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6987 send_http_error(con
, status
, printer
);
6992 * Send the attributes...
6995 ra
= create_requested_array(con
->request
);
6997 copy_printer_attrs(con
, printer
, ra
);
6999 cupsArrayDelete(ra
);
7001 con
->response
->request
.status
.status_code
= IPP_OK
;
7006 * 'get_printer_supported()' - Get printer supported values.
7010 get_printer_supported(
7011 cupsd_client_t
*con
, /* I - Client connection */
7012 ipp_attribute_t
*uri
) /* I - Printer URI */
7014 http_status_t status
; /* Policy status */
7015 cups_ptype_t dtype
; /* Destination type (printer/class) */
7016 cupsd_printer_t
*printer
; /* Printer/class */
7019 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7020 con
->number
, uri
->values
[0].string
.text
);
7023 * Is the destination valid?
7026 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7032 send_ipp_status(con
, IPP_NOT_FOUND
,
7033 _("The printer or class does not exist."));
7041 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7043 send_http_error(con
, status
, printer
);
7048 * Return a list of attributes that can be set via Set-Printer-Attributes.
7051 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7052 "printer-geo-location", 0);
7053 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7055 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7056 "printer-location", 0);
7057 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7058 "printer-organization", 0);
7059 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7060 "printer-organizational-unit", 0);
7062 con
->response
->request
.status
.status_code
= IPP_OK
;
7067 * 'get_printers()' - Get a list of printers or classes.
7071 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7072 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7074 http_status_t status
; /* Policy status */
7075 ipp_attribute_t
*attr
; /* Current attribute */
7076 int limit
; /* Max number of printers to return */
7077 int count
; /* Number of printers that match */
7078 cupsd_printer_t
*printer
; /* Current printer pointer */
7079 cups_ptype_t printer_type
, /* printer-type attribute */
7080 printer_mask
; /* printer-type-mask attribute */
7081 char *location
; /* Location string */
7082 const char *username
; /* Current user */
7083 char *first_printer_name
; /* first-printer-name attribute */
7084 cups_array_t
*ra
; /* Requested attributes array */
7085 int local
; /* Local connection? */
7088 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7095 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7097 send_http_error(con
, status
, NULL
);
7102 * Check for printers...
7105 if (!Printers
|| !cupsArrayCount(Printers
))
7107 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7112 * See if they want to limit the number of printers reported...
7115 if ((attr
= ippFindAttribute(con
->request
, "limit",
7116 IPP_TAG_INTEGER
)) != NULL
)
7117 limit
= attr
->values
[0].integer
;
7121 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7122 IPP_TAG_NAME
)) != NULL
)
7123 first_printer_name
= attr
->values
[0].string
.text
;
7125 first_printer_name
= NULL
;
7128 * Support filtering...
7131 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7132 IPP_TAG_ENUM
)) != NULL
)
7133 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7135 printer_type
= (cups_ptype_t
)0;
7137 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7138 IPP_TAG_ENUM
)) != NULL
)
7139 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7141 printer_mask
= (cups_ptype_t
)0;
7143 local
= httpAddrLocalhost(&(con
->clientaddr
));
7145 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7146 IPP_TAG_TEXT
)) != NULL
)
7147 location
= attr
->values
[0].string
.text
;
7151 if (con
->username
[0])
7152 username
= con
->username
;
7153 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7154 IPP_TAG_NAME
)) != NULL
)
7155 username
= attr
->values
[0].string
.text
;
7159 ra
= create_requested_array(con
->request
);
7162 * OK, build a list of printers for this printer...
7165 if (first_printer_name
)
7167 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7168 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7171 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7174 count
< limit
&& printer
;
7175 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7177 if (!local
&& !printer
->shared
)
7180 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7181 (printer
->type
& printer_mask
) == printer_type
&&
7183 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7186 * If a username is specified, see if it is allowed or denied
7190 if (cupsArrayCount(printer
->users
) && username
&&
7191 !user_allowed(printer
, username
))
7195 * Add the group separator as needed...
7199 ippAddSeparator(con
->response
);
7204 * Send the attributes...
7207 copy_printer_attrs(con
, printer
, ra
);
7211 cupsArrayDelete(ra
);
7213 con
->response
->request
.status
.status_code
= IPP_OK
;
7218 * 'get_subscription_attrs()' - Get subscription attributes.
7222 get_subscription_attrs(
7223 cupsd_client_t
*con
, /* I - Client connection */
7224 int sub_id
) /* I - Subscription ID */
7226 http_status_t status
; /* Policy status */
7227 cupsd_subscription_t
*sub
; /* Subscription */
7228 cupsd_policy_t
*policy
; /* Current security policy */
7229 cups_array_t
*ra
, /* Requested attributes array */
7230 *exclude
; /* Private attributes array */
7233 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7234 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7235 con
, con
->number
, sub_id
);
7238 * Is the subscription ID valid?
7241 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7244 * Bad subscription ID...
7247 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7257 policy
= sub
->dest
->op_policy_ptr
;
7259 policy
= DefaultPolicyPtr
;
7261 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7263 send_http_error(con
, status
, sub
->dest
);
7267 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7270 * Copy the subscription attributes to the response using the
7271 * requested-attributes attribute that may be provided by the client.
7274 ra
= create_requested_array(con
->request
);
7276 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7278 cupsArrayDelete(ra
);
7280 con
->response
->request
.status
.status_code
= IPP_OK
;
7285 * 'get_subscriptions()' - Get subscriptions.
7289 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7290 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7292 http_status_t status
; /* Policy status */
7293 int count
; /* Number of subscriptions */
7294 int limit
; /* Limit */
7295 cupsd_subscription_t
*sub
; /* Subscription */
7296 cups_array_t
*ra
; /* Requested attributes array */
7297 ipp_attribute_t
*attr
; /* Attribute */
7298 cups_ptype_t dtype
; /* Destination type (printer/class) */
7299 char scheme
[HTTP_MAX_URI
],
7300 /* Scheme portion of URI */
7301 username
[HTTP_MAX_URI
],
7302 /* Username portion of URI */
7304 /* Host portion of URI */
7305 resource
[HTTP_MAX_URI
];
7306 /* Resource portion of URI */
7307 int port
; /* Port portion of URI */
7308 cupsd_job_t
*job
; /* Job pointer */
7309 cupsd_printer_t
*printer
; /* Printer */
7310 cupsd_policy_t
*policy
; /* Policy */
7311 cups_array_t
*exclude
; /* Private attributes array */
7314 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7315 "get_subscriptions(con=%p[%d], uri=%s)",
7316 con
, con
->number
, uri
->values
[0].string
.text
);
7319 * Is the destination valid?
7322 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7323 sizeof(scheme
), username
, sizeof(username
), host
,
7324 sizeof(host
), &port
, resource
, sizeof(resource
));
7326 if (!strcmp(resource
, "/") ||
7327 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7328 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7329 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7334 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7337 job
= cupsdFindJob(atoi(resource
+ 6));
7341 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7342 atoi(resource
+ 6));
7346 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7352 send_ipp_status(con
, IPP_NOT_FOUND
,
7353 _("The printer or class does not exist."));
7356 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7357 IPP_TAG_INTEGER
)) != NULL
)
7359 job
= cupsdFindJob(attr
->values
[0].integer
);
7363 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7364 attr
->values
[0].integer
);
7376 policy
= printer
->op_policy_ptr
;
7378 policy
= DefaultPolicyPtr
;
7380 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7382 send_http_error(con
, status
, printer
);
7387 * Copy the subscription attributes to the response using the
7388 * requested-attributes attribute that may be provided by the client.
7391 ra
= create_requested_array(con
->request
);
7393 if ((attr
= ippFindAttribute(con
->request
, "limit",
7394 IPP_TAG_INTEGER
)) != NULL
)
7395 limit
= attr
->values
[0].integer
;
7400 * See if we only want to see subscriptions for a specific user...
7403 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7404 IPP_TAG_BOOLEAN
)) != NULL
&&
7405 attr
->values
[0].boolean
)
7406 strlcpy(username
, get_username(con
), sizeof(username
));
7410 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7412 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7413 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7414 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7416 ippAddSeparator(con
->response
);
7418 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7419 policy
, con
, sub
->dest
,
7422 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7425 if (limit
&& count
>= limit
)
7429 cupsArrayDelete(ra
);
7432 con
->response
->request
.status
.status_code
= IPP_OK
;
7434 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7439 * 'get_username()' - Get the username associated with a request.
7442 static const char * /* O - Username */
7443 get_username(cupsd_client_t
*con
) /* I - Connection */
7445 ipp_attribute_t
*attr
; /* Attribute */
7448 if (con
->username
[0])
7449 return (con
->username
);
7450 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7451 IPP_TAG_NAME
)) != NULL
)
7452 return (attr
->values
[0].string
.text
);
7454 return ("anonymous");
7459 * 'hold_job()' - Hold a print job.
7463 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7464 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7466 ipp_attribute_t
*attr
; /* Current job-hold-until */
7467 const char *when
; /* New value */
7468 int jobid
; /* Job ID */
7469 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7470 username
[HTTP_MAX_URI
], /* Username portion of URI */
7471 host
[HTTP_MAX_URI
], /* Host portion of URI */
7472 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7473 int port
; /* Port portion of URI */
7474 cupsd_job_t
*job
; /* Job information */
7477 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7478 uri
->values
[0].string
.text
);
7481 * See if we have a job URI or a printer URI...
7484 if (!strcmp(uri
->name
, "printer-uri"))
7487 * Got a printer URI; see if we also have a job-id attribute...
7490 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7491 IPP_TAG_INTEGER
)) == NULL
)
7493 send_ipp_status(con
, IPP_BAD_REQUEST
,
7494 _("Got a printer-uri attribute but no job-id."));
7498 jobid
= attr
->values
[0].integer
;
7503 * Got a job URI; parse it to get the job ID...
7506 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7507 sizeof(scheme
), username
, sizeof(username
), host
,
7508 sizeof(host
), &port
, resource
, sizeof(resource
));
7510 if (strncmp(resource
, "/jobs/", 6))
7516 send_ipp_status(con
, IPP_BAD_REQUEST
,
7517 _("Bad job-uri \"%s\"."),
7518 uri
->values
[0].string
.text
);
7522 jobid
= atoi(resource
+ 6);
7526 * See if the job exists...
7529 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7532 * Nope - return a "not found" error...
7535 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7540 * See if the job is owned by the requesting user...
7543 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7545 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7546 cupsdFindDest(job
->dest
));
7551 * See if the job is in a state that allows holding...
7554 if (job
->state_value
> IPP_JOB_STOPPED
)
7557 * Return a "not-possible" error...
7560 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7561 _("Job #%d is finished and cannot be altered."),
7567 * Hold the job and return...
7570 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
7571 IPP_TAG_KEYWORD
)) == NULL
)
7572 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7576 when
= attr
->values
[0].string
.text
;
7578 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7579 "Job job-hold-until value changed by user.");
7582 when
= "indefinite";
7584 cupsdSetJobHoldUntil(job
, when
, 1);
7585 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
7588 con
->response
->request
.status
.status_code
= IPP_OK
;
7593 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7597 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
7598 ipp_attribute_t
*uri
) /* I - Printer URI */
7600 http_status_t status
; /* Policy status */
7601 cups_ptype_t dtype
; /* Destination type (printer/class) */
7602 cupsd_printer_t
*printer
; /* Printer data */
7605 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
7606 con
->number
, uri
->values
[0].string
.text
);
7609 * Is the destination valid?
7612 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7618 send_ipp_status(con
, IPP_NOT_FOUND
,
7619 _("The printer or class does not exist."));
7627 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7629 send_http_error(con
, status
, printer
);
7634 * Hold pending/new jobs sent to the printer...
7637 printer
->holding_new_jobs
= 1;
7639 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
7641 if (dtype
& CUPS_PRINTER_CLASS
)
7642 cupsdLogMessage(CUPSD_LOG_INFO
,
7643 "Class \"%s\" now holding pending/new jobs (\"%s\").",
7644 printer
->name
, get_username(con
));
7646 cupsdLogMessage(CUPSD_LOG_INFO
,
7647 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
7648 printer
->name
, get_username(con
));
7651 * Everything was ok, so return OK status...
7654 con
->response
->request
.status
.status_code
= IPP_OK
;
7659 * 'move_job()' - Move a job to a new destination.
7663 move_job(cupsd_client_t
*con
, /* I - Client connection */
7664 ipp_attribute_t
*uri
) /* I - Job URI */
7666 http_status_t status
; /* Policy status */
7667 ipp_attribute_t
*attr
; /* Current attribute */
7668 int jobid
; /* Job ID */
7669 cupsd_job_t
*job
; /* Current job */
7670 const char *src
; /* Source printer/class */
7671 cups_ptype_t stype
, /* Source type (printer or class) */
7672 dtype
; /* Destination type (printer/class) */
7673 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7674 username
[HTTP_MAX_URI
], /* Username portion of URI */
7675 host
[HTTP_MAX_URI
], /* Host portion of URI */
7676 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7677 int port
; /* Port portion of URI */
7678 cupsd_printer_t
*sprinter
, /* Source printer */
7679 *dprinter
; /* Destination printer */
7682 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
7683 uri
->values
[0].string
.text
);
7686 * Get the new printer or class...
7689 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7690 IPP_TAG_URI
)) == NULL
)
7693 * Need job-printer-uri...
7696 send_ipp_status(con
, IPP_BAD_REQUEST
,
7697 _("job-printer-uri attribute missing."));
7701 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7707 send_ipp_status(con
, IPP_NOT_FOUND
,
7708 _("The printer or class does not exist."));
7713 * See if we have a job URI or a printer URI...
7716 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7717 sizeof(scheme
), username
, sizeof(username
), host
,
7718 sizeof(host
), &port
, resource
, sizeof(resource
));
7720 if (!strcmp(uri
->name
, "printer-uri"))
7723 * Got a printer URI; see if we also have a job-id attribute...
7726 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7727 IPP_TAG_INTEGER
)) == NULL
)
7733 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7734 &sprinter
)) == NULL
)
7740 send_ipp_status(con
, IPP_NOT_FOUND
,
7741 _("The printer or class does not exist."));
7750 * Otherwise, just move a single job...
7753 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7756 * Nope - return a "not found" error...
7759 send_ipp_status(con
, IPP_NOT_FOUND
,
7760 _("Job #%d does not exist."), attr
->values
[0].integer
);
7766 * Job found, initialize source pointers...
7777 * Got a job URI; parse it to get the job ID...
7780 if (strncmp(resource
, "/jobs/", 6))
7786 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
7787 uri
->values
[0].string
.text
);
7792 * See if the job exists...
7795 jobid
= atoi(resource
+ 6);
7797 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7800 * Nope - return a "not found" error...
7803 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7809 * Job found, initialize source pointers...
7818 * Check the policy of the destination printer...
7821 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7822 job
? job
->username
: NULL
)) != HTTP_OK
)
7824 send_http_error(con
, status
, dprinter
);
7829 * Now move the job or jobs...
7835 * See if the job has been completed...
7838 if (job
->state_value
> IPP_JOB_STOPPED
)
7841 * Return a "not-possible" error...
7844 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7845 _("Job #%d is finished and cannot be altered."),
7851 * See if the job is owned by the requesting user...
7854 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7856 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7857 cupsdFindDest(job
->dest
));
7862 * Move the job to a different printer or class...
7865 cupsdMoveJob(job
, dprinter
);
7870 * Got the source printer, now look through the jobs...
7873 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7875 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7878 * See if the job is pointing at the source printer or has not been
7882 if (_cups_strcasecmp(job
->dest
, src
) ||
7883 job
->state_value
> IPP_JOB_STOPPED
)
7887 * See if the job can be moved by the requesting user...
7890 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7894 * Move the job to a different printer or class...
7897 cupsdMoveJob(job
, dprinter
);
7902 * Start jobs if possible...
7908 * Return with "everything is OK" status...
7911 con
->response
->request
.status
.status_code
= IPP_OK
;
7916 * 'ppd_parse_line()' - Parse a PPD default line.
7919 static int /* O - 0 on success, -1 on failure */
7920 ppd_parse_line(const char *line
, /* I - Line */
7921 char *option
, /* O - Option name */
7922 int olen
, /* I - Size of option name */
7923 char *choice
, /* O - Choice name */
7924 int clen
) /* I - Size of choice name */
7927 * Verify this is a default option line...
7930 if (strncmp(line
, "*Default", 8))
7934 * Read the option name...
7937 for (line
+= 8, olen
--;
7938 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
7949 * Skip everything else up to the colon (:)...
7952 while (*line
&& *line
!= ':')
7961 * Now grab the option choice, skipping leading whitespace...
7964 while (isspace(*line
& 255))
7968 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
7979 * Return with no errors...
7987 * 'print_job()' - Print a file to a printer or class.
7991 print_job(cupsd_client_t
*con
, /* I - Client connection */
7992 ipp_attribute_t
*uri
) /* I - Printer URI */
7994 ipp_attribute_t
*attr
; /* Current attribute */
7995 ipp_attribute_t
*doc_name
; /* document-name attribute */
7996 ipp_attribute_t
*format
; /* Document-format attribute */
7997 const char *default_format
; /* document-format-default value */
7998 cupsd_job_t
*job
; /* New job */
7999 char filename
[1024]; /* Job filename */
8000 mime_type_t
*filetype
; /* Type of file */
8001 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8002 type
[MIME_MAX_TYPE
], /* Subtype of file */
8003 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8004 /* Textual name of mime type */
8005 cupsd_printer_t
*printer
; /* Printer data */
8006 struct stat fileinfo
; /* File information */
8007 int kbytes
; /* Size of file */
8008 int compression
; /* Document compression */
8011 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8012 uri
->values
[0].string
.text
);
8015 * Validate print file attributes, for now just document-format and
8016 * compression (CUPS only supports "none" and "gzip")...
8019 compression
= CUPS_FILE_NONE
;
8021 if ((attr
= ippFindAttribute(con
->request
, "compression",
8022 IPP_TAG_KEYWORD
)) != NULL
)
8024 if (strcmp(attr
->values
[0].string
.text
, "none")
8026 && strcmp(attr
->values
[0].string
.text
, "gzip")
8027 #endif /* HAVE_LIBZ */
8030 send_ipp_status(con
, IPP_ATTRIBUTES
,
8031 _("Unsupported compression \"%s\"."),
8032 attr
->values
[0].string
.text
);
8033 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8034 "compression", NULL
, attr
->values
[0].string
.text
);
8039 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8040 compression
= CUPS_FILE_GZIP
;
8041 #endif /* HAVE_LIBZ */
8045 * Do we have a file to print?
8050 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8055 * Is the destination valid?
8058 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8064 send_ipp_status(con
, IPP_NOT_FOUND
,
8065 _("The printer or class does not exist."));
8070 * Is it a format we support?
8073 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8075 ippSetName(con
->request
, &doc_name
, "document-name-supplied");
8077 if ((format
= ippFindAttribute(con
->request
, "document-format",
8078 IPP_TAG_MIMETYPE
)) != NULL
)
8081 * Grab format from client...
8084 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8087 send_ipp_status(con
, IPP_BAD_REQUEST
,
8088 _("Bad document-format \"%s\"."),
8089 format
->values
[0].string
.text
);
8093 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
8095 else if ((default_format
= cupsGetOption("document-format",
8096 printer
->num_options
,
8097 printer
->options
)) != NULL
)
8100 * Use default document format...
8103 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8105 send_ipp_status(con
, IPP_BAD_REQUEST
,
8106 _("Bad document-format \"%s\"."),
8117 strlcpy(super
, "application", sizeof(super
));
8118 strlcpy(type
, "octet-stream", sizeof(type
));
8121 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8124 * Auto-type the file...
8127 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8130 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8131 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8135 filetype
= mimeType(MimeDatabase
, super
, type
);
8137 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8138 filetype
->super
, filetype
->type
);
8140 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
8141 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
8144 filetype
= mimeType(MimeDatabase
, super
, type
);
8148 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8151 * Replace the document-format attribute value with the auto-typed or
8155 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8159 ippSetString(con
->request
, &format
, 0, mimetype
);
8161 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8162 "document-format", NULL
, mimetype
);
8166 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8167 _("Unsupported document-format \"%s\"."),
8168 format
? format
->values
[0].string
.text
:
8169 "application/octet-stream");
8170 cupsdLogMessage(CUPSD_LOG_INFO
,
8171 "Hint: Do you have the raw file printing rules enabled?");
8174 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8175 "document-format", NULL
, format
->values
[0].string
.text
);
8181 * Read any embedded job ticket info from PS files...
8184 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8185 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8186 !_cups_strcasecmp(filetype
->type
, "pdf")))
8187 read_job_ticket(con
);
8190 * Create the job object...
8193 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8197 * Update quota data...
8200 if (stat(con
->filename
, &fileinfo
))
8203 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8205 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8207 job
->koctets
+= kbytes
;
8209 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8210 attr
->values
[0].integer
+= kbytes
;
8213 * Add the job file...
8216 if (add_file(con
, job
, filetype
, compression
))
8219 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
8220 if (rename(con
->filename
, filename
))
8222 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
8224 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
8228 cupsdClearString(&con
->filename
);
8231 * See if we need to add the ending sheet...
8234 if (cupsdTimeoutJob(job
))
8238 * Log and save the job...
8241 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8242 "File of type %s/%s queued by \"%s\".",
8243 filetype
->super
, filetype
->type
, job
->username
);
8244 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8245 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8246 job
->dest
, job
->username
);
8249 * Start the job if possible...
8257 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8259 * This function only gets called when printing a single PDF or PostScript
8260 * file using the Print-Job operation. It doesn't work for Create-Job +
8261 * Send-File, since the job attributes need to be set at job creation
8262 * time for banners to work. The embedded job ticket stuff is here
8263 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8264 * options and IPP attributes which otherwise would be lost.
8266 * The format of a job ticket is simple:
8268 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8270 * %cupsJobTicket: attr1=value1
8271 * %cupsJobTicket: attr2=value2
8273 * %cupsJobTicket: attrN=valueN
8275 * Job ticket lines must appear immediately after the first line that
8276 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8277 * stops looking for job ticket info when it finds a line that does not begin
8278 * with "%cupsJobTicket:".
8280 * The maximum length of a job ticket line, including the prefix, is
8281 * 255 characters to conform with the Adobe DSC.
8283 * Read-only attributes are rejected with a notice to the error log in
8284 * case a malicious user tries anything. Since the job ticket is read
8285 * prior to attribute validation in print_job(), job ticket attributes
8286 * will go through the same validation as IPP attributes...
8290 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8292 cups_file_t
*fp
; /* File to read from */
8293 char line
[256]; /* Line data */
8294 int num_options
; /* Number of options */
8295 cups_option_t
*options
; /* Options */
8296 ipp_t
*ticket
; /* New attributes */
8297 ipp_attribute_t
*attr
, /* Current attribute */
8298 *attr2
, /* Job attribute */
8299 *prev2
; /* Previous job attribute */
8303 * First open the print file...
8306 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8308 cupsdLogMessage(CUPSD_LOG_ERROR
,
8309 "Unable to open print file for job ticket - %s",
8315 * Skip the first line...
8318 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8320 cupsdLogMessage(CUPSD_LOG_ERROR
,
8321 "Unable to read from print file for job ticket - %s",
8327 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8330 * Not a DSC-compliant file, so no job ticket info will be available...
8338 * Read job ticket info from the file...
8344 while (cupsFileGets(fp
, line
, sizeof(line
)))
8347 * Stop at the first non-ticket line...
8350 if (strncmp(line
, "%cupsJobTicket:", 15))
8354 * Add the options to the option array...
8357 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8361 * Done with the file; see if we have any options...
8366 if (num_options
== 0)
8370 * OK, convert the options to an attribute list, and apply them to
8375 cupsEncodeOptions(ticket
, num_options
, options
);
8378 * See what the user wants to change.
8381 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8383 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8386 if (!strncmp(attr
->name
, "date-time-at-", 13) ||
8387 !strcmp(attr
->name
, "job-impressions-completed") ||
8388 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8389 !strncmp(attr
->name
, "job-k-octets", 12) ||
8390 !strcmp(attr
->name
, "job-id") ||
8391 !strcmp(attr
->name
, "job-originating-host-name") ||
8392 !strcmp(attr
->name
, "job-originating-user-name") ||
8393 !strcmp(attr
->name
, "job-pages-completed") ||
8394 !strcmp(attr
->name
, "job-printer-uri") ||
8395 !strncmp(attr
->name
, "job-state", 9) ||
8396 !strcmp(attr
->name
, "job-uri") ||
8397 !strncmp(attr
->name
, "time-at-", 8))
8398 continue; /* Read-only attrs */
8400 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8401 IPP_TAG_ZERO
)) != NULL
)
8404 * Some other value; first free the old value...
8407 if (con
->request
->attrs
== attr2
)
8409 con
->request
->attrs
= attr2
->next
;
8414 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8415 if (prev2
->next
== attr2
)
8417 prev2
->next
= attr2
->next
;
8422 if (con
->request
->last
== attr2
)
8423 con
->request
->last
= prev2
;
8425 ippDeleteAttribute(NULL
, attr2
);
8429 * Add new option by copying it...
8432 ippCopyAttribute(con
->request
, attr
, 0);
8436 * Then free the attribute list and option array...
8440 cupsFreeOptions(num_options
, options
);
8445 * 'reject_jobs()' - Reject print jobs to a printer.
8449 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8450 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8452 http_status_t status
; /* Policy status */
8453 cups_ptype_t dtype
; /* Destination type (printer/class) */
8454 cupsd_printer_t
*printer
; /* Printer data */
8455 ipp_attribute_t
*attr
; /* printer-state-message text */
8458 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8459 con
->number
, uri
->values
[0].string
.text
);
8462 * Is the destination valid?
8465 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8471 send_ipp_status(con
, IPP_NOT_FOUND
,
8472 _("The printer or class does not exist."));
8480 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8482 send_http_error(con
, status
, printer
);
8487 * Reject jobs sent to the printer...
8490 printer
->accepting
= 0;
8492 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8493 IPP_TAG_TEXT
)) == NULL
)
8494 strlcpy(printer
->state_message
, "Rejecting Jobs",
8495 sizeof(printer
->state_message
));
8497 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8498 sizeof(printer
->state_message
));
8500 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8501 "No longer accepting jobs.");
8503 if (dtype
& CUPS_PRINTER_CLASS
)
8505 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8507 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8508 printer
->name
, get_username(con
));
8512 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8514 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8515 printer
->name
, get_username(con
));
8519 * Everything was ok, so return OK status...
8522 con
->response
->request
.status
.status_code
= IPP_OK
;
8527 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8531 release_held_new_jobs(
8532 cupsd_client_t
*con
, /* I - Connection */
8533 ipp_attribute_t
*uri
) /* I - Printer URI */
8535 http_status_t status
; /* Policy status */
8536 cups_ptype_t dtype
; /* Destination type (printer/class) */
8537 cupsd_printer_t
*printer
; /* Printer data */
8540 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8541 con
->number
, uri
->values
[0].string
.text
);
8544 * Is the destination valid?
8547 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8553 send_ipp_status(con
, IPP_NOT_FOUND
,
8554 _("The printer or class does not exist."));
8562 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8564 send_http_error(con
, status
, printer
);
8569 * Hold pending/new jobs sent to the printer...
8572 printer
->holding_new_jobs
= 0;
8574 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8576 if (dtype
& CUPS_PRINTER_CLASS
)
8577 cupsdLogMessage(CUPSD_LOG_INFO
,
8578 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8579 printer
->name
, get_username(con
));
8581 cupsdLogMessage(CUPSD_LOG_INFO
,
8582 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8583 printer
->name
, get_username(con
));
8586 * Everything was ok, so return OK status...
8589 con
->response
->request
.status
.status_code
= IPP_OK
;
8594 * 'release_job()' - Release a held print job.
8598 release_job(cupsd_client_t
*con
, /* I - Client connection */
8599 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8601 ipp_attribute_t
*attr
; /* Current attribute */
8602 int jobid
; /* Job ID */
8603 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8604 username
[HTTP_MAX_URI
], /* Username portion of URI */
8605 host
[HTTP_MAX_URI
], /* Host portion of URI */
8606 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8607 int port
; /* Port portion of URI */
8608 cupsd_job_t
*job
; /* Job information */
8611 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8612 con
->number
, uri
->values
[0].string
.text
);
8615 * See if we have a job URI or a printer URI...
8618 if (!strcmp(uri
->name
, "printer-uri"))
8621 * Got a printer URI; see if we also have a job-id attribute...
8624 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8625 IPP_TAG_INTEGER
)) == NULL
)
8627 send_ipp_status(con
, IPP_BAD_REQUEST
,
8628 _("Got a printer-uri attribute but no job-id."));
8632 jobid
= attr
->values
[0].integer
;
8637 * Got a job URI; parse it to get the job ID...
8640 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8641 sizeof(scheme
), username
, sizeof(username
), host
,
8642 sizeof(host
), &port
, resource
, sizeof(resource
));
8644 if (strncmp(resource
, "/jobs/", 6))
8650 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8651 uri
->values
[0].string
.text
);
8655 jobid
= atoi(resource
+ 6);
8659 * See if the job exists...
8662 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8665 * Nope - return a "not found" error...
8668 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8673 * See if job is "held"...
8676 if (job
->state_value
!= IPP_JOB_HELD
)
8679 * Nope - return a "not possible" error...
8682 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
8687 * See if the job is owned by the requesting user...
8690 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8692 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8693 cupsdFindDest(job
->dest
));
8698 * Reset the job-hold-until value to "no-hold"...
8701 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8702 IPP_TAG_KEYWORD
)) == NULL
)
8703 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8707 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
8708 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
8710 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8711 "Job job-hold-until value changed by user.");
8712 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
8716 * Release the job and return...
8719 cupsdReleaseJob(job
);
8721 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8722 "Job released by user.");
8724 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8726 con
->response
->request
.status
.status_code
= IPP_OK
;
8733 * 'renew_subscription()' - Renew an existing subscription...
8738 cupsd_client_t
*con
, /* I - Client connection */
8739 int sub_id
) /* I - Subscription ID */
8741 http_status_t status
; /* Policy status */
8742 cupsd_subscription_t
*sub
; /* Subscription */
8743 ipp_attribute_t
*lease
; /* notify-lease-duration */
8746 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8747 "renew_subscription(con=%p[%d], sub_id=%d)",
8748 con
, con
->number
, sub_id
);
8751 * Is the subscription ID valid?
8754 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8757 * Bad subscription ID...
8760 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
8768 * Job subscriptions cannot be renewed...
8771 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8772 _("Job subscriptions cannot be renewed."));
8780 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8782 con
, sub
->owner
)) != HTTP_OK
)
8784 send_http_error(con
, status
, sub
->dest
);
8789 * Renew the subscription...
8792 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8795 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8797 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8799 cupsdLogMessage(CUPSD_LOG_INFO
,
8800 "renew_subscription: Limiting notify-lease-duration to "
8803 sub
->lease
= MaxLeaseDuration
;
8806 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8808 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
8810 con
->response
->request
.status
.status_code
= IPP_OK
;
8812 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8813 "notify-lease-duration", sub
->lease
);
8818 * 'restart_job()' - Restart an old print job.
8822 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8823 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8825 ipp_attribute_t
*attr
; /* Current attribute */
8826 int jobid
; /* Job ID */
8827 cupsd_job_t
*job
; /* Job information */
8828 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8829 username
[HTTP_MAX_URI
], /* Username portion of URI */
8830 host
[HTTP_MAX_URI
], /* Host portion of URI */
8831 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8832 int port
; /* Port portion of URI */
8835 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8836 con
->number
, uri
->values
[0].string
.text
);
8839 * See if we have a job URI or a printer URI...
8842 if (!strcmp(uri
->name
, "printer-uri"))
8845 * Got a printer URI; see if we also have a job-id attribute...
8848 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8849 IPP_TAG_INTEGER
)) == NULL
)
8851 send_ipp_status(con
, IPP_BAD_REQUEST
,
8852 _("Got a printer-uri attribute but no job-id."));
8856 jobid
= attr
->values
[0].integer
;
8861 * Got a job URI; parse it to get the job ID...
8864 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8865 sizeof(scheme
), username
, sizeof(username
), host
,
8866 sizeof(host
), &port
, resource
, sizeof(resource
));
8868 if (strncmp(resource
, "/jobs/", 6))
8874 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8875 uri
->values
[0].string
.text
);
8879 jobid
= atoi(resource
+ 6);
8883 * See if the job exists...
8886 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8889 * Nope - return a "not found" error...
8892 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8897 * See if job is in any of the "completed" states...
8900 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8903 * Nope - return a "not possible" error...
8906 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
8912 * See if we have retained the job files...
8917 if (!job
->attrs
|| job
->num_files
== 0)
8920 * Nope - return a "not possible" error...
8923 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8924 _("Job #%d cannot be restarted - no files."), jobid
);
8929 * See if the job is owned by the requesting user...
8932 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8934 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8935 cupsdFindDest(job
->dest
));
8940 * See if the job-hold-until attribute is specified...
8943 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
8944 IPP_TAG_KEYWORD
)) == NULL
)
8945 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
8947 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
8950 * Return the job to a held state...
8953 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
8954 "Restarted by \"%s\" with job-hold-until=%s.",
8955 username
, attr
->values
[0].string
.text
);
8956 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
8958 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
8959 NULL
, job
, "Job restarted by user with job-hold-until=%s",
8960 attr
->values
[0].string
.text
);
8965 * Restart the job...
8968 cupsdRestartJob(job
);
8972 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
8974 con
->response
->request
.status
.status_code
= IPP_OK
;
8979 * 'save_auth_info()' - Save authentication information for a job.
8984 cupsd_client_t
*con
, /* I - Client connection */
8985 cupsd_job_t
*job
, /* I - Job */
8986 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8988 int i
; /* Looping var */
8989 char filename
[1024]; /* Job authentication filename */
8990 cups_file_t
*fp
; /* Job authentication file */
8991 char line
[65536]; /* Line for file */
8992 cupsd_printer_t
*dest
; /* Destination printer/class */
8996 * This function saves the in-memory authentication information for
8997 * a job so that it can be used to authenticate with a remote host.
8998 * The information is stored in a file that is readable only by the
8999 * root user. The fields are Base-64 encoded, each on a separate line,
9000 * followed by random number (up to 1024) of newlines to limit the
9001 * amount of information that is exposed.
9003 * Because of the potential for exposing of authentication information,
9004 * this functionality is only enabled when running cupsd as root.
9006 * This caching only works for the Basic and BasicDigest authentication
9007 * types. Digest authentication cannot be cached this way, and in
9008 * the future Kerberos authentication may make all of this obsolete.
9010 * Authentication information is saved whenever an authenticated
9011 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9014 * This information is deleted after a job is completed or canceled,
9015 * so reprints may require subsequent re-authentication.
9021 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9025 * Create the authentication file and change permissions...
9028 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9029 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9031 cupsdLogMessage(CUPSD_LOG_ERROR
,
9032 "Unable to save authentication info to \"%s\" - %s",
9033 filename
, strerror(errno
));
9037 fchown(cupsFileNumber(fp
), 0, 0);
9038 fchmod(cupsFileNumber(fp
), 0400);
9040 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9043 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9045 cupsdClearString(job
->auth_env
+ i
);
9047 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9050 * Write 1 to 3 auth values...
9054 i
< auth_info
->num_values
&&
9055 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9058 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9060 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9061 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9064 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9065 auth_info
->values
[i
].string
.text
);
9067 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9068 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9069 auth_info
->values
[i
].string
.text
);
9070 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9071 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9072 auth_info
->values
[i
].string
.text
);
9073 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9074 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9075 auth_info
->values
[i
].string
.text
);
9076 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9077 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9078 auth_info
->values
[i
].string
.text
);
9083 else if (auth_info
&& auth_info
->num_values
== 2 &&
9084 dest
->num_auth_info_required
== 1 &&
9085 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9088 * Allow fallback to username+password for Kerberized queues...
9091 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9092 cupsFilePutConf(fp
, "username", line
);
9094 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9095 auth_info
->values
[0].string
.text
);
9097 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9098 cupsFilePutConf(fp
, "password", line
);
9100 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9101 auth_info
->values
[1].string
.text
);
9103 else if (con
->username
[0])
9106 * Write the authenticated username...
9109 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9110 cupsFilePutConf(fp
, "username", line
);
9112 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9115 * Write the authenticated password...
9118 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9119 cupsFilePutConf(fp
, "password", line
);
9121 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9125 if (con
->gss_uid
> 0)
9127 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9128 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9130 #endif /* HAVE_GSSAPI */
9133 * Write a random number of newlines to the end of the file...
9136 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9137 cupsFilePutChar(fp
, '\n');
9140 * Close the file and return...
9148 * 'send_document()' - Send a file to a printer or class.
9152 send_document(cupsd_client_t
*con
, /* I - Client connection */
9153 ipp_attribute_t
*uri
) /* I - Printer URI */
9155 ipp_attribute_t
*attr
; /* Current attribute */
9156 ipp_attribute_t
*format
; /* Request's document-format attribute */
9157 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9158 const char *default_format
;/* document-format-default value */
9159 int jobid
; /* Job ID number */
9160 cupsd_job_t
*job
; /* Current job */
9161 char job_uri
[HTTP_MAX_URI
],
9163 scheme
[HTTP_MAX_URI
],
9164 /* Method portion of URI */
9165 username
[HTTP_MAX_URI
],
9166 /* Username portion of URI */
9168 /* Host portion of URI */
9169 resource
[HTTP_MAX_URI
];
9170 /* Resource portion of URI */
9171 int port
; /* Port portion of URI */
9172 mime_type_t
*filetype
; /* Type of file */
9173 char super
[MIME_MAX_SUPER
],
9174 /* Supertype of file */
9175 type
[MIME_MAX_TYPE
],
9176 /* Subtype of file */
9177 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9178 /* Textual name of mime type */
9179 char filename
[1024]; /* Job filename */
9180 cupsd_printer_t
*printer
; /* Current printer */
9181 struct stat fileinfo
; /* File information */
9182 int kbytes
; /* Size of file */
9183 int compression
; /* Type of compression */
9184 int start_job
; /* Start the job? */
9187 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9188 con
->number
, uri
->values
[0].string
.text
);
9191 * See if we have a job URI or a printer URI...
9194 if (!strcmp(uri
->name
, "printer-uri"))
9197 * Got a printer URI; see if we also have a job-id attribute...
9200 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9201 IPP_TAG_INTEGER
)) == NULL
)
9203 send_ipp_status(con
, IPP_BAD_REQUEST
,
9204 _("Got a printer-uri attribute but no job-id."));
9208 jobid
= attr
->values
[0].integer
;
9213 * Got a job URI; parse it to get the job ID...
9216 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9217 sizeof(scheme
), username
, sizeof(username
), host
,
9218 sizeof(host
), &port
, resource
, sizeof(resource
));
9220 if (strncmp(resource
, "/jobs/", 6))
9226 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9227 uri
->values
[0].string
.text
);
9231 jobid
= atoi(resource
+ 6);
9235 * See if the job exists...
9238 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9241 * Nope - return a "not found" error...
9244 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9248 printer
= cupsdFindDest(job
->dest
);
9251 * See if the job is owned by the requesting user...
9254 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9256 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9257 cupsdFindDest(job
->dest
));
9262 * OK, see if the client is sending the document compressed - CUPS
9263 * only supports "none" and "gzip".
9266 compression
= CUPS_FILE_NONE
;
9268 if ((attr
= ippFindAttribute(con
->request
, "compression",
9269 IPP_TAG_KEYWORD
)) != NULL
)
9271 if (strcmp(attr
->values
[0].string
.text
, "none")
9273 && strcmp(attr
->values
[0].string
.text
, "gzip")
9274 #endif /* HAVE_LIBZ */
9277 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9278 attr
->values
[0].string
.text
);
9279 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9280 "compression", NULL
, attr
->values
[0].string
.text
);
9285 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9286 compression
= CUPS_FILE_GZIP
;
9287 #endif /* HAVE_LIBZ */
9291 * Do we have a file to print?
9294 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9295 IPP_TAG_BOOLEAN
)) == NULL
)
9297 send_ipp_status(con
, IPP_BAD_REQUEST
,
9298 _("Missing last-document attribute in request."));
9305 * Check for an empty request with "last-document" set to true, which is
9306 * used to close an "open" job by RFC 2911, section 3.3.2.
9309 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9312 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9317 * Is it a format we support?
9322 if ((format
= ippFindAttribute(con
->request
, "document-format",
9323 IPP_TAG_MIMETYPE
)) != NULL
)
9326 * Grab format from client...
9329 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9332 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9333 format
->values
[0].string
.text
);
9337 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
9339 else if ((default_format
= cupsGetOption("document-format",
9340 printer
->num_options
,
9341 printer
->options
)) != NULL
)
9344 * Use default document format...
9347 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9349 send_ipp_status(con
, IPP_BAD_REQUEST
,
9350 _("Bad document-format-default \"%s\"."), default_format
);
9357 * No document format attribute? Auto-type it!
9360 strlcpy(super
, "application", sizeof(super
));
9361 strlcpy(type
, "octet-stream", sizeof(type
));
9364 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9367 * Auto-type the file...
9370 ipp_attribute_t
*doc_name
; /* document-name attribute */
9373 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9375 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9376 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9377 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9381 filetype
= mimeType(MimeDatabase
, super
, type
);
9384 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9385 filetype
->super
, filetype
->type
);
9387 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
9388 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
9391 filetype
= mimeType(MimeDatabase
, super
, type
);
9396 * Replace the document-format attribute value with the auto-typed or
9400 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9403 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9404 IPP_TAG_MIMETYPE
)) != NULL
)
9405 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9407 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9408 "document-format", NULL
, mimetype
);
9412 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9413 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9414 cupsdLogMessage(CUPSD_LOG_INFO
,
9415 "Hint: Do you have the raw file printing rules enabled?");
9418 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9419 "document-format", NULL
, format
->values
[0].string
.text
);
9424 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9426 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9429 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9430 _("Unsupported document-format \"%s\"."), mimetype
);
9432 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9433 "document-format", NULL
, mimetype
);
9439 * Add the file to the job...
9442 if (add_file(con
, job
, filetype
, compression
))
9445 if ((attr
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
)) != NULL
)
9446 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name-supplied", NULL
, ippGetString(attr
, 0, NULL
));
9448 if (stat(con
->filename
, &fileinfo
))
9451 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9453 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9455 job
->koctets
+= kbytes
;
9457 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9458 attr
->values
[0].integer
+= kbytes
;
9460 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
9461 if (rename(con
->filename
, filename
))
9463 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
9465 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
9469 cupsdClearString(&con
->filename
);
9471 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9472 filetype
->super
, filetype
->type
, job
->username
);
9475 * Start the job if this is the last document...
9480 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9481 IPP_TAG_BOOLEAN
)) != NULL
&&
9482 attr
->values
[0].boolean
)
9485 * See if we need to add the ending sheet...
9488 if (cupsdTimeoutJob(job
))
9491 if (job
->state_value
== IPP_JOB_STOPPED
)
9493 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9494 job
->state_value
= IPP_JOB_PENDING
;
9496 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9498 else if (job
->state_value
== IPP_JOB_HELD
)
9500 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9501 IPP_TAG_KEYWORD
)) == NULL
)
9502 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9504 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9506 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9507 job
->state_value
= IPP_JOB_PENDING
;
9509 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9512 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9516 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9522 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9523 IPP_TAG_KEYWORD
)) == NULL
)
9524 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9526 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9528 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9529 job
->state_value
= IPP_JOB_HELD
;
9530 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9532 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9535 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9542 * Fill in the response info...
9545 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9546 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9547 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9550 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9552 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9554 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
9555 NULL
, job
->reasons
->values
[0].string
.text
);
9557 con
->response
->request
.status
.status_code
= IPP_OK
;
9560 * Start the job if necessary...
9569 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9574 cupsd_client_t
*con
, /* I - Client connection */
9575 http_status_t status
, /* I - HTTP status code */
9576 cupsd_printer_t
*printer
) /* I - Printer, if any */
9578 ipp_attribute_t
*uri
; /* Request URI, if any */
9581 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9582 IPP_TAG_URI
)) == NULL
)
9583 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9585 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9586 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9587 con
->number
, httpStatus(status
),
9589 ippOpString(con
->request
->request
.op
.operation_id
) :
9591 uri
? uri
->values
[0].string
.text
: "no URI",
9592 con
->http
->hostname
);
9596 int auth_type
; /* Type of authentication required */
9599 auth_type
= CUPSD_AUTH_NONE
;
9601 if (status
== HTTP_UNAUTHORIZED
&&
9602 printer
->num_auth_info_required
> 0 &&
9603 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9605 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9606 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9607 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9610 * Creating and authenticating jobs requires Kerberos...
9613 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9618 * Use policy/location-defined authentication requirements...
9621 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9622 cupsd_location_t
*auth
; /* Pointer to authentication element */
9625 if (printer
->type
& CUPS_PRINTER_CLASS
)
9626 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9628 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9630 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9631 auth
->type
== CUPSD_AUTH_NONE
)
9632 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9634 con
->request
->request
.op
.operation_id
:
9639 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9640 auth_type
= cupsdDefaultAuthType();
9642 auth_type
= auth
->type
;
9646 cupsdSendError(con
, status
, auth_type
);
9649 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9651 ippDelete(con
->response
);
9652 con
->response
= NULL
;
9659 * 'send_ipp_status()' - Send a status back to the IPP client.
9663 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9664 ipp_status_t status
, /* I - IPP status code */
9665 const char *message
,/* I - Status message */
9666 ...) /* I - Additional args as needed */
9668 va_list ap
; /* Pointer to additional args */
9669 char formatted
[1024]; /* Formatted errror message */
9672 va_start(ap
, message
);
9673 vsnprintf(formatted
, sizeof(formatted
),
9674 _cupsLangString(con
->language
, message
), ap
);
9677 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9678 ippOpString(con
->request
->request
.op
.operation_id
),
9679 ippErrorString(status
), formatted
);
9681 con
->response
->request
.status
.status_code
= status
;
9683 if (ippFindAttribute(con
->response
, "attributes-charset",
9684 IPP_TAG_ZERO
) == NULL
)
9685 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9686 "attributes-charset", NULL
, "utf-8");
9688 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9689 IPP_TAG_ZERO
) == NULL
)
9690 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9691 "attributes-natural-language", NULL
, DefaultLanguage
);
9693 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9694 "status-message", NULL
, formatted
);
9699 * 'set_default()' - Set the default destination...
9703 set_default(cupsd_client_t
*con
, /* I - Client connection */
9704 ipp_attribute_t
*uri
) /* I - Printer URI */
9706 http_status_t status
; /* Policy status */
9707 cups_ptype_t dtype
; /* Destination type (printer/class) */
9708 cupsd_printer_t
*printer
, /* Printer */
9709 *oldprinter
; /* Old default printer */
9712 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9713 con
->number
, uri
->values
[0].string
.text
);
9716 * Is the destination valid?
9719 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9725 send_ipp_status(con
, IPP_NOT_FOUND
,
9726 _("The printer or class does not exist."));
9734 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9736 send_http_error(con
, status
, NULL
);
9741 * Set it as the default...
9744 oldprinter
= DefaultPrinter
;
9745 DefaultPrinter
= printer
;
9748 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9749 "%s is no longer the default printer.", oldprinter
->name
);
9751 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9752 "%s is now the default printer.", printer
->name
);
9754 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9755 CUPSD_DIRTY_PRINTCAP
);
9757 cupsdLogMessage(CUPSD_LOG_INFO
,
9758 "Default destination set to \"%s\" by \"%s\".",
9759 printer
->name
, get_username(con
));
9762 * Everything was ok, so return OK status...
9765 con
->response
->request
.status
.status_code
= IPP_OK
;
9770 * 'set_job_attrs()' - Set job attributes.
9774 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9775 ipp_attribute_t
*uri
) /* I - Job URI */
9777 ipp_attribute_t
*attr
, /* Current attribute */
9778 *attr2
; /* Job attribute */
9779 int jobid
; /* Job ID */
9780 cupsd_job_t
*job
; /* Current job */
9781 char scheme
[HTTP_MAX_URI
],
9782 /* Method portion of URI */
9783 username
[HTTP_MAX_URI
],
9784 /* Username portion of URI */
9786 /* Host portion of URI */
9787 resource
[HTTP_MAX_URI
];
9788 /* Resource portion of URI */
9789 int port
; /* Port portion of URI */
9790 int event
; /* Events? */
9791 int check_jobs
; /* Check jobs? */
9794 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9795 con
->number
, uri
->values
[0].string
.text
);
9798 * Start with "everything is OK" status...
9801 con
->response
->request
.status
.status_code
= IPP_OK
;
9804 * See if we have a job URI or a printer URI...
9807 if (!strcmp(uri
->name
, "printer-uri"))
9810 * Got a printer URI; see if we also have a job-id attribute...
9813 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9814 IPP_TAG_INTEGER
)) == NULL
)
9816 send_ipp_status(con
, IPP_BAD_REQUEST
,
9817 _("Got a printer-uri attribute but no job-id."));
9821 jobid
= attr
->values
[0].integer
;
9826 * Got a job URI; parse it to get the job ID...
9829 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9830 sizeof(scheme
), username
, sizeof(username
), host
,
9831 sizeof(host
), &port
, resource
, sizeof(resource
));
9833 if (strncmp(resource
, "/jobs/", 6))
9839 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9840 uri
->values
[0].string
.text
);
9844 jobid
= atoi(resource
+ 6);
9848 * See if the job exists...
9851 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9854 * Nope - return a "not found" error...
9857 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9862 * See if the job has been completed...
9865 if (job
->state_value
> IPP_JOB_STOPPED
)
9868 * Return a "not-possible" error...
9871 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9872 _("Job #%d is finished and cannot be altered."), jobid
);
9877 * See if the job is owned by the requesting user...
9880 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9882 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9883 cupsdFindDest(job
->dest
));
9888 * See what the user wants to change.
9896 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9898 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9901 if (!strcmp(attr
->name
, "attributes-charset") ||
9902 !strcmp(attr
->name
, "attributes-natural-language") ||
9903 !strncmp(attr
->name
, "date-time-at-", 13) ||
9904 !strncmp(attr
->name
, "document-compression", 20) ||
9905 !strncmp(attr
->name
, "document-format", 15) ||
9906 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9907 !strcmp(attr
->name
, "job-document-access-errors") ||
9908 !strcmp(attr
->name
, "job-id") ||
9909 !strcmp(attr
->name
, "job-impressions-completed") ||
9910 !strcmp(attr
->name
, "job-k-octets-completed") ||
9911 !strcmp(attr
->name
, "job-media-sheets-completed") ||
9912 !strcmp(attr
->name
, "job-originating-host-name") ||
9913 !strcmp(attr
->name
, "job-originating-user-name") ||
9914 !strcmp(attr
->name
, "job-pages-completed") ||
9915 !strcmp(attr
->name
, "job-printer-up-time") ||
9916 !strcmp(attr
->name
, "job-printer-uri") ||
9917 !strcmp(attr
->name
, "job-sheets") ||
9918 !strcmp(attr
->name
, "job-state-message") ||
9919 !strcmp(attr
->name
, "job-state-reasons") ||
9920 !strcmp(attr
->name
, "job-uri") ||
9921 !strcmp(attr
->name
, "number-of-documents") ||
9922 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9923 !strcmp(attr
->name
, "output-device-assigned") ||
9924 !strncmp(attr
->name
, "time-at-", 8))
9930 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9931 _("%s cannot be changed."), attr
->name
);
9933 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9934 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9938 if (!strcmp(attr
->name
, "job-priority"))
9941 * Change the job priority...
9944 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9946 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
9948 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9949 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9951 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9953 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9954 _("Job is completed and cannot be changed."));
9957 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9959 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
9960 attr
->values
[0].integer
);
9961 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9964 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
9965 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
9968 else if (!strcmp(attr
->name
, "job-state"))
9971 * Change the job state...
9974 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9976 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
9978 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9979 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9983 switch (attr
->values
[0].integer
)
9985 case IPP_JOB_PENDING
:
9987 if (job
->state_value
> IPP_JOB_HELD
)
9989 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9990 _("Job state cannot be changed."));
9993 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9995 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
9996 attr
->values
[0].integer
);
9997 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
10002 case IPP_JOB_PROCESSING
:
10003 case IPP_JOB_STOPPED
:
10004 if (job
->state_value
!= attr
->values
[0].integer
)
10006 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10007 _("Job state cannot be changed."));
10012 case IPP_JOB_CANCELED
:
10013 case IPP_JOB_ABORTED
:
10014 case IPP_JOB_COMPLETED
:
10015 if (job
->state_value
> IPP_JOB_PROCESSING
)
10017 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10018 _("Job state cannot be changed."));
10021 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10023 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10024 attr
->values
[0].integer
);
10025 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10027 "Job state changed by \"%s\"", username
);
10034 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10036 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10037 IPP_TAG_ZERO
)) != NULL
)
10040 * Some other value; first free the old value...
10043 if (job
->attrs
->prev
)
10044 job
->attrs
->prev
->next
= attr2
->next
;
10046 job
->attrs
->attrs
= attr2
->next
;
10048 if (job
->attrs
->last
== attr2
)
10049 job
->attrs
->last
= job
->attrs
->prev
;
10051 ippDeleteAttribute(NULL
, attr2
);
10054 * Then copy the attribute...
10057 ippCopyAttribute(job
->attrs
, attr
, 0);
10060 * See if the job-name or job-hold-until is being changed.
10063 if (!strcmp(attr
->name
, "job-hold-until"))
10065 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10066 attr
->values
[0].string
.text
);
10067 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10069 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10071 cupsdReleaseJob(job
);
10075 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10076 "Job held by \"%s\".", username
);
10078 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10081 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10084 * Delete the attribute...
10087 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10088 IPP_TAG_ZERO
)) != NULL
)
10090 if (job
->attrs
->prev
)
10091 job
->attrs
->prev
->next
= attr2
->next
;
10093 job
->attrs
->attrs
= attr2
->next
;
10095 if (attr2
== job
->attrs
->last
)
10096 job
->attrs
->last
= job
->attrs
->prev
;
10098 ippDeleteAttribute(NULL
, attr2
);
10100 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10106 * Add new option by copying it...
10109 ippCopyAttribute(job
->attrs
, attr
, 0);
10111 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10120 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10123 * Send events as needed...
10126 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10127 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10128 cupsdFindDest(job
->dest
), job
,
10129 "Job priority changed by user.");
10131 if (event
& CUPSD_EVENT_JOB_STATE
)
10132 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10133 job
->state_value
== IPP_JOB_HELD
?
10134 "Job held by user." : "Job restarted by user.");
10136 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10137 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10138 "Job options changed by user.");
10141 * Start jobs if possible...
10150 * 'set_printer_attrs()' - Set printer attributes.
10154 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10155 ipp_attribute_t
*uri
) /* I - Printer */
10157 http_status_t status
; /* Policy status */
10158 cups_ptype_t dtype
; /* Destination type (printer/class) */
10159 cupsd_printer_t
*printer
; /* Printer/class */
10160 ipp_attribute_t
*attr
; /* Printer attribute */
10161 int changed
= 0; /* Was anything changed? */
10164 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10165 con
->number
, uri
->values
[0].string
.text
);
10168 * Is the destination valid?
10171 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10177 send_ipp_status(con
, IPP_NOT_FOUND
,
10178 _("The printer or class does not exist."));
10186 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10188 send_http_error(con
, status
, printer
);
10193 * Return a list of attributes that can be set via Set-Printer-Attributes.
10196 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10197 IPP_TAG_TEXT
)) != NULL
)
10199 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10203 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
10205 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
10209 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
10211 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
10215 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
10217 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
10221 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10222 IPP_TAG_TEXT
)) != NULL
)
10224 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10229 * Update the printer attributes and return...
10234 printer
->config_time
= time(NULL
);
10236 cupsdSetPrinterAttrs(printer
);
10237 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10239 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10240 "Printer \"%s\" description or location changed by \"%s\".",
10241 printer
->name
, get_username(con
));
10243 cupsdLogMessage(CUPSD_LOG_INFO
,
10244 "Printer \"%s\" description or location changed by \"%s\".",
10245 printer
->name
, get_username(con
));
10248 con
->response
->request
.status
.status_code
= IPP_OK
;
10253 * 'set_printer_defaults()' - Set printer default options from a request.
10257 set_printer_defaults(
10258 cupsd_client_t
*con
, /* I - Client connection */
10259 cupsd_printer_t
*printer
) /* I - Printer */
10261 int i
; /* Looping var */
10262 ipp_attribute_t
*attr
; /* Current attribute */
10263 size_t namelen
; /* Length of attribute name */
10264 char name
[256], /* New attribute name */
10265 value
[256]; /* String version of integer attrs */
10268 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10271 * Skip non-printer attributes...
10274 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10277 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10279 if (!strcmp(attr
->name
, "job-sheets-default"))
10282 * Only allow keywords and names...
10285 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10289 * Only allow job-sheets-default to be set when running without a
10290 * system high classification level...
10293 if (Classification
)
10296 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10298 if (attr
->num_values
> 1)
10299 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10301 cupsdSetString(&printer
->job_sheets
[1], "none");
10303 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10305 cupsdFreeStrings(&(printer
->users
));
10307 printer
->deny_users
= 0;
10309 if (attr
->value_tag
== IPP_TAG_NAME
&&
10310 (attr
->num_values
> 1 ||
10311 strcmp(attr
->values
[0].string
.text
, "all")))
10313 for (i
= 0; i
< attr
->num_values
; i
++)
10314 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10317 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10319 cupsdFreeStrings(&(printer
->users
));
10321 printer
->deny_users
= 1;
10323 if (attr
->value_tag
== IPP_TAG_NAME
&&
10324 (attr
->num_values
> 1 ||
10325 strcmp(attr
->values
[0].string
.text
, "none")))
10327 for (i
= 0; i
< attr
->num_values
; i
++)
10328 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10331 else if (!strcmp(attr
->name
, "job-quota-period"))
10333 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10336 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10337 attr
->values
[0].integer
);
10338 cupsdFreeQuotas(printer
);
10340 printer
->quota_period
= attr
->values
[0].integer
;
10342 else if (!strcmp(attr
->name
, "job-k-limit"))
10344 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10347 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10348 attr
->values
[0].integer
);
10349 cupsdFreeQuotas(printer
);
10351 printer
->k_limit
= attr
->values
[0].integer
;
10353 else if (!strcmp(attr
->name
, "job-page-limit"))
10355 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10358 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10359 attr
->values
[0].integer
);
10360 cupsdFreeQuotas(printer
);
10362 printer
->page_limit
= attr
->values
[0].integer
;
10364 else if (!strcmp(attr
->name
, "printer-op-policy"))
10366 cupsd_policy_t
*p
; /* Policy */
10369 if (attr
->value_tag
!= IPP_TAG_NAME
)
10372 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10374 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10375 "Setting printer-op-policy to \"%s\"...",
10376 attr
->values
[0].string
.text
);
10377 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10378 printer
->op_policy_ptr
= p
;
10382 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10383 _("Unknown printer-op-policy \"%s\"."),
10384 attr
->values
[0].string
.text
);
10388 else if (!strcmp(attr
->name
, "printer-error-policy"))
10390 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10393 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10394 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10395 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10396 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10397 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10399 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10400 _("Unknown printer-error-policy \"%s\"."),
10401 attr
->values
[0].string
.text
);
10405 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10406 "Setting printer-error-policy to \"%s\"...",
10407 attr
->values
[0].string
.text
);
10408 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10412 * Skip any other non-default attributes...
10415 namelen
= strlen(attr
->name
);
10416 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10417 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10421 * OK, anything else must be a user-defined default...
10424 strlcpy(name
, attr
->name
, sizeof(name
));
10425 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10427 switch (attr
->value_tag
)
10429 case IPP_TAG_DELETEATTR
:
10430 printer
->num_options
= cupsRemoveOption(name
,
10431 printer
->num_options
,
10432 &(printer
->options
));
10433 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10434 "Deleting %s", attr
->name
);
10437 case IPP_TAG_NAME
:
10438 case IPP_TAG_TEXT
:
10439 case IPP_TAG_KEYWORD
:
10441 printer
->num_options
= cupsAddOption(name
,
10442 attr
->values
[0].string
.text
,
10443 printer
->num_options
,
10444 &(printer
->options
));
10445 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10446 "Setting %s to \"%s\"...", attr
->name
,
10447 attr
->values
[0].string
.text
);
10450 case IPP_TAG_BOOLEAN
:
10451 printer
->num_options
= cupsAddOption(name
,
10452 attr
->values
[0].boolean
?
10454 printer
->num_options
,
10455 &(printer
->options
));
10456 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10457 "Setting %s to %s...", attr
->name
,
10458 attr
->values
[0].boolean
? "true" : "false");
10461 case IPP_TAG_INTEGER
:
10462 case IPP_TAG_ENUM
:
10463 sprintf(value
, "%d", attr
->values
[0].integer
);
10464 printer
->num_options
= cupsAddOption(name
, value
,
10465 printer
->num_options
,
10466 &(printer
->options
));
10467 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10468 "Setting %s to %s...", attr
->name
, value
);
10471 case IPP_TAG_RANGE
:
10472 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10473 attr
->values
[0].range
.upper
);
10474 printer
->num_options
= cupsAddOption(name
, value
,
10475 printer
->num_options
,
10476 &(printer
->options
));
10477 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10478 "Setting %s to %s...", attr
->name
, value
);
10481 case IPP_TAG_RESOLUTION
:
10482 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10483 attr
->values
[0].resolution
.yres
,
10484 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10486 printer
->num_options
= cupsAddOption(name
, value
,
10487 printer
->num_options
,
10488 &(printer
->options
));
10489 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10490 "Setting %s to %s...", attr
->name
, value
);
10494 /* Do nothing for other values */
10502 * 'start_printer()' - Start a printer.
10506 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10507 ipp_attribute_t
*uri
) /* I - Printer URI */
10509 int i
; /* Temporary variable */
10510 http_status_t status
; /* Policy status */
10511 cups_ptype_t dtype
; /* Destination type (printer/class) */
10512 cupsd_printer_t
*printer
; /* Printer data */
10515 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10516 con
->number
, uri
->values
[0].string
.text
);
10519 * Is the destination valid?
10522 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10528 send_ipp_status(con
, IPP_NOT_FOUND
,
10529 _("The printer or class does not exist."));
10537 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10539 send_http_error(con
, status
, printer
);
10544 * Start the printer...
10547 printer
->state_message
[0] = '\0';
10549 cupsdStartPrinter(printer
, 1);
10551 if (dtype
& CUPS_PRINTER_CLASS
)
10552 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10553 printer
->name
, get_username(con
));
10555 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10556 printer
->name
, get_username(con
));
10564 if ((i
= check_quotas(con
, printer
)) < 0)
10566 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
10571 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
10576 * Everything was ok, so return OK status...
10579 con
->response
->request
.status
.status_code
= IPP_OK
;
10584 * 'stop_printer()' - Stop a printer.
10588 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10589 ipp_attribute_t
*uri
) /* I - Printer URI */
10591 http_status_t status
; /* Policy status */
10592 cups_ptype_t dtype
; /* Destination type (printer/class) */
10593 cupsd_printer_t
*printer
; /* Printer data */
10594 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10597 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10598 con
->number
, uri
->values
[0].string
.text
);
10601 * Is the destination valid?
10604 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10610 send_ipp_status(con
, IPP_NOT_FOUND
,
10611 _("The printer or class does not exist."));
10619 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10621 send_http_error(con
, status
, printer
);
10626 * Stop the printer...
10629 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10630 IPP_TAG_TEXT
)) == NULL
)
10631 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
10634 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10635 sizeof(printer
->state_message
));
10638 cupsdStopPrinter(printer
, 1);
10640 if (dtype
& CUPS_PRINTER_CLASS
)
10641 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10642 printer
->name
, get_username(con
));
10644 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10645 printer
->name
, get_username(con
));
10648 * Everything was ok, so return OK status...
10651 con
->response
->request
.status
.status_code
= IPP_OK
;
10656 * 'url_encode_attr()' - URL-encode a string attribute.
10660 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10661 char *buffer
,/* I - String buffer */
10662 size_t bufsize
)/* I - Size of buffer */
10664 int i
; /* Looping var */
10665 char *bufptr
, /* Pointer into buffer */
10666 *bufend
; /* End of buffer */
10669 strlcpy(buffer
, attr
->name
, bufsize
);
10670 bufptr
= buffer
+ strlen(buffer
);
10671 bufend
= buffer
+ bufsize
- 1;
10673 for (i
= 0; i
< attr
->num_values
; i
++)
10675 if (bufptr
>= bufend
)
10683 if (bufptr
>= bufend
)
10688 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
10690 if (bufptr
>= bufend
)
10701 * 'url_encode_string()' - URL-encode a string.
10704 static char * /* O - End of string */
10705 url_encode_string(const char *s
, /* I - String */
10706 char *buffer
, /* I - String buffer */
10707 size_t bufsize
) /* I - Size of buffer */
10709 char *bufptr
, /* Pointer into buffer */
10710 *bufend
; /* End of buffer */
10711 static const char *hex
= "0123456789ABCDEF";
10716 bufend
= buffer
+ bufsize
- 1;
10718 while (*s
&& bufptr
< bufend
)
10720 if (*s
== ' ' || *s
== '%' || *s
== '+')
10722 if (bufptr
>= (bufend
- 2))
10726 *bufptr
++ = hex
[(*s
>> 4) & 15];
10727 *bufptr
++ = hex
[*s
& 15];
10731 else if (*s
== '\'' || *s
== '\\')
10733 if (bufptr
>= (bufend
- 1))
10750 * 'user_allowed()' - See if a user is allowed to print to a queue.
10753 static int /* O - 0 if not allowed, 1 if allowed */
10754 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10755 const char *username
) /* I - Username */
10757 struct passwd
*pw
; /* User password data */
10758 char baseuser
[256], /* Base username */
10759 *baseptr
, /* Pointer to "@" in base username */
10760 *name
; /* Current user name */
10763 if (cupsArrayCount(p
->users
) == 0)
10766 if (!strcmp(username
, "root"))
10769 if (strchr(username
, '@'))
10772 * Strip @REALM for username check...
10775 strlcpy(baseuser
, username
, sizeof(baseuser
));
10777 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10780 username
= baseuser
;
10783 pw
= getpwnam(username
);
10786 for (name
= (char *)cupsArrayFirst(p
->users
);
10788 name
= (char *)cupsArrayNext(p
->users
))
10790 if (name
[0] == '@')
10793 * Check group membership...
10796 if (cupsdCheckGroup(username
, pw
, name
+ 1))
10799 else if (name
[0] == '#')
10805 if (cupsdCheckGroup(username
, pw
, name
))
10808 else if (!_cups_strcasecmp(username
, name
))
10812 return ((name
!= NULL
) != p
->deny_users
);
10817 * 'validate_job()' - Validate printer options and destination.
10821 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10822 ipp_attribute_t
*uri
) /* I - Printer URI */
10824 http_status_t status
; /* Policy status */
10825 ipp_attribute_t
*attr
; /* Current attribute */
10827 ipp_attribute_t
*auth_info
; /* auth-info attribute */
10828 #endif /* HAVE_SSL */
10829 ipp_attribute_t
*format
, /* Document-format attribute */
10830 *name
; /* Job-name attribute */
10831 cups_ptype_t dtype
; /* Destination type (printer/class) */
10832 char super
[MIME_MAX_SUPER
],
10833 /* Supertype of file */
10834 type
[MIME_MAX_TYPE
];
10835 /* Subtype of file */
10836 cupsd_printer_t
*printer
; /* Printer */
10839 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10840 con
->number
, uri
->values
[0].string
.text
);
10843 * OK, see if the client is sending the document compressed - CUPS
10844 * doesn't support compression yet...
10847 if ((attr
= ippFindAttribute(con
->request
, "compression",
10848 IPP_TAG_KEYWORD
)) != NULL
)
10850 if (strcmp(attr
->values
[0].string
.text
, "none")
10852 && strcmp(attr
->values
[0].string
.text
, "gzip")
10853 #endif /* HAVE_LIBZ */
10856 send_ipp_status(con
, IPP_ATTRIBUTES
,
10857 _("Unsupported 'compression' value \"%s\"."),
10858 attr
->values
[0].string
.text
);
10859 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10860 "compression", NULL
, attr
->values
[0].string
.text
);
10866 * Is it a format we support?
10869 if ((format
= ippFindAttribute(con
->request
, "document-format",
10870 IPP_TAG_MIMETYPE
)) != NULL
)
10872 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
10875 send_ipp_status(con
, IPP_BAD_REQUEST
,
10876 _("Bad 'document-format' value \"%s\"."),
10877 format
->values
[0].string
.text
);
10881 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10882 !mimeType(MimeDatabase
, super
, type
))
10884 cupsdLogMessage(CUPSD_LOG_INFO
,
10885 "Hint: Do you have the raw file printing rules enabled?");
10886 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10887 _("Unsupported 'document-format' value \"%s\"."),
10888 format
->values
[0].string
.text
);
10889 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10890 "document-format", NULL
, format
->values
[0].string
.text
);
10896 * Is the job-name valid?
10899 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
10901 int bad_name
= 0; /* Is the job-name value bad? */
10903 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
10904 name
->num_values
!= 1)
10911 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
10912 * IPP Everywhere requirements for "name" values...
10915 const unsigned char *nameptr
; /* Pointer into "job-name" attribute */
10917 for (nameptr
= (unsigned char *)name
->values
[0].string
.text
;
10921 if (*nameptr
< ' ' && *nameptr
!= '\t')
10923 else if (*nameptr
== 0x7f)
10925 else if ((*nameptr
& 0xe0) == 0xc0)
10927 if ((nameptr
[1] & 0xc0) != 0x80)
10932 else if ((*nameptr
& 0xf0) == 0xe0)
10934 if ((nameptr
[1] & 0xc0) != 0x80 ||
10935 (nameptr
[2] & 0xc0) != 0x80)
10940 else if ((*nameptr
& 0xf8) == 0xf0)
10942 if ((nameptr
[1] & 0xc0) != 0x80 ||
10943 (nameptr
[2] & 0xc0) != 0x80 ||
10944 (nameptr
[3] & 0xc0) != 0x80)
10949 else if (*nameptr
& 0x80)
10959 if (StrictConformance
)
10961 send_ipp_status(con
, IPP_ATTRIBUTES
,
10962 _("Unsupported 'job-name' value."));
10963 ippCopyAttribute(con
->response
, name
, 0);
10968 cupsdLogMessage(CUPSD_LOG_WARN
,
10969 "Unsupported 'job-name' value, deleting from request.");
10970 ippDeleteAttribute(con
->request
, name
);
10976 * Is the destination valid?
10979 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10985 send_ipp_status(con
, IPP_NOT_FOUND
,
10986 _("The printer or class does not exist."));
10995 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
10996 #endif /* HAVE_SSL */
10998 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11000 send_http_error(con
, status
, printer
);
11003 else if (printer
->num_auth_info_required
== 1 &&
11004 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
11007 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
11011 else if (auth_info
&& !con
->http
->tls
&&
11012 !httpAddrLocalhost(con
->http
->hostaddr
))
11015 * Require encryption of auth-info over non-local connections...
11018 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
11021 #endif /* HAVE_SSL */
11024 * Everything was ok, so return OK status...
11027 con
->response
->request
.status
.status_code
= IPP_OK
;
11032 * 'validate_name()' - Make sure the printer name only contains valid chars.
11035 static int /* O - 0 if name is no good, 1 if good */
11036 validate_name(const char *name
) /* I - Name to check */
11038 const char *ptr
; /* Pointer into name */
11042 * Scan the whole name...
11045 for (ptr
= name
; *ptr
; ptr
++)
11046 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11050 * All the characters are good; validate the length, too...
11053 return ((ptr
- name
) < 128);
11058 * 'validate_user()' - Validate the user for the request.
11061 static int /* O - 1 if permitted, 0 otherwise */
11062 validate_user(cupsd_job_t
*job
, /* I - Job */
11063 cupsd_client_t
*con
, /* I - Client connection */
11064 const char *owner
, /* I - Owner of job/resource */
11065 char *username
, /* O - Authenticated username */
11066 size_t userlen
) /* I - Length of username */
11068 cupsd_printer_t
*printer
; /* Printer for job */
11071 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
);
11074 * Validate input...
11077 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11081 * Get the best authenticated username that is available.
11084 strlcpy(username
, get_username(con
), userlen
);
11087 * Check the username against the owner...
11090 printer
= cupsdFindDest(job
->dest
);
11092 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11093 con
, owner
) == HTTP_OK
);