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
);
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/interface script? */
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 an interface script or 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 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2602 if (!strncmp(line
, "*PPD-Adobe", 10))
2605 * The new file is a PPD file, so remove any old interface script
2606 * that might be lying around...
2614 * This must be an interface script, so move the file over to the
2615 * interfaces directory and make it executable...
2618 if (copy_file(srcfile
, dstfile
))
2620 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2621 _("Unable to copy interface script - %s"),
2626 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2627 "Copied interface script successfully");
2628 chmod(dstfile
, 0755);
2631 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2634 if (!strncmp(line
, "*PPD-Adobe", 10))
2637 * The new file is a PPD file, so move the file over to the
2638 * ppd directory and make it readable by all...
2641 if (copy_file(srcfile
, dstfile
))
2643 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2644 _("Unable to copy PPD file - %s"),
2649 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2650 "Copied PPD file successfully");
2651 chmod(dstfile
, 0644);
2656 * This must be an interface script, so remove any old PPD file that
2657 * may be lying around...
2664 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2665 IPP_TAG_NAME
)) != NULL
)
2667 need_restart_job
= 1;
2670 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2673 * Raw driver, remove any existing PPD or interface script files.
2676 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2680 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2690 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2694 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2697 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2699 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file."));
2703 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2704 "Copied PPD file successfully");
2711 * If we changed the PPD/interface script, then remove the printer's cache
2712 * file and clear the printer-state-reasons...
2715 char cache_name
[1024]; /* Cache filename for printer attrs */
2717 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.data", CacheDir
,
2721 cupsdSetPrinterReasons(printer
, "none");
2724 * (Re)register color profiles...
2727 cupsdRegisterColor(printer
);
2731 * If we set the device URI but not the port monitor, check which port
2732 * monitor to use by default...
2735 if (set_device_uri
&& !set_port_monitor
)
2737 ppd_file_t
*ppd
; /* PPD file */
2738 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2741 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2742 sizeof(scheme
), username
, sizeof(username
), host
,
2743 sizeof(host
), &port
, resource
, sizeof(resource
));
2745 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2747 if ((ppd
= _ppdOpenFile(srcfile
, _PPD_LOCALIZATION_NONE
)) != NULL
)
2749 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2751 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2752 if (!strcmp(scheme
, ppdattr
->spec
))
2754 cupsdLogMessage(CUPSD_LOG_INFO
,
2755 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2756 printer
->name
, ppdattr
->value
,
2757 printer
->port_monitor
? printer
->port_monitor
2760 if (strcmp(ppdattr
->value
, "none"))
2761 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2763 cupsdClearString(&printer
->port_monitor
);
2772 printer
->config_time
= time(NULL
);
2775 * Update the printer attributes and return...
2778 cupsdSetPrinterAttrs(printer
);
2779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2781 if (need_restart_job
&& printer
->job
)
2784 * Restart the current job...
2787 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
2788 "Job restarted because the printer was modified.");
2791 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2795 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
2796 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
2797 printer
->name
, get_username(con
));
2799 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2800 printer
->name
, get_username(con
));
2804 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
2805 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
2806 printer
->name
, get_username(con
));
2808 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2809 printer
->name
, get_username(con
));
2812 con
->response
->request
.status
.status_code
= IPP_OK
;
2817 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2818 * based upon the printer state...
2822 add_printer_state_reasons(
2823 cupsd_client_t
*con
, /* I - Client connection */
2824 cupsd_printer_t
*p
) /* I - Printer info */
2826 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2827 "add_printer_state_reasons(%p[%d], %p[%s])",
2828 con
, con
->number
, p
, p
->name
);
2830 if (p
->num_reasons
== 0)
2831 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2832 "printer-state-reasons", NULL
, "none");
2834 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2835 "printer-state-reasons", p
->num_reasons
, NULL
,
2836 (const char * const *)p
->reasons
);
2841 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2842 * the specified printer or class.
2846 add_queued_job_count(
2847 cupsd_client_t
*con
, /* I - Client connection */
2848 cupsd_printer_t
*p
) /* I - Printer or class */
2850 int count
; /* Number of jobs on destination */
2853 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2854 con
, con
->number
, p
, p
->name
);
2856 count
= cupsdGetPrinterJobCount(p
->name
);
2858 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2859 "queued-job-count", count
);
2864 * 'apply_printer_defaults()' - Apply printer default options to a job.
2868 apply_printer_defaults(
2869 cupsd_printer_t
*printer
, /* I - Printer */
2870 cupsd_job_t
*job
) /* I - Job */
2872 int i
, /* Looping var */
2873 num_options
; /* Number of default options */
2874 cups_option_t
*options
, /* Default options */
2875 *option
; /* Current option */
2879 * Collect all of the default options and add the missing ones to the
2883 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
2884 option
= printer
->options
;
2887 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2889 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2894 * Encode these options as attributes in the job object...
2897 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2898 cupsFreeOptions(num_options
, options
);
2903 * 'authenticate_job()' - Set job authentication info.
2907 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2908 ipp_attribute_t
*uri
) /* I - Job URI */
2910 ipp_attribute_t
*attr
, /* job-id attribute */
2911 *auth_info
; /* auth-info attribute */
2912 int jobid
; /* Job ID */
2913 cupsd_job_t
*job
; /* Current job */
2914 char scheme
[HTTP_MAX_URI
],
2915 /* Method portion of URI */
2916 username
[HTTP_MAX_URI
],
2917 /* Username portion of URI */
2919 /* Host portion of URI */
2920 resource
[HTTP_MAX_URI
];
2921 /* Resource portion of URI */
2922 int port
; /* Port portion of URI */
2925 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2926 con
, con
->number
, uri
->values
[0].string
.text
);
2929 * Start with "everything is OK" status...
2932 con
->response
->request
.status
.status_code
= IPP_OK
;
2935 * See if we have a job URI or a printer URI...
2938 if (!strcmp(uri
->name
, "printer-uri"))
2941 * Got a printer URI; see if we also have a job-id attribute...
2944 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2945 IPP_TAG_INTEGER
)) == NULL
)
2947 send_ipp_status(con
, IPP_BAD_REQUEST
,
2948 _("Got a printer-uri attribute but no job-id."));
2952 jobid
= attr
->values
[0].integer
;
2957 * Got a job URI; parse it to get the job ID...
2960 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2961 sizeof(scheme
), username
, sizeof(username
), host
,
2962 sizeof(host
), &port
, resource
, sizeof(resource
));
2964 if (strncmp(resource
, "/jobs/", 6))
2970 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
2971 uri
->values
[0].string
.text
);
2975 jobid
= atoi(resource
+ 6);
2979 * See if the job exists...
2982 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2985 * Nope - return a "not found" error...
2988 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
2993 * See if the job has been completed...
2996 if (job
->state_value
!= IPP_JOB_HELD
)
2999 * Return a "not-possible" error...
3002 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3003 _("Job #%d is not held for authentication."),
3009 * See if we have already authenticated...
3012 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3014 if (!con
->username
[0] && !auth_info
)
3016 cupsd_printer_t
*printer
; /* Job destination */
3019 * No auth data. If we need to authenticate via Kerberos, send a
3020 * HTTP auth challenge, otherwise just return an IPP error...
3023 printer
= cupsdFindDest(job
->dest
);
3025 if (printer
&& printer
->num_auth_info_required
> 0 &&
3026 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3027 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3029 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3030 _("No authentication information provided."));
3035 * See if the job is owned by the requesting user...
3038 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3040 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3041 cupsdFindDest(job
->dest
));
3046 * Save the authentication information for this job...
3049 save_auth_info(con
, job
, auth_info
);
3052 * Reset the job-hold-until value to "no-hold"...
3055 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3056 IPP_TAG_KEYWORD
)) == NULL
)
3057 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3061 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
3062 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
3066 * Release the job and return...
3069 cupsdReleaseJob(job
);
3071 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, NULL
, job
, "Job authenticated by user");
3073 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3080 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3084 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3085 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3087 int i
; /* Looping var */
3088 http_status_t status
; /* Policy status */
3089 cups_ptype_t dtype
; /* Destination type */
3090 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3091 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3092 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3093 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3094 int port
; /* Port portion of URI */
3095 ipp_attribute_t
*attr
; /* Attribute in request */
3096 const char *username
= NULL
; /* Username */
3097 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3099 cupsd_printer_t
*printer
; /* Printer */
3100 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3101 cupsd_job_t
*job
; /* Job */
3104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3105 con
->number
, uri
->values
[0].string
.text
);
3108 * Get the jobs to cancel/purge...
3111 switch (con
->request
->request
.op
.operation_id
)
3113 case IPP_PURGE_JOBS
:
3115 * Get the username (if any) for the jobs we want to cancel (only if
3116 * "my-jobs" is specified...
3119 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3120 IPP_TAG_BOOLEAN
)) != NULL
&&
3121 attr
->values
[0].boolean
)
3123 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3124 IPP_TAG_NAME
)) != NULL
)
3125 username
= attr
->values
[0].string
.text
;
3128 send_ipp_status(con
, IPP_BAD_REQUEST
,
3129 _("Missing requesting-user-name attribute."));
3135 * Look for the "purge-jobs" attribute...
3138 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3139 IPP_TAG_BOOLEAN
)) != NULL
)
3140 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3142 purge
= CUPSD_JOB_PURGE
;
3145 case IPP_CANCEL_MY_JOBS
:
3146 if (con
->username
[0])
3147 username
= con
->username
;
3148 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3149 IPP_TAG_NAME
)) != NULL
)
3150 username
= attr
->values
[0].string
.text
;
3153 send_ipp_status(con
, IPP_BAD_REQUEST
,
3154 _("Missing requesting-user-name attribute."));
3162 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3165 * See if we have a printer URI...
3168 if (strcmp(uri
->name
, "printer-uri"))
3170 send_ipp_status(con
, IPP_BAD_REQUEST
,
3171 _("The printer-uri attribute is required."));
3176 * And if the destination is valid...
3179 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3185 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3186 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3187 hostname
, sizeof(hostname
), &port
,
3188 resource
, sizeof(resource
));
3190 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3191 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3193 send_ipp_status(con
, IPP_NOT_FOUND
,
3194 _("The printer or class does not exist."));
3202 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3204 send_http_error(con
, status
, NULL
);
3210 for (i
= 0; i
< job_ids
->num_values
; i
++)
3212 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
3215 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3216 _cups_strcasecmp(job
->username
, username
))
3220 if (i
< job_ids
->num_values
)
3222 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3223 job_ids
->values
[i
].integer
);
3227 for (i
= 0; i
< job_ids
->num_values
; i
++)
3229 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3231 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3232 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3233 "Job canceled by user.");
3236 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3237 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3243 * Cancel all jobs on all printers...
3246 cupsdCancelJobs(NULL
, username
, purge
);
3248 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3249 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3259 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3262 send_http_error(con
, status
, printer
);
3268 for (i
= 0; i
< job_ids
->num_values
; i
++)
3270 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
3271 _cups_strcasecmp(job
->dest
, printer
->name
))
3274 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3275 _cups_strcasecmp(job
->username
, username
))
3279 if (i
< job_ids
->num_values
)
3281 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3282 job_ids
->values
[i
].integer
);
3286 for (i
= 0; i
< job_ids
->num_values
; i
++)
3288 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3290 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3291 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3292 "Job canceled by user.");
3295 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3296 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3302 * Cancel all of the jobs on the named printer...
3305 cupsdCancelJobs(printer
->name
, username
, purge
);
3307 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3309 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3314 con
->response
->request
.status
.status_code
= IPP_OK
;
3321 * 'cancel_job()' - Cancel a print job.
3325 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3326 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3328 ipp_attribute_t
*attr
; /* Current attribute */
3329 int jobid
; /* Job ID */
3330 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3331 username
[HTTP_MAX_URI
], /* Username portion of URI */
3332 host
[HTTP_MAX_URI
], /* Host portion of URI */
3333 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3334 int port
; /* Port portion of URI */
3335 cupsd_job_t
*job
; /* Job information */
3336 cups_ptype_t dtype
; /* Destination type (printer/class) */
3337 cupsd_printer_t
*printer
; /* Printer data */
3338 cupsd_jobaction_t purge
; /* Purge the job? */
3341 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3342 con
->number
, uri
->values
[0].string
.text
);
3345 * See if we have a job URI or a printer URI...
3348 if (!strcmp(uri
->name
, "printer-uri"))
3351 * Got a printer URI; see if we also have a job-id attribute...
3354 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3355 IPP_TAG_INTEGER
)) == NULL
)
3357 send_ipp_status(con
, IPP_BAD_REQUEST
,
3358 _("Got a printer-uri attribute but no job-id."));
3362 if ((jobid
= attr
->values
[0].integer
) == 0)
3365 * Find the current job on the specified printer...
3368 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3374 send_ipp_status(con
, IPP_NOT_FOUND
,
3375 _("The printer or class does not exist."));
3380 * See if there are any pending jobs...
3383 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3385 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3386 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3387 !_cups_strcasecmp(job
->dest
, printer
->name
))
3395 * No, try stopped jobs...
3398 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3400 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3401 if (job
->state_value
== IPP_JOB_STOPPED
&&
3402 !_cups_strcasecmp(job
->dest
, printer
->name
))
3409 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3419 * Got a job URI; parse it to get the job ID...
3422 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3423 sizeof(scheme
), username
, sizeof(username
), host
,
3424 sizeof(host
), &port
, resource
, sizeof(resource
));
3426 if (strncmp(resource
, "/jobs/", 6))
3432 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3433 uri
->values
[0].string
.text
);
3437 jobid
= atoi(resource
+ 6);
3441 * Look for the "purge-job" attribute...
3444 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3445 IPP_TAG_BOOLEAN
)) != NULL
)
3446 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3448 purge
= CUPSD_JOB_DEFAULT
;
3451 * See if the job exists...
3454 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3457 * Nope - return a "not found" error...
3460 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3465 * See if the job is owned by the requesting user...
3468 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3470 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3471 cupsdFindDest(job
->dest
));
3476 * See if the job is already completed, canceled, or aborted; if so,
3477 * we can't cancel...
3480 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3482 switch (job
->state_value
)
3484 case IPP_JOB_CANCELED
:
3485 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3486 _("Job #%d is already canceled - can\'t cancel."),
3490 case IPP_JOB_ABORTED
:
3491 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3492 _("Job #%d is already aborted - can\'t cancel."),
3497 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3498 _("Job #%d is already completed - can\'t cancel."),
3507 * Cancel the job and return...
3510 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3511 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3512 "Job canceled by \"%s\"",
3516 if (purge
== CUPSD_JOB_PURGE
)
3517 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3520 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3523 con
->response
->request
.status
.status_code
= IPP_OK
;
3528 * 'cancel_subscription()' - Cancel a subscription.
3532 cancel_subscription(
3533 cupsd_client_t
*con
, /* I - Client connection */
3534 int sub_id
) /* I - Subscription ID */
3536 http_status_t status
; /* Policy status */
3537 cupsd_subscription_t
*sub
; /* Subscription */
3540 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3541 "cancel_subscription(con=%p[%d], sub_id=%d)",
3542 con
, con
->number
, sub_id
);
3545 * Is the subscription ID valid?
3548 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3551 * Bad subscription ID...
3554 send_ipp_status(con
, IPP_NOT_FOUND
,
3555 _("Subscription #%d does not exist."), sub_id
);
3563 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3565 con
, sub
->owner
)) != HTTP_OK
)
3567 send_http_error(con
, status
, sub
->dest
);
3572 * Cancel the subscription...
3575 cupsdDeleteSubscription(sub
, 1);
3577 con
->response
->request
.status
.status_code
= IPP_OK
;
3582 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3585 static int /* O - 1 if OK, 0 if not */
3586 check_rss_recipient(
3587 const char *recipient
) /* I - Recipient URI */
3589 cupsd_subscription_t
*sub
; /* Current subscription */
3592 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3594 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3598 * Compare the URIs up to the first ?...
3601 const char *r1
, *r2
;
3603 for (r1
= recipient
, r2
= sub
->recipient
;
3604 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3616 * 'check_quotas()' - Check quotas for a printer and user.
3619 static int /* O - 1 if OK, 0 if forbidden,
3620 -1 if limit reached */
3621 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3622 cupsd_printer_t
*p
) /* I - Printer or class */
3624 char username
[33], /* Username */
3625 *name
; /* Current user name */
3626 cupsd_quota_t
*q
; /* Quota data */
3627 #ifdef HAVE_MBR_UID_TO_UUID
3629 * Use Apple membership APIs which require that all names represent
3630 * valid user account or group records accessible by the server.
3633 uuid_t usr_uuid
; /* UUID for job requesting user */
3634 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3635 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3636 int mbr_err
; /* Error from membership function */
3637 int is_member
; /* Is this user a member? */
3640 * Use standard POSIX APIs for checking users and groups...
3643 struct passwd
*pw
; /* User password data */
3644 #endif /* HAVE_MBR_UID_TO_UUID */
3647 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3648 con
, con
->number
, p
, p
->name
);
3651 * Figure out who is printing...
3654 strlcpy(username
, get_username(con
), sizeof(username
));
3656 if ((name
= strchr(username
, '@')) != NULL
)
3657 *name
= '\0'; /* Strip @REALM */
3660 * Check global active job limits for printers and users...
3663 if (MaxJobsPerPrinter
)
3666 * Check if there are too many pending jobs on this printer...
3669 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3671 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3680 * Check if there are too many pending jobs for this user...
3683 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3685 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3692 * Check against users...
3695 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3698 if (cupsArrayCount(p
->users
))
3700 #ifdef HAVE_MBR_UID_TO_UUID
3702 * Get UUID for job requesting user...
3705 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3711 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3712 "check_quotas: UUID lookup failed for user \"%s\"",
3714 cupsdLogMessage(CUPSD_LOG_INFO
,
3715 "Denying user \"%s\" access to printer \"%s\" "
3716 "(unknown user)...",
3722 * Get UID and GID of requesting user...
3725 pw
= getpwnam(username
);
3727 #endif /* HAVE_MBR_UID_TO_UUID */
3729 for (name
= (char *)cupsArrayFirst(p
->users
);
3731 name
= (char *)cupsArrayNext(p
->users
))
3735 * Check group membership...
3738 #ifdef HAVE_MBR_UID_TO_UUID
3741 if (uuid_parse(name
+ 2, grp_uuid
))
3742 uuid_clear(grp_uuid
);
3744 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3747 * Invalid ACL entries are ignored for matching; just record a
3748 * warning in the log...
3751 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3752 "check_quotas: UUID lookup failed for ACL entry "
3753 "\"%s\" (err=%d)", name
, mbr_err
);
3754 cupsdLogMessage(CUPSD_LOG_WARN
,
3755 "Access control entry \"%s\" not a valid group name; "
3756 "entry ignored", name
);
3759 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3763 * At this point, there should be no errors, but check anyways...
3766 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3767 "check_quotas: group \"%s\" membership check "
3768 "failed (err=%d)", name
+ 1, mbr_err
);
3773 * Stop if we found a match...
3780 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3782 #endif /* HAVE_MBR_UID_TO_UUID */
3784 #ifdef HAVE_MBR_UID_TO_UUID
3789 if (uuid_parse(name
+ 1, usr2_uuid
))
3790 uuid_clear(usr2_uuid
);
3792 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3795 * Invalid ACL entries are ignored for matching; just record a
3796 * warning in the log...
3799 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3800 "check_quotas: UUID lookup failed for ACL entry "
3801 "\"%s\" (err=%d)", name
, mbr_err
);
3802 cupsdLogMessage(CUPSD_LOG_WARN
,
3803 "Access control entry \"%s\" not a valid user name; "
3804 "entry ignored", name
);
3807 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3811 else if (!_cups_strcasecmp(username
, name
))
3813 #endif /* HAVE_MBR_UID_TO_UUID */
3815 if ((name
!= NULL
) == p
->deny_users
)
3817 cupsdLogMessage(CUPSD_LOG_INFO
,
3818 "Denying user \"%s\" access to printer \"%s\"...",
3828 if (p
->k_limit
|| p
->page_limit
)
3830 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3832 cupsdLogMessage(CUPSD_LOG_ERROR
,
3833 "Unable to allocate quota data for user \"%s\"",
3838 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3839 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3841 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3848 * If we have gotten this far, we're done!
3856 * 'close_job()' - Close a multi-file job.
3860 close_job(cupsd_client_t
*con
, /* I - Client connection */
3861 ipp_attribute_t
*uri
) /* I - Printer URI */
3863 cupsd_job_t
*job
; /* Job */
3864 ipp_attribute_t
*attr
; /* Attribute */
3865 char job_uri
[HTTP_MAX_URI
],
3867 username
[256]; /* User name */
3870 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3871 con
->number
, uri
->values
[0].string
.text
);
3874 * See if we have a job URI or a printer URI...
3877 if (strcmp(uri
->name
, "printer-uri"))
3880 * job-uri is not supported by Close-Job!
3883 send_ipp_status(con
, IPP_BAD_REQUEST
,
3884 _("Close-Job doesn't support the job-uri attribute."));
3889 * Got a printer URI; see if we also have a job-id attribute...
3892 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3893 IPP_TAG_INTEGER
)) == NULL
)
3895 send_ipp_status(con
, IPP_BAD_REQUEST
,
3896 _("Got a printer-uri attribute but no job-id."));
3900 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
3903 * Nope - return a "not found" error...
3906 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3907 attr
->values
[0].integer
);
3912 * See if the job is owned by the requesting user...
3915 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3917 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3918 cupsdFindDest(job
->dest
));
3923 * Add any ending sheet...
3926 if (cupsdTimeoutJob(job
))
3929 if (job
->state_value
== IPP_JOB_STOPPED
)
3931 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3932 job
->state_value
= IPP_JOB_PENDING
;
3934 else if (job
->state_value
== IPP_JOB_HELD
)
3936 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3937 IPP_TAG_KEYWORD
)) == NULL
)
3938 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3940 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
3942 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3943 job
->state_value
= IPP_JOB_PENDING
;
3948 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3951 * Fill in the response info...
3954 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
3955 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
3956 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
3959 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
3961 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
3964 con
->response
->request
.status
.status_code
= IPP_OK
;
3967 * Start the job if necessary...
3975 * 'copy_attrs()' - Copy attributes from one request to another.
3979 copy_attrs(ipp_t
*to
, /* I - Destination request */
3980 ipp_t
*from
, /* I - Source request */
3981 cups_array_t
*ra
, /* I - Requested attributes */
3982 ipp_tag_t group
, /* I - Group to copy */
3983 int quickcopy
, /* I - Do a quick copy? */
3984 cups_array_t
*exclude
) /* I - Attributes to exclude? */
3986 ipp_attribute_t
*fromattr
; /* Source attribute */
3989 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3990 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3991 to
, from
, ra
, group
, quickcopy
);
3996 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3999 * Filter attributes as needed...
4002 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4003 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4006 if (!strcmp(fromattr
->name
, "document-password") ||
4007 !strcmp(fromattr
->name
, "job-authorization-uri") ||
4008 !strcmp(fromattr
->name
, "job-password") ||
4009 !strcmp(fromattr
->name
, "job-password-encryption") ||
4010 !strcmp(fromattr
->name
, "job-printer-uri"))
4014 (cupsArrayFind(exclude
, fromattr
->name
) ||
4015 cupsArrayFind(exclude
, "all")))
4018 * We need to exclude this attribute for security reasons; we require the
4019 * job-id attribute regardless of the security settings for IPP
4022 * The job-printer-uri attribute is handled by copy_job_attrs().
4024 * Subscription attribute security is handled by copy_subscription_attrs().
4027 if (strcmp(fromattr
->name
, "job-id"))
4031 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4034 * Don't send collection attributes by default to IPP/1.x clients
4035 * since many do not support collections. Also don't send
4036 * media-col-database unless specifically requested by the client.
4039 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4041 (to
->request
.status
.version
[0] == 1 ||
4042 !strcmp(fromattr
->name
, "media-col-database")))
4045 ippCopyAttribute(to
, fromattr
, quickcopy
);
4052 * 'copy_banner()' - Copy a banner file to the requests directory for the
4056 static int /* O - Size of banner file in kbytes */
4057 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4058 cupsd_job_t
*job
, /* I - Job information */
4059 const char *name
) /* I - Name of banner */
4061 int i
; /* Looping var */
4062 int kbytes
; /* Size of banner file in kbytes */
4063 char filename
[1024]; /* Job filename */
4064 cupsd_banner_t
*banner
; /* Pointer to banner */
4065 cups_file_t
*in
; /* Input file */
4066 cups_file_t
*out
; /* Output file */
4067 int ch
; /* Character from file */
4068 char attrname
[255], /* Name of attribute */
4069 *s
; /* Pointer into name */
4070 ipp_attribute_t
*attr
; /* Attribute */
4073 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4074 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4075 con
, con
? con
->number
: -1, job
, job
->id
,
4076 name
? name
: "(null)");
4079 * Find the banner; return if not found or "none"...
4082 if (!name
|| !strcmp(name
, "none") ||
4083 (banner
= cupsdFindBanner(name
)) == NULL
)
4087 * Open the banner and job files...
4090 if (add_file(con
, job
, banner
->filetype
, 0))
4093 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4095 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4097 cupsdLogMessage(CUPSD_LOG_ERROR
,
4098 "Unable to create banner job file %s - %s",
4099 filename
, strerror(errno
));
4104 fchmod(cupsFileNumber(out
), 0640);
4105 fchown(cupsFileNumber(out
), RunUser
, Group
);
4108 * Try the localized banner file under the subdirectory...
4111 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4113 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4116 * Convert ll-cc to ll_CC...
4120 attrname
[3] = (char)toupper(attrname
[3] & 255);
4121 attrname
[4] = (char)toupper(attrname
[4] & 255);
4124 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4127 if (access(filename
, 0) && strlen(attrname
) > 2)
4130 * Wasn't able to find "ll_CC" locale file; try the non-national
4131 * localization banner directory.
4136 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4140 if (access(filename
, 0))
4143 * Use the non-localized banner file.
4146 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4149 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4153 cupsdLogMessage(CUPSD_LOG_ERROR
,
4154 "Unable to open banner template file %s - %s",
4155 filename
, strerror(errno
));
4161 * Parse the file to the end...
4164 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4168 * Get an attribute name...
4171 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4172 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4174 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4184 * Ignore { followed by stuff that is not an attribute name...
4187 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4192 * See if it is defined...
4195 if (attrname
[0] == '?')
4200 if (!strcmp(s
, "printer-name"))
4202 cupsFilePuts(out
, job
->dest
);
4205 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4208 * See if we have a leading question mark...
4211 if (attrname
[0] != '?')
4214 * Nope, write to file as-is; probably a PostScript procedure...
4217 cupsFilePrintf(out
, "{%s}", attrname
);
4224 * Output value(s)...
4227 for (i
= 0; i
< attr
->num_values
; i
++)
4230 cupsFilePutChar(out
, ',');
4232 switch (attr
->value_tag
)
4234 case IPP_TAG_INTEGER
:
4236 if (!strncmp(s
, "time-at-", 8))
4238 struct timeval tv
; /* Time value */
4240 tv
.tv_sec
= attr
->values
[i
].integer
;
4243 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4246 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4249 case IPP_TAG_BOOLEAN
:
4250 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4253 case IPP_TAG_NOVALUE
:
4254 cupsFilePuts(out
, "novalue");
4257 case IPP_TAG_RANGE
:
4258 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4259 attr
->values
[i
].range
.upper
);
4262 case IPP_TAG_RESOLUTION
:
4263 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4264 attr
->values
[i
].resolution
.yres
,
4265 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4270 case IPP_TAG_STRING
:
4273 case IPP_TAG_KEYWORD
:
4274 case IPP_TAG_CHARSET
:
4275 case IPP_TAG_LANGUAGE
:
4276 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4279 * Need to quote strings for PS banners...
4284 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4286 if (*p
== '(' || *p
== ')' || *p
== '\\')
4288 cupsFilePutChar(out
, '\\');
4289 cupsFilePutChar(out
, *p
);
4291 else if (*p
< 32 || *p
> 126)
4292 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4294 cupsFilePutChar(out
, *p
);
4298 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4302 break; /* anti-compiler-warning-code */
4306 else if (ch
== '\\') /* Quoted char */
4308 ch
= cupsFileGetChar(in
);
4310 if (ch
!= '{') /* Only do special handling for \{ */
4311 cupsFilePutChar(out
, '\\');
4313 cupsFilePutChar(out
, ch
);
4316 cupsFilePutChar(out
, ch
);
4320 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4322 job
->koctets
+= kbytes
;
4324 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4325 attr
->values
[0].integer
+= kbytes
;
4334 * 'copy_file()' - Copy a PPD file or interface script...
4337 static int /* O - 0 = success, -1 = error */
4338 copy_file(const char *from
, /* I - Source file */
4339 const char *to
) /* I - Destination file */
4341 cups_file_t
*src
, /* Source file */
4342 *dst
; /* Destination file */
4343 int bytes
; /* Bytes to read/write */
4344 char buffer
[2048]; /* Copy buffer */
4347 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4350 * Open the source and destination file for a copy...
4353 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4356 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4363 * Copy the source file to the destination...
4366 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4367 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4375 * Close both files and return...
4380 return (cupsFileClose(dst
));
4385 * 'copy_model()' - Copy a PPD model file, substituting default values
4389 static int /* O - 0 = success, -1 = error */
4390 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4391 const char *from
, /* I - Source file */
4392 const char *to
) /* I - Destination file */
4394 fd_set input
; /* select() input set */
4395 struct timeval timeout
; /* select() timeout */
4396 int maxfd
; /* Max file descriptor for select() */
4397 char tempfile
[1024]; /* Temporary PPD file */
4398 int tempfd
; /* Temporary PPD file descriptor */
4399 int temppid
; /* Process ID of cups-driverd */
4400 int temppipe
[2]; /* Temporary pipes */
4401 char *argv
[4], /* Command-line arguments */
4402 *envp
[MAX_ENV
]; /* Environment */
4403 cups_file_t
*src
, /* Source file */
4404 *dst
; /* Destination file */
4405 ppd_file_t
*ppd
; /* PPD file */
4406 int bytes
, /* Bytes from pipe */
4407 total
; /* Total bytes from pipe */
4408 char buffer
[2048]; /* Copy buffer */
4409 int i
; /* Looping var */
4410 char option
[PPD_MAX_NAME
], /* Option name */
4411 choice
[PPD_MAX_NAME
]; /* Choice name */
4412 ppd_size_t
*size
; /* Default size */
4413 int num_defaults
; /* Number of default options */
4414 cups_option_t
*defaults
; /* Default options */
4415 char cups_protocol
[PPD_MAX_LINE
];
4416 /* cupsProtocol attribute */
4419 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4420 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4424 * Run cups-driverd to get the PPD file...
4427 argv
[0] = "cups-driverd";
4429 argv
[2] = (char *)from
;
4432 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4434 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4435 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4436 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4437 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4440 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4441 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4443 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4444 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4455 * Wait up to 30 seconds for the PPD file to be copied...
4460 if (temppipe
[0] > CGIPipes
[0])
4461 maxfd
= temppipe
[0] + 1;
4463 maxfd
= CGIPipes
[0] + 1;
4468 * See if we have data ready...
4472 FD_SET(temppipe
[0], &input
);
4473 FD_SET(CGIPipes
[0], &input
);
4475 timeout
.tv_sec
= 30;
4476 timeout
.tv_usec
= 0;
4478 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4488 * We have timed out...
4494 if (FD_ISSET(temppipe
[0], &input
))
4497 * Read the PPD file from the pipe, and write it to the PPD file.
4500 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4502 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4511 if (FD_ISSET(CGIPipes
[0], &input
))
4521 * No data from cups-deviced...
4524 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4530 * Open the source file for a copy...
4533 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4540 * Read the source file and see what page sizes are supported...
4543 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4551 * Open the destination (if possible) and set the default options...
4556 cups_protocol
[0] = '\0';
4558 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4561 * Read all of the default lines from the old PPD...
4564 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4565 if (!strncmp(buffer
, "*Default", 8))
4568 * Add the default option...
4571 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4572 choice
, sizeof(choice
)))
4574 ppd_option_t
*ppdo
; /* PPD option */
4578 * Only add the default if the default hasn't already been
4579 * set and the choice exists in the new PPD...
4582 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4583 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4584 ppdFindChoice(ppdo
, choice
))
4585 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4589 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4590 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4594 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4597 * Add the default media sizes...
4600 num_defaults
= cupsAddOption("PageSize", size
->name
,
4601 num_defaults
, &defaults
);
4602 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4603 num_defaults
, &defaults
);
4604 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4605 num_defaults
, &defaults
);
4606 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4607 num_defaults
, &defaults
);
4613 * Open the destination file for a copy...
4616 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4618 cupsFreeOptions(num_defaults
, defaults
);
4625 * Copy the source file to the destination...
4628 cupsFileRewind(src
);
4630 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4632 if (!strncmp(buffer
, "*Default", 8))
4635 * Check for an previous default option choice...
4638 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4639 choice
, sizeof(choice
)))
4641 const char *val
; /* Default option value */
4644 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4647 * Substitute the previous choice...
4650 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4655 cupsFilePrintf(dst
, "%s\n", buffer
);
4658 if (cups_protocol
[0])
4659 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4661 cupsFreeOptions(num_defaults
, defaults
);
4664 * Close both files and return...
4671 return (cupsdCloseCreatedConfFile(dst
, to
));
4676 * 'copy_job_attrs()' - Copy job attributes.
4680 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4681 cupsd_job_t
*job
, /* I - Job */
4682 cups_array_t
*ra
, /* I - Requested attributes array */
4683 cups_array_t
*exclude
) /* I - Private attributes array */
4685 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4689 * Send the requested attributes for each job...
4692 if (!cupsArrayFind(exclude
, "all"))
4694 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4695 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4696 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4697 "number-of-documents", job
->num_files
);
4699 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4700 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4701 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4702 "job-media-progress", job
->progress
);
4704 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4705 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4707 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4708 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4710 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4711 "job-more-info", NULL
, job_uri
);
4714 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4715 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4716 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4717 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4718 job
->num_files
> 0);
4720 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4721 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4722 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4723 "job-printer-up-time", time(NULL
));
4726 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4728 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4729 con
->clientname
, con
->clientport
,
4730 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4733 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4734 "job-printer-uri", NULL
, job_uri
);
4737 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4739 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4740 con
->clientname
, con
->clientport
, "/jobs/%d",
4742 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4743 "job-uri", NULL
, job_uri
);
4748 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4753 * Generate attributes from the job structure...
4756 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-completed")))
4757 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-completed", ippTimeToDate(job
->completed_time
));
4759 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "date-time-at-creation")))
4760 ippAddDate(con
->response
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(job
->creation_time
));
4762 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4763 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4765 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4766 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4768 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4769 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4771 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4772 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4774 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4775 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4777 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4779 switch (job
->state_value
)
4781 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4783 case IPP_JSTATE_ABORTED
:
4784 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4786 case IPP_JSTATE_CANCELED
:
4787 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4789 case IPP_JSTATE_COMPLETED
:
4790 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4795 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4796 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4798 if (job
->creation_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4799 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4805 * 'copy_printer_attrs()' - Copy printer attributes.
4810 cupsd_client_t
*con
, /* I - Client connection */
4811 cupsd_printer_t
*printer
, /* I - Printer */
4812 cups_array_t
*ra
) /* I - Requested attributes array */
4814 char printer_uri
[HTTP_MAX_URI
];
4816 char printer_icons
[HTTP_MAX_URI
];
4818 time_t curtime
; /* Current time */
4819 int i
; /* Looping var */
4823 * Copy the printer attributes to the response using requested-attributes
4824 * and document-format attributes that may be provided by the client.
4827 curtime
= time(NULL
);
4829 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4830 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4831 "marker-change-time", printer
->marker_time
);
4833 if (printer
->num_printers
> 0 &&
4834 (!ra
|| cupsArrayFind(ra
, "member-uris")))
4836 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4837 cupsd_printer_t
*p2
; /* Printer in class */
4838 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4841 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
4842 IPP_TAG_URI
, "member-uris",
4843 printer
->num_printers
, NULL
,
4846 for (i
= 0; i
< printer
->num_printers
; i
++)
4848 p2
= printer
->printers
[i
];
4850 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
4851 IPP_TAG_URI
)) != NULL
)
4852 member_uris
->values
[i
].string
.text
=
4853 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4856 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
4857 sizeof(printer_uri
), "ipp", NULL
, con
->clientname
,
4859 (p2
->type
& CUPS_PRINTER_CLASS
) ?
4860 "/classes/%s" : "/printers/%s", p2
->name
);
4861 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
4867 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4868 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4869 "printer-alert", NULL
, printer
->alert
);
4871 if (printer
->alert_description
&&
4872 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4873 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4874 "printer-alert-description", NULL
,
4875 printer
->alert_description
);
4877 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-date-time"))
4878 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-config-change-date-time", ippTimeToDate(printer
->config_time
));
4880 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-time"))
4881 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4882 "printer-config-change-time", printer
->config_time
);
4884 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4885 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4886 ippTimeToDate(curtime
));
4888 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4889 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4891 if (printer
->reg_name
)
4892 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4893 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4895 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4896 "printer-dns-sd-name", 0);
4898 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4900 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4901 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4902 "printer-error-policy", NULL
, printer
->error_policy
);
4904 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
4906 static const char * const errors
[] =/* printer-error-policy-supported values */
4909 "retry-current-job",
4914 if (printer
->type
& CUPS_PRINTER_CLASS
)
4915 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4916 "printer-error-policy-supported", NULL
, "retry-current-job");
4918 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4919 "printer-error-policy-supported",
4920 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
4923 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
4925 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_icons
, sizeof(printer_icons
),
4926 "http", NULL
, con
->clientname
, con
->clientport
,
4927 "/icons/%s.png", printer
->name
);
4928 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons",
4929 NULL
, printer_icons
);
4930 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", printer_icons
);
4933 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4934 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
4936 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4937 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
4939 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
4941 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4942 "http", NULL
, con
->clientname
, con
->clientport
,
4943 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4944 "/classes/%s" : "/printers/%s", printer
->name
);
4945 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4946 "printer-more-info", NULL
, printer_uri
);
4949 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4950 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4951 "printer-op-policy", NULL
, printer
->op_policy
);
4953 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4954 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4957 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-date-time"))
4958 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-state-change-date-time", ippTimeToDate(printer
->state_time
));
4960 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4961 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4962 "printer-state-change-time", printer
->state_time
);
4964 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4965 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4966 "printer-state-message", NULL
, printer
->state_message
);
4968 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4969 add_printer_state_reasons(con
, printer
);
4971 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4973 cups_ptype_t type
; /* printer-type value */
4976 * Add the CUPS-specific printer-type attribute...
4979 type
= printer
->type
;
4981 if (printer
== DefaultPrinter
)
4982 type
|= CUPS_PRINTER_DEFAULT
;
4984 if (!printer
->accepting
)
4985 type
|= CUPS_PRINTER_REJECTING
;
4987 if (!printer
->shared
)
4988 type
|= CUPS_PRINTER_NOT_SHARED
;
4990 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
4993 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4994 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4995 "printer-up-time", curtime
);
4997 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
4999 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5000 "ipp", NULL
, con
->clientname
, con
->clientport
,
5001 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5002 "/classes/%s" : "/printers/%s", printer
->name
);
5003 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5004 "printer-uri-supported", NULL
, printer_uri
);
5005 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5009 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5010 add_queued_job_count(con
, printer
);
5012 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5013 if (printer
->ppd_attrs
)
5014 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
5015 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
5020 * 'copy_subscription_attrs()' - Copy subscription attributes.
5024 copy_subscription_attrs(
5025 cupsd_client_t
*con
, /* I - Client connection */
5026 cupsd_subscription_t
*sub
, /* I - Subscription */
5027 cups_array_t
*ra
, /* I - Requested attributes array */
5028 cups_array_t
*exclude
) /* I - Private attributes array */
5030 ipp_attribute_t
*attr
; /* Current attribute */
5031 char printer_uri
[HTTP_MAX_URI
];
5033 int count
; /* Number of events */
5034 unsigned mask
; /* Current event mask */
5035 const char *name
; /* Current event name */
5038 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5039 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
5040 con
, sub
, ra
, exclude
);
5043 * Copy the subscription attributes to the response using the
5044 * requested-attributes attribute that may be provided by the client.
5047 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5049 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5050 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5052 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5054 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5057 * Simple event list...
5060 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5061 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5062 "notify-events", NULL
, name
);
5067 * Complex event list...
5070 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5071 if (sub
->mask
& mask
)
5074 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5075 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5076 "notify-events", count
, NULL
, NULL
);
5078 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5079 if (sub
->mask
& mask
)
5081 attr
->values
[count
].string
.text
=
5082 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5089 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5090 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5091 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5092 "notify-lease-duration", sub
->lease
);
5094 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5095 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5096 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5097 "notify-recipient-uri", NULL
, sub
->recipient
);
5098 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5099 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5100 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5101 "notify-pull-method", NULL
, "ippget");
5103 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5104 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5105 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5106 "notify-subscriber-user-name", NULL
, sub
->owner
);
5108 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5109 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5110 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5111 "notify-time-interval", sub
->interval
);
5113 if (sub
->user_data_len
> 0 &&
5114 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5115 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5116 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5117 sub
->user_data
, sub
->user_data_len
);
5120 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5121 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5122 "notify-job-id", sub
->job
->id
);
5124 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5126 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5127 "ipp", NULL
, con
->clientname
, con
->clientport
,
5128 "/printers/%s", sub
->dest
->name
);
5129 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5130 "notify-printer-uri", NULL
, printer_uri
);
5133 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5134 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5135 "notify-subscription-id", sub
->id
);
5140 * 'create_job()' - Print a file to a printer or class.
5144 create_job(cupsd_client_t
*con
, /* I - Client connection */
5145 ipp_attribute_t
*uri
) /* I - Printer URI */
5147 int i
; /* Looping var */
5148 cupsd_printer_t
*printer
; /* Printer */
5149 cupsd_job_t
*job
; /* New job */
5150 static const char * const forbidden_attrs
[] =
5151 { /* List of forbidden attributes */
5155 "document-natural-language"
5159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5160 con
->number
, uri
->values
[0].string
.text
);
5163 * Is the destination valid?
5166 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5172 send_ipp_status(con
, IPP_NOT_FOUND
,
5173 _("The printer or class does not exist."));
5178 * Check for invalid Create-Job attributes and log a warning or error depending
5179 * on whether cupsd is running in "strict conformance" mode...
5183 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5185 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5187 if (StrictConformance
)
5189 send_ipp_status(con
, IPP_BAD_REQUEST
,
5190 _("The '%s' operation attribute cannot be supplied in a "
5191 "Create-Job request."), forbidden_attrs
[i
]);
5195 cupsdLogMessage(CUPSD_LOG_WARN
,
5196 "Unexpected '%s' operation attribute in a Create-Job "
5197 "request.", forbidden_attrs
[i
]);
5201 * Create the job object...
5204 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5207 job
->pending_timeout
= 1;
5210 * Save and log the job...
5213 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5214 job
->dest
, job
->username
);
5219 * 'create_requested_array()' - Create an array for the requested-attributes.
5222 static cups_array_t
* /* O - Array of attributes or NULL */
5223 create_requested_array(ipp_t
*request
) /* I - IPP request */
5225 cups_array_t
*ra
; /* Requested attributes array */
5229 * Create the array for standard attributes...
5232 ra
= ippCreateRequestedArray(request
);
5235 * Add CUPS defaults as needed...
5238 if (cupsArrayFind(ra
, "printer-defaults"))
5241 * Include user-set defaults...
5244 char *name
; /* Option name */
5246 cupsArrayRemove(ra
, "printer-defaults");
5248 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5250 name
= (char *)cupsArrayNext(CommonDefaults
))
5251 if (!cupsArrayFind(ra
, name
))
5252 cupsArrayAdd(ra
, name
);
5260 * 'create_subscriptions()' - Create one or more notification subscriptions.
5264 create_subscriptions(
5265 cupsd_client_t
*con
, /* I - Client connection */
5266 ipp_attribute_t
*uri
) /* I - Printer URI */
5268 http_status_t status
; /* Policy status */
5269 int i
; /* Looping var */
5270 ipp_attribute_t
*attr
; /* Current attribute */
5271 cups_ptype_t dtype
; /* Destination type (printer/class) */
5272 char scheme
[HTTP_MAX_URI
],
5273 /* Scheme portion of URI */
5274 userpass
[HTTP_MAX_URI
],
5275 /* Username portion of URI */
5277 /* Host portion of URI */
5278 resource
[HTTP_MAX_URI
];
5279 /* Resource portion of URI */
5280 int port
; /* Port portion of URI */
5281 cupsd_printer_t
*printer
; /* Printer/class */
5282 cupsd_job_t
*job
; /* Job */
5283 int jobid
; /* Job ID */
5284 cupsd_subscription_t
*sub
; /* Subscription object */
5285 const char *username
, /* requesting-user-name or
5286 authenticated username */
5287 *recipient
, /* notify-recipient-uri */
5288 *pullmethod
; /* notify-pull-method */
5289 ipp_attribute_t
*user_data
; /* notify-user-data */
5290 int interval
, /* notify-time-interval */
5291 lease
; /* notify-lease-duration */
5292 unsigned mask
; /* notify-events */
5293 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5294 *notify_lease
; /* notify-lease-duration(-default) */
5298 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5300 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5301 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5302 attr
->value_tag
, attr
->name
);
5304 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5309 * Is the destination valid?
5312 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5314 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5315 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5316 sizeof(host
), &port
, resource
, sizeof(resource
));
5318 if (!strcmp(resource
, "/"))
5320 dtype
= (cups_ptype_t
)0;
5323 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5325 dtype
= (cups_ptype_t
)0;
5328 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5330 dtype
= CUPS_PRINTER_CLASS
;
5333 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5339 send_ipp_status(con
, IPP_NOT_FOUND
,
5340 _("The printer or class does not exist."));
5350 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5353 send_http_error(con
, status
, printer
);
5357 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5359 send_http_error(con
, status
, NULL
);
5364 * Get the user that is requesting the subscription...
5367 username
= get_username(con
);
5370 * Find the first subscription group attribute; return if we have
5374 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5375 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5380 send_ipp_status(con
, IPP_BAD_REQUEST
,
5381 _("No subscription attributes in request."));
5386 * Process the subscription attributes in the request...
5389 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5397 lease
= DefaultLeaseDuration
;
5399 mask
= CUPSD_EVENT_NONE
;
5403 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5405 notify_lease
= ippFindAttribute(printer
->attrs
,
5406 "notify-lease-duration-default",
5410 lease
= notify_lease
->values
[0].integer
;
5414 notify_events
= NULL
;
5415 notify_lease
= NULL
;
5418 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5420 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5421 attr
->value_tag
== IPP_TAG_URI
)
5424 * Validate the recipient scheme against the ServerBin/notifier
5428 char notifier
[1024]; /* Notifier filename */
5431 recipient
= attr
->values
[0].string
.text
;
5433 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5434 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5435 host
, sizeof(host
), &port
,
5436 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5438 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5439 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5440 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5441 "notify-status-code", IPP_URI_SCHEME
);
5445 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5447 if (access(notifier
, X_OK
))
5449 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5450 _("notify-recipient-uri URI \"%s\" uses unknown "
5451 "scheme."), recipient
);
5452 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5453 "notify-status-code", IPP_URI_SCHEME
);
5457 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5459 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5460 _("notify-recipient-uri URI \"%s\" is already used."),
5462 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5463 "notify-status-code", IPP_ATTRIBUTES
);
5467 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5468 attr
->value_tag
== IPP_TAG_KEYWORD
)
5470 pullmethod
= attr
->values
[0].string
.text
;
5472 if (strcmp(pullmethod
, "ippget"))
5474 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5475 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5476 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5477 "notify-status-code", IPP_ATTRIBUTES
);
5481 else if (!strcmp(attr
->name
, "notify-charset") &&
5482 attr
->value_tag
== IPP_TAG_CHARSET
&&
5483 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5484 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5486 send_ipp_status(con
, IPP_CHARSET
,
5487 _("Character set \"%s\" not supported."),
5488 attr
->values
[0].string
.text
);
5491 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5492 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5493 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5495 send_ipp_status(con
, IPP_CHARSET
,
5496 _("Language \"%s\" not supported."),
5497 attr
->values
[0].string
.text
);
5500 else if (!strcmp(attr
->name
, "notify-user-data") &&
5501 attr
->value_tag
== IPP_TAG_STRING
)
5503 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5505 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5506 _("The notify-user-data value is too large "
5507 "(%d > 63 octets)."),
5508 attr
->values
[0].unknown
.length
);
5514 else if (!strcmp(attr
->name
, "notify-events") &&
5515 attr
->value_tag
== IPP_TAG_KEYWORD
)
5516 notify_events
= attr
;
5517 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5518 attr
->value_tag
== IPP_TAG_INTEGER
)
5519 lease
= attr
->values
[0].integer
;
5520 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5521 attr
->value_tag
== IPP_TAG_INTEGER
)
5522 interval
= attr
->values
[0].integer
;
5523 else if (!strcmp(attr
->name
, "notify-job-id") &&
5524 attr
->value_tag
== IPP_TAG_INTEGER
)
5525 jobid
= attr
->values
[0].integer
;
5532 for (i
= 0; i
< notify_events
->num_values
; i
++)
5533 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5537 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5539 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5540 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5541 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5543 if (!recipient
&& !pullmethod
)
5546 if (mask
== CUPSD_EVENT_NONE
)
5549 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5551 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5554 send_ipp_status(con
, IPP_BAD_REQUEST
,
5555 _("notify-events not specified."));
5560 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5562 cupsdLogMessage(CUPSD_LOG_INFO
,
5563 "create_subscriptions: Limiting notify-lease-duration to "
5566 lease
= MaxLeaseDuration
;
5571 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5573 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5581 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5583 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5584 _("There are too many subscriptions."));
5589 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5592 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5593 "Added subscription #%d for printer \"%s\".",
5594 sub
->id
, printer
->name
);
5596 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5599 sub
->interval
= interval
;
5601 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5603 cupsdSetString(&sub
->owner
, username
);
5607 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5608 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5609 (size_t)sub
->user_data_len
);
5612 ippAddSeparator(con
->response
);
5613 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5614 "notify-subscription-id", sub
->id
);
5616 con
->response
->request
.status
.status_code
= IPP_OK
;
5622 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5627 * 'delete_printer()' - Remove a printer or class from the system.
5631 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5632 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5634 http_status_t status
; /* Policy status */
5635 cups_ptype_t dtype
; /* Destination type (printer/class) */
5636 cupsd_printer_t
*printer
; /* Printer/class */
5637 char filename
[1024]; /* Script/PPD filename */
5640 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5641 con
->number
, uri
->values
[0].string
.text
);
5644 * Do we have a valid URI?
5647 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5653 send_ipp_status(con
, IPP_NOT_FOUND
,
5654 _("The printer or class does not exist."));
5662 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5664 send_http_error(con
, status
, NULL
);
5669 * Remove old jobs...
5672 cupsdCancelJobs(printer
->name
, NULL
, 1);
5675 * Remove old subscriptions and send a "deleted printer" event...
5678 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5679 "%s \"%s\" deleted by \"%s\".",
5680 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5681 printer
->name
, get_username(con
));
5683 cupsdExpireSubscriptions(printer
, NULL
);
5686 * Remove any old PPD or script files...
5689 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5692 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s.O", ServerRoot
,
5696 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5699 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
5703 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
5706 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
5710 * Unregister color profiles...
5713 cupsdUnregisterColor(printer
);
5715 if (dtype
& CUPS_PRINTER_CLASS
)
5717 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5718 printer
->name
, get_username(con
));
5720 cupsdDeletePrinter(printer
, 0);
5721 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5725 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5726 printer
->name
, get_username(con
));
5728 if (cupsdDeletePrinter(printer
, 0))
5729 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5731 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5734 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
5737 * Return with no errors...
5740 con
->response
->request
.status
.status_code
= IPP_OK
;
5745 * 'get_default()' - Get the default destination.
5749 get_default(cupsd_client_t
*con
) /* I - Client connection */
5751 http_status_t status
; /* Policy status */
5752 cups_array_t
*ra
; /* Requested attributes array */
5755 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
5761 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5763 send_http_error(con
, status
, NULL
);
5769 ra
= create_requested_array(con
->request
);
5771 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5773 cupsArrayDelete(ra
);
5775 con
->response
->request
.status
.status_code
= IPP_OK
;
5778 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
5783 * 'get_devices()' - Get the list of available devices on the local system.
5787 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5789 http_status_t status
; /* Policy status */
5790 ipp_attribute_t
*limit
, /* limit attribute */
5791 *timeout
, /* timeout attribute */
5792 *requested
, /* requested-attributes attribute */
5793 *exclude
, /* exclude-schemes attribute */
5794 *include
; /* include-schemes attribute */
5795 char command
[1024], /* cups-deviced command */
5796 options
[2048], /* Options to pass to command */
5798 /* String for requested attributes */
5800 /* String for excluded schemes */
5802 /* String for included schemes */
5805 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
5811 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5813 send_http_error(con
, status
, NULL
);
5818 * Run cups-deviced command with the given options...
5821 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5822 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
5823 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5825 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
5826 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
5829 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5831 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5834 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
5836 exclude_str
[0] = '\0';
5839 url_encode_attr(include
, include_str
, sizeof(include_str
));
5841 include_str
[0] = '\0';
5843 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5844 snprintf(options
, sizeof(options
),
5845 "%d+%d+%d+%d+%s%s%s%s%s",
5846 con
->request
->request
.op
.request_id
,
5847 limit
? limit
->values
[0].integer
: 0,
5848 timeout
? timeout
->values
[0].integer
: 15,
5851 exclude_str
[0] ? "%20" : "", exclude_str
,
5852 include_str
[0] ? "%20" : "", include_str
);
5854 if (cupsdSendCommand(con
, command
, options
, 1))
5857 * Command started successfully, don't send an IPP response here...
5860 ippDelete(con
->response
);
5861 con
->response
= NULL
;
5866 * Command failed, return "internal error" so the user knows something
5870 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5871 _("cups-deviced failed to execute."));
5877 * 'get_document()' - Get a copy of a job file.
5881 get_document(cupsd_client_t
*con
, /* I - Client connection */
5882 ipp_attribute_t
*uri
) /* I - Job URI */
5884 http_status_t status
; /* Policy status */
5885 ipp_attribute_t
*attr
; /* Current attribute */
5886 int jobid
; /* Job ID */
5887 int docnum
; /* Document number */
5888 cupsd_job_t
*job
; /* Current job */
5889 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
5890 username
[HTTP_MAX_URI
], /* Username portion of URI */
5891 host
[HTTP_MAX_URI
], /* Host portion of URI */
5892 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5893 int port
; /* Port portion of URI */
5894 char filename
[1024], /* Filename for document */
5895 format
[1024]; /* Format for document */
5898 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5899 con
->number
, uri
->values
[0].string
.text
);
5902 * See if we have a job URI or a printer URI...
5905 if (!strcmp(uri
->name
, "printer-uri"))
5908 * Got a printer URI; see if we also have a job-id attribute...
5911 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5912 IPP_TAG_INTEGER
)) == NULL
)
5914 send_ipp_status(con
, IPP_BAD_REQUEST
,
5915 _("Got a printer-uri attribute but no job-id."));
5919 jobid
= attr
->values
[0].integer
;
5924 * Got a job URI; parse it to get the job ID...
5927 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5928 sizeof(scheme
), username
, sizeof(username
), host
,
5929 sizeof(host
), &port
, resource
, sizeof(resource
));
5931 if (strncmp(resource
, "/jobs/", 6))
5937 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
5938 uri
->values
[0].string
.text
);
5942 jobid
= atoi(resource
+ 6);
5946 * See if the job exists...
5949 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5952 * Nope - return a "not found" error...
5955 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
5963 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
5964 job
->username
)) != HTTP_OK
)
5966 send_http_error(con
, status
, NULL
);
5971 * Get the document number...
5974 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5975 IPP_TAG_INTEGER
)) == NULL
)
5977 send_ipp_status(con
, IPP_BAD_REQUEST
,
5978 _("Missing document-number attribute."));
5982 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5983 attr
->num_values
> 1)
5985 send_ipp_status(con
, IPP_NOT_FOUND
,
5986 _("Document #%d does not exist in job #%d."), docnum
,
5991 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5993 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5995 cupsdLogMessage(CUPSD_LOG_ERROR
,
5996 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5998 send_ipp_status(con
, IPP_NOT_FOUND
,
5999 _("Unable to open document #%d in job #%d."), docnum
,
6004 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6008 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6009 job
->filetypes
[docnum
- 1]->type
);
6011 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6013 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6015 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6016 IPP_TAG_NAME
)) != NULL
)
6017 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6018 NULL
, attr
->values
[0].string
.text
);
6023 * 'get_job_attrs()' - Get job attributes.
6027 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6028 ipp_attribute_t
*uri
) /* I - Job URI */
6030 http_status_t status
; /* Policy status */
6031 ipp_attribute_t
*attr
; /* Current attribute */
6032 int jobid
; /* Job ID */
6033 cupsd_job_t
*job
; /* Current job */
6034 cupsd_printer_t
*printer
; /* Current printer */
6035 cupsd_policy_t
*policy
; /* Current security policy */
6036 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6037 username
[HTTP_MAX_URI
], /* Username portion of URI */
6038 host
[HTTP_MAX_URI
], /* Host portion of URI */
6039 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6040 int port
; /* Port portion of URI */
6041 cups_array_t
*ra
, /* Requested attributes array */
6042 *exclude
; /* Private attributes array */
6045 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6046 con
->number
, uri
->values
[0].string
.text
);
6049 * See if we have a job URI or a printer URI...
6052 if (!strcmp(uri
->name
, "printer-uri"))
6055 * Got a printer URI; see if we also have a job-id attribute...
6058 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6059 IPP_TAG_INTEGER
)) == NULL
)
6061 send_ipp_status(con
, IPP_BAD_REQUEST
,
6062 _("Got a printer-uri attribute but no job-id."));
6066 jobid
= attr
->values
[0].integer
;
6071 * Got a job URI; parse it to get the job ID...
6074 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6075 sizeof(scheme
), username
, sizeof(username
), host
,
6076 sizeof(host
), &port
, resource
, sizeof(resource
));
6078 if (strncmp(resource
, "/jobs/", 6))
6084 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6085 uri
->values
[0].string
.text
);
6089 jobid
= atoi(resource
+ 6);
6093 * See if the job exists...
6096 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6099 * Nope - return a "not found" error...
6102 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6110 if ((printer
= job
->printer
) == NULL
)
6111 printer
= cupsdFindDest(job
->dest
);
6114 policy
= printer
->op_policy_ptr
;
6116 policy
= DefaultPolicyPtr
;
6118 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6120 send_http_error(con
, status
, NULL
);
6124 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6127 * Copy attributes...
6132 ra
= create_requested_array(con
->request
);
6133 copy_job_attrs(con
, job
, ra
, exclude
);
6134 cupsArrayDelete(ra
);
6136 con
->response
->request
.status
.status_code
= IPP_OK
;
6141 * 'get_jobs()' - Get a list of jobs for the specified printer.
6145 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6146 ipp_attribute_t
*uri
) /* I - Printer URI */
6148 http_status_t status
; /* Policy status */
6149 ipp_attribute_t
*attr
; /* Current attribute */
6150 const char *dest
; /* Destination */
6151 cups_ptype_t dtype
; /* Destination type (printer/class) */
6152 cups_ptype_t dmask
; /* Destination type mask */
6153 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6154 username
[HTTP_MAX_URI
], /* Username portion of URI */
6155 host
[HTTP_MAX_URI
], /* Host portion of URI */
6156 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6157 int port
; /* Port portion of URI */
6158 int job_comparison
; /* Job comparison */
6159 ipp_jstate_t job_state
; /* job-state value */
6160 int first_job_id
= 1, /* First job ID */
6161 first_index
= 1, /* First index */
6162 current_index
= 0; /* Current index */
6163 int limit
= 0; /* Maximum number of jobs to return */
6164 int count
; /* Number of jobs that match */
6165 int need_load_job
= 0; /* Do we need to load the job? */
6166 const char *job_attr
; /* Job attribute requested */
6167 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6168 cupsd_job_t
*job
; /* Current job pointer */
6169 cupsd_printer_t
*printer
; /* Printer */
6170 cups_array_t
*list
; /* Which job list... */
6171 int delete_list
= 0; /* Delete the list afterwards? */
6172 cups_array_t
*ra
, /* Requested attributes array */
6173 *exclude
; /* Private attributes array */
6174 cupsd_policy_t
*policy
; /* Current policy */
6177 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6178 uri
->values
[0].string
.text
);
6181 * Is the destination valid?
6184 if (strcmp(uri
->name
, "printer-uri"))
6186 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6190 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6191 sizeof(scheme
), username
, sizeof(username
), host
,
6192 sizeof(host
), &port
, resource
, sizeof(resource
));
6194 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6197 dtype
= (cups_ptype_t
)0;
6198 dmask
= (cups_ptype_t
)0;
6201 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6204 dtype
= (cups_ptype_t
)0;
6205 dmask
= CUPS_PRINTER_CLASS
;
6208 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6211 dtype
= CUPS_PRINTER_CLASS
;
6212 dmask
= CUPS_PRINTER_CLASS
;
6215 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6222 send_ipp_status(con
, IPP_NOT_FOUND
,
6223 _("The printer or class does not exist."));
6228 dtype
&= CUPS_PRINTER_CLASS
;
6229 dmask
= CUPS_PRINTER_CLASS
;
6237 policy
= printer
->op_policy_ptr
;
6239 policy
= DefaultPolicyPtr
;
6241 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6243 send_http_error(con
, status
, NULL
);
6247 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6250 * See if the "which-jobs" attribute have been specified...
6253 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6254 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6256 send_ipp_status(con
, IPP_CONFLICT
,
6257 _("The %s attribute cannot be provided with job-ids."),
6261 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6263 job_comparison
= -1;
6264 job_state
= IPP_JOB_STOPPED
;
6267 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6270 job_state
= IPP_JOB_CANCELED
;
6271 list
= cupsdGetCompletedJobs(printer
);
6274 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6277 job_state
= IPP_JOB_ABORTED
;
6278 list
= cupsdGetCompletedJobs(printer
);
6281 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6284 job_state
= IPP_JOB_PENDING
;
6287 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6290 job_state
= IPP_JOB_CANCELED
;
6291 list
= cupsdGetCompletedJobs(printer
);
6294 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6297 job_state
= IPP_JOB_PENDING
;
6300 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6303 job_state
= IPP_JOB_HELD
;
6306 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6309 job_state
= IPP_JOB_PROCESSING
;
6310 list
= PrintingJobs
;
6312 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6315 job_state
= IPP_JOB_STOPPED
;
6320 send_ipp_status(con
, IPP_ATTRIBUTES
,
6321 _("The which-jobs value \"%s\" is not supported."),
6322 attr
->values
[0].string
.text
);
6323 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6324 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6329 * See if they want to limit the number of jobs reported...
6332 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6336 send_ipp_status(con
, IPP_CONFLICT
,
6337 _("The %s attribute cannot be provided with job-ids."),
6342 limit
= attr
->values
[0].integer
;
6345 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6349 send_ipp_status(con
, IPP_CONFLICT
,
6350 _("The %s attribute cannot be provided with job-ids."),
6355 first_index
= attr
->values
[0].integer
;
6357 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6361 send_ipp_status(con
, IPP_CONFLICT
,
6362 _("The %s attribute cannot be provided with job-ids."),
6367 first_job_id
= attr
->values
[0].integer
;
6371 * See if we only want to see jobs for a specific user...
6374 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6376 send_ipp_status(con
, IPP_CONFLICT
,
6377 _("The %s attribute cannot be provided with job-ids."),
6381 else if (attr
&& attr
->values
[0].boolean
)
6382 strlcpy(username
, get_username(con
), sizeof(username
));
6386 ra
= create_requested_array(con
->request
);
6387 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6388 if (strcmp(job_attr
, "job-id") &&
6389 strcmp(job_attr
, "job-k-octets") &&
6390 strcmp(job_attr
, "job-media-progress") &&
6391 strcmp(job_attr
, "job-more-info") &&
6392 strcmp(job_attr
, "job-name") &&
6393 strcmp(job_attr
, "job-originating-user-name") &&
6394 strcmp(job_attr
, "job-preserved") &&
6395 strcmp(job_attr
, "job-printer-up-time") &&
6396 strcmp(job_attr
, "job-printer-uri") &&
6397 strcmp(job_attr
, "job-state") &&
6398 strcmp(job_attr
, "job-state-reasons") &&
6399 strcmp(job_attr
, "job-uri") &&
6400 strcmp(job_attr
, "time-at-completed") &&
6401 strcmp(job_attr
, "time-at-creation") &&
6402 strcmp(job_attr
, "number-of-documents"))
6408 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6411 * Limit expensive Get-Jobs for job history to 500 jobs...
6414 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6417 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6421 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6425 * OK, build a list of jobs for this printer...
6430 int i
; /* Looping var */
6432 for (i
= 0; i
< job_ids
->num_values
; i
++)
6434 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6438 if (i
< job_ids
->num_values
)
6440 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6441 job_ids
->values
[i
].integer
);
6445 for (i
= 0; i
< job_ids
->num_values
; i
++)
6447 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6449 if (need_load_job
&& !job
->attrs
)
6455 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6461 ippAddSeparator(con
->response
);
6463 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6464 job
->printer
->op_policy_ptr
:
6465 policy
, con
, job
->printer
,
6468 copy_job_attrs(con
, job
, ra
, exclude
);
6473 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6474 (limit
<= 0 || count
< limit
) && job
;
6475 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6478 * Filter out jobs that don't match...
6481 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6482 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6483 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6484 job
->username
, job
->state_value
, job
->attrs
);
6486 if (!job
->dest
|| !job
->username
)
6489 if (!job
->dest
|| !job
->username
)
6492 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6493 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6495 if ((job
->dtype
& dmask
) != dtype
&&
6496 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6499 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6500 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6501 (job_comparison
> 0 && job
->state_value
< job_state
))
6504 if (job
->id
< first_job_id
)
6508 if (current_index
< first_index
)
6511 if (need_load_job
&& !job
->attrs
)
6517 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6522 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6526 ippAddSeparator(con
->response
);
6530 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6531 job
->printer
->op_policy_ptr
:
6532 policy
, con
, job
->printer
,
6535 copy_job_attrs(con
, job
, ra
, exclude
);
6538 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6541 cupsArrayDelete(ra
);
6544 cupsArrayDelete(list
);
6546 con
->response
->request
.status
.status_code
= IPP_OK
;
6551 * 'get_notifications()' - Get events for a subscription.
6555 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6557 int i
, j
; /* Looping vars */
6558 http_status_t status
; /* Policy status */
6559 cupsd_subscription_t
*sub
; /* Subscription */
6560 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6561 *sequences
; /* notify-sequence-numbers */
6562 int min_seq
; /* Minimum sequence number */
6563 int interval
; /* Poll interval */
6566 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6570 * Get subscription attributes...
6573 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6575 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6580 send_ipp_status(con
, IPP_BAD_REQUEST
,
6581 _("Missing notify-subscription-ids attribute."));
6586 * Are the subscription IDs valid?
6589 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6591 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6594 * Bad subscription ID...
6597 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6598 ids
->values
[i
].integer
);
6606 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6608 con
, sub
->owner
)) != HTTP_OK
)
6610 send_http_error(con
, status
, sub
->dest
);
6615 * Check the subscription type and update the interval accordingly.
6618 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6621 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6623 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6629 * Tell the client to poll again in N seconds...
6633 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6634 "notify-get-interval", interval
);
6636 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6637 "printer-up-time", time(NULL
));
6640 * Copy the subscription event attributes to the response.
6643 con
->response
->request
.status
.status_code
=
6644 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6646 for (i
= 0; i
< ids
->num_values
; i
++)
6649 * Get the subscription and sequence number...
6652 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6654 if (sequences
&& i
< sequences
->num_values
)
6655 min_seq
= sequences
->values
[i
].integer
;
6660 * If we don't have any new events, nothing to do here...
6663 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
6667 * Otherwise copy all of the new events...
6670 if (sub
->first_event_id
> min_seq
)
6673 j
= min_seq
- sub
->first_event_id
;
6675 for (; j
< cupsArrayCount(sub
->events
); j
++)
6677 ippAddSeparator(con
->response
);
6679 copy_attrs(con
->response
,
6680 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
6681 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
6688 * 'get_ppd()' - Get a named PPD from the local system.
6692 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6693 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6695 http_status_t status
; /* Policy status */
6696 cupsd_printer_t
*dest
; /* Destination */
6697 cups_ptype_t dtype
; /* Destination type */
6700 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6701 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6703 if (!strcmp(uri
->name
, "ppd-name"))
6706 * Return a PPD file from cups-driverd...
6709 char command
[1024], /* cups-driverd command */
6710 options
[1024], /* Options to pass to command */
6711 ppd_name
[1024]; /* ppd-name */
6718 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6720 send_http_error(con
, status
, NULL
);
6725 * Run cups-driverd command with the given options...
6728 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6729 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6730 snprintf(options
, sizeof(options
), "get+%d+%s",
6731 con
->request
->request
.op
.request_id
, ppd_name
);
6733 if (cupsdSendCommand(con
, command
, options
, 0))
6736 * Command started successfully, don't send an IPP response here...
6739 ippDelete(con
->response
);
6740 con
->response
= NULL
;
6745 * Command failed, return "internal error" so the user knows something
6749 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6750 _("cups-driverd failed to execute."));
6753 else if (!strcmp(uri
->name
, "printer-uri") &&
6754 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6756 int i
; /* Looping var */
6757 char filename
[1024]; /* PPD filename */
6764 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6766 send_http_error(con
, status
, dest
);
6771 * See if we need the PPD for a class or remote printer...
6774 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6777 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6779 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6780 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6781 "printer-uri", NULL
, dest
->uri
);
6784 else if (dtype
& CUPS_PRINTER_CLASS
)
6786 for (i
= 0; i
< dest
->num_printers
; i
++)
6787 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
6789 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6790 dest
->printers
[i
]->name
);
6792 if (!access(filename
, 0))
6796 if (i
< dest
->num_printers
)
6797 dest
= dest
->printers
[i
];
6800 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6801 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6802 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6808 * Found the printer with the PPD file, now see if there is one...
6811 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6813 send_ipp_status(con
, IPP_NOT_FOUND
,
6814 _("The PPD file \"%s\" could not be opened: %s"),
6815 uri
->values
[0].string
.text
, strerror(errno
));
6819 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6823 con
->response
->request
.status
.status_code
= IPP_OK
;
6826 send_ipp_status(con
, IPP_NOT_FOUND
,
6827 _("The PPD file \"%s\" could not be found."),
6828 uri
->values
[0].string
.text
);
6833 * 'get_ppds()' - Get the list of PPD files on the local system.
6837 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6839 http_status_t status
; /* Policy status */
6840 ipp_attribute_t
*limit
, /* Limit attribute */
6841 *device
, /* ppd-device-id attribute */
6842 *language
, /* ppd-natural-language attribute */
6843 *make
, /* ppd-make attribute */
6844 *model
, /* ppd-make-and-model attribute */
6845 *model_number
, /* ppd-model-number attribute */
6846 *product
, /* ppd-product attribute */
6847 *psversion
, /* ppd-psverion attribute */
6848 *type
, /* ppd-type attribute */
6849 *requested
, /* requested-attributes attribute */
6850 *exclude
, /* exclude-schemes attribute */
6851 *include
; /* include-schemes attribute */
6852 char command
[1024], /* cups-driverd command */
6853 options
[4096], /* Options to pass to command */
6854 device_str
[256],/* Escaped ppd-device-id string */
6856 /* Escaped ppd-natural-language */
6857 make_str
[256], /* Escaped ppd-make string */
6858 model_str
[256], /* Escaped ppd-make-and-model string */
6859 model_number_str
[256],
6860 /* ppd-model-number string */
6862 /* Escaped ppd-product string */
6864 /* Escaped ppd-psversion string */
6865 type_str
[256], /* Escaped ppd-type string */
6867 /* String for requested attributes */
6869 /* String for excluded schemes */
6871 /* String for included schemes */
6874 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
6880 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6882 send_http_error(con
, status
, NULL
);
6887 * Run cups-driverd command with the given options...
6890 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6891 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6892 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6894 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6895 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6897 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6899 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6900 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6901 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6902 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6904 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
6906 include
= ippFindAttribute(con
->request
, "include-schemes",
6910 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6912 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6915 url_encode_attr(device
, device_str
, sizeof(device_str
));
6917 device_str
[0] = '\0';
6920 url_encode_attr(language
, language_str
, sizeof(language_str
));
6922 language_str
[0] = '\0';
6925 url_encode_attr(make
, make_str
, sizeof(make_str
));
6930 url_encode_attr(model
, model_str
, sizeof(model_str
));
6932 model_str
[0] = '\0';
6935 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6936 model_number
->values
[0].integer
);
6938 model_number_str
[0] = '\0';
6941 url_encode_attr(product
, product_str
, sizeof(product_str
));
6943 product_str
[0] = '\0';
6946 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6948 psversion_str
[0] = '\0';
6951 url_encode_attr(type
, type_str
, sizeof(type_str
));
6956 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6958 exclude_str
[0] = '\0';
6961 url_encode_attr(include
, include_str
, sizeof(include_str
));
6963 include_str
[0] = '\0';
6965 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6966 snprintf(options
, sizeof(options
),
6967 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6968 con
->request
->request
.op
.request_id
,
6969 limit
? limit
->values
[0].integer
: 0,
6971 device
? "%20" : "", device_str
,
6972 language
? "%20" : "", language_str
,
6973 make
? "%20" : "", make_str
,
6974 model
? "%20" : "", model_str
,
6975 model_number
? "%20" : "", model_number_str
,
6976 product
? "%20" : "", product_str
,
6977 psversion
? "%20" : "", psversion_str
,
6978 type
? "%20" : "", type_str
,
6979 exclude_str
[0] ? "%20" : "", exclude_str
,
6980 include_str
[0] ? "%20" : "", include_str
);
6982 if (cupsdSendCommand(con
, command
, options
, 0))
6985 * Command started successfully, don't send an IPP response here...
6988 ippDelete(con
->response
);
6989 con
->response
= NULL
;
6994 * Command failed, return "internal error" so the user knows something
6998 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6999 _("cups-driverd failed to execute."));
7005 * 'get_printer_attrs()' - Get printer attributes.
7009 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7010 ipp_attribute_t
*uri
) /* I - Printer URI */
7012 http_status_t status
; /* Policy status */
7013 cups_ptype_t dtype
; /* Destination type (printer/class) */
7014 cupsd_printer_t
*printer
; /* Printer/class */
7015 cups_array_t
*ra
; /* Requested attributes array */
7018 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7019 con
->number
, uri
->values
[0].string
.text
);
7022 * Is the destination valid?
7025 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7031 send_ipp_status(con
, IPP_NOT_FOUND
,
7032 _("The printer or class does not exist."));
7040 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7042 send_http_error(con
, status
, printer
);
7047 * Send the attributes...
7050 ra
= create_requested_array(con
->request
);
7052 copy_printer_attrs(con
, printer
, ra
);
7054 cupsArrayDelete(ra
);
7056 con
->response
->request
.status
.status_code
= IPP_OK
;
7061 * 'get_printer_supported()' - Get printer supported values.
7065 get_printer_supported(
7066 cupsd_client_t
*con
, /* I - Client connection */
7067 ipp_attribute_t
*uri
) /* I - Printer URI */
7069 http_status_t status
; /* Policy status */
7070 cups_ptype_t dtype
; /* Destination type (printer/class) */
7071 cupsd_printer_t
*printer
; /* Printer/class */
7074 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7075 con
->number
, uri
->values
[0].string
.text
);
7078 * Is the destination valid?
7081 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7087 send_ipp_status(con
, IPP_NOT_FOUND
,
7088 _("The printer or class does not exist."));
7096 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7098 send_http_error(con
, status
, printer
);
7103 * Return a list of attributes that can be set via Set-Printer-Attributes.
7106 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7107 "printer-geo-location", 0);
7108 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7110 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7111 "printer-location", 0);
7112 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7113 "printer-organization", 0);
7114 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7115 "printer-organizational-unit", 0);
7117 con
->response
->request
.status
.status_code
= IPP_OK
;
7122 * 'get_printers()' - Get a list of printers or classes.
7126 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7127 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7129 http_status_t status
; /* Policy status */
7130 ipp_attribute_t
*attr
; /* Current attribute */
7131 int limit
; /* Max number of printers to return */
7132 int count
; /* Number of printers that match */
7133 cupsd_printer_t
*printer
; /* Current printer pointer */
7134 cups_ptype_t printer_type
, /* printer-type attribute */
7135 printer_mask
; /* printer-type-mask attribute */
7136 char *location
; /* Location string */
7137 const char *username
; /* Current user */
7138 char *first_printer_name
; /* first-printer-name attribute */
7139 cups_array_t
*ra
; /* Requested attributes array */
7140 int local
; /* Local connection? */
7143 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7150 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7152 send_http_error(con
, status
, NULL
);
7157 * Check for printers...
7160 if (!Printers
|| !cupsArrayCount(Printers
))
7162 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7167 * See if they want to limit the number of printers reported...
7170 if ((attr
= ippFindAttribute(con
->request
, "limit",
7171 IPP_TAG_INTEGER
)) != NULL
)
7172 limit
= attr
->values
[0].integer
;
7176 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7177 IPP_TAG_NAME
)) != NULL
)
7178 first_printer_name
= attr
->values
[0].string
.text
;
7180 first_printer_name
= NULL
;
7183 * Support filtering...
7186 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7187 IPP_TAG_ENUM
)) != NULL
)
7188 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7190 printer_type
= (cups_ptype_t
)0;
7192 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7193 IPP_TAG_ENUM
)) != NULL
)
7194 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7196 printer_mask
= (cups_ptype_t
)0;
7198 local
= httpAddrLocalhost(&(con
->clientaddr
));
7200 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7201 IPP_TAG_TEXT
)) != NULL
)
7202 location
= attr
->values
[0].string
.text
;
7206 if (con
->username
[0])
7207 username
= con
->username
;
7208 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7209 IPP_TAG_NAME
)) != NULL
)
7210 username
= attr
->values
[0].string
.text
;
7214 ra
= create_requested_array(con
->request
);
7217 * OK, build a list of printers for this printer...
7220 if (first_printer_name
)
7222 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7223 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7226 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7229 count
< limit
&& printer
;
7230 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7232 if (!local
&& !printer
->shared
)
7235 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7236 (printer
->type
& printer_mask
) == printer_type
&&
7238 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7241 * If a username is specified, see if it is allowed or denied
7245 if (cupsArrayCount(printer
->users
) && username
&&
7246 !user_allowed(printer
, username
))
7250 * Add the group separator as needed...
7254 ippAddSeparator(con
->response
);
7259 * Send the attributes...
7262 copy_printer_attrs(con
, printer
, ra
);
7266 cupsArrayDelete(ra
);
7268 con
->response
->request
.status
.status_code
= IPP_OK
;
7273 * 'get_subscription_attrs()' - Get subscription attributes.
7277 get_subscription_attrs(
7278 cupsd_client_t
*con
, /* I - Client connection */
7279 int sub_id
) /* I - Subscription ID */
7281 http_status_t status
; /* Policy status */
7282 cupsd_subscription_t
*sub
; /* Subscription */
7283 cupsd_policy_t
*policy
; /* Current security policy */
7284 cups_array_t
*ra
, /* Requested attributes array */
7285 *exclude
; /* Private attributes array */
7288 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7289 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7290 con
, con
->number
, sub_id
);
7293 * Is the subscription ID valid?
7296 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7299 * Bad subscription ID...
7302 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7312 policy
= sub
->dest
->op_policy_ptr
;
7314 policy
= DefaultPolicyPtr
;
7316 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7318 send_http_error(con
, status
, sub
->dest
);
7322 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7325 * Copy the subscription attributes to the response using the
7326 * requested-attributes attribute that may be provided by the client.
7329 ra
= create_requested_array(con
->request
);
7331 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7333 cupsArrayDelete(ra
);
7335 con
->response
->request
.status
.status_code
= IPP_OK
;
7340 * 'get_subscriptions()' - Get subscriptions.
7344 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7345 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7347 http_status_t status
; /* Policy status */
7348 int count
; /* Number of subscriptions */
7349 int limit
; /* Limit */
7350 cupsd_subscription_t
*sub
; /* Subscription */
7351 cups_array_t
*ra
; /* Requested attributes array */
7352 ipp_attribute_t
*attr
; /* Attribute */
7353 cups_ptype_t dtype
; /* Destination type (printer/class) */
7354 char scheme
[HTTP_MAX_URI
],
7355 /* Scheme portion of URI */
7356 username
[HTTP_MAX_URI
],
7357 /* Username portion of URI */
7359 /* Host portion of URI */
7360 resource
[HTTP_MAX_URI
];
7361 /* Resource portion of URI */
7362 int port
; /* Port portion of URI */
7363 cupsd_job_t
*job
; /* Job pointer */
7364 cupsd_printer_t
*printer
; /* Printer */
7365 cupsd_policy_t
*policy
; /* Policy */
7366 cups_array_t
*exclude
; /* Private attributes array */
7369 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7370 "get_subscriptions(con=%p[%d], uri=%s)",
7371 con
, con
->number
, uri
->values
[0].string
.text
);
7374 * Is the destination valid?
7377 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7378 sizeof(scheme
), username
, sizeof(username
), host
,
7379 sizeof(host
), &port
, resource
, sizeof(resource
));
7381 if (!strcmp(resource
, "/") ||
7382 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7383 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7384 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7389 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7392 job
= cupsdFindJob(atoi(resource
+ 6));
7396 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7397 atoi(resource
+ 6));
7401 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7407 send_ipp_status(con
, IPP_NOT_FOUND
,
7408 _("The printer or class does not exist."));
7411 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7412 IPP_TAG_INTEGER
)) != NULL
)
7414 job
= cupsdFindJob(attr
->values
[0].integer
);
7418 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7419 attr
->values
[0].integer
);
7431 policy
= printer
->op_policy_ptr
;
7433 policy
= DefaultPolicyPtr
;
7435 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7437 send_http_error(con
, status
, printer
);
7442 * Copy the subscription attributes to the response using the
7443 * requested-attributes attribute that may be provided by the client.
7446 ra
= create_requested_array(con
->request
);
7448 if ((attr
= ippFindAttribute(con
->request
, "limit",
7449 IPP_TAG_INTEGER
)) != NULL
)
7450 limit
= attr
->values
[0].integer
;
7455 * See if we only want to see subscriptions for a specific user...
7458 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7459 IPP_TAG_BOOLEAN
)) != NULL
&&
7460 attr
->values
[0].boolean
)
7461 strlcpy(username
, get_username(con
), sizeof(username
));
7465 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7467 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7468 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7469 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7471 ippAddSeparator(con
->response
);
7473 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7474 policy
, con
, sub
->dest
,
7477 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7480 if (limit
&& count
>= limit
)
7484 cupsArrayDelete(ra
);
7487 con
->response
->request
.status
.status_code
= IPP_OK
;
7489 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7494 * 'get_username()' - Get the username associated with a request.
7497 static const char * /* O - Username */
7498 get_username(cupsd_client_t
*con
) /* I - Connection */
7500 ipp_attribute_t
*attr
; /* Attribute */
7503 if (con
->username
[0])
7504 return (con
->username
);
7505 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7506 IPP_TAG_NAME
)) != NULL
)
7507 return (attr
->values
[0].string
.text
);
7509 return ("anonymous");
7514 * 'hold_job()' - Hold a print job.
7518 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7519 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7521 ipp_attribute_t
*attr
; /* Current job-hold-until */
7522 const char *when
; /* New value */
7523 int jobid
; /* Job ID */
7524 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7525 username
[HTTP_MAX_URI
], /* Username portion of URI */
7526 host
[HTTP_MAX_URI
], /* Host portion of URI */
7527 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7528 int port
; /* Port portion of URI */
7529 cupsd_job_t
*job
; /* Job information */
7532 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7533 uri
->values
[0].string
.text
);
7536 * See if we have a job URI or a printer URI...
7539 if (!strcmp(uri
->name
, "printer-uri"))
7542 * Got a printer URI; see if we also have a job-id attribute...
7545 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7546 IPP_TAG_INTEGER
)) == NULL
)
7548 send_ipp_status(con
, IPP_BAD_REQUEST
,
7549 _("Got a printer-uri attribute but no job-id."));
7553 jobid
= attr
->values
[0].integer
;
7558 * Got a job URI; parse it to get the job ID...
7561 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7562 sizeof(scheme
), username
, sizeof(username
), host
,
7563 sizeof(host
), &port
, resource
, sizeof(resource
));
7565 if (strncmp(resource
, "/jobs/", 6))
7571 send_ipp_status(con
, IPP_BAD_REQUEST
,
7572 _("Bad job-uri \"%s\"."),
7573 uri
->values
[0].string
.text
);
7577 jobid
= atoi(resource
+ 6);
7581 * See if the job exists...
7584 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7587 * Nope - return a "not found" error...
7590 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7595 * See if the job is owned by the requesting user...
7598 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7600 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7601 cupsdFindDest(job
->dest
));
7606 * See if the job is in a state that allows holding...
7609 if (job
->state_value
> IPP_JOB_STOPPED
)
7612 * Return a "not-possible" error...
7615 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7616 _("Job #%d is finished and cannot be altered."),
7622 * Hold the job and return...
7625 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
7626 IPP_TAG_KEYWORD
)) == NULL
)
7627 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7631 when
= attr
->values
[0].string
.text
;
7633 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7634 "Job job-hold-until value changed by user.");
7637 when
= "indefinite";
7639 cupsdSetJobHoldUntil(job
, when
, 1);
7640 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
7643 con
->response
->request
.status
.status_code
= IPP_OK
;
7648 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7652 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
7653 ipp_attribute_t
*uri
) /* I - Printer URI */
7655 http_status_t status
; /* Policy status */
7656 cups_ptype_t dtype
; /* Destination type (printer/class) */
7657 cupsd_printer_t
*printer
; /* Printer data */
7660 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
7661 con
->number
, uri
->values
[0].string
.text
);
7664 * Is the destination valid?
7667 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7673 send_ipp_status(con
, IPP_NOT_FOUND
,
7674 _("The printer or class does not exist."));
7682 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7684 send_http_error(con
, status
, printer
);
7689 * Hold pending/new jobs sent to the printer...
7692 printer
->holding_new_jobs
= 1;
7694 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
7696 if (dtype
& CUPS_PRINTER_CLASS
)
7697 cupsdLogMessage(CUPSD_LOG_INFO
,
7698 "Class \"%s\" now holding pending/new jobs (\"%s\").",
7699 printer
->name
, get_username(con
));
7701 cupsdLogMessage(CUPSD_LOG_INFO
,
7702 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
7703 printer
->name
, get_username(con
));
7706 * Everything was ok, so return OK status...
7709 con
->response
->request
.status
.status_code
= IPP_OK
;
7714 * 'move_job()' - Move a job to a new destination.
7718 move_job(cupsd_client_t
*con
, /* I - Client connection */
7719 ipp_attribute_t
*uri
) /* I - Job URI */
7721 http_status_t status
; /* Policy status */
7722 ipp_attribute_t
*attr
; /* Current attribute */
7723 int jobid
; /* Job ID */
7724 cupsd_job_t
*job
; /* Current job */
7725 const char *src
; /* Source printer/class */
7726 cups_ptype_t stype
, /* Source type (printer or class) */
7727 dtype
; /* Destination type (printer/class) */
7728 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7729 username
[HTTP_MAX_URI
], /* Username portion of URI */
7730 host
[HTTP_MAX_URI
], /* Host portion of URI */
7731 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7732 int port
; /* Port portion of URI */
7733 cupsd_printer_t
*sprinter
, /* Source printer */
7734 *dprinter
; /* Destination printer */
7737 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
7738 uri
->values
[0].string
.text
);
7741 * Get the new printer or class...
7744 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7745 IPP_TAG_URI
)) == NULL
)
7748 * Need job-printer-uri...
7751 send_ipp_status(con
, IPP_BAD_REQUEST
,
7752 _("job-printer-uri attribute missing."));
7756 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7762 send_ipp_status(con
, IPP_NOT_FOUND
,
7763 _("The printer or class does not exist."));
7768 * See if we have a job URI or a printer URI...
7771 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7772 sizeof(scheme
), username
, sizeof(username
), host
,
7773 sizeof(host
), &port
, resource
, sizeof(resource
));
7775 if (!strcmp(uri
->name
, "printer-uri"))
7778 * Got a printer URI; see if we also have a job-id attribute...
7781 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7782 IPP_TAG_INTEGER
)) == NULL
)
7788 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7789 &sprinter
)) == NULL
)
7795 send_ipp_status(con
, IPP_NOT_FOUND
,
7796 _("The printer or class does not exist."));
7805 * Otherwise, just move a single job...
7808 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7811 * Nope - return a "not found" error...
7814 send_ipp_status(con
, IPP_NOT_FOUND
,
7815 _("Job #%d does not exist."), attr
->values
[0].integer
);
7821 * Job found, initialize source pointers...
7832 * Got a job URI; parse it to get the job ID...
7835 if (strncmp(resource
, "/jobs/", 6))
7841 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
7842 uri
->values
[0].string
.text
);
7847 * See if the job exists...
7850 jobid
= atoi(resource
+ 6);
7852 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7855 * Nope - return a "not found" error...
7858 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7864 * Job found, initialize source pointers...
7873 * Check the policy of the destination printer...
7876 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7877 job
? job
->username
: NULL
)) != HTTP_OK
)
7879 send_http_error(con
, status
, dprinter
);
7884 * Now move the job or jobs...
7890 * See if the job has been completed...
7893 if (job
->state_value
> IPP_JOB_STOPPED
)
7896 * Return a "not-possible" error...
7899 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7900 _("Job #%d is finished and cannot be altered."),
7906 * See if the job is owned by the requesting user...
7909 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7911 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7912 cupsdFindDest(job
->dest
));
7917 * Move the job to a different printer or class...
7920 cupsdMoveJob(job
, dprinter
);
7925 * Got the source printer, now look through the jobs...
7928 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7930 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7933 * See if the job is pointing at the source printer or has not been
7937 if (_cups_strcasecmp(job
->dest
, src
) ||
7938 job
->state_value
> IPP_JOB_STOPPED
)
7942 * See if the job can be moved by the requesting user...
7945 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7949 * Move the job to a different printer or class...
7952 cupsdMoveJob(job
, dprinter
);
7957 * Start jobs if possible...
7963 * Return with "everything is OK" status...
7966 con
->response
->request
.status
.status_code
= IPP_OK
;
7971 * 'ppd_parse_line()' - Parse a PPD default line.
7974 static int /* O - 0 on success, -1 on failure */
7975 ppd_parse_line(const char *line
, /* I - Line */
7976 char *option
, /* O - Option name */
7977 int olen
, /* I - Size of option name */
7978 char *choice
, /* O - Choice name */
7979 int clen
) /* I - Size of choice name */
7982 * Verify this is a default option line...
7985 if (strncmp(line
, "*Default", 8))
7989 * Read the option name...
7992 for (line
+= 8, olen
--;
7993 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8004 * Skip everything else up to the colon (:)...
8007 while (*line
&& *line
!= ':')
8016 * Now grab the option choice, skipping leading whitespace...
8019 while (isspace(*line
& 255))
8023 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
8034 * Return with no errors...
8042 * 'print_job()' - Print a file to a printer or class.
8046 print_job(cupsd_client_t
*con
, /* I - Client connection */
8047 ipp_attribute_t
*uri
) /* I - Printer URI */
8049 ipp_attribute_t
*attr
; /* Current attribute */
8050 ipp_attribute_t
*doc_name
; /* document-name attribute */
8051 ipp_attribute_t
*format
; /* Document-format attribute */
8052 const char *default_format
; /* document-format-default value */
8053 cupsd_job_t
*job
; /* New job */
8054 char filename
[1024]; /* Job filename */
8055 mime_type_t
*filetype
; /* Type of file */
8056 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8057 type
[MIME_MAX_TYPE
], /* Subtype of file */
8058 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8059 /* Textual name of mime type */
8060 cupsd_printer_t
*printer
; /* Printer data */
8061 struct stat fileinfo
; /* File information */
8062 int kbytes
; /* Size of file */
8063 int compression
; /* Document compression */
8066 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8067 uri
->values
[0].string
.text
);
8070 * Validate print file attributes, for now just document-format and
8071 * compression (CUPS only supports "none" and "gzip")...
8074 compression
= CUPS_FILE_NONE
;
8076 if ((attr
= ippFindAttribute(con
->request
, "compression",
8077 IPP_TAG_KEYWORD
)) != NULL
)
8079 if (strcmp(attr
->values
[0].string
.text
, "none")
8081 && strcmp(attr
->values
[0].string
.text
, "gzip")
8082 #endif /* HAVE_LIBZ */
8085 send_ipp_status(con
, IPP_ATTRIBUTES
,
8086 _("Unsupported compression \"%s\"."),
8087 attr
->values
[0].string
.text
);
8088 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8089 "compression", NULL
, attr
->values
[0].string
.text
);
8094 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8095 compression
= CUPS_FILE_GZIP
;
8096 #endif /* HAVE_LIBZ */
8100 * Do we have a file to print?
8105 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8110 * Is the destination valid?
8113 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8119 send_ipp_status(con
, IPP_NOT_FOUND
,
8120 _("The printer or class does not exist."));
8125 * Is it a format we support?
8128 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8130 ippSetName(con
->request
, &doc_name
, "document-name-supplied");
8132 if ((format
= ippFindAttribute(con
->request
, "document-format",
8133 IPP_TAG_MIMETYPE
)) != NULL
)
8136 * Grab format from client...
8139 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8142 send_ipp_status(con
, IPP_BAD_REQUEST
,
8143 _("Bad document-format \"%s\"."),
8144 format
->values
[0].string
.text
);
8148 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
8150 else if ((default_format
= cupsGetOption("document-format",
8151 printer
->num_options
,
8152 printer
->options
)) != NULL
)
8155 * Use default document format...
8158 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8160 send_ipp_status(con
, IPP_BAD_REQUEST
,
8161 _("Bad document-format \"%s\"."),
8172 strlcpy(super
, "application", sizeof(super
));
8173 strlcpy(type
, "octet-stream", sizeof(type
));
8176 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8179 * Auto-type the file...
8182 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8185 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8186 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8190 filetype
= mimeType(MimeDatabase
, super
, type
);
8192 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8193 filetype
->super
, filetype
->type
);
8195 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
8196 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
8199 filetype
= mimeType(MimeDatabase
, super
, type
);
8203 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8206 * Replace the document-format attribute value with the auto-typed or
8210 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8214 ippSetString(con
->request
, &format
, 0, mimetype
);
8216 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8217 "document-format", NULL
, mimetype
);
8221 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8222 _("Unsupported document-format \"%s\"."),
8223 format
? format
->values
[0].string
.text
:
8224 "application/octet-stream");
8225 cupsdLogMessage(CUPSD_LOG_INFO
,
8226 "Hint: Do you have the raw file printing rules enabled?");
8229 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8230 "document-format", NULL
, format
->values
[0].string
.text
);
8236 * Read any embedded job ticket info from PS files...
8239 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8240 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8241 !_cups_strcasecmp(filetype
->type
, "pdf")))
8242 read_job_ticket(con
);
8245 * Create the job object...
8248 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8252 * Update quota data...
8255 if (stat(con
->filename
, &fileinfo
))
8258 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8260 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8262 job
->koctets
+= kbytes
;
8264 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8265 attr
->values
[0].integer
+= kbytes
;
8268 * Add the job file...
8271 if (add_file(con
, job
, filetype
, compression
))
8274 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
8275 if (rename(con
->filename
, filename
))
8277 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
8279 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
8283 cupsdClearString(&con
->filename
);
8286 * See if we need to add the ending sheet...
8289 if (cupsdTimeoutJob(job
))
8293 * Log and save the job...
8296 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8297 "File of type %s/%s queued by \"%s\".",
8298 filetype
->super
, filetype
->type
, job
->username
);
8299 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8300 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8301 job
->dest
, job
->username
);
8304 * Start the job if possible...
8312 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8314 * This function only gets called when printing a single PDF or PostScript
8315 * file using the Print-Job operation. It doesn't work for Create-Job +
8316 * Send-File, since the job attributes need to be set at job creation
8317 * time for banners to work. The embedded job ticket stuff is here
8318 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8319 * options and IPP attributes which otherwise would be lost.
8321 * The format of a job ticket is simple:
8323 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8325 * %cupsJobTicket: attr1=value1
8326 * %cupsJobTicket: attr2=value2
8328 * %cupsJobTicket: attrN=valueN
8330 * Job ticket lines must appear immediately after the first line that
8331 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8332 * stops looking for job ticket info when it finds a line that does not begin
8333 * with "%cupsJobTicket:".
8335 * The maximum length of a job ticket line, including the prefix, is
8336 * 255 characters to conform with the Adobe DSC.
8338 * Read-only attributes are rejected with a notice to the error log in
8339 * case a malicious user tries anything. Since the job ticket is read
8340 * prior to attribute validation in print_job(), job ticket attributes
8341 * will go through the same validation as IPP attributes...
8345 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8347 cups_file_t
*fp
; /* File to read from */
8348 char line
[256]; /* Line data */
8349 int num_options
; /* Number of options */
8350 cups_option_t
*options
; /* Options */
8351 ipp_t
*ticket
; /* New attributes */
8352 ipp_attribute_t
*attr
, /* Current attribute */
8353 *attr2
, /* Job attribute */
8354 *prev2
; /* Previous job attribute */
8358 * First open the print file...
8361 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8363 cupsdLogMessage(CUPSD_LOG_ERROR
,
8364 "Unable to open print file for job ticket - %s",
8370 * Skip the first line...
8373 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8375 cupsdLogMessage(CUPSD_LOG_ERROR
,
8376 "Unable to read from print file for job ticket - %s",
8382 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8385 * Not a DSC-compliant file, so no job ticket info will be available...
8393 * Read job ticket info from the file...
8399 while (cupsFileGets(fp
, line
, sizeof(line
)))
8402 * Stop at the first non-ticket line...
8405 if (strncmp(line
, "%cupsJobTicket:", 15))
8409 * Add the options to the option array...
8412 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8416 * Done with the file; see if we have any options...
8421 if (num_options
== 0)
8425 * OK, convert the options to an attribute list, and apply them to
8430 cupsEncodeOptions(ticket
, num_options
, options
);
8433 * See what the user wants to change.
8436 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8438 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8441 if (!strncmp(attr
->name
, "date-time-at-", 13) ||
8442 !strcmp(attr
->name
, "job-impressions-completed") ||
8443 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8444 !strncmp(attr
->name
, "job-k-octets", 12) ||
8445 !strcmp(attr
->name
, "job-id") ||
8446 !strcmp(attr
->name
, "job-originating-host-name") ||
8447 !strcmp(attr
->name
, "job-originating-user-name") ||
8448 !strcmp(attr
->name
, "job-pages-completed") ||
8449 !strcmp(attr
->name
, "job-printer-uri") ||
8450 !strncmp(attr
->name
, "job-state", 9) ||
8451 !strcmp(attr
->name
, "job-uri") ||
8452 !strncmp(attr
->name
, "time-at-", 8))
8453 continue; /* Read-only attrs */
8455 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8456 IPP_TAG_ZERO
)) != NULL
)
8459 * Some other value; first free the old value...
8462 if (con
->request
->attrs
== attr2
)
8464 con
->request
->attrs
= attr2
->next
;
8469 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8470 if (prev2
->next
== attr2
)
8472 prev2
->next
= attr2
->next
;
8477 if (con
->request
->last
== attr2
)
8478 con
->request
->last
= prev2
;
8480 ippDeleteAttribute(NULL
, attr2
);
8484 * Add new option by copying it...
8487 ippCopyAttribute(con
->request
, attr
, 0);
8491 * Then free the attribute list and option array...
8495 cupsFreeOptions(num_options
, options
);
8500 * 'reject_jobs()' - Reject print jobs to a printer.
8504 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8505 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8507 http_status_t status
; /* Policy status */
8508 cups_ptype_t dtype
; /* Destination type (printer/class) */
8509 cupsd_printer_t
*printer
; /* Printer data */
8510 ipp_attribute_t
*attr
; /* printer-state-message text */
8513 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8514 con
->number
, uri
->values
[0].string
.text
);
8517 * Is the destination valid?
8520 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8526 send_ipp_status(con
, IPP_NOT_FOUND
,
8527 _("The printer or class does not exist."));
8535 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8537 send_http_error(con
, status
, printer
);
8542 * Reject jobs sent to the printer...
8545 printer
->accepting
= 0;
8547 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8548 IPP_TAG_TEXT
)) == NULL
)
8549 strlcpy(printer
->state_message
, "Rejecting Jobs",
8550 sizeof(printer
->state_message
));
8552 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8553 sizeof(printer
->state_message
));
8555 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8556 "No longer accepting jobs.");
8558 if (dtype
& CUPS_PRINTER_CLASS
)
8560 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8562 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8563 printer
->name
, get_username(con
));
8567 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8569 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8570 printer
->name
, get_username(con
));
8574 * Everything was ok, so return OK status...
8577 con
->response
->request
.status
.status_code
= IPP_OK
;
8582 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8586 release_held_new_jobs(
8587 cupsd_client_t
*con
, /* I - Connection */
8588 ipp_attribute_t
*uri
) /* I - Printer URI */
8590 http_status_t status
; /* Policy status */
8591 cups_ptype_t dtype
; /* Destination type (printer/class) */
8592 cupsd_printer_t
*printer
; /* Printer data */
8595 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8596 con
->number
, uri
->values
[0].string
.text
);
8599 * Is the destination valid?
8602 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8608 send_ipp_status(con
, IPP_NOT_FOUND
,
8609 _("The printer or class does not exist."));
8617 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8619 send_http_error(con
, status
, printer
);
8624 * Hold pending/new jobs sent to the printer...
8627 printer
->holding_new_jobs
= 0;
8629 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8631 if (dtype
& CUPS_PRINTER_CLASS
)
8632 cupsdLogMessage(CUPSD_LOG_INFO
,
8633 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8634 printer
->name
, get_username(con
));
8636 cupsdLogMessage(CUPSD_LOG_INFO
,
8637 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8638 printer
->name
, get_username(con
));
8641 * Everything was ok, so return OK status...
8644 con
->response
->request
.status
.status_code
= IPP_OK
;
8649 * 'release_job()' - Release a held print job.
8653 release_job(cupsd_client_t
*con
, /* I - Client connection */
8654 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8656 ipp_attribute_t
*attr
; /* Current attribute */
8657 int jobid
; /* Job ID */
8658 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8659 username
[HTTP_MAX_URI
], /* Username portion of URI */
8660 host
[HTTP_MAX_URI
], /* Host portion of URI */
8661 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8662 int port
; /* Port portion of URI */
8663 cupsd_job_t
*job
; /* Job information */
8666 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8667 con
->number
, uri
->values
[0].string
.text
);
8670 * See if we have a job URI or a printer URI...
8673 if (!strcmp(uri
->name
, "printer-uri"))
8676 * Got a printer URI; see if we also have a job-id attribute...
8679 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8680 IPP_TAG_INTEGER
)) == NULL
)
8682 send_ipp_status(con
, IPP_BAD_REQUEST
,
8683 _("Got a printer-uri attribute but no job-id."));
8687 jobid
= attr
->values
[0].integer
;
8692 * Got a job URI; parse it to get the job ID...
8695 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8696 sizeof(scheme
), username
, sizeof(username
), host
,
8697 sizeof(host
), &port
, resource
, sizeof(resource
));
8699 if (strncmp(resource
, "/jobs/", 6))
8705 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8706 uri
->values
[0].string
.text
);
8710 jobid
= atoi(resource
+ 6);
8714 * See if the job exists...
8717 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8720 * Nope - return a "not found" error...
8723 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8728 * See if job is "held"...
8731 if (job
->state_value
!= IPP_JOB_HELD
)
8734 * Nope - return a "not possible" error...
8737 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
8742 * See if the job is owned by the requesting user...
8745 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8747 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8748 cupsdFindDest(job
->dest
));
8753 * Reset the job-hold-until value to "no-hold"...
8756 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8757 IPP_TAG_KEYWORD
)) == NULL
)
8758 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8762 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
8763 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
8765 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8766 "Job job-hold-until value changed by user.");
8767 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
8771 * Release the job and return...
8774 cupsdReleaseJob(job
);
8776 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8777 "Job released by user.");
8779 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8781 con
->response
->request
.status
.status_code
= IPP_OK
;
8788 * 'renew_subscription()' - Renew an existing subscription...
8793 cupsd_client_t
*con
, /* I - Client connection */
8794 int sub_id
) /* I - Subscription ID */
8796 http_status_t status
; /* Policy status */
8797 cupsd_subscription_t
*sub
; /* Subscription */
8798 ipp_attribute_t
*lease
; /* notify-lease-duration */
8801 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8802 "renew_subscription(con=%p[%d], sub_id=%d)",
8803 con
, con
->number
, sub_id
);
8806 * Is the subscription ID valid?
8809 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8812 * Bad subscription ID...
8815 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
8823 * Job subscriptions cannot be renewed...
8826 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8827 _("Job subscriptions cannot be renewed."));
8835 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8837 con
, sub
->owner
)) != HTTP_OK
)
8839 send_http_error(con
, status
, sub
->dest
);
8844 * Renew the subscription...
8847 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8850 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8852 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8854 cupsdLogMessage(CUPSD_LOG_INFO
,
8855 "renew_subscription: Limiting notify-lease-duration to "
8858 sub
->lease
= MaxLeaseDuration
;
8861 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8863 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
8865 con
->response
->request
.status
.status_code
= IPP_OK
;
8867 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8868 "notify-lease-duration", sub
->lease
);
8873 * 'restart_job()' - Restart an old print job.
8877 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8878 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8880 ipp_attribute_t
*attr
; /* Current attribute */
8881 int jobid
; /* Job ID */
8882 cupsd_job_t
*job
; /* Job information */
8883 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8884 username
[HTTP_MAX_URI
], /* Username portion of URI */
8885 host
[HTTP_MAX_URI
], /* Host portion of URI */
8886 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8887 int port
; /* Port portion of URI */
8890 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8891 con
->number
, uri
->values
[0].string
.text
);
8894 * See if we have a job URI or a printer URI...
8897 if (!strcmp(uri
->name
, "printer-uri"))
8900 * Got a printer URI; see if we also have a job-id attribute...
8903 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8904 IPP_TAG_INTEGER
)) == NULL
)
8906 send_ipp_status(con
, IPP_BAD_REQUEST
,
8907 _("Got a printer-uri attribute but no job-id."));
8911 jobid
= attr
->values
[0].integer
;
8916 * Got a job URI; parse it to get the job ID...
8919 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8920 sizeof(scheme
), username
, sizeof(username
), host
,
8921 sizeof(host
), &port
, resource
, sizeof(resource
));
8923 if (strncmp(resource
, "/jobs/", 6))
8929 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8930 uri
->values
[0].string
.text
);
8934 jobid
= atoi(resource
+ 6);
8938 * See if the job exists...
8941 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8944 * Nope - return a "not found" error...
8947 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8952 * See if job is in any of the "completed" states...
8955 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8958 * Nope - return a "not possible" error...
8961 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
8967 * See if we have retained the job files...
8972 if (!job
->attrs
|| job
->num_files
== 0)
8975 * Nope - return a "not possible" error...
8978 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8979 _("Job #%d cannot be restarted - no files."), jobid
);
8984 * See if the job is owned by the requesting user...
8987 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8989 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8990 cupsdFindDest(job
->dest
));
8995 * See if the job-hold-until attribute is specified...
8998 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
8999 IPP_TAG_KEYWORD
)) == NULL
)
9000 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
9002 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
9005 * Return the job to a held state...
9008 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
9009 "Restarted by \"%s\" with job-hold-until=%s.",
9010 username
, attr
->values
[0].string
.text
);
9011 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
9013 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
9014 NULL
, job
, "Job restarted by user with job-hold-until=%s",
9015 attr
->values
[0].string
.text
);
9020 * Restart the job...
9023 cupsdRestartJob(job
);
9027 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9029 con
->response
->request
.status
.status_code
= IPP_OK
;
9034 * 'save_auth_info()' - Save authentication information for a job.
9039 cupsd_client_t
*con
, /* I - Client connection */
9040 cupsd_job_t
*job
, /* I - Job */
9041 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9043 int i
; /* Looping var */
9044 char filename
[1024]; /* Job authentication filename */
9045 cups_file_t
*fp
; /* Job authentication file */
9046 char line
[65536]; /* Line for file */
9047 cupsd_printer_t
*dest
; /* Destination printer/class */
9051 * This function saves the in-memory authentication information for
9052 * a job so that it can be used to authenticate with a remote host.
9053 * The information is stored in a file that is readable only by the
9054 * root user. The fields are Base-64 encoded, each on a separate line,
9055 * followed by random number (up to 1024) of newlines to limit the
9056 * amount of information that is exposed.
9058 * Because of the potential for exposing of authentication information,
9059 * this functionality is only enabled when running cupsd as root.
9061 * This caching only works for the Basic and BasicDigest authentication
9062 * types. Digest authentication cannot be cached this way, and in
9063 * the future Kerberos authentication may make all of this obsolete.
9065 * Authentication information is saved whenever an authenticated
9066 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9069 * This information is deleted after a job is completed or canceled,
9070 * so reprints may require subsequent re-authentication.
9076 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9080 * Create the authentication file and change permissions...
9083 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9084 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9086 cupsdLogMessage(CUPSD_LOG_ERROR
,
9087 "Unable to save authentication info to \"%s\" - %s",
9088 filename
, strerror(errno
));
9092 fchown(cupsFileNumber(fp
), 0, 0);
9093 fchmod(cupsFileNumber(fp
), 0400);
9095 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9098 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9100 cupsdClearString(job
->auth_env
+ i
);
9102 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9105 * Write 1 to 3 auth values...
9109 i
< auth_info
->num_values
&&
9110 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9113 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9115 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9116 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9119 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9120 auth_info
->values
[i
].string
.text
);
9122 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9123 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9124 auth_info
->values
[i
].string
.text
);
9125 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9126 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9127 auth_info
->values
[i
].string
.text
);
9128 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9129 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9130 auth_info
->values
[i
].string
.text
);
9131 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9132 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9133 auth_info
->values
[i
].string
.text
);
9138 else if (auth_info
&& auth_info
->num_values
== 2 &&
9139 dest
->num_auth_info_required
== 1 &&
9140 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9143 * Allow fallback to username+password for Kerberized queues...
9146 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9147 cupsFilePutConf(fp
, "username", line
);
9149 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9150 auth_info
->values
[0].string
.text
);
9152 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9153 cupsFilePutConf(fp
, "password", line
);
9155 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9156 auth_info
->values
[1].string
.text
);
9158 else if (con
->username
[0])
9161 * Write the authenticated username...
9164 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9165 cupsFilePutConf(fp
, "username", line
);
9167 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9170 * Write the authenticated password...
9173 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9174 cupsFilePutConf(fp
, "password", line
);
9176 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9180 if (con
->gss_uid
> 0)
9182 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9183 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9185 #endif /* HAVE_GSSAPI */
9188 * Write a random number of newlines to the end of the file...
9191 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9192 cupsFilePutChar(fp
, '\n');
9195 * Close the file and return...
9203 * 'send_document()' - Send a file to a printer or class.
9207 send_document(cupsd_client_t
*con
, /* I - Client connection */
9208 ipp_attribute_t
*uri
) /* I - Printer URI */
9210 ipp_attribute_t
*attr
; /* Current attribute */
9211 ipp_attribute_t
*format
; /* Request's document-format attribute */
9212 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9213 const char *default_format
;/* document-format-default value */
9214 int jobid
; /* Job ID number */
9215 cupsd_job_t
*job
; /* Current job */
9216 char job_uri
[HTTP_MAX_URI
],
9218 scheme
[HTTP_MAX_URI
],
9219 /* Method portion of URI */
9220 username
[HTTP_MAX_URI
],
9221 /* Username portion of URI */
9223 /* Host portion of URI */
9224 resource
[HTTP_MAX_URI
];
9225 /* Resource portion of URI */
9226 int port
; /* Port portion of URI */
9227 mime_type_t
*filetype
; /* Type of file */
9228 char super
[MIME_MAX_SUPER
],
9229 /* Supertype of file */
9230 type
[MIME_MAX_TYPE
],
9231 /* Subtype of file */
9232 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9233 /* Textual name of mime type */
9234 char filename
[1024]; /* Job filename */
9235 cupsd_printer_t
*printer
; /* Current printer */
9236 struct stat fileinfo
; /* File information */
9237 int kbytes
; /* Size of file */
9238 int compression
; /* Type of compression */
9239 int start_job
; /* Start the job? */
9242 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9243 con
->number
, uri
->values
[0].string
.text
);
9246 * See if we have a job URI or a printer URI...
9249 if (!strcmp(uri
->name
, "printer-uri"))
9252 * Got a printer URI; see if we also have a job-id attribute...
9255 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9256 IPP_TAG_INTEGER
)) == NULL
)
9258 send_ipp_status(con
, IPP_BAD_REQUEST
,
9259 _("Got a printer-uri attribute but no job-id."));
9263 jobid
= attr
->values
[0].integer
;
9268 * Got a job URI; parse it to get the job ID...
9271 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9272 sizeof(scheme
), username
, sizeof(username
), host
,
9273 sizeof(host
), &port
, resource
, sizeof(resource
));
9275 if (strncmp(resource
, "/jobs/", 6))
9281 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9282 uri
->values
[0].string
.text
);
9286 jobid
= atoi(resource
+ 6);
9290 * See if the job exists...
9293 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9296 * Nope - return a "not found" error...
9299 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9303 printer
= cupsdFindDest(job
->dest
);
9306 * See if the job is owned by the requesting user...
9309 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9311 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9312 cupsdFindDest(job
->dest
));
9317 * OK, see if the client is sending the document compressed - CUPS
9318 * only supports "none" and "gzip".
9321 compression
= CUPS_FILE_NONE
;
9323 if ((attr
= ippFindAttribute(con
->request
, "compression",
9324 IPP_TAG_KEYWORD
)) != NULL
)
9326 if (strcmp(attr
->values
[0].string
.text
, "none")
9328 && strcmp(attr
->values
[0].string
.text
, "gzip")
9329 #endif /* HAVE_LIBZ */
9332 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9333 attr
->values
[0].string
.text
);
9334 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9335 "compression", NULL
, attr
->values
[0].string
.text
);
9340 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9341 compression
= CUPS_FILE_GZIP
;
9342 #endif /* HAVE_LIBZ */
9346 * Do we have a file to print?
9349 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9350 IPP_TAG_BOOLEAN
)) == NULL
)
9352 send_ipp_status(con
, IPP_BAD_REQUEST
,
9353 _("Missing last-document attribute in request."));
9360 * Check for an empty request with "last-document" set to true, which is
9361 * used to close an "open" job by RFC 2911, section 3.3.2.
9364 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9367 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9372 * Is it a format we support?
9377 if ((format
= ippFindAttribute(con
->request
, "document-format",
9378 IPP_TAG_MIMETYPE
)) != NULL
)
9381 * Grab format from client...
9384 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9387 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9388 format
->values
[0].string
.text
);
9392 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, ippGetString(format
, 0, NULL
));
9394 else if ((default_format
= cupsGetOption("document-format",
9395 printer
->num_options
,
9396 printer
->options
)) != NULL
)
9399 * Use default document format...
9402 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9404 send_ipp_status(con
, IPP_BAD_REQUEST
,
9405 _("Bad document-format-default \"%s\"."), default_format
);
9412 * No document format attribute? Auto-type it!
9415 strlcpy(super
, "application", sizeof(super
));
9416 strlcpy(type
, "octet-stream", sizeof(type
));
9419 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9422 * Auto-type the file...
9425 ipp_attribute_t
*doc_name
; /* document-name attribute */
9428 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9430 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9431 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9432 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9436 filetype
= mimeType(MimeDatabase
, super
, type
);
9439 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9440 filetype
->super
, filetype
->type
);
9442 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
, filetype
->type
);
9443 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, mimetype
);
9446 filetype
= mimeType(MimeDatabase
, super
, type
);
9451 * Replace the document-format attribute value with the auto-typed or
9455 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9458 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9459 IPP_TAG_MIMETYPE
)) != NULL
)
9460 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9462 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9463 "document-format", NULL
, mimetype
);
9467 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9468 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9469 cupsdLogMessage(CUPSD_LOG_INFO
,
9470 "Hint: Do you have the raw file printing rules enabled?");
9473 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9474 "document-format", NULL
, format
->values
[0].string
.text
);
9479 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9481 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9484 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9485 _("Unsupported document-format \"%s\"."), mimetype
);
9487 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9488 "document-format", NULL
, mimetype
);
9494 * Add the file to the job...
9497 if (add_file(con
, job
, filetype
, compression
))
9500 if ((attr
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
)) != NULL
)
9501 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name-supplied", NULL
, ippGetString(attr
, 0, NULL
));
9503 if (stat(con
->filename
, &fileinfo
))
9506 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9508 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9510 job
->koctets
+= kbytes
;
9512 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9513 attr
->values
[0].integer
+= kbytes
;
9515 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
, job
->num_files
);
9516 if (rename(con
->filename
, filename
))
9518 cupsdLogJob(job
, CUPSD_LOG_ERROR
, "Unable to rename job document file \"%s\": %s", filename
, strerror(errno
));
9520 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to rename job document file."));
9524 cupsdClearString(&con
->filename
);
9526 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9527 filetype
->super
, filetype
->type
, job
->username
);
9530 * Start the job if this is the last document...
9535 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9536 IPP_TAG_BOOLEAN
)) != NULL
&&
9537 attr
->values
[0].boolean
)
9540 * See if we need to add the ending sheet...
9543 if (cupsdTimeoutJob(job
))
9546 if (job
->state_value
== IPP_JOB_STOPPED
)
9548 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9549 job
->state_value
= IPP_JOB_PENDING
;
9551 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9553 else if (job
->state_value
== IPP_JOB_HELD
)
9555 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9556 IPP_TAG_KEYWORD
)) == NULL
)
9557 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9559 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9561 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9562 job
->state_value
= IPP_JOB_PENDING
;
9564 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9567 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9571 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9577 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9578 IPP_TAG_KEYWORD
)) == NULL
)
9579 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9581 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9583 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9584 job
->state_value
= IPP_JOB_HELD
;
9585 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9587 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9590 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9597 * Fill in the response info...
9600 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9601 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9602 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9605 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9607 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9609 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
9610 NULL
, job
->reasons
->values
[0].string
.text
);
9612 con
->response
->request
.status
.status_code
= IPP_OK
;
9615 * Start the job if necessary...
9624 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9629 cupsd_client_t
*con
, /* I - Client connection */
9630 http_status_t status
, /* I - HTTP status code */
9631 cupsd_printer_t
*printer
) /* I - Printer, if any */
9633 ipp_attribute_t
*uri
; /* Request URI, if any */
9636 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9637 IPP_TAG_URI
)) == NULL
)
9638 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9640 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9641 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9642 con
->number
, httpStatus(status
),
9644 ippOpString(con
->request
->request
.op
.operation_id
) :
9646 uri
? uri
->values
[0].string
.text
: "no URI",
9647 con
->http
->hostname
);
9651 int auth_type
; /* Type of authentication required */
9654 auth_type
= CUPSD_AUTH_NONE
;
9656 if (status
== HTTP_UNAUTHORIZED
&&
9657 printer
->num_auth_info_required
> 0 &&
9658 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9660 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9661 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9662 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9665 * Creating and authenticating jobs requires Kerberos...
9668 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9673 * Use policy/location-defined authentication requirements...
9676 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9677 cupsd_location_t
*auth
; /* Pointer to authentication element */
9680 if (printer
->type
& CUPS_PRINTER_CLASS
)
9681 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9683 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9685 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9686 auth
->type
== CUPSD_AUTH_NONE
)
9687 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9689 con
->request
->request
.op
.operation_id
:
9694 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9695 auth_type
= cupsdDefaultAuthType();
9697 auth_type
= auth
->type
;
9701 cupsdSendError(con
, status
, auth_type
);
9704 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9706 ippDelete(con
->response
);
9707 con
->response
= NULL
;
9714 * 'send_ipp_status()' - Send a status back to the IPP client.
9718 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9719 ipp_status_t status
, /* I - IPP status code */
9720 const char *message
,/* I - Status message */
9721 ...) /* I - Additional args as needed */
9723 va_list ap
; /* Pointer to additional args */
9724 char formatted
[1024]; /* Formatted errror message */
9727 va_start(ap
, message
);
9728 vsnprintf(formatted
, sizeof(formatted
),
9729 _cupsLangString(con
->language
, message
), ap
);
9732 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9733 ippOpString(con
->request
->request
.op
.operation_id
),
9734 ippErrorString(status
), formatted
);
9736 con
->response
->request
.status
.status_code
= status
;
9738 if (ippFindAttribute(con
->response
, "attributes-charset",
9739 IPP_TAG_ZERO
) == NULL
)
9740 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9741 "attributes-charset", NULL
, "utf-8");
9743 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9744 IPP_TAG_ZERO
) == NULL
)
9745 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9746 "attributes-natural-language", NULL
, DefaultLanguage
);
9748 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9749 "status-message", NULL
, formatted
);
9754 * 'set_default()' - Set the default destination...
9758 set_default(cupsd_client_t
*con
, /* I - Client connection */
9759 ipp_attribute_t
*uri
) /* I - Printer URI */
9761 http_status_t status
; /* Policy status */
9762 cups_ptype_t dtype
; /* Destination type (printer/class) */
9763 cupsd_printer_t
*printer
, /* Printer */
9764 *oldprinter
; /* Old default printer */
9767 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9768 con
->number
, uri
->values
[0].string
.text
);
9771 * Is the destination valid?
9774 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9780 send_ipp_status(con
, IPP_NOT_FOUND
,
9781 _("The printer or class does not exist."));
9789 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9791 send_http_error(con
, status
, NULL
);
9796 * Set it as the default...
9799 oldprinter
= DefaultPrinter
;
9800 DefaultPrinter
= printer
;
9803 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9804 "%s is no longer the default printer.", oldprinter
->name
);
9806 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9807 "%s is now the default printer.", printer
->name
);
9809 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9810 CUPSD_DIRTY_PRINTCAP
);
9812 cupsdLogMessage(CUPSD_LOG_INFO
,
9813 "Default destination set to \"%s\" by \"%s\".",
9814 printer
->name
, get_username(con
));
9817 * Everything was ok, so return OK status...
9820 con
->response
->request
.status
.status_code
= IPP_OK
;
9825 * 'set_job_attrs()' - Set job attributes.
9829 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9830 ipp_attribute_t
*uri
) /* I - Job URI */
9832 ipp_attribute_t
*attr
, /* Current attribute */
9833 *attr2
; /* Job attribute */
9834 int jobid
; /* Job ID */
9835 cupsd_job_t
*job
; /* Current job */
9836 char scheme
[HTTP_MAX_URI
],
9837 /* Method portion of URI */
9838 username
[HTTP_MAX_URI
],
9839 /* Username portion of URI */
9841 /* Host portion of URI */
9842 resource
[HTTP_MAX_URI
];
9843 /* Resource portion of URI */
9844 int port
; /* Port portion of URI */
9845 int event
; /* Events? */
9846 int check_jobs
; /* Check jobs? */
9849 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9850 con
->number
, uri
->values
[0].string
.text
);
9853 * Start with "everything is OK" status...
9856 con
->response
->request
.status
.status_code
= IPP_OK
;
9859 * See if we have a job URI or a printer URI...
9862 if (!strcmp(uri
->name
, "printer-uri"))
9865 * Got a printer URI; see if we also have a job-id attribute...
9868 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9869 IPP_TAG_INTEGER
)) == NULL
)
9871 send_ipp_status(con
, IPP_BAD_REQUEST
,
9872 _("Got a printer-uri attribute but no job-id."));
9876 jobid
= attr
->values
[0].integer
;
9881 * Got a job URI; parse it to get the job ID...
9884 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9885 sizeof(scheme
), username
, sizeof(username
), host
,
9886 sizeof(host
), &port
, resource
, sizeof(resource
));
9888 if (strncmp(resource
, "/jobs/", 6))
9894 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9895 uri
->values
[0].string
.text
);
9899 jobid
= atoi(resource
+ 6);
9903 * See if the job exists...
9906 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9909 * Nope - return a "not found" error...
9912 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9917 * See if the job has been completed...
9920 if (job
->state_value
> IPP_JOB_STOPPED
)
9923 * Return a "not-possible" error...
9926 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9927 _("Job #%d is finished and cannot be altered."), jobid
);
9932 * See if the job is owned by the requesting user...
9935 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9937 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9938 cupsdFindDest(job
->dest
));
9943 * See what the user wants to change.
9951 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9953 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9956 if (!strcmp(attr
->name
, "attributes-charset") ||
9957 !strcmp(attr
->name
, "attributes-natural-language") ||
9958 !strncmp(attr
->name
, "date-time-at-", 13) ||
9959 !strncmp(attr
->name
, "document-compression", 20) ||
9960 !strncmp(attr
->name
, "document-format", 15) ||
9961 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9962 !strcmp(attr
->name
, "job-document-access-errors") ||
9963 !strcmp(attr
->name
, "job-id") ||
9964 !strcmp(attr
->name
, "job-impressions-completed") ||
9965 !strcmp(attr
->name
, "job-k-octets-completed") ||
9966 !strcmp(attr
->name
, "job-media-sheets-completed") ||
9967 !strcmp(attr
->name
, "job-originating-host-name") ||
9968 !strcmp(attr
->name
, "job-originating-user-name") ||
9969 !strcmp(attr
->name
, "job-pages-completed") ||
9970 !strcmp(attr
->name
, "job-printer-up-time") ||
9971 !strcmp(attr
->name
, "job-printer-uri") ||
9972 !strcmp(attr
->name
, "job-sheets") ||
9973 !strcmp(attr
->name
, "job-state-message") ||
9974 !strcmp(attr
->name
, "job-state-reasons") ||
9975 !strcmp(attr
->name
, "job-uri") ||
9976 !strcmp(attr
->name
, "number-of-documents") ||
9977 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9978 !strcmp(attr
->name
, "output-device-assigned") ||
9979 !strncmp(attr
->name
, "time-at-", 8))
9985 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9986 _("%s cannot be changed."), attr
->name
);
9988 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9989 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9993 if (!strcmp(attr
->name
, "job-priority"))
9996 * Change the job priority...
9999 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10001 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
10003 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10004 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10006 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10008 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10009 _("Job is completed and cannot be changed."));
10012 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10014 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10015 attr
->values
[0].integer
);
10016 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10019 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10020 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10023 else if (!strcmp(attr
->name
, "job-state"))
10026 * Change the job state...
10029 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10031 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
10033 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
10034 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
10038 switch (attr
->values
[0].integer
)
10040 case IPP_JOB_PENDING
:
10041 case IPP_JOB_HELD
:
10042 if (job
->state_value
> IPP_JOB_HELD
)
10044 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10045 _("Job state cannot be changed."));
10048 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10050 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10051 attr
->values
[0].integer
);
10052 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
10057 case IPP_JOB_PROCESSING
:
10058 case IPP_JOB_STOPPED
:
10059 if (job
->state_value
!= attr
->values
[0].integer
)
10061 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10062 _("Job state cannot be changed."));
10067 case IPP_JOB_CANCELED
:
10068 case IPP_JOB_ABORTED
:
10069 case IPP_JOB_COMPLETED
:
10070 if (job
->state_value
> IPP_JOB_PROCESSING
)
10072 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10073 _("Job state cannot be changed."));
10076 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10078 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10079 attr
->values
[0].integer
);
10080 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10082 "Job state changed by \"%s\"", username
);
10089 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10091 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10092 IPP_TAG_ZERO
)) != NULL
)
10095 * Some other value; first free the old value...
10098 if (job
->attrs
->prev
)
10099 job
->attrs
->prev
->next
= attr2
->next
;
10101 job
->attrs
->attrs
= attr2
->next
;
10103 if (job
->attrs
->last
== attr2
)
10104 job
->attrs
->last
= job
->attrs
->prev
;
10106 ippDeleteAttribute(NULL
, attr2
);
10109 * Then copy the attribute...
10112 ippCopyAttribute(job
->attrs
, attr
, 0);
10115 * See if the job-name or job-hold-until is being changed.
10118 if (!strcmp(attr
->name
, "job-hold-until"))
10120 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10121 attr
->values
[0].string
.text
);
10122 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10124 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10126 cupsdReleaseJob(job
);
10130 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10131 "Job held by \"%s\".", username
);
10133 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10136 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10139 * Delete the attribute...
10142 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10143 IPP_TAG_ZERO
)) != NULL
)
10145 if (job
->attrs
->prev
)
10146 job
->attrs
->prev
->next
= attr2
->next
;
10148 job
->attrs
->attrs
= attr2
->next
;
10150 if (attr2
== job
->attrs
->last
)
10151 job
->attrs
->last
= job
->attrs
->prev
;
10153 ippDeleteAttribute(NULL
, attr2
);
10155 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10161 * Add new option by copying it...
10164 ippCopyAttribute(job
->attrs
, attr
, 0);
10166 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10175 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10178 * Send events as needed...
10181 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10182 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10183 cupsdFindDest(job
->dest
), job
,
10184 "Job priority changed by user.");
10186 if (event
& CUPSD_EVENT_JOB_STATE
)
10187 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10188 job
->state_value
== IPP_JOB_HELD
?
10189 "Job held by user." : "Job restarted by user.");
10191 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10192 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10193 "Job options changed by user.");
10196 * Start jobs if possible...
10205 * 'set_printer_attrs()' - Set printer attributes.
10209 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10210 ipp_attribute_t
*uri
) /* I - Printer */
10212 http_status_t status
; /* Policy status */
10213 cups_ptype_t dtype
; /* Destination type (printer/class) */
10214 cupsd_printer_t
*printer
; /* Printer/class */
10215 ipp_attribute_t
*attr
; /* Printer attribute */
10216 int changed
= 0; /* Was anything changed? */
10219 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10220 con
->number
, uri
->values
[0].string
.text
);
10223 * Is the destination valid?
10226 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10232 send_ipp_status(con
, IPP_NOT_FOUND
,
10233 _("The printer or class does not exist."));
10241 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10243 send_http_error(con
, status
, printer
);
10248 * Return a list of attributes that can be set via Set-Printer-Attributes.
10251 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10252 IPP_TAG_TEXT
)) != NULL
)
10254 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10258 if ((attr
= ippFindAttribute(con
->request
, "printer-geo-location", IPP_TAG_URI
)) != NULL
&& !strncmp(attr
->values
[0].string
.text
, "geo:", 4))
10260 cupsdSetString(&printer
->geo_location
, attr
->values
[0].string
.text
);
10264 if ((attr
= ippFindAttribute(con
->request
, "printer-organization", IPP_TAG_TEXT
)) != NULL
)
10266 cupsdSetString(&printer
->organization
, attr
->values
[0].string
.text
);
10270 if ((attr
= ippFindAttribute(con
->request
, "printer-organizational-unit", IPP_TAG_TEXT
)) != NULL
)
10272 cupsdSetString(&printer
->organizational_unit
, attr
->values
[0].string
.text
);
10276 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10277 IPP_TAG_TEXT
)) != NULL
)
10279 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10284 * Update the printer attributes and return...
10289 printer
->config_time
= time(NULL
);
10291 cupsdSetPrinterAttrs(printer
);
10292 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10294 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10295 "Printer \"%s\" description or location changed by \"%s\".",
10296 printer
->name
, get_username(con
));
10298 cupsdLogMessage(CUPSD_LOG_INFO
,
10299 "Printer \"%s\" description or location changed by \"%s\".",
10300 printer
->name
, get_username(con
));
10303 con
->response
->request
.status
.status_code
= IPP_OK
;
10308 * 'set_printer_defaults()' - Set printer default options from a request.
10312 set_printer_defaults(
10313 cupsd_client_t
*con
, /* I - Client connection */
10314 cupsd_printer_t
*printer
) /* I - Printer */
10316 int i
; /* Looping var */
10317 ipp_attribute_t
*attr
; /* Current attribute */
10318 size_t namelen
; /* Length of attribute name */
10319 char name
[256], /* New attribute name */
10320 value
[256]; /* String version of integer attrs */
10323 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10326 * Skip non-printer attributes...
10329 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10332 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10334 if (!strcmp(attr
->name
, "job-sheets-default"))
10337 * Only allow keywords and names...
10340 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10344 * Only allow job-sheets-default to be set when running without a
10345 * system high classification level...
10348 if (Classification
)
10351 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10353 if (attr
->num_values
> 1)
10354 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10356 cupsdSetString(&printer
->job_sheets
[1], "none");
10358 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10360 cupsdFreeStrings(&(printer
->users
));
10362 printer
->deny_users
= 0;
10364 if (attr
->value_tag
== IPP_TAG_NAME
&&
10365 (attr
->num_values
> 1 ||
10366 strcmp(attr
->values
[0].string
.text
, "all")))
10368 for (i
= 0; i
< attr
->num_values
; i
++)
10369 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10372 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10374 cupsdFreeStrings(&(printer
->users
));
10376 printer
->deny_users
= 1;
10378 if (attr
->value_tag
== IPP_TAG_NAME
&&
10379 (attr
->num_values
> 1 ||
10380 strcmp(attr
->values
[0].string
.text
, "none")))
10382 for (i
= 0; i
< attr
->num_values
; i
++)
10383 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10386 else if (!strcmp(attr
->name
, "job-quota-period"))
10388 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10391 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10392 attr
->values
[0].integer
);
10393 cupsdFreeQuotas(printer
);
10395 printer
->quota_period
= attr
->values
[0].integer
;
10397 else if (!strcmp(attr
->name
, "job-k-limit"))
10399 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10402 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10403 attr
->values
[0].integer
);
10404 cupsdFreeQuotas(printer
);
10406 printer
->k_limit
= attr
->values
[0].integer
;
10408 else if (!strcmp(attr
->name
, "job-page-limit"))
10410 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10413 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10414 attr
->values
[0].integer
);
10415 cupsdFreeQuotas(printer
);
10417 printer
->page_limit
= attr
->values
[0].integer
;
10419 else if (!strcmp(attr
->name
, "printer-op-policy"))
10421 cupsd_policy_t
*p
; /* Policy */
10424 if (attr
->value_tag
!= IPP_TAG_NAME
)
10427 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10429 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10430 "Setting printer-op-policy to \"%s\"...",
10431 attr
->values
[0].string
.text
);
10432 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10433 printer
->op_policy_ptr
= p
;
10437 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10438 _("Unknown printer-op-policy \"%s\"."),
10439 attr
->values
[0].string
.text
);
10443 else if (!strcmp(attr
->name
, "printer-error-policy"))
10445 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10448 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10449 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10450 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10451 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10452 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10454 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10455 _("Unknown printer-error-policy \"%s\"."),
10456 attr
->values
[0].string
.text
);
10460 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10461 "Setting printer-error-policy to \"%s\"...",
10462 attr
->values
[0].string
.text
);
10463 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10467 * Skip any other non-default attributes...
10470 namelen
= strlen(attr
->name
);
10471 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10472 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10476 * OK, anything else must be a user-defined default...
10479 strlcpy(name
, attr
->name
, sizeof(name
));
10480 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10482 switch (attr
->value_tag
)
10484 case IPP_TAG_DELETEATTR
:
10485 printer
->num_options
= cupsRemoveOption(name
,
10486 printer
->num_options
,
10487 &(printer
->options
));
10488 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10489 "Deleting %s", attr
->name
);
10492 case IPP_TAG_NAME
:
10493 case IPP_TAG_TEXT
:
10494 case IPP_TAG_KEYWORD
:
10496 printer
->num_options
= cupsAddOption(name
,
10497 attr
->values
[0].string
.text
,
10498 printer
->num_options
,
10499 &(printer
->options
));
10500 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10501 "Setting %s to \"%s\"...", attr
->name
,
10502 attr
->values
[0].string
.text
);
10505 case IPP_TAG_BOOLEAN
:
10506 printer
->num_options
= cupsAddOption(name
,
10507 attr
->values
[0].boolean
?
10509 printer
->num_options
,
10510 &(printer
->options
));
10511 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10512 "Setting %s to %s...", attr
->name
,
10513 attr
->values
[0].boolean
? "true" : "false");
10516 case IPP_TAG_INTEGER
:
10517 case IPP_TAG_ENUM
:
10518 sprintf(value
, "%d", attr
->values
[0].integer
);
10519 printer
->num_options
= cupsAddOption(name
, value
,
10520 printer
->num_options
,
10521 &(printer
->options
));
10522 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10523 "Setting %s to %s...", attr
->name
, value
);
10526 case IPP_TAG_RANGE
:
10527 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10528 attr
->values
[0].range
.upper
);
10529 printer
->num_options
= cupsAddOption(name
, value
,
10530 printer
->num_options
,
10531 &(printer
->options
));
10532 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10533 "Setting %s to %s...", attr
->name
, value
);
10536 case IPP_TAG_RESOLUTION
:
10537 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10538 attr
->values
[0].resolution
.yres
,
10539 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10541 printer
->num_options
= cupsAddOption(name
, value
,
10542 printer
->num_options
,
10543 &(printer
->options
));
10544 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10545 "Setting %s to %s...", attr
->name
, value
);
10549 /* Do nothing for other values */
10557 * 'start_printer()' - Start a printer.
10561 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10562 ipp_attribute_t
*uri
) /* I - Printer URI */
10564 int i
; /* Temporary variable */
10565 http_status_t status
; /* Policy status */
10566 cups_ptype_t dtype
; /* Destination type (printer/class) */
10567 cupsd_printer_t
*printer
; /* Printer data */
10570 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10571 con
->number
, uri
->values
[0].string
.text
);
10574 * Is the destination valid?
10577 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10583 send_ipp_status(con
, IPP_NOT_FOUND
,
10584 _("The printer or class does not exist."));
10592 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10594 send_http_error(con
, status
, printer
);
10599 * Start the printer...
10602 printer
->state_message
[0] = '\0';
10604 cupsdStartPrinter(printer
, 1);
10606 if (dtype
& CUPS_PRINTER_CLASS
)
10607 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10608 printer
->name
, get_username(con
));
10610 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10611 printer
->name
, get_username(con
));
10619 if ((i
= check_quotas(con
, printer
)) < 0)
10621 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
10626 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
10631 * Everything was ok, so return OK status...
10634 con
->response
->request
.status
.status_code
= IPP_OK
;
10639 * 'stop_printer()' - Stop a printer.
10643 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10644 ipp_attribute_t
*uri
) /* I - Printer URI */
10646 http_status_t status
; /* Policy status */
10647 cups_ptype_t dtype
; /* Destination type (printer/class) */
10648 cupsd_printer_t
*printer
; /* Printer data */
10649 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10652 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10653 con
->number
, uri
->values
[0].string
.text
);
10656 * Is the destination valid?
10659 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10665 send_ipp_status(con
, IPP_NOT_FOUND
,
10666 _("The printer or class does not exist."));
10674 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10676 send_http_error(con
, status
, printer
);
10681 * Stop the printer...
10684 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10685 IPP_TAG_TEXT
)) == NULL
)
10686 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
10689 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10690 sizeof(printer
->state_message
));
10693 cupsdStopPrinter(printer
, 1);
10695 if (dtype
& CUPS_PRINTER_CLASS
)
10696 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10697 printer
->name
, get_username(con
));
10699 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10700 printer
->name
, get_username(con
));
10703 * Everything was ok, so return OK status...
10706 con
->response
->request
.status
.status_code
= IPP_OK
;
10711 * 'url_encode_attr()' - URL-encode a string attribute.
10715 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10716 char *buffer
,/* I - String buffer */
10717 size_t bufsize
)/* I - Size of buffer */
10719 int i
; /* Looping var */
10720 char *bufptr
, /* Pointer into buffer */
10721 *bufend
; /* End of buffer */
10724 strlcpy(buffer
, attr
->name
, bufsize
);
10725 bufptr
= buffer
+ strlen(buffer
);
10726 bufend
= buffer
+ bufsize
- 1;
10728 for (i
= 0; i
< attr
->num_values
; i
++)
10730 if (bufptr
>= bufend
)
10738 if (bufptr
>= bufend
)
10743 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
10745 if (bufptr
>= bufend
)
10756 * 'url_encode_string()' - URL-encode a string.
10759 static char * /* O - End of string */
10760 url_encode_string(const char *s
, /* I - String */
10761 char *buffer
, /* I - String buffer */
10762 size_t bufsize
) /* I - Size of buffer */
10764 char *bufptr
, /* Pointer into buffer */
10765 *bufend
; /* End of buffer */
10766 static const char *hex
= "0123456789ABCDEF";
10771 bufend
= buffer
+ bufsize
- 1;
10773 while (*s
&& bufptr
< bufend
)
10775 if (*s
== ' ' || *s
== '%' || *s
== '+')
10777 if (bufptr
>= (bufend
- 2))
10781 *bufptr
++ = hex
[(*s
>> 4) & 15];
10782 *bufptr
++ = hex
[*s
& 15];
10786 else if (*s
== '\'' || *s
== '\\')
10788 if (bufptr
>= (bufend
- 1))
10805 * 'user_allowed()' - See if a user is allowed to print to a queue.
10808 static int /* O - 0 if not allowed, 1 if allowed */
10809 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10810 const char *username
) /* I - Username */
10812 struct passwd
*pw
; /* User password data */
10813 char baseuser
[256], /* Base username */
10814 *baseptr
, /* Pointer to "@" in base username */
10815 *name
; /* Current user name */
10818 if (cupsArrayCount(p
->users
) == 0)
10821 if (!strcmp(username
, "root"))
10824 if (strchr(username
, '@'))
10827 * Strip @REALM for username check...
10830 strlcpy(baseuser
, username
, sizeof(baseuser
));
10832 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10835 username
= baseuser
;
10838 pw
= getpwnam(username
);
10841 for (name
= (char *)cupsArrayFirst(p
->users
);
10843 name
= (char *)cupsArrayNext(p
->users
))
10845 if (name
[0] == '@')
10848 * Check group membership...
10851 if (cupsdCheckGroup(username
, pw
, name
+ 1))
10854 else if (name
[0] == '#')
10860 if (cupsdCheckGroup(username
, pw
, name
))
10863 else if (!_cups_strcasecmp(username
, name
))
10867 return ((name
!= NULL
) != p
->deny_users
);
10872 * 'validate_job()' - Validate printer options and destination.
10876 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10877 ipp_attribute_t
*uri
) /* I - Printer URI */
10879 http_status_t status
; /* Policy status */
10880 ipp_attribute_t
*attr
; /* Current attribute */
10882 ipp_attribute_t
*auth_info
; /* auth-info attribute */
10883 #endif /* HAVE_SSL */
10884 ipp_attribute_t
*format
, /* Document-format attribute */
10885 *name
; /* Job-name attribute */
10886 cups_ptype_t dtype
; /* Destination type (printer/class) */
10887 char super
[MIME_MAX_SUPER
],
10888 /* Supertype of file */
10889 type
[MIME_MAX_TYPE
];
10890 /* Subtype of file */
10891 cupsd_printer_t
*printer
; /* Printer */
10894 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10895 con
->number
, uri
->values
[0].string
.text
);
10898 * OK, see if the client is sending the document compressed - CUPS
10899 * doesn't support compression yet...
10902 if ((attr
= ippFindAttribute(con
->request
, "compression",
10903 IPP_TAG_KEYWORD
)) != NULL
)
10905 if (strcmp(attr
->values
[0].string
.text
, "none")
10907 && strcmp(attr
->values
[0].string
.text
, "gzip")
10908 #endif /* HAVE_LIBZ */
10911 send_ipp_status(con
, IPP_ATTRIBUTES
,
10912 _("Unsupported 'compression' value \"%s\"."),
10913 attr
->values
[0].string
.text
);
10914 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10915 "compression", NULL
, attr
->values
[0].string
.text
);
10921 * Is it a format we support?
10924 if ((format
= ippFindAttribute(con
->request
, "document-format",
10925 IPP_TAG_MIMETYPE
)) != NULL
)
10927 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
10930 send_ipp_status(con
, IPP_BAD_REQUEST
,
10931 _("Bad 'document-format' value \"%s\"."),
10932 format
->values
[0].string
.text
);
10936 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10937 !mimeType(MimeDatabase
, super
, type
))
10939 cupsdLogMessage(CUPSD_LOG_INFO
,
10940 "Hint: Do you have the raw file printing rules enabled?");
10941 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10942 _("Unsupported 'document-format' value \"%s\"."),
10943 format
->values
[0].string
.text
);
10944 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10945 "document-format", NULL
, format
->values
[0].string
.text
);
10951 * Is the job-name valid?
10954 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
10956 int bad_name
= 0; /* Is the job-name value bad? */
10958 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
10959 name
->num_values
!= 1)
10966 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
10967 * IPP Everywhere requirements for "name" values...
10970 const unsigned char *nameptr
; /* Pointer into "job-name" attribute */
10972 for (nameptr
= (unsigned char *)name
->values
[0].string
.text
;
10976 if (*nameptr
< ' ' && *nameptr
!= '\t')
10978 else if (*nameptr
== 0x7f)
10980 else if ((*nameptr
& 0xe0) == 0xc0)
10982 if ((nameptr
[1] & 0xc0) != 0x80)
10987 else if ((*nameptr
& 0xf0) == 0xe0)
10989 if ((nameptr
[1] & 0xc0) != 0x80 ||
10990 (nameptr
[2] & 0xc0) != 0x80)
10995 else if ((*nameptr
& 0xf8) == 0xf0)
10997 if ((nameptr
[1] & 0xc0) != 0x80 ||
10998 (nameptr
[2] & 0xc0) != 0x80 ||
10999 (nameptr
[3] & 0xc0) != 0x80)
11004 else if (*nameptr
& 0x80)
11014 if (StrictConformance
)
11016 send_ipp_status(con
, IPP_ATTRIBUTES
,
11017 _("Unsupported 'job-name' value."));
11018 ippCopyAttribute(con
->response
, name
, 0);
11023 cupsdLogMessage(CUPSD_LOG_WARN
,
11024 "Unsupported 'job-name' value, deleting from request.");
11025 ippDeleteAttribute(con
->request
, name
);
11031 * Is the destination valid?
11034 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11040 send_ipp_status(con
, IPP_NOT_FOUND
,
11041 _("The printer or class does not exist."));
11050 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
11051 #endif /* HAVE_SSL */
11053 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11055 send_http_error(con
, status
, printer
);
11058 else if (printer
->num_auth_info_required
== 1 &&
11059 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
11062 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
11066 else if (auth_info
&& !con
->http
->tls
&&
11067 !httpAddrLocalhost(con
->http
->hostaddr
))
11070 * Require encryption of auth-info over non-local connections...
11073 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
11076 #endif /* HAVE_SSL */
11079 * Everything was ok, so return OK status...
11082 con
->response
->request
.status
.status_code
= IPP_OK
;
11087 * 'validate_name()' - Make sure the printer name only contains valid chars.
11090 static int /* O - 0 if name is no good, 1 if good */
11091 validate_name(const char *name
) /* I - Name to check */
11093 const char *ptr
; /* Pointer into name */
11097 * Scan the whole name...
11100 for (ptr
= name
; *ptr
; ptr
++)
11101 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11105 * All the characters are good; validate the length, too...
11108 return ((ptr
- name
) < 128);
11113 * 'validate_user()' - Validate the user for the request.
11116 static int /* O - 1 if permitted, 0 otherwise */
11117 validate_user(cupsd_job_t
*job
, /* I - Job */
11118 cupsd_client_t
*con
, /* I - Client connection */
11119 const char *owner
, /* I - Owner of job/resource */
11120 char *username
, /* O - Authenticated username */
11121 size_t userlen
) /* I - Length of username */
11123 cupsd_printer_t
*printer
; /* Printer for job */
11126 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
);
11129 * Validate input...
11132 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11136 * Get the best authenticated username that is available.
11139 strlcpy(username
, get_username(con
), userlen
);
11142 * Check the username against the owner...
11145 printer
= cupsdFindDest(job
->dest
);
11147 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11148 con
, owner
) == HTTP_OK
);