2 * "$Id: ipp.c 12778 2015-07-07 17:28:51Z 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
;
3293 * 'cancel_job()' - Cancel a print job.
3297 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3298 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3300 ipp_attribute_t
*attr
; /* Current attribute */
3301 int jobid
; /* Job ID */
3302 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3303 username
[HTTP_MAX_URI
], /* Username portion of URI */
3304 host
[HTTP_MAX_URI
], /* Host portion of URI */
3305 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3306 int port
; /* Port portion of URI */
3307 cupsd_job_t
*job
; /* Job information */
3308 cups_ptype_t dtype
; /* Destination type (printer/class) */
3309 cupsd_printer_t
*printer
; /* Printer data */
3310 cupsd_jobaction_t purge
; /* Purge the job? */
3313 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3314 con
->number
, uri
->values
[0].string
.text
);
3317 * See if we have a job URI or a printer URI...
3320 if (!strcmp(uri
->name
, "printer-uri"))
3323 * Got a printer URI; see if we also have a job-id attribute...
3326 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3327 IPP_TAG_INTEGER
)) == NULL
)
3329 send_ipp_status(con
, IPP_BAD_REQUEST
,
3330 _("Got a printer-uri attribute but no job-id."));
3334 if ((jobid
= attr
->values
[0].integer
) == 0)
3337 * Find the current job on the specified printer...
3340 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3346 send_ipp_status(con
, IPP_NOT_FOUND
,
3347 _("The printer or class does not exist."));
3352 * See if there are any pending jobs...
3355 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3357 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3358 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3359 !_cups_strcasecmp(job
->dest
, printer
->name
))
3367 * No, try stopped jobs...
3370 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3372 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3373 if (job
->state_value
== IPP_JOB_STOPPED
&&
3374 !_cups_strcasecmp(job
->dest
, printer
->name
))
3381 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s."),
3391 * Got a job URI; parse it to get the job ID...
3394 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3395 sizeof(scheme
), username
, sizeof(username
), host
,
3396 sizeof(host
), &port
, resource
, sizeof(resource
));
3398 if (strncmp(resource
, "/jobs/", 6))
3404 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
3405 uri
->values
[0].string
.text
);
3409 jobid
= atoi(resource
+ 6);
3413 * Look for the "purge-job" attribute...
3416 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
3417 IPP_TAG_BOOLEAN
)) != NULL
)
3418 purge
= attr
->values
[0].boolean
? CUPSD_JOB_PURGE
: CUPSD_JOB_DEFAULT
;
3420 purge
= CUPSD_JOB_DEFAULT
;
3423 * See if the job exists...
3426 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3429 * Nope - return a "not found" error...
3432 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
3437 * See if the job is owned by the requesting user...
3440 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3442 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3443 cupsdFindDest(job
->dest
));
3448 * See if the job is already completed, canceled, or aborted; if so,
3449 * we can't cancel...
3452 if (job
->state_value
>= IPP_JOB_CANCELED
&& purge
!= CUPSD_JOB_PURGE
)
3454 switch (job
->state_value
)
3456 case IPP_JOB_CANCELED
:
3457 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3458 _("Job #%d is already canceled - can\'t cancel."),
3462 case IPP_JOB_ABORTED
:
3463 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3464 _("Job #%d is already aborted - can\'t cancel."),
3469 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3470 _("Job #%d is already completed - can\'t cancel."),
3479 * Cancel the job and return...
3482 cupsdSetJobState(job
, IPP_JOB_CANCELED
, purge
,
3483 purge
== CUPSD_JOB_PURGE
? "Job purged by \"%s\"" :
3484 "Job canceled by \"%s\"",
3488 if (purge
== CUPSD_JOB_PURGE
)
3489 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
3492 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
3495 con
->response
->request
.status
.status_code
= IPP_OK
;
3500 * 'cancel_subscription()' - Cancel a subscription.
3504 cancel_subscription(
3505 cupsd_client_t
*con
, /* I - Client connection */
3506 int sub_id
) /* I - Subscription ID */
3508 http_status_t status
; /* Policy status */
3509 cupsd_subscription_t
*sub
; /* Subscription */
3512 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3513 "cancel_subscription(con=%p[%d], sub_id=%d)",
3514 con
, con
->number
, sub_id
);
3517 * Is the subscription ID valid?
3520 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
3523 * Bad subscription ID...
3526 send_ipp_status(con
, IPP_NOT_FOUND
,
3527 _("Subscription #%d does not exist."), sub_id
);
3535 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
3537 con
, sub
->owner
)) != HTTP_OK
)
3539 send_http_error(con
, status
, sub
->dest
);
3544 * Cancel the subscription...
3547 cupsdDeleteSubscription(sub
, 1);
3549 con
->response
->request
.status
.status_code
= IPP_OK
;
3554 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
3557 static int /* O - 1 if OK, 0 if not */
3558 check_rss_recipient(
3559 const char *recipient
) /* I - Recipient URI */
3561 cupsd_subscription_t
*sub
; /* Current subscription */
3564 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
3566 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
3570 * Compare the URIs up to the first ?...
3573 const char *r1
, *r2
;
3575 for (r1
= recipient
, r2
= sub
->recipient
;
3576 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
3588 * 'check_quotas()' - Check quotas for a printer and user.
3591 static int /* O - 1 if OK, 0 if forbidden,
3592 -1 if limit reached */
3593 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
3594 cupsd_printer_t
*p
) /* I - Printer or class */
3596 char username
[33], /* Username */
3597 *name
; /* Current user name */
3598 cupsd_quota_t
*q
; /* Quota data */
3599 #ifdef HAVE_MBR_UID_TO_UUID
3601 * Use Apple membership APIs which require that all names represent
3602 * valid user account or group records accessible by the server.
3605 uuid_t usr_uuid
; /* UUID for job requesting user */
3606 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
3607 uuid_t grp_uuid
; /* UUID for ACL group name entry */
3608 int mbr_err
; /* Error from membership function */
3609 int is_member
; /* Is this user a member? */
3612 * Use standard POSIX APIs for checking users and groups...
3615 struct passwd
*pw
; /* User password data */
3616 #endif /* HAVE_MBR_UID_TO_UUID */
3619 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
3620 con
, con
->number
, p
, p
->name
);
3623 * Figure out who is printing...
3626 strlcpy(username
, get_username(con
), sizeof(username
));
3628 if ((name
= strchr(username
, '@')) != NULL
)
3629 *name
= '\0'; /* Strip @REALM */
3632 * Check global active job limits for printers and users...
3635 if (MaxJobsPerPrinter
)
3638 * Check if there are too many pending jobs on this printer...
3641 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
3643 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
3652 * Check if there are too many pending jobs for this user...
3655 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
3657 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
3664 * Check against users...
3667 if (cupsArrayCount(p
->users
) == 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
3670 if (cupsArrayCount(p
->users
))
3672 #ifdef HAVE_MBR_UID_TO_UUID
3674 * Get UUID for job requesting user...
3677 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
3683 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3684 "check_quotas: UUID lookup failed for user \"%s\"",
3686 cupsdLogMessage(CUPSD_LOG_INFO
,
3687 "Denying user \"%s\" access to printer \"%s\" "
3688 "(unknown user)...",
3694 * Get UID and GID of requesting user...
3697 pw
= getpwnam(username
);
3699 #endif /* HAVE_MBR_UID_TO_UUID */
3701 for (name
= (char *)cupsArrayFirst(p
->users
);
3703 name
= (char *)cupsArrayNext(p
->users
))
3707 * Check group membership...
3710 #ifdef HAVE_MBR_UID_TO_UUID
3713 if (uuid_parse(name
+ 2, grp_uuid
))
3714 uuid_clear(grp_uuid
);
3716 else if ((mbr_err
= mbr_group_name_to_uuid(name
+ 1, grp_uuid
)) != 0)
3719 * Invalid ACL entries are ignored for matching; just record a
3720 * warning in the log...
3723 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3724 "check_quotas: UUID lookup failed for ACL entry "
3725 "\"%s\" (err=%d)", name
, mbr_err
);
3726 cupsdLogMessage(CUPSD_LOG_WARN
,
3727 "Access control entry \"%s\" not a valid group name; "
3728 "entry ignored", name
);
3731 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
3735 * At this point, there should be no errors, but check anyways...
3738 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3739 "check_quotas: group \"%s\" membership check "
3740 "failed (err=%d)", name
+ 1, mbr_err
);
3745 * Stop if we found a match...
3752 if (cupsdCheckGroup(username
, pw
, name
+ 1))
3754 #endif /* HAVE_MBR_UID_TO_UUID */
3756 #ifdef HAVE_MBR_UID_TO_UUID
3761 if (uuid_parse(name
+ 1, usr2_uuid
))
3762 uuid_clear(usr2_uuid
);
3764 else if ((mbr_err
= mbr_user_name_to_uuid(name
, usr2_uuid
)) != 0)
3767 * Invalid ACL entries are ignored for matching; just record a
3768 * warning in the log...
3771 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3772 "check_quotas: UUID lookup failed for ACL entry "
3773 "\"%s\" (err=%d)", name
, mbr_err
);
3774 cupsdLogMessage(CUPSD_LOG_WARN
,
3775 "Access control entry \"%s\" not a valid user name; "
3776 "entry ignored", name
);
3779 if (!uuid_compare(usr_uuid
, usr2_uuid
))
3783 else if (!_cups_strcasecmp(username
, name
))
3785 #endif /* HAVE_MBR_UID_TO_UUID */
3787 if ((name
!= NULL
) == p
->deny_users
)
3789 cupsdLogMessage(CUPSD_LOG_INFO
,
3790 "Denying user \"%s\" access to printer \"%s\"...",
3800 if (p
->k_limit
|| p
->page_limit
)
3802 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
3804 cupsdLogMessage(CUPSD_LOG_ERROR
,
3805 "Unable to allocate quota data for user \"%s\"",
3810 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
3811 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
3813 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
3820 * If we have gotten this far, we're done!
3828 * 'close_job()' - Close a multi-file job.
3832 close_job(cupsd_client_t
*con
, /* I - Client connection */
3833 ipp_attribute_t
*uri
) /* I - Printer URI */
3835 cupsd_job_t
*job
; /* Job */
3836 ipp_attribute_t
*attr
; /* Attribute */
3837 char job_uri
[HTTP_MAX_URI
],
3839 username
[256]; /* User name */
3842 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "close_job(%p[%d], %s)", con
,
3843 con
->number
, uri
->values
[0].string
.text
);
3846 * See if we have a job URI or a printer URI...
3849 if (strcmp(uri
->name
, "printer-uri"))
3852 * job-uri is not supported by Close-Job!
3855 send_ipp_status(con
, IPP_BAD_REQUEST
,
3856 _("Close-Job doesn't support the job-uri attribute."));
3861 * Got a printer URI; see if we also have a job-id attribute...
3864 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3865 IPP_TAG_INTEGER
)) == NULL
)
3867 send_ipp_status(con
, IPP_BAD_REQUEST
,
3868 _("Got a printer-uri attribute but no job-id."));
3872 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
3875 * Nope - return a "not found" error...
3878 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
3879 attr
->values
[0].integer
);
3884 * See if the job is owned by the requesting user...
3887 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3889 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
3890 cupsdFindDest(job
->dest
));
3895 * Add any ending sheet...
3898 if (cupsdTimeoutJob(job
))
3901 if (job
->state_value
== IPP_JOB_STOPPED
)
3903 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3904 job
->state_value
= IPP_JOB_PENDING
;
3906 else if (job
->state_value
== IPP_JOB_HELD
)
3908 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3909 IPP_TAG_KEYWORD
)) == NULL
)
3910 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3912 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
3914 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
3915 job
->state_value
= IPP_JOB_PENDING
;
3920 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
3923 * Fill in the response info...
3926 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
3927 con
->clientname
, con
->clientport
, "/jobs/%d", job
->id
);
3928 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
3931 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
3933 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
3936 con
->response
->request
.status
.status_code
= IPP_OK
;
3939 * Start the job if necessary...
3947 * 'copy_attrs()' - Copy attributes from one request to another.
3951 copy_attrs(ipp_t
*to
, /* I - Destination request */
3952 ipp_t
*from
, /* I - Source request */
3953 cups_array_t
*ra
, /* I - Requested attributes */
3954 ipp_tag_t group
, /* I - Group to copy */
3955 int quickcopy
, /* I - Do a quick copy? */
3956 cups_array_t
*exclude
) /* I - Attributes to exclude? */
3958 ipp_attribute_t
*fromattr
; /* Source attribute */
3961 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3962 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
3963 to
, from
, ra
, group
, quickcopy
);
3968 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
3971 * Filter attributes as needed...
3974 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
3975 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
3978 if (!strcmp(fromattr
->name
, "document-password") ||
3979 !strcmp(fromattr
->name
, "job-authorization-uri") ||
3980 !strcmp(fromattr
->name
, "job-password") ||
3981 !strcmp(fromattr
->name
, "job-password-encryption") ||
3982 !strcmp(fromattr
->name
, "job-printer-uri"))
3986 (cupsArrayFind(exclude
, fromattr
->name
) ||
3987 cupsArrayFind(exclude
, "all")))
3990 * We need to exclude this attribute for security reasons; we require the
3991 * job-id attribute regardless of the security settings for IPP
3994 * The job-printer-uri attribute is handled by copy_job_attrs().
3996 * Subscription attribute security is handled by copy_subscription_attrs().
3999 if (strcmp(fromattr
->name
, "job-id"))
4003 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4006 * Don't send collection attributes by default to IPP/1.x clients
4007 * since many do not support collections. Also don't send
4008 * media-col-database unless specifically requested by the client.
4011 if (fromattr
->value_tag
== IPP_TAG_BEGIN_COLLECTION
&&
4013 (to
->request
.status
.version
[0] == 1 ||
4014 !strcmp(fromattr
->name
, "media-col-database")))
4017 ippCopyAttribute(to
, fromattr
, quickcopy
);
4024 * 'copy_banner()' - Copy a banner file to the requests directory for the
4028 static int /* O - Size of banner file in kbytes */
4029 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4030 cupsd_job_t
*job
, /* I - Job information */
4031 const char *name
) /* I - Name of banner */
4033 int i
; /* Looping var */
4034 int kbytes
; /* Size of banner file in kbytes */
4035 char filename
[1024]; /* Job filename */
4036 cupsd_banner_t
*banner
; /* Pointer to banner */
4037 cups_file_t
*in
; /* Input file */
4038 cups_file_t
*out
; /* Output file */
4039 int ch
; /* Character from file */
4040 char attrname
[255], /* Name of attribute */
4041 *s
; /* Pointer into name */
4042 ipp_attribute_t
*attr
; /* Attribute */
4045 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4046 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4047 con
, con
? con
->number
: -1, job
, job
->id
,
4048 name
? name
: "(null)");
4051 * Find the banner; return if not found or "none"...
4054 if (!name
|| !strcmp(name
, "none") ||
4055 (banner
= cupsdFindBanner(name
)) == NULL
)
4059 * Open the banner and job files...
4062 if (add_file(con
, job
, banner
->filetype
, 0))
4065 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4067 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4069 cupsdLogMessage(CUPSD_LOG_ERROR
,
4070 "Unable to create banner job file %s - %s",
4071 filename
, strerror(errno
));
4076 fchmod(cupsFileNumber(out
), 0640);
4077 fchown(cupsFileNumber(out
), RunUser
, Group
);
4080 * Try the localized banner file under the subdirectory...
4083 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4085 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4088 * Convert ll-cc to ll_CC...
4092 attrname
[3] = (char)toupper(attrname
[3] & 255);
4093 attrname
[4] = (char)toupper(attrname
[4] & 255);
4096 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4099 if (access(filename
, 0) && strlen(attrname
) > 2)
4102 * Wasn't able to find "ll_CC" locale file; try the non-national
4103 * localization banner directory.
4108 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4112 if (access(filename
, 0))
4115 * Use the non-localized banner file.
4118 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4121 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4125 cupsdLogMessage(CUPSD_LOG_ERROR
,
4126 "Unable to open banner template file %s - %s",
4127 filename
, strerror(errno
));
4133 * Parse the file to the end...
4136 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4140 * Get an attribute name...
4143 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4144 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4146 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4156 * Ignore { followed by stuff that is not an attribute name...
4159 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4164 * See if it is defined...
4167 if (attrname
[0] == '?')
4172 if (!strcmp(s
, "printer-name"))
4174 cupsFilePuts(out
, job
->dest
);
4177 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4180 * See if we have a leading question mark...
4183 if (attrname
[0] != '?')
4186 * Nope, write to file as-is; probably a PostScript procedure...
4189 cupsFilePrintf(out
, "{%s}", attrname
);
4196 * Output value(s)...
4199 for (i
= 0; i
< attr
->num_values
; i
++)
4202 cupsFilePutChar(out
, ',');
4204 switch (attr
->value_tag
)
4206 case IPP_TAG_INTEGER
:
4208 if (!strncmp(s
, "time-at-", 8))
4210 struct timeval tv
; /* Time value */
4212 tv
.tv_sec
= attr
->values
[i
].integer
;
4215 cupsFilePuts(out
, cupsdGetDateTime(&tv
, CUPSD_TIME_STANDARD
));
4218 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4221 case IPP_TAG_BOOLEAN
:
4222 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4225 case IPP_TAG_NOVALUE
:
4226 cupsFilePuts(out
, "novalue");
4229 case IPP_TAG_RANGE
:
4230 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4231 attr
->values
[i
].range
.upper
);
4234 case IPP_TAG_RESOLUTION
:
4235 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4236 attr
->values
[i
].resolution
.yres
,
4237 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4242 case IPP_TAG_STRING
:
4245 case IPP_TAG_KEYWORD
:
4246 case IPP_TAG_CHARSET
:
4247 case IPP_TAG_LANGUAGE
:
4248 if (!_cups_strcasecmp(banner
->filetype
->type
, "postscript"))
4251 * Need to quote strings for PS banners...
4256 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4258 if (*p
== '(' || *p
== ')' || *p
== '\\')
4260 cupsFilePutChar(out
, '\\');
4261 cupsFilePutChar(out
, *p
);
4263 else if (*p
< 32 || *p
> 126)
4264 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4266 cupsFilePutChar(out
, *p
);
4270 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4274 break; /* anti-compiler-warning-code */
4278 else if (ch
== '\\') /* Quoted char */
4280 ch
= cupsFileGetChar(in
);
4282 if (ch
!= '{') /* Only do special handling for \{ */
4283 cupsFilePutChar(out
, '\\');
4285 cupsFilePutChar(out
, ch
);
4288 cupsFilePutChar(out
, ch
);
4292 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4294 job
->koctets
+= kbytes
;
4296 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
4297 attr
->values
[0].integer
+= kbytes
;
4306 * 'copy_file()' - Copy a PPD file or interface script...
4309 static int /* O - 0 = success, -1 = error */
4310 copy_file(const char *from
, /* I - Source file */
4311 const char *to
) /* I - Destination file */
4313 cups_file_t
*src
, /* Source file */
4314 *dst
; /* Destination file */
4315 int bytes
; /* Bytes to read/write */
4316 char buffer
[2048]; /* Copy buffer */
4319 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
4322 * Open the source and destination file for a copy...
4325 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
4328 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
4335 * Copy the source file to the destination...
4338 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
4339 if (cupsFileWrite(dst
, buffer
, (size_t)bytes
) < bytes
)
4347 * Close both files and return...
4352 return (cupsFileClose(dst
));
4357 * 'copy_model()' - Copy a PPD model file, substituting default values
4361 static int /* O - 0 = success, -1 = error */
4362 copy_model(cupsd_client_t
*con
, /* I - Client connection */
4363 const char *from
, /* I - Source file */
4364 const char *to
) /* I - Destination file */
4366 fd_set input
; /* select() input set */
4367 struct timeval timeout
; /* select() timeout */
4368 int maxfd
; /* Max file descriptor for select() */
4369 char tempfile
[1024]; /* Temporary PPD file */
4370 int tempfd
; /* Temporary PPD file descriptor */
4371 int temppid
; /* Process ID of cups-driverd */
4372 int temppipe
[2]; /* Temporary pipes */
4373 char *argv
[4], /* Command-line arguments */
4374 *envp
[MAX_ENV
]; /* Environment */
4375 cups_file_t
*src
, /* Source file */
4376 *dst
; /* Destination file */
4377 ppd_file_t
*ppd
; /* PPD file */
4378 int bytes
, /* Bytes from pipe */
4379 total
; /* Total bytes from pipe */
4380 char buffer
[2048]; /* Copy buffer */
4381 int i
; /* Looping var */
4382 char option
[PPD_MAX_NAME
], /* Option name */
4383 choice
[PPD_MAX_NAME
]; /* Choice name */
4384 ppd_size_t
*size
; /* Default size */
4385 int num_defaults
; /* Number of default options */
4386 cups_option_t
*defaults
; /* Default options */
4387 char cups_protocol
[PPD_MAX_LINE
];
4388 /* cupsProtocol attribute */
4391 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4392 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
4396 * Run cups-driverd to get the PPD file...
4399 argv
[0] = "cups-driverd";
4401 argv
[2] = (char *)from
;
4404 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4406 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
4407 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->number
);
4408 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
4409 if (tempfd
< 0 || cupsdOpenPipe(temppipe
))
4412 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4413 "copy_model: Running \"cups-driverd cat %s\"...", from
);
4415 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
4416 -1, -1, 0, DefaultProfile
, NULL
, &temppid
))
4427 * Wait up to 30 seconds for the PPD file to be copied...
4432 if (temppipe
[0] > CGIPipes
[0])
4433 maxfd
= temppipe
[0] + 1;
4435 maxfd
= CGIPipes
[0] + 1;
4440 * See if we have data ready...
4444 FD_SET(temppipe
[0], &input
);
4445 FD_SET(CGIPipes
[0], &input
);
4447 timeout
.tv_sec
= 30;
4448 timeout
.tv_usec
= 0;
4450 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
4460 * We have timed out...
4466 if (FD_ISSET(temppipe
[0], &input
))
4469 * Read the PPD file from the pipe, and write it to the PPD file.
4472 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
4474 if (write(tempfd
, buffer
, (size_t)bytes
) < bytes
)
4483 if (FD_ISSET(CGIPipes
[0], &input
))
4493 * No data from cups-deviced...
4496 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file");
4502 * Open the source file for a copy...
4505 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
4512 * Read the source file and see what page sizes are supported...
4515 if ((ppd
= _ppdOpen(src
, _PPD_LOCALIZATION_NONE
)) == NULL
)
4523 * Open the destination (if possible) and set the default options...
4528 cups_protocol
[0] = '\0';
4530 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
4533 * Read all of the default lines from the old PPD...
4536 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
4537 if (!strncmp(buffer
, "*Default", 8))
4540 * Add the default option...
4543 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4544 choice
, sizeof(choice
)))
4546 ppd_option_t
*ppdo
; /* PPD option */
4550 * Only add the default if the default hasn't already been
4551 * set and the choice exists in the new PPD...
4554 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
4555 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
4556 ppdFindChoice(ppdo
, choice
))
4557 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
4561 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
4562 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
4566 else if ((size
= ppdPageSize(ppd
, DefaultPaperSize
)) != NULL
)
4569 * Add the default media sizes...
4572 num_defaults
= cupsAddOption("PageSize", size
->name
,
4573 num_defaults
, &defaults
);
4574 num_defaults
= cupsAddOption("PageRegion", size
->name
,
4575 num_defaults
, &defaults
);
4576 num_defaults
= cupsAddOption("PaperDimension", size
->name
,
4577 num_defaults
, &defaults
);
4578 num_defaults
= cupsAddOption("ImageableArea", size
->name
,
4579 num_defaults
, &defaults
);
4585 * Open the destination file for a copy...
4588 if ((dst
= cupsdCreateConfFile(to
, ConfigFilePerm
)) == NULL
)
4590 cupsFreeOptions(num_defaults
, defaults
);
4597 * Copy the source file to the destination...
4600 cupsFileRewind(src
);
4602 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
4604 if (!strncmp(buffer
, "*Default", 8))
4607 * Check for an previous default option choice...
4610 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
4611 choice
, sizeof(choice
)))
4613 const char *val
; /* Default option value */
4616 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
4619 * Substitute the previous choice...
4622 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
4627 cupsFilePrintf(dst
, "%s\n", buffer
);
4630 if (cups_protocol
[0])
4631 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
4633 cupsFreeOptions(num_defaults
, defaults
);
4636 * Close both files and return...
4643 return (cupsdCloseCreatedConfFile(dst
, to
));
4648 * 'copy_job_attrs()' - Copy job attributes.
4652 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
4653 cupsd_job_t
*job
, /* I - Job */
4654 cups_array_t
*ra
, /* I - Requested attributes array */
4655 cups_array_t
*exclude
) /* I - Private attributes array */
4657 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
4661 * Send the requested attributes for each job...
4664 if (!cupsArrayFind(exclude
, "all"))
4666 if ((!exclude
|| !cupsArrayFind(exclude
, "number-of-documents")) &&
4667 (!ra
|| cupsArrayFind(ra
, "number-of-documents")))
4668 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4669 "number-of-documents", job
->num_files
);
4671 if ((!exclude
|| !cupsArrayFind(exclude
, "job-media-progress")) &&
4672 (!ra
|| cupsArrayFind(ra
, "job-media-progress")))
4673 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4674 "job-media-progress", job
->progress
);
4676 if ((!exclude
|| !cupsArrayFind(exclude
, "job-more-info")) &&
4677 (!ra
|| cupsArrayFind(ra
, "job-more-info")))
4679 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "http",
4680 NULL
, con
->clientname
, con
->clientport
, "/jobs/%d",
4682 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4683 "job-more-info", NULL
, job_uri
);
4686 if (job
->state_value
> IPP_JOB_PROCESSING
&&
4687 (!exclude
|| !cupsArrayFind(exclude
, "job-preserved")) &&
4688 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
4689 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
4690 job
->num_files
> 0);
4692 if ((!exclude
|| !cupsArrayFind(exclude
, "job-printer-up-time")) &&
4693 (!ra
|| cupsArrayFind(ra
, "job-printer-up-time")))
4694 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
4695 "job-printer-up-time", time(NULL
));
4698 if (!ra
|| cupsArrayFind(ra
, "job-printer-uri"))
4700 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4701 con
->clientname
, con
->clientport
,
4702 (job
->dtype
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
4705 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4706 "job-printer-uri", NULL
, job_uri
);
4709 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
4711 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
4712 con
->clientname
, con
->clientport
, "/jobs/%d",
4714 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
4715 "job-uri", NULL
, job_uri
);
4720 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0, exclude
);
4725 * Generate attributes from the job structure...
4728 if (!ra
|| cupsArrayFind(ra
, "job-id"))
4729 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
4731 if (!ra
|| cupsArrayFind(ra
, "job-k-octets"))
4732 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", job
->koctets
);
4734 if (job
->name
&& (!ra
|| cupsArrayFind(ra
, "job-name")))
4735 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-name", NULL
, job
->name
);
4737 if (job
->username
&& (!ra
|| cupsArrayFind(ra
, "job-originating-user-name")))
4738 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-originating-user-name", NULL
, job
->username
);
4740 if (!ra
|| cupsArrayFind(ra
, "job-state"))
4741 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state_value
);
4743 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
4745 switch (job
->state_value
)
4747 default : /* Should never get here for processing, pending, held, or stopped jobs since they don't get unloaded... */
4749 case IPP_JSTATE_ABORTED
:
4750 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-aborted-by-system");
4752 case IPP_JSTATE_CANCELED
:
4753 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-canceled-by-user");
4755 case IPP_JSTATE_COMPLETED
:
4756 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons", NULL
, "job-completed-successfully");
4761 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-completed")))
4762 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-completed", (int)job
->completed_time
);
4764 if (job
->completed_time
&& (!ra
|| cupsArrayFind(ra
, "time-at-creation")))
4765 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)job
->creation_time
);
4771 * 'copy_printer_attrs()' - Copy printer attributes.
4776 cupsd_client_t
*con
, /* I - Client connection */
4777 cupsd_printer_t
*printer
, /* I - Printer */
4778 cups_array_t
*ra
) /* I - Requested attributes array */
4780 char printer_uri
[HTTP_MAX_URI
];
4782 char printer_icons
[HTTP_MAX_URI
];
4784 time_t curtime
; /* Current time */
4785 int i
; /* Looping var */
4789 * Copy the printer attributes to the response using requested-attributes
4790 * and document-format attributes that may be provided by the client.
4793 curtime
= time(NULL
);
4795 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
4796 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4797 "marker-change-time", printer
->marker_time
);
4799 if (printer
->num_printers
> 0 &&
4800 (!ra
|| cupsArrayFind(ra
, "member-uris")))
4802 ipp_attribute_t
*member_uris
; /* member-uris attribute */
4803 cupsd_printer_t
*p2
; /* Printer in class */
4804 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
4807 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
4808 IPP_TAG_URI
, "member-uris",
4809 printer
->num_printers
, NULL
,
4812 for (i
= 0; i
< printer
->num_printers
; i
++)
4814 p2
= printer
->printers
[i
];
4816 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
4817 IPP_TAG_URI
)) != NULL
)
4818 member_uris
->values
[i
].string
.text
=
4819 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
4822 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
4823 sizeof(printer_uri
), "ipp", NULL
, con
->clientname
,
4825 (p2
->type
& CUPS_PRINTER_CLASS
) ?
4826 "/classes/%s" : "/printers/%s", p2
->name
);
4827 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
4833 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
4834 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
4835 "printer-alert", NULL
, printer
->alert
);
4837 if (printer
->alert_description
&&
4838 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
4839 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4840 "printer-alert-description", NULL
,
4841 printer
->alert_description
);
4843 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
4844 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
4845 ippTimeToDate(curtime
));
4847 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
4848 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
4850 if (printer
->reg_name
)
4851 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4852 "printer-dns-sd-name", NULL
, printer
->reg_name
);
4854 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
4855 "printer-dns-sd-name", 0);
4857 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4859 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
4860 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4861 "printer-error-policy", NULL
, printer
->error_policy
);
4863 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy-supported"))
4865 static const char * const errors
[] =/* printer-error-policy-supported values */
4868 "retry-current-job",
4873 if (printer
->type
& CUPS_PRINTER_CLASS
)
4874 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4875 "printer-error-policy-supported", NULL
, "retry-current-job");
4877 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
4878 "printer-error-policy-supported",
4879 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
4882 if (!ra
|| cupsArrayFind(ra
, "printer-icons"))
4884 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_icons
, sizeof(printer_icons
),
4885 "http", NULL
, con
->clientname
, con
->clientport
,
4886 "/icons/%s.png", printer
->name
);
4887 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons",
4888 NULL
, printer_icons
);
4889 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-icons=\"%s\"", printer_icons
);
4892 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
4893 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", (char)printer
->accepting
);
4895 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
4896 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared", (char)printer
->shared
);
4898 if (!ra
|| cupsArrayFind(ra
, "printer-more-info"))
4900 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4901 "http", NULL
, con
->clientname
, con
->clientport
,
4902 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4903 "/classes/%s" : "/printers/%s", printer
->name
);
4904 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4905 "printer-more-info", NULL
, printer_uri
);
4908 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
4909 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4910 "printer-op-policy", NULL
, printer
->op_policy
);
4912 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
4913 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
4916 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
4917 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4918 "printer-state-change-time", printer
->state_time
);
4920 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
4921 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4922 "printer-state-message", NULL
, printer
->state_message
);
4924 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
4925 add_printer_state_reasons(con
, printer
);
4927 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
4929 cups_ptype_t type
; /* printer-type value */
4932 * Add the CUPS-specific printer-type attribute...
4935 type
= printer
->type
;
4937 if (printer
== DefaultPrinter
)
4938 type
|= CUPS_PRINTER_DEFAULT
;
4940 if (!printer
->accepting
)
4941 type
|= CUPS_PRINTER_REJECTING
;
4943 if (!printer
->shared
)
4944 type
|= CUPS_PRINTER_NOT_SHARED
;
4946 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type", (int)type
);
4949 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
4950 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4951 "printer-up-time", curtime
);
4953 if (!ra
|| cupsArrayFind(ra
, "printer-uri-supported"))
4955 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
4956 "ipp", NULL
, con
->clientname
, con
->clientport
,
4957 (printer
->type
& CUPS_PRINTER_CLASS
) ?
4958 "/classes/%s" : "/printers/%s", printer
->name
);
4959 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4960 "printer-uri-supported", NULL
, printer_uri
);
4961 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
4965 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
4966 add_queued_job_count(con
, printer
);
4968 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
4969 if (printer
->ppd_attrs
)
4970 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0, NULL
);
4971 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
, NULL
);
4976 * 'copy_subscription_attrs()' - Copy subscription attributes.
4980 copy_subscription_attrs(
4981 cupsd_client_t
*con
, /* I - Client connection */
4982 cupsd_subscription_t
*sub
, /* I - Subscription */
4983 cups_array_t
*ra
, /* I - Requested attributes array */
4984 cups_array_t
*exclude
) /* I - Private attributes array */
4986 ipp_attribute_t
*attr
; /* Current attribute */
4987 char printer_uri
[HTTP_MAX_URI
];
4989 int count
; /* Number of events */
4990 unsigned mask
; /* Current event mask */
4991 const char *name
; /* Current event name */
4994 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4995 "copy_subscription_attrs(con=%p, sub=%p, ra=%p, exclude=%p)",
4996 con
, sub
, ra
, exclude
);
4999 * Copy the subscription attributes to the response using the
5000 * requested-attributes attribute that may be provided by the client.
5003 if (!exclude
|| !cupsArrayFind(exclude
, "all"))
5005 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-events")) &&
5006 (!ra
|| cupsArrayFind(ra
, "notify-events")))
5008 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_subscription_attrs: notify-events");
5010 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5013 * Simple event list...
5016 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5017 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5018 "notify-events", NULL
, name
);
5023 * Complex event list...
5026 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5027 if (sub
->mask
& mask
)
5030 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5031 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5032 "notify-events", count
, NULL
, NULL
);
5034 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5035 if (sub
->mask
& mask
)
5037 attr
->values
[count
].string
.text
=
5038 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5045 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-lease-duration")) &&
5046 (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration"))))
5047 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5048 "notify-lease-duration", sub
->lease
);
5050 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-recipient-uri")) &&
5051 (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri"))))
5052 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5053 "notify-recipient-uri", NULL
, sub
->recipient
);
5054 else if ((!exclude
|| !cupsArrayFind(exclude
, "notify-pull-method")) &&
5055 (!ra
|| cupsArrayFind(ra
, "notify-pull-method")))
5056 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5057 "notify-pull-method", NULL
, "ippget");
5059 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-subscriber-user-name")) &&
5060 (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name")))
5061 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5062 "notify-subscriber-user-name", NULL
, sub
->owner
);
5064 if ((!exclude
|| !cupsArrayFind(exclude
, "notify-time-interval")) &&
5065 (!ra
|| cupsArrayFind(ra
, "notify-time-interval")))
5066 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5067 "notify-time-interval", sub
->interval
);
5069 if (sub
->user_data_len
> 0 &&
5070 (!exclude
|| !cupsArrayFind(exclude
, "notify-user-data")) &&
5071 (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5072 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5073 sub
->user_data
, sub
->user_data_len
);
5076 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5077 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5078 "notify-job-id", sub
->job
->id
);
5080 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5082 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5083 "ipp", NULL
, con
->clientname
, con
->clientport
,
5084 "/printers/%s", sub
->dest
->name
);
5085 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5086 "notify-printer-uri", NULL
, printer_uri
);
5089 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5090 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5091 "notify-subscription-id", sub
->id
);
5096 * 'create_job()' - Print a file to a printer or class.
5100 create_job(cupsd_client_t
*con
, /* I - Client connection */
5101 ipp_attribute_t
*uri
) /* I - Printer URI */
5103 int i
; /* Looping var */
5104 cupsd_printer_t
*printer
; /* Printer */
5105 cupsd_job_t
*job
; /* New job */
5106 static const char * const forbidden_attrs
[] =
5107 { /* List of forbidden attributes */
5111 "document-natural-language"
5115 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5116 con
->number
, uri
->values
[0].string
.text
);
5119 * Is the destination valid?
5122 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5128 send_ipp_status(con
, IPP_NOT_FOUND
,
5129 _("The printer or class does not exist."));
5134 * Check for invalid Create-Job attributes and log a warning or error depending
5135 * on whether cupsd is running in "strict conformance" mode...
5139 i
< (int)(sizeof(forbidden_attrs
) / sizeof(forbidden_attrs
[0]));
5141 if (ippFindAttribute(con
->request
, forbidden_attrs
[i
], IPP_TAG_ZERO
))
5143 if (StrictConformance
)
5145 send_ipp_status(con
, IPP_BAD_REQUEST
,
5146 _("The '%s' operation attribute cannot be supplied in a "
5147 "Create-Job request."), forbidden_attrs
[i
]);
5151 cupsdLogMessage(CUPSD_LOG_WARN
,
5152 "Unexpected '%s' operation attribute in a Create-Job "
5153 "request.", forbidden_attrs
[i
]);
5157 * Create the job object...
5160 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5163 job
->pending_timeout
= 1;
5166 * Save and log the job...
5169 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5170 job
->dest
, job
->username
);
5175 * 'create_requested_array()' - Create an array for the requested-attributes.
5178 static cups_array_t
* /* O - Array of attributes or NULL */
5179 create_requested_array(ipp_t
*request
) /* I - IPP request */
5181 cups_array_t
*ra
; /* Requested attributes array */
5185 * Create the array for standard attributes...
5188 ra
= ippCreateRequestedArray(request
);
5191 * Add CUPS defaults as needed...
5194 if (cupsArrayFind(ra
, "printer-defaults"))
5197 * Include user-set defaults...
5200 char *name
; /* Option name */
5202 cupsArrayRemove(ra
, "printer-defaults");
5204 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5206 name
= (char *)cupsArrayNext(CommonDefaults
))
5207 if (!cupsArrayFind(ra
, name
))
5208 cupsArrayAdd(ra
, name
);
5216 * 'create_subscriptions()' - Create one or more notification subscriptions.
5220 create_subscriptions(
5221 cupsd_client_t
*con
, /* I - Client connection */
5222 ipp_attribute_t
*uri
) /* I - Printer URI */
5224 http_status_t status
; /* Policy status */
5225 int i
; /* Looping var */
5226 ipp_attribute_t
*attr
; /* Current attribute */
5227 cups_ptype_t dtype
; /* Destination type (printer/class) */
5228 char scheme
[HTTP_MAX_URI
],
5229 /* Scheme portion of URI */
5230 userpass
[HTTP_MAX_URI
],
5231 /* Username portion of URI */
5233 /* Host portion of URI */
5234 resource
[HTTP_MAX_URI
];
5235 /* Resource portion of URI */
5236 int port
; /* Port portion of URI */
5237 cupsd_printer_t
*printer
; /* Printer/class */
5238 cupsd_job_t
*job
; /* Job */
5239 int jobid
; /* Job ID */
5240 cupsd_subscription_t
*sub
; /* Subscription object */
5241 const char *username
, /* requesting-user-name or
5242 authenticated username */
5243 *recipient
, /* notify-recipient-uri */
5244 *pullmethod
; /* notify-pull-method */
5245 ipp_attribute_t
*user_data
; /* notify-user-data */
5246 int interval
, /* notify-time-interval */
5247 lease
; /* notify-lease-duration */
5248 unsigned mask
; /* notify-events */
5249 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5250 *notify_lease
; /* notify-lease-duration(-default) */
5254 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5256 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5257 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5258 attr
->value_tag
, attr
->name
);
5260 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5265 * Is the destination valid?
5268 cupsdLogMessage(CUPSD_LOG_DEBUG
, "create_subscriptions(con=%p(%d), uri=\"%s\")", con
, con
->number
, uri
->values
[0].string
.text
);
5270 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5271 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5272 sizeof(host
), &port
, resource
, sizeof(resource
));
5274 if (!strcmp(resource
, "/"))
5276 dtype
= (cups_ptype_t
)0;
5279 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5281 dtype
= (cups_ptype_t
)0;
5284 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
5286 dtype
= CUPS_PRINTER_CLASS
;
5289 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5295 send_ipp_status(con
, IPP_NOT_FOUND
,
5296 _("The printer or class does not exist."));
5306 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
5309 send_http_error(con
, status
, printer
);
5313 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5315 send_http_error(con
, status
, NULL
);
5320 * Get the user that is requesting the subscription...
5323 username
= get_username(con
);
5326 * Find the first subscription group attribute; return if we have
5330 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5331 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
5336 send_ipp_status(con
, IPP_BAD_REQUEST
,
5337 _("No subscription attributes in request."));
5342 * Process the subscription attributes in the request...
5345 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
5353 lease
= DefaultLeaseDuration
;
5355 mask
= CUPSD_EVENT_NONE
;
5359 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
5361 notify_lease
= ippFindAttribute(printer
->attrs
,
5362 "notify-lease-duration-default",
5366 lease
= notify_lease
->values
[0].integer
;
5370 notify_events
= NULL
;
5371 notify_lease
= NULL
;
5374 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
5376 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
5377 attr
->value_tag
== IPP_TAG_URI
)
5380 * Validate the recipient scheme against the ServerBin/notifier
5384 char notifier
[1024]; /* Notifier filename */
5387 recipient
= attr
->values
[0].string
.text
;
5389 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
5390 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
5391 host
, sizeof(host
), &port
,
5392 resource
, sizeof(resource
)) < HTTP_URI_OK
)
5394 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5395 _("Bad notify-recipient-uri \"%s\"."), recipient
);
5396 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5397 "notify-status-code", IPP_URI_SCHEME
);
5401 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
5403 if (access(notifier
, X_OK
))
5405 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5406 _("notify-recipient-uri URI \"%s\" uses unknown "
5407 "scheme."), recipient
);
5408 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5409 "notify-status-code", IPP_URI_SCHEME
);
5413 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
5415 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5416 _("notify-recipient-uri URI \"%s\" is already used."),
5418 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5419 "notify-status-code", IPP_ATTRIBUTES
);
5423 else if (!strcmp(attr
->name
, "notify-pull-method") &&
5424 attr
->value_tag
== IPP_TAG_KEYWORD
)
5426 pullmethod
= attr
->values
[0].string
.text
;
5428 if (strcmp(pullmethod
, "ippget"))
5430 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
5431 _("Bad notify-pull-method \"%s\"."), pullmethod
);
5432 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
5433 "notify-status-code", IPP_ATTRIBUTES
);
5437 else if (!strcmp(attr
->name
, "notify-charset") &&
5438 attr
->value_tag
== IPP_TAG_CHARSET
&&
5439 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
5440 strcmp(attr
->values
[0].string
.text
, "utf-8"))
5442 send_ipp_status(con
, IPP_CHARSET
,
5443 _("Character set \"%s\" not supported."),
5444 attr
->values
[0].string
.text
);
5447 else if (!strcmp(attr
->name
, "notify-natural-language") &&
5448 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
5449 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
5451 send_ipp_status(con
, IPP_CHARSET
,
5452 _("Language \"%s\" not supported."),
5453 attr
->values
[0].string
.text
);
5456 else if (!strcmp(attr
->name
, "notify-user-data") &&
5457 attr
->value_tag
== IPP_TAG_STRING
)
5459 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
5461 send_ipp_status(con
, IPP_REQUEST_VALUE
,
5462 _("The notify-user-data value is too large "
5463 "(%d > 63 octets)."),
5464 attr
->values
[0].unknown
.length
);
5470 else if (!strcmp(attr
->name
, "notify-events") &&
5471 attr
->value_tag
== IPP_TAG_KEYWORD
)
5472 notify_events
= attr
;
5473 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
5474 attr
->value_tag
== IPP_TAG_INTEGER
)
5475 lease
= attr
->values
[0].integer
;
5476 else if (!strcmp(attr
->name
, "notify-time-interval") &&
5477 attr
->value_tag
== IPP_TAG_INTEGER
)
5478 interval
= attr
->values
[0].integer
;
5479 else if (!strcmp(attr
->name
, "notify-job-id") &&
5480 attr
->value_tag
== IPP_TAG_INTEGER
)
5481 jobid
= attr
->values
[0].integer
;
5488 for (i
= 0; i
< notify_events
->num_values
; i
++)
5489 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
5493 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
5495 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
5496 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
5497 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
5499 if (!recipient
&& !pullmethod
)
5502 if (mask
== CUPSD_EVENT_NONE
)
5505 mask
= CUPSD_EVENT_JOB_COMPLETED
;
5507 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
5510 send_ipp_status(con
, IPP_BAD_REQUEST
,
5511 _("notify-events not specified."));
5516 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
5518 cupsdLogMessage(CUPSD_LOG_INFO
,
5519 "create_subscriptions: Limiting notify-lease-duration to "
5522 lease
= MaxLeaseDuration
;
5527 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5529 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
5537 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
5539 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
5540 _("There are too many subscriptions."));
5545 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for job %d.",
5548 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5549 "Added subscription #%d for printer \"%s\".",
5550 sub
->id
, printer
->name
);
5552 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription #%d for server.",
5555 sub
->interval
= interval
;
5557 sub
->expire
= lease
? time(NULL
) + lease
: 0;
5559 cupsdSetString(&sub
->owner
, username
);
5563 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
5564 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
5565 (size_t)sub
->user_data_len
);
5568 ippAddSeparator(con
->response
);
5569 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5570 "notify-subscription-id", sub
->id
);
5572 con
->response
->request
.status
.status_code
= IPP_OK
;
5578 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
5583 * 'delete_printer()' - Remove a printer or class from the system.
5587 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
5588 ipp_attribute_t
*uri
) /* I - URI of printer or class */
5590 http_status_t status
; /* Policy status */
5591 cups_ptype_t dtype
; /* Destination type (printer/class) */
5592 cupsd_printer_t
*printer
; /* Printer/class */
5593 char filename
[1024]; /* Script/PPD filename */
5596 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
5597 con
->number
, uri
->values
[0].string
.text
);
5600 * Do we have a valid URI?
5603 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
5609 send_ipp_status(con
, IPP_NOT_FOUND
,
5610 _("The printer or class does not exist."));
5618 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5620 send_http_error(con
, status
, NULL
);
5625 * Remove old jobs...
5628 cupsdCancelJobs(printer
->name
, NULL
, 1);
5631 * Remove old subscriptions and send a "deleted printer" event...
5634 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
5635 "%s \"%s\" deleted by \"%s\".",
5636 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
5637 printer
->name
, get_username(con
));
5639 cupsdExpireSubscriptions(printer
, NULL
);
5642 * Remove any old PPD or script files...
5645 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
5648 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s.O", ServerRoot
,
5652 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
5655 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd.O", ServerRoot
,
5659 snprintf(filename
, sizeof(filename
), "%s/%s.png", CacheDir
, printer
->name
);
5662 snprintf(filename
, sizeof(filename
), "%s/%s.data", CacheDir
, printer
->name
);
5666 * Unregister color profiles...
5669 cupsdUnregisterColor(printer
);
5671 if (dtype
& CUPS_PRINTER_CLASS
)
5673 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
5674 printer
->name
, get_username(con
));
5676 cupsdDeletePrinter(printer
, 0);
5677 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5681 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
5682 printer
->name
, get_username(con
));
5684 if (cupsdDeletePrinter(printer
, 0))
5685 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
5687 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
5690 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
5693 * Return with no errors...
5696 con
->response
->request
.status
.status_code
= IPP_OK
;
5701 * 'get_default()' - Get the default destination.
5705 get_default(cupsd_client_t
*con
) /* I - Client connection */
5707 http_status_t status
; /* Policy status */
5708 cups_array_t
*ra
; /* Requested attributes array */
5711 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->number
);
5717 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5719 send_http_error(con
, status
, NULL
);
5725 ra
= create_requested_array(con
->request
);
5727 copy_printer_attrs(con
, DefaultPrinter
, ra
);
5729 cupsArrayDelete(ra
);
5731 con
->response
->request
.status
.status_code
= IPP_OK
;
5734 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer."));
5739 * 'get_devices()' - Get the list of available devices on the local system.
5743 get_devices(cupsd_client_t
*con
) /* I - Client connection */
5745 http_status_t status
; /* Policy status */
5746 ipp_attribute_t
*limit
, /* limit attribute */
5747 *timeout
, /* timeout attribute */
5748 *requested
, /* requested-attributes attribute */
5749 *exclude
, /* exclude-schemes attribute */
5750 *include
; /* include-schemes attribute */
5751 char command
[1024], /* cups-deviced command */
5752 options
[2048], /* Options to pass to command */
5754 /* String for requested attributes */
5756 /* String for excluded schemes */
5758 /* String for included schemes */
5761 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->number
);
5767 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
5769 send_http_error(con
, status
, NULL
);
5774 * Run cups-deviced command with the given options...
5777 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
5778 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
5779 requested
= ippFindAttribute(con
->request
, "requested-attributes",
5781 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
5782 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
5785 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
5787 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
5790 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
5792 exclude_str
[0] = '\0';
5795 url_encode_attr(include
, include_str
, sizeof(include_str
));
5797 include_str
[0] = '\0';
5799 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
5800 snprintf(options
, sizeof(options
),
5801 "%d+%d+%d+%d+%s%s%s%s%s",
5802 con
->request
->request
.op
.request_id
,
5803 limit
? limit
->values
[0].integer
: 0,
5804 timeout
? timeout
->values
[0].integer
: 15,
5807 exclude_str
[0] ? "%20" : "", exclude_str
,
5808 include_str
[0] ? "%20" : "", include_str
);
5810 if (cupsdSendCommand(con
, command
, options
, 1))
5813 * Command started successfully, don't send an IPP response here...
5816 ippDelete(con
->response
);
5817 con
->response
= NULL
;
5822 * Command failed, return "internal error" so the user knows something
5826 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
5827 _("cups-deviced failed to execute."));
5833 * 'get_document()' - Get a copy of a job file.
5837 get_document(cupsd_client_t
*con
, /* I - Client connection */
5838 ipp_attribute_t
*uri
) /* I - Job URI */
5840 http_status_t status
; /* Policy status */
5841 ipp_attribute_t
*attr
; /* Current attribute */
5842 int jobid
; /* Job ID */
5843 int docnum
; /* Document number */
5844 cupsd_job_t
*job
; /* Current job */
5845 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
5846 username
[HTTP_MAX_URI
], /* Username portion of URI */
5847 host
[HTTP_MAX_URI
], /* Host portion of URI */
5848 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5849 int port
; /* Port portion of URI */
5850 char filename
[1024], /* Filename for document */
5851 format
[1024]; /* Format for document */
5854 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
5855 con
->number
, uri
->values
[0].string
.text
);
5858 * See if we have a job URI or a printer URI...
5861 if (!strcmp(uri
->name
, "printer-uri"))
5864 * Got a printer URI; see if we also have a job-id attribute...
5867 if ((attr
= ippFindAttribute(con
->request
, "job-id",
5868 IPP_TAG_INTEGER
)) == NULL
)
5870 send_ipp_status(con
, IPP_BAD_REQUEST
,
5871 _("Got a printer-uri attribute but no job-id."));
5875 jobid
= attr
->values
[0].integer
;
5880 * Got a job URI; parse it to get the job ID...
5883 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5884 sizeof(scheme
), username
, sizeof(username
), host
,
5885 sizeof(host
), &port
, resource
, sizeof(resource
));
5887 if (strncmp(resource
, "/jobs/", 6))
5893 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
5894 uri
->values
[0].string
.text
);
5898 jobid
= atoi(resource
+ 6);
5902 * See if the job exists...
5905 if ((job
= cupsdFindJob(jobid
)) == NULL
)
5908 * Nope - return a "not found" error...
5911 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
5919 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
,
5920 job
->username
)) != HTTP_OK
)
5922 send_http_error(con
, status
, NULL
);
5927 * Get the document number...
5930 if ((attr
= ippFindAttribute(con
->request
, "document-number",
5931 IPP_TAG_INTEGER
)) == NULL
)
5933 send_ipp_status(con
, IPP_BAD_REQUEST
,
5934 _("Missing document-number attribute."));
5938 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
5939 attr
->num_values
> 1)
5941 send_ipp_status(con
, IPP_NOT_FOUND
,
5942 _("Document #%d does not exist in job #%d."), docnum
,
5947 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
5949 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
5951 cupsdLogMessage(CUPSD_LOG_ERROR
,
5952 "Unable to open document %d in job %d - %s", docnum
, jobid
,
5954 send_ipp_status(con
, IPP_NOT_FOUND
,
5955 _("Unable to open document #%d in job #%d."), docnum
,
5960 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
5964 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
5965 job
->filetypes
[docnum
- 1]->type
);
5967 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
5969 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
5971 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
5972 IPP_TAG_NAME
)) != NULL
)
5973 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
5974 NULL
, attr
->values
[0].string
.text
);
5979 * 'get_job_attrs()' - Get job attributes.
5983 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5984 ipp_attribute_t
*uri
) /* I - Job URI */
5986 http_status_t status
; /* Policy status */
5987 ipp_attribute_t
*attr
; /* Current attribute */
5988 int jobid
; /* Job ID */
5989 cupsd_job_t
*job
; /* Current job */
5990 cupsd_printer_t
*printer
; /* Current printer */
5991 cupsd_policy_t
*policy
; /* Current security policy */
5992 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
5993 username
[HTTP_MAX_URI
], /* Username portion of URI */
5994 host
[HTTP_MAX_URI
], /* Host portion of URI */
5995 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
5996 int port
; /* Port portion of URI */
5997 cups_array_t
*ra
, /* Requested attributes array */
5998 *exclude
; /* Private attributes array */
6001 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6002 con
->number
, uri
->values
[0].string
.text
);
6005 * See if we have a job URI or a printer URI...
6008 if (!strcmp(uri
->name
, "printer-uri"))
6011 * Got a printer URI; see if we also have a job-id attribute...
6014 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6015 IPP_TAG_INTEGER
)) == NULL
)
6017 send_ipp_status(con
, IPP_BAD_REQUEST
,
6018 _("Got a printer-uri attribute but no job-id."));
6022 jobid
= attr
->values
[0].integer
;
6027 * Got a job URI; parse it to get the job ID...
6030 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6031 sizeof(scheme
), username
, sizeof(username
), host
,
6032 sizeof(host
), &port
, resource
, sizeof(resource
));
6034 if (strncmp(resource
, "/jobs/", 6))
6040 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
6041 uri
->values
[0].string
.text
);
6045 jobid
= atoi(resource
+ 6);
6049 * See if the job exists...
6052 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6055 * Nope - return a "not found" error...
6058 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
6066 if ((printer
= job
->printer
) == NULL
)
6067 printer
= cupsdFindDest(job
->dest
);
6070 policy
= printer
->op_policy_ptr
;
6072 policy
= DefaultPolicyPtr
;
6074 if ((status
= cupsdCheckPolicy(policy
, con
, job
->username
)) != HTTP_OK
)
6076 send_http_error(con
, status
, NULL
);
6080 exclude
= cupsdGetPrivateAttrs(policy
, con
, printer
, job
->username
);
6083 * Copy attributes...
6088 ra
= create_requested_array(con
->request
);
6089 copy_job_attrs(con
, job
, ra
, exclude
);
6090 cupsArrayDelete(ra
);
6092 con
->response
->request
.status
.status_code
= IPP_OK
;
6097 * 'get_jobs()' - Get a list of jobs for the specified printer.
6101 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6102 ipp_attribute_t
*uri
) /* I - Printer URI */
6104 http_status_t status
; /* Policy status */
6105 ipp_attribute_t
*attr
; /* Current attribute */
6106 const char *dest
; /* Destination */
6107 cups_ptype_t dtype
; /* Destination type (printer/class) */
6108 cups_ptype_t dmask
; /* Destination type mask */
6109 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6110 username
[HTTP_MAX_URI
], /* Username portion of URI */
6111 host
[HTTP_MAX_URI
], /* Host portion of URI */
6112 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6113 int port
; /* Port portion of URI */
6114 int job_comparison
; /* Job comparison */
6115 ipp_jstate_t job_state
; /* job-state value */
6116 int first_job_id
= 1, /* First job ID */
6117 first_index
= 1, /* First index */
6118 current_index
= 0; /* Current index */
6119 int limit
= 0; /* Maximum number of jobs to return */
6120 int count
; /* Number of jobs that match */
6121 int need_load_job
= 0; /* Do we need to load the job? */
6122 const char *job_attr
; /* Job attribute requested */
6123 ipp_attribute_t
*job_ids
; /* job-ids attribute */
6124 cupsd_job_t
*job
; /* Current job pointer */
6125 cupsd_printer_t
*printer
; /* Printer */
6126 cups_array_t
*list
; /* Which job list... */
6127 int delete_list
= 0; /* Delete the list afterwards? */
6128 cups_array_t
*ra
, /* Requested attributes array */
6129 *exclude
; /* Private attributes array */
6130 cupsd_policy_t
*policy
; /* Current policy */
6133 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->number
,
6134 uri
->values
[0].string
.text
);
6137 * Is the destination valid?
6140 if (strcmp(uri
->name
, "printer-uri"))
6142 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request."));
6146 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6147 sizeof(scheme
), username
, sizeof(username
), host
,
6148 sizeof(host
), &port
, resource
, sizeof(resource
));
6150 if (!strcmp(resource
, "/") || !strcmp(resource
, "/jobs"))
6153 dtype
= (cups_ptype_t
)0;
6154 dmask
= (cups_ptype_t
)0;
6157 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6160 dtype
= (cups_ptype_t
)0;
6161 dmask
= CUPS_PRINTER_CLASS
;
6164 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6167 dtype
= CUPS_PRINTER_CLASS
;
6168 dmask
= CUPS_PRINTER_CLASS
;
6171 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6178 send_ipp_status(con
, IPP_NOT_FOUND
,
6179 _("The printer or class does not exist."));
6184 dtype
&= CUPS_PRINTER_CLASS
;
6185 dmask
= CUPS_PRINTER_CLASS
;
6193 policy
= printer
->op_policy_ptr
;
6195 policy
= DefaultPolicyPtr
;
6197 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
6199 send_http_error(con
, status
, NULL
);
6203 job_ids
= ippFindAttribute(con
->request
, "job-ids", IPP_TAG_INTEGER
);
6206 * See if the "which-jobs" attribute have been specified...
6209 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6210 IPP_TAG_KEYWORD
)) != NULL
&& job_ids
)
6212 send_ipp_status(con
, IPP_CONFLICT
,
6213 _("The %s attribute cannot be provided with job-ids."),
6217 else if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
6219 job_comparison
= -1;
6220 job_state
= IPP_JOB_STOPPED
;
6223 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
6226 job_state
= IPP_JOB_CANCELED
;
6227 list
= cupsdGetCompletedJobs(printer
);
6230 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
6233 job_state
= IPP_JOB_ABORTED
;
6234 list
= cupsdGetCompletedJobs(printer
);
6237 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
6240 job_state
= IPP_JOB_PENDING
;
6243 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
6246 job_state
= IPP_JOB_CANCELED
;
6247 list
= cupsdGetCompletedJobs(printer
);
6250 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
6253 job_state
= IPP_JOB_PENDING
;
6256 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
6259 job_state
= IPP_JOB_HELD
;
6262 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
6265 job_state
= IPP_JOB_PROCESSING
;
6266 list
= PrintingJobs
;
6268 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
6271 job_state
= IPP_JOB_STOPPED
;
6276 send_ipp_status(con
, IPP_ATTRIBUTES
,
6277 _("The which-jobs value \"%s\" is not supported."),
6278 attr
->values
[0].string
.text
);
6279 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
6280 "which-jobs", NULL
, attr
->values
[0].string
.text
);
6285 * See if they want to limit the number of jobs reported...
6288 if ((attr
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
)) != NULL
)
6292 send_ipp_status(con
, IPP_CONFLICT
,
6293 _("The %s attribute cannot be provided with job-ids."),
6298 limit
= attr
->values
[0].integer
;
6301 if ((attr
= ippFindAttribute(con
->request
, "first-index", IPP_TAG_INTEGER
)) != NULL
)
6305 send_ipp_status(con
, IPP_CONFLICT
,
6306 _("The %s attribute cannot be provided with job-ids."),
6311 first_index
= attr
->values
[0].integer
;
6313 else if ((attr
= ippFindAttribute(con
->request
, "first-job-id", IPP_TAG_INTEGER
)) != NULL
)
6317 send_ipp_status(con
, IPP_CONFLICT
,
6318 _("The %s attribute cannot be provided with job-ids."),
6323 first_job_id
= attr
->values
[0].integer
;
6327 * See if we only want to see jobs for a specific user...
6330 if ((attr
= ippFindAttribute(con
->request
, "my-jobs", IPP_TAG_BOOLEAN
)) != NULL
&& job_ids
)
6332 send_ipp_status(con
, IPP_CONFLICT
,
6333 _("The %s attribute cannot be provided with job-ids."),
6337 else if (attr
&& attr
->values
[0].boolean
)
6338 strlcpy(username
, get_username(con
), sizeof(username
));
6342 ra
= create_requested_array(con
->request
);
6343 for (job_attr
= (char *)cupsArrayFirst(ra
); job_attr
; job_attr
= (char *)cupsArrayNext(ra
))
6344 if (strcmp(job_attr
, "job-id") &&
6345 strcmp(job_attr
, "job-k-octets") &&
6346 strcmp(job_attr
, "job-media-progress") &&
6347 strcmp(job_attr
, "job-more-info") &&
6348 strcmp(job_attr
, "job-name") &&
6349 strcmp(job_attr
, "job-originating-user-name") &&
6350 strcmp(job_attr
, "job-preserved") &&
6351 strcmp(job_attr
, "job-printer-up-time") &&
6352 strcmp(job_attr
, "job-printer-uri") &&
6353 strcmp(job_attr
, "job-state") &&
6354 strcmp(job_attr
, "job-state-reasons") &&
6355 strcmp(job_attr
, "job-uri") &&
6356 strcmp(job_attr
, "time-at-completed") &&
6357 strcmp(job_attr
, "time-at-creation") &&
6358 strcmp(job_attr
, "number-of-documents"))
6364 if (need_load_job
&& (limit
== 0 || limit
> 500) && (list
== Jobs
|| delete_list
))
6367 * Limit expensive Get-Jobs for job history to 500 jobs...
6370 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "limit", 500);
6373 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
, "limit", limit
);
6377 cupsdLogClient(con
, CUPSD_LOG_INFO
, "Limiting Get-Jobs response to %d jobs.", limit
);
6381 * OK, build a list of jobs for this printer...
6386 int i
; /* Looping var */
6388 for (i
= 0; i
< job_ids
->num_values
; i
++)
6390 if (!cupsdFindJob(job_ids
->values
[i
].integer
))
6394 if (i
< job_ids
->num_values
)
6396 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
6397 job_ids
->values
[i
].integer
);
6401 for (i
= 0; i
< job_ids
->num_values
; i
++)
6403 job
= cupsdFindJob(job_ids
->values
[i
].integer
);
6405 if (need_load_job
&& !job
->attrs
)
6411 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6417 ippAddSeparator(con
->response
);
6419 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6420 job
->printer
->op_policy_ptr
:
6421 policy
, con
, job
->printer
,
6424 copy_job_attrs(con
, job
, ra
, exclude
);
6429 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6430 (limit
<= 0 || count
< limit
) && job
;
6431 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6434 * Filter out jobs that don't match...
6437 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
6438 "get_jobs: job->id=%d, dest=\"%s\", username=\"%s\", "
6439 "state_value=%d, attrs=%p", job
->id
, job
->dest
,
6440 job
->username
, job
->state_value
, job
->attrs
);
6442 if (!job
->dest
|| !job
->username
)
6445 if (!job
->dest
|| !job
->username
)
6448 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6449 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6451 if ((job
->dtype
& dmask
) != dtype
&&
6452 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6455 if ((job_comparison
< 0 && job
->state_value
> job_state
) ||
6456 (job_comparison
== 0 && job
->state_value
!= job_state
) ||
6457 (job_comparison
> 0 && job
->state_value
< job_state
))
6460 if (job
->id
< first_job_id
)
6464 if (current_index
< first_index
)
6467 if (need_load_job
&& !job
->attrs
)
6473 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: No attributes for job %d", job
->id
);
6478 if (username
[0] && _cups_strcasecmp(username
, job
->username
))
6482 ippAddSeparator(con
->response
);
6486 exclude
= cupsdGetPrivateAttrs(job
->printer
?
6487 job
->printer
->op_policy_ptr
:
6488 policy
, con
, job
->printer
,
6491 copy_job_attrs(con
, job
, ra
, exclude
);
6494 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count=%d", count
);
6497 cupsArrayDelete(ra
);
6500 cupsArrayDelete(list
);
6502 con
->response
->request
.status
.status_code
= IPP_OK
;
6507 * 'get_notifications()' - Get events for a subscription.
6511 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
6513 int i
, j
; /* Looping vars */
6514 http_status_t status
; /* Policy status */
6515 cupsd_subscription_t
*sub
; /* Subscription */
6516 ipp_attribute_t
*ids
, /* notify-subscription-ids */
6517 *sequences
; /* notify-sequence-numbers */
6518 int min_seq
; /* Minimum sequence number */
6519 int interval
; /* Poll interval */
6522 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
6526 * Get subscription attributes...
6529 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
6531 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
6536 send_ipp_status(con
, IPP_BAD_REQUEST
,
6537 _("Missing notify-subscription-ids attribute."));
6542 * Are the subscription IDs valid?
6545 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
6547 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
6550 * Bad subscription ID...
6553 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
6554 ids
->values
[i
].integer
);
6562 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
6564 con
, sub
->owner
)) != HTTP_OK
)
6566 send_http_error(con
, status
, sub
->dest
);
6571 * Check the subscription type and update the interval accordingly.
6574 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
6577 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
6579 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
6585 * Tell the client to poll again in N seconds...
6589 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6590 "notify-get-interval", interval
);
6592 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
6593 "printer-up-time", time(NULL
));
6596 * Copy the subscription event attributes to the response.
6599 con
->response
->request
.status
.status_code
=
6600 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
6602 for (i
= 0; i
< ids
->num_values
; i
++)
6605 * Get the subscription and sequence number...
6608 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
6610 if (sequences
&& i
< sequences
->num_values
)
6611 min_seq
= sequences
->values
[i
].integer
;
6616 * If we don't have any new events, nothing to do here...
6619 if (min_seq
> (sub
->first_event_id
+ cupsArrayCount(sub
->events
)))
6623 * Otherwise copy all of the new events...
6626 if (sub
->first_event_id
> min_seq
)
6629 j
= min_seq
- sub
->first_event_id
;
6631 for (; j
< cupsArrayCount(sub
->events
); j
++)
6633 ippAddSeparator(con
->response
);
6635 copy_attrs(con
->response
,
6636 ((cupsd_event_t
*)cupsArrayIndex(sub
->events
, j
))->attrs
, NULL
,
6637 IPP_TAG_EVENT_NOTIFICATION
, 0, NULL
);
6644 * 'get_ppd()' - Get a named PPD from the local system.
6648 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
6649 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
6651 http_status_t status
; /* Policy status */
6652 cupsd_printer_t
*dest
; /* Destination */
6653 cups_ptype_t dtype
; /* Destination type */
6656 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
6657 con
->number
, uri
, uri
->name
, uri
->values
[0].string
.text
);
6659 if (!strcmp(uri
->name
, "ppd-name"))
6662 * Return a PPD file from cups-driverd...
6665 char command
[1024], /* cups-driverd command */
6666 options
[1024], /* Options to pass to command */
6667 ppd_name
[1024]; /* ppd-name */
6674 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6676 send_http_error(con
, status
, NULL
);
6681 * Run cups-driverd command with the given options...
6684 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6685 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
6686 snprintf(options
, sizeof(options
), "get+%d+%s",
6687 con
->request
->request
.op
.request_id
, ppd_name
);
6689 if (cupsdSendCommand(con
, command
, options
, 0))
6692 * Command started successfully, don't send an IPP response here...
6695 ippDelete(con
->response
);
6696 con
->response
= NULL
;
6701 * Command failed, return "internal error" so the user knows something
6705 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6706 _("cups-driverd failed to execute."));
6709 else if (!strcmp(uri
->name
, "printer-uri") &&
6710 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
6712 int i
; /* Looping var */
6713 char filename
[1024]; /* PPD filename */
6720 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6722 send_http_error(con
, status
, dest
);
6727 * See if we need the PPD for a class or remote printer...
6730 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6733 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
6735 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6736 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6737 "printer-uri", NULL
, dest
->uri
);
6740 else if (dtype
& CUPS_PRINTER_CLASS
)
6742 for (i
= 0; i
< dest
->num_printers
; i
++)
6743 if (!(dest
->printers
[i
]->type
& CUPS_PRINTER_CLASS
))
6745 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6746 dest
->printers
[i
]->name
);
6748 if (!access(filename
, 0))
6752 if (i
< dest
->num_printers
)
6753 dest
= dest
->printers
[i
];
6756 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
6757 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
6758 "printer-uri", NULL
, dest
->printers
[0]->uri
);
6764 * Found the printer with the PPD file, now see if there is one...
6767 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
6769 send_ipp_status(con
, IPP_NOT_FOUND
,
6770 _("The PPD file \"%s\" could not be opened: %s"),
6771 uri
->values
[0].string
.text
, strerror(errno
));
6775 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6779 con
->response
->request
.status
.status_code
= IPP_OK
;
6782 send_ipp_status(con
, IPP_NOT_FOUND
,
6783 _("The PPD file \"%s\" could not be found."),
6784 uri
->values
[0].string
.text
);
6789 * 'get_ppds()' - Get the list of PPD files on the local system.
6793 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
6795 http_status_t status
; /* Policy status */
6796 ipp_attribute_t
*limit
, /* Limit attribute */
6797 *device
, /* ppd-device-id attribute */
6798 *language
, /* ppd-natural-language attribute */
6799 *make
, /* ppd-make attribute */
6800 *model
, /* ppd-make-and-model attribute */
6801 *model_number
, /* ppd-model-number attribute */
6802 *product
, /* ppd-product attribute */
6803 *psversion
, /* ppd-psverion attribute */
6804 *type
, /* ppd-type attribute */
6805 *requested
, /* requested-attributes attribute */
6806 *exclude
, /* exclude-schemes attribute */
6807 *include
; /* include-schemes attribute */
6808 char command
[1024], /* cups-driverd command */
6809 options
[4096], /* Options to pass to command */
6810 device_str
[256],/* Escaped ppd-device-id string */
6812 /* Escaped ppd-natural-language */
6813 make_str
[256], /* Escaped ppd-make string */
6814 model_str
[256], /* Escaped ppd-make-and-model string */
6815 model_number_str
[256],
6816 /* ppd-model-number string */
6818 /* Escaped ppd-product string */
6820 /* Escaped ppd-psversion string */
6821 type_str
[256], /* Escaped ppd-type string */
6823 /* String for requested attributes */
6825 /* String for excluded schemes */
6827 /* String for included schemes */
6830 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->number
);
6836 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6838 send_http_error(con
, status
, NULL
);
6843 * Run cups-driverd command with the given options...
6846 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6847 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
6848 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
6850 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
6851 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
6853 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
6855 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
6856 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
6857 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
6858 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6860 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
6862 include
= ippFindAttribute(con
->request
, "include-schemes",
6866 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6868 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6871 url_encode_attr(device
, device_str
, sizeof(device_str
));
6873 device_str
[0] = '\0';
6876 url_encode_attr(language
, language_str
, sizeof(language_str
));
6878 language_str
[0] = '\0';
6881 url_encode_attr(make
, make_str
, sizeof(make_str
));
6886 url_encode_attr(model
, model_str
, sizeof(model_str
));
6888 model_str
[0] = '\0';
6891 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
6892 model_number
->values
[0].integer
);
6894 model_number_str
[0] = '\0';
6897 url_encode_attr(product
, product_str
, sizeof(product_str
));
6899 product_str
[0] = '\0';
6902 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
6904 psversion_str
[0] = '\0';
6907 url_encode_attr(type
, type_str
, sizeof(type_str
));
6912 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6914 exclude_str
[0] = '\0';
6917 url_encode_attr(include
, include_str
, sizeof(include_str
));
6919 include_str
[0] = '\0';
6921 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
6922 snprintf(options
, sizeof(options
),
6923 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
6924 con
->request
->request
.op
.request_id
,
6925 limit
? limit
->values
[0].integer
: 0,
6927 device
? "%20" : "", device_str
,
6928 language
? "%20" : "", language_str
,
6929 make
? "%20" : "", make_str
,
6930 model
? "%20" : "", model_str
,
6931 model_number
? "%20" : "", model_number_str
,
6932 product
? "%20" : "", product_str
,
6933 psversion
? "%20" : "", psversion_str
,
6934 type
? "%20" : "", type_str
,
6935 exclude_str
[0] ? "%20" : "", exclude_str
,
6936 include_str
[0] ? "%20" : "", include_str
);
6938 if (cupsdSendCommand(con
, command
, options
, 0))
6941 * Command started successfully, don't send an IPP response here...
6944 ippDelete(con
->response
);
6945 con
->response
= NULL
;
6950 * Command failed, return "internal error" so the user knows something
6954 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6955 _("cups-driverd failed to execute."));
6961 * 'get_printer_attrs()' - Get printer attributes.
6965 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
6966 ipp_attribute_t
*uri
) /* I - Printer URI */
6968 http_status_t status
; /* Policy status */
6969 cups_ptype_t dtype
; /* Destination type (printer/class) */
6970 cupsd_printer_t
*printer
; /* Printer/class */
6971 cups_array_t
*ra
; /* Requested attributes array */
6974 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
6975 con
->number
, uri
->values
[0].string
.text
);
6978 * Is the destination valid?
6981 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6987 send_ipp_status(con
, IPP_NOT_FOUND
,
6988 _("The printer or class does not exist."));
6996 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
6998 send_http_error(con
, status
, printer
);
7003 * Send the attributes...
7006 ra
= create_requested_array(con
->request
);
7008 copy_printer_attrs(con
, printer
, ra
);
7010 cupsArrayDelete(ra
);
7012 con
->response
->request
.status
.status_code
= IPP_OK
;
7017 * 'get_printer_supported()' - Get printer supported values.
7021 get_printer_supported(
7022 cupsd_client_t
*con
, /* I - Client connection */
7023 ipp_attribute_t
*uri
) /* I - Printer URI */
7025 http_status_t status
; /* Policy status */
7026 cups_ptype_t dtype
; /* Destination type (printer/class) */
7027 cupsd_printer_t
*printer
; /* Printer/class */
7030 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_supported(%p[%d], %s)", con
,
7031 con
->number
, uri
->values
[0].string
.text
);
7034 * Is the destination valid?
7037 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7043 send_ipp_status(con
, IPP_NOT_FOUND
,
7044 _("The printer or class does not exist."));
7052 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7054 send_http_error(con
, status
, printer
);
7059 * Return a list of attributes that can be set via Set-Printer-Attributes.
7062 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7064 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ADMINDEFINE
,
7065 "printer-location", 0);
7067 con
->response
->request
.status
.status_code
= IPP_OK
;
7072 * 'get_printers()' - Get a list of printers or classes.
7076 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7077 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7079 http_status_t status
; /* Policy status */
7080 ipp_attribute_t
*attr
; /* Current attribute */
7081 int limit
; /* Max number of printers to return */
7082 int count
; /* Number of printers that match */
7083 cupsd_printer_t
*printer
; /* Current printer pointer */
7084 cups_ptype_t printer_type
, /* printer-type attribute */
7085 printer_mask
; /* printer-type-mask attribute */
7086 char *location
; /* Location string */
7087 const char *username
; /* Current user */
7088 char *first_printer_name
; /* first-printer-name attribute */
7089 cups_array_t
*ra
; /* Requested attributes array */
7090 int local
; /* Local connection? */
7093 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7100 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7102 send_http_error(con
, status
, NULL
);
7107 * Check for printers...
7110 if (!Printers
|| !cupsArrayCount(Printers
))
7112 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7117 * See if they want to limit the number of printers reported...
7120 if ((attr
= ippFindAttribute(con
->request
, "limit",
7121 IPP_TAG_INTEGER
)) != NULL
)
7122 limit
= attr
->values
[0].integer
;
7126 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7127 IPP_TAG_NAME
)) != NULL
)
7128 first_printer_name
= attr
->values
[0].string
.text
;
7130 first_printer_name
= NULL
;
7133 * Support filtering...
7136 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7137 IPP_TAG_ENUM
)) != NULL
)
7138 printer_type
= (cups_ptype_t
)attr
->values
[0].integer
;
7140 printer_type
= (cups_ptype_t
)0;
7142 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7143 IPP_TAG_ENUM
)) != NULL
)
7144 printer_mask
= (cups_ptype_t
)attr
->values
[0].integer
;
7146 printer_mask
= (cups_ptype_t
)0;
7148 local
= httpAddrLocalhost(&(con
->clientaddr
));
7150 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7151 IPP_TAG_TEXT
)) != NULL
)
7152 location
= attr
->values
[0].string
.text
;
7156 if (con
->username
[0])
7157 username
= con
->username
;
7158 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7159 IPP_TAG_NAME
)) != NULL
)
7160 username
= attr
->values
[0].string
.text
;
7164 ra
= create_requested_array(con
->request
);
7167 * OK, build a list of printers for this printer...
7170 if (first_printer_name
)
7172 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7173 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7176 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7179 count
< limit
&& printer
;
7180 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7182 if (!local
&& !printer
->shared
)
7185 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7186 (printer
->type
& printer_mask
) == printer_type
&&
7188 (printer
->location
&& !_cups_strcasecmp(printer
->location
, location
))))
7191 * If a username is specified, see if it is allowed or denied
7195 if (cupsArrayCount(printer
->users
) && username
&&
7196 !user_allowed(printer
, username
))
7200 * Add the group separator as needed...
7204 ippAddSeparator(con
->response
);
7209 * Send the attributes...
7212 copy_printer_attrs(con
, printer
, ra
);
7216 cupsArrayDelete(ra
);
7218 con
->response
->request
.status
.status_code
= IPP_OK
;
7223 * 'get_subscription_attrs()' - Get subscription attributes.
7227 get_subscription_attrs(
7228 cupsd_client_t
*con
, /* I - Client connection */
7229 int sub_id
) /* I - Subscription ID */
7231 http_status_t status
; /* Policy status */
7232 cupsd_subscription_t
*sub
; /* Subscription */
7233 cupsd_policy_t
*policy
; /* Current security policy */
7234 cups_array_t
*ra
, /* Requested attributes array */
7235 *exclude
; /* Private attributes array */
7238 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7239 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7240 con
, con
->number
, sub_id
);
7243 * Is the subscription ID valid?
7246 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7249 * Bad subscription ID...
7252 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
7262 policy
= sub
->dest
->op_policy_ptr
;
7264 policy
= DefaultPolicyPtr
;
7266 if ((status
= cupsdCheckPolicy(policy
, con
, sub
->owner
)) != HTTP_OK
)
7268 send_http_error(con
, status
, sub
->dest
);
7272 exclude
= cupsdGetPrivateAttrs(policy
, con
, sub
->dest
, sub
->owner
);
7275 * Copy the subscription attributes to the response using the
7276 * requested-attributes attribute that may be provided by the client.
7279 ra
= create_requested_array(con
->request
);
7281 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7283 cupsArrayDelete(ra
);
7285 con
->response
->request
.status
.status_code
= IPP_OK
;
7290 * 'get_subscriptions()' - Get subscriptions.
7294 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7295 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7297 http_status_t status
; /* Policy status */
7298 int count
; /* Number of subscriptions */
7299 int limit
; /* Limit */
7300 cupsd_subscription_t
*sub
; /* Subscription */
7301 cups_array_t
*ra
; /* Requested attributes array */
7302 ipp_attribute_t
*attr
; /* Attribute */
7303 cups_ptype_t dtype
; /* Destination type (printer/class) */
7304 char scheme
[HTTP_MAX_URI
],
7305 /* Scheme portion of URI */
7306 username
[HTTP_MAX_URI
],
7307 /* Username portion of URI */
7309 /* Host portion of URI */
7310 resource
[HTTP_MAX_URI
];
7311 /* Resource portion of URI */
7312 int port
; /* Port portion of URI */
7313 cupsd_job_t
*job
; /* Job pointer */
7314 cupsd_printer_t
*printer
; /* Printer */
7315 cupsd_policy_t
*policy
; /* Policy */
7316 cups_array_t
*exclude
; /* Private attributes array */
7319 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7320 "get_subscriptions(con=%p[%d], uri=%s)",
7321 con
, con
->number
, uri
->values
[0].string
.text
);
7324 * Is the destination valid?
7327 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7328 sizeof(scheme
), username
, sizeof(username
), host
,
7329 sizeof(host
), &port
, resource
, sizeof(resource
));
7331 if (!strcmp(resource
, "/") ||
7332 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7333 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7334 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7339 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7342 job
= cupsdFindJob(atoi(resource
+ 6));
7346 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7347 atoi(resource
+ 6));
7351 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7357 send_ipp_status(con
, IPP_NOT_FOUND
,
7358 _("The printer or class does not exist."));
7361 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7362 IPP_TAG_INTEGER
)) != NULL
)
7364 job
= cupsdFindJob(attr
->values
[0].integer
);
7368 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."),
7369 attr
->values
[0].integer
);
7381 policy
= printer
->op_policy_ptr
;
7383 policy
= DefaultPolicyPtr
;
7385 if ((status
= cupsdCheckPolicy(policy
, con
, NULL
)) != HTTP_OK
)
7387 send_http_error(con
, status
, printer
);
7392 * Copy the subscription attributes to the response using the
7393 * requested-attributes attribute that may be provided by the client.
7396 ra
= create_requested_array(con
->request
);
7398 if ((attr
= ippFindAttribute(con
->request
, "limit",
7399 IPP_TAG_INTEGER
)) != NULL
)
7400 limit
= attr
->values
[0].integer
;
7405 * See if we only want to see subscriptions for a specific user...
7408 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7409 IPP_TAG_BOOLEAN
)) != NULL
&&
7410 attr
->values
[0].boolean
)
7411 strlcpy(username
, get_username(con
), sizeof(username
));
7415 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7417 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7418 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7419 (!username
[0] || !_cups_strcasecmp(username
, sub
->owner
)))
7421 ippAddSeparator(con
->response
);
7423 exclude
= cupsdGetPrivateAttrs(sub
->dest
? sub
->dest
->op_policy_ptr
:
7424 policy
, con
, sub
->dest
,
7427 copy_subscription_attrs(con
, sub
, ra
, exclude
);
7430 if (limit
&& count
>= limit
)
7434 cupsArrayDelete(ra
);
7437 con
->response
->request
.status
.status_code
= IPP_OK
;
7439 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7444 * 'get_username()' - Get the username associated with a request.
7447 static const char * /* O - Username */
7448 get_username(cupsd_client_t
*con
) /* I - Connection */
7450 ipp_attribute_t
*attr
; /* Attribute */
7453 if (con
->username
[0])
7454 return (con
->username
);
7455 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7456 IPP_TAG_NAME
)) != NULL
)
7457 return (attr
->values
[0].string
.text
);
7459 return ("anonymous");
7464 * 'hold_job()' - Hold a print job.
7468 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7469 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7471 ipp_attribute_t
*attr
; /* Current job-hold-until */
7472 const char *when
; /* New value */
7473 int jobid
; /* Job ID */
7474 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7475 username
[HTTP_MAX_URI
], /* Username portion of URI */
7476 host
[HTTP_MAX_URI
], /* Host portion of URI */
7477 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7478 int port
; /* Port portion of URI */
7479 cupsd_job_t
*job
; /* Job information */
7482 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->number
,
7483 uri
->values
[0].string
.text
);
7486 * See if we have a job URI or a printer URI...
7489 if (!strcmp(uri
->name
, "printer-uri"))
7492 * Got a printer URI; see if we also have a job-id attribute...
7495 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7496 IPP_TAG_INTEGER
)) == NULL
)
7498 send_ipp_status(con
, IPP_BAD_REQUEST
,
7499 _("Got a printer-uri attribute but no job-id."));
7503 jobid
= attr
->values
[0].integer
;
7508 * Got a job URI; parse it to get the job ID...
7511 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7512 sizeof(scheme
), username
, sizeof(username
), host
,
7513 sizeof(host
), &port
, resource
, sizeof(resource
));
7515 if (strncmp(resource
, "/jobs/", 6))
7521 send_ipp_status(con
, IPP_BAD_REQUEST
,
7522 _("Bad job-uri \"%s\"."),
7523 uri
->values
[0].string
.text
);
7527 jobid
= atoi(resource
+ 6);
7531 * See if the job exists...
7534 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7537 * Nope - return a "not found" error...
7540 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7545 * See if the job is owned by the requesting user...
7548 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7550 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7551 cupsdFindDest(job
->dest
));
7556 * See if the job is in a state that allows holding...
7559 if (job
->state_value
> IPP_JOB_STOPPED
)
7562 * Return a "not-possible" error...
7565 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7566 _("Job #%d is finished and cannot be altered."),
7572 * Hold the job and return...
7575 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
7576 IPP_TAG_KEYWORD
)) == NULL
)
7577 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7581 when
= attr
->values
[0].string
.text
;
7583 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
7584 "Job job-hold-until value changed by user.");
7587 when
= "indefinite";
7589 cupsdSetJobHoldUntil(job
, when
, 1);
7590 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
, "Job held by \"%s\".",
7593 con
->response
->request
.status
.status_code
= IPP_OK
;
7598 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
7602 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
7603 ipp_attribute_t
*uri
) /* I - Printer URI */
7605 http_status_t status
; /* Policy status */
7606 cups_ptype_t dtype
; /* Destination type (printer/class) */
7607 cupsd_printer_t
*printer
; /* Printer data */
7610 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
7611 con
->number
, uri
->values
[0].string
.text
);
7614 * Is the destination valid?
7617 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7623 send_ipp_status(con
, IPP_NOT_FOUND
,
7624 _("The printer or class does not exist."));
7632 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7634 send_http_error(con
, status
, printer
);
7639 * Hold pending/new jobs sent to the printer...
7642 printer
->holding_new_jobs
= 1;
7644 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
7646 if (dtype
& CUPS_PRINTER_CLASS
)
7647 cupsdLogMessage(CUPSD_LOG_INFO
,
7648 "Class \"%s\" now holding pending/new jobs (\"%s\").",
7649 printer
->name
, get_username(con
));
7651 cupsdLogMessage(CUPSD_LOG_INFO
,
7652 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
7653 printer
->name
, get_username(con
));
7656 * Everything was ok, so return OK status...
7659 con
->response
->request
.status
.status_code
= IPP_OK
;
7664 * 'move_job()' - Move a job to a new destination.
7668 move_job(cupsd_client_t
*con
, /* I - Client connection */
7669 ipp_attribute_t
*uri
) /* I - Job URI */
7671 http_status_t status
; /* Policy status */
7672 ipp_attribute_t
*attr
; /* Current attribute */
7673 int jobid
; /* Job ID */
7674 cupsd_job_t
*job
; /* Current job */
7675 const char *src
; /* Source printer/class */
7676 cups_ptype_t stype
, /* Source type (printer or class) */
7677 dtype
; /* Destination type (printer/class) */
7678 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
7679 username
[HTTP_MAX_URI
], /* Username portion of URI */
7680 host
[HTTP_MAX_URI
], /* Host portion of URI */
7681 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7682 int port
; /* Port portion of URI */
7683 cupsd_printer_t
*sprinter
, /* Source printer */
7684 *dprinter
; /* Destination printer */
7687 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->number
,
7688 uri
->values
[0].string
.text
);
7691 * Get the new printer or class...
7694 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
7695 IPP_TAG_URI
)) == NULL
)
7698 * Need job-printer-uri...
7701 send_ipp_status(con
, IPP_BAD_REQUEST
,
7702 _("job-printer-uri attribute missing."));
7706 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
7712 send_ipp_status(con
, IPP_NOT_FOUND
,
7713 _("The printer or class does not exist."));
7718 * See if we have a job URI or a printer URI...
7721 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7722 sizeof(scheme
), username
, sizeof(username
), host
,
7723 sizeof(host
), &port
, resource
, sizeof(resource
));
7725 if (!strcmp(uri
->name
, "printer-uri"))
7728 * Got a printer URI; see if we also have a job-id attribute...
7731 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7732 IPP_TAG_INTEGER
)) == NULL
)
7738 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
7739 &sprinter
)) == NULL
)
7745 send_ipp_status(con
, IPP_NOT_FOUND
,
7746 _("The printer or class does not exist."));
7755 * Otherwise, just move a single job...
7758 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
7761 * Nope - return a "not found" error...
7764 send_ipp_status(con
, IPP_NOT_FOUND
,
7765 _("Job #%d does not exist."), attr
->values
[0].integer
);
7771 * Job found, initialize source pointers...
7782 * Got a job URI; parse it to get the job ID...
7785 if (strncmp(resource
, "/jobs/", 6))
7791 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
7792 uri
->values
[0].string
.text
);
7797 * See if the job exists...
7800 jobid
= atoi(resource
+ 6);
7802 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7805 * Nope - return a "not found" error...
7808 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
7814 * Job found, initialize source pointers...
7823 * Check the policy of the destination printer...
7826 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
7827 job
? job
->username
: NULL
)) != HTTP_OK
)
7829 send_http_error(con
, status
, dprinter
);
7834 * Now move the job or jobs...
7840 * See if the job has been completed...
7843 if (job
->state_value
> IPP_JOB_STOPPED
)
7846 * Return a "not-possible" error...
7849 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
7850 _("Job #%d is finished and cannot be altered."),
7856 * See if the job is owned by the requesting user...
7859 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7861 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
7862 cupsdFindDest(job
->dest
));
7867 * Move the job to a different printer or class...
7870 cupsdMoveJob(job
, dprinter
);
7875 * Got the source printer, now look through the jobs...
7878 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
7880 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
7883 * See if the job is pointing at the source printer or has not been
7887 if (_cups_strcasecmp(job
->dest
, src
) ||
7888 job
->state_value
> IPP_JOB_STOPPED
)
7892 * See if the job can be moved by the requesting user...
7895 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7899 * Move the job to a different printer or class...
7902 cupsdMoveJob(job
, dprinter
);
7907 * Start jobs if possible...
7913 * Return with "everything is OK" status...
7916 con
->response
->request
.status
.status_code
= IPP_OK
;
7921 * 'ppd_parse_line()' - Parse a PPD default line.
7924 static int /* O - 0 on success, -1 on failure */
7925 ppd_parse_line(const char *line
, /* I - Line */
7926 char *option
, /* O - Option name */
7927 int olen
, /* I - Size of option name */
7928 char *choice
, /* O - Choice name */
7929 int clen
) /* I - Size of choice name */
7932 * Verify this is a default option line...
7935 if (strncmp(line
, "*Default", 8))
7939 * Read the option name...
7942 for (line
+= 8, olen
--;
7943 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
7954 * Skip everything else up to the colon (:)...
7957 while (*line
&& *line
!= ':')
7966 * Now grab the option choice, skipping leading whitespace...
7969 while (isspace(*line
& 255))
7973 *line
> ' ' && *line
< 0x7f && *line
!= ':' && *line
!= '/';
7984 * Return with no errors...
7992 * 'print_job()' - Print a file to a printer or class.
7996 print_job(cupsd_client_t
*con
, /* I - Client connection */
7997 ipp_attribute_t
*uri
) /* I - Printer URI */
7999 ipp_attribute_t
*attr
; /* Current attribute */
8000 ipp_attribute_t
*format
; /* Document-format attribute */
8001 const char *default_format
; /* document-format-default value */
8002 cupsd_job_t
*job
; /* New job */
8003 char filename
[1024]; /* Job filename */
8004 mime_type_t
*filetype
; /* Type of file */
8005 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8006 type
[MIME_MAX_TYPE
], /* Subtype of file */
8007 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8008 /* Textual name of mime type */
8009 cupsd_printer_t
*printer
; /* Printer data */
8010 struct stat fileinfo
; /* File information */
8011 int kbytes
; /* Size of file */
8012 int compression
; /* Document compression */
8015 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->number
,
8016 uri
->values
[0].string
.text
);
8019 * Validate print file attributes, for now just document-format and
8020 * compression (CUPS only supports "none" and "gzip")...
8023 compression
= CUPS_FILE_NONE
;
8025 if ((attr
= ippFindAttribute(con
->request
, "compression",
8026 IPP_TAG_KEYWORD
)) != NULL
)
8028 if (strcmp(attr
->values
[0].string
.text
, "none")
8030 && strcmp(attr
->values
[0].string
.text
, "gzip")
8031 #endif /* HAVE_LIBZ */
8034 send_ipp_status(con
, IPP_ATTRIBUTES
,
8035 _("Unsupported compression \"%s\"."),
8036 attr
->values
[0].string
.text
);
8037 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8038 "compression", NULL
, attr
->values
[0].string
.text
);
8043 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8044 compression
= CUPS_FILE_GZIP
;
8045 #endif /* HAVE_LIBZ */
8049 * Do we have a file to print?
8054 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
8059 * Is the destination valid?
8062 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8068 send_ipp_status(con
, IPP_NOT_FOUND
,
8069 _("The printer or class does not exist."));
8074 * Is it a format we support?
8077 if ((format
= ippFindAttribute(con
->request
, "document-format",
8078 IPP_TAG_MIMETYPE
)) != NULL
)
8081 * Grab format from client...
8084 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]", super
,
8087 send_ipp_status(con
, IPP_BAD_REQUEST
,
8088 _("Bad document-format \"%s\"."),
8089 format
->values
[0].string
.text
);
8093 else if ((default_format
= cupsGetOption("document-format",
8094 printer
->num_options
,
8095 printer
->options
)) != NULL
)
8098 * Use default document format...
8101 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
8103 send_ipp_status(con
, IPP_BAD_REQUEST
,
8104 _("Bad document-format \"%s\"."),
8115 strlcpy(super
, "application", sizeof(super
));
8116 strlcpy(type
, "octet-stream", sizeof(type
));
8119 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8122 * Auto-type the file...
8125 ipp_attribute_t
*doc_name
; /* document-name attribute */
8128 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8130 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8131 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8132 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8136 filetype
= mimeType(MimeDatabase
, super
, type
);
8138 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8139 filetype
->super
, filetype
->type
);
8142 filetype
= mimeType(MimeDatabase
, super
, type
);
8146 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8149 * Replace the document-format attribute value with the auto-typed or
8153 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8157 ippSetString(con
->request
, &format
, 0, mimetype
);
8159 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8160 "document-format", NULL
, mimetype
);
8164 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8165 _("Unsupported document-format \"%s\"."),
8166 format
? format
->values
[0].string
.text
:
8167 "application/octet-stream");
8168 cupsdLogMessage(CUPSD_LOG_INFO
,
8169 "Hint: Do you have the raw file printing rules enabled?");
8172 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8173 "document-format", NULL
, format
->values
[0].string
.text
);
8179 * Read any embedded job ticket info from PS files...
8182 if (!_cups_strcasecmp(filetype
->super
, "application") &&
8183 (!_cups_strcasecmp(filetype
->type
, "postscript") ||
8184 !_cups_strcasecmp(filetype
->type
, "pdf")))
8185 read_job_ticket(con
);
8188 * Create the job object...
8191 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8195 * Update quota data...
8198 if (stat(con
->filename
, &fileinfo
))
8201 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8203 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8205 job
->koctets
+= kbytes
;
8207 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
8208 attr
->values
[0].integer
+= kbytes
;
8211 * Add the job file...
8214 if (add_file(con
, job
, filetype
, compression
))
8217 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8219 rename(con
->filename
, filename
);
8220 cupsdClearString(&con
->filename
);
8223 * See if we need to add the ending sheet...
8226 if (cupsdTimeoutJob(job
))
8230 * Log and save the job...
8233 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8234 "File of type %s/%s queued by \"%s\".",
8235 filetype
->super
, filetype
->type
, job
->username
);
8236 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8237 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8238 job
->dest
, job
->username
);
8241 * Start the job if possible...
8249 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8251 * This function only gets called when printing a single PDF or PostScript
8252 * file using the Print-Job operation. It doesn't work for Create-Job +
8253 * Send-File, since the job attributes need to be set at job creation
8254 * time for banners to work. The embedded job ticket stuff is here
8255 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8256 * options and IPP attributes which otherwise would be lost.
8258 * The format of a job ticket is simple:
8260 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8262 * %cupsJobTicket: attr1=value1
8263 * %cupsJobTicket: attr2=value2
8265 * %cupsJobTicket: attrN=valueN
8267 * Job ticket lines must appear immediately after the first line that
8268 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8269 * stops looking for job ticket info when it finds a line that does not begin
8270 * with "%cupsJobTicket:".
8272 * The maximum length of a job ticket line, including the prefix, is
8273 * 255 characters to conform with the Adobe DSC.
8275 * Read-only attributes are rejected with a notice to the error log in
8276 * case a malicious user tries anything. Since the job ticket is read
8277 * prior to attribute validation in print_job(), job ticket attributes
8278 * will go through the same validation as IPP attributes...
8282 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8284 cups_file_t
*fp
; /* File to read from */
8285 char line
[256]; /* Line data */
8286 int num_options
; /* Number of options */
8287 cups_option_t
*options
; /* Options */
8288 ipp_t
*ticket
; /* New attributes */
8289 ipp_attribute_t
*attr
, /* Current attribute */
8290 *attr2
, /* Job attribute */
8291 *prev2
; /* Previous job attribute */
8295 * First open the print file...
8298 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8300 cupsdLogMessage(CUPSD_LOG_ERROR
,
8301 "Unable to open print file for job ticket - %s",
8307 * Skip the first line...
8310 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8312 cupsdLogMessage(CUPSD_LOG_ERROR
,
8313 "Unable to read from print file for job ticket - %s",
8319 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8322 * Not a DSC-compliant file, so no job ticket info will be available...
8330 * Read job ticket info from the file...
8336 while (cupsFileGets(fp
, line
, sizeof(line
)))
8339 * Stop at the first non-ticket line...
8342 if (strncmp(line
, "%cupsJobTicket:", 15))
8346 * Add the options to the option array...
8349 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8353 * Done with the file; see if we have any options...
8358 if (num_options
== 0)
8362 * OK, convert the options to an attribute list, and apply them to
8367 cupsEncodeOptions(ticket
, num_options
, options
);
8370 * See what the user wants to change.
8373 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8375 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8378 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8379 !strcmp(attr
->name
, "job-originating-user-name") ||
8380 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8381 !strcmp(attr
->name
, "job-k-octets") ||
8382 !strcmp(attr
->name
, "job-id") ||
8383 !strncmp(attr
->name
, "job-state", 9) ||
8384 !strncmp(attr
->name
, "time-at-", 8))
8385 continue; /* Read-only attrs */
8387 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8388 IPP_TAG_ZERO
)) != NULL
)
8391 * Some other value; first free the old value...
8394 if (con
->request
->attrs
== attr2
)
8396 con
->request
->attrs
= attr2
->next
;
8401 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8402 if (prev2
->next
== attr2
)
8404 prev2
->next
= attr2
->next
;
8409 if (con
->request
->last
== attr2
)
8410 con
->request
->last
= prev2
;
8412 ippDeleteAttribute(NULL
, attr2
);
8416 * Add new option by copying it...
8419 ippCopyAttribute(con
->request
, attr
, 0);
8423 * Then free the attribute list and option array...
8427 cupsFreeOptions(num_options
, options
);
8432 * 'reject_jobs()' - Reject print jobs to a printer.
8436 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8437 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8439 http_status_t status
; /* Policy status */
8440 cups_ptype_t dtype
; /* Destination type (printer/class) */
8441 cupsd_printer_t
*printer
; /* Printer data */
8442 ipp_attribute_t
*attr
; /* printer-state-message text */
8445 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8446 con
->number
, uri
->values
[0].string
.text
);
8449 * Is the destination valid?
8452 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8458 send_ipp_status(con
, IPP_NOT_FOUND
,
8459 _("The printer or class does not exist."));
8467 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8469 send_http_error(con
, status
, printer
);
8474 * Reject jobs sent to the printer...
8477 printer
->accepting
= 0;
8479 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8480 IPP_TAG_TEXT
)) == NULL
)
8481 strlcpy(printer
->state_message
, "Rejecting Jobs",
8482 sizeof(printer
->state_message
));
8484 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8485 sizeof(printer
->state_message
));
8487 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
8488 "No longer accepting jobs.");
8490 if (dtype
& CUPS_PRINTER_CLASS
)
8492 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8494 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8495 printer
->name
, get_username(con
));
8499 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8501 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8502 printer
->name
, get_username(con
));
8506 * Everything was ok, so return OK status...
8509 con
->response
->request
.status
.status_code
= IPP_OK
;
8514 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8518 release_held_new_jobs(
8519 cupsd_client_t
*con
, /* I - Connection */
8520 ipp_attribute_t
*uri
) /* I - Printer URI */
8522 http_status_t status
; /* Policy status */
8523 cups_ptype_t dtype
; /* Destination type (printer/class) */
8524 cupsd_printer_t
*printer
; /* Printer data */
8527 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8528 con
->number
, uri
->values
[0].string
.text
);
8531 * Is the destination valid?
8534 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8540 send_ipp_status(con
, IPP_NOT_FOUND
,
8541 _("The printer or class does not exist."));
8549 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8551 send_http_error(con
, status
, printer
);
8556 * Hold pending/new jobs sent to the printer...
8559 printer
->holding_new_jobs
= 0;
8561 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8563 if (dtype
& CUPS_PRINTER_CLASS
)
8564 cupsdLogMessage(CUPSD_LOG_INFO
,
8565 "Class \"%s\" now printing pending/new jobs (\"%s\").",
8566 printer
->name
, get_username(con
));
8568 cupsdLogMessage(CUPSD_LOG_INFO
,
8569 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
8570 printer
->name
, get_username(con
));
8573 * Everything was ok, so return OK status...
8576 con
->response
->request
.status
.status_code
= IPP_OK
;
8581 * 'release_job()' - Release a held print job.
8585 release_job(cupsd_client_t
*con
, /* I - Client connection */
8586 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8588 ipp_attribute_t
*attr
; /* Current attribute */
8589 int jobid
; /* Job ID */
8590 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8591 username
[HTTP_MAX_URI
], /* Username portion of URI */
8592 host
[HTTP_MAX_URI
], /* Host portion of URI */
8593 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8594 int port
; /* Port portion of URI */
8595 cupsd_job_t
*job
; /* Job information */
8598 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
8599 con
->number
, uri
->values
[0].string
.text
);
8602 * See if we have a job URI or a printer URI...
8605 if (!strcmp(uri
->name
, "printer-uri"))
8608 * Got a printer URI; see if we also have a job-id attribute...
8611 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8612 IPP_TAG_INTEGER
)) == NULL
)
8614 send_ipp_status(con
, IPP_BAD_REQUEST
,
8615 _("Got a printer-uri attribute but no job-id."));
8619 jobid
= attr
->values
[0].integer
;
8624 * Got a job URI; parse it to get the job ID...
8627 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8628 sizeof(scheme
), username
, sizeof(username
), host
,
8629 sizeof(host
), &port
, resource
, sizeof(resource
));
8631 if (strncmp(resource
, "/jobs/", 6))
8637 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8638 uri
->values
[0].string
.text
);
8642 jobid
= atoi(resource
+ 6);
8646 * See if the job exists...
8649 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8652 * Nope - return a "not found" error...
8655 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8660 * See if job is "held"...
8663 if (job
->state_value
!= IPP_JOB_HELD
)
8666 * Nope - return a "not possible" error...
8669 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held."), jobid
);
8674 * See if the job is owned by the requesting user...
8677 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8679 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8680 cupsdFindDest(job
->dest
));
8685 * Reset the job-hold-until value to "no-hold"...
8688 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
8689 IPP_TAG_KEYWORD
)) == NULL
)
8690 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8694 ippSetValueTag(job
->attrs
, &attr
, IPP_TAG_KEYWORD
);
8695 ippSetString(job
->attrs
, &attr
, 0, "no-hold");
8697 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8698 "Job job-hold-until value changed by user.");
8699 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
8703 * Release the job and return...
8706 cupsdReleaseJob(job
);
8708 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
8709 "Job released by user.");
8711 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
8713 con
->response
->request
.status
.status_code
= IPP_OK
;
8720 * 'renew_subscription()' - Renew an existing subscription...
8725 cupsd_client_t
*con
, /* I - Client connection */
8726 int sub_id
) /* I - Subscription ID */
8728 http_status_t status
; /* Policy status */
8729 cupsd_subscription_t
*sub
; /* Subscription */
8730 ipp_attribute_t
*lease
; /* notify-lease-duration */
8733 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
8734 "renew_subscription(con=%p[%d], sub_id=%d)",
8735 con
, con
->number
, sub_id
);
8738 * Is the subscription ID valid?
8741 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
8744 * Bad subscription ID...
8747 send_ipp_status(con
, IPP_NOT_FOUND
, _("Subscription #%d does not exist."),
8755 * Job subscriptions cannot be renewed...
8758 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8759 _("Job subscriptions cannot be renewed."));
8767 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
8769 con
, sub
->owner
)) != HTTP_OK
)
8771 send_http_error(con
, status
, sub
->dest
);
8776 * Renew the subscription...
8779 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
8782 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
8784 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
8786 cupsdLogMessage(CUPSD_LOG_INFO
,
8787 "renew_subscription: Limiting notify-lease-duration to "
8790 sub
->lease
= MaxLeaseDuration
;
8793 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
8795 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
8797 con
->response
->request
.status
.status_code
= IPP_OK
;
8799 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
8800 "notify-lease-duration", sub
->lease
);
8805 * 'restart_job()' - Restart an old print job.
8809 restart_job(cupsd_client_t
*con
, /* I - Client connection */
8810 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
8812 ipp_attribute_t
*attr
; /* Current attribute */
8813 int jobid
; /* Job ID */
8814 cupsd_job_t
*job
; /* Job information */
8815 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
8816 username
[HTTP_MAX_URI
], /* Username portion of URI */
8817 host
[HTTP_MAX_URI
], /* Host portion of URI */
8818 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8819 int port
; /* Port portion of URI */
8822 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
8823 con
->number
, uri
->values
[0].string
.text
);
8826 * See if we have a job URI or a printer URI...
8829 if (!strcmp(uri
->name
, "printer-uri"))
8832 * Got a printer URI; see if we also have a job-id attribute...
8835 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8836 IPP_TAG_INTEGER
)) == NULL
)
8838 send_ipp_status(con
, IPP_BAD_REQUEST
,
8839 _("Got a printer-uri attribute but no job-id."));
8843 jobid
= attr
->values
[0].integer
;
8848 * Got a job URI; parse it to get the job ID...
8851 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8852 sizeof(scheme
), username
, sizeof(username
), host
,
8853 sizeof(host
), &port
, resource
, sizeof(resource
));
8855 if (strncmp(resource
, "/jobs/", 6))
8861 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
8862 uri
->values
[0].string
.text
);
8866 jobid
= atoi(resource
+ 6);
8870 * See if the job exists...
8873 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8876 * Nope - return a "not found" error...
8879 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
8884 * See if job is in any of the "completed" states...
8887 if (job
->state_value
<= IPP_JOB_PROCESSING
)
8890 * Nope - return a "not possible" error...
8893 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete."),
8899 * See if we have retained the job files...
8904 if (!job
->attrs
|| job
->num_files
== 0)
8907 * Nope - return a "not possible" error...
8910 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8911 _("Job #%d cannot be restarted - no files."), jobid
);
8916 * See if the job is owned by the requesting user...
8919 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8921 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
8922 cupsdFindDest(job
->dest
));
8927 * See if the job-hold-until attribute is specified...
8930 if ((attr
= ippFindAttribute(con
->request
, "job-hold-until",
8931 IPP_TAG_KEYWORD
)) == NULL
)
8932 attr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
8934 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold"))
8937 * Return the job to a held state...
8940 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
8941 "Restarted by \"%s\" with job-hold-until=%s.",
8942 username
, attr
->values
[0].string
.text
);
8943 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
8945 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
,
8946 NULL
, job
, "Job restarted by user with job-hold-until=%s",
8947 attr
->values
[0].string
.text
);
8952 * Restart the job...
8955 cupsdRestartJob(job
);
8959 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
8961 con
->response
->request
.status
.status_code
= IPP_OK
;
8966 * 'save_auth_info()' - Save authentication information for a job.
8971 cupsd_client_t
*con
, /* I - Client connection */
8972 cupsd_job_t
*job
, /* I - Job */
8973 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
8975 int i
; /* Looping var */
8976 char filename
[1024]; /* Job authentication filename */
8977 cups_file_t
*fp
; /* Job authentication file */
8978 char line
[65536]; /* Line for file */
8979 cupsd_printer_t
*dest
; /* Destination printer/class */
8983 * This function saves the in-memory authentication information for
8984 * a job so that it can be used to authenticate with a remote host.
8985 * The information is stored in a file that is readable only by the
8986 * root user. The fields are Base-64 encoded, each on a separate line,
8987 * followed by random number (up to 1024) of newlines to limit the
8988 * amount of information that is exposed.
8990 * Because of the potential for exposing of authentication information,
8991 * this functionality is only enabled when running cupsd as root.
8993 * This caching only works for the Basic and BasicDigest authentication
8994 * types. Digest authentication cannot be cached this way, and in
8995 * the future Kerberos authentication may make all of this obsolete.
8997 * Authentication information is saved whenever an authenticated
8998 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9001 * This information is deleted after a job is completed or canceled,
9002 * so reprints may require subsequent re-authentication.
9008 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9012 * Create the authentication file and change permissions...
9015 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9016 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9018 cupsdLogMessage(CUPSD_LOG_ERROR
,
9019 "Unable to save authentication info to \"%s\" - %s",
9020 filename
, strerror(errno
));
9024 fchown(cupsFileNumber(fp
), 0, 0);
9025 fchmod(cupsFileNumber(fp
), 0400);
9027 cupsFilePuts(fp
, "CUPSD-AUTH-V3\n");
9030 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9032 cupsdClearString(job
->auth_env
+ i
);
9034 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9037 * Write 1 to 3 auth values...
9041 i
< auth_info
->num_values
&&
9042 i
< (int)(sizeof(job
->auth_env
) / sizeof(job
->auth_env
[0]));
9045 if (strcmp(dest
->auth_info_required
[i
], "negotiate"))
9047 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
, (int)strlen(auth_info
->values
[i
].string
.text
));
9048 cupsFilePutConf(fp
, dest
->auth_info_required
[i
], line
);
9051 cupsFilePutConf(fp
, dest
->auth_info_required
[i
],
9052 auth_info
->values
[i
].string
.text
);
9054 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9055 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_USERNAME=%s",
9056 auth_info
->values
[i
].string
.text
);
9057 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9058 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_DOMAIN=%s",
9059 auth_info
->values
[i
].string
.text
);
9060 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9061 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_PASSWORD=%s",
9062 auth_info
->values
[i
].string
.text
);
9063 else if (!strcmp(dest
->auth_info_required
[i
], "negotiate"))
9064 cupsdSetStringf(job
->auth_env
+ i
, "AUTH_NEGOTIATE=%s",
9065 auth_info
->values
[i
].string
.text
);
9070 else if (auth_info
&& auth_info
->num_values
== 2 &&
9071 dest
->num_auth_info_required
== 1 &&
9072 !strcmp(dest
->auth_info_required
[0], "negotiate"))
9075 * Allow fallback to username+password for Kerberized queues...
9078 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[0].string
.text
, (int)strlen(auth_info
->values
[0].string
.text
));
9079 cupsFilePutConf(fp
, "username", line
);
9081 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s",
9082 auth_info
->values
[0].string
.text
);
9084 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[1].string
.text
, (int)strlen(auth_info
->values
[1].string
.text
));
9085 cupsFilePutConf(fp
, "password", line
);
9087 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s",
9088 auth_info
->values
[1].string
.text
);
9090 else if (con
->username
[0])
9093 * Write the authenticated username...
9096 httpEncode64_2(line
, sizeof(line
), con
->username
, (int)strlen(con
->username
));
9097 cupsFilePutConf(fp
, "username", line
);
9099 cupsdSetStringf(job
->auth_env
+ 0, "AUTH_USERNAME=%s", con
->username
);
9102 * Write the authenticated password...
9105 httpEncode64_2(line
, sizeof(line
), con
->password
, (int)strlen(con
->password
));
9106 cupsFilePutConf(fp
, "password", line
);
9108 cupsdSetStringf(job
->auth_env
+ 1, "AUTH_PASSWORD=%s", con
->password
);
9112 if (con
->gss_uid
> 0)
9114 cupsFilePrintf(fp
, "uid %d\n", (int)con
->gss_uid
);
9115 cupsdSetStringf(&job
->auth_uid
, "AUTH_UID=%d", (int)con
->gss_uid
);
9117 #endif /* HAVE_GSSAPI */
9120 * Write a random number of newlines to the end of the file...
9123 for (i
= (CUPS_RAND() % 1024); i
>= 0; i
--)
9124 cupsFilePutChar(fp
, '\n');
9127 * Close the file and return...
9135 * 'send_document()' - Send a file to a printer or class.
9139 send_document(cupsd_client_t
*con
, /* I - Client connection */
9140 ipp_attribute_t
*uri
) /* I - Printer URI */
9142 ipp_attribute_t
*attr
; /* Current attribute */
9143 ipp_attribute_t
*format
; /* Request's document-format attribute */
9144 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9145 const char *default_format
;/* document-format-default value */
9146 int jobid
; /* Job ID number */
9147 cupsd_job_t
*job
; /* Current job */
9148 char job_uri
[HTTP_MAX_URI
],
9150 scheme
[HTTP_MAX_URI
],
9151 /* Method portion of URI */
9152 username
[HTTP_MAX_URI
],
9153 /* Username portion of URI */
9155 /* Host portion of URI */
9156 resource
[HTTP_MAX_URI
];
9157 /* Resource portion of URI */
9158 int port
; /* Port portion of URI */
9159 mime_type_t
*filetype
; /* Type of file */
9160 char super
[MIME_MAX_SUPER
],
9161 /* Supertype of file */
9162 type
[MIME_MAX_TYPE
],
9163 /* Subtype of file */
9164 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9165 /* Textual name of mime type */
9166 char filename
[1024]; /* Job filename */
9167 cupsd_printer_t
*printer
; /* Current printer */
9168 struct stat fileinfo
; /* File information */
9169 int kbytes
; /* Size of file */
9170 int compression
; /* Type of compression */
9171 int start_job
; /* Start the job? */
9174 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9175 con
->number
, uri
->values
[0].string
.text
);
9178 * See if we have a job URI or a printer URI...
9181 if (!strcmp(uri
->name
, "printer-uri"))
9184 * Got a printer URI; see if we also have a job-id attribute...
9187 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9188 IPP_TAG_INTEGER
)) == NULL
)
9190 send_ipp_status(con
, IPP_BAD_REQUEST
,
9191 _("Got a printer-uri attribute but no job-id."));
9195 jobid
= attr
->values
[0].integer
;
9200 * Got a job URI; parse it to get the job ID...
9203 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9204 sizeof(scheme
), username
, sizeof(username
), host
,
9205 sizeof(host
), &port
, resource
, sizeof(resource
));
9207 if (strncmp(resource
, "/jobs/", 6))
9213 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9214 uri
->values
[0].string
.text
);
9218 jobid
= atoi(resource
+ 6);
9222 * See if the job exists...
9225 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9228 * Nope - return a "not found" error...
9231 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9235 printer
= cupsdFindDest(job
->dest
);
9238 * See if the job is owned by the requesting user...
9241 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9243 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9244 cupsdFindDest(job
->dest
));
9249 * OK, see if the client is sending the document compressed - CUPS
9250 * only supports "none" and "gzip".
9253 compression
= CUPS_FILE_NONE
;
9255 if ((attr
= ippFindAttribute(con
->request
, "compression",
9256 IPP_TAG_KEYWORD
)) != NULL
)
9258 if (strcmp(attr
->values
[0].string
.text
, "none")
9260 && strcmp(attr
->values
[0].string
.text
, "gzip")
9261 #endif /* HAVE_LIBZ */
9264 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"."),
9265 attr
->values
[0].string
.text
);
9266 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9267 "compression", NULL
, attr
->values
[0].string
.text
);
9272 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9273 compression
= CUPS_FILE_GZIP
;
9274 #endif /* HAVE_LIBZ */
9278 * Do we have a file to print?
9281 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9282 IPP_TAG_BOOLEAN
)) == NULL
)
9284 send_ipp_status(con
, IPP_BAD_REQUEST
,
9285 _("Missing last-document attribute in request."));
9292 * Check for an empty request with "last-document" set to true, which is
9293 * used to close an "open" job by RFC 2911, section 3.3.2.
9296 if (job
->num_files
> 0 && attr
->values
[0].boolean
)
9299 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file in print request."));
9304 * Is it a format we support?
9307 if ((format
= ippFindAttribute(con
->request
, "document-format",
9308 IPP_TAG_MIMETYPE
)) != NULL
)
9311 * Grab format from client...
9314 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
9317 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"."),
9318 format
->values
[0].string
.text
);
9322 else if ((default_format
= cupsGetOption("document-format",
9323 printer
->num_options
,
9324 printer
->options
)) != NULL
)
9327 * Use default document format...
9330 if (sscanf(default_format
, "%15[^/]/%255[^;]", super
, type
) != 2)
9332 send_ipp_status(con
, IPP_BAD_REQUEST
,
9333 _("Bad document-format-default \"%s\"."), default_format
);
9340 * No document format attribute? Auto-type it!
9343 strlcpy(super
, "application", sizeof(super
));
9344 strlcpy(type
, "octet-stream", sizeof(type
));
9347 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9350 * Auto-type the file...
9353 ipp_attribute_t
*doc_name
; /* document-name attribute */
9356 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9358 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9359 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9360 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9364 filetype
= mimeType(MimeDatabase
, super
, type
);
9367 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9368 filetype
->super
, filetype
->type
);
9371 filetype
= mimeType(MimeDatabase
, super
, type
);
9376 * Replace the document-format attribute value with the auto-typed or
9380 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9383 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9384 IPP_TAG_MIMETYPE
)) != NULL
)
9385 ippSetString(job
->attrs
, &jformat
, 0, mimetype
);
9387 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9388 "document-format", NULL
, mimetype
);
9392 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9393 _("Unsupported document-format \"%s/%s\"."), super
, type
);
9394 cupsdLogMessage(CUPSD_LOG_INFO
,
9395 "Hint: Do you have the raw file printing rules enabled?");
9398 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9399 "document-format", NULL
, format
->values
[0].string
.text
);
9404 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9406 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9409 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9410 _("Unsupported document-format \"%s\"."), mimetype
);
9412 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9413 "document-format", NULL
, mimetype
);
9419 * Add the file to the job...
9424 if (add_file(con
, job
, filetype
, compression
))
9427 if (stat(con
->filename
, &fileinfo
))
9430 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9432 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9434 job
->koctets
+= kbytes
;
9436 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets", IPP_TAG_INTEGER
)) != NULL
)
9437 attr
->values
[0].integer
+= kbytes
;
9439 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9441 rename(con
->filename
, filename
);
9443 cupsdClearString(&con
->filename
);
9445 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9446 filetype
->super
, filetype
->type
, job
->username
);
9449 * Start the job if this is the last document...
9454 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9455 IPP_TAG_BOOLEAN
)) != NULL
&&
9456 attr
->values
[0].boolean
)
9459 * See if we need to add the ending sheet...
9462 if (cupsdTimeoutJob(job
))
9465 if (job
->state_value
== IPP_JOB_STOPPED
)
9467 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9468 job
->state_value
= IPP_JOB_PENDING
;
9470 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9472 else if (job
->state_value
== IPP_JOB_HELD
)
9474 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9475 IPP_TAG_KEYWORD
)) == NULL
)
9476 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9478 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9480 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9481 job
->state_value
= IPP_JOB_PENDING
;
9483 ippSetString(job
->attrs
, &job
->reasons
, 0, "none");
9486 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-hold-until-specified");
9490 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9496 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9497 IPP_TAG_KEYWORD
)) == NULL
)
9498 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9500 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9502 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9503 job
->state_value
= IPP_JOB_HELD
;
9504 job
->hold_until
= time(NULL
) + MultipleOperationTimeout
;
9506 ippSetString(job
->attrs
, &job
->reasons
, 0, "job-incoming");
9509 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9516 * Fill in the response info...
9519 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
9520 con
->clientname
, con
->clientport
, "/jobs/%d", jobid
);
9521 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
9524 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
9526 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
9528 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "job-state-reasons",
9529 NULL
, job
->reasons
->values
[0].string
.text
);
9531 con
->response
->request
.status
.status_code
= IPP_OK
;
9534 * Start the job if necessary...
9543 * 'send_http_error()' - Send a HTTP error back to the IPP client.
9548 cupsd_client_t
*con
, /* I - Client connection */
9549 http_status_t status
, /* I - HTTP status code */
9550 cupsd_printer_t
*printer
) /* I - Printer, if any */
9552 ipp_attribute_t
*uri
; /* Request URI, if any */
9555 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
9556 IPP_TAG_URI
)) == NULL
)
9557 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
9559 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
9560 "[Client %d] Returning HTTP %s for %s (%s) from %s",
9561 con
->number
, httpStatus(status
),
9563 ippOpString(con
->request
->request
.op
.operation_id
) :
9565 uri
? uri
->values
[0].string
.text
: "no URI",
9566 con
->http
->hostname
);
9570 int auth_type
; /* Type of authentication required */
9573 auth_type
= CUPSD_AUTH_NONE
;
9575 if (status
== HTTP_UNAUTHORIZED
&&
9576 printer
->num_auth_info_required
> 0 &&
9577 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
9579 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
9580 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
9581 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
9584 * Creating and authenticating jobs requires Kerberos...
9587 auth_type
= CUPSD_AUTH_NEGOTIATE
;
9592 * Use policy/location-defined authentication requirements...
9595 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9596 cupsd_location_t
*auth
; /* Pointer to authentication element */
9599 if (printer
->type
& CUPS_PRINTER_CLASS
)
9600 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
9602 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
9604 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
9605 auth
->type
== CUPSD_AUTH_NONE
)
9606 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
9608 con
->request
->request
.op
.operation_id
:
9613 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
9614 auth_type
= cupsdDefaultAuthType();
9616 auth_type
= auth
->type
;
9620 cupsdSendError(con
, status
, auth_type
);
9623 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
9625 ippDelete(con
->response
);
9626 con
->response
= NULL
;
9633 * 'send_ipp_status()' - Send a status back to the IPP client.
9637 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
9638 ipp_status_t status
, /* I - IPP status code */
9639 const char *message
,/* I - Status message */
9640 ...) /* I - Additional args as needed */
9642 va_list ap
; /* Pointer to additional args */
9643 char formatted
[1024]; /* Formatted errror message */
9646 va_start(ap
, message
);
9647 vsnprintf(formatted
, sizeof(formatted
),
9648 _cupsLangString(con
->language
, message
), ap
);
9651 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
9652 ippOpString(con
->request
->request
.op
.operation_id
),
9653 ippErrorString(status
), formatted
);
9655 con
->response
->request
.status
.status_code
= status
;
9657 if (ippFindAttribute(con
->response
, "attributes-charset",
9658 IPP_TAG_ZERO
) == NULL
)
9659 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
9660 "attributes-charset", NULL
, "utf-8");
9662 if (ippFindAttribute(con
->response
, "attributes-natural-language",
9663 IPP_TAG_ZERO
) == NULL
)
9664 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
9665 "attributes-natural-language", NULL
, DefaultLanguage
);
9667 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
9668 "status-message", NULL
, formatted
);
9673 * 'set_default()' - Set the default destination...
9677 set_default(cupsd_client_t
*con
, /* I - Client connection */
9678 ipp_attribute_t
*uri
) /* I - Printer URI */
9680 http_status_t status
; /* Policy status */
9681 cups_ptype_t dtype
; /* Destination type (printer/class) */
9682 cupsd_printer_t
*printer
, /* Printer */
9683 *oldprinter
; /* Old default printer */
9686 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
9687 con
->number
, uri
->values
[0].string
.text
);
9690 * Is the destination valid?
9693 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
9699 send_ipp_status(con
, IPP_NOT_FOUND
,
9700 _("The printer or class does not exist."));
9708 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
9710 send_http_error(con
, status
, NULL
);
9715 * Set it as the default...
9718 oldprinter
= DefaultPrinter
;
9719 DefaultPrinter
= printer
;
9722 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
9723 "%s is no longer the default printer.", oldprinter
->name
);
9725 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
9726 "%s is now the default printer.", printer
->name
);
9728 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
9729 CUPSD_DIRTY_PRINTCAP
);
9731 cupsdLogMessage(CUPSD_LOG_INFO
,
9732 "Default destination set to \"%s\" by \"%s\".",
9733 printer
->name
, get_username(con
));
9736 * Everything was ok, so return OK status...
9739 con
->response
->request
.status
.status_code
= IPP_OK
;
9744 * 'set_job_attrs()' - Set job attributes.
9748 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
9749 ipp_attribute_t
*uri
) /* I - Job URI */
9751 ipp_attribute_t
*attr
, /* Current attribute */
9752 *attr2
; /* Job attribute */
9753 int jobid
; /* Job ID */
9754 cupsd_job_t
*job
; /* Current job */
9755 char scheme
[HTTP_MAX_URI
],
9756 /* Method portion of URI */
9757 username
[HTTP_MAX_URI
],
9758 /* Username portion of URI */
9760 /* Host portion of URI */
9761 resource
[HTTP_MAX_URI
];
9762 /* Resource portion of URI */
9763 int port
; /* Port portion of URI */
9764 int event
; /* Events? */
9765 int check_jobs
; /* Check jobs? */
9768 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
9769 con
->number
, uri
->values
[0].string
.text
);
9772 * Start with "everything is OK" status...
9775 con
->response
->request
.status
.status_code
= IPP_OK
;
9778 * See if we have a job URI or a printer URI...
9781 if (!strcmp(uri
->name
, "printer-uri"))
9784 * Got a printer URI; see if we also have a job-id attribute...
9787 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9788 IPP_TAG_INTEGER
)) == NULL
)
9790 send_ipp_status(con
, IPP_BAD_REQUEST
,
9791 _("Got a printer-uri attribute but no job-id."));
9795 jobid
= attr
->values
[0].integer
;
9800 * Got a job URI; parse it to get the job ID...
9803 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9804 sizeof(scheme
), username
, sizeof(username
), host
,
9805 sizeof(host
), &port
, resource
, sizeof(resource
));
9807 if (strncmp(resource
, "/jobs/", 6))
9813 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri \"%s\"."),
9814 uri
->values
[0].string
.text
);
9818 jobid
= atoi(resource
+ 6);
9822 * See if the job exists...
9825 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9828 * Nope - return a "not found" error...
9831 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist."), jobid
);
9836 * See if the job has been completed...
9839 if (job
->state_value
> IPP_JOB_STOPPED
)
9842 * Return a "not-possible" error...
9845 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9846 _("Job #%d is finished and cannot be altered."), jobid
);
9851 * See if the job is owned by the requesting user...
9854 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9856 send_http_error(con
, con
->username
[0] ? HTTP_FORBIDDEN
: HTTP_UNAUTHORIZED
,
9857 cupsdFindDest(job
->dest
));
9862 * See what the user wants to change.
9870 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
9872 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
9875 if (!strcmp(attr
->name
, "attributes-charset") ||
9876 !strcmp(attr
->name
, "attributes-natural-language") ||
9877 !strcmp(attr
->name
, "document-compression") ||
9878 !strcmp(attr
->name
, "document-format") ||
9879 !strcmp(attr
->name
, "job-detailed-status-messages") ||
9880 !strcmp(attr
->name
, "job-document-access-errors") ||
9881 !strcmp(attr
->name
, "job-id") ||
9882 !strcmp(attr
->name
, "job-impressions-completed") ||
9883 !strcmp(attr
->name
, "job-k-octets") ||
9884 !strcmp(attr
->name
, "job-originating-host-name") ||
9885 !strcmp(attr
->name
, "job-originating-user-name") ||
9886 !strcmp(attr
->name
, "job-printer-up-time") ||
9887 !strcmp(attr
->name
, "job-printer-uri") ||
9888 !strcmp(attr
->name
, "job-sheets") ||
9889 !strcmp(attr
->name
, "job-state-message") ||
9890 !strcmp(attr
->name
, "job-state-reasons") ||
9891 !strcmp(attr
->name
, "job-uri") ||
9892 !strcmp(attr
->name
, "number-of-documents") ||
9893 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
9894 !strcmp(attr
->name
, "output-device-assigned") ||
9895 !strncmp(attr
->name
, "date-time-at-", 13) ||
9896 !strncmp(attr
->name
, "job-k-octets", 12) ||
9897 !strncmp(attr
->name
, "job-media-sheets", 16) ||
9898 !strncmp(attr
->name
, "time-at-", 8))
9904 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
9905 _("%s cannot be changed."), attr
->name
);
9907 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9908 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9912 if (!strcmp(attr
->name
, "job-priority"))
9915 * Change the job priority...
9918 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
9920 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value."));
9922 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9923 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9925 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
9927 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9928 _("Job is completed and cannot be changed."));
9931 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9933 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
9934 attr
->values
[0].integer
);
9935 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
9938 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
9939 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
9942 else if (!strcmp(attr
->name
, "job-state"))
9945 * Change the job state...
9948 if (attr
->value_tag
!= IPP_TAG_ENUM
)
9950 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value."));
9952 attr2
= ippCopyAttribute(con
->response
, attr
, 0);
9953 ippSetGroupTag(con
->response
, &attr2
, IPP_TAG_UNSUPPORTED_GROUP
);
9957 switch (attr
->values
[0].integer
)
9959 case IPP_JOB_PENDING
:
9961 if (job
->state_value
> IPP_JOB_HELD
)
9963 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9964 _("Job state cannot be changed."));
9967 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9969 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
9970 attr
->values
[0].integer
);
9971 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
, CUPSD_JOB_DEFAULT
, "Job state changed by \"%s\"", username
);
9976 case IPP_JOB_PROCESSING
:
9977 case IPP_JOB_STOPPED
:
9978 if (job
->state_value
!= attr
->values
[0].integer
)
9980 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9981 _("Job state cannot be changed."));
9986 case IPP_JOB_CANCELED
:
9987 case IPP_JOB_ABORTED
:
9988 case IPP_JOB_COMPLETED
:
9989 if (job
->state_value
> IPP_JOB_PROCESSING
)
9991 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9992 _("Job state cannot be changed."));
9995 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
9997 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
9998 attr
->values
[0].integer
);
9999 cupsdSetJobState(job
, (ipp_jstate_t
)attr
->values
[0].integer
,
10001 "Job state changed by \"%s\"", username
);
10008 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10010 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10011 IPP_TAG_ZERO
)) != NULL
)
10014 * Some other value; first free the old value...
10017 if (job
->attrs
->prev
)
10018 job
->attrs
->prev
->next
= attr2
->next
;
10020 job
->attrs
->attrs
= attr2
->next
;
10022 if (job
->attrs
->last
== attr2
)
10023 job
->attrs
->last
= job
->attrs
->prev
;
10025 ippDeleteAttribute(NULL
, attr2
);
10028 * Then copy the attribute...
10031 ippCopyAttribute(job
->attrs
, attr
, 0);
10034 * See if the job-name or job-hold-until is being changed.
10037 if (!strcmp(attr
->name
, "job-hold-until"))
10039 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10040 attr
->values
[0].string
.text
);
10041 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
, 0);
10043 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10045 cupsdReleaseJob(job
);
10049 cupsdSetJobState(job
, IPP_JOB_HELD
, CUPSD_JOB_DEFAULT
,
10050 "Job held by \"%s\".", username
);
10052 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10055 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10058 * Delete the attribute...
10061 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10062 IPP_TAG_ZERO
)) != NULL
)
10064 if (job
->attrs
->prev
)
10065 job
->attrs
->prev
->next
= attr2
->next
;
10067 job
->attrs
->attrs
= attr2
->next
;
10069 if (attr2
== job
->attrs
->last
)
10070 job
->attrs
->last
= job
->attrs
->prev
;
10072 ippDeleteAttribute(NULL
, attr2
);
10074 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10080 * Add new option by copying it...
10083 ippCopyAttribute(job
->attrs
, attr
, 0);
10085 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10094 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10097 * Send events as needed...
10100 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10101 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10102 cupsdFindDest(job
->dest
), job
,
10103 "Job priority changed by user.");
10105 if (event
& CUPSD_EVENT_JOB_STATE
)
10106 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10107 job
->state_value
== IPP_JOB_HELD
?
10108 "Job held by user." : "Job restarted by user.");
10110 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10111 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10112 "Job options changed by user.");
10115 * Start jobs if possible...
10124 * 'set_printer_attrs()' - Set printer attributes.
10128 set_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
10129 ipp_attribute_t
*uri
) /* I - Printer */
10131 http_status_t status
; /* Policy status */
10132 cups_ptype_t dtype
; /* Destination type (printer/class) */
10133 cupsd_printer_t
*printer
; /* Printer/class */
10134 ipp_attribute_t
*attr
; /* Printer attribute */
10135 int changed
= 0; /* Was anything changed? */
10138 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_attrs(%p[%d], %s)", con
,
10139 con
->number
, uri
->values
[0].string
.text
);
10142 * Is the destination valid?
10145 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10151 send_ipp_status(con
, IPP_NOT_FOUND
,
10152 _("The printer or class does not exist."));
10160 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10162 send_http_error(con
, status
, printer
);
10167 * Return a list of attributes that can be set via Set-Printer-Attributes.
10170 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
10171 IPP_TAG_TEXT
)) != NULL
)
10173 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
10177 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
10178 IPP_TAG_TEXT
)) != NULL
)
10180 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
10185 * Update the printer attributes and return...
10190 cupsdSetPrinterAttrs(printer
);
10191 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
10193 cupsdAddEvent(CUPSD_EVENT_PRINTER_CONFIG
, printer
, NULL
,
10194 "Printer \"%s\" description or location changed by \"%s\".",
10195 printer
->name
, get_username(con
));
10197 cupsdLogMessage(CUPSD_LOG_INFO
,
10198 "Printer \"%s\" description or location changed by \"%s\".",
10199 printer
->name
, get_username(con
));
10202 con
->response
->request
.status
.status_code
= IPP_OK
;
10207 * 'set_printer_defaults()' - Set printer default options from a request.
10211 set_printer_defaults(
10212 cupsd_client_t
*con
, /* I - Client connection */
10213 cupsd_printer_t
*printer
) /* I - Printer */
10215 int i
; /* Looping var */
10216 ipp_attribute_t
*attr
; /* Current attribute */
10217 size_t namelen
; /* Length of attribute name */
10218 char name
[256], /* New attribute name */
10219 value
[256]; /* String version of integer attrs */
10222 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10225 * Skip non-printer attributes...
10228 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10231 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10233 if (!strcmp(attr
->name
, "job-sheets-default"))
10236 * Only allow keywords and names...
10239 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10243 * Only allow job-sheets-default to be set when running without a
10244 * system high classification level...
10247 if (Classification
)
10250 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10252 if (attr
->num_values
> 1)
10253 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10255 cupsdSetString(&printer
->job_sheets
[1], "none");
10257 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10259 cupsdFreeStrings(&(printer
->users
));
10261 printer
->deny_users
= 0;
10263 if (attr
->value_tag
== IPP_TAG_NAME
&&
10264 (attr
->num_values
> 1 ||
10265 strcmp(attr
->values
[0].string
.text
, "all")))
10267 for (i
= 0; i
< attr
->num_values
; i
++)
10268 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10271 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10273 cupsdFreeStrings(&(printer
->users
));
10275 printer
->deny_users
= 1;
10277 if (attr
->value_tag
== IPP_TAG_NAME
&&
10278 (attr
->num_values
> 1 ||
10279 strcmp(attr
->values
[0].string
.text
, "none")))
10281 for (i
= 0; i
< attr
->num_values
; i
++)
10282 cupsdAddString(&(printer
->users
), attr
->values
[i
].string
.text
);
10285 else if (!strcmp(attr
->name
, "job-quota-period"))
10287 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10290 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10291 attr
->values
[0].integer
);
10292 cupsdFreeQuotas(printer
);
10294 printer
->quota_period
= attr
->values
[0].integer
;
10296 else if (!strcmp(attr
->name
, "job-k-limit"))
10298 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10301 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10302 attr
->values
[0].integer
);
10303 cupsdFreeQuotas(printer
);
10305 printer
->k_limit
= attr
->values
[0].integer
;
10307 else if (!strcmp(attr
->name
, "job-page-limit"))
10309 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10312 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10313 attr
->values
[0].integer
);
10314 cupsdFreeQuotas(printer
);
10316 printer
->page_limit
= attr
->values
[0].integer
;
10318 else if (!strcmp(attr
->name
, "printer-op-policy"))
10320 cupsd_policy_t
*p
; /* Policy */
10323 if (attr
->value_tag
!= IPP_TAG_NAME
)
10326 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10328 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10329 "Setting printer-op-policy to \"%s\"...",
10330 attr
->values
[0].string
.text
);
10331 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10332 printer
->op_policy_ptr
= p
;
10336 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10337 _("Unknown printer-op-policy \"%s\"."),
10338 attr
->values
[0].string
.text
);
10342 else if (!strcmp(attr
->name
, "printer-error-policy"))
10344 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10347 if (strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10348 ((printer
->type
& CUPS_PRINTER_CLASS
) ||
10349 (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10350 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10351 strcmp(attr
->values
[0].string
.text
, "stop-printer"))))
10353 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10354 _("Unknown printer-error-policy \"%s\"."),
10355 attr
->values
[0].string
.text
);
10359 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10360 "Setting printer-error-policy to \"%s\"...",
10361 attr
->values
[0].string
.text
);
10362 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10366 * Skip any other non-default attributes...
10369 namelen
= strlen(attr
->name
);
10370 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10371 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10375 * OK, anything else must be a user-defined default...
10378 strlcpy(name
, attr
->name
, sizeof(name
));
10379 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10381 switch (attr
->value_tag
)
10383 case IPP_TAG_DELETEATTR
:
10384 printer
->num_options
= cupsRemoveOption(name
,
10385 printer
->num_options
,
10386 &(printer
->options
));
10387 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10388 "Deleting %s", attr
->name
);
10391 case IPP_TAG_NAME
:
10392 case IPP_TAG_TEXT
:
10393 case IPP_TAG_KEYWORD
:
10395 printer
->num_options
= cupsAddOption(name
,
10396 attr
->values
[0].string
.text
,
10397 printer
->num_options
,
10398 &(printer
->options
));
10399 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10400 "Setting %s to \"%s\"...", attr
->name
,
10401 attr
->values
[0].string
.text
);
10404 case IPP_TAG_BOOLEAN
:
10405 printer
->num_options
= cupsAddOption(name
,
10406 attr
->values
[0].boolean
?
10408 printer
->num_options
,
10409 &(printer
->options
));
10410 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10411 "Setting %s to %s...", attr
->name
,
10412 attr
->values
[0].boolean
? "true" : "false");
10415 case IPP_TAG_INTEGER
:
10416 case IPP_TAG_ENUM
:
10417 sprintf(value
, "%d", attr
->values
[0].integer
);
10418 printer
->num_options
= cupsAddOption(name
, value
,
10419 printer
->num_options
,
10420 &(printer
->options
));
10421 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10422 "Setting %s to %s...", attr
->name
, value
);
10425 case IPP_TAG_RANGE
:
10426 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10427 attr
->values
[0].range
.upper
);
10428 printer
->num_options
= cupsAddOption(name
, value
,
10429 printer
->num_options
,
10430 &(printer
->options
));
10431 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10432 "Setting %s to %s...", attr
->name
, value
);
10435 case IPP_TAG_RESOLUTION
:
10436 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10437 attr
->values
[0].resolution
.yres
,
10438 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10440 printer
->num_options
= cupsAddOption(name
, value
,
10441 printer
->num_options
,
10442 &(printer
->options
));
10443 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10444 "Setting %s to %s...", attr
->name
, value
);
10448 /* Do nothing for other values */
10456 * 'start_printer()' - Start a printer.
10460 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10461 ipp_attribute_t
*uri
) /* I - Printer URI */
10463 int i
; /* Temporary variable */
10464 http_status_t status
; /* Policy status */
10465 cups_ptype_t dtype
; /* Destination type (printer/class) */
10466 cupsd_printer_t
*printer
; /* Printer data */
10469 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10470 con
->number
, uri
->values
[0].string
.text
);
10473 * Is the destination valid?
10476 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10482 send_ipp_status(con
, IPP_NOT_FOUND
,
10483 _("The printer or class does not exist."));
10491 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10493 send_http_error(con
, status
, printer
);
10498 * Start the printer...
10501 printer
->state_message
[0] = '\0';
10503 cupsdStartPrinter(printer
, 1);
10505 if (dtype
& CUPS_PRINTER_CLASS
)
10506 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10507 printer
->name
, get_username(con
));
10509 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10510 printer
->name
, get_username(con
));
10518 if ((i
= check_quotas(con
, printer
)) < 0)
10520 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
10525 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
10530 * Everything was ok, so return OK status...
10533 con
->response
->request
.status
.status_code
= IPP_OK
;
10538 * 'stop_printer()' - Stop a printer.
10542 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10543 ipp_attribute_t
*uri
) /* I - Printer URI */
10545 http_status_t status
; /* Policy status */
10546 cups_ptype_t dtype
; /* Destination type (printer/class) */
10547 cupsd_printer_t
*printer
; /* Printer data */
10548 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10551 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10552 con
->number
, uri
->values
[0].string
.text
);
10555 * Is the destination valid?
10558 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10564 send_ipp_status(con
, IPP_NOT_FOUND
,
10565 _("The printer or class does not exist."));
10573 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10575 send_http_error(con
, status
, printer
);
10580 * Stop the printer...
10583 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10584 IPP_TAG_TEXT
)) == NULL
)
10585 strlcpy(printer
->state_message
, "Paused", sizeof(printer
->state_message
));
10588 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10589 sizeof(printer
->state_message
));
10592 cupsdStopPrinter(printer
, 1);
10594 if (dtype
& CUPS_PRINTER_CLASS
)
10595 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10596 printer
->name
, get_username(con
));
10598 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10599 printer
->name
, get_username(con
));
10602 * Everything was ok, so return OK status...
10605 con
->response
->request
.status
.status_code
= IPP_OK
;
10610 * 'url_encode_attr()' - URL-encode a string attribute.
10614 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10615 char *buffer
,/* I - String buffer */
10616 size_t bufsize
)/* I - Size of buffer */
10618 int i
; /* Looping var */
10619 char *bufptr
, /* Pointer into buffer */
10620 *bufend
; /* End of buffer */
10623 strlcpy(buffer
, attr
->name
, bufsize
);
10624 bufptr
= buffer
+ strlen(buffer
);
10625 bufend
= buffer
+ bufsize
- 1;
10627 for (i
= 0; i
< attr
->num_values
; i
++)
10629 if (bufptr
>= bufend
)
10637 if (bufptr
>= bufend
)
10642 bufptr
= url_encode_string(attr
->values
[i
].string
.text
, bufptr
, (size_t)(bufend
- bufptr
+ 1));
10644 if (bufptr
>= bufend
)
10655 * 'url_encode_string()' - URL-encode a string.
10658 static char * /* O - End of string */
10659 url_encode_string(const char *s
, /* I - String */
10660 char *buffer
, /* I - String buffer */
10661 size_t bufsize
) /* I - Size of buffer */
10663 char *bufptr
, /* Pointer into buffer */
10664 *bufend
; /* End of buffer */
10665 static const char *hex
= "0123456789ABCDEF";
10670 bufend
= buffer
+ bufsize
- 1;
10672 while (*s
&& bufptr
< bufend
)
10674 if (*s
== ' ' || *s
== '%' || *s
== '+')
10676 if (bufptr
>= (bufend
- 2))
10680 *bufptr
++ = hex
[(*s
>> 4) & 15];
10681 *bufptr
++ = hex
[*s
& 15];
10685 else if (*s
== '\'' || *s
== '\\')
10687 if (bufptr
>= (bufend
- 1))
10704 * 'user_allowed()' - See if a user is allowed to print to a queue.
10707 static int /* O - 0 if not allowed, 1 if allowed */
10708 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
10709 const char *username
) /* I - Username */
10711 struct passwd
*pw
; /* User password data */
10712 char baseuser
[256], /* Base username */
10713 *baseptr
, /* Pointer to "@" in base username */
10714 *name
; /* Current user name */
10717 if (cupsArrayCount(p
->users
) == 0)
10720 if (!strcmp(username
, "root"))
10723 if (strchr(username
, '@'))
10726 * Strip @REALM for username check...
10729 strlcpy(baseuser
, username
, sizeof(baseuser
));
10731 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
10734 username
= baseuser
;
10737 pw
= getpwnam(username
);
10740 for (name
= (char *)cupsArrayFirst(p
->users
);
10742 name
= (char *)cupsArrayNext(p
->users
))
10744 if (name
[0] == '@')
10747 * Check group membership...
10750 if (cupsdCheckGroup(username
, pw
, name
+ 1))
10753 else if (name
[0] == '#')
10759 if (cupsdCheckGroup(username
, pw
, name
))
10762 else if (!_cups_strcasecmp(username
, name
))
10766 return ((name
!= NULL
) != p
->deny_users
);
10771 * 'validate_job()' - Validate printer options and destination.
10775 validate_job(cupsd_client_t
*con
, /* I - Client connection */
10776 ipp_attribute_t
*uri
) /* I - Printer URI */
10778 http_status_t status
; /* Policy status */
10779 ipp_attribute_t
*attr
; /* Current attribute */
10781 ipp_attribute_t
*auth_info
; /* auth-info attribute */
10782 #endif /* HAVE_SSL */
10783 ipp_attribute_t
*format
, /* Document-format attribute */
10784 *name
; /* Job-name attribute */
10785 cups_ptype_t dtype
; /* Destination type (printer/class) */
10786 char super
[MIME_MAX_SUPER
],
10787 /* Supertype of file */
10788 type
[MIME_MAX_TYPE
];
10789 /* Subtype of file */
10790 cupsd_printer_t
*printer
; /* Printer */
10793 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
10794 con
->number
, uri
->values
[0].string
.text
);
10797 * OK, see if the client is sending the document compressed - CUPS
10798 * doesn't support compression yet...
10801 if ((attr
= ippFindAttribute(con
->request
, "compression",
10802 IPP_TAG_KEYWORD
)) != NULL
)
10804 if (strcmp(attr
->values
[0].string
.text
, "none")
10806 && strcmp(attr
->values
[0].string
.text
, "gzip")
10807 #endif /* HAVE_LIBZ */
10810 send_ipp_status(con
, IPP_ATTRIBUTES
,
10811 _("Unsupported 'compression' value \"%s\"."),
10812 attr
->values
[0].string
.text
);
10813 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
10814 "compression", NULL
, attr
->values
[0].string
.text
);
10820 * Is it a format we support?
10823 if ((format
= ippFindAttribute(con
->request
, "document-format",
10824 IPP_TAG_MIMETYPE
)) != NULL
)
10826 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%255[^;]",
10829 send_ipp_status(con
, IPP_BAD_REQUEST
,
10830 _("Bad 'document-format' value \"%s\"."),
10831 format
->values
[0].string
.text
);
10835 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
10836 !mimeType(MimeDatabase
, super
, type
))
10838 cupsdLogMessage(CUPSD_LOG_INFO
,
10839 "Hint: Do you have the raw file printing rules enabled?");
10840 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
10841 _("Unsupported 'document-format' value \"%s\"."),
10842 format
->values
[0].string
.text
);
10843 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
10844 "document-format", NULL
, format
->values
[0].string
.text
);
10850 * Is the job-name valid?
10853 if ((name
= ippFindAttribute(con
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
10855 int bad_name
= 0; /* Is the job-name value bad? */
10857 if ((name
->value_tag
!= IPP_TAG_NAME
&& name
->value_tag
!= IPP_TAG_NAMELANG
) ||
10858 name
->num_values
!= 1)
10865 * Validate that job-name conforms to RFC 5198 (Network Unicode) and
10866 * IPP Everywhere requirements for "name" values...
10869 const unsigned char *nameptr
; /* Pointer into "job-name" attribute */
10871 for (nameptr
= (unsigned char *)name
->values
[0].string
.text
;
10875 if (*nameptr
< ' ' && *nameptr
!= '\t')
10877 else if (*nameptr
== 0x7f)
10879 else if ((*nameptr
& 0xe0) == 0xc0)
10881 if ((nameptr
[1] & 0xc0) != 0x80)
10886 else if ((*nameptr
& 0xf0) == 0xe0)
10888 if ((nameptr
[1] & 0xc0) != 0x80 ||
10889 (nameptr
[2] & 0xc0) != 0x80)
10894 else if ((*nameptr
& 0xf8) == 0xf0)
10896 if ((nameptr
[1] & 0xc0) != 0x80 ||
10897 (nameptr
[2] & 0xc0) != 0x80 ||
10898 (nameptr
[3] & 0xc0) != 0x80)
10903 else if (*nameptr
& 0x80)
10913 if (StrictConformance
)
10915 send_ipp_status(con
, IPP_ATTRIBUTES
,
10916 _("Unsupported 'job-name' value."));
10917 ippCopyAttribute(con
->response
, name
, 0);
10922 cupsdLogMessage(CUPSD_LOG_WARN
,
10923 "Unsupported 'job-name' value, deleting from request.");
10924 ippDeleteAttribute(con
->request
, name
);
10930 * Is the destination valid?
10933 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10939 send_ipp_status(con
, IPP_NOT_FOUND
,
10940 _("The printer or class does not exist."));
10949 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
10950 #endif /* HAVE_SSL */
10952 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10954 send_http_error(con
, status
, printer
);
10957 else if (printer
->num_auth_info_required
== 1 &&
10958 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
10961 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
10965 else if (auth_info
&& !con
->http
->tls
&&
10966 !httpAddrLocalhost(con
->http
->hostaddr
))
10969 * Require encryption of auth-info over non-local connections...
10972 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
10975 #endif /* HAVE_SSL */
10978 * Everything was ok, so return OK status...
10981 con
->response
->request
.status
.status_code
= IPP_OK
;
10986 * 'validate_name()' - Make sure the printer name only contains valid chars.
10989 static int /* O - 0 if name is no good, 1 if good */
10990 validate_name(const char *name
) /* I - Name to check */
10992 const char *ptr
; /* Pointer into name */
10996 * Scan the whole name...
10999 for (ptr
= name
; *ptr
; ptr
++)
11000 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11004 * All the characters are good; validate the length, too...
11007 return ((ptr
- name
) < 128);
11012 * 'validate_user()' - Validate the user for the request.
11015 static int /* O - 1 if permitted, 0 otherwise */
11016 validate_user(cupsd_job_t
*job
, /* I - Job */
11017 cupsd_client_t
*con
, /* I - Client connection */
11018 const char *owner
, /* I - Owner of job/resource */
11019 char *username
, /* O - Authenticated username */
11020 size_t userlen
) /* I - Length of username */
11022 cupsd_printer_t
*printer
; /* Printer for job */
11025 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
);
11028 * Validate input...
11031 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11035 * Get the best authenticated username that is available.
11038 strlcpy(username
, get_username(con
), userlen
);
11041 * Check the username against the owner...
11044 printer
= cupsdFindDest(job
->dest
);
11046 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11047 con
, owner
) == HTTP_OK
);
11052 * End of "$Id: ipp.c 12778 2015-07-07 17:28:51Z msweet $".