2 * "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $"
4 * IPP routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2008 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 * cupsdProcessIPPRequest() - Process an incoming IPP request.
21 * cupsdTimeoutJob() - Timeout a job waiting on job files.
22 * accept_jobs() - Accept print jobs to a printer.
23 * add_class() - Add a class to the system.
24 * add_file() - Add a file to a job.
25 * add_job() - Add a job to a print queue.
26 * add_job_state_reasons() - Add the "job-state-reasons" attribute based
27 * upon the job and printer state...
28 * add_job_subscriptions() - Add any subscriptions for a job.
29 * add_job_uuid() - Add job-uuid attribute to a job.
30 * add_printer() - Add a printer to the system.
31 * add_printer_state_reasons() - Add the "printer-state-reasons" attribute
32 * based upon the printer state...
33 * add_queued_job_count() - Add the "queued-job-count" attribute for the
34 * specified printer or class.
35 * apple_init_profile() - Initialize a color profile.
36 * apple_register_profiles() - Register color profiles for a printer.
37 * apple_unregister_profiles() - Remove color profiles for the specified
39 * apply_printer_defaults() - Apply printer default options to a job.
40 * authenticate_job() - Set job authentication info.
41 * cancel_all_jobs() - Cancel all print jobs.
42 * cancel_job() - Cancel a print job.
43 * cancel_subscription() - Cancel a subscription.
44 * check_quotas() - Check quotas for a printer and user.
45 * check_rss_recipient() - Check that we do not have a duplicate RSS
47 * copy_attribute() - Copy a single attribute.
48 * copy_attrs() - Copy attributes from one request to another.
49 * copy_banner() - Copy a banner file to the requests directory
50 * for the specified job.
51 * copy_file() - Copy a PPD file or interface script...
52 * copy_model() - Copy a PPD model file, substituting default
54 * copy_job_attrs() - Copy job attributes.
55 * copy_printer_attrs() - Copy printer attributes.
56 * copy_subscription_attrs() - Copy subscription attributes.
57 * create_job() - Print a file to a printer or class.
58 * create_requested_array() - Create an array for the requested-attributes.
59 * create_subscription() - Create a notification subscription.
60 * delete_printer() - Remove a printer or class from the system.
61 * get_default() - Get the default destination.
62 * get_devices() - Get the list of available devices on the
64 * get_document() - Get a copy of a job file.
65 * get_job_attrs() - Get job attributes.
66 * get_jobs() - Get a list of jobs for the specified printer.
67 * get_notifications() - Get events for a subscription.
68 * get_ppd() - Get a named PPD from the local system.
69 * get_ppds() - Get the list of PPD files on the local
71 * get_printer_attrs() - Get printer attributes.
72 * get_printers() - Get a list of printers or classes.
73 * get_subscription_attrs() - Get subscription attributes.
74 * get_subscriptions() - Get subscriptions.
75 * get_username() - Get the username associated with a request.
76 * hold_job() - Hold a print job.
77 * move_job() - Move a job to a new destination.
78 * ppd_parse_line() - Parse a PPD default line.
79 * print_job() - Print a file to a printer or class.
80 * read_job_ticket() - Read a job ticket embedded in a print file.
81 * reject_jobs() - Reject print jobs to a printer.
82 * release_job() - Release a held print job.
83 * renew_subscription() - Renew an existing subscription...
84 * restart_job() - Restart an old print job.
85 * save_auth_info() - Save authentication information for a job.
86 * save_krb5_creds() - Save Kerberos credentials for the job.
87 * send_document() - Send a file to a printer or class.
88 * send_http_error() - Send a HTTP error back to the IPP client.
89 * send_ipp_status() - Send a status back to the IPP client.
90 * set_default() - Set the default destination...
91 * set_job_attrs() - Set job attributes.
92 * set_printer_defaults() - Set printer default options from a request.
93 * start_printer() - Start a printer.
94 * stop_printer() - Stop a printer.
95 * url_encode_attr() - URL-encode a string attribute.
96 * url_encode_string() - URL-encode a string.
97 * user_allowed() - See if a user is allowed to print to a queue.
98 * validate_job() - Validate printer options and destination.
99 * validate_name() - Make sure the printer name only contains
101 * validate_user() - Validate the user for the request.
105 * Include necessary headers...
109 #include <cups/ppd-private.h>
113 #endif /* HAVE_LIBPAPER */
116 # include <ApplicationServices/ApplicationServices.h>
117 # include <CoreFoundation/CoreFoundation.h>
118 # ifdef HAVE_MEMBERSHIP_H
119 # include <membership.h>
120 # endif /* HAVE_MEMBERSHIP_H */
121 # ifdef HAVE_MEMBERSHIPPRIV_H
122 # include <membershipPriv.h>
124 extern int mbr_user_name_to_uuid(const char* name
, uuid_t uu
);
125 extern int mbr_group_name_to_uuid(const char* name
, uuid_t uu
);
126 extern int mbr_check_membership_by_id(uuid_t user
, gid_t group
, int* ismember
);
127 # endif /* HAVE_MEMBERSHIPPRIV_H */
128 #endif /* __APPLE__ */
135 static void accept_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
136 static void add_class(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
137 static int add_file(cupsd_client_t
*con
, cupsd_job_t
*job
,
138 mime_type_t
*filetype
, int compression
);
139 static cupsd_job_t
*add_job(cupsd_client_t
*con
, cupsd_printer_t
*printer
,
140 mime_type_t
*filetype
);
141 static void add_job_state_reasons(cupsd_client_t
*con
, cupsd_job_t
*job
);
142 static void add_job_subscriptions(cupsd_client_t
*con
, cupsd_job_t
*job
);
143 static void add_job_uuid(cupsd_client_t
*con
, cupsd_job_t
*job
);
144 static void add_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
145 static void add_printer_state_reasons(cupsd_client_t
*con
,
147 static void add_queued_job_count(cupsd_client_t
*con
, cupsd_printer_t
*p
);
149 static void apple_init_profile(ppd_file_t
*ppd
, cups_array_t
*languages
,
150 CMDeviceProfileInfo
*profile
, unsigned id
,
151 const char *name
, const char *text
,
152 const char *iccfile
);
153 static void apple_register_profiles(cupsd_printer_t
*p
);
154 static void apple_unregister_profiles(cupsd_printer_t
*p
);
155 #endif /* __APPLE__ */
156 static void apply_printer_defaults(cupsd_printer_t
*printer
,
158 static void authenticate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
159 static void cancel_all_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
160 static void cancel_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
161 static void cancel_subscription(cupsd_client_t
*con
, int id
);
162 static int check_rss_recipient(const char *recipient
);
163 static int check_quotas(cupsd_client_t
*con
, cupsd_printer_t
*p
);
164 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
166 static void copy_attrs(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
167 ipp_tag_t group
, int quickcopy
);
168 static int copy_banner(cupsd_client_t
*con
, cupsd_job_t
*job
,
170 static int copy_file(const char *from
, const char *to
);
171 static int copy_model(cupsd_client_t
*con
, const char *from
,
173 static void copy_job_attrs(cupsd_client_t
*con
,
176 static void copy_printer_attrs(cupsd_client_t
*con
,
177 cupsd_printer_t
*printer
,
179 static void copy_subscription_attrs(cupsd_client_t
*con
,
180 cupsd_subscription_t
*sub
,
182 static void create_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
183 static cups_array_t
*create_requested_array(ipp_t
*request
);
184 static void create_subscription(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
185 static void delete_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
186 static void get_default(cupsd_client_t
*con
);
187 static void get_devices(cupsd_client_t
*con
);
188 static void get_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
189 static void get_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
190 static void get_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
191 static void get_notifications(cupsd_client_t
*con
);
192 static void get_ppd(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
193 static void get_ppds(cupsd_client_t
*con
);
194 static void get_printers(cupsd_client_t
*con
, int type
);
195 static void get_printer_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
196 static void get_subscription_attrs(cupsd_client_t
*con
, int sub_id
);
197 static void get_subscriptions(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
198 static const char *get_username(cupsd_client_t
*con
);
199 static void hold_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
200 static void hold_new_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
201 static void move_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
202 static int ppd_parse_line(const char *line
, char *option
, int olen
,
203 char *choice
, int clen
);
204 static void print_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
205 static void read_job_ticket(cupsd_client_t
*con
);
206 static void reject_jobs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
207 static void release_held_new_jobs(cupsd_client_t
*con
,
208 ipp_attribute_t
*uri
);
209 static void release_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
210 static void renew_subscription(cupsd_client_t
*con
, int sub_id
);
211 static void restart_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
212 static void save_auth_info(cupsd_client_t
*con
, cupsd_job_t
*job
,
213 ipp_attribute_t
*auth_info
);
214 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
215 static void save_krb5_creds(cupsd_client_t
*con
, cupsd_job_t
*job
);
216 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
217 static void send_document(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
218 static void send_http_error(cupsd_client_t
*con
, http_status_t status
,
219 cupsd_printer_t
*printer
);
220 static void send_ipp_status(cupsd_client_t
*con
, ipp_status_t status
,
221 const char *message
, ...)
223 __attribute__ ((__format__ (__printf__
, 3, 4)))
224 # endif /* __GNUC__ */
226 static void set_default(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
227 static void set_job_attrs(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
228 static void set_printer_defaults(cupsd_client_t
*con
,
229 cupsd_printer_t
*printer
);
230 static void start_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
231 static void stop_printer(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
232 static void url_encode_attr(ipp_attribute_t
*attr
, char *buffer
,
234 static char *url_encode_string(const char *s
, char *buffer
, int bufsize
);
235 static int user_allowed(cupsd_printer_t
*p
, const char *username
);
236 static void validate_job(cupsd_client_t
*con
, ipp_attribute_t
*uri
);
237 static int validate_name(const char *name
);
238 static int validate_user(cupsd_job_t
*job
, cupsd_client_t
*con
,
239 const char *owner
, char *username
,
244 * 'cupsdProcessIPPRequest()' - Process an incoming IPP request.
247 int /* O - 1 on success, 0 on failure */
248 cupsdProcessIPPRequest(
249 cupsd_client_t
*con
) /* I - Client connection */
251 ipp_tag_t group
; /* Current group tag */
252 ipp_attribute_t
*attr
; /* Current attribute */
253 ipp_attribute_t
*charset
; /* Character set attribute */
254 ipp_attribute_t
*language
; /* Language attribute */
255 ipp_attribute_t
*uri
= NULL
; /* Printer or job URI attribute */
256 ipp_attribute_t
*username
; /* requesting-user-name attr */
257 int sub_id
; /* Subscription ID */
260 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
261 "cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
262 con
, con
->http
.fd
, con
->request
->request
.op
.operation_id
);
265 * First build an empty response message for this request...
268 con
->response
= ippNew();
270 con
->response
->request
.status
.version
[0] =
271 con
->request
->request
.op
.version
[0];
272 con
->response
->request
.status
.version
[1] =
273 con
->request
->request
.op
.version
[1];
274 con
->response
->request
.status
.request_id
=
275 con
->request
->request
.op
.request_id
;
278 * Then validate the request header and required attributes...
281 if (con
->request
->request
.any
.version
[0] != 1)
284 * Return an error, since we only support IPP 1.x.
287 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
288 "%04X %s Bad request version number %d.%d",
289 IPP_VERSION_NOT_SUPPORTED
, con
->http
.hostname
,
290 con
->request
->request
.any
.version
[0],
291 con
->request
->request
.any
.version
[1]);
293 send_ipp_status(con
, IPP_VERSION_NOT_SUPPORTED
,
294 _("Bad request version number %d.%d!"),
295 con
->request
->request
.any
.version
[0],
296 con
->request
->request
.any
.version
[1]);
298 else if (!con
->request
->attrs
)
300 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
301 "%04X %s No attributes in request",
302 IPP_BAD_REQUEST
, con
->http
.hostname
);
304 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No attributes in request!"));
309 * Make sure that the attributes are provided in the correct order and
310 * don't repeat groups...
313 for (attr
= con
->request
->attrs
, group
= attr
->group_tag
;
316 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
319 * Out of order; return an error...
322 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
323 "%04X %s Attribute groups are out of order",
324 IPP_BAD_REQUEST
, con
->http
.hostname
);
326 send_ipp_status(con
, IPP_BAD_REQUEST
,
327 _("Attribute groups are out of order (%x < %x)!"),
328 attr
->group_tag
, group
);
332 group
= attr
->group_tag
;
337 * Then make sure that the first three attributes are:
340 * attributes-natural-language
341 * printer-uri/job-uri
344 attr
= con
->request
->attrs
;
345 if (attr
&& !strcmp(attr
->name
, "attributes-charset") &&
346 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
354 if (attr
&& !strcmp(attr
->name
, "attributes-natural-language") &&
355 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
360 if ((attr
= ippFindAttribute(con
->request
, "printer-uri",
361 IPP_TAG_URI
)) != NULL
)
363 else if ((attr
= ippFindAttribute(con
->request
, "job-uri",
364 IPP_TAG_URI
)) != NULL
)
366 else if (con
->request
->request
.op
.operation_id
== CUPS_GET_PPD
)
367 uri
= ippFindAttribute(con
->request
, "ppd-name", IPP_TAG_NAME
);
372 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
373 "attributes-charset", NULL
,
374 charset
->values
[0].string
.text
);
376 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
377 "attributes-charset", NULL
, DefaultCharset
);
380 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
381 "attributes-natural-language", NULL
,
382 language
->values
[0].string
.text
);
384 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
385 "attributes-natural-language", NULL
, DefaultLanguage
);
388 strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
389 strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
392 * Bad character set...
395 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unsupported character set \"%s\"!",
396 charset
->values
[0].string
.text
);
397 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
398 "%04X %s Unsupported attributes-charset value \"%s\"",
399 IPP_CHARSET
, con
->http
.hostname
,
400 charset
->values
[0].string
.text
);
401 send_ipp_status(con
, IPP_BAD_REQUEST
,
402 _("Unsupported character set \"%s\"!"),
403 charset
->values
[0].string
.text
);
405 else if (!charset
|| !language
||
407 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEFAULT
&&
408 con
->request
->request
.op
.operation_id
!= CUPS_GET_PRINTERS
&&
409 con
->request
->request
.op
.operation_id
!= CUPS_GET_CLASSES
&&
410 con
->request
->request
.op
.operation_id
!= CUPS_GET_DEVICES
&&
411 con
->request
->request
.op
.operation_id
!= CUPS_GET_PPDS
))
414 * Return an error, since attributes-charset,
415 * attributes-natural-language, and printer-uri/job-uri are required
416 * for all operations.
421 cupsdLogMessage(CUPSD_LOG_ERROR
,
422 "Missing attributes-charset attribute!");
424 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
425 "%04X %s Missing attributes-charset attribute",
426 IPP_BAD_REQUEST
, con
->http
.hostname
);
431 cupsdLogMessage(CUPSD_LOG_ERROR
,
432 "Missing attributes-natural-language attribute!");
434 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
435 "%04X %s Missing attributes-natural-language attribute",
436 IPP_BAD_REQUEST
, con
->http
.hostname
);
441 cupsdLogMessage(CUPSD_LOG_ERROR
,
442 "Missing printer-uri, job-uri, or ppd-name "
445 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
446 "%04X %s Missing printer-uri, job-uri, or ppd-name "
447 "attribute", IPP_BAD_REQUEST
, con
->http
.hostname
);
450 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Request attributes follow...");
452 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
453 cupsdLogMessage(CUPSD_LOG_DEBUG
,
454 "attr \"%s\": group_tag = %x, value_tag = %x",
455 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
458 cupsdLogMessage(CUPSD_LOG_DEBUG
, "End of attributes...");
460 send_ipp_status(con
, IPP_BAD_REQUEST
,
461 _("Missing required attributes!"));
466 * OK, all the checks pass so far; make sure requesting-user-name is
467 * not "root" from a remote host...
470 if ((username
= ippFindAttribute(con
->request
, "requesting-user-name",
471 IPP_TAG_NAME
)) != NULL
)
474 * Check for root user...
477 if (!strcmp(username
->values
[0].string
.text
, "root") &&
478 strcasecmp(con
->http
.hostname
, "localhost") &&
479 strcmp(con
->username
, "root"))
482 * Remote unauthenticated user masquerading as local root...
485 _cupsStrFree(username
->values
[0].string
.text
);
486 username
->values
[0].string
.text
= _cupsStrAlloc(RemoteRoot
);
490 if ((attr
= ippFindAttribute(con
->request
, "notify-subscription-id",
491 IPP_TAG_INTEGER
)) != NULL
)
492 sub_id
= attr
->values
[0].integer
;
497 * Then try processing the operation...
501 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s",
502 ippOpString(con
->request
->request
.op
.operation_id
),
503 uri
->values
[0].string
.text
);
505 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s",
506 ippOpString(con
->request
->request
.op
.operation_id
));
508 switch (con
->request
->request
.op
.operation_id
)
514 case IPP_VALIDATE_JOB
:
515 validate_job(con
, uri
);
518 case IPP_CREATE_JOB
:
519 create_job(con
, uri
);
522 case IPP_SEND_DOCUMENT
:
523 send_document(con
, uri
);
526 case IPP_CANCEL_JOB
:
527 cancel_job(con
, uri
);
530 case IPP_GET_JOB_ATTRIBUTES
:
531 get_job_attrs(con
, uri
);
538 case IPP_GET_PRINTER_ATTRIBUTES
:
539 get_printer_attrs(con
, uri
);
546 case IPP_RELEASE_JOB
:
547 release_job(con
, uri
);
550 case IPP_RESTART_JOB
:
551 restart_job(con
, uri
);
554 case IPP_PAUSE_PRINTER
:
555 stop_printer(con
, uri
);
558 case IPP_RESUME_PRINTER
:
559 start_printer(con
, uri
);
562 case IPP_PURGE_JOBS
:
563 cancel_all_jobs(con
, uri
);
566 case IPP_SET_JOB_ATTRIBUTES
:
567 set_job_attrs(con
, uri
);
570 case IPP_HOLD_NEW_JOBS
:
571 hold_new_jobs(con
, uri
);
574 case IPP_RELEASE_HELD_NEW_JOBS
:
575 release_held_new_jobs(con
, uri
);
578 case CUPS_GET_DEFAULT
:
582 case CUPS_GET_PRINTERS
:
583 get_printers(con
, 0);
586 case CUPS_GET_CLASSES
:
587 get_printers(con
, CUPS_PRINTER_CLASS
);
590 case CUPS_ADD_PRINTER
:
591 add_printer(con
, uri
);
594 case CUPS_DELETE_PRINTER
:
595 delete_printer(con
, uri
);
598 case CUPS_ADD_CLASS
:
602 case CUPS_DELETE_CLASS
:
603 delete_printer(con
, uri
);
606 case CUPS_ACCEPT_JOBS
:
607 case IPP_ENABLE_PRINTER
:
608 accept_jobs(con
, uri
);
611 case CUPS_REJECT_JOBS
:
612 case IPP_DISABLE_PRINTER
:
613 reject_jobs(con
, uri
);
616 case CUPS_SET_DEFAULT
:
617 set_default(con
, uri
);
620 case CUPS_GET_DEVICES
:
624 case CUPS_GET_DOCUMENT
:
625 get_document(con
, uri
);
640 case CUPS_AUTHENTICATE_JOB
:
641 authenticate_job(con
, uri
);
644 case IPP_CREATE_PRINTER_SUBSCRIPTION
:
645 case IPP_CREATE_JOB_SUBSCRIPTION
:
646 create_subscription(con
, uri
);
649 case IPP_GET_SUBSCRIPTION_ATTRIBUTES
:
650 get_subscription_attrs(con
, sub_id
);
653 case IPP_GET_SUBSCRIPTIONS
:
654 get_subscriptions(con
, uri
);
657 case IPP_RENEW_SUBSCRIPTION
:
658 renew_subscription(con
, sub_id
);
661 case IPP_CANCEL_SUBSCRIPTION
:
662 cancel_subscription(con
, sub_id
);
665 case IPP_GET_NOTIFICATIONS
:
666 get_notifications(con
);
670 cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT
, NULL
, NULL
,
671 "%04X %s Operation %04X (%s) not supported",
672 IPP_OPERATION_NOT_SUPPORTED
, con
->http
.hostname
,
673 con
->request
->request
.op
.operation_id
,
674 ippOpString(con
->request
->request
.op
.operation_id
));
676 send_ipp_status(con
, IPP_OPERATION_NOT_SUPPORTED
,
677 _("%s not supported!"),
679 con
->request
->request
.op
.operation_id
));
689 * Sending data from the scheduler...
692 cupsdLogMessage(con
->response
->request
.status
.status_code
693 >= IPP_BAD_REQUEST
&&
694 con
->response
->request
.status
.status_code
695 != IPP_NOT_FOUND
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
696 "Returning IPP %s for %s (%s) from %s",
697 ippErrorString(con
->response
->request
.status
.status_code
),
698 ippOpString(con
->request
->request
.op
.operation_id
),
699 uri
? uri
->values
[0].string
.text
: "no URI",
702 if (cupsdSendHeader(con
, HTTP_OK
, "application/ipp", CUPSD_AUTH_NONE
))
704 #ifdef CUPSD_USE_CHUNKING
706 * Because older versions of CUPS (1.1.17 and older) and some IPP
707 * clients do not implement chunking properly, we cannot use
708 * chunking by default. This may become the default in future
709 * CUPS releases, or we might add a configuration directive for
713 if (con
->http
.version
== HTTP_1_1
)
715 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
718 if (cupsdFlushHeader(con
) < 0)
721 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
724 #endif /* CUPSD_USE_CHUNKING */
726 size_t length
; /* Length of response */
729 length
= ippLength(con
->response
);
731 if (con
->file
>= 0 && !con
->pipe_pid
)
733 struct stat fileinfo
; /* File information */
736 if (!fstat(con
->file
, &fileinfo
))
737 length
+= fileinfo
.st_size
;
740 if (httpPrintf(HTTP(con
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
741 CUPS_LLCAST length
) < 0)
744 if (cupsdFlushHeader(con
) < 0)
747 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
748 con
->http
.data_remaining
= length
;
750 if (con
->http
.data_remaining
<= INT_MAX
)
751 con
->http
._data_remaining
= con
->http
.data_remaining
;
753 con
->http
._data_remaining
= INT_MAX
;
756 cupsdAddSelect(con
->http
.fd
, (cupsd_selfunc_t
)cupsdReadClient
,
757 (cupsd_selfunc_t
)cupsdWriteClient
, con
);
760 * Tell the caller the response header was sent successfully...
768 * Tell the caller the response header could not be sent...
777 * Sending data from a subprocess like cups-deviced; tell the caller
778 * everything is A-OK so far...
787 * 'cupsdTimeoutJob()' - Timeout a job waiting on job files.
790 int /* O - 0 on success, -1 on error */
791 cupsdTimeoutJob(cupsd_job_t
*job
) /* I - Job to timeout */
793 cupsd_printer_t
*printer
; /* Destination printer or class */
794 ipp_attribute_t
*attr
; /* job-sheets attribute */
795 int kbytes
; /* Kilobytes in banner */
798 job
->pending_timeout
= 0;
801 * See if we need to add the ending sheet...
804 printer
= cupsdFindDest(job
->dest
);
805 attr
= ippFindAttribute(job
->attrs
, "job-sheets", IPP_TAG_NAME
);
808 !(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
809 attr
&& attr
->num_values
> 1)
815 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding end banner page \"%s\".",
816 attr
->values
[1].string
.text
);
818 if ((kbytes
= copy_banner(NULL
, job
, attr
->values
[1].string
.text
)) < 0)
821 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
829 * 'accept_jobs()' - Accept print jobs to a printer.
833 accept_jobs(cupsd_client_t
*con
, /* I - Client connection */
834 ipp_attribute_t
*uri
) /* I - Printer or class URI */
836 http_status_t status
; /* Policy status */
837 cups_ptype_t dtype
; /* Destination type (printer/class) */
838 cupsd_printer_t
*printer
; /* Printer data */
841 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "accept_jobs(%p[%d], %s)", con
,
842 con
->http
.fd
, uri
->values
[0].string
.text
);
845 * Is the destination valid?
848 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
854 send_ipp_status(con
, IPP_NOT_FOUND
,
855 _("The printer or class was not found."));
863 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
865 send_http_error(con
, status
, printer
);
870 * Accept jobs sent to the printer...
873 printer
->accepting
= 1;
874 printer
->state_message
[0] = '\0';
876 cupsdAddPrinterHistory(printer
);
878 if (dtype
& CUPS_PRINTER_CLASS
)
880 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
882 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" now accepting jobs (\"%s\").",
883 printer
->name
, get_username(con
));
887 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
889 cupsdLogMessage(CUPSD_LOG_INFO
,
890 "Printer \"%s\" now accepting jobs (\"%s\").",
891 printer
->name
, get_username(con
));
895 * Everything was ok, so return OK status...
898 con
->response
->request
.status
.status_code
= IPP_OK
;
903 * 'add_class()' - Add a class to the system.
907 add_class(cupsd_client_t
*con
, /* I - Client connection */
908 ipp_attribute_t
*uri
) /* I - URI of class */
910 http_status_t status
; /* Policy status */
911 int i
; /* Looping var */
912 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
913 username
[HTTP_MAX_URI
], /* Username portion of URI */
914 host
[HTTP_MAX_URI
], /* Host portion of URI */
915 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
916 int port
; /* Port portion of URI */
917 cupsd_printer_t
*pclass
, /* Class */
918 *member
; /* Member printer/class */
919 cups_ptype_t dtype
; /* Destination type */
920 ipp_attribute_t
*attr
; /* Printer attribute */
921 int modify
; /* Non-zero if we just modified */
922 char newname
[IPP_MAX_NAME
]; /* New class name */
923 int need_restart_job
; /* Need to restart job? */
926 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_class(%p[%d], %s)", con
,
927 con
->http
.fd
, uri
->values
[0].string
.text
);
930 * Do we have a valid URI?
933 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
934 sizeof(scheme
), username
, sizeof(username
), host
,
935 sizeof(host
), &port
, resource
, sizeof(resource
));
938 if (strncmp(resource
, "/classes/", 9) || strlen(resource
) == 9)
941 * No, return an error...
944 send_ipp_status(con
, IPP_BAD_REQUEST
,
945 _("The printer-uri must be of the form "
946 "\"ipp://HOSTNAME/classes/CLASSNAME\"."));
951 * Do we have a valid printer name?
954 if (!validate_name(resource
+ 9))
957 * No, return an error...
960 send_ipp_status(con
, IPP_BAD_REQUEST
,
961 _("The printer-uri \"%s\" contains invalid characters."),
962 uri
->values
[0].string
.text
);
967 * See if the class already exists; if not, create a new class...
970 if ((pclass
= cupsdFindClass(resource
+ 9)) == NULL
)
973 * Class doesn't exist; see if we have a printer of the same name...
976 if ((pclass
= cupsdFindPrinter(resource
+ 9)) != NULL
&&
977 !(pclass
->type
& CUPS_PRINTER_DISCOVERED
))
980 * Yes, return an error...
983 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
984 _("A printer named \"%s\" already exists!"),
990 * No, check the default policy and then add the class...
993 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
995 send_http_error(con
, status
, NULL
);
999 pclass
= cupsdAddClass(resource
+ 9);
1002 else if (pclass
->type
& CUPS_PRINTER_IMPLICIT
)
1005 * Check the default policy, then rename the implicit class to "AnyClass"
1009 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1011 send_http_error(con
, status
, NULL
);
1015 if (ImplicitAnyClasses
)
1017 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 9);
1018 cupsdRenamePrinter(pclass
, newname
);
1021 cupsdDeletePrinter(pclass
, 1);
1024 * Add the class as a new local class...
1027 pclass
= cupsdAddClass(resource
+ 9);
1030 else if (pclass
->type
& CUPS_PRINTER_DISCOVERED
)
1033 * Check the default policy, then rename the remote class to "Class"...
1036 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
1038 send_http_error(con
, status
, NULL
);
1042 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 9, pclass
->hostname
);
1043 cupsdRenamePrinter(pclass
, newname
);
1046 * Add the class as a new local class...
1049 pclass
= cupsdAddClass(resource
+ 9);
1052 else if ((status
= cupsdCheckPolicy(pclass
->op_policy_ptr
, con
,
1055 send_http_error(con
, status
, pclass
);
1062 * Look for attributes and copy them over as needed...
1065 need_restart_job
= 0;
1067 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
1068 IPP_TAG_TEXT
)) != NULL
)
1069 cupsdSetString(&pclass
->location
, attr
->values
[0].string
.text
);
1071 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
1072 IPP_TAG_TEXT
)) != NULL
)
1073 cupsdSetString(&pclass
->info
, attr
->values
[0].string
.text
);
1075 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
1076 IPP_TAG_BOOLEAN
)) != NULL
)
1078 cupsdLogMessage(CUPSD_LOG_INFO
,
1079 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
1080 pclass
->name
, attr
->values
[0].boolean
, pclass
->accepting
);
1082 pclass
->accepting
= attr
->values
[0].boolean
;
1083 cupsdAddPrinterHistory(pclass
);
1086 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
1087 IPP_TAG_BOOLEAN
)) != NULL
)
1089 if (pclass
->shared
&& !attr
->values
[0].boolean
)
1090 cupsdDeregisterPrinter(pclass
, 1);
1092 cupsdLogMessage(CUPSD_LOG_INFO
,
1093 "Setting %s printer-is-shared to %d (was %d.)",
1094 pclass
->name
, attr
->values
[0].boolean
, pclass
->shared
);
1096 pclass
->shared
= attr
->values
[0].boolean
;
1099 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
1100 IPP_TAG_ENUM
)) != NULL
)
1102 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
1103 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
1105 send_ipp_status(con
, IPP_BAD_REQUEST
,
1106 _("Attempt to set %s printer-state to bad value %d!"),
1107 pclass
->name
, attr
->values
[0].integer
);
1111 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
1112 pclass
->name
, attr
->values
[0].integer
, pclass
->state
);
1114 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
1115 cupsdStopPrinter(pclass
, 0);
1118 cupsdSetPrinterState(pclass
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
1119 need_restart_job
= 1;
1122 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
1123 IPP_TAG_TEXT
)) != NULL
)
1125 strlcpy(pclass
->state_message
, attr
->values
[0].string
.text
,
1126 sizeof(pclass
->state_message
));
1127 cupsdAddPrinterHistory(pclass
);
1129 if ((attr
= ippFindAttribute(con
->request
, "member-uris",
1130 IPP_TAG_URI
)) != NULL
)
1133 * Clear the printer array as needed...
1136 need_restart_job
= 1;
1138 if (pclass
->num_printers
> 0)
1140 free(pclass
->printers
);
1141 pclass
->num_printers
= 0;
1145 * Add each printer or class that is listed...
1148 for (i
= 0; i
< attr
->num_values
; i
++)
1151 * Search for the printer or class URI...
1154 if (!cupsdValidateDest(attr
->values
[i
].string
.text
, &dtype
, &member
))
1160 send_ipp_status(con
, IPP_NOT_FOUND
,
1161 _("The printer or class was not found."));
1166 * Add it to the class...
1169 cupsdAddPrinterToClass(pclass
, member
);
1173 set_printer_defaults(con
, pclass
);
1175 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
1176 IPP_TAG_KEYWORD
)) != NULL
)
1177 cupsdSetAuthInfoRequired(pclass
, NULL
, attr
);
1180 * Update the printer class attributes and return...
1183 cupsdSetPrinterAttrs(pclass
);
1184 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
1186 if (need_restart_job
&& pclass
->job
)
1191 * Stop the current job and then restart it below...
1194 job
= (cupsd_job_t
*)pclass
->job
;
1196 cupsdStopJob(job
, 1);
1198 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1199 job
->state_value
= IPP_JOB_PENDING
;
1202 if (need_restart_job
)
1205 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
1209 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, pclass
, NULL
,
1210 "Class \"%s\" modified by \"%s\".", pclass
->name
,
1213 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" modified by \"%s\".",
1214 pclass
->name
, get_username(con
));
1218 cupsdAddPrinterHistory(pclass
);
1220 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, pclass
, NULL
,
1221 "New class \"%s\" added by \"%s\".", pclass
->name
,
1224 cupsdLogMessage(CUPSD_LOG_INFO
, "New class \"%s\" added by \"%s\".",
1225 pclass
->name
, get_username(con
));
1228 con
->response
->request
.status
.status_code
= IPP_OK
;
1233 * 'add_file()' - Add a file to a job.
1236 static int /* O - 0 on success, -1 on error */
1237 add_file(cupsd_client_t
*con
, /* I - Connection to client */
1238 cupsd_job_t
*job
, /* I - Job to add to */
1239 mime_type_t
*filetype
, /* I - Type of file */
1240 int compression
) /* I - Compression */
1242 mime_type_t
**filetypes
; /* New filetypes array... */
1243 int *compressions
; /* New compressions array... */
1246 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1247 "add_file(con=%p[%d], job=%d, filetype=%s/%s, "
1248 "compression=%d)", con
, con
? con
->http
.fd
: -1, job
->id
,
1249 filetype
->super
, filetype
->type
, compression
);
1252 * Add the file to the job...
1255 if (job
->num_files
== 0)
1257 compressions
= (int *)malloc(sizeof(int));
1258 filetypes
= (mime_type_t
**)malloc(sizeof(mime_type_t
*));
1262 compressions
= (int *)realloc(job
->compressions
,
1263 (job
->num_files
+ 1) * sizeof(int));
1264 filetypes
= (mime_type_t
**)realloc(job
->filetypes
,
1265 (job
->num_files
+ 1) *
1266 sizeof(mime_type_t
*));
1269 if (!compressions
|| !filetypes
)
1271 cupsdCancelJob(job
, 1, IPP_JOB_ABORTED
);
1274 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1275 _("Unable to allocate memory for file types!"));
1280 job
->compressions
= compressions
;
1281 job
->compressions
[job
->num_files
] = compression
;
1282 job
->filetypes
= filetypes
;
1283 job
->filetypes
[job
->num_files
] = filetype
;
1288 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1295 * 'add_job()' - Add a job to a print queue.
1298 static cupsd_job_t
* /* O - Job object */
1299 add_job(cupsd_client_t
*con
, /* I - Client connection */
1300 cupsd_printer_t
*printer
, /* I - Destination printer */
1301 mime_type_t
*filetype
) /* I - First print file type, if any */
1303 http_status_t status
; /* Policy status */
1304 ipp_attribute_t
*attr
, /* Current attribute */
1305 *auth_info
; /* auth-info attribute */
1306 const char *val
; /* Default option value */
1307 int priority
; /* Job priority */
1308 char *title
; /* Job name/title */
1309 cupsd_job_t
*job
; /* Current job */
1310 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
1311 int kbytes
; /* Size of print file */
1312 int i
; /* Looping var */
1313 int lowerpagerange
; /* Page range bound */
1316 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job(%p[%d], %p(%s), %p(%s/%s))",
1317 con
, con
->http
.fd
, printer
, printer
->name
,
1318 filetype
, filetype
? filetype
->super
: "none",
1319 filetype
? filetype
->type
: "none");
1322 * Check remote printing to non-shared printer...
1325 if (!printer
->shared
&&
1326 strcasecmp(con
->http
.hostname
, "localhost") &&
1327 strcasecmp(con
->http
.hostname
, ServerName
))
1329 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
1330 _("The printer or class is not shared!"));
1338 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
1340 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
1342 send_http_error(con
, status
, printer
);
1345 else if (printer
->num_auth_info_required
> 0 &&
1346 strcmp(printer
->auth_info_required
[0], "none") &&
1347 !con
->username
[0] && !auth_info
)
1349 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
1353 else if (auth_info
&& !con
->http
.tls
&&
1354 !httpAddrLocalhost(con
->http
.hostaddr
))
1357 * Require encryption of auth-info over non-local connections...
1360 send_http_error(con
, HTTP_UPGRADE_REQUIRED
, printer
);
1363 #endif /* HAVE_SSL */
1366 * See if the printer is accepting jobs...
1369 if (!printer
->accepting
)
1371 send_ipp_status(con
, IPP_NOT_ACCEPTING
,
1372 _("Destination \"%s\" is not accepting jobs."),
1378 * Validate job template attributes; for now just document-format,
1379 * copies, number-up, and page-ranges...
1382 if (filetype
&& printer
->filetypes
&&
1383 !cupsArrayFind(printer
->filetypes
, filetype
))
1385 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
1386 /* MIME media type string */
1389 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
1392 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
1393 _("Unsupported format \'%s\'!"), mimetype
);
1395 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
1396 "document-format", NULL
, mimetype
);
1401 if ((attr
= ippFindAttribute(con
->request
, "copies",
1402 IPP_TAG_INTEGER
)) != NULL
)
1404 if (attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> MaxCopies
)
1406 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad copies value %d."),
1407 attr
->values
[0].integer
);
1408 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1409 "copies", attr
->values
[0].integer
);
1414 if ((attr
= ippFindAttribute(con
->request
, "job-sheets",
1415 IPP_TAG_ZERO
)) != NULL
)
1417 if (attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1418 attr
->value_tag
!= IPP_TAG_NAME
)
1420 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value type!"));
1424 if (attr
->num_values
> 2)
1426 send_ipp_status(con
, IPP_BAD_REQUEST
,
1427 _("Too many job-sheets values (%d > 2)!"),
1432 for (i
= 0; i
< attr
->num_values
; i
++)
1433 if (strcmp(attr
->values
[i
].string
.text
, "none") &&
1434 !cupsdFindBanner(attr
->values
[i
].string
.text
))
1436 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-sheets value \"%s\"!"),
1437 attr
->values
[i
].string
.text
);
1442 if ((attr
= ippFindAttribute(con
->request
, "number-up",
1443 IPP_TAG_INTEGER
)) != NULL
)
1445 if (attr
->values
[0].integer
!= 1 &&
1446 attr
->values
[0].integer
!= 2 &&
1447 attr
->values
[0].integer
!= 4 &&
1448 attr
->values
[0].integer
!= 6 &&
1449 attr
->values
[0].integer
!= 9 &&
1450 attr
->values
[0].integer
!= 16)
1452 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Bad number-up value %d."),
1453 attr
->values
[0].integer
);
1454 ippAddInteger(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_INTEGER
,
1455 "number-up", attr
->values
[0].integer
);
1460 if ((attr
= ippFindAttribute(con
->request
, "page-ranges",
1461 IPP_TAG_RANGE
)) != NULL
)
1463 for (i
= 0, lowerpagerange
= 1; i
< attr
->num_values
; i
++)
1465 if (attr
->values
[i
].range
.lower
< lowerpagerange
||
1466 attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
1468 send_ipp_status(con
, IPP_BAD_REQUEST
,
1469 _("Bad page-ranges values %d-%d."),
1470 attr
->values
[i
].range
.lower
,
1471 attr
->values
[i
].range
.upper
);
1475 lowerpagerange
= attr
->values
[i
].range
.upper
+ 1;
1480 * Make sure we aren't over our limit...
1483 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1486 if (MaxJobs
&& cupsArrayCount(Jobs
) >= MaxJobs
)
1488 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
1489 _("Too many active jobs."));
1493 if ((i
= check_quotas(con
, printer
)) < 0)
1495 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Quota limit reached."));
1500 send_ipp_status(con
, IPP_NOT_AUTHORIZED
, _("Not allowed to print."));
1505 * Create the job and set things up...
1508 if ((attr
= ippFindAttribute(con
->request
, "job-priority",
1509 IPP_TAG_INTEGER
)) != NULL
)
1510 priority
= attr
->values
[0].integer
;
1513 if ((val
= cupsGetOption("job-priority", printer
->num_options
,
1514 printer
->options
)) != NULL
)
1515 priority
= atoi(val
);
1519 ippAddInteger(con
->request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-priority",
1523 if ((attr
= ippFindAttribute(con
->request
, "job-name",
1524 IPP_TAG_NAME
)) != NULL
)
1525 title
= attr
->values
[0].string
.text
;
1527 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1528 title
= "Untitled");
1530 if ((job
= cupsdAddJob(priority
, printer
->name
)) == NULL
)
1532 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
1533 _("Unable to add job for destination \"%s\"!"),
1538 job
->dtype
= printer
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
1539 CUPS_PRINTER_REMOTE
);
1540 job
->attrs
= con
->request
;
1542 con
->request
= ippNewRequest(job
->attrs
->request
.op
.operation_id
);
1544 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
1546 add_job_uuid(con
, job
);
1547 apply_printer_defaults(printer
, job
);
1549 attr
= ippFindAttribute(job
->attrs
, "requesting-user-name", IPP_TAG_NAME
);
1551 if (con
->username
[0])
1553 cupsdSetString(&job
->username
, con
->username
);
1556 cupsdSetString(&attr
->values
[0].string
.text
, con
->username
);
1560 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1561 "add_job: requesting-user-name=\"%s\"",
1562 attr
->values
[0].string
.text
);
1564 cupsdSetString(&job
->username
, attr
->values
[0].string
.text
);
1567 cupsdSetString(&job
->username
, "anonymous");
1570 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1571 "job-originating-user-name", NULL
, job
->username
);
1574 attr
->group_tag
= IPP_TAG_JOB
;
1575 _cupsStrFree(attr
->name
);
1576 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1579 if (con
->username
[0] || auth_info
)
1581 save_auth_info(con
, job
, auth_info
);
1584 * Remove the auth-info attribute from the attribute data...
1588 ippDeleteAttribute(job
->attrs
, auth_info
);
1591 if ((attr
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
1592 IPP_TAG_ZERO
)) != NULL
)
1595 * Request contains a job-originating-host-name attribute; validate it...
1598 if (attr
->value_tag
!= IPP_TAG_NAME
||
1599 attr
->num_values
!= 1 ||
1600 strcmp(con
->http
.hostname
, "localhost"))
1603 * Can't override the value if we aren't connected via localhost.
1604 * Also, we can only have 1 value and it must be a name value.
1607 switch (attr
->value_tag
)
1609 case IPP_TAG_STRING
:
1610 case IPP_TAG_TEXTLANG
:
1611 case IPP_TAG_NAMELANG
:
1614 case IPP_TAG_KEYWORD
:
1616 case IPP_TAG_URISCHEME
:
1617 case IPP_TAG_CHARSET
:
1618 case IPP_TAG_LANGUAGE
:
1619 case IPP_TAG_MIMETYPE
:
1621 * Free old strings...
1624 for (i
= 0; i
< attr
->num_values
; i
++)
1626 _cupsStrFree(attr
->values
[i
].string
.text
);
1627 attr
->values
[i
].string
.text
= NULL
;
1628 if (attr
->values
[i
].string
.charset
)
1630 _cupsStrFree(attr
->values
[i
].string
.charset
);
1631 attr
->values
[i
].string
.charset
= NULL
;
1640 * Use the default connection hostname instead...
1643 attr
->value_tag
= IPP_TAG_NAME
;
1644 attr
->num_values
= 1;
1645 attr
->values
[0].string
.text
= _cupsStrAlloc(con
->http
.hostname
);
1648 attr
->group_tag
= IPP_TAG_JOB
;
1653 * No job-originating-host-name attribute, so use the hostname from
1657 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
,
1658 "job-originating-host-name", NULL
, con
->http
.hostname
);
1661 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1663 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1664 "time-at-processing", 0);
1665 attr
->value_tag
= IPP_TAG_NOVALUE
;
1666 attr
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1667 "time-at-completed", 0);
1668 attr
->value_tag
= IPP_TAG_NOVALUE
;
1671 * Add remaining job attributes...
1674 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1675 job
->state
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
1676 "job-state", IPP_JOB_STOPPED
);
1677 job
->state_value
= (ipp_jstate_t
)job
->state
->values
[0].integer
;
1678 job
->sheets
= ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
1679 "job-media-sheets-completed", 0);
1680 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1682 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
,
1685 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
1686 IPP_TAG_INTEGER
)) != NULL
)
1687 attr
->values
[0].integer
= 0;
1689 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-k-octets", 0);
1691 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
1692 IPP_TAG_KEYWORD
)) == NULL
)
1693 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
1696 if ((val
= cupsGetOption("job-hold-until", printer
->num_options
,
1697 printer
->options
)) == NULL
)
1700 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1701 "job-hold-until", NULL
, val
);
1703 if (attr
&& strcmp(attr
->values
[0].string
.text
, "no-hold") &&
1704 !(printer
->type
& CUPS_PRINTER_REMOTE
))
1707 * Hold job until specified time...
1710 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
1712 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1713 job
->state_value
= IPP_JOB_HELD
;
1715 else if (job
->attrs
->request
.op
.operation_id
== IPP_CREATE_JOB
)
1717 job
->hold_until
= time(NULL
) + 60;
1718 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
1719 job
->state_value
= IPP_JOB_HELD
;
1723 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1724 job
->state_value
= IPP_JOB_PENDING
;
1727 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
1731 * Add job sheets options...
1734 if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1735 IPP_TAG_ZERO
)) == NULL
)
1737 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1738 "Adding default job-sheets values \"%s,%s\"...",
1739 printer
->job_sheets
[0], printer
->job_sheets
[1]);
1741 attr
= ippAddStrings(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-sheets",
1743 attr
->values
[0].string
.text
= _cupsStrRetain(printer
->job_sheets
[0]);
1744 attr
->values
[1].string
.text
= _cupsStrRetain(printer
->job_sheets
[1]);
1747 job
->job_sheets
= attr
;
1750 * Enforce classification level if set...
1755 cupsdLogMessage(CUPSD_LOG_INFO
,
1756 "Classification=\"%s\", ClassifyOverride=%d",
1757 Classification
? Classification
: "(null)",
1760 if (ClassifyOverride
)
1762 if (!strcmp(attr
->values
[0].string
.text
, "none") &&
1763 (attr
->num_values
== 1 ||
1764 !strcmp(attr
->values
[1].string
.text
, "none")))
1767 * Force the leading banner to have the classification on it...
1770 cupsdSetString(&attr
->values
[0].string
.text
, Classification
);
1772 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1773 "job-sheets=\"%s,none\", "
1774 "job-originating-user-name=\"%s\"",
1775 Classification
, job
->username
);
1777 else if (attr
->num_values
== 2 &&
1778 strcmp(attr
->values
[0].string
.text
,
1779 attr
->values
[1].string
.text
) &&
1780 strcmp(attr
->values
[0].string
.text
, "none") &&
1781 strcmp(attr
->values
[1].string
.text
, "none"))
1784 * Can't put two different security markings on the same document!
1787 cupsdSetString(&attr
->values
[1].string
.text
, attr
->values
[0].string
.text
);
1789 cupsdLogJob(job
, CUPSD_LOG_NOTICE
, "CLASSIFICATION FORCED "
1790 "job-sheets=\"%s,%s\", "
1791 "job-originating-user-name=\"%s\"",
1792 attr
->values
[0].string
.text
,
1793 attr
->values
[1].string
.text
, job
->username
);
1795 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1796 strcmp(attr
->values
[0].string
.text
, "none") &&
1797 (attr
->num_values
== 1 ||
1798 (strcmp(attr
->values
[1].string
.text
, Classification
) &&
1799 strcmp(attr
->values
[1].string
.text
, "none"))))
1801 if (attr
->num_values
== 1)
1802 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1803 "CLASSIFICATION OVERRIDDEN "
1804 "job-sheets=\"%s\", "
1805 "job-originating-user-name=\"%s\"",
1806 attr
->values
[0].string
.text
, job
->username
);
1808 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1809 "CLASSIFICATION OVERRIDDEN "
1810 "job-sheets=\"%s,%s\",fffff "
1811 "job-originating-user-name=\"%s\"",
1812 attr
->values
[0].string
.text
,
1813 attr
->values
[1].string
.text
, job
->username
);
1816 else if (strcmp(attr
->values
[0].string
.text
, Classification
) &&
1817 (attr
->num_values
== 1 ||
1818 strcmp(attr
->values
[1].string
.text
, Classification
)))
1821 * Force the banner to have the classification on it...
1824 if (attr
->num_values
> 1 &&
1825 !strcmp(attr
->values
[0].string
.text
, attr
->values
[1].string
.text
))
1827 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1828 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1832 if (attr
->num_values
== 1 ||
1833 strcmp(attr
->values
[0].string
.text
, "none"))
1834 cupsdSetString(&(attr
->values
[0].string
.text
), Classification
);
1836 if (attr
->num_values
> 1 &&
1837 strcmp(attr
->values
[1].string
.text
, "none"))
1838 cupsdSetString(&(attr
->values
[1].string
.text
), Classification
);
1841 if (attr
->num_values
> 1)
1842 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1843 "CLASSIFICATION FORCED "
1844 "job-sheets=\"%s,%s\", "
1845 "job-originating-user-name=\"%s\"",
1846 attr
->values
[0].string
.text
,
1847 attr
->values
[1].string
.text
, job
->username
);
1849 cupsdLogJob(job
, CUPSD_LOG_NOTICE
,
1850 "CLASSIFICATION FORCED "
1851 "job-sheets=\"%s\", "
1852 "job-originating-user-name=\"%s\"",
1853 Classification
, job
->username
);
1858 * See if we need to add the starting sheet...
1861 if (!(printer
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1863 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Adding start banner page \"%s\".",
1864 attr
->values
[0].string
.text
);
1866 if ((kbytes
= copy_banner(con
, job
, attr
->values
[0].string
.text
)) < 0)
1868 cupsdDeleteJob(job
);
1872 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
1875 else if ((attr
= ippFindAttribute(job
->attrs
, "job-sheets",
1876 IPP_TAG_ZERO
)) != NULL
)
1880 * Fill in the response info...
1883 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
1884 LocalPort
, job
->id
);
1886 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
1889 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1891 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
1893 add_job_state_reasons(con
, job
);
1895 con
->response
->request
.status
.status_code
= IPP_OK
;
1898 * Add any job subscriptions...
1901 add_job_subscriptions(con
, job
);
1904 * Set all but the first two attributes to the job attributes group...
1907 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
1908 attr
->group_tag
= IPP_TAG_JOB
;
1911 * Fire the "job created" event...
1914 cupsdAddEvent(CUPSD_EVENT_JOB_CREATED
, printer
, job
, "Job created.");
1917 * Return the new job...
1925 * 'add_job_state_reasons()' - Add the "job-state-reasons" attribute based
1926 * upon the job and printer state...
1930 add_job_state_reasons(
1931 cupsd_client_t
*con
, /* I - Client connection */
1932 cupsd_job_t
*job
) /* I - Job info */
1934 cupsd_printer_t
*dest
; /* Destination printer */
1937 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_job_state_reasons(%p[%d], %d)",
1938 con
, con
->http
.fd
, job
? job
->id
: 0);
1940 switch (job
? job
->state_value
: IPP_JOB_CANCELED
)
1942 case IPP_JOB_PENDING
:
1943 dest
= cupsdFindDest(job
->dest
);
1945 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
1946 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1947 "job-state-reasons", NULL
, "printer-stopped");
1949 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1950 "job-state-reasons", NULL
, "none");
1954 if (ippFindAttribute(job
->attrs
, "job-hold-until",
1955 IPP_TAG_KEYWORD
) != NULL
||
1956 ippFindAttribute(job
->attrs
, "job-hold-until",
1957 IPP_TAG_NAME
) != NULL
)
1958 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1959 "job-state-reasons", NULL
, "job-hold-until-specified");
1961 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1962 "job-state-reasons", NULL
, "job-incoming");
1965 case IPP_JOB_PROCESSING
:
1966 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1967 "job-state-reasons", NULL
, "job-printing");
1970 case IPP_JOB_STOPPED
:
1971 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1972 "job-state-reasons", NULL
, "job-stopped");
1975 case IPP_JOB_CANCELED
:
1976 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1977 "job-state-reasons", NULL
, "job-canceled-by-user");
1980 case IPP_JOB_ABORTED
:
1981 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1982 "job-state-reasons", NULL
, "aborted-by-system");
1985 case IPP_JOB_COMPLETED
:
1986 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
1987 "job-state-reasons", NULL
, "job-completed-successfully");
1994 * 'add_job_subscriptions()' - Add any subscriptions for a job.
1998 add_job_subscriptions(
1999 cupsd_client_t
*con
, /* I - Client connection */
2000 cupsd_job_t
*job
) /* I - Newly created job */
2002 int i
; /* Looping var */
2003 ipp_attribute_t
*prev
, /* Previous attribute */
2004 *next
, /* Next attribute */
2005 *attr
; /* Current attribute */
2006 cupsd_subscription_t
*sub
; /* Subscription object */
2007 const char *recipient
, /* notify-recipient-uri */
2008 *pullmethod
; /* notify-pull-method */
2009 ipp_attribute_t
*user_data
; /* notify-user-data */
2010 int interval
; /* notify-time-interval */
2011 unsigned mask
; /* notify-events */
2015 * Find the first subscription group attribute; return if we have
2019 for (attr
= job
->attrs
->attrs
; attr
; attr
= attr
->next
)
2020 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
2027 * Process the subscription attributes in the request...
2036 mask
= CUPSD_EVENT_NONE
;
2038 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
2040 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
2041 attr
->value_tag
== IPP_TAG_URI
)
2044 * Validate the recipient scheme against the ServerBin/notifier
2048 char notifier
[1024], /* Notifier filename */
2049 scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
2050 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
2051 host
[HTTP_MAX_URI
], /* Host portion of URI */
2052 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2053 int port
; /* Port portion of URI */
2056 recipient
= attr
->values
[0].string
.text
;
2058 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
2059 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
2060 host
, sizeof(host
), &port
,
2061 resource
, sizeof(resource
)) < HTTP_URI_OK
)
2063 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2064 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
2065 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2066 "notify-status-code", IPP_URI_SCHEME
);
2070 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
2072 if (access(notifier
, X_OK
))
2074 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2075 _("notify-recipient-uri URI \"%s\" uses unknown "
2076 "scheme!"), recipient
);
2077 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2078 "notify-status-code", IPP_URI_SCHEME
);
2082 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
2084 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2085 _("notify-recipient-uri URI \"%s\" is already used!"),
2087 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2088 "notify-status-code", IPP_ATTRIBUTES
);
2092 else if (!strcmp(attr
->name
, "notify-pull-method") &&
2093 attr
->value_tag
== IPP_TAG_KEYWORD
)
2095 pullmethod
= attr
->values
[0].string
.text
;
2097 if (strcmp(pullmethod
, "ippget"))
2099 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2100 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
2101 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
2102 "notify-status-code", IPP_ATTRIBUTES
);
2106 else if (!strcmp(attr
->name
, "notify-charset") &&
2107 attr
->value_tag
== IPP_TAG_CHARSET
&&
2108 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
2109 strcmp(attr
->values
[0].string
.text
, "utf-8"))
2111 send_ipp_status(con
, IPP_CHARSET
,
2112 _("Character set \"%s\" not supported!"),
2113 attr
->values
[0].string
.text
);
2116 else if (!strcmp(attr
->name
, "notify-natural-language") &&
2117 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
2118 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
2120 send_ipp_status(con
, IPP_CHARSET
,
2121 _("Language \"%s\" not supported!"),
2122 attr
->values
[0].string
.text
);
2125 else if (!strcmp(attr
->name
, "notify-user-data") &&
2126 attr
->value_tag
== IPP_TAG_STRING
)
2128 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
2130 send_ipp_status(con
, IPP_REQUEST_VALUE
,
2131 _("The notify-user-data value is too large "
2132 "(%d > 63 octets)!"),
2133 attr
->values
[0].unknown
.length
);
2139 else if (!strcmp(attr
->name
, "notify-events") &&
2140 attr
->value_tag
== IPP_TAG_KEYWORD
)
2142 for (i
= 0; i
< attr
->num_values
; i
++)
2143 mask
|= cupsdEventValue(attr
->values
[i
].string
.text
);
2145 else if (!strcmp(attr
->name
, "notify-lease-duration"))
2147 send_ipp_status(con
, IPP_BAD_REQUEST
,
2148 _("The notify-lease-duration attribute cannot be "
2149 "used with job subscriptions."));
2152 else if (!strcmp(attr
->name
, "notify-time-interval") &&
2153 attr
->value_tag
== IPP_TAG_INTEGER
)
2154 interval
= attr
->values
[0].integer
;
2159 if (!recipient
&& !pullmethod
)
2162 if (mask
== CUPSD_EVENT_NONE
)
2163 mask
= CUPSD_EVENT_JOB_COMPLETED
;
2165 if ((sub
= cupsdAddSubscription(mask
, cupsdFindDest(job
->dest
), job
,
2166 recipient
, 0)) != NULL
)
2168 sub
->interval
= interval
;
2170 cupsdSetString(&sub
->owner
, job
->username
);
2174 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
2175 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
2176 sub
->user_data_len
);
2179 ippAddSeparator(con
->response
);
2180 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
2181 "notify-subscription-id", sub
->id
);
2188 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
2191 * Remove all of the subscription attributes from the job request...
2193 * TODO: Optimize this since subscription groups have to come at the
2194 * end of the request...
2197 for (attr
= job
->attrs
->attrs
, prev
= NULL
; attr
; attr
= next
)
2201 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
||
2202 attr
->group_tag
== IPP_TAG_ZERO
)
2205 * Free and remove this attribute...
2213 job
->attrs
->attrs
= next
;
2219 job
->attrs
->last
= prev
;
2220 job
->attrs
->current
= prev
;
2225 * 'add_job_uuid()' - Add job-uuid attribute to a job.
2227 * See RFC 4122 for the definition of UUIDs and the format.
2231 add_job_uuid(cupsd_client_t
*con
, /* I - Client connection */
2232 cupsd_job_t
*job
) /* I - Job */
2234 char uuid
[1024]; /* job-uuid string */
2235 _cups_md5_state_t md5state
; /* MD5 state */
2236 unsigned char md5sum
[16]; /* MD5 digest/sum */
2240 * First see if the job already has a job-uuid attribute; if so, return...
2243 if (ippFindAttribute(job
->attrs
, "job-uuid", IPP_TAG_URI
))
2247 * No job-uuid attribute, so build a version 3 UUID with the local job
2248 * ID at the end; see RFC 4122 for details. Start with the MD5 sum of
2249 * the ServerName, server name and port that the client connected to,
2250 * and local job ID...
2253 snprintf(uuid
, sizeof(uuid
), "%s:%s:%d:%d", ServerName
, con
->servername
,
2254 con
->serverport
, job
->id
);
2256 _cupsMD5Init(&md5state
);
2257 _cupsMD5Append(&md5state
, (unsigned char *)uuid
, strlen(uuid
));
2258 _cupsMD5Finish(&md5state
, md5sum
);
2261 * Format the UUID URI using the MD5 sum and job ID.
2264 snprintf(uuid
, sizeof(uuid
),
2265 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2266 "%02x%02x%02x%02x%02x%02x",
2267 md5sum
[0], md5sum
[1], md5sum
[2], md5sum
[3], md5sum
[4], md5sum
[5],
2268 (md5sum
[6] & 15) | 0x30, md5sum
[7], (md5sum
[8] & 0x3f) | 0x40,
2269 md5sum
[9], md5sum
[10], md5sum
[11], md5sum
[12], md5sum
[13],
2270 md5sum
[14], md5sum
[15]);
2272 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
2277 * 'add_printer()' - Add a printer to the system.
2281 add_printer(cupsd_client_t
*con
, /* I - Client connection */
2282 ipp_attribute_t
*uri
) /* I - URI of printer */
2284 http_status_t status
; /* Policy status */
2285 int i
; /* Looping var */
2286 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
2287 username
[HTTP_MAX_URI
], /* Username portion of URI */
2288 host
[HTTP_MAX_URI
], /* Host portion of URI */
2289 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2290 int port
; /* Port portion of URI */
2291 cupsd_printer_t
*printer
; /* Printer/class */
2292 ipp_attribute_t
*attr
; /* Printer attribute */
2293 cups_file_t
*fp
; /* Script/PPD file */
2294 char line
[1024]; /* Line from file... */
2295 char srcfile
[1024], /* Source Script/PPD file */
2296 dstfile
[1024]; /* Destination Script/PPD file */
2297 int modify
; /* Non-zero if we are modifying */
2298 char newname
[IPP_MAX_NAME
]; /* New printer name */
2299 int need_restart_job
; /* Need to restart job? */
2300 int set_device_uri
, /* Did we set the device URI? */
2301 set_port_monitor
; /* Did we set the port monitor? */
2304 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer(%p[%d], %s)", con
,
2305 con
->http
.fd
, uri
->values
[0].string
.text
);
2308 * Do we have a valid URI?
2311 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
2312 sizeof(scheme
), username
, sizeof(username
), host
,
2313 sizeof(host
), &port
, resource
, sizeof(resource
));
2315 if (strncmp(resource
, "/printers/", 10) || strlen(resource
) == 10)
2318 * No, return an error...
2321 send_ipp_status(con
, IPP_BAD_REQUEST
,
2322 _("The printer-uri must be of the form "
2323 "\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
2328 * Do we have a valid printer name?
2331 if (!validate_name(resource
+ 10))
2334 * No, return an error...
2337 send_ipp_status(con
, IPP_BAD_REQUEST
,
2338 _("The printer-uri \"%s\" contains invalid characters."),
2339 uri
->values
[0].string
.text
);
2344 * See if the printer already exists; if not, create a new printer...
2347 if ((printer
= cupsdFindPrinter(resource
+ 10)) == NULL
)
2350 * Printer doesn't exist; see if we have a class of the same name...
2353 if ((printer
= cupsdFindClass(resource
+ 10)) != NULL
&&
2354 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
2357 * Yes, return an error...
2360 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2361 _("A class named \"%s\" already exists!"),
2367 * No, check the default policy then add the printer...
2370 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2372 send_http_error(con
, status
, NULL
);
2376 printer
= cupsdAddPrinter(resource
+ 10);
2379 else if (printer
->type
& CUPS_PRINTER_IMPLICIT
)
2382 * Check the default policy, then rename the implicit printer to
2383 * "AnyPrinter" or delete it...
2386 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2388 send_http_error(con
, status
, NULL
);
2392 if (ImplicitAnyClasses
)
2394 snprintf(newname
, sizeof(newname
), "Any%s", resource
+ 10);
2395 cupsdRenamePrinter(printer
, newname
);
2398 cupsdDeletePrinter(printer
, 1);
2401 * Add the printer as a new local printer...
2404 printer
= cupsdAddPrinter(resource
+ 10);
2407 else if (printer
->type
& CUPS_PRINTER_DISCOVERED
)
2410 * Check the default policy, then rename the remote printer to
2411 * "Printer@server"...
2414 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
2416 send_http_error(con
, status
, NULL
);
2420 snprintf(newname
, sizeof(newname
), "%s@%s", resource
+ 10,
2422 cupsdRenamePrinter(printer
, newname
);
2425 * Add the printer as a new local printer...
2428 printer
= cupsdAddPrinter(resource
+ 10);
2431 else if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
2434 send_http_error(con
, status
, printer
);
2441 * Look for attributes and copy them over as needed...
2444 need_restart_job
= 0;
2446 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
2447 IPP_TAG_TEXT
)) != NULL
)
2448 cupsdSetString(&printer
->location
, attr
->values
[0].string
.text
);
2450 if ((attr
= ippFindAttribute(con
->request
, "printer-info",
2451 IPP_TAG_TEXT
)) != NULL
)
2452 cupsdSetString(&printer
->info
, attr
->values
[0].string
.text
);
2456 if ((attr
= ippFindAttribute(con
->request
, "device-uri",
2457 IPP_TAG_URI
)) != NULL
)
2460 * Do we have a valid device URI?
2463 http_uri_status_t uri_status
; /* URI separation status */
2464 char old_device_uri
[1024];
2465 /* Old device URI */
2468 need_restart_job
= 1;
2470 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
2471 attr
->values
[0].string
.text
,
2472 scheme
, sizeof(scheme
),
2473 username
, sizeof(username
),
2474 host
, sizeof(host
), &port
,
2475 resource
, sizeof(resource
));
2477 if (uri_status
< HTTP_URI_OK
)
2479 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri \"%s\"!"),
2480 attr
->values
[0].string
.text
);
2481 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2482 "add_printer: httpSeparateURI returned %d", uri_status
);
2486 if (!strcmp(scheme
, "file"))
2489 * See if the administrator has enabled file devices...
2492 if (!FileDevice
&& strcmp(resource
, "/dev/null"))
2495 * File devices are disabled and the URL is not file:/dev/null...
2498 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2499 _("File device URIs have been disabled! "
2500 "To enable, see the FileDevice directive in "
2501 "\"%s/cupsd.conf\"."),
2509 * See if the backend exists and is executable...
2512 snprintf(srcfile
, sizeof(srcfile
), "%s/backend/%s", ServerBin
, scheme
);
2513 if (access(srcfile
, X_OK
))
2516 * Could not find device in list!
2519 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad device-uri scheme \"%s\"!"),
2525 if (printer
->sanitized_device_uri
)
2526 strlcpy(old_device_uri
, printer
->sanitized_device_uri
,
2527 sizeof(old_device_uri
));
2529 old_device_uri
[0] = '\0';
2531 cupsdSetDeviceURI(printer
, attr
->values
[0].string
.text
);
2533 cupsdLogMessage(CUPSD_LOG_INFO
,
2534 "Setting %s device-uri to \"%s\" (was \"%s\".)",
2535 printer
->name
, printer
->sanitized_device_uri
,
2541 set_port_monitor
= 0;
2543 if ((attr
= ippFindAttribute(con
->request
, "port-monitor",
2544 IPP_TAG_NAME
)) != NULL
)
2546 ipp_attribute_t
*supported
; /* port-monitor-supported attribute */
2549 need_restart_job
= 1;
2551 supported
= ippFindAttribute(printer
->ppd_attrs
, "port-monitor-supported",
2555 for (i
= 0; i
< supported
->num_values
; i
++)
2556 if (!strcmp(supported
->values
[i
].string
.text
,
2557 attr
->values
[0].string
.text
))
2561 if (!supported
|| i
>= supported
->num_values
)
2563 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Bad port-monitor \"%s\"!"),
2564 attr
->values
[0].string
.text
);
2568 cupsdLogMessage(CUPSD_LOG_INFO
,
2569 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2570 printer
->name
, attr
->values
[0].string
.text
,
2571 printer
->port_monitor
? printer
->port_monitor
: "none");
2573 if (strcmp(attr
->values
[0].string
.text
, "none"))
2574 cupsdSetString(&printer
->port_monitor
, attr
->values
[0].string
.text
);
2576 cupsdClearString(&printer
->port_monitor
);
2578 set_port_monitor
= 1;
2581 if ((attr
= ippFindAttribute(con
->request
, "printer-is-accepting-jobs",
2582 IPP_TAG_BOOLEAN
)) != NULL
)
2584 cupsdLogMessage(CUPSD_LOG_INFO
,
2585 "Setting %s printer-is-accepting-jobs to %d (was %d.)",
2586 printer
->name
, attr
->values
[0].boolean
, printer
->accepting
);
2588 printer
->accepting
= attr
->values
[0].boolean
;
2589 cupsdAddPrinterHistory(printer
);
2592 if ((attr
= ippFindAttribute(con
->request
, "printer-is-shared",
2593 IPP_TAG_BOOLEAN
)) != NULL
)
2595 if (printer
->shared
&& !attr
->values
[0].boolean
)
2596 cupsdDeregisterPrinter(printer
, 1);
2598 cupsdLogMessage(CUPSD_LOG_INFO
,
2599 "Setting %s printer-is-shared to %d (was %d.)",
2600 printer
->name
, attr
->values
[0].boolean
, printer
->shared
);
2602 printer
->shared
= attr
->values
[0].boolean
;
2605 if ((attr
= ippFindAttribute(con
->request
, "printer-state",
2606 IPP_TAG_ENUM
)) != NULL
)
2608 if (attr
->values
[0].integer
!= IPP_PRINTER_IDLE
&&
2609 attr
->values
[0].integer
!= IPP_PRINTER_STOPPED
)
2611 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad printer-state value %d!"),
2612 attr
->values
[0].integer
);
2616 cupsdLogMessage(CUPSD_LOG_INFO
, "Setting %s printer-state to %d (was %d.)",
2617 printer
->name
, attr
->values
[0].integer
, printer
->state
);
2619 if (attr
->values
[0].integer
== IPP_PRINTER_STOPPED
)
2620 cupsdStopPrinter(printer
, 0);
2623 need_restart_job
= 1;
2624 cupsdSetPrinterState(printer
, (ipp_pstate_t
)(attr
->values
[0].integer
), 0);
2627 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
2628 IPP_TAG_TEXT
)) != NULL
)
2630 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
2631 sizeof(printer
->state_message
));
2632 cupsdAddPrinterHistory(printer
);
2635 if ((attr
= ippFindAttribute(con
->request
, "printer-state-reasons",
2636 IPP_TAG_KEYWORD
)) != NULL
)
2638 if (attr
->num_values
>
2639 (int)(sizeof(printer
->reasons
) / sizeof(printer
->reasons
[0])))
2641 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
2642 _("Too many printer-state-reasons values (%d > %d)!"),
2644 (int)(sizeof(printer
->reasons
) /
2645 sizeof(printer
->reasons
[0])));
2649 for (i
= 0; i
< printer
->num_reasons
; i
++)
2650 _cupsStrFree(printer
->reasons
[i
]);
2652 printer
->num_reasons
= 0;
2653 for (i
= 0; i
< attr
->num_values
; i
++)
2655 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
2658 printer
->reasons
[printer
->num_reasons
] =
2659 _cupsStrRetain(attr
->values
[i
].string
.text
);
2661 if (!strcmp(printer
->reasons
[printer
->num_reasons
], "paused") &&
2662 printer
->state
!= IPP_PRINTER_STOPPED
)
2664 cupsdLogMessage(CUPSD_LOG_INFO
,
2665 "Setting %s printer-state to %d (was %d.)",
2666 printer
->name
, IPP_PRINTER_STOPPED
, printer
->state
);
2667 cupsdStopPrinter(printer
, 0);
2670 printer
->num_reasons
++;
2673 if (PrintcapFormat
== PRINTCAP_PLIST
)
2674 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2677 set_printer_defaults(con
, printer
);
2679 if ((attr
= ippFindAttribute(con
->request
, "auth-info-required",
2680 IPP_TAG_KEYWORD
)) != NULL
)
2681 cupsdSetAuthInfoRequired(printer
, NULL
, attr
);
2684 * See if we have all required attributes...
2687 if (!printer
->device_uri
)
2688 cupsdSetString(&printer
->device_uri
, "file:///dev/null");
2691 * See if we have an interface script or PPD file attached to the request...
2696 need_restart_job
= 1;
2698 strlcpy(srcfile
, con
->filename
, sizeof(srcfile
));
2700 if ((fp
= cupsFileOpen(srcfile
, "rb")))
2703 * Yes; get the first line from it...
2707 cupsFileGets(fp
, line
, sizeof(line
));
2711 * Then see what kind of file it is...
2714 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2717 if (!strncmp(line
, "*PPD-Adobe", 10))
2720 * The new file is a PPD file, so remove any old interface script
2721 * that might be lying around...
2729 * This must be an interface script, so move the file over to the
2730 * interfaces directory and make it executable...
2733 if (copy_file(srcfile
, dstfile
))
2735 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2736 _("Unable to copy interface script - %s!"),
2741 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2742 "Copied interface script successfully!");
2743 chmod(dstfile
, 0755);
2746 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2749 if (!strncmp(line
, "*PPD-Adobe", 10))
2752 * The new file is a PPD file, so move the file over to the
2753 * ppd directory and make it readable by all...
2756 if (copy_file(srcfile
, dstfile
))
2758 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
2759 _("Unable to copy PPD file - %s!"),
2764 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2765 "Copied PPD file successfully!");
2766 chmod(dstfile
, 0644);
2770 * (Re)register color profiles...
2775 apple_unregister_profiles(printer
);
2776 apple_register_profiles(printer
);
2778 #endif /* __APPLE__ */
2783 * This must be an interface script, so remove any old PPD file that
2784 * may be lying around...
2791 else if ((attr
= ippFindAttribute(con
->request
, "ppd-name",
2792 IPP_TAG_NAME
)) != NULL
)
2794 need_restart_job
= 1;
2796 if (!strcmp(attr
->values
[0].string
.text
, "raw"))
2799 * Raw driver, remove any existing PPD or interface script files.
2802 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2806 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2816 snprintf(dstfile
, sizeof(dstfile
), "%s/interfaces/%s", ServerRoot
,
2820 snprintf(dstfile
, sizeof(dstfile
), "%s/ppd/%s.ppd", ServerRoot
,
2823 if (copy_model(con
, attr
->values
[0].string
.text
, dstfile
))
2825 send_ipp_status(con
, IPP_INTERNAL_ERROR
, _("Unable to copy PPD file!"));
2829 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2830 "Copied PPD file successfully!");
2831 chmod(dstfile
, 0644);
2835 * (Re)register color profiles...
2840 apple_unregister_profiles(printer
);
2841 apple_register_profiles(printer
);
2843 #endif /* __APPLE__ */
2848 * If we set the device URI but not the port monitor, check which port
2849 * monitor to use by default...
2852 if (set_device_uri
&& !set_port_monitor
)
2854 ppd_file_t
*ppd
; /* PPD file */
2855 ppd_attr_t
*ppdattr
; /* cupsPortMonitor attribute */
2858 httpSeparateURI(HTTP_URI_CODING_ALL
, printer
->device_uri
, scheme
,
2859 sizeof(scheme
), username
, sizeof(username
), host
,
2860 sizeof(host
), &port
, resource
, sizeof(resource
));
2862 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
,
2864 if ((ppd
= ppdOpenFile(srcfile
)) != NULL
)
2866 for (ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
2868 ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
2869 if (!strcmp(scheme
, ppdattr
->spec
))
2871 cupsdLogMessage(CUPSD_LOG_INFO
,
2872 "Setting %s port-monitor to \"%s\" (was \"%s\".)",
2873 printer
->name
, ppdattr
->value
,
2874 printer
->port_monitor
? printer
->port_monitor
2877 if (strcmp(ppdattr
->value
, "none"))
2878 cupsdSetString(&printer
->port_monitor
, ppdattr
->value
);
2880 cupsdClearString(&printer
->port_monitor
);
2890 * Update the printer attributes and return...
2893 cupsdSetPrinterAttrs(printer
);
2894 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2896 if (need_restart_job
&& printer
->job
)
2901 * Stop the current job and then restart it below...
2904 job
= (cupsd_job_t
*)printer
->job
;
2906 cupsdStopJob(job
, 1);
2908 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2909 job
->state_value
= IPP_JOB_PENDING
;
2912 if (need_restart_job
)
2915 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2919 cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED
, printer
, NULL
,
2920 "Printer \"%s\" modified by \"%s\".", printer
->name
,
2923 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" modified by \"%s\".",
2924 printer
->name
, get_username(con
));
2928 cupsdAddPrinterHistory(printer
);
2930 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, printer
, NULL
,
2931 "New printer \"%s\" added by \"%s\".", printer
->name
,
2934 cupsdLogMessage(CUPSD_LOG_INFO
, "New printer \"%s\" added by \"%s\".",
2935 printer
->name
, get_username(con
));
2938 con
->response
->request
.status
.status_code
= IPP_OK
;
2943 * 'add_printer_state_reasons()' - Add the "printer-state-reasons" attribute
2944 * based upon the printer state...
2948 add_printer_state_reasons(
2949 cupsd_client_t
*con
, /* I - Client connection */
2950 cupsd_printer_t
*p
) /* I - Printer info */
2952 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2953 "add_printer_state_reasons(%p[%d], %p[%s])",
2954 con
, con
->http
.fd
, p
, p
->name
);
2956 if (p
->num_reasons
== 0)
2957 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2958 "printer-state-reasons", NULL
,
2959 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
2961 ippAddStrings(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2962 "printer-state-reasons", p
->num_reasons
, NULL
,
2963 (const char * const *)p
->reasons
);
2968 * 'add_queued_job_count()' - Add the "queued-job-count" attribute for
2969 * the specified printer or class.
2973 add_queued_job_count(
2974 cupsd_client_t
*con
, /* I - Client connection */
2975 cupsd_printer_t
*p
) /* I - Printer or class */
2977 int count
; /* Number of jobs on destination */
2980 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_queued_job_count(%p[%d], %p[%s])",
2981 con
, con
->http
.fd
, p
, p
->name
);
2983 count
= cupsdGetPrinterJobCount(p
->name
);
2985 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2986 "queued-job-count", count
);
2992 * 'apple_init_profile()' - Initialize a color profile.
2997 ppd_file_t
*ppd
, /* I - PPD file */
2998 cups_array_t
*languages
, /* I - Languages in the PPD file */
2999 CMDeviceProfileInfo
*profile
, /* I - Profile record */
3000 unsigned id
, /* I - Profile ID */
3001 const char *name
, /* I - Profile name */
3002 const char *text
, /* I - Profile UI text */
3003 const char *iccfile
) /* I - ICC filename */
3005 char url
[1024]; /* URL for profile filename */
3006 CFMutableDictionaryRef dict
; /* Dictionary for name */
3007 char *language
; /* Current language */
3008 ppd_attr_t
*attr
; /* Profile attribute */
3009 CFStringRef cflang
, /* Language string */
3010 cftext
; /* Localized text */
3014 * Build the profile name dictionary...
3017 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3018 &kCFTypeDictionaryKeyCallBacks
,
3019 &kCFTypeDictionaryValueCallBacks
);
3021 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, text
,
3022 kCFStringEncodingUTF8
);
3026 CFDictionarySetValue(dict
, CFSTR("en"), cftext
);
3033 * Find localized names for the color profiles...
3036 cupsArraySave(ppd
->sorted_attrs
);
3038 for (language
= (char *)cupsArrayFirst(languages
);
3040 language
= (char *)cupsArrayNext(languages
))
3044 if ((attr
= _ppdLocalizedAttr(ppd
, "cupsICCProfile", name
,
3046 attr
= _ppdLocalizedAttr(ppd
, "APTiogaProfile", name
, language
);
3049 attr
= _ppdLocalizedAttr(ppd
, "ColorModel", name
, language
);
3051 if (attr
&& attr
->text
[0])
3053 cflang
= CFStringCreateWithCString(kCFAllocatorDefault
, language
,
3054 kCFStringEncodingUTF8
);
3055 cftext
= CFStringCreateWithCString(kCFAllocatorDefault
, attr
->text
,
3056 kCFStringEncodingUTF8
);
3058 if (cflang
&& cftext
)
3059 CFDictionarySetValue(dict
, cflang
, cftext
);
3069 cupsArrayRestore(ppd
->sorted_attrs
);
3073 * Fill in the profile data...
3077 httpAssembleURI(HTTP_URI_CODING_ALL
, url
, sizeof(url
), "file", NULL
, "", 0,
3080 profile
->dataVersion
= cmDeviceProfileInfoVersion1
;
3081 profile
->profileID
= id
;
3082 profile
->profileLoc
.locType
= iccfile
? cmPathBasedProfile
: cmNoProfileBase
;
3083 profile
->profileName
= dict
;
3086 strlcpy(profile
->profileLoc
.u
.pathLoc
.path
, iccfile
,
3087 sizeof(profile
->profileLoc
.u
.pathLoc
.path
));
3092 * 'apple_register_profiles()' - Register color profiles for a printer.
3096 apple_register_profiles(
3097 cupsd_printer_t
*p
) /* I - Printer */
3099 int i
; /* Looping var */
3100 char ppdfile
[1024], /* PPD filename */
3101 iccfile
[1024], /* ICC filename */
3102 selector
[PPD_MAX_NAME
];
3103 /* Profile selection string */
3104 ppd_file_t
*ppd
; /* PPD file */
3105 ppd_attr_t
*attr
, /* Profile attributes */
3106 *profileid_attr
,/* cupsProfileID attribute */
3107 *q1_attr
, /* ColorModel (or other) qualifier */
3108 *q2_attr
, /* MediaType (or other) qualifier */
3109 *q3_attr
; /* Resolution (or other) qualifier */
3110 char q_keyword
[PPD_MAX_NAME
];
3111 /* Qualifier keyword */
3112 const char *q1_choice
, /* ColorModel (or other) choice */
3113 *q2_choice
, /* MediaType (or other) choice */
3114 *q3_choice
; /* Resolution (or other) choice */
3115 const char *profile_key
; /* Profile keyword */
3116 ppd_option_t
*cm_option
; /* Color model option */
3117 ppd_choice_t
*cm_choice
; /* Color model choice */
3118 int num_profiles
; /* Number of profiles */
3119 CMError error
; /* Last error */
3120 unsigned device_id
, /* Printer device ID */
3121 profile_id
, /* Profile ID */
3122 default_profile_id
= 0;
3123 /* Default profile ID */
3124 CFMutableDictionaryRef device_name
; /* Printer device name dictionary */
3125 CFStringRef printer_name
; /* Printer name string */
3126 CMDeviceScope scope
= /* Scope of the registration */
3128 kCFPreferencesAnyUser
,
3129 kCFPreferencesCurrentHost
3131 CMDeviceProfileArrayPtr profiles
; /* Profiles */
3132 CMDeviceProfileInfo
*profile
; /* Current profile */
3133 cups_array_t
*languages
; /* Languages array */
3137 * Make sure ColorSync is available...
3140 if (CMRegisterColorDevice
== NULL
)
3144 * Try opening the PPD file for this printer...
3147 snprintf(ppdfile
, sizeof(ppdfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3148 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
3152 * See if we have any profiles...
3155 if ((attr
= ppdFindAttr(ppd
, "APTiogaProfile", NULL
)) != NULL
)
3156 profile_key
= "APTiogaProfile";
3159 attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
3160 profile_key
= "cupsICCProfile";
3163 for (num_profiles
= 0; attr
; attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3164 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3166 if (attr
->value
[0] != '/')
3167 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3170 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3172 if (access(iccfile
, 0))
3180 * If we have profiles, add them...
3183 if (num_profiles
> 0)
3185 if (profile_key
[0] == 'A')
3188 * For Tioga PPDs, get the default profile using the DefaultAPTiogaProfile
3192 if ((attr
= ppdFindAttr(ppd
, "DefaultAPTiogaProfile", NULL
)) != NULL
&&
3194 default_profile_id
= atoi(attr
->value
);
3196 q1_choice
= q2_choice
= q3_choice
= NULL
;
3201 * For CUPS PPDs, figure out the default profile selector values...
3204 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier1", NULL
)) != NULL
&&
3205 attr
->value
&& attr
->value
[0])
3207 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3208 q1_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3210 else if ((q1_attr
= ppdFindAttr(ppd
, "DefaultColorModel", NULL
)) == NULL
)
3211 q1_attr
= ppdFindAttr(ppd
, "DefaultColorSpace", NULL
);
3213 if (q1_attr
&& q1_attr
->value
&& q1_attr
->value
[0])
3214 q1_choice
= q1_attr
->value
;
3218 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier2", NULL
)) != NULL
&&
3219 attr
->value
&& attr
->value
[0])
3221 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3222 q2_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3225 q2_attr
= ppdFindAttr(ppd
, "DefaultMediaType", NULL
);
3227 if (q2_attr
&& q2_attr
->value
&& q2_attr
->value
[0])
3228 q2_choice
= q2_attr
->value
;
3232 if ((attr
= ppdFindAttr(ppd
, "cupsICCQualifier3", NULL
)) != NULL
&&
3233 attr
->value
&& attr
->value
[0])
3235 snprintf(q_keyword
, sizeof(q_keyword
), "Default%s", attr
->value
);
3236 q3_attr
= ppdFindAttr(ppd
, q_keyword
, NULL
);
3239 q3_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
);
3241 if (q3_attr
&& q3_attr
->value
&& q3_attr
->value
[0])
3242 q3_choice
= q3_attr
->value
;
3248 * Build the array of profiles...
3250 * Note: This calloc actually requests slightly more memory than needed.
3253 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3255 cupsdLogMessage(CUPSD_LOG_ERROR
,
3256 "Unable to allocate memory for %d profiles!",
3262 profiles
->profileCount
= num_profiles
;
3263 languages
= _ppdGetLanguages(ppd
);
3265 for (profile
= profiles
->profiles
,
3266 attr
= ppdFindAttr(ppd
, profile_key
, NULL
);
3268 attr
= ppdFindNextAttr(ppd
, profile_key
, NULL
))
3269 if (attr
->spec
[0] && attr
->value
&& attr
->value
[0])
3272 * Add this profile...
3275 if (attr
->value
[0] != '/')
3276 snprintf(iccfile
, sizeof(iccfile
), "%s/profiles/%s", DataDir
,
3279 strlcpy(iccfile
, attr
->value
, sizeof(iccfile
));
3281 if (access(iccfile
, 0))
3284 if (profile_key
[0] == 'c')
3286 cupsArraySave(ppd
->sorted_attrs
);
3288 if ((profileid_attr
= ppdFindAttr(ppd
, "cupsProfileID",
3289 attr
->spec
)) != NULL
&&
3290 profileid_attr
->value
&& isdigit(profileid_attr
->value
[0] & 255))
3291 profile_id
= (unsigned)strtoul(profileid_attr
->value
, NULL
, 10);
3293 profile_id
= _ppdHashName(attr
->spec
);
3295 cupsArrayRestore(ppd
->sorted_attrs
);
3298 profile_id
= atoi(attr
->spec
);
3300 apple_init_profile(ppd
, languages
, profile
, profile_id
, attr
->spec
,
3301 attr
->text
[0] ? attr
->text
: attr
->spec
, iccfile
);
3306 * See if this is the default profile...
3309 if (!default_profile_id
)
3315 snprintf(selector
, sizeof(selector
), "%s.%s.%s",
3316 q1_choice
, q2_choice
, q3_choice
);
3317 if (!strcmp(selector
, attr
->spec
))
3318 default_profile_id
= profile_id
;
3321 if (!default_profile_id
)
3323 snprintf(selector
, sizeof(selector
), "%s.%s.", q1_choice
,
3325 if (!strcmp(selector
, attr
->spec
))
3326 default_profile_id
= profile_id
;
3330 if (!default_profile_id
&& q3_choice
)
3332 snprintf(selector
, sizeof(selector
), "%s..%s", q1_choice
,
3334 if (!strcmp(selector
, attr
->spec
))
3335 default_profile_id
= profile_id
;
3338 if (!default_profile_id
)
3340 snprintf(selector
, sizeof(selector
), "%s..", q1_choice
);
3341 if (!strcmp(selector
, attr
->spec
))
3342 default_profile_id
= profile_id
;
3347 _ppdFreeLanguages(languages
);
3349 else if ((cm_option
= ppdFindOption(ppd
, "ColorModel")) != NULL
)
3352 * Extract profiles from ColorModel option...
3355 const char *profile_name
; /* Name of generic profile */
3358 num_profiles
= cm_option
->num_choices
;
3360 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3362 cupsdLogMessage(CUPSD_LOG_ERROR
,
3363 "Unable to allocate memory for %d profiles!",
3369 profiles
->profileCount
= num_profiles
;
3371 for (profile
= profiles
->profiles
, i
= cm_option
->num_choices
,
3372 cm_choice
= cm_option
->choices
;
3374 i
--, cm_choice
++, profile
++)
3376 if (!strcmp(cm_choice
->choice
, "Gray") ||
3377 !strcmp(cm_choice
->choice
, "Black"))
3378 profile_name
= "Gray";
3379 else if (!strcmp(cm_choice
->choice
, "RGB") ||
3380 !strcmp(cm_choice
->choice
, "CMY"))
3381 profile_name
= "RGB";
3382 else if (!strcmp(cm_choice
->choice
, "CMYK") ||
3383 !strcmp(cm_choice
->choice
, "KCMY"))
3384 profile_name
= "CMYK";
3386 profile_name
= "DeviceN";
3388 snprintf(selector
, sizeof(selector
), "%s..", profile_name
);
3389 profile_id
= _ppdHashName(selector
);
3391 apple_init_profile(ppd
, NULL
, profile
, profile_id
, cm_choice
->choice
,
3392 cm_choice
->text
, NULL
);
3394 if (cm_choice
->marked
)
3395 default_profile_id
= profile_id
;
3401 * Use the default colorspace...
3404 num_profiles
= 1 + ppd
->colorspace
!= PPD_CS_GRAY
;
3406 if ((profiles
= calloc(num_profiles
, sizeof(CMDeviceProfileArray
))) == NULL
)
3408 cupsdLogMessage(CUPSD_LOG_ERROR
,
3409 "Unable to allocate memory for %d profiles!",
3415 profiles
->profileCount
= num_profiles
;
3417 apple_init_profile(ppd
, NULL
, profiles
->profiles
, _ppdHashName("Gray.."),
3418 "Gray", "Gray", NULL
);
3420 switch (ppd
->colorspace
)
3424 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3425 _ppdHashName("RGB.."), "RGB", "RGB", NULL
);
3429 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3430 _ppdHashName("CMYK.."), "CMYK", "CMYK", NULL
);
3434 apple_init_profile(ppd
, NULL
, profiles
->profiles
+ 1,
3435 _ppdHashName("DeviceN.."), "DeviceN", "DeviceN",
3444 if (num_profiles
> 0)
3447 * Make sure we have a default profile ID...
3450 if (!default_profile_id
)
3451 default_profile_id
= profiles
->profiles
[num_profiles
- 1].profileID
;
3454 * Get the device ID hash and pathelogical name dictionary.
3457 cupsdLogMessage(CUPSD_LOG_INFO
, "Registering ICC color profiles for \"%s\"",
3460 device_id
= _ppdHashName(p
->name
);
3461 device_name
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
3462 &kCFTypeDictionaryKeyCallBacks
,
3463 &kCFTypeDictionaryValueCallBacks
);
3464 printer_name
= CFStringCreateWithCString(kCFAllocatorDefault
,
3465 p
->name
, kCFStringEncodingUTF8
);
3467 if (device_name
&& printer_name
)
3469 CFDictionarySetValue(device_name
, CFSTR("en"), printer_name
);
3472 * Register the device with ColorSync...
3475 error
= CMRegisterColorDevice(cmPrinterDeviceClass
, device_id
,
3476 device_name
, &scope
);
3479 * Register the profiles...
3483 error
= CMSetDeviceFactoryProfiles(cmPrinterDeviceClass
, device_id
,
3484 default_profile_id
, profiles
);
3494 cupsdLogMessage(CUPSD_LOG_ERROR
,
3495 "Unable to register ICC color profiles for \"%s\" - %d",
3496 p
->name
, (int)error
);
3498 for (profile
= profiles
->profiles
;
3500 profile
++, num_profiles
--)
3501 CFRelease(profile
->profileName
);
3506 CFRelease(printer_name
);
3509 CFRelease(device_name
);
3517 * 'apple_unregister_profiles()' - Remove color profiles for the specified
3522 apple_unregister_profiles(
3523 cupsd_printer_t
*p
) /* I - Printer */
3526 * Make sure ColorSync is available...
3529 if (CMUnregisterColorDevice
!= NULL
)
3530 CMUnregisterColorDevice(cmPrinterDeviceClass
, _ppdHashName(p
->name
));
3532 #endif /* __APPLE__ */
3535 * 'apply_printer_defaults()' - Apply printer default options to a job.
3539 apply_printer_defaults(
3540 cupsd_printer_t
*printer
, /* I - Printer */
3541 cupsd_job_t
*job
) /* I - Job */
3543 int i
, /* Looping var */
3544 num_options
; /* Number of default options */
3545 cups_option_t
*options
, /* Default options */
3546 *option
; /* Current option */
3550 * Collect all of the default options and add the missing ones to the
3554 for (i
= printer
->num_options
, num_options
= 0, options
= NULL
,
3555 option
= printer
->options
;
3558 if (!ippFindAttribute(job
->attrs
, option
->name
, IPP_TAG_ZERO
))
3560 num_options
= cupsAddOption(option
->name
, option
->value
, num_options
,
3565 * Encode these options as attributes in the job object...
3568 cupsEncodeOptions2(job
->attrs
, num_options
, options
, IPP_TAG_JOB
);
3569 cupsFreeOptions(num_options
, options
);
3574 * 'authenticate_job()' - Set job authentication info.
3578 authenticate_job(cupsd_client_t
*con
, /* I - Client connection */
3579 ipp_attribute_t
*uri
) /* I - Job URI */
3581 ipp_attribute_t
*attr
, /* job-id attribute */
3582 *auth_info
; /* auth-info attribute */
3583 int jobid
; /* Job ID */
3584 cupsd_job_t
*job
; /* Current job */
3585 char scheme
[HTTP_MAX_URI
],
3586 /* Method portion of URI */
3587 username
[HTTP_MAX_URI
],
3588 /* Username portion of URI */
3590 /* Host portion of URI */
3591 resource
[HTTP_MAX_URI
];
3592 /* Resource portion of URI */
3593 int port
; /* Port portion of URI */
3596 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "authenticate_job(%p[%d], %s)",
3597 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
3600 * Start with "everything is OK" status...
3603 con
->response
->request
.status
.status_code
= IPP_OK
;
3606 * See if we have a job URI or a printer URI...
3609 if (!strcmp(uri
->name
, "printer-uri"))
3612 * Got a printer URI; see if we also have a job-id attribute...
3615 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3616 IPP_TAG_INTEGER
)) == NULL
)
3618 send_ipp_status(con
, IPP_BAD_REQUEST
,
3619 _("Got a printer-uri attribute but no job-id!"));
3623 jobid
= attr
->values
[0].integer
;
3628 * Got a job URI; parse it to get the job ID...
3631 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3632 sizeof(scheme
), username
, sizeof(username
), host
,
3633 sizeof(host
), &port
, resource
, sizeof(resource
));
3635 if (strncmp(resource
, "/jobs/", 6))
3641 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad job-uri attribute \"%s\"!"),
3642 uri
->values
[0].string
.text
);
3646 jobid
= atoi(resource
+ 6);
3650 * See if the job exists...
3653 if ((job
= cupsdFindJob(jobid
)) == NULL
)
3656 * Nope - return a "not found" error...
3659 send_ipp_status(con
, IPP_NOT_FOUND
,
3660 _("Job #%d does not exist!"), jobid
);
3665 * See if the job has been completed...
3668 if (job
->state_value
!= IPP_JOB_HELD
)
3671 * Return a "not-possible" error...
3674 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
3675 _("Job #%d is not held for authentication!"),
3681 * See if we have already authenticated...
3684 auth_info
= ippFindAttribute(con
->request
, "auth-info", IPP_TAG_TEXT
);
3686 if (!con
->username
[0] && !auth_info
)
3688 cupsd_printer_t
*printer
; /* Job destination */
3692 * No auth data. If we need to authenticate via Kerberos, send a
3693 * HTTP auth challenge, otherwise just return an IPP error...
3696 printer
= cupsdFindDest(job
->dest
);
3698 if (printer
&& printer
->num_auth_info_required
> 0 &&
3699 !strcmp(printer
->auth_info_required
[0], "negotiate"))
3700 send_http_error(con
, HTTP_UNAUTHORIZED
, printer
);
3702 send_ipp_status(con
, IPP_NOT_AUTHORIZED
,
3703 _("No authentication information provided!"));
3708 * See if the job is owned by the requesting user...
3711 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
3713 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
3718 * Save the authentication information for this job...
3721 save_auth_info(con
, job
, auth_info
);
3724 * Reset the job-hold-until value to "no-hold"...
3727 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
3728 IPP_TAG_KEYWORD
)) == NULL
)
3729 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
3733 attr
->value_tag
= IPP_TAG_KEYWORD
;
3734 cupsdSetString(&(attr
->values
[0].string
.text
), "no-hold");
3738 * Release the job and return...
3741 cupsdReleaseJob(job
);
3743 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Authenticated by \"%s\".", con
->username
);
3748 * 'cancel_all_jobs()' - Cancel all print jobs.
3752 cancel_all_jobs(cupsd_client_t
*con
, /* I - Client connection */
3753 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3755 http_status_t status
; /* Policy status */
3756 cups_ptype_t dtype
; /* Destination type */
3757 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3758 userpass
[HTTP_MAX_URI
], /* Username portion of URI */
3759 hostname
[HTTP_MAX_URI
], /* Host portion of URI */
3760 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3761 int port
; /* Port portion of URI */
3762 ipp_attribute_t
*attr
; /* Attribute in request */
3763 const char *username
; /* Username */
3764 int purge
; /* Purge? */
3765 cupsd_printer_t
*printer
; /* Printer */
3768 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_all_jobs(%p[%d], %s)", con
,
3769 con
->http
.fd
, uri
->values
[0].string
.text
);
3772 * See if we have a printer URI...
3775 if (strcmp(uri
->name
, "printer-uri"))
3777 send_ipp_status(con
, IPP_BAD_REQUEST
,
3778 _("The printer-uri attribute is required!"));
3783 * Get the username (if any) for the jobs we want to cancel (only if
3784 * "my-jobs" is specified...
3787 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
3788 IPP_TAG_BOOLEAN
)) != NULL
&&
3789 attr
->values
[0].boolean
)
3791 if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
3792 IPP_TAG_NAME
)) != NULL
)
3793 username
= attr
->values
[0].string
.text
;
3796 send_ipp_status(con
, IPP_BAD_REQUEST
,
3797 _("Missing requesting-user-name attribute!"));
3805 * Look for the "purge-jobs" attribute...
3808 if ((attr
= ippFindAttribute(con
->request
, "purge-jobs",
3809 IPP_TAG_BOOLEAN
)) != NULL
)
3810 purge
= attr
->values
[0].boolean
;
3815 * And if the destination is valid...
3818 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3824 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
,
3825 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
3826 hostname
, sizeof(hostname
), &port
,
3827 resource
, sizeof(resource
));
3829 if ((!strncmp(resource
, "/printers/", 10) && resource
[10]) ||
3830 (!strncmp(resource
, "/classes/", 9) && resource
[9]))
3832 send_ipp_status(con
, IPP_NOT_FOUND
,
3833 _("The printer or class was not found."));
3841 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
3843 send_http_error(con
, status
, NULL
);
3848 * Cancel all jobs on all printers...
3851 cupsdCancelJobs(NULL
, username
, purge
);
3853 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs were %s by \"%s\".",
3854 purge
? "purged" : "canceled", get_username(con
));
3862 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
3865 send_http_error(con
, status
, printer
);
3870 * Cancel all of the jobs on the named printer...
3873 cupsdCancelJobs(printer
->name
, username
, purge
);
3875 cupsdLogMessage(CUPSD_LOG_INFO
, "All jobs on \"%s\" were %s by \"%s\".",
3876 printer
->name
, purge
? "purged" : "canceled",
3880 con
->response
->request
.status
.status_code
= IPP_OK
;
3885 * 'cancel_job()' - Cancel a print job.
3889 cancel_job(cupsd_client_t
*con
, /* I - Client connection */
3890 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
3892 ipp_attribute_t
*attr
; /* Current attribute */
3893 int jobid
; /* Job ID */
3894 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
3895 username
[HTTP_MAX_URI
], /* Username portion of URI */
3896 host
[HTTP_MAX_URI
], /* Host portion of URI */
3897 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
3898 int port
; /* Port portion of URI */
3899 cupsd_job_t
*job
; /* Job information */
3900 cups_ptype_t dtype
; /* Destination type (printer/class) */
3901 cupsd_printer_t
*printer
; /* Printer data */
3902 int purge
; /* Purge the job? */
3905 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cancel_job(%p[%d], %s)", con
,
3906 con
->http
.fd
, uri
->values
[0].string
.text
);
3909 * See if we have a job URI or a printer URI...
3912 if (!strcmp(uri
->name
, "printer-uri"))
3915 * Got a printer URI; see if we also have a job-id attribute...
3918 if ((attr
= ippFindAttribute(con
->request
, "job-id",
3919 IPP_TAG_INTEGER
)) == NULL
)
3921 send_ipp_status(con
, IPP_BAD_REQUEST
,
3922 _("Got a printer-uri attribute but no job-id!"));
3926 if ((jobid
= attr
->values
[0].integer
) == 0)
3929 * Find the current job on the specified printer...
3932 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
3938 send_ipp_status(con
, IPP_NOT_FOUND
,
3939 _("The printer or class was not found."));
3944 * See if the printer is currently printing a job...
3948 jobid
= ((cupsd_job_t
*)printer
->job
)->id
;
3952 * No, see if there are any pending jobs...
3955 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3957 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3958 if (job
->state_value
<= IPP_JOB_PROCESSING
&&
3959 !strcasecmp(job
->dest
, printer
->name
))
3966 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
3968 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
3969 if (job
->state_value
== IPP_JOB_STOPPED
&&
3970 !strcasecmp(job
->dest
, printer
->name
))
3977 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("No active jobs on %s!"),
3988 * Got a job URI; parse it to get the job ID...
3991 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
3992 sizeof(scheme
), username
, sizeof(username
), host
,
3993 sizeof(host
), &port
, resource
, sizeof(resource
));
3995 if (strncmp(resource
, "/jobs/", 6))
4001 send_ipp_status(con
, IPP_BAD_REQUEST
,
4002 _("Bad job-uri attribute \"%s\"!"),
4003 uri
->values
[0].string
.text
);
4007 jobid
= atoi(resource
+ 6);
4011 * Look for the "purge-job" attribute...
4014 if ((attr
= ippFindAttribute(con
->request
, "purge-job",
4015 IPP_TAG_BOOLEAN
)) != NULL
)
4016 purge
= attr
->values
[0].boolean
;
4021 * See if the job exists...
4024 if ((job
= cupsdFindJob(jobid
)) == NULL
)
4027 * Nope - return a "not found" error...
4030 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
4035 * See if the job is owned by the requesting user...
4038 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
4040 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
4045 * See if the job is already completed, canceled, or aborted; if so,
4046 * we can't cancel...
4049 if (job
->state_value
>= IPP_JOB_CANCELED
&& !purge
)
4051 switch (job
->state_value
)
4053 case IPP_JOB_CANCELED
:
4054 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4055 _("Job #%d is already canceled - can\'t cancel."),
4059 case IPP_JOB_ABORTED
:
4060 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4061 _("Job #%d is already aborted - can\'t cancel."),
4066 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
4067 _("Job #%d is already completed - can\'t cancel."),
4076 * Cancel the job and return...
4079 cupsdCancelJob(job
, purge
, IPP_JOB_CANCELED
);
4083 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Purged by \"%s\".", jobid
,
4086 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job %d] Canceled by \"%s\".", jobid
,
4089 con
->response
->request
.status
.status_code
= IPP_OK
;
4094 * 'cancel_subscription()' - Cancel a subscription.
4098 cancel_subscription(
4099 cupsd_client_t
*con
, /* I - Client connection */
4100 int sub_id
) /* I - Subscription ID */
4102 http_status_t status
; /* Policy status */
4103 cupsd_subscription_t
*sub
; /* Subscription */
4106 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4107 "cancel_subscription(con=%p[%d], sub_id=%d)",
4108 con
, con
->http
.fd
, sub_id
);
4111 * Is the subscription ID valid?
4114 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
4117 * Bad subscription ID...
4120 send_ipp_status(con
, IPP_NOT_FOUND
,
4121 _("notify-subscription-id %d no good!"), sub_id
);
4129 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
4131 con
, sub
->owner
)) != HTTP_OK
)
4133 send_http_error(con
, status
, sub
->dest
);
4138 * Cancel the subscription...
4141 cupsdDeleteSubscription(sub
, 1);
4143 con
->response
->request
.status
.status_code
= IPP_OK
;
4148 * 'check_rss_recipient()' - Check that we do not have a duplicate RSS feed URI.
4151 static int /* O - 1 if OK, 0 if not */
4152 check_rss_recipient(
4153 const char *recipient
) /* I - Recipient URI */
4155 cupsd_subscription_t
*sub
; /* Current subscription */
4158 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
4160 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
4164 * Compare the URIs up to the first ?...
4167 const char *r1
, *r2
;
4169 for (r1
= recipient
, r2
= sub
->recipient
;
4170 *r1
== *r2
&& *r1
&& *r1
!= '?' && *r2
&& *r2
!= '?';
4182 * 'check_quotas()' - Check quotas for a printer and user.
4185 static int /* O - 1 if OK, 0 if not */
4186 check_quotas(cupsd_client_t
*con
, /* I - Client connection */
4187 cupsd_printer_t
*p
) /* I - Printer or class */
4189 int i
; /* Looping var */
4190 char username
[33]; /* Username */
4191 cupsd_quota_t
*q
; /* Quota data */
4192 #ifdef HAVE_MBR_UID_TO_UUID
4194 * Use Apple membership APIs which require that all names represent
4195 * valid user account or group records accessible by the server.
4198 uuid_t usr_uuid
; /* UUID for job requesting user */
4199 uuid_t usr2_uuid
; /* UUID for ACL user name entry */
4200 uuid_t grp_uuid
; /* UUID for ACL group name entry */
4201 int mbr_err
; /* Error from membership function */
4202 int is_member
; /* Is this user a member? */
4205 * Use standard POSIX APIs for checking users and groups...
4208 struct passwd
*pw
; /* User password data */
4209 #endif /* HAVE_MBR_UID_TO_UUID */
4212 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "check_quotas(%p[%d], %p[%s])",
4213 con
, con
->http
.fd
, p
, p
->name
);
4216 * Figure out who is printing...
4219 strlcpy(username
, get_username(con
), sizeof(username
));
4222 * Check global active job limits for printers and users...
4225 if (MaxJobsPerPrinter
)
4228 * Check if there are too many pending jobs on this printer...
4231 if (cupsdGetPrinterJobCount(p
->name
) >= MaxJobsPerPrinter
)
4233 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for printer \"%s\"...",
4242 * Check if there are too many pending jobs for this user...
4245 if (cupsdGetUserJobCount(username
) >= MaxJobsPerUser
)
4247 cupsdLogMessage(CUPSD_LOG_INFO
, "Too many jobs for user \"%s\"...",
4254 * Check against users...
4257 if (p
->num_users
== 0 && p
->k_limit
== 0 && p
->page_limit
== 0)
4262 #ifdef HAVE_MBR_UID_TO_UUID
4264 * Get UUID for job requesting user...
4267 if (mbr_user_name_to_uuid((char *)username
, usr_uuid
))
4273 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4274 "check_quotas: UUID lookup failed for user \"%s\"",
4276 cupsdLogMessage(CUPSD_LOG_INFO
,
4277 "Denying user \"%s\" access to printer \"%s\" "
4278 "(unknown user)...",
4284 * Get UID and GID of requesting user...
4287 pw
= getpwnam(username
);
4289 #endif /* HAVE_MBR_UID_TO_UUID */
4291 for (i
= 0; i
< p
->num_users
; i
++)
4292 if (p
->users
[i
][0] == '@')
4295 * Check group membership...
4298 #ifdef HAVE_MBR_UID_TO_UUID
4299 if (p
->users
[i
][1] == '#')
4301 if (uuid_parse((char *)p
->users
[i
] + 2, grp_uuid
))
4302 uuid_clear(grp_uuid
);
4304 else if ((mbr_err
= mbr_group_name_to_uuid((char *)p
->users
[i
] + 1,
4308 * Invalid ACL entries are ignored for matching; just record a
4309 * warning in the log...
4312 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4313 "check_quotas: UUID lookup failed for ACL entry "
4314 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4315 cupsdLogMessage(CUPSD_LOG_WARN
,
4316 "Access control entry \"%s\" not a valid group name; "
4317 "entry ignored", p
->users
[i
]);
4320 if ((mbr_err
= mbr_check_membership(usr_uuid
, grp_uuid
,
4324 * At this point, there should be no errors, but check anyways...
4327 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4328 "check_quotas: group \"%s\" membership check "
4329 "failed (err=%d)", p
->users
[i
] + 1, mbr_err
);
4334 * Stop if we found a match...
4341 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
4343 #endif /* HAVE_MBR_UID_TO_UUID */
4345 #ifdef HAVE_MBR_UID_TO_UUID
4348 if (p
->users
[i
][0] == '#')
4350 if (uuid_parse((char *)p
->users
[i
] + 1, usr2_uuid
))
4351 uuid_clear(usr2_uuid
);
4353 else if ((mbr_err
= mbr_user_name_to_uuid((char *)p
->users
[i
],
4357 * Invalid ACL entries are ignored for matching; just record a
4358 * warning in the log...
4361 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4362 "check_quotas: UUID lookup failed for ACL entry "
4363 "\"%s\" (err=%d)", p
->users
[i
], mbr_err
);
4364 cupsdLogMessage(CUPSD_LOG_WARN
,
4365 "Access control entry \"%s\" not a valid user name; "
4366 "entry ignored", p
->users
[i
]);
4369 if ((mbr_err
= mbr_check_membership(usr_uuid
, usr2_uuid
,
4372 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4373 "check_quotas: User \"%s\" identity check failed "
4374 "(err=%d)", p
->users
[i
], mbr_err
);
4382 else if (!strcasecmp(username
, p
->users
[i
]))
4384 #endif /* HAVE_MBR_UID_TO_UUID */
4386 if ((i
< p
->num_users
) == p
->deny_users
)
4388 cupsdLogMessage(CUPSD_LOG_INFO
,
4389 "Denying user \"%s\" access to printer \"%s\"...",
4400 if (AppleQuotas
&& (q
= cupsdFindQuota(p
, username
)) != NULL
)
4403 * TODO: Define these special page count values as constants!
4406 if (q
->page_count
== -4) /* special case: unlimited user */
4408 cupsdLogMessage(CUPSD_LOG_INFO
,
4409 "User \"%s\" request approved for printer %s (%s): "
4411 username
, p
->name
, p
->info
);
4412 q
->page_count
= 0; /* allow user to print */
4415 else if (q
->page_count
== -3) /* quota exceeded */
4417 cupsdLogMessage(CUPSD_LOG_INFO
,
4418 "User \"%s\" request denied for printer %s (%s): "
4419 "quota limit exceeded.",
4420 username
, p
->name
, p
->info
);
4421 q
->page_count
= 2; /* force quota exceeded failure */
4424 else if (q
->page_count
== -2) /* quota disabled for user */
4426 cupsdLogMessage(CUPSD_LOG_INFO
,
4427 "User \"%s\" request denied for printer %s (%s): "
4428 "printing disabled for user.",
4429 username
, p
->name
, p
->info
);
4430 q
->page_count
= 2; /* force quota exceeded failure */
4433 else if (q
->page_count
== -1) /* quota access error */
4435 cupsdLogMessage(CUPSD_LOG_INFO
,
4436 "User \"%s\" request denied for printer %s (%s): "
4437 "unable to determine quota limit.",
4438 username
, p
->name
, p
->info
);
4439 q
->page_count
= 2; /* force quota exceeded failure */
4442 else if (q
->page_count
< 0) /* user not found or other error */
4444 cupsdLogMessage(CUPSD_LOG_INFO
,
4445 "User \"%s\" request denied for printer %s (%s): "
4446 "user disabled / missing quota.",
4447 username
, p
->name
, p
->info
);
4448 q
->page_count
= 2; /* force quota exceeded failure */
4451 else /* page within user limits */
4453 q
->page_count
= 0; /* allow user to print */
4458 #endif /* __APPLE__ */
4459 if (p
->k_limit
|| p
->page_limit
)
4461 if ((q
= cupsdUpdateQuota(p
, username
, 0, 0)) == NULL
)
4463 cupsdLogMessage(CUPSD_LOG_ERROR
,
4464 "Unable to allocate quota data for user \"%s\"!",
4469 if ((q
->k_count
>= p
->k_limit
&& p
->k_limit
) ||
4470 (q
->page_count
>= p
->page_limit
&& p
->page_limit
))
4472 cupsdLogMessage(CUPSD_LOG_INFO
, "User \"%s\" is over the quota limit...",
4479 * If we have gotten this far, we're done!
4487 * 'copy_attribute()' - Copy a single attribute.
4490 static ipp_attribute_t
* /* O - New attribute */
4492 ipp_t
*to
, /* O - Destination request/response */
4493 ipp_attribute_t
*attr
, /* I - Attribute to copy */
4494 int quickcopy
) /* I - Do a quick copy? */
4496 int i
; /* Looping var */
4497 ipp_attribute_t
*toattr
; /* Destination attribute */
4500 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4501 "copy_attribute(%p, %p[%s,%x,%x])", to
, attr
,
4502 attr
->name
? attr
->name
: "(null)", attr
->group_tag
,
4505 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
4508 toattr
= ippAddSeparator(to
);
4511 case IPP_TAG_INTEGER
:
4513 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4514 attr
->name
, attr
->num_values
, NULL
);
4516 for (i
= 0; i
< attr
->num_values
; i
++)
4517 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
4520 case IPP_TAG_BOOLEAN
:
4521 toattr
= ippAddBooleans(to
, attr
->group_tag
, attr
->name
,
4522 attr
->num_values
, NULL
);
4524 for (i
= 0; i
< attr
->num_values
; i
++)
4525 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
4528 case IPP_TAG_STRING
:
4531 case IPP_TAG_KEYWORD
:
4533 case IPP_TAG_URISCHEME
:
4534 case IPP_TAG_CHARSET
:
4535 case IPP_TAG_LANGUAGE
:
4536 case IPP_TAG_MIMETYPE
:
4537 toattr
= ippAddStrings(to
, attr
->group_tag
,
4538 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4539 attr
->name
, attr
->num_values
, NULL
, NULL
);
4543 for (i
= 0; i
< attr
->num_values
; i
++)
4544 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4546 else if (attr
->value_tag
& IPP_TAG_COPY
)
4548 for (i
= 0; i
< attr
->num_values
; i
++)
4549 toattr
->values
[i
].string
.text
=
4550 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4554 for (i
= 0; i
< attr
->num_values
; i
++)
4555 toattr
->values
[i
].string
.text
=
4556 _cupsStrRetain(attr
->values
[i
].string
.text
);
4561 toattr
= ippAddDate(to
, attr
->group_tag
, attr
->name
,
4562 attr
->values
[0].date
);
4565 case IPP_TAG_RESOLUTION
:
4566 toattr
= ippAddResolutions(to
, attr
->group_tag
, attr
->name
,
4567 attr
->num_values
, IPP_RES_PER_INCH
,
4570 for (i
= 0; i
< attr
->num_values
; i
++)
4572 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
4573 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
4574 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
4578 case IPP_TAG_RANGE
:
4579 toattr
= ippAddRanges(to
, attr
->group_tag
, attr
->name
,
4580 attr
->num_values
, NULL
, NULL
);
4582 for (i
= 0; i
< attr
->num_values
; i
++)
4584 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
4585 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
4589 case IPP_TAG_TEXTLANG
:
4590 case IPP_TAG_NAMELANG
:
4591 toattr
= ippAddStrings(to
, attr
->group_tag
,
4592 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
4593 attr
->name
, attr
->num_values
, NULL
, NULL
);
4597 for (i
= 0; i
< attr
->num_values
; i
++)
4599 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
4600 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
4603 else if (attr
->value_tag
& IPP_TAG_COPY
)
4605 for (i
= 0; i
< attr
->num_values
; i
++)
4608 toattr
->values
[i
].string
.charset
=
4609 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
4611 toattr
->values
[i
].string
.charset
=
4612 toattr
->values
[0].string
.charset
;
4614 toattr
->values
[i
].string
.text
=
4615 _cupsStrAlloc(attr
->values
[i
].string
.text
);
4620 for (i
= 0; i
< attr
->num_values
; i
++)
4623 toattr
->values
[i
].string
.charset
=
4624 _cupsStrRetain(attr
->values
[i
].string
.charset
);
4626 toattr
->values
[i
].string
.charset
=
4627 toattr
->values
[0].string
.charset
;
4629 toattr
->values
[i
].string
.text
=
4630 _cupsStrRetain(attr
->values
[i
].string
.text
);
4635 case IPP_TAG_BEGIN_COLLECTION
:
4636 toattr
= ippAddCollections(to
, attr
->group_tag
, attr
->name
,
4637 attr
->num_values
, NULL
);
4639 for (i
= 0; i
< attr
->num_values
; i
++)
4641 toattr
->values
[i
].collection
= ippNew();
4642 copy_attrs(toattr
->values
[i
].collection
, attr
->values
[i
].collection
,
4643 NULL
, IPP_TAG_ZERO
, 0);
4648 toattr
= ippAddIntegers(to
, attr
->group_tag
, attr
->value_tag
,
4649 attr
->name
, attr
->num_values
, NULL
);
4651 for (i
= 0; i
< attr
->num_values
; i
++)
4653 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
4655 if (toattr
->values
[i
].unknown
.length
> 0)
4657 if ((toattr
->values
[i
].unknown
.data
=
4658 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
4659 toattr
->values
[i
].unknown
.length
= 0;
4661 memcpy(toattr
->values
[i
].unknown
.data
,
4662 attr
->values
[i
].unknown
.data
,
4663 toattr
->values
[i
].unknown
.length
);
4666 break; /* anti-compiler-warning-code */
4674 * 'copy_attrs()' - Copy attributes from one request to another.
4678 copy_attrs(ipp_t
*to
, /* I - Destination request */
4679 ipp_t
*from
, /* I - Source request */
4680 cups_array_t
*ra
, /* I - Requested attributes */
4681 ipp_tag_t group
, /* I - Group to copy */
4682 int quickcopy
) /* I - Do a quick copy? */
4684 ipp_attribute_t
*fromattr
; /* Source attribute */
4687 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4688 "copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
4689 to
, from
, ra
, group
, quickcopy
);
4694 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
4697 * Filter attributes as needed...
4700 if ((group
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group
&&
4701 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
4704 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
4705 copy_attribute(to
, fromattr
, quickcopy
);
4711 * 'copy_banner()' - Copy a banner file to the requests directory for the
4715 static int /* O - Size of banner file in kbytes */
4716 copy_banner(cupsd_client_t
*con
, /* I - Client connection */
4717 cupsd_job_t
*job
, /* I - Job information */
4718 const char *name
) /* I - Name of banner */
4720 int i
; /* Looping var */
4721 int kbytes
; /* Size of banner file in kbytes */
4722 char filename
[1024]; /* Job filename */
4723 cupsd_banner_t
*banner
; /* Pointer to banner */
4724 cups_file_t
*in
; /* Input file */
4725 cups_file_t
*out
; /* Output file */
4726 int ch
; /* Character from file */
4727 char attrname
[255], /* Name of attribute */
4728 *s
; /* Pointer into name */
4729 ipp_attribute_t
*attr
; /* Attribute */
4732 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4733 "copy_banner(con=%p[%d], job=%p[%d], name=\"%s\")",
4734 con
, con
? con
->http
.fd
: -1, job
, job
->id
,
4735 name
? name
: "(null)");
4738 * Find the banner; return if not found or "none"...
4741 if (!name
|| !strcmp(name
, "none") ||
4742 (banner
= cupsdFindBanner(name
)) == NULL
)
4746 * Open the banner and job files...
4749 if (add_file(con
, job
, banner
->filetype
, 0))
4752 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
4754 if ((out
= cupsFileOpen(filename
, "w")) == NULL
)
4756 cupsdLogMessage(CUPSD_LOG_ERROR
,
4757 "Unable to create banner job file %s - %s",
4758 filename
, strerror(errno
));
4763 fchmod(cupsFileNumber(out
), 0640);
4764 fchown(cupsFileNumber(out
), RunUser
, Group
);
4767 * Try the localized banner file under the subdirectory...
4770 strlcpy(attrname
, job
->attrs
->attrs
->next
->values
[0].string
.text
,
4772 if (strlen(attrname
) > 2 && attrname
[2] == '-')
4775 * Convert ll-cc to ll_CC...
4779 attrname
[3] = toupper(attrname
[3] & 255);
4780 attrname
[4] = toupper(attrname
[4] & 255);
4783 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4786 if (access(filename
, 0) && strlen(attrname
) > 2)
4789 * Wasn't able to find "ll_CC" locale file; try the non-national
4790 * localization banner directory.
4795 snprintf(filename
, sizeof(filename
), "%s/banners/%s/%s", DataDir
,
4799 if (access(filename
, 0))
4802 * Use the non-localized banner file.
4805 snprintf(filename
, sizeof(filename
), "%s/banners/%s", DataDir
, name
);
4808 if ((in
= cupsFileOpen(filename
, "r")) == NULL
)
4812 cupsdLogMessage(CUPSD_LOG_ERROR
,
4813 "Unable to open banner template file %s - %s",
4814 filename
, strerror(errno
));
4820 * Parse the file to the end...
4823 while ((ch
= cupsFileGetChar(in
)) != EOF
)
4827 * Get an attribute name...
4830 for (s
= attrname
; (ch
= cupsFileGetChar(in
)) != EOF
;)
4831 if (!isalpha(ch
& 255) && ch
!= '-' && ch
!= '?')
4833 else if (s
< (attrname
+ sizeof(attrname
) - 1))
4843 * Ignore { followed by stuff that is not an attribute name...
4846 cupsFilePrintf(out
, "{%s%c", attrname
, ch
);
4851 * See if it is defined...
4854 if (attrname
[0] == '?')
4859 if (!strcmp(s
, "printer-name"))
4861 cupsFilePuts(out
, job
->dest
);
4864 else if ((attr
= ippFindAttribute(job
->attrs
, s
, IPP_TAG_ZERO
)) == NULL
)
4867 * See if we have a leading question mark...
4870 if (attrname
[0] != '?')
4873 * Nope, write to file as-is; probably a PostScript procedure...
4876 cupsFilePrintf(out
, "{%s}", attrname
);
4883 * Output value(s)...
4886 for (i
= 0; i
< attr
->num_values
; i
++)
4889 cupsFilePutChar(out
, ',');
4891 switch (attr
->value_tag
)
4893 case IPP_TAG_INTEGER
:
4895 if (!strncmp(s
, "time-at-", 8))
4896 cupsFilePuts(out
, cupsdGetDateTime(attr
->values
[i
].integer
));
4898 cupsFilePrintf(out
, "%d", attr
->values
[i
].integer
);
4901 case IPP_TAG_BOOLEAN
:
4902 cupsFilePrintf(out
, "%d", attr
->values
[i
].boolean
);
4905 case IPP_TAG_NOVALUE
:
4906 cupsFilePuts(out
, "novalue");
4909 case IPP_TAG_RANGE
:
4910 cupsFilePrintf(out
, "%d-%d", attr
->values
[i
].range
.lower
,
4911 attr
->values
[i
].range
.upper
);
4914 case IPP_TAG_RESOLUTION
:
4915 cupsFilePrintf(out
, "%dx%d%s", attr
->values
[i
].resolution
.xres
,
4916 attr
->values
[i
].resolution
.yres
,
4917 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4922 case IPP_TAG_STRING
:
4925 case IPP_TAG_KEYWORD
:
4926 case IPP_TAG_CHARSET
:
4927 case IPP_TAG_LANGUAGE
:
4928 if (!strcasecmp(banner
->filetype
->type
, "postscript"))
4931 * Need to quote strings for PS banners...
4936 for (p
= attr
->values
[i
].string
.text
; *p
; p
++)
4938 if (*p
== '(' || *p
== ')' || *p
== '\\')
4940 cupsFilePutChar(out
, '\\');
4941 cupsFilePutChar(out
, *p
);
4943 else if (*p
< 32 || *p
> 126)
4944 cupsFilePrintf(out
, "\\%03o", *p
& 255);
4946 cupsFilePutChar(out
, *p
);
4950 cupsFilePuts(out
, attr
->values
[i
].string
.text
);
4954 break; /* anti-compiler-warning-code */
4958 else if (ch
== '\\') /* Quoted char */
4960 ch
= cupsFileGetChar(in
);
4962 if (ch
!= '{') /* Only do special handling for \{ */
4963 cupsFilePutChar(out
, '\\');
4965 cupsFilePutChar(out
, ch
);
4968 cupsFilePutChar(out
, ch
);
4972 kbytes
= (cupsFileTell(out
) + 1023) / 1024;
4974 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
4975 IPP_TAG_INTEGER
)) != NULL
)
4976 attr
->values
[0].integer
+= kbytes
;
4985 * 'copy_file()' - Copy a PPD file or interface script...
4988 static int /* O - 0 = success, -1 = error */
4989 copy_file(const char *from
, /* I - Source file */
4990 const char *to
) /* I - Destination file */
4992 cups_file_t
*src
, /* Source file */
4993 *dst
; /* Destination file */
4994 int bytes
; /* Bytes to read/write */
4995 char buffer
[2048]; /* Copy buffer */
4998 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "copy_file(\"%s\", \"%s\")", from
, to
);
5001 * Open the source and destination file for a copy...
5004 if ((src
= cupsFileOpen(from
, "rb")) == NULL
)
5007 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5014 * Copy the source file to the destination...
5017 while ((bytes
= cupsFileRead(src
, buffer
, sizeof(buffer
))) > 0)
5018 if (cupsFileWrite(dst
, buffer
, bytes
) < bytes
)
5026 * Close both files and return...
5031 return (cupsFileClose(dst
));
5036 * 'copy_model()' - Copy a PPD model file, substituting default values
5040 static int /* O - 0 = success, -1 = error */
5041 copy_model(cupsd_client_t
*con
, /* I - Client connection */
5042 const char *from
, /* I - Source file */
5043 const char *to
) /* I - Destination file */
5045 fd_set input
; /* select() input set */
5046 struct timeval timeout
; /* select() timeout */
5047 int maxfd
; /* Max file descriptor for select() */
5048 char tempfile
[1024]; /* Temporary PPD file */
5049 int tempfd
; /* Temporary PPD file descriptor */
5050 int temppid
; /* Process ID of cups-driverd */
5051 int temppipe
[2]; /* Temporary pipes */
5052 char *argv
[4], /* Command-line arguments */
5053 *envp
[MAX_ENV
]; /* Environment */
5054 cups_file_t
*src
, /* Source file */
5055 *dst
; /* Destination file */
5056 ppd_file_t
*ppd
; /* PPD file */
5057 int bytes
, /* Bytes from pipe */
5058 total
; /* Total bytes from pipe */
5059 char buffer
[2048]; /* Copy buffer */
5060 int i
; /* Looping var */
5061 char option
[PPD_MAX_NAME
], /* Option name */
5062 choice
[PPD_MAX_NAME
]; /* Choice name */
5063 int num_defaults
; /* Number of default options */
5064 cups_option_t
*defaults
; /* Default options */
5065 char cups_protocol
[PPD_MAX_LINE
];
5066 /* cupsProtocol attribute */
5069 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5070 "copy_model(con=%p, from=\"%s\", to=\"%s\")",
5074 * Run cups-driverd to get the PPD file...
5077 argv
[0] = "cups-driverd";
5079 argv
[2] = (char *)from
;
5082 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5084 snprintf(buffer
, sizeof(buffer
), "%s/daemon/cups-driverd", ServerBin
);
5085 snprintf(tempfile
, sizeof(tempfile
), "%s/%d.ppd", TempDir
, con
->http
.fd
);
5086 tempfd
= open(tempfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
5090 cupsdOpenPipe(temppipe
);
5092 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5093 "copy_model: Running \"cups-driverd cat %s\"...", from
);
5095 if (!cupsdStartProcess(buffer
, argv
, envp
, -1, temppipe
[1], CGIPipes
[1],
5096 -1, -1, 0, DefaultProfile
, &temppid
))
5107 * Wait up to 30 seconds for the PPD file to be copied...
5112 if (temppipe
[0] > CGIPipes
[0])
5113 maxfd
= temppipe
[0] + 1;
5115 maxfd
= CGIPipes
[0] + 1;
5120 * See if we have data ready...
5124 FD_SET(temppipe
[0], &input
);
5125 FD_SET(CGIPipes
[0], &input
);
5127 timeout
.tv_sec
= 30;
5128 timeout
.tv_usec
= 0;
5130 if ((i
= select(maxfd
, &input
, NULL
, NULL
, &timeout
)) < 0)
5140 * We have timed out...
5146 if (FD_ISSET(temppipe
[0], &input
))
5149 * Read the PPD file from the pipe, and write it to the PPD file.
5152 if ((bytes
= read(temppipe
[0], buffer
, sizeof(buffer
))) > 0)
5154 if (write(tempfd
, buffer
, bytes
) < bytes
)
5163 if (FD_ISSET(CGIPipes
[0], &input
))
5173 * No data from cups-deviced...
5176 cupsdLogMessage(CUPSD_LOG_ERROR
, "copy_model: empty PPD file!");
5182 * Read the source file and see what page sizes are supported...
5185 if ((ppd
= ppdOpenFile(tempfile
)) == NULL
)
5192 * Open the destination (if possible) and set the default options...
5197 cups_protocol
[0] = '\0';
5199 if ((dst
= cupsFileOpen(to
, "rb")) != NULL
)
5202 * Read all of the default lines from the old PPD...
5205 while (cupsFileGets(dst
, buffer
, sizeof(buffer
)))
5206 if (!strncmp(buffer
, "*Default", 8))
5209 * Add the default option...
5212 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5213 choice
, sizeof(choice
)))
5215 ppd_option_t
*ppdo
; /* PPD option */
5219 * Only add the default if the default hasn't already been
5220 * set and the choice exists in the new PPD...
5223 if (!cupsGetOption(option
, num_defaults
, defaults
) &&
5224 (ppdo
= ppdFindOption(ppd
, option
)) != NULL
&&
5225 ppdFindChoice(ppdo
, choice
))
5226 num_defaults
= cupsAddOption(option
, choice
, num_defaults
,
5230 else if (!strncmp(buffer
, "*cupsProtocol:", 14))
5231 strlcpy(cups_protocol
, buffer
, sizeof(cups_protocol
));
5235 else if (ppdPageSize(ppd
, DefaultPaperSize
))
5238 * Add the default media sizes...
5241 num_defaults
= cupsAddOption("PageSize", DefaultPaperSize
,
5242 num_defaults
, &defaults
);
5243 num_defaults
= cupsAddOption("PageRegion", DefaultPaperSize
,
5244 num_defaults
, &defaults
);
5245 num_defaults
= cupsAddOption("PaperDimension", DefaultPaperSize
,
5246 num_defaults
, &defaults
);
5247 num_defaults
= cupsAddOption("ImageableArea", DefaultPaperSize
,
5248 num_defaults
, &defaults
);
5254 * Open the source file for a copy...
5257 if ((src
= cupsFileOpen(tempfile
, "rb")) == NULL
)
5259 cupsFreeOptions(num_defaults
, defaults
);
5265 * Open the destination file for a copy...
5268 if ((dst
= cupsFileOpen(to
, "wb")) == NULL
)
5270 cupsFreeOptions(num_defaults
, defaults
);
5277 * Copy the source file to the destination...
5280 while (cupsFileGets(src
, buffer
, sizeof(buffer
)))
5282 if (!strncmp(buffer
, "*Default", 8))
5285 * Check for an previous default option choice...
5288 if (!ppd_parse_line(buffer
, option
, sizeof(option
),
5289 choice
, sizeof(choice
)))
5291 const char *val
; /* Default option value */
5294 if ((val
= cupsGetOption(option
, num_defaults
, defaults
)) != NULL
)
5297 * Substitute the previous choice...
5300 snprintf(buffer
, sizeof(buffer
), "*Default%s: %s", option
, val
);
5305 cupsFilePrintf(dst
, "%s\n", buffer
);
5308 if (cups_protocol
[0])
5309 cupsFilePrintf(dst
, "%s\n", cups_protocol
);
5311 cupsFreeOptions(num_defaults
, defaults
);
5314 * Close both files and return...
5321 return (cupsFileClose(dst
));
5326 * 'copy_job_attrs()' - Copy job attributes.
5330 copy_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
5331 cupsd_job_t
*job
, /* I - Job */
5332 cups_array_t
*ra
) /* I - Requested attributes array */
5334 char job_uri
[HTTP_MAX_URI
]; /* Job URI */
5338 * Send the requested attributes for each job...
5341 httpAssembleURIf(HTTP_URI_CODING_ALL
, job_uri
, sizeof(job_uri
), "ipp", NULL
,
5342 con
->servername
, con
->serverport
, "/jobs/%d",
5345 if (!ra
|| cupsArrayFind(ra
, "document-count"))
5346 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5347 "document-count", job
->num_files
);
5349 if (!ra
|| cupsArrayFind(ra
, "job-media-progress"))
5350 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5351 "job-media-progress", job
->progress
);
5353 if (!ra
|| cupsArrayFind(ra
, "job-more-info"))
5354 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5355 "job-more-info", NULL
, job_uri
);
5357 if (job
->state_value
> IPP_JOB_PROCESSING
&&
5358 (!ra
|| cupsArrayFind(ra
, "job-preserved")))
5359 ippAddBoolean(con
->response
, IPP_TAG_JOB
, "job-preserved",
5360 job
->num_files
> 0);
5362 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
5363 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
5364 "job-printer-up-time", time(NULL
));
5366 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
5367 add_job_state_reasons(con
, job
);
5369 if (!ra
|| cupsArrayFind(ra
, "job-uri"))
5370 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
,
5371 "job-uri", NULL
, job_uri
);
5373 copy_attrs(con
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
5378 * 'copy_printer_attrs()' - Copy printer attributes.
5383 cupsd_client_t
*con
, /* I - Client connection */
5384 cupsd_printer_t
*printer
, /* I - Printer */
5385 cups_array_t
*ra
) /* I - Requested attributes array */
5387 char printer_uri
[HTTP_MAX_URI
];
5389 time_t curtime
; /* Current time */
5390 int i
; /* Looping var */
5391 ipp_attribute_t
*history
; /* History collection */
5395 * Copy the printer attributes to the response using requested-attributes
5396 * and document-format attributes that may be provided by the client.
5399 curtime
= time(NULL
);
5402 if ((!ra
|| cupsArrayFind(ra
, "com.apple.print.recoverable-message")) &&
5403 printer
->recoverable
)
5404 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5405 "com.apple.print.recoverable-message", NULL
,
5406 printer
->recoverable
);
5407 #endif /* __APPLE__ */
5409 if (!ra
|| cupsArrayFind(ra
, "marker-change-time"))
5410 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5411 "marker-change-time", printer
->marker_time
);
5413 if (printer
->num_printers
> 0 &&
5414 (!ra
|| cupsArrayFind(ra
, "member-uris")))
5416 ipp_attribute_t
*member_uris
; /* member-uris attribute */
5417 cupsd_printer_t
*p2
; /* Printer in class */
5418 ipp_attribute_t
*p2_uri
; /* printer-uri-supported for class printer */
5421 if ((member_uris
= ippAddStrings(con
->response
, IPP_TAG_PRINTER
,
5422 IPP_TAG_URI
, "member-uris",
5423 printer
->num_printers
, NULL
,
5426 for (i
= 0; i
< printer
->num_printers
; i
++)
5428 p2
= printer
->printers
[i
];
5430 if ((p2_uri
= ippFindAttribute(p2
->attrs
, "printer-uri-supported",
5431 IPP_TAG_URI
)) != NULL
)
5432 member_uris
->values
[i
].string
.text
=
5433 _cupsStrRetain(p2_uri
->values
[0].string
.text
);
5436 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
,
5437 sizeof(printer_uri
), "ipp", NULL
, con
->servername
,
5439 (p2
->type
& CUPS_PRINTER_CLASS
) ?
5440 "/classes/%s" : "/printers/%s", p2
->name
);
5441 member_uris
->values
[i
].string
.text
= _cupsStrAlloc(printer_uri
);
5447 if (printer
->alert
&& (!ra
|| cupsArrayFind(ra
, "printer-alert")))
5448 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_STRING
,
5449 "printer-alert", NULL
, printer
->alert
);
5451 if (printer
->alert_description
&&
5452 (!ra
|| cupsArrayFind(ra
, "printer-alert-description")))
5453 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5454 "printer-alert-description", NULL
,
5455 printer
->alert_description
);
5457 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
5458 ippAddDate(con
->response
, IPP_TAG_PRINTER
, "printer-current-time",
5459 ippTimeToDate(curtime
));
5462 if (!ra
|| cupsArrayFind(ra
, "printer-dns-sd-name"))
5464 if (printer
->reg_name
)
5465 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5466 "printer-dns-sd-name", NULL
, printer
->reg_name
);
5468 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
5469 "printer-dns-sd-name", 0);
5471 #endif /* HAVE_DNSSD */
5473 if (!ra
|| cupsArrayFind(ra
, "printer-error-policy"))
5474 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5475 "printer-error-policy", NULL
, printer
->error_policy
);
5477 if (!ra
|| cupsArrayFind(ra
, "printer-is-accepting-jobs"))
5478 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
5479 printer
->accepting
);
5481 if (!ra
|| cupsArrayFind(ra
, "printer-is-shared"))
5482 ippAddBoolean(con
->response
, IPP_TAG_PRINTER
, "printer-is-shared",
5485 if ((!ra
|| cupsArrayFind(ra
, "printer-more-info")) &&
5486 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
5488 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5489 "http", NULL
, con
->servername
, con
->serverport
,
5490 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5491 "/classes/%s" : "/printers/%s", printer
->name
);
5492 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5493 "printer-more-info", NULL
, printer_uri
);
5496 if (!ra
|| cupsArrayFind(ra
, "printer-op-policy"))
5497 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
5498 "printer-op-policy", NULL
, printer
->op_policy
);
5500 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
5501 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
5504 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
5505 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5506 "printer-state-change-time", printer
->state_time
);
5508 if (MaxPrinterHistory
> 0 && printer
->num_history
> 0 &&
5509 cupsArrayFind(ra
, "printer-state-history"))
5512 * Printer history is only sent if specifically requested, so that
5513 * older CUPS/IPP clients won't barf on the collection attributes.
5516 history
= ippAddCollections(con
->response
, IPP_TAG_PRINTER
,
5517 "printer-state-history",
5518 printer
->num_history
, NULL
);
5520 for (i
= 0; i
< printer
->num_history
; i
++)
5521 copy_attrs(history
->values
[i
].collection
= ippNew(), printer
->history
[i
],
5522 NULL
, IPP_TAG_ZERO
, 0);
5525 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
5526 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
5527 "printer-state-message", NULL
, printer
->state_message
);
5529 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
5530 add_printer_state_reasons(con
, printer
);
5532 if (!ra
|| cupsArrayFind(ra
, "printer-type"))
5534 int type
; /* printer-type value */
5538 * Add the CUPS-specific printer-type attribute...
5541 type
= printer
->type
;
5543 if (printer
== DefaultPrinter
)
5544 type
|= CUPS_PRINTER_DEFAULT
;
5546 if (!printer
->accepting
)
5547 type
|= CUPS_PRINTER_REJECTING
;
5549 if (!printer
->shared
)
5550 type
|= CUPS_PRINTER_NOT_SHARED
;
5552 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
5556 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
5557 ippAddInteger(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
5558 "printer-up-time", curtime
);
5560 if ((!ra
|| cupsArrayFind(ra
, "printer-uri-supported")) &&
5561 !(printer
->type
& CUPS_PRINTER_DISCOVERED
))
5563 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5564 "ipp", NULL
, con
->servername
, con
->serverport
,
5565 (printer
->type
& CUPS_PRINTER_CLASS
) ?
5566 "/classes/%s" : "/printers/%s", printer
->name
);
5567 ippAddString(con
->response
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
5568 "printer-uri-supported", NULL
, printer_uri
);
5569 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "printer-uri-supported=\"%s\"",
5573 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
5574 add_queued_job_count(con
, printer
);
5576 copy_attrs(con
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
, 0);
5577 if (printer
->ppd_attrs
)
5578 copy_attrs(con
->response
, printer
->ppd_attrs
, ra
, IPP_TAG_ZERO
, 0);
5579 copy_attrs(con
->response
, CommonData
, ra
, IPP_TAG_ZERO
, IPP_TAG_COPY
);
5584 * 'copy_subscription_attrs()' - Copy subscription attributes.
5588 copy_subscription_attrs(
5589 cupsd_client_t
*con
, /* I - Client connection */
5590 cupsd_subscription_t
*sub
, /* I - Subscription */
5591 cups_array_t
*ra
) /* I - Requested attributes array */
5593 ipp_attribute_t
*attr
; /* Current attribute */
5594 char printer_uri
[HTTP_MAX_URI
];
5596 int count
; /* Number of events */
5597 unsigned mask
; /* Current event mask */
5598 const char *name
; /* Current event name */
5602 * Copy the subscription attributes to the response using the
5603 * requested-attributes attribute that may be provided by the client.
5606 if (!ra
|| cupsArrayFind(ra
, "notify-events"))
5608 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
5611 * Simple event list...
5614 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
,
5615 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5616 "notify-events", NULL
, name
);
5621 * Complex event list...
5624 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5625 if (sub
->mask
& mask
)
5628 attr
= ippAddStrings(con
->response
, IPP_TAG_SUBSCRIPTION
,
5629 (ipp_tag_t
)(IPP_TAG_KEYWORD
| IPP_TAG_COPY
),
5630 "notify-events", count
, NULL
, NULL
);
5632 for (mask
= 1, count
= 0; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
5633 if (sub
->mask
& mask
)
5635 attr
->values
[count
].string
.text
=
5636 (char *)cupsdEventName((cupsd_eventmask_t
)mask
);
5643 if (sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-job-id")))
5644 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5645 "notify-job-id", sub
->job
->id
);
5647 if (!sub
->job
&& (!ra
|| cupsArrayFind(ra
, "notify-lease-duration")))
5648 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5649 "notify-lease-duration", sub
->lease
);
5651 if (sub
->dest
&& (!ra
|| cupsArrayFind(ra
, "notify-printer-uri")))
5653 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
5654 "ipp", NULL
, con
->servername
, con
->serverport
,
5655 "/printers/%s", sub
->dest
->name
);
5656 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5657 "notify-printer-uri", NULL
, printer_uri
);
5660 if (sub
->recipient
&& (!ra
|| cupsArrayFind(ra
, "notify-recipient-uri")))
5661 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_URI
,
5662 "notify-recipient-uri", NULL
, sub
->recipient
);
5663 else if (!ra
|| cupsArrayFind(ra
, "notify-pull-method"))
5664 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
5665 "notify-pull-method", NULL
, "ippget");
5667 if (!ra
|| cupsArrayFind(ra
, "notify-subscriber-user-name"))
5668 ippAddString(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_NAME
,
5669 "notify-subscriber-user-name", NULL
, sub
->owner
);
5671 if (!ra
|| cupsArrayFind(ra
, "notify-subscription-id"))
5672 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5673 "notify-subscription-id", sub
->id
);
5675 if (!ra
|| cupsArrayFind(ra
, "notify-time-interval"))
5676 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
5677 "notify-time-interval", sub
->interval
);
5679 if (sub
->user_data_len
> 0 && (!ra
|| cupsArrayFind(ra
, "notify-user-data")))
5680 ippAddOctetString(con
->response
, IPP_TAG_SUBSCRIPTION
, "notify-user-data",
5681 sub
->user_data
, sub
->user_data_len
);
5686 * 'create_job()' - Print a file to a printer or class.
5690 create_job(cupsd_client_t
*con
, /* I - Client connection */
5691 ipp_attribute_t
*uri
) /* I - Printer URI */
5693 cupsd_printer_t
*printer
; /* Printer */
5694 cupsd_job_t
*job
; /* New job */
5697 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "create_job(%p[%d], %s)", con
,
5698 con
->http
.fd
, uri
->values
[0].string
.text
);
5701 * Is the destination valid?
5704 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
5710 send_ipp_status(con
, IPP_NOT_FOUND
,
5711 _("The printer or class was not found."));
5716 * Create the job object...
5719 if ((job
= add_job(con
, printer
, NULL
)) == NULL
)
5722 job
->pending_timeout
= 1;
5725 * Save and log the job...
5728 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
5729 job
->dest
, job
->username
);
5734 * 'create_requested_array()' - Create an array for the requested-attributes.
5737 static cups_array_t
* /* O - Array of attributes or NULL */
5738 create_requested_array(ipp_t
*request
) /* I - IPP request */
5740 int i
; /* Looping var */
5741 ipp_attribute_t
*requested
; /* requested-attributes attribute */
5742 cups_array_t
*ra
; /* Requested attributes array */
5743 char *value
; /* Current value */
5747 * Get the requested-attributes attribute, and return NULL if we don't
5751 if ((requested
= ippFindAttribute(request
, "requested-attributes",
5752 IPP_TAG_KEYWORD
)) == NULL
)
5756 * If the attribute contains a single "all" keyword, return NULL...
5759 if (requested
->num_values
== 1 &&
5760 !strcmp(requested
->values
[0].string
.text
, "all"))
5764 * Create an array using "strcmp" as the comparison function...
5767 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
5769 for (i
= 0; i
< requested
->num_values
; i
++)
5771 value
= requested
->values
[i
].string
.text
;
5773 if (!strcmp(value
, "job-template"))
5775 cupsArrayAdd(ra
, "copies");
5776 cupsArrayAdd(ra
, "copies-default");
5777 cupsArrayAdd(ra
, "copies-supported");
5778 cupsArrayAdd(ra
, "finishings");
5779 cupsArrayAdd(ra
, "finishings-default");
5780 cupsArrayAdd(ra
, "finishings-supported");
5781 cupsArrayAdd(ra
, "job-hold-until");
5782 cupsArrayAdd(ra
, "job-hold-until-default");
5783 cupsArrayAdd(ra
, "job-hold-until-supported");
5784 cupsArrayAdd(ra
, "job-priority");
5785 cupsArrayAdd(ra
, "job-priority-default");
5786 cupsArrayAdd(ra
, "job-priority-supported");
5787 cupsArrayAdd(ra
, "job-sheets");
5788 cupsArrayAdd(ra
, "job-sheets-default");
5789 cupsArrayAdd(ra
, "job-sheets-supported");
5790 cupsArrayAdd(ra
, "media");
5791 cupsArrayAdd(ra
, "media-default");
5792 cupsArrayAdd(ra
, "media-supported");
5793 cupsArrayAdd(ra
, "multiple-document-handling");
5794 cupsArrayAdd(ra
, "multiple-document-handling-default");
5795 cupsArrayAdd(ra
, "multiple-document-handling-supported");
5796 cupsArrayAdd(ra
, "number-up");
5797 cupsArrayAdd(ra
, "number-up-default");
5798 cupsArrayAdd(ra
, "number-up-supported");
5799 cupsArrayAdd(ra
, "orientation-requested");
5800 cupsArrayAdd(ra
, "orientation-requested-default");
5801 cupsArrayAdd(ra
, "orientation-requested-supported");
5802 cupsArrayAdd(ra
, "page-ranges");
5803 cupsArrayAdd(ra
, "page-ranges-supported");
5804 cupsArrayAdd(ra
, "printer-resolution");
5805 cupsArrayAdd(ra
, "printer-resolution-default");
5806 cupsArrayAdd(ra
, "printer-resolution-supported");
5807 cupsArrayAdd(ra
, "print-quality");
5808 cupsArrayAdd(ra
, "print-quality-default");
5809 cupsArrayAdd(ra
, "print-quality-supported");
5810 cupsArrayAdd(ra
, "sides");
5811 cupsArrayAdd(ra
, "sides-default");
5812 cupsArrayAdd(ra
, "sides-supported");
5814 else if (!strcmp(value
, "job-description"))
5816 cupsArrayAdd(ra
, "date-time-at-completed");
5817 cupsArrayAdd(ra
, "date-time-at-creation");
5818 cupsArrayAdd(ra
, "date-time-at-processing");
5819 cupsArrayAdd(ra
, "job-detailed-status-message");
5820 cupsArrayAdd(ra
, "job-document-access-errors");
5821 cupsArrayAdd(ra
, "job-id");
5822 cupsArrayAdd(ra
, "job-impressions");
5823 cupsArrayAdd(ra
, "job-impressions-completed");
5824 cupsArrayAdd(ra
, "job-k-octets");
5825 cupsArrayAdd(ra
, "job-k-octets-processed");
5826 cupsArrayAdd(ra
, "job-media-progress");
5827 cupsArrayAdd(ra
, "job-media-sheets");
5828 cupsArrayAdd(ra
, "job-media-sheets-completed");
5829 cupsArrayAdd(ra
, "job-message-from-operator");
5830 cupsArrayAdd(ra
, "job-more-info");
5831 cupsArrayAdd(ra
, "job-name");
5832 cupsArrayAdd(ra
, "job-originating-user-name");
5833 cupsArrayAdd(ra
, "job-printer-up-time");
5834 cupsArrayAdd(ra
, "job-printer-uri");
5835 cupsArrayAdd(ra
, "job-state");
5836 cupsArrayAdd(ra
, "job-state-message");
5837 cupsArrayAdd(ra
, "job-state-reasons");
5838 cupsArrayAdd(ra
, "job-uri");
5839 cupsArrayAdd(ra
, "number-of-documents");
5840 cupsArrayAdd(ra
, "number-of-intervening-jobs");
5841 cupsArrayAdd(ra
, "output-device-assigned");
5842 cupsArrayAdd(ra
, "time-at-completed");
5843 cupsArrayAdd(ra
, "time-at-creation");
5844 cupsArrayAdd(ra
, "time-at-processing");
5846 else if (!strcmp(value
, "printer-description"))
5848 cupsArrayAdd(ra
, "charset-configured");
5849 cupsArrayAdd(ra
, "charset-supported");
5850 cupsArrayAdd(ra
, "color-supported");
5851 cupsArrayAdd(ra
, "compression-supported");
5852 cupsArrayAdd(ra
, "document-format-default");
5853 cupsArrayAdd(ra
, "document-format-supported");
5854 cupsArrayAdd(ra
, "generated-natural-language-supported");
5855 cupsArrayAdd(ra
, "ipp-versions-supported");
5856 cupsArrayAdd(ra
, "job-impressions-supported");
5857 cupsArrayAdd(ra
, "job-k-octets-supported");
5858 cupsArrayAdd(ra
, "job-media-sheets-supported");
5859 cupsArrayAdd(ra
, "job-settable-attributes-supported");
5860 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
5861 cupsArrayAdd(ra
, "multiple-operation-time-out");
5862 cupsArrayAdd(ra
, "natural-language-configured");
5863 cupsArrayAdd(ra
, "notify-attributes-supported");
5864 cupsArrayAdd(ra
, "notify-lease-duration-default");
5865 cupsArrayAdd(ra
, "notify-lease-duration-supported");
5866 cupsArrayAdd(ra
, "notify-max-events-supported");
5867 cupsArrayAdd(ra
, "notify-events-default");
5868 cupsArrayAdd(ra
, "notify-events-supported");
5869 cupsArrayAdd(ra
, "notify-pull-method-supported");
5870 cupsArrayAdd(ra
, "notify-schemes-supported");
5871 cupsArrayAdd(ra
, "operations-supported");
5872 cupsArrayAdd(ra
, "pages-per-minute");
5873 cupsArrayAdd(ra
, "pages-per-minute-color");
5874 cupsArrayAdd(ra
, "pdl-override-supported");
5875 cupsArrayAdd(ra
, "printer-alert");
5876 cupsArrayAdd(ra
, "printer-alert-description");
5877 cupsArrayAdd(ra
, "printer-commands");
5878 cupsArrayAdd(ra
, "printer-current-time");
5879 cupsArrayAdd(ra
, "printer-driver-installer");
5880 cupsArrayAdd(ra
, "printer-dns-sd-name");
5881 cupsArrayAdd(ra
, "printer-info");
5882 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
5883 cupsArrayAdd(ra
, "printer-location");
5884 cupsArrayAdd(ra
, "printer-make-and-model");
5885 cupsArrayAdd(ra
, "printer-message-from-operator");
5886 cupsArrayAdd(ra
, "printer-more-info");
5887 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
5888 cupsArrayAdd(ra
, "printer-name");
5889 cupsArrayAdd(ra
, "printer-state");
5890 cupsArrayAdd(ra
, "printer-state-message");
5891 cupsArrayAdd(ra
, "printer-state-reasons");
5892 cupsArrayAdd(ra
, "printer-settable-attributes-supported");
5893 cupsArrayAdd(ra
, "printer-type");
5894 cupsArrayAdd(ra
, "printer-up-time");
5895 cupsArrayAdd(ra
, "printer-uri-supported");
5896 cupsArrayAdd(ra
, "queued-job-count");
5897 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
5898 cupsArrayAdd(ra
, "uri-authentication-supported");
5899 cupsArrayAdd(ra
, "uri-security-supported");
5901 else if (!strcmp(value
, "printer-defaults"))
5903 char *name
; /* Option name */
5906 for (name
= (char *)cupsArrayFirst(CommonDefaults
);
5908 name
= (char *)cupsArrayNext(CommonDefaults
))
5909 cupsArrayAdd(ra
, name
);
5911 else if (!strcmp(value
, "subscription-template"))
5913 cupsArrayAdd(ra
, "notify-attributes");
5914 cupsArrayAdd(ra
, "notify-charset");
5915 cupsArrayAdd(ra
, "notify-events");
5916 cupsArrayAdd(ra
, "notify-lease-duration");
5917 cupsArrayAdd(ra
, "notify-natural-language");
5918 cupsArrayAdd(ra
, "notify-pull-method");
5919 cupsArrayAdd(ra
, "notify-recipient-uri");
5920 cupsArrayAdd(ra
, "notify-time-interval");
5921 cupsArrayAdd(ra
, "notify-user-data");
5924 cupsArrayAdd(ra
, value
);
5932 * 'create_subscription()' - Create a notification subscription.
5936 create_subscription(
5937 cupsd_client_t
*con
, /* I - Client connection */
5938 ipp_attribute_t
*uri
) /* I - Printer URI */
5940 http_status_t status
; /* Policy status */
5941 int i
; /* Looping var */
5942 ipp_attribute_t
*attr
; /* Current attribute */
5943 cups_ptype_t dtype
; /* Destination type (printer/class) */
5944 char scheme
[HTTP_MAX_URI
],
5945 /* Scheme portion of URI */
5946 userpass
[HTTP_MAX_URI
],
5947 /* Username portion of URI */
5949 /* Host portion of URI */
5950 resource
[HTTP_MAX_URI
];
5951 /* Resource portion of URI */
5952 int port
; /* Port portion of URI */
5953 cupsd_printer_t
*printer
; /* Printer/class */
5954 cupsd_job_t
*job
; /* Job */
5955 int jobid
; /* Job ID */
5956 cupsd_subscription_t
*sub
; /* Subscription object */
5957 const char *username
, /* requesting-user-name or
5958 authenticated username */
5959 *recipient
, /* notify-recipient-uri */
5960 *pullmethod
; /* notify-pull-method */
5961 ipp_attribute_t
*user_data
; /* notify-user-data */
5962 int interval
, /* notify-time-interval */
5963 lease
; /* notify-lease-duration */
5964 unsigned mask
; /* notify-events */
5965 ipp_attribute_t
*notify_events
,/* notify-events(-default) */
5966 *notify_lease
; /* notify-lease-duration(-default) */
5970 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
5972 if (attr
->group_tag
!= IPP_TAG_ZERO
)
5973 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "g%04x v%04x %s", attr
->group_tag
,
5974 attr
->value_tag
, attr
->name
);
5976 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "----SEP----");
5981 * Is the destination valid?
5984 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5985 "cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
5986 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
5988 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
5989 sizeof(scheme
), userpass
, sizeof(userpass
), host
,
5990 sizeof(host
), &port
, resource
, sizeof(resource
));
5992 if (!strcmp(resource
, "/"))
5994 dtype
= (cups_ptype_t
)0;
5997 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
5999 dtype
= (cups_ptype_t
)0;
6002 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6004 dtype
= CUPS_PRINTER_CLASS
;
6007 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6013 send_ipp_status(con
, IPP_NOT_FOUND
,
6014 _("The printer or class was not found."));
6024 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6027 send_http_error(con
, status
, printer
);
6031 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6033 send_http_error(con
, status
, NULL
);
6038 * Get the user that is requesting the subscription...
6041 username
= get_username(con
);
6044 * Find the first subscription group attribute; return if we have
6048 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
6049 if (attr
->group_tag
== IPP_TAG_SUBSCRIPTION
)
6054 send_ipp_status(con
, IPP_BAD_REQUEST
,
6055 _("No subscription attributes in request!"));
6060 * Process the subscription attributes in the request...
6063 con
->response
->request
.status
.status_code
= IPP_BAD_REQUEST
;
6071 lease
= DefaultLeaseDuration
;
6073 mask
= CUPSD_EVENT_NONE
;
6077 notify_events
= ippFindAttribute(printer
->attrs
, "notify-events-default",
6079 notify_lease
= ippFindAttribute(printer
->attrs
,
6080 "notify-lease-duration-default",
6084 lease
= notify_lease
->values
[0].integer
;
6088 notify_events
= NULL
;
6089 notify_lease
= NULL
;
6092 while (attr
&& attr
->group_tag
!= IPP_TAG_ZERO
)
6094 if (!strcmp(attr
->name
, "notify-recipient-uri") &&
6095 attr
->value_tag
== IPP_TAG_URI
)
6098 * Validate the recipient scheme against the ServerBin/notifier
6102 char notifier
[1024]; /* Notifier filename */
6105 recipient
= attr
->values
[0].string
.text
;
6107 if (httpSeparateURI(HTTP_URI_CODING_ALL
, recipient
,
6108 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
6109 host
, sizeof(host
), &port
,
6110 resource
, sizeof(resource
)) < HTTP_URI_OK
)
6112 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6113 _("Bad notify-recipient-uri URI \"%s\"!"), recipient
);
6114 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6115 "notify-status-code", IPP_URI_SCHEME
);
6119 snprintf(notifier
, sizeof(notifier
), "%s/notifier/%s", ServerBin
,
6121 if (access(notifier
, X_OK
))
6123 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6124 _("notify-recipient-uri URI \"%s\" uses unknown "
6125 "scheme!"), recipient
);
6126 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6127 "notify-status-code", IPP_URI_SCHEME
);
6131 if (!strcmp(scheme
, "rss") && !check_rss_recipient(recipient
))
6133 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6134 _("notify-recipient-uri URI \"%s\" is already used!"),
6136 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6137 "notify-status-code", IPP_ATTRIBUTES
);
6141 else if (!strcmp(attr
->name
, "notify-pull-method") &&
6142 attr
->value_tag
== IPP_TAG_KEYWORD
)
6144 pullmethod
= attr
->values
[0].string
.text
;
6146 if (strcmp(pullmethod
, "ippget"))
6148 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
6149 _("Bad notify-pull-method \"%s\"!"), pullmethod
);
6150 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_ENUM
,
6151 "notify-status-code", IPP_ATTRIBUTES
);
6155 else if (!strcmp(attr
->name
, "notify-charset") &&
6156 attr
->value_tag
== IPP_TAG_CHARSET
&&
6157 strcmp(attr
->values
[0].string
.text
, "us-ascii") &&
6158 strcmp(attr
->values
[0].string
.text
, "utf-8"))
6160 send_ipp_status(con
, IPP_CHARSET
,
6161 _("Character set \"%s\" not supported!"),
6162 attr
->values
[0].string
.text
);
6165 else if (!strcmp(attr
->name
, "notify-natural-language") &&
6166 (attr
->value_tag
!= IPP_TAG_LANGUAGE
||
6167 strcmp(attr
->values
[0].string
.text
, DefaultLanguage
)))
6169 send_ipp_status(con
, IPP_CHARSET
,
6170 _("Language \"%s\" not supported!"),
6171 attr
->values
[0].string
.text
);
6174 else if (!strcmp(attr
->name
, "notify-user-data") &&
6175 attr
->value_tag
== IPP_TAG_STRING
)
6177 if (attr
->num_values
> 1 || attr
->values
[0].unknown
.length
> 63)
6179 send_ipp_status(con
, IPP_REQUEST_VALUE
,
6180 _("The notify-user-data value is too large "
6181 "(%d > 63 octets)!"),
6182 attr
->values
[0].unknown
.length
);
6188 else if (!strcmp(attr
->name
, "notify-events") &&
6189 attr
->value_tag
== IPP_TAG_KEYWORD
)
6190 notify_events
= attr
;
6191 else if (!strcmp(attr
->name
, "notify-lease-duration") &&
6192 attr
->value_tag
== IPP_TAG_INTEGER
)
6193 lease
= attr
->values
[0].integer
;
6194 else if (!strcmp(attr
->name
, "notify-time-interval") &&
6195 attr
->value_tag
== IPP_TAG_INTEGER
)
6196 interval
= attr
->values
[0].integer
;
6197 else if (!strcmp(attr
->name
, "notify-job-id") &&
6198 attr
->value_tag
== IPP_TAG_INTEGER
)
6199 jobid
= attr
->values
[0].integer
;
6206 for (i
= 0; i
< notify_events
->num_values
; i
++)
6207 mask
|= cupsdEventValue(notify_events
->values
[i
].string
.text
);
6211 cupsdLogMessage(CUPSD_LOG_DEBUG
, "recipient=\"%s\"", recipient
);
6213 cupsdLogMessage(CUPSD_LOG_DEBUG
, "pullmethod=\"%s\"", pullmethod
);
6214 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-lease-duration=%d", lease
);
6215 cupsdLogMessage(CUPSD_LOG_DEBUG
, "notify-time-interval=%d", interval
);
6217 if (!recipient
&& !pullmethod
)
6220 if (mask
== CUPSD_EVENT_NONE
)
6223 mask
= CUPSD_EVENT_JOB_COMPLETED
;
6225 mask
= CUPSD_EVENT_PRINTER_STATE_CHANGED
;
6228 send_ipp_status(con
, IPP_BAD_REQUEST
,
6229 _("notify-events not specified!"));
6234 if (MaxLeaseDuration
&& (lease
== 0 || lease
> MaxLeaseDuration
))
6236 cupsdLogMessage(CUPSD_LOG_INFO
,
6237 "create_subscription: Limiting notify-lease-duration to "
6240 lease
= MaxLeaseDuration
;
6245 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6247 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job %d not found!"), jobid
);
6254 if ((sub
= cupsdAddSubscription(mask
, printer
, job
, recipient
, 0)) == NULL
)
6256 send_ipp_status(con
, IPP_TOO_MANY_SUBSCRIPTIONS
,
6257 _("There are too many subscriptions."));
6262 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for job %d",
6265 cupsdLogMessage(CUPSD_LOG_DEBUG
,
6266 "Added subscription %d for printer \"%s\"",
6267 sub
->id
, printer
->name
);
6269 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added subscription %d for server",
6272 sub
->interval
= interval
;
6274 sub
->expire
= lease
? time(NULL
) + lease
: 0;
6276 cupsdSetString(&sub
->owner
, username
);
6280 sub
->user_data_len
= user_data
->values
[0].unknown
.length
;
6281 memcpy(sub
->user_data
, user_data
->values
[0].unknown
.data
,
6282 sub
->user_data_len
);
6285 ippAddSeparator(con
->response
);
6286 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
6287 "notify-subscription-id", sub
->id
);
6289 con
->response
->request
.status
.status_code
= IPP_OK
;
6295 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
6300 * 'delete_printer()' - Remove a printer or class from the system.
6304 delete_printer(cupsd_client_t
*con
, /* I - Client connection */
6305 ipp_attribute_t
*uri
) /* I - URI of printer or class */
6307 http_status_t status
; /* Policy status */
6308 cups_ptype_t dtype
; /* Destination type (printer/class) */
6309 cupsd_printer_t
*printer
; /* Printer/class */
6310 char filename
[1024]; /* Script/PPD filename */
6313 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "delete_printer(%p[%d], %s)", con
,
6314 con
->http
.fd
, uri
->values
[0].string
.text
);
6317 * Do we have a valid URI?
6320 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
6326 send_ipp_status(con
, IPP_NOT_FOUND
,
6327 _("The printer or class was not found."));
6335 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6337 send_http_error(con
, status
, NULL
);
6342 * Remove old jobs...
6345 cupsdCancelJobs(printer
->name
, NULL
, 1);
6348 * Remove old subscriptions and send a "deleted printer" event...
6351 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, printer
, NULL
,
6352 "%s \"%s\" deleted by \"%s\".",
6353 (dtype
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
6354 printer
->name
, get_username(con
));
6356 cupsdExpireSubscriptions(printer
, NULL
);
6359 * Remove any old PPD or script files...
6362 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
6366 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
6370 snprintf(filename
, sizeof(filename
), "%s/%s.ipp", CacheDir
, printer
->name
);
6375 * Unregister color profiles...
6378 apple_unregister_profiles(printer
);
6379 #endif /* __APPLE__ */
6381 if (dtype
& CUPS_PRINTER_CLASS
)
6383 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" deleted by \"%s\".",
6384 printer
->name
, get_username(con
));
6386 cupsdDeletePrinter(printer
, 0);
6387 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
6391 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" deleted by \"%s\".",
6392 printer
->name
, get_username(con
));
6394 cupsdDeletePrinter(printer
, 0);
6395 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
6398 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
6401 * Return with no errors...
6404 con
->response
->request
.status
.status_code
= IPP_OK
;
6409 * 'get_default()' - Get the default destination.
6413 get_default(cupsd_client_t
*con
) /* I - Client connection */
6415 http_status_t status
; /* Policy status */
6416 cups_array_t
*ra
; /* Requested attributes array */
6419 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_default(%p[%d])", con
, con
->http
.fd
);
6425 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6427 send_http_error(con
, status
, NULL
);
6433 ra
= create_requested_array(con
->request
);
6435 copy_printer_attrs(con
, DefaultPrinter
, ra
);
6437 cupsArrayDelete(ra
);
6439 con
->response
->request
.status
.status_code
= IPP_OK
;
6442 send_ipp_status(con
, IPP_NOT_FOUND
, _("No default printer"));
6447 * 'get_devices()' - Get the list of available devices on the local system.
6451 get_devices(cupsd_client_t
*con
) /* I - Client connection */
6453 http_status_t status
; /* Policy status */
6454 ipp_attribute_t
*limit
, /* limit attribute */
6455 *timeout
, /* timeout attribute */
6456 *requested
, /* requested-attributes attribute */
6457 *exclude
, /* exclude-schemes attribute */
6458 *include
; /* include-schemes attribute */
6459 char command
[1024], /* cups-deviced command */
6460 options
[2048], /* Options to pass to command */
6462 /* String for requested attributes */
6464 /* String for excluded schemes */
6466 /* String for included schemes */
6469 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_devices(%p[%d])", con
, con
->http
.fd
);
6475 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6477 send_http_error(con
, status
, NULL
);
6482 * Run cups-deviced command with the given options...
6485 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
6486 timeout
= ippFindAttribute(con
->request
, "timeout", IPP_TAG_INTEGER
);
6487 requested
= ippFindAttribute(con
->request
, "requested-attributes",
6489 exclude
= ippFindAttribute(con
->request
, "exclude-schemes", IPP_TAG_NAME
);
6490 include
= ippFindAttribute(con
->request
, "include-schemes", IPP_TAG_NAME
);
6493 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
6495 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
6498 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
6500 exclude_str
[0] = '\0';
6503 url_encode_attr(include
, include_str
, sizeof(include_str
));
6505 include_str
[0] = '\0';
6507 snprintf(command
, sizeof(command
), "%s/daemon/cups-deviced", ServerBin
);
6508 snprintf(options
, sizeof(options
),
6509 "%d+%d+%d+%d+%s%s%s%s%s",
6510 con
->request
->request
.op
.request_id
,
6511 limit
? limit
->values
[0].integer
: 0,
6512 timeout
? timeout
->values
[0].integer
: 10,
6515 exclude_str
[0] ? "%20" : "", exclude_str
,
6516 include_str
[0] ? "%20" : "", include_str
);
6518 if (cupsdSendCommand(con
, command
, options
, 1))
6521 * Command started successfully, don't send an IPP response here...
6524 ippDelete(con
->response
);
6525 con
->response
= NULL
;
6530 * Command failed, return "internal error" so the user knows something
6534 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
6535 _("cups-deviced failed to execute."));
6541 * 'get_document()' - Get a copy of a job file.
6545 get_document(cupsd_client_t
*con
, /* I - Client connection */
6546 ipp_attribute_t
*uri
) /* I - Job URI */
6548 http_status_t status
; /* Policy status */
6549 ipp_attribute_t
*attr
; /* Current attribute */
6550 int jobid
; /* Job ID */
6551 int docnum
; /* Document number */
6552 cupsd_job_t
*job
; /* Current job */
6553 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6554 username
[HTTP_MAX_URI
], /* Username portion of URI */
6555 host
[HTTP_MAX_URI
], /* Host portion of URI */
6556 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6557 int port
; /* Port portion of URI */
6558 char filename
[1024], /* Filename for document */
6559 format
[1024]; /* Format for document */
6562 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_document(%p[%d], %s)", con
,
6563 con
->http
.fd
, uri
->values
[0].string
.text
);
6566 * See if we have a job URI or a printer URI...
6569 if (!strcmp(uri
->name
, "printer-uri"))
6572 * Got a printer URI; see if we also have a job-id attribute...
6575 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6576 IPP_TAG_INTEGER
)) == NULL
)
6578 send_ipp_status(con
, IPP_BAD_REQUEST
,
6579 _("Got a printer-uri attribute but no job-id!"));
6583 jobid
= attr
->values
[0].integer
;
6588 * Got a job URI; parse it to get the job ID...
6591 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6592 sizeof(scheme
), username
, sizeof(username
), host
,
6593 sizeof(host
), &port
, resource
, sizeof(resource
));
6595 if (strncmp(resource
, "/jobs/", 6))
6601 send_ipp_status(con
, IPP_BAD_REQUEST
,
6602 _("Bad job-uri attribute \"%s\"!"),
6603 uri
->values
[0].string
.text
);
6607 jobid
= atoi(resource
+ 6);
6611 * See if the job exists...
6614 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6617 * Nope - return a "not found" error...
6620 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6628 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6630 send_http_error(con
, status
, NULL
);
6635 * Get the document number...
6638 if ((attr
= ippFindAttribute(con
->request
, "document-number",
6639 IPP_TAG_INTEGER
)) == NULL
)
6641 send_ipp_status(con
, IPP_BAD_REQUEST
,
6642 _("Missing document-number attribute!"));
6646 if ((docnum
= attr
->values
[0].integer
) < 1 || docnum
> job
->num_files
||
6647 attr
->num_values
> 1)
6649 send_ipp_status(con
, IPP_NOT_FOUND
, _("Document %d not found in job %d."),
6654 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, jobid
,
6656 if ((con
->file
= open(filename
, O_RDONLY
)) == -1)
6658 cupsdLogMessage(CUPSD_LOG_ERROR
,
6659 "Unable to open document %d in job %d - %s", docnum
, jobid
,
6661 send_ipp_status(con
, IPP_NOT_FOUND
,
6662 _("Unable to open document %d in job %d!"), docnum
, jobid
);
6666 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
6670 snprintf(format
, sizeof(format
), "%s/%s", job
->filetypes
[docnum
- 1]->super
,
6671 job
->filetypes
[docnum
- 1]->type
);
6673 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
6675 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "document-number",
6677 if ((attr
= ippFindAttribute(job
->attrs
, "document-name",
6678 IPP_TAG_NAME
)) != NULL
)
6679 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name",
6680 NULL
, attr
->values
[0].string
.text
);
6685 * 'get_job_attrs()' - Get job attributes.
6689 get_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
6690 ipp_attribute_t
*uri
) /* I - Job URI */
6692 http_status_t status
; /* Policy status */
6693 ipp_attribute_t
*attr
; /* Current attribute */
6694 int jobid
; /* Job ID */
6695 cupsd_job_t
*job
; /* Current job */
6696 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
6697 username
[HTTP_MAX_URI
], /* Username portion of URI */
6698 host
[HTTP_MAX_URI
], /* Host portion of URI */
6699 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6700 int port
; /* Port portion of URI */
6701 cups_array_t
*ra
; /* Requested attributes array */
6704 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_job_attrs(%p[%d], %s)", con
,
6705 con
->http
.fd
, uri
->values
[0].string
.text
);
6708 * See if we have a job URI or a printer URI...
6711 if (!strcmp(uri
->name
, "printer-uri"))
6714 * Got a printer URI; see if we also have a job-id attribute...
6717 if ((attr
= ippFindAttribute(con
->request
, "job-id",
6718 IPP_TAG_INTEGER
)) == NULL
)
6720 send_ipp_status(con
, IPP_BAD_REQUEST
,
6721 _("Got a printer-uri attribute but no job-id!"));
6725 jobid
= attr
->values
[0].integer
;
6730 * Got a job URI; parse it to get the job ID...
6733 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6734 sizeof(scheme
), username
, sizeof(username
), host
,
6735 sizeof(host
), &port
, resource
, sizeof(resource
));
6737 if (strncmp(resource
, "/jobs/", 6))
6743 send_ipp_status(con
, IPP_BAD_REQUEST
,
6744 _("Bad job-uri attribute \"%s\"!"),
6745 uri
->values
[0].string
.text
);
6749 jobid
= atoi(resource
+ 6);
6753 * See if the job exists...
6756 if ((job
= cupsdFindJob(jobid
)) == NULL
)
6759 * Nope - return a "not found" error...
6762 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
6770 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6772 send_http_error(con
, status
, NULL
);
6777 * Copy attributes...
6782 ra
= create_requested_array(con
->request
);
6783 copy_job_attrs(con
, job
, ra
);
6784 cupsArrayDelete(ra
);
6786 con
->response
->request
.status
.status_code
= IPP_OK
;
6791 * 'get_jobs()' - Get a list of jobs for the specified printer.
6795 get_jobs(cupsd_client_t
*con
, /* I - Client connection */
6796 ipp_attribute_t
*uri
) /* I - Printer URI */
6798 http_status_t status
; /* Policy status */
6799 ipp_attribute_t
*attr
; /* Current attribute */
6800 const char *dest
; /* Destination */
6801 cups_ptype_t dtype
; /* Destination type (printer/class) */
6802 cups_ptype_t dmask
; /* Destination type mask */
6803 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
6804 username
[HTTP_MAX_URI
], /* Username portion of URI */
6805 host
[HTTP_MAX_URI
], /* Host portion of URI */
6806 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
6807 int port
; /* Port portion of URI */
6808 int completed
; /* Completed jobs? */
6809 int first_job_id
; /* First job ID */
6810 int limit
; /* Maximum number of jobs to return */
6811 int count
; /* Number of jobs that match */
6812 cupsd_job_t
*job
; /* Current job pointer */
6813 cupsd_printer_t
*printer
; /* Printer */
6814 cups_array_t
*list
; /* Which job list... */
6815 cups_array_t
*ra
; /* Requested attributes array */
6818 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs(%p[%d], %s)", con
, con
->http
.fd
,
6819 uri
->values
[0].string
.text
);
6822 * Is the destination valid?
6825 if (strcmp(uri
->name
, "printer-uri"))
6827 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No printer-uri in request!"));
6831 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
6832 sizeof(scheme
), username
, sizeof(username
), host
,
6833 sizeof(host
), &port
, resource
, sizeof(resource
));
6835 if (!strcmp(resource
, "/"))
6838 dtype
= (cups_ptype_t
)0;
6839 dmask
= (cups_ptype_t
)0;
6842 else if (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10)
6845 dtype
= (cups_ptype_t
)0;
6846 dmask
= CUPS_PRINTER_CLASS
;
6849 else if (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9)
6852 dtype
= CUPS_PRINTER_CLASS
;
6853 dmask
= CUPS_PRINTER_CLASS
;
6856 else if ((dest
= cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
,
6863 send_ipp_status(con
, IPP_NOT_FOUND
,
6864 _("The printer or class was not found."));
6869 dtype
&= CUPS_PRINTER_CLASS
;
6870 dmask
= CUPS_PRINTER_CLASS
;
6879 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
,
6882 send_http_error(con
, status
, printer
);
6886 else if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
6888 send_http_error(con
, status
, NULL
);
6893 * See if the "which-jobs" attribute have been specified...
6896 if ((attr
= ippFindAttribute(con
->request
, "which-jobs",
6897 IPP_TAG_KEYWORD
)) != NULL
&&
6898 !strcmp(attr
->values
[0].string
.text
, "completed"))
6903 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "all"))
6908 else if (attr
&& !strcmp(attr
->values
[0].string
.text
, "printing"))
6911 list
= PrintingJobs
;
6920 * See if they want to limit the number of jobs reported...
6923 if ((attr
= ippFindAttribute(con
->request
, "limit",
6924 IPP_TAG_INTEGER
)) != NULL
)
6925 limit
= attr
->values
[0].integer
;
6929 if ((attr
= ippFindAttribute(con
->request
, "first-job-id",
6930 IPP_TAG_INTEGER
)) != NULL
)
6931 first_job_id
= attr
->values
[0].integer
;
6936 * See if we only want to see jobs for a specific user...
6939 if ((attr
= ippFindAttribute(con
->request
, "my-jobs",
6940 IPP_TAG_BOOLEAN
)) != NULL
&&
6941 attr
->values
[0].boolean
)
6942 strlcpy(username
, get_username(con
), sizeof(username
));
6946 ra
= create_requested_array(con
->request
);
6949 * OK, build a list of jobs for this printer...
6952 for (count
= 0, job
= (cupsd_job_t
*)cupsArrayFirst(list
);
6953 (limit
<= 0 || count
< limit
) && job
;
6954 job
= (cupsd_job_t
*)cupsArrayNext(list
))
6957 * Filter out jobs that don't match...
6960 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: job->id = %d", job
->id
);
6962 if (!job
->dest
|| !job
->username
)
6965 if (!job
->dest
|| !job
->username
)
6968 if ((dest
&& strcmp(job
->dest
, dest
)) &&
6969 (!job
->printer
|| !dest
|| strcmp(job
->printer
->name
, dest
)))
6971 if ((job
->dtype
& dmask
) != dtype
&&
6972 (!job
->printer
|| (job
->printer
->type
& dmask
) != dtype
))
6974 if (completed
&& job
->state_value
<= IPP_JOB_STOPPED
)
6977 if (job
->id
< first_job_id
)
6985 if (username
[0] && strcasecmp(username
, job
->username
))
6989 ippAddSeparator(con
->response
);
6993 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_jobs: count = %d", count
);
6995 copy_job_attrs(con
, job
, ra
);
6998 cupsArrayDelete(ra
);
7000 con
->response
->request
.status
.status_code
= IPP_OK
;
7005 * 'get_notifications()' - Get events for a subscription.
7009 get_notifications(cupsd_client_t
*con
) /* I - Client connection */
7011 int i
, j
; /* Looping vars */
7012 http_status_t status
; /* Policy status */
7013 cupsd_subscription_t
*sub
; /* Subscription */
7014 ipp_attribute_t
*ids
, /* notify-subscription-ids */
7015 *sequences
; /* notify-sequence-numbers */
7016 int min_seq
; /* Minimum sequence number */
7017 int interval
; /* Poll interval */
7020 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_notifications(con=%p[%d])",
7024 * Get subscription attributes...
7027 ids
= ippFindAttribute(con
->request
, "notify-subscription-ids",
7029 sequences
= ippFindAttribute(con
->request
, "notify-sequence-numbers",
7034 send_ipp_status(con
, IPP_BAD_REQUEST
,
7035 _("Missing notify-subscription-ids attribute!"));
7040 * Are the subscription IDs valid?
7043 for (i
= 0, interval
= 60; i
< ids
->num_values
; i
++)
7045 if ((sub
= cupsdFindSubscription(ids
->values
[i
].integer
)) == NULL
)
7048 * Bad subscription ID...
7051 send_ipp_status(con
, IPP_NOT_FOUND
,
7052 _("notify-subscription-id %d no good!"),
7053 ids
->values
[i
].integer
);
7061 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7063 con
, sub
->owner
)) != HTTP_OK
)
7065 send_http_error(con
, status
, sub
->dest
);
7070 * Check the subscription type and update the interval accordingly.
7073 if (sub
->job
&& sub
->job
->state_value
== IPP_JOB_PROCESSING
&&
7076 else if (sub
->job
&& sub
->job
->state_value
>= IPP_JOB_STOPPED
)
7078 else if (sub
->dest
&& sub
->dest
->state
== IPP_PRINTER_PROCESSING
&&
7084 * Tell the client to poll again in N seconds...
7088 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7089 "notify-get-interval", interval
);
7091 ippAddInteger(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
7092 "printer-up-time", time(NULL
));
7095 * Copy the subscription event attributes to the response.
7098 con
->response
->request
.status
.status_code
=
7099 interval
? IPP_OK
: IPP_OK_EVENTS_COMPLETE
;
7101 for (i
= 0; i
< ids
->num_values
; i
++)
7104 * Get the subscription and sequence number...
7107 sub
= cupsdFindSubscription(ids
->values
[i
].integer
);
7109 if (sequences
&& i
< sequences
->num_values
)
7110 min_seq
= sequences
->values
[i
].integer
;
7115 * If we don't have any new events, nothing to do here...
7118 if (min_seq
> (sub
->first_event_id
+ sub
->num_events
))
7122 * Otherwise copy all of the new events...
7125 if (sub
->first_event_id
> min_seq
)
7128 j
= min_seq
- sub
->first_event_id
;
7130 for (; j
< sub
->num_events
; j
++)
7132 ippAddSeparator(con
->response
);
7134 copy_attrs(con
->response
, sub
->events
[j
]->attrs
, NULL
,
7135 IPP_TAG_EVENT_NOTIFICATION
, 0);
7142 * 'get_ppd()' - Get a named PPD from the local system.
7146 get_ppd(cupsd_client_t
*con
, /* I - Client connection */
7147 ipp_attribute_t
*uri
) /* I - Printer URI or PPD name */
7149 http_status_t status
; /* Policy status */
7150 cupsd_printer_t
*dest
; /* Destination */
7151 cups_ptype_t dtype
; /* Destination type */
7154 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppd(%p[%d], %p[%s=%s])", con
,
7155 con
->http
.fd
, uri
, uri
->name
, uri
->values
[0].string
.text
);
7157 if (!strcmp(uri
->name
, "ppd-name"))
7160 * Return a PPD file from cups-driverd...
7163 char command
[1024], /* cups-driverd command */
7164 options
[1024], /* Options to pass to command */
7165 ppd_name
[1024]; /* ppd-name */
7172 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7174 send_http_error(con
, status
, NULL
);
7179 * Run cups-driverd command with the given options...
7182 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7183 url_encode_string(uri
->values
[0].string
.text
, ppd_name
, sizeof(ppd_name
));
7184 snprintf(options
, sizeof(options
), "get+%d+%s",
7185 con
->request
->request
.op
.request_id
, ppd_name
);
7187 if (cupsdSendCommand(con
, command
, options
, 0))
7190 * Command started successfully, don't send an IPP response here...
7193 ippDelete(con
->response
);
7194 con
->response
= NULL
;
7199 * Command failed, return "internal error" so the user knows something
7203 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7204 _("cups-driverd failed to execute."));
7207 else if (!strcmp(uri
->name
, "printer-uri") &&
7208 cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &dest
))
7210 int i
; /* Looping var */
7211 char filename
[1024]; /* PPD filename */
7218 if ((status
= cupsdCheckPolicy(dest
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7220 send_http_error(con
, status
, dest
);
7225 * See if we need the PPD for a class or remote printer...
7228 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7231 if ((dtype
& CUPS_PRINTER_REMOTE
) && access(filename
, 0))
7233 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7234 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7235 "printer-uri", NULL
, dest
->uri
);
7238 else if (dtype
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
7240 for (i
= 0; i
< dest
->num_printers
; i
++)
7241 if (!(dest
->printers
[i
]->type
&
7242 (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
7244 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
7245 dest
->printers
[i
]->name
);
7247 if (!access(filename
, 0))
7251 if (i
< dest
->num_printers
)
7252 dest
= dest
->printers
[i
];
7255 con
->response
->request
.status
.status_code
= CUPS_SEE_OTHER
;
7256 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
7257 "printer-uri", NULL
, dest
->printers
[0]->uri
);
7263 * Found the printer with the PPD file, now see if there is one...
7266 if ((con
->file
= open(filename
, O_RDONLY
)) < 0)
7268 send_ipp_status(con
, IPP_NOT_FOUND
,
7269 _("The PPD file \"%s\" could not be opened: %s"),
7270 uri
->values
[0].string
.text
, strerror(errno
));
7274 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
7278 con
->response
->request
.status
.status_code
= IPP_OK
;
7281 send_ipp_status(con
, IPP_NOT_FOUND
,
7282 _("The PPD file \"%s\" could not be found."),
7283 uri
->values
[0].string
.text
);
7288 * 'get_ppds()' - Get the list of PPD files on the local system.
7292 get_ppds(cupsd_client_t
*con
) /* I - Client connection */
7294 http_status_t status
; /* Policy status */
7295 ipp_attribute_t
*limit
, /* Limit attribute */
7296 *device
, /* ppd-device-id attribute */
7297 *language
, /* ppd-natural-language attribute */
7298 *make
, /* ppd-make attribute */
7299 *model
, /* ppd-make-and-model attribute */
7300 *model_number
, /* ppd-model-number attribute */
7301 *product
, /* ppd-product attribute */
7302 *psversion
, /* ppd-psverion attribute */
7303 *type
, /* ppd-type attribute */
7304 *requested
, /* requested-attributes attribute */
7305 *exclude
, /* exclude-schemes attribute */
7306 *include
; /* include-schemes attribute */
7307 char command
[1024], /* cups-driverd command */
7308 options
[4096], /* Options to pass to command */
7309 device_str
[256],/* Escaped ppd-device-id string */
7311 /* Escaped ppd-natural-language */
7312 make_str
[256], /* Escaped ppd-make string */
7313 model_str
[256], /* Escaped ppd-make-and-model string */
7314 model_number_str
[256],
7315 /* ppd-model-number string */
7317 /* Escaped ppd-product string */
7319 /* Escaped ppd-psversion string */
7320 type_str
[256], /* Escaped ppd-type string */
7322 /* String for requested attributes */
7324 /* String for excluded schemes */
7326 /* String for included schemes */
7329 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_ppds(%p[%d])", con
, con
->http
.fd
);
7335 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7337 send_http_error(con
, status
, NULL
);
7342 * Run cups-driverd command with the given options...
7345 limit
= ippFindAttribute(con
->request
, "limit", IPP_TAG_INTEGER
);
7346 device
= ippFindAttribute(con
->request
, "ppd-device-id", IPP_TAG_TEXT
);
7347 language
= ippFindAttribute(con
->request
, "ppd-natural-language",
7349 make
= ippFindAttribute(con
->request
, "ppd-make", IPP_TAG_TEXT
);
7350 model
= ippFindAttribute(con
->request
, "ppd-make-and-model",
7352 model_number
= ippFindAttribute(con
->request
, "ppd-model-number",
7354 product
= ippFindAttribute(con
->request
, "ppd-product", IPP_TAG_TEXT
);
7355 psversion
= ippFindAttribute(con
->request
, "ppd-psversion", IPP_TAG_TEXT
);
7356 type
= ippFindAttribute(con
->request
, "ppd-type", IPP_TAG_KEYWORD
);
7357 requested
= ippFindAttribute(con
->request
, "requested-attributes",
7359 exclude
= ippFindAttribute(con
->request
, "exclude-schemes",
7361 include
= ippFindAttribute(con
->request
, "include-schemes",
7365 url_encode_attr(requested
, requested_str
, sizeof(requested_str
));
7367 strlcpy(requested_str
, "requested-attributes=all", sizeof(requested_str
));
7370 url_encode_attr(device
, device_str
, sizeof(device_str
));
7372 device_str
[0] = '\0';
7375 url_encode_attr(language
, language_str
, sizeof(language_str
));
7377 language_str
[0] = '\0';
7380 url_encode_attr(make
, make_str
, sizeof(make_str
));
7385 url_encode_attr(model
, model_str
, sizeof(model_str
));
7387 model_str
[0] = '\0';
7390 snprintf(model_number_str
, sizeof(model_number_str
), "ppd-model-number=%d",
7391 model_number
->values
[0].integer
);
7393 model_number_str
[0] = '\0';
7396 url_encode_attr(product
, product_str
, sizeof(product_str
));
7398 product_str
[0] = '\0';
7401 url_encode_attr(psversion
, psversion_str
, sizeof(psversion_str
));
7403 psversion_str
[0] = '\0';
7406 url_encode_attr(type
, type_str
, sizeof(type_str
));
7411 url_encode_attr(exclude
, exclude_str
, sizeof(exclude_str
));
7413 exclude_str
[0] = '\0';
7416 url_encode_attr(include
, include_str
, sizeof(include_str
));
7418 include_str
[0] = '\0';
7420 snprintf(command
, sizeof(command
), "%s/daemon/cups-driverd", ServerBin
);
7421 snprintf(options
, sizeof(options
),
7422 "list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
7423 con
->request
->request
.op
.request_id
,
7424 limit
? limit
->values
[0].integer
: 0,
7426 device
? "%20" : "", device_str
,
7427 language
? "%20" : "", language_str
,
7428 make
? "%20" : "", make_str
,
7429 model
? "%20" : "", model_str
,
7430 model_number
? "%20" : "", model_number_str
,
7431 product
? "%20" : "", product_str
,
7432 psversion
? "%20" : "", psversion_str
,
7433 type
? "%20" : "", type_str
,
7434 exclude_str
[0] ? "%20" : "", exclude_str
,
7435 include_str
[0] ? "%20" : "", include_str
);
7437 if (cupsdSendCommand(con
, command
, options
, 0))
7440 * Command started successfully, don't send an IPP response here...
7443 ippDelete(con
->response
);
7444 con
->response
= NULL
;
7449 * Command failed, return "internal error" so the user knows something
7453 send_ipp_status(con
, IPP_INTERNAL_ERROR
,
7454 _("cups-driverd failed to execute."));
7460 * 'get_printer_attrs()' - Get printer attributes.
7464 get_printer_attrs(cupsd_client_t
*con
, /* I - Client connection */
7465 ipp_attribute_t
*uri
) /* I - Printer URI */
7467 http_status_t status
; /* Policy status */
7468 cups_ptype_t dtype
; /* Destination type (printer/class) */
7469 cupsd_printer_t
*printer
; /* Printer/class */
7470 cups_array_t
*ra
; /* Requested attributes array */
7473 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printer_attrs(%p[%d], %s)", con
,
7474 con
->http
.fd
, uri
->values
[0].string
.text
);
7477 * Is the destination valid?
7480 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7486 send_ipp_status(con
, IPP_NOT_FOUND
,
7487 _("The printer or class was not found."));
7495 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
7497 send_http_error(con
, status
, printer
);
7502 * Send the attributes...
7505 ra
= create_requested_array(con
->request
);
7507 copy_printer_attrs(con
, printer
, ra
);
7509 cupsArrayDelete(ra
);
7511 con
->response
->request
.status
.status_code
= IPP_OK
;
7516 * 'get_printers()' - Get a list of printers or classes.
7520 get_printers(cupsd_client_t
*con
, /* I - Client connection */
7521 int type
) /* I - 0 or CUPS_PRINTER_CLASS */
7523 http_status_t status
; /* Policy status */
7524 ipp_attribute_t
*attr
; /* Current attribute */
7525 int limit
; /* Max number of printers to return */
7526 int count
; /* Number of printers that match */
7527 cupsd_printer_t
*printer
; /* Current printer pointer */
7528 int printer_type
, /* printer-type attribute */
7529 printer_mask
; /* printer-type-mask attribute */
7530 char *location
; /* Location string */
7531 const char *username
; /* Current user */
7532 char *first_printer_name
; /* first-printer-name attribute */
7533 cups_array_t
*ra
; /* Requested attributes array */
7536 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "get_printers(%p[%d], %x)", con
,
7537 con
->http
.fd
, type
);
7543 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
7545 send_http_error(con
, status
, NULL
);
7550 * Check for printers...
7553 if (!Printers
|| !cupsArrayCount(Printers
))
7555 send_ipp_status(con
, IPP_NOT_FOUND
, _("No destinations added."));
7560 * See if they want to limit the number of printers reported...
7563 if ((attr
= ippFindAttribute(con
->request
, "limit",
7564 IPP_TAG_INTEGER
)) != NULL
)
7565 limit
= attr
->values
[0].integer
;
7569 if ((attr
= ippFindAttribute(con
->request
, "first-printer-name",
7570 IPP_TAG_NAME
)) != NULL
)
7571 first_printer_name
= attr
->values
[0].string
.text
;
7573 first_printer_name
= NULL
;
7576 * Support filtering...
7579 if ((attr
= ippFindAttribute(con
->request
, "printer-type",
7580 IPP_TAG_ENUM
)) != NULL
)
7581 printer_type
= attr
->values
[0].integer
;
7585 if ((attr
= ippFindAttribute(con
->request
, "printer-type-mask",
7586 IPP_TAG_ENUM
)) != NULL
)
7587 printer_mask
= attr
->values
[0].integer
;
7591 if ((attr
= ippFindAttribute(con
->request
, "printer-location",
7592 IPP_TAG_TEXT
)) != NULL
)
7593 location
= attr
->values
[0].string
.text
;
7597 if (con
->username
[0])
7598 username
= con
->username
;
7599 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7600 IPP_TAG_NAME
)) != NULL
)
7601 username
= attr
->values
[0].string
.text
;
7605 ra
= create_requested_array(con
->request
);
7608 * OK, build a list of printers for this printer...
7611 if (first_printer_name
)
7613 if ((printer
= cupsdFindDest(first_printer_name
)) == NULL
)
7614 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7617 printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
7620 count
< limit
&& printer
;
7621 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
7623 if ((!type
|| (printer
->type
& CUPS_PRINTER_CLASS
) == type
) &&
7624 (printer
->type
& printer_mask
) == printer_type
&&
7626 (printer
->location
&& !strcasecmp(printer
->location
, location
))))
7629 * If HideImplicitMembers is enabled, see if this printer or class
7630 * is a member of an implicit class...
7633 if (ImplicitClasses
&& HideImplicitMembers
&&
7634 printer
->in_implicit_class
)
7638 * If a username is specified, see if it is allowed or denied
7642 if (!(printer
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
7643 printer
->num_users
&& username
&& !user_allowed(printer
, username
))
7647 * Add the group separator as needed...
7651 ippAddSeparator(con
->response
);
7656 * Send the attributes...
7659 copy_printer_attrs(con
, printer
, ra
);
7663 cupsArrayDelete(ra
);
7665 con
->response
->request
.status
.status_code
= IPP_OK
;
7670 * 'get_subscription_attrs()' - Get subscription attributes.
7674 get_subscription_attrs(
7675 cupsd_client_t
*con
, /* I - Client connection */
7676 int sub_id
) /* I - Subscription ID */
7678 http_status_t status
; /* Policy status */
7679 cupsd_subscription_t
*sub
; /* Subscription */
7680 cups_array_t
*ra
; /* Requested attributes array */
7683 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7684 "get_subscription_attrs(con=%p[%d], sub_id=%d)",
7685 con
, con
->http
.fd
, sub_id
);
7688 * Is the subscription ID valid?
7691 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
7694 * Bad subscription ID...
7697 send_ipp_status(con
, IPP_NOT_FOUND
,
7698 _("notify-subscription-id %d no good!"), sub_id
);
7706 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
7708 con
, sub
->owner
)) != HTTP_OK
)
7710 send_http_error(con
, status
, sub
->dest
);
7715 * Copy the subscription attributes to the response using the
7716 * requested-attributes attribute that may be provided by the client.
7719 ra
= create_requested_array(con
->request
);
7721 copy_subscription_attrs(con
, sub
, ra
);
7723 cupsArrayDelete(ra
);
7725 con
->response
->request
.status
.status_code
= IPP_OK
;
7730 * 'get_subscriptions()' - Get subscriptions.
7734 get_subscriptions(cupsd_client_t
*con
, /* I - Client connection */
7735 ipp_attribute_t
*uri
) /* I - Printer/job URI */
7737 http_status_t status
; /* Policy status */
7738 int count
; /* Number of subscriptions */
7739 int limit
; /* Limit */
7740 cupsd_subscription_t
*sub
; /* Subscription */
7741 cups_array_t
*ra
; /* Requested attributes array */
7742 ipp_attribute_t
*attr
; /* Attribute */
7743 cups_ptype_t dtype
; /* Destination type (printer/class) */
7744 char scheme
[HTTP_MAX_URI
],
7745 /* Scheme portion of URI */
7746 username
[HTTP_MAX_URI
],
7747 /* Username portion of URI */
7749 /* Host portion of URI */
7750 resource
[HTTP_MAX_URI
];
7751 /* Resource portion of URI */
7752 int port
; /* Port portion of URI */
7753 cupsd_job_t
*job
; /* Job pointer */
7754 cupsd_printer_t
*printer
; /* Printer */
7757 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
7758 "get_subscriptions(con=%p[%d], uri=%s)",
7759 con
, con
->http
.fd
, uri
->values
[0].string
.text
);
7762 * Is the destination valid?
7765 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7766 sizeof(scheme
), username
, sizeof(username
), host
,
7767 sizeof(host
), &port
, resource
, sizeof(resource
));
7769 if (!strcmp(resource
, "/") ||
7770 (!strncmp(resource
, "/jobs", 5) && strlen(resource
) <= 6) ||
7771 (!strncmp(resource
, "/printers", 9) && strlen(resource
) <= 10) ||
7772 (!strncmp(resource
, "/classes", 8) && strlen(resource
) <= 9))
7777 else if (!strncmp(resource
, "/jobs/", 6) && resource
[6])
7780 job
= cupsdFindJob(atoi(resource
+ 6));
7784 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%s does not exist!"),
7789 else if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
7795 send_ipp_status(con
, IPP_NOT_FOUND
,
7796 _("The printer or class was not found."));
7799 else if ((attr
= ippFindAttribute(con
->request
, "notify-job-id",
7800 IPP_TAG_INTEGER
)) != NULL
)
7802 job
= cupsdFindJob(attr
->values
[0].integer
);
7806 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"),
7807 attr
->values
[0].integer
);
7818 if ((status
= cupsdCheckPolicy(printer
? printer
->op_policy_ptr
:
7820 con
, NULL
)) != HTTP_OK
)
7822 send_http_error(con
, status
, printer
);
7827 * Copy the subscription attributes to the response using the
7828 * requested-attributes attribute that may be provided by the client.
7831 ra
= create_requested_array(con
->request
);
7833 if ((attr
= ippFindAttribute(con
->request
, "limit",
7834 IPP_TAG_INTEGER
)) != NULL
)
7835 limit
= attr
->values
[0].integer
;
7840 * See if we only want to see subscriptions for a specific user...
7843 if ((attr
= ippFindAttribute(con
->request
, "my-subscriptions",
7844 IPP_TAG_BOOLEAN
)) != NULL
&&
7845 attr
->values
[0].boolean
)
7846 strlcpy(username
, get_username(con
), sizeof(username
));
7850 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), count
= 0;
7852 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
7853 if ((!printer
|| sub
->dest
== printer
) && (!job
|| sub
->job
== job
) &&
7854 (!username
[0] || !strcasecmp(username
, sub
->owner
)))
7856 ippAddSeparator(con
->response
);
7857 copy_subscription_attrs(con
, sub
, ra
);
7860 if (limit
&& count
>= limit
)
7864 cupsArrayDelete(ra
);
7867 con
->response
->request
.status
.status_code
= IPP_OK
;
7869 send_ipp_status(con
, IPP_NOT_FOUND
, _("No subscriptions found."));
7874 * 'get_username()' - Get the username associated with a request.
7877 static const char * /* O - Username */
7878 get_username(cupsd_client_t
*con
) /* I - Connection */
7880 ipp_attribute_t
*attr
; /* Attribute */
7883 if (con
->username
[0])
7884 return (con
->username
);
7885 else if ((attr
= ippFindAttribute(con
->request
, "requesting-user-name",
7886 IPP_TAG_NAME
)) != NULL
)
7887 return (attr
->values
[0].string
.text
);
7889 return ("anonymous");
7894 * 'hold_job()' - Hold a print job.
7898 hold_job(cupsd_client_t
*con
, /* I - Client connection */
7899 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
7901 ipp_attribute_t
*attr
, /* Current job-hold-until */
7902 *newattr
; /* New job-hold-until */
7903 int jobid
; /* Job ID */
7904 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
7905 username
[HTTP_MAX_URI
], /* Username portion of URI */
7906 host
[HTTP_MAX_URI
], /* Host portion of URI */
7907 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
7908 int port
; /* Port portion of URI */
7909 cupsd_job_t
*job
; /* Job information */
7912 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_job(%p[%d], %s)", con
, con
->http
.fd
,
7913 uri
->values
[0].string
.text
);
7916 * See if we have a job URI or a printer URI...
7919 if (!strcmp(uri
->name
, "printer-uri"))
7922 * Got a printer URI; see if we also have a job-id attribute...
7925 if ((attr
= ippFindAttribute(con
->request
, "job-id",
7926 IPP_TAG_INTEGER
)) == NULL
)
7928 send_ipp_status(con
, IPP_BAD_REQUEST
,
7929 _("Got a printer-uri attribute but no job-id!"));
7933 jobid
= attr
->values
[0].integer
;
7938 * Got a job URI; parse it to get the job ID...
7941 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
7942 sizeof(scheme
), username
, sizeof(username
), host
,
7943 sizeof(host
), &port
, resource
, sizeof(resource
));
7945 if (strncmp(resource
, "/jobs/", 6))
7951 send_ipp_status(con
, IPP_BAD_REQUEST
,
7952 _("Bad job-uri attribute \"%s\"!"),
7953 uri
->values
[0].string
.text
);
7957 jobid
= atoi(resource
+ 6);
7961 * See if the job exists...
7964 if ((job
= cupsdFindJob(jobid
)) == NULL
)
7967 * Nope - return a "not found" error...
7970 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
7975 * See if the job is owned by the requesting user...
7978 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
7980 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
7985 * Hold the job and return...
7990 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
7991 "Job held by user.");
7993 if ((newattr
= ippFindAttribute(con
->request
, "job-hold-until",
7994 IPP_TAG_KEYWORD
)) == NULL
)
7995 newattr
= ippFindAttribute(con
->request
, "job-hold-until", IPP_TAG_NAME
);
7997 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
7998 IPP_TAG_KEYWORD
)) == NULL
)
7999 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
8004 * Free the old hold value and copy the new one over...
8007 _cupsStrFree(attr
->values
[0].string
.text
);
8011 attr
->value_tag
= newattr
->value_tag
;
8012 attr
->values
[0].string
.text
=
8013 _cupsStrRetain(newattr
->values
[0].string
.text
);
8017 attr
->value_tag
= IPP_TAG_KEYWORD
;
8018 attr
->values
[0].string
.text
= _cupsStrAlloc("indefinite");
8022 * Hold job until specified time...
8025 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
8027 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
8028 "Job job-hold-until value changed by user.");
8031 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Held by \"%s\".", username
);
8033 con
->response
->request
.status
.status_code
= IPP_OK
;
8038 * 'hold_new_jobs()' - Hold pending/new jobs on a printer or class.
8042 hold_new_jobs(cupsd_client_t
*con
, /* I - Connection */
8043 ipp_attribute_t
*uri
) /* I - Printer URI */
8045 http_status_t status
; /* Policy status */
8046 cups_ptype_t dtype
; /* Destination type (printer/class) */
8047 cupsd_printer_t
*printer
; /* Printer data */
8050 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "hold_new_jobs(%p[%d], %s)", con
,
8051 con
->http
.fd
, uri
->values
[0].string
.text
);
8054 * Is the destination valid?
8057 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8063 send_ipp_status(con
, IPP_NOT_FOUND
,
8064 _("The printer or class was not found."));
8072 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8074 send_http_error(con
, status
, printer
);
8079 * Hold pending/new jobs sent to the printer...
8082 printer
->holding_new_jobs
= 1;
8084 cupsdSetPrinterReasons(printer
, "+hold-new-jobs");
8085 cupsdAddPrinterHistory(printer
);
8087 if (dtype
& CUPS_PRINTER_CLASS
)
8088 cupsdLogMessage(CUPSD_LOG_INFO
,
8089 "Class \"%s\" now holding pending/new jobs (\"%s\").",
8090 printer
->name
, get_username(con
));
8092 cupsdLogMessage(CUPSD_LOG_INFO
,
8093 "Printer \"%s\" now holding pending/new jobs (\"%s\").",
8094 printer
->name
, get_username(con
));
8097 * Everything was ok, so return OK status...
8100 con
->response
->request
.status
.status_code
= IPP_OK
;
8105 * 'move_job()' - Move a job to a new destination.
8109 move_job(cupsd_client_t
*con
, /* I - Client connection */
8110 ipp_attribute_t
*uri
) /* I - Job URI */
8112 http_status_t status
; /* Policy status */
8113 ipp_attribute_t
*attr
; /* Current attribute */
8114 int jobid
; /* Job ID */
8115 cupsd_job_t
*job
; /* Current job */
8116 const char *src
; /* Source printer/class */
8117 cups_ptype_t stype
, /* Source type (printer or class) */
8118 dtype
; /* Destination type (printer/class) */
8119 char scheme
[HTTP_MAX_URI
], /* Scheme portion of URI */
8120 username
[HTTP_MAX_URI
], /* Username portion of URI */
8121 host
[HTTP_MAX_URI
], /* Host portion of URI */
8122 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
8123 int port
; /* Port portion of URI */
8124 cupsd_printer_t
*sprinter
, /* Source printer */
8125 *dprinter
; /* Destination printer */
8128 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "move_job(%p[%d], %s)", con
, con
->http
.fd
,
8129 uri
->values
[0].string
.text
);
8132 * Get the new printer or class...
8135 if ((attr
= ippFindAttribute(con
->request
, "job-printer-uri",
8136 IPP_TAG_URI
)) == NULL
)
8139 * Need job-printer-uri...
8142 send_ipp_status(con
, IPP_BAD_REQUEST
,
8143 _("job-printer-uri attribute missing!"));
8147 if (!cupsdValidateDest(attr
->values
[0].string
.text
, &dtype
, &dprinter
))
8153 send_ipp_status(con
, IPP_NOT_FOUND
,
8154 _("The printer or class was not found."));
8159 * See if we have a job URI or a printer URI...
8162 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
8163 sizeof(scheme
), username
, sizeof(username
), host
,
8164 sizeof(host
), &port
, resource
, sizeof(resource
));
8166 if (!strcmp(uri
->name
, "printer-uri"))
8169 * Got a printer URI; see if we also have a job-id attribute...
8172 if ((attr
= ippFindAttribute(con
->request
, "job-id",
8173 IPP_TAG_INTEGER
)) == NULL
)
8179 if ((src
= cupsdValidateDest(uri
->values
[0].string
.text
, &stype
,
8180 &sprinter
)) == NULL
)
8186 send_ipp_status(con
, IPP_NOT_FOUND
,
8187 _("The printer or class was not found."));
8196 * Otherwise, just move a single job...
8199 if ((job
= cupsdFindJob(attr
->values
[0].integer
)) == NULL
)
8202 * Nope - return a "not found" error...
8205 send_ipp_status(con
, IPP_NOT_FOUND
,
8206 _("Job #%d does not exist!"), attr
->values
[0].integer
);
8212 * Job found, initialize source pointers...
8223 * Got a job URI; parse it to get the job ID...
8226 if (strncmp(resource
, "/jobs/", 6))
8232 send_ipp_status(con
, IPP_BAD_REQUEST
,
8233 _("Bad job-uri attribute \"%s\"!"),
8234 uri
->values
[0].string
.text
);
8239 * See if the job exists...
8242 jobid
= atoi(resource
+ 6);
8244 if ((job
= cupsdFindJob(jobid
)) == NULL
)
8247 * Nope - return a "not found" error...
8250 send_ipp_status(con
, IPP_NOT_FOUND
,
8251 _("Job #%d does not exist!"), jobid
);
8257 * Job found, initialize source pointers...
8266 * Check the policy of the destination printer...
8269 if ((status
= cupsdCheckPolicy(dprinter
->op_policy_ptr
, con
,
8270 job
? job
->username
: NULL
)) != HTTP_OK
)
8272 send_http_error(con
, status
, dprinter
);
8277 * Now move the job or jobs...
8283 * See if the job has been completed...
8286 if (job
->state_value
> IPP_JOB_STOPPED
)
8289 * Return a "not-possible" error...
8292 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
8293 _("Job #%d is finished and cannot be altered!"),
8299 * See if the job is owned by the requesting user...
8302 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8304 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
8309 * Move the job to a different printer or class...
8312 cupsdMoveJob(job
, dprinter
);
8317 * Got the source printer, now look through the jobs...
8320 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
8322 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
8325 * See if the job is pointing at the source printer or has not been
8329 if (strcasecmp(job
->dest
, src
) ||
8330 job
->state_value
> IPP_JOB_STOPPED
)
8334 * See if the job can be moved by the requesting user...
8337 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
8341 * Move the job to a different printer or class...
8344 cupsdMoveJob(job
, dprinter
);
8349 * Start jobs if possible...
8355 * Return with "everything is OK" status...
8358 con
->response
->request
.status
.status_code
= IPP_OK
;
8363 * 'ppd_parse_line()' - Parse a PPD default line.
8366 static int /* O - 0 on success, -1 on failure */
8367 ppd_parse_line(const char *line
, /* I - Line */
8368 char *option
, /* O - Option name */
8369 int olen
, /* I - Size of option name */
8370 char *choice
, /* O - Choice name */
8371 int clen
) /* I - Size of choice name */
8374 * Verify this is a default option line...
8377 if (strncmp(line
, "*Default", 8))
8381 * Read the option name...
8384 for (line
+= 8, olen
--; isalnum(*line
& 255); line
++)
8394 * Skip everything else up to the colon (:)...
8397 while (*line
&& *line
!= ':')
8406 * Now grab the option choice, skipping leading whitespace...
8409 while (isspace(*line
& 255))
8412 for (clen
--; isalnum(*line
& 255); line
++)
8422 * Return with no errors...
8430 * 'print_job()' - Print a file to a printer or class.
8434 print_job(cupsd_client_t
*con
, /* I - Client connection */
8435 ipp_attribute_t
*uri
) /* I - Printer URI */
8437 ipp_attribute_t
*attr
; /* Current attribute */
8438 ipp_attribute_t
*format
; /* Document-format attribute */
8439 const char *default_format
; /* document-format-default value */
8440 cupsd_job_t
*job
; /* New job */
8441 char filename
[1024]; /* Job filename */
8442 mime_type_t
*filetype
; /* Type of file */
8443 char super
[MIME_MAX_SUPER
], /* Supertype of file */
8444 type
[MIME_MAX_TYPE
], /* Subtype of file */
8445 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
8446 /* Textual name of mime type */
8447 cupsd_printer_t
*printer
; /* Printer data */
8448 struct stat fileinfo
; /* File information */
8449 int kbytes
; /* Size of file */
8450 int compression
; /* Document compression */
8453 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "print_job(%p[%d], %s)", con
, con
->http
.fd
,
8454 uri
->values
[0].string
.text
);
8457 * Validate print file attributes, for now just document-format and
8458 * compression (CUPS only supports "none" and "gzip")...
8461 compression
= CUPS_FILE_NONE
;
8463 if ((attr
= ippFindAttribute(con
->request
, "compression",
8464 IPP_TAG_KEYWORD
)) != NULL
)
8466 if (strcmp(attr
->values
[0].string
.text
, "none")
8468 && strcmp(attr
->values
[0].string
.text
, "gzip")
8469 #endif /* HAVE_LIBZ */
8472 send_ipp_status(con
, IPP_ATTRIBUTES
,
8473 _("Unsupported compression \"%s\"!"),
8474 attr
->values
[0].string
.text
);
8475 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
8476 "compression", NULL
, attr
->values
[0].string
.text
);
8481 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
8482 compression
= CUPS_FILE_GZIP
;
8483 #endif /* HAVE_LIBZ */
8487 * Do we have a file to print?
8492 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
8497 * Is the destination valid?
8500 if (!cupsdValidateDest(uri
->values
[0].string
.text
, NULL
, &printer
))
8506 send_ipp_status(con
, IPP_NOT_FOUND
,
8507 _("The printer or class was not found."));
8512 * Is it a format we support?
8515 if ((format
= ippFindAttribute(con
->request
, "document-format",
8516 IPP_TAG_MIMETYPE
)) != NULL
)
8519 * Grab format from client...
8522 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]", super
,
8525 send_ipp_status(con
, IPP_BAD_REQUEST
,
8526 _("Could not scan type \"%s\"!"),
8527 format
->values
[0].string
.text
);
8531 else if ((default_format
= cupsGetOption("document-format",
8532 printer
->num_options
,
8533 printer
->options
)) != NULL
)
8536 * Use default document format...
8539 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
8541 send_ipp_status(con
, IPP_BAD_REQUEST
,
8542 _("Could not scan type \"%s\"!"),
8553 strcpy(super
, "application");
8554 strcpy(type
, "octet-stream");
8557 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
8560 * Auto-type the file...
8563 ipp_attribute_t
*doc_name
; /* document-name attribute */
8566 cupsdLogMessage(CUPSD_LOG_DEBUG
, "[Job ???] Auto-typing file...");
8568 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
8569 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
8570 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
8574 filetype
= mimeType(MimeDatabase
, super
, type
);
8576 cupsdLogMessage(CUPSD_LOG_INFO
, "[Job ???] Request file type is %s/%s.",
8577 filetype
->super
, filetype
->type
);
8580 filetype
= mimeType(MimeDatabase
, super
, type
);
8584 (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))))
8587 * Replace the document-format attribute value with the auto-typed or
8591 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
8596 _cupsStrFree(format
->values
[0].string
.text
);
8598 format
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
8601 ippAddString(con
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
8602 "document-format", NULL
, mimetype
);
8606 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
8607 _("Unsupported format \'%s/%s\'!"), super
, type
);
8608 cupsdLogMessage(CUPSD_LOG_INFO
,
8609 "Hint: Do you have the raw file printing rules enabled?");
8612 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
8613 "document-format", NULL
, format
->values
[0].string
.text
);
8619 * Read any embedded job ticket info from PS files...
8622 if (!strcasecmp(filetype
->super
, "application") &&
8623 (!strcasecmp(filetype
->type
, "postscript") ||
8624 !strcasecmp(filetype
->type
, "pdf")))
8625 read_job_ticket(con
);
8628 * Create the job object...
8631 if ((job
= add_job(con
, printer
, filetype
)) == NULL
)
8635 * Update quota data...
8638 if (stat(con
->filename
, &fileinfo
))
8641 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
8643 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
8645 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
8646 IPP_TAG_INTEGER
)) != NULL
)
8647 attr
->values
[0].integer
+= kbytes
;
8650 * Add the job file...
8653 if (add_file(con
, job
, filetype
, compression
))
8656 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
8658 rename(con
->filename
, filename
);
8659 cupsdClearString(&con
->filename
);
8662 * See if we need to add the ending sheet...
8665 if (cupsdTimeoutJob(job
))
8669 * Log and save the job...
8672 cupsdLogJob(job
, CUPSD_LOG_INFO
,
8673 "File of type %s/%s queued by \"%s\".",
8674 filetype
->super
, filetype
->type
, job
->username
);
8675 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "hold_until=%d", (int)job
->hold_until
);
8676 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Queued on \"%s\" by \"%s\".",
8677 job
->dest
, job
->username
);
8680 * Start the job if possible...
8688 * 'read_job_ticket()' - Read a job ticket embedded in a print file.
8690 * This function only gets called when printing a single PDF or PostScript
8691 * file using the Print-Job operation. It doesn't work for Create-Job +
8692 * Send-File, since the job attributes need to be set at job creation
8693 * time for banners to work. The embedded job ticket stuff is here
8694 * primarily to allow the Windows printer driver for CUPS to pass in JCL
8695 * options and IPP attributes which otherwise would be lost.
8697 * The format of a job ticket is simple:
8699 * %cupsJobTicket: attr1=value1 attr2=value2 ... attrN=valueN
8701 * %cupsJobTicket: attr1=value1
8702 * %cupsJobTicket: attr2=value2
8704 * %cupsJobTicket: attrN=valueN
8706 * Job ticket lines must appear immediately after the first line that
8707 * specifies PostScript (%!PS-Adobe-3.0) or PDF (%PDF) format, and CUPS
8708 * stops looking for job ticket info when it finds a line that does not begin
8709 * with "%cupsJobTicket:".
8711 * The maximum length of a job ticket line, including the prefix, is
8712 * 255 characters to conform with the Adobe DSC.
8714 * Read-only attributes are rejected with a notice to the error log in
8715 * case a malicious user tries anything. Since the job ticket is read
8716 * prior to attribute validation in print_job(), job ticket attributes
8717 * will go through the same validation as IPP attributes...
8721 read_job_ticket(cupsd_client_t
*con
) /* I - Client connection */
8723 cups_file_t
*fp
; /* File to read from */
8724 char line
[256]; /* Line data */
8725 int num_options
; /* Number of options */
8726 cups_option_t
*options
; /* Options */
8727 ipp_t
*ticket
; /* New attributes */
8728 ipp_attribute_t
*attr
, /* Current attribute */
8729 *attr2
, /* Job attribute */
8730 *prev2
; /* Previous job attribute */
8734 * First open the print file...
8737 if ((fp
= cupsFileOpen(con
->filename
, "rb")) == NULL
)
8739 cupsdLogMessage(CUPSD_LOG_ERROR
,
8740 "Unable to open print file for job ticket - %s",
8746 * Skip the first line...
8749 if (cupsFileGets(fp
, line
, sizeof(line
)) == NULL
)
8751 cupsdLogMessage(CUPSD_LOG_ERROR
,
8752 "Unable to read from print file for job ticket - %s",
8758 if (strncmp(line
, "%!PS-Adobe-", 11) && strncmp(line
, "%PDF-", 5))
8761 * Not a DSC-compliant file, so no job ticket info will be available...
8769 * Read job ticket info from the file...
8775 while (cupsFileGets(fp
, line
, sizeof(line
)))
8778 * Stop at the first non-ticket line...
8781 if (strncmp(line
, "%cupsJobTicket:", 15))
8785 * Add the options to the option array...
8788 num_options
= cupsParseOptions(line
+ 15, num_options
, &options
);
8792 * Done with the file; see if we have any options...
8797 if (num_options
== 0)
8801 * OK, convert the options to an attribute list, and apply them to
8806 cupsEncodeOptions(ticket
, num_options
, options
);
8809 * See what the user wants to change.
8812 for (attr
= ticket
->attrs
; attr
; attr
= attr
->next
)
8814 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
8817 if (!strcmp(attr
->name
, "job-originating-host-name") ||
8818 !strcmp(attr
->name
, "job-originating-user-name") ||
8819 !strcmp(attr
->name
, "job-media-sheets-completed") ||
8820 !strcmp(attr
->name
, "job-k-octets") ||
8821 !strcmp(attr
->name
, "job-id") ||
8822 !strncmp(attr
->name
, "job-state", 9) ||
8823 !strncmp(attr
->name
, "time-at-", 8))
8824 continue; /* Read-only attrs */
8826 if ((attr2
= ippFindAttribute(con
->request
, attr
->name
,
8827 IPP_TAG_ZERO
)) != NULL
)
8830 * Some other value; first free the old value...
8833 if (con
->request
->attrs
== attr2
)
8835 con
->request
->attrs
= attr2
->next
;
8840 for (prev2
= con
->request
->attrs
; prev2
; prev2
= prev2
->next
)
8841 if (prev2
->next
== attr2
)
8843 prev2
->next
= attr2
->next
;
8848 if (con
->request
->last
== attr2
)
8849 con
->request
->last
= prev2
;
8851 _ippFreeAttr(attr2
);
8855 * Add new option by copying it...
8858 copy_attribute(con
->request
, attr
, 0);
8862 * Then free the attribute list and option array...
8866 cupsFreeOptions(num_options
, options
);
8871 * 'reject_jobs()' - Reject print jobs to a printer.
8875 reject_jobs(cupsd_client_t
*con
, /* I - Client connection */
8876 ipp_attribute_t
*uri
) /* I - Printer or class URI */
8878 http_status_t status
; /* Policy status */
8879 cups_ptype_t dtype
; /* Destination type (printer/class) */
8880 cupsd_printer_t
*printer
; /* Printer data */
8881 ipp_attribute_t
*attr
; /* printer-state-message text */
8884 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "reject_jobs(%p[%d], %s)", con
,
8885 con
->http
.fd
, uri
->values
[0].string
.text
);
8888 * Is the destination valid?
8891 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8897 send_ipp_status(con
, IPP_NOT_FOUND
,
8898 _("The printer or class was not found."));
8906 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8908 send_http_error(con
, status
, printer
);
8913 * Reject jobs sent to the printer...
8916 printer
->accepting
= 0;
8918 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
8919 IPP_TAG_TEXT
)) == NULL
)
8920 strcpy(printer
->state_message
, "Rejecting Jobs");
8922 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
8923 sizeof(printer
->state_message
));
8925 cupsdAddPrinterHistory(printer
);
8927 if (dtype
& CUPS_PRINTER_CLASS
)
8929 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
8931 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" rejecting jobs (\"%s\").",
8932 printer
->name
, get_username(con
));
8936 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
8938 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" rejecting jobs (\"%s\").",
8939 printer
->name
, get_username(con
));
8943 * Everything was ok, so return OK status...
8946 con
->response
->request
.status
.status_code
= IPP_OK
;
8951 * 'release_held_new_jobs()' - Release pending/new jobs on a printer or class.
8955 release_held_new_jobs(
8956 cupsd_client_t
*con
, /* I - Connection */
8957 ipp_attribute_t
*uri
) /* I - Printer URI */
8959 http_status_t status
; /* Policy status */
8960 cups_ptype_t dtype
; /* Destination type (printer/class) */
8961 cupsd_printer_t
*printer
; /* Printer data */
8964 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_held_new_jobs(%p[%d], %s)", con
,
8965 con
->http
.fd
, uri
->values
[0].string
.text
);
8968 * Is the destination valid?
8971 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
8977 send_ipp_status(con
, IPP_NOT_FOUND
,
8978 _("The printer or class was not found."));
8986 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
8988 send_http_error(con
, status
, printer
);
8993 * Hold pending/new jobs sent to the printer...
8996 printer
->holding_new_jobs
= 0;
8998 cupsdSetPrinterReasons(printer
, "-hold-new-jobs");
8999 cupsdAddPrinterHistory(printer
);
9001 if (dtype
& CUPS_PRINTER_CLASS
)
9002 cupsdLogMessage(CUPSD_LOG_INFO
,
9003 "Class \"%s\" now printing pending/new jobs (\"%s\").",
9004 printer
->name
, get_username(con
));
9006 cupsdLogMessage(CUPSD_LOG_INFO
,
9007 "Printer \"%s\" now printing pending/new jobs (\"%s\").",
9008 printer
->name
, get_username(con
));
9011 * Everything was ok, so return OK status...
9014 con
->response
->request
.status
.status_code
= IPP_OK
;
9019 * 'release_job()' - Release a held print job.
9023 release_job(cupsd_client_t
*con
, /* I - Client connection */
9024 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9026 ipp_attribute_t
*attr
; /* Current attribute */
9027 int jobid
; /* Job ID */
9028 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9029 username
[HTTP_MAX_URI
], /* Username portion of URI */
9030 host
[HTTP_MAX_URI
], /* Host portion of URI */
9031 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9032 int port
; /* Port portion of URI */
9033 cupsd_job_t
*job
; /* Job information */
9036 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "release_job(%p[%d], %s)", con
,
9037 con
->http
.fd
, uri
->values
[0].string
.text
);
9040 * See if we have a job URI or a printer URI...
9043 if (!strcmp(uri
->name
, "printer-uri"))
9046 * Got a printer URI; see if we also have a job-id attribute...
9049 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9050 IPP_TAG_INTEGER
)) == NULL
)
9052 send_ipp_status(con
, IPP_BAD_REQUEST
,
9053 _("Got a printer-uri attribute but no job-id!"));
9057 jobid
= attr
->values
[0].integer
;
9062 * Got a job URI; parse it to get the job ID...
9065 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9066 sizeof(scheme
), username
, sizeof(username
), host
,
9067 sizeof(host
), &port
, resource
, sizeof(resource
));
9069 if (strncmp(resource
, "/jobs/", 6))
9075 send_ipp_status(con
, IPP_BAD_REQUEST
,
9076 _("Bad job-uri attribute \"%s\"!"),
9077 uri
->values
[0].string
.text
);
9081 jobid
= atoi(resource
+ 6);
9085 * See if the job exists...
9088 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9091 * Nope - return a "not found" error...
9094 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9099 * See if job is "held"...
9102 if (job
->state_value
!= IPP_JOB_HELD
)
9105 * Nope - return a "not possible" error...
9108 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not held!"), jobid
);
9113 * See if the job is owned by the requesting user...
9116 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9118 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9123 * Reset the job-hold-until value to "no-hold"...
9126 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9127 IPP_TAG_KEYWORD
)) == NULL
)
9128 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9132 _cupsStrFree(attr
->values
[0].string
.text
);
9134 attr
->value_tag
= IPP_TAG_KEYWORD
;
9135 attr
->values
[0].string
.text
= _cupsStrAlloc("no-hold");
9137 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
9138 "Job job-hold-until value changed by user.");
9142 * Release the job and return...
9145 cupsdReleaseJob(job
);
9147 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
9148 "Job released by user.");
9150 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Released by \"%s\".", username
);
9152 con
->response
->request
.status
.status_code
= IPP_OK
;
9159 * 'renew_subscription()' - Renew an existing subscription...
9164 cupsd_client_t
*con
, /* I - Client connection */
9165 int sub_id
) /* I - Subscription ID */
9167 http_status_t status
; /* Policy status */
9168 cupsd_subscription_t
*sub
; /* Subscription */
9169 ipp_attribute_t
*lease
; /* notify-lease-duration */
9172 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
9173 "renew_subscription(con=%p[%d], sub_id=%d)",
9174 con
, con
->http
.fd
, sub_id
);
9177 * Is the subscription ID valid?
9180 if ((sub
= cupsdFindSubscription(sub_id
)) == NULL
)
9183 * Bad subscription ID...
9186 send_ipp_status(con
, IPP_NOT_FOUND
,
9187 _("notify-subscription-id %d no good!"), sub_id
);
9194 * Job subscriptions cannot be renewed...
9197 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9198 _("Job subscriptions cannot be renewed!"));
9206 if ((status
= cupsdCheckPolicy(sub
->dest
? sub
->dest
->op_policy_ptr
:
9208 con
, sub
->owner
)) != HTTP_OK
)
9210 send_http_error(con
, status
, sub
->dest
);
9215 * Renew the subscription...
9218 lease
= ippFindAttribute(con
->request
, "notify-lease-duration",
9221 sub
->lease
= lease
? lease
->values
[0].integer
: DefaultLeaseDuration
;
9223 if (MaxLeaseDuration
&& (sub
->lease
== 0 || sub
->lease
> MaxLeaseDuration
))
9225 cupsdLogMessage(CUPSD_LOG_INFO
,
9226 "renew_subscription: Limiting notify-lease-duration to "
9229 sub
->lease
= MaxLeaseDuration
;
9232 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
9234 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
9236 con
->response
->request
.status
.status_code
= IPP_OK
;
9238 ippAddInteger(con
->response
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_INTEGER
,
9239 "notify-lease-duration", sub
->lease
);
9244 * 'restart_job()' - Restart an old print job.
9248 restart_job(cupsd_client_t
*con
, /* I - Client connection */
9249 ipp_attribute_t
*uri
) /* I - Job or Printer URI */
9251 ipp_attribute_t
*attr
; /* Current attribute */
9252 int jobid
; /* Job ID */
9253 char scheme
[HTTP_MAX_URI
], /* Method portion of URI */
9254 username
[HTTP_MAX_URI
], /* Username portion of URI */
9255 host
[HTTP_MAX_URI
], /* Host portion of URI */
9256 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
9257 int port
; /* Port portion of URI */
9258 cupsd_job_t
*job
; /* Job information */
9261 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "restart_job(%p[%d], %s)", con
,
9262 con
->http
.fd
, uri
->values
[0].string
.text
);
9265 * See if we have a job URI or a printer URI...
9268 if (!strcmp(uri
->name
, "printer-uri"))
9271 * Got a printer URI; see if we also have a job-id attribute...
9274 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9275 IPP_TAG_INTEGER
)) == NULL
)
9277 send_ipp_status(con
, IPP_BAD_REQUEST
,
9278 _("Got a printer-uri attribute but no job-id!"));
9282 jobid
= attr
->values
[0].integer
;
9287 * Got a job URI; parse it to get the job ID...
9290 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9291 sizeof(scheme
), username
, sizeof(username
), host
,
9292 sizeof(host
), &port
, resource
, sizeof(resource
));
9294 if (strncmp(resource
, "/jobs/", 6))
9300 send_ipp_status(con
, IPP_BAD_REQUEST
,
9301 _("Bad job-uri attribute \"%s\"!"),
9302 uri
->values
[0].string
.text
);
9306 jobid
= atoi(resource
+ 6);
9310 * See if the job exists...
9313 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9316 * Nope - return a "not found" error...
9319 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9324 * See if job is in any of the "completed" states...
9327 if (job
->state_value
<= IPP_JOB_PROCESSING
)
9330 * Nope - return a "not possible" error...
9333 send_ipp_status(con
, IPP_NOT_POSSIBLE
, _("Job #%d is not complete!"),
9339 * See if we have retained the job files...
9344 if (!job
->attrs
|| job
->num_files
== 0)
9347 * Nope - return a "not possible" error...
9350 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
9351 _("Job #%d cannot be restarted - no files!"), jobid
);
9356 * See if the job is owned by the requesting user...
9359 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9361 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9366 * Restart the job and return...
9369 cupsdRestartJob(job
);
9371 cupsdLogJob(job
, CUPSD_LOG_INFO
, "Restarted by \"%s\".", username
);
9373 con
->response
->request
.status
.status_code
= IPP_OK
;
9378 * 'save_auth_info()' - Save authentication information for a job.
9383 cupsd_client_t
*con
, /* I - Client connection */
9384 cupsd_job_t
*job
, /* I - Job */
9385 ipp_attribute_t
*auth_info
) /* I - auth-info attribute, if any */
9387 int i
; /* Looping var */
9388 char filename
[1024]; /* Job authentication filename */
9389 cups_file_t
*fp
; /* Job authentication file */
9390 char line
[2048]; /* Line for file */
9391 cupsd_printer_t
*dest
; /* Destination printer/class */
9395 * This function saves the in-memory authentication information for
9396 * a job so that it can be used to authenticate with a remote host.
9397 * The information is stored in a file that is readable only by the
9398 * root user. The fields are Base-64 encoded, each on a separate line,
9399 * followed by random number (up to 1024) of newlines to limit the
9400 * amount of information that is exposed.
9402 * Because of the potential for exposing of authentication information,
9403 * this functionality is only enabled when running cupsd as root.
9405 * This caching only works for the Basic and BasicDigest authentication
9406 * types. Digest authentication cannot be cached this way, and in
9407 * the future Kerberos authentication may make all of this obsolete.
9409 * Authentication information is saved whenever an authenticated
9410 * Print-Job, Create-Job, or CUPS-Authenticate-Job operation is
9413 * This information is deleted after a job is completed or canceled,
9414 * so reprints may require subsequent re-authentication.
9420 if ((dest
= cupsdFindDest(job
->dest
)) == NULL
)
9424 * Create the authentication file and change permissions...
9427 snprintf(filename
, sizeof(filename
), "%s/a%05d", RequestRoot
, job
->id
);
9428 if ((fp
= cupsFileOpen(filename
, "w")) == NULL
)
9430 cupsdLogMessage(CUPSD_LOG_ERROR
,
9431 "Unable to save authentication info to \"%s\" - %s",
9432 filename
, strerror(errno
));
9436 fchown(cupsFileNumber(fp
), 0, 0);
9437 fchmod(cupsFileNumber(fp
), 0400);
9439 if (auth_info
&& auth_info
->num_values
== dest
->num_auth_info_required
)
9442 * Write 1 to 3 auth values...
9445 cupsdClearString(&job
->auth_username
);
9446 cupsdClearString(&job
->auth_domain
);
9447 cupsdClearString(&job
->auth_password
);
9449 for (i
= 0; i
< auth_info
->num_values
; i
++)
9451 httpEncode64_2(line
, sizeof(line
), auth_info
->values
[i
].string
.text
,
9452 strlen(auth_info
->values
[i
].string
.text
));
9453 cupsFilePrintf(fp
, "%s\n", line
);
9455 if (!strcmp(dest
->auth_info_required
[i
], "username"))
9456 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s",
9457 auth_info
->values
[i
].string
.text
);
9458 else if (!strcmp(dest
->auth_info_required
[i
], "domain"))
9459 cupsdSetStringf(&job
->auth_domain
, "AUTH_DOMAIN=%s",
9460 auth_info
->values
[i
].string
.text
);
9461 else if (!strcmp(dest
->auth_info_required
[i
], "password"))
9462 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s",
9463 auth_info
->values
[i
].string
.text
);
9466 else if (con
->username
[0])
9469 * Write the authenticated username...
9472 httpEncode64_2(line
, sizeof(line
), con
->username
, strlen(con
->username
));
9473 cupsFilePrintf(fp
, "%s\n", line
);
9475 cupsdSetStringf(&job
->auth_username
, "AUTH_USERNAME=%s", con
->username
);
9476 cupsdClearString(&job
->auth_domain
);
9479 * Write the authenticated password...
9482 httpEncode64_2(line
, sizeof(line
), con
->password
, strlen(con
->password
));
9483 cupsFilePrintf(fp
, "%s\n", line
);
9485 cupsdSetStringf(&job
->auth_password
, "AUTH_PASSWORD=%s", con
->password
);
9489 * Write a random number of newlines to the end of the file...
9492 for (i
= (rand() % 1024); i
>= 0; i
--)
9493 cupsFilePutChar(fp
, '\n');
9496 * Close the file and return...
9501 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9502 if (con
->gss_have_creds
)
9503 save_krb5_creds(con
, job
);
9504 else if (job
->ccname
)
9505 cupsdClearString(&(job
->ccname
));
9506 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9510 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
9512 * 'save_krb5_creds()' - Save Kerberos credentials for the job.
9516 save_krb5_creds(cupsd_client_t
*con
, /* I - Client connection */
9517 cupsd_job_t
*job
) /* I - Job */
9519 # if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
9520 cupsdLogMessage(CUPSD_LOG_INFO
,
9521 "Sorry, your version of Kerberos does not support delegated "
9526 krb5_error_code error
; /* Kerberos error code */
9527 OM_uint32 major_status
, /* Major status code */
9528 minor_status
; /* Minor status code */
9529 krb5_principal principal
; /* Kerberos principal */
9534 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
9538 if (krb5_init_context
== NULL
)
9540 # endif /* __APPLE__ */
9542 if (!KerberosInitialized
)
9545 * Setup a Kerberos context for the scheduler to use...
9548 KerberosInitialized
= 1;
9550 if (krb5_init_context(&KerberosContext
))
9552 KerberosContext
= NULL
;
9554 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize Kerberos context");
9560 * We MUST create a file-based cache because memory-based caches are
9561 * only valid for the current process/address space.
9563 * Due to various bugs/features in different versions of Kerberos, we
9564 * need either the krb5_cc_new_unique() function or Heimdal's version
9565 * of krb5_cc_gen_new() to create a new FILE: credential cache that
9566 * can be passed to the backend. These functions create a temporary
9567 * file (typically in /tmp) containing the cached credentials, which
9568 * are removed when we have successfully printed a job.
9571 # ifdef HAVE_KRB5_CC_NEW_UNIQUE
9572 if ((error
= krb5_cc_new_unique(KerberosContext
, "FILE", NULL
,
9573 &(job
->ccache
))) != 0)
9574 # else /* HAVE_HEIMDAL */
9575 if ((error
= krb5_cc_gen_new(KerberosContext
, &krb5_fcc_ops
,
9576 &(job
->ccache
))) != 0)
9577 # endif /* HAVE_KRB5_CC_NEW_UNIQUE */
9579 cupsdLogMessage(CUPSD_LOG_ERROR
,
9580 "Unable to create new credentials cache (%d/%s)",
9581 error
, strerror(errno
));
9586 if ((error
= krb5_parse_name(KerberosContext
, con
->username
, &principal
)) != 0)
9588 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to parse kerberos username (%d/%s)",
9589 error
, strerror(errno
));
9590 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9595 if ((error
= krb5_cc_initialize(KerberosContext
, job
->ccache
, principal
)))
9597 cupsdLogMessage(CUPSD_LOG_ERROR
,
9598 "Unable to initialize credentials cache (%d/%s)", error
,
9600 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9601 krb5_free_principal(KerberosContext
, principal
);
9606 krb5_free_principal(KerberosContext
, principal
);
9609 * Copy the user's credentials to the new cache file...
9612 major_status
= gss_krb5_copy_ccache(&minor_status
, con
->gss_delegated_cred
,
9615 if (GSS_ERROR(major_status
))
9617 cupsdLogGSSMessage(CUPSD_LOG_ERROR
, major_status
, minor_status
,
9618 "Unable to import client credentials cache");
9619 krb5_cc_destroy(KerberosContext
, job
->ccache
);
9625 * Add the KRB5CCNAME environment variable to the job so that the
9626 * backend can use the credentials when printing.
9629 cupsdSetStringf(&(job
->ccname
), "KRB5CCNAME=FILE:%s",
9630 krb5_cc_get_name(KerberosContext
, job
->ccache
));
9632 cupsdLogJob(job
, CUPSD_LOG_DEBUG2
, "save_krb5_creds: %s", job
->ccname
);
9633 # endif /* HAVE_KRB5_CC_NEW_UNIQUE || HAVE_HEIMDAL */
9635 #endif /* HAVE_GSSAPI && HAVE_KRB5_H */
9639 * 'send_document()' - Send a file to a printer or class.
9643 send_document(cupsd_client_t
*con
, /* I - Client connection */
9644 ipp_attribute_t
*uri
) /* I - Printer URI */
9646 ipp_attribute_t
*attr
; /* Current attribute */
9647 ipp_attribute_t
*format
; /* Request's document-format attribute */
9648 ipp_attribute_t
*jformat
; /* Job's document-format attribute */
9649 const char *default_format
;/* document-format-default value */
9650 int jobid
; /* Job ID number */
9651 cupsd_job_t
*job
; /* Current job */
9652 char job_uri
[HTTP_MAX_URI
],
9654 scheme
[HTTP_MAX_URI
],
9655 /* Method portion of URI */
9656 username
[HTTP_MAX_URI
],
9657 /* Username portion of URI */
9659 /* Host portion of URI */
9660 resource
[HTTP_MAX_URI
];
9661 /* Resource portion of URI */
9662 int port
; /* Port portion of URI */
9663 mime_type_t
*filetype
; /* Type of file */
9664 char super
[MIME_MAX_SUPER
],
9665 /* Supertype of file */
9666 type
[MIME_MAX_TYPE
],
9667 /* Subtype of file */
9668 mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
9669 /* Textual name of mime type */
9670 char filename
[1024]; /* Job filename */
9671 cupsd_printer_t
*printer
; /* Current printer */
9672 struct stat fileinfo
; /* File information */
9673 int kbytes
; /* Size of file */
9674 int compression
; /* Type of compression */
9675 int start_job
; /* Start the job? */
9678 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_document(%p[%d], %s)", con
,
9679 con
->http
.fd
, uri
->values
[0].string
.text
);
9682 * See if we have a job URI or a printer URI...
9685 if (!strcmp(uri
->name
, "printer-uri"))
9688 * Got a printer URI; see if we also have a job-id attribute...
9691 if ((attr
= ippFindAttribute(con
->request
, "job-id",
9692 IPP_TAG_INTEGER
)) == NULL
)
9694 send_ipp_status(con
, IPP_BAD_REQUEST
,
9695 _("Got a printer-uri attribute but no job-id!"));
9699 jobid
= attr
->values
[0].integer
;
9704 * Got a job URI; parse it to get the job ID...
9707 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
9708 sizeof(scheme
), username
, sizeof(username
), host
,
9709 sizeof(host
), &port
, resource
, sizeof(resource
));
9711 if (strncmp(resource
, "/jobs/", 6))
9717 send_ipp_status(con
, IPP_BAD_REQUEST
,
9718 _("Bad job-uri attribute \"%s\"!"),
9719 uri
->values
[0].string
.text
);
9723 jobid
= atoi(resource
+ 6);
9727 * See if the job exists...
9730 if ((job
= cupsdFindJob(jobid
)) == NULL
)
9733 * Nope - return a "not found" error...
9736 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
9740 printer
= cupsdFindDest(job
->dest
);
9743 * See if the job is owned by the requesting user...
9746 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
9748 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
9753 * OK, see if the client is sending the document compressed - CUPS
9754 * only supports "none" and "gzip".
9757 compression
= CUPS_FILE_NONE
;
9759 if ((attr
= ippFindAttribute(con
->request
, "compression",
9760 IPP_TAG_KEYWORD
)) != NULL
)
9762 if (strcmp(attr
->values
[0].string
.text
, "none")
9764 && strcmp(attr
->values
[0].string
.text
, "gzip")
9765 #endif /* HAVE_LIBZ */
9768 send_ipp_status(con
, IPP_ATTRIBUTES
, _("Unsupported compression \"%s\"!"),
9769 attr
->values
[0].string
.text
);
9770 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
9771 "compression", NULL
, attr
->values
[0].string
.text
);
9776 if (!strcmp(attr
->values
[0].string
.text
, "gzip"))
9777 compression
= CUPS_FILE_GZIP
;
9778 #endif /* HAVE_LIBZ */
9782 * Do we have a file to print?
9787 send_ipp_status(con
, IPP_BAD_REQUEST
, _("No file!?!"));
9792 * Is it a format we support?
9795 if ((format
= ippFindAttribute(con
->request
, "document-format",
9796 IPP_TAG_MIMETYPE
)) != NULL
)
9799 * Grab format from client...
9802 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
9805 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
9806 format
->values
[0].string
.text
);
9810 else if ((default_format
= cupsGetOption("document-format",
9811 printer
->num_options
,
9812 printer
->options
)) != NULL
)
9815 * Use default document format...
9818 if (sscanf(default_format
, "%15[^/]/%31[^;]", super
, type
) != 2)
9820 send_ipp_status(con
, IPP_BAD_REQUEST
,
9821 _("Could not scan type \"%s\"!"),
9829 * No document format attribute? Auto-type it!
9832 strcpy(super
, "application");
9833 strcpy(type
, "octet-stream");
9836 if (!strcmp(super
, "application") && !strcmp(type
, "octet-stream"))
9839 * Auto-type the file...
9842 ipp_attribute_t
*doc_name
; /* document-name attribute */
9845 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Auto-typing file...");
9847 doc_name
= ippFindAttribute(con
->request
, "document-name", IPP_TAG_NAME
);
9848 filetype
= mimeFileType(MimeDatabase
, con
->filename
,
9849 doc_name
? doc_name
->values
[0].string
.text
: NULL
,
9853 filetype
= mimeType(MimeDatabase
, super
, type
);
9855 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Request file type is %s/%s.",
9856 filetype
->super
, filetype
->type
);
9859 filetype
= mimeType(MimeDatabase
, super
, type
);
9864 * Replace the document-format attribute value with the auto-typed or
9868 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9871 if ((jformat
= ippFindAttribute(job
->attrs
, "document-format",
9872 IPP_TAG_MIMETYPE
)) != NULL
)
9874 _cupsStrFree(jformat
->values
[0].string
.text
);
9876 jformat
->values
[0].string
.text
= _cupsStrAlloc(mimetype
);
9879 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
9880 "document-format", NULL
, mimetype
);
9884 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9885 _("Unsupported format \'%s/%s\'!"), super
, type
);
9886 cupsdLogMessage(CUPSD_LOG_INFO
,
9887 "Hint: Do you have the raw file printing rules enabled?");
9890 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9891 "document-format", NULL
, format
->values
[0].string
.text
);
9896 if (printer
->filetypes
&& !cupsArrayFind(printer
->filetypes
, filetype
))
9898 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", filetype
->super
,
9901 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
9902 _("Unsupported format \'%s\'!"), mimetype
);
9904 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
9905 "document-format", NULL
, mimetype
);
9911 * Add the file to the job...
9916 if (add_file(con
, job
, filetype
, compression
))
9919 if (stat(con
->filename
, &fileinfo
))
9922 kbytes
= (fileinfo
.st_size
+ 1023) / 1024;
9924 cupsdUpdateQuota(printer
, job
->username
, 0, kbytes
);
9926 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
9927 IPP_TAG_INTEGER
)) != NULL
)
9928 attr
->values
[0].integer
+= kbytes
;
9930 snprintf(filename
, sizeof(filename
), "%s/d%05d-%03d", RequestRoot
, job
->id
,
9932 rename(con
->filename
, filename
);
9934 cupsdClearString(&con
->filename
);
9936 cupsdLogJob(job
, CUPSD_LOG_INFO
, "File of type %s/%s queued by \"%s\".",
9937 filetype
->super
, filetype
->type
, job
->username
);
9940 * Start the job if this is the last document...
9943 if ((attr
= ippFindAttribute(con
->request
, "last-document",
9944 IPP_TAG_BOOLEAN
)) != NULL
&&
9945 attr
->values
[0].boolean
)
9948 * See if we need to add the ending sheet...
9951 if (cupsdTimeoutJob(job
))
9954 if (job
->state_value
== IPP_JOB_STOPPED
)
9956 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9957 job
->state_value
= IPP_JOB_PENDING
;
9959 else if (job
->state_value
== IPP_JOB_HELD
)
9961 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9962 IPP_TAG_KEYWORD
)) == NULL
)
9963 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9965 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9967 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
9968 job
->state_value
= IPP_JOB_PENDING
;
9973 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9979 if ((attr
= ippFindAttribute(job
->attrs
, "job-hold-until",
9980 IPP_TAG_KEYWORD
)) == NULL
)
9981 attr
= ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
);
9983 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "no-hold"))
9985 job
->state
->values
[0].integer
= IPP_JOB_HELD
;
9986 job
->state_value
= IPP_JOB_HELD
;
9987 job
->hold_until
= time(NULL
) + 60;
9990 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
9997 * Fill in the response info...
10000 snprintf(job_uri
, sizeof(job_uri
), "http://%s:%d/jobs/%d", ServerName
,
10003 ippAddString(con
->response
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
,
10006 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", jobid
);
10008 ippAddInteger(con
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state",
10010 add_job_state_reasons(con
, job
);
10012 con
->response
->request
.status
.status_code
= IPP_OK
;
10015 * Start the job if necessary...
10024 * 'send_http_error()' - Send a HTTP error back to the IPP client.
10029 cupsd_client_t
*con
, /* I - Client connection */
10030 http_status_t status
, /* I - HTTP status code */
10031 cupsd_printer_t
*printer
) /* I - Printer, if any */
10033 ipp_attribute_t
*uri
; /* Request URI, if any */
10036 if ((uri
= ippFindAttribute(con
->request
, "printer-uri",
10037 IPP_TAG_URI
)) == NULL
)
10038 uri
= ippFindAttribute(con
->request
, "job-uri", IPP_TAG_URI
);
10040 cupsdLogMessage(status
== HTTP_FORBIDDEN
? CUPSD_LOG_ERROR
: CUPSD_LOG_DEBUG
,
10041 "Returning HTTP %s for %s (%s) from %s",
10042 httpStatus(status
),
10043 ippOpString(con
->request
->request
.op
.operation_id
),
10044 uri
? uri
->values
[0].string
.text
: "no URI",
10045 con
->http
.hostname
);
10049 int auth_type
; /* Type of authentication required */
10052 auth_type
= CUPSD_AUTH_NONE
;
10054 if (status
== HTTP_UNAUTHORIZED
&&
10055 printer
->num_auth_info_required
> 0 &&
10056 !strcmp(printer
->auth_info_required
[0], "negotiate") &&
10058 (con
->request
->request
.op
.operation_id
== IPP_PRINT_JOB
||
10059 con
->request
->request
.op
.operation_id
== IPP_CREATE_JOB
||
10060 con
->request
->request
.op
.operation_id
== CUPS_AUTHENTICATE_JOB
))
10063 * Creating and authenticating jobs requires Kerberos...
10066 auth_type
= CUPSD_AUTH_NEGOTIATE
;
10071 * Use policy/location-defined authentication requirements...
10074 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
10075 cupsd_location_t
*auth
; /* Pointer to authentication element */
10078 if (printer
->type
& CUPS_PRINTER_CLASS
)
10079 snprintf(resource
, sizeof(resource
), "/classes/%s", printer
->name
);
10081 snprintf(resource
, sizeof(resource
), "/printers/%s", printer
->name
);
10083 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
10084 auth
->type
== CUPSD_AUTH_NONE
)
10085 auth
= cupsdFindPolicyOp(printer
->op_policy_ptr
,
10087 con
->request
->request
.op
.operation_id
:
10092 if (auth
->type
== CUPSD_AUTH_DEFAULT
)
10093 auth_type
= DefaultAuthType
;
10095 auth_type
= auth
->type
;
10099 cupsdSendError(con
, status
, auth_type
);
10102 cupsdSendError(con
, status
, CUPSD_AUTH_NONE
);
10104 ippDelete(con
->response
);
10105 con
->response
= NULL
;
10112 * 'send_ipp_status()' - Send a status back to the IPP client.
10116 send_ipp_status(cupsd_client_t
*con
, /* I - Client connection */
10117 ipp_status_t status
, /* I - IPP status code */
10118 const char *message
,/* I - Status message */
10119 ...) /* I - Additional args as needed */
10121 va_list ap
; /* Pointer to additional args */
10122 char formatted
[1024]; /* Formatted errror message */
10125 va_start(ap
, message
);
10126 vsnprintf(formatted
, sizeof(formatted
),
10127 _cupsLangString(con
->language
, message
), ap
);
10130 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%s %s: %s",
10131 ippOpString(con
->request
->request
.op
.operation_id
),
10132 ippErrorString(status
), formatted
);
10134 con
->response
->request
.status
.status_code
= status
;
10136 if (ippFindAttribute(con
->response
, "attributes-charset",
10137 IPP_TAG_ZERO
) == NULL
)
10138 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
10139 "attributes-charset", NULL
, DefaultCharset
);
10141 if (ippFindAttribute(con
->response
, "attributes-natural-language",
10142 IPP_TAG_ZERO
) == NULL
)
10143 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
10144 "attributes-natural-language", NULL
, DefaultLanguage
);
10146 ippAddString(con
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
10147 "status-message", NULL
, formatted
);
10152 * 'set_default()' - Set the default destination...
10156 set_default(cupsd_client_t
*con
, /* I - Client connection */
10157 ipp_attribute_t
*uri
) /* I - Printer URI */
10159 http_status_t status
; /* Policy status */
10160 cups_ptype_t dtype
; /* Destination type (printer/class) */
10161 cupsd_printer_t
*printer
, /* Printer */
10162 *oldprinter
; /* Old default printer */
10165 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_default(%p[%d], %s)", con
,
10166 con
->http
.fd
, uri
->values
[0].string
.text
);
10169 * Is the destination valid?
10172 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10178 send_ipp_status(con
, IPP_NOT_FOUND
,
10179 _("The printer or class was not found."));
10187 if ((status
= cupsdCheckPolicy(DefaultPolicyPtr
, con
, NULL
)) != HTTP_OK
)
10189 send_http_error(con
, status
, NULL
);
10194 * Set it as the default...
10197 oldprinter
= DefaultPrinter
;
10198 DefaultPrinter
= printer
;
10201 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, oldprinter
, NULL
,
10202 "%s is no longer the default printer.", oldprinter
->name
);
10204 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, printer
, NULL
,
10205 "%s is now the default printer.", printer
->name
);
10207 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
| CUPSD_DIRTY_CLASSES
|
10208 CUPSD_DIRTY_REMOTE
| CUPSD_DIRTY_PRINTCAP
);
10210 cupsdLogMessage(CUPSD_LOG_INFO
,
10211 "Default destination set to \"%s\" by \"%s\".",
10212 printer
->name
, get_username(con
));
10215 * Everything was ok, so return OK status...
10218 con
->response
->request
.status
.status_code
= IPP_OK
;
10223 * 'set_job_attrs()' - Set job attributes.
10227 set_job_attrs(cupsd_client_t
*con
, /* I - Client connection */
10228 ipp_attribute_t
*uri
) /* I - Job URI */
10230 ipp_attribute_t
*attr
, /* Current attribute */
10231 *attr2
; /* Job attribute */
10232 int jobid
; /* Job ID */
10233 cupsd_job_t
*job
; /* Current job */
10234 char scheme
[HTTP_MAX_URI
],
10235 /* Method portion of URI */
10236 username
[HTTP_MAX_URI
],
10237 /* Username portion of URI */
10238 host
[HTTP_MAX_URI
],
10239 /* Host portion of URI */
10240 resource
[HTTP_MAX_URI
];
10241 /* Resource portion of URI */
10242 int port
; /* Port portion of URI */
10243 int event
; /* Events? */
10244 int check_jobs
; /* Check jobs? */
10247 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_job_attrs(%p[%d], %s)", con
,
10248 con
->http
.fd
, uri
->values
[0].string
.text
);
10251 * Start with "everything is OK" status...
10254 con
->response
->request
.status
.status_code
= IPP_OK
;
10257 * See if we have a job URI or a printer URI...
10260 if (!strcmp(uri
->name
, "printer-uri"))
10263 * Got a printer URI; see if we also have a job-id attribute...
10266 if ((attr
= ippFindAttribute(con
->request
, "job-id",
10267 IPP_TAG_INTEGER
)) == NULL
)
10269 send_ipp_status(con
, IPP_BAD_REQUEST
,
10270 _("Got a printer-uri attribute but no job-id!"));
10274 jobid
= attr
->values
[0].integer
;
10279 * Got a job URI; parse it to get the job ID...
10282 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
->values
[0].string
.text
, scheme
,
10283 sizeof(scheme
), username
, sizeof(username
), host
,
10284 sizeof(host
), &port
, resource
, sizeof(resource
));
10286 if (strncmp(resource
, "/jobs/", 6))
10292 send_ipp_status(con
, IPP_BAD_REQUEST
,
10293 _("Bad job-uri attribute \"%s\"!"),
10294 uri
->values
[0].string
.text
);
10298 jobid
= atoi(resource
+ 6);
10302 * See if the job exists...
10305 if ((job
= cupsdFindJob(jobid
)) == NULL
)
10308 * Nope - return a "not found" error...
10311 send_ipp_status(con
, IPP_NOT_FOUND
, _("Job #%d does not exist!"), jobid
);
10316 * See if the job has been completed...
10319 if (job
->state_value
> IPP_JOB_STOPPED
)
10322 * Return a "not-possible" error...
10325 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10326 _("Job #%d is finished and cannot be altered!"), jobid
);
10331 * See if the job is owned by the requesting user...
10334 if (!validate_user(job
, con
, job
->username
, username
, sizeof(username
)))
10336 send_http_error(con
, HTTP_UNAUTHORIZED
, cupsdFindDest(job
->dest
));
10341 * See what the user wants to change.
10349 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10351 if (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
)
10354 if (!strcmp(attr
->name
, "attributes-charset") ||
10355 !strcmp(attr
->name
, "attributes-natural-language") ||
10356 !strcmp(attr
->name
, "document-compression") ||
10357 !strcmp(attr
->name
, "document-format") ||
10358 !strcmp(attr
->name
, "job-detailed-status-messages") ||
10359 !strcmp(attr
->name
, "job-document-access-errors") ||
10360 !strcmp(attr
->name
, "job-id") ||
10361 !strcmp(attr
->name
, "job-impressions-completed") ||
10362 !strcmp(attr
->name
, "job-k-octets") ||
10363 !strcmp(attr
->name
, "job-originating-host-name") ||
10364 !strcmp(attr
->name
, "job-originating-user-name") ||
10365 !strcmp(attr
->name
, "job-printer-up-time") ||
10366 !strcmp(attr
->name
, "job-printer-uri") ||
10367 !strcmp(attr
->name
, "job-sheets") ||
10368 !strcmp(attr
->name
, "job-state-message") ||
10369 !strcmp(attr
->name
, "job-state-reasons") ||
10370 !strcmp(attr
->name
, "job-uri") ||
10371 !strcmp(attr
->name
, "number-of-documents") ||
10372 !strcmp(attr
->name
, "number-of-intervening-jobs") ||
10373 !strcmp(attr
->name
, "output-device-assigned") ||
10374 !strncmp(attr
->name
, "date-time-at-", 13) ||
10375 !strncmp(attr
->name
, "job-k-octets", 12) ||
10376 !strncmp(attr
->name
, "job-media-sheets", 16) ||
10377 !strncmp(attr
->name
, "time-at-", 8))
10383 send_ipp_status(con
, IPP_ATTRIBUTES_NOT_SETTABLE
,
10384 _("%s cannot be changed."), attr
->name
);
10386 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10387 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10392 if (!strcmp(attr
->name
, "job-priority"))
10395 * Change the job priority...
10398 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10400 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-priority value!"));
10402 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10403 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10405 else if (job
->state_value
>= IPP_JOB_PROCESSING
)
10407 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10408 _("Job is completed and cannot be changed."));
10411 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10413 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-priority to %d",
10414 attr
->values
[0].integer
);
10415 cupsdSetJobPriority(job
, attr
->values
[0].integer
);
10418 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
|
10419 CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
;
10422 else if (!strcmp(attr
->name
, "job-state"))
10425 * Change the job state...
10428 if (attr
->value_tag
!= IPP_TAG_ENUM
)
10430 send_ipp_status(con
, IPP_REQUEST_VALUE
, _("Bad job-state value!"));
10432 if ((attr2
= copy_attribute(con
->response
, attr
, 0)) != NULL
)
10433 attr2
->group_tag
= IPP_TAG_UNSUPPORTED_GROUP
;
10437 switch (attr
->values
[0].integer
)
10439 case IPP_JOB_PENDING
:
10440 case IPP_JOB_HELD
:
10441 if (job
->state_value
> IPP_JOB_HELD
)
10443 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10444 _("Job state cannot be changed."));
10447 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10449 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10450 attr
->values
[0].integer
);
10452 job
->state
->values
[0].integer
= attr
->values
[0].integer
;
10453 job
->state_value
= (ipp_jstate_t
)attr
->values
[0].integer
;
10455 event
|= CUPSD_EVENT_JOB_STATE
;
10460 case IPP_JOB_PROCESSING
:
10461 case IPP_JOB_STOPPED
:
10462 if (job
->state_value
!= attr
->values
[0].integer
)
10464 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10465 _("Job state cannot be changed."));
10470 case IPP_JOB_CANCELED
:
10471 case IPP_JOB_ABORTED
:
10472 case IPP_JOB_COMPLETED
:
10473 if (job
->state_value
> IPP_JOB_PROCESSING
)
10475 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10476 _("Job state cannot be changed."));
10479 else if (con
->response
->request
.status
.status_code
== IPP_OK
)
10481 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-state to %d",
10482 attr
->values
[0].integer
);
10483 cupsdCancelJob(job
, 0, (ipp_jstate_t
)attr
->values
[0].integer
);
10491 else if (con
->response
->request
.status
.status_code
!= IPP_OK
)
10493 else if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10494 IPP_TAG_ZERO
)) != NULL
)
10497 * Some other value; first free the old value...
10500 if (job
->attrs
->prev
)
10501 job
->attrs
->prev
->next
= attr2
->next
;
10503 job
->attrs
->attrs
= attr2
->next
;
10505 if (job
->attrs
->last
== attr2
)
10506 job
->attrs
->last
= job
->attrs
->prev
;
10508 _ippFreeAttr(attr2
);
10511 * Then copy the attribute...
10514 copy_attribute(job
->attrs
, attr
, 0);
10517 * See if the job-name or job-hold-until is being changed.
10520 if (!strcmp(attr
->name
, "job-hold-until"))
10522 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "Setting job-hold-until to %s",
10523 attr
->values
[0].string
.text
);
10524 cupsdSetJobHoldUntil(job
, attr
->values
[0].string
.text
);
10526 if (!strcmp(attr
->values
[0].string
.text
, "no-hold"))
10527 cupsdReleaseJob(job
);
10532 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
| CUPSD_EVENT_JOB_STATE
;
10535 else if (attr
->value_tag
== IPP_TAG_DELETEATTR
)
10538 * Delete the attribute...
10541 if ((attr2
= ippFindAttribute(job
->attrs
, attr
->name
,
10542 IPP_TAG_ZERO
)) != NULL
)
10544 if (job
->attrs
->prev
)
10545 job
->attrs
->prev
->next
= attr2
->next
;
10547 job
->attrs
->attrs
= attr2
->next
;
10549 if (attr2
== job
->attrs
->last
)
10550 job
->attrs
->last
= job
->attrs
->prev
;
10552 _ippFreeAttr(attr2
);
10554 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10560 * Add new option by copying it...
10563 copy_attribute(job
->attrs
, attr
, 0);
10565 event
|= CUPSD_EVENT_JOB_CONFIG_CHANGED
;
10574 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
10577 * Send events as needed...
10580 if (event
& CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
)
10581 cupsdAddEvent(CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
,
10582 cupsdFindDest(job
->dest
), job
,
10583 "Job priority changed by user.");
10585 if (event
& CUPSD_EVENT_JOB_STATE
)
10586 cupsdAddEvent(CUPSD_EVENT_JOB_STATE
, cupsdFindDest(job
->dest
), job
,
10587 job
->state_value
== IPP_JOB_HELD
?
10588 "Job held by user." : "Job restarted by user.");
10590 if (event
& CUPSD_EVENT_JOB_CONFIG_CHANGED
)
10591 cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED
, cupsdFindDest(job
->dest
), job
,
10592 "Job options changed by user.");
10595 * Start jobs if possible...
10604 * 'set_printer_defaults()' - Set printer default options from a request.
10608 set_printer_defaults(
10609 cupsd_client_t
*con
, /* I - Client connection */
10610 cupsd_printer_t
*printer
) /* I - Printer */
10612 int i
; /* Looping var */
10613 ipp_attribute_t
*attr
; /* Current attribute */
10614 int namelen
; /* Length of attribute name */
10615 char name
[256], /* New attribute name */
10616 value
[256]; /* String version of integer attrs */
10619 for (attr
= con
->request
->attrs
; attr
; attr
= attr
->next
)
10622 * Skip non-printer attributes...
10625 if (attr
->group_tag
!= IPP_TAG_PRINTER
|| !attr
->name
)
10628 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "set_printer_defaults: %s", attr
->name
);
10630 if (!strcmp(attr
->name
, "job-sheets-default"))
10633 * Only allow keywords and names...
10636 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10640 * Only allow job-sheets-default to be set when running without a
10641 * system high classification level...
10644 if (Classification
)
10647 cupsdSetString(&printer
->job_sheets
[0], attr
->values
[0].string
.text
);
10649 if (attr
->num_values
> 1)
10650 cupsdSetString(&printer
->job_sheets
[1], attr
->values
[1].string
.text
);
10652 cupsdSetString(&printer
->job_sheets
[1], "none");
10654 else if (!strcmp(attr
->name
, "requesting-user-name-allowed"))
10656 cupsdFreePrinterUsers(printer
);
10658 printer
->deny_users
= 0;
10660 if (attr
->value_tag
== IPP_TAG_NAME
&&
10661 (attr
->num_values
> 1 ||
10662 strcmp(attr
->values
[0].string
.text
, "all")))
10664 for (i
= 0; i
< attr
->num_values
; i
++)
10665 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10668 else if (!strcmp(attr
->name
, "requesting-user-name-denied"))
10670 cupsdFreePrinterUsers(printer
);
10672 printer
->deny_users
= 1;
10674 if (attr
->value_tag
== IPP_TAG_NAME
&&
10675 (attr
->num_values
> 1 ||
10676 strcmp(attr
->values
[0].string
.text
, "none")))
10678 for (i
= 0; i
< attr
->num_values
; i
++)
10679 cupsdAddPrinterUser(printer
, attr
->values
[i
].string
.text
);
10682 else if (!strcmp(attr
->name
, "job-quota-period"))
10684 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10687 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-quota-period to %d...",
10688 attr
->values
[0].integer
);
10689 cupsdFreeQuotas(printer
);
10691 printer
->quota_period
= attr
->values
[0].integer
;
10693 else if (!strcmp(attr
->name
, "job-k-limit"))
10695 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10698 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-k-limit to %d...",
10699 attr
->values
[0].integer
);
10700 cupsdFreeQuotas(printer
);
10702 printer
->k_limit
= attr
->values
[0].integer
;
10704 else if (!strcmp(attr
->name
, "job-page-limit"))
10706 if (attr
->value_tag
!= IPP_TAG_INTEGER
)
10709 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Setting job-page-limit to %d...",
10710 attr
->values
[0].integer
);
10711 cupsdFreeQuotas(printer
);
10713 printer
->page_limit
= attr
->values
[0].integer
;
10715 else if (!strcmp(attr
->name
, "printer-op-policy"))
10717 cupsd_policy_t
*p
; /* Policy */
10720 if (attr
->value_tag
!= IPP_TAG_NAME
)
10723 if ((p
= cupsdFindPolicy(attr
->values
[0].string
.text
)) != NULL
)
10725 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10726 "Setting printer-op-policy to \"%s\"...",
10727 attr
->values
[0].string
.text
);
10728 cupsdSetString(&printer
->op_policy
, attr
->values
[0].string
.text
);
10729 printer
->op_policy_ptr
= p
;
10733 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10734 _("Unknown printer-op-policy \"%s\"."),
10735 attr
->values
[0].string
.text
);
10739 else if (!strcmp(attr
->name
, "printer-error-policy"))
10741 if (attr
->value_tag
!= IPP_TAG_NAME
&& attr
->value_tag
!= IPP_TAG_KEYWORD
)
10744 if (strcmp(attr
->values
[0].string
.text
, "abort-job") &&
10745 strcmp(attr
->values
[0].string
.text
, "retry-current-job") &&
10746 strcmp(attr
->values
[0].string
.text
, "retry-job") &&
10747 strcmp(attr
->values
[0].string
.text
, "stop-printer"))
10749 send_ipp_status(con
, IPP_NOT_POSSIBLE
,
10750 _("Unknown printer-error-policy \"%s\"."),
10751 attr
->values
[0].string
.text
);
10755 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10756 "Setting printer-error-policy to \"%s\"...",
10757 attr
->values
[0].string
.text
);
10758 cupsdSetString(&printer
->error_policy
, attr
->values
[0].string
.text
);
10762 * Skip any other non-default attributes...
10765 namelen
= strlen(attr
->name
);
10766 if (namelen
< 9 || strcmp(attr
->name
+ namelen
- 8, "-default") ||
10767 namelen
> (sizeof(name
) - 1) || attr
->num_values
!= 1)
10771 * OK, anything else must be a user-defined default...
10774 strlcpy(name
, attr
->name
, sizeof(name
));
10775 name
[namelen
- 8] = '\0'; /* Strip "-default" */
10777 switch (attr
->value_tag
)
10779 case IPP_TAG_DELETEATTR
:
10780 printer
->num_options
= cupsRemoveOption(name
,
10781 printer
->num_options
,
10782 &(printer
->options
));
10783 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10784 "Deleting %s", attr
->name
);
10787 case IPP_TAG_NAME
:
10788 case IPP_TAG_KEYWORD
:
10790 printer
->num_options
= cupsAddOption(name
,
10791 attr
->values
[0].string
.text
,
10792 printer
->num_options
,
10793 &(printer
->options
));
10794 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10795 "Setting %s to \"%s\"...", attr
->name
,
10796 attr
->values
[0].string
.text
);
10799 case IPP_TAG_BOOLEAN
:
10800 printer
->num_options
= cupsAddOption(name
,
10801 attr
->values
[0].boolean
?
10803 printer
->num_options
,
10804 &(printer
->options
));
10805 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10806 "Setting %s to %s...", attr
->name
,
10807 attr
->values
[0].boolean
? "true" : "false");
10810 case IPP_TAG_INTEGER
:
10811 case IPP_TAG_ENUM
:
10812 sprintf(value
, "%d", attr
->values
[0].integer
);
10813 printer
->num_options
= cupsAddOption(name
, value
,
10814 printer
->num_options
,
10815 &(printer
->options
));
10816 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10817 "Setting %s to %s...", attr
->name
, value
);
10820 case IPP_TAG_RANGE
:
10821 sprintf(value
, "%d-%d", attr
->values
[0].range
.lower
,
10822 attr
->values
[0].range
.upper
);
10823 printer
->num_options
= cupsAddOption(name
, value
,
10824 printer
->num_options
,
10825 &(printer
->options
));
10826 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10827 "Setting %s to %s...", attr
->name
, value
);
10830 case IPP_TAG_RESOLUTION
:
10831 sprintf(value
, "%dx%d%s", attr
->values
[0].resolution
.xres
,
10832 attr
->values
[0].resolution
.yres
,
10833 attr
->values
[0].resolution
.units
== IPP_RES_PER_INCH
?
10835 printer
->num_options
= cupsAddOption(name
, value
,
10836 printer
->num_options
,
10837 &(printer
->options
));
10838 cupsdLogMessage(CUPSD_LOG_DEBUG
,
10839 "Setting %s to %s...", attr
->name
, value
);
10843 /* Do nothing for other values */
10851 * 'start_printer()' - Start a printer.
10855 start_printer(cupsd_client_t
*con
, /* I - Client connection */
10856 ipp_attribute_t
*uri
) /* I - Printer URI */
10858 http_status_t status
; /* Policy status */
10859 cups_ptype_t dtype
; /* Destination type (printer/class) */
10860 cupsd_printer_t
*printer
; /* Printer data */
10863 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "start_printer(%p[%d], %s)", con
,
10864 con
->http
.fd
, uri
->values
[0].string
.text
);
10867 * Is the destination valid?
10870 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10876 send_ipp_status(con
, IPP_NOT_FOUND
,
10877 _("The printer or class was not found."));
10885 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10887 send_http_error(con
, status
, printer
);
10892 * Start the printer...
10895 printer
->state_message
[0] = '\0';
10897 cupsdStartPrinter(printer
, 1);
10899 if (dtype
& CUPS_PRINTER_CLASS
)
10900 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" started by \"%s\".",
10901 printer
->name
, get_username(con
));
10903 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" started by \"%s\".",
10904 printer
->name
, get_username(con
));
10909 * Everything was ok, so return OK status...
10912 con
->response
->request
.status
.status_code
= IPP_OK
;
10917 * 'stop_printer()' - Stop a printer.
10921 stop_printer(cupsd_client_t
*con
, /* I - Client connection */
10922 ipp_attribute_t
*uri
) /* I - Printer URI */
10924 http_status_t status
; /* Policy status */
10925 cups_ptype_t dtype
; /* Destination type (printer/class) */
10926 cupsd_printer_t
*printer
; /* Printer data */
10927 ipp_attribute_t
*attr
; /* printer-state-message attribute */
10930 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "stop_printer(%p[%d], %s)", con
,
10931 con
->http
.fd
, uri
->values
[0].string
.text
);
10934 * Is the destination valid?
10937 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
10943 send_ipp_status(con
, IPP_NOT_FOUND
,
10944 _("The printer or class was not found."));
10952 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
10954 send_http_error(con
, status
, printer
);
10959 * Stop the printer...
10962 if ((attr
= ippFindAttribute(con
->request
, "printer-state-message",
10963 IPP_TAG_TEXT
)) == NULL
)
10964 strcpy(printer
->state_message
, "Paused");
10967 strlcpy(printer
->state_message
, attr
->values
[0].string
.text
,
10968 sizeof(printer
->state_message
));
10971 cupsdStopPrinter(printer
, 1);
10973 if (dtype
& CUPS_PRINTER_CLASS
)
10974 cupsdLogMessage(CUPSD_LOG_INFO
, "Class \"%s\" stopped by \"%s\".",
10975 printer
->name
, get_username(con
));
10977 cupsdLogMessage(CUPSD_LOG_INFO
, "Printer \"%s\" stopped by \"%s\".",
10978 printer
->name
, get_username(con
));
10981 * Everything was ok, so return OK status...
10984 con
->response
->request
.status
.status_code
= IPP_OK
;
10989 * 'url_encode_attr()' - URL-encode a string attribute.
10993 url_encode_attr(ipp_attribute_t
*attr
, /* I - Attribute */
10994 char *buffer
,/* I - String buffer */
10995 int bufsize
)/* I - Size of buffer */
10997 int i
; /* Looping var */
10998 char *bufptr
, /* Pointer into buffer */
10999 *bufend
; /* End of buffer */
11002 strlcpy(buffer
, attr
->name
, bufsize
);
11003 bufptr
= buffer
+ strlen(buffer
);
11004 bufend
= buffer
+ bufsize
- 1;
11006 for (i
= 0; i
< attr
->num_values
; i
++)
11008 if (bufptr
>= bufend
)
11016 if (bufptr
>= bufend
)
11021 bufptr
= url_encode_string(attr
->values
[i
].string
.text
,
11022 bufptr
, bufend
- bufptr
+ 1);
11024 if (bufptr
>= bufend
)
11035 * 'url_encode_string()' - URL-encode a string.
11038 static char * /* O - End of string */
11039 url_encode_string(const char *s
, /* I - String */
11040 char *buffer
, /* I - String buffer */
11041 int bufsize
) /* I - Size of buffer */
11043 char *bufptr
, /* Pointer into buffer */
11044 *bufend
; /* End of buffer */
11045 static const char *hex
= "0123456789ABCDEF";
11050 bufend
= buffer
+ bufsize
- 1;
11052 while (*s
&& bufptr
< bufend
)
11054 if (*s
== ' ' || *s
== '%')
11056 if (bufptr
>= (bufend
- 2))
11060 *bufptr
++ = hex
[(*s
>> 4) & 15];
11061 *bufptr
++ = hex
[*s
& 15];
11065 else if (*s
== '\'' || *s
== '\\')
11067 if (bufptr
>= (bufend
- 1))
11084 * 'user_allowed()' - See if a user is allowed to print to a queue.
11087 static int /* O - 0 if not allowed, 1 if allowed */
11088 user_allowed(cupsd_printer_t
*p
, /* I - Printer or class */
11089 const char *username
) /* I - Username */
11091 int i
; /* Looping var */
11092 struct passwd
*pw
; /* User password data */
11093 char baseuser
[256], /* Base username */
11094 *baseptr
; /* Pointer to "@" in base username */
11097 if (p
->num_users
== 0)
11100 if (!strcmp(username
, "root"))
11103 if (strchr(username
, '@'))
11106 * Strip @REALM for username check...
11109 strlcpy(baseuser
, username
, sizeof(baseuser
));
11111 if ((baseptr
= strchr(baseuser
, '@')) != NULL
)
11114 username
= baseuser
;
11117 pw
= getpwnam(username
);
11120 for (i
= 0; i
< p
->num_users
; i
++)
11122 if (p
->users
[i
][0] == '@')
11125 * Check group membership...
11128 if (cupsdCheckGroup(username
, pw
, p
->users
[i
] + 1))
11131 else if (p
->users
[i
][0] == '#')
11137 if (cupsdCheckGroup(username
, pw
, p
->users
[i
]))
11140 else if (!strcasecmp(username
, p
->users
[i
]))
11144 return ((i
< p
->num_users
) != p
->deny_users
);
11149 * 'validate_job()' - Validate printer options and destination.
11153 validate_job(cupsd_client_t
*con
, /* I - Client connection */
11154 ipp_attribute_t
*uri
) /* I - Printer URI */
11156 http_status_t status
; /* Policy status */
11157 ipp_attribute_t
*attr
; /* Current attribute */
11158 ipp_attribute_t
*format
; /* Document-format attribute */
11159 cups_ptype_t dtype
; /* Destination type (printer/class) */
11160 char super
[MIME_MAX_SUPER
],
11161 /* Supertype of file */
11162 type
[MIME_MAX_TYPE
];
11163 /* Subtype of file */
11164 cupsd_printer_t
*printer
; /* Printer */
11167 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "validate_job(%p[%d], %s)", con
,
11168 con
->http
.fd
, uri
->values
[0].string
.text
);
11171 * OK, see if the client is sending the document compressed - CUPS
11172 * doesn't support compression yet...
11175 if ((attr
= ippFindAttribute(con
->request
, "compression",
11176 IPP_TAG_KEYWORD
)) != NULL
&&
11177 !strcmp(attr
->values
[0].string
.text
, "none"))
11179 send_ipp_status(con
, IPP_ATTRIBUTES
,
11180 _("Unsupported compression attribute %s!"),
11181 attr
->values
[0].string
.text
);
11182 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
11183 "compression", NULL
, attr
->values
[0].string
.text
);
11188 * Is it a format we support?
11191 if ((format
= ippFindAttribute(con
->request
, "document-format",
11192 IPP_TAG_MIMETYPE
)) != NULL
)
11194 if (sscanf(format
->values
[0].string
.text
, "%15[^/]/%31[^;]",
11197 send_ipp_status(con
, IPP_BAD_REQUEST
, _("Bad document-format \"%s\"!"),
11198 format
->values
[0].string
.text
);
11202 if ((strcmp(super
, "application") || strcmp(type
, "octet-stream")) &&
11203 !mimeType(MimeDatabase
, super
, type
))
11205 cupsdLogMessage(CUPSD_LOG_INFO
,
11206 "Hint: Do you have the raw file printing rules enabled?");
11207 send_ipp_status(con
, IPP_DOCUMENT_FORMAT
,
11208 _("Unsupported format \"%s\"!"),
11209 format
->values
[0].string
.text
);
11210 ippAddString(con
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_MIMETYPE
,
11211 "document-format", NULL
, format
->values
[0].string
.text
);
11217 * Is the destination valid?
11220 if (!cupsdValidateDest(uri
->values
[0].string
.text
, &dtype
, &printer
))
11226 send_ipp_status(con
, IPP_NOT_FOUND
,
11227 _("The printer or class was not found."));
11235 if ((status
= cupsdCheckPolicy(printer
->op_policy_ptr
, con
, NULL
)) != HTTP_OK
)
11237 send_http_error(con
, status
, printer
);
11242 * Everything was ok, so return OK status...
11245 con
->response
->request
.status
.status_code
= IPP_OK
;
11250 * 'validate_name()' - Make sure the printer name only contains valid chars.
11253 static int /* O - 0 if name is no good, 1 if good */
11254 validate_name(const char *name
) /* I - Name to check */
11256 const char *ptr
; /* Pointer into name */
11260 * Scan the whole name...
11263 for (ptr
= name
; *ptr
; ptr
++)
11264 if ((*ptr
> 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' || *ptr
== '#')
11268 * All the characters are good; validate the length, too...
11271 return ((ptr
- name
) < 128);
11276 * 'validate_user()' - Validate the user for the request.
11279 static int /* O - 1 if permitted, 0 otherwise */
11280 validate_user(cupsd_job_t
*job
, /* I - Job */
11281 cupsd_client_t
*con
, /* I - Client connection */
11282 const char *owner
, /* I - Owner of job/resource */
11283 char *username
, /* O - Authenticated username */
11284 int userlen
) /* I - Length of username */
11286 cupsd_printer_t
*printer
; /* Printer for job */
11289 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
11290 "validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
11292 job
->id
, con
? con
->http
.fd
: 0,
11293 owner
? owner
: "(null)", username
, userlen
);
11296 * Validate input...
11299 if (!con
|| !owner
|| !username
|| userlen
<= 0)
11303 * Get the best authenticated username that is available.
11306 strlcpy(username
, get_username(con
), userlen
);
11309 * Check the username against the owner...
11312 printer
= cupsdFindDest(job
->dest
);
11314 return (cupsdCheckPolicy(printer
? printer
->op_policy_ptr
: DefaultPolicyPtr
,
11315 con
, owner
) == HTTP_OK
);
11320 * End of "$Id: ipp.c 7944 2008-09-16 22:32:42Z mike $".