2 * "$Id: ipp.c 12701 2015-06-08 18:33:44Z msweet $"
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",
949 IPP_TAG_TEXT
)) != NULL
)
950 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
952 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
953 IPP_TAG_TEXT
)) != NULL
)
954 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
956 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
957 IPP_TAG_BOOLEAN
)) != NULL
&&
958 attr
->values
[0].boolean
!= pclass
->accepting
)
960 cupsdLogMessage(CUPSD_LOG_INFO
,
961 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
962 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
964 pclass
->accepting
= attr
->values
[0].boolean
;
966 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s accepting jobs.",
967 pclass
->accepting
? "Now" : "No longer");
970 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
971 IPP_TAG_BOOLEAN
)) != NULL
)
973 if (pclass
->shared
&& !attr
->values
[0].boolean
)
974 cupsdDeregisterPrinter(pclass
, 1);
976 cupsdLogMessage(CUPSD_LOG_INFO
,
977 "Setting %s printer-is-shared to %d (was %d.)",
978 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
980 pclass
->shared
= attr
->values
[0].boolean
;
983 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
984 IPP_TAG_ENUM
)) != NULL
)
986 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
987 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
989 send_ipp_status(con
, IPP_BAD_REQUEST
,
990 _("Attempt to set %s printer-state to bad value %d."),
991 pclass
->name
, attr
->values
[0].integer
);
995 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
996 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
998 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
999 cupsdStopPrinter(pclass
, 0);
1002 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1003 need_restart_job
= 1;
1006 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1007 IPP_TAG_TEXT
)) != NULL
)
1009 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1010 sizeof(pclass
->state_message
));
1012 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, pclass
, NULL
, "%s",
1013 pclass
->state_message
);
1015 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1016 IPP_TAG_URI
)) != NULL
)
1019 * Clear the printer array as needed...
1022 need_restart_job
= 1;
1024 if (pclass
->num_printers
> 0)
1026 free(pclass
->printers
);
1027 pclass
->num_printers
= 0;
1031 * Add each printer or class that is listed...
1034 for (i
= 0; i
< attr
->num_values
; i
++)
1037 * Search for the printer or class URI...
1040 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1046 send_ipp_status(con
, IPP_NOT_FOUND
,
1047 _("The printer or class does not exist."));
1050 else if (dtype
& CUPS_PRINTER_CLASS
)
1052 send_ipp_status(con
, IPP_BAD_REQUEST
,
1053 _("Nested classes are not allowed."));
1058 * Add it to the class...
1061 cupsdAddPrinterToClass(pclass
, member
);
1065 set_printer_defaults(con
, pclass
);
1067 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1068 IPP_TAG_KEYWORD
)) != NULL
)
1069 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1072 * Update the printer class attributes and return...
1075 cupsdSetPrinterAttrs(pclass
);
1076 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1078 if (need_restart_job
&& pclass
->job
)
1081 * Reset the current job to a "pending" status...
1084 cupsdSetJobState(pclass
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
1085 "Job restarted because the class was modified.");
1088 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1092 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
1093 pclass
, NULL
, "Class \"%s\" modified by \"%s\".",
1094 pclass
->name
, get_username(con
));
1096 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1097 pclass
->name
, get_username(con
));
1101 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
1102 pclass
, NULL
, "New class \"%s\" added by \"%s\".",
1103 pclass
->name
, get_username(con
));
1105 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1106 pclass
->name
, get_username(con
));
1109 con
->response
->request
.status
.status_code
= IPP_OK
;
1114 * 'add_file()' - Add a file to a job.
1117 static int /* O - 0 on success, -1 on error */
1118 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1119 cupsd_job_t
*job
, /* I - Job to add to */
1120 mime_type_t
*filetype
, /* I - Type of file */
1121 int compression
) /* I - Compression */
1123 mime_type_t
**filetypes
; /* New filetypes array... */
1124 int *compressions
; /* New compressions array... */
1127 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1128 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1129 "compression=%d)", con
, con
? con
->number
: -1, job
->id
,
1130 filetype
->super
, filetype
->type
, compression
);
1133 * Add the file to the job...
1136 if (job
->num_files
== 0)
1138 compressions
= (int *)malloc(sizeof(int));
1139 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1143 compressions
= (int *)realloc(job
->compressions
,
1144 (size_t)(job
->num_files
+ 1) * sizeof(int));
1145 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1146 (size_t)(job
->num_files
+ 1) *
1147 sizeof(mime_type_t
*));
1151 job
->compressions
= compressions
;
1154 job
->filetypes
= filetypes
;
1156 if (!compressions
|| !filetypes
)
1158 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1159 "Job aborted because the scheduler ran out of memory.");
1162 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1163 _("Unable to allocate memory for file types."));
1168 job
->compressions
[job
->num_files
] = compression
;
1169 job
->filetypes
[job
->num_files
] = filetype
;
1174 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1181 * 'add_job()' - Add a job to a print queue.
1184 static cupsd_job_t
* /* O - Job object */
1185 add_job(cupsd_client_t
*con
, /* I - Client connection */
1186 cupsd_printer_t
*printer
, /* I - Destination printer */
1187 mime_type_t
*filetype
) /* I - First print file type, if any */
1189 http_status_t status
; /* Policy status */
1190 ipp_attribute_t
*attr
, /* Current attribute */
1191 *auth_info
; /* auth-info attribute */
1192 const char *mandatory
; /* Current mandatory job attribute */
1193 const char *val
; /* Default option value */
1194 int priority
; /* Job priority */
1195 cupsd_job_t
*job
; /* Current job */
1196 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1197 int kbytes
; /* Size of print file */
1198 int i
; /* Looping var */
1199 int lowerpagerange
; /* Page range bound */
1200 int exact
; /* Did we have an exact match? */
1201 ipp_attribute_t
*media_col
, /* media-col attribute */
1202 *media_margin
; /* media-*-margin attribute */
1203 ipp_t
*unsup_col
; /* media-col in unsupported response */
1204 static const char * const readonly
[] =/* List of read-only attributes */
1207 "job-k-octets-completed",
1208 "job-impressions-completed",
1209 "job-media-sheets-completed",
1211 "job-state-message",
1212 "job-state-reasons",
1213 "time-at-completed",
1215 "time-at-processing"
1219 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1220 con
, con
->number
, printer
, printer
->name
,
1221 filetype
, filetype
? filetype
->super
: "none",
1222 filetype
? filetype
->type
: "none");
1225 * Check remote printing to non-shared printer...
1228 if (!printer
->shared
&&
1229 _cups_strcasecmp(con
->http
->hostname
, "localhost") &&
1230 _cups_strcasecmp(con
->http
->hostname
, ServerName
))
1232 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1233 _("The printer or class is not shared."));
1241 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1243 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1245 send_http_error(con
, status
, printer
);
1248 else if (printer
->num_auth_info_required
== 1 &&
1249 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
1252 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1256 else if (auth_info
&& !con
->http
->tls
&&
1257 !httpAddrLocalhost(con
->http
->hostaddr
))
1260 * Require encryption of auth-info over non-local connections...
1263 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1266 #endif /* HAVE_SSL */
1269 * See if the printer is accepting jobs...
1272 if (!printer
->accepting
)
1274 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1275 _("Destination \"%s\" is not accepting jobs."),
1281 * Validate job template attributes; for now just document-format,
1282 * copies, job-sheets, number-up, page-ranges, mandatory attributes, and
1286 for (i
= 0; i
< (int)(sizeof(readonly
) / sizeof(readonly
[0])); i
++)
1288 if ((attr
= ippFindAttribute(con
->request
, readonly
[i
],
1289 IPP_TAG_ZERO
)) != NULL
)
1291 ippDeleteAttribute(con
->request
, attr
);
1293 if (StrictConformance
)
1295 send_ipp_status(con
, IPP_BAD_REQUEST
,
1296 _("The '%s' Job Description attribute cannot be "
1297 "supplied in a job creation request."), readonly
[i
]);
1301 cupsdLogMessage(CUPSD_LOG_INFO
,
1302 "Unexpected '%s' Job Description attribute in a job "
1303 "creation request.", readonly
[i
]);
1309 for (mandatory
= (char *)cupsArrayFirst(printer
->pc
->mandatory
);
1311 mandatory
= (char *)cupsArrayNext(printer
->pc
->mandatory
))
1313 if (!ippFindAttribute(con
->request
, mandatory
, IPP_TAG_ZERO
))
1316 * Missing a required attribute...
1319 send_ipp_status(con
, IPP_CONFLICT
,
1320 _("The \"%s\" attribute is required for print jobs."),
1327 if (filetype
&& printer
->filetypes
&&
1328 !cupsArrayFind(printer
->filetypes
, filetype
))
1330 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1331 /* MIME media type string */
1334 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1337 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1338 _("Unsupported format \"%s\"."), mimetype
);
1340 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1341 "document-format", NULL
, mimetype
);
1346 if ((attr
= ippFindAttribute(con
->request
, "copies",
1347 IPP_TAG_INTEGER
)) != NULL
)
1349 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1351 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1352 attr
->values
[0].integer
);
1353 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1354 "copies", attr
->values
[0].integer
);
1359 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1360 IPP_TAG_ZERO
)) != NULL
)
1362 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1363 attr
->value_tag
!= IPP_TAG_NAME
)
1365 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type."));
1369 if (attr
->num_values
> 2)
1371 send_ipp_status(con
, IPP_BAD_REQUEST
,
1372 _("Too many job-sheets values (%d > 2)."),
1377 for (i
= 0; i
< attr
->num_values
; i
++)
1378 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1379 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1381 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\"."),
1382 attr
->values
[i
].string
.text
);
1387 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1388 IPP_TAG_INTEGER
)) != NULL
)
1390 if (attr
->values
[0].integer
!= 1 &&
1391 attr
->values
[0].integer
!= 2 &&
1392 attr
->values
[0].integer
!= 4 &&
1393 attr
->values
[0].integer
!= 6 &&
1394 attr
->values
[0].integer
!= 9 &&
1395 attr
->values
[0].integer
!= 16)
1397 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1398 attr
->values
[0].integer
);
1399 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1400 "number-up", attr
->values
[0].integer
);
1405 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1406 IPP_TAG_RANGE
)) != NULL
)
1408 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1410 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1411 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1413 send_ipp_status(con
, IPP_BAD_REQUEST
,
1414 _("Bad page-ranges values %d-%d."),
1415 attr
->values
[i
].range
.lower
,
1416 attr
->values
[i
].range
.upper
);
1420 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1425 * Do media selection as needed...
1428 if (!ippFindAttribute(con
->request
, "PageRegion", IPP_TAG_ZERO
) &&
1429 !ippFindAttribute(con
->request
, "PageSize", IPP_TAG_ZERO
) &&
1430 _ppdCacheGetPageSize(printer
->pc
, con
->request
, NULL
, &exact
))
1433 (media_col
= ippFindAttribute(con
->request
, "media-col",
1434 IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
1436 send_ipp_status(con
, IPP_OK_SUBST
, _("Unsupported margins."));
1438 unsup_col
= ippNew();
1439 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1440 "media-bottom-margin",
1441 IPP_TAG_INTEGER
)) != NULL
)
1442 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1443 "media-bottom-margin", media_margin
->values
[0].integer
);
1445 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1446 "media-left-margin",
1447 IPP_TAG_INTEGER
)) != NULL
)
1448 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1449 "media-left-margin", media_margin
->values
[0].integer
);
1451 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1452 "media-right-margin",
1453 IPP_TAG_INTEGER
)) != NULL
)
1454 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1455 "media-right-margin", media_margin
->values
[0].integer
);
1457 if ((media_margin
= ippFindAttribute(media_col
->values
[0].collection
,
1459 IPP_TAG_INTEGER
)) != NULL
)
1460 ippAddInteger(unsup_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
1461 "media-top-margin", media_margin
->values
[0].integer
);
1463 ippAddCollection(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, "media-col",
1465 ippDelete(unsup_col
);
1470 * Make sure we aren't over our limit...
1473 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1476 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1478 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Too many active jobs."));
1482 if ((i
= check_quotas(con
, printer
)) < 0)
1484 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1489 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1494 * Create the job and set things up...
1497 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1498 IPP_TAG_INTEGER
)) != NULL
)
1499 priority
= attr
->values
[0].integer
;
1502 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1503 printer
->options
)) != NULL
)
1504 priority
= atoi(val
);
1508 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1512 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) == NULL
)
1513 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
1514 else if ((attr
->value_tag
!= IPP_TAG_NAME
&&
1515 attr
->value_tag
!= IPP_TAG_NAMELANG
) ||
1516 attr
->num_values
!= 1)
1518 send_ipp_status(con
, IPP_ATTRIBUTES
,
1519 _("Bad job-name value: Wrong type or count."));
1520 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1521 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1524 else if (!ippValidateAttribute(attr
))
1526 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad job-name value: %s"),
1527 cupsLastErrorString());
1528 if ((attr
= ippCopyAttribute(con
->response
, attr
, 0)) != NULL
)
1529 attr
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
1533 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1535 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1536 _("Unable to add job for destination \"%s\"."),
1541 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_REMOTE
);
1542 job
->attrs
= con
->request
;
1544 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1546 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1549 apply_printer_defaults(printer
, job
);
1551 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1553 if (con
->username
[0])
1555 cupsdSetString(&job
->username
, con
->username
);
1558 ippSetString(job
->attrs
, &attr
, 0, con
->username
);
1562 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1563 "add_job: requesting-user-name=\"%s\"",
1564 attr
->values
[0].string
.text
);
1566 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1569 cupsdSetString(&job
->username
, "anonymous");
1572 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1573 "job-originating-user-name", NULL
, job
->username
);
1576 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1577 ippSetName(job
->attrs
, &attr
, "job-originating-user-name");
1580 if (con
->username
[0] || auth_info
)
1582 save_auth_info(con
, job
, auth_info
);
1585 * Remove the auth-info attribute from the attribute data...
1589 ippDeleteAttribute(job
->attrs
, auth_info
);
1592 if ((attr
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_NAME
)) != NULL
)
1593 cupsdSetString(&(job
->name
), attr
->values
[0].string
.text
);
1595 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1596 IPP_TAG_ZERO
)) != NULL
)
1599 * Request contains a job-originating-host-name attribute; validate it...
1602 if (attr
->value_tag
!= IPP_TAG_NAME
||
1603 attr
->num_values
!= 1 ||
1604 strcmp(con
->http
->hostname
, "localhost"))
1607 * Can't override the value if we aren't connected via localhost.
1608 * Also, we can only have 1 value and it must be a name value.
1611 ippDeleteAttribute(job
->attrs
, attr
);
1612 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-originating-host-name", NULL
, con
->http
->hostname
);
1615 ippSetGroupTag(job
->attrs
, &attr
, IPP_TAG_JOB
);
1620 * No job-originating-host-name attribute, so use the hostname from
1624 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1625 "job-originating-host-name", NULL
, con
->http
->hostname
);
1628 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1630 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1631 "time-at-processing", 0);
1632 attr
->value_tag
= IPP_TAG_NOVALUE
;
1633 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1634 "time-at-completed", 0);
1635 attr
->value_tag
= IPP_TAG_NOVALUE
;
1638 * Add remaining job attributes...
1641 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1642 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1643 "job-state", IPP_JOB_STOPPED
);
1644 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1645 job
->reasons
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1646 "job-state-reasons", NULL
, "job-incoming");
1647 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1648 "job-media-sheets-completed", 0);
1649 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1652 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
1653 attr
->values
[0].integer
= 0;
1655 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1657 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1658 IPP_TAG_KEYWORD
)) == NULL
)
1659 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1662 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1663 printer
->options
)) == NULL
)
1666 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1667 "job-hold-until", NULL
, val
);
1669 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
1672 * Hold job until specified time...
1675 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
1677 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1678 job
->state_value
= IPP_JOB_HELD
;
1680 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
1682 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1684 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
1685 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1686 job
->state_value
= IPP_JOB_HELD
;
1690 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1691 job
->state_value
= IPP_JOB_PENDING
;
1693 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
1696 if (!(printer
->type
& CUPS_PRINTER_REMOTE
) || Classification
)
1699 * Add job sheets options...
1702 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1703 IPP_TAG_ZERO
)) == NULL
)
1705 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1706 "Adding default job-sheets values \"%s,%s\"...",
1707 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1709 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1711 ippSetString(job
->attrs
, &attr
, 0, printer
->job_sheets
[0]);
1712 ippSetString(job
->attrs
, &attr
, 1, printer
->job_sheets
[1]);
1715 job
->job_sheets
= attr
;
1718 * Enforce classification level if set...
1723 cupsdLogMessage(CUPSD_LOG_INFO
,
1724 "Classification=\"%s\", ClassifyOverride=%d",
1725 Classification
? Classification
: "(null)",
1728 if (ClassifyOverride
)
1730 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1731 (attr
->num_values
== 1 ||
1732 !strcmp(attr
->values
[1].string
.text
, "none")))
1735 * Force the leading banner to have the classification on it...
1738 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1740 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1741 "job-sheets=\"%s,none\", "
1742 "job-originating-user-name=\"%s\"",
1743 Classification
, job
->username
);
1745 else if (attr
->num_values
== 2 &&
1746 strcmp(attr
->values
[0].string
.text
,
1747 attr
->values
[1].string
.text
) &&
1748 strcmp(attr
->values
[0].string
.text
, "none") &&
1749 strcmp(attr
->values
[1].string
.text
, "none"))
1752 * Can't put two different security markings on the same document!
1755 ippSetString(job
->attrs
, &attr
, 1, attr
->values
[0].string
.text
);
1757 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1758 "job-sheets=\"%s,%s\", "
1759 "job-originating-user-name=\"%s\"",
1760 attr
->values
[0].string
.text
,
1761 attr
->values
[1].string
.text
, job
->username
);
1763 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1764 strcmp(attr
->values
[0].string
.text
, "none") &&
1765 (attr
->num_values
== 1 ||
1766 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1767 strcmp(attr
->values
[1].string
.text
, "none"))))
1769 if (attr
->num_values
== 1)
1770 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1771 "CLASSIFICATION OVERRIDDEN "
1772 "job-sheets=\"%s\", "
1773 "job-originating-user-name=\"%s\"",
1774 attr
->values
[0].string
.text
, job
->username
);
1776 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1777 "CLASSIFICATION OVERRIDDEN "
1778 "job-sheets=\"%s,%s\",fffff "
1779 "job-originating-user-name=\"%s\"",
1780 attr
->values
[0].string
.text
,
1781 attr
->values
[1].string
.text
, job
->username
);
1784 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1785 (attr
->num_values
== 1 ||
1786 strcmp(attr
->values
[1].string
.text
, Classification
)))
1789 * Force the banner to have the classification on it...
1792 if (attr
->num_values
> 1 &&
1793 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1795 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1796 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1800 if (attr
->num_values
== 1 ||
1801 strcmp(attr
->values
[0].string
.text
, "none"))
1802 ippSetString(job
->attrs
, &attr
, 0, Classification
);
1804 if (attr
->num_values
> 1 &&
1805 strcmp(attr
->values
[1].string
.text
, "none"))
1806 ippSetString(job
->attrs
, &attr
, 1, Classification
);
1809 if (attr
->num_values
> 1)
1810 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1811 "CLASSIFICATION FORCED "
1812 "job-sheets=\"%s,%s\", "
1813 "job-originating-user-name=\"%s\"",
1814 attr
->values
[0].string
.text
,
1815 attr
->values
[1].string
.text
, job
->username
);
1817 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1818 "CLASSIFICATION FORCED "
1819 "job-sheets=\"%s\", "
1820 "job-originating-user-name=\"%s\"",
1821 Classification
, job
->username
);
1826 * See if we need to add the starting sheet...
1829 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
1831 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1832 attr
->values
[0].string
.text
);
1834 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1836 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_PURGE
,
1837 "Aborting job because the start banner could not be "
1842 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1845 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1846 IPP_TAG_ZERO
)) != NULL
)
1847 job
->job_sheets
= attr
;
1850 * Fill in the response info...
1853 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
1854 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
1855 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1858 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1860 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1862 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
1863 NULL
, job
->reasons
->values
[0].string
.text
);
1865 con
->response
->request
.status
.status_code
= IPP_OK
;
1868 * Add any job subscriptions...
1871 add_job_subscriptions(con
, job
);
1874 * Set all but the first two attributes to the job attributes group...
1877 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1878 attr
->group_tag
= IPP_TAG_JOB
;
1881 * Fire the "job created" event...
1884 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1887 * Return the new job...
1895 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1899 add_job_subscriptions(
1900 cupsd_client_t
*con
, /* I - Client connection */
1901 cupsd_job_t
*job
) /* I - Newly created job */
1903 int i
; /* Looping var */
1904 ipp_attribute_t
*prev
, /* Previous attribute */
1905 *next
, /* Next attribute */
1906 *attr
; /* Current attribute */
1907 cupsd_subscription_t
*sub
; /* Subscription object */
1908 const char *recipient
, /* notify-recipient-uri */
1909 *pullmethod
; /* notify-pull-method */
1910 ipp_attribute_t
*user_data
; /* notify-user-data */
1911 int interval
; /* notify-time-interval */
1912 unsigned mask
; /* notify-events */
1916 * Find the first subscription group attribute; return if we have
1920 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
1921 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
1928 * Process the subscription attributes in the request...
1937 mask
= CUPSD_EVENT_NONE
;
1939 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
1941 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
1942 attr
->value_tag
== IPP_TAG_URI
)
1945 * Validate the recipient scheme against the ServerBin/notifier
1949 char notifier
[1024], /* Notifier filename */
1950 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
1951 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
1952 host
[HTTP_MAX_URI
], /* Host portion of URI */
1953 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1954 int port
; /* Port portion of URI */
1957 recipient
= attr
->values
[0].string
.text
;
1959 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
1960 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
1961 host
, sizeof(host
), &port
,
1962 resource
, sizeof(resource
)) < HTTP_URI_OK
)
1964 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1965 _("Bad notify-recipient-uri \"%s\"."), recipient
);
1966 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
1967 "notify-status-code", IPP_URI_SCHEME
);
1971 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
1973 if (access(notifier
, X_OK
))
1975 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1976 _("notify-recipient-uri URI \"%s\" uses unknown "
1977 "scheme."), recipient
);
1978 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
1979 "notify-status-code", IPP_URI_SCHEME
);
1983 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
1985 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1986 _("notify-recipient-uri URI \"%s\" is already used."),
1988 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
1989 "notify-status-code", IPP_ATTRIBUTES
);
1993 else if (!strcmp(attr
->name
, "notify-pull-method") &&
1994 attr
->value_tag
== IPP_TAG_KEYWORD
)
1996 pullmethod
= attr
->values
[0].string
.text
;
1998 if (strcmp(pullmethod
, "ippget"))
2000 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2001 _("Bad notify-pull-method \"%s\"."), pullmethod
);
2002 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2003 "notify-status-code", IPP_ATTRIBUTES
);
2007 else if (!strcmp(attr
->name
, "notify-charset") &&
2008 attr
->value_tag
== IPP_TAG_CHARSET
&&
2009 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2010 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2012 send_ipp_status(con
, IPP_CHARSET
,
2013 _("Character set \"%s\" not supported."),
2014 attr
->values
[0].string
.text
);
2017 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2018 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2019 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2021 send_ipp_status(con
, IPP_CHARSET
,
2022 _("Language \"%s\" not supported."),
2023 attr
->values
[0].string
.text
);
2026 else if (!strcmp(attr
->name
, "notify-user-data") &&
2027 attr
->value_tag
== IPP_TAG_STRING
)
2029 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2031 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2032 _("The notify-user-data value is too large "
2033 "(%d > 63 octets)."),
2034 attr
->values
[0].unknown
.length
);
2040 else if (!strcmp(attr
->name
, "notify-events") &&
2041 attr
->value_tag
== IPP_TAG_KEYWORD
)
2043 for (i
= 0; i
< attr
->num_values
; i
++)
2044 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2046 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2048 send_ipp_status(con
, IPP_BAD_REQUEST
,
2049 _("The notify-lease-duration attribute cannot be "
2050 "used with job subscriptions."));
2053 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2054 attr
->value_tag
== IPP_TAG_INTEGER
)
2055 interval
= attr
->values
[0].integer
;
2060 if (!recipient
&& !pullmethod
)
2063 if (mask
== CUPSD_EVENT_NONE
)
2064 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2066 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2067 recipient
, 0)) != NULL
)
2069 sub
->interval
= interval
;
2071 cupsdSetString(&sub
->owner
, job
->username
);
2075 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2076 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2077 (size_t)sub
->user_data_len
);
2080 ippAddSeparator(con
->response
);
2081 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2082 "notify-subscription-id", sub
->id
);
2084 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
2092 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2095 * Remove all of the subscription attributes from the job request...
2097 * TODO: Optimize this since subscription groups have to come at the
2098 * end of the request...
2101 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2105 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2106 attr
->group_tag
== IPP_TAG_ZERO
)
2109 * Free and remove this attribute...
2112 ippDeleteAttribute(NULL
, attr
);
2117 job
->attrs
->attrs
= next
;
2123 job
->attrs
->last
= prev
;
2124 job
->attrs
->current
= prev
;
2129 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2131 * See RFC 4122 for the definition of UUIDs and the format.
2135 add_job_uuid(cupsd_job_t
*job
) /* I - Job */
2137 char uuid
[64]; /* job-uuid string */
2141 * Add a job-uuid attribute if none exists...
2144 if (!ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2145 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
,
2146 httpAssembleUUID(ServerName
, RemotePort
, job
->dest
, job
->id
,
2147 uuid
, sizeof(uuid
)));
2152 * 'add_printer()' - Add a printer to the system.
2156 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2157 ipp_attribute_t
*uri
) /* I - URI of printer */
2159 http_status_t status
; /* Policy status */
2160 int i
; /* Looping var */
2161 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2162 username
[HTTP_MAX_URI
], /* Username portion of URI */
2163 host
[HTTP_MAX_URI
], /* Host portion of URI */
2164 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2165 int port
; /* Port portion of URI */
2166 cupsd_printer_t
*printer
; /* Printer/class */
2167 ipp_attribute_t
*attr
; /* Printer attribute */
2168 cups_file_t
*fp
; /* Script/PPD file */
2169 char line
[1024]; /* Line from file... */
2170 char srcfile
[1024], /* Source Script/PPD file */
2171 dstfile
[1024]; /* Destination Script/PPD file */
2172 int modify
; /* Non-zero if we are modifying */
2173 int changed_driver
, /* Changed the PPD/interface script? */
2174 need_restart_job
, /* Need to restart job? */
2175 set_device_uri
, /* Did we set the device URI? */
2176 set_port_monitor
; /* Did we set the port monitor? */
2179 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2180 con
->number
, uri
->values
[0].string
.text
);
2183 * Do we have a valid URI?
2186 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2187 sizeof(scheme
), username
, sizeof(username
), host
,
2188 sizeof(host
), &port
, resource
, sizeof(resource
));
2190 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2193 * No, return an error...
2196 send_ipp_status(con
, IPP_BAD_REQUEST
,
2197 _("The printer-uri must be of the form "
2198 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2203 * Do we have a valid printer name?
2206 if (!validate_name(resource
+ 10))
2209 * No, return an error...
2212 send_ipp_status(con
, IPP_BAD_REQUEST
,
2213 _("The printer-uri \"%s\" contains invalid characters."),
2214 uri
->values
[0].string
.text
);
2219 * See if the printer already exists; if not, create a new printer...
2222 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2225 * Printer doesn't exist; see if we have a class of the same name...
2228 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
)
2231 * Yes, return an error...
2234 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2235 _("A class named \"%s\" already exists."),
2241 * No, check the default policy then add the printer...
2244 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2246 send_http_error(con
, status
, NULL
);
2250 printer
= cupsdAddPrinter(resource
+ 10);
2253 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2256 send_http_error(con
, status
, printer
);
2263 * Look for attributes and copy them over as needed...
2267 need_restart_job
= 0;
2269 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2270 IPP_TAG_TEXT
)) != NULL
)
2271 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2273 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2274 IPP_TAG_TEXT
)) != NULL
)
2275 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2279 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2280 IPP_TAG_URI
)) != NULL
)
2283 * Do we have a valid device URI?
2286 http_uri_status_t uri_status
; /* URI separation status */
2287 char old_device_uri
[1024];
2288 /* Old device URI */
2289 static const char * const uri_status_strings
[] =
2292 "Bad arguments to function.",
2293 "Bad resource path.",
2295 "Bad hostname/address.",
2296 "Bad username/password.",
2300 "Missing URI scheme.",
2301 "Unknown URI scheme",
2302 "Missing resource path."
2306 need_restart_job
= 1;
2308 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2309 attr
->values
[0].string
.text
,
2310 scheme
, sizeof(scheme
),
2311 username
, sizeof(username
),
2312 host
, sizeof(host
), &port
,
2313 resource
, sizeof(resource
));
2315 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2316 "%s device-uri: %s", printer
->name
,
2317 uri_status_strings
[uri_status
- HTTP_URI_STATUS_OVERFLOW
]);
2319 if (uri_status
< HTTP_URI_OK
)
2321 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"."),
2322 attr
->values
[0].string
.text
);
2326 if (!strcmp(scheme
, "file"))
2329 * See if the administrator has enabled file devices...
2332 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2335 * File devices are disabled and the URL is not file:/dev/null...
2338 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2339 _("File device URIs have been disabled. "
2340 "To enable, see the FileDevice directive in "
2341 "\"%s/cups-files.conf\"."),
2349 * See if the backend exists and is executable...
2352 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2353 if (access(srcfile
, X_OK
))
2356 * Could not find device in list!
2359 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2360 _("Bad device-uri scheme \"%s\"."), scheme
);
2365 if (printer
->sanitized_device_uri
)
2366 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2367 sizeof(old_device_uri
));
2369 old_device_uri
[0] = '\0';
2371 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2373 cupsdLogMessage(CUPSD_LOG_INFO
,
2374 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2375 printer
->name
, printer
->sanitized_device_uri
,
2381 set_port_monitor
= 0;
2383 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2384 IPP_TAG_NAME
)) != NULL
)
2386 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2389 need_restart_job
= 1;
2391 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2395 for (i
= 0; i
< supported
->num_values
; i
++)
2396 if (!strcmp(supported
->values
[i
].string
.text
,
2397 attr
->values
[0].string
.text
))
2401 if (!supported
|| i
>= supported
->num_values
)
2403 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"."),
2404 attr
->values
[0].string
.text
);
2408 cupsdLogMessage(CUPSD_LOG_INFO
,
2409 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2410 printer
->name
, attr
->values
[0].string
.text
,
2411 printer
->port_monitor
? printer
->port_monitor
: "none");
2413 if (strcmp(attr
->values
[0].string
.text
, "none"))
2414 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2416 cupsdClearString(&printer
->port_monitor
);
2418 set_port_monitor
= 1;
2421 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2422 IPP_TAG_BOOLEAN
)) != NULL
&&
2423 attr
->values
[0].boolean
!= printer
->accepting
)
2425 cupsdLogMessage(CUPSD_LOG_INFO
,
2426 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2427 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2429 printer
->accepting
= attr
->values
[0].boolean
;
2431 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2432 "%s accepting jobs.",
2433 printer
->accepting
? "Now" : "No longer");
2436 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2437 IPP_TAG_BOOLEAN
)) != NULL
)
2439 if (attr
->values
[0].boolean
&&
2440 printer
->num_auth_info_required
== 1 &&
2441 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2443 send_ipp_status(con
, IPP_BAD_REQUEST
,
2444 _("Cannot share a remote Kerberized printer."));
2448 if (printer
->shared
&& !attr
->values
[0].boolean
)
2449 cupsdDeregisterPrinter(printer
, 1);
2451 cupsdLogMessage(CUPSD_LOG_INFO
,
2452 "Setting %s printer-is-shared to %d (was %d.)",
2453 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2455 printer
->shared
= attr
->values
[0].boolean
;
2458 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2459 IPP_TAG_ENUM
)) != NULL
)
2461 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2462 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2464 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d."),
2465 attr
->values
[0].integer
);
2469 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2470 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2472 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2473 cupsdStopPrinter(printer
, 0);
2476 need_restart_job
= 1;
2477 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2481 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2482 IPP_TAG_TEXT
)) != NULL
)
2484 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2485 sizeof(printer
->state_message
));
2487 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
, "%s",
2488 printer
->state_message
);
2491 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2492 IPP_TAG_KEYWORD
)) != NULL
)
2494 if (attr
->num_values
>
2495 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2497 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2498 _("Too many printer-state-reasons values (%d > %d)."),
2500 (int)(sizeof(printer
->reasons
) /
2501 sizeof(printer
->reasons
[0])));
2505 for (i
= 0; i
< printer
->num_reasons
; i
++)
2506 _cupsStrFree(printer
->reasons
[i
]);
2508 printer
->num_reasons
= 0;
2509 for (i
= 0; i
< attr
->num_values
; i
++)
2511 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2514 printer
->reasons
[printer
->num_reasons
] =
2515 _cupsStrRetain(attr
->values
[i
].string
.text
);
2516 printer
->num_reasons
++;
2518 if (!strcmp(attr
->values
[i
].string
.text
, "paused") &&
2519 printer
->state
!= IPP_PRINTER_STOPPED
)
2521 cupsdLogMessage(CUPSD_LOG_INFO
,
2522 "Setting %s printer-state to %d (was %d.)",
2523 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2524 cupsdStopPrinter(printer
, 0);
2528 if (PrintcapFormat
== PRINTCAP_PLIST
)
2529 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2531 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
2532 "Printer \"%s\" state changed.", printer
->name
);
2535 set_printer_defaults(con
, printer
);
2537 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2538 IPP_TAG_KEYWORD
)) != NULL
)
2539 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2542 * See if we have all required attributes...
2545 if (!printer
->device_uri
)
2546 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2549 * See if we have an interface script or PPD file attached to the request...
2554 need_restart_job
= 1;
2557 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2559 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2562 * Yes; get the first line from it...
2566 cupsFileGets(fp
, line
, sizeof(line
));
2570 * Then see what kind of file it is...
2573 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2576 if (!strncmp(line
, "*PPD-Adobe", 10))
2579 * The new file is a PPD file, so remove any old interface script
2580 * that might be lying around...
2588 * This must be an interface script, so move the file over to the
2589 * interfaces directory and make it executable...
2592 if (copy_file(srcfile
, dstfile
))
2594 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2595 _("Unable to copy interface script - %s"),
2600 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2601 "Copied interface script successfully");
2602 chmod(dstfile
, 0755);
2605 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2608 if (!strncmp(line
, "*PPD-Adobe", 10))
2611 * The new file is a PPD file, so move the file over to the
2612 * ppd directory and make it readable by all...
2615 if (copy_file(srcfile
, dstfile
))
2617 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2618 _("Unable to copy PPD file - %s"),
2623 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2624 "Copied PPD file successfully");
2625 chmod(dstfile
, 0644);
2630 * This must be an interface script, so remove any old PPD file that
2631 * may be lying around...
2638 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2639 IPP_TAG_NAME
)) != NULL
)
2641 need_restart_job
= 1;
2644 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2647 * Raw driver, remove any existing PPD or interface script files.
2650 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2654 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2664 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2668 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2671 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2673 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file."));
2677 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2678 "Copied PPD file successfully");
2685 * If we changed the PPD/interface script, then remove the printer's cache
2686 * file and clear the printer-state-reasons...
2689 char cache_name
[1024]; /* Cache filename for printer attrs */
2691 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.data", CacheDir
,
2695 cupsdSetPrinterReasons(printer
, "none");
2698 * (Re)register color profiles...
2701 cupsdRegisterColor(printer
);
2705 * If we set the device URI but not the port monitor, check which port
2706 * monitor to use by default...
2709 if (set_device_uri
&& !set_port_monitor
)
2711 ppd_file_t
*ppd
; /* PPD file */
2712 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2715 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2716 sizeof(scheme
), username
, sizeof(username
), host
,
2717 sizeof(host
), &port
, resource
, sizeof(resource
));
2719 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2721 if ((ppd
= _ppdOpenFile(srcfile
, _PPD_LOCALIZATION_NONE
)) != NULL
)
2723 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2725 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2726 if (!strcmp(scheme
, ppdattr
->spec
))
2728 cupsdLogMessage(CUPSD_LOG_INFO
,
2729 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2730 printer
->name
, ppdattr
->value
,
2731 printer
->port_monitor
? printer
->port_monitor
2734 if (strcmp(ppdattr
->value
, "none"))
2735 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2737 cupsdClearString(&printer
->port_monitor
);
2747 * Update the printer attributes and return...
2750 cupsdSetPrinterAttrs(printer
);
2751 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2753 if (need_restart_job
&& printer
->job
)
2756 * Restart the current job...
2759 cupsdSetJobState(printer
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
2760 "Job restarted because the printer was modified.");
2763 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2767 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
,
2768 printer
, NULL
, "Printer \"%s\" modified by \"%s\".",
2769 printer
->name
, get_username(con
));
2771 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2772 printer
->name
, get_username(con
));
2776 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
,
2777 printer
, NULL
, "New printer \"%s\" added by \"%s\".",
2778 printer
->name
, get_username(con
));
2780 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2781 printer
->name
, get_username(con
));
2784 con
->response
->request
.status
.status_code
= IPP_OK
;
2789 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2790 * based upon the printer state...
2794 add_printer_state_reasons(
2795 cupsd_client_t
*con
, /* I - Client connection */
2796 cupsd_printer_t
*p
) /* I - Printer info */
2798 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2799 "add_printer_state_reasons(%p[%d], %p[%s])",
2800 con
, con
->number
, p
, p
->name
);
2802 if (p
->num_reasons
== 0)
2803 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2804 "printer-state-reasons", NULL
, "none");
2806 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2807 "printer-state-reasons", p
->num_reasons
, NULL
,
2808 (const char * const *)p
->reasons
);
2813 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2814 * the specified printer or class.
2818 add_queued_job_count(
2819 cupsd_client_t
*con
, /* I - Client connection */
2820 cupsd_printer_t
*p
) /* I - Printer or class */
2822 int count
; /* Number of jobs on destination */
2825 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2826 con
, con
->number
, p
, p
->name
);
2828 count
= cupsdGetPrinterJobCount(p
->name
);
2830 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2831 "queued-job-count", count
);
2836 * 'apply_printer_defaults()' - Apply printer default options to a job.
2840 apply_printer_defaults(
2841 cupsd_printer_t
*printer
, /* I - Printer */
2842 cupsd_job_t
*job
) /* I - Job */
2844 int i
, /* Looping var */
2845 num_options
; /* Number of default options */
2846 cups_option_t
*options
, /* Default options */
2847 *option
; /* Current option */
2851 * Collect all of the default options and add the missing ones to the
2855 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
2856 option
= printer
->options
;
2859 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
2861 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
2866 * Encode these options as attributes in the job object...
2869 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
2870 cupsFreeOptions(num_options
, options
);
2875 * 'authenticate_job()' - Set job authentication info.
2879 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
2880 ipp_attribute_t
*uri
) /* I - Job URI */
2882 ipp_attribute_t
*attr
, /* job-id attribute */
2883 *auth_info
; /* auth-info attribute */
2884 int jobid
; /* Job ID */
2885 cupsd_job_t
*job
; /* Current job */
2886 char scheme
[HTTP_MAX_URI
],
2887 /* Method portion of URI */
2888 username
[HTTP_MAX_URI
],
2889 /* Username portion of URI */
2891 /* Host portion of URI */
2892 resource
[HTTP_MAX_URI
];
2893 /* Resource portion of URI */
2894 int port
; /* Port portion of URI */
2897 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
2898 con
, con
->number
, uri
->values
[0].string
.text
);
2901 * Start with "everything is OK" status...
2904 con
->response
->request
.status
.status_code
= IPP_OK
;
2907 * See if we have a job URI or a printer URI...
2910 if (!strcmp(uri
->name
, "printer-uri"))
2913 * Got a printer URI; see if we also have a job-id attribute...
2916 if ((attr
= ippFindAttribute(con
->request
, "job-id",
2917 IPP_TAG_INTEGER
)) == NULL
)
2919 send_ipp_status(con
, IPP_BAD_REQUEST
,
2920 _("Got a printer-uri attribute but no job-id."));
2924 jobid
= attr
->values
[0].integer
;
2929 * Got a job URI; parse it to get the job ID...
2932 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2933 sizeof(scheme
), username
, sizeof(username
), host
,
2934 sizeof(host
), &port
, resource
, sizeof(resource
));
2936 if (strncmp(resource
, "/jobs/", 6))
2942 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
2943 uri
->values
[0].string
.text
);
2947 jobid
= atoi(resource
+ 6);
2951 * See if the job exists...
2954 if ((job
= cupsdFindJob(jobid
)) == NULL
)
2957 * Nope - return a "not found" error...
2960 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
2965 * See if the job has been completed...
2968 if (job
->state_value
!= IPP_JOB_HELD
)
2971 * Return a "not-possible" error...
2974 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2975 _("Job #%d is not held for authentication."),
2981 * See if we have already authenticated...
2984 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
2986 if (!con
->username
[0] && !auth_info
)
2988 cupsd_printer_t
*printer
; /* Job destination */
2991 * No auth data. If we need to authenticate via Kerberos, send a
2992 * HTTP auth challenge, otherwise just return an IPP error...
2995 printer
= cupsdFindDest(job
->dest
);
2997 if (printer
&& printer
->num_auth_info_required
> 0 &&
2998 !strcmp(printer
->auth_info_required
[0], "negotiate"))
2999 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3001 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3002 _("No authentication information provided."));
3007 * See if the job is owned by the requesting user...
3010 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3012 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3013 cupsdFindDest(job
->dest
));
3018 * Save the authentication information for this job...
3021 save_auth_info(con
, job
, auth_info
);
3024 * Reset the job-hold-until value to "no-hold"...
3027 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3028 IPP_TAG_KEYWORD
)) == NULL
)
3029 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3033 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
3034 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
3038 * Release the job and return...
3041 cupsdReleaseJob(job
);
3043 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, NULL
, job
, "Job authenticated by user");
3045 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3052 * 'cancel_all_jobs()' - Cancel all or selected print jobs.
3056 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3057 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3059 int i
; /* Looping var */
3060 http_status_t status
; /* Policy status */
3061 cups_ptype_t dtype
; /* Destination type */
3062 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3063 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3064 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3065 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3066 int port
; /* Port portion of URI */
3067 ipp_attribute_t
*attr
; /* Attribute in request */
3068 const char *username
= NULL
; /* Username */
3069 cupsd_jobaction_t purge
= CUPSD_JOB_DEFAULT
;
3071 cupsd_printer_t
*printer
; /* Printer */
3072 ipp_attribute_t
*job_ids
; /* job-ids attribute */
3073 cupsd_job_t
*job
; /* Job */
3076 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3077 con
->number
, uri
->values
[0].string
.text
);
3080 * Get the jobs to cancel/purge...
3083 switch (con
->request
->request
.op
.operation_id
)
3085 case IPP_PURGE_JOBS
:
3087 * Get the username (if any) for the jobs we want to cancel (only if
3088 * "my-jobs" is specified...
3091 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3092 IPP_TAG_BOOLEAN
)) != NULL
&&
3093 attr
->values
[0].boolean
)
3095 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3096 IPP_TAG_NAME
)) != NULL
)
3097 username
= attr
->values
[0].string
.text
;
3100 send_ipp_status(con
, IPP_BAD_REQUEST
,
3101 _("Missing requesting-user-name attribute."));
3107 * Look for the "purge-jobs" attribute...
3110 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3111 IPP_TAG_BOOLEAN
)) != NULL
)
3112 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3114 purge
= CUPSD_JOB_PURGE
;
3117 case IPP_CANCEL_MY_JOBS
:
3118 if (con
->username
[0])
3119 username
= con
->username
;
3120 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3121 IPP_TAG_NAME
)) != NULL
)
3122 username
= attr
->values
[0].string
.text
;
3125 send_ipp_status(con
, IPP_BAD_REQUEST
,
3126 _("Missing requesting-user-name attribute."));
3134 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
3137 * See if we have a printer URI...
3140 if (strcmp(uri
->name
, "printer-uri"))
3142 send_ipp_status(con
, IPP_BAD_REQUEST
,
3143 _("The printer-uri attribute is required."));
3148 * And if the destination is valid...
3151 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3157 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3158 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3159 hostname
, sizeof(hostname
), &port
,
3160 resource
, sizeof(resource
));
3162 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3163 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3165 send_ipp_status(con
, IPP_NOT_FOUND
,
3166 _("The printer or class does not exist."));
3174 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3176 send_http_error(con
, status
, NULL
);
3182 for (i
= 0; i
< job_ids
->num_values
; i
++)
3184 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
)
3187 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3188 _cups_strcasecmp(job
->username
, username
))
3192 if (i
< job_ids
->num_values
)
3194 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3195 job_ids
->values
[i
].integer
);
3199 for (i
= 0; i
< job_ids
->num_values
; i
++)
3201 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3203 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3204 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3205 "Job canceled by user.");
3208 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3209 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3215 * Cancel all jobs on all printers...
3218 cupsdCancelJobs(NULL
, username
, purge
);
3220 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3221 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3231 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3234 send_http_error(con
, status
, printer
);
3240 for (i
= 0; i
< job_ids
->num_values
; i
++)
3242 if ((job
= cupsdFindJob(job_ids
->values
[i
].integer
)) == NULL
||
3243 _cups_strcasecmp(job
->dest
, printer
->name
))
3246 if (con
->request
->request
.op
.operation_id
== IPP_CANCEL_MY_JOBS
&&
3247 _cups_strcasecmp(job
->username
, username
))
3251 if (i
< job_ids
->num_values
)
3253 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3254 job_ids
->values
[i
].integer
);
3258 for (i
= 0; i
< job_ids
->num_values
; i
++)
3260 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
3262 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3263 purge
== CUPSD_JOB_PURGE
? "Job purged by user." :
3264 "Job canceled by user.");
3267 cupsdLogMessage(CUPSD_LOG_INFO
, "Selected jobs were %s by \"%s\".",
3268 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3274 * Cancel all of the jobs on the named printer...
3277 cupsdCancelJobs(printer
->name
, username
, purge
);
3279 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3281 purge
== CUPSD_JOB_PURGE
? "purged" : "canceled",
3286 con
->response
->request
.status
.status_code
= IPP_OK
;
3291 * 'cancel_job()' - Cancel a print job.
3295 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3296 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3298 ipp_attribute_t
*attr
; /* Current attribute */
3299 int jobid
; /* Job ID */
3300 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3301 username
[HTTP_MAX_URI
], /* Username portion of URI */
3302 host
[HTTP_MAX_URI
], /* Host portion of URI */
3303 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3304 int port
; /* Port portion of URI */
3305 cupsd_job_t
*job
; /* Job information */
3306 cups_ptype_t dtype
; /* Destination type (printer/class) */
3307 cupsd_printer_t
*printer
; /* Printer data */
3308 cupsd_jobaction_t purge
; /* Purge the job? */
3311 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3312 con
->number
, uri
->values
[0].string
.text
);
3315 * See if we have a job URI or a printer URI...
3318 if (!strcmp(uri
->name
, "printer-uri"))
3321 * Got a printer URI; see if we also have a job-id attribute...
3324 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3325 IPP_TAG_INTEGER
)) == NULL
)
3327 send_ipp_status(con
, IPP_BAD_REQUEST
,
3328 _("Got a printer-uri attribute but no job-id."));
3332 if ((jobid
= attr
->values
[0].integer
) == 0)
3335 * Find the current job on the specified printer...
3338 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3344 send_ipp_status(con
, IPP_NOT_FOUND
,
3345 _("The printer or class does not exist."));
3350 * See if there are any pending jobs...
3353 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3355 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3356 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3357 !_cups_strcasecmp(job
->dest
, printer
->name
))
3365 * No, try stopped jobs...
3368 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3370 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3371 if (job
->state_value
== IPP_JOB_STOPPED
&&
3372 !_cups_strcasecmp(job
->dest
, printer
->name
))
3379 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3389 * Got a job URI; parse it to get the job ID...
3392 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3393 sizeof(scheme
), username
, sizeof(username
), host
,
3394 sizeof(host
), &port
, resource
, sizeof(resource
));
3396 if (strncmp(resource
, "/jobs/", 6))
3402 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3403 uri
->values
[0].string
.text
);
3407 jobid
= atoi(resource
+ 6);
3411 * Look for the "purge-job" attribute...
3414 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3415 IPP_TAG_BOOLEAN
)) != NULL
)
3416 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3418 purge
= CUPSD_JOB_DEFAULT
;
3421 * See if the job exists...
3424 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3427 * Nope - return a "not found" error...
3430 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3435 * See if the job is owned by the requesting user...
3438 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3440 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3441 cupsdFindDest(job
->dest
));
3446 * See if the job is already completed, canceled, or aborted; if so,
3447 * we can't cancel...
3450 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3452 switch (job
->state_value
)
3454 case IPP_JOB_CANCELED
:
3455 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3456 _("Job #%d is already canceled - can\'t cancel."),
3460 case IPP_JOB_ABORTED
:
3461 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3462 _("Job #%d is already aborted - can\'t cancel."),
3467 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3468 _("Job #%d is already completed - can\'t cancel."),
3477 * Cancel the job and return...
3480 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3481 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3482 "Job canceled by \"%s\"",
3486 if (purge
== CUPSD_JOB_PURGE
)
3487 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3490 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3493 con
->response
->request
.status
.status_code
= IPP_OK
;
3498 * 'cancel_subscription()' - Cancel a subscription.
3502 cancel_subscription(
3503 cupsd_client_t
*con
, /* I - Client connection */
3504 int sub_id
) /* I - Subscription ID */
3506 http_status_t status
; /* Policy status */
3507 cupsd_subscription_t
*sub
; /* Subscription */
3510 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3511 "cancel_subscription(con=%p[%d], sub_id=%d)",
3512 con
, con
->number
, sub_id
);
3515 * Is the subscription ID valid?
3518 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3521 * Bad subscription ID...
3524 send_ipp_status(con
, IPP_NOT_FOUND
,
3525 _("Subscription #%d does not exist."), sub_id
);
3533 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3535 con
, sub
->owner
)) != HTTP_OK
)
3537 send_http_error(con
, status
, sub
->dest
);
3542 * Cancel the subscription...
3545 cupsdDeleteSubscription(sub
, 1);
3547 con
->response
->request
.status
.status_code
= IPP_OK
;
3552 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3555 static int /* O - 1 if OK, 0 if not */
3556 check_rss_recipient(
3557 const char *recipient
) /* I - Recipient URI */
3559 cupsd_subscription_t
*sub
; /* Current subscription */
3562 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3564 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3568 * Compare the URIs up to the first ?...
3571 const char *r1
, *r2
;
3573 for (r1
= recipient
, r2
= sub
->recipient
;
3574 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3586 * 'check_quotas()' - Check quotas for a printer and user.
3589 static int /* O - 1 if OK, 0 if forbidden,
3590 -1 if limit reached */
3591 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3592 cupsd_printer_t
*p
) /* I - Printer or class */
3594 char username
[33], /* Username */
3595 *name
; /* Current user name */
3596 cupsd_quota_t
*q
; /* Quota data */
3597 #ifdef HAVE_MBR_UID_TO_UUID
3599 * Use Apple membership APIs which require that all names represent
3600 * valid user account or group records accessible by the server.
3603 uuid_t usr_uuid
; /* UUID for job requesting user */
3604 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3605 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3606 int mbr_err
; /* Error from membership function */
3607 int is_member
; /* Is this user a member? */
3610 * Use standard POSIX APIs for checking users and groups...
3613 struct passwd
*pw
; /* User password data */
3614 #endif /* HAVE_MBR_UID_TO_UUID */
3617 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3618 con
, con
->number
, p
, p
->name
);
3621 * Figure out who is printing...
3624 strlcpy(username
, get_username(con
), sizeof(username
));
3626 if ((name
= strchr(username
, '@')) != NULL
)
3627 *name
= '\0'; /* Strip @REALM */
3630 * Check global active job limits for printers and users...
3633 if (MaxJobsPerPrinter
)
3636 * Check if there are too many pending jobs on this printer...
3639 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3641 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3650 * Check if there are too many pending jobs for this user...
3653 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3655 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3662 * Check against users...
3665 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3668 if (cupsArrayCount(p
->users
))
3670 #ifdef HAVE_MBR_UID_TO_UUID
3672 * Get UUID for job requesting user...
3675 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3681 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3682 "check_quotas: UUID lookup failed for user \"%s\"",
3684 cupsdLogMessage(CUPSD_LOG_INFO
,
3685 "Denying user \"%s\" access to printer \"%s\" "
3686 "(unknown user)...",
3692 * Get UID and GID of requesting user...
3695 pw
= getpwnam(username
);
3697 #endif /* HAVE_MBR_UID_TO_UUID */
3699 for (name
= (char *)cupsArrayFirst(p
->users
);
3701 name
= (char *)cupsArrayNext(p
->users
))
3705 * Check group membership...
3708 #ifdef HAVE_MBR_UID_TO_UUID
3711 if (uuid_parse(name
+ 2, grp_uuid
))
3712 uuid_clear(grp_uuid
);
3714 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3717 * Invalid ACL entries are ignored for matching; just record a
3718 * warning in the log...
3721 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3722 "check_quotas: UUID lookup failed for ACL entry "
3723 "\"%s\" (err=%d)", name
, mbr_err
);
3724 cupsdLogMessage(CUPSD_LOG_WARN
,
3725 "Access control entry \"%s\" not a valid group name; "
3726 "entry ignored", name
);
3729 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3733 * At this point, there should be no errors, but check anyways...
3736 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3737 "check_quotas: group \"%s\" membership check "
3738 "failed (err=%d)", name
+ 1, mbr_err
);
3743 * Stop if we found a match...
3750 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3752 #endif /* HAVE_MBR_UID_TO_UUID */
3754 #ifdef HAVE_MBR_UID_TO_UUID
3759 if (uuid_parse(name
+ 1, usr2_uuid
))
3760 uuid_clear(usr2_uuid
);
3762 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3765 * Invalid ACL entries are ignored for matching; just record a
3766 * warning in the log...
3769 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3770 "check_quotas: UUID lookup failed for ACL entry "
3771 "\"%s\" (err=%d)", name
, mbr_err
);
3772 cupsdLogMessage(CUPSD_LOG_WARN
,
3773 "Access control entry \"%s\" not a valid user name; "
3774 "entry ignored", name
);
3777 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3781 else if (!_cups_strcasecmp(username
, name
))
3783 #endif /* HAVE_MBR_UID_TO_UUID */
3785 if ((name
!= NULL
) == p
->deny_users
)
3787 cupsdLogMessage(CUPSD_LOG_INFO
,
3788 "Denying user \"%s\" access to printer \"%s\"...",
3798 if (p
->k_limit
|| p
->page_limit
)
3800 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3802 cupsdLogMessage(CUPSD_LOG_ERROR
,
3803 "Unable to allocate quota data for user \"%s\"",
3808 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3809 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3811 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3818 * If we have gotten this far, we're done!
3826 * 'close_job()' - Close a multi-file job.
3830 close_job(cupsd_client_t
*con
, /* I - Client connection */
3831 ipp_attribute_t
*uri
) /* I - Printer URI */
3833 cupsd_job_t
*job
; /* Job */
3834 ipp_attribute_t
*attr
; /* Attribute */
3835 char job_uri
[HTTP_MAX_URI
],
3837 username
[256]; /* User name */
3840 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3841 con
->number
, uri
->values
[0].string
.text
);
3844 * See if we have a job URI or a printer URI...
3847 if (strcmp(uri
->name
, "printer-uri"))
3850 * job-uri is not supported by Close-Job!
3853 send_ipp_status(con
, IPP_BAD_REQUEST
,
3854 _("Close-Job doesn't support the job-uri attribute."));
3859 * Got a printer URI; see if we also have a job-id attribute...
3862 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3863 IPP_TAG_INTEGER
)) == NULL
)
3865 send_ipp_status(con
, IPP_BAD_REQUEST
,
3866 _("Got a printer-uri attribute but no job-id."));
3870 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
3873 * Nope - return a "not found" error...
3876 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3877 attr
->values
[0].integer
);
3882 * See if the job is owned by the requesting user...
3885 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3887 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3888 cupsdFindDest(job
->dest
));
3893 * Add any ending sheet...
3896 if (cupsdTimeoutJob(job
))
3899 if (job
->state_value
== IPP_JOB_STOPPED
)
3901 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3902 job
->state_value
= IPP_JOB_PENDING
;
3904 else if (job
->state_value
== IPP_JOB_HELD
)
3906 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3907 IPP_TAG_KEYWORD
)) == NULL
)
3908 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3910 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
3912 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3913 job
->state_value
= IPP_JOB_PENDING
;
3918 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3921 * Fill in the response info...
3924 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
3925 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
3926 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
3929 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
3931 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
3934 con
->response
->request
.status
.status_code
= IPP_OK
;
3937 * Start the job if necessary...
3945 * 'copy_attrs()' - Copy attributes from one request to another.
3949 copy_attrs(ipp_t
*to
, /* I - Destination request */
3950 ipp_t
*from
, /* I - Source request */
3951 cups_array_t
*ra
, /* I - Requested attributes */
3952 ipp_tag_t group
, /* I - Group to copy */
3953 int quickcopy
, /* I - Do a quick copy? */
3954 cups_array_t
*exclude
) /* I - Attributes to exclude? */
3956 ipp_attribute_t
*fromattr
; /* Source attribute */
3959 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3960 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3961 to
, from
, ra
, group
, quickcopy
);
3966 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3969 * Filter attributes as needed...
3972 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3973 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3976 if (!strcmp(fromattr
->name
, "document-password") ||
3977 !strcmp(fromattr
->name
, "job-authorization-uri") ||
3978 !strcmp(fromattr
->name
, "job-password") ||
3979 !strcmp(fromattr
->name
, "job-password-encryption") ||
3980 !strcmp(fromattr
->name
, "job-printer-uri"))
3984 (cupsArrayFind(exclude
, fromattr
->name
) ||
3985 cupsArrayFind(exclude
, "all")))
3988 * We need to exclude this attribute for security reasons; we require the
3989 * job-id attribute regardless of the security settings for IPP
3992 * The job-printer-uri attribute is handled by copy_job_attrs().
3994 * Subscription attribute security is handled by copy_subscription_attrs().
3997 if (strcmp(fromattr
->name
, "job-id"))
4001 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4004 * Don't send collection attributes by default to IPP/1.x clients
4005 * since many do not support collections. Also don't send
4006 * media-col-database unless specifically requested by the client.
4009 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4011 (to
->request
.status
.version
[0] == 1 ||
4012 !strcmp(fromattr
->name
, "media-col-database")))
4015 ippCopyAttribute(to
, fromattr
, quickcopy
);
4022 * 'copy_banner()' - Copy a banner file to the requests directory for the
4026 static int /* O - Size of banner file in kbytes */
4027 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4028 cupsd_job_t
*job
, /* I - Job information */
4029 const char *name
) /* I - Name of banner */
4031 int i
; /* Looping var */
4032 int kbytes
; /* Size of banner file in kbytes */
4033 char filename
[1024]; /* Job filename */
4034 cupsd_banner_t
*banner
; /* Pointer to banner */
4035 cups_file_t
*in
; /* Input file */
4036 cups_file_t
*out
; /* Output file */
4037 int ch
; /* Character from file */
4038 char attrname
[255], /* Name of attribute */
4039 *s
; /* Pointer into name */
4040 ipp_attribute_t
*attr
; /* Attribute */
4043 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4044 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4045 con
, con
? con
->number
: -1, job
, job
->id
,
4046 name
? name
: "(null)");
4049 * Find the banner; return if not found or "none"...
4052 if (!name
|| !strcmp(name
, "none") ||
4053 (banner
= cupsdFindBanner(name
)) == NULL
)
4057 * Open the banner and job files...
4060 if (add_file(con
, job
, banner
->filetype
, 0))
4063 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4065 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4067 cupsdLogMessage(CUPSD_LOG_ERROR
,
4068 "Unable to create banner job file %s - %s",
4069 filename
, strerror(errno
));
4074 fchmod(cupsFileNumber(out
), 0640);
4075 fchown(cupsFileNumber(out
), RunUser
, Group
);
4078 * Try the localized banner file under the subdirectory...
4081 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4083 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4086 * Convert ll-cc to ll_CC...
4090 attrname
[3] = (char)toupper(attrname
[3] & 255);
4091 attrname
[4] = (char)toupper(attrname
[4] & 255);
4094 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4097 if (access(filename
, 0) && strlen(attrname
) > 2)
4100 * Wasn't able to find "ll_CC" locale file; try the non-national
4101 * localization banner directory.
4106 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4110 if (access(filename
, 0))
4113 * Use the non-localized banner file.
4116 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4119 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4123 cupsdLogMessage(CUPSD_LOG_ERROR
,
4124 "Unable to open banner template file %s - %s",
4125 filename
, strerror(errno
));
4131 * Parse the file to the end...
4134 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4138 * Get an attribute name...
4141 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4142 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4144 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4154 * Ignore { followed by stuff that is not an attribute name...
4157 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4162 * See if it is defined...
4165 if (attrname
[0] == '?')
4170 if (!strcmp(s
, "printer-name"))
4172 cupsFilePuts(out
, job
->dest
);
4175 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4178 * See if we have a leading question mark...
4181 if (attrname
[0] != '?')
4184 * Nope, write to file as-is; probably a PostScript procedure...
4187 cupsFilePrintf(out
, "{%s}", attrname
);
4194 * Output value(s)...
4197 for (i
= 0; i
< attr
->num_values
; i
++)
4200 cupsFilePutChar(out
, ',');
4202 switch (attr
->value_tag
)
4204 case IPP_TAG_INTEGER
:
4206 if (!strncmp(s
, "time-at-", 8))
4208 struct timeval tv
; /* Time value */
4210 tv
.tv_sec
= attr
->values
[i
].integer
;
4213 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4216 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4219 case IPP_TAG_BOOLEAN
:
4220 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4223 case IPP_TAG_NOVALUE
:
4224 cupsFilePuts(out
, "novalue");
4227 case IPP_TAG_RANGE
:
4228 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4229 attr
->values
[i
].range
.upper
);
4232 case IPP_TAG_RESOLUTION
:
4233 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4234 attr
->values
[i
].resolution
.yres
,
4235 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4240 case IPP_TAG_STRING
:
4243 case IPP_TAG_KEYWORD
:
4244 case IPP_TAG_CHARSET
:
4245 case IPP_TAG_LANGUAGE
:
4246 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4249 * Need to quote strings for PS banners...
4254 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4256 if (*p
== '(' || *p
== ')' || *p
== '\\')
4258 cupsFilePutChar(out
, '\\');
4259 cupsFilePutChar(out
, *p
);
4261 else if (*p
< 32 || *p
> 126)
4262 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4264 cupsFilePutChar(out
, *p
);
4268 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4272 break; /* anti-compiler-warning-code */
4276 else if (ch
== '\\') /* Quoted char */
4278 ch
= cupsFileGetChar(in
);
4280 if (ch
!= '{') /* Only do special handling for \{ */
4281 cupsFilePutChar(out
, '\\');
4283 cupsFilePutChar(out
, ch
);
4286 cupsFilePutChar(out
, ch
);
4290 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4292 job
->koctets
+= kbytes
;
4294 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4295 attr
->values
[0].integer
+= kbytes
;
4304 * 'copy_file()' - Copy a PPD file or interface script...
4307 static int /* O - 0 = success, -1 = error */
4308 copy_file(const char *from
, /* I - Source file */
4309 const char *to
) /* I - Destination file */
4311 cups_file_t
*src
, /* Source file */
4312 *dst
; /* Destination file */
4313 int bytes
; /* Bytes to read/write */
4314 char buffer
[2048]; /* Copy buffer */
4317 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4320 * Open the source and destination file for a copy...
4323 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4326 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4333 * Copy the source file to the destination...
4336 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4337 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4345 * Close both files and return...
4350 return (cupsFileClose(dst
));
4355 * 'copy_model()' - Copy a PPD model file, substituting default values
4359 static int /* O - 0 = success, -1 = error */
4360 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4361 const char *from
, /* I - Source file */
4362 const char *to
) /* I - Destination file */
4364 fd_set input
; /* select() input set */
4365 struct timeval timeout
; /* select() timeout */
4366 int maxfd
; /* Max file descriptor for select() */
4367 char tempfile
[1024]; /* Temporary PPD file */
4368 int tempfd
; /* Temporary PPD file descriptor */
4369 int temppid
; /* Process ID of cups-driverd */
4370 int temppipe
[2]; /* Temporary pipes */
4371 char *argv
[4], /* Command-line arguments */
4372 *envp
[MAX_ENV
]; /* Environment */
4373 cups_file_t
*src
, /* Source file */
4374 *dst
; /* Destination file */
4375 ppd_file_t
*ppd
; /* PPD file */
4376 int bytes
, /* Bytes from pipe */
4377 total
; /* Total bytes from pipe */
4378 char buffer
[2048]; /* Copy buffer */
4379 int i
; /* Looping var */
4380 char option
[PPD_MAX_NAME
], /* Option name */
4381 choice
[PPD_MAX_NAME
]; /* Choice name */
4382 ppd_size_t
*size
; /* Default size */
4383 int num_defaults
; /* Number of default options */
4384 cups_option_t
*defaults
; /* Default options */
4385 char cups_protocol
[PPD_MAX_LINE
];
4386 /* cupsProtocol attribute */
4389 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4390 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4394 * Run cups-driverd to get the PPD file...
4397 argv
[0] = "cups-driverd";
4399 argv
[2] = (char *)from
;
4402 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4404 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4405 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4406 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4407 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4410 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4411 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4413 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4414 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4425 * Wait up to 30 seconds for the PPD file to be copied...
4430 if (temppipe
[0] > CGIPipes
[0])
4431 maxfd
= temppipe
[0] + 1;
4433 maxfd
= CGIPipes
[0] + 1;
4438 * See if we have data ready...
4442 FD_SET(temppipe
[0], &input
);
4443 FD_SET(CGIPipes
[0], &input
);
4445 timeout
.tv_sec
= 30;
4446 timeout
.tv_usec
= 0;
4448 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4458 * We have timed out...
4464 if (FD_ISSET(temppipe
[0], &input
))
4467 * Read the PPD file from the pipe, and write it to the PPD file.
4470 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4472 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4481 if (FD_ISSET(CGIPipes
[0], &input
))
4491 * No data from cups-deviced...
4494 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4500 * Open the source file for a copy...
4503 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4510 * Read the source file and see what page sizes are supported...
4513 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4521 * Open the destination (if possible) and set the default options...
4526 cups_protocol
[0] = '\0';
4528 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4531 * Read all of the default lines from the old PPD...
4534 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4535 if (!strncmp(buffer
, "*Default", 8))
4538 * Add the default option...
4541 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4542 choice
, sizeof(choice
)))
4544 ppd_option_t
*ppdo
; /* PPD option */
4548 * Only add the default if the default hasn't already been
4549 * set and the choice exists in the new PPD...
4552 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4553 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4554 ppdFindChoice(ppdo
, choice
))
4555 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4559 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4560 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4564 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4567 * Add the default media sizes...
4570 num_defaults
= cupsAddOption("PageSize", size
->name
,
4571 num_defaults
, &defaults
);
4572 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4573 num_defaults
, &defaults
);
4574 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4575 num_defaults
, &defaults
);
4576 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4577 num_defaults
, &defaults
);
4583 * Open the destination file for a copy...
4586 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4588 cupsFreeOptions(num_defaults
, defaults
);
4595 * Copy the source file to the destination...
4598 cupsFileRewind(src
);
4600 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4602 if (!strncmp(buffer
, "*Default", 8))
4605 * Check for an previous default option choice...
4608 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4609 choice
, sizeof(choice
)))
4611 const char *val
; /* Default option value */
4614 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4617 * Substitute the previous choice...
4620 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4625 cupsFilePrintf(dst
, "%s\n", buffer
);
4628 if (cups_protocol
[0])
4629 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4631 cupsFreeOptions(num_defaults
, defaults
);
4634 * Close both files and return...
4641 return (cupsdCloseCreatedConfFile(dst
, to
));
4646 * 'copy_job_attrs()' - Copy job attributes.
4650 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4651 cupsd_job_t
*job
, /* I - Job */
4652 cups_array_t
*ra
, /* I - Requested attributes array */
4653 cups_array_t
*exclude
) /* I - Private attributes array */
4655 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4659 * Send the requested attributes for each job...
4662 if (!cupsArrayFind(exclude
, "all"))
4664 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4665 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4666 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4667 "number-of-documents", job
->num_files
);
4669 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4670 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4671 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4672 "job-media-progress", job
->progress
);
4674 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4675 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4677 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4678 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4680 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4681 "job-more-info", NULL
, job_uri
);
4684 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4685 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4686 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4687 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4688 job
->num_files
> 0);
4690 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4691 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4692 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4693 "job-printer-up-time", time(NULL
));
4696 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4698 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4699 con
->clientname
, con
->clientport
,
4700 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4703 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4704 "job-printer-uri", NULL
, job_uri
);
4707 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4709 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4710 con
->clientname
, con
->clientport
, "/jobs/%d",
4712 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4713 "job-uri", NULL
, job_uri
);
4718 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4723 * Generate attributes from the job structure...
4726 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4727 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4729 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4730 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4732 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4733 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4735 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4736 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4738 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4739 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4741 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4743 switch (job
->state_value
)
4745 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4747 case IPP_JSTATE_ABORTED
:
4748 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4750 case IPP_JSTATE_CANCELED
:
4751 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4753 case IPP_JSTATE_COMPLETED
:
4754 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4759 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4760 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4762 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4763 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4769 * 'copy_printer_attrs()' - Copy printer attributes.
4774 cupsd_client_t
*con
, /* I - Client connection */
4775 cupsd_printer_t
*printer
, /* I - Printer */
4776 cups_array_t
*ra
) /* I - Requested attributes array */
4778 char printer_uri
[HTTP_MAX_URI
];
4780 char printer_icons
[HTTP_MAX_URI
];
4782 time_t curtime
; /* Current time */
4783 int i
; /* Looping var */
4787 * Copy the printer attributes to the response using requested-attributes
4788 * and document-format attributes that may be provided by the client.
4791 curtime
= time(NULL
);
4793 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4794 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4795 "marker-change-time", printer
->marker_time
);
4797 if (printer
->num_printers
> 0 &&
4798 (!ra
|| cupsArrayFind(ra
, "member-uris")))
4800 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4801 cupsd_printer_t
*p2
; /* Printer in class */
4802 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4805 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
4806 IPP_TAG_URI
, "member-uris",
4807 printer
->num_printers
, NULL
,
4810 for (i
= 0; i
< printer
->num_printers
; i
++)
4812 p2
= printer
->printers
[i
];
4814 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
4815 IPP_TAG_URI
)) != NULL
)
4816 member_uris
->values
[i
].string
.text
=
4817 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4820 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
4821 sizeof(printer_uri
), "ipp", NULL
, con
->clientname
,
4823 (p2
->type
& CUPS_PRINTER_CLASS
) ?
4824 "/classes/%s" : "/printers/%s", p2
->name
);
4825 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
4831 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4832 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4833 "printer-alert", NULL
, printer
->alert
);
4835 if (printer
->alert_description
&&
4836 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4837 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4838 "printer-alert-description", NULL
,
4839 printer
->alert_description
);
4841 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4842 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4843 ippTimeToDate(curtime
));
4845 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4846 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4848 if (printer
->reg_name
)
4849 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4850 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4852 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4853 "printer-dns-sd-name", 0);
4855 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4857 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4858 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4859 "printer-error-policy", NULL
, printer
->error_policy
);
4861 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
4863 static const char * const errors
[] =/* printer-error-policy-supported values */
4866 "retry-current-job",
4871 if (printer
->type
& CUPS_PRINTER_CLASS
)
4872 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4873 "printer-error-policy-supported", NULL
, "retry-current-job");
4875 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4876 "printer-error-policy-supported",
4877 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
4880 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
4882 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_icons
, sizeof(printer_icons
),
4883 "http", NULL
, con
->clientname
, con
->clientport
,
4884 "/icons/%s.png", printer
->name
);
4885 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons",
4886 NULL
, printer_icons
);
4887 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", printer_icons
);
4890 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4891 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
4893 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4894 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
4896 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
4898 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4899 "http", NULL
, con
->clientname
, con
->clientport
,
4900 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4901 "/classes/%s" : "/printers/%s", printer
->name
);
4902 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4903 "printer-more-info", NULL
, printer_uri
);
4906 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4907 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4908 "printer-op-policy", NULL
, printer
->op_policy
);
4910 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4911 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4914 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4915 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4916 "printer-state-change-time", printer
->state_time
);
4918 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4919 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4920 "printer-state-message", NULL
, printer
->state_message
);
4922 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4923 add_printer_state_reasons(con
, printer
);
4925 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4927 cups_ptype_t type
; /* printer-type value */
4930 * Add the CUPS-specific printer-type attribute...
4933 type
= printer
->type
;
4935 if (printer
== DefaultPrinter
)
4936 type
|= CUPS_PRINTER_DEFAULT
;
4938 if (!printer
->accepting
)
4939 type
|= CUPS_PRINTER_REJECTING
;
4941 if (!printer
->shared
)
4942 type
|= CUPS_PRINTER_NOT_SHARED
;
4944 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
4947 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4948 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4949 "printer-up-time", curtime
);
4951 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
4953 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4954 "ipp", NULL
, con
->clientname
, con
->clientport
,
4955 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4956 "/classes/%s" : "/printers/%s", printer
->name
);
4957 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4958 "printer-uri-supported", NULL
, printer_uri
);
4959 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4963 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4964 add_queued_job_count(con
, printer
);
4966 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
4967 if (printer
->ppd_attrs
)
4968 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
4969 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
4974 * 'copy_subscription_attrs()' - Copy subscription attributes.
4978 copy_subscription_attrs(
4979 cupsd_client_t
*con
, /* I - Client connection */
4980 cupsd_subscription_t
*sub
, /* I - Subscription */
4981 cups_array_t
*ra
, /* I - Requested attributes array */
4982 cups_array_t
*exclude
) /* I - Private attributes array */
4984 ipp_attribute_t
*attr
; /* Current attribute */
4985 char printer_uri
[HTTP_MAX_URI
];
4987 int count
; /* Number of events */
4988 unsigned mask
; /* Current event mask */
4989 const char *name
; /* Current event name */
4992 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4993 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
4994 con
, sub
, ra
, exclude
);
4997 * Copy the subscription attributes to the response using the
4998 * requested-attributes attribute that may be provided by the client.
5001 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5003 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5004 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5006 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5008 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5011 * Simple event list...
5014 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5015 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5016 "notify-events", NULL
, name
);
5021 * Complex event list...
5024 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5025 if (sub
->mask
& mask
)
5028 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5029 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5030 "notify-events", count
, NULL
, NULL
);
5032 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5033 if (sub
->mask
& mask
)
5035 attr
->values
[count
].string
.text
=
5036 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5043 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5044 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5045 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5046 "notify-lease-duration", sub
->lease
);
5048 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5049 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5050 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5051 "notify-recipient-uri", NULL
, sub
->recipient
);
5052 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5053 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5054 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5055 "notify-pull-method", NULL
, "ippget");
5057 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5058 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5059 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5060 "notify-subscriber-user-name", NULL
, sub
->owner
);
5062 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5063 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5064 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5065 "notify-time-interval", sub
->interval
);
5067 if (sub
->user_data_len
> 0 &&
5068 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5069 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5070 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5071 sub
->user_data
, sub
->user_data_len
);
5074 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5075 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5076 "notify-job-id", sub
->job
->id
);
5078 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5080 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5081 "ipp", NULL
, con
->clientname
, con
->clientport
,
5082 "/printers/%s", sub
->dest
->name
);
5083 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5084 "notify-printer-uri", NULL
, printer_uri
);
5087 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5088 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5089 "notify-subscription-id", sub
->id
);
5094 * 'create_job()' - Print a file to a printer or class.
5098 create_job(cupsd_client_t
*con
, /* I - Client connection */
5099 ipp_attribute_t
*uri
) /* I - Printer URI */
5101 int i
; /* Looping var */
5102 cupsd_printer_t
*printer
; /* Printer */
5103 cupsd_job_t
*job
; /* New job */
5104 static const char * const forbidden_attrs
[] =
5105 { /* List of forbidden attributes */
5109 "document-natural-language"
5113 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5114 con
->number
, uri
->values
[0].string
.text
);
5117 * Is the destination valid?
5120 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5126 send_ipp_status(con
, IPP_NOT_FOUND
,
5127 _("The printer or class does not exist."));
5132 * Check for invalid Create-Job attributes and log a warning or error depending
5133 * on whether cupsd is running in "strict conformance" mode...
5137 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5139 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5141 if (StrictConformance
)
5143 send_ipp_status(con
, IPP_BAD_REQUEST
,
5144 _("The '%s' operation attribute cannot be supplied in a "
5145 "Create-Job request."), forbidden_attrs
[i
]);
5149 cupsdLogMessage(CUPSD_LOG_WARN
,
5150 "Unexpected '%s' operation attribute in a Create-Job "
5151 "request.", forbidden_attrs
[i
]);
5155 * Create the job object...
5158 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5161 job
->pending_timeout
= 1;
5164 * Save and log the job...
5167 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5168 job
->dest
, job
->username
);
5173 * 'create_requested_array()' - Create an array for the requested-attributes.
5176 static cups_array_t
* /* O - Array of attributes or NULL */
5177 create_requested_array(ipp_t
*request
) /* I - IPP request */
5179 cups_array_t
*ra
; /* Requested attributes array */
5183 * Create the array for standard attributes...
5186 ra
= ippCreateRequestedArray(request
);
5189 * Add CUPS defaults as needed...
5192 if (cupsArrayFind(ra
, "printer-defaults"))
5195 * Include user-set defaults...
5198 char *name
; /* Option name */
5200 cupsArrayRemove(ra
, "printer-defaults");
5202 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5204 name
= (char *)cupsArrayNext(CommonDefaults
))
5205 if (!cupsArrayFind(ra
, name
))
5206 cupsArrayAdd(ra
, name
);
5214 * 'create_subscriptions()' - Create one or more notification subscriptions.
5218 create_subscriptions(
5219 cupsd_client_t
*con
, /* I - Client connection */
5220 ipp_attribute_t
*uri
) /* I - Printer URI */
5222 http_status_t status
; /* Policy status */
5223 int i
; /* Looping var */
5224 ipp_attribute_t
*attr
; /* Current attribute */
5225 cups_ptype_t dtype
; /* Destination type (printer/class) */
5226 char scheme
[HTTP_MAX_URI
],
5227 /* Scheme portion of URI */
5228 userpass
[HTTP_MAX_URI
],
5229 /* Username portion of URI */
5231 /* Host portion of URI */
5232 resource
[HTTP_MAX_URI
];
5233 /* Resource portion of URI */
5234 int port
; /* Port portion of URI */
5235 cupsd_printer_t
*printer
; /* Printer/class */
5236 cupsd_job_t
*job
; /* Job */
5237 int jobid
; /* Job ID */
5238 cupsd_subscription_t
*sub
; /* Subscription object */
5239 const char *username
, /* requesting-user-name or
5240 authenticated username */
5241 *recipient
, /* notify-recipient-uri */
5242 *pullmethod
; /* notify-pull-method */
5243 ipp_attribute_t
*user_data
; /* notify-user-data */
5244 int interval
, /* notify-time-interval */
5245 lease
; /* notify-lease-duration */
5246 unsigned mask
; /* notify-events */
5247 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5248 *notify_lease
; /* notify-lease-duration(-default) */
5252 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5254 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5255 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5256 attr
->value_tag
, attr
->name
);
5258 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5263 * Is the destination valid?
5266 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5268 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5269 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5270 sizeof(host
), &port
, resource
, sizeof(resource
));
5272 if (!strcmp(resource
, "/"))
5274 dtype
= (cups_ptype_t
)0;
5277 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5279 dtype
= (cups_ptype_t
)0;
5282 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5284 dtype
= CUPS_PRINTER_CLASS
;
5287 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5293 send_ipp_status(con
, IPP_NOT_FOUND
,
5294 _("The printer or class does not exist."));
5304 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5307 send_http_error(con
, status
, printer
);
5311 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5313 send_http_error(con
, status
, NULL
);
5318 * Get the user that is requesting the subscription...
5321 username
= get_username(con
);
5324 * Find the first subscription group attribute; return if we have
5328 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5329 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5334 send_ipp_status(con
, IPP_BAD_REQUEST
,
5335 _("No subscription attributes in request."));
5340 * Process the subscription attributes in the request...
5343 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5351 lease
= DefaultLeaseDuration
;
5353 mask
= CUPSD_EVENT_NONE
;
5357 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5359 notify_lease
= ippFindAttribute(printer
->attrs
,
5360 "notify-lease-duration-default",
5364 lease
= notify_lease
->values
[0].integer
;
5368 notify_events
= NULL
;
5369 notify_lease
= NULL
;
5372 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5374 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5375 attr
->value_tag
== IPP_TAG_URI
)
5378 * Validate the recipient scheme against the ServerBin/notifier
5382 char notifier
[1024]; /* Notifier filename */
5385 recipient
= attr
->values
[0].string
.text
;
5387 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5388 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5389 host
, sizeof(host
), &port
,
5390 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5392 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5393 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5394 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5395 "notify-status-code", IPP_URI_SCHEME
);
5399 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5401 if (access(notifier
, X_OK
))
5403 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5404 _("notify-recipient-uri URI \"%s\" uses unknown "
5405 "scheme."), recipient
);
5406 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5407 "notify-status-code", IPP_URI_SCHEME
);
5411 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5413 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5414 _("notify-recipient-uri URI \"%s\" is already used."),
5416 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5417 "notify-status-code", IPP_ATTRIBUTES
);
5421 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5422 attr
->value_tag
== IPP_TAG_KEYWORD
)
5424 pullmethod
= attr
->values
[0].string
.text
;
5426 if (strcmp(pullmethod
, "ippget"))
5428 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5429 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5430 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5431 "notify-status-code", IPP_ATTRIBUTES
);
5435 else if (!strcmp(attr
->name
, "notify-charset") &&
5436 attr
->value_tag
== IPP_TAG_CHARSET
&&
5437 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5438 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5440 send_ipp_status(con
, IPP_CHARSET
,
5441 _("Character set \"%s\" not supported."),
5442 attr
->values
[0].string
.text
);
5445 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5446 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5447 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5449 send_ipp_status(con
, IPP_CHARSET
,
5450 _("Language \"%s\" not supported."),
5451 attr
->values
[0].string
.text
);
5454 else if (!strcmp(attr
->name
, "notify-user-data") &&
5455 attr
->value_tag
== IPP_TAG_STRING
)
5457 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5459 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5460 _("The notify-user-data value is too large "
5461 "(%d > 63 octets)."),
5462 attr
->values
[0].unknown
.length
);
5468 else if (!strcmp(attr
->name
, "notify-events") &&
5469 attr
->value_tag
== IPP_TAG_KEYWORD
)
5470 notify_events
= attr
;
5471 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5472 attr
->value_tag
== IPP_TAG_INTEGER
)
5473 lease
= attr
->values
[0].integer
;
5474 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5475 attr
->value_tag
== IPP_TAG_INTEGER
)
5476 interval
= attr
->values
[0].integer
;
5477 else if (!strcmp(attr
->name
, "notify-job-id") &&
5478 attr
->value_tag
== IPP_TAG_INTEGER
)
5479 jobid
= attr
->values
[0].integer
;
5486 for (i
= 0; i
< notify_events
->num_values
; i
++)
5487 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5491 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5493 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5494 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5495 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5497 if (!recipient
&& !pullmethod
)
5500 if (mask
== CUPSD_EVENT_NONE
)
5503 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5505 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5508 send_ipp_status(con
, IPP_BAD_REQUEST
,
5509 _("notify-events not specified."));
5514 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5516 cupsdLogMessage(CUPSD_LOG_INFO
,
5517 "create_subscriptions: Limiting notify-lease-duration to "
5520 lease
= MaxLeaseDuration
;
5525 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5527 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5535 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5537 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5538 _("There are too many subscriptions."));
5543 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5546 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5547 "Added subscription #%d for printer \"%s\".",
5548 sub
->id
, printer
->name
);
5550 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5553 sub
->interval
= interval
;
5555 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5557 cupsdSetString(&sub
->owner
, username
);
5561 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5562 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5563 (size_t)sub
->user_data_len
);
5566 ippAddSeparator(con
->response
);
5567 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5568 "notify-subscription-id", sub
->id
);
5570 con
->response
->request
.status
.status_code
= IPP_OK
;
5576 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5581 * 'delete_printer()' - Remove a printer or class from the system.
5585 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5586 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5588 http_status_t status
; /* Policy status */
5589 cups_ptype_t dtype
; /* Destination type (printer/class) */
5590 cupsd_printer_t
*printer
; /* Printer/class */
5591 char filename
[1024]; /* Script/PPD filename */
5594 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5595 con
->number
, uri
->values
[0].string
.text
);
5598 * Do we have a valid URI?
5601 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5607 send_ipp_status(con
, IPP_NOT_FOUND
,
5608 _("The printer or class does not exist."));
5616 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5618 send_http_error(con
, status
, NULL
);
5623 * Remove old jobs...
5626 cupsdCancelJobs(printer
->name
, NULL
, 1);
5629 * Remove old subscriptions and send a "deleted printer" event...
5632 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5633 "%s \"%s\" deleted by \"%s\".",
5634 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5635 printer
->name
, get_username(con
));
5637 cupsdExpireSubscriptions(printer
, NULL
);
5640 * Remove any old PPD or script files...
5643 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5646 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s.O", ServerRoot
,
5650 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5653 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
5657 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
5660 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
5664 * Unregister color profiles...
5667 cupsdUnregisterColor(printer
);
5669 if (dtype
& CUPS_PRINTER_CLASS
)
5671 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5672 printer
->name
, get_username(con
));
5674 cupsdDeletePrinter(printer
, 0);
5675 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5679 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5680 printer
->name
, get_username(con
));
5682 if (cupsdDeletePrinter(printer
, 0))
5683 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5685 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5688 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
5691 * Return with no errors...
5694 con
->response
->request
.status
.status_code
= IPP_OK
;
5699 * 'get_default()' - Get the default destination.
5703 get_default(cupsd_client_t
*con
) /* I - Client connection */
5705 http_status_t status
; /* Policy status */
5706 cups_array_t
*ra
; /* Requested attributes array */
5709 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
5715 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5717 send_http_error(con
, status
, NULL
);
5723 ra
= create_requested_array(con
->request
);
5725 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5727 cupsArrayDelete(ra
);
5729 con
->response
->request
.status
.status_code
= IPP_OK
;
5732 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
5737 * 'get_devices()' - Get the list of available devices on the local system.
5741 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5743 http_status_t status
; /* Policy status */
5744 ipp_attribute_t
*limit
, /* limit attribute */
5745 *timeout
, /* timeout attribute */
5746 *requested
, /* requested-attributes attribute */
5747 *exclude
, /* exclude-schemes attribute */
5748 *include
; /* include-schemes attribute */
5749 char command
[1024], /* cups-deviced command */
5750 options
[2048], /* Options to pass to command */
5752 /* String for requested attributes */
5754 /* String for excluded schemes */
5756 /* String for included schemes */
5759 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
5765 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5767 send_http_error(con
, status
, NULL
);
5772 * Run cups-deviced command with the given options...
5775 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5776 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
5777 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5779 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
5780 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
5783 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5785 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5788 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
5790 exclude_str
[0] = '\0';
5793 url_encode_attr(include
, include_str
, sizeof(include_str
));
5795 include_str
[0] = '\0';
5797 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5798 snprintf(options
, sizeof(options
),
5799 "%d+%d+%d+%d+%s%s%s%s%s",
5800 con
->request
->request
.op
.request_id
,
5801 limit
? limit
->values
[0].integer
: 0,
5802 timeout
? timeout
->values
[0].integer
: 15,
5805 exclude_str
[0] ? "%20" : "", exclude_str
,
5806 include_str
[0] ? "%20" : "", include_str
);
5808 if (cupsdSendCommand(con
, command
, options
, 1))
5811 * Command started successfully, don't send an IPP response here...
5814 ippDelete(con
->response
);
5815 con
->response
= NULL
;
5820 * Command failed, return "internal error" so the user knows something
5824 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5825 _("cups-deviced failed to execute."));
5831 * 'get_document()' - Get a copy of a job file.
5835 get_document(cupsd_client_t
*con
, /* I - Client connection */
5836 ipp_attribute_t
*uri
) /* I - Job URI */
5838 http_status_t status
; /* Policy status */
5839 ipp_attribute_t
*attr
; /* Current attribute */
5840 int jobid
; /* Job ID */
5841 int docnum
; /* Document number */
5842 cupsd_job_t
*job
; /* Current job */
5843 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
5844 username
[HTTP_MAX_URI
], /* Username portion of URI */
5845 host
[HTTP_MAX_URI
], /* Host portion of URI */
5846 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5847 int port
; /* Port portion of URI */
5848 char filename
[1024], /* Filename for document */
5849 format
[1024]; /* Format for document */
5852 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5853 con
->number
, uri
->values
[0].string
.text
);
5856 * See if we have a job URI or a printer URI...
5859 if (!strcmp(uri
->name
, "printer-uri"))
5862 * Got a printer URI; see if we also have a job-id attribute...
5865 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5866 IPP_TAG_INTEGER
)) == NULL
)
5868 send_ipp_status(con
, IPP_BAD_REQUEST
,
5869 _("Got a printer-uri attribute but no job-id."));
5873 jobid
= attr
->values
[0].integer
;
5878 * Got a job URI; parse it to get the job ID...
5881 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5882 sizeof(scheme
), username
, sizeof(username
), host
,
5883 sizeof(host
), &port
, resource
, sizeof(resource
));
5885 if (strncmp(resource
, "/jobs/", 6))
5891 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
5892 uri
->values
[0].string
.text
);
5896 jobid
= atoi(resource
+ 6);
5900 * See if the job exists...
5903 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5906 * Nope - return a "not found" error...
5909 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
5917 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
5918 job
->username
)) != HTTP_OK
)
5920 send_http_error(con
, status
, NULL
);
5925 * Get the document number...
5928 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5929 IPP_TAG_INTEGER
)) == NULL
)
5931 send_ipp_status(con
, IPP_BAD_REQUEST
,
5932 _("Missing document-number attribute."));
5936 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5937 attr
->num_values
> 1)
5939 send_ipp_status(con
, IPP_NOT_FOUND
,
5940 _("Document #%d does not exist in job #%d."), docnum
,
5945 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5947 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5949 cupsdLogMessage(CUPSD_LOG_ERROR
,
5950 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5952 send_ipp_status(con
, IPP_NOT_FOUND
,
5953 _("Unable to open document #%d in job #%d."), docnum
,
5958 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5962 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
5963 job
->filetypes
[docnum
- 1]->type
);
5965 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
5967 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
5969 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
5970 IPP_TAG_NAME
)) != NULL
)
5971 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
5972 NULL
, attr
->values
[0].string
.text
);
5977 * 'get_job_attrs()' - Get job attributes.
5981 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5982 ipp_attribute_t
*uri
) /* I - Job URI */
5984 http_status_t status
; /* Policy status */
5985 ipp_attribute_t
*attr
; /* Current attribute */
5986 int jobid
; /* Job ID */
5987 cupsd_job_t
*job
; /* Current job */
5988 cupsd_printer_t
*printer
; /* Current printer */
5989 cupsd_policy_t
*policy
; /* Current security policy */
5990 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5991 username
[HTTP_MAX_URI
], /* Username portion of URI */
5992 host
[HTTP_MAX_URI
], /* Host portion of URI */
5993 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5994 int port
; /* Port portion of URI */
5995 cups_array_t
*ra
, /* Requested attributes array */
5996 *exclude
; /* Private attributes array */
5999 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6000 con
->number
, uri
->values
[0].string
.text
);
6003 * See if we have a job URI or a printer URI...
6006 if (!strcmp(uri
->name
, "printer-uri"))
6009 * Got a printer URI; see if we also have a job-id attribute...
6012 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6013 IPP_TAG_INTEGER
)) == NULL
)
6015 send_ipp_status(con
, IPP_BAD_REQUEST
,
6016 _("Got a printer-uri attribute but no job-id."));
6020 jobid
= attr
->values
[0].integer
;
6025 * Got a job URI; parse it to get the job ID...
6028 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6029 sizeof(scheme
), username
, sizeof(username
), host
,
6030 sizeof(host
), &port
, resource
, sizeof(resource
));
6032 if (strncmp(resource
, "/jobs/", 6))
6038 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6039 uri
->values
[0].string
.text
);
6043 jobid
= atoi(resource
+ 6);
6047 * See if the job exists...
6050 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6053 * Nope - return a "not found" error...
6056 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6064 if ((printer
= job
->printer
) == NULL
)
6065 printer
= cupsdFindDest(job
->dest
);
6068 policy
= printer
->op_policy_ptr
;
6070 policy
= DefaultPolicyPtr
;
6072 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6074 send_http_error(con
, status
, NULL
);
6078 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6081 * Copy attributes...
6086 ra
= create_requested_array(con
->request
);
6087 copy_job_attrs(con
, job
, ra
, exclude
);
6088 cupsArrayDelete(ra
);
6090 con
->response
->request
.status
.status_code
= IPP_OK
;
6095 * 'get_jobs()' - Get a list of jobs for the specified printer.
6099 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6100 ipp_attribute_t
*uri
) /* I - Printer URI */
6102 http_status_t status
; /* Policy status */
6103 ipp_attribute_t
*attr
; /* Current attribute */
6104 const char *dest
; /* Destination */
6105 cups_ptype_t dtype
; /* Destination type (printer/class) */
6106 cups_ptype_t dmask
; /* Destination type mask */
6107 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6108 username
[HTTP_MAX_URI
], /* Username portion of URI */
6109 host
[HTTP_MAX_URI
], /* Host portion of URI */
6110 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6111 int port
; /* Port portion of URI */
6112 int job_comparison
; /* Job comparison */
6113 ipp_jstate_t job_state
; /* job-state value */
6114 int first_job_id
= 1, /* First job ID */
6115 first_index
= 1, /* First index */
6116 current_index
= 0; /* Current index */
6117 int limit
= 0; /* Maximum number of jobs to return */
6118 int count
; /* Number of jobs that match */
6119 int need_load_job
= 0; /* Do we need to load the job? */
6120 const char *job_attr
; /* Job attribute requested */
6121 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6122 cupsd_job_t
*job
; /* Current job pointer */
6123 cupsd_printer_t
*printer
; /* Printer */
6124 cups_array_t
*list
; /* Which job list... */
6125 int delete_list
= 0; /* Delete the list afterwards? */
6126 cups_array_t
*ra
, /* Requested attributes array */
6127 *exclude
; /* Private attributes array */
6128 cupsd_policy_t
*policy
; /* Current policy */
6131 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6132 uri
->values
[0].string
.text
);
6135 * Is the destination valid?
6138 if (strcmp(uri
->name
, "printer-uri"))
6140 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6144 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6145 sizeof(scheme
), username
, sizeof(username
), host
,
6146 sizeof(host
), &port
, resource
, sizeof(resource
));
6148 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6151 dtype
= (cups_ptype_t
)0;
6152 dmask
= (cups_ptype_t
)0;
6155 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6158 dtype
= (cups_ptype_t
)0;
6159 dmask
= CUPS_PRINTER_CLASS
;
6162 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6165 dtype
= CUPS_PRINTER_CLASS
;
6166 dmask
= CUPS_PRINTER_CLASS
;
6169 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6176 send_ipp_status(con
, IPP_NOT_FOUND
,
6177 _("The printer or class does not exist."));
6182 dtype
&= CUPS_PRINTER_CLASS
;
6183 dmask
= CUPS_PRINTER_CLASS
;
6191 policy
= printer
->op_policy_ptr
;
6193 policy
= DefaultPolicyPtr
;
6195 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6197 send_http_error(con
, status
, NULL
);
6201 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6204 * See if the "which-jobs" attribute have been specified...
6207 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6208 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6210 send_ipp_status(con
, IPP_CONFLICT
,
6211 _("The %s attribute cannot be provided with job-ids."),
6215 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6217 job_comparison
= -1;
6218 job_state
= IPP_JOB_STOPPED
;
6221 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6224 job_state
= IPP_JOB_CANCELED
;
6225 list
= cupsdGetCompletedJobs(printer
);
6228 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6231 job_state
= IPP_JOB_ABORTED
;
6232 list
= cupsdGetCompletedJobs(printer
);
6235 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6238 job_state
= IPP_JOB_PENDING
;
6241 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6244 job_state
= IPP_JOB_CANCELED
;
6245 list
= cupsdGetCompletedJobs(printer
);
6248 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6251 job_state
= IPP_JOB_PENDING
;
6254 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6257 job_state
= IPP_JOB_HELD
;
6260 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6263 job_state
= IPP_JOB_PROCESSING
;
6264 list
= PrintingJobs
;
6266 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6269 job_state
= IPP_JOB_STOPPED
;
6274 send_ipp_status(con
, IPP_ATTRIBUTES
,
6275 _("The which-jobs value \"%s\" is not supported."),
6276 attr
->values
[0].string
.text
);
6277 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6278 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6283 * See if they want to limit the number of jobs reported...
6286 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6290 send_ipp_status(con
, IPP_CONFLICT
,
6291 _("The %s attribute cannot be provided with job-ids."),
6296 limit
= attr
->values
[0].integer
;
6299 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6303 send_ipp_status(con
, IPP_CONFLICT
,
6304 _("The %s attribute cannot be provided with job-ids."),
6309 first_index
= attr
->values
[0].integer
;
6311 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6315 send_ipp_status(con
, IPP_CONFLICT
,
6316 _("The %s attribute cannot be provided with job-ids."),
6321 first_job_id
= attr
->values
[0].integer
;
6325 * See if we only want to see jobs for a specific user...
6328 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6330 send_ipp_status(con
, IPP_CONFLICT
,
6331 _("The %s attribute cannot be provided with job-ids."),
6335 else if (attr
&& attr
->values
[0].boolean
)
6336 strlcpy(username
, get_username(con
), sizeof(username
));
6340 ra
= create_requested_array(con
->request
);
6341 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6342 if (strcmp(job_attr
, "job-id") &&
6343 strcmp(job_attr
, "job-k-octets") &&
6344 strcmp(job_attr
, "job-media-progress") &&
6345 strcmp(job_attr
, "job-more-info") &&
6346 strcmp(job_attr
, "job-name") &&
6347 strcmp(job_attr
, "job-originating-user-name") &&
6348 strcmp(job_attr
, "job-preserved") &&
6349 strcmp(job_attr
, "job-printer-up-time") &&
6350 strcmp(job_attr
, "job-printer-uri") &&
6351 strcmp(job_attr
, "job-state") &&
6352 strcmp(job_attr
, "job-state-reasons") &&
6353 strcmp(job_attr
, "job-uri") &&
6354 strcmp(job_attr
, "time-at-completed") &&
6355 strcmp(job_attr
, "time-at-creation") &&
6356 strcmp(job_attr
, "number-of-documents"))
6362 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6365 * Limit expensive Get-Jobs for job history to 500 jobs...
6368 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6371 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6375 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6379 * OK, build a list of jobs for this printer...
6384 int i
; /* Looping var */
6386 for (i
= 0; i
< job_ids
->num_values
; i
++)
6388 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6392 if (i
< job_ids
->num_values
)
6394 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6395 job_ids
->values
[i
].integer
);
6399 for (i
= 0; i
< job_ids
->num_values
; i
++)
6401 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6403 if (need_load_job
&& !job
->attrs
)
6409 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6415 ippAddSeparator(con
->response
);
6417 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6418 job
->printer
->op_policy_ptr
:
6419 policy
, con
, job
->printer
,
6422 copy_job_attrs(con
, job
, ra
, exclude
);
6427 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6428 (limit
<= 0 || count
< limit
) && job
;
6429 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6432 * Filter out jobs that don't match...
6435 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6436 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6437 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6438 job
->username
, job
->state_value
, job
->attrs
);
6440 if (!job
->dest
|| !job
->username
)
6443 if (!job
->dest
|| !job
->username
)
6446 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6447 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6449 if ((job
->dtype
& dmask
) != dtype
&&
6450 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6453 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6454 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6455 (job_comparison
> 0 && job
->state_value
< job_state
))
6458 if (job
->id
< first_job_id
)
6462 if (current_index
< first_index
)
6465 if (need_load_job
&& !job
->attrs
)
6471 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6476 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6480 ippAddSeparator(con
->response
);
6484 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6485 job
->printer
->op_policy_ptr
:
6486 policy
, con
, job
->printer
,
6489 copy_job_attrs(con
, job
, ra
, exclude
);
6492 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6495 cupsArrayDelete(ra
);
6498 cupsArrayDelete(list
);
6500 con
->response
->request
.status
.status_code
= IPP_OK
;
6505 * 'get_notifications()' - Get events for a subscription.
6509 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6511 int i
, j
; /* Looping vars */
6512 http_status_t status
; /* Policy status */
6513 cupsd_subscription_t
*sub
; /* Subscription */
6514 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6515 *sequences
; /* notify-sequence-numbers */
6516 int min_seq
; /* Minimum sequence number */
6517 int interval
; /* Poll interval */
6520 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6524 * Get subscription attributes...
6527 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6529 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6534 send_ipp_status(con
, IPP_BAD_REQUEST
,
6535 _("Missing notify-subscription-ids attribute."));
6540 * Are the subscription IDs valid?
6543 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6545 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6548 * Bad subscription ID...
6551 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6552 ids
->values
[i
].integer
);
6560 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6562 con
, sub
->owner
)) != HTTP_OK
)
6564 send_http_error(con
, status
, sub
->dest
);
6569 * Check the subscription type and update the interval accordingly.
6572 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6575 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6577 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6583 * Tell the client to poll again in N seconds...
6587 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6588 "notify-get-interval", interval
);
6590 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6591 "printer-up-time", time(NULL
));
6594 * Copy the subscription event attributes to the response.
6597 con
->response
->request
.status
.status_code
=
6598 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6600 for (i
= 0; i
< ids
->num_values
; i
++)
6603 * Get the subscription and sequence number...
6606 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6608 if (sequences
&& i
< sequences
->num_values
)
6609 min_seq
= sequences
->values
[i
].integer
;
6614 * If we don't have any new events, nothing to do here...
6617 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
6621 * Otherwise copy all of the new events...
6624 if (sub
->first_event_id
> min_seq
)
6627 j
= min_seq
- sub
->first_event_id
;
6629 for (; j
< cupsArrayCount(sub
->events
); j
++)
6631 ippAddSeparator(con
->response
);
6633 copy_attrs(con
->response
,
6634 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
6635 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
6642 * 'get_ppd()' - Get a named PPD from the local system.
6646 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6647 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6649 http_status_t status
; /* Policy status */
6650 cupsd_printer_t
*dest
; /* Destination */
6651 cups_ptype_t dtype
; /* Destination type */
6654 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6655 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6657 if (!strcmp(uri
->name
, "ppd-name"))
6660 * Return a PPD file from cups-driverd...
6663 char command
[1024], /* cups-driverd command */
6664 options
[1024], /* Options to pass to command */
6665 ppd_name
[1024]; /* ppd-name */
6672 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6674 send_http_error(con
, status
, NULL
);
6679 * Run cups-driverd command with the given options...
6682 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6683 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6684 snprintf(options
, sizeof(options
), "get+%d+%s",
6685 con
->request
->request
.op
.request_id
, ppd_name
);
6687 if (cupsdSendCommand(con
, command
, options
, 0))
6690 * Command started successfully, don't send an IPP response here...
6693 ippDelete(con
->response
);
6694 con
->response
= NULL
;
6699 * Command failed, return "internal error" so the user knows something
6703 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6704 _("cups-driverd failed to execute."));
6707 else if (!strcmp(uri
->name
, "printer-uri") &&
6708 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6710 int i
; /* Looping var */
6711 char filename
[1024]; /* PPD filename */
6718 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6720 send_http_error(con
, status
, dest
);
6725 * See if we need the PPD for a class or remote printer...
6728 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6731 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6733 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6734 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6735 "printer-uri", NULL
, dest
->uri
);
6738 else if (dtype
& CUPS_PRINTER_CLASS
)
6740 for (i
= 0; i
< dest
->num_printers
; i
++)
6741 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
6743 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6744 dest
->printers
[i
]->name
);
6746 if (!access(filename
, 0))
6750 if (i
< dest
->num_printers
)
6751 dest
= dest
->printers
[i
];
6754 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6755 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6756 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6762 * Found the printer with the PPD file, now see if there is one...
6765 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6767 send_ipp_status(con
, IPP_NOT_FOUND
,
6768 _("The PPD file \"%s\" could not be opened: %s"),
6769 uri
->values
[0].string
.text
, strerror(errno
));
6773 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6777 con
->response
->request
.status
.status_code
= IPP_OK
;
6780 send_ipp_status(con
, IPP_NOT_FOUND
,
6781 _("The PPD file \"%s\" could not be found."),
6782 uri
->values
[0].string
.text
);
6787 * 'get_ppds()' - Get the list of PPD files on the local system.
6791 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6793 http_status_t status
; /* Policy status */
6794 ipp_attribute_t
*limit
, /* Limit attribute */
6795 *device
, /* ppd-device-id attribute */
6796 *language
, /* ppd-natural-language attribute */
6797 *make
, /* ppd-make attribute */
6798 *model
, /* ppd-make-and-model attribute */
6799 *model_number
, /* ppd-model-number attribute */
6800 *product
, /* ppd-product attribute */
6801 *psversion
, /* ppd-psverion attribute */
6802 *type
, /* ppd-type attribute */
6803 *requested
, /* requested-attributes attribute */
6804 *exclude
, /* exclude-schemes attribute */
6805 *include
; /* include-schemes attribute */
6806 char command
[1024], /* cups-driverd command */
6807 options
[4096], /* Options to pass to command */
6808 device_str
[256],/* Escaped ppd-device-id string */
6810 /* Escaped ppd-natural-language */
6811 make_str
[256], /* Escaped ppd-make string */
6812 model_str
[256], /* Escaped ppd-make-and-model string */
6813 model_number_str
[256],
6814 /* ppd-model-number string */
6816 /* Escaped ppd-product string */
6818 /* Escaped ppd-psversion string */
6819 type_str
[256], /* Escaped ppd-type string */
6821 /* String for requested attributes */
6823 /* String for excluded schemes */
6825 /* String for included schemes */
6828 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
6834 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6836 send_http_error(con
, status
, NULL
);
6841 * Run cups-driverd command with the given options...
6844 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6845 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6846 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6848 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6849 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6851 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6853 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6854 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6855 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6856 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6858 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
6860 include
= ippFindAttribute(con
->request
, "include-schemes",
6864 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6866 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6869 url_encode_attr(device
, device_str
, sizeof(device_str
));
6871 device_str
[0] = '\0';
6874 url_encode_attr(language
, language_str
, sizeof(language_str
));
6876 language_str
[0] = '\0';
6879 url_encode_attr(make
, make_str
, sizeof(make_str
));
6884 url_encode_attr(model
, model_str
, sizeof(model_str
));
6886 model_str
[0] = '\0';
6889 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6890 model_number
->values
[0].integer
);
6892 model_number_str
[0] = '\0';
6895 url_encode_attr(product
, product_str
, sizeof(product_str
));
6897 product_str
[0] = '\0';
6900 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6902 psversion_str
[0] = '\0';
6905 url_encode_attr(type
, type_str
, sizeof(type_str
));
6910 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6912 exclude_str
[0] = '\0';
6915 url_encode_attr(include
, include_str
, sizeof(include_str
));
6917 include_str
[0] = '\0';
6919 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6920 snprintf(options
, sizeof(options
),
6921 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6922 con
->request
->request
.op
.request_id
,
6923 limit
? limit
->values
[0].integer
: 0,
6925 device
? "%20" : "", device_str
,
6926 language
? "%20" : "", language_str
,
6927 make
? "%20" : "", make_str
,
6928 model
? "%20" : "", model_str
,
6929 model_number
? "%20" : "", model_number_str
,
6930 product
? "%20" : "", product_str
,
6931 psversion
? "%20" : "", psversion_str
,
6932 type
? "%20" : "", type_str
,
6933 exclude_str
[0] ? "%20" : "", exclude_str
,
6934 include_str
[0] ? "%20" : "", include_str
);
6936 if (cupsdSendCommand(con
, command
, options
, 0))
6939 * Command started successfully, don't send an IPP response here...
6942 ippDelete(con
->response
);
6943 con
->response
= NULL
;
6948 * Command failed, return "internal error" so the user knows something
6952 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6953 _("cups-driverd failed to execute."));
6959 * 'get_printer_attrs()' - Get printer attributes.
6963 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6964 ipp_attribute_t
*uri
) /* I - Printer URI */
6966 http_status_t status
; /* Policy status */
6967 cups_ptype_t dtype
; /* Destination type (printer/class) */
6968 cupsd_printer_t
*printer
; /* Printer/class */
6969 cups_array_t
*ra
; /* Requested attributes array */
6972 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6973 con
->number
, uri
->values
[0].string
.text
);
6976 * Is the destination valid?
6979 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6985 send_ipp_status(con
, IPP_NOT_FOUND
,
6986 _("The printer or class does not exist."));
6994 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6996 send_http_error(con
, status
, printer
);
7001 * Send the attributes...
7004 ra
= create_requested_array(con
->request
);
7006 copy_printer_attrs(con
, printer
, ra
);
7008 cupsArrayDelete(ra
);
7010 con
->response
->request
.status
.status_code
= IPP_OK
;
7015 * 'get_printer_supported()' - Get printer supported values.
7019 get_printer_supported(
7020 cupsd_client_t
*con
, /* I - Client connection */
7021 ipp_attribute_t
*uri
) /* I - Printer URI */
7023 http_status_t status
; /* Policy status */
7024 cups_ptype_t dtype
; /* Destination type (printer/class) */
7025 cupsd_printer_t
*printer
; /* Printer/class */
7028 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7029 con
->number
, uri
->values
[0].string
.text
);
7032 * Is the destination valid?
7035 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7041 send_ipp_status(con
, IPP_NOT_FOUND
,
7042 _("The printer or class does not exist."));
7050 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7052 send_http_error(con
, status
, printer
);
7057 * Return a list of attributes that can be set via Set-Printer-Attributes.
7060 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7062 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7063 "printer-location", 0);
7065 con
->response
->request
.status
.status_code
= IPP_OK
;
7070 * 'get_printers()' - Get a list of printers or classes.
7074 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7075 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7077 http_status_t status
; /* Policy status */
7078 ipp_attribute_t
*attr
; /* Current attribute */
7079 int limit
; /* Max number of printers to return */
7080 int count
; /* Number of printers that match */
7081 cupsd_printer_t
*printer
; /* Current printer pointer */
7082 cups_ptype_t printer_type
, /* printer-type attribute */
7083 printer_mask
; /* printer-type-mask attribute */
7084 char *location
; /* Location string */
7085 const char *username
; /* Current user */
7086 char *first_printer_name
; /* first-printer-name attribute */
7087 cups_array_t
*ra
; /* Requested attributes array */
7088 int local
; /* Local connection? */
7091 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7098 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7100 send_http_error(con
, status
, NULL
);
7105 * Check for printers...
7108 if (!Printers
|| !cupsArrayCount(Printers
))
7110 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7115 * See if they want to limit the number of printers reported...
7118 if ((attr
= ippFindAttribute(con
->request
, "limit",
7119 IPP_TAG_INTEGER
)) != NULL
)
7120 limit
= attr
->values
[0].integer
;
7124 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7125 IPP_TAG_NAME
)) != NULL
)
7126 first_printer_name
= attr
->values
[0].string
.text
;
7128 first_printer_name
= NULL
;
7131 * Support filtering...
7134 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7135 IPP_TAG_ENUM
)) != NULL
)
7136 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7138 printer_type
= (cups_ptype_t
)0;
7140 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7141 IPP_TAG_ENUM
)) != NULL
)
7142 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7144 printer_mask
= (cups_ptype_t
)0;
7146 local
= httpAddrLocalhost(&(con
->clientaddr
));
7148 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7149 IPP_TAG_TEXT
)) != NULL
)
7150 location
= attr
->values
[0].string
.text
;
7154 if (con
->username
[0])
7155 username
= con
->username
;
7156 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7157 IPP_TAG_NAME
)) != NULL
)
7158 username
= attr
->values
[0].string
.text
;
7162 ra
= create_requested_array(con
->request
);
7165 * OK, build a list of printers for this printer...
7168 if (first_printer_name
)
7170 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7171 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7174 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7177 count
< limit
&& printer
;
7178 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7180 if (!local
&& !printer
->shared
)
7183 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7184 (printer
->type
& printer_mask
) == printer_type
&&
7186 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7189 * If a username is specified, see if it is allowed or denied
7193 if (cupsArrayCount(printer
->users
) && username
&&
7194 !user_allowed(printer
, username
))
7198 * Add the group separator as needed...
7202 ippAddSeparator(con
->response
);
7207 * Send the attributes...
7210 copy_printer_attrs(con
, printer
, ra
);
7214 cupsArrayDelete(ra
);
7216 con
->response
->request
.status
.status_code
= IPP_OK
;
7221 * 'get_subscription_attrs()' - Get subscription attributes.
7225 get_subscription_attrs(
7226 cupsd_client_t
*con
, /* I - Client connection */
7227 int sub_id
) /* I - Subscription ID */
7229 http_status_t status
; /* Policy status */
7230 cupsd_subscription_t
*sub
; /* Subscription */
7231 cupsd_policy_t
*policy
; /* Current security policy */
7232 cups_array_t
*ra
, /* Requested attributes array */
7233 *exclude
; /* Private attributes array */
7236 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7237 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7238 con
, con
->number
, sub_id
);
7241 * Is the subscription ID valid?
7244 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7247 * Bad subscription ID...
7250 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7260 policy
= sub
->dest
->op_policy_ptr
;
7262 policy
= DefaultPolicyPtr
;
7264 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7266 send_http_error(con
, status
, sub
->dest
);
7270 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7273 * Copy the subscription attributes to the response using the
7274 * requested-attributes attribute that may be provided by the client.
7277 ra
= create_requested_array(con
->request
);
7279 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7281 cupsArrayDelete(ra
);
7283 con
->response
->request
.status
.status_code
= IPP_OK
;
7288 * 'get_subscriptions()' - Get subscriptions.
7292 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7293 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7295 http_status_t status
; /* Policy status */
7296 int count
; /* Number of subscriptions */
7297 int limit
; /* Limit */
7298 cupsd_subscription_t
*sub
; /* Subscription */
7299 cups_array_t
*ra
; /* Requested attributes array */
7300 ipp_attribute_t
*attr
; /* Attribute */
7301 cups_ptype_t dtype
; /* Destination type (printer/class) */
7302 char scheme
[HTTP_MAX_URI
],
7303 /* Scheme portion of URI */
7304 username
[HTTP_MAX_URI
],
7305 /* Username portion of URI */
7307 /* Host portion of URI */
7308 resource
[HTTP_MAX_URI
];
7309 /* Resource portion of URI */
7310 int port
; /* Port portion of URI */
7311 cupsd_job_t
*job
; /* Job pointer */
7312 cupsd_printer_t
*printer
; /* Printer */
7313 cupsd_policy_t
*policy
; /* Policy */
7314 cups_array_t
*exclude
; /* Private attributes array */
7317 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7318 "get_subscriptions(con=%p[%d], uri=%s)",
7319 con
, con
->number
, uri
->values
[0].string
.text
);
7322 * Is the destination valid?
7325 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7326 sizeof(scheme
), username
, sizeof(username
), host
,
7327 sizeof(host
), &port
, resource
, sizeof(resource
));
7329 if (!strcmp(resource
, "/") ||
7330 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7331 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7332 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7337 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7340 job
= cupsdFindJob(atoi(resource
+ 6));
7344 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7345 atoi(resource
+ 6));
7349 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7355 send_ipp_status(con
, IPP_NOT_FOUND
,
7356 _("The printer or class does not exist."));
7359 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7360 IPP_TAG_INTEGER
)) != NULL
)
7362 job
= cupsdFindJob(attr
->values
[0].integer
);
7366 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7367 attr
->values
[0].integer
);
7379 policy
= printer
->op_policy_ptr
;
7381 policy
= DefaultPolicyPtr
;
7383 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7385 send_http_error(con
, status
, printer
);
7390 * Copy the subscription attributes to the response using the
7391 * requested-attributes attribute that may be provided by the client.
7394 ra
= create_requested_array(con
->request
);
7396 if ((attr
= ippFindAttribute(con
->request
, "limit",
7397 IPP_TAG_INTEGER
)) != NULL
)
7398 limit
= attr
->values
[0].integer
;
7403 * See if we only want to see subscriptions for a specific user...
7406 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7407 IPP_TAG_BOOLEAN
)) != NULL
&&
7408 attr
->values
[0].boolean
)
7409 strlcpy(username
, get_username(con
), sizeof(username
));
7413 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7415 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7416 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7417 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7419 ippAddSeparator(con
->response
);
7421 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7422 policy
, con
, sub
->dest
,
7425 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7428 if (limit
&& count
>= limit
)
7432 cupsArrayDelete(ra
);
7435 con
->response
->request
.status
.status_code
= IPP_OK
;
7437 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7442 * 'get_username()' - Get the username associated with a request.
7445 static const char * /* O - Username */
7446 get_username(cupsd_client_t
*con
) /* I - Connection */
7448 ipp_attribute_t
*attr
; /* Attribute */
7451 if (con
->username
[0])
7452 return (con
->username
);
7453 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7454 IPP_TAG_NAME
)) != NULL
)
7455 return (attr
->values
[0].string
.text
);
7457 return ("anonymous");
7462 * 'hold_job()' - Hold a print job.
7466 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7467 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7469 ipp_attribute_t
*attr
; /* Current job-hold-until */
7470 const char *when
; /* New value */
7471 int jobid
; /* Job ID */
7472 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7473 username
[HTTP_MAX_URI
], /* Username portion of URI */
7474 host
[HTTP_MAX_URI
], /* Host portion of URI */
7475 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7476 int port
; /* Port portion of URI */
7477 cupsd_job_t
*job
; /* Job information */
7480 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7481 uri
->values
[0].string
.text
);
7484 * See if we have a job URI or a printer URI...
7487 if (!strcmp(uri
->name
, "printer-uri"))
7490 * Got a printer URI; see if we also have a job-id attribute...
7493 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7494 IPP_TAG_INTEGER
)) == NULL
)
7496 send_ipp_status(con
, IPP_BAD_REQUEST
,
7497 _("Got a printer-uri attribute but no job-id."));
7501 jobid
= attr
->values
[0].integer
;
7506 * Got a job URI; parse it to get the job ID...
7509 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7510 sizeof(scheme
), username
, sizeof(username
), host
,
7511 sizeof(host
), &port
, resource
, sizeof(resource
));
7513 if (strncmp(resource
, "/jobs/", 6))
7519 send_ipp_status(con
, IPP_BAD_REQUEST
,
7520 _("Bad job-uri \"%s\"."),
7521 uri
->values
[0].string
.text
);
7525 jobid
= atoi(resource
+ 6);
7529 * See if the job exists...
7532 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7535 * Nope - return a "not found" error...
7538 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7543 * See if the job is owned by the requesting user...
7546 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7548 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7549 cupsdFindDest(job
->dest
));
7554 * See if the job is in a state that allows holding...
7557 if (job
->state_value
> IPP_JOB_STOPPED
)
7560 * Return a "not-possible" error...
7563 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7564 _("Job #%d is finished and cannot be altered."),
7570 * Hold the job and return...
7573 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
7574 IPP_TAG_KEYWORD
)) == NULL
)
7575 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7579 when
= attr
->values
[0].string
.text
;
7581 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7582 "Job job-hold-until value changed by user.");
7585 when
= "indefinite";
7587 cupsdSetJobHoldUntil(job
, when
, 1);
7588 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
7591 con
->response
->request
.status
.status_code
= IPP_OK
;
7596 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7600 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
7601 ipp_attribute_t
*uri
) /* I - Printer URI */
7603 http_status_t status
; /* Policy status */
7604 cups_ptype_t dtype
; /* Destination type (printer/class) */
7605 cupsd_printer_t
*printer
; /* Printer data */
7608 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
7609 con
->number
, uri
->values
[0].string
.text
);
7612 * Is the destination valid?
7615 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7621 send_ipp_status(con
, IPP_NOT_FOUND
,
7622 _("The printer or class does not exist."));
7630 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7632 send_http_error(con
, status
, printer
);
7637 * Hold pending/new jobs sent to the printer...
7640 printer
->holding_new_jobs
= 1;
7642 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
7644 if (dtype
& CUPS_PRINTER_CLASS
)
7645 cupsdLogMessage(CUPSD_LOG_INFO
,
7646 "Class \"%s\" now holding pending/new jobs (\"%s\").",
7647 printer
->name
, get_username(con
));
7649 cupsdLogMessage(CUPSD_LOG_INFO
,
7650 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
7651 printer
->name
, get_username(con
));
7654 * Everything was ok, so return OK status...
7657 con
->response
->request
.status
.status_code
= IPP_OK
;
7662 * 'move_job()' - Move a job to a new destination.
7666 move_job(cupsd_client_t
*con
, /* I - Client connection */
7667 ipp_attribute_t
*uri
) /* I - Job URI */
7669 http_status_t status
; /* Policy status */
7670 ipp_attribute_t
*attr
; /* Current attribute */
7671 int jobid
; /* Job ID */
7672 cupsd_job_t
*job
; /* Current job */
7673 const char *src
; /* Source printer/class */
7674 cups_ptype_t stype
, /* Source type (printer or class) */
7675 dtype
; /* Destination type (printer/class) */
7676 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7677 username
[HTTP_MAX_URI
], /* Username portion of URI */
7678 host
[HTTP_MAX_URI
], /* Host portion of URI */
7679 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7680 int port
; /* Port portion of URI */
7681 cupsd_printer_t
*sprinter
, /* Source printer */
7682 *dprinter
; /* Destination printer */
7685 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
7686 uri
->values
[0].string
.text
);
7689 * Get the new printer or class...
7692 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7693 IPP_TAG_URI
)) == NULL
)
7696 * Need job-printer-uri...
7699 send_ipp_status(con
, IPP_BAD_REQUEST
,
7700 _("job-printer-uri attribute missing."));
7704 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7710 send_ipp_status(con
, IPP_NOT_FOUND
,
7711 _("The printer or class does not exist."));
7716 * See if we have a job URI or a printer URI...
7719 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7720 sizeof(scheme
), username
, sizeof(username
), host
,
7721 sizeof(host
), &port
, resource
, sizeof(resource
));
7723 if (!strcmp(uri
->name
, "printer-uri"))
7726 * Got a printer URI; see if we also have a job-id attribute...
7729 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7730 IPP_TAG_INTEGER
)) == NULL
)
7736 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7737 &sprinter
)) == NULL
)
7743 send_ipp_status(con
, IPP_NOT_FOUND
,
7744 _("The printer or class does not exist."));
7753 * Otherwise, just move a single job...
7756 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7759 * Nope - return a "not found" error...
7762 send_ipp_status(con
, IPP_NOT_FOUND
,
7763 _("Job #%d does not exist."), attr
->values
[0].integer
);
7769 * Job found, initialize source pointers...
7780 * Got a job URI; parse it to get the job ID...
7783 if (strncmp(resource
, "/jobs/", 6))
7789 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
7790 uri
->values
[0].string
.text
);
7795 * See if the job exists...
7798 jobid
= atoi(resource
+ 6);
7800 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7803 * Nope - return a "not found" error...
7806 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7812 * Job found, initialize source pointers...
7821 * Check the policy of the destination printer...
7824 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7825 job
? job
->username
: NULL
)) != HTTP_OK
)
7827 send_http_error(con
, status
, dprinter
);
7832 * Now move the job or jobs...
7838 * See if the job has been completed...
7841 if (job
->state_value
> IPP_JOB_STOPPED
)
7844 * Return a "not-possible" error...
7847 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7848 _("Job #%d is finished and cannot be altered."),
7854 * See if the job is owned by the requesting user...
7857 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7859 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7860 cupsdFindDest(job
->dest
));
7865 * Move the job to a different printer or class...
7868 cupsdMoveJob(job
, dprinter
);
7873 * Got the source printer, now look through the jobs...
7876 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7878 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7881 * See if the job is pointing at the source printer or has not been
7885 if (_cups_strcasecmp(job
->dest
, src
) ||
7886 job
->state_value
> IPP_JOB_STOPPED
)
7890 * See if the job can be moved by the requesting user...
7893 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7897 * Move the job to a different printer or class...
7900 cupsdMoveJob(job
, dprinter
);
7905 * Start jobs if possible...
7911 * Return with "everything is OK" status...
7914 con
->response
->request
.status
.status_code
= IPP_OK
;
7919 * 'ppd_parse_line()' - Parse a PPD default line.
7922 static int /* O - 0 on success, -1 on failure */
7923 ppd_parse_line(const char *line
, /* I - Line */
7924 char *option
, /* O - Option name */
7925 int olen
, /* I - Size of option name */
7926 char *choice
, /* O - Choice name */
7927 int clen
) /* I - Size of choice name */
7930 * Verify this is a default option line...
7933 if (strncmp(line
, "*Default", 8))
7937 * Read the option name...
7940 for (line
+= 8, olen
--;
7941 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
7952 * Skip everything else up to the colon (:)...
7955 while (*line
&& *line
!= ':')
7964 * Now grab the option choice, skipping leading whitespace...
7967 while (isspace(*line
& 255))
7971 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
7982 * Return with no errors...
7990 * 'print_job()' - Print a file to a printer or class.
7994 print_job(cupsd_client_t
*con
, /* I - Client connection */
7995 ipp_attribute_t
*uri
) /* I - Printer URI */
7997 ipp_attribute_t
*attr
; /* Current attribute */
7998 ipp_attribute_t
*format
; /* Document-format attribute */
7999 const char *default_format
; /* document-format-default value */
8000 cupsd_job_t
*job
; /* New job */
8001 char filename
[1024]; /* Job filename */
8002 mime_type_t
*filetype
; /* Type of file */
8003 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8004 type
[MIME_MAX_TYPE
], /* Subtype of file */
8005 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8006 /* Textual name of mime type */
8007 cupsd_printer_t
*printer
; /* Printer data */
8008 struct stat fileinfo
; /* File information */
8009 int kbytes
; /* Size of file */
8010 int compression
; /* Document compression */
8013 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8014 uri
->values
[0].string
.text
);
8017 * Validate print file attributes, for now just document-format and
8018 * compression (CUPS only supports "none" and "gzip")...
8021 compression
= CUPS_FILE_NONE
;
8023 if ((attr
= ippFindAttribute(con
->request
, "compression",
8024 IPP_TAG_KEYWORD
)) != NULL
)
8026 if (strcmp(attr
->values
[0].string
.text
, "none")
8028 && strcmp(attr
->values
[0].string
.text
, "gzip")
8029 #endif /* HAVE_LIBZ */
8032 send_ipp_status(con
, IPP_ATTRIBUTES
,
8033 _("Unsupported compression \"%s\"."),
8034 attr
->values
[0].string
.text
);
8035 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8036 "compression", NULL
, attr
->values
[0].string
.text
);
8041 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8042 compression
= CUPS_FILE_GZIP
;
8043 #endif /* HAVE_LIBZ */
8047 * Do we have a file to print?
8052 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8057 * Is the destination valid?
8060 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8066 send_ipp_status(con
, IPP_NOT_FOUND
,
8067 _("The printer or class does not exist."));
8072 * Is it a format we support?
8075 if ((format
= ippFindAttribute(con
->request
, "document-format",
8076 IPP_TAG_MIMETYPE
)) != NULL
)
8079 * Grab format from client...
8082 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8085 send_ipp_status(con
, IPP_BAD_REQUEST
,
8086 _("Bad document-format \"%s\"."),
8087 format
->values
[0].string
.text
);
8091 else if ((default_format
= cupsGetOption("document-format",
8092 printer
->num_options
,
8093 printer
->options
)) != NULL
)
8096 * Use default document format...
8099 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8101 send_ipp_status(con
, IPP_BAD_REQUEST
,
8102 _("Bad document-format \"%s\"."),
8113 strlcpy(super
, "application", sizeof(super
));
8114 strlcpy(type
, "octet-stream", sizeof(type
));
8117 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8120 * Auto-type the file...
8123 ipp_attribute_t
*doc_name
; /* document-name attribute */
8126 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8128 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8129 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8130 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8134 filetype
= mimeType(MimeDatabase
, super
, type
);
8136 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8137 filetype
->super
, filetype
->type
);
8140 filetype
= mimeType(MimeDatabase
, super
, type
);
8144 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8147 * Replace the document-format attribute value with the auto-typed or
8151 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8155 ippSetString(con
->request
, &format
, 0, mimetype
);
8157 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8158 "document-format", NULL
, mimetype
);
8162 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8163 _("Unsupported document-format \"%s\"."),
8164 format
? format
->values
[0].string
.text
:
8165 "application/octet-stream");
8166 cupsdLogMessage(CUPSD_LOG_INFO
,
8167 "Hint: Do you have the raw file printing rules enabled?");
8170 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8171 "document-format", NULL
, format
->values
[0].string
.text
);
8177 * Read any embedded job ticket info from PS files...
8180 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8181 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8182 !_cups_strcasecmp(filetype
->type
, "pdf")))
8183 read_job_ticket(con
);
8186 * Create the job object...
8189 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8193 * Update quota data...
8196 if (stat(con
->filename
, &fileinfo
))
8199 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8201 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8203 job
->koctets
+= kbytes
;
8205 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8206 attr
->values
[0].integer
+= kbytes
;
8209 * Add the job file...
8212 if (add_file(con
, job
, filetype
, compression
))
8215 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8217 rename(con
->filename
, filename
);
8218 cupsdClearString(&con
->filename
);
8221 * See if we need to add the ending sheet...
8224 if (cupsdTimeoutJob(job
))
8228 * Log and save the job...
8231 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8232 "File of type %s/%s queued by \"%s\".",
8233 filetype
->super
, filetype
->type
, job
->username
);
8234 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8235 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8236 job
->dest
, job
->username
);
8239 * Start the job if possible...
8247 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8249 * This function only gets called when printing a single PDF or PostScript
8250 * file using the Print-Job operation. It doesn't work for Create-Job +
8251 * Send-File, since the job attributes need to be set at job creation
8252 * time for banners to work. The embedded job ticket stuff is here
8253 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8254 * options and IPP attributes which otherwise would be lost.
8256 * The format of a job ticket is simple:
8258 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8260 * %cupsJobTicket: attr1=value1
8261 * %cupsJobTicket: attr2=value2
8263 * %cupsJobTicket: attrN=valueN
8265 * Job ticket lines must appear immediately after the first line that
8266 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8267 * stops looking for job ticket info when it finds a line that does not begin
8268 * with "%cupsJobTicket:".
8270 * The maximum length of a job ticket line, including the prefix, is
8271 * 255 characters to conform with the Adobe DSC.
8273 * Read-only attributes are rejected with a notice to the error log in
8274 * case a malicious user tries anything. Since the job ticket is read
8275 * prior to attribute validation in print_job(), job ticket attributes
8276 * will go through the same validation as IPP attributes...
8280 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8282 cups_file_t
*fp
; /* File to read from */
8283 char line
[256]; /* Line data */
8284 int num_options
; /* Number of options */
8285 cups_option_t
*options
; /* Options */
8286 ipp_t
*ticket
; /* New attributes */
8287 ipp_attribute_t
*attr
, /* Current attribute */
8288 *attr2
, /* Job attribute */
8289 *prev2
; /* Previous job attribute */
8293 * First open the print file...
8296 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8298 cupsdLogMessage(CUPSD_LOG_ERROR
,
8299 "Unable to open print file for job ticket - %s",
8305 * Skip the first line...
8308 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8310 cupsdLogMessage(CUPSD_LOG_ERROR
,
8311 "Unable to read from print file for job ticket - %s",
8317 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8320 * Not a DSC-compliant file, so no job ticket info will be available...
8328 * Read job ticket info from the file...
8334 while (cupsFileGets(fp
, line
, sizeof(line
)))
8337 * Stop at the first non-ticket line...
8340 if (strncmp(line
, "%cupsJobTicket:", 15))
8344 * Add the options to the option array...
8347 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8351 * Done with the file; see if we have any options...
8356 if (num_options
== 0)
8360 * OK, convert the options to an attribute list, and apply them to
8365 cupsEncodeOptions(ticket
, num_options
, options
);
8368 * See what the user wants to change.
8371 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8373 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8376 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8377 !strcmp(attr
->name
, "job-originating-user-name") ||
8378 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8379 !strcmp(attr
->name
, "job-k-octets") ||
8380 !strcmp(attr
->name
, "job-id") ||
8381 !strncmp(attr
->name
, "job-state", 9) ||
8382 !strncmp(attr
->name
, "time-at-", 8))
8383 continue; /* Read-only attrs */
8385 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8386 IPP_TAG_ZERO
)) != NULL
)
8389 * Some other value; first free the old value...
8392 if (con
->request
->attrs
== attr2
)
8394 con
->request
->attrs
= attr2
->next
;
8399 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8400 if (prev2
->next
== attr2
)
8402 prev2
->next
= attr2
->next
;
8407 if (con
->request
->last
== attr2
)
8408 con
->request
->last
= prev2
;
8410 ippDeleteAttribute(NULL
, attr2
);
8414 * Add new option by copying it...
8417 ippCopyAttribute(con
->request
, attr
, 0);
8421 * Then free the attribute list and option array...
8425 cupsFreeOptions(num_options
, options
);
8430 * 'reject_jobs()' - Reject print jobs to a printer.
8434 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8435 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8437 http_status_t status
; /* Policy status */
8438 cups_ptype_t dtype
; /* Destination type (printer/class) */
8439 cupsd_printer_t
*printer
; /* Printer data */
8440 ipp_attribute_t
*attr
; /* printer-state-message text */
8443 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8444 con
->number
, uri
->values
[0].string
.text
);
8447 * Is the destination valid?
8450 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8456 send_ipp_status(con
, IPP_NOT_FOUND
,
8457 _("The printer or class does not exist."));
8465 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8467 send_http_error(con
, status
, printer
);
8472 * Reject jobs sent to the printer...
8475 printer
->accepting
= 0;
8477 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8478 IPP_TAG_TEXT
)) == NULL
)
8479 strlcpy(printer
->state_message
, "Rejecting Jobs",
8480 sizeof(printer
->state_message
));
8482 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8483 sizeof(printer
->state_message
));
8485 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8486 "No longer accepting jobs.");
8488 if (dtype
& CUPS_PRINTER_CLASS
)
8490 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8492 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8493 printer
->name
, get_username(con
));
8497 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8499 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8500 printer
->name
, get_username(con
));
8504 * Everything was ok, so return OK status...
8507 con
->response
->request
.status
.status_code
= IPP_OK
;
8512 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8516 release_held_new_jobs(
8517 cupsd_client_t
*con
, /* I - Connection */
8518 ipp_attribute_t
*uri
) /* I - Printer URI */
8520 http_status_t status
; /* Policy status */
8521 cups_ptype_t dtype
; /* Destination type (printer/class) */
8522 cupsd_printer_t
*printer
; /* Printer data */
8525 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8526 con
->number
, uri
->values
[0].string
.text
);
8529 * Is the destination valid?
8532 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8538 send_ipp_status(con
, IPP_NOT_FOUND
,
8539 _("The printer or class does not exist."));
8547 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8549 send_http_error(con
, status
, printer
);
8554 * Hold pending/new jobs sent to the printer...
8557 printer
->holding_new_jobs
= 0;
8559 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8561 if (dtype
& CUPS_PRINTER_CLASS
)
8562 cupsdLogMessage(CUPSD_LOG_INFO
,
8563 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8564 printer
->name
, get_username(con
));
8566 cupsdLogMessage(CUPSD_LOG_INFO
,
8567 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8568 printer
->name
, get_username(con
));
8571 * Everything was ok, so return OK status...
8574 con
->response
->request
.status
.status_code
= IPP_OK
;
8579 * 'release_job()' - Release a held print job.
8583 release_job(cupsd_client_t
*con
, /* I - Client connection */
8584 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8586 ipp_attribute_t
*attr
; /* Current attribute */
8587 int jobid
; /* Job ID */
8588 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8589 username
[HTTP_MAX_URI
], /* Username portion of URI */
8590 host
[HTTP_MAX_URI
], /* Host portion of URI */
8591 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8592 int port
; /* Port portion of URI */
8593 cupsd_job_t
*job
; /* Job information */
8596 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8597 con
->number
, uri
->values
[0].string
.text
);
8600 * See if we have a job URI or a printer URI...
8603 if (!strcmp(uri
->name
, "printer-uri"))
8606 * Got a printer URI; see if we also have a job-id attribute...
8609 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8610 IPP_TAG_INTEGER
)) == NULL
)
8612 send_ipp_status(con
, IPP_BAD_REQUEST
,
8613 _("Got a printer-uri attribute but no job-id."));
8617 jobid
= attr
->values
[0].integer
;
8622 * Got a job URI; parse it to get the job ID...
8625 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8626 sizeof(scheme
), username
, sizeof(username
), host
,
8627 sizeof(host
), &port
, resource
, sizeof(resource
));
8629 if (strncmp(resource
, "/jobs/", 6))
8635 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8636 uri
->values
[0].string
.text
);
8640 jobid
= atoi(resource
+ 6);
8644 * See if the job exists...
8647 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8650 * Nope - return a "not found" error...
8653 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8658 * See if job is "held"...
8661 if (job
->state_value
!= IPP_JOB_HELD
)
8664 * Nope - return a "not possible" error...
8667 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
8672 * See if the job is owned by the requesting user...
8675 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8677 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8678 cupsdFindDest(job
->dest
));
8683 * Reset the job-hold-until value to "no-hold"...
8686 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8687 IPP_TAG_KEYWORD
)) == NULL
)
8688 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8692 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
8693 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
8695 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8696 "Job job-hold-until value changed by user.");
8697 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
8701 * Release the job and return...
8704 cupsdReleaseJob(job
);
8706 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8707 "Job released by user.");
8709 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8711 con
->response
->request
.status
.status_code
= IPP_OK
;
8718 * 'renew_subscription()' - Renew an existing subscription...
8723 cupsd_client_t
*con
, /* I - Client connection */
8724 int sub_id
) /* I - Subscription ID */
8726 http_status_t status
; /* Policy status */
8727 cupsd_subscription_t
*sub
; /* Subscription */
8728 ipp_attribute_t
*lease
; /* notify-lease-duration */
8731 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8732 "renew_subscription(con=%p[%d], sub_id=%d)",
8733 con
, con
->number
, sub_id
);
8736 * Is the subscription ID valid?
8739 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8742 * Bad subscription ID...
8745 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
8753 * Job subscriptions cannot be renewed...
8756 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8757 _("Job subscriptions cannot be renewed."));
8765 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8767 con
, sub
->owner
)) != HTTP_OK
)
8769 send_http_error(con
, status
, sub
->dest
);
8774 * Renew the subscription...
8777 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8780 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8782 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8784 cupsdLogMessage(CUPSD_LOG_INFO
,
8785 "renew_subscription: Limiting notify-lease-duration to "
8788 sub
->lease
= MaxLeaseDuration
;
8791 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8793 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
8795 con
->response
->request
.status
.status_code
= IPP_OK
;
8797 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8798 "notify-lease-duration", sub
->lease
);
8803 * 'restart_job()' - Restart an old print job.
8807 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8808 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8810 ipp_attribute_t
*attr
; /* Current attribute */
8811 int jobid
; /* Job ID */
8812 cupsd_job_t
*job
; /* Job information */
8813 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8814 username
[HTTP_MAX_URI
], /* Username portion of URI */
8815 host
[HTTP_MAX_URI
], /* Host portion of URI */
8816 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8817 int port
; /* Port portion of URI */
8820 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8821 con
->number
, uri
->values
[0].string
.text
);
8824 * See if we have a job URI or a printer URI...
8827 if (!strcmp(uri
->name
, "printer-uri"))
8830 * Got a printer URI; see if we also have a job-id attribute...
8833 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8834 IPP_TAG_INTEGER
)) == NULL
)
8836 send_ipp_status(con
, IPP_BAD_REQUEST
,
8837 _("Got a printer-uri attribute but no job-id."));
8841 jobid
= attr
->values
[0].integer
;
8846 * Got a job URI; parse it to get the job ID...
8849 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8850 sizeof(scheme
), username
, sizeof(username
), host
,
8851 sizeof(host
), &port
, resource
, sizeof(resource
));
8853 if (strncmp(resource
, "/jobs/", 6))
8859 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8860 uri
->values
[0].string
.text
);
8864 jobid
= atoi(resource
+ 6);
8868 * See if the job exists...
8871 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8874 * Nope - return a "not found" error...
8877 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8882 * See if job is in any of the "completed" states...
8885 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8888 * Nope - return a "not possible" error...
8891 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
8897 * See if we have retained the job files...
8902 if (!job
->attrs
|| job
->num_files
== 0)
8905 * Nope - return a "not possible" error...
8908 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8909 _("Job #%d cannot be restarted - no files."), jobid
);
8914 * See if the job is owned by the requesting user...
8917 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8919 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8920 cupsdFindDest(job
->dest
));
8925 * See if the job-hold-until attribute is specified...
8928 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
8929 IPP_TAG_KEYWORD
)) == NULL
)
8930 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
8932 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
8935 * Return the job to a held state...
8938 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
8939 "Restarted by \"%s\" with job-hold-until=%s.",
8940 username
, attr
->values
[0].string
.text
);
8941 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
8943 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
8944 NULL
, job
, "Job restarted by user with job-hold-until=%s",
8945 attr
->values
[0].string
.text
);
8950 * Restart the job...
8953 cupsdRestartJob(job
);
8957 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
8959 con
->response
->request
.status
.status_code
= IPP_OK
;
8964 * 'save_auth_info()' - Save authentication information for a job.
8969 cupsd_client_t
*con
, /* I - Client connection */
8970 cupsd_job_t
*job
, /* I - Job */
8971 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8973 int i
; /* Looping var */
8974 char filename
[1024]; /* Job authentication filename */
8975 cups_file_t
*fp
; /* Job authentication file */
8976 char line
[65536]; /* Line for file */
8977 cupsd_printer_t
*dest
; /* Destination printer/class */
8981 * This function saves the in-memory authentication information for
8982 * a job so that it can be used to authenticate with a remote host.
8983 * The information is stored in a file that is readable only by the
8984 * root user. The fields are Base-64 encoded, each on a separate line,
8985 * followed by random number (up to 1024) of newlines to limit the
8986 * amount of information that is exposed.
8988 * Because of the potential for exposing of authentication information,
8989 * this functionality is only enabled when running cupsd as root.
8991 * This caching only works for the Basic and BasicDigest authentication
8992 * types. Digest authentication cannot be cached this way, and in
8993 * the future Kerberos authentication may make all of this obsolete.
8995 * Authentication information is saved whenever an authenticated
8996 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
8999 * This information is deleted after a job is completed or canceled,
9000 * so reprints may require subsequent re-authentication.
9006 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9010 * Create the authentication file and change permissions...
9013 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9014 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9016 cupsdLogMessage(CUPSD_LOG_ERROR
,
9017 "Unable to save authentication info to \"%s\" - %s",
9018 filename
, strerror(errno
));
9022 fchown(cupsFileNumber(fp
), 0, 0);
9023 fchmod(cupsFileNumber(fp
), 0400);
9025 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9028 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9030 cupsdClearString(job
->auth_env
+ i
);
9032 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9035 * Write 1 to 3 auth values...
9039 i
< auth_info
->num_values
&&
9040 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9043 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9045 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9046 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9049 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9050 auth_info
->values
[i
].string
.text
);
9052 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9053 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9054 auth_info
->values
[i
].string
.text
);
9055 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9056 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9057 auth_info
->values
[i
].string
.text
);
9058 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9059 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9060 auth_info
->values
[i
].string
.text
);
9061 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9062 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9063 auth_info
->values
[i
].string
.text
);
9068 else if (auth_info
&& auth_info
->num_values
== 2 &&
9069 dest
->num_auth_info_required
== 1 &&
9070 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9073 * Allow fallback to username+password for Kerberized queues...
9076 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9077 cupsFilePutConf(fp
, "username", line
);
9079 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9080 auth_info
->values
[0].string
.text
);
9082 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9083 cupsFilePutConf(fp
, "password", line
);
9085 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9086 auth_info
->values
[1].string
.text
);
9088 else if (con
->username
[0])
9091 * Write the authenticated username...
9094 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9095 cupsFilePutConf(fp
, "username", line
);
9097 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9100 * Write the authenticated password...
9103 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9104 cupsFilePutConf(fp
, "password", line
);
9106 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9110 if (con
->gss_uid
> 0)
9112 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9113 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9115 #endif /* HAVE_GSSAPI */
9118 * Write a random number of newlines to the end of the file...
9121 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9122 cupsFilePutChar(fp
, '\n');
9125 * Close the file and return...
9133 * 'send_document()' - Send a file to a printer or class.
9137 send_document(cupsd_client_t
*con
, /* I - Client connection */
9138 ipp_attribute_t
*uri
) /* I - Printer URI */
9140 ipp_attribute_t
*attr
; /* Current attribute */
9141 ipp_attribute_t
*format
; /* Request's document-format attribute */
9142 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9143 const char *default_format
;/* document-format-default value */
9144 int jobid
; /* Job ID number */
9145 cupsd_job_t
*job
; /* Current job */
9146 char job_uri
[HTTP_MAX_URI
],
9148 scheme
[HTTP_MAX_URI
],
9149 /* Method portion of URI */
9150 username
[HTTP_MAX_URI
],
9151 /* Username portion of URI */
9153 /* Host portion of URI */
9154 resource
[HTTP_MAX_URI
];
9155 /* Resource portion of URI */
9156 int port
; /* Port portion of URI */
9157 mime_type_t
*filetype
; /* Type of file */
9158 char super
[MIME_MAX_SUPER
],
9159 /* Supertype of file */
9160 type
[MIME_MAX_TYPE
],
9161 /* Subtype of file */
9162 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9163 /* Textual name of mime type */
9164 char filename
[1024]; /* Job filename */
9165 cupsd_printer_t
*printer
; /* Current printer */
9166 struct stat fileinfo
; /* File information */
9167 int kbytes
; /* Size of file */
9168 int compression
; /* Type of compression */
9169 int start_job
; /* Start the job? */
9172 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9173 con
->number
, uri
->values
[0].string
.text
);
9176 * See if we have a job URI or a printer URI...
9179 if (!strcmp(uri
->name
, "printer-uri"))
9182 * Got a printer URI; see if we also have a job-id attribute...
9185 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9186 IPP_TAG_INTEGER
)) == NULL
)
9188 send_ipp_status(con
, IPP_BAD_REQUEST
,
9189 _("Got a printer-uri attribute but no job-id."));
9193 jobid
= attr
->values
[0].integer
;
9198 * Got a job URI; parse it to get the job ID...
9201 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9202 sizeof(scheme
), username
, sizeof(username
), host
,
9203 sizeof(host
), &port
, resource
, sizeof(resource
));
9205 if (strncmp(resource
, "/jobs/", 6))
9211 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9212 uri
->values
[0].string
.text
);
9216 jobid
= atoi(resource
+ 6);
9220 * See if the job exists...
9223 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9226 * Nope - return a "not found" error...
9229 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9233 printer
= cupsdFindDest(job
->dest
);
9236 * See if the job is owned by the requesting user...
9239 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9241 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9242 cupsdFindDest(job
->dest
));
9247 * OK, see if the client is sending the document compressed - CUPS
9248 * only supports "none" and "gzip".
9251 compression
= CUPS_FILE_NONE
;
9253 if ((attr
= ippFindAttribute(con
->request
, "compression",
9254 IPP_TAG_KEYWORD
)) != NULL
)
9256 if (strcmp(attr
->values
[0].string
.text
, "none")
9258 && strcmp(attr
->values
[0].string
.text
, "gzip")
9259 #endif /* HAVE_LIBZ */
9262 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9263 attr
->values
[0].string
.text
);
9264 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9265 "compression", NULL
, attr
->values
[0].string
.text
);
9270 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9271 compression
= CUPS_FILE_GZIP
;
9272 #endif /* HAVE_LIBZ */
9276 * Do we have a file to print?
9279 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9280 IPP_TAG_BOOLEAN
)) == NULL
)
9282 send_ipp_status(con
, IPP_BAD_REQUEST
,
9283 _("Missing last-document attribute in request."));
9290 * Check for an empty request with "last-document" set to true, which is
9291 * used to close an "open" job by RFC 2911, section 3.3.2.
9294 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9297 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9302 * Is it a format we support?
9305 if ((format
= ippFindAttribute(con
->request
, "document-format",
9306 IPP_TAG_MIMETYPE
)) != NULL
)
9309 * Grab format from client...
9312 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9315 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9316 format
->values
[0].string
.text
);
9320 else if ((default_format
= cupsGetOption("document-format",
9321 printer
->num_options
,
9322 printer
->options
)) != NULL
)
9325 * Use default document format...
9328 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9330 send_ipp_status(con
, IPP_BAD_REQUEST
,
9331 _("Bad document-format-default \"%s\"."), default_format
);
9338 * No document format attribute? Auto-type it!
9341 strlcpy(super
, "application", sizeof(super
));
9342 strlcpy(type
, "octet-stream", sizeof(type
));
9345 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9348 * Auto-type the file...
9351 ipp_attribute_t
*doc_name
; /* document-name attribute */
9354 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9356 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9357 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9358 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9362 filetype
= mimeType(MimeDatabase
, super
, type
);
9365 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9366 filetype
->super
, filetype
->type
);
9369 filetype
= mimeType(MimeDatabase
, super
, type
);
9374 * Replace the document-format attribute value with the auto-typed or
9378 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9381 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9382 IPP_TAG_MIMETYPE
)) != NULL
)
9383 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9385 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9386 "document-format", NULL
, mimetype
);
9390 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9391 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9392 cupsdLogMessage(CUPSD_LOG_INFO
,
9393 "Hint: Do you have the raw file printing rules enabled?");
9396 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9397 "document-format", NULL
, format
->values
[0].string
.text
);
9402 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9404 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9407 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9408 _("Unsupported document-format \"%s\"."), mimetype
);
9410 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9411 "document-format", NULL
, mimetype
);
9417 * Add the file to the job...
9422 if (add_file(con
, job
, filetype
, compression
))
9425 if (stat(con
->filename
, &fileinfo
))
9428 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9430 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9432 job
->koctets
+= kbytes
;
9434 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9435 attr
->values
[0].integer
+= kbytes
;
9437 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9439 rename(con
->filename
, filename
);
9441 cupsdClearString(&con
->filename
);
9443 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9444 filetype
->super
, filetype
->type
, job
->username
);
9447 * Start the job if this is the last document...
9452 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9453 IPP_TAG_BOOLEAN
)) != NULL
&&
9454 attr
->values
[0].boolean
)
9457 * See if we need to add the ending sheet...
9460 if (cupsdTimeoutJob(job
))
9463 if (job
->state_value
== IPP_JOB_STOPPED
)
9465 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9466 job
->state_value
= IPP_JOB_PENDING
;
9468 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9470 else if (job
->state_value
== IPP_JOB_HELD
)
9472 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9473 IPP_TAG_KEYWORD
)) == NULL
)
9474 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9476 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9478 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9479 job
->state_value
= IPP_JOB_PENDING
;
9481 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9484 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9488 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9494 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9495 IPP_TAG_KEYWORD
)) == NULL
)
9496 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9498 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9500 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9501 job
->state_value
= IPP_JOB_HELD
;
9502 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9504 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9507 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9514 * Fill in the response info...
9517 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9518 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9519 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9522 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9524 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9526 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
9527 NULL
, job
->reasons
->values
[0].string
.text
);
9529 con
->response
->request
.status
.status_code
= IPP_OK
;
9532 * Start the job if necessary...
9541 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9546 cupsd_client_t
*con
, /* I - Client connection */
9547 http_status_t status
, /* I - HTTP status code */
9548 cupsd_printer_t
*printer
) /* I - Printer, if any */
9550 ipp_attribute_t
*uri
; /* Request URI, if any */
9553 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9554 IPP_TAG_URI
)) == NULL
)
9555 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9557 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9558 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9559 con
->number
, httpStatus(status
),
9561 ippOpString(con
->request
->request
.op
.operation_id
) :
9563 uri
? uri
->values
[0].string
.text
: "no URI",
9564 con
->http
->hostname
);
9568 int auth_type
; /* Type of authentication required */
9571 auth_type
= CUPSD_AUTH_NONE
;
9573 if (status
== HTTP_UNAUTHORIZED
&&
9574 printer
->num_auth_info_required
> 0 &&
9575 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9577 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9578 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9579 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9582 * Creating and authenticating jobs requires Kerberos...
9585 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9590 * Use policy/location-defined authentication requirements...
9593 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9594 cupsd_location_t
*auth
; /* Pointer to authentication element */
9597 if (printer
->type
& CUPS_PRINTER_CLASS
)
9598 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9600 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9602 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9603 auth
->type
== CUPSD_AUTH_NONE
)
9604 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9606 con
->request
->request
.op
.operation_id
:
9611 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9612 auth_type
= cupsdDefaultAuthType();
9614 auth_type
= auth
->type
;
9618 cupsdSendError(con
, status
, auth_type
);
9621 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9623 ippDelete(con
->response
);
9624 con
->response
= NULL
;
9631 * 'send_ipp_status()' - Send a status back to the IPP client.
9635 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9636 ipp_status_t status
, /* I - IPP status code */
9637 const char *message
,/* I - Status message */
9638 ...) /* I - Additional args as needed */
9640 va_list ap
; /* Pointer to additional args */
9641 char formatted
[1024]; /* Formatted errror message */
9644 va_start(ap
, message
);
9645 vsnprintf(formatted
, sizeof(formatted
),
9646 _cupsLangString(con
->language
, message
), ap
);
9649 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9650 ippOpString(con
->request
->request
.op
.operation_id
),
9651 ippErrorString(status
), formatted
);
9653 con
->response
->request
.status
.status_code
= status
;
9655 if (ippFindAttribute(con
->response
, "attributes-charset",
9656 IPP_TAG_ZERO
) == NULL
)
9657 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9658 "attributes-charset", NULL
, "utf-8");
9660 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9661 IPP_TAG_ZERO
) == NULL
)
9662 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9663 "attributes-natural-language", NULL
, DefaultLanguage
);
9665 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9666 "status-message", NULL
, formatted
);
9671 * 'set_default()' - Set the default destination...
9675 set_default(cupsd_client_t
*con
, /* I - Client connection */
9676 ipp_attribute_t
*uri
) /* I - Printer URI */
9678 http_status_t status
; /* Policy status */
9679 cups_ptype_t dtype
; /* Destination type (printer/class) */
9680 cupsd_printer_t
*printer
, /* Printer */
9681 *oldprinter
; /* Old default printer */
9684 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9685 con
->number
, uri
->values
[0].string
.text
);
9688 * Is the destination valid?
9691 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9697 send_ipp_status(con
, IPP_NOT_FOUND
,
9698 _("The printer or class does not exist."));
9706 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9708 send_http_error(con
, status
, NULL
);
9713 * Set it as the default...
9716 oldprinter
= DefaultPrinter
;
9717 DefaultPrinter
= printer
;
9720 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9721 "%s is no longer the default printer.", oldprinter
->name
);
9723 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9724 "%s is now the default printer.", printer
->name
);
9726 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9727 CUPSD_DIRTY_PRINTCAP
);
9729 cupsdLogMessage(CUPSD_LOG_INFO
,
9730 "Default destination set to \"%s\" by \"%s\".",
9731 printer
->name
, get_username(con
));
9734 * Everything was ok, so return OK status...
9737 con
->response
->request
.status
.status_code
= IPP_OK
;
9742 * 'set_job_attrs()' - Set job attributes.
9746 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9747 ipp_attribute_t
*uri
) /* I - Job URI */
9749 ipp_attribute_t
*attr
, /* Current attribute */
9750 *attr2
; /* Job attribute */
9751 int jobid
; /* Job ID */
9752 cupsd_job_t
*job
; /* Current job */
9753 char scheme
[HTTP_MAX_URI
],
9754 /* Method portion of URI */
9755 username
[HTTP_MAX_URI
],
9756 /* Username portion of URI */
9758 /* Host portion of URI */
9759 resource
[HTTP_MAX_URI
];
9760 /* Resource portion of URI */
9761 int port
; /* Port portion of URI */
9762 int event
; /* Events? */
9763 int check_jobs
; /* Check jobs? */
9766 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9767 con
->number
, uri
->values
[0].string
.text
);
9770 * Start with "everything is OK" status...
9773 con
->response
->request
.status
.status_code
= IPP_OK
;
9776 * See if we have a job URI or a printer URI...
9779 if (!strcmp(uri
->name
, "printer-uri"))
9782 * Got a printer URI; see if we also have a job-id attribute...
9785 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9786 IPP_TAG_INTEGER
)) == NULL
)
9788 send_ipp_status(con
, IPP_BAD_REQUEST
,
9789 _("Got a printer-uri attribute but no job-id."));
9793 jobid
= attr
->values
[0].integer
;
9798 * Got a job URI; parse it to get the job ID...
9801 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9802 sizeof(scheme
), username
, sizeof(username
), host
,
9803 sizeof(host
), &port
, resource
, sizeof(resource
));
9805 if (strncmp(resource
, "/jobs/", 6))
9811 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9812 uri
->values
[0].string
.text
);
9816 jobid
= atoi(resource
+ 6);
9820 * See if the job exists...
9823 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9826 * Nope - return a "not found" error...
9829 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9834 * See if the job has been completed...
9837 if (job
->state_value
> IPP_JOB_STOPPED
)
9840 * Return a "not-possible" error...
9843 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9844 _("Job #%d is finished and cannot be altered."), jobid
);
9849 * See if the job is owned by the requesting user...
9852 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9854 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9855 cupsdFindDest(job
->dest
));
9860 * See what the user wants to change.
9868 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9870 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9873 if (!strcmp(attr
->name
, "attributes-charset") ||
9874 !strcmp(attr
->name
, "attributes-natural-language") ||
9875 !strcmp(attr
->name
, "document-compression") ||
9876 !strcmp(attr
->name
, "document-format") ||
9877 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9878 !strcmp(attr
->name
, "job-document-access-errors") ||
9879 !strcmp(attr
->name
, "job-id") ||
9880 !strcmp(attr
->name
, "job-impressions-completed") ||
9881 !strcmp(attr
->name
, "job-k-octets") ||
9882 !strcmp(attr
->name
, "job-originating-host-name") ||
9883 !strcmp(attr
->name
, "job-originating-user-name") ||
9884 !strcmp(attr
->name
, "job-printer-up-time") ||
9885 !strcmp(attr
->name
, "job-printer-uri") ||
9886 !strcmp(attr
->name
, "job-sheets") ||
9887 !strcmp(attr
->name
, "job-state-message") ||
9888 !strcmp(attr
->name
, "job-state-reasons") ||
9889 !strcmp(attr
->name
, "job-uri") ||
9890 !strcmp(attr
->name
, "number-of-documents") ||
9891 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9892 !strcmp(attr
->name
, "output-device-assigned") ||
9893 !strncmp(attr
->name
, "date-time-at-", 13) ||
9894 !strncmp(attr
->name
, "job-k-octets", 12) ||
9895 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9896 !strncmp(attr
->name
, "time-at-", 8))
9902 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9903 _("%s cannot be changed."), attr
->name
);
9905 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9906 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9910 if (!strcmp(attr
->name
, "job-priority"))
9913 * Change the job priority...
9916 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9918 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
9920 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9921 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9923 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9925 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9926 _("Job is completed and cannot be changed."));
9929 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9931 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
9932 attr
->values
[0].integer
);
9933 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9936 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
9937 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
9940 else if (!strcmp(attr
->name
, "job-state"))
9943 * Change the job state...
9946 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9948 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
9950 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9951 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9955 switch (attr
->values
[0].integer
)
9957 case IPP_JOB_PENDING
:
9959 if (job
->state_value
> IPP_JOB_HELD
)
9961 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9962 _("Job state cannot be changed."));
9965 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9967 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
9968 attr
->values
[0].integer
);
9969 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
9974 case IPP_JOB_PROCESSING
:
9975 case IPP_JOB_STOPPED
:
9976 if (job
->state_value
!= attr
->values
[0].integer
)
9978 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9979 _("Job state cannot be changed."));
9984 case IPP_JOB_CANCELED
:
9985 case IPP_JOB_ABORTED
:
9986 case IPP_JOB_COMPLETED
:
9987 if (job
->state_value
> IPP_JOB_PROCESSING
)
9989 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9990 _("Job state cannot be changed."));
9993 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9995 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
9996 attr
->values
[0].integer
);
9997 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
9999 "Job state changed by \"%s\"", username
);
10006 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10008 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10009 IPP_TAG_ZERO
)) != NULL
)
10012 * Some other value; first free the old value...
10015 if (job
->attrs
->prev
)
10016 job
->attrs
->prev
->next
= attr2
->next
;
10018 job
->attrs
->attrs
= attr2
->next
;
10020 if (job
->attrs
->last
== attr2
)
10021 job
->attrs
->last
= job
->attrs
->prev
;
10023 ippDeleteAttribute(NULL
, attr2
);
10026 * Then copy the attribute...
10029 ippCopyAttribute(job
->attrs
, attr
, 0);
10032 * See if the job-name or job-hold-until is being changed.
10035 if (!strcmp(attr
->name
, "job-hold-until"))
10037 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10038 attr
->values
[0].string
.text
);
10039 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10041 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10043 cupsdReleaseJob(job
);
10047 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10048 "Job held by \"%s\".", username
);
10050 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10053 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10056 * Delete the attribute...
10059 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10060 IPP_TAG_ZERO
)) != NULL
)
10062 if (job
->attrs
->prev
)
10063 job
->attrs
->prev
->next
= attr2
->next
;
10065 job
->attrs
->attrs
= attr2
->next
;
10067 if (attr2
== job
->attrs
->last
)
10068 job
->attrs
->last
= job
->attrs
->prev
;
10070 ippDeleteAttribute(NULL
, attr2
);
10072 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10078 * Add new option by copying it...
10081 ippCopyAttribute(job
->attrs
, attr
, 0);
10083 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10092 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10095 * Send events as needed...
10098 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10099 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10100 cupsdFindDest(job
->dest
), job
,
10101 "Job priority changed by user.");
10103 if (event
& CUPSD_EVENT_JOB_STATE
)
10104 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10105 job
->state_value
== IPP_JOB_HELD
?
10106 "Job held by user." : "Job restarted by user.");
10108 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10109 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10110 "Job options changed by user.");
10113 * Start jobs if possible...
10122 * 'set_printer_attrs()' - Set printer attributes.
10126 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10127 ipp_attribute_t
*uri
) /* I - Printer */
10129 http_status_t status
; /* Policy status */
10130 cups_ptype_t dtype
; /* Destination type (printer/class) */
10131 cupsd_printer_t
*printer
; /* Printer/class */
10132 ipp_attribute_t
*attr
; /* Printer attribute */
10133 int changed
= 0; /* Was anything changed? */
10136 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10137 con
->number
, uri
->values
[0].string
.text
);
10140 * Is the destination valid?
10143 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10149 send_ipp_status(con
, IPP_NOT_FOUND
,
10150 _("The printer or class does not exist."));
10158 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10160 send_http_error(con
, status
, printer
);
10165 * Return a list of attributes that can be set via Set-Printer-Attributes.
10168 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10169 IPP_TAG_TEXT
)) != NULL
)
10171 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10175 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10176 IPP_TAG_TEXT
)) != NULL
)
10178 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10183 * Update the printer attributes and return...
10188 cupsdSetPrinterAttrs(printer
);
10189 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10191 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10192 "Printer \"%s\" description or location changed by \"%s\".",
10193 printer
->name
, get_username(con
));
10195 cupsdLogMessage(CUPSD_LOG_INFO
,
10196 "Printer \"%s\" description or location changed by \"%s\".",
10197 printer
->name
, get_username(con
));
10200 con
->response
->request
.status
.status_code
= IPP_OK
;
10205 * 'set_printer_defaults()' - Set printer default options from a request.
10209 set_printer_defaults(
10210 cupsd_client_t
*con
, /* I - Client connection */
10211 cupsd_printer_t
*printer
) /* I - Printer */
10213 int i
; /* Looping var */
10214 ipp_attribute_t
*attr
; /* Current attribute */
10215 size_t namelen
; /* Length of attribute name */
10216 char name
[256], /* New attribute name */
10217 value
[256]; /* String version of integer attrs */
10220 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10223 * Skip non-printer attributes...
10226 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10229 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10231 if (!strcmp(attr
->name
, "job-sheets-default"))
10234 * Only allow keywords and names...
10237 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10241 * Only allow job-sheets-default to be set when running without a
10242 * system high classification level...
10245 if (Classification
)
10248 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10250 if (attr
->num_values
> 1)
10251 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10253 cupsdSetString(&printer
->job_sheets
[1], "none");
10255 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10257 cupsdFreeStrings(&(printer
->users
));
10259 printer
->deny_users
= 0;
10261 if (attr
->value_tag
== IPP_TAG_NAME
&&
10262 (attr
->num_values
> 1 ||
10263 strcmp(attr
->values
[0].string
.text
, "all")))
10265 for (i
= 0; i
< attr
->num_values
; i
++)
10266 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10269 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10271 cupsdFreeStrings(&(printer
->users
));
10273 printer
->deny_users
= 1;
10275 if (attr
->value_tag
== IPP_TAG_NAME
&&
10276 (attr
->num_values
> 1 ||
10277 strcmp(attr
->values
[0].string
.text
, "none")))
10279 for (i
= 0; i
< attr
->num_values
; i
++)
10280 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10283 else if (!strcmp(attr
->name
, "job-quota-period"))
10285 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10288 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10289 attr
->values
[0].integer
);
10290 cupsdFreeQuotas(printer
);
10292 printer
->quota_period
= attr
->values
[0].integer
;
10294 else if (!strcmp(attr
->name
, "job-k-limit"))
10296 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10299 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10300 attr
->values
[0].integer
);
10301 cupsdFreeQuotas(printer
);
10303 printer
->k_limit
= attr
->values
[0].integer
;
10305 else if (!strcmp(attr
->name
, "job-page-limit"))
10307 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10310 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10311 attr
->values
[0].integer
);
10312 cupsdFreeQuotas(printer
);
10314 printer
->page_limit
= attr
->values
[0].integer
;
10316 else if (!strcmp(attr
->name
, "printer-op-policy"))
10318 cupsd_policy_t
*p
; /* Policy */
10321 if (attr
->value_tag
!= IPP_TAG_NAME
)
10324 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10326 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10327 "Setting printer-op-policy to \"%s\"...",
10328 attr
->values
[0].string
.text
);
10329 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10330 printer
->op_policy_ptr
= p
;
10334 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10335 _("Unknown printer-op-policy \"%s\"."),
10336 attr
->values
[0].string
.text
);
10340 else if (!strcmp(attr
->name
, "printer-error-policy"))
10342 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10345 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10346 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10347 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10348 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10349 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10351 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10352 _("Unknown printer-error-policy \"%s\"."),
10353 attr
->values
[0].string
.text
);
10357 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10358 "Setting printer-error-policy to \"%s\"...",
10359 attr
->values
[0].string
.text
);
10360 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10364 * Skip any other non-default attributes...
10367 namelen
= strlen(attr
->name
);
10368 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10369 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10373 * OK, anything else must be a user-defined default...
10376 strlcpy(name
, attr
->name
, sizeof(name
));
10377 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10379 switch (attr
->value_tag
)
10381 case IPP_TAG_DELETEATTR
:
10382 printer
->num_options
= cupsRemoveOption(name
,
10383 printer
->num_options
,
10384 &(printer
->options
));
10385 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10386 "Deleting %s", attr
->name
);
10389 case IPP_TAG_NAME
:
10390 case IPP_TAG_TEXT
:
10391 case IPP_TAG_KEYWORD
:
10393 printer
->num_options
= cupsAddOption(name
,
10394 attr
->values
[0].string
.text
,
10395 printer
->num_options
,
10396 &(printer
->options
));
10397 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10398 "Setting %s to \"%s\"...", attr
->name
,
10399 attr
->values
[0].string
.text
);
10402 case IPP_TAG_BOOLEAN
:
10403 printer
->num_options
= cupsAddOption(name
,
10404 attr
->values
[0].boolean
?
10406 printer
->num_options
,
10407 &(printer
->options
));
10408 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10409 "Setting %s to %s...", attr
->name
,
10410 attr
->values
[0].boolean
? "true" : "false");
10413 case IPP_TAG_INTEGER
:
10414 case IPP_TAG_ENUM
:
10415 sprintf(value
, "%d", attr
->values
[0].integer
);
10416 printer
->num_options
= cupsAddOption(name
, value
,
10417 printer
->num_options
,
10418 &(printer
->options
));
10419 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10420 "Setting %s to %s...", attr
->name
, value
);
10423 case IPP_TAG_RANGE
:
10424 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10425 attr
->values
[0].range
.upper
);
10426 printer
->num_options
= cupsAddOption(name
, value
,
10427 printer
->num_options
,
10428 &(printer
->options
));
10429 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10430 "Setting %s to %s...", attr
->name
, value
);
10433 case IPP_TAG_RESOLUTION
:
10434 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10435 attr
->values
[0].resolution
.yres
,
10436 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10438 printer
->num_options
= cupsAddOption(name
, value
,
10439 printer
->num_options
,
10440 &(printer
->options
));
10441 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10442 "Setting %s to %s...", attr
->name
, value
);
10446 /* Do nothing for other values */
10454 * 'start_printer()' - Start a printer.
10458 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10459 ipp_attribute_t
*uri
) /* I - Printer URI */
10461 int i
; /* Temporary variable */
10462 http_status_t status
; /* Policy status */
10463 cups_ptype_t dtype
; /* Destination type (printer/class) */
10464 cupsd_printer_t
*printer
; /* Printer data */
10467 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10468 con
->number
, uri
->values
[0].string
.text
);
10471 * Is the destination valid?
10474 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10480 send_ipp_status(con
, IPP_NOT_FOUND
,
10481 _("The printer or class does not exist."));
10489 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10491 send_http_error(con
, status
, printer
);
10496 * Start the printer...
10499 printer
->state_message
[0] = '\0';
10501 cupsdStartPrinter(printer
, 1);
10503 if (dtype
& CUPS_PRINTER_CLASS
)
10504 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10505 printer
->name
, get_username(con
));
10507 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10508 printer
->name
, get_username(con
));
10516 if ((i
= check_quotas(con
, printer
)) < 0)
10518 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
10523 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
10528 * Everything was ok, so return OK status...
10531 con
->response
->request
.status
.status_code
= IPP_OK
;
10536 * 'stop_printer()' - Stop a printer.
10540 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10541 ipp_attribute_t
*uri
) /* I - Printer URI */
10543 http_status_t status
; /* Policy status */
10544 cups_ptype_t dtype
; /* Destination type (printer/class) */
10545 cupsd_printer_t
*printer
; /* Printer data */
10546 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10549 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10550 con
->number
, uri
->values
[0].string
.text
);
10553 * Is the destination valid?
10556 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10562 send_ipp_status(con
, IPP_NOT_FOUND
,
10563 _("The printer or class does not exist."));
10571 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10573 send_http_error(con
, status
, printer
);
10578 * Stop the printer...
10581 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10582 IPP_TAG_TEXT
)) == NULL
)
10583 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
10586 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10587 sizeof(printer
->state_message
));
10590 cupsdStopPrinter(printer
, 1);
10592 if (dtype
& CUPS_PRINTER_CLASS
)
10593 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10594 printer
->name
, get_username(con
));
10596 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10597 printer
->name
, get_username(con
));
10600 * Everything was ok, so return OK status...
10603 con
->response
->request
.status
.status_code
= IPP_OK
;
10608 * 'url_encode_attr()' - URL-encode a string attribute.
10612 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10613 char *buffer
,/* I - String buffer */
10614 size_t bufsize
)/* I - Size of buffer */
10616 int i
; /* Looping var */
10617 char *bufptr
, /* Pointer into buffer */
10618 *bufend
; /* End of buffer */
10621 strlcpy(buffer
, attr
->name
, bufsize
);
10622 bufptr
= buffer
+ strlen(buffer
);
10623 bufend
= buffer
+ bufsize
- 1;
10625 for (i
= 0; i
< attr
->num_values
; i
++)
10627 if (bufptr
>= bufend
)
10635 if (bufptr
>= bufend
)
10640 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
10642 if (bufptr
>= bufend
)
10653 * 'url_encode_string()' - URL-encode a string.
10656 static char * /* O - End of string */
10657 url_encode_string(const char *s
, /* I - String */
10658 char *buffer
, /* I - String buffer */
10659 size_t bufsize
) /* I - Size of buffer */
10661 char *bufptr
, /* Pointer into buffer */
10662 *bufend
; /* End of buffer */
10663 static const char *hex
= "0123456789ABCDEF";
10668 bufend
= buffer
+ bufsize
- 1;
10670 while (*s
&& bufptr
< bufend
)
10672 if (*s
== ' ' || *s
== '%' || *s
== '+')
10674 if (bufptr
>= (bufend
- 2))
10678 *bufptr
++ = hex
[(*s
>> 4) & 15];
10679 *bufptr
++ = hex
[*s
& 15];
10683 else if (*s
== '\'' || *s
== '\\')
10685 if (bufptr
>= (bufend
- 1))
10702 * 'user_allowed()' - See if a user is allowed to print to a queue.
10705 static int /* O - 0 if not allowed, 1 if allowed */
10706 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10707 const char *username
) /* I - Username */
10709 struct passwd
*pw
; /* User password data */
10710 char baseuser
[256], /* Base username */
10711 *baseptr
, /* Pointer to "@" in base username */
10712 *name
; /* Current user name */
10715 if (cupsArrayCount(p
->users
) == 0)
10718 if (!strcmp(username
, "root"))
10721 if (strchr(username
, '@'))
10724 * Strip @REALM for username check...
10727 strlcpy(baseuser
, username
, sizeof(baseuser
));
10729 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10732 username
= baseuser
;
10735 pw
= getpwnam(username
);
10738 for (name
= (char *)cupsArrayFirst(p
->users
);
10740 name
= (char *)cupsArrayNext(p
->users
))
10742 if (name
[0] == '@')
10745 * Check group membership...
10748 if (cupsdCheckGroup(username
, pw
, name
+ 1))
10751 else if (name
[0] == '#')
10757 if (cupsdCheckGroup(username
, pw
, name
))
10760 else if (!_cups_strcasecmp(username
, name
))
10764 return ((name
!= NULL
) != p
->deny_users
);
10769 * 'validate_job()' - Validate printer options and destination.
10773 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10774 ipp_attribute_t
*uri
) /* I - Printer URI */
10776 http_status_t status
; /* Policy status */
10777 ipp_attribute_t
*attr
; /* Current attribute */
10779 ipp_attribute_t
*auth_info
; /* auth-info attribute */
10780 #endif /* HAVE_SSL */
10781 ipp_attribute_t
*format
, /* Document-format attribute */
10782 *name
; /* Job-name attribute */
10783 cups_ptype_t dtype
; /* Destination type (printer/class) */
10784 char super
[MIME_MAX_SUPER
],
10785 /* Supertype of file */
10786 type
[MIME_MAX_TYPE
];
10787 /* Subtype of file */
10788 cupsd_printer_t
*printer
; /* Printer */
10791 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10792 con
->number
, uri
->values
[0].string
.text
);
10795 * OK, see if the client is sending the document compressed - CUPS
10796 * doesn't support compression yet...
10799 if ((attr
= ippFindAttribute(con
->request
, "compression",
10800 IPP_TAG_KEYWORD
)) != NULL
)
10802 if (strcmp(attr
->values
[0].string
.text
, "none")
10804 && strcmp(attr
->values
[0].string
.text
, "gzip")
10805 #endif /* HAVE_LIBZ */
10808 send_ipp_status(con
, IPP_ATTRIBUTES
,
10809 _("Unsupported 'compression' value \"%s\"."),
10810 attr
->values
[0].string
.text
);
10811 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10812 "compression", NULL
, attr
->values
[0].string
.text
);
10818 * Is it a format we support?
10821 if ((format
= ippFindAttribute(con
->request
, "document-format",
10822 IPP_TAG_MIMETYPE
)) != NULL
)
10824 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
10827 send_ipp_status(con
, IPP_BAD_REQUEST
,
10828 _("Bad 'document-format' value \"%s\"."),
10829 format
->values
[0].string
.text
);
10833 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10834 !mimeType(MimeDatabase
, super
, type
))
10836 cupsdLogMessage(CUPSD_LOG_INFO
,
10837 "Hint: Do you have the raw file printing rules enabled?");
10838 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10839 _("Unsupported 'document-format' value \"%s\"."),
10840 format
->values
[0].string
.text
);
10841 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10842 "document-format", NULL
, format
->values
[0].string
.text
);
10848 * Is the job-name valid?
10851 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
10853 int bad_name
= 0; /* Is the job-name value bad? */
10855 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
10856 name
->num_values
!= 1)
10863 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
10864 * IPP Everywhere requirements for "name" values...
10867 const unsigned char *nameptr
; /* Pointer into "job-name" attribute */
10869 for (nameptr
= (unsigned char *)name
->values
[0].string
.text
;
10873 if (*nameptr
< ' ' && *nameptr
!= '\t')
10875 else if (*nameptr
== 0x7f)
10877 else if ((*nameptr
& 0xe0) == 0xc0)
10879 if ((nameptr
[1] & 0xc0) != 0x80)
10884 else if ((*nameptr
& 0xf0) == 0xe0)
10886 if ((nameptr
[1] & 0xc0) != 0x80 ||
10887 (nameptr
[2] & 0xc0) != 0x80)
10892 else if ((*nameptr
& 0xf8) == 0xf0)
10894 if ((nameptr
[1] & 0xc0) != 0x80 ||
10895 (nameptr
[2] & 0xc0) != 0x80 ||
10896 (nameptr
[3] & 0xc0) != 0x80)
10901 else if (*nameptr
& 0x80)
10911 if (StrictConformance
)
10913 send_ipp_status(con
, IPP_ATTRIBUTES
,
10914 _("Unsupported 'job-name' value."));
10915 ippCopyAttribute(con
->response
, name
, 0);
10920 cupsdLogMessage(CUPSD_LOG_WARN
,
10921 "Unsupported 'job-name' value, deleting from request.");
10922 ippDeleteAttribute(con
->request
, name
);
10928 * Is the destination valid?
10931 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10937 send_ipp_status(con
, IPP_NOT_FOUND
,
10938 _("The printer or class does not exist."));
10947 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
10948 #endif /* HAVE_SSL */
10950 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10952 send_http_error(con
, status
, printer
);
10955 else if (printer
->num_auth_info_required
== 1 &&
10956 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
10959 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
10963 else if (auth_info
&& !con
->http
->tls
&&
10964 !httpAddrLocalhost(con
->http
->hostaddr
))
10967 * Require encryption of auth-info over non-local connections...
10970 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
10973 #endif /* HAVE_SSL */
10976 * Everything was ok, so return OK status...
10979 con
->response
->request
.status
.status_code
= IPP_OK
;
10984 * 'validate_name()' - Make sure the printer name only contains valid chars.
10987 static int /* O - 0 if name is no good, 1 if good */
10988 validate_name(const char *name
) /* I - Name to check */
10990 const char *ptr
; /* Pointer into name */
10994 * Scan the whole name...
10997 for (ptr
= name
; *ptr
; ptr
++)
10998 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11002 * All the characters are good; validate the length, too...
11005 return ((ptr
- name
) < 128);
11010 * 'validate_user()' - Validate the user for the request.
11013 static int /* O - 1 if permitted, 0 otherwise */
11014 validate_user(cupsd_job_t
*job
, /* I - Job */
11015 cupsd_client_t
*con
, /* I - Client connection */
11016 const char *owner
, /* I - Owner of job/resource */
11017 char *username
, /* O - Authenticated username */
11018 size_t userlen
) /* I - Length of username */
11020 cupsd_printer_t
*printer
; /* Printer for job */
11023 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
);
11026 * Validate input...
11029 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11033 * Get the best authenticated username that is available.
11036 strlcpy(username
, get_username(con
), userlen
);
11039 * Check the username against the owner...
11042 printer
= cupsdFindDest(job
->dest
);
11044 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11045 con
, owner
) == HTTP_OK
);
11050 * End of "$Id: ipp.c 12701 2015-06-08 18:33:44Z msweet $".